SQL conversion#
This page answers common “how do I…” questions for converting SQL and
polars lazy plans to ONNX with yobx.sql.to_onnx().
How to convert a SQL query string#
Pass the SQL query and the input dtypes to yobx.sql.to_onnx().
<<<
import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import to_onnx
dtypes = {"a": np.float32, "b": np.float32}
onx = to_onnx("SELECT a + b AS total FROM t WHERE a > 0", dtypes)
print(pretty_onnx(onx))
>>>
opset: domain='' version=29
input: name='a' type=dtype('float32') shape=['N']
input: name='b' type=dtype('float32') shape=['N']
init: name='filter_mask_r_lit' type=int64 shape=(1,) -- array([0])
CastLike(filter_mask_r_lit, a) -> castlike
Greater(a, castlike) -> greater
Compress(a, greater, axis=0) -> compress
Compress(b, greater, axis=0) -> compress_2
Add(compress, compress_2) -> total
output: name='total' type=dtype('float32') shape=['broadcast(Compress_compress_count, Compress_compress_2_count)']
How to convert a SQL WHERE query#
Use the WHERE clause directly in the SQL query passed to
yobx.sql.to_onnx().
<<<
import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import to_onnx
dtypes = {"a": np.float32, "b": np.float32}
query = "SELECT a, b FROM t WHERE b >= 5"
onx = to_onnx(query, dtypes)
print(pretty_onnx(onx))
>>>
opset: domain='' version=29
input: name='b' type=dtype('float32') shape=['N']
input: name='a' type=dtype('float32') shape=['N']
init: name='filter_mask_r_lit' type=int64 shape=(1,) -- array([5])
CastLike(filter_mask_r_lit, b) -> castlike
GreaterOrEqual(b, castlike) -> greaterorequal
Compress(b, greaterorequal, axis=0) -> output_1
Compress(a, greaterorequal, axis=0) -> output_0
output: name='output_0' type=dtype('float32') shape=['Compress_compress_2_count']
output: name='output_1' type=dtype('float32') shape=['Compress_compress_count']
How to convert a SQL JOIN query#
Use yobx.sql.sql_to_onnx() and pass the right-table dtypes through
right_input_dtypes.
<<<
import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import sql_to_onnx
query = "SELECT a.x, b.y FROM a JOIN b ON a.id = b.rid"
left_dtypes = {"id": np.int64, "x": np.float32}
right_dtypes = {"rid": np.int64, "y": np.float32}
onx = sql_to_onnx(query, left_dtypes, right_input_dtypes=right_dtypes)
print(pretty_onnx(onx))
>>>
opset: domain='' version=29
input: name='id' type=dtype('int64') shape=['N']
input: name='x' type=dtype('float32') shape=['N']
input: name='rid' type=dtype('int64') shape=['N']
input: name='y' type=dtype('float32') shape=['N']
init: name='init' type=int64 shape=(1,) -- array([1])
init: name='init_2' type=int64 shape=(1,) -- array([0])
init: name='ReduceArgTopKPattern.K' type=int64 shape=(1,) -- array([1])
init: name='ReduceArgTopKPattern.axes' type=int64 shape=(1,) -- array([1])
Unsqueeze(id, init) -> unsqueeze
Unsqueeze(rid, init_2) -> unsqueeze_2
Equal(unsqueeze, unsqueeze_2) -> equal
Cast(equal, to=6) -> cast
TopK(cast, ReduceArgTopKPattern.K, axis=1, largest=1) -> ReduceArgTopKPattern_reducemax_0, ReduceArgTopKPattern_argmax_1
Squeeze(ReduceArgTopKPattern_reducemax_0, ReduceArgTopKPattern.axes) -> reducemax
Cast(reducemax, to=9) -> cast_2
Compress(x, cast_2, axis=0) -> output_0
Squeeze(ReduceArgTopKPattern_argmax_1, ReduceArgTopKPattern.axes) -> argmax
Compress(argmax, cast_2, axis=0) -> compress_3
Gather(y, compress_3, axis=0) -> output_1
output: name='output_0' type=dtype('float32') shape=['Compress_compress_2_count']
output: name='output_1' type=dtype('float32') shape=['Compress_compress_3_count']
How to convert a SQL GROUP BY query#
Use aggregate functions with GROUP BY in the SQL query.
<<<
import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import to_onnx
dtypes = {"city": np.str_, "sales": np.float32}
query = "SELECT city, SUM(sales) AS total_sales FROM t GROUP BY city"
onx = to_onnx(query, dtypes)
print(pretty_onnx(onx))
>>>
opset: domain='' version=29
input: name='city' type=dtype('O') shape=['N']
input: name='sales' type=dtype('float32') shape=['N']
Unique(city, sorted=1) -> unique, unique_2, unique_3, unique_4
Shape(unique, start=0, end=1) -> GatherShapePattern--gb_n_groups_1d_0
ConstantOfShape(GatherShapePattern--gb_n_groups_1d_0) -> constantofshape
CastLike(constantofshape, sales) -> castlike
ScatterElements(castlike, unique_3, sales, reduction=b'add', axis=0) -> total_sales
Identity(unique) -> output_0
output: name='output_0' type=dtype('O') shape=['Unique_unique_n']
output: name='total_sales' type=dtype('float32') shape=['Unique_unique_n']
How to convert a polars LazyFrame#
The same yobx.sql.to_onnx() entrypoint also accepts a
polars.LazyFrame.
<<<
import numpy as np
import polars as pl
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.sql import to_onnx
lf = pl.LazyFrame({"a": [1.0, -2.0, 3.0], "b": [4.0, 5.0, 6.0]}).select(
[(pl.col("a") + pl.col("b")).alias("total")]
)
dtypes = {"a": np.float64, "b": np.float64}
onx = to_onnx(lf, dtypes)
print(pretty_onnx(onx))
>>>
opset: domain='' version=29
input: name='a' type=dtype('float64') shape=['N']
input: name='b' type=dtype('float64') shape=['N']
Add(a, b) -> total
output: name='total' type=dtype('float64') shape=['N']
See also
SQL-to-ONNX Converter — SQL converter design details.
Polars LazyFrame to ONNX — polars LazyFrame conversion design.