Skip to content

Commit 1b2013a

Browse files
authored
Merge pull request #2943 from mabel-dev/#2942
BLOB -> VARBINARY
2 parents c895fcf + 03a3bec commit 1b2013a

6 files changed

Lines changed: 48 additions & 26 deletions

File tree

opteryx/__version__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# THIS FILE IS AUTOMATICALLY UPDATED DURING THE BUILD PROCESS
22
# DO NOT EDIT THIS FILE DIRECTLY
33

4-
__build__ = 1894
4+
__build__ = 1895
55
__author__ = "@joocer"
6-
__version__ = "0.26.2-beta.1894"
6+
__version__ = "0.26.2-beta.1895"
77

88
# Store the version here so:
99
# 1) we don't load dependencies by storing it in __init__.py

opteryx/functions/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ def sleep(x):
494494
"INT": "INTEGER", # remove 0.27.0
495495
"GET": None, # remove 0.28.0
496496
"SEARCH": None, # remove 0.28.0
497+
"BLOB": "VARBINARY", # remove 0.29.0
498+
"TRY_BLOB": "TRY_VARBINARY", # remove 0.29.0
497499
}
498500

499501
# fmt:off
@@ -526,11 +528,13 @@ def sleep(x):
526528
"DATE": (lambda x: compute.cast(x, pyarrow.date32()), "DATE", 1.0),
527529
"PASSTHRU": (lambda x: x, "VARIANT", 1.0),
528530
"BLOB": (cast_to_blob, "BLOB", 1.0),
531+
"VARBINARY": (cast_to_blob, "BLOB", 1.0),
529532
"TRY_ARRAY": (other_functions.array_cast_safe, "VARIANT", 1.0),
530533
"TRY_TIMESTAMP": (try_cast("TIMESTAMP"), "TIMESTAMP", 1.0),
531534
"TRY_BOOLEAN": (try_cast("BOOLEAN"), "BOOLEAN", 1.0),
532535
"TRY_VARCHAR": (try_cast("VARCHAR"), "VARCHAR", 1.0),
533536
"TRY_BLOB": (try_cast("BLOB"), "BLOB", 1.0),
537+
"TRY_VARBINARY": (try_cast("BLOB"), "BLOB", 1.0),
534538
"TRY_INTEGER": (try_cast("INTEGER"), "INTEGER", 1.0),
535539
"TRY_DECIMAL": (try_cast("DECIMAL"), "DECIMAL", 1.0),
536540
"TRY_DOUBLE": (try_cast("DOUBLE"), "DOUBLE", 1.0),

opteryx/operators/exit_node.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
This node doesn't do any calculations, it is a pure Projection.
1818
"""
1919

20+
import pyarrow
2021
from orso.types import OrsoTypes
2122
from pyarrow import Table
2223

@@ -76,7 +77,6 @@ def execute(self, morsel: Table, **kwargs) -> Table:
7677
# Exit doesn't return EOS
7778
if morsel == EOS:
7879
if not self.at_least_one:
79-
import pyarrow
8080
from orso.schema import RelationSchema
8181
from orso.schema import convert_orso_schema_to_arrow_schema
8282

@@ -119,9 +119,22 @@ def execute(self, morsel: Table, **kwargs) -> Table:
119119
morsel = morsel.select(self.final_columns)
120120

121121
for index, column in enumerate(self.columns):
122-
if column.schema_column.type == OrsoTypes.INTERVAL:
123-
converted = to_arrow_interval(morsel.column(index))
124-
morsel = morsel.set_column(index, column.schema_column.identity, converted)
122+
column_array = morsel.column(index)
123+
column_identity = column.schema_column.identity
124+
column_type = column.schema_column.type
125+
126+
if column_type == OrsoTypes.INTERVAL:
127+
converted = to_arrow_interval(column_array)
128+
morsel = morsel.set_column(index, column_identity, converted)
129+
continue
130+
131+
if column_type == OrsoTypes.VARCHAR and (
132+
pyarrow.types.is_binary(column_array.type)
133+
or pyarrow.types.is_large_binary(column_array.type)
134+
or pyarrow.types.is_fixed_size_binary(column_array.type)
135+
):
136+
converted = column_array.cast(pyarrow.string())
137+
morsel = morsel.set_column(index, column_identity, converted)
125138

126139
morsel = morsel.rename_columns(self.final_names)
127140

opteryx/planner/logical_planner/logical_planner_builders.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,30 +199,34 @@ def cast(branch, alias: Optional[List[str]] = None, key=None):
199199
data_type = type_key
200200
if "Custom" in data_type:
201201
data_type = branch["data_type"]["Custom"][0][0]["Identifier"]["value"].upper()
202-
if data_type == "Timestamp":
202+
lower_data_type = data_type.lower()
203+
upper_data_type = data_type.upper()
204+
if lower_data_type == "timestamp":
203205
data_type = "TIMESTAMP"
204-
elif data_type == "Date":
206+
elif lower_data_type == "date":
205207
data_type = "DATE"
206-
elif "Varchar" in data_type:
208+
elif "varchar" in lower_data_type:
207209
data_type = "VARCHAR"
208-
elif "Decimal" in data_type:
210+
elif "decimal" in lower_data_type:
209211
data_type = "DECIMAL"
210212
if "PrecisionAndScale" in branch["data_type"]["Decimal"]:
211213
precision = branch["data_type"]["Decimal"]["PrecisionAndScale"][0]
212214
scale = branch["data_type"]["Decimal"]["PrecisionAndScale"][1]
213215
args.append(build_literal_node(precision))
214216
args.append(build_literal_node(scale))
215-
elif "Integer" in data_type:
217+
elif "integer" in lower_data_type:
216218
data_type = "INTEGER"
217-
elif "Double" in data_type:
219+
elif "double" in lower_data_type:
218220
data_type = "DOUBLE"
219-
elif "Boolean" in data_type:
221+
elif "boolean" in lower_data_type:
220222
data_type = "BOOLEAN"
221-
elif "STRUCT" in data_type:
223+
elif "struct" in lower_data_type:
222224
data_type = "STRUCT"
223-
elif "Blob" in data_type:
225+
elif "blob" in lower_data_type:
224226
data_type = "BLOB"
225-
elif "Array" in data_type:
227+
elif any(token in lower_data_type for token in ("varbinary", "binary", "raw")):
228+
data_type = "VARBINARY"
229+
elif "array" in lower_data_type:
226230
element_key = branch["data_type"]["Array"].get("AngleBracket", {"Varchar": None})
227231
if isinstance(element_key, dict):
228232
element_key = next(iter(element_key))
@@ -231,23 +235,19 @@ def cast(branch, alias: Optional[List[str]] = None, key=None):
231235
args.append(element_key)
232236
data_type = "ARRAY"
233237
else:
234-
if data_type in ("String", "Char", "Text", "Nvarchar"):
238+
if upper_data_type in ("STRING", "CHAR", "TEXT", "NVARCHAR"):
235239
raise SqlError(
236240
f"Unsupported type for CAST - '{data_type.upper()}'. Did you mean 'VARCHAR'?"
237241
)
238-
if data_type in ("Float", "Numeric", "Real"):
242+
if upper_data_type in ("FLOAT", "NUMERIC", "REAL"):
239243
raise SqlError(
240244
f"Unsupported type for CAST - '{data_type.upper()}'. Did you mean 'DOUBLE'?"
241245
)
242-
if data_type in ("Binary", "Raw", "VarBinary"):
243-
raise SqlError(
244-
f"Unsupported type for CAST - '{data_type.upper()}'. Did you mean 'BLOB'?"
245-
)
246-
if data_type in ("Int", "SmallInt", "TinyInt", "BigInt", "BYTE"):
246+
if upper_data_type in ("INT", "SMALLINT", "TINYINT", "BIGINT", "BYTE"):
247247
raise SqlError(
248248
f"Unsupported type for CAST - '{data_type.upper()}'. Did you mean 'INTEGER'?"
249249
)
250-
if data_type in ("Bool", "Bit"):
250+
if upper_data_type in ("BOOL", "BIT"):
251251
raise SqlError(
252252
f"Unsupported type for CAST - '{data_type.upper()}'. Did you mean 'BOOLEAN'?"
253253
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "opteryx"
3-
version = "0.26.2-beta.1894"
3+
version = "0.26.2-beta.1895"
44
description = "Query your data, where it lives"
55
requires-python = '>=3.11'
66
readme = {file = "README.md", content-type = "text/markdown"}

tests/integration/sql_battery/test_casts_battery.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@
9797
("SELECT CAST(CAST(CAST('456' AS INTEGER) AS DOUBLE) AS VARCHAR)", 1, 1, None),
9898
("SELECT CAST(CAST(TRUE AS INTEGER) AS VARCHAR)", 1, 1, None),
9999

100-
# BLOB/BINARY casts
100+
# VARBINARY casts
101101
("SELECT CAST('test' AS BLOB)", 1, 1, None),
102102
("SELECT CAST(CAST('test' AS BLOB) AS VARCHAR)", 1, 1, None),
103+
("SELECT CAST('test' AS VARBINARY)", 1, 1, None),
104+
("SELECT VARBINARY('test')", 1, 1, None),
105+
("SELECT TRY_VARBINARY('test')", 1, 1, None),
106+
("SELECT 'test'::VARBINARY", 1, 1, None),
107+
("SELECT CAST(CAST('test' AS VARBINARY) AS VARCHAR)", 1, 1, None),
103108

104109
]
105110
# fmt:on

0 commit comments

Comments
 (0)