Skip to content

Commit

Permalink
introduce scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Gil Mizrahi committed Aug 22, 2024
1 parent 05fbb85 commit 1d28034
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 179 deletions.
16 changes: 13 additions & 3 deletions crates/query-engine/translation/src/translation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ pub enum Error {
field_name: models::FieldName,
actual_type: Type,
},
ScopeOutOfRange {
requested: usize,
size: usize,
},
}

/// Capabilities we don't currently support.
Expand Down Expand Up @@ -169,9 +173,6 @@ impl std::fmt::Display for Error {
write!(f, "{string}")
}
Error::UnexpectedStructure(structure) => write!(f, "Unexpected {structure}."),
Error::InternalError(thing) => {
write!(f, "Internal error: {thing}.")
}
Error::NonScalarTypeUsedInOperator { r#type } => {
write!(f, "Non-scalar-type used in operator: {type:?}")
}
Expand Down Expand Up @@ -199,6 +200,15 @@ impl std::fmt::Display for Error {
"Nested field '{field_name}' not of array type. Actual type: {actual_type:?}"
)
}
Error::ScopeOutOfRange { requested, size } => {
write!(
f,
"Scope out of range. Got '{requested}' but the scope size is '{size}'."
)
}
Error::InternalError(thing) => {
write!(f, "Internal error: {thing}.")
}
}
}
}
Expand Down
35 changes: 32 additions & 3 deletions crates/query-engine/translation/src/translation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,40 @@ pub struct NativeQueryInfo {
/// an alias we generate), and what is their name in the metadata (so we can get
/// their information such as which columns are available for that table).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RootAndCurrentTables {
/// The root (top-most) table in the query.
pub root_table: TableSourceAndReference,
pub struct CurrentTableAndScope {
/// The current table we are processing.
pub current_table: TableSourceAndReference,
/// Named scope of tables ordered from farthest to closest to where we are.
scope: Vec<TableSourceAndReference>,
}

impl CurrentTableAndScope {
pub fn new(current_table: TableSourceAndReference) -> Self {
CurrentTableAndScope {
current_table,
scope: vec![],
}
}
pub fn get(&self, i: usize) -> Result<&TableSourceAndReference, Error> {
if i == 0 {
Ok(&self.current_table)
} else {
self.scope
.get(self.scope.len() - i)
.ok_or(Error::ScopeOutOfRange {
requested: i,
size: self.scope.len() + 1,
})
}
}
pub fn push(&self, table: TableSourceAndReference) -> Self {
let mut scope = self.scope.clone();
scope.push(self.current_table.clone());
CurrentTableAndScope {
current_table: table,
scope,
}
}
}

/// For a table in the query, We'd like to track what is its reference in the query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ pub fn translate(
let predicate_expression = filtering::translate(
env,
state,
&helpers::RootAndCurrentTables {
root_table: table_name_and_reference.clone(),
current_table: table_name_and_reference,
},
&helpers::CurrentTableAndScope::new(table_name_and_reference),
&predicate,
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ pub fn translate(
let predicate_expression = filtering::translate(
env,
state,
&helpers::RootAndCurrentTables {
root_table: table_name_and_reference.clone(),
current_table: table_name_and_reference,
},
&helpers::CurrentTableAndScope::new(table_name_and_reference),
&predicate,
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,7 @@ pub fn translate(
})
.collect::<Result<Vec<sql::ast::Expression>, Error>>()?;

let root_and_current_tables = helpers::RootAndCurrentTables {
root_table: table_name_and_reference.clone(),
current_table: table_name_and_reference,
};
let tables = helpers::CurrentTableAndScope::new(table_name_and_reference);

// Build the `pre_constraint` argument boolean expression.
let pre_predicate_json =
Expand All @@ -179,7 +176,7 @@ pub fn translate(
})?;

let pre_predicate_expression =
filtering::translate(env, state, &root_and_current_tables, &pre_predicate)?;
filtering::translate(env, state, &tables, &pre_predicate)?;

// Build the `post_constraint` argument boolean expression.
let post_predicate_json = arguments.get(&mutation.post_check.argument_name).ok_or(
Expand All @@ -195,7 +192,7 @@ pub fn translate(
})?;

let post_predicate_expression =
filtering::translate(env, state, &root_and_current_tables, &post_predicate)?;
filtering::translate(env, state, &tables, &post_predicate)?;

let check_constraint_alias =
sql::helpers::make_column_alias(sql::helpers::CHECK_CONSTRAINT_FIELD.to_string());
Expand Down
Loading

0 comments on commit 1d28034

Please sign in to comment.