Skip to content

Commit a56462e

Browse files
authored
chore(sqlplanner): Sql planner another schema refactoring (#9038)
* template engine * in work * in work * multi stage and running total pass tests * update * update * fix(cubeplanner): Fixes to pass the tests * some fixes * views support * evaluation_node refactoring * in work * in work * in work * update * update
1 parent fc7e0c5 commit a56462e

File tree

98 files changed

+4037
-1786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4037
-1786
lines changed

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import cronParser from 'cron-parser';
1111

1212
import moment from 'moment-timezone';
1313
import inflection from 'inflection';
14-
import { FROM_PARTITION_RANGE, inDbTimeZone, MAX_SOURCE_ROW_LIMIT, QueryAlias, getEnv } from '@cubejs-backend/shared';
14+
import { FROM_PARTITION_RANGE, inDbTimeZone, MAX_SOURCE_ROW_LIMIT, QueryAlias, getEnv, timeSeries as timeSeriesBase } from '@cubejs-backend/shared';
1515

1616
import {
1717
buildSqlAndParams as nativeBuildSqlAndParams,
@@ -606,6 +606,11 @@ export class BaseQuery {
606606
}
607607

608608
buildSqlAndParamsRust(exportAnnotatedSql) {
609+
const order = this.options.order && R.pipe(
610+
R.map((hash) => ((!hash || !hash.id) ? null : hash)),
611+
R.reject(R.isNil),
612+
)(this.options.order);
613+
609614
const queryParams = {
610615
measures: this.options.measures,
611616
dimensions: this.options.dimensions,
@@ -614,12 +619,13 @@ export class BaseQuery {
614619
joinRoot: this.join.root,
615620
joinGraph: this.joinGraph,
616621
cubeEvaluator: this.cubeEvaluator,
617-
order: this.options.order,
622+
order,
618623
filters: this.options.filters,
619624
limit: this.options.limit ? this.options.limit.toString() : null,
620625
rowLimit: this.options.rowLimit ? this.options.rowLimit.toString() : null,
621626
offset: this.options.offset ? this.options.offset.toString() : null,
622627
baseTools: this,
628+
ungrouped: this.options.ungrouped
623629

624630
};
625631
const res = nativeBuildSqlAndParams(queryParams);
@@ -628,6 +634,21 @@ export class BaseQuery {
628634
return res;
629635
}
630636

637+
allCubeMembers(path) {
638+
const fromPath = this.cubeEvaluator.cubeFromPath(path);
639+
640+
return Object.keys(fromPath.measures).concat(Object.keys(fromPath.dimensions));
641+
}
642+
643+
getAllocatedParams() {
644+
return this.paramAllocator.getParams();
645+
}
646+
647+
// FIXME helper for native generator, maybe should be moved entire to rust
648+
generateTimeSeries(granularity, dateRange) {
649+
return timeSeriesBase(granularity, dateRange);
650+
}
651+
631652
get shouldReuseParams() {
632653
return false;
633654
}
@@ -3236,7 +3257,16 @@ export class BaseQuery {
32363257
'{% if offset is not none %}\nOFFSET {{ offset }}{% endif %}',
32373258
group_by_exprs: '{{ group_by | map(attribute=\'index\') | join(\', \') }}',
32383259
join: '{{ join_type }} JOIN {{ source }} ON {{ condition }}',
3239-
cte: '{{ alias }} AS ({{ query | indent(2, true) }})'
3260+
cte: '{{ alias }} AS ({{ query | indent(2, true) }})',
3261+
time_series_select: 'SELECT date_from::timestamp AS "date_from",\n' +
3262+
'date_to::timestamp AS "date_to" \n' +
3263+
'FROM(\n' +
3264+
' VALUES ' +
3265+
'{% for time_item in seria %}' +
3266+
'(\'{{ time_item | join(\'\\\', \\\'\') }}\')' +
3267+
'{% if not loop.last %}, {% endif %}' +
3268+
'{% endfor %}' +
3269+
') AS dates (date_from, date_to)'
32403270
},
32413271
expressions: {
32423272
column_reference: '{% if table_name %}{{ table_name }}.{% endif %}{{ name }}',
@@ -3257,6 +3287,8 @@ export class BaseQuery {
32573287
cube: 'CUBE({{ exprs_concat }})',
32583288
negative: '-({{ expr }})',
32593289
not: 'NOT ({{ expr }})',
3290+
add_interval: '{{ date }} + interval \'{{ interval }}\'',
3291+
sub_interval: '{{ date }} - interval \'{{ interval }}\'',
32603292
true: 'TRUE',
32613293
false: 'FALSE',
32623294
like: '{{ expr }} {% if negated %}NOT {% endif %}LIKE {{ pattern }}',
@@ -3276,7 +3308,8 @@ export class BaseQuery {
32763308
gte: '{{ column }} >= {{ param }}',
32773309
lt: '{{ column }} < {{ param }}',
32783310
lte: '{{ column }} <= {{ param }}',
3279-
always_true: '1 == 1'
3311+
like_pattern: '{% if start_wild %}\'%\' || {% endif %}{{ value }}{% if end_wild %}|| \'%\'{% endif %}',
3312+
always_true: '1 = 1'
32803313

32813314
},
32823315
quotes: {

packages/cubejs-schema-compiler/src/adapter/ParamAllocator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class ParamAllocator {
5151
return `$${paramIndex}$`;
5252
}
5353

54+
public getParams() {
55+
return this.params;
56+
}
57+
5458
// eslint-disable-next-line no-unused-vars
5559
protected paramPlaceHolder(paramIndex) {
5660
return '?';

packages/cubejs-schema-compiler/src/compiler/CubeSymbols.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class CubeSymbols {
106106
},
107107
set segments(v) {
108108
// Dont allow to modify
109-
}
109+
},
110110
}, cubeDefinition);
111111

112112
if (cubeDefinition.extends) {

packages/cubejs-schema-compiler/test/integration/clickhouse/ClickHouseDbRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { StartedTestContainer } from 'testcontainers';
55
import { format as formatSql } from 'sqlstring';
66
import { v4 as uuidv4 } from 'uuid';
77
import { ClickHouseQuery } from '../../../src/adapter/ClickHouseQuery';
8-
import { BaseDbRunner } from "../utils/BaseDbRunner";
8+
import { BaseDbRunner } from '../utils/BaseDbRunner';
99

1010
process.env.TZ = 'GMT';
1111

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ describe('SQL Generation', () => {
453453
}
454454
});
455455

456-
it('having filter with operator OR', async () => {
456+
it('having filter with operator OR 1', async () => {
457457
await compiler.compile();
458458

459459
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
@@ -648,7 +648,7 @@ describe('SQL Generation', () => {
648648
});
649649
});
650650

651-
it('where filter with operators OR & AND', async () => {
651+
it('where filter with operators OR & AND 1', async () => {
652652
await compiler.compile();
653653

654654
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('SQL Generation', () => {
6464
offset: 'start'
6565
}
6666
},
67-
revenueRolling3day: {
67+
revenueRollingThreeDay: {
6868
type: 'sum',
6969
sql: 'amount',
7070
rollingWindow: {
@@ -780,7 +780,7 @@ describe('SQL Generation', () => {
780780

781781
it('rolling month', async () => runQueryTest({
782782
measures: [
783-
'visitors.revenueRolling3day'
783+
'visitors.revenueRollingThreeDay'
784784
],
785785
timeDimensions: [{
786786
dimension: 'visitors.created_at',
@@ -792,7 +792,7 @@ describe('SQL Generation', () => {
792792
}],
793793
timezone: 'America/Los_Angeles'
794794
}, [
795-
{ visitors__created_at_week: '2017-01-09T00:00:00.000Z', visitors__revenue_rolling3day: '900' }
795+
{ visitors__created_at_week: '2017-01-09T00:00:00.000Z', visitors__revenue_rolling_three_day: '900' }
796796
]));
797797

798798
it('rolling count', async () => runQueryTest({
@@ -1791,7 +1791,7 @@ describe('SQL Generation', () => {
17911791
]));
17921792

17931793
it(
1794-
'contains filter',
1794+
'contains filter 1',
17951795
() => runQueryTest({
17961796
measures: [],
17971797
dimensions: [
@@ -2583,7 +2583,7 @@ describe('SQL Generation', () => {
25832583
]
25842584
));
25852585

2586-
it('rank measure', async () => runQueryTest(
2586+
it('rank measure 1', async () => runQueryTest(
25872587
{
25882588
measures: ['visitors.revenue_rank'],
25892589
},

packages/cubejs-schema-compiler/test/unit/base-query.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,7 +1596,7 @@ describe('SQL Generation', () => {
15961596
sql: 'product_id',
15971597
type: 'avg',
15981598
filters: [
1599-
{ sql: `{FILTER_PARAMS.Order.category.filter('category')}` }
1599+
{ sql: '{FILTER_PARAMS.Order.category.filter(\'category\')}' }
16001600
]
16011601
}
16021602
],
@@ -1613,7 +1613,7 @@ describe('SQL Generation', () => {
16131613
},
16141614
{
16151615
name: 'proxied',
1616-
sql: `{FILTER_PARAMS.Order.type.filter("x => type = 'online'")}`,
1616+
sql: '{FILTER_PARAMS.Order.type.filter("x => type = \'online\'")}',
16171617
type: 'boolean',
16181618
}
16191619
]

packages/cubejs-schema-compiler/test/unit/pre-aggregations.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ describe('pre-aggregations', () => {
217217
console.log(JSON.stringify(preAggregationsDescription, null, 2));
218218

219219
expect(preAggregationsDescription.length).toEqual(2);
220-
expect(preAggregationsDescription[0].preAggregationId).toEqual("Orders.simple1");
221-
expect(preAggregationsDescription[1].preAggregationId).toEqual("Orders.simple2");
220+
expect(preAggregationsDescription[0].preAggregationId).toEqual('Orders.simple1');
221+
expect(preAggregationsDescription[1].preAggregationId).toEqual('Orders.simple2');
222222
});
223223

224224
// @link https://github.com/cube-js/cube/issues/6623

rust/cubesqlplanner/cubesqlplanner/src/cube_bridge/base_query_options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub struct BaseQueryOptionsStatic {
6060
#[serde(rename = "rowLimit")]
6161
pub row_limit: Option<String>,
6262
pub offset: Option<String>,
63+
pub ungrouped: Option<bool>,
6364
}
6465

6566
#[nativebridge::native_bridge(BaseQueryOptionsStatic)]

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ pub trait BaseTools {
3838
fn filter_group_function(&self) -> Result<Rc<dyn FilterGroup>, CubeError>;
3939
fn timestamp_precision(&self) -> Result<u32, CubeError>;
4040
fn in_db_time_zone(&self, date: String) -> Result<String, CubeError>;
41+
fn generate_time_series(
42+
&self,
43+
granularity: String,
44+
date_range: Vec<String>,
45+
) -> Result<Vec<Vec<String>>, CubeError>;
46+
fn get_allocated_params(&self) -> Result<Vec<String>, CubeError>;
47+
fn all_cube_members(&self, path: String) -> Result<Vec<String>, CubeError>;
4148
}

0 commit comments

Comments
 (0)