|
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'; |
@@ -739,6 +740,27 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
739 | 740 | ); |
740 | 741 | } |
741 | 742 |
|
| 743 | + type QueryWithParams = [string, Array<unknown>]; |
| 744 | + |
| 745 | + async function testWithPreAgg( |
| 746 | + preAggregationsDescription: { loadSql: QueryWithParams, invalidateKeyQueries: Array<QueryWithParams> }, |
| 747 | + query: BaseQuery, |
| 748 | + ) { |
| 749 | + const preAggSql = preAggregationsDescription |
| 750 | + .loadSql[0] |
| 751 | + // Without `ON COMMIT DROP` temp tables are session-bound, and can live across multiple transactions |
| 752 | + .replace(/CREATE TABLE (.+) AS SELECT/, 'CREATE TEMP TABLE $1 ON COMMIT DROP AS SELECT'); |
| 753 | + const preAggParams = preAggregationsDescription.loadSql[1]; |
| 754 | + |
| 755 | + const queries = [ |
| 756 | + ...preAggregationsDescription.invalidateKeyQueries, |
| 757 | + [preAggSql, preAggParams], |
| 758 | + query.buildSqlAndParams(), |
| 759 | + ]; |
| 760 | + |
| 761 | + return dbRunner.testQueries(queries); |
| 762 | + } |
| 763 | + |
742 | 764 | it('simple join total', async () => runQueryTest({ |
743 | 765 | measures: [ |
744 | 766 | 'visitors.visitor_revenue', |
@@ -1892,6 +1914,40 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1892 | 1914 | }); |
1893 | 1915 | }); |
1894 | 1916 |
|
| 1917 | + /// Test that query with segment member expression, that references dimension, that is covered by pre-agg |
| 1918 | + /// would _not_ trigger stuff like `path.split is not a function` due to unexpected member expression |
| 1919 | + it('pre-aggregation with segment member expression', async () => { |
| 1920 | + await compiler.compile(); |
| 1921 | + |
| 1922 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 1923 | + segments: [ |
| 1924 | + { |
| 1925 | + // eslint-disable-next-line no-new-func |
| 1926 | + expression: new Function( |
| 1927 | + 'visitor_checkins', |
| 1928 | + // eslint-disable-next-line no-template-curly-in-string |
| 1929 | + 'return `${visitor_checkins.source} IS NOT NULL`' |
| 1930 | + ), |
| 1931 | + expressionName: 'source_is_some', |
| 1932 | + // eslint-disable-next-line no-template-curly-in-string |
| 1933 | + definition: '${visitor_checkins.source} IS NOT NULL', |
| 1934 | + cubeName: 'visitor_checkins', |
| 1935 | + }, |
| 1936 | + ], |
| 1937 | + timezone: 'America/Los_Angeles', |
| 1938 | + order: [], |
| 1939 | + preAggregationsSchema: '' |
| 1940 | + }); |
| 1941 | + |
| 1942 | + const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
| 1943 | + |
| 1944 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 1945 | + expect(res).toEqual( |
| 1946 | + // Empty result set, only segments in query |
| 1947 | + [{}] |
| 1948 | + ); |
| 1949 | + }); |
| 1950 | + |
1895 | 1951 | it('join rollup pre-aggregation', async () => { |
1896 | 1952 | await compiler.compile(); |
1897 | 1953 |
|
@@ -1926,21 +1982,17 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1926 | 1982 | const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
1927 | 1983 | console.log(preAggregationsDescription); |
1928 | 1984 |
|
1929 | | - return dbRunner.testQueries(preAggregationsDescription.invalidateKeyQueries.concat([ |
1930 | | - [preAggregationsDescription.loadSql[0].replace('CREATE TABLE', 'CREATE TEMP TABLE'), preAggregationsDescription.loadSql[1]], |
1931 | | - query.buildSqlAndParams() |
1932 | | - ])).then(res => { |
1933 | | - console.log(JSON.stringify(res)); |
1934 | | - expect(res).toEqual( |
1935 | | - [ |
1936 | | - { |
1937 | | - vc__source: 'google', |
1938 | | - visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
1939 | | - visitors__per_visitor_revenue: '100' |
1940 | | - } |
1941 | | - ] |
1942 | | - ); |
1943 | | - }); |
| 1985 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 1986 | + console.log(JSON.stringify(res)); |
| 1987 | + expect(res).toEqual( |
| 1988 | + [ |
| 1989 | + { |
| 1990 | + vc__source: 'google', |
| 1991 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 1992 | + visitors__per_visitor_revenue: '100' |
| 1993 | + } |
| 1994 | + ] |
| 1995 | + ); |
1944 | 1996 | }); |
1945 | 1997 |
|
1946 | 1998 | it('join rollup total pre-aggregation', async () => { |
@@ -1971,22 +2023,15 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1971 | 2023 | const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
1972 | 2024 | console.log(preAggregationsDescription); |
1973 | 2025 |
|
1974 | | - return dbRunner.testQueries(preAggregationsDescription.invalidateKeyQueries.concat([ |
1975 | | - [ |
1976 | | - preAggregationsDescription.loadSql[0].replace('CREATE TABLE', 'CREATE TEMP TABLE'), |
1977 | | - preAggregationsDescription.loadSql[1] |
1978 | | - ], |
1979 | | - query.buildSqlAndParams() |
1980 | | - ])).then(res => { |
1981 | | - console.log(JSON.stringify(res)); |
1982 | | - expect(res).toEqual( |
1983 | | - [{ |
1984 | | - vc__source: 'google', |
1985 | | - visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
1986 | | - visitors__visitor_revenue: '100' |
1987 | | - }] |
1988 | | - ); |
1989 | | - }); |
| 2026 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 2027 | + console.log(JSON.stringify(res)); |
| 2028 | + expect(res).toEqual( |
| 2029 | + [{ |
| 2030 | + vc__source: 'google', |
| 2031 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 2032 | + visitors__visitor_revenue: '100' |
| 2033 | + }] |
| 2034 | + ); |
1990 | 2035 | }); |
1991 | 2036 |
|
1992 | 2037 | it('security context', async () => { |
|
0 commit comments