Skip to content

Commit 0965599

Browse files
committed
8 tests remaining
1 parent b1aad9c commit 0965599

File tree

9 files changed

+57
-13
lines changed

9 files changed

+57
-13
lines changed

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,8 +945,6 @@ describe('SQL Generation', () => {
945945
timezone: 'America/Los_Angeles'
946946
});
947947

948-
console.log(query.buildSqlAndParams());
949-
950948
expect(query.buildSqlAndParams()[0]).toMatch(/HLL_COUNT\.MERGE/);
951949
expect(query.buildSqlAndParams()[0]).toMatch(/HLL_COUNT\.INIT/);
952950
});
@@ -971,7 +969,7 @@ describe('SQL Generation', () => {
971969

972970
console.log(query.buildSqlAndParams());
973971

974-
expect(query.buildSqlAndParams()[0]).toMatch(/OFFSET (\d) LIMIT (\d)/);
972+
expect(query.buildSqlAndParams()[0]).toMatch(/OFFSET (\d)\s+LIMIT (\d)/);
975973
});
976974

977975
it('calculated join', async () => {
@@ -2535,6 +2533,9 @@ describe('SQL Generation', () => {
25352533

25362534
const sqlBuild = query.buildSqlAndParams();
25372535

2536+
console.log(sqlBuild[0]);
2537+
console.log(sqlBuild[1]);
2538+
25382539
expect(sqlBuild[0].includes('America/Los_Angeles')).toEqual(true);
25392540
expect(sqlBuild[1][0]).toEqual(granularityTest.from);
25402541
expect(sqlBuild[1][1]).toEqual(granularityTest.to);

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/base_tools.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ pub trait BaseTools {
4747
) -> Result<Vec<Vec<String>>, CubeError>;
4848
fn get_allocated_params(&self) -> Result<Vec<String>, CubeError>;
4949
fn all_cube_members(&self, path: String) -> Result<Vec<String>, CubeError>;
50+
//===== TODO Move to templates
51+
fn hll_init(&self, sql: String) -> Result<String, CubeError>;
52+
fn hll_merge(&self, sql: String) -> Result<String, CubeError>;
53+
fn hll_cardinality_merge(&self, sql: String) -> Result<String, CubeError>;
54+
fn count_distinct_approx(&self, sql: String) -> Result<String, CubeError>;
5055
}

rust/cubesqlplanner/cubesqlplanner/src/planner/base_measure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl BaseMeasure {
200200

201201
pub fn can_used_as_addictive_in_multplied(&self) -> Result<bool, CubeError> {
202202
let measure_type = self.measure_type();
203-
let res = if measure_type == "countDistinct" {
203+
let res = if measure_type == "countDistinct" || measure_type == "countDistinctApprox" {
204204
true
205205
} else if measure_type == "count" {
206206
if let Some(definition) = &self.definition {

rust/cubesqlplanner/cubesqlplanner/src/planner/planners/multi_stage/member.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,21 @@ pub struct MultiStageMember {
186186
member_type: MultiStageMemberType,
187187
evaluation_node: Rc<MemberSymbol>,
188188
is_ungrupped: bool,
189+
has_aggregates_on_top: bool,
189190
}
190191

191192
impl MultiStageMember {
192193
pub fn new(
193194
member_type: MultiStageMemberType,
194195
evaluation_node: Rc<MemberSymbol>,
195196
is_ungrupped: bool,
197+
has_aggregates_on_top: bool,
196198
) -> Rc<Self> {
197199
Rc::new(Self {
198200
member_type,
199201
evaluation_node,
200202
is_ungrupped,
203+
has_aggregates_on_top,
201204
})
202205
}
203206

@@ -216,4 +219,8 @@ impl MultiStageMember {
216219
pub fn is_ungrupped(&self) -> bool {
217220
self.is_ungrupped
218221
}
222+
223+
pub fn has_aggregates_on_top(&self) -> bool {
224+
self.has_aggregates_on_top
225+
}
219226
}

rust/cubesqlplanner/cubesqlplanner/src/planner/planners/multi_stage/member_query_planner.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,9 @@ impl MultiStageMemberQueryPlanner {
384384

385385
let mut node_factory = SqlNodesFactory::new();
386386
node_factory.set_time_shifts(self.description.state().time_shifts().clone());
387+
if self.description.member().has_aggregates_on_top() {
388+
node_factory.set_count_approx_as_state(true);
389+
}
387390

388391
if cte_query_properties
389392
.full_key_aggregate_measures()?

rust/cubesqlplanner/cubesqlplanner/src/planner/planners/multi_stage_query_planner.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ impl MultiStageQueryPlanner {
173173
)),
174174
time_dimension.member_evaluator(),
175175
true,
176+
false,
176177
),
177178
state.clone(),
178179
vec![],
@@ -196,6 +197,7 @@ impl MultiStageQueryPlanner {
196197
MultiStageMemberType::Leaf(MultiStageLeafMemberType::Measure),
197198
member,
198199
self.query_properties.ungrouped(),
200+
true,
199201
),
200202
state,
201203
vec![],
@@ -336,6 +338,7 @@ impl MultiStageQueryPlanner {
336338
MultiStageMemberType::Inode(inode_member),
337339
member,
338340
self.query_properties.ungrouped(),
341+
false,
339342
),
340343
state.clone(),
341344
input,
@@ -386,6 +389,7 @@ impl MultiStageQueryPlanner {
386389
MultiStageMemberType::Leaf(MultiStageLeafMemberType::Measure),
387390
member.clone(),
388391
self.query_properties.ungrouped(),
392+
false,
389393
),
390394
state.clone(),
391395
vec![],
@@ -435,6 +439,7 @@ impl MultiStageQueryPlanner {
435439
MultiStageMemberType::Inode(multi_stage_member),
436440
member,
437441
is_ungrupped,
442+
false,
438443
),
439444
state.clone(),
440445
input,

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/sql_nodes/factory.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct SqlNodesFactory {
1313
time_shifts: HashMap<String, String>,
1414
ungrouped: bool,
1515
ungrouped_measure: bool,
16+
count_approx_as_state: bool,
1617
render_references: HashMap<String, QualifiedColumnName>,
1718
rendered_as_multiplied_measures: HashSet<String>,
1819
ungrouped_measure_references: HashMap<String, QualifiedColumnName>,
@@ -28,6 +29,7 @@ impl SqlNodesFactory {
2829
time_shifts: HashMap::new(),
2930
ungrouped: false,
3031
ungrouped_measure: false,
32+
count_approx_as_state: false,
3133
render_references: HashMap::new(),
3234
ungrouped_measure_references: HashMap::new(),
3335
cube_name_references: HashMap::new(),
@@ -78,6 +80,10 @@ impl SqlNodesFactory {
7880
self.rolling_window = value;
7981
}
8082

83+
pub fn set_count_approx_as_state(&mut self, value: bool) {
84+
self.count_approx_as_state = value;
85+
}
86+
8187
pub fn set_ungrouped_measure_references(
8288
&mut self,
8389
value: HashMap<String, QualifiedColumnName>,
@@ -161,7 +167,11 @@ impl SqlNodesFactory {
161167
} else if self.rolling_window {
162168
RollingWindowNode::new(input)
163169
} else {
164-
FinalMeasureSqlNode::new(input, self.rendered_as_multiplied_measures.clone())
170+
FinalMeasureSqlNode::new(
171+
input,
172+
self.rendered_as_multiplied_measures.clone(),
173+
self.count_approx_as_state,
174+
)
165175
}
166176
}
167177

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/sql_nodes/final_measure.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ use std::rc::Rc;
1212
pub struct FinalMeasureSqlNode {
1313
input: Rc<dyn SqlNode>,
1414
rendered_as_multiplied_measures: HashSet<String>,
15+
count_approx_as_state: bool,
1516
}
1617

1718
impl FinalMeasureSqlNode {
1819
pub fn new(
1920
input: Rc<dyn SqlNode>,
2021
rendered_as_multiplied_measures: HashSet<String>,
22+
count_approx_as_state: bool,
2123
) -> Rc<Self> {
2224
Rc::new(Self {
2325
input,
2426
rendered_as_multiplied_measures,
27+
count_approx_as_state,
2528
})
2629
}
2730

@@ -60,6 +63,12 @@ impl SqlNode for FinalMeasureSqlNode {
6063

6164
if ev.is_calculated() {
6265
input
66+
} else if ev.measure_type() == "countDistinctApprox" {
67+
if self.count_approx_as_state {
68+
query_tools.base_tools().hll_init(input)?
69+
} else {
70+
query_tools.base_tools().count_distinct_approx(input)?
71+
}
6372
} else if self.is_count_disttinct(ev) {
6473
templates.count_distinct(&input)?
6574
} else {

rust/cubesqlplanner/cubesqlplanner/src/planner/sql_evaluator/sql_nodes/rolling_window.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ impl SqlNode for RollingWindowNode {
3939
templates,
4040
)?;
4141
if m.is_cumulative() {
42-
let aggregate_function = if m.measure_type() == "sum"
43-
|| m.measure_type() == "count"
44-
|| m.measure_type() == "runningTotal"
45-
{
46-
"sum"
42+
if m.measure_type() == "countDistinctApprox" {
43+
query_tools.base_tools().hll_cardinality_merge(input)?
4744
} else {
48-
m.measure_type()
49-
};
45+
let aggregate_function = if m.measure_type() == "sum"
46+
|| m.measure_type() == "count"
47+
|| m.measure_type() == "runningTotal"
48+
{
49+
"sum"
50+
} else {
51+
m.measure_type()
52+
};
5053

51-
format!("{}({})", aggregate_function, input)
54+
format!("{}({})", aggregate_function, input)
55+
}
5256
} else {
5357
input
5458
}

0 commit comments

Comments
 (0)