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,12 +571,9 @@ 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 )
574+ return `select count(*) ${ this . escapeColumnName ( QueryAlias . TOTAL_COUNT )
575+ } from (\n${ sql
576+ } \n) ${ this . escapeColumnName ( QueryAlias . ORIGINAL_QUERY )
580577 } `;
581578 }
582579
@@ -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
@@ -1956,10 +1952,9 @@ export class BaseQuery {
19561952 )
19571953 ) , inlineWhereConditions ) ;
19581954
1959- return `SELECT ${ this . selectAllDimensionsAndMeasures ( measures ) } FROM ${
1960- query
1955+ return `SELECT ${ this . selectAllDimensionsAndMeasures ( measures ) } FROM ${ query
19611956 } ${ this . baseWhere ( filters . concat ( inlineWhereConditions ) ) } ` +
1962- ( ! this . safeEvaluateSymbolContext ( ) . ungrouped && this . groupByClause ( ) || '' ) ;
1957+ ( ! this . safeEvaluateSymbolContext ( ) . ungrouped && this . groupByClause ( ) || '' ) ;
19631958 }
19641959
19651960 /**
@@ -2011,14 +2006,11 @@ export class BaseQuery {
20112006 . join ( ', ' ) ;
20122007
20132008 const primaryKeyJoinConditions = primaryKeyDimensions . map ( ( pkd ) => (
2014- `${
2015- this . escapeColumnName ( QueryAlias . AGG_SUB_QUERY_KEYS )
2016- } .${
2017- pkd . aliasName ( )
2018- } = ${
2019- shouldBuildJoinForMeasureSelect
2020- ? `${ this . cubeAlias ( keyCubeName ) } .${ pkd . aliasName ( ) } `
2021- : this . dimensionSql ( pkd )
2009+ `${ this . escapeColumnName ( QueryAlias . AGG_SUB_QUERY_KEYS )
2010+ } .${ pkd . aliasName ( )
2011+ } = ${ shouldBuildJoinForMeasureSelect
2012+ ? `${ this . cubeAlias ( keyCubeName ) } .${ pkd . aliasName ( ) } `
2013+ : this . dimensionSql ( pkd )
20222014 } `
20232015 ) ) . join ( ' AND ' ) ;
20242016
@@ -2110,8 +2102,7 @@ export class BaseQuery {
21102102 'collectSubQueryDimensionsFor'
21112103 )
21122104 ) , inlineWhereConditions ) ;
2113- return `SELECT DISTINCT ${ this . keysSelect ( primaryKeyDimensions ) } FROM ${
2114- query
2105+ return `SELECT DISTINCT ${ this . keysSelect ( primaryKeyDimensions ) } FROM ${ query
21152106 } ${ this . baseWhere ( filters . concat ( inlineWhereConditions ) ) } `;
21162107 }
21172108
@@ -3425,7 +3416,7 @@ export class BaseQuery {
34253416 }
34263417 throw new UserError ( 'Output schema is only supported for rollup pre-aggregations' ) ;
34273418 } ,
3428- { inputProps : { } , cache : this . queryCache }
3419+ { inputProps : { } , cache : this . queryCache }
34293420 ) ;
34303421 }
34313422
@@ -3545,14 +3536,18 @@ export class BaseQuery {
35453536 join : '{{ join_type }} JOIN {{ source }} ON {{ condition }}' ,
35463537 cte : '{{ alias }} AS ({{ query | indent(2, true) }})' ,
35473538 time_series_select : 'SELECT date_from::timestamp AS "date_from",\n' +
3548- 'date_to::timestamp AS "date_to" \n' +
3549- 'FROM(\n' +
3550- ' VALUES ' +
3551- '{% for time_item in seria %}' +
3552- '(\'{{ time_item | join(\'\\\', \\\'\') }}\')' +
3553- '{% if not loop.last %}, {% endif %}' +
3554- '{% endfor %}' +
3555- ') AS dates (date_from, date_to)'
3539+ 'date_to::timestamp AS "date_to" \n' +
3540+ 'FROM(\n' +
3541+ ' VALUES ' +
3542+ '{% for time_item in seria %}' +
3543+ '(\'{{ time_item | join(\'\\\', \\\'\') }}\')' +
3544+ '{% if not loop.last %}, {% endif %}' +
3545+ '{% endfor %}' +
3546+ ') AS dates (date_from, date_to)' ,
3547+ time_series_get_range : 'SELECT {{ max_expr }} as {{ quoted_max_name }},\n' +
3548+ '{{ min_expr }} as {{ quoted_min_name }}\n' +
3549+ 'FROM {{ from_prepared }}\n' +
3550+ '{% if filter %}WHERE {{ filter }}{% endif %}'
35563551 } ,
35573552 expressions : {
35583553 column_reference : '{% if table_name %}{{ table_name }}.{% endif %}{{ name }}' ,
@@ -4166,11 +4161,11 @@ export class BaseQuery {
41664161 const filterParamArg = filterParamArgs . filter ( p => {
41674162 const member = p . __member ( ) ;
41684163 return member === filter . measure ||
4169- member === filter . dimension ||
4170- ( aliases [ member ] && (
4171- aliases [ member ] === filter . measure ||
4172- aliases [ member ] === filter . dimension
4173- ) ) ;
4164+ member === filter . dimension ||
4165+ ( aliases [ member ] && (
4166+ aliases [ member ] === filter . measure ||
4167+ aliases [ member ] === filter . dimension
4168+ ) ) ;
41744169 } ) [ 0 ] ;
41754170
41764171 if ( ! filterParamArg ) {
0 commit comments