Skip to content

Commit edc7222

Browse files
authored
chore(query): allow group by constant literal (#17711)
* chore(query): allow group by constant literal * chore(query): allow group by constant literal
1 parent 19dabc8 commit edc7222

File tree

5 files changed

+43
-33
lines changed

5 files changed

+43
-33
lines changed

src/query/sql/src/planner/binder/aggregate.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,23 +879,24 @@ impl Binder {
879879
self.metadata.clone(),
880880
&[],
881881
);
882+
882883
let (mut scalar_expr, _) = scalar_binder
883884
.bind(expr)
884885
.or_else(|e| self.resolve_alias_item(bind_context, expr, available_aliases, e))?;
885886

886887
let mut analyzer = SetReturningAnalyzer::new(bind_context, self.metadata.clone());
887888
analyzer.visit(&mut scalar_expr)?;
888-
889889
if collect_grouping_sets && !grouping_sets.last().unwrap().contains(&scalar_expr) {
890890
grouping_sets.last_mut().unwrap().push(scalar_expr.clone());
891891
}
892892

893+
// Was add by previous
894+
// The group key is duplicated
893895
if bind_context
894896
.aggregate_info
895897
.group_items_map
896898
.contains_key(&scalar_expr)
897899
{
898-
// The group key is duplicated
899900
continue;
900901
}
901902

@@ -974,6 +975,7 @@ impl Binder {
974975
.insert(item.scalar.clone(), i);
975976
}
976977
bind_context.aggregate_info.group_items = results;
978+
977979
Ok(())
978980
}
979981

src/query/sql/src/planner/binder/scalar_common.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ pub fn prune_by_children(scalar: &ScalarExpr, columns: &HashSet<ScalarExpr>) ->
217217
self.can_prune = false;
218218
Ok(())
219219
}
220+
221+
fn visit_constant(&mut self, _constant: &'a crate::plans::ConstantExpr) -> Result<()> {
222+
self.can_prune = false;
223+
Ok(())
224+
}
220225
}
221226

222227
let mut visitor = PruneVisitor::new(columns);

tests/sqllogictests/suites/base/03_common/03_0003_select_group_by.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ SELECT ( null, to_hour(to_timestamp(3501857592331)), number::Date) from numbers(
299299
(NULL,18,'1970-01-02')
300300
(NULL,18,'1970-01-03')
301301

302+
query TTT rowsort
303+
SELECT TRY_CAST('1900-12-30 12:00:00' AS TIMESTAMP) AS "TEMP(Test)(4058757556)(0)",
304+
1.1000000000000001 AS "$__alias__0"
305+
FROM numbers(10)
306+
GROUP BY "$__alias__0";
307+
----
308+
1900-12-30 12:00:00.000000 1.1000000000000001
309+
302310
query TT rowsort
303311
select to_string(to_bitmap(number)), to_string(to_bitmap(number+3)) FROM numbers(3) GROUP BY GROUPING SETS ((to_bitmap(number), to_bitmap(number+3)))
304312
----

tests/sqllogictests/suites/mode/standalone/explain/aggregate.test

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,36 @@ AggregateFinal
4444
└── estimated rows: 10.00
4545

4646

47+
## TODO optimize group by constant
4748
query T
48-
explain select 1 from numbers(10) group by 1;
49+
explain select 1 a from numbers(10) group by a;
4950
----
5051
EvalScalar
51-
├── output columns: [1 (#2)]
52-
├── expressions: [1]
52+
├── output columns: [a (#2)]
53+
├── expressions: [group_item (#1)]
5354
├── estimated rows: 10.00
54-
└── TableScan
55-
├── table: default.system.numbers
56-
├── output columns: []
57-
├── read rows: 10
58-
├── read size: < 1 KiB
59-
├── partitions total: 1
60-
├── partitions scanned: 1
61-
├── push downs: [filters: [], limit: NONE]
62-
└── estimated rows: 10.00
63-
55+
└── AggregateFinal
56+
├── output columns: [a (#1)]
57+
├── group by: [a]
58+
├── aggregate functions: []
59+
├── estimated rows: 10.00
60+
└── AggregatePartial
61+
├── group by: [a]
62+
├── aggregate functions: []
63+
├── estimated rows: 10.00
64+
└── EvalScalar
65+
├── output columns: [a (#1)]
66+
├── expressions: [1]
67+
├── estimated rows: 10.00
68+
└── TableScan
69+
├── table: default.system.numbers
70+
├── output columns: []
71+
├── read rows: 10
72+
├── read size: < 1 KiB
73+
├── partitions total: 1
74+
├── partitions scanned: 1
75+
├── push downs: [filters: [], limit: NONE]
76+
└── estimated rows: 10.00
6477

6578

6679
query T

tests/sqllogictests/suites/mode/standalone/explain_native/aggregate.test

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,6 @@ AggregateFinal
4444
└── estimated rows: 10.00
4545

4646

47-
query T
48-
explain select 1 from numbers(10) group by 1;
49-
----
50-
EvalScalar
51-
├── output columns: [1 (#2)]
52-
├── expressions: [1]
53-
├── estimated rows: 10.00
54-
└── TableScan
55-
├── table: default.system.numbers
56-
├── output columns: []
57-
├── read rows: 10
58-
├── read size: < 1 KiB
59-
├── partitions total: 1
60-
├── partitions scanned: 1
61-
├── push downs: [filters: [], limit: NONE]
62-
└── estimated rows: 10.00
63-
64-
6547

6648
query T
6749
explain select 1, number, number + 1, number -1 from numbers(10) group by number, abs(number), cast(number as int);

0 commit comments

Comments
 (0)