|
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'; |
@@ -732,6 +733,27 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
732 | 733 | ); |
733 | 734 | } |
734 | 735 |
|
| 736 | + type QueryWithParams = [string, Array<unknown>]; |
| 737 | + |
| 738 | + async function testWithPreAgg( |
| 739 | + preAggregationsDescription: { loadSql: QueryWithParams, invalidateKeyQueries: Array<QueryWithParams> }, |
| 740 | + query: BaseQuery, |
| 741 | + ) { |
| 742 | + const preAggSql = preAggregationsDescription |
| 743 | + .loadSql[0] |
| 744 | + // Without `ON COMMIT DROP` temp tables are session-bound, and can live across multiple transactions |
| 745 | + .replace(/CREATE TABLE (.+) AS SELECT/, 'CREATE TEMP TABLE $1 ON COMMIT DROP AS SELECT'); |
| 746 | + const preAggParams = preAggregationsDescription.loadSql[1]; |
| 747 | + |
| 748 | + const queries = [ |
| 749 | + ...preAggregationsDescription.invalidateKeyQueries, |
| 750 | + [preAggSql, preAggParams], |
| 751 | + query.buildSqlAndParams(), |
| 752 | + ]; |
| 753 | + |
| 754 | + return dbRunner.testQueries(queries); |
| 755 | + } |
| 756 | + |
735 | 757 | it('simple join total', async () => runQueryTest({ |
736 | 758 | measures: [ |
737 | 759 | 'visitors.visitor_revenue', |
@@ -1783,6 +1805,40 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1783 | 1805 | }); |
1784 | 1806 | }); |
1785 | 1807 |
|
| 1808 | + /// Test that query with segment member expression, that references dimension, that is covered by pre-agg |
| 1809 | + /// would _not_ trigger stuff like `path.split is not a function` due to unexpected member expression |
| 1810 | + it('pre-aggregation with segment member expression', async () => { |
| 1811 | + await compiler.compile(); |
| 1812 | + |
| 1813 | + const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, { |
| 1814 | + segments: [ |
| 1815 | + { |
| 1816 | + // eslint-disable-next-line no-new-func |
| 1817 | + expression: new Function( |
| 1818 | + 'visitor_checkins', |
| 1819 | + // eslint-disable-next-line no-template-curly-in-string |
| 1820 | + 'return `${visitor_checkins.source} IS NOT NULL`' |
| 1821 | + ), |
| 1822 | + expressionName: 'source_is_some', |
| 1823 | + // eslint-disable-next-line no-template-curly-in-string |
| 1824 | + definition: '${visitor_checkins.source} IS NOT NULL', |
| 1825 | + cubeName: 'visitor_checkins', |
| 1826 | + }, |
| 1827 | + ], |
| 1828 | + timezone: 'America/Los_Angeles', |
| 1829 | + order: [], |
| 1830 | + preAggregationsSchema: '' |
| 1831 | + }); |
| 1832 | + |
| 1833 | + const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
| 1834 | + |
| 1835 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 1836 | + expect(res).toEqual( |
| 1837 | + // Empty result set, only segments in query |
| 1838 | + [{}] |
| 1839 | + ); |
| 1840 | + }); |
| 1841 | + |
1786 | 1842 | it('join rollup pre-aggregation', async () => { |
1787 | 1843 | await compiler.compile(); |
1788 | 1844 |
|
@@ -1817,21 +1873,17 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1817 | 1873 | const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
1818 | 1874 | console.log(preAggregationsDescription); |
1819 | 1875 |
|
1820 | | - return dbRunner.testQueries(preAggregationsDescription.invalidateKeyQueries.concat([ |
1821 | | - [preAggregationsDescription.loadSql[0].replace('CREATE TABLE', 'CREATE TEMP TABLE'), preAggregationsDescription.loadSql[1]], |
1822 | | - query.buildSqlAndParams() |
1823 | | - ])).then(res => { |
1824 | | - console.log(JSON.stringify(res)); |
1825 | | - expect(res).toEqual( |
1826 | | - [ |
1827 | | - { |
1828 | | - vc__source: 'google', |
1829 | | - visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
1830 | | - visitors__per_visitor_revenue: '100' |
1831 | | - } |
1832 | | - ] |
1833 | | - ); |
1834 | | - }); |
| 1876 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 1877 | + console.log(JSON.stringify(res)); |
| 1878 | + expect(res).toEqual( |
| 1879 | + [ |
| 1880 | + { |
| 1881 | + vc__source: 'google', |
| 1882 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 1883 | + visitors__per_visitor_revenue: '100' |
| 1884 | + } |
| 1885 | + ] |
| 1886 | + ); |
1835 | 1887 | }); |
1836 | 1888 |
|
1837 | 1889 | it('join rollup total pre-aggregation', async () => { |
@@ -1862,22 +1914,15 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL |
1862 | 1914 | const preAggregationsDescription: any = query.preAggregations?.preAggregationsDescription()[0]; |
1863 | 1915 | console.log(preAggregationsDescription); |
1864 | 1916 |
|
1865 | | - return dbRunner.testQueries(preAggregationsDescription.invalidateKeyQueries.concat([ |
1866 | | - [ |
1867 | | - preAggregationsDescription.loadSql[0].replace('CREATE TABLE', 'CREATE TEMP TABLE'), |
1868 | | - preAggregationsDescription.loadSql[1] |
1869 | | - ], |
1870 | | - query.buildSqlAndParams() |
1871 | | - ])).then(res => { |
1872 | | - console.log(JSON.stringify(res)); |
1873 | | - expect(res).toEqual( |
1874 | | - [{ |
1875 | | - vc__source: 'google', |
1876 | | - visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
1877 | | - visitors__visitor_revenue: '100' |
1878 | | - }] |
1879 | | - ); |
1880 | | - }); |
| 1917 | + const res = await testWithPreAgg(preAggregationsDescription, query); |
| 1918 | + console.log(JSON.stringify(res)); |
| 1919 | + expect(res).toEqual( |
| 1920 | + [{ |
| 1921 | + vc__source: 'google', |
| 1922 | + visitors__created_at_day: '2017-01-02T00:00:00.000Z', |
| 1923 | + visitors__visitor_revenue: '100' |
| 1924 | + }] |
| 1925 | + ); |
1881 | 1926 | }); |
1882 | 1927 |
|
1883 | 1928 | it('security context', async () => { |
|
0 commit comments