yobx.sql.sql_to_onnx_graph#

yobx.sql.sql_to_onnx_graph(g: GraphBuilderProtocol, sts: Dict | None, outputs: List[str], query: str, input_dtypes: Dict[str, dtype | type | str], right_input_dtypes: Dict[str, dtype | type | str] | None = None, n_rows: int | None = None) List[str][source]#

Build ONNX nodes for a SQL query into an existing graph builder g.

This is the low-level entry point for callers that are already managing a GraphBuilderProtocol instance (e.g. as part of a larger model). The signature follows the standard SQL op converter convention (g, sts, outputs, ...). Any column referenced in the query that is not yet registered as an input in g is added automatically. The SELECT expressions are emitted as model outputs and their tensor names are returned.

Parameters:
  • g – an existing graph builder that satisfies GraphBuilderProtocol. Nodes and inputs are added to this builder in-place.

  • sts

    shape/type context dict forwarded to SQL op converters, or None. May contain:

    • "custom_functions" — a mapping from function name (as it appears in the SQL string) to a Python callable. Each callable must accept one or more numpy arrays and return a numpy array. The function body is traced with trace_numpy_function() so that numpy arithmetic is translated into ONNX nodes.

    When None (or an empty dict) no custom functions are available.

  • outputs – expected output column names for the query result. Passed through to individual op converters following the standard converter convention; may be an empty list when the caller does not know the output names in advance.

  • query – a SQL string. Supported clauses: SELECT, FROM, [INNER|LEFT|RIGHT|FULL] JOIN ON, WHERE, GROUP BY.

  • input_dtypes – a mapping from left-table column name to numpy dtype (np.float32, np.int64, etc.). Only columns actually referenced in the query need to be listed.

  • right_input_dtypes – if the query contains a JOIN, a mapping from right-table column name to numpy dtype. Defaults to input_dtypes when None.

  • n_rows – optional static number of rows; used to fix the first dimension of every input tensor that is newly added to g. When None the first dimension is symbolic ("N").

Returns:

a list of output tensor names that were added to g as model outputs (one per expression in the SELECT clause, in order).

Example:

<<<

import numpy as np
from yobx.helpers.onnx_helper import pretty_onnx
from yobx.xbuilder import GraphBuilder
from yobx.sql import sql_to_onnx_graph

g = GraphBuilder(18, ir_version=10)
dtypes = {"a": np.float32, "b": np.float32}
out_names = sql_to_onnx_graph(
    g,
    None,
    [],
    "SELECT a + b AS total FROM t WHERE a > 0",
    dtypes,
)
art = g.to_onnx()
print(pretty_onnx(art))

>>>

    opset: domain='' version=18
    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) -> _onx_castlike_filter_mask_r_lit
      Greater(a, _onx_castlike_filter_mask_r_lit) -> _onx_greater_a
        Compress(a, _onx_greater_a, axis=0) -> _onx_compress_a
    Compress(b, _onx_greater_a, axis=0) -> _onx_compress_b
      Add(_onx_compress_a, _onx_compress_b) -> total
    output: name='total' type='NOTENSOR' shape=None