use super::*;
use hawdb_cypher::ArithmeticOp;
pub(super) fn plan_composed_returns(
scope: &BTreeSet<String>,
columns: &BTreeSet<String>,
items: &[ReturnItem],
parameters: &BTreeMap<String, Value>,
) -> Result<PlannedReturns> {
let aggregate = items
.iter()
.any(|item| contains_aggregate(&item.expression));
let mut binder = ComposedReturnBinder {
scope,
columns,
parameters,
aggregate,
literal_names: false,
group_keys: Vec::new(),
aggregations: Vec::new(),
};
let projections = items
.iter()
.map(|item| {
let (expression, name) = binder.bind(&item.expression)?;
Ok(Projection {
expression,
name: item.alias.clone().unwrap_or(name),
})
})
.collect::<Result<Vec<_>>>()?;
if aggregate {
Ok(PlannedReturns::AggregateProjection {
group_keys: binder.group_keys,
items: binder.aggregations,
projections,
})
} else {
Ok(PlannedReturns::Projections(projections))
}
}
pub(super) fn bind_aggregate_argument(
scope: &BTreeSet<String>,
columns: &BTreeSet<String>,
argument: &ReturnExpression,
parameters: &BTreeMap<String, Value>,
) -> Result<(ProjectionExpression, String)> {
if contains_aggregate(argument) {
return Err(HawDBError::Semantic(
"aggregate function arguments cannot contain aggregate functions".to_string(),
));
}
ComposedReturnBinder {
scope,
columns,
parameters,
aggregate: false,
literal_names: true,
group_keys: Vec::new(),
aggregations: Vec::new(),
}
.bind(argument)
}
pub(super) fn bind_composed_expression(
scope: &BTreeSet<String>,
columns: &BTreeSet<String>,
expression: &ReturnExpression,
parameters: &BTreeMap<String, Value>,
) -> Result<ProjectionExpression> {
ComposedReturnBinder {
scope,
columns,
parameters,
aggregate: false,
literal_names: false,
group_keys: Vec::new(),
aggregations: Vec::new(),
}
.bind(expression)
.map(|(expression, _)| expression)
}
fn literal_name(value: &ValueExpression) -> Option<String> {
Some(match &value.kind {
ValueExpressionKind::Parameter(name) => format!("${name}"),
ValueExpressionKind::Literal(Value::Null) => "null".to_string(),
ValueExpressionKind::Literal(Value::Bool(value)) => value.to_string(),
ValueExpressionKind::Literal(Value::Int(value)) => value.to_string(),
ValueExpressionKind::Literal(Value::Float(value)) => format!("{value:?}"),
ValueExpressionKind::Literal(Value::String(value)) => {
format!("'{}'", value.replace('\\', "\\\\").replace('\'', "\\'"))
}
_ => return None,
})
}
pub(super) fn contains_aggregate(expression: &ReturnExpression) -> bool {
match &expression.kind {
ReturnExpressionKind::Aggregate(_) => true,
ReturnExpressionKind::Arithmetic { first, rest } => {
contains_aggregate(first) || rest.iter().any(|(_, child)| contains_aggregate(child))
}
ReturnExpressionKind::ListFunction { arguments, .. } => {
arguments.iter().any(contains_aggregate)
}
ReturnExpressionKind::Value(_) | ReturnExpressionKind::Path(_) => false,
}
}
struct ComposedReturnBinder<'a> {
scope: &'a BTreeSet<String>,
columns: &'a BTreeSet<String>,
parameters: &'a BTreeMap<String, Value>,
aggregate: bool,
literal_names: bool,
group_keys: Vec<Projection>,
aggregations: Vec<Aggregation>,
}
impl ComposedReturnBinder<'_> {
fn bind(&mut self, expression: &ReturnExpression) -> Result<(ProjectionExpression, String)> {
match &expression.kind {
ReturnExpressionKind::Arithmetic { first, rest } => {
let (mut bound, mut name) = self.bind(first)?;
for (op, right) in rest {
let (right, right_name) = self.bind(right)?;
let (op, symbol) = match op {
ArithmeticOp::Add => (ScalarBinaryOp::Add, "+"),
ArithmeticOp::Subtract => (ScalarBinaryOp::Subtract, "-"),
ArithmeticOp::Multiply => (ScalarBinaryOp::Multiply, "*"),
ArithmeticOp::Divide => (ScalarBinaryOp::Divide, "/"),
ArithmeticOp::Remainder => (ScalarBinaryOp::Remainder, "%"),
};
bound = ProjectionExpression::Binary {
left: Box::new(bound),
op,
right: Box::new(right),
};
name = format!("({name} {symbol} {right_name})");
}
Ok((bound, name))
}
ReturnExpressionKind::ListFunction {
function,
arguments,
} => {
let mut bound = Vec::with_capacity(arguments.len());
let mut names = Vec::with_capacity(arguments.len());
for argument in arguments {
let (argument, name) = self.bind(argument)?;
bound.push(argument);
names.push(name);
}
Ok((
ProjectionExpression::ListFunction {
function: *function,
arguments: bound,
},
format!("{}({})", function.function_name(), names.join(", ")),
))
}
ReturnExpressionKind::Aggregate(_) => {
let item = AstNode::synthetic(ReturnItemKind {
expression: expression.clone(),
alias: None,
});
let mut aggregation = plan_aggregation_with_columns(
self.scope,
self.columns,
&item,
self.parameters,
)?;
let name = aggregation.name.clone();
aggregation.name = format!("\0aggregate.{}", self.aggregations.len());
let column = ProjectionExpression::Column(aggregation.name.clone());
self.aggregations.push(aggregation);
Ok((column, name))
}
ReturnExpressionKind::Value(value) => {
let item = AstNode::synthetic(ReturnItemKind {
expression: expression.clone(),
alias: None,
});
let mut projection =
plan_projection_with_columns(self.scope, self.columns, &item, self.parameters)?;
let name = match &value.kind {
ScalarExpressionKind::Value(literal) if self.literal_names => {
literal_name(literal).unwrap_or_else(|| projection.name.clone())
}
_ => projection.name.clone(),
};
if self.aggregate
&& !scalar_expression_is_scoped(value, &BTreeSet::new(), &BTreeSet::new())
{
projection.name = format!("\0group.{}", self.group_keys.len());
let column = ProjectionExpression::Column(projection.name.clone());
self.group_keys.push(projection);
Ok((column, name))
} else {
Ok((projection.expression, name))
}
}
ReturnExpressionKind::Path(path) => self.bind(&AstNode {
kind: ReturnExpressionKind::Value(AstNode {
kind: ScalarExpressionKind::Path(path.clone()),
span: expression.span,
}),
span: expression.span,
}),
}
}
}
pub(super) fn scalar_expression_is_scoped(
expression: &ScalarExpression,
scope: &BTreeSet<String>,
column_names: &BTreeSet<String>,
) -> bool {
match &expression.kind {
ScalarExpressionKind::Variable(variable)
| ScalarExpressionKind::Property { variable, .. }
| ScalarExpressionKind::DefaultIfNullOrEq { variable, .. }
| ScalarExpressionKind::DefaultIfNull { variable, .. }
| ScalarExpressionKind::CasePropertyNotNullOrEq { variable, .. }
| ScalarExpressionKind::CasePropertyEqualsRank { variable, .. }
| ScalarExpressionKind::CaseLowerPropertyDefault { variable, .. }
| ScalarExpressionKind::CaseCoalesceDifferenceFloorZero { variable, .. } => {
column_names.contains(variable) || scope.contains(variable)
}
ScalarExpressionKind::Case { .. }
| ScalarExpressionKind::VectorDistance { .. }
| ScalarExpressionKind::Binary { .. }
| ScalarExpressionKind::Not(_)
| ScalarExpressionKind::IsNull { .. }
| ScalarExpressionKind::ListFunction { .. }
| ScalarExpressionKind::StringFunction { .. }
| ScalarExpressionKind::List(_)
| ScalarExpressionKind::ListIndex { .. }
| ScalarExpressionKind::Conversion { .. }
| ScalarExpressionKind::NumericFunction { .. }
| ScalarExpressionKind::ListSlice { .. }
| ScalarExpressionKind::TemporalFunction { .. }
| ScalarExpressionKind::PropertyAccess { .. }
| ScalarExpressionKind::MapLiteral(_) => expression
.kind
.all_children(|child| scalar_expression_is_scoped(child, scope, column_names)),
ScalarExpressionKind::Reduce {
accumulator,
initial,
variable,
list,
expression,
} => {
let mut body_columns = column_names.clone();
body_columns.insert(accumulator.clone());
body_columns.insert(variable.clone());
scalar_expression_is_scoped(initial, scope, column_names)
&& scalar_expression_is_scoped(list, scope, column_names)
&& scalar_expression_is_scoped(expression, scope, &body_columns)
}
ScalarExpressionKind::ListComprehension {
variable,
list,
predicate,
projection,
} => {
let mut body_columns = column_names.clone();
body_columns.insert(variable.clone());
scalar_expression_is_scoped(list, scope, column_names)
&& predicate
.iter()
.chain(projection.iter())
.all(|child| scalar_expression_is_scoped(child, scope, &body_columns))
}
ScalarExpressionKind::Value(_) => true,
ScalarExpressionKind::Coalesce(expressions) => expressions
.iter()
.all(|expression| scalar_expression_is_scoped(expression, scope, column_names)),
ScalarExpressionKind::Left { expression, .. }
| ScalarExpressionKind::Lower(expression)
| ScalarExpressionKind::JsonValue { expression, .. } => {
scalar_expression_is_scoped(expression, scope, column_names)
}
ScalarExpressionKind::Path(path) => column_names.contains(path.path_variable()),
ScalarExpressionKind::Id(_)
| ScalarExpressionKind::RelationshipType(_)
| ScalarExpressionKind::RelationshipEndpoint { .. }
| ScalarExpressionKind::DatePart { .. }
| ScalarExpressionKind::PatternPredicate(_)
| ScalarExpressionKind::PatternComprehension { .. } => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn bind(query: &str) -> PlannedReturns {
let pipeline = hawdb_cypher::parse_pipeline(query).unwrap();
let hawdb_cypher::ClauseKind::Return(projection) = &pipeline.clauses.last().unwrap().kind
else {
panic!("expected RETURN");
};
plan_return_items_with_columns(
&BTreeSet::from(["n".to_string()]),
&BTreeSet::new(),
&projection.items,
&BTreeMap::new(),
)
.unwrap()
}
#[test]
fn aggregate_arithmetic_keeps_constants_out_of_grouping_and_hides_internal_columns() {
let bound = bind("MATCH (n:Item) RETURN COUNT(n) + 1 AS total");
assert_eq!(bound.names(), vec!["total"]);
let PlannedReturns::AggregateProjection {
group_keys,
items,
projections,
} = bound
else {
panic!("expected aggregate projection")
};
assert!(group_keys.is_empty());
assert_eq!(items.len(), 1);
assert_eq!(projections.len(), 1);
let ProjectionExpression::Binary { left, op, right } = &projections[0].expression else {
panic!("expected arithmetic")
};
assert_eq!(*op, ScalarBinaryOp::Add);
assert_eq!(
left.as_ref(),
&ProjectionExpression::Column(items[0].name.clone())
);
assert_eq!(
right.as_ref(),
&ProjectionExpression::Literal(Value::Int(1))
);
}
#[test]
fn arithmetic_preserves_grouping_and_public_projection_order() {
let bound = bind("MATCH (n:Item) RETURN COUNT(n) + 2 AS total, n.category AS category, n.weight + COUNT(n) AS weighted");
assert_eq!(bound.names(), vec!["total", "category", "weighted"]);
let PlannedReturns::AggregateProjection {
group_keys,
items,
projections,
} = bound
else {
panic!("expected aggregate projection")
};
assert_eq!(group_keys.len(), 2);
assert_eq!(items.len(), 2);
assert_eq!(
projections[1].expression,
ProjectionExpression::Column(group_keys[0].name.clone())
);
assert_eq!(
group_keys[0].expression,
ProjectionExpression::Property {
variable: "n".to_string(),
property: "category".to_string()
}
);
assert_eq!(
group_keys[1].expression,
ProjectionExpression::Property {
variable: "n".to_string(),
property: "weight".to_string()
}
);
}
#[test]
fn scalar_arithmetic_is_left_associative_with_multiplication_precedence() {
let PlannedReturns::Projections(projections) = bind("RETURN 20 - 3 * 2 - 4 AS value")
else {
panic!("expected project")
};
let expression = &projections[0].expression;
let ProjectionExpression::Binary { left, op, right } = expression else {
panic!("expected arithmetic")
};
assert_eq!(*op, ScalarBinaryOp::Subtract);
assert_eq!(
right.as_ref(),
&ProjectionExpression::Literal(Value::Int(4))
);
let ProjectionExpression::Binary { right, op, .. } = left.as_ref() else {
panic!("expected left-associative subtraction")
};
assert_eq!(*op, ScalarBinaryOp::Subtract);
assert!(matches!(
right.as_ref(),
ProjectionExpression::Binary {
op: ScalarBinaryOp::Multiply,
..
}
));
}
}