|
1 | 1 | import { getEnv } from '@cubejs-backend/shared'; |
2 | 2 | import { UserError } from '../../../src/compiler/UserError'; |
| 3 | +import type { BaseQuery } from '../../../src'; |
3 | 4 | import { PostgresQuery } from '../../../src/adapter/PostgresQuery'; |
4 | 5 | import { BigqueryQuery } from '../../../src/adapter/BigqueryQuery'; |
5 | 6 | import { PrestodbQuery } from '../../../src/adapter/PrestodbQuery'; |
@@ -825,6 +826,27 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
825 | 826 | ); |
826 | 827 | } |
827 | 828 |
|
| 829 | + type QueryWithParams = [string, Array<unknown>]; |
| 830 | + |
| 831 | + async function testWithPreAgg( |
| 832 | + preAggregationsDescription: { loadSql: QueryWithParams, invalidateKeyQueries: Array<QueryWithParams> }, |
| 833 | + query: BaseQuery, |
| 834 | + ) { |
| 835 | + const preAggSql = preAggregationsDescription |
| 836 | + .loadSql[0] |
| 837 | + // Without `ON COMMIT DROP` temp tables are session-bound, and can live across multiple transactions |
| 838 | + .replace(/CREATE TABLE (.+) AS SELECT/, 'CREATE TEMP TABLE $1 ON COMMIT DROP AS SELECT'); |
| 839 | + const preAggParams = preAggregationsDescription.loadSql[1]; |
| 840 | + |
| 841 | + const queries = [ |
| 842 | + ...preAggregationsDescription.invalidateKeyQueries, |
| 843 | + [preAggSql, preAggParams], |
| 844 | + query.buildSqlAndParams(), |
| 845 | + ]; |
| 846 | + |
| 847 | + return dbRunner.testQueries(queries); |
| 848 | + } |
| 849 | + |
828 | 850 | it('simple join total', async () => runQueryTest({ |
829 | 851 | measures: [ |
830 | 852 | 'visitors.visitor_revenue', |
@@ -1978,6 +2000,40 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1978 | 2000 | }); |
1979 | 2001 | }); |
1980 | 2002 |
|
| 2003 | + /// Test that query with segment member expression, that references dimension, that is covered by pre-agg |
| 2004 | + /// would _not_ trigger stuff like `path.split is not a function` due to unexpected member expression |
| 2005 | + it('pre-aggregation with segment member expression', async () => { |
| 2006 | + await compiler.compile(); |
| 2007 | + |
| 2008 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 2009 | + segments: [ |
| 2010 | + { |
| 2011 | + // eslint-disable-next-line no-new-func |
| 2012 | + expression: new Function( |
| 2013 | + 'visitor_checkins', |
| 2014 | + // eslint-disable-next-line no-template-curly-in-string |
| 2015 | + 'return `${visitor_checkins.source} IS NOT NULL`' |
| 2016 | + ), |
| 2017 | + expressionName: 'source_is_some', |
| 2018 | + // eslint-disable-next-line no-template-curly-in-string |
| 2019 | + definition: '${visitor_checkins.source} IS NOT NULL', |
| 2020 | + cubeName: 'visitor_checkins', |
| 2021 | + }, |
| 2022 | + ], |
| 2023 | + timezone: 'America/Los_Angeles', |
| 2024 | + order: [], |
| 2025 | + preAggregationsSchema: '' |
| 2026 | + }); |
| 2027 | + |
| 2028 | + const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
| 2029 | + |
| 2030 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 2031 | + expect(res).toEqual( |
| 2032 | + // Empty result set, only segments in query |
| 2033 | + [{}] |
| 2034 | + ); |
| 2035 | + }); |
| 2036 | + |
1981 | 2037 | it('join rollup pre-aggregation', async () => { |
1982 | 2038 | await compiler.compile(); |
1983 | 2039 |
|
@@ -2012,21 +2068,17 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
2012 | 2068 | const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
2013 | 2069 | console.log(preAggregationsDescription); |
2014 | 2070 |
|
2015 | | - return dbRunner.testQueries(preAggregationsDescription.invalidateKeyQueries.concat([ |
2016 | | - [preAggregationsDescription.loadSql[0].replace('CREATE TABLE', 'CREATE TEMP TABLE'), preAggregationsDescription.loadSql[1]], |
2017 | | - query.buildSqlAndParams() |
2018 | | - ])).then(res => { |
2019 | | - console.log(JSON.stringify(res)); |
2020 | | - expect(res).toEqual( |
2021 | | - [ |
2022 | | - { |
2023 | | - vc__source: 'google', |
2024 | | - visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
2025 | | - visitors__per_visitor_revenue: '100' |
2026 | | - } |
2027 | | - ] |
2028 | | - ); |
2029 | | - }); |
| 2071 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 2072 | + console.log(JSON.stringify(res)); |
| 2073 | + expect(res).toEqual( |
| 2074 | + [ |
| 2075 | + { |
| 2076 | + vc__source: 'google', |
| 2077 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 2078 | + visitors__per_visitor_revenue: '100' |
| 2079 | + } |
| 2080 | + ] |
| 2081 | + ); |
2030 | 2082 | }); |
2031 | 2083 |
|
2032 | 2084 | it('join rollup total pre-aggregation', async () => { |
@@ -2057,22 +2109,15 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
2057 | 2109 | const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
2058 | 2110 | console.log(preAggregationsDescription); |
2059 | 2111 |
|
2060 | | - return dbRunner.testQueries(preAggregationsDescription.invalidateKeyQueries.concat([ |
2061 | | - [ |
2062 | | - preAggregationsDescription.loadSql[0].replace('CREATE TABLE', 'CREATE TEMP TABLE'), |
2063 | | - preAggregationsDescription.loadSql[1] |
2064 | | - ], |
2065 | | - query.buildSqlAndParams() |
2066 | | - ])).then(res => { |
2067 | | - console.log(JSON.stringify(res)); |
2068 | | - expect(res).toEqual( |
2069 | | - [{ |
2070 | | - vc__source: 'google', |
2071 | | - visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
2072 | | - visitors__visitor_revenue: '100' |
2073 | | - }] |
2074 | | - ); |
2075 | | - }); |
| 2112 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 2113 | + console.log(JSON.stringify(res)); |
| 2114 | + expect(res).toEqual( |
| 2115 | + [{ |
| 2116 | + vc__source: 'google', |
| 2117 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 2118 | + visitors__visitor_revenue: '100' |
| 2119 | + }] |
| 2120 | + ); |
2076 | 2121 | }); |
2077 | 2122 |
|
2078 | 2123 | it('security context', async () => { |
|
0 commit comments