This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcompiler.py
More file actions
239 lines (200 loc) · 8.49 KB
/
compiler.py
File metadata and controls
239 lines (200 loc) · 8.49 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
229
230
231
232
233
234
235
236
237
238
239
# Copyright 2026 Google LLC
#
# Licensed 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.
from __future__ import annotations
import dataclasses
import functools
from typing import Type, TYPE_CHECKING
import pandas as pd
import pyarrow as pa
import bigframes.core
from bigframes.core import agg_expressions, nodes
import bigframes.core.expression as ex
import bigframes.dtypes
import bigframes.operations as ops
datafusion_installed = True
if TYPE_CHECKING:
import datafusion
else:
try:
import bigframes._importing
datafusion = bigframes._importing.import_datafusion()
except Exception:
datafusion_installed = False
def register_op(op: Type):
"""Register a compilation from BigFrames to DataFusion.
This decorator can be used, even if DataFusion is not installed.
Args:
op: The type of the operator the wrapped function compiles.
"""
def decorator(func):
if datafusion_installed:
return DataFusionExpressionCompiler.compile_op.register(op)(func) # type: ignore
else:
return func
return decorator
if datafusion_installed:
_DTYPE_MAPPING = {
bigframes.dtypes.INT_DTYPE: pa.int64(),
bigframes.dtypes.FLOAT_DTYPE: pa.float64(),
bigframes.dtypes.BOOL_DTYPE: pa.bool_(),
bigframes.dtypes.STRING_DTYPE: pa.string(),
# For now, map numeric to double or decimal if supported
bigframes.dtypes.NUMERIC_DTYPE: pa.decimal128(38, 9),
bigframes.dtypes.BIGNUMERIC_DTYPE: pa.decimal256(76, 38),
bigframes.dtypes.BYTES_DTYPE: pa.binary(),
bigframes.dtypes.DATE_DTYPE: pa.date32(),
bigframes.dtypes.DATETIME_DTYPE: pa.timestamp("us"),
bigframes.dtypes.TIMESTAMP_DTYPE: pa.timestamp("us", tz="UTC"),
bigframes.dtypes.TIME_DTYPE: pa.time64("us"),
bigframes.dtypes.TIMEDELTA_DTYPE: pa.duration("us"),
bigframes.dtypes.GEO_DTYPE: pa.string(),
bigframes.dtypes.JSON_DTYPE: pa.string(),
}
def _bigframes_dtype_to_arrow_dtype(
dtype: bigframes.dtypes.ExpressionType,
) -> pa.DataType:
if dtype is None:
return pa.null()
# TODO: Add struct and array handling if needed
return _DTYPE_MAPPING[dtype]
@dataclasses.dataclass(frozen=True)
class DataFusionExpressionCompiler:
"""
Compiler for converting bigframes expressions to datafusion expressions.
"""
@functools.singledispatchmethod
def compile_expression(self, expression: ex.Expression) -> datafusion.Expr:
raise NotImplementedError(f"Cannot compile expression: {expression}")
@compile_expression.register
def _(
self,
expression: ex.ScalarConstantExpression,
) -> datafusion.Expr:
value = expression.value
if not isinstance(value, float) and pd.isna(value): # type: ignore
value = None
if expression.dtype is None:
return datafusion.lit(None)
# DataFusion lit handles standard types
return datafusion.lit(value)
@compile_expression.register
def _(
self,
expression: ex.DerefOp,
) -> datafusion.Expr:
return datafusion.col(expression.id.sql)
@compile_expression.register
def _(
self,
expression: ex.ResolvedDerefOp,
) -> datafusion.Expr:
return datafusion.col(expression.id.sql)
@compile_expression.register
def _(
self,
expression: ex.OpExpression,
) -> datafusion.Expr:
op = expression.op
args = tuple(map(self.compile_expression, expression.inputs))
return self.compile_op(op, *args)
@functools.singledispatchmethod
def compile_op(
self, op: ops.ScalarOp, *args: datafusion.Expr
) -> datafusion.Expr:
raise NotImplementedError(f"DataFusion compiler hasn't implemented {op}")
# Add basic ops here, others via register_op
# df expressions overload operators usually
@dataclasses.dataclass(frozen=True)
class DataFusionAggregateCompiler:
scalar_compiler = DataFusionExpressionCompiler()
def compile_agg_expr(self, expr: agg_expressions.Aggregation):
# Skeleton for now
raise NotImplementedError("Aggregate compilation not implemented")
@dataclasses.dataclass(frozen=True)
class DataFusionCompiler:
"""
Compiles BigFrameNode to DataFusion DataFrame.
"""
expr_compiler = DataFusionExpressionCompiler()
agg_compiler = DataFusionAggregateCompiler()
def compile(self, plan: nodes.BigFrameNode) -> datafusion.DataFrame:
if not datafusion_installed:
raise ValueError(
"DataFusion is not installed, cannot compile to datafusion engine."
)
from bigframes.core.compile.datafusion import lowering
node = lowering.lower_ops_to_datafusion(plan)
return self.compile_node(node)
@functools.singledispatchmethod
def compile_node(self, node: nodes.BigFrameNode) -> datafusion.DataFrame:
raise ValueError(f"Can't compile unrecognized node: {node}")
@compile_node.register
def compile_readlocal(self, node: nodes.ReadLocalNode):
# Need SessionContext, maybe pass it in or create one
ctx = datafusion.SessionContext()
df = ctx.from_arrow(node.local_data_source.data)
cols_to_read = {
scan_item.source_id: scan_item.id.sql
for scan_item in node.scan_list.items
}
# Rename columns
# DataFusion select can take list of expressions
exprs = [
datafusion.col(orig).alias(new) for orig, new in cols_to_read.items()
]
df = df.select(*exprs)
if node.offsets_col:
# DataFusion has row_number()?
# But ReadLocalNode usually has small data, could just use arrow offsets if needed
# For now, let's just make it error if offsets_col is requested and see
raise NotImplementedError(
"offsets_col in ReadLocalNode not supported yet for DataFusion"
)
return df
@compile_node.register
def compile_filter(self, node: nodes.FilterNode):
return self.compile_node(node.child).filter(
self.expr_compiler.compile_expression(node.predicate)
)
@compile_node.register
def compile_selection(self, node: nodes.SelectionNode):
df = self.compile_node(node.child)
exprs = [
datafusion.col(orig.id.sql).alias(new.sql)
for orig, new in node.input_output_pairs
]
return df.select(*exprs)
@compile_node.register
def compile_projection(self, node: nodes.ProjectionNode):
df = self.compile_node(node.child)
new_cols = []
for proj_expr, name in node.assignments:
# bind_schema_fields might be needed
bound_expr = ex.bind_schema_fields(proj_expr, node.child.field_by_id)
new_col = self.expr_compiler.compile_expression(bound_expr).alias(
name.sql
)
new_cols.append(new_col)
# with_columns takes dict or list of aliases?
# DF DataFrame has with_column
for col in new_cols:
# df = df.with_column(col) # wait, with_column usually takes name and expr
# let's see df.select(*existing, new)
pass
# Better to use select with existing columns + new columns
new_names = [name.sql for _, name in node.assignments]
filtered_existing = [
datafusion.col(c) for c in df.schema().names if c not in new_names
]
return df.select(*(filtered_existing + new_cols))