|
| 1 | +import { getEnv } from '@cubejs-backend/shared'; |
| 2 | +import { prepareJsCompiler } from '../../unit/PrepareCompiler'; |
| 3 | +import { dbRunner } from './PostgresDBRunner'; |
| 4 | + |
| 5 | +describe('Views Join Order using join maps', () => { |
| 6 | + jest.setTimeout(200000); |
| 7 | + |
| 8 | + const { compiler, joinGraph, cubeEvaluator } = prepareJsCompiler( |
| 9 | + // language=JavaScript |
| 10 | + ` |
| 11 | +view(\`View\`, { |
| 12 | + description: 'A view', |
| 13 | + cubes: [ |
| 14 | + { |
| 15 | + join_path: A, |
| 16 | + includes: ['id', 'name', 'd_name'], |
| 17 | + prefix: true, |
| 18 | + }, |
| 19 | + { |
| 20 | + join_path: A.B.C.D, |
| 21 | + includes: ['id', 'name'], |
| 22 | + prefix: true, |
| 23 | + }, |
| 24 | + ], |
| 25 | +}); |
| 26 | +
|
| 27 | +cube('A', { |
| 28 | + sql: \` |
| 29 | + SELECT 1 id, 'a'::text as "name"\`, |
| 30 | + joins: { |
| 31 | + B: { |
| 32 | + relationship: \`one_to_many\`, |
| 33 | + sql: \`\${CUBE.id} = \${B}.fk\`, |
| 34 | + }, |
| 35 | + D: { |
| 36 | + relationship: \`one_to_many\`, |
| 37 | + sql: \`\${CUBE.id} = \${D}.fk\`, |
| 38 | + }, |
| 39 | + }, |
| 40 | + dimensions: { |
| 41 | + id: { |
| 42 | + sql: \`id\`, |
| 43 | + type: \`string\`, |
| 44 | + primaryKey: true, |
| 45 | + }, |
| 46 | + name: { |
| 47 | + sql: \`\${CUBE}."name"\`, |
| 48 | + type: \`string\`, |
| 49 | + }, |
| 50 | + d_name: { |
| 51 | + sql: \`\${D.name}\`, |
| 52 | + type: \`string\`, |
| 53 | + }, |
| 54 | + }, |
| 55 | +}); |
| 56 | +
|
| 57 | +cube('B', { |
| 58 | + sql: \` |
| 59 | + SELECT 2 id, 1 as fk, 'b'::text as "name"\`, |
| 60 | + joins: { |
| 61 | + C: { |
| 62 | + relationship: \`many_to_one\`, |
| 63 | + sql: \`\${CUBE.id} = \${C}.fk\`, |
| 64 | + }, |
| 65 | + }, |
| 66 | +
|
| 67 | + dimensions: { |
| 68 | + id: { |
| 69 | + sql: \`id\`, |
| 70 | + type: \`string\`, |
| 71 | + primaryKey: true, |
| 72 | + }, |
| 73 | + name: { |
| 74 | + sql: \`\${CUBE}."name"\`, |
| 75 | + type: \`string\`, |
| 76 | + }, |
| 77 | + }, |
| 78 | +}); |
| 79 | +
|
| 80 | +cube('C', { |
| 81 | + sql: \` |
| 82 | + SELECT 3 id, 2 as fk, 'c'::text as "name"\`, |
| 83 | + joins: { |
| 84 | + D: { |
| 85 | + relationship: \`many_to_one\`, |
| 86 | + sql: \`\${CUBE.id} = \${D}.fk_d\`, |
| 87 | + }, |
| 88 | + }, |
| 89 | + dimensions: { |
| 90 | + id: { |
| 91 | + sql: \`id\`, |
| 92 | + type: \`string\`, |
| 93 | + primaryKey: true, |
| 94 | + }, |
| 95 | + name: { |
| 96 | + sql: \`\${CUBE}."name"\`, |
| 97 | + type: \`string\`, |
| 98 | + }, |
| 99 | + }, |
| 100 | +}); |
| 101 | +
|
| 102 | +cube('D', { |
| 103 | + sql: \` |
| 104 | + SELECT 4 id, 1 as fk, 1 fk_d, 'd1'::text as "name" |
| 105 | + UNION ALL |
| 106 | + SELECT 5 id, 3 as fk, 3 fk_d, 'd3'::text as "name"\`, |
| 107 | + dimensions: { |
| 108 | + id: { |
| 109 | + sql: \`id\`, |
| 110 | + type: \`string\`, |
| 111 | + primaryKey: true, |
| 112 | + }, |
| 113 | + name: { |
| 114 | + sql: \`\${CUBE}."name"\`, |
| 115 | + type: \`string\`, |
| 116 | + }, |
| 117 | + }, |
| 118 | +}); |
| 119 | + ` |
| 120 | + ); |
| 121 | + |
| 122 | + if (getEnv('nativeSqlPlanner')) { |
| 123 | + it('join order', async () => { |
| 124 | + const [sql, _params] = await dbRunner.runQueryTest({ |
| 125 | + dimensions: [ |
| 126 | + 'View.A_id', |
| 127 | + 'View.A_name', |
| 128 | + 'View.A_d_name', |
| 129 | + ], |
| 130 | + timeDimensions: [], |
| 131 | + segments: [], |
| 132 | + filters: [], |
| 133 | + }, [{ |
| 134 | + view__a_id: 1, |
| 135 | + view__a_name: 'a', |
| 136 | + view__a_d_name: 'd3', |
| 137 | + }], { compiler, joinGraph, cubeEvaluator }); |
| 138 | + |
| 139 | + expect(sql).toMatch(/AS "b"/); |
| 140 | + expect(sql).toMatch(/AS "c"/); |
| 141 | + expect(sql).toMatch(/AS "d"/); |
| 142 | + expect(sql).toMatch(/ON "a".id = "b".fk/); |
| 143 | + expect(sql).toMatch(/ON "b".id = "c".fk/); |
| 144 | + expect(sql).toMatch(/ON "c".id = "d".fk_d/); |
| 145 | + expect(sql).not.toMatch(/ON "a".id = "d".fk/); |
| 146 | + }); |
| 147 | + } else { |
| 148 | + it('join order', async () => { |
| 149 | + const [sql, _params] = await dbRunner.runQueryTest({ |
| 150 | + dimensions: [ |
| 151 | + 'View.A_id', |
| 152 | + 'View.A_name', |
| 153 | + 'View.A_d_name', |
| 154 | + ], |
| 155 | + timeDimensions: [], |
| 156 | + segments: [], |
| 157 | + filters: [], |
| 158 | + total: true, |
| 159 | + }, [{ |
| 160 | + view___a_id: 1, |
| 161 | + view___a_name: 'a', |
| 162 | + view___a_d_name: 'd3', |
| 163 | + }], { compiler, joinGraph, cubeEvaluator }); |
| 164 | + |
| 165 | + expect(sql).toMatch(/AS "b"/); |
| 166 | + expect(sql).toMatch(/AS "c"/); |
| 167 | + expect(sql).toMatch(/AS "d"/); |
| 168 | + expect(sql).toMatch(/ON "a".id = "b".fk/); |
| 169 | + expect(sql).toMatch(/ON "b".id = "c".fk/); |
| 170 | + expect(sql).toMatch(/ON "c".id = "d".fk_d/); |
| 171 | + expect(sql).not.toMatch(/ON "a".id = "d".fk/); |
| 172 | + }); |
| 173 | + } |
| 174 | +}); |
0 commit comments