-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathtest_substrait.py
More file actions
149 lines (123 loc) · 4.46 KB
/
test_substrait.py
File metadata and controls
149 lines (123 loc) · 4.46 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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pyarrow as pa
import pytest
from datafusion import SessionContext
from datafusion import substrait as ss
@pytest.fixture
def ctx():
return SessionContext()
def test_substrait_serialization(ctx):
batch = pa.RecordBatch.from_arrays(
[pa.array([1, 2, 3]), pa.array([4, 5, 6])],
names=["a", "b"],
)
ctx.register_record_batches("t", [[batch]])
assert ctx.catalog().schema().names() == {"t"}
# For now just make sure the method calls blow up
substrait_plan = ss.Serde.serialize_to_plan("SELECT * FROM t", ctx)
substrait_bytes = substrait_plan.encode()
assert isinstance(substrait_bytes, bytes)
substrait_bytes = ss.Serde.serialize_bytes("SELECT * FROM t", ctx)
substrait_plan = ss.Serde.deserialize_bytes(substrait_bytes)
logical_plan = ss.Consumer.from_substrait_plan(ctx, substrait_plan)
# demonstrate how to create a DataFrame from a deserialized logical plan
df = ctx.create_dataframe_from_logical_plan(logical_plan)
substrait_plan = ss.Producer.to_substrait_plan(df.logical_plan(), ctx)
@pytest.mark.parametrize("path_to_str", [True, False])
def test_substrait_file_serialization(ctx, tmp_path, path_to_str):
batch = pa.RecordBatch.from_arrays(
[pa.array([1, 2, 3]), pa.array([4, 5, 6])],
names=["a", "b"],
)
ctx.register_record_batches("t", [[batch]])
assert ctx.catalog().schema().names() == {"t"}
path = tmp_path / "substrait_plan"
path = str(path) if path_to_str else path
sql_command = "SELECT * FROM T"
ss.Serde.serialize(sql_command, ctx, path)
expected_plan = ss.Serde.serialize_to_plan(sql_command, ctx)
actual_plan = ss.Serde.deserialize(path)
expected_logical_plan = ss.Consumer.from_substrait_plan(ctx, expected_plan)
expected_actual_plan = ss.Consumer.from_substrait_plan(ctx, actual_plan)
assert str(expected_logical_plan) == str(expected_actual_plan)
def test_json_processing_round_trip(ctx: SessionContext):
ctx.register_record_batches("t", [[pa.record_batch({"a": [1]})]])
original_logical_plan = ctx.sql("SELECT * FROM t").logical_plan()
substrait_plan = ss.Producer.to_substrait_plan(original_logical_plan, ctx)
json_plan = substrait_plan.to_json()
expected = """\
"relations": [
{
"root": {
"input": {
"project": {
"common": {
"emit": {
"outputMapping": [
1
]
}
},
"input": {
"read": {
"baseSchema": {
"names": [
"a"
],
"struct": {
"types": [
{
"i64": {
"nullability": "NULLABILITY_NULLABLE"
}
}
],
"nullability": "NULLABILITY_REQUIRED"
}
},
"namedTable": {
"names": [
"t"
]
}
}
},
"expressions": [
{
"selection": {
"directReference": {
"structField": {}
},
"rootReference": {}
}
}
]
}
},
"names": [
"a"
]
}
}
]"""
assert expected in json_plan
round_trip_substrait_plan = ss.Plan.from_json(json_plan)
round_trip_logical_plan = ss.Consumer.from_substrait_plan(
ctx, round_trip_substrait_plan
)
assert round_trip_logical_plan == original_logical_plan