diff --git a/query-algebrizer/src/clauses/where_fn.rs b/query-algebrizer/src/clauses/where_fn.rs index ce348657..40c047fb 100644 --- a/query-algebrizer/src/clauses/where_fn.rs +++ b/query-algebrizer/src/clauses/where_fn.rs @@ -40,6 +40,7 @@ use errors::{ use super::QualifiedAlias; use types::{ + ColumnConstraint, ComputedTable, EmptyBecause, SourceAlias, @@ -241,6 +242,8 @@ impl ConjoiningClauses { self.bind_value(&var, value.clone()); } + let vt = value.value_type(); + // Check to see whether this variable is already associated to a column. // If so, we want to add an equality filter (or, in the future, redo the existing patterns). if let Some(QualifiedAlias(table, column)) = self.column_bindings @@ -249,6 +252,13 @@ impl ConjoiningClauses { self.constrain_column_to_constant(table, column, value); } + // Are we also trying to figure out the type of the value when the query runs? + // If so, constrain that! + if let Some(table) = self.extracted_types.get(&var) + .map(|qa| qa.0.clone()) { + self.wheres.add_intersection(ColumnConstraint::HasType(table, vt)); + } + Ok(()) } diff --git a/query-translator/tests/translate.rs b/query-translator/tests/translate.rs index a9c42274..5b3b0c1f 100644 --- a/query-translator/tests/translate.rs +++ b/query-translator/tests/translate.rs @@ -684,12 +684,15 @@ fn test_unbound_attribute_with_ground_entity() { #[test] fn test_unbound_attribute_with_ground() { - // TODO: this needs to expand the type code. #475. - let query = r#"[:find ?x :where [?x _ ?v] (not [(ground 5) ?v])]"#; + let query = r#"[:find ?x ?v :where [?x _ ?v] (not [(ground 17) ?v])]"#; let schema = prepopulated_schema(); let SQLQuery { sql, .. } = translate(&schema, query); - assert_eq!(sql, "SELECT DISTINCT `all_datoms00`.e AS `?x` FROM `all_datoms` AS `all_datoms00` \ - WHERE NOT EXISTS (SELECT 1 WHERE `all_datoms00`.v = 5)"); + assert_eq!(sql, "SELECT DISTINCT `all_datoms00`.e AS `?x`, \ + `all_datoms00`.v AS `?v`, \ + `all_datoms00`.value_type_tag AS `?v_value_type_tag` \ + FROM `all_datoms` AS `all_datoms00` \ + WHERE NOT EXISTS (SELECT 1 WHERE `all_datoms00`.v = 17 AND \ + `all_datoms00`.value_type_tag = 5)"); }