Skip to content

Commit b87c68d

Browse files
committed
refactor
1 parent ff3aa74 commit b87c68d

File tree

9 files changed

+40
-62
lines changed

9 files changed

+40
-62
lines changed

meerkat-core/src/ast-builder/ast-builder.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import {
77
QueryFiltersWithInfoSingular,
88
} from '../cube-to-duckdb/cube-filter-to-duckdb';
99
import { traverseAndFilter } from '../filter-params/filter-params-ast';
10-
import {
11-
constructAlias,
12-
shouldUseSafeAlias,
13-
} from '../member-formatters/get-alias';
10+
import { constructAlias } from '../member-formatters/get-alias';
1411
import {
1512
FilterType,
1613
MeerkatQueryFilter,
@@ -38,9 +35,9 @@ const formatFilters = (
3835
member: constructAlias({
3936
name: item.member,
4037
alias: item.memberInfo.alias,
41-
safe: shouldUseSafeAlias({
38+
aliasContext: {
4239
isAstIdentifier: true,
43-
}),
40+
},
4441
}),
4542
};
4643
}) as QueryFiltersWithInfo);

meerkat-core/src/cube-group-by-transformer/cube-group-by-transformer.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
getAliasFromSchema,
3-
shouldUseSafeAlias,
4-
} from '../member-formatters/get-alias';
1+
import { getAliasFromSchema } from '../member-formatters/get-alias';
52
import { TableSchema } from '../types/cube-types';
63
import { Member } from '../types/cube-types/query';
74
import {
@@ -22,7 +19,7 @@ export const cubeDimensionToGroupByAST = (
2219
getAliasFromSchema({
2320
name: dimension,
2421
tableSchema,
25-
safe: shouldUseSafeAlias({ isAstIdentifier: true }),
22+
aliasContext: { isAstIdentifier: true },
2623
}),
2724
],
2825
};

meerkat-core/src/cube-measure-transformer/cube-measure-transformer.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import {
2-
getAliasFromSchema,
3-
getNamespacedKey,
4-
shouldUseSafeAlias,
5-
} from '../member-formatters';
1+
import { getAliasFromSchema, getNamespacedKey } from '../member-formatters';
62
import { splitIntoDataSourceAndFields } from '../member-formatters/split-into-data-source-and-fields';
73
import { Member } from '../types/cube-types/query';
84
import { Measure, TableSchema } from '../types/cube-types/table';
@@ -25,9 +21,9 @@ export const cubeMeasureToSQLSelectString = (
2521
const aliasKey = getAliasFromSchema({
2622
name: measure,
2723
tableSchema,
28-
safe: shouldUseSafeAlias({
24+
aliasContext: {
2925
isAstIdentifier: false,
30-
}),
26+
},
3127
});
3228
const measureSchema = tableSchema.measures.find(
3329
(m) => m.name === measureKeyWithoutTable
@@ -61,9 +57,9 @@ export const cubeMeasureToSQLSelectString = (
6157
const columnKey = getAliasFromSchema({
6258
name: memberKey,
6359
tableSchema,
64-
safe: shouldUseSafeAlias({
60+
aliasContext: {
6561
isAstIdentifier: false,
66-
}),
62+
},
6763
});
6864
meerkatReplacedSqlString = meerkatReplacedSqlString.replace(
6965
memberKey,
@@ -95,9 +91,9 @@ const addDimensionToSQLProjection = (
9591
const aliasKey = getAliasFromSchema({
9692
name: dimension,
9793
tableSchema,
98-
safe: shouldUseSafeAlias({
94+
aliasContext: {
9995
isAstIdentifier: false,
100-
}),
96+
},
10197
});
10298

10399
if (!dimensionSchema) {

meerkat-core/src/cube-order-by-transformer/cube-order-by-transformer.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
getAliasFromSchema,
3-
shouldUseSafeAlias,
4-
} from '../member-formatters/get-alias';
1+
import { getAliasFromSchema } from '../member-formatters/get-alias';
52
import { TableSchema } from '../types/cube-types';
63
import {
74
ExpressionClass,
@@ -33,7 +30,7 @@ export const cubeOrderByToAST = (
3330
getAliasFromSchema({
3431
name: key,
3532
tableSchema,
36-
safe: shouldUseSafeAlias({ isAstIdentifier: true }),
33+
aliasContext: { isAstIdentifier: true },
3734
}),
3835
],
3936
},

meerkat-core/src/get-wrapped-base-query-with-projections/get-aliased-columns-from-filters.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
getAliasFromSchema,
3-
shouldUseSafeAlias,
4-
} from '../member-formatters/get-alias';
1+
import { getAliasFromSchema } from '../member-formatters/get-alias';
52
import { splitIntoDataSourceAndFields } from '../member-formatters/split-into-data-source-and-fields';
63
import { MeerkatQueryFilter, Query, TableSchema } from '../types/cube-types';
74
import {
@@ -46,7 +43,7 @@ export const getDimensionProjection = ({
4643
const aliasKey = getAliasFromSchema({
4744
name: key,
4845
tableSchema,
49-
safe: shouldUseSafeAlias({ isAstIdentifier: false }),
46+
aliasContext: { isAstIdentifier: false },
5047
});
5148
// Add the alias key to the set. So we have a reference to all the previously selected members.
5249
return { sql: `${modifiedSql} AS ${aliasKey}`, foundMember, aliasKey };
@@ -77,7 +74,7 @@ export const getFilterMeasureProjection = ({
7774
const aliasKey = getAliasFromSchema({
7875
name: key,
7976
tableSchema,
80-
safe: shouldUseSafeAlias({ isAstIdentifier: false }),
77+
aliasContext: { isAstIdentifier: false },
8178
});
8279
return { sql: `${key} AS ${aliasKey}`, foundMember, aliasKey };
8380
};

meerkat-core/src/get-wrapped-base-query-with-projections/get-projection-clause.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { getAllColumnUsedInMeasures } from '../cube-measure-transformer/cube-measure-transformer';
2-
import {
3-
getAliasFromSchema,
4-
shouldUseSafeAlias,
5-
} from '../member-formatters/get-alias';
2+
import { getAliasFromSchema } from '../member-formatters/get-alias';
63
import { splitIntoDataSourceAndFields } from '../member-formatters/split-into-data-source-and-fields';
74
import { Query, TableSchema } from '../types/cube-types';
85
import {
@@ -58,7 +55,7 @@ export const getProjectionClause = (
5855
member: getAliasFromSchema({
5956
name: member,
6057
tableSchema,
61-
safe: shouldUseSafeAlias({ isAstIdentifier: false }),
58+
aliasContext: { isAstIdentifier: false },
6259
}),
6360
aliasedColumnSet,
6461
acc,
@@ -110,7 +107,7 @@ export const getProjectionClause = (
110107
const safeKey = getAliasFromSchema({
111108
name: column,
112109
tableSchema,
113-
safe: true,
110+
aliasContext: { isAstIdentifier: false },
114111
});
115112
columnsUsedInMeasuresInProjection += `${column} AS ${safeKey}`;
116113
if (index !== columnsUsedInMeasures.length - 1) {

meerkat-core/src/member-formatters/get-alias.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { findInSchema } from '../utils/find-in-table-schema';
33
import { memberKeyToSafeKey } from './member-key-to-safe-key';
44
import { splitIntoDataSourceAndFields } from './split-into-data-source-and-fields';
55

6-
export const shouldUseSafeAlias = ({
7-
isAstIdentifier,
8-
isTableSchemaAlias,
9-
}: {
6+
export interface AliasContext {
107
isAstIdentifier?: boolean;
118
isTableSchemaAlias?: boolean;
12-
}): boolean => {
9+
}
10+
11+
const shouldUseSafeAlias = ({
12+
isAstIdentifier,
13+
isTableSchemaAlias,
14+
}: AliasContext): boolean => {
1315
if (isAstIdentifier) {
1416
// Duckdb will automatically quote identifiers that contain special characters or spaces.
1517
// when converting the AST to SQL.
@@ -27,31 +29,31 @@ export const shouldUseSafeAlias = ({
2729
export const getAliasFromSchema = ({
2830
name,
2931
tableSchema,
30-
safe,
32+
aliasContext,
3133
}: {
3234
name: string;
3335
tableSchema: TableSchema;
34-
safe: boolean;
36+
aliasContext: AliasContext;
3537
}): string => {
3638
const [, field] = splitIntoDataSourceAndFields(name);
3739
return constructAlias({
3840
name,
3941
alias: findInSchema(field, tableSchema)?.alias,
40-
safe,
42+
aliasContext,
4143
});
4244
};
4345

4446
export const constructAlias = ({
4547
name,
4648
alias,
47-
safe,
49+
aliasContext,
4850
}: {
4951
name: string;
5052
alias?: string;
51-
safe: boolean;
53+
aliasContext: AliasContext;
5254
}): string => {
5355
if (alias) {
54-
if (safe) {
56+
if (shouldUseSafeAlias(aliasContext)) {
5557
// Alias may contain special characters or spaces, so we need to wrap in quotes.
5658
return `"${alias}"`;
5759
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
export { COLUMN_NAME_DELIMITER } from './constants';
2-
export {
3-
constructAlias,
4-
getAliasFromSchema,
5-
shouldUseSafeAlias,
6-
} from './get-alias';
2+
export { constructAlias, getAliasFromSchema } from './get-alias';
73
export { getNamespacedKey } from './get-namespaced-key';
84
export { memberKeyToSafeKey } from './member-key-to-safe-key';
95
export { splitIntoDataSourceAndFields } from './split-into-data-source-and-fields';

meerkat-core/src/resolution/resolution.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
constructAlias,
33
getNamespacedKey,
44
memberKeyToSafeKey,
5-
shouldUseSafeAlias,
65
} from '../member-formatters';
76
import { JoinPath, Member, Query } from '../types/cube-types/query';
87
import { Dimension, Measure, TableSchema } from '../types/cube-types/table';
@@ -18,14 +17,14 @@ const constructBaseDimension = (name: string, schema: Measure | Dimension) => {
1817
sql: `${BASE_DATA_SOURCE_NAME}.${constructAlias({
1918
name,
2019
alias: schema.alias,
21-
safe: shouldUseSafeAlias({ isAstIdentifier: false }),
20+
aliasContext: { isAstIdentifier: false },
2221
})}`,
2322
type: schema.type,
2423
// Constructs alias to match the name in the base query.
2524
alias: constructAlias({
2625
name,
2726
alias: schema.alias,
28-
safe: shouldUseSafeAlias({ isTableSchemaAlias: true }),
27+
aliasContext: { isTableSchemaAlias: true },
2928
}),
3029
};
3130
};
@@ -64,7 +63,7 @@ export const createBaseTableSchema = (
6463
sql: `${BASE_DATA_SOURCE_NAME}.${constructAlias({
6564
name: config.name,
6665
alias: schemaByName[config.name]?.alias,
67-
safe: shouldUseSafeAlias({ isAstIdentifier: false }),
66+
aliasContext: { isAstIdentifier: false },
6867
})} = ${memberKeyToSafeKey(config.name)}.${config.joinColumn}`,
6968
})),
7069
};
@@ -87,7 +86,7 @@ export const generateResolutionSchemas = (
8786
const baseAlias = constructAlias({
8887
name: colConfig.name,
8988
alias: findInSchemas(colConfig.name, baseTableSchemas)?.alias,
90-
safe: shouldUseSafeAlias({ isTableSchemaAlias: true }),
89+
aliasContext: { isTableSchemaAlias: true },
9190
});
9291

9392
// For each column that needs to be resolved, create a copy of the relevant table schema.
@@ -114,7 +113,7 @@ export const generateResolutionSchemas = (
114113
alias: `${baseAlias} - ${constructAlias({
115114
name: col,
116115
alias: dimension.alias,
117-
safe: shouldUseSafeAlias({ isTableSchemaAlias: true }),
116+
aliasContext: { isTableSchemaAlias: true },
118117
})}`,
119118
};
120119
}),
@@ -163,7 +162,7 @@ export const generateResolutionJoinPaths = (
163162
on: constructAlias({
164163
name: config.name,
165164
alias: findInSchemas(config.name, baseTableSchemas)?.alias,
166-
safe: shouldUseSafeAlias({ isAstIdentifier: false }),
165+
aliasContext: { isAstIdentifier: false },
167166
}),
168167
},
169168
]);

0 commit comments

Comments
 (0)