66 * @license Apache-2.0
77 */
88
9- import R from 'ramda' ;
109import cronParser from 'cron-parser' ;
11- import moment from 'moment-timezone' ;
1210import inflection from 'inflection' ;
11+ import moment from 'moment-timezone' ;
12+ import R from 'ramda' ;
1313
14+ import {
15+ buildSqlAndParams as nativeBuildSqlAndParams ,
16+ } from '@cubejs-backend/native' ;
1417import {
1518 FROM_PARTITION_RANGE ,
1619 MAX_SOURCE_ROW_LIMIT ,
17- localTimestampToUtc ,
1820 QueryAlias ,
1921 getEnv ,
22+ localTimestampToUtc ,
2023 timeSeries as timeSeriesBase
2124} from '@cubejs-backend/shared' ;
22- import {
23- buildSqlAndParams as nativeBuildSqlAndParams ,
24- } from '@cubejs-backend/native' ;
2525
2626import { UserError } from '../compiler/UserError' ;
27- import { BaseMeasure } from './BaseMeasure ' ;
27+ import { SqlParser } from '../parser/SqlParser ' ;
2828import { BaseDimension } from './BaseDimension' ;
29- import { BaseSegment } from './BaseSegment' ;
3029import { BaseFilter } from './BaseFilter' ;
3130import { BaseGroupFilter } from './BaseGroupFilter' ;
31+ import { BaseMeasure } from './BaseMeasure' ;
32+ import { BaseSegment } from './BaseSegment' ;
3233import { BaseTimeDimension } from './BaseTimeDimension' ;
34+ import { Granularity } from './Granularity' ;
3335import { ParamAllocator } from './ParamAllocator' ;
3436import { PreAggregations } from './PreAggregations' ;
35- import { SqlParser } from '../parser/SqlParser' ;
36- import { Granularity } from './Granularity' ;
3737
3838const DEFAULT_PREAGGREGATIONS_SCHEMA = 'stb_pre_aggregations' ;
3939
@@ -571,13 +571,10 @@ export class BaseQuery {
571571 * @returns {string }
572572 */
573573 countAllQuery ( sql ) {
574- return `select count(*) ${
575- this . escapeColumnName ( QueryAlias . TOTAL_COUNT )
576- } from (\n${
577- sql
578- } \n) ${
579- this . escapeColumnName ( QueryAlias . ORIGINAL_QUERY )
580- } `;
574+ return `select count(*) ${ this . escapeColumnName ( QueryAlias . TOTAL_COUNT )
575+ } from (\n${ sql
576+ } \n) ${ this . escapeColumnName ( QueryAlias . ORIGINAL_QUERY )
577+ } `;
581578 }
582579
583580 regularAndTimeSeriesRollupQuery ( regularMeasures , multipliedMeasures , cumulativeMeasures , preAggregationForQuery ) {
@@ -967,8 +964,7 @@ export class BaseQuery {
967964 ) . concat (
968965 R . map (
969966 ( [ multiplied , measure ] ) => this . withCubeAliasPrefix (
970- `${
971- this . aliasName ( measure . measure . replace ( '.' , '_' ) )
967+ `${ this . aliasName ( measure . measure . replace ( '.' , '_' ) )
972968 } _cumulative`,
973969 ( ) => this . overTimeSeriesQuery (
974970 multiplied
@@ -983,7 +979,7 @@ export class BaseQuery {
983979 ) ,
984980 )
985981 ) ( cumulativeMeasures )
986- // TODO SELECT *
982+ // TODO SELECT *
987983 ) . concat ( multiStageMembers . map ( m => `SELECT * FROM ${ m . alias } ` ) ) ;
988984 }
989985
@@ -1891,10 +1887,9 @@ export class BaseQuery {
18911887 )
18921888 ) , inlineWhereConditions ) ;
18931889
1894- return `SELECT ${ this . selectAllDimensionsAndMeasures ( measures ) } FROM ${
1895- query
1896- } ${ this . baseWhere ( filters . concat ( inlineWhereConditions ) ) } ` +
1897- ( ! this . safeEvaluateSymbolContext ( ) . ungrouped && this . groupByClause ( ) || '' ) ;
1890+ return `SELECT ${ this . selectAllDimensionsAndMeasures ( measures ) } FROM ${ query
1891+ } ${ this . baseWhere ( filters . concat ( inlineWhereConditions ) ) } ` +
1892+ ( ! this . safeEvaluateSymbolContext ( ) . ungrouped && this . groupByClause ( ) || '' ) ;
18981893 }
18991894
19001895 /**
@@ -1946,14 +1941,11 @@ export class BaseQuery {
19461941 . join ( ', ' ) ;
19471942
19481943 const primaryKeyJoinConditions = primaryKeyDimensions . map ( ( pkd ) => (
1949- `${
1950- this . escapeColumnName ( QueryAlias . AGG_SUB_QUERY_KEYS )
1951- } .${
1952- pkd . aliasName ( )
1953- } = ${
1954- shouldBuildJoinForMeasureSelect
1955- ? `${ this . cubeAlias ( keyCubeName ) } .${ pkd . aliasName ( ) } `
1956- : this . dimensionSql ( pkd )
1944+ `${ this . escapeColumnName ( QueryAlias . AGG_SUB_QUERY_KEYS )
1945+ } .${ pkd . aliasName ( )
1946+ } = ${ shouldBuildJoinForMeasureSelect
1947+ ? `${ this . cubeAlias ( keyCubeName ) } .${ pkd . aliasName ( ) } `
1948+ : this . dimensionSql ( pkd )
19571949 } `
19581950 ) ) . join ( ' AND ' ) ;
19591951
@@ -2026,9 +2018,8 @@ export class BaseQuery {
20262018 'collectSubQueryDimensionsFor'
20272019 )
20282020 ) , inlineWhereConditions ) ;
2029- return `SELECT DISTINCT ${ this . keysSelect ( primaryKeyDimensions ) } FROM ${
2030- query
2031- } ${ this . baseWhere ( filters . concat ( inlineWhereConditions ) ) } `;
2021+ return `SELECT DISTINCT ${ this . keysSelect ( primaryKeyDimensions ) } FROM ${ query
2022+ } ${ this . baseWhere ( filters . concat ( inlineWhereConditions ) ) } `;
20322023 }
20332024
20342025 keysSelect ( primaryKeyDimensions ) {
@@ -3337,7 +3328,7 @@ export class BaseQuery {
33373328 }
33383329 throw new UserError ( 'Output schema is only supported for rollup pre-aggregations' ) ;
33393330 } ,
3340- { inputProps : { } , cache : this . queryCache }
3331+ { inputProps : { } , cache : this . queryCache }
33413332 ) ;
33423333 }
33433334
@@ -3457,14 +3448,18 @@ export class BaseQuery {
34573448 join : '{{ join_type }} JOIN {{ source }} ON {{ condition }}' ,
34583449 cte : '{{ alias }} AS ({{ query | indent(2, true) }})' ,
34593450 time_series_select : 'SELECT date_from::timestamp AS "date_from",\n' +
3460- 'date_to::timestamp AS "date_to" \n' +
3461- 'FROM(\n' +
3462- ' VALUES ' +
3463- '{% for time_item in seria %}' +
3464- '(\'{{ time_item | join(\'\\\', \\\'\') }}\')' +
3465- '{% if not loop.last %}, {% endif %}' +
3466- '{% endfor %}' +
3467- ') AS dates (date_from, date_to)'
3451+ 'date_to::timestamp AS "date_to" \n' +
3452+ 'FROM(\n' +
3453+ ' VALUES ' +
3454+ '{% for time_item in seria %}' +
3455+ '(\'{{ time_item | join(\'\\\', \\\'\') }}\')' +
3456+ '{% if not loop.last %}, {% endif %}' +
3457+ '{% endfor %}' +
3458+ ') AS dates (date_from, date_to)' ,
3459+ time_series_get_range : 'SELECT {{ max_expr }} as {{ quoted_max_name }},\n' +
3460+ '{{ min_expr }} as {{ quoted_min_name }}\n' +
3461+ 'FROM {{ from_prepared }}\n' +
3462+ '{% if filter %}WHERE {{ filter }}{% endif %}'
34683463 } ,
34693464 expressions : {
34703465 column_reference : '{% if table_name %}{{ table_name }}.{% endif %}{{ name }}' ,
@@ -3742,7 +3737,7 @@ export class BaseQuery {
37423737 sql : `${ refreshKeyQuery . nowTimestampSql ( ) } < ${ updateWindow ?
37433738 refreshKeyQuery . addTimestampInterval ( dateTo , updateWindow ) :
37443739 dateTo
3745- } `,
3740+ } `,
37463741 label : originalRefreshKey
37473742 } ] ) ;
37483743 }
@@ -4078,11 +4073,11 @@ export class BaseQuery {
40784073 const filterParamArg = filterParamArgs . filter ( p => {
40794074 const member = p . __member ( ) ;
40804075 return member === filter . measure ||
4081- member === filter . dimension ||
4082- ( aliases [ member ] && (
4083- aliases [ member ] === filter . measure ||
4084- aliases [ member ] === filter . dimension
4085- ) ) ;
4076+ member === filter . dimension ||
4077+ ( aliases [ member ] && (
4078+ aliases [ member ] === filter . measure ||
4079+ aliases [ member ] === filter . dimension
4080+ ) ) ;
40864081 } ) [ 0 ] ;
40874082
40884083 if ( ! filterParamArg ) {
0 commit comments