Skip to content

Commit

Permalink
Fix select from top missing alias bug (#65)
Browse files Browse the repository at this point in the history
* Fix select from top missing alias bug

* build docs
lucasavila00 authored Jul 12, 2022
1 parent 77e88c6 commit 39e5d82
Showing 17 changed files with 109 additions and 46 deletions.
4 changes: 2 additions & 2 deletions docs/api/classes/common-table-expression.ts.md
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ Added in v1.0.0
with_: <Selection2 extends string, Alias2 extends string>(
alias: Alias2,
select: (acc: {
[K in Aliases]: Table<FilterStarting<Scope, K>, K>;
[K in Aliases]: Table<never, FilterStarting<Scope, K>, K>;
}) => SelectStatement<any, Selection2>
) =>
CommonTableExpressionFactory<
@@ -66,7 +66,7 @@ withR: <Selection2 extends string, Alias2 extends string>(
alias: Alias2,
columns: readonly Selection2[],
select: (acc: {
[K in Aliases]: Table<FilterStarting<Scope, K>, K>;
[K in Aliases]: Table<never, FilterStarting<Scope, K>, K>;
}) => SelectStatement<any, any>
) =>
CommonTableExpressionFactory<
6 changes: 4 additions & 2 deletions docs/api/classes/compound.ts.md
Original file line number Diff line number Diff line change
@@ -109,12 +109,13 @@ Added in v0.0.0
```ts
joinTable: <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisCompoundAlias: Alias1,
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
JoinedFactory<
Selection,
@@ -137,11 +138,12 @@ Added in v0.0.0
```ts
commaJoinTable: <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisSelectAlias: Alias1,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
Joined<
Selection,
16 changes: 12 additions & 4 deletions docs/api/classes/joined.ts.md
Original file line number Diff line number Diff line change
@@ -95,8 +95,12 @@ Added in v0.0.0
**Signature**
```ts
commaJoinTable: <Selection2 extends string, Alias2 extends string>(
table: Table<Selection2, Alias2>
commaJoinTable: <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
table: Table<Scope2, Selection2, Alias2>
) =>
Joined<
Selection,
@@ -117,9 +121,13 @@ Added in v0.0.0
**Signature**
```ts
joinTable: <Selection2 extends string, Alias2 extends string>(
joinTable: <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
JoinedFactory<
Selection,
6 changes: 4 additions & 2 deletions docs/api/classes/select-statement.ts.md
Original file line number Diff line number Diff line change
@@ -225,11 +225,12 @@ Added in v0.0.0
```ts
commaJoinTable: <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
Joined<
Selection,
@@ -250,12 +251,13 @@ Added in v0.0.0
```ts
joinTable: <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
JoinedFactory<
Selection,
6 changes: 4 additions & 2 deletions docs/api/classes/stringified-select-statement.ts.md
Original file line number Diff line number Diff line change
@@ -78,11 +78,12 @@ Added in v0.0.3
```ts
commaJoinTable: <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
Joined<
Selection,
@@ -103,12 +104,13 @@ Added in v0.0.3
```ts
joinTable: <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
JoinedFactory<
Selection,
20 changes: 14 additions & 6 deletions docs/api/classes/table.ts.md
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ This class is not meant to be used directly, but rather through the `table` func
**Signature**

```ts
export declare class Table<Selection, Alias> {
export declare class Table<Scope, Selection, Alias> {
private constructor(
/* @internal */
public __props: {
@@ -57,7 +57,7 @@ Added in v0.0.0

```ts
clickhouse: {
final: () => Table<Selection, Alias>;
final: () => Table<Scope, Selection, Alias>;
}
```

@@ -103,8 +103,12 @@ Added in v0.0.0
**Signature**

```ts
commaJoinTable: <Selection2 extends string, Alias2 extends string>(
table: Table<Selection2, Alias2>
commaJoinTable: <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
table: Table<Scope2, Selection2, Alias2>
) =>
Joined<
Selection,
@@ -124,9 +128,13 @@ Added in v0.0.0
**Signature**

```ts
joinTable: <Selection2 extends string, Alias2 extends string>(
joinTable: <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
) =>
JoinedFactory<
Selection,
7 changes: 4 additions & 3 deletions docs/api/index.ts.md
Original file line number Diff line number Diff line change
@@ -221,10 +221,11 @@ export declare const select: <
f:
| readonly SubSelection[]
| ((
f: Record<FromSelection, SafeString> & NoSelectFieldsCompileError
f: Record<FromSelection | FromScope, SafeString> &
NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>),
from: TableOrSubquery<FromAlias, FromScope, FromSelection, FromAmbigous>
) => SelectStatement<FromSelection | FromScope, NewSelection | SubSelection>;
) => SelectStatement<FromSelection, NewSelection | SubSelection>;
```

Added in v1.0.0
@@ -259,7 +260,7 @@ export declare const table: <Selection extends string, Alias extends string>(
columns: readonly Selection[],
alias: Alias,
name?: string
) => Table<Selection, Alias>;
) => Table<`${Alias}.${Selection}`, Selection, Alias>;
```

**Example**
4 changes: 2 additions & 2 deletions src/classes/common-table-expression.ts
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ export class CommonTableExpressionFactory<
public with_ = <Selection2 extends string, Alias2 extends string>(
alias: Alias2,
select: (acc: {
[K in Aliases]: Table<FilterStarting<Scope, K>, K>;
[K in Aliases]: Table<never, FilterStarting<Scope, K>, K>;
}) => SelectStatement<any, Selection2>
): CommonTableExpressionFactory<
`${Alias2}.${Selection2}` | Scope,
@@ -86,7 +86,7 @@ export class CommonTableExpressionFactory<
alias: Alias2,
columns: ReadonlyArray<Selection2>,
select: (acc: {
[K in Aliases]: Table<FilterStarting<Scope, K>, K>;
[K in Aliases]: Table<never, FilterStarting<Scope, K>, K>;
}) => SelectStatement<any, any>
): CommonTableExpressionFactory<
`${Alias2}.${Selection2}` | Scope,
6 changes: 4 additions & 2 deletions src/classes/compound.ts
Original file line number Diff line number Diff line change
@@ -188,12 +188,13 @@ export class Compound<Scope extends string, Selection extends string> {
*/
public joinTable = <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisCompoundAlias: Alias1,
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): JoinedFactory<
Selection,
| Exclude<Selection, Selection2>
@@ -223,11 +224,12 @@ export class Compound<Scope extends string, Selection extends string> {
*/
public commaJoinTable = <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisSelectAlias: Alias1,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): Joined<
Selection,
| Exclude<Selection, Selection2>
16 changes: 12 additions & 4 deletions src/classes/joined.ts
Original file line number Diff line number Diff line change
@@ -188,8 +188,12 @@ export class Joined<
/**
* @since 0.0.0
*/
public commaJoinTable = <Selection2 extends string, Alias2 extends string>(
table: Table<Selection2, Alias2>
public commaJoinTable = <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
table: Table<Scope2, Selection2, Alias2>
): Joined<
Selection,
| Scope
@@ -214,9 +218,13 @@ export class Joined<
/**
* @since 0.0.0
*/
public joinTable = <Selection2 extends string, Alias2 extends string>(
public joinTable = <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): JoinedFactory<
Selection,
| Scope
6 changes: 4 additions & 2 deletions src/classes/select-statement.ts
Original file line number Diff line number Diff line change
@@ -369,11 +369,12 @@ export class SelectStatement<Scope extends string, Selection extends string> {
*/
public commaJoinTable = <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): Joined<
Selection,
| Exclude<Selection, Selection2>
@@ -398,12 +399,13 @@ export class SelectStatement<Scope extends string, Selection extends string> {
*/
public joinTable = <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): JoinedFactory<
Selection,
| Exclude<Selection, Selection2>
6 changes: 4 additions & 2 deletions src/classes/stringified-select-statement.ts
Original file line number Diff line number Diff line change
@@ -66,11 +66,12 @@ export class StringifiedSelectStatement<Selection extends string> {
*/
public commaJoinTable = <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): Joined<
Selection,
| Exclude<Selection, Selection2>
@@ -95,12 +96,13 @@ export class StringifiedSelectStatement<Selection extends string> {
*/
public joinTable = <
Alias1 extends string,
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
thisQueryAlias: Alias1,
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): JoinedFactory<
Selection,
| Exclude<Selection, Selection2>
28 changes: 20 additions & 8 deletions src/classes/table.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,11 @@ import { StringifiedSelectStatement } from "./stringified-select-statement";
*
* @since 0.0.0
*/
export class Table<Selection extends string, Alias extends string> {
export class Table<
Scope extends string,
Selection extends string,
Alias extends string
> {
/* @internal */
private constructor(
/* @internal */
@@ -42,10 +46,10 @@ export class Table<Selection extends string, Alias extends string> {
columns: ReadonlyArray<Selection>,
alias: Alias,
name: string = alias
): Table<Selection, Alias> =>
): Table<`${Alias}.${Selection}`, Selection, Alias> =>
new Table({ columns, alias, name, final: false });

private copy = (): Table<Selection, Alias> =>
private copy = (): Table<Scope, Selection, Alias> =>
new Table({ ...this.__props });

private setFinal = (final: boolean): this => {
@@ -60,7 +64,7 @@ export class Table<Selection extends string, Alias extends string> {
/**
* @since 0.0.0
*/
final: (): Table<Selection, Alias> => this.copy().setFinal(true),
final: (): Table<Scope, Selection, Alias> => this.copy().setFinal(true),
};

/**
@@ -95,8 +99,12 @@ export class Table<Selection extends string, Alias extends string> {
/**
* @since 0.0.0
*/
public commaJoinTable = <Selection2 extends string, Alias2 extends string>(
table: Table<Selection2, Alias2>
public commaJoinTable = <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
table: Table<Scope2, Selection2, Alias2>
): Joined<
Selection,
| Exclude<Selection, Selection2>
@@ -120,9 +128,13 @@ export class Table<Selection extends string, Alias extends string> {
/**
* @since 0.0.0
*/
public joinTable = <Selection2 extends string, Alias2 extends string>(
public joinTable = <
Scope2 extends string,
Selection2 extends string,
Alias2 extends string
>(
operator: string,
table: Table<Selection2, Alias2>
table: Table<Scope2, Selection2, Alias2>
): JoinedFactory<
Selection,
| Exclude<Selection, Selection2>
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -298,10 +298,11 @@ export const select = <
f:
| ReadonlyArray<SubSelection>
| ((
f: Record<FromSelection, SafeString> & NoSelectFieldsCompileError
f: Record<FromSelection | FromScope, SafeString> &
NoSelectFieldsCompileError
) => Record<NewSelection, SafeString>),
from: TableOrSubquery<FromAlias, FromScope, FromSelection, FromAmbigous>
): SelectStatement<FromScope | FromSelection, NewSelection | SubSelection> =>
): SelectStatement<FromSelection, NewSelection | SubSelection> =>
//@ts-expect-error
from.select(f);

8 changes: 6 additions & 2 deletions src/print.ts
Original file line number Diff line number Diff line change
@@ -60,8 +60,12 @@ const printStringifiedSelectInternal = <Selection extends string>(
it: StringifiedSelectStatement<Selection>
): PrintInternalRet => `(${it.__props.content.content})`;

const printTableInternal = <Selection extends string, Alias extends string>(
table: Table<Selection, Alias>
const printTableInternal = <
Scope extends string,
Selection extends string,
Alias extends string
>(
table: Table<Scope, Selection, Alias>
): PrintInternalRet => {
const final = table.__props.final ? ` FINAL` : "";
if (table.__props.name === table.__props.alias) {
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ export type TableOrSubquery<
> =
| SelectStatement<Scope, Selection>
| StringifiedSelectStatement<Selection>
| Table<Selection, Alias>
| Table<Scope, Selection, Alias>
| Joined<Selection, Scope, Alias, Ambiguous>
| Compound<Scope, Selection>;

9 changes: 9 additions & 0 deletions tests/unit/select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { select, table } from "../../src";
const cols = ["a", "b"] as const;
it("alias from table", () => {
const t = table(cols, "t");

t.select((f) => ({ abc: f["t.a"] }));
select((f) => ({ abc: f["t.a"] }), t);
expect(1).toBe(1);
});

0 comments on commit 39e5d82

Please sign in to comment.