-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_rules.py
More file actions
276 lines (264 loc) · 7.96 KB
/
_rules.py
File metadata and controls
276 lines (264 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import builtins
import keyword
from dataclasses import dataclass, field
import polars as pl
from pyochain import Dict, Iter, Seq, Set
from sqlglot.parsers.duckdb import DuckDBParser
from .._utils import Builtins, Pql, Typing
from ._dtypes import Categories, DuckDbTypes
CONVERTER = Iter(DuckDbTypes).map(lambda t: (t, t.into_py())).collect(dict)
"""DuckDB type -> Python type hint mapping."""
DK_FUNC_KEYS = pl.LazyFrame({"glot_name": tuple(DuckDBParser.FUNCTIONS)}) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType]
"""DuckDBParser.FUNCTIONS keys as a single-column LazyFrame."""
SHADOWERS = (
Pql
.into_iter()
.chain(Typing, Builtins)
.map(lambda s: s.value)
.chain(dir(builtins), keyword.kwlist)
.insert("l")
.collect(Set)
)
"""Names that should be renamed to avoid shadowing."""
RENAME_RULES = Dict.from_ref({
"list": "implode",
"json": "json_parse",
"map": "to_map",
"kurtosis": "kurtosis_samp",
"isnan": "is_nan",
"isinf": "is_inf",
"isfinite": "is_finite",
"bool_and": "all",
"bool_or": "any",
"round": "round_from_zero", # allow to expose round as parametrizable method
"entropy": "entropy_shannon", # allow to use entropy for polars aligned version
"list_unique": "list_n_unique",
"array_unique": "array_n_unique",
})
"""Explicit SQL function name -> generated Python method name mapping."""
# TODO: handle order_by arguments for those
AGG_ORDER_SENSITIVE = Set({
"any_value",
"arg_max",
"arg_max_null",
"arg_max_nulls_last",
"arg_min",
"arg_min_null",
"arg_min_nulls_last",
"avg",
"favg",
"fsum",
"geometric_mean",
"first",
"last",
"list",
"mode",
"product",
"string_agg",
"group_concat",
"listagg",
"sum",
"weighted_avg",
})
# TODO: currently we handle SOME of those manually. need to check if available in duckdb, sqlglot, or implement this correctly
WINDOW_FUNC = Set({
"fill",
"row_number",
"rank",
"cume_dist",
"percent_rank",
"lag",
"lead",
"first_value",
"last_value",
"nth_value",
"ntile",
})
SPECIAL_CASES = Set({
# "raw" operators
"+",
"-",
"/",
"*",
"//",
"%",
"**",
"&",
"|",
"^",
"~",
"&&",
"||",
"@",
"^@",
"@>",
"<@",
"<->",
"<=>",
"<<",
">>",
"->>",
"~~",
"!~~",
"~~*",
"!~~*",
"~~~",
"!__postfix",
"!",
"…",
# Primary operators, we want to handle them manually
"add",
"subtract",
"multiply",
"divide",
"alias", # conflicts with duckdb alias method
# Need arg swapping
"log", # Need to swap argument order to take self.inner as value and not as base
"date_trunc", # Need to swap argument order to take self.inner as timestamp and not as precision
"datetrunc", # alias of date_trunc, same issue
# Need to transform the expr input in a lambda in all cases, better to handle it manually
"array_filter",
"list_filter",
"filter",
# Generic functions that cause too much conflicts with other names
"greatest", # Has 5 categories, same behavior across thoses, no namespace needed
"least", # Has 5 categories, same behavior across thoses, no namespace needed
"concat", # too much conflict with list_concat, array_concat, etc..
# sqlglot issues
"xor", # Actual match casing logic gives it `XOR` when really it should be `BitwiseXor`
"quantile", # Allow to make quantile a parametrizable method
# Specific handling of arguments needed
"array_sort",
"list_sort",
"max_by",
"min_by",
# Makes more sense as a pure function
"generate_series", # Otherwise we need to do `lit(x).generate_series(y)` which is a bit weird
})
"""Function to exclude by name, either because they require special handling or because they conflict with existing names."""
PREFIXES = Set((
"__", # Internal functions
"current_", # Utility fns
"has_", # Utility fns
"pg_", # Postgres fns
"icu_", # timestamp extension
))
"""Functions to exclude by prefixes."""
def _rule[T](*args: T) -> Seq[T]:
return Seq(args)
@dataclass(slots=True)
class NamespaceSpec:
name: str
doc: str
prefixes: Seq[str]
strip_prefixes: Seq[str]
categories: Seq[Categories] = field(default_factory=lambda: Seq[Categories](()))
explicit_names: Seq[str] = field(default_factory=lambda: Seq[str](()))
NAMESPACE_SPECS = Seq((
NamespaceSpec(
name="ListFns",
doc="Mixin providing auto-generated DuckDB list functions as methods.",
prefixes=Seq(("list_",)),
categories=_rule(Categories.LIST),
strip_prefixes=_rule("list_", "array_"),
),
NamespaceSpec(
name="StructFns",
doc="Mixin providing auto-generated DuckDB struct functions as methods.",
prefixes=Seq(("struct_",)),
categories=_rule(Categories.STRUCT),
strip_prefixes=Seq(("struct_",)),
),
NamespaceSpec(
name="RegexFns",
doc="Mixin providing auto-generated DuckDB regex functions as methods.",
prefixes=Seq(("regexp_",)),
categories=Seq((Categories.REGEX,)),
strip_prefixes=_rule("regexp_", "str_", "string_"),
),
NamespaceSpec(
name="StringFns",
doc="Mixin providing auto-generated DuckDB string functions as methods.",
prefixes=_rule("string_", "str_"),
categories=_rule(Categories.STRING, Categories.TEXT_SIMILARITY),
strip_prefixes=_rule("string_", "str_"),
explicit_names=_rule("strftime", "strptime"),
),
NamespaceSpec(
name="DateTimeFns",
doc="Mixin providing auto-generated DuckDB datetime functions as methods.",
prefixes=_rule("date", "epoch", "iso", "time", "day", "month", "week", "year"),
categories=_rule(Categories.TIMESTAMP, Categories.DATE),
strip_prefixes=_rule("date_", "date"),
explicit_names=_rule(
"microsecond",
"nanosecond",
"millisecond",
"second",
"minute",
"hour",
"quarter",
"decade",
"century",
"millennium",
"era",
"julian",
"last_day",
"to_timestamp",
"to_microseconds",
"to_milliseconds",
"to_seconds",
"to_minutes",
"to_hours",
"to_days",
"to_weeks",
"to_months",
"to_quarters",
"to_years",
"to_decades",
"to_centuries",
"to_millennia",
"make_date",
"make_date_month_day",
"make_time",
"make_timestamp",
"make_timestamp_ms",
"make_timestamp_ns",
"make_timestamptz",
"normalized_interval",
),
),
NamespaceSpec(
name="ArrayFns",
doc="Mixin providing auto-generated DuckDB array functions as methods.",
prefixes=Seq(("array_",)),
categories=Seq((Categories.ARRAY,)),
strip_prefixes=Seq(("array_",)),
),
NamespaceSpec(
name="JsonFns",
doc="Mixin providing auto-generated DuckDB JSON functions as methods.",
prefixes=Seq(("json_",)),
strip_prefixes=Seq(("json_",)),
),
NamespaceSpec(
name="MapFns",
doc="Mixin providing auto-generated DuckDB map functions as methods.",
prefixes=Seq(("map_",)),
strip_prefixes=Seq(("map_",)),
),
NamespaceSpec(
name="EnumFns",
doc="Mixin providing auto-generated DuckDB enum functions as methods.",
prefixes=Seq(("enum_",)),
strip_prefixes=Seq(("enum_",)),
),
NamespaceSpec(
name="GeoSpatialFns",
doc="Mixin providing auto-generated DuckDB geospatial functions as methods.",
categories=Seq((Categories.GEOMETRY,)),
prefixes=Seq(("st_", "ST_")),
strip_prefixes=Seq(("st_", "ST_")),
),
))
"""Namespace metadata and function prefixes."""