Skip to content

Commit fe70d7f

Browse files
authored
Use a stack for aggregate expressions. (#561)
1 parent 933f4b4 commit fe70d7f

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- *BREAKING* partiql-eval: Fixed behavior of comparison and `BETWEEN` operations w.r.t. type mismatches
1515
- *BREAKING* partiql-eval: `BindEvalExpr::bind` takes `self` rather than `&self`
1616
- *BREAKING* Changed modeling of Ion Literals to be evaluated to Boxed Variants rather than eagerly transformed to PartiQL Values.
17+
- *BREAKING* partiql-logical: Change the modeling of `ProjectAll`
18+
- Fix some query evaluation edges cases.
1719

1820
### Added
1921
- partiql-value: Pretty-printing of `Value` via `ToPretty` trait

extension/partiql-extension-csv/tests/scan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use partiql_eval::eval::BasicContext;
77
use partiql_eval::plan::EvaluationMode;
88
use partiql_extension_csv::CsvExtension;
99
use partiql_parser::{Parsed, ParserResult};
10-
use partiql_value::{bag, tuple, DateTime, Value};
10+
use partiql_value::{DateTime, Value};
1111
use std::path::PathBuf;
1212

1313
#[track_caller]
@@ -120,7 +120,7 @@ fn join() {
120120
INNER JOIN scan_csv('{pets}') as pets \
121121
ON people.Pet=pets.Pet"
122122
);
123-
let (result, errs) = evaluate_with_csv_scan(&query, &None);
123+
let (result, _) = evaluate_with_csv_scan(&query, &None);
124124

125125
insta::assert_snapshot!(result);
126126
}

partiql-logical-planner/src/lower.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub struct AstToLogical<'a> {
169169
arg_stack: Vec<Vec<CallArgument>>,
170170
path_stack: Vec<Vec<PathComponent>>,
171171
sort_stack: Vec<Vec<logical::SortSpec>>,
172-
aggregate_exprs: Vec<AggregateExpression>,
172+
aggregate_exprs: Vec<Vec<AggregateExpression>>,
173173
projection_renames: Vec<FnvIndexMap<String, BindingsName<'a>>>,
174174

175175
aliases: FnvIndexMap<NodeId, SymbolPrimitive>,
@@ -493,6 +493,7 @@ impl<'a> AstToLogical<'a> {
493493
#[inline]
494494
fn enter_q(&mut self) {
495495
self.q_stack.push(Default::default());
496+
self.aggregate_exprs.push(Default::default());
496497
self.ctx_stack.push(QueryContext::Query);
497498
self.projection_renames.push(Default::default());
498499
}
@@ -501,6 +502,7 @@ impl<'a> AstToLogical<'a> {
501502
fn exit_q(&mut self) -> QueryClauses {
502503
self.projection_renames.pop().expect("q level");
503504
self.ctx_stack.pop().expect("q level");
505+
self.aggregate_exprs.pop().expect("q level");
504506
self.q_stack.pop().expect("q level")
505507
}
506508

@@ -852,7 +854,8 @@ impl<'ast> Visitor<'ast> for AstToLogical<'_> {
852854
fn exit_select(&mut self, _select: &'ast Select) -> Traverse {
853855
// PartiQL permits SQL aggregations without a GROUP BY (e.g. SELECT SUM(t.a) FROM ...)
854856
// What follows adds a GROUP BY clause with the rewrite `... GROUP BY true AS $__gk`
855-
if !self.aggregate_exprs.is_empty() && self.current_clauses_mut().group_by_clause.is_none()
857+
if !self.aggregate_exprs.last().unwrap().is_empty()
858+
&& self.current_clauses_mut().group_by_clause.is_none()
856859
{
857860
let exprs = HashMap::from([(
858861
"$__gk".to_string(),
@@ -861,7 +864,7 @@ impl<'ast> Visitor<'ast> for AstToLogical<'_> {
861864
let group_by: BindingsOp = BindingsOp::GroupBy(logical::GroupBy {
862865
strategy: logical::GroupingStrategy::GroupFull,
863866
exprs,
864-
aggregate_exprs: self.aggregate_exprs.clone(),
867+
aggregate_exprs: self.aggregate_exprs.last().unwrap().clone(),
865868
group_as_alias: None,
866869
});
867870
let id = self.curr_plan().add_operator(group_by);
@@ -1367,7 +1370,7 @@ impl<'ast> Visitor<'ast> for AstToLogical<'_> {
13671370
}
13681371
}
13691372
};
1370-
self.aggregate_exprs.push(agg_expr);
1373+
self.aggregate_exprs.last_mut().unwrap().push(agg_expr);
13711374
Traverse::Continue
13721375
}
13731376

@@ -1680,8 +1683,8 @@ impl<'ast> Visitor<'ast> for AstToLogical<'_> {
16801683
Traverse::Continue
16811684
}
16821685

1683-
fn exit_group_by_expr(&mut self, _group_by_expr: &'ast GroupByExpr) -> Traverse {
1684-
let aggregate_exprs = self.aggregate_exprs.clone();
1686+
fn exit_group_by_expr(&mut self, group_by_expr: &'ast GroupByExpr) -> Traverse {
1687+
let aggregate_exprs = self.aggregate_exprs.last().unwrap().clone();
16851688
let benv = self.exit_benv();
16861689
if !benv.is_empty() {
16871690
{
@@ -1691,12 +1694,12 @@ impl<'ast> Visitor<'ast> for AstToLogical<'_> {
16911694
let env = self.exit_env();
16921695
true_or_fault!(self, env.len().is_even(), "env.len() is not even");
16931696

1694-
let group_as_alias = _group_by_expr
1697+
let group_as_alias = group_by_expr
16951698
.group_as_alias
16961699
.as_ref()
16971700
.map(|SymbolPrimitive { value, case: _ }| value.clone());
16981701

1699-
let strategy = match _group_by_expr.strategy {
1702+
let strategy = match group_by_expr.strategy {
17001703
None => logical::GroupingStrategy::GroupFull,
17011704
Some(GroupingStrategy::GroupFull) => logical::GroupingStrategy::GroupFull,
17021705
Some(GroupingStrategy::GroupPartial) => logical::GroupingStrategy::GroupPartial,

0 commit comments

Comments
 (0)