Skip to content

Commit cc4b678

Browse files
committed
use table schemas
1 parent 5cacc82 commit cc4b678

File tree

5 files changed

+90
-97
lines changed

5 files changed

+90
-97
lines changed

meerkat-browser/src/browser-cube-to-sql-with-resolution/browser-cube-to-sql-with-resolution.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {
2-
constructDimensionsNameMap,
3-
constructMeasuresNameMap,
42
ContextParams,
53
createBaseTableSchema,
64
generateResolutionJoinPaths,
@@ -43,31 +41,26 @@ export const cubeQueryToSQLWithResolution = async ({
4341
return baseSql;
4442
}
4543

46-
const columnNameMap = {
47-
...constructMeasuresNameMap(tableSchemas),
48-
...constructDimensionsNameMap(tableSchemas),
49-
};
50-
5144
// Create a table schema for the base query.
5245
const baseTable: TableSchema = createBaseTableSchema(
5346
baseSql,
54-
columnNameMap,
47+
tableSchemas,
5548
resolutionConfig,
5649
query.measures,
5750
query.dimensions
5851
);
5952

6053
const resolutionSchemas: TableSchema[] = generateResolutionSchemas(
6154
resolutionConfig,
62-
columnNameMap
55+
tableSchemas
6356
);
6457

6558
const resolveParams: CubeQueryToSQLParams = {
6659
connection: connection,
6760
query: {
6861
measures: [],
6962
dimensions: generateResolvedDimensions(query, resolutionConfig),
70-
joinPaths: generateResolutionJoinPaths(resolutionConfig, columnNameMap),
63+
joinPaths: generateResolutionJoinPaths(resolutionConfig, tableSchemas),
7164
},
7265
tableSchemas: [baseTable, ...resolutionSchemas],
7366
};

meerkat-core/src/resolution/resolution.spec.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import {
2-
constructDimensionsNameMap,
3-
constructMeasuresNameMap,
42
createBaseTableSchema,
53
generateResolutionJoinPaths,
64
generateResolutionSchemas,
@@ -37,10 +35,6 @@ describe('Create base table schema', () => {
3735
],
3836
},
3937
];
40-
const columnNameMap = {
41-
...constructMeasuresNameMap(tableSchemas),
42-
...constructDimensionsNameMap(tableSchemas),
43-
};
4438
const resolutionConfig: ResolutionConfig = {
4539
columnConfigs: [],
4640
tableSchemas: [],
@@ -50,7 +44,7 @@ describe('Create base table schema', () => {
5044

5145
const baseTableSchema = createBaseTableSchema(
5246
sql,
53-
columnNameMap,
47+
tableSchemas,
5448
resolutionConfig,
5549
measures,
5650
dimensions
@@ -105,7 +99,6 @@ describe('Create base table schema', () => {
10599
],
106100
},
107101
];
108-
const columnNameMap = constructDimensionsNameMap(tableSchemas);
109102
const resolutionConfig = {
110103
columnConfigs: [
111104
{
@@ -127,7 +120,7 @@ describe('Create base table schema', () => {
127120

128121
const baseTableSchema = createBaseTableSchema(
129122
sql,
130-
columnNameMap,
123+
tableSchemas,
131124
resolutionConfig,
132125
[],
133126
dimensions
@@ -178,7 +171,6 @@ describe('Create base table schema', () => {
178171
],
179172
},
180173
];
181-
const columnNameMap = constructDimensionsNameMap(tableSchemas);
182174
const resolutionConfig = {
183175
columnConfigs: [],
184176
tableSchemas: [],
@@ -188,7 +180,7 @@ describe('Create base table schema', () => {
188180
expect(() => {
189181
createBaseTableSchema(
190182
sql,
191-
columnNameMap,
183+
tableSchemas,
192184
resolutionConfig,
193185
[],
194186
dimensions
@@ -219,7 +211,6 @@ describe('Create base table schema', () => {
219211
],
220212
},
221213
];
222-
const columnNameMap = constructDimensionsNameMap(tableSchemas);
223214
const resolutionConfig = {
224215
columnConfigs: [
225216
{
@@ -241,7 +232,7 @@ describe('Create base table schema', () => {
241232

242233
const baseTableSchema = createBaseTableSchema(
243234
sql,
244-
columnNameMap,
235+
tableSchemas,
245236
resolutionConfig,
246237
[],
247238
dimensions
@@ -298,7 +289,6 @@ describe('Generate resolution schemas', () => {
298289
],
299290
},
300291
];
301-
const columnNameMap = constructDimensionsNameMap(baseTableSchemas);
302292

303293
const resolutionConfig = {
304294
columnConfigs: [
@@ -341,7 +331,10 @@ describe('Generate resolution schemas', () => {
341331
],
342332
};
343333

344-
const schemas = generateResolutionSchemas(resolutionConfig, columnNameMap);
334+
const schemas = generateResolutionSchemas(
335+
resolutionConfig,
336+
baseTableSchemas
337+
);
345338

346339
expect(schemas).toEqual([
347340
{
@@ -422,7 +415,7 @@ describe('Generate resolution schemas', () => {
422415
};
423416

424417
expect(() => {
425-
generateResolutionSchemas(resolutionConfig, {});
418+
generateResolutionSchemas(resolutionConfig, []);
426419
}).toThrow('Table schema not found for resolution_table1');
427420
});
428421

@@ -459,7 +452,7 @@ describe('Generate resolution schemas', () => {
459452
};
460453

461454
expect(() => {
462-
generateResolutionSchemas(resolutionConfig, {});
455+
generateResolutionSchemas(resolutionConfig, []);
463456
}).toThrow('Dimension not found: display_id');
464457
});
465458

@@ -494,7 +487,7 @@ describe('Generate resolution schemas', () => {
494487
],
495488
};
496489

497-
const schemas = generateResolutionSchemas(resolutionConfig, {});
490+
const schemas = generateResolutionSchemas(resolutionConfig, []);
498491
expect(schemas).toEqual([
499492
{
500493
name: 'base_table__column1',
@@ -528,7 +521,6 @@ describe('Generate resolution schemas', () => {
528521
],
529522
},
530523
];
531-
const columnNameMap = constructDimensionsNameMap(baseTableSchemas);
532524

533525
const resolutionConfig = {
534526
columnConfigs: [
@@ -562,7 +554,10 @@ describe('Generate resolution schemas', () => {
562554
],
563555
};
564556

565-
const schemas = generateResolutionSchemas(resolutionConfig, columnNameMap);
557+
const schemas = generateResolutionSchemas(
558+
resolutionConfig,
559+
baseTableSchemas
560+
);
566561
expect(schemas).toEqual([
567562
{
568563
name: 'base_table__column1',
@@ -665,7 +660,7 @@ describe('Generate resolution join paths', () => {
665660
tableSchemas: [],
666661
};
667662

668-
const joinPaths = generateResolutionJoinPaths(resolutionConfig, {});
663+
const joinPaths = generateResolutionJoinPaths(resolutionConfig, []);
669664

670665
expect(joinPaths).toEqual([
671666
[
@@ -707,7 +702,6 @@ describe('Generate resolution join paths', () => {
707702
],
708703
},
709704
];
710-
const columnNameMap = constructDimensionsNameMap(baseTableSchemas);
711705

712706
const resolutionConfig = {
713707
columnConfigs: [
@@ -723,7 +717,7 @@ describe('Generate resolution join paths', () => {
723717

724718
const joinPaths = generateResolutionJoinPaths(
725719
resolutionConfig,
726-
columnNameMap
720+
baseTableSchemas
727721
);
728722
expect(joinPaths).toEqual([
729723
[

meerkat-core/src/resolution/resolution.ts

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,12 @@ import { getNamespacedKey, memberKeyToSafeKey } from '../member-formatters';
22
import { constructAlias } from '../member-formatters/get-alias';
33
import { JoinPath, Member, Query } from '../types/cube-types/query';
44
import { Dimension, Measure, TableSchema } from '../types/cube-types/table';
5+
import {
6+
findInDimensionSchemas,
7+
findInSchemas,
8+
} from '../utils/find-in-table-schema';
59
import { BASE_DATA_SOURCE_NAME, ResolutionConfig } from './types';
610

7-
export const constructDimensionsNameMap = (tableSchema: TableSchema[]) => {
8-
const columnNameMap: Record<string, Dimension> = {};
9-
tableSchema.forEach((table) => {
10-
table.dimensions.forEach((dimension) => {
11-
columnNameMap[getNamespacedKey(table.name, dimension.name)] = dimension;
12-
});
13-
});
14-
return columnNameMap;
15-
};
16-
17-
export const constructMeasuresNameMap = (tableSchema: TableSchema[]) => {
18-
const columnNameMap: Record<string, Measure> = {};
19-
tableSchema.forEach((table) => {
20-
table.measures.forEach((measure) => {
21-
columnNameMap[getNamespacedKey(table.name, measure.name)] = measure;
22-
});
23-
});
24-
return columnNameMap;
25-
};
26-
2711
const constructBaseDimension = (name: string, schema: Measure | Dimension) => {
2812
return {
2913
name: memberKeyToSafeKey(name),
@@ -36,7 +20,7 @@ const constructBaseDimension = (name: string, schema: Measure | Dimension) => {
3620

3721
export const createBaseTableSchema = (
3822
baseSql: string,
39-
columnsByName: Record<string, Measure | Dimension>,
23+
tableSchemas: TableSchema[],
4024
resolutionConfig: ResolutionConfig,
4125
measures: Member[],
4226
dimensions?: Member[]
@@ -45,7 +29,7 @@ export const createBaseTableSchema = (
4529
sql: baseSql,
4630
measures: [],
4731
dimensions: [...measures, ...(dimensions || [])].map((member) => {
48-
const schema = columnsByName[member];
32+
const schema = findInSchemas(member, tableSchemas);
4933
if (schema) {
5034
return constructBaseDimension(member, schema);
5135
} else {
@@ -55,20 +39,16 @@ export const createBaseTableSchema = (
5539
joins: resolutionConfig.columnConfigs.map((config) => ({
5640
sql: `${BASE_DATA_SOURCE_NAME}.${constructAlias(
5741
config.name,
58-
columnsByName[config.name]?.alias,
42+
findInSchemas(config.name, tableSchemas)?.alias,
5943
true
6044
)} = ${memberKeyToSafeKey(config.name)}.${config.joinColumn}`,
6145
})),
6246
});
6347

6448
export const generateResolutionSchemas = (
6549
config: ResolutionConfig,
66-
baseColumnsByName: Record<string, Measure | Dimension>
50+
baseTableSchemas: TableSchema[]
6751
) => {
68-
const resolutionColumnsByName = constructDimensionsNameMap(
69-
config.tableSchemas
70-
);
71-
7252
const resolutionSchemas: TableSchema[] = [];
7353
config.columnConfigs.forEach((colConfig) => {
7454
const tableSchema = config.tableSchemas.find(
@@ -81,7 +61,7 @@ export const generateResolutionSchemas = (
8161
const baseName = memberKeyToSafeKey(colConfig.name);
8262
const baseAlias = constructAlias(
8363
colConfig.name,
84-
baseColumnsByName[colConfig.name]?.alias
64+
findInSchemas(colConfig.name, baseTableSchemas)?.alias
8565
);
8666

8767
// For each column that needs to be resolved, create a copy of the relevant table schema.
@@ -92,8 +72,10 @@ export const generateResolutionSchemas = (
9272
sql: tableSchema.sql,
9373
measures: [],
9474
dimensions: colConfig.resolutionColumns.map((col) => {
95-
const dimension =
96-
resolutionColumnsByName[getNamespacedKey(colConfig.source, col)];
75+
const dimension = findInDimensionSchemas(
76+
getNamespacedKey(colConfig.source, col),
77+
config.tableSchemas
78+
);
9779
if (!dimension) {
9880
throw new Error(`Dimension not found: ${col}`);
9981
}
@@ -142,15 +124,15 @@ export const generateResolvedDimensions = (
142124

143125
export const generateResolutionJoinPaths = (
144126
resolutionConfig: ResolutionConfig,
145-
baseColumnsByName: Record<string, Measure | Dimension>
127+
baseTableSchemas: TableSchema[]
146128
): JoinPath[] => {
147129
return resolutionConfig.columnConfigs.map((config) => [
148130
{
149131
left: BASE_DATA_SOURCE_NAME,
150132
right: memberKeyToSafeKey(config.name),
151133
on: constructAlias(
152134
config.name,
153-
baseColumnsByName[config.name]?.alias,
135+
findInSchemas(config.name, baseTableSchemas)?.alias,
154136
true
155137
),
156138
},

0 commit comments

Comments
 (0)