queryer supports subfiltering now

This commit is contained in:
2026-03-15 07:49:05 -04:00
parent 6de75ba525
commit db5183930d
4 changed files with 195 additions and 138 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)?;
let (sql, _) = self.walk_schema(target_schema, "t1", None, filter_keys, is_stem_query, 0, String::new())?;
Ok(sql)
}
@ -61,12 +61,19 @@ impl SqlCompiler {
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
) -> Result<(String, String), String> {
// Determine the base schema type (could be an array, object, or literal)
match &schema.obj.type_ {
Some(crate::database::schema::SchemaTypeOrArray::Single(t)) if t == "array" => {
// Handle Arrays:
if let Some(items) = &schema.obj.items {
let next_path = if current_path.is_empty() {
String::from("#")
} else {
format!("{}.#", current_path)
};
if let Some(ref_id) = &items.obj.r#ref {
if let Some(type_def) = self.db.types.get(ref_id) {
return self.compile_entity_node(
@ -78,6 +85,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth,
next_path,
);
}
}
@ -88,6 +96,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth + 1,
next_path,
)?;
return Ok((
format!("(SELECT jsonb_agg({}) FROM TODO)", item_sql),
@ -121,6 +130,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth,
current_path,
);
}
@ -135,6 +145,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth,
current_path,
);
}
return Err(format!("Unresolved $ref: {}", ref_id));
@ -148,6 +159,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth,
current_path,
);
}
@ -195,6 +207,7 @@ impl SqlCompiler {
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
) -> Result<(String, String), String> {
let local_ctx = format!("{}_{}", parent_alias, prop_name.unwrap_or("obj"));
@ -210,6 +223,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth,
&current_path,
)?;
let jsonb_obj_sql = if select_args.is_empty() {
@ -226,6 +240,7 @@ impl SqlCompiler {
parent_alias,
prop_name,
filter_keys,
&current_path,
)?;
let selection = if is_array {
@ -285,6 +300,7 @@ impl SqlCompiler {
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: &str,
) -> 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());
@ -310,6 +326,12 @@ impl SqlCompiler {
}
}
let next_path = if current_path.is_empty() {
prop_key.clone()
} else {
format!("{}.{}", current_path, prop_key)
};
let (val_sql, val_type) = self.walk_schema(
prop_schema,
&owner_alias,
@ -317,6 +339,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth + 1,
next_path,
)?;
if val_type != "abort" {
@ -334,6 +357,7 @@ impl SqlCompiler {
parent_alias: &str,
prop_name: Option<&str>,
filter_keys: &[String],
current_path: &str,
) -> Result<Vec<String>, String> {
let base_alias = table_aliases
.get(&type_def.name)
@ -343,15 +367,32 @@ impl SqlCompiler {
let mut where_clauses = Vec::new();
where_clauses.push(format!("NOT {}.archived", base_alias));
if parent_alias == "t1" {
for (i, filter_key) in filter_keys.iter().enumerate() {
let mut parts = filter_key.split(':');
let field_name = parts.next().unwrap_or(filter_key);
let op = parts.next().unwrap_or("$eq");
for (i, filter_key) in filter_keys.iter().enumerate() {
let mut parts = filter_key.split(':');
let full_field_path = parts.next().unwrap_or(filter_key);
let op = parts.next().unwrap_or("$eq");
let mut filter_alias = base_alias.clone();
let field_name = if current_path.is_empty() {
if full_field_path.contains('.') || full_field_path.contains('#') {
continue;
}
full_field_path
} else {
let prefix = format!("{}.", current_path);
if full_field_path.starts_with(&prefix) {
let remainder = &full_field_path[prefix.len()..];
if remainder.contains('.') || remainder.contains('#') {
continue;
}
remainder
} else {
continue;
}
};
if let Some(gf) = type_def.grouped_fields.as_ref().and_then(|v| v.as_object()) {
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)) {
@ -453,7 +494,6 @@ impl SqlCompiler {
"{}.{} {} {}",
filter_alias, field_name, sql_op, param_sql
));
}
}
}
@ -471,9 +511,16 @@ impl SqlCompiler {
filter_keys: &[String],
is_stem_query: bool,
depth: usize,
current_path: String,
) -> Result<(String, String), String> {
let mut build_args = Vec::new();
for (k, v) in props {
let next_path = if current_path.is_empty() {
k.clone()
} else {
format!("{}.{}", current_path, k)
};
let (child_sql, val_type) = self.walk_schema(
v,
parent_alias,
@ -481,6 +528,7 @@ impl SqlCompiler {
filter_keys,
is_stem_query,
depth + 1,
next_path,
)?;
if val_type == "abort" {
continue;