Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Gil Mizrahi committed Aug 23, 2024
1 parent a798efd commit 15dc6f9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 70 deletions.
13 changes: 1 addition & 12 deletions crates/query-engine/translation/src/translation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ impl CurrentTableAndScope {
scope: vec![],
}
}
pub fn get(&self, scope_index: usize) -> Result<&TableSourceAndReference, Error> {
if scope_index == 0 {
Ok(&self.current_table)
} else {
self.scope
.get(self.scope.len() - scope_index)
.ok_or(Error::ScopeOutOfRange {
requested: scope_index,
size: self.scope.len() + 1,
})
}
}
/// Rewind scope to get scope in index.
pub fn get_scope(&self, scope_index: usize) -> Result<CurrentTableAndScope, Error> {
if scope_index == 0 {
Expand All @@ -101,6 +89,7 @@ impl CurrentTableAndScope {
})
}
}
#[must_use]
pub fn push(&self, table: TableSourceAndReference) -> Self {
let mut scope = self.scope.clone();
scope.push(self.current_table.clone());
Expand Down
86 changes: 41 additions & 45 deletions crates/query-engine/translation/src/translation/query/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,16 @@ pub fn translate_expression_with_joins(
joins.extend(left_joins);

match value {
// todo!(): fix all of this
models::ComparisonValue::Column {
name,
field_path,
path,
scope,
} => {
if !path.is_empty() {
todo!("comparison value with path not supported");
}
if scope.is_some() {
todo!("comparison value with scope not supported");
}

let (right, right_joins) = translate_comparison_target(
env,
state,
tables,
&models::ComparisonTarget::Column {
name: name.clone(),
field_path: field_path.clone(),
},
let (right, right_joins) = translate_comparison_value_column(
env, state, tables, name, field_path, path, scope,
)?;

joins.extend(right_joins);

let right = vec![make_unnest_subquery(state, right)];
Expand Down Expand Up @@ -438,8 +425,7 @@ fn translate_comparison_target(
match column {
models::ComparisonTarget::Aggregate { .. } => todo!(),
models::ComparisonTarget::Column { name, field_path } => {
let (table_ref, joins) =
translate_comparison_pathelements(env, state, tables, &vec![])?;
let (table_ref, joins) = translate_comparison_pathelements(env, state, tables, &[])?;

// get the unrelated table information from the metadata.
let collection_info = env.lookup_fields_info(&table_ref.source)?;
Expand Down Expand Up @@ -468,37 +454,12 @@ fn translate_comparison_value(
typ: &database::Type,
) -> Result<(sql::ast::Expression, Vec<sql::ast::Join>), Error> {
match value {
// todo!() fix these paths
models::ComparisonValue::Column {
name,
field_path,
path,
scope,
} => {
// get the scope.
let (table_ref, joins) = match *scope {
Some(scope_index) if scope_index > 0 => {
let tables = tables.get_scope(scope_index)?;
translate_comparison_pathelements(env, state, &tables, &path)?
}
_ => translate_comparison_pathelements(env, state, tables, &path)?,
};

// get the unrelated table information from the metadata.
let collection_info = env.lookup_fields_info(&table_ref.source)?;
let ColumnInfo { name, .. } = collection_info.lookup_column(name)?;

Ok((
wrap_in_field_path(
&field_path.into(),
sql::ast::Expression::ColumnReference(sql::ast::ColumnReference::TableColumn {
table: table_ref.reference.clone(),
name,
}),
),
joins,
))
}
} => translate_comparison_value_column(env, state, tables, name, field_path, path, scope),
models::ComparisonValue::Scalar { value: json_value } => {
Ok((values::translate(env, state, json_value, typ)?, vec![]))
}
Expand All @@ -509,6 +470,41 @@ fn translate_comparison_value(
}
}

/// translate a comparison value column.
fn translate_comparison_value_column(
env: &Env,
state: &mut State,
tables: &CurrentTableAndScope,
name: &models::FieldName,
field_path: &Option<Vec<models::FieldName>>,
path: &[models::PathElement],
scope: &Option<usize>,
) -> Result<(sql::ast::Expression, Vec<sql::ast::Join>), Error> {
// get the scope.
let (table_ref, joins) = match *scope {
Some(scope_index) if scope_index > 0 => {
let tables = tables.get_scope(scope_index)?;
translate_comparison_pathelements(env, state, &tables, path)?
}
_ => translate_comparison_pathelements(env, state, tables, path)?,
};

// get the unrelated table information from the metadata.
let collection_info = env.lookup_fields_info(&table_ref.source)?;
let ColumnInfo { name, .. } = collection_info.lookup_column(name)?;

Ok((
wrap_in_field_path(
&field_path.into(),
sql::ast::Expression::ColumnReference(sql::ast::ColumnReference::TableColumn {
table: table_ref.reference.clone(),
name,
}),
),
joins,
))
}

/// Translate an EXISTS clause into a SQL subquery of the following form:
///
/// > EXISTS (SELECT 1 as 'one' FROM <table> AS <alias> WHERE <predicate>)
Expand Down Expand Up @@ -549,7 +545,7 @@ pub fn translate_exists_in_collection(

let new_tables = tables.push(TableSourceAndReference {
reference: table.reference.clone(),
source: table.source.clone(),
source: table.source,
});

let (expr, expr_joins) =
Expand Down
21 changes: 8 additions & 13 deletions crates/query-engine/translation/src/translation/query/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ struct Column(models::FieldName);
/// An aggregate operation to select from a table used in an order by.
#[derive(Debug)]
enum Aggregate {
CountStarAggregate,
CountStar,
SingleColumnCount {
column: models::FieldName,
distinct: bool,
},
SingleColumnAggregate {
SingleColumn {
column: models::FieldName,
function: models::AggregateFunctionName,
},
Expand Down Expand Up @@ -181,12 +181,12 @@ fn group_elements(elements: &[models::OrderByElement]) -> Vec<OrderByElementGrou
i,
path,
element.order_direction,
Aggregate::SingleColumnAggregate {
Aggregate::SingleColumn {
column: column.clone(),
function: function.clone(),
},
),
)
);
}
}
models::Aggregate::ColumnCount {
Expand All @@ -211,17 +211,12 @@ fn group_elements(elements: &[models::OrderByElement]) -> Vec<OrderByElementGrou
distinct: *distinct,
},
),
)
);
}
}
models::Aggregate::StarCount {} => aggregate_element_groups.insert(
hash_path(path),
(
i,
path,
element.order_direction,
Aggregate::CountStarAggregate,
),
(i, path, element.order_direction, Aggregate::CountStar),
),
},
}
Expand Down Expand Up @@ -722,7 +717,7 @@ fn translate_targets(
.iter()
.map(|element| {
match &element.element {
Aggregate::CountStarAggregate => {
Aggregate::CountStar => {
let column_alias = sql::helpers::make_column_alias("count".to_string());
Ok(OrderBySelectExpression {
index: element.index,
Expand Down Expand Up @@ -764,7 +759,7 @@ fn translate_targets(
}),
})
}
Aggregate::SingleColumnAggregate { column, function } => {
Aggregate::SingleColumn { column, function } => {
let selected_column = target_collection.lookup_column(column)?;
// we are going to deliberately use the table column name and not an alias we get from
// the query request because this is internal to the sorting mechanism.
Expand Down

0 comments on commit 15dc6f9

Please sign in to comment.