Skip to content

Commit

Permalink
refactor: extract getAdHocAlias and getAdHocPath functions
Browse files Browse the repository at this point in the history
  • Loading branch information
retro committed Apr 19, 2024
1 parent c6a8377 commit 3679eed
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
7 changes: 3 additions & 4 deletions src/lib/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { buildQuery } from "./query-builder/build-query.js";
import { findOptimalJoinGraph } from "./query-builder/optimal-join-graph.js";
import { processQueryAndExpandToSegments } from "./query-builder/process-query-and-expand-to-segments.js";
import type { AnyRepository } from "./repository.js";
import { getAdHocAlias, getAdHocPath } from "./util.js";

function getDimensionNamesSchema(dimensionPaths: string[]) {
return z
Expand Down Expand Up @@ -199,11 +200,9 @@ export class QueryBuilder<
const aggregateWith = memberNameOrAdHoc.aggregateWith;
const dimensionName = memberNameOrAdHoc.dimension;
const member = this.repository.getMember(dimensionName);
acc[
`${dimensionName.replaceAll(".", "___")}___adhoc_${aggregateWith}`
] = {
acc[getAdHocAlias(dimensionName, aggregateWith)] = {
memberType: "metric",
path: `${member.getPath()}.adhoc_${aggregateWith}`,
path: getAdHocPath(member.getPath(), aggregateWith),
format: undefined,
type: "unknown",
description: undefined,
Expand Down
17 changes: 8 additions & 9 deletions src/lib/query-builder/build-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ import { BaseDialect } from "../dialect/base.js";
import type { AnyJoin } from "../join.js";
import { AnyModel } from "../model.js";
import type { AnyRepository } from "../repository.js";
import { getAdHocAlias } from "../util.js";

interface ReferencedModels {
all: string[];
dimensions: string[];
metrics: string[];
}

function adHocMetricAlias(adHocMetric: QueryAdHocMetric) {
return `${adHocMetric.dimension.replaceAll(".", "___")}___adhoc_${
adHocMetric.aggregateWith
}`;
function getAdHocMetricAlias(adHocMetric: QueryAdHocMetric) {
return getAdHocAlias(adHocMetric.dimension, adHocMetric.aggregateWith);
}

function getSortableMetric(metrics: QueryMetric[] | undefined) {
Expand Down Expand Up @@ -158,7 +157,7 @@ function buildQuerySegmentJoinQuery(
context,
);
sqlQuery.select(
knex.raw(`${sql} as ${adHocMetricAlias(adHocMetric)}`, bindings),
knex.raw(`${sql} as ${getAdHocMetricAlias(adHocMetric)}`, bindings),
);
}

Expand Down Expand Up @@ -271,14 +270,14 @@ function buildQuerySegment(
const dimension = repository.getDimension(adHocMetric.dimension);
const initialSql = dialect.aggregate(
adHocMetric.aggregateWith,
`${dialect.asIdentifier(alias)}.${adHocMetricAlias(adHocMetric)}`,
`${dialect.asIdentifier(alias)}.${getAdHocMetricAlias(adHocMetric)}`,
);
const dimensionGranularity = dimension.getGranularity();
const sql = dimensionGranularity
? dialect.withGranularity(dimensionGranularity, initialSql)
: initialSql;

sqlQuery.select(knex.raw(`${sql} as ${adHocMetricAlias(adHocMetric)}`));
sqlQuery.select(knex.raw(`${sql} as ${getAdHocMetricAlias(adHocMetric)}`));
}

return { ...segment, sqlQuery };
Expand Down Expand Up @@ -347,9 +346,9 @@ export function buildQuery(
.adHocMetrics || []) {
rootSqlQuery.select(
knex.raw(
`${dialect.asIdentifier(rootAlias)}.${adHocMetricAlias(
`${dialect.asIdentifier(rootAlias)}.${getAdHocMetricAlias(
adHocMetric,
)} as ${adHocMetricAlias(adHocMetric)}`,
)} as ${getAdHocMetricAlias(adHocMetric)}`,
),
);
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function getAdHocPath(dimensionPath: string, aggregateWith: string) {
return `${dimensionPath}.adhoc_${aggregateWith}`;
}
export function getAdHocAlias(dimensionPath: string, aggregateWith: string) {
return `${dimensionPath.replaceAll(".", "___")}___adhoc_${aggregateWith}`;
}

0 comments on commit 3679eed

Please sign in to comment.