vtlengine 1.0.3rc3__py3-none-any.whl → 1.1__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 vtlengine might be problematic. Click here for more details.
- vtlengine/API/_InternalApi.py +288 -61
- vtlengine/API/__init__.py +269 -71
- vtlengine/API/data/schema/json_schema_2.1.json +116 -0
- vtlengine/AST/ASTComment.py +56 -0
- vtlengine/AST/ASTConstructor.py +76 -22
- vtlengine/AST/ASTConstructorModules/Expr.py +238 -120
- vtlengine/AST/ASTConstructorModules/ExprComponents.py +126 -61
- vtlengine/AST/ASTConstructorModules/Terminals.py +97 -42
- vtlengine/AST/ASTConstructorModules/__init__.py +50 -0
- vtlengine/AST/ASTEncoders.py +5 -1
- vtlengine/AST/ASTString.py +608 -0
- vtlengine/AST/ASTTemplate.py +28 -2
- vtlengine/AST/DAG/__init__.py +10 -4
- vtlengine/AST/Grammar/lexer.py +0 -1
- vtlengine/AST/Grammar/parser.py +185 -440
- vtlengine/AST/VtlVisitor.py +0 -1
- vtlengine/AST/__init__.py +127 -14
- vtlengine/DataTypes/TimeHandling.py +50 -15
- vtlengine/DataTypes/__init__.py +79 -7
- vtlengine/Exceptions/__init__.py +3 -5
- vtlengine/Exceptions/messages.py +74 -105
- vtlengine/Interpreter/__init__.py +136 -46
- vtlengine/Model/__init__.py +14 -11
- vtlengine/Operators/Aggregation.py +17 -9
- vtlengine/Operators/Analytic.py +64 -20
- vtlengine/Operators/Assignment.py +0 -1
- vtlengine/Operators/CastOperator.py +44 -44
- vtlengine/Operators/Clause.py +16 -10
- vtlengine/Operators/Comparison.py +20 -12
- vtlengine/Operators/Conditional.py +47 -15
- vtlengine/Operators/General.py +9 -4
- vtlengine/Operators/HROperators.py +4 -14
- vtlengine/Operators/Join.py +15 -14
- vtlengine/Operators/Numeric.py +32 -26
- vtlengine/Operators/RoleSetter.py +6 -2
- vtlengine/Operators/Set.py +12 -8
- vtlengine/Operators/String.py +9 -9
- vtlengine/Operators/Time.py +145 -124
- vtlengine/Operators/Validation.py +10 -4
- vtlengine/Operators/__init__.py +56 -69
- vtlengine/Utils/__init__.py +55 -1
- vtlengine/__extras_check.py +17 -0
- vtlengine/__init__.py +2 -2
- vtlengine/files/output/__init__.py +2 -1
- vtlengine/files/output/_time_period_representation.py +2 -1
- vtlengine/files/parser/__init__.py +52 -46
- vtlengine/files/parser/_time_checking.py +4 -4
- {vtlengine-1.0.3rc3.dist-info → vtlengine-1.1.dist-info}/METADATA +21 -17
- vtlengine-1.1.dist-info/RECORD +61 -0
- {vtlengine-1.0.3rc3.dist-info → vtlengine-1.1.dist-info}/WHEEL +1 -1
- vtlengine/DataTypes/NumericTypesHandling.py +0 -38
- vtlengine-1.0.3rc3.dist-info/RECORD +0 -58
- {vtlengine-1.0.3rc3.dist-info → vtlengine-1.1.dist-info}/LICENSE.md +0 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Any, Optional, Tuple, Union
|
|
4
|
+
|
|
5
|
+
import vtlengine.AST.Grammar.tokens
|
|
6
|
+
from vtlengine import AST
|
|
7
|
+
from vtlengine.AST import Comment, DPRuleset, HRuleset, Operator, TimeAggregation
|
|
8
|
+
from vtlengine.AST.ASTTemplate import ASTTemplate
|
|
9
|
+
from vtlengine.AST.Grammar.tokens import (
|
|
10
|
+
AGGREGATE,
|
|
11
|
+
ATTRIBUTE,
|
|
12
|
+
CAST,
|
|
13
|
+
CHECK_DATAPOINT,
|
|
14
|
+
CHECK_HIERARCHY,
|
|
15
|
+
DATE_ADD,
|
|
16
|
+
DATEDIFF,
|
|
17
|
+
DROP,
|
|
18
|
+
FILL_TIME_SERIES,
|
|
19
|
+
FILTER,
|
|
20
|
+
HAVING,
|
|
21
|
+
HIERARCHY,
|
|
22
|
+
IDENTIFIER,
|
|
23
|
+
INSTR,
|
|
24
|
+
INTERSECT,
|
|
25
|
+
LOG,
|
|
26
|
+
MAX,
|
|
27
|
+
MEASURE,
|
|
28
|
+
MEMBERSHIP,
|
|
29
|
+
MIN,
|
|
30
|
+
MINUS,
|
|
31
|
+
MOD,
|
|
32
|
+
NVL,
|
|
33
|
+
PLUS,
|
|
34
|
+
POWER,
|
|
35
|
+
RANDOM,
|
|
36
|
+
REPLACE,
|
|
37
|
+
ROUND,
|
|
38
|
+
SETDIFF,
|
|
39
|
+
SUBSTR,
|
|
40
|
+
SYMDIFF,
|
|
41
|
+
TIMESHIFT,
|
|
42
|
+
TRUNC,
|
|
43
|
+
UNION,
|
|
44
|
+
VIRAL_ATTRIBUTE,
|
|
45
|
+
)
|
|
46
|
+
from vtlengine.DataTypes import SCALAR_TYPES_CLASS_REVERSE
|
|
47
|
+
from vtlengine.Model import Component, Dataset
|
|
48
|
+
|
|
49
|
+
nl = "\n"
|
|
50
|
+
tab = "\t"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _handle_literal(value: Union[str, int, float, bool]):
|
|
54
|
+
if isinstance(value, str):
|
|
55
|
+
if '"' in value:
|
|
56
|
+
return value
|
|
57
|
+
return f'"{value}"'
|
|
58
|
+
elif isinstance(value, bool):
|
|
59
|
+
return "true" if value else "false"
|
|
60
|
+
elif isinstance(value, float):
|
|
61
|
+
decimal = str(value).split(".")[1]
|
|
62
|
+
if len(decimal) > 4:
|
|
63
|
+
return f"{value:f}".rstrip("0")
|
|
64
|
+
else:
|
|
65
|
+
return f"{value:g}"
|
|
66
|
+
return str(value)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _format_dataset_eval(dataset: Dataset) -> str:
|
|
70
|
+
def __format_component(component: Component) -> str:
|
|
71
|
+
return (
|
|
72
|
+
f"\n\t\t\t{component.role.value.lower()}"
|
|
73
|
+
f"<{SCALAR_TYPES_CLASS_REVERSE[component.data_type].lower()}> "
|
|
74
|
+
f"{component.name}"
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
return f"{{ {', '.join([__format_component(x) for x in dataset.components.values()])} \n\t\t}}"
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _format_reserved_word(value: str):
|
|
81
|
+
tokens_dict = vtlengine.AST.Grammar.tokens.__dict__
|
|
82
|
+
tokens_dict = {k: v for k, v in tokens_dict.items() if not k.startswith("__")}
|
|
83
|
+
if value in tokens_dict.values():
|
|
84
|
+
return f"'{value}'"
|
|
85
|
+
return value
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass
|
|
89
|
+
class ASTString(ASTTemplate):
|
|
90
|
+
vtl_script: str = ""
|
|
91
|
+
pretty: bool = False
|
|
92
|
+
is_first_assignment: bool = False
|
|
93
|
+
is_from_agg: bool = False # Handler to write grouping at aggr level
|
|
94
|
+
|
|
95
|
+
def render(self, ast: AST.AST) -> str:
|
|
96
|
+
self.vtl_script = ""
|
|
97
|
+
result = self.visit(ast)
|
|
98
|
+
if result:
|
|
99
|
+
self.vtl_script += result
|
|
100
|
+
return self.vtl_script
|
|
101
|
+
|
|
102
|
+
def visit_Start(self, node: AST.Start) -> Any:
|
|
103
|
+
transformations = [
|
|
104
|
+
x for x in node.children if not isinstance(x, (HRuleset, DPRuleset, Operator, Comment))
|
|
105
|
+
]
|
|
106
|
+
for child in node.children:
|
|
107
|
+
if child in transformations:
|
|
108
|
+
self.is_first_assignment = True
|
|
109
|
+
self.visit(child)
|
|
110
|
+
self.vtl_script += "\n"
|
|
111
|
+
|
|
112
|
+
# ---------------------- Rulesets ----------------------
|
|
113
|
+
def visit_HRuleset(self, node: AST.HRuleset) -> None:
|
|
114
|
+
signature = f"{node.signature_type} rule {node.element.value}"
|
|
115
|
+
if self.pretty:
|
|
116
|
+
self.vtl_script += f"define hierarchical ruleset {node.name}({signature}) is{nl}"
|
|
117
|
+
for i, rule in enumerate(node.rules):
|
|
118
|
+
self.vtl_script += f"{tab}{self.visit(rule)}{nl}"
|
|
119
|
+
if rule.erCode:
|
|
120
|
+
self.vtl_script += f"{tab}errorcode {_handle_literal(rule.erCode)}{nl}"
|
|
121
|
+
if rule.erLevel:
|
|
122
|
+
self.vtl_script += f"{tab}errorlevel {rule.erLevel}"
|
|
123
|
+
if i != len(node.rules) - 1:
|
|
124
|
+
self.vtl_script += f";{nl}"
|
|
125
|
+
self.vtl_script += nl
|
|
126
|
+
self.vtl_script += f"end hierarchical ruleset;{nl}"
|
|
127
|
+
else:
|
|
128
|
+
rules_strs = []
|
|
129
|
+
for rule in node.rules:
|
|
130
|
+
rule_str = self.visit(rule)
|
|
131
|
+
if rule.erCode:
|
|
132
|
+
rule_str += f" errorcode {_handle_literal(rule.erCode)}"
|
|
133
|
+
if rule.erLevel:
|
|
134
|
+
rule_str += f" errorlevel {rule.erLevel}"
|
|
135
|
+
rules_strs.append(rule_str)
|
|
136
|
+
rules_sep = "; " if len(rules_strs) > 1 else ""
|
|
137
|
+
rules = rules_sep.join(rules_strs)
|
|
138
|
+
self.vtl_script += (
|
|
139
|
+
f"define hierarchical ruleset {node.name} ({signature}) is {rules} "
|
|
140
|
+
f"end hierarchical ruleset;"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
def visit_HRule(self, node: AST.HRule) -> str:
|
|
144
|
+
vtl_script = ""
|
|
145
|
+
if node.name is not None:
|
|
146
|
+
vtl_script += f"{node.name}: "
|
|
147
|
+
vtl_script += f"{self.visit(node.rule)}"
|
|
148
|
+
return vtl_script
|
|
149
|
+
|
|
150
|
+
def visit_HRBinOp(self, node: AST.HRBinOp) -> str:
|
|
151
|
+
if node.op == "when":
|
|
152
|
+
if self.pretty:
|
|
153
|
+
return (
|
|
154
|
+
f"{tab * 3}when{nl}"
|
|
155
|
+
f"{tab * 4}{self.visit(node.left)}{nl}"
|
|
156
|
+
f"{tab * 3}then{nl}"
|
|
157
|
+
f"{tab * 4}{self.visit(node.right)}"
|
|
158
|
+
)
|
|
159
|
+
else:
|
|
160
|
+
return f"{node.op} {self.visit(node.left)} then {self.visit(node.right)}"
|
|
161
|
+
return f"{self.visit(node.left)} {node.op} {self.visit(node.right)}"
|
|
162
|
+
|
|
163
|
+
def visit_HRUnOp(self, node: AST.HRUnOp) -> str:
|
|
164
|
+
return f"{node.op} {self.visit(node.operand)}"
|
|
165
|
+
|
|
166
|
+
def visit_DefIdentifier(self, node: AST.DefIdentifier) -> str:
|
|
167
|
+
return node.value
|
|
168
|
+
|
|
169
|
+
def visit_DPRule(self, node: AST.DPRule) -> str:
|
|
170
|
+
if self.pretty:
|
|
171
|
+
lines = []
|
|
172
|
+
if node.name is not None:
|
|
173
|
+
lines.append(f"{tab}{node.name}: ")
|
|
174
|
+
lines.append(self.visit(node.rule))
|
|
175
|
+
if node.erCode is not None:
|
|
176
|
+
lines.append(f"{tab * 3}errorcode {_handle_literal(node.erCode)}")
|
|
177
|
+
if node.erLevel is not None:
|
|
178
|
+
lines.append(f"{tab * 3}errorlevel {node.erLevel}; ")
|
|
179
|
+
return nl.join(lines)
|
|
180
|
+
else:
|
|
181
|
+
vtl_script = ""
|
|
182
|
+
if node.name is not None:
|
|
183
|
+
vtl_script += f"{node.name}: "
|
|
184
|
+
vtl_script += f"{self.visit(node.rule)}"
|
|
185
|
+
if node.erCode is not None:
|
|
186
|
+
vtl_script += f" errorcode {_handle_literal(node.erCode)}"
|
|
187
|
+
if node.erLevel is not None:
|
|
188
|
+
vtl_script += f" errorlevel {node.erLevel}"
|
|
189
|
+
return vtl_script
|
|
190
|
+
|
|
191
|
+
def visit_DPRIdentifier(self, node: AST.DPRIdentifier) -> str:
|
|
192
|
+
vtl_script = f"{node.value}"
|
|
193
|
+
if node.alias is not None:
|
|
194
|
+
vtl_script += f" as {node.alias}"
|
|
195
|
+
return vtl_script
|
|
196
|
+
|
|
197
|
+
def visit_DPRuleset(self, node: AST.DPRuleset) -> None:
|
|
198
|
+
rules_sep = "; " if len(node.rules) > 1 else ""
|
|
199
|
+
signature_sep = ", " if len(node.params) > 1 else ""
|
|
200
|
+
signature = (
|
|
201
|
+
f"{node.signature_type} {signature_sep.join([self.visit(x) for x in node.params])}"
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
if self.pretty:
|
|
205
|
+
self.vtl_script += f"define datapoint ruleset {node.name}({signature}) is {nl}"
|
|
206
|
+
for rule in node.rules:
|
|
207
|
+
self.vtl_script += f"\t{self.visit(rule)}{nl * 2}"
|
|
208
|
+
self.vtl_script += f"end datapoint ruleset;{nl}"
|
|
209
|
+
else:
|
|
210
|
+
rules = rules_sep.join([self.visit(x) for x in node.rules])
|
|
211
|
+
self.vtl_script += (
|
|
212
|
+
f"define datapoint ruleset {node.name} "
|
|
213
|
+
f"({signature}) is {rules} end datapoint ruleset;"
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# ---------------------- User Defined Operators ----------------------
|
|
217
|
+
|
|
218
|
+
def visit_Argument(self, node: AST.Argument) -> str:
|
|
219
|
+
default = f" default {self.visit(node.default)}" if node.default is not None else ""
|
|
220
|
+
|
|
221
|
+
if isinstance(node.type_, Dataset):
|
|
222
|
+
argument_type = "dataset"
|
|
223
|
+
elif isinstance(node.type_, Component):
|
|
224
|
+
argument_type = "component"
|
|
225
|
+
else:
|
|
226
|
+
argument_type = node.type_.__name__.lower()
|
|
227
|
+
|
|
228
|
+
name = _format_reserved_word(node.name)
|
|
229
|
+
|
|
230
|
+
return f"{name} {argument_type}{default}"
|
|
231
|
+
|
|
232
|
+
def visit_Operator(self, node: AST.Operator) -> None:
|
|
233
|
+
signature_sep = ", " if len(node.parameters) > 1 else ""
|
|
234
|
+
signature = signature_sep.join([self.visit(x) for x in node.parameters])
|
|
235
|
+
if self.pretty:
|
|
236
|
+
self.vtl_script += f"define operator {node.op}({signature}){nl}"
|
|
237
|
+
self.vtl_script += f"\treturns {node.output_type.lower()} is{nl}"
|
|
238
|
+
expression = self.visit(node.expression)
|
|
239
|
+
if "(" in expression:
|
|
240
|
+
expression = expression.replace("(", f"({nl}{tab * 2}")
|
|
241
|
+
expression = expression.replace(")", f"{nl}{tab * 2})")
|
|
242
|
+
|
|
243
|
+
self.vtl_script += f"{tab * 2}{expression}{nl}"
|
|
244
|
+
self.vtl_script += f"end operator;{nl}"
|
|
245
|
+
else:
|
|
246
|
+
body = f"returns {node.output_type.lower()} is {self.visit(node.expression)}"
|
|
247
|
+
self.vtl_script += f"define operator {node.op}({signature}) {body} end operator;"
|
|
248
|
+
|
|
249
|
+
# ---------------------- Basic Operators ----------------------
|
|
250
|
+
def visit_Assignment(self, node: AST.Assignment) -> Optional[str]:
|
|
251
|
+
return_element = not copy.deepcopy(self.is_first_assignment)
|
|
252
|
+
is_first = self.is_first_assignment
|
|
253
|
+
if is_first:
|
|
254
|
+
self.is_first_assignment = False
|
|
255
|
+
if self.pretty:
|
|
256
|
+
if is_first:
|
|
257
|
+
expression = f"{self.visit(node.left)} {node.op}{nl}{tab}{self.visit(node.right)}"
|
|
258
|
+
else:
|
|
259
|
+
expression = f"{self.visit(node.left)} {node.op} {self.visit(node.right)}"
|
|
260
|
+
else:
|
|
261
|
+
expression = f"{self.visit(node.left)} {node.op} {self.visit(node.right)}"
|
|
262
|
+
if return_element:
|
|
263
|
+
return expression
|
|
264
|
+
self.vtl_script += f"{expression};"
|
|
265
|
+
|
|
266
|
+
def visit_PersistentAssignment(self, node: AST.PersistentAssignment) -> Optional[str]:
|
|
267
|
+
return self.visit_Assignment(node)
|
|
268
|
+
|
|
269
|
+
def visit_BinOp(self, node: AST.BinOp) -> str:
|
|
270
|
+
if node.op in [NVL, LOG, MOD, POWER, RANDOM, TIMESHIFT, DATEDIFF]:
|
|
271
|
+
return f"{node.op}({self.visit(node.left)}, {self.visit(node.right)})"
|
|
272
|
+
elif node.op == MEMBERSHIP:
|
|
273
|
+
return f"{self.visit(node.left)} {node.op} {self.visit(node.right)}"
|
|
274
|
+
if self.pretty:
|
|
275
|
+
return f"{self.visit(node.left)} {node.op} {self.visit(node.right)}"
|
|
276
|
+
|
|
277
|
+
return f"{self.visit(node.left)} {node.op} {self.visit(node.right)}"
|
|
278
|
+
|
|
279
|
+
def visit_UnaryOp(self, node: AST.UnaryOp) -> str:
|
|
280
|
+
if node.op in [PLUS, MINUS]:
|
|
281
|
+
return f"{node.op}{self.visit(node.operand)}"
|
|
282
|
+
elif node.op in [IDENTIFIER, ATTRIBUTE, VIRAL_ATTRIBUTE]:
|
|
283
|
+
return f"{node.op} {self.visit(node.operand)}"
|
|
284
|
+
elif node.op == MEASURE:
|
|
285
|
+
return self.visit(node.operand)
|
|
286
|
+
|
|
287
|
+
if self.pretty:
|
|
288
|
+
return f"{node.op} {self.visit(node.operand)}"
|
|
289
|
+
return f"{node.op}({self.visit(node.operand)})"
|
|
290
|
+
|
|
291
|
+
def visit_MulOp(self, node: AST.MulOp) -> str:
|
|
292
|
+
sep = ", " if len(node.children) > 1 else ""
|
|
293
|
+
body = sep.join([self.visit(x) for x in node.children])
|
|
294
|
+
if self.pretty:
|
|
295
|
+
return f"{node.op}({body})"
|
|
296
|
+
return f"{node.op}({body})"
|
|
297
|
+
|
|
298
|
+
def visit_ParamOp(self, node: AST.ParamOp) -> str:
|
|
299
|
+
if node.op == HAVING:
|
|
300
|
+
return f"{node.op} {self.visit(node.params)}"
|
|
301
|
+
elif node.op in [SUBSTR, INSTR, REPLACE, ROUND, TRUNC, UNION, SETDIFF, SYMDIFF, INTERSECT]:
|
|
302
|
+
params_sep = ", " if len(node.params) > 1 else ""
|
|
303
|
+
return (
|
|
304
|
+
f"{node.op}({self.visit(node.children[0])}, "
|
|
305
|
+
f"{params_sep.join([self.visit(x) for x in node.params])})"
|
|
306
|
+
)
|
|
307
|
+
elif node.op in (CHECK_HIERARCHY, HIERARCHY):
|
|
308
|
+
operand = self.visit(node.children[0])
|
|
309
|
+
component_name = self.visit(node.children[1])
|
|
310
|
+
rule_name = self.visit(node.children[2])
|
|
311
|
+
param_mode_value = node.params[0].value
|
|
312
|
+
param_input_value = node.params[1].value
|
|
313
|
+
param_output_value = node.params[2].value
|
|
314
|
+
|
|
315
|
+
default_value_input = "dataset" if node.op == CHECK_HIERARCHY else "rule"
|
|
316
|
+
default_value_output = "invalid" if node.op == CHECK_HIERARCHY else "computed"
|
|
317
|
+
|
|
318
|
+
param_mode = f" {param_mode_value}" if param_mode_value != "non_null" else ""
|
|
319
|
+
param_input = (
|
|
320
|
+
f" {param_input_value}" if param_input_value != default_value_input else ""
|
|
321
|
+
)
|
|
322
|
+
param_output = (
|
|
323
|
+
f" {param_output_value}" if param_output_value != default_value_output else ""
|
|
324
|
+
)
|
|
325
|
+
if self.pretty:
|
|
326
|
+
return (
|
|
327
|
+
f"{node.op}({nl}{tab * 2}{operand},{nl}{tab * 2}{rule_name},{nl}{tab * 2}rule "
|
|
328
|
+
f"{component_name}"
|
|
329
|
+
f"{param_mode}{param_input}{param_output})"
|
|
330
|
+
)
|
|
331
|
+
else:
|
|
332
|
+
return (
|
|
333
|
+
f"{node.op}({operand}, {rule_name} rule {component_name}"
|
|
334
|
+
f"{param_mode}{param_input}{param_output})"
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
elif node.op == CHECK_DATAPOINT:
|
|
338
|
+
operand = self.visit(node.children[0])
|
|
339
|
+
rule_name = node.children[1]
|
|
340
|
+
output = ""
|
|
341
|
+
if len(node.params) == 1 and node.params[0] != "invalid":
|
|
342
|
+
output = f"{node.params[0]}"
|
|
343
|
+
if self.pretty:
|
|
344
|
+
return f"{node.op}({nl}{tab}{operand},{nl}{tab}{rule_name}{nl}{tab}{output}{nl})"
|
|
345
|
+
else:
|
|
346
|
+
return f"{node.op}({operand}, {rule_name}{output})"
|
|
347
|
+
elif node.op == CAST:
|
|
348
|
+
operand = self.visit(node.children[0])
|
|
349
|
+
data_type = SCALAR_TYPES_CLASS_REVERSE[node.children[1]].lower()
|
|
350
|
+
mask = ""
|
|
351
|
+
if len(node.params) == 1:
|
|
352
|
+
mask = f", {self.visit(node.params[0])}"
|
|
353
|
+
if self.pretty:
|
|
354
|
+
return f"{node.op}({operand},{data_type}{mask})"
|
|
355
|
+
else:
|
|
356
|
+
return f"{node.op}({operand},{data_type}{mask})"
|
|
357
|
+
elif node.op == FILL_TIME_SERIES:
|
|
358
|
+
operand = self.visit(node.children[0])
|
|
359
|
+
param = node.params[0].value if node.params else "all"
|
|
360
|
+
if self.pretty:
|
|
361
|
+
return f"{node.op}({operand},{param})"
|
|
362
|
+
else:
|
|
363
|
+
return f"{node.op}({operand}, {param})"
|
|
364
|
+
elif node.op == DATE_ADD:
|
|
365
|
+
operand = self.visit(node.children[0])
|
|
366
|
+
shift_number = self.visit(node.params[0])
|
|
367
|
+
period_indicator = self.visit(node.params[1])
|
|
368
|
+
if self.pretty:
|
|
369
|
+
return (
|
|
370
|
+
f"{node.op}({nl}{tab * 2}{operand},{nl}{tab * 2}{shift_number},"
|
|
371
|
+
f"{nl}{tab * 2}"
|
|
372
|
+
f"{period_indicator})"
|
|
373
|
+
)
|
|
374
|
+
else:
|
|
375
|
+
return f"{node.op}({operand}, {shift_number}, {period_indicator})"
|
|
376
|
+
|
|
377
|
+
# ---------------------- Individual operators ----------------------
|
|
378
|
+
|
|
379
|
+
def _handle_grouping_having(self, node: AST) -> Tuple[str, str]:
|
|
380
|
+
if self.is_from_agg:
|
|
381
|
+
return "", ""
|
|
382
|
+
grouping = ""
|
|
383
|
+
if node.grouping is not None:
|
|
384
|
+
grouping_sep = ", " if len(node.grouping) > 1 else ""
|
|
385
|
+
grouping_values = []
|
|
386
|
+
for grouping_value in node.grouping:
|
|
387
|
+
if isinstance(grouping_value, TimeAggregation):
|
|
388
|
+
grouping_values.append(self.visit(grouping_value))
|
|
389
|
+
else:
|
|
390
|
+
grouping_values.append(grouping_value.value)
|
|
391
|
+
grouping = f" {node.grouping_op} {grouping_sep.join(grouping_values)}"
|
|
392
|
+
having = f" {self.visit(node.having_clause)}" if node.having_clause is not None else ""
|
|
393
|
+
return grouping, having
|
|
394
|
+
|
|
395
|
+
def visit_Aggregation(self, node: AST.Aggregation) -> str:
|
|
396
|
+
grouping, having = self._handle_grouping_having(node)
|
|
397
|
+
if self.pretty and node.op not in (MAX, MIN):
|
|
398
|
+
operand = self.visit(node.operand)
|
|
399
|
+
return f"{node.op}({nl}{tab * 2}{operand}{grouping}{having}{nl}{tab * 2})"
|
|
400
|
+
return f"{node.op}({self.visit(node.operand)}{grouping}{having})"
|
|
401
|
+
|
|
402
|
+
def visit_Analytic(self, node: AST.Analytic) -> str:
|
|
403
|
+
operand = "" if node.operand is None else self.visit(node.operand)
|
|
404
|
+
partition = ""
|
|
405
|
+
if node.partition_by:
|
|
406
|
+
partition_sep = ", " if len(node.partition_by) > 1 else ""
|
|
407
|
+
partition = f"partition by {partition_sep.join(node.partition_by)}"
|
|
408
|
+
order = ""
|
|
409
|
+
if node.order_by:
|
|
410
|
+
order_sep = ", " if len(node.order_by) > 1 else ""
|
|
411
|
+
order = f" order by {order_sep.join([self.visit(x) for x in node.order_by])}"
|
|
412
|
+
window = f" {self.visit(node.window)}" if node.window is not None else ""
|
|
413
|
+
params = ""
|
|
414
|
+
if node.params:
|
|
415
|
+
params = "" if len(node.params) == 0 else f", {int(node.params[0])}"
|
|
416
|
+
if self.pretty:
|
|
417
|
+
result = (
|
|
418
|
+
f"{node.op}({nl}{tab * 3}{operand}{params} over({partition}{order} {window})"
|
|
419
|
+
f"{nl}{tab * 2})"
|
|
420
|
+
)
|
|
421
|
+
else:
|
|
422
|
+
result = f"{node.op}({operand}{params} over ({partition}{order}{window}))"
|
|
423
|
+
|
|
424
|
+
return result
|
|
425
|
+
|
|
426
|
+
def visit_Case(self, node: AST.Case) -> str:
|
|
427
|
+
if self.pretty:
|
|
428
|
+
else_str = f"{nl}{tab * 2}else{nl}{tab * 3}{self.visit(node.elseOp)}"
|
|
429
|
+
body_sep = " " if len(node.cases) > 1 else ""
|
|
430
|
+
body = body_sep.join([self.visit(x) for x in node.cases])
|
|
431
|
+
return f"case {body} {else_str}"
|
|
432
|
+
else:
|
|
433
|
+
else_str = f"else {self.visit(node.elseOp)}"
|
|
434
|
+
body_sep = " " if len(node.cases) > 1 else ""
|
|
435
|
+
body = body_sep.join([self.visit(x) for x in node.cases])
|
|
436
|
+
return f"case {body} {else_str}"
|
|
437
|
+
|
|
438
|
+
def visit_CaseObj(self, node: AST.CaseObj) -> str:
|
|
439
|
+
if self.pretty:
|
|
440
|
+
return (
|
|
441
|
+
f"{nl}{tab * 2}when{nl}{tab * 3}{self.visit(node.condition)}{nl}{tab * 2}then"
|
|
442
|
+
f"{nl}{tab * 3}{self.visit(node.thenOp)}"
|
|
443
|
+
)
|
|
444
|
+
else:
|
|
445
|
+
return f"when {self.visit(node.condition)} then {self.visit(node.thenOp)}"
|
|
446
|
+
|
|
447
|
+
def visit_EvalOp(self, node: AST.EvalOp) -> str:
|
|
448
|
+
operand_sep = ", " if len(node.operands) > 1 else ""
|
|
449
|
+
if self.pretty:
|
|
450
|
+
operands = operand_sep.join([self.visit(x) for x in node.operands])
|
|
451
|
+
ext_routine = f"{nl}{tab * 2}{node.name}({operands})"
|
|
452
|
+
language = f"{nl}{tab * 2}language {_handle_literal(node.language)}{nl}"
|
|
453
|
+
output = f"{tab * 2}returns dataset {_format_dataset_eval(node.output)}"
|
|
454
|
+
return f"eval({ext_routine} {language} {output})"
|
|
455
|
+
else:
|
|
456
|
+
operands = operand_sep.join([self.visit(x) for x in node.operands])
|
|
457
|
+
ext_routine = f"{node.name}({operands})"
|
|
458
|
+
language = f"language {_handle_literal(node.language)}"
|
|
459
|
+
output = f"returns dataset {_format_dataset_eval(node.output)}"
|
|
460
|
+
return f"eval({ext_routine} {language} {output})"
|
|
461
|
+
|
|
462
|
+
def visit_If(self, node: AST.If) -> str:
|
|
463
|
+
if self.pretty:
|
|
464
|
+
else_str = (
|
|
465
|
+
f"else{nl}{tab * 5}{self.visit(node.elseOp)}" if node.elseOp is not None else ""
|
|
466
|
+
)
|
|
467
|
+
return (
|
|
468
|
+
f"{nl}{tab * 4}if {nl}{tab * 5}{self.visit(node.condition)} "
|
|
469
|
+
f"{nl}{tab * 4}then {nl}{tab * 5}{self.visit(node.thenOp)}{nl}{tab * 4}{else_str}"
|
|
470
|
+
)
|
|
471
|
+
else:
|
|
472
|
+
else_str = f"else {self.visit(node.elseOp)}" if node.elseOp is not None else ""
|
|
473
|
+
return f"if {self.visit(node.condition)} then {self.visit(node.thenOp)} {else_str}"
|
|
474
|
+
|
|
475
|
+
def visit_JoinOp(self, node: AST.JoinOp) -> str:
|
|
476
|
+
if self.pretty:
|
|
477
|
+
sep = f",{nl}{tab * 2}" if len(node.clauses) > 1 else ""
|
|
478
|
+
clauses = sep.join([self.visit(x) for x in node.clauses])
|
|
479
|
+
using = ""
|
|
480
|
+
if node.using is not None:
|
|
481
|
+
using_sep = ", " if len(node.using) > 1 else ""
|
|
482
|
+
using = f"using {using_sep.join(node.using)}"
|
|
483
|
+
return f"{node.op}({nl}{tab * 2}{clauses}{nl}{tab * 2}{using})"
|
|
484
|
+
else:
|
|
485
|
+
sep = ", " if len(node.clauses) > 1 else ""
|
|
486
|
+
clauses = sep.join([self.visit(x) for x in node.clauses])
|
|
487
|
+
using = ""
|
|
488
|
+
if node.using is not None:
|
|
489
|
+
using_sep = ", " if len(node.using) > 1 else ""
|
|
490
|
+
using = f" using {using_sep.join(node.using)}"
|
|
491
|
+
return f"{node.op}({clauses}{using})"
|
|
492
|
+
|
|
493
|
+
def visit_ParFunction(self, node: AST.ParFunction) -> str:
|
|
494
|
+
return f"({self.visit(node.operand)})"
|
|
495
|
+
|
|
496
|
+
def visit_RegularAggregation(self, node: AST.RegularAggregation) -> str:
|
|
497
|
+
child_sep = ", " if len(node.children) > 1 else ""
|
|
498
|
+
if node.op == AGGREGATE:
|
|
499
|
+
self.is_from_agg = True
|
|
500
|
+
body = child_sep.join([self.visit(x) for x in node.children])
|
|
501
|
+
self.is_from_agg = False
|
|
502
|
+
grouping, having = self._handle_grouping_having(node.children[0].right)
|
|
503
|
+
body = f"{nl}{tab * 3}{body}{nl}{tab * 3}{grouping}{having}{nl}{tab * 2}"
|
|
504
|
+
elif node.op == DROP and self.pretty:
|
|
505
|
+
drop_sep = f",{nl}{tab * 3}" if len(node.children) > 1 else ""
|
|
506
|
+
body = f"{drop_sep.join([self.visit(x) for x in node.children])}{nl}{tab * 2}"
|
|
507
|
+
elif node.op == FILTER and self.pretty:
|
|
508
|
+
condition = self.visit(node.children[0])
|
|
509
|
+
if " and " in condition or " or " in condition:
|
|
510
|
+
for op in (" and ", " or "):
|
|
511
|
+
condition = condition.replace(op, f"{op}{nl}{tab * 5}")
|
|
512
|
+
body = f"{nl}{tab * 4}{condition}{nl}{tab * 2}"
|
|
513
|
+
else:
|
|
514
|
+
body = child_sep.join([self.visit(x) for x in node.children])
|
|
515
|
+
if isinstance(node.dataset, AST.JoinOp):
|
|
516
|
+
dataset = self.visit(node.dataset)
|
|
517
|
+
if self.pretty:
|
|
518
|
+
return f"{dataset[:-1]}{(node.op)} {body}{nl}{tab})"
|
|
519
|
+
else:
|
|
520
|
+
return f"{dataset[:-1]} {node.op} {body})"
|
|
521
|
+
else:
|
|
522
|
+
dataset = self.visit(node.dataset)
|
|
523
|
+
if self.pretty:
|
|
524
|
+
return f"{dataset}{nl}{tab * 2}[{node.op} {body}]"
|
|
525
|
+
else:
|
|
526
|
+
return f"{dataset} [{node.op} {body}]"
|
|
527
|
+
|
|
528
|
+
def visit_RenameNode(self, node: AST.RenameNode) -> str:
|
|
529
|
+
return f"{node.old_name} to {node.new_name}"
|
|
530
|
+
|
|
531
|
+
def visit_TimeAggregation(self, node: AST.TimeAggregation) -> str:
|
|
532
|
+
operand = self.visit(node.operand)
|
|
533
|
+
period_from = "_" if node.period_from is None else node.period_from
|
|
534
|
+
period_to = _handle_literal(node.period_to)
|
|
535
|
+
if self.pretty:
|
|
536
|
+
return f"{node.op}({period_to},{period_from},{operand})"
|
|
537
|
+
else:
|
|
538
|
+
return f"{node.op}({period_to}, {period_from}, {operand})"
|
|
539
|
+
|
|
540
|
+
def visit_UDOCall(self, node: AST.UDOCall) -> str:
|
|
541
|
+
params_sep = ", " if len(node.params) > 1 else ""
|
|
542
|
+
params = params_sep.join([self.visit(x) for x in node.params])
|
|
543
|
+
return f"{node.op}({params})"
|
|
544
|
+
|
|
545
|
+
def visit_Validation(self, node: AST.Validation) -> str:
|
|
546
|
+
operand = self.visit(node.validation)
|
|
547
|
+
imbalance = f" imbalance {self.visit(node.imbalance)}" if node.imbalance is not None else ""
|
|
548
|
+
error_code = (
|
|
549
|
+
f" errorcode {_handle_literal(node.error_code)}" if node.error_code is not None else ""
|
|
550
|
+
)
|
|
551
|
+
error_level = f" errorlevel {node.error_level}" if node.error_level is not None else ""
|
|
552
|
+
invalid = " invalid" if node.invalid else " all"
|
|
553
|
+
return f"{node.op}({operand}{error_code}{error_level}{imbalance}{invalid})"
|
|
554
|
+
|
|
555
|
+
# ---------------------- Constants and IDs ----------------------
|
|
556
|
+
|
|
557
|
+
def visit_VarID(self, node: AST.VarID) -> str:
|
|
558
|
+
return _format_reserved_word(node.value)
|
|
559
|
+
|
|
560
|
+
def visit_Identifier(self, node: AST.Identifier) -> Any:
|
|
561
|
+
return _format_reserved_word(node.value)
|
|
562
|
+
|
|
563
|
+
def visit_Constant(self, node: AST.Constant) -> str:
|
|
564
|
+
if node.value is None:
|
|
565
|
+
return "null"
|
|
566
|
+
return _handle_literal(node.value)
|
|
567
|
+
|
|
568
|
+
def visit_ParamConstant(self, node: AST.ParamConstant) -> Any:
|
|
569
|
+
if node.value is None:
|
|
570
|
+
return "null"
|
|
571
|
+
return _handle_literal(node.value)
|
|
572
|
+
|
|
573
|
+
def visit_Collection(self, node: AST.Collection) -> str:
|
|
574
|
+
if node.kind == "ValueDomain":
|
|
575
|
+
return node.name
|
|
576
|
+
sep = ", " if len(node.children) > 1 else ""
|
|
577
|
+
return f"{{{sep.join([self.visit(x) for x in node.children])}}}"
|
|
578
|
+
|
|
579
|
+
def visit_Windowing(self, node: AST.Windowing) -> str:
|
|
580
|
+
if (
|
|
581
|
+
node.type_ == "data"
|
|
582
|
+
and node.start == -1
|
|
583
|
+
and node.start_mode == "preceding"
|
|
584
|
+
and node.stop == 0
|
|
585
|
+
and node.stop_mode == "current"
|
|
586
|
+
):
|
|
587
|
+
return ""
|
|
588
|
+
if node.start == -1:
|
|
589
|
+
start = f"unbounded {node.start_mode}"
|
|
590
|
+
elif node.start_mode == "current":
|
|
591
|
+
start = "current data point"
|
|
592
|
+
else:
|
|
593
|
+
start = f"{node.start} {node.start_mode}"
|
|
594
|
+
stop = f"{node.stop} {node.stop_mode}"
|
|
595
|
+
if node.stop_mode == "current":
|
|
596
|
+
stop = "current data point"
|
|
597
|
+
mode = "data points" if node.type_ == "data" else "range"
|
|
598
|
+
return f"{mode} between {start} and {stop}"
|
|
599
|
+
|
|
600
|
+
def visit_OrderBy(self, node: AST.OrderBy) -> str:
|
|
601
|
+
if node.order == "asc":
|
|
602
|
+
return f"{node.component}"
|
|
603
|
+
return f"{node.component} {node.order}"
|
|
604
|
+
|
|
605
|
+
def visit_Comment(self, node: AST.Comment) -> None:
|
|
606
|
+
value = copy.copy(node.value)
|
|
607
|
+
value = value[:-1] if value[-1] == "\n" else value
|
|
608
|
+
self.vtl_script += value
|
vtlengine/AST/ASTTemplate.py
CHANGED
|
@@ -186,8 +186,6 @@ class ASTTemplate(NodeVisitor):
|
|
|
186
186
|
|
|
187
187
|
return node.value
|
|
188
188
|
"""
|
|
189
|
-
if node.value in self.datasets:
|
|
190
|
-
return self.datasets[node.value]
|
|
191
189
|
return node.value
|
|
192
190
|
|
|
193
191
|
def visit_Optional(self, node: AST.Optional) -> AST.AST:
|
|
@@ -329,6 +327,18 @@ class ASTTemplate(NodeVisitor):
|
|
|
329
327
|
self.visit(case.thenOp)
|
|
330
328
|
self.visit(node.elseOp)
|
|
331
329
|
|
|
330
|
+
def visit_CaseObj(self, node: AST.CaseObj) -> Any:
|
|
331
|
+
"""
|
|
332
|
+
CaseObj: (condition, thenOp)
|
|
333
|
+
|
|
334
|
+
Basic usage:
|
|
335
|
+
|
|
336
|
+
self.visit(node.condition)
|
|
337
|
+
self.visit(node.thenOp)
|
|
338
|
+
"""
|
|
339
|
+
self.visit(node.condition)
|
|
340
|
+
self.visit(node.thenOp)
|
|
341
|
+
|
|
332
342
|
def visit_Validation(self, node: AST.Validation) -> Any:
|
|
333
343
|
"""
|
|
334
344
|
Validation: (op, validation, params, inbalance, invalid)
|
|
@@ -505,6 +515,12 @@ class ASTTemplate(NodeVisitor):
|
|
|
505
515
|
for child in node.operands:
|
|
506
516
|
self.visit(child)
|
|
507
517
|
|
|
518
|
+
def visit_ParFunction(self, node: AST.ParFunction) -> None:
|
|
519
|
+
"""
|
|
520
|
+
ParFunction: (operand)
|
|
521
|
+
"""
|
|
522
|
+
self.visit(node.operand)
|
|
523
|
+
|
|
508
524
|
def visit_NoOp(self, node: AST.NoOp) -> None: # pylint: disable=unused-argument
|
|
509
525
|
"""
|
|
510
526
|
NoOp: ()
|
|
@@ -526,3 +542,13 @@ class ASTTemplate(NodeVisitor):
|
|
|
526
542
|
"""
|
|
527
543
|
for param in node.params:
|
|
528
544
|
self.visit(param)
|
|
545
|
+
|
|
546
|
+
def visit_Windowing(self, node: AST.Windowing) -> None:
|
|
547
|
+
"""
|
|
548
|
+
Windowing: (type_, start, start_mode, stop, stop_mode)
|
|
549
|
+
"""
|
|
550
|
+
|
|
551
|
+
def visit_Comment(self, node: AST.Comment) -> None:
|
|
552
|
+
"""
|
|
553
|
+
Comment: (value)
|
|
554
|
+
"""
|