-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtest_if_then.py
More file actions
228 lines (215 loc) · 8.81 KB
/
test_if_then.py
File metadata and controls
228 lines (215 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import substrait.gen.proto.algebra_pb2 as stalg
import substrait.gen.proto.type_pb2 as stt
import substrait.gen.proto.extended_expression_pb2 as stee
import substrait.gen.proto.extensions.extensions_pb2 as ste
from substrait.builders.extended_expression import (
if_then,
literal,
scalar_function,
column,
)
from substrait.extension_registry import ExtensionRegistry
struct = stt.Type.Struct(
types=[
stt.Type(i64=stt.Type.I64(nullability=stt.Type.NULLABILITY_REQUIRED)),
stt.Type(string=stt.Type.String(nullability=stt.Type.NULLABILITY_NULLABLE)),
stt.Type(fp32=stt.Type.FP32(nullability=stt.Type.NULLABILITY_NULLABLE)),
]
)
named_struct = stt.NamedStruct(
names=["order_id", "description", "order_total"], struct=struct
)
def test_if_else():
actual = if_then(
ifs=[
(
literal(
True,
type=stt.Type(
bool=stt.Type.Boolean(nullability=stt.Type.NULLABILITY_REQUIRED)
),
),
literal(
10,
type=stt.Type(
i8=stt.Type.I8(nullability=stt.Type.NULLABILITY_REQUIRED)
),
),
)
],
_else=literal(
20, type=stt.Type(i8=stt.Type.I8(nullability=stt.Type.NULLABILITY_REQUIRED))
),
)(named_struct, None)
expected = stee.ExtendedExpression(
referred_expr=[
stee.ExpressionReference(
expression=stalg.Expression(
if_then=stalg.Expression.IfThen(
**{
"ifs": [
stalg.Expression.IfThen.IfClause(
**{
"if": stalg.Expression(
literal=stalg.Expression.Literal(
boolean=True, nullable=False
)
),
"then": stalg.Expression(
literal=stalg.Expression.Literal(
i8=10, nullable=False
)
),
}
)
],
"else": stalg.Expression(
literal=stalg.Expression.Literal(i8=20, nullable=False)
),
}
)
),
output_names=["IfThen(Literal(True),Literal(10),Literal(20))"],
)
],
base_schema=named_struct,
)
assert actual == expected
def test_if_then_with_extension():
import yaml
registry = ExtensionRegistry(load_default_extensions=False)
content = """%YAML 1.2
---
urn: extension:io.substrait:functions_comparison
scalar_functions:
- name: "gt"
description: ""
impls:
- args:
- value: fp32
- value: fp32
return: boolean
"""
registry.register_extension_dict(
yaml.safe_load(content),
uri="https://github.com/substrait-io/substrait/blob/main/extensions/functions_comparison.yaml",
)
# Create if_then: if order_total > 100 then "expensive" else "cheap"
actual = if_then(
ifs=[
(
scalar_function(
"extension:io.substrait:functions_comparison:gt",
expressions=[
column("order_total"),
literal(
100.0,
type=stt.Type(
fp32=stt.Type.FP32(
nullability=stt.Type.NULLABILITY_REQUIRED
)
),
),
],
),
literal(
"expensive",
type=stt.Type(
string=stt.Type.String(
nullability=stt.Type.NULLABILITY_REQUIRED
)
),
),
)
],
_else=literal(
"cheap",
type=stt.Type(
string=stt.Type.String(nullability=stt.Type.NULLABILITY_REQUIRED)
),
),
)(named_struct, registry)
expected = stee.ExtendedExpression(
extension_uris=[
ste.SimpleExtensionURI(
extension_uri_anchor=1,
uri="https://github.com/substrait-io/substrait/blob/main/extensions/functions_comparison.yaml",
)
],
extension_urns=[
ste.SimpleExtensionURN(
extension_urn_anchor=1,
urn="extension:io.substrait:functions_comparison",
)
],
extensions=[
ste.SimpleExtensionDeclaration(
extension_function=ste.SimpleExtensionDeclaration.ExtensionFunction(
extension_uri_reference=1,
extension_urn_reference=1,
function_anchor=1,
name="gt:fp32_fp32",
)
)
],
referred_expr=[
stee.ExpressionReference(
expression=stalg.Expression(
if_then=stalg.Expression.IfThen(
**{
"ifs": [
stalg.Expression.IfThen.IfClause(
**{
"if": stalg.Expression(
scalar_function=stalg.Expression.ScalarFunction(
function_reference=1,
output_type=stt.Type(
bool=stt.Type.Boolean(
nullability=stt.Type.NULLABILITY_NULLABLE
)
),
arguments=[
stalg.FunctionArgument(
value=stalg.Expression(
selection=stalg.Expression.FieldReference(
direct_reference=stalg.Expression.ReferenceSegment(
struct_field=stalg.Expression.ReferenceSegment.StructField(
field=2
)
),
root_reference=stalg.Expression.FieldReference.RootReference(),
)
)
),
stalg.FunctionArgument(
value=stalg.Expression(
literal=stalg.Expression.Literal(
fp32=100.0
)
)
),
],
)
),
"then": stalg.Expression(
literal=stalg.Expression.Literal(
string="expensive"
)
),
}
)
],
"else": stalg.Expression(
literal=stalg.Expression.Literal(string="cheap")
),
}
)
),
output_names=[
"IfThen(gt(order_total,Literal(100.0)),Literal(expensive),Literal(cheap))"
],
)
],
base_schema=named_struct,
)
assert actual == expected