forked from apache/datafusion-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstrait.py
More file actions
193 lines (143 loc) · 5.29 KB
/
substrait.py
File metadata and controls
193 lines (143 loc) · 5.29 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
# 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.
"""This module provides support for using substrait with datafusion.
For additional information about substrait, see https://substrait.io/ for more
information about substrait.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
try:
from warnings import deprecated # Python 3.13+
except ImportError:
from typing_extensions import deprecated # Python 3.12
from datafusion.plan import LogicalPlan
from ._internal import substrait as substrait_internal
if TYPE_CHECKING:
import pathlib
from datafusion.context import SessionContext
__all__ = [
"Consumer",
"Plan",
"Producer",
"Serde",
]
class Plan:
"""A class representing an encodable substrait plan."""
def __init__(self, plan: substrait_internal.Plan) -> None:
"""Create a substrait plan.
The user should not have to call this constructor directly. Rather, it
should be created via :py:class:`Serde` or py:class:`Producer` classes
in this module.
"""
self.plan_internal = plan
def encode(self) -> bytes:
"""Encode the plan to bytes.
Returns:
Encoded plan.
"""
return self.plan_internal.encode()
@deprecated("Use `Plan` instead.")
class plan(Plan): # noqa: N801
"""See `Plan`."""
class Serde:
"""Provides the ``Substrait`` serialization and deserialization."""
@staticmethod
def serialize(sql: str, ctx: SessionContext, path: str | pathlib.Path) -> None:
"""Serialize a SQL query to a Substrait plan and write it to a file.
Args:
sql:SQL query to serialize.
ctx: SessionContext to use.
path: Path to write the Substrait plan to.
"""
return substrait_internal.Serde.serialize(sql, ctx.ctx, str(path))
@staticmethod
def serialize_to_plan(sql: str, ctx: SessionContext) -> Plan:
"""Serialize a SQL query to a Substrait plan.
Args:
sql: SQL query to serialize.
ctx: SessionContext to use.
Returns:
Substrait plan.
"""
return Plan(substrait_internal.Serde.serialize_to_plan(sql, ctx.ctx))
@staticmethod
def serialize_bytes(sql: str, ctx: SessionContext) -> bytes:
"""Serialize a SQL query to a Substrait plan as bytes.
Args:
sql: SQL query to serialize.
ctx: SessionContext to use.
Returns:
Substrait plan as bytes.
"""
return substrait_internal.Serde.serialize_bytes(sql, ctx.ctx)
@staticmethod
def deserialize(path: str | pathlib.Path) -> Plan:
"""Deserialize a Substrait plan from a file.
Args:
path: Path to read the Substrait plan from.
Returns:
Substrait plan.
"""
return Plan(substrait_internal.Serde.deserialize(str(path)))
@staticmethod
def deserialize_bytes(proto_bytes: bytes) -> Plan:
"""Deserialize a Substrait plan from bytes.
Args:
proto_bytes: Bytes to read the Substrait plan from.
Returns:
Substrait plan.
"""
return Plan(substrait_internal.Serde.deserialize_bytes(proto_bytes))
@deprecated("Use `Serde` instead.")
class serde(Serde): # noqa: N801
"""See `Serde` instead."""
class Producer:
"""Generates substrait plans from a logical plan."""
@staticmethod
def to_substrait_plan(logical_plan: LogicalPlan, ctx: SessionContext) -> Plan:
"""Convert a DataFusion LogicalPlan to a Substrait plan.
Args:
logical_plan: LogicalPlan to convert.
ctx: SessionContext to use.
Returns:
Substrait plan.
"""
return Plan(
substrait_internal.Producer.to_substrait_plan(
logical_plan._raw_plan, ctx.ctx
)
)
@deprecated("Use `Producer` instead.")
class producer(Producer): # noqa: N801
"""Use `Producer` instead."""
class Consumer:
"""Generates a logical plan from a substrait plan."""
@staticmethod
def from_substrait_plan(ctx: SessionContext, plan: Plan) -> LogicalPlan:
"""Convert a Substrait plan to a DataFusion LogicalPlan.
Args:
ctx: SessionContext to use.
plan: Substrait plan to convert.
Returns:
LogicalPlan.
"""
return LogicalPlan(
substrait_internal.Consumer.from_substrait_plan(ctx.ctx, plan.plan_internal)
)
@deprecated("Use `Consumer` instead.")
class consumer(Consumer): # noqa: N801
"""Use `Consumer` instead."""