Compare commits

..

5 Commits

Author SHA1 Message Date
4cc5245336 version: 1.0.152 2026-06-03 10:50:28 -04:00
c71e99527d dynamic type variables now recursive 2026-06-03 10:50:15 -04:00
843891f67e version upped 2026-05-28 14:57:29 -04:00
8bb7085f76 cleaned out raits_debug_val 2026-05-28 14:56:17 -04:00
ea03584bbd re-applied fix for family in conditions 2026-05-28 14:54:57 -04:00
10 changed files with 270 additions and 111 deletions

View File

@ -295,6 +295,7 @@ The Queryer transforms Postgres into a pre-compiled Semantic Query Engine, desig
* **The Dot Convention**: When a schema requests `family: "target.schema"`, the compiler extracts the base type (e.g. `schema`) and looks up its Physical Table definition.
* **Multi-Table Branching**: If the Physical Table is a parent to other tables (e.g. `organization` has variations `["organization", "bot", "person"]`), the compiler generates a dynamic `CASE WHEN type = '...' THEN ...` query, expanding into sub-queries for each variation. To ensure safe resolution, the compiler dynamically evaluates correlation boundaries: it attempts standard Relational Edge discovery first. If no explicit relational edge exists (indicating pure Table Inheritance rather than a standard foreign-key graph relationship), it safely invokes a **Table Parity Fallback**. This generates an explicit ID correlation constraint (`AND inner.id = outer.id`), perfectly binding the structural variations back to the parent row to eliminate Cartesian products.
* **Single-Table Bypass**: If the Physical Table is a leaf node with only one variation (e.g. `person` has variations `["person"]`), the compiler cleanly bypasses `CASE` generation and compiles a simple `SELECT` across the base table, as all schema extensions (e.g. `light.person`, `full.person`) are guaranteed to reside in the exact same physical row.
* **Polymorphic Relation Type Filtering**: When a relationship maps to a polymorphic target with variations, the Queryer compiles an `IN` clause containing all allowed table variations (e.g., `counterparty_type IN ('bot', 'organization', 'person')`) rather than matching the base type literal, ensuring all polymorphic types are loaded correctly.
---

View File

@ -37,6 +37,14 @@
},
"filter": {
"type": "$kind.filter"
},
"conditions": {
"type": "object",
"properties": {
"new": { "type": "$kind.filter" },
"old": { "type": "$kind.filter" },
"complete": { "type": "$kind.filter" }
}
}
}
}
@ -149,7 +157,48 @@
}
]
}
},
{
"description": "Valid nested filter payload",
"data": {
"kind": "person",
"conditions": {
"new": {
"age": 30
}
}
},
"schema_id": "search",
"action": "validate",
"expect": {
"success": true
}
},
{
"description": "Invalid nested filter payload (fails constraint)",
"data": {
"kind": "person",
"conditions": {
"new": {
"age": "thirty"
}
}
},
"schema_id": "search",
"action": "validate",
"expect": {
"success": false,
"errors": [
{
"code": "INVALID_TYPE",
"details": {
"path": "conditions/new/age"
}
}
]
}
}
]
}
]

View File

@ -1,8 +1,7 @@
use std::collections::{HashMap, HashSet};
use serde_json::Value;
use std::collections::{HashMap, HashSet};
pub fn compose(val: &mut Value, errors: &mut Vec<crate::drop::Error>) -> Result<(), String> {
let _ = std::fs::write("/Users/awgneo/Repositories/thoughtpatterns/cellular/jspg/traits_debug_val.json", serde_json::to_string_pretty(val).unwrap());
let mut traits = HashMap::new();
let mut schemas = HashMap::new();
@ -74,7 +73,9 @@ fn resolve_in_place(
return;
}
let include_opt = current.as_object_mut().and_then(|obj| obj.remove("include"));
let include_opt = current
.as_object_mut()
.and_then(|obj| obj.remove("include"));
if let Some(include_val) = include_opt {
if let Some(include_arr) = include_val.as_array() {
let mut merged_props = serde_json::Map::new();
@ -145,7 +146,10 @@ fn resolve_in_place(
visited.remove(inc_name);
// Merge properties (host overrides trait)
if let Some(target_props) = resolved_target.get("properties").and_then(|v| v.as_object()) {
if let Some(target_props) = resolved_target
.get("properties")
.and_then(|v| v.as_object())
{
for (k, v) in target_props {
if !merged_props.contains_key(k) {
merged_props.insert(k.clone(), v.clone());
@ -154,7 +158,10 @@ fn resolve_in_place(
}
// Merge patternProperties (host overrides trait)
if let Some(target_pat_props) = resolved_target.get("patternProperties").and_then(|v| v.as_object()) {
if let Some(target_pat_props) = resolved_target
.get("patternProperties")
.and_then(|v| v.as_object())
{
for (k, v) in target_pat_props {
if !merged_pattern_props.contains_key(k) {
merged_pattern_props.insert(k.clone(), v.clone());
@ -181,11 +188,19 @@ fn resolve_in_place(
}
// Merge dependencies
if let Some(target_deps) = resolved_target.get("dependencies").and_then(|v| v.as_object()) {
if let Some(target_deps) = resolved_target
.get("dependencies")
.and_then(|v| v.as_object())
{
for (dep_prop, dep_val) in target_deps {
if let Some(existing_val) = merged_dependencies.get_mut(dep_prop) {
if let (Some(arr_existing), Some(arr_target)) = (existing_val.as_array_mut(), dep_val.as_array()) {
let mut set: HashSet<String> = arr_existing.iter().filter_map(|x| x.as_str().map(String::from)).collect();
if let (Some(arr_existing), Some(arr_target)) =
(existing_val.as_array_mut(), dep_val.as_array())
{
let mut set: HashSet<String> = arr_existing
.iter()
.filter_map(|x| x.as_str().map(String::from))
.collect();
for x in arr_target {
if let Some(s) = x.as_str() {
if set.insert(s.to_string()) {
@ -203,7 +218,13 @@ fn resolve_in_place(
// Inherit other non-merged schemas/scalars if not defined in host (type, items, cases, family, format, etc.)
if let Some(obj) = current.as_object_mut() {
for (k, v) in resolved_target.as_object().unwrap() {
if k != "properties" && k != "patternProperties" && k != "required" && k != "display" && k != "dependencies" && k != "include" {
if k != "properties"
&& k != "patternProperties"
&& k != "required"
&& k != "display"
&& k != "dependencies"
&& k != "include"
{
if !obj.contains_key(k) {
obj.insert(k.clone(), v.clone());
}
@ -229,7 +250,10 @@ fn resolve_in_place(
obj.insert("properties".to_string(), Value::Object(merged_props));
}
if !merged_pattern_props.is_empty() {
obj.insert("patternProperties".to_string(), Value::Object(merged_pattern_props));
obj.insert(
"patternProperties".to_string(),
Value::Object(merged_pattern_props),
);
}
if !merged_required.is_empty() {
let mut req_vec: Vec<Value> = merged_required.into_iter().map(Value::String).collect();
@ -242,7 +266,10 @@ fn resolve_in_place(
obj.insert("display".to_string(), Value::Array(disp_vec));
}
if !merged_dependencies.is_empty() {
obj.insert("dependencies".to_string(), Value::Object(merged_dependencies));
obj.insert(
"dependencies".to_string(),
Value::Object(merged_dependencies),
);
}
}
}
@ -252,47 +279,138 @@ fn resolve_in_place(
if let Some(obj) = current.as_object_mut() {
if let Some(props) = obj.get_mut("properties").and_then(|v| v.as_object_mut()) {
for (k, v) in props {
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/{}", path, k), visited);
resolve_in_place(
v,
traits,
schemas,
errors,
schema_id,
&format!("{}/{}", path, k),
visited,
);
}
}
if let Some(pat_props) = obj.get_mut("patternProperties").and_then(|v| v.as_object_mut()) {
if let Some(pat_props) = obj
.get_mut("patternProperties")
.and_then(|v| v.as_object_mut())
{
for (k, v) in pat_props {
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/{}", path, k), visited);
resolve_in_place(
v,
traits,
schemas,
errors,
schema_id,
&format!("{}/{}", path, k),
visited,
);
}
}
if let Some(items) = obj.get_mut("items") {
resolve_in_place(items, traits, schemas, errors, schema_id, &format!("{}/items", path), visited);
resolve_in_place(
items,
traits,
schemas,
errors,
schema_id,
&format!("{}/items", path),
visited,
);
}
if let Some(prefix_items) = obj.get_mut("prefixItems").and_then(|v| v.as_array_mut()) {
for (i, v) in prefix_items.iter_mut().enumerate() {
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/prefixItems/{}", path, i), visited);
resolve_in_place(
v,
traits,
schemas,
errors,
schema_id,
&format!("{}/prefixItems/{}", path, i),
visited,
);
}
}
if let Some(additional_props) = obj.get_mut("additionalProperties") {
resolve_in_place(additional_props, traits, schemas, errors, schema_id, &format!("{}/additionalProperties", path), visited);
resolve_in_place(
additional_props,
traits,
schemas,
errors,
schema_id,
&format!("{}/additionalProperties", path),
visited,
);
}
if let Some(one_of) = obj.get_mut("oneOf").and_then(|v| v.as_array_mut()) {
for (i, v) in one_of.iter_mut().enumerate() {
resolve_in_place(v, traits, schemas, errors, schema_id, &format!("{}/oneOf/{}", path, i), visited);
resolve_in_place(
v,
traits,
schemas,
errors,
schema_id,
&format!("{}/oneOf/{}", path, i),
visited,
);
}
}
if let Some(contains) = obj.get_mut("contains") {
resolve_in_place(contains, traits, schemas, errors, schema_id, &format!("{}/contains", path), visited);
resolve_in_place(
contains,
traits,
schemas,
errors,
schema_id,
&format!("{}/contains", path),
visited,
);
}
if let Some(not) = obj.get_mut("not") {
resolve_in_place(not, traits, schemas, errors, schema_id, &format!("{}/not", path), visited);
resolve_in_place(
not,
traits,
schemas,
errors,
schema_id,
&format!("{}/not", path),
visited,
);
}
if let Some(cases) = obj.get_mut("cases").and_then(|v| v.as_array_mut()) {
for (i, c_val) in cases.iter_mut().enumerate() {
if let Some(c_obj) = c_val.as_object_mut() {
if let Some(when) = c_obj.get_mut("when") {
resolve_in_place(when, traits, schemas, errors, schema_id, &format!("{}/cases/{}/when", path, i), visited);
resolve_in_place(
when,
traits,
schemas,
errors,
schema_id,
&format!("{}/cases/{}/when", path, i),
visited,
);
}
if let Some(then) = c_obj.get_mut("then") {
resolve_in_place(then, traits, schemas, errors, schema_id, &format!("{}/cases/{}/then", path, i), visited);
resolve_in_place(
then,
traits,
schemas,
errors,
schema_id,
&format!("{}/cases/{}/then", path, i),
visited,
);
}
if let Some(else_) = c_obj.get_mut("else") {
resolve_in_place(else_, traits, schemas, errors, schema_id, &format!("{}/cases/{}/else", path, i), visited);
resolve_in_place(
else_,
traits,
schemas,
errors,
schema_id,
&format!("{}/cases/{}/else", path, i),
visited,
);
}
}
}

View File

@ -621,19 +621,17 @@ impl<'a> Compiler<'a> {
.get(table_to_alias)
.or_else(|| type_aliases.get(&node.parent_alias))
{
// DEBUG: See what variations we have for this type
#[cfg(not(test))]
pgrx::notice!("JSPG_POLY_BOUNDS: type={}, col={}, variations_len={}, variations={:?}", type_name, col, type_def.variations.len(), type_def.variations);
// Use IN clause with all variations to support inherited types.
// e.g., "asset" has variations ["asset", "property", "unit", ...]
// so target_type IN ('asset','property','unit') instead of = 'asset'
if type_def.variations.len() > 1 {
let quoted: Vec<String> = type_def.variations.iter()
let quoted: Vec<String> = type_def
.variations
.iter()
.map(|v| format!("'{}'", v))
.collect();
where_clauses.push(format!(
"{}.{} IN ({})", alias, col, quoted.join(", ")
"{}.{} IN ({})",
alias,
col,
quoted.join(", ")
));
} else {
where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name));

View File

@ -50,10 +50,6 @@ impl Queryer {
Err(drop) => return drop,
};
// DEBUG: Emit compiled SQL as a NOTICE for inspection
#[cfg(not(test))]
pgrx::notice!("JSPG_SQL[{}]: {}", schema_id, sql);
// 3. Execute via Database Executor
self.execute_sql(schema_id, &sql, args)
}

View File

@ -1277,6 +1277,18 @@ fn test_dynamic_type_0_4() {
crate::tests::runner::run_test_case(&path, 0, 4).unwrap();
}
#[test]
fn test_dynamic_type_0_5() {
let path = format!("{}/fixtures/dynamicType.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 5).unwrap();
}
#[test]
fn test_dynamic_type_0_6() {
let path = format!("{}/fixtures/dynamicType.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 6).unwrap();
}
#[test]
fn test_property_names_0_0() {
let path = format!("{}/fixtures/propertyNames.json", env!("CARGO_MANIFEST_DIR"));

View File

@ -15,7 +15,7 @@ pub struct ValidationContext<'a> {
pub extensible: bool,
pub reporter: bool,
pub overrides: HashSet<String>,
pub parent: Option<&'a serde_json::Value>,
pub parents: Vec<&'a serde_json::Value>,
}
impl<'a> ValidationContext<'a> {
@ -39,7 +39,7 @@ impl<'a> ValidationContext<'a> {
extensible: effective_extensible,
reporter,
overrides,
parent: None,
parents: Vec::new(),
}
}
@ -63,6 +63,11 @@ impl<'a> ValidationContext<'a> {
) -> Self {
let effective_extensible = schema.extensible.unwrap_or(extensible);
let mut parents = self.parents.clone();
if let Some(p) = parent_instance {
parents.push(p);
}
Self {
db: self.db,
root: self.root,
@ -73,7 +78,7 @@ impl<'a> ValidationContext<'a> {
extensible: effective_extensible,
reporter,
overrides,
parent: parent_instance,
parents,
}
}
@ -85,7 +90,7 @@ impl<'a> ValidationContext<'a> {
HashSet::new(),
self.extensible,
reporter,
self.parent,
None,
)
}

View File

@ -42,83 +42,62 @@ impl Validator {
}
pub fn validate(&self, schema_id: &str, instance: &Value) -> crate::drop::Drop {
let schema_arc = if schema_id.trim().starts_with('{') {
match serde_json::from_str::<crate::database::schema::Schema>(schema_id) {
Ok(schema) => {
let mut errors = Vec::new();
schema.compile(&self.db, "inline", "inline".to_string(), &mut errors);
if !errors.is_empty() {
return crate::drop::Drop::with_errors(errors);
let schema_opt = self.db.schemas.get(schema_id);
if let Some(schema) = schema_opt {
let ctx = ValidationContext::new(
&self.db,
&schema,
&schema,
instance,
HashSet::new(),
false,
false,
);
match ctx.validate_scoped() {
Ok(result) => {
if result.is_valid() {
crate::drop::Drop::success()
} else {
let errors: Vec<crate::drop::Error> = result
.errors
.into_iter()
.map(|e| crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
})
.collect();
crate::drop::Drop::with_errors(errors)
}
Arc::new(schema)
}
Err(e) => {
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_PARSE_FAILED".to_string(),
message: format!("Failed to parse inline schema: {}", e),
details: crate::drop::ErrorDetails::default(),
}]);
}
Err(e) => crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
}]),
}
} else {
match self.db.schemas.get(schema_id) {
Some(schema) => Arc::clone(schema),
None => {
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_NOT_FOUND".to_string(),
message: format!("Schema {} not found", schema_id),
details: crate::drop::ErrorDetails {
path: Some("/".to_string()),
cause: None,
context: None,
schema: None,
},
}]);
}
}
};
let ctx = ValidationContext::new(
&self.db,
&schema_arc,
&schema_arc,
instance,
HashSet::new(),
false,
false,
);
match ctx.validate_scoped() {
Ok(result) => {
if result.is_valid() {
crate::drop::Drop::success()
} else {
let errors: Vec<crate::drop::Error> = result
.errors
.into_iter()
.map(|e| crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
})
.collect();
crate::drop::Drop::with_errors(errors)
}
}
Err(e) => crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: e.code,
message: e.message,
crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_NOT_FOUND".to_string(),
message: format!("Schema {} not found", schema_id),
details: crate::drop::ErrorDetails {
path: Some(e.path),
path: Some("/".to_string()),
cause: None,
context: None,
schema: None,
},
}]),
}])
}
}
}

View File

@ -59,12 +59,13 @@ impl<'a> ValidationContext<'a> {
};
let mut resolved = false;
if let Some(parent) = self.parent {
for parent in self.parents.iter().rev() {
if let Some(obj) = parent.as_object() {
if let Some(val) = obj.get(var_name) {
if let Some(str_val) = val.as_str() {
target_id = format!("{}{}", str_val, suffix);
resolved = true;
break;
}
}
}
@ -97,7 +98,7 @@ impl<'a> ValidationContext<'a> {
new_overrides,
self.extensible,
true, // Reporter mode
self.parent,
None,
);
shadow.root = &global_schema;
result.merge(shadow.validate()?);

View File

@ -1 +1 @@
1.0.151
1.0.152