Compare commits

...

4 Commits

5 changed files with 137 additions and 28 deletions

View File

@ -70,6 +70,17 @@
} }
} }
} }
},
{
"name": "get_people",
"schemas": {
"get_people.response": {
"type": "array",
"items": {
"type": "person"
}
}
}
} }
], ],
"enums": [], "enums": [],
@ -1413,7 +1424,7 @@
" NOT entity_7.archived", " NOT entity_7.archived",
" AND relationship_5.target_id = entity_7.id", " AND relationship_5.target_id = entity_7.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_4.created_at, entity_4.id), '[]'::jsonb)",
" FROM agreego.entity entity_4", " FROM agreego.entity entity_4",
" JOIN agreego.relationship relationship_5", " JOIN agreego.relationship relationship_5",
" JOIN agreego.contact contact_6", " JOIN agreego.contact contact_6",
@ -1443,7 +1454,7 @@
" NOT entity_12.archived", " NOT entity_12.archived",
" AND relationship_10.target_id = entity_12.id", " AND relationship_10.target_id = entity_12.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_9.created_at, entity_9.id), '[]'::jsonb)",
" FROM agreego.entity entity_9", " FROM agreego.entity entity_9",
" JOIN agreego.relationship relationship_10", " JOIN agreego.relationship relationship_10",
" JOIN agreego.contact contact_11", " JOIN agreego.contact contact_11",
@ -1473,7 +1484,7 @@
" NOT entity_17.archived", " NOT entity_17.archived",
" AND relationship_15.target_id = entity_17.id", " AND relationship_15.target_id = entity_17.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_14.created_at, entity_14.id), '[]'::jsonb)",
" FROM agreego.entity entity_14", " FROM agreego.entity entity_14",
" JOIN agreego.relationship relationship_15", " JOIN agreego.relationship relationship_15",
" JOIN agreego.contact contact_16", " JOIN agreego.contact contact_16",
@ -1534,7 +1545,7 @@
" ))", " ))",
" ELSE NULL", " ELSE NULL",
" END", " END",
" )), '[]'::jsonb)", " ) ORDER BY entity_19.created_at, entity_19.id), '[]'::jsonb)",
" FROM agreego.entity entity_19", " FROM agreego.entity entity_19",
" JOIN agreego.relationship relationship_20", " JOIN agreego.relationship relationship_20",
" JOIN agreego.contact contact_21", " JOIN agreego.contact contact_21",
@ -1667,7 +1678,7 @@
" AND phone_number_8.number ILIKE $32 #>> '{}'", " AND phone_number_8.number ILIKE $32 #>> '{}'",
" AND relationship_5.target_id = entity_7.id", " AND relationship_5.target_id = entity_7.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_4.created_at, entity_4.id), '[]'::jsonb)",
" FROM agreego.entity entity_4", " FROM agreego.entity entity_4",
" JOIN agreego.relationship relationship_5", " JOIN agreego.relationship relationship_5",
" JOIN agreego.contact contact_6", " JOIN agreego.contact contact_6",
@ -1697,7 +1708,7 @@
" NOT entity_12.archived", " NOT entity_12.archived",
" AND relationship_10.target_id = entity_12.id", " AND relationship_10.target_id = entity_12.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_9.created_at, entity_9.id), '[]'::jsonb)",
" FROM agreego.entity entity_9", " FROM agreego.entity entity_9",
" JOIN agreego.relationship relationship_10", " JOIN agreego.relationship relationship_10",
" JOIN agreego.contact contact_11", " JOIN agreego.contact contact_11",
@ -1727,7 +1738,7 @@
" NOT entity_17.archived", " NOT entity_17.archived",
" AND relationship_15.target_id = entity_17.id", " AND relationship_15.target_id = entity_17.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_14.created_at, entity_14.id), '[]'::jsonb)",
" FROM agreego.entity entity_14", " FROM agreego.entity entity_14",
" JOIN agreego.relationship relationship_15", " JOIN agreego.relationship relationship_15",
" JOIN agreego.contact contact_16", " JOIN agreego.contact contact_16",
@ -1788,7 +1799,7 @@
" ))", " ))",
" ELSE NULL", " ELSE NULL",
" END", " END",
" )), '[]'::jsonb)", " ) ORDER BY entity_19.created_at, entity_19.id), '[]'::jsonb)",
" FROM agreego.entity entity_19", " FROM agreego.entity entity_19",
" JOIN agreego.relationship relationship_20", " JOIN agreego.relationship relationship_20",
" JOIN agreego.contact contact_21", " JOIN agreego.contact contact_21",
@ -1867,7 +1878,7 @@
" NOT entity_4.archived", " NOT entity_4.archived",
" AND relationship_2.target_id = entity_4.id", " AND relationship_2.target_id = entity_4.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_1.created_at, entity_1.id), '[]'::jsonb)",
" FROM agreego.entity entity_1", " FROM agreego.entity entity_1",
" JOIN agreego.relationship relationship_2", " JOIN agreego.relationship relationship_2",
" JOIN agreego.contact contact_3", " JOIN agreego.contact contact_3",
@ -1925,7 +1936,7 @@
" 'order_id', order_line_7.order_id,", " 'order_id', order_line_7.order_id,",
" 'product', order_line_7.product,", " 'product', order_line_7.product,",
" 'price', order_line_7.price", " 'price', order_line_7.price",
" )), '[]'::jsonb)", " ) ORDER BY entity_6.created_at, entity_6.id), '[]'::jsonb)",
" FROM agreego.entity entity_6", " FROM agreego.entity entity_6",
" JOIN agreego.order_line order_line_7", " JOIN agreego.order_line order_line_7",
" WHERE", " WHERE",
@ -2030,7 +2041,7 @@
" AND entity_8.id = entity_1.id", " AND entity_8.id = entity_1.id",
" ))", " ))",
" ELSE NULL", " ELSE NULL",
" END), '[]'::jsonb)", " END ORDER BY entity_1.created_at, entity_1.id), '[]'::jsonb)",
" FROM agreego.entity entity_1", " FROM agreego.entity entity_1",
" JOIN agreego.organization organization_2", " JOIN agreego.organization organization_2",
" WHERE", " WHERE",
@ -2141,7 +2152,7 @@
" NOT entity_9.archived", " NOT entity_9.archived",
" AND relationship_7.target_id = entity_9.id", " AND relationship_7.target_id = entity_9.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_6.created_at, entity_6.id), '[]'::jsonb)",
" FROM agreego.entity entity_6", " FROM agreego.entity entity_6",
" JOIN agreego.relationship relationship_7", " JOIN agreego.relationship relationship_7",
" JOIN agreego.contact contact_8", " JOIN agreego.contact contact_8",
@ -2171,7 +2182,7 @@
" NOT entity_14.archived", " NOT entity_14.archived",
" AND relationship_12.target_id = entity_14.id", " AND relationship_12.target_id = entity_14.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_11.created_at, entity_11.id), '[]'::jsonb)",
" FROM agreego.entity entity_11", " FROM agreego.entity entity_11",
" JOIN agreego.relationship relationship_12", " JOIN agreego.relationship relationship_12",
" JOIN agreego.contact contact_13", " JOIN agreego.contact contact_13",
@ -2201,7 +2212,7 @@
" NOT entity_19.archived", " NOT entity_19.archived",
" AND relationship_17.target_id = entity_19.id", " AND relationship_17.target_id = entity_19.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_16.created_at, entity_16.id), '[]'::jsonb)",
" FROM agreego.entity entity_16", " FROM agreego.entity entity_16",
" JOIN agreego.relationship relationship_17", " JOIN agreego.relationship relationship_17",
" JOIN agreego.contact contact_18", " JOIN agreego.contact contact_18",
@ -2262,7 +2273,7 @@
" ))", " ))",
" ELSE NULL", " ELSE NULL",
" END", " END",
" )), '[]'::jsonb)", " ) ORDER BY entity_21.created_at, entity_21.id), '[]'::jsonb)",
" FROM agreego.entity entity_21", " FROM agreego.entity entity_21",
" JOIN agreego.relationship relationship_22", " JOIN agreego.relationship relationship_22",
" JOIN agreego.contact contact_23", " JOIN agreego.contact contact_23",
@ -2326,7 +2337,7 @@
" NOT entity_3.archived", " NOT entity_3.archived",
" AND order_2.customer_id = person_5.id", " AND order_2.customer_id = person_5.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_1.created_at, entity_1.id), '[]'::jsonb)",
" FROM agreego.entity entity_1", " FROM agreego.entity entity_1",
" JOIN agreego.order order_2", " JOIN agreego.order order_2",
" WHERE", " WHERE",
@ -2361,7 +2372,7 @@
" 'kind', widget_2.kind", " 'kind', widget_2.kind",
" ))", " ))",
" ELSE NULL", " ELSE NULL",
" END), '[]'::jsonb)", " END ORDER BY entity_1.created_at, entity_1.id), '[]'::jsonb)",
" FROM agreego.entity entity_1", " FROM agreego.entity entity_1",
" JOIN agreego.widget widget_2", " JOIN agreego.widget widget_2",
" WHERE", " WHERE",
@ -2394,7 +2405,7 @@
" 'created_at', entity_3.created_at,", " 'created_at', entity_3.created_at,",
" 'invoice_id', invoice_line_4.invoice_id,", " 'invoice_id', invoice_line_4.invoice_id,",
" 'price', invoice_line_4.price", " 'price', invoice_line_4.price",
" )), '[]'::jsonb)", " ) ORDER BY entity_3.created_at, entity_3.id), '[]'::jsonb)",
" FROM agreego.entity entity_3", " FROM agreego.entity entity_3",
" JOIN agreego.invoice_line invoice_line_4", " JOIN agreego.invoice_line invoice_line_4",
" WHERE", " WHERE",
@ -2520,7 +2531,7 @@
" NOT entity_3.archived", " NOT entity_3.archived",
" AND order_2.counterparty_id = entity_3.id", " AND order_2.counterparty_id = entity_3.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_1.created_at, entity_1.id), '[]'::jsonb)",
" FROM agreego.entity entity_1", " FROM agreego.entity entity_1",
" JOIN agreego.order order_2", " JOIN agreego.order order_2",
" WHERE", " WHERE",
@ -2588,7 +2599,7 @@
" NOT entity_7.archived", " NOT entity_7.archived",
" AND relationship_5.target_id = entity_7.id", " AND relationship_5.target_id = entity_7.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_4.created_at, entity_4.id), '[]'::jsonb)",
" FROM agreego.entity entity_4", " FROM agreego.entity entity_4",
" JOIN agreego.relationship relationship_5", " JOIN agreego.relationship relationship_5",
" JOIN agreego.contact contact_6", " JOIN agreego.contact contact_6",
@ -2618,7 +2629,7 @@
" NOT entity_12.archived", " NOT entity_12.archived",
" AND relationship_10.target_id = entity_12.id", " AND relationship_10.target_id = entity_12.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_9.created_at, entity_9.id), '[]'::jsonb)",
" FROM agreego.entity entity_9", " FROM agreego.entity entity_9",
" JOIN agreego.relationship relationship_10", " JOIN agreego.relationship relationship_10",
" JOIN agreego.contact contact_11", " JOIN agreego.contact contact_11",
@ -2648,7 +2659,7 @@
" NOT entity_17.archived", " NOT entity_17.archived",
" AND relationship_15.target_id = entity_17.id", " AND relationship_15.target_id = entity_17.id",
" )", " )",
" )), '[]'::jsonb)", " ) ORDER BY entity_14.created_at, entity_14.id), '[]'::jsonb)",
" FROM agreego.entity entity_14", " FROM agreego.entity entity_14",
" JOIN agreego.relationship relationship_15", " JOIN agreego.relationship relationship_15",
" JOIN agreego.contact contact_16", " JOIN agreego.contact contact_16",
@ -2709,7 +2720,7 @@
" ))", " ))",
" ELSE NULL", " ELSE NULL",
" END", " END",
" )), '[]'::jsonb)", " ) ORDER BY entity_19.created_at, entity_19.id), '[]'::jsonb)",
" FROM agreego.entity entity_19", " FROM agreego.entity entity_19",
" JOIN agreego.relationship relationship_20", " JOIN agreego.relationship relationship_20",
" JOIN agreego.contact contact_21", " JOIN agreego.contact contact_21",
@ -2731,6 +2742,55 @@
] ]
] ]
} }
},
{
"description": "Punc response items proxy: jsonb array containment filter falls back to the type schema",
"action": "query",
"schema_id": "get_people.response",
"filter": {
"tags": {
"$eq": "landscaping"
},
"labels": {
"$of": [
"vip",
"board"
]
},
"rating": {
"$gte": 4.0
}
},
"expect": {
"success": true,
"sql": [
[
"((SELECT jsonb_strip_nulls((",
" SELECT COALESCE(jsonb_agg(jsonb_build_object(",
" 'id', entity_1.id,",
" 'type', entity_1.type,",
" 'archived', entity_1.archived,",
" 'created_at', entity_1.created_at,",
" 'name', organization_2.name,",
" 'first_name', person_3.first_name,",
" 'last_name', person_3.last_name,",
" 'age', person_3.age,",
" 'rating', person_3.rating,",
" 'tags', person_3.tags,",
" 'labels', person_3.labels",
" ) ORDER BY entity_1.created_at, entity_1.id), '[]'::jsonb)",
" FROM agreego.entity entity_1",
" JOIN agreego.organization organization_2",
" JOIN agreego.person person_3",
" WHERE",
" NOT entity_1.archived",
" AND person_3.labels ?| ARRAY()",
" AND person_3.rating >= ($2 #>> '{}')::NUMERIC",
" AND person_3.tags ? ($3 #>> '{)",
" ))))"
]
]
}
} }
] ]
} }

View File

@ -234,7 +234,17 @@ impl<'a> Compiler<'a> {
let where_clauses = self.compile_where_clause(r#type, &table_aliases, node)?; let where_clauses = self.compile_where_clause(r#type, &table_aliases, node)?;
let selection = if is_array { let selection = if is_array {
format!("COALESCE(jsonb_agg({}), '[]'::jsonb)", jsonb_obj_sql) // Deterministic order: aggregation over an unordered heap made result
// order vary run-to-run (flaky reads, unstable grids). Creation order
// with id as the tiebreaker; the entity alias exists for every
// entity-family type.
match table_aliases.get("entity") {
Some(entity_alias) => format!(
"COALESCE(jsonb_agg({} ORDER BY {}.created_at, {}.id), '[]'::jsonb)",
jsonb_obj_sql, entity_alias, entity_alias
),
None => format!("COALESCE(jsonb_agg({}), '[]'::jsonb)", jsonb_obj_sql),
}
} else { } else {
jsonb_obj_sql jsonb_obj_sql
}; };
@ -762,14 +772,26 @@ impl<'a> Compiler<'a> {
.and_then(|v| v.as_str()) .and_then(|v| v.as_str())
== Some("jsonb"); == Some("jsonb");
if is_jsonb { if is_jsonb {
// Compiled schemas normalize to the list form ("type": ["array"]), // The node schema may be a punc-response proxy without properties —
// hand-written ones may use the single form — accept both // fall back to the type's own schema. Compiled schemas normalize to
let is_array_prop = node // the list form ("type": ["array"]), hand-written ones may use the
// single form — accept both.
let prop_schema = node
.schema .schema
.obj .obj
.properties .properties
.as_ref() .as_ref()
.and_then(|p| p.get(field_name)) .and_then(|p| p.get(field_name))
.cloned()
.or_else(|| {
r#type
.schemas
.get(&r#type.name)
.and_then(|s| s.obj.properties.as_ref())
.and_then(|p| p.get(field_name))
.cloned()
});
let is_array_prop = prop_schema
.map(|ps| match &ps.obj.type_ { .map(|ps| match &ps.obj.type_ {
Some(crate::database::object::SchemaTypeOrArray::Single(t)) => t == "array", Some(crate::database::object::SchemaTypeOrArray::Single(t)) => t == "array",
Some(crate::database::object::SchemaTypeOrArray::Multiple(ts)) => { Some(crate::database::object::SchemaTypeOrArray::Multiple(ts)) => {

View File

@ -1307,6 +1307,12 @@ fn test_queryer_0_16() {
crate::tests::runner::run_test_case(&path, 0, 16).unwrap(); crate::tests::runner::run_test_case(&path, 0, 16).unwrap();
} }
#[test]
fn test_queryer_0_17() {
let path = format!("{}/fixtures/queryer.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 17).unwrap();
}
#[test] #[test]
fn test_polymorphism_0_0() { fn test_polymorphism_0_0() {
let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR")); let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR"));

View File

@ -389,6 +389,7 @@ impl SqlFormatter {
i += 2; i += 2;
} }
self.indent -= 2; self.indent -= 2;
self.format_function_clauses(list);
self.push_line(")"); self.push_line(")");
} else { } else {
for (i, arg) in list.args.iter().enumerate() { for (i, arg) in list.args.iter().enumerate() {
@ -396,6 +397,7 @@ impl SqlFormatter {
self.format_function_arg(arg); self.format_function_arg(arg);
self.push_str(comma); self.push_str(comma);
} }
self.format_function_clauses(list);
self.push_str(")"); self.push_str(")");
} }
} else { } else {
@ -403,6 +405,25 @@ impl SqlFormatter {
} }
} }
// Aggregate clauses (e.g. jsonb_agg(x ORDER BY y)) — without this the
// snapshot silently drops the ORDER BY the compiler emits.
fn format_function_clauses(&mut self, list: &sqlparser::ast::FunctionArgumentList) {
for clause in &list.clauses {
if let sqlparser::ast::FunctionArgumentClause::OrderBy(order) = clause {
self.push_str(" ORDER BY ");
for (i, ob) in order.iter().enumerate() {
if i > 0 {
self.push_str(", ");
}
self.format_expr(&ob.expr);
if let Some(asc) = ob.options.asc {
self.push_str(if asc { " ASC" } else { " DESC" });
}
}
}
}
}
fn format_function_arg(&mut self, arg: &FunctionArg) { fn format_function_arg(&mut self, arg: &FunctionArg) {
match arg { match arg {
FunctionArg::Unnamed(sqlparser::ast::FunctionArgExpr::Expr(expr)) => self.format_expr(expr), FunctionArg::Unnamed(sqlparser::ast::FunctionArgExpr::Expr(expr)) => self.format_expr(expr),

View File

@ -1 +1 @@
1.0.174 1.0.176