pytrilogy 0.0.3.96__py3-none-any.whl → 0.0.3.97__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.4
2
2
  Name: pytrilogy
3
- Version: 0.0.3.96
3
+ Version: 0.0.3.97
4
4
  Summary: Declarative, typed query language that compiles to SQL.
5
5
  Home-page:
6
6
  Author:
@@ -334,7 +334,21 @@ from pytrilogy.authoring import Concept, Function, ...
334
334
 
335
335
  Are likely to be unstable. Open an issue if you need to take dependencies on other modules outside those two paths.
336
336
 
337
- ## Trilogy Syntax Reference
337
+ ## MCP/Server
338
+
339
+ Trilogy is straightforward to run as a server/MCP server; the former to generate SQL on demand and integrate into other tools, and MCP
340
+ for full interactive query loops.
341
+
342
+ This makes it easy to integrate Trilogy into existing tools or workflows.
343
+
344
+ You can see examples of both use cases in the trilogy-studio codebase [here](https://github.com/trilogy-data/trilogy-studio-core)
345
+ and install and run an MCP server directly with that codebase.
346
+
347
+ If you're interested in a more fleshed out standalone server or MCP server, please open an issue and we'll prioritize it!
348
+
349
+ ## Trilogy Syntax Reference
350
+
351
+ Not exhaustive - see [documentation](https://trilogydata.dev/) for more details.
338
352
 
339
353
  ### Import
340
354
  ```sql
@@ -1,6 +1,6 @@
1
- pytrilogy-0.0.3.96.dist-info/licenses/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
2
- trilogy/__init__.py,sha256=hBxtfxlbUvTLp_8FCY_-wDqJM7G2RJQ2jntfMf3a0PM,303
3
- trilogy/constants.py,sha256=eKb_EJvSqjN9tGbdVEViwdtwwh8fZ3-jpOEDqL71y70,1691
1
+ pytrilogy-0.0.3.97.dist-info/licenses/LICENSE.md,sha256=5ZRvtTyCCFwz1THxDTjAu3Lidds9WjPvvzgVwPSYNDo,1042
2
+ trilogy/__init__.py,sha256=cYPR5Qx9Z_vJCc8nN83bLxuX2AWSJ8GNmHHEIFoyexA,303
3
+ trilogy/constants.py,sha256=SSsRMg9HTou259nMKAw-rJNBgzkWjQ3QIQXcrq9i5Kk,1717
4
4
  trilogy/engine.py,sha256=3MiADf5MKcmxqiHBuRqiYdsXiLj7oitDfVvXvHrfjkA,2178
5
5
  trilogy/executor.py,sha256=YfSjuJ0FVm2gHnNgmUlXijWDTUFjqq9FNakWpeEYO48,15769
6
6
  trilogy/parser.py,sha256=o4cfk3j3yhUFoiDKq9ZX_GjBF3dKhDjXEwb63rcBkBM,293
@@ -10,11 +10,11 @@ trilogy/utility.py,sha256=euQccZLKoYBz0LNg5tzLlvv2YHvXh9HArnYp1V3uXsM,763
10
10
  trilogy/authoring/__init__.py,sha256=TABMOETSMERrWuyDLR0nK4ISlqR0yaqeXrmuOdrSvAY,3060
11
11
  trilogy/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
12
  trilogy/core/constants.py,sha256=nizWYDCJQ1bigQMtkNIEMNTcN0NoEAXiIHLzpelxQ24,201
13
- trilogy/core/enums.py,sha256=EusAzz7o_YrWf64TLIED7MfziFOJk8EHM8se5W3nyJk,8644
13
+ trilogy/core/enums.py,sha256=H8I2Dz4POHZ4ixYCGzNs4c3KDqxLQklGLVfmje1DSMo,8877
14
14
  trilogy/core/env_processor.py,sha256=H-rr2ALj31l5oh3FqeI47Qju6OOfiXBacXNJGNZ92zQ,4521
15
15
  trilogy/core/environment_helpers.py,sha256=TRlqVctqIRBxzfjRBmpQsAVoiCcsEKBhG1B6PUE0l1M,12743
16
16
  trilogy/core/ergonomics.py,sha256=e-7gE29vPLFdg0_A1smQ7eOrUwKl5VYdxRSTddHweRA,1631
17
- trilogy/core/exceptions.py,sha256=0Lmc3awJYx94k6uifbHc-EIqlFGV6YrX0QIwP84D4a4,1150
17
+ trilogy/core/exceptions.py,sha256=fI16oTNCVMMAJFSn2AFzZVapzsF5M9WbdN5e5UixwXc,2807
18
18
  trilogy/core/functions.py,sha256=ESUWMRmwtavwCLl6z1NP9EFzWTJoXn3orTaaOSsj33Q,33093
19
19
  trilogy/core/graph_models.py,sha256=4EWFTHGfYd72zvS2HYoV6hm7nMC_VEd7vWr6txY-ig0,3400
20
20
  trilogy/core/internal.py,sha256=r9QagDB2GvpqlyD_I7VrsfbVfIk5mnok2znEbv72Aa4,2681
@@ -76,12 +76,13 @@ trilogy/core/statements/build.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hS
76
76
  trilogy/core/statements/common.py,sha256=VnVLULQg1TJLNUFzJaROT1tsf2ewk3IpuhvZaP36R6A,535
77
77
  trilogy/core/statements/execute.py,sha256=kiwJcVeMa4wZR-xLfM2oYOJ9DeyJkP8An38WFyJxktM,2413
78
78
  trilogy/core/validation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
79
- trilogy/core/validation/common.py,sha256=cVbDSowtLf2nl0-QVmNauAeLBBNFkSE5bRZtTHIzW20,3193
80
- trilogy/core/validation/concept.py,sha256=23wZYw_cGmTQuFvaRM-0T7M2b5ZwqjFMucfvfzyQxlc,4425
81
- trilogy/core/validation/datasource.py,sha256=HIk7iEKK99k0-WXosiIhedH-U2rDGYGdUMKEt1eMl1w,6394
82
- trilogy/core/validation/environment.py,sha256=yjSnEH893mTiW9o6YXBtXJWbGSR2kMQWjszDuECznLs,2784
79
+ trilogy/core/validation/common.py,sha256=Sd-towAX1uSDe3dK51FcVtIwVrMhayEwdHqhzeJHro0,4776
80
+ trilogy/core/validation/concept.py,sha256=Jr8H9wn0-OMJeSFD5GydiB-LvCSj4CUe72evHjRq85E,4991
81
+ trilogy/core/validation/datasource.py,sha256=d9AQNcukIRgN2spItPsXFiNtlZva-lDnfei3i06yQCE,6489
82
+ trilogy/core/validation/environment.py,sha256=waBPMB6rxa-9SKUAdhfT8UUDqh4a76n27pKtRDre560,2834
83
+ trilogy/core/validation/fix.py,sha256=Z818UFNLxndMTLiyhB3doLxIfnOZ-16QGvVFWuD7UsA,3750
83
84
  trilogy/dialect/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
84
- trilogy/dialect/base.py,sha256=m2a8azbI3AWfQz-VtSn84H5T-BqjF5PULH6BrgwZzok,49666
85
+ trilogy/dialect/base.py,sha256=0QVHv4F0t3_gRQrZ0woFoUNKu7vaXGo-BG1l47CZUKc,49698
85
86
  trilogy/dialect/bigquery.py,sha256=XS3hpybeowgfrOrkycAigAF3NX2YUzTzfgE6f__2fT4,4316
86
87
  trilogy/dialect/common.py,sha256=tSthIZOXXRPQ4KeMKnDDsH7KlTmf2EVqigVtLyoc4zc,6071
87
88
  trilogy/dialect/config.py,sha256=olnyeVU5W5T6b9-dMeNAnvxuPlyc2uefb7FRME094Ec,3834
@@ -103,9 +104,9 @@ trilogy/parsing/common.py,sha256=550-L0444GUuBFdiDWkOg_DxnMXtcJFUMES2R5zlwik,310
103
104
  trilogy/parsing/config.py,sha256=Z-DaefdKhPDmSXLgg5V4pebhSB0h590vI0_VtHnlukI,111
104
105
  trilogy/parsing/exceptions.py,sha256=Xwwsv2C9kSNv2q-HrrKC1f60JNHShXcCMzstTSEbiCw,154
105
106
  trilogy/parsing/helpers.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
106
- trilogy/parsing/parse_engine.py,sha256=fZGFNN7PEVJ7_tk0GxBHJQerIJTifbrjoMcHfTR_TYk,81647
107
- trilogy/parsing/render.py,sha256=HSNntD82GiiwHT-TWPLXAaIMWLYVV5B5zQEsgwrHIBE,19605
108
- trilogy/parsing/trilogy.lark,sha256=9-SMrKFGZLdBTNheK1szif0VYOIt5m0xhVd8pOFCByU,16267
107
+ trilogy/parsing/parse_engine.py,sha256=Zd4Zpj8k_Q95onXlThV_AWSwjUAGDsD57uTyKKmXxNI,81784
108
+ trilogy/parsing/render.py,sha256=OirN76I8z9xDNGvydO4DhsTsN-NS_1SC5_77_V23CkI,20515
109
+ trilogy/parsing/trilogy.lark,sha256=rM4WleeyGhoRgU-FOGcaeHOzZcYVxN4f13e_3B4OeLQ,16389
109
110
  trilogy/scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
110
111
  trilogy/scripts/trilogy.py,sha256=1L0XrH4mVHRt1C9T1HnaDv2_kYEfbWTb5_-cBBke79w,3774
111
112
  trilogy/std/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -116,8 +117,8 @@ trilogy/std/money.preql,sha256=XWwvAV3WxBsHX9zfptoYRnBigcfYwrYtBHXTME0xJuQ,2082
116
117
  trilogy/std/net.preql,sha256=WZCuvH87_rZntZiuGJMmBDMVKkdhTtxeHOkrXNwJ1EE,416
117
118
  trilogy/std/ranking.preql,sha256=LDoZrYyz4g3xsII9XwXfmstZD-_92i1Eox1UqkBIfi8,83
118
119
  trilogy/std/report.preql,sha256=LbV-XlHdfw0jgnQ8pV7acG95xrd1-p65fVpiIc-S7W4,202
119
- pytrilogy-0.0.3.96.dist-info/METADATA,sha256=l4yiGzDzMYd4of8nPXBMBKyetasPk4yfIOyENWjEqcU,11023
120
- pytrilogy-0.0.3.96.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
121
- pytrilogy-0.0.3.96.dist-info/entry_points.txt,sha256=ewBPU2vLnVexZVnB-NrVj-p3E-4vukg83Zk8A55Wp2w,56
122
- pytrilogy-0.0.3.96.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
123
- pytrilogy-0.0.3.96.dist-info/RECORD,,
120
+ pytrilogy-0.0.3.97.dist-info/METADATA,sha256=t-pwxx9XVd6oSuAMdPfTDHFaY7AevCESieu_mK6YrcU,11683
121
+ pytrilogy-0.0.3.97.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
122
+ pytrilogy-0.0.3.97.dist-info/entry_points.txt,sha256=ewBPU2vLnVexZVnB-NrVj-p3E-4vukg83Zk8A55Wp2w,56
123
+ pytrilogy-0.0.3.97.dist-info/top_level.txt,sha256=cAy__NW_eMAa_yT9UnUNlZLFfxcg6eimUAZ184cdNiE,8
124
+ pytrilogy-0.0.3.97.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.96"
7
+ __version__ = "0.0.3.97"
8
8
 
9
9
  __all__ = ["parse", "Executor", "Dialects", "Environment", "CONFIG"]
trilogy/constants.py CHANGED
@@ -16,6 +16,7 @@ ENV_CACHE_NAME = ".preql_cache.json"
16
16
 
17
17
  class MagicConstants(Enum):
18
18
  NULL = "null"
19
+ LINE_SEPARATOR = "\n"
19
20
 
20
21
 
21
22
  NULL_VALUE = MagicConstants.NULL
trilogy/core/enums.py CHANGED
@@ -82,6 +82,15 @@ class Modifier(Enum):
82
82
  return Modifier.NULLABLE
83
83
  return super()._missing_(value=strval.capitalize())
84
84
 
85
+ def __lt__(self, other):
86
+ order = [
87
+ Modifier.HIDDEN,
88
+ Modifier.PARTIAL,
89
+ Modifier.NULLABLE,
90
+ Modifier.OPTIONAL,
91
+ ]
92
+ return order.index(self) < order.index(other)
93
+
85
94
 
86
95
  class JoinType(Enum):
87
96
  INNER = "inner"
@@ -1,4 +1,15 @@
1
- from typing import List, Sequence
1
+ from dataclasses import dataclass
2
+ from typing import Any, List, Sequence
3
+
4
+ from trilogy.core.enums import Modifier
5
+ from trilogy.core.models.core import (
6
+ ArrayType,
7
+ DataType,
8
+ MapType,
9
+ NumericType,
10
+ StructType,
11
+ TraitDataType,
12
+ )
2
13
 
3
14
 
4
15
  class UndefinedConceptException(Exception):
@@ -29,7 +40,7 @@ class ModelValidationError(Exception):
29
40
  self,
30
41
  message,
31
42
  children: Sequence["ModelValidationError"] | None = None,
32
- **kwargs
43
+ **kwargs,
33
44
  ):
34
45
  super().__init__(self, message, **kwargs)
35
46
  self.message = message
@@ -40,6 +51,49 @@ class DatasourceModelValidationError(ModelValidationError):
40
51
  pass
41
52
 
42
53
 
54
+ class DatasourceGrainValidationError(DatasourceModelValidationError):
55
+ pass
56
+
57
+
58
+ @dataclass
59
+ class DatasourceColumnBindingData:
60
+ address: str
61
+ value: Any
62
+ value_type: (
63
+ DataType | ArrayType | StructType | MapType | NumericType | TraitDataType
64
+ )
65
+ value_modifiers: List[Modifier]
66
+ actual_type: (
67
+ DataType | ArrayType | StructType | MapType | NumericType | TraitDataType
68
+ )
69
+ actual_modifiers: List[Modifier]
70
+
71
+ def format_failure(self):
72
+ return f"Concept {self.address} value '{self.value}' with type {self.value_modifiers} does not conform to expected type {str(self.actual_type)} with modifiers {self.actual_modifiers}"
73
+
74
+ def is_modifier_issue(self) -> bool:
75
+ return len(self.value_modifiers) > 0 and any(
76
+ [x not in self.actual_modifiers for x in self.value_modifiers]
77
+ )
78
+
79
+ def is_type_issue(self) -> bool:
80
+ return self.value_type != self.actual_type
81
+
82
+
83
+ class DatasourceColumnBindingError(DatasourceModelValidationError):
84
+ def __init__(
85
+ self,
86
+ address: str,
87
+ errors: list[DatasourceColumnBindingData],
88
+ message: str | None = None,
89
+ ):
90
+ if not message:
91
+ message = f"Datasource {address} failed validation. Found rows that do not conform to types: {[failure.format_failure() for failure in errors]}"
92
+ super().__init__(message)
93
+ self.errors = errors
94
+ self.dataset_address = address
95
+
96
+
43
97
  class ConceptModelValidationError(ModelValidationError):
44
98
  pass
45
99
 
@@ -2,13 +2,25 @@ from dataclasses import dataclass
2
2
  from enum import Enum
3
3
 
4
4
  from trilogy import Environment
5
- from trilogy.authoring import ConceptRef
5
+ from trilogy.authoring import (
6
+ ConceptRef,
7
+ DataType,
8
+ Ordering,
9
+ Purpose,
10
+ )
11
+ from trilogy.constants import MagicConstants
12
+ from trilogy.core.enums import ComparisonOperator, FunctionType
6
13
  from trilogy.core.exceptions import ModelValidationError
7
14
  from trilogy.core.models.build import (
15
+ BuildCaseElse,
16
+ BuildCaseWhen,
8
17
  BuildComparison,
9
18
  BuildConcept,
10
19
  BuildConditional,
11
20
  BuildDatasource,
21
+ BuildFunction,
22
+ BuildOrderBy,
23
+ BuildOrderItem,
12
24
  )
13
25
  from trilogy.core.models.environment import EnvironmentConceptDict
14
26
  from trilogy.core.models.execute import (
@@ -39,6 +51,32 @@ class ValidationType(Enum):
39
51
  CONCEPTS = "concepts"
40
52
 
41
53
 
54
+ def build_order_args(concepts: list[BuildConcept]) -> list[BuildFunction]:
55
+ order_args = []
56
+ for concept in concepts:
57
+ order_args.append(
58
+ BuildFunction(
59
+ operator=FunctionType.CASE,
60
+ arguments=[
61
+ BuildCaseWhen(
62
+ comparison=BuildComparison(
63
+ left=concept,
64
+ operator=ComparisonOperator.IS,
65
+ right=MagicConstants.NULL,
66
+ ),
67
+ expr=1,
68
+ ),
69
+ BuildCaseElse(expr=0),
70
+ ],
71
+ output_data_type=DataType.INTEGER,
72
+ output_purpose=Purpose.PROPERTY,
73
+ arg_count=2,
74
+ )
75
+ )
76
+
77
+ return order_args
78
+
79
+
42
80
  def easy_query(
43
81
  concepts: list[BuildConcept],
44
82
  datasource: BuildDatasource,
@@ -81,7 +119,6 @@ def easy_query(
81
119
  group_to_grain=True,
82
120
  base_alias_override=datasource.safe_identifier,
83
121
  )
84
-
85
122
  filter_cte = CTE(
86
123
  name=f"datasource_{datasource.name}_filter",
87
124
  source=QueryDatasource(
@@ -100,6 +137,20 @@ def easy_query(
100
137
  grain=cte.grain,
101
138
  condition=condition,
102
139
  limit=limit,
140
+ order_by=BuildOrderBy(
141
+ items=[
142
+ BuildOrderItem(
143
+ expr=BuildFunction(
144
+ operator=FunctionType.SUM,
145
+ arguments=build_order_args(concepts),
146
+ output_data_type=DataType.INTEGER,
147
+ output_purpose=Purpose.PROPERTY,
148
+ arg_count=len(concepts),
149
+ ),
150
+ order=Ordering.DESCENDING,
151
+ )
152
+ ]
153
+ ),
103
154
  )
104
155
 
105
156
  return ProcessedQuery(
@@ -1,8 +1,9 @@
1
1
  from trilogy import Environment, Executor
2
- from trilogy.core.enums import Derivation, Purpose
2
+ from trilogy.core.enums import Derivation, Modifier, Purpose
3
3
  from trilogy.core.exceptions import (
4
4
  ConceptModelValidationError,
5
- DatasourceModelValidationError,
5
+ DatasourceColumnBindingData,
6
+ DatasourceColumnBindingError,
6
7
  )
7
8
  from trilogy.core.models.build import (
8
9
  BuildConcept,
@@ -69,8 +70,19 @@ def validate_key_concept(
69
70
  err = None
70
71
  datasource_count: int = seen.get(datasource.name, 0)
71
72
  if datasource_count < max_seen and assignment.is_complete:
72
- err = DatasourceModelValidationError(
73
- f"Key concept {concept.address} is missing values in datasource {datasource.name} (max cardinality in data {max_seen}, datasource has {seen[datasource.name]} values) but is not marked as partial."
73
+ err = DatasourceColumnBindingError(
74
+ address=datasource.identifier,
75
+ errors=[
76
+ DatasourceColumnBindingData(
77
+ address=concept.address,
78
+ value=None,
79
+ value_type=concept.datatype,
80
+ value_modifiers=[Modifier.PARTIAL],
81
+ actual_type=concept.datatype,
82
+ actual_modifiers=concept.modifiers,
83
+ )
84
+ ],
85
+ message=f"Key concept {concept.address} is missing values in datasource {datasource.name} (max cardinality in data {max_seen}, datasource has {seen[datasource.name]} values) but is not marked as partial.",
74
86
  )
75
87
  results.append(
76
88
  ValidationTest(
@@ -10,9 +10,14 @@ from trilogy.authoring import (
10
10
  NumericType,
11
11
  StructType,
12
12
  TraitDataType,
13
+ arg_to_datatype,
14
+ )
15
+ from trilogy.core.enums import ComparisonOperator, Modifier
16
+ from trilogy.core.exceptions import (
17
+ DatasourceColumnBindingData,
18
+ DatasourceColumnBindingError,
19
+ DatasourceModelValidationError,
13
20
  )
14
- from trilogy.core.enums import ComparisonOperator
15
- from trilogy.core.exceptions import DatasourceModelValidationError
16
21
  from trilogy.core.models.build import (
17
22
  BuildComparison,
18
23
  BuildDatasource,
@@ -64,6 +69,7 @@ def validate_datasource(
64
69
  env: Environment,
65
70
  build_env: BuildEnvironment,
66
71
  exec: Executor | None = None,
72
+ fix: bool = False,
67
73
  ) -> list[ValidationTest]:
68
74
  results: list[ValidationTest] = []
69
75
  # we might have merged concepts, where both will map out to the same
@@ -109,14 +115,7 @@ def validate_datasource(
109
115
  )
110
116
  )
111
117
  return results
112
- failures: list[
113
- tuple[
114
- str,
115
- Any,
116
- DataType | ArrayType | StructType | MapType | NumericType | TraitDataType,
117
- bool,
118
- ]
119
- ] = []
118
+ failures: list[DatasourceColumnBindingData] = []
120
119
  cols_with_error = set()
121
120
  for row in rows:
122
121
  for col in datasource.columns:
@@ -127,26 +126,29 @@ def validate_datasource(
127
126
  passed = type_check(rval, col.concept.datatype, col.is_nullable)
128
127
  if not passed:
129
128
  failures.append(
130
- (
131
- col.concept.address,
132
- rval,
133
- col.concept.datatype,
134
- col.is_nullable,
129
+ DatasourceColumnBindingData(
130
+ address=col.concept.address,
131
+ value=rval,
132
+ value_type=(
133
+ arg_to_datatype(rval)
134
+ if rval is not None
135
+ else col.concept.datatype
136
+ ),
137
+ value_modifiers=[Modifier.NULLABLE] if rval is None else [],
138
+ actual_type=col.concept.datatype,
139
+ actual_modifiers=col.concept.modifiers,
135
140
  )
136
141
  )
137
142
  cols_with_error.add(actual_address)
138
143
 
139
- def format_failure(failure):
140
- return f"Concept {failure[0]} value '{failure[1]}' does not conform to expected type {str(failure[2])} (nullable={failure[3]})"
141
-
142
144
  if failures:
143
145
  results.append(
144
146
  ValidationTest(
145
147
  check_type=ExpectationType.LOGICAL,
146
148
  expected="datatype_match",
147
149
  ran=True,
148
- result=DatasourceModelValidationError(
149
- f"Datasource {datasource.name} failed validation. Found rows that do not conform to types: {[format_failure(failure) for failure in failures]}",
150
+ result=DatasourceColumnBindingError(
151
+ address=datasource.identifier, errors=failures
150
152
  ),
151
153
  )
152
154
  )
@@ -15,9 +15,10 @@ def validate_environment(
15
15
  scope: ValidationScope = ValidationScope.ALL,
16
16
  targets: list[str] | None = None,
17
17
  exec: Executor | None = None,
18
+ generate_only: bool = False,
18
19
  ) -> list[ValidationTest]:
19
20
  # avoid mutating the environment for validation
20
- generate_only = exec is None
21
+ generate_only = exec is None or generate_only
21
22
  env = env.duplicate()
22
23
  grain_check = function_to_concept(
23
24
  parent=Function(
@@ -0,0 +1,106 @@
1
+ from collections import defaultdict
2
+ from pathlib import Path
3
+ from typing import Any
4
+
5
+ from trilogy import Environment, Executor
6
+ from trilogy.authoring import ConceptDeclarationStatement, Datasource
7
+ from trilogy.core.exceptions import (
8
+ DatasourceColumnBindingData,
9
+ DatasourceColumnBindingError,
10
+ )
11
+ from trilogy.core.validation.environment import validate_environment
12
+ from trilogy.parsing.render import Renderer
13
+
14
+
15
+ def rewrite_file_with_errors(
16
+ statements: list[Any], errors: list[DatasourceColumnBindingError]
17
+ ):
18
+ renderer = Renderer()
19
+ output = []
20
+ ds_error_map: dict[str, list[DatasourceColumnBindingData]] = defaultdict(list)
21
+ concept_error_map: dict[str, list[DatasourceColumnBindingData]] = defaultdict(list)
22
+ for error in errors:
23
+ if isinstance(error, DatasourceColumnBindingError):
24
+ for x in error.errors:
25
+ if error.dataset_address not in ds_error_map:
26
+ ds_error_map[error.dataset_address] = []
27
+ # this is by dataset address
28
+ if x.is_modifier_issue():
29
+ ds_error_map[error.dataset_address].append(x)
30
+ # this is by column
31
+ if x.is_type_issue():
32
+ concept_error_map[x.address].append(x)
33
+ for statement in statements:
34
+ if isinstance(statement, Datasource):
35
+ if statement.identifier in ds_error_map:
36
+ error_cols = ds_error_map[statement.identifier]
37
+ for col in statement.columns:
38
+ if col.concept.address in [x.address for x in error_cols]:
39
+ error_col = [
40
+ x for x in error_cols if x.address == col.concept.address
41
+ ][0]
42
+ col.modifiers = list(
43
+ set(col.modifiers + error_col.value_modifiers)
44
+ )
45
+ elif isinstance(statement, ConceptDeclarationStatement):
46
+ if statement.concept.address in concept_error_map:
47
+ error_cols = concept_error_map[statement.concept.address]
48
+ statement.concept.datatype = error_cols[0].value_type
49
+ output.append(statement)
50
+
51
+ return renderer.render_statement_string(output)
52
+
53
+
54
+ DEPTH_CUTOFF = 3
55
+
56
+
57
+ def validate_and_rewrite(
58
+ input: Path | str, exec: Executor | None = None, depth: int = 0
59
+ ) -> str | None:
60
+ if depth > DEPTH_CUTOFF:
61
+ print(f"Reached depth cutoff of {DEPTH_CUTOFF}, stopping.")
62
+ return None
63
+ if isinstance(input, str):
64
+ raw = input
65
+ env = Environment()
66
+ else:
67
+ with open(input, "r") as f:
68
+ raw = f.read()
69
+ env = Environment(working_path=input.parent)
70
+ if exec:
71
+ env = exec.environment
72
+ env, statements = env.parse(raw)
73
+
74
+ validation_results = validate_environment(env, exec=exec, generate_only=True)
75
+
76
+ errors = [
77
+ x.result
78
+ for x in validation_results
79
+ if isinstance(x.result, DatasourceColumnBindingError)
80
+ ]
81
+
82
+ if not errors:
83
+ print("No validation errors found")
84
+ return None
85
+ print(
86
+ f"Found {len(errors)} validation errors, attempting to fix, current depth: {depth}..."
87
+ )
88
+ for error in errors:
89
+ for item in error.errors:
90
+ print(f"- {item.format_failure()}")
91
+
92
+ new_text = rewrite_file_with_errors(statements, errors)
93
+
94
+ while iteration := validate_and_rewrite(new_text, exec=exec, depth=depth + 1):
95
+ depth = depth + 1
96
+ if depth >= DEPTH_CUTOFF:
97
+ break
98
+ if iteration:
99
+ new_text = iteration
100
+ depth += 1
101
+ if isinstance(input, Path):
102
+ with open(input, "w") as f:
103
+ f.write(new_text)
104
+ return None
105
+ else:
106
+ return new_text
trilogy/dialect/base.py CHANGED
@@ -761,6 +761,7 @@ class BaseDialect:
761
761
  elif isinstance(e, MagicConstants):
762
762
  if e == MagicConstants.NULL:
763
763
  return "null"
764
+ return str(e.value)
764
765
  elif isinstance(e, date):
765
766
  return self.FUNCTION_MAP[FunctionType.DATE_LITERAL](e)
766
767
  elif isinstance(e, datetime):
@@ -1139,7 +1140,7 @@ class BaseDialect:
1139
1140
  if isinstance(query, ProcessedShowStatement):
1140
1141
  return ";\n".join(
1141
1142
  [
1142
- f'{self.EXPLAIN_KEYWORD} {self.compile_statement(x)}'
1143
+ f"{self.EXPLAIN_KEYWORD} {self.compile_statement(x)}"
1143
1144
  for x in query.output_values
1144
1145
  if isinstance(x, (ProcessedQuery, ProcessedCopyStatement))
1145
1146
  ]
@@ -379,14 +379,16 @@ class ParseToObjects(Transformer):
379
379
  def start(self, args):
380
380
  return args
381
381
 
382
+ def LINE_SEPARATOR(self, args):
383
+ return MagicConstants.LINE_SEPARATOR
384
+
382
385
  def block(self, args):
383
386
  output = args[0]
384
387
  if isinstance(output, ConceptDeclarationStatement):
385
- if len(args) > 1 and isinstance(args[1], Comment):
386
- output.concept.metadata.description = (
387
- output.concept.metadata.description
388
- or args[1].text.split("#")[1].strip()
389
- )
388
+ if len(args) > 1 and args[1] != MagicConstants.LINE_SEPARATOR:
389
+ comments = [x for x in args[1:] if isinstance(x, Comment)]
390
+ merged = "\n".join([x.text.split("#")[1].rstrip() for x in comments])
391
+ output.concept.metadata.description = merged
390
392
  # this is a bad plan for now;
391
393
  # because a comment after an import statement is very common
392
394
  # and it's not intuitive that it modifies the import description
@@ -913,7 +915,7 @@ class ParseToObjects(Transformer):
913
915
  return Comment(text=args[0].value)
914
916
 
915
917
  def PARSE_COMMENT(self, args):
916
- return Comment(text=args.value)
918
+ return Comment(text=args.value.rstrip())
917
919
 
918
920
  @v_args(meta=True)
919
921
  def select_transform(self, meta: Meta, args) -> ConceptTransform:
trilogy/parsing/render.py CHANGED
@@ -1,6 +1,7 @@
1
1
  from collections import defaultdict
2
2
  from datetime import date, datetime
3
3
  from functools import singledispatchmethod
4
+ from typing import Any
4
5
 
5
6
  from jinja2 import Template
6
7
 
@@ -12,6 +13,7 @@ from trilogy.core.models.author import (
12
13
  AlignItem,
13
14
  CaseElse,
14
15
  CaseWhen,
16
+ Comment,
15
17
  Comparison,
16
18
  Concept,
17
19
  ConceptRef,
@@ -83,6 +85,23 @@ class Renderer:
83
85
  def __init__(self, environment: Environment | None = None):
84
86
  self.environment = environment
85
87
 
88
+ def render_statement_string(self, list_of_statements: list[Any]) -> str:
89
+ new = []
90
+ last_statement_type = None
91
+ for stmt in list_of_statements:
92
+ stmt_type = type(stmt)
93
+ if last_statement_type is None:
94
+ pass
95
+ elif last_statement_type == Comment:
96
+ new.append("\n")
97
+ elif stmt_type != last_statement_type:
98
+ new.append("\n\n")
99
+ else:
100
+ new.append("\n")
101
+ new.append(Renderer().to_string(stmt))
102
+ last_statement_type = stmt_type
103
+ return "".join(new)
104
+
86
105
  @singledispatchmethod
87
106
  def to_string(self, arg):
88
107
  raise NotImplementedError("Cannot render type {}".format(type(arg)))
@@ -269,6 +288,8 @@ class Renderer:
269
288
  @to_string.register
270
289
  def _(self, arg: "Address"):
271
290
  if arg.is_query:
291
+ if arg.location.startswith("("):
292
+ return f"query '''{arg.location[1:-1]}'''"
272
293
  return f"query '''{arg.location}'''"
273
294
  return f"address {arg.location}"
274
295
 
@@ -286,7 +307,7 @@ class Renderer:
286
307
  def _(self, arg: "ColumnAssignment"):
287
308
  if arg.modifiers:
288
309
  modifiers = "".join(
289
- [self.to_string(modifier) for modifier in arg.modifiers]
310
+ [self.to_string(modifier) for modifier in sorted(arg.modifiers)]
290
311
  )
291
312
  else:
292
313
  modifiers = ""
@@ -328,7 +349,7 @@ class Renderer:
328
349
  else:
329
350
  output = f"{concept.purpose.value} {namespace}{concept.name} <- {self.to_string(concept.lineage)};"
330
351
  if base_description:
331
- output += f" # {base_description}"
352
+ output += f" #{base_description}"
332
353
  return output
333
354
 
334
355
  @to_string.register
@@ -428,6 +449,10 @@ class Renderer:
428
449
  def _(self, arg: "Comparison"):
429
450
  return f"{self.to_string(arg.left)} {arg.operator.value} {self.to_string(arg.right)}"
430
451
 
452
+ @to_string.register
453
+ def _(self, arg: "Comment"):
454
+ return f"{arg.text}"
455
+
431
456
  @to_string.register
432
457
  def _(self, arg: "WindowItem"):
433
458
  over = ",".join(self.to_string(c) for c in arg.over)
@@ -551,8 +576,10 @@ class Renderer:
551
576
  def _(self, arg: Modifier):
552
577
  if arg == Modifier.PARTIAL:
553
578
  return "~"
554
- if arg == Modifier.HIDDEN:
579
+ elif arg == Modifier.HIDDEN:
555
580
  return "--"
581
+ elif arg == Modifier.NULLABLE:
582
+ return "?"
556
583
  return arg.value
557
584
 
558
585
  @to_string.register
@@ -1,5 +1,5 @@
1
- !start: ( block | show_statement )*
2
- block: statement _TERMINATOR PARSE_COMMENT?
1
+ !start: ( block | show_statement | PARSE_COMMENT )*
2
+ block: statement _TERMINATOR LINE_SEPARATOR? PARSE_COMMENT*
3
3
  ?statement: concept
4
4
  | datasource
5
5
  | function
@@ -14,9 +14,12 @@
14
14
  | rawsql_statement
15
15
  | validate_statement
16
16
 
17
- _TERMINATOR: ";"i /\s*/
17
+ _TERMINATOR: ";"i
18
18
 
19
- PARSE_COMMENT.1: /#.*(\n|$)/ | /\/\/.*\n/
19
+ PARSE_COMMENT.1: /#.*(\n|$)/ | /\/\/.*(\n|$)/
20
+
21
+ // when whitespace matters - comment placement
22
+ LINE_SEPARATOR.1: /[ \t\r\f\v]*\n+/
20
23
 
21
24
  // property display_name string
22
25
  concept_declaration: PURPOSE IDENTIFIER data_type concept_nullable_modifier? metadata?