Skip to content

Commit e452203

Browse files
refactor: Move ibis type mappings to compile module (#745)
1 parent 93353c3 commit e452203

13 files changed

+645
-522
lines changed

bigframes/core/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ArrayValue:
6060

6161
node: nodes.BigFrameNode
6262

63+
# DO NOT use, on deprecation path
6364
@classmethod
6465
def from_ibis(
6566
cls,
@@ -69,11 +70,13 @@ def from_ibis(
6970
hidden_ordering_columns: Sequence[ibis_types.Value],
7071
ordering: orderings.ExpressionOrdering,
7172
):
73+
import bigframes.core.compile.ibis_types
74+
7275
node = nodes.ReadGbqNode(
7376
table=table,
7477
table_session=session,
7578
columns=tuple(
76-
bigframes.dtypes.ibis_value_to_canonical_type(column)
79+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(column)
7780
for column in columns
7881
),
7982
hidden_ordering_columns=tuple(hidden_ordering_columns),

bigframes/core/compile/aggregate_compiler.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import pandas as pd
2323

2424
import bigframes.constants as constants
25+
import bigframes.core.compile.ibis_types as compile_ibis_types
2526
import bigframes.core.compile.scalar_op_compiler as scalar_compilers
2627
import bigframes.core.expression as ex
2728
import bigframes.core.window_spec as window_spec
28-
import bigframes.dtypes as dtypes
2929
import bigframes.operations.aggregations as agg_ops
3030

3131
scalar_compiler = scalar_compilers.scalar_op_compiler
@@ -323,7 +323,7 @@ def _(
323323
for this_bin in range(op.bins - 1):
324324
out = out.when(
325325
x <= (col_min + (this_bin + 1) * bin_width),
326-
dtypes.literal_to_ibis_scalar(
326+
compile_ibis_types.literal_to_ibis_scalar(
327327
this_bin, force_dtype=pd.Int64Dtype()
328328
),
329329
)
@@ -352,8 +352,8 @@ def _(
352352
out = out.when(x.notnull(), interval_struct)
353353
else: # Interpret as intervals
354354
for interval in op.bins:
355-
left = dtypes.literal_to_ibis_scalar(interval[0])
356-
right = dtypes.literal_to_ibis_scalar(interval[1])
355+
left = compile_ibis_types.literal_to_ibis_scalar(interval[0])
356+
right = compile_ibis_types.literal_to_ibis_scalar(interval[1])
357357
condition = (x > left) & (x <= right)
358358
interval_struct = ibis.struct(
359359
{"left_exclusive": left, "right_inclusive": right}
@@ -370,7 +370,7 @@ def _(
370370
window=None,
371371
) -> ibis_types.IntegerValue:
372372
if isinstance(self.quantiles, int):
373-
quantiles_ibis = dtypes.literal_to_ibis_scalar(self.quantiles)
373+
quantiles_ibis = compile_ibis_types.literal_to_ibis_scalar(self.quantiles)
374374
percent_ranks = cast(
375375
ibis_types.FloatingColumn,
376376
_apply_window_if_present(column.percent_rank(), window),
@@ -383,13 +383,19 @@ def _(
383383
_apply_window_if_present(column.percent_rank(), window),
384384
)
385385
out = ibis.case()
386-
first_ibis_quantile = dtypes.literal_to_ibis_scalar(self.quantiles[0])
386+
first_ibis_quantile = compile_ibis_types.literal_to_ibis_scalar(
387+
self.quantiles[0]
388+
)
387389
out = out.when(percent_ranks < first_ibis_quantile, None)
388390
for bucket_n in range(len(self.quantiles) - 1):
389-
ibis_quantile = dtypes.literal_to_ibis_scalar(self.quantiles[bucket_n + 1])
391+
ibis_quantile = compile_ibis_types.literal_to_ibis_scalar(
392+
self.quantiles[bucket_n + 1]
393+
)
390394
out = out.when(
391395
percent_ranks <= ibis_quantile,
392-
dtypes.literal_to_ibis_scalar(bucket_n, force_dtype=pd.Int64Dtype()),
396+
compile_ibis_types.literal_to_ibis_scalar(
397+
bucket_n, force_dtype=pd.Int64Dtype()
398+
),
393399
)
394400
out = out.else_(None)
395401
return out.end() # type: ignore

bigframes/core/compile/compiled.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import pandas
2929

3030
import bigframes.core.compile.aggregate_compiler as agg_compiler
31+
import bigframes.core.compile.ibis_types
3132
import bigframes.core.compile.scalar_op_compiler as op_compilers
3233
import bigframes.core.expression as ex
3334
import bigframes.core.guid
@@ -157,16 +158,19 @@ def _get_ibis_column(self, key: str) -> ibis_types.Value:
157158
)
158159
return typing.cast(
159160
ibis_types.Value,
160-
bigframes.dtypes.ibis_value_to_canonical_type(self._column_names[key]),
161+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(
162+
self._column_names[key]
163+
),
161164
)
162165

163166
def get_column_type(self, key: str) -> bigframes.dtypes.Dtype:
164167
ibis_type = typing.cast(
165-
bigframes.dtypes.IbisDtype, self._get_ibis_column(key).type()
168+
bigframes.core.compile.ibis_types.IbisDtype,
169+
self._get_ibis_column(key).type(),
166170
)
167171
return typing.cast(
168172
bigframes.dtypes.Dtype,
169-
bigframes.dtypes.ibis_dtype_to_bigframes_dtype(ibis_type),
173+
bigframes.core.compile.ibis_types.ibis_dtype_to_bigframes_dtype(ibis_type),
170174
)
171175

172176
def _aggregate_base(
@@ -332,7 +336,8 @@ def _to_ibis_expr(
332336
# Make sure all dtypes are the "canonical" ones for BigFrames. This is
333337
# important for operations like UNION where the schema must match.
334338
table = self._table.select(
335-
bigframes.dtypes.ibis_value_to_canonical_type(column) for column in columns
339+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(column)
340+
for column in columns
336341
)
337342
base_table = table
338343
if self._reduced_predicate is not None:
@@ -579,7 +584,10 @@ def from_pandas(
579584
ibis_values = ibis_values.assign(**{ORDER_ID_COLUMN: range(len(pd_df))})
580585
# derive the ibis schema from the original pandas schema
581586
ibis_schema = [
582-
(name, bigframes.dtypes.bigframes_dtype_to_ibis_dtype(dtype))
587+
(
588+
name,
589+
bigframes.core.compile.ibis_types.bigframes_dtype_to_ibis_dtype(dtype),
590+
)
583591
for name, dtype in zip(schema.names, schema.dtypes)
584592
]
585593
ibis_schema.append((ORDER_ID_COLUMN, ibis_dtypes.int64))
@@ -993,7 +1001,9 @@ def _to_ibis_expr(
9931001
# Make sure all dtypes are the "canonical" ones for BigFrames. This is
9941002
# important for operations like UNION where the schema must match.
9951003
table = table.select(
996-
bigframes.dtypes.ibis_value_to_canonical_type(table[column])
1004+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(
1005+
table[column]
1006+
)
9971007
for column in table.columns
9981008
)
9991009
base_table = table

bigframes/core/compile/compiler.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
import bigframes.core.compile.compiled as compiled
2727
import bigframes.core.compile.concat as concat_impl
2828
import bigframes.core.compile.default_ordering as default_ordering
29+
import bigframes.core.compile.ibis_types
2930
import bigframes.core.compile.schema_translator
3031
import bigframes.core.compile.single_column
3132
import bigframes.core.nodes as nodes
3233
import bigframes.core.ordering as bf_ordering
33-
import bigframes.dtypes as bigframes_dtypes
3434

3535
if typing.TYPE_CHECKING:
3636
import bigframes.core
@@ -112,7 +112,9 @@ def compile_cached_table(node: nodes.CachedTableNode, ordered: bool = True):
112112
return compiled.OrderedIR(
113113
ibis_table,
114114
columns=tuple(
115-
bigframes_dtypes.ibis_value_to_canonical_type(ibis_table[col])
115+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(
116+
ibis_table[col]
117+
)
116118
for col in node.schema.names
117119
),
118120
ordering=node.ordering,
@@ -123,7 +125,9 @@ def compile_cached_table(node: nodes.CachedTableNode, ordered: bool = True):
123125
return compiled.UnorderedIR(
124126
ibis_table,
125127
columns=tuple(
126-
bigframes_dtypes.ibis_value_to_canonical_type(ibis_table[col])
128+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(
129+
ibis_table[col]
130+
)
127131
for col in node.schema.names
128132
),
129133
)
@@ -166,7 +170,9 @@ def compile_read_table_unordered(node: nodes.ReadTableNode):
166170
return compiled.UnorderedIR(
167171
ibis_table,
168172
tuple(
169-
bigframes_dtypes.ibis_value_to_canonical_type(ibis_table[col])
173+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(
174+
ibis_table[col]
175+
)
170176
for col in node.schema.names
171177
),
172178
)
@@ -202,7 +208,9 @@ def compile_read_table_ordered(node: nodes.ReadTableNode):
202208
return compiled.OrderedIR(
203209
ibis_table,
204210
columns=tuple(
205-
bigframes_dtypes.ibis_value_to_canonical_type(ibis_table[col])
211+
bigframes.core.compile.ibis_types.ibis_value_to_canonical_type(
212+
ibis_table[col]
213+
)
206214
for col in node.schema.names
207215
),
208216
ordering=ordering,

0 commit comments

Comments
 (0)