queryer test checkpoit

This commit is contained in:
2026-03-17 18:00:36 -04:00
parent e1314496dd
commit 3d66a7fc3c
6 changed files with 461 additions and 294 deletions

View File

@ -47,7 +47,19 @@ impl SqlCompiler {
// We expect the top level to typically be an Object or Array
let is_stem_query = stem_path.is_some();
let (sql, _) = self.walk_schema(target_schema, "t1", None, None, filter_keys, is_stem_query, 0, String::new())?;
let mut alias_counter: usize = 0;
let (sql, _) = self.walk_schema(
target_schema,
"t1",
None,
None,
None,
filter_keys,
is_stem_query,
0,
String::new(),
&mut alias_counter,
)?;
Ok(sql)
}
@ -57,12 +69,14 @@ impl SqlCompiler {
&self,
schema: &crate::database::schema::Schema,
parent_alias: &str,
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name_context: Option<&str>,
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
alias_counter: &mut usize,
) -> Result<(String, String), String> {
// Determine the base schema type (could be an array, object, or literal)
match &schema.obj.type_ {
@ -81,6 +95,7 @@ impl SqlCompiler {
items,
type_def,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
true,
@ -88,18 +103,21 @@ impl SqlCompiler {
is_stem_query,
depth,
next_path,
alias_counter,
);
}
}
let (item_sql, _) = self.walk_schema(
items,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
depth + 1,
next_path,
alias_counter,
)?;
return Ok((
format!("(SELECT jsonb_agg({}) FROM TODO)", item_sql),
@ -128,6 +146,7 @@ impl SqlCompiler {
schema,
type_def,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
false,
@ -135,6 +154,7 @@ impl SqlCompiler {
is_stem_query,
depth,
current_path,
alias_counter,
);
}
@ -145,12 +165,14 @@ impl SqlCompiler {
return self.walk_schema(
target_schema,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path,
alias_counter,
);
}
return Err(format!("Unresolved $ref: {}", ref_id));
@ -174,12 +196,14 @@ impl SqlCompiler {
return self.compile_one_of(
&family_schemas,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path,
alias_counter,
);
}
@ -188,12 +212,14 @@ impl SqlCompiler {
return self.compile_one_of(
one_of,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path,
alias_counter,
);
}
@ -202,11 +228,13 @@ impl SqlCompiler {
return self.compile_inline_object(
props,
parent_alias,
parent_table_aliases,
parent_type_def,
filter_keys,
is_stem_query,
depth,
current_path,
alias_counter,
);
}
@ -249,6 +277,7 @@ impl SqlCompiler {
schema: &crate::database::schema::Schema,
type_def: &crate::database::r#type::Type,
parent_alias: &str,
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name: Option<&str>,
is_array: bool,
@ -256,11 +285,10 @@ impl SqlCompiler {
is_stem_query: bool,
depth: usize,
current_path: String,
alias_counter: &mut usize,
) -> Result<(String, String), String> {
let local_ctx = format!("{}_{}", parent_alias, prop_name.unwrap_or("obj"));
// 1. Build FROM clauses and table aliases
let (table_aliases, from_clauses) = self.build_hierarchy_from_clauses(type_def, &local_ctx);
let (table_aliases, from_clauses) = self.build_hierarchy_from_clauses(type_def, alias_counter);
// 2. Map properties and build jsonb_build_object args
let mut select_args = self.map_properties_to_aliases(
@ -272,6 +300,7 @@ impl SqlCompiler {
is_stem_query,
depth,
&current_path,
alias_counter,
)?;
// 2.5 Inject polymorphism directly into the query object
@ -281,10 +310,10 @@ impl SqlCompiler {
let mut sorted_targets: Vec<String> = base_type.variations.iter().cloned().collect();
// Ensure the base type is included if not listed in variations by default
if !sorted_targets.contains(family_target) {
sorted_targets.push(family_target.clone());
sorted_targets.push(family_target.clone());
}
sorted_targets.sort();
for target in sorted_targets {
let mut ref_schema = crate::database::schema::Schema::default();
ref_schema.obj.r#ref = Some(target);
@ -297,14 +326,42 @@ impl SqlCompiler {
family_schemas.push(std::sync::Arc::new(ref_schema));
}
let base_alias = table_aliases.get(&type_def.name).cloned().unwrap_or_else(|| parent_alias.to_string());
let base_alias = table_aliases
.get(&type_def.name)
.cloned()
.unwrap_or_else(|| parent_alias.to_string());
select_args.push(format!("'id', {}.id", base_alias));
let (case_sql, _) = self.compile_one_of(&family_schemas, &base_alias, parent_type_def, None, filter_keys, is_stem_query, depth, current_path.clone())?;
let (case_sql, _) = self.compile_one_of(
&family_schemas,
&base_alias,
Some(&table_aliases),
parent_type_def,
None,
filter_keys,
is_stem_query,
depth,
current_path.clone(),
alias_counter,
)?;
select_args.push(format!("'type', {}", case_sql));
} else if let Some(one_of) = &schema.obj.one_of {
let base_alias = table_aliases.get(&type_def.name).cloned().unwrap_or_else(|| parent_alias.to_string());
let base_alias = table_aliases
.get(&type_def.name)
.cloned()
.unwrap_or_else(|| parent_alias.to_string());
select_args.push(format!("'id', {}.id", base_alias));
let (case_sql, _) = self.compile_one_of(one_of, &base_alias, parent_type_def, None, filter_keys, is_stem_query, depth, current_path.clone())?;
let (case_sql, _) = self.compile_one_of(
one_of,
&base_alias,
Some(&table_aliases),
parent_type_def,
None,
filter_keys,
is_stem_query,
depth,
current_path.clone(),
alias_counter,
)?;
select_args.push(format!("'type', {}", case_sql));
}
@ -320,6 +377,7 @@ impl SqlCompiler {
type_def,
&table_aliases,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name,
filter_keys,
@ -352,19 +410,20 @@ impl SqlCompiler {
fn build_hierarchy_from_clauses(
&self,
type_def: &crate::database::r#type::Type,
local_ctx: &str,
alias_counter: &mut usize,
) -> (std::collections::HashMap<String, String>, Vec<String>) {
let mut table_aliases = std::collections::HashMap::new();
let mut from_clauses = Vec::new();
for (i, table_name) in type_def.hierarchy.iter().enumerate() {
let alias = format!("{}_t{}", local_ctx, i + 1);
*alias_counter += 1;
let alias = format!("{}_{}", table_name, alias_counter);
table_aliases.insert(table_name.clone(), alias.clone());
if i == 0 {
from_clauses.push(format!("agreego.{} {}", table_name, alias));
} else {
let prev_alias = format!("{}_t{}", local_ctx, i);
let prev_alias = format!("{}_{}", type_def.hierarchy[i - 1], *alias_counter - 1);
from_clauses.push(format!(
"JOIN agreego.{} {} ON {}.id = {}.id",
table_name, alias, alias, prev_alias
@ -384,6 +443,7 @@ impl SqlCompiler {
is_stem_query: bool,
depth: usize,
current_path: &str,
alias_counter: &mut usize,
) -> Result<Vec<String>, String> {
let mut select_args = Vec::new();
let grouped_fields = type_def.grouped_fields.as_ref().and_then(|v| v.as_object());
@ -410,16 +470,20 @@ impl SqlCompiler {
}
let is_object_or_array = match &prop_schema.obj.type_ {
Some(crate::database::schema::SchemaTypeOrArray::Single(s)) => s == "object" || s == "array",
Some(crate::database::schema::SchemaTypeOrArray::Multiple(v)) => v.contains(&"object".to_string()) || v.contains(&"array".to_string()),
_ => false
Some(crate::database::schema::SchemaTypeOrArray::Single(s)) => {
s == "object" || s == "array"
}
Some(crate::database::schema::SchemaTypeOrArray::Multiple(v)) => {
v.contains(&"object".to_string()) || v.contains(&"array".to_string())
}
_ => false,
};
let is_primitive = prop_schema.obj.r#ref.is_none()
&& prop_schema.obj.items.is_none()
&& prop_schema.obj.properties.is_none()
&& prop_schema.obj.one_of.is_none()
&& !is_object_or_array;
let is_primitive = prop_schema.obj.r#ref.is_none()
&& prop_schema.obj.items.is_none()
&& prop_schema.obj.properties.is_none()
&& prop_schema.obj.one_of.is_none()
&& !is_object_or_array;
if is_primitive {
if let Some(ft) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
@ -438,12 +502,14 @@ impl SqlCompiler {
let (val_sql, val_type) = self.walk_schema(
prop_schema,
&owner_alias,
Some(table_aliases),
Some(type_def), // Pass current type_def as parent_type_def for child properties
Some(prop_key),
filter_keys,
is_stem_query,
depth + 1,
next_path,
alias_counter,
)?;
if val_type != "abort" {
@ -459,6 +525,7 @@ impl SqlCompiler {
type_def: &crate::database::r#type::Type,
table_aliases: &std::collections::HashMap<String, String>,
parent_alias: &str,
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name: Option<&str>,
filter_keys: &[String],
@ -503,129 +570,151 @@ impl SqlCompiler {
let mut filter_alias = base_alias.clone();
if let Some(gf) = type_def.grouped_fields.as_ref().and_then(|v| v.as_object()) {
for (t_name, fields_val) in gf {
if let Some(fields_arr) = fields_val.as_array() {
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
filter_alias = table_aliases
.get(t_name)
.cloned()
.unwrap_or_else(|| base_alias.clone());
break;
for (t_name, fields_val) in gf {
if let Some(fields_arr) = fields_val.as_array() {
if fields_arr.iter().any(|v| v.as_str() == Some(field_name)) {
filter_alias = table_aliases
.get(t_name)
.cloned()
.unwrap_or_else(|| base_alias.clone());
break;
}
}
}
}
let mut is_ilike = false;
let mut cast = "";
if let Some(field_types) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
if let Some(pg_type_val) = field_types.get(field_name) {
if let Some(pg_type) = pg_type_val.as_str() {
if pg_type == "uuid" {
cast = "::uuid";
} else if pg_type == "boolean" || pg_type == "bool" {
cast = "::boolean";
} else if pg_type.contains("timestamp") || pg_type == "timestamptz" || pg_type == "date"
{
cast = "::timestamptz";
} else if pg_type == "numeric"
|| pg_type.contains("int")
|| pg_type == "real"
|| pg_type == "double precision"
{
cast = "::numeric";
} else if pg_type == "text" || pg_type.contains("char") {
let mut is_enum = false;
if let Some(props) = &schema.obj.properties {
if let Some(ps) = props.get(field_name) {
is_enum = ps.obj.enum_.is_some();
}
}
if !is_enum {
is_ilike = true;
}
}
}
}
}
let mut is_ilike = false;
let mut cast = "";
let param_index = i + 1;
let p_val = format!("${}#>>'{{}}'", param_index);
if let Some(field_types) = type_def.field_types.as_ref().and_then(|v| v.as_object()) {
if let Some(pg_type_val) = field_types.get(field_name) {
if let Some(pg_type) = pg_type_val.as_str() {
if pg_type == "uuid" {
cast = "::uuid";
} else if pg_type == "boolean" || pg_type == "bool" {
cast = "::boolean";
} else if pg_type.contains("timestamp")
|| pg_type == "timestamptz"
|| pg_type == "date"
{
cast = "::timestamptz";
} else if pg_type == "numeric"
|| pg_type.contains("int")
|| pg_type == "real"
|| pg_type == "double precision"
{
cast = "::numeric";
} else if pg_type == "text" || pg_type.contains("char") {
let mut is_enum = false;
if let Some(props) = &schema.obj.properties {
if let Some(ps) = props.get(field_name) {
is_enum = ps.obj.enum_.is_some();
}
}
if !is_enum {
is_ilike = true;
}
}
if op == "$in" || op == "$nin" {
let sql_op = if op == "$in" { "IN" } else { "NOT IN" };
let subquery = format!(
"(SELECT value{} FROM jsonb_array_elements_text(({})::jsonb))",
cast, p_val
);
where_clauses.push(format!(
"{}.{} {} {}",
filter_alias, field_name, sql_op, subquery
));
} else {
let sql_op = match op {
"$eq" => {
if is_ilike {
"ILIKE"
} else {
"="
}
}
}
"$ne" => {
if is_ilike {
"NOT ILIKE"
} else {
"!="
}
}
"$gt" => ">",
"$gte" => ">=",
"$lt" => "<",
"$lte" => "<=",
_ => {
if is_ilike {
"ILIKE"
} else {
"="
}
}
};
let param_index = i + 1;
let p_val = format!("${}#>>'{{}}'", param_index);
if op == "$in" || op == "$nin" {
let sql_op = if op == "$in" { "IN" } else { "NOT IN" };
let subquery = format!(
"(SELECT value{} FROM jsonb_array_elements_text(({})::jsonb))",
cast, p_val
);
where_clauses.push(format!(
"{}.{} {} {}",
filter_alias, field_name, sql_op, subquery
));
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
p_val
} else {
let sql_op = match op {
"$eq" => {
if is_ilike {
"ILIKE"
} else {
"="
}
}
"$ne" => {
if is_ilike {
"NOT ILIKE"
} else {
"!="
}
}
"$gt" => ">",
"$gte" => ">=",
"$lt" => "<",
"$lte" => "<=",
_ => {
if is_ilike {
"ILIKE"
} else {
"="
}
}
};
format!("({}){}", p_val, cast)
};
let param_sql = if is_ilike && (op == "$eq" || op == "$ne") {
p_val
} else {
format!("({}){}", p_val, cast)
};
where_clauses.push(format!(
"{}.{} {} {}",
filter_alias, field_name, sql_op, param_sql
));
where_clauses.push(format!(
"{}.{} {} {}",
filter_alias, field_name, sql_op, param_sql
));
}
}
if let Some(prop) = prop_name {
// Find what type the parent alias is actually mapping to
let mut relation_alias = parent_alias.to_string();
let mut relation_resolved = false;
if let Some(parent_type) = parent_type_def {
if let Some(relation) = self.db.get_relation(&parent_type.name, &type_def.name, prop, None) {
if let Some(relation) = self
.db
.get_relation(&parent_type.name, &type_def.name, prop, None)
{
let source_col = &relation.source_columns[0];
let dest_col = &relation.destination_columns[0];
let mut possible_relation_alias = None;
if let Some(pta) = parent_table_aliases {
if let Some(a) = pta.get(&relation.source_type) {
possible_relation_alias = Some(a.clone());
} else if let Some(a) = pta.get(&relation.destination_type) {
possible_relation_alias = Some(a.clone());
}
}
if let Some(pa) = possible_relation_alias {
relation_alias = pa;
}
// Determine directionality based on the Relation metadata
if relation.source_type == parent_type.name || parent_type.hierarchy.contains(&relation.source_type) {
if relation.source_type == parent_type.name
|| parent_type.hierarchy.contains(&relation.source_type)
{
// Parent is the source
where_clauses.push(format!("{}.{} = {}.{}", parent_alias, source_col, base_alias, dest_col));
where_clauses.push(format!(
"{}.{} = {}.{}",
relation_alias, source_col, base_alias, dest_col
));
relation_resolved = true;
} else if relation.destination_type == parent_type.name || parent_type.hierarchy.contains(&relation.destination_type) {
} else if relation.destination_type == parent_type.name
|| parent_type.hierarchy.contains(&relation.destination_type)
{
// Parent is the destination
where_clauses.push(format!("{}.{} = {}.{}", base_alias, source_col, parent_alias, dest_col));
where_clauses.push(format!(
"{}.{} = {}.{}",
base_alias, source_col, relation_alias, dest_col
));
relation_resolved = true;
}
}
@ -634,10 +723,15 @@ impl SqlCompiler {
if !relation_resolved {
// Fallback heuristics for unmapped polymorphism or abstract models
if prop == "target" || prop == "source" {
if parent_alias.ends_with("_t1") {
relation_alias = parent_alias.replace("_t1", "_t2");
if let Some(pta) = parent_table_aliases {
if let Some(a) = pta.get("relationship") {
relation_alias = a.clone();
}
}
where_clauses.push(format!("{}.id = {}.{}_id", base_alias, relation_alias, prop));
where_clauses.push(format!(
"{}.id = {}.{}_id",
base_alias, relation_alias, prop
));
} else {
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, relation_alias));
}
@ -651,11 +745,13 @@ impl SqlCompiler {
&self,
props: &std::collections::BTreeMap<String, std::sync::Arc<crate::database::schema::Schema>>,
parent_alias: &str,
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
parent_type_def: Option<&crate::database::r#type::Type>,
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
alias_counter: &mut usize,
) -> Result<(String, String), String> {
let mut build_args = Vec::new();
for (k, v) in props {
@ -664,16 +760,18 @@ impl SqlCompiler {
} else {
format!("{}.{}", current_path, k)
};
let (child_sql, val_type) = self.walk_schema(
v,
parent_alias,
parent_table_aliases,
parent_type_def,
Some(k),
filter_keys,
is_stem_query,
depth + 1,
next_path,
alias_counter,
)?;
if val_type == "abort" {
continue;
@ -688,12 +786,14 @@ impl SqlCompiler {
&self,
schemas: &[Arc<crate::database::schema::Schema>],
parent_alias: &str,
parent_table_aliases: Option<&std::collections::HashMap<String, String>>,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name_context: Option<&str>,
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
alias_counter: &mut usize,
) -> Result<(String, String), String> {
let mut case_statements = Vec::new();
let type_col = if let Some(prop) = prop_name_context {
@ -706,17 +806,19 @@ impl SqlCompiler {
if let Some(ref_id) = &option_schema.obj.r#ref {
// Find the physical type this ref maps to
let base_type_name = ref_id.split('.').next_back().unwrap_or("").to_string();
// Generate the nested SQL for this specific target type
let (val_sql, _) = self.walk_schema(
option_schema,
parent_alias,
parent_table_aliases,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
depth,
current_path.clone(),
alias_counter,
)?;
case_statements.push(format!(
@ -730,10 +832,7 @@ impl SqlCompiler {
return Ok(("NULL".to_string(), "string".to_string()));
}
let sql = format!(
"CASE {} ELSE NULL END",
case_statements.join(" ")
);
let sql = format!("CASE {} ELSE NULL END", case_statements.join(" "));
Ok((sql, "object".to_string()))
}