queryer test checkpoint

This commit is contained in:
2026-03-17 15:06:02 -04:00
parent 70a27b430d
commit e1314496dd
3 changed files with 172 additions and 117 deletions

View File

@ -47,7 +47,7 @@ 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, filter_keys, is_stem_query, 0, String::new())?;
let (sql, _) = self.walk_schema(target_schema, "t1", None, None, filter_keys, is_stem_query, 0, String::new())?;
Ok(sql)
}
@ -57,6 +57,7 @@ impl SqlCompiler {
&self,
schema: &crate::database::schema::Schema,
parent_alias: &str,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name_context: Option<&str>,
filter_keys: &[String],
is_stem_query: bool,
@ -80,6 +81,7 @@ impl SqlCompiler {
items,
type_def,
parent_alias,
parent_type_def,
prop_name_context,
true,
filter_keys,
@ -92,6 +94,7 @@ impl SqlCompiler {
let (item_sql, _) = self.walk_schema(
items,
parent_alias,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
@ -125,6 +128,7 @@ impl SqlCompiler {
schema,
type_def,
parent_alias,
parent_type_def,
prop_name_context,
false,
filter_keys,
@ -141,6 +145,7 @@ impl SqlCompiler {
return self.walk_schema(
target_schema,
parent_alias,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
@ -169,6 +174,7 @@ impl SqlCompiler {
return self.compile_one_of(
&family_schemas,
parent_alias,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
@ -182,6 +188,7 @@ impl SqlCompiler {
return self.compile_one_of(
one_of,
parent_alias,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,
@ -195,6 +202,7 @@ impl SqlCompiler {
return self.compile_inline_object(
props,
parent_alias,
parent_type_def,
filter_keys,
is_stem_query,
depth,
@ -241,6 +249,7 @@ impl SqlCompiler {
schema: &crate::database::schema::Schema,
type_def: &crate::database::r#type::Type,
parent_alias: &str,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name: Option<&str>,
is_array: bool,
filter_keys: &[String],
@ -290,12 +299,12 @@ impl SqlCompiler {
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, None, filter_keys, is_stem_query, depth, current_path.clone())?;
let (case_sql, _) = self.compile_one_of(&family_schemas, &base_alias, parent_type_def, None, filter_keys, is_stem_query, depth, current_path.clone())?;
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());
select_args.push(format!("'id', {}.id", base_alias));
let (case_sql, _) = self.compile_one_of(one_of, &base_alias, None, filter_keys, is_stem_query, depth, current_path.clone())?;
let (case_sql, _) = self.compile_one_of(one_of, &base_alias, parent_type_def, None, filter_keys, is_stem_query, depth, current_path.clone())?;
select_args.push(format!("'type', {}", case_sql));
}
@ -311,6 +320,7 @@ impl SqlCompiler {
type_def,
&table_aliases,
parent_alias,
parent_type_def,
prop_name,
filter_keys,
&current_path,
@ -428,6 +438,7 @@ impl SqlCompiler {
let (val_sql, val_type) = self.walk_schema(
prop_schema,
&owner_alias,
Some(type_def), // Pass current type_def as parent_type_def for child properties
Some(prop_key),
filter_keys,
is_stem_query,
@ -448,6 +459,7 @@ impl SqlCompiler {
type_def: &crate::database::r#type::Type,
table_aliases: &std::collections::HashMap<String, String>,
parent_alias: &str,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name: Option<&str>,
filter_keys: &[String],
current_path: &str,
@ -596,10 +608,39 @@ impl SqlCompiler {
}
if let Some(prop) = prop_name {
if prop == "target" || prop == "source" {
where_clauses.push(format!("{}.id = {}.{}_id", base_alias, parent_alias, prop));
} else {
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, parent_alias));
// 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) {
let source_col = &relation.source_columns[0];
let dest_col = &relation.destination_columns[0];
// Determine directionality based on the Relation metadata
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));
relation_resolved = true;
} 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));
relation_resolved = true;
}
}
}
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");
}
where_clauses.push(format!("{}.id = {}.{}_id", base_alias, relation_alias, prop));
} else {
where_clauses.push(format!("{}.parent_id = {}.id", base_alias, relation_alias));
}
}
}
@ -610,6 +651,7 @@ impl SqlCompiler {
&self,
props: &std::collections::BTreeMap<String, std::sync::Arc<crate::database::schema::Schema>>,
parent_alias: &str,
parent_type_def: Option<&crate::database::r#type::Type>,
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
@ -626,6 +668,7 @@ impl SqlCompiler {
let (child_sql, val_type) = self.walk_schema(
v,
parent_alias,
parent_type_def,
Some(k),
filter_keys,
is_stem_query,
@ -645,6 +688,7 @@ impl SqlCompiler {
&self,
schemas: &[Arc<crate::database::schema::Schema>],
parent_alias: &str,
parent_type_def: Option<&crate::database::r#type::Type>,
prop_name_context: Option<&str>,
filter_keys: &[String],
is_stem_query: bool,
@ -667,6 +711,7 @@ impl SqlCompiler {
let (val_sql, _) = self.walk_schema(
option_schema,
parent_alias,
parent_type_def,
prop_name_context,
filter_keys,
is_stem_query,