Skip to content

Commit 94b35df

Browse files
authored
add support for ranking functions (#252)
* add more function supports * fmt
1 parent a49018a commit 94b35df

9 files changed

Lines changed: 96 additions & 47 deletions

File tree

src/ts_generator/sql_parser/expressions/functions.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
11
// LIST OF FUNCTIONS FOUND https://www.w3schools.com/mysql/mysql_ref_functions.asp
22
pub static NUMERIC_FUNCTIONS: &[&str] = &[
3-
"ABS", "ACOS", "ASIN", "ATAN", "ATAN2", "AVG", "CEIL", "CEILING", "COS", "COT", "COUNT", "DEGREES", "DIV", "EXP",
4-
"FLOOR", "GREATEST", "LEAST", "LN", "LOG", "LOG10", "LOG2", "MAX", "MIN", "MOD", "PI", "POW", "POWER", "RADIANS",
5-
"RAND", "ROUND", "SIGN", "SIN", "SQRT", "SUM", "TAN", "TRUNCATE", "TRUNC",
3+
"ABS",
4+
"ACOS",
5+
"ASIN",
6+
"ATAN",
7+
"ATAN2",
8+
"AVG",
9+
"CEIL",
10+
"CEILING",
11+
"COS",
12+
"COT",
13+
"COUNT",
14+
"DEGREES",
15+
"DIV",
16+
"EXP",
17+
"FLOOR",
18+
"GREATEST",
19+
"LEAST",
20+
"LN",
21+
"LOG",
22+
"LOG10",
23+
"LOG2",
24+
"MAX",
25+
"MIN",
26+
"MOD",
27+
"PI",
28+
"POW",
29+
"POWER",
30+
"RADIANS",
31+
"RAND",
32+
"ROUND",
33+
"SIGN",
34+
"SIN",
35+
"SQRT",
36+
"SUM",
37+
"TAN",
38+
"TRUNCATE",
39+
"TRUNC",
40+
// Window / ranking functions that always return a numeric value
41+
"RANK",
42+
"DENSE_RANK",
43+
"ROW_NUMBER",
44+
"NTILE",
45+
"PERCENT_RANK",
46+
"CUME_DIST",
647
];
748

849
pub static STRING_FUNCTIONS: &[&str] = &[
@@ -113,7 +154,18 @@ pub fn is_date_function(func_name: &str) -> bool {
113154
}
114155

115156
// Type-polymorphic functions that return the type of their first argument
116-
pub static TYPE_POLYMORPHIC_FUNCTIONS: &[&str] = &["IFNULL", "COALESCE", "NULLIF", "NVL"];
157+
pub static TYPE_POLYMORPHIC_FUNCTIONS: &[&str] = &[
158+
"IFNULL",
159+
"COALESCE",
160+
"NULLIF",
161+
"NVL",
162+
// Window value functions — return the same type as their first argument
163+
"LAG",
164+
"LEAD",
165+
"FIRST_VALUE",
166+
"LAST_VALUE",
167+
"NTH_VALUE",
168+
];
117169

118170
pub fn is_type_polymorphic_function(func_name: &str) -> bool {
119171
TYPE_POLYMORPHIC_FUNCTIONS.contains(&func_name.to_uppercase().as_str())

tests/demo/cte/cte.queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type RankWithCteParams = [];
1515
export interface IRankWithCteResult {
1616
id: number;
1717
name: string;
18-
rk: any;
18+
rk: number;
1919
}
2020

2121
export interface IRankWithCteQuery {

tests/demo/cte/cte.snapshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type RankWithCteParams = [];
1515
export interface IRankWithCteResult {
1616
id: number;
1717
name: string;
18-
rk: any;
18+
rk: number;
1919
}
2020

2121
export interface IRankWithCteQuery {

tests/demo/window/lag_lead.queries.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type BasicLagParams = [];
33
export interface IBasicLagResult {
44
id: number;
55
name: string;
6-
previousName: any;
6+
previousName: string;
77
}
88

99
export interface IBasicLagQuery {
@@ -16,7 +16,7 @@ export type BasicLeadParams = [];
1616
export interface IBasicLeadResult {
1717
id: number;
1818
name: string;
19-
nextName: any;
19+
nextName: string;
2020
}
2121

2222
export interface IBasicLeadQuery {
@@ -29,7 +29,7 @@ export type LagWithDefaultParams = [];
2929
export interface ILagWithDefaultResult {
3030
id: number;
3131
name: string;
32-
previousName: any;
32+
previousName: string;
3333
}
3434

3535
export interface ILagWithDefaultQuery {
@@ -42,8 +42,8 @@ export type LagAndLeadParams = [];
4242
export interface ILagAndLeadResult {
4343
id: number;
4444
name: string;
45-
nextName: any;
46-
previousName: any;
45+
nextName: string;
46+
previousName: string;
4747
}
4848

4949
export interface ILagAndLeadQuery {
@@ -56,7 +56,7 @@ export type LagWithPartitionParams = [];
5656
export interface ILagWithPartitionResult {
5757
id: number;
5858
name: string;
59-
previousInRarity: any;
59+
previousInRarity: string;
6060
rarity: string | null;
6161
}
6262

@@ -68,9 +68,9 @@ export interface ILagWithPartitionQuery {
6868
export type FirstLastValueParams = [];
6969

7070
export interface IFirstLastValueResult {
71-
firstInRarity: any;
71+
firstInRarity: string;
7272
id: number;
73-
lastInRarity: any;
73+
lastInRarity: string;
7474
name: string;
7575
rarity: string | null;
7676
}

tests/demo/window/lag_lead.snapshot.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type BasicLagParams = [];
33
export interface IBasicLagResult {
44
id: number;
55
name: string;
6-
previousName: any;
6+
previousName: string;
77
}
88

99
export interface IBasicLagQuery {
@@ -16,7 +16,7 @@ export type BasicLeadParams = [];
1616
export interface IBasicLeadResult {
1717
id: number;
1818
name: string;
19-
nextName: any;
19+
nextName: string;
2020
}
2121

2222
export interface IBasicLeadQuery {
@@ -29,7 +29,7 @@ export type LagWithDefaultParams = [];
2929
export interface ILagWithDefaultResult {
3030
id: number;
3131
name: string;
32-
previousName: any;
32+
previousName: string;
3333
}
3434

3535
export interface ILagWithDefaultQuery {
@@ -42,8 +42,8 @@ export type LagAndLeadParams = [];
4242
export interface ILagAndLeadResult {
4343
id: number;
4444
name: string;
45-
nextName: any;
46-
previousName: any;
45+
nextName: string;
46+
previousName: string;
4747
}
4848

4949
export interface ILagAndLeadQuery {
@@ -56,7 +56,7 @@ export type LagWithPartitionParams = [];
5656
export interface ILagWithPartitionResult {
5757
id: number;
5858
name: string;
59-
previousInRarity: any;
59+
previousInRarity: string;
6060
rarity: string | null;
6161
}
6262

@@ -68,9 +68,9 @@ export interface ILagWithPartitionQuery {
6868
export type FirstLastValueParams = [];
6969

7070
export interface IFirstLastValueResult {
71-
firstInRarity: any;
71+
firstInRarity: string;
7272
id: number;
73-
lastInRarity: any;
73+
lastInRarity: string;
7474
name: string;
7575
rarity: string | null;
7676
}
@@ -79,4 +79,3 @@ export interface IFirstLastValueQuery {
7979
params: FirstLastValueParams;
8080
result: IFirstLastValueResult;
8181
}
82-

tests/demo/window/rank_functions.queries.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type BasicRankParams = [];
33
export interface IBasicRankResult {
44
id: number;
55
name: string;
6-
rank: any;
6+
rank: number;
77
rarity: string | null;
88
}
99

@@ -15,7 +15,7 @@ export interface IBasicRankQuery {
1515
export type DenseRankParams = [];
1616

1717
export interface IDenseRankResult {
18-
denseRank: any;
18+
denseRank: number;
1919
id: number;
2020
name: string;
2121
rarity: string | null;
@@ -31,7 +31,7 @@ export type RankWithPartitionParams = [];
3131
export interface IRankWithPartitionResult {
3232
id: number;
3333
name: string;
34-
rank: any;
34+
rank: number;
3535
rarity: string | null;
3636
}
3737

@@ -43,12 +43,12 @@ export interface IRankWithPartitionQuery {
4343
export type MultipleRankingParams = [];
4444

4545
export interface IMultipleRankingResult {
46-
denseRank: any;
46+
denseRank: number;
4747
id: number;
4848
name: string;
49-
rank: any;
49+
rank: number;
5050
rarity: string | null;
51-
rowNum: any;
51+
rowNum: number;
5252
}
5353

5454
export interface IMultipleRankingQuery {
@@ -61,7 +61,7 @@ export type NtileQuartilesParams = [];
6161
export interface INtileQuartilesResult {
6262
id: number;
6363
name: string;
64-
quartile: any;
64+
quartile: number;
6565
}
6666

6767
export interface INtileQuartilesQuery {

tests/demo/window/rank_functions.snapshot.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export type BasicRankParams = [];
33
export interface IBasicRankResult {
44
id: number;
55
name: string;
6-
rank: any;
6+
rank: number;
77
rarity: string | null;
88
}
99

@@ -15,7 +15,7 @@ export interface IBasicRankQuery {
1515
export type DenseRankParams = [];
1616

1717
export interface IDenseRankResult {
18-
denseRank: any;
18+
denseRank: number;
1919
id: number;
2020
name: string;
2121
rarity: string | null;
@@ -31,7 +31,7 @@ export type RankWithPartitionParams = [];
3131
export interface IRankWithPartitionResult {
3232
id: number;
3333
name: string;
34-
rank: any;
34+
rank: number;
3535
rarity: string | null;
3636
}
3737

@@ -43,12 +43,12 @@ export interface IRankWithPartitionQuery {
4343
export type MultipleRankingParams = [];
4444

4545
export interface IMultipleRankingResult {
46-
denseRank: any;
46+
denseRank: number;
4747
id: number;
4848
name: string;
49-
rank: any;
49+
rank: number;
5050
rarity: string | null;
51-
rowNum: any;
51+
rowNum: number;
5252
}
5353

5454
export interface IMultipleRankingQuery {
@@ -61,11 +61,10 @@ export type NtileQuartilesParams = [];
6161
export interface INtileQuartilesResult {
6262
id: number;
6363
name: string;
64-
quartile: any;
64+
quartile: number;
6565
}
6666

6767
export interface INtileQuartilesQuery {
6868
params: NtileQuartilesParams;
6969
result: INtileQuartilesResult;
7070
}
71-

tests/demo/window/row_number.queries.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface IBasicRowNumberResult {
44
id: number;
55
name: string;
66
rarity: string | null;
7-
rowNum: any;
7+
rowNum: number;
88
}
99

1010
export interface IBasicRowNumberQuery {
@@ -18,7 +18,7 @@ export interface IRowNumberWithPartitionResult {
1818
id: number;
1919
name: string;
2020
rarity: string | null;
21-
rowNum: any;
21+
rowNum: number;
2222
}
2323

2424
export interface IRowNumberWithPartitionQuery {
@@ -32,7 +32,7 @@ export interface IRowNumberWithWhereResult {
3232
id: number;
3333
name: string;
3434
rarity: string | null;
35-
rowNum: any;
35+
rowNum: number;
3636
}
3737

3838
export interface IRowNumberWithWhereQuery {
@@ -46,7 +46,7 @@ export interface IRowNumberWithParamsResult {
4646
id: number;
4747
name: string;
4848
rarity: string | null;
49-
rowNum: any;
49+
rowNum: number;
5050
}
5151

5252
export interface IRowNumberWithParamsQuery {

tests/demo/window/row_number.snapshot.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface IBasicRowNumberResult {
44
id: number;
55
name: string;
66
rarity: string | null;
7-
rowNum: any;
7+
rowNum: number;
88
}
99

1010
export interface IBasicRowNumberQuery {
@@ -18,7 +18,7 @@ export interface IRowNumberWithPartitionResult {
1818
id: number;
1919
name: string;
2020
rarity: string | null;
21-
rowNum: any;
21+
rowNum: number;
2222
}
2323

2424
export interface IRowNumberWithPartitionQuery {
@@ -32,7 +32,7 @@ export interface IRowNumberWithWhereResult {
3232
id: number;
3333
name: string;
3434
rarity: string | null;
35-
rowNum: any;
35+
rowNum: number;
3636
}
3737

3838
export interface IRowNumberWithWhereQuery {
@@ -46,11 +46,10 @@ export interface IRowNumberWithParamsResult {
4646
id: number;
4747
name: string;
4848
rarity: string | null;
49-
rowNum: any;
49+
rowNum: number;
5050
}
5151

5252
export interface IRowNumberWithParamsQuery {
5353
params: RowNumberWithParamsParams;
5454
result: IRowNumberWithParamsResult;
5555
}
56-

0 commit comments

Comments
 (0)