pytrilogy 0.0.2.8__py3-none-any.whl → 0.0.2.9__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.

Files changed (31) hide show
  1. {pytrilogy-0.0.2.8.dist-info → pytrilogy-0.0.2.9.dist-info}/METADATA +1 -1
  2. {pytrilogy-0.0.2.8.dist-info → pytrilogy-0.0.2.9.dist-info}/RECORD +31 -31
  3. {pytrilogy-0.0.2.8.dist-info → pytrilogy-0.0.2.9.dist-info}/WHEEL +1 -1
  4. trilogy/__init__.py +1 -1
  5. trilogy/constants.py +1 -0
  6. trilogy/core/models.py +144 -54
  7. trilogy/core/optimization.py +44 -5
  8. trilogy/core/optimizations/inline_datasource.py +14 -8
  9. trilogy/core/optimizations/predicate_pushdown.py +73 -44
  10. trilogy/core/processing/concept_strategies_v3.py +69 -28
  11. trilogy/core/processing/node_generators/common.py +42 -16
  12. trilogy/core/processing/node_generators/filter_node.py +89 -48
  13. trilogy/core/processing/node_generators/group_node.py +3 -1
  14. trilogy/core/processing/node_generators/rowset_node.py +13 -54
  15. trilogy/core/processing/node_generators/select_node.py +10 -13
  16. trilogy/core/processing/node_generators/unnest_node.py +5 -3
  17. trilogy/core/processing/node_generators/window_node.py +23 -2
  18. trilogy/core/processing/nodes/__init__.py +34 -6
  19. trilogy/core/processing/nodes/base_node.py +67 -13
  20. trilogy/core/processing/nodes/filter_node.py +3 -0
  21. trilogy/core/processing/nodes/group_node.py +3 -0
  22. trilogy/core/processing/nodes/merge_node.py +1 -11
  23. trilogy/core/processing/nodes/select_node_v2.py +1 -0
  24. trilogy/core/processing/utility.py +29 -10
  25. trilogy/core/query_processor.py +47 -20
  26. trilogy/dialect/base.py +11 -4
  27. trilogy/parsing/parse_engine.py +20 -1
  28. trilogy/parsing/trilogy.lark +3 -1
  29. {pytrilogy-0.0.2.8.dist-info → pytrilogy-0.0.2.9.dist-info}/LICENSE.md +0 -0
  30. {pytrilogy-0.0.2.8.dist-info → pytrilogy-0.0.2.9.dist-info}/entry_points.txt +0 -0
  31. {pytrilogy-0.0.2.8.dist-info → pytrilogy-0.0.2.9.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pytrilogy
3
- Version: 0.0.2.8
3
+ Version: 0.0.2.9
4
4
  Summary: Declarative, typed query language that compiles to SQL.
5
5
  Home-page:
6
6
  Author:
@@ -1,6 +1,6 @@
1
- trilogy/__init__.py,sha256=zJsrRvfQaVItXzGc-5Fgqgq2jhQrGMrKaI1adR7HE84,290
1
+ trilogy/__init__.py,sha256=aJ0glX-aTfIGPSjtNqSF-k7RYaH3odx9C8DPBaxv1_4,290
2
2
  trilogy/compiler.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- trilogy/constants.py,sha256=3YKp-xvS7yB5z9t9YDcGofkrdZ6v13iePDnn4O5mwTE,886
3
+ trilogy/constants.py,sha256=gdaaoXq08_F7UVbZpJcUOfU-AcpykOE2blby6dL1L8c,922
4
4
  trilogy/engine.py,sha256=R5ubIxYyrxRExz07aZCUfrTsoXCHQ8DKFTDsobXdWdA,1102
5
5
  trilogy/executor.py,sha256=5cRbU4Rj7p1pNV76rfp1pz704Hx_0q8_O8HFURjgXxQ,11016
6
6
  trilogy/parser.py,sha256=UtuqSiGiCjpMAYgo1bvNq-b7NSzCA5hzbUW31RXaMII,281
@@ -16,40 +16,40 @@ trilogy/core/exceptions.py,sha256=NvV_4qLOgKXbpotgRf7c8BANDEvHxlqRPaA53IThQ2o,56
16
16
  trilogy/core/functions.py,sha256=ARJAyBjeS415-54k3G_bx807rkPZonEulMaLRxSP7vU,10371
17
17
  trilogy/core/graph_models.py,sha256=oJUMSpmYhqXlavckHLpR07GJxuQ8dZ1VbB1fB0KaS8c,2036
18
18
  trilogy/core/internal.py,sha256=jNGFHKENnbMiMCtAgsnLZYVSENDK4b5ALecXFZpTDzQ,1075
19
- trilogy/core/models.py,sha256=ubtmdVsm_7QkigbvlAXEe7SXbZB502GLean0jFu8VLU,139884
20
- trilogy/core/optimization.py,sha256=A8S9C9H5RcQcFSQLYtEEBnm-r1CW_e9GEWlLK7q3MqA,4930
21
- trilogy/core/query_processor.py,sha256=fIRJWLBfMeKZ_o1-QB2rsQ8jZwsd9zxYkZ5QQzkGBus,17678
19
+ trilogy/core/models.py,sha256=jkJnt9Os8dATWJDuS5W_RPlmVKu82OEaoel6icX8ReQ,142371
20
+ trilogy/core/optimization.py,sha256=RJmDr2f9hyFePF-B6LpmHkj69-AzUm-nNtvO59899O8,6601
21
+ trilogy/core/query_processor.py,sha256=AWMX7Qguo6iI_e3Cm6iHrkvTdC_c8kWi22uPR84HiFA,18561
22
22
  trilogy/core/optimizations/__init__.py,sha256=pxRzNzd2g8oRMy4f_ub5va6bNS2pd4hnyp9JBzTKc1E,300
23
23
  trilogy/core/optimizations/base_optimization.py,sha256=tWWT-xnTbnEU-mNi_isMNbywm8B9WTRsNFwGpeh3rqE,468
24
24
  trilogy/core/optimizations/inline_constant.py,sha256=kHNyc2UoaPVdYfVAPAFwnWuk4sJ_IF5faRtVcDOrBtw,1110
25
- trilogy/core/optimizations/inline_datasource.py,sha256=PyThSsQeZ6fi5sM2mUprC8kXXKo1sP8qLVwaGqpFDJI,3428
26
- trilogy/core/optimizations/predicate_pushdown.py,sha256=iVZV_BVaL3I6wlELPOuJAiuF0heOM7bCUnNqyCxxRVw,6370
25
+ trilogy/core/optimizations/inline_datasource.py,sha256=AATzQ6YrtW_1-aQFjQyTYqEYKBoMFhek7ADfBr4uUdQ,3634
26
+ trilogy/core/optimizations/predicate_pushdown.py,sha256=4Y6zfJN3VVexkD6p9IYWN4BTL8RwW6hyNb0VQZ8ETdI,8020
27
27
  trilogy/core/processing/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
- trilogy/core/processing/concept_strategies_v3.py,sha256=A0-RVwoqD06gUapGRD4SANj3g9hniKPWs7LvqyeHtbg,24445
28
+ trilogy/core/processing/concept_strategies_v3.py,sha256=yjCiGKzghjAc0E0QAcdJ1Xr5RXHAQL2zMPOz-dMhEzM,25431
29
29
  trilogy/core/processing/graph_utils.py,sha256=aq-kqk4Iado2HywDxWEejWc-7PGO6Oa-ZQLAM6XWPHw,1199
30
- trilogy/core/processing/utility.py,sha256=rhnkn3KgB1MW17mZYSEZ8XFEbC__B7PPmZgpDql4Myg,13614
30
+ trilogy/core/processing/utility.py,sha256=0Teib4_VZlURRy1TvjDB6VBBxsqfJ7zTm2sXfPQjgXc,13950
31
31
  trilogy/core/processing/node_generators/__init__.py,sha256=-mzYkRsaRNa_dfTckYkKVFSR8h8a3ihEiPJDU_tAmDo,672
32
32
  trilogy/core/processing/node_generators/basic_node.py,sha256=4242PNGTCm2tklqMIkqVu5Iv4m_IeTnOYXxDveuCDZM,2856
33
- trilogy/core/processing/node_generators/common.py,sha256=Z3R5Ucb0s09pzs3lkz-t9sI2A5R-SIum1UdwGvqKpXA,8975
34
- trilogy/core/processing/node_generators/filter_node.py,sha256=0zey11fmXhI-ihkv-cK43tUKHlwG10cHYogByOnGbiw,5433
35
- trilogy/core/processing/node_generators/group_node.py,sha256=u9X0gAEY7AAWwYsK4g3jPgxcTwSSlI1uDQqNIY6vfG8,2988
33
+ trilogy/core/processing/node_generators/common.py,sha256=lDBRq9X6dQ_xSwXxLLNDq2pW8D-XwAY-ylTJLMugkLw,9525
34
+ trilogy/core/processing/node_generators/filter_node.py,sha256=T4qmS_XvNDrvzm7srjpmlZQ1IkKhifn5oP4Gkc7Sk7Y,7133
35
+ trilogy/core/processing/node_generators/group_node.py,sha256=JwT8qYeGbozrZNGAlpndc9aaIN7K9ROYkkc2rAS8Qz8,3079
36
36
  trilogy/core/processing/node_generators/group_to_node.py,sha256=nzITnhaALIT7FMonyo16nNo-kSrLfefa9sZBYecrvkU,2887
37
37
  trilogy/core/processing/node_generators/multiselect_node.py,sha256=vP84dnLQy6dtypi6mUbt9sMAcmmrTgQ1Oz4GI6X1IEo,6421
38
38
  trilogy/core/processing/node_generators/node_merge_node.py,sha256=yZ75lsl8RtM8kr-XQxqZEU6fm-JN5DTJxNbneayu36c,12319
39
- trilogy/core/processing/node_generators/rowset_node.py,sha256=wFkzQtXfudvAGifySvobgc9Qo1usTEEKpSbjkzchNy0,6282
40
- trilogy/core/processing/node_generators/select_node.py,sha256=XGcz74XsWv5POWhV2gGC_jbi7T9g7sY-VtP3sjlppFc,19057
41
- trilogy/core/processing/node_generators/unnest_node.py,sha256=6CH66eGwpadNX7TzUhWZ8aqIisOtQeHINbLV6X3QBUk,1779
42
- trilogy/core/processing/node_generators/window_node.py,sha256=9nXUXUgQrNczU1gaOqhOZPNzCUxw-lkxt0R7HORI6ss,2582
43
- trilogy/core/processing/nodes/__init__.py,sha256=-8ONG7Nqa6sNLpst5wNNyMigyaHRfUKGdE3xce8E3mM,3998
44
- trilogy/core/processing/nodes/base_node.py,sha256=7AeDm9WK3uiDGUL6QefeZ8Ok9OcWhbh5h1f9K0hr8IQ,11191
45
- trilogy/core/processing/nodes/filter_node.py,sha256=stY0tkppSdfCD43ZKRfCxfX9XxdLJ9gEtDA2_wPDcGE,1962
46
- trilogy/core/processing/nodes/group_node.py,sha256=j3vtFQIn_t4EfCke8dGULlwVkG2KKPYfgGwo2rvgcww,6322
47
- trilogy/core/processing/nodes/merge_node.py,sha256=RAuBPHPIQcGHqyU_R4HT6Kq9E40fWjQoOUCIngLEGQI,14629
48
- trilogy/core/processing/nodes/select_node_v2.py,sha256=COu-WPuyabGAc3HTkJB-_7eNzR4OgxIL4YDuZIRrST0,7179
39
+ trilogy/core/processing/node_generators/rowset_node.py,sha256=6KVnuk75mRzWJ-jIk7e8azN8BIPPuCn-VxPlxDqfPVE,4616
40
+ trilogy/core/processing/node_generators/select_node.py,sha256=E8bKOAUpwLwZy1iiaFVD5sM4XK-eFpHgijdyIWLMyH4,18904
41
+ trilogy/core/processing/node_generators/unnest_node.py,sha256=gHjurhr86JFkbq6vxTHDQGDKt95EdotSFHVKgrR_Z3Q,1860
42
+ trilogy/core/processing/node_generators/window_node.py,sha256=lFfmEjX_mLB7MuOM6CuKNnks1CabokGImpwhbQzjnkE,3283
43
+ trilogy/core/processing/nodes/__init__.py,sha256=jyduHk96j5fpju72sc8swOiBjR3Md866kt8JZGkp3ZU,4866
44
+ trilogy/core/processing/nodes/base_node.py,sha256=KVLAPLlo5QQq9Cm6lTLsqLN7l8EyXGxQkKrZFHdyRp8,12871
45
+ trilogy/core/processing/nodes/filter_node.py,sha256=DBOSGFfkiILrZa1BlLv2uxUSkgWtSIKiZplqyKXPjg8,2132
46
+ trilogy/core/processing/nodes/group_node.py,sha256=zLBOM_nWxSG_jIlRyayFIBDt-8yP8TIPgjNSewUCGlc,6492
47
+ trilogy/core/processing/nodes/merge_node.py,sha256=ZUcFIm5v4hip_Ml_oJXqVMrevUkE62wvPrvCrO9FWAc,14272
48
+ trilogy/core/processing/nodes/select_node_v2.py,sha256=QuXNcwgjTRYamOoIooGrp4ie6INcqA9whtC5LZWjD8s,7180
49
49
  trilogy/core/processing/nodes/unnest_node.py,sha256=JFtm90IVM-46aCYkTNIaJah6v9ApAfonjVhcVM1HmDE,1903
50
50
  trilogy/core/processing/nodes/window_node.py,sha256=X7qxLUKd3tekjUUsmH_4vz5b-U89gMnGd04VBxuu2Ns,1280
51
51
  trilogy/dialect/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
52
- trilogy/dialect/base.py,sha256=kamdgq1zTO2bF3bMAfvNZBNO3LKP-n2kYXPJKM6fMzw,30102
52
+ trilogy/dialect/base.py,sha256=qYPs_d-5Qu0ZK3Y1zQfwzCNNFKbWv0VlEbfl0DWsSrE,30377
53
53
  trilogy/dialect/bigquery.py,sha256=15KJ-cOpBlk9O7FPviPgmg8xIydJeKx7WfmL3SSsPE8,2953
54
54
  trilogy/dialect/common.py,sha256=HVNPL8dGyQjT2REruV5C2YPpBbVR7KlD0akVM7GcuPI,3329
55
55
  trilogy/dialect/config.py,sha256=tLVEMctaTDhUgARKXUNfHUcIolGaALkQ0RavUvXAY4w,2994
@@ -69,14 +69,14 @@ trilogy/parsing/common.py,sha256=zNd5buKxK4z9WSszOk4zOI0GexDFukzZtdNfQxg3kVw,621
69
69
  trilogy/parsing/config.py,sha256=Z-DaefdKhPDmSXLgg5V4pebhSB0h590vI0_VtHnlukI,111
70
70
  trilogy/parsing/exceptions.py,sha256=92E5i2frv5hj9wxObJZsZqj5T6bglvPzvdvco_vW1Zk,38
71
71
  trilogy/parsing/helpers.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
72
- trilogy/parsing/parse_engine.py,sha256=cIoQuDGaCx_HYgNjOvG6GDVGOu5pcLBylZsK-wy5r6Q,61098
72
+ trilogy/parsing/parse_engine.py,sha256=L-Mlb1rdFG4UPxx1YY1JdNUe2dkGuXU6fuQzM5w-pek,61859
73
73
  trilogy/parsing/render.py,sha256=Gy_6wVYPwYLf35Iota08sbqveuWILtUhI8MYStcvtJM,12174
74
- trilogy/parsing/trilogy.lark,sha256=cUcwxUTlxU7jKFzYEXYARLTsPHG5cVLk-Xhltw6m2lY,11357
74
+ trilogy/parsing/trilogy.lark,sha256=kj1DXkqRFAb20PXN4gGDtXtQI61nUv1U8nfPWSWCr1Y,11407
75
75
  trilogy/scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
76
76
  trilogy/scripts/trilogy.py,sha256=PHxvv6f2ODv0esyyhWxlARgra8dVhqQhYl0lTrSyVNo,3729
77
- pytrilogy-0.0.2.8.dist-info/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
78
- pytrilogy-0.0.2.8.dist-info/METADATA,sha256=bKl-Q_qHdoiH1rJZDDBXD425zCYaNShnuPioYKxBTAA,7906
79
- pytrilogy-0.0.2.8.dist-info/WHEEL,sha256=uCRv0ZEik_232NlR4YDw4Pv3Ajt5bKvMH13NUU7hFuI,91
80
- pytrilogy-0.0.2.8.dist-info/entry_points.txt,sha256=0petKryjvvtEfTlbZC1AuMFumH_WQ9v8A19LvoS6G6c,54
81
- pytrilogy-0.0.2.8.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
82
- pytrilogy-0.0.2.8.dist-info/RECORD,,
77
+ pytrilogy-0.0.2.9.dist-info/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
78
+ pytrilogy-0.0.2.9.dist-info/METADATA,sha256=DkZW90_Pp71fS8wh_B6UulpaM-J8RU5CT2zzLyGSHGk,7906
79
+ pytrilogy-0.0.2.9.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
80
+ pytrilogy-0.0.2.9.dist-info/entry_points.txt,sha256=0petKryjvvtEfTlbZC1AuMFumH_WQ9v8A19LvoS6G6c,54
81
+ pytrilogy-0.0.2.9.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
82
+ pytrilogy-0.0.2.9.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (74.1.1)
2
+ Generator: setuptools (74.1.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
trilogy/__init__.py CHANGED
@@ -4,6 +4,6 @@ from trilogy.executor import Executor
4
4
  from trilogy.parser import parse
5
5
  from trilogy.constants import CONFIG
6
6
 
7
- __version__ = "0.0.2.8"
7
+ __version__ = "0.0.2.9"
8
8
 
9
9
  __all__ = ["parse", "Executor", "Dialects", "Environment", "CONFIG"]
trilogy/constants.py CHANGED
@@ -24,6 +24,7 @@ class Optimizations:
24
24
  predicate_pushdown: bool = True
25
25
  datasource_inlining: bool = True
26
26
  constant_inlining: bool = True
27
+ constant_inline_cutoff: int = 2
27
28
  direct_return: bool = True
28
29
 
29
30
 
trilogy/core/models.py CHANGED
@@ -154,7 +154,10 @@ class ConceptArgs(ABC):
154
154
  class SelectContext(ABC):
155
155
 
156
156
  def with_select_context(
157
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
157
+ self,
158
+ grain: Grain,
159
+ conditional: Conditional | Comparison | Parenthetical | None,
160
+ environment: Environment | None = None,
158
161
  ):
159
162
  raise NotImplementedError
160
163
 
@@ -166,6 +169,7 @@ class ConstantInlineable(ABC):
166
169
 
167
170
  class SelectTypeMixin(BaseModel):
168
171
  where_clause: Union["WhereClause", None] = Field(default=None)
172
+ having_clause: Union["HavingClause", None] = Field(default=None)
169
173
 
170
174
  @property
171
175
  def output_components(self) -> List[Concept]:
@@ -595,13 +599,16 @@ class Concept(Mergeable, Namespaced, SelectContext, BaseModel):
595
599
  self,
596
600
  grain: Optional["Grain"] = None,
597
601
  conditional: Conditional | Comparison | Parenthetical | None = None,
602
+ environment: Environment | None = None,
598
603
  ) -> "Concept":
599
604
  if not all([isinstance(x, Concept) for x in self.keys or []]):
600
605
  raise ValueError(f"Invalid keys {self.keys} for concept {self.address}")
601
606
  new_grain = grain or self.grain
602
607
  new_lineage = self.lineage
603
608
  if isinstance(self.lineage, SelectContext):
604
- new_lineage = self.lineage.with_select_context(new_grain, conditional)
609
+ new_lineage = self.lineage.with_select_context(
610
+ new_grain, conditional, environment=environment
611
+ )
605
612
  return self.__class__(
606
613
  name=self.name,
607
614
  datatype=self.datatype,
@@ -788,7 +795,9 @@ class Concept(Mergeable, Namespaced, SelectContext, BaseModel):
788
795
  return Granularity.MULTI_ROW
789
796
 
790
797
  def with_filter(
791
- self, condition: "Conditional | Comparison | Parenthetical"
798
+ self,
799
+ condition: "Conditional | Comparison | Parenthetical",
800
+ environment: Environment | None = None,
792
801
  ) -> "Concept":
793
802
  from trilogy.utility import string_to_hash
794
803
 
@@ -805,12 +814,15 @@ class Concept(Mergeable, Namespaced, SelectContext, BaseModel):
805
814
  modifiers=self.modifiers,
806
815
  pseudonyms=self.pseudonyms,
807
816
  )
817
+ if environment:
818
+ environment.add_concept(new)
808
819
  return new
809
820
 
810
821
 
811
822
  class Grain(Mergeable, BaseModel):
812
823
  nested: bool = False
813
824
  components: List[Concept] = Field(default_factory=list, validate_default=True)
825
+ where_clause: Optional[WhereClause] = Field(default=None)
814
826
 
815
827
  @field_validator("components")
816
828
  def component_validator(cls, v, info: ValidationInfo):
@@ -836,10 +848,12 @@ class Grain(Mergeable, BaseModel):
836
848
 
837
849
  def __str__(self):
838
850
  if self.abstract:
839
- return (
840
- "Grain<Abstract" + ",".join([c.address for c in self.components]) + ">"
841
- )
842
- return "Grain<" + ",".join([c.address for c in self.components]) + ">"
851
+ base = "Grain<Abstract>"
852
+ else:
853
+ base = "Grain<" + ",".join([c.address for c in self.components]) + ">"
854
+ if self.where_clause:
855
+ base += f"|{str(self.where_clause)}"
856
+ return base
843
857
 
844
858
  def with_namespace(self, namespace: str) -> "Grain":
845
859
  return Grain(
@@ -1046,12 +1060,15 @@ class Function(Mergeable, Namespaced, SelectContext, BaseModel):
1046
1060
  return self.output_datatype
1047
1061
 
1048
1062
  def with_select_context(
1049
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
1063
+ self,
1064
+ grain: Grain,
1065
+ conditional: Conditional | Comparison | Parenthetical | None,
1066
+ environment: Environment | None = None,
1050
1067
  ) -> Function:
1051
1068
  if self.operator in FunctionClass.AGGREGATE_FUNCTIONS.value and conditional:
1052
1069
  base = [
1053
1070
  (
1054
- c.with_select_context(grain, conditional)
1071
+ c.with_select_context(grain, conditional, environment)
1055
1072
  if isinstance(
1056
1073
  c,
1057
1074
  SelectContext,
@@ -1061,7 +1078,7 @@ class Function(Mergeable, Namespaced, SelectContext, BaseModel):
1061
1078
  for c in self.arguments
1062
1079
  ]
1063
1080
  final = [
1064
- c.with_filter(conditional) if isinstance(c, Concept) else c
1081
+ c.with_filter(conditional, environment) if isinstance(c, Concept) else c
1065
1082
  for c in base
1066
1083
  ]
1067
1084
  return Function(
@@ -1077,7 +1094,7 @@ class Function(Mergeable, Namespaced, SelectContext, BaseModel):
1077
1094
  operator=self.operator,
1078
1095
  arguments=[
1079
1096
  (
1080
- c.with_select_context(grain, conditional)
1097
+ c.with_select_context(grain, conditional, environment)
1081
1098
  if isinstance(
1082
1099
  c,
1083
1100
  SelectContext,
@@ -1293,13 +1310,22 @@ class WindowItem(Mergeable, Namespaced, SelectContext, BaseModel):
1293
1310
  )
1294
1311
 
1295
1312
  def with_select_context(
1296
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
1313
+ self,
1314
+ grain: Grain,
1315
+ conditional: Conditional | Comparison | Parenthetical | None,
1316
+ environment: Environment | None = None,
1297
1317
  ) -> "WindowItem":
1298
1318
  return WindowItem(
1299
1319
  type=self.type,
1300
- content=self.content.with_select_context(grain, conditional),
1301
- over=[x.with_select_context(grain, conditional) for x in self.over],
1302
- order_by=[x.with_select_context(grain, conditional) for x in self.order_by],
1320
+ content=self.content.with_select_context(grain, conditional, environment),
1321
+ over=[
1322
+ x.with_select_context(grain, conditional, environment)
1323
+ for x in self.over
1324
+ ],
1325
+ order_by=[
1326
+ x.with_select_context(grain, conditional, environment)
1327
+ for x in self.order_by
1328
+ ],
1303
1329
  )
1304
1330
 
1305
1331
  @property
@@ -1368,11 +1394,14 @@ class FilterItem(Namespaced, SelectContext, BaseModel):
1368
1394
  )
1369
1395
 
1370
1396
  def with_select_context(
1371
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
1397
+ self,
1398
+ grain: Grain,
1399
+ conditional: Conditional | Comparison | Parenthetical | None,
1400
+ environment: Environment | None = None,
1372
1401
  ) -> FilterItem:
1373
1402
  return FilterItem(
1374
- content=self.content.with_select_context(grain, conditional),
1375
- where=self.where.with_select_context(grain, conditional),
1403
+ content=self.content.with_select_context(grain, conditional, environment),
1404
+ where=self.where.with_select_context(grain, conditional, environment),
1376
1405
  )
1377
1406
 
1378
1407
  @property
@@ -1452,9 +1481,17 @@ class OrderItem(Mergeable, SelectContext, Namespaced, BaseModel):
1452
1481
  return OrderItem(expr=self.expr.with_namespace(namespace), order=self.order)
1453
1482
 
1454
1483
  def with_select_context(
1455
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
1484
+ self,
1485
+ grain: Grain,
1486
+ conditional: Conditional | Comparison | Parenthetical | None,
1487
+ environment: Environment | None = None,
1456
1488
  ) -> "OrderItem":
1457
- return OrderItem(expr=self.expr.with_grain(grain), order=self.order)
1489
+ return OrderItem(
1490
+ expr=self.expr.with_select_context(
1491
+ grain, conditional=conditional, environment=environment
1492
+ ),
1493
+ order=self.order,
1494
+ )
1458
1495
 
1459
1496
  def with_merge(
1460
1497
  self, source: Concept, target: Concept, modifiers: List[Modifier]
@@ -1643,7 +1680,9 @@ class SelectStatement(Mergeable, Namespaced, SelectTypeMixin, BaseModel):
1643
1680
  )
1644
1681
  ):
1645
1682
  output.append(item)
1646
- return Grain(components=unique(output, "address"))
1683
+ return Grain(
1684
+ components=unique(output, "address"), where_clause=self.where_clause
1685
+ )
1647
1686
 
1648
1687
  def with_namespace(self, namespace: str) -> "SelectStatement":
1649
1688
  return SelectStatement(
@@ -2229,17 +2268,14 @@ class QueryDatasource(BaseModel):
2229
2268
  @classmethod
2230
2269
  def validate_source_map(cls, v, info: ValidationInfo):
2231
2270
  values = info.data
2232
- expected = {c.address for c in values["output_concepts"]}.union(
2233
- c.address for c in values["input_concepts"]
2234
- )
2235
- seen = set()
2236
- for k, _ in v.items():
2237
- seen.add(k)
2238
- for x in expected:
2239
- if x not in seen and CONFIG.validate_missing:
2240
- raise SyntaxError(
2241
- f"source map missing {x} on (expected {expected}, have {seen})"
2242
- )
2271
+ for key in ("input_concepts", "output_concepts"):
2272
+ if not values.get(key):
2273
+ continue
2274
+ for concept in values[key]:
2275
+ if concept.address not in v and CONFIG.validate_missing:
2276
+ raise SyntaxError(
2277
+ f"Missing source map for {concept.address} on {key}, have {v}"
2278
+ )
2243
2279
  return v
2244
2280
 
2245
2281
  def __str__(self):
@@ -2849,6 +2885,7 @@ class UndefinedConcept(Concept, Mergeable, Namespaced):
2849
2885
  self,
2850
2886
  grain: Optional["Grain"] = None,
2851
2887
  conditional: Conditional | Comparison | Parenthetical | None = None,
2888
+ environment: Environment | None = None,
2852
2889
  ) -> "UndefinedConcept":
2853
2890
  if not all([isinstance(x, Concept) for x in self.keys or []]):
2854
2891
  raise ValueError(f"Invalid keys {self.keys} for concept {self.address}")
@@ -2856,7 +2893,9 @@ class UndefinedConcept(Concept, Mergeable, Namespaced):
2856
2893
  if self.lineage:
2857
2894
  new_lineage = self.lineage
2858
2895
  if isinstance(self.lineage, SelectContext):
2859
- new_lineage = self.lineage.with_select_context(new_grain, conditional)
2896
+ new_lineage = self.lineage.with_select_context(
2897
+ new_grain, conditional, environment
2898
+ )
2860
2899
  else:
2861
2900
  new_lineage = None
2862
2901
  return self.__class__(
@@ -3299,7 +3338,9 @@ class LazyEnvironment(Environment):
3299
3338
  ) or name.startswith("_"):
3300
3339
  return super().__getattribute__(name)
3301
3340
  if not self.loaded:
3302
- print(f"lazily evaluating load path {self.load_path} to access {name}")
3341
+ logger.info(
3342
+ f"lazily evaluating load path {self.load_path} to access {name}"
3343
+ )
3303
3344
  from trilogy import parse
3304
3345
 
3305
3346
  env = Environment(working_path=str(self.working_path))
@@ -3441,16 +3482,23 @@ class Comparison(
3441
3482
  )
3442
3483
 
3443
3484
  def with_select_context(
3444
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3485
+ self,
3486
+ grain: Grain,
3487
+ conditional: Conditional | Comparison | Parenthetical | None,
3488
+ environment: Environment | None = None,
3445
3489
  ):
3446
3490
  return self.__class__(
3447
3491
  left=(
3448
- self.left.with_select_context(grain, conditional)
3492
+ self.left.with_select_context(grain, conditional, environment)
3449
3493
  if isinstance(self.left, SelectContext)
3450
3494
  else self.left
3451
3495
  ),
3452
3496
  # the right side does NOT need to inherit select grain
3453
- right=self.right,
3497
+ right=(
3498
+ self.right.with_select_context(grain, conditional, environment)
3499
+ if isinstance(self.right, SelectContext)
3500
+ else self.right
3501
+ ),
3454
3502
  operator=self.operator,
3455
3503
  )
3456
3504
 
@@ -3534,12 +3582,15 @@ class SubselectComparison(Comparison):
3534
3582
  return [tuple(get_concept_arguments(self.right))]
3535
3583
 
3536
3584
  def with_select_context(
3537
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3585
+ self,
3586
+ grain: Grain,
3587
+ conditional: Conditional | Comparison | Parenthetical | None,
3588
+ environment: Environment | None = None,
3538
3589
  ):
3539
- # there's no need to pass the select grain through to a subselect comparison
3590
+ # there's no need to pass the select grain through to a subselect comparison on the right
3540
3591
  return self.__class__(
3541
3592
  left=(
3542
- self.left.with_select_context(grain, conditional)
3593
+ self.left.with_select_context(grain, conditional, environment)
3543
3594
  if isinstance(self.left, SelectContext)
3544
3595
  else self.left
3545
3596
  ),
@@ -3570,12 +3621,17 @@ class CaseWhen(Namespaced, SelectContext, BaseModel):
3570
3621
  )
3571
3622
 
3572
3623
  def with_select_context(
3573
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3624
+ self,
3625
+ grain: Grain,
3626
+ conditional: Conditional | Comparison | Parenthetical | None,
3627
+ environment: Environment | None = None,
3574
3628
  ) -> CaseWhen:
3575
3629
  return CaseWhen(
3576
- comparison=self.comparison.with_select_context(grain, conditional),
3630
+ comparison=self.comparison.with_select_context(
3631
+ grain, conditional, environment
3632
+ ),
3577
3633
  expr=(
3578
- (self.expr.with_select_context(grain, conditional))
3634
+ (self.expr.with_select_context(grain, conditional, environment))
3579
3635
  if isinstance(self.expr, SelectContext)
3580
3636
  else self.expr
3581
3637
  ),
@@ -3592,12 +3648,15 @@ class CaseElse(Namespaced, SelectContext, BaseModel):
3592
3648
  return get_concept_arguments(self.expr)
3593
3649
 
3594
3650
  def with_select_context(
3595
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3651
+ self,
3652
+ grain: Grain,
3653
+ conditional: Conditional | Comparison | Parenthetical | None,
3654
+ environment: Environment | None = None,
3596
3655
  ) -> CaseElse:
3597
3656
  return CaseElse(
3598
3657
  discriminant=self.discriminant,
3599
3658
  expr=(
3600
- self.expr.with_select_context(grain, conditional)
3659
+ self.expr.with_select_context(grain, conditional, environment)
3601
3660
  if isinstance(
3602
3661
  self.expr,
3603
3662
  SelectContext,
@@ -3737,16 +3796,19 @@ class Conditional(
3737
3796
  )
3738
3797
 
3739
3798
  def with_select_context(
3740
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3799
+ self,
3800
+ grain: Grain,
3801
+ conditional: Conditional | Comparison | Parenthetical | None,
3802
+ environment: Environment | None = None,
3741
3803
  ):
3742
3804
  return Conditional(
3743
3805
  left=(
3744
- self.left.with_select_context(grain, conditional)
3806
+ self.left.with_select_context(grain, conditional, environment)
3745
3807
  if isinstance(self.left, SelectContext)
3746
3808
  else self.left
3747
3809
  ),
3748
3810
  right=(
3749
- self.right.with_select_context(grain, conditional)
3811
+ self.right.with_select_context(grain, conditional, environment)
3750
3812
  if isinstance(self.right, SelectContext)
3751
3813
  else self.right
3752
3814
  ),
@@ -3855,13 +3917,16 @@ class AggregateWrapper(Mergeable, Namespaced, SelectContext, BaseModel):
3855
3917
  )
3856
3918
 
3857
3919
  def with_select_context(
3858
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3920
+ self,
3921
+ grain: Grain,
3922
+ conditional: Conditional | Comparison | Parenthetical | None,
3923
+ environment: Environment | None = None,
3859
3924
  ) -> AggregateWrapper:
3860
3925
  if not self.by:
3861
3926
  by = grain.components_copy
3862
3927
  else:
3863
3928
  by = self.by
3864
- parent = self.function.with_select_context(grain, conditional)
3929
+ parent = self.function.with_select_context(grain, conditional, environment)
3865
3930
  return AggregateWrapper(function=parent, by=by)
3866
3931
 
3867
3932
 
@@ -3893,10 +3958,15 @@ class WhereClause(Mergeable, ConceptArgs, Namespaced, SelectContext, BaseModel):
3893
3958
  return WhereClause(conditional=self.conditional.with_namespace(namespace))
3894
3959
 
3895
3960
  def with_select_context(
3896
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
3961
+ self,
3962
+ grain: Grain,
3963
+ conditional: Conditional | Comparison | Parenthetical | None,
3964
+ environment: Environment | None = None,
3897
3965
  ) -> WhereClause:
3898
3966
  return WhereClause(
3899
- conditional=self.conditional.with_select_context(grain, conditional)
3967
+ conditional=self.conditional.with_select_context(
3968
+ grain, conditional, environment
3969
+ )
3900
3970
  )
3901
3971
 
3902
3972
  @property
@@ -3909,6 +3979,22 @@ class WhereClause(Mergeable, ConceptArgs, Namespaced, SelectContext, BaseModel):
3909
3979
  output += item.grain.components if item.grain else []
3910
3980
  return Grain(components=list(set(output)))
3911
3981
 
3982
+ @property
3983
+ def components(self):
3984
+ from trilogy.core.processing.utility import decompose_condition
3985
+
3986
+ return decompose_condition(self.conditional)
3987
+
3988
+ @property
3989
+ def is_scalar(self):
3990
+ from trilogy.core.processing.utility import is_scalar_condition
3991
+
3992
+ return is_scalar_condition(self.conditional)
3993
+
3994
+
3995
+ class HavingClause(WhereClause):
3996
+ pass
3997
+
3912
3998
 
3913
3999
  class MaterializedDataset(BaseModel):
3914
4000
  address: Address
@@ -3928,6 +4014,7 @@ class ProcessedQuery(BaseModel):
3928
4014
  hidden_columns: List[Concept] = Field(default_factory=list)
3929
4015
  limit: Optional[int] = None
3930
4016
  where_clause: Optional[WhereClause] = None
4017
+ having_clause: Optional[HavingClause] = None
3931
4018
  order_by: Optional[OrderBy] = None
3932
4019
 
3933
4020
 
@@ -4130,11 +4217,14 @@ class Parenthetical(
4130
4217
  )
4131
4218
 
4132
4219
  def with_select_context(
4133
- self, grain: Grain, conditional: Conditional | Comparison | Parenthetical | None
4220
+ self,
4221
+ grain: Grain,
4222
+ conditional: Conditional | Comparison | Parenthetical | None,
4223
+ environment: Environment | None = None,
4134
4224
  ):
4135
4225
  return Parenthetical(
4136
4226
  content=(
4137
- self.content.with_select_context(grain, conditional)
4227
+ self.content.with_select_context(grain, conditional, environment)
4138
4228
  if isinstance(self.content, SelectContext)
4139
4229
  else self.content
4140
4230
  )
@@ -17,19 +17,58 @@ from trilogy.core.optimizations import (
17
17
  MAX_OPTIMIZATION_LOOPS = 100
18
18
 
19
19
 
20
+ # other optimizations may make a CTE a pure passthrough
21
+ # remove those
22
+ # def is_locally_irrelevant(cte: CTE) -> CTE | bool:
23
+ # if not len(cte.parent_ctes) == 1:
24
+ # return False
25
+ # parent = cte.parent_ctes[0]
26
+ # if not parent.output_columns == cte.output_columns:
27
+ # return False
28
+ # if cte.condition is not None:
29
+ # return False
30
+ # if cte.group_to_grain:
31
+ # return False
32
+ # if len(cte.joins)>1:
33
+ # return False
34
+ # return parent
35
+
36
+
20
37
  def filter_irrelevant_ctes(
21
38
  input: list[CTE],
22
39
  root_cte: CTE,
23
40
  ):
24
41
  relevant_ctes = set()
25
42
 
26
- def recurse(cte: CTE):
43
+ def recurse(cte: CTE, inverse_map: dict[str, list[CTE]]):
44
+ # TODO: revisit this
45
+ # if parent := is_locally_irrelevant(cte):
46
+ # logger.info(
47
+ # f"[Optimization][Irrelevent CTE filtering] Removing redundant CTE {cte.name} and replacing with {parent.name}"
48
+ # )
49
+ # for child in inverse_map.get(cte.name, []):
50
+ # child.parent_ctes = [
51
+ # x for x in child.parent_ctes if x.name != cte.name
52
+ # ] + [parent]
53
+ # for x in child.source_map:
54
+ # if cte.name in child.source_map[x]:
55
+ # child.source_map[x].remove(cte.name)
56
+ # child.source_map[x].append(parent.name)
57
+ # for x2 in child.existence_source_map:
58
+ # if cte.name in child.existence_source_map[x2]:
59
+ # child.existence_source_map[x2].remove(cte.name)
60
+ # child.existence_source_map[x2].append(parent.name)
61
+ # else:
27
62
  relevant_ctes.add(cte.name)
28
63
  for cte in cte.parent_ctes:
29
- recurse(cte)
30
-
31
- recurse(root_cte)
32
- return [cte for cte in input if cte.name in relevant_ctes]
64
+ recurse(cte, inverse_map)
65
+
66
+ inverse_map = gen_inverse_map(input)
67
+ recurse(root_cte, inverse_map)
68
+ final = [cte for cte in input if cte.name in relevant_ctes]
69
+ if len(final) == len(input):
70
+ return input
71
+ return filter_irrelevant_ctes(final, root_cte)
33
72
 
34
73
 
35
74
  def gen_inverse_map(input: list[CTE]) -> dict[str, list[CTE]]: