Skip to content

Commit

Permalink
Add clickhouse except (#80)
Browse files Browse the repository at this point in the history
* Add clickhouse except

* fix ci

* fix ci
  • Loading branch information
lucasavila00 authored Sep 28, 2023
1 parent 30ff217 commit dbbf3fc
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
docs-eval
coverage
docs
docs
eval-mds/examples/
6 changes: 6 additions & 0 deletions docs/api/classes/select-statement.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export declare class SelectStatement<Selection, Alias, Scope, FlatScope> {
readonly limit: SafeString | number | null;
readonly where: ReadonlyArray<SafeString>;
readonly prewhere: ReadonlyArray<SafeString>;
readonly except: ReadonlyArray<SafeString>;
readonly having: ReadonlyArray<SafeString>;
readonly distinct: boolean;
readonly clickhouseWith: ReadonlyArray<ClickhouseWith>;
Expand Down Expand Up @@ -230,6 +231,11 @@ clickhouse: {
| readonly (Selection | FlatScope)[]
| ((fields: Record<Selection | FlatScope, SafeString>) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Selection, Alias, Scope, FlatScope>
except: (
f:
| readonly (Selection | FlatScope)[]
| ((fields: Record<Selection | FlatScope, SafeString>) => ReadonlyArray<SafeString> | SafeString)
) => SelectStatement<Selection, Alias, Scope, FlatScope>
replace: (
_: (
f: Record<Selection | FlatScope, SafeString> & SelectionOfScope<Scope> & NoSelectFieldsCompileError
Expand Down
28 changes: 28 additions & 0 deletions src/classes/select-statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class SelectStatement<
readonly limit: SafeString | number | null;
readonly where: ReadonlyArray<SafeString>;
readonly prewhere: ReadonlyArray<SafeString>;
readonly except: ReadonlyArray<SafeString>;
readonly having: ReadonlyArray<SafeString>;
readonly distinct: boolean;
readonly clickhouseWith: ReadonlyArray<ClickhouseWith>;
Expand Down Expand Up @@ -98,6 +99,7 @@ export class SelectStatement<
limit: null,
where: [],
prewhere: [],
except: [],
having: [],
distinct: false,
clickhouseWith: [],
Expand All @@ -124,6 +126,7 @@ export class SelectStatement<
limit: null,
where: [],
prewhere: [],
except: [],
having: [],
distinct: false,
clickhouseWith: [],
Expand All @@ -149,6 +152,7 @@ export class SelectStatement<
limit: null,
where: [],
prewhere: [],
except: [],
having: [],
distinct: false,
clickhouseWith: [],
Expand Down Expand Up @@ -243,6 +247,14 @@ export class SelectStatement<
};
return this;
};
private setExcept = (except: ReadonlyArray<SafeString>): this => {
this.__props = {
...this.__props,
except,
};
return this;
};

private setHaving = (having: ReadonlyArray<SafeString>): this => {
this.__props = {
...this.__props,
Expand Down Expand Up @@ -305,6 +317,22 @@ export class SelectStatement<
),
]),

/**
* @since 2.0.0
*/
except: (
f:
| ReadonlyArray<Selection | FlatScope>
| ((
fields: Record<Selection | FlatScope, SafeString>
) => ReadonlyArray<SafeString> | SafeString)
): SelectStatement<Selection, Alias, Scope, FlatScope> =>
this.copy().setExcept([
...this.__props.except,
...makeArray(
consumeArrayCallback(f as any, this.__props.scope)
),
]),
/**
* @since 2.0.0
*/
Expand Down
7 changes: 6 additions & 1 deletion src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ export const printSelectStatementInternal = (
.map((it) => it.content)
.join(" AND ")}`
: "";

const vs = selectStatement.__props.except
.map((it) => it.content)
.join(", ");
const except =
selectStatement.__props.except.length > 0 ? `EXCEPT (${vs})` : "";
const from =
selectStatement.__props.from != null
? `FROM ${printInternal(selectStatement.__props.from, true)}`
Expand Down Expand Up @@ -241,6 +245,7 @@ export const printSelectStatementInternal = (
"SELECT",
distinct,
selection,
except,
replace,
from,
prewhere,
Expand Down
33 changes: 33 additions & 0 deletions tests/clickhouse-suite/except.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { table } from "../../src";
import { addSimpleStringSerializer } from "../utils";
addSimpleStringSerializer();

describe("clickhouse replace", () => {
const t3 = table(["x", "y"], "t3_clickhouse");

it("works with 1 item", async () => {
const q = t3
.selectStar()
.clickhouse.except((f) => [f.x])
.stringify();
expect(q).toMatchInlineSnapshot(
`SELECT * EXCEPT (\`x\`) FROM \`t3_clickhouse\``
);
});
it("works with 2 items", async () => {
const q = t3
.selectStar()
.clickhouse.except((f) => [f.x, f.y])
.stringify();
expect(q).toMatchInlineSnapshot(
`SELECT * EXCEPT (\`x\`, \`y\`) FROM \`t3_clickhouse\``
);
});

it("checks column names", async () => {
t3.selectStar()
//@ts-expect-error
.clickhouse.except((f) => [f.x123]);
expect(1).toBe(1);
});
});

0 comments on commit dbbf3fc

Please sign in to comment.