Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WITH ROLLUP #83

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/classes/select-statement.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export declare class SelectStatement<Selection, Alias, Scope, FlatScope> {
readonly ctes: ReadonlyArray<CTE>;
readonly alias?: string;
readonly scope: ScopeStorage;
readonly rollup: boolean;
}
);
}
Expand Down Expand Up @@ -241,6 +242,7 @@ clickhouse: {
f: Record<Selection | FlatScope, SafeString> & SelectionOfScope<Scope> & NoSelectFieldsCompileError
) => ReplaceT<Selection>
) => SelectStatement<Selection, Alias, Scope, FlatScope>
withRollup: () => SelectStatement<Selection, Alias, Scope, FlatScope>
}
```

Expand Down
15 changes: 15 additions & 0 deletions src/classes/select-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class SelectStatement<
readonly ctes: ReadonlyArray<CTE>;
readonly alias?: string;
readonly scope: ScopeStorage;
readonly rollup: boolean;
}
) {}

Expand Down Expand Up @@ -106,6 +107,7 @@ export class SelectStatement<
ctes: [],
alias,
scope,
rollup: false,
});

/**
Expand Down Expand Up @@ -133,6 +135,7 @@ export class SelectStatement<
ctes: [],
alias,
scope,
rollup: false,
});

/**
Expand All @@ -159,6 +162,7 @@ export class SelectStatement<
ctes: [],
scope: {},
alias: undefined,
rollup: false,
}
);

Expand All @@ -176,6 +180,14 @@ export class SelectStatement<
return this;
};

private setRollup = (rollup: boolean): this => {
this.__props = {
...this.__props,
rollup,
};
return this;
};

private setReplace = (replace: ReplaceT<Selection>): this => {
this.__props = {
...this.__props,
Expand Down Expand Up @@ -350,6 +362,9 @@ export class SelectStatement<
this.__props.scope
) as any),
]),

withRollup: (): SelectStatement<Selection, Alias, Scope, FlatScope> =>
this.copy().setRollup(true),
};

/**
Expand Down
13 changes: 10 additions & 3 deletions src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ const printOrderBy = (orderBy: ReadonlyArray<SafeString>): string =>
orderBy.length > 0
? `ORDER BY ${orderBy.map((it) => it.content).join(", ")}`
: "";
const printGroupBy = (orderBy: ReadonlyArray<SafeString>): string =>
const printGroupBy = (
orderBy: ReadonlyArray<SafeString>,
withRollup: boolean
): string =>
orderBy.length > 0
? `GROUP BY ${orderBy.map((it) => it.content).join(", ")}`
? `GROUP BY ${orderBy.map((it) => it.content).join(", ")}` +
(withRollup ? " WITH ROLLUP" : "")
: "";
const printLimit = (limit: number | SafeString | null): string =>
limit == null
Expand Down Expand Up @@ -250,7 +254,10 @@ export const printSelectStatementInternal = (
from,
prewhere,
where,
printGroupBy(selectStatement.__props.groupBy),
printGroupBy(
selectStatement.__props.groupBy,
selectStatement.__props.rollup
),
having,
printOrderBy(selectStatement.__props.orderBy),
printLimit(selectStatement.__props.limit),
Expand Down
28 changes: 28 additions & 0 deletions tests/clickhouse-suite/rollup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { table } from "../../src";
import { configureClickhouse } from "../utils";
import { addSimpleStringSerializer } from "../utils";
addSimpleStringSerializer();

describe("clickhouse final", () => {
const t1 = table(["x", "y"], "t11_clickhouse").clickhouse.final();
const { run } = configureClickhouse();

beforeAll(async () => {
await run(`DROP TABLE IF EXISTS t11_clickhouse`);
await run(
`CREATE TABLE IF NOT EXISTS t11_clickhouse(x Int64, y Int64) ENGINE = AggregatingMergeTree() ORDER BY y`
);
});

it("no alias", async () => {
const q = t1
.selectStar()
.groupBy((f) => [f.x, f.y])
.clickhouse.withRollup()
.stringify();
expect(q).toMatchInlineSnapshot(
`SELECT * FROM \`t11_clickhouse\` FINAL GROUP BY \`x\`, \`y\` WITH ROLLUP`
);
expect(await run(q)).toMatchInlineSnapshot(`Array []`);
});
});
26 changes: 26 additions & 0 deletions tests/joinTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ describe("join", () => {
);
});

it("table repro", async () => {
const q = t1
.select((f) => ({ a: f.a }))
.as("main_alias")
.join("inner", t2.select((f) => ({ b: f.b })).as("alias2"))
.on((f) => equals(f.main_alias.a, f.alias2.b))
.select((f) => ({ x: f.a }))
.stringify();

expect(q).toMatchInlineSnapshot(
`SELECT \`a\` AS \`x\` FROM (SELECT \`a\` AS \`a\` FROM \`t1\`) AS \`main_alias\` inner JOIN (SELECT \`b\` AS \`b\` FROM \`t2\`) AS \`alias2\` ON \`main_alias\`.\`a\` = \`alias2\`.\`b\``
);
const q2 = t1
.select((f) => ({ a: f.a }))
.as("main_alias")
.join("inner", t2.select((f) => ({ b: f.b })).as("alias2"))
.on((f) => equals(f.main_alias.a, f.alias2.b))
.selectStar()
.select((f) => ({ x: f.a }))
.stringify();

expect(q2).toMatchInlineSnapshot(
`SELECT \`a\` AS \`x\` FROM (SELECT * FROM (SELECT \`a\` AS \`a\` FROM \`t1\`) AS \`main_alias\` inner JOIN (SELECT \`b\` AS \`b\` FROM \`t2\`) AS \`alias2\` ON \`main_alias\`.\`a\` = \`alias2\`.\`b\`)`
);
});

it("table -> table -- select", async () => {
const q = t1
.join("NATURAL", t2)
Expand Down
Loading