pytrilogy 0.0.3.18__py3-none-any.whl → 0.0.3.19__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pytrilogy might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: pytrilogy
3
- Version: 0.0.3.18
3
+ Version: 0.0.3.19
4
4
  Summary: Declarative, typed query language that compiles to SQL.
5
5
  Home-page:
6
6
  Author:
@@ -1,4 +1,4 @@
1
- trilogy/__init__.py,sha256=jrbWIot3RjwpdU7heWZocVfCD4BBtWw40jekbkQLvdg,303
1
+ trilogy/__init__.py,sha256=KxuTkRMFOlZrKTEJ94OxeHN92vhWtBys2MRzNG8uGBU,303
2
2
  trilogy/compiler.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  trilogy/constants.py,sha256=qZ1d0hoKPPV2HHCoFwPYTVB7b6bXjpWvXd3lE-zEhy8,1494
4
4
  trilogy/engine.py,sha256=OK2RuqCIUId6yZ5hfF8J1nxGP0AJqHRZiafcowmW0xc,1728
@@ -21,12 +21,12 @@ trilogy/core/internal.py,sha256=iicDBlC6nM8d7e7jqzf_ZOmpUsW8yrr2AA8AqEiLx-s,1577
21
21
  trilogy/core/optimization.py,sha256=xGO8piVsLrpqrx-Aid_Y56_5slSv4eZmlP64hCHRiEc,7957
22
22
  trilogy/core/query_processor.py,sha256=Do8YpdPBdsbKtl9n37hobzk8SORMGqH-e_zNNxd-BE4,19456
23
23
  trilogy/core/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
- trilogy/core/models/author.py,sha256=u2wS2rg3Qsqb5xNRUYvjgA7jOwQS2dMZzytlwJtglBs,70859
24
+ trilogy/core/models/author.py,sha256=gsn_vkm8gvnwTvzgqOL5v3X1Lx4n8xg32tRwk_9Mxnc,73494
25
25
  trilogy/core/models/build.py,sha256=bO1qYvuGl6LeNGgsfS6ZHAzZBR2lBPLg-QJymp9hgkU,57235
26
26
  trilogy/core/models/build_environment.py,sha256=8UggvlPU708GZWYPJMc_ou2r7M3TY2g69eqGvz03YX0,5528
27
27
  trilogy/core/models/core.py,sha256=nb4h1HHm5_qwmUkYth4zRhEttS1EtsMZCP4vT20EEAE,10326
28
28
  trilogy/core/models/datasource.py,sha256=6RjJUd2u4nYmEwFBpJlM9LbHVYDv8iHJxqiBMZqUrwI,9422
29
- trilogy/core/models/environment.py,sha256=4w1a9iHdRuOuSoZxz5Kzoy61Vjj4TZVXzKkCSP6c-Ow,26298
29
+ trilogy/core/models/environment.py,sha256=RlHNrRer4p1uSQM030iwGJL82M1hMyY5p8a550XTfUI,26606
30
30
  trilogy/core/models/execute.py,sha256=kxkw14vgUudHHELXLo73AHiaMEpLfiv0qAvbxjJxn_k,33929
31
31
  trilogy/core/optimizations/__init__.py,sha256=EBanqTXEzf1ZEYjAneIWoIcxtMDite5-n2dQ5xcfUtg,356
32
32
  trilogy/core/optimizations/base_optimization.py,sha256=gzDOKImoFn36k7XBD3ysEYDnbnb6vdVIztUfFQZsGnM,513
@@ -90,14 +90,14 @@ trilogy/parsing/common.py,sha256=99tDKrpQTk-SpyTXUqKFtm-lfmhjCOQIn25hxbQvRRg,214
90
90
  trilogy/parsing/config.py,sha256=Z-DaefdKhPDmSXLgg5V4pebhSB0h590vI0_VtHnlukI,111
91
91
  trilogy/parsing/exceptions.py,sha256=92E5i2frv5hj9wxObJZsZqj5T6bglvPzvdvco_vW1Zk,38
92
92
  trilogy/parsing/helpers.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
93
- trilogy/parsing/parse_engine.py,sha256=Hodpr2QjJNHSS5xGP-wPFjKK9gpygGG6loCikyRiwH8,60287
93
+ trilogy/parsing/parse_engine.py,sha256=AAzekSp9tcpI3lbfDoghB6zi7FkjLZIbZRSD9GLbDmU,59605
94
94
  trilogy/parsing/render.py,sha256=o_XuQWhcwx1lD9eGVqkqZEwkmQK0HdmWWokGBtdeH4I,17837
95
95
  trilogy/parsing/trilogy.lark,sha256=7libFS5HNiyHJYzr5_lEiY-Lpqit04_PgyIPHMZT7-w,12928
96
96
  trilogy/scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
97
97
  trilogy/scripts/trilogy.py,sha256=1L0XrH4mVHRt1C9T1HnaDv2_kYEfbWTb5_-cBBke79w,3774
98
- pytrilogy-0.0.3.18.dist-info/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
99
- pytrilogy-0.0.3.18.dist-info/METADATA,sha256=V6EpL5Z-9Xief5rqKaourYFLzmRMZH9jM31orwdgYAk,8984
100
- pytrilogy-0.0.3.18.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
101
- pytrilogy-0.0.3.18.dist-info/entry_points.txt,sha256=0petKryjvvtEfTlbZC1AuMFumH_WQ9v8A19LvoS6G6c,54
102
- pytrilogy-0.0.3.18.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
103
- pytrilogy-0.0.3.18.dist-info/RECORD,,
98
+ pytrilogy-0.0.3.19.dist-info/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
99
+ pytrilogy-0.0.3.19.dist-info/METADATA,sha256=Gm_Cjipo1VE5HncqCyT3MNW3YLoLDAmV0P7pScqO_Ro,8984
100
+ pytrilogy-0.0.3.19.dist-info/WHEEL,sha256=52BFRY2Up02UkjOa29eZOS2VxUrpPORXg1pkohGGUS8,91
101
+ pytrilogy-0.0.3.19.dist-info/entry_points.txt,sha256=0petKryjvvtEfTlbZC1AuMFumH_WQ9v8A19LvoS6G6c,54
102
+ pytrilogy-0.0.3.19.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
103
+ pytrilogy-0.0.3.19.dist-info/RECORD,,
trilogy/__init__.py CHANGED
@@ -4,6 +4,6 @@ from trilogy.dialect.enums import Dialects
4
4
  from trilogy.executor import Executor
5
5
  from trilogy.parser import parse
6
6
 
7
- __version__ = "0.0.3.18"
7
+ __version__ = "0.0.3.19"
8
8
 
9
9
  __all__ = ["parse", "Executor", "Dialects", "Environment", "CONFIG"]
@@ -318,6 +318,21 @@ class Conditional(Mergeable, ConceptArgs, Namespaced, BaseModel):
318
318
  operator=self.operator,
319
319
  )
320
320
 
321
+ def with_reference_replacement(self, source, target):
322
+ return self.__class__.model_construct(
323
+ left=(
324
+ self.left.with_reference_replacement(source, target)
325
+ if isinstance(self.left, Mergeable)
326
+ else self.left
327
+ ),
328
+ right=(
329
+ self.right.with_reference_replacement(source, target)
330
+ if isinstance(self.right, Mergeable)
331
+ else self.right
332
+ ),
333
+ operator=self.operator,
334
+ )
335
+
321
336
  @property
322
337
  def concept_arguments(self) -> Sequence[ConceptRef]:
323
338
  """Return concepts directly referenced in where clause"""
@@ -2135,6 +2150,52 @@ class AlignItem(Namespaced, BaseModel):
2135
2150
  )
2136
2151
 
2137
2152
 
2153
+ class CustomFunctionFactory:
2154
+ def __init__(
2155
+ self, function: Expr, namespace: str, function_arguments: list[ArgBinding]
2156
+ ):
2157
+ self.namespace = namespace
2158
+ self.function = function
2159
+ self.function_arguments = function_arguments
2160
+
2161
+ def with_namespace(self, namespace: str):
2162
+ self.namespace = namespace
2163
+ self.function = (
2164
+ self.function.with_namespace(namespace)
2165
+ if isinstance(self.function, Namespaced)
2166
+ else self.function
2167
+ )
2168
+ self.function_arguments = [
2169
+ x.with_namespace(namespace) for x in self.function_arguments
2170
+ ]
2171
+ return self
2172
+
2173
+ def __call__(self, *creation_args: list[Expr]):
2174
+ nout = (
2175
+ self.function.model_copy(deep=True)
2176
+ if isinstance(self.function, BaseModel)
2177
+ else self.function
2178
+ )
2179
+ creation_arg_list: list[Expr] = list(creation_args)
2180
+ if len(creation_args) < len(self.function_arguments):
2181
+ for binding in self.function_arguments[len(creation_arg_list) :]:
2182
+ if binding.default is None:
2183
+ raise ValueError(f"Missing argument {binding.name}")
2184
+ creation_arg_list.append(binding.default)
2185
+ if isinstance(nout, Mergeable):
2186
+ for idx, x in enumerate(creation_arg_list):
2187
+ if self.namespace == DEFAULT_NAMESPACE:
2188
+ target = f"{DEFAULT_NAMESPACE}.{self.function_arguments[idx].name}"
2189
+ else:
2190
+ target = self.function_arguments[idx].name
2191
+ nout = (
2192
+ nout.with_reference_replacement(target, x)
2193
+ if isinstance(nout, Mergeable)
2194
+ else nout
2195
+ )
2196
+ return nout
2197
+
2198
+
2138
2199
  class Metadata(BaseModel):
2139
2200
  """Metadata container object.
2140
2201
  TODO: support arbitrary tags"""
@@ -2164,10 +2225,20 @@ class Comment(BaseModel):
2164
2225
  text: str
2165
2226
 
2166
2227
 
2167
- class ArgBinding(BaseModel):
2228
+ class ArgBinding(Namespaced, BaseModel):
2168
2229
  name: str
2169
2230
  default: Expr | None = None
2170
2231
 
2232
+ def with_namespace(self, namespace):
2233
+ return ArgBinding(
2234
+ name=address_with_namespace(self.name, namespace),
2235
+ default=(
2236
+ self.default.with_namespace(namespace)
2237
+ if isinstance(self.default, Namespaced)
2238
+ else self.default
2239
+ ),
2240
+ )
2241
+
2171
2242
 
2172
2243
  class CustomType(BaseModel):
2173
2244
  name: str
@@ -9,7 +9,6 @@ from typing import (
9
9
  TYPE_CHECKING,
10
10
  Annotated,
11
11
  Any,
12
- Callable,
13
12
  Dict,
14
13
  ItemsView,
15
14
  List,
@@ -40,6 +39,7 @@ from trilogy.core.exceptions import (
40
39
  from trilogy.core.models.author import (
41
40
  Concept,
42
41
  ConceptRef,
42
+ CustomFunctionFactory,
43
43
  CustomType,
44
44
  Function,
45
45
  SelectLineage,
@@ -207,7 +207,7 @@ class Environment(BaseModel):
207
207
  datasources: Annotated[
208
208
  EnvironmentDatasourceDict, PlainValidator(validate_datasources)
209
209
  ] = Field(default_factory=EnvironmentDatasourceDict)
210
- functions: Dict[str, Callable[..., Any]] = Field(default_factory=dict)
210
+ functions: Dict[str, CustomFunctionFactory] = Field(default_factory=dict)
211
211
  data_types: Dict[str, CustomType] = Field(default_factory=dict)
212
212
  named_statements: Dict[str, SelectLineage] = Field(default_factory=dict)
213
213
  imports: Dict[str, list[Import]] = Field(
@@ -434,6 +434,14 @@ class Environment(BaseModel):
434
434
  self.alias_origin_lookup[address_with_namespace(key, alias)] = (
435
435
  val.with_namespace(alias)
436
436
  )
437
+
438
+ for key, function in source.functions.items():
439
+ if same_namespace:
440
+ self.functions[key] = function
441
+ else:
442
+ self.functions[address_with_namespace(key, alias)] = (
443
+ function.with_namespace(alias)
444
+ )
437
445
  return self
438
446
 
439
447
  def add_file_import(
@@ -57,13 +57,13 @@ from trilogy.core.models.author import (
57
57
  Concept,
58
58
  ConceptRef,
59
59
  Conditional,
60
+ CustomFunctionFactory,
60
61
  CustomType,
61
62
  Expr,
62
63
  FilterItem,
63
64
  Function,
64
65
  Grain,
65
66
  HavingClause,
66
- Mergeable,
67
67
  Metadata,
68
68
  OrderBy,
69
69
  OrderItem,
@@ -1198,23 +1198,11 @@ class ParseToObjects(Transformer):
1198
1198
  function_arguments: list[ArgBinding] = args[1]
1199
1199
  output = args[2]
1200
1200
 
1201
- def function_factory(*creation_args: list[Expr]):
1202
- nout = output.copy(deep=True)
1203
- creation_arg_list: list[Expr] = list(creation_args)
1204
- if len(creation_args) < len(function_arguments):
1205
- for binding in function_arguments[len(creation_arg_list) :]:
1206
- if binding.default is None:
1207
- raise ValueError(f"Missing argument {binding.name}")
1208
- creation_arg_list.append(binding.default)
1209
- if isinstance(nout, Mergeable):
1210
- for idx, x in enumerate(creation_arg_list):
1211
- # these will always be local namespace
1212
- nout = nout.with_reference_replacement(
1213
- f"{DEFAULT_NAMESPACE}.{function_arguments[idx].name}", x
1214
- )
1215
- return nout
1216
-
1217
- self.environment.functions[identity] = function_factory
1201
+ self.environment.functions[identity] = CustomFunctionFactory(
1202
+ function=output,
1203
+ namespace=self.environment.namespace,
1204
+ function_arguments=function_arguments,
1205
+ )
1218
1206
  return FunctionDeclaration(name=identity, args=function_arguments, expr=output)
1219
1207
 
1220
1208
  def custom_function(self, args):