diff --git a/query-algebrizer/src/clauses/mod.rs b/query-algebrizer/src/clauses/mod.rs index 0d0bef12..0b51fcdd 100644 --- a/query-algebrizer/src/clauses/mod.rs +++ b/query-algebrizer/src/clauses/mod.rs @@ -48,6 +48,7 @@ use errors::{ use types::{ ColumnConstraint, ColumnIntersection, + ComputedTable, DatomsColumn, DatomsTable, EmptyBecause, @@ -141,6 +142,10 @@ pub struct ConjoiningClauses { /// A vector of source/alias pairs used to construct a SQL `FROM` list. pub from: Vec, + /// A vector of computed tables (typically subqueries). The index into this vector is used as + /// an identifier in a `DatomsTable::Computed(c)` table reference. + pub computed_tables: Vec, + /// A list of fragments that can be joined by `AND`. pub wheres: ColumnIntersection, @@ -196,6 +201,7 @@ impl Default for ConjoiningClauses { empty_because: None, alias_counter: RcCounter::new(), from: vec![], + computed_tables: vec![], wheres: ColumnIntersection::default(), input_variables: BTreeSet::new(), column_bindings: BTreeMap::new(), @@ -588,7 +594,12 @@ impl ConjoiningClauses { } pub fn next_alias_for_table(&mut self, table: DatomsTable) -> TableAlias { - format!("{}{:02}", table.name(), self.alias_counter.next()) + match table { + DatomsTable::Computed(u) => + format!("{}{:02}", table.name(), u), + _ => + format!("{}{:02}", table.name(), self.alias_counter.next()), + } } /// Produce a (table, alias) pair to handle the provided pattern. diff --git a/query-algebrizer/src/lib.rs b/query-algebrizer/src/lib.rs index 8ab9d5db..64558b6b 100644 --- a/query-algebrizer/src/lib.rs +++ b/query-algebrizer/src/lib.rs @@ -110,6 +110,7 @@ pub use types::{ ColumnConstraint, ColumnConstraintOrAlternation, ColumnIntersection, + ComputedTable, DatomsColumn, DatomsTable, QualifiedAlias, diff --git a/query-algebrizer/src/types.rs b/query-algebrizer/src/types.rs index 8d98ddae..934e8684 100644 --- a/query-algebrizer/src/types.rs +++ b/query-algebrizer/src/types.rs @@ -8,6 +8,7 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +use std::collections::BTreeSet; use std::collections::HashSet; use std::fmt::{ @@ -28,13 +29,24 @@ use mentat_query::{ }; /// This enum models the fixed set of default tables we have -- two -/// tables and two views. +/// tables and two views -- and computed tables defined in the enclosing CC. #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub enum DatomsTable { Datoms, // The non-fulltext datoms table. FulltextValues, // The virtual table mapping IDs to strings. FulltextDatoms, // The fulltext-datoms view. AllDatoms, // Fulltext and non-fulltext datoms. + Computed(usize), // A computed table, tracked elsewhere in the query. +} + +/// A source of rows that isn't a named table -- typically a subquery or union. +pub enum ComputedTable { + // Subquery(BTreeSet, ::clauses::ConjoiningClauses), + Union { + projection: BTreeSet, + type_extraction: BTreeSet, + arms: Vec<::clauses::ConjoiningClauses>, + }, } impl DatomsTable { @@ -44,6 +56,7 @@ impl DatomsTable { DatomsTable::FulltextValues => "fulltext_values", DatomsTable::FulltextDatoms => "fulltext_datoms", DatomsTable::AllDatoms => "all_datoms", + DatomsTable::Computed(_) => "c", } } }