Skip to content

Commit e79bedd

Browse files
committed
in work
1 parent 725abcf commit e79bedd

File tree

26 files changed

+494
-124
lines changed

26 files changed

+494
-124
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4214,7 +4214,7 @@ export class BaseQuery {
42144214
'{{ min_expr }} as {{ quoted_min_name }}\n' +
42154215
'FROM {{ from_prepared }}\n' +
42164216
'{% if filter %}WHERE {{ filter }}{% endif %}',
4217-
calc_groups_join: 'SELECT {{ original_cube }}.*, ' +
4217+
/* calc_groups_join: 'SELECT {{ original_cube }}.*, ' +
42184218
'{%for single_value in single_values %}' +
42194219
'{{ single_value.value }} as {{ single_value.name }}{% if not loop.last %}, {% endif %}' +
42204220
'{% endfor %}' +
@@ -4231,6 +4231,16 @@ export class BaseQuery {
42314231
'{% if not loop.last %} UNION ALL\n{% endif %}' +
42324232
'{% endfor %}' +
42334233
') {{ group.alias }}\n' +
4234+
'{% endfor %}', */
4235+
calc_groups_join: '{% if original_sql %}{{ original_sql }}\n{% endif %}' +
4236+
'{% for group in groups %}' +
4237+
'{% if original_sql or not loop.first %}CROSS JOIN\n{% endif %}' +
4238+
'(\n' +
4239+
'{% for value in group.values %}' +
4240+
'SELECT {{ value }} as {{ group.name }}' +
4241+
'{% if not loop.last %} UNION ALL\n{% endif %}' +
4242+
'{% endfor %}' +
4243+
') {{ group.alias }}\n' +
42344244
'{% endfor %}'
42354245
},
42364246
expressions: {

packages/cubejs-schema-compiler/test/integration/postgres/calc-groups.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ views:
436436
`);
437437

438438
if (getEnv('nativeSqlPlanner')) {
439-
it('basic cross join', async () => dbRunner.runQueryTest({
439+
it('basic cross join 1', async () => dbRunner.runQueryTest({
440440
dimensions: ['orders.currency'],
441441
timeDimensions: [
442442
{

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/calc_groups_cross_join.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::planner::sql_evaluator::MemberSymbol;
33
use super::*;
44
use cubenativeutils::CubeError;
55
use std::rc::Rc;
6+
use typed_builder::TypedBuilder;
67

78
#[derive(Clone)]
89
pub struct CalcGroupDescription {
@@ -20,15 +21,20 @@ impl PrettyPrint for CalcGroupDescription {
2021
}
2122
}
2223

23-
logical_source_enum!(
24-
CalcGroupsCrossJoinSource,
25-
[LogicalJoin, FullKeyAggregate, PreAggregation]
26-
);
27-
28-
#[derive(Clone)]
24+
#[derive(Clone, TypedBuilder)]
2925
pub struct CalcGroupsCrossJoin {
30-
pub source: CalcGroupsCrossJoinSource,
31-
pub calc_groups: Vec<Rc<CalcGroupDescription>>,
26+
source: BaseQuerySource,
27+
calc_groups: Vec<Rc<CalcGroupDescription>>,
28+
}
29+
30+
impl CalcGroupsCrossJoin {
31+
pub fn source(&self) -> &BaseQuerySource {
32+
&self.source
33+
}
34+
35+
pub fn calc_groups(&self) -> &Vec<Rc<CalcGroupDescription>> {
36+
&self.calc_groups
37+
}
3238
}
3339

3440
impl LogicalNode for CalcGroupsCrossJoin {

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/cube.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@ impl OriginalSqlPreAggregation {
1717

1818
impl PrettyPrint for OriginalSqlPreAggregation {
1919
fn pretty_print(&self, result: &mut PrettyPrintResult, state: &PrettyPrintState) {
20-
result.println(&format!("OriginalSqlPreAggregation: {}", self.name()), state);
20+
result.println(
21+
&format!("OriginalSqlPreAggregation: {}", self.name()),
22+
state,
23+
);
2124
}
2225
}
2326

2427
#[derive(Clone, TypedBuilder)]
2528
pub struct Cube {
26-
name: String,
2729
cube: Rc<BaseCube>,
2830
#[builder(default)]
2931
original_sql_pre_aggregation: Option<OriginalSqlPreAggregation>,
3032
}
3133

3234
impl Cube {
3335
pub fn name(&self) -> &String {
34-
&self.name
36+
&self.cube.name()
3537
}
3638

3739
pub fn cube(&self) -> &Rc<BaseCube> {
@@ -54,21 +56,19 @@ impl PrettyPrint for Cube {
5456

5557
impl Cube {
5658
pub fn new(cube: Rc<BaseCube>) -> Rc<Self> {
57-
Rc::new(Self::builder()
58-
.name(cube.name().clone())
59-
.cube(cube)
60-
.build())
59+
Rc::new(Self::builder().cube(cube).build())
6160
}
6261

6362
pub fn with_original_sql_pre_aggregation(
6463
self: Rc<Self>,
6564
original_sql_pre_aggregation: OriginalSqlPreAggregation,
6665
) -> Rc<Self> {
67-
Rc::new(Self::builder()
68-
.name(self.name().clone())
69-
.cube(self.cube().clone())
70-
.original_sql_pre_aggregation(Some(original_sql_pre_aggregation))
71-
.build())
66+
Rc::new(
67+
Self::builder()
68+
.cube(self.cube().clone())
69+
.original_sql_pre_aggregation(Some(original_sql_pre_aggregation))
70+
.build(),
71+
)
7272
}
7373
}
7474

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/filter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::pretty_print::*;
22
use crate::plan::{Filter, FilterItem};
33
use itertools::Itertools;
44

5+
#[derive(Default)]
56
pub struct LogicalFilter {
67
pub dimensions_filters: Vec<FilterItem>,
78
pub time_dimensions_filters: Vec<FilterItem>,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use itertools::Itertools;
2+
3+
use crate::planner::sql_evaluator::MemberSymbol;
4+
5+
use super::*;
6+
use std::rc::Rc;
7+
pub fn all_symbols(schema: &Rc<LogicalSchema>, filters: &LogicalFilter) -> Vec<Rc<MemberSymbol>> {
8+
let mut symbols = schema.all_members().cloned().collect_vec();
9+
10+
if let Some(dim_filter) = filters.all_filters() {
11+
symbols.extend(dim_filter.all_member_evaluators().iter().cloned());
12+
}
13+
if let Some(meas_filter) = filters.measures_filter() {
14+
symbols.extend(meas_filter.all_member_evaluators().iter().cloned());
15+
}
16+
symbols
17+
}

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/logical_node.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub enum PlanNode {
2626
MeasureSubquery(Rc<MeasureSubquery>),
2727
DimensionSubQuery(Rc<DimensionSubQuery>),
2828
KeysSubQuery(Rc<KeysSubQuery>),
29-
CalcGroupsCrossJoin(Rc<CalcGroupsCrossJoin>),
3029
MultiStageGetDateRange(Rc<MultiStageGetDateRange>),
3130
MultiStageLeafMeasure(Rc<MultiStageLeafMeasure>),
3231
MultiStageMeasureCalculation(Rc<MultiStageMeasureCalculation>),
@@ -49,7 +48,6 @@ macro_rules! match_plan_node {
4948
PlanNode::MeasureSubquery($node) => $block,
5049
PlanNode::DimensionSubQuery($node) => $block,
5150
PlanNode::KeysSubQuery($node) => $block,
52-
PlanNode::CalcGroupsCrossJoin($node) => $block,
5351
PlanNode::MultiStageGetDateRange($node) => $block,
5452
PlanNode::MultiStageLeafMeasure($node) => $block,
5553
PlanNode::MultiStageMeasureCalculation($node) => $block,

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#[macro_use]
22
mod logical_source;
33
mod aggregate_multiplied_subquery;
4-
mod calc_groups_cross_join;
54
mod cube;
65
mod dimension_subquery;
76
mod filter;
87
mod full_key_aggregate;
8+
mod helper;
99
mod join;
1010
mod keys_subquery;
1111
mod logical_node;
@@ -22,11 +22,11 @@ mod schema;
2222
pub mod visitor;
2323

2424
pub use aggregate_multiplied_subquery::*;
25-
pub use calc_groups_cross_join::*;
2625
pub use cube::*;
2726
pub use dimension_subquery::*;
2827
pub use filter::*;
2928
pub use full_key_aggregate::*;
29+
pub use helper::*;
3030
pub use join::*;
3131
pub use keys_subquery::*;
3232
pub use logical_node::*;

rust/cubesqlplanner/cubesqlplanner/src/logical_plan/query_source.rs

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,74 @@ use super::*;
22
use cubenativeutils::CubeError;
33
use std::rc::Rc;
44

5-
logical_source_enum!(QuerySource, [LogicalJoin, FullKeyAggregate, PreAggregation]);
5+
logical_source_enum!(
6+
QuerySource,
7+
[LogicalJoin, FullKeyAggregate, PreAggregation]
8+
);
9+
10+
/* #[derive(Clone)]
11+
pub enum QuerySource {
12+
CalcGroupsCrossJoin(Rc<CalcGroupsCrossJoin>),
13+
BaseSource(BaseQuerySource),
14+
}
15+
16+
impl QuerySource {
17+
pub fn base_source(&self) -> &BaseQuerySource {
18+
match &self {
19+
QuerySource::CalcGroupsCrossJoin(cross_join) => cross_join.source(),
20+
QuerySource::BaseSource(base) => base,
21+
}
22+
}
23+
}
24+
25+
impl LogicalSource for QuerySource {
26+
fn as_plan_node(&self) -> PlanNode {
27+
match &self {
28+
QuerySource::CalcGroupsCrossJoin(item) => item.as_plan_node(),
29+
QuerySource::BaseSource(base) => base.as_plan_node(),
30+
}
31+
}
32+
fn with_plan_node(&self, plan_node: PlanNode) -> Result<Self, CubeError> {
33+
Ok(match &self {
34+
QuerySource::CalcGroupsCrossJoin(_) => {
35+
QuerySource::CalcGroupsCrossJoin(plan_node.into_logical_node()?)
36+
}
37+
QuerySource::BaseSource(base) => {
38+
QuerySource::BaseSource(base.with_plan_node(plan_node)?)
39+
}
40+
})
41+
}
42+
}
43+
44+
impl PrettyPrint for QuerySource {
45+
fn pretty_print(&self, result: &mut PrettyPrintResult, state: &PrettyPrintState) {
46+
match &self {
47+
QuerySource::CalcGroupsCrossJoin(item) => item.pretty_print(result, state),
48+
QuerySource::BaseSource(base) => base.pretty_print(result, state),
49+
}
50+
}
51+
}
52+
53+
impl From<Rc<CalcGroupsCrossJoin>> for QuerySource {
54+
fn from(value: Rc<CalcGroupsCrossJoin>) -> Self {
55+
Self::CalcGroupsCrossJoin(value)
56+
}
57+
}
58+
59+
impl From<Rc<LogicalJoin>> for QuerySource {
60+
fn from(value: Rc<LogicalJoin>) -> Self {
61+
Self::BaseSource(value.into())
62+
}
63+
}
64+
65+
impl From<Rc<FullKeyAggregate>> for QuerySource {
66+
fn from(value: Rc<FullKeyAggregate>) -> Self {
67+
Self::BaseSource(value.into())
68+
}
69+
}
70+
71+
impl From<Rc<PreAggregation>> for QuerySource {
72+
fn from(value: Rc<PreAggregation>) -> Self {
73+
Self::BaseSource(value.into())
74+
}
75+
} */

rust/cubesqlplanner/cubesqlplanner/src/physical_plan_builder/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(super) struct PushDownBuilderContext {
1818
pub dimensions_query: bool,
1919
pub measure_subquery: bool,
2020
pub multi_stage_schemas: HashMap<String, Rc<Schema>>,
21+
pub calc_groups_resolved: bool,
2122
}
2223

2324
impl PushDownBuilderContext {

0 commit comments

Comments
 (0)