API Reference
orbital
orbital, translate ML pipelines into SQL queries
orbital is a library for translating scikit-learn pipelines and PyTorch feed-forward neural networks into SQL queries and Ibis expressions.
It provides a way to execute machine learning models on databases without the need for a python runtime environment.
orbital.ResultsProjection
Projection of the results of the pipeline.
This class is used to select the columns to be returned from the pipeline. It can be used to select specific columns to include in the final result set.
It can also be used to skip the select step of columns from the pipeline.
You can use the omit method to skip the projection
step entirely.
Source code in orbital/translate.py
__init__
omit
classmethod
omit() -> ResultsProjection
orbital.parse_pipeline
parse_pipeline(
pipeline: Pipeline, features: FeaturesTypes
) -> ParsedPipeline
Parse a scikit-learn pipeline into an intermediate representation.
Returns a orbital.ast.ParsedPipeline object that can be converted to SQL queries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Pipeline
|
The fitted scikit-learn pipeline to parse |
required |
features
|
FeaturesTypes
|
Mapping of column names to their orbital.types.ColumnType objects from the orbital.types module
|
required |
Source code in orbital/ast.py
orbital.parse_pytorch_model
parse_pytorch_model(
model: Module, features: FeaturesTypes
) -> ParsedPipeline
Parse a PyTorch model into an intermediate representation.
Returns a orbital.ast.ParsedPipeline object that can be converted to SQL queries.
Requires PyTorch, which can be installed with the orbital[pytorch] extra.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The trained PyTorch model to parse |
required |
features
|
FeaturesTypes
|
Mapping of column names to their orbital.types.ColumnType objects from the orbital.types module
As the model consumes a single input tensor, all features must be of the same type. |
required |
Source code in orbital/ast.py
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
orbital.export_sql
export_sql(
table_name: str,
pipeline: ParsedPipeline,
dialect: str = "duckdb",
projection: ResultsProjection = ResultsProjection(),
optimize: bool = True,
allow_text_tensors: bool = False,
separate_trees: bool = False,
) -> str
Export SQL for a given pipeline.
Generates a SQL statement equivalent to the provided pipeline for the
requested dialect. The statement can be executed directly on a database
that exposes a table matching table_name. Dialect names correspond to
those listed in sqlglot.dialects.DIALECTS.
If optimize is True the statement is post-processed by sqlglot's
optimizer, which often produces more compact SQL but may fail on complex
expressions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the source table used in generated SQL. |
required |
pipeline
|
ParsedPipeline
|
Parsed pipeline to export. |
required |
dialect
|
str
|
Target SQL dialect (any supported by sqlglot). |
'duckdb'
|
projection
|
ResultsProjection
|
Optional result projection helper. |
ResultsProjection()
|
optimize
|
bool
|
Whether to run the sqlglot optimizer (default |
True
|
allow_text_tensors
|
bool
|
Forwarded to orbital.translate.translate; controls whether
numeric/bool tensors coerced to text in ONNX should remain text in the
resulting SQL. Defaults to |
False
|
separate_trees
|
bool
|
Forwarded to orbital.translate.translate;
materialises each tree in an ensemble as its own SQL column before
summing so that columnar engines (e.g. DuckDB) can evaluate trees in
parallel. Defaults to |
False
|
Source code in orbital/sql.py
orbital.ast
Translate scikit-learn models to an intermediate represetation.
The IR is what will be processed to generate the SQL queries.
orbital.ast.ParsedPipeline
An intermediate representation of a scikit-learn pipeline.
This object can be converted to a SQL query and run on a database. It can also be saved and loaded back in binary format for the sake of model distribution. Even though distributing the SQL query is usually more convenient.
Source code in orbital/ast.py
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 | |
__init__
orbital.ast.ParsedPipeline objects can only be created by the orbital.ast.parse_pipeline function.
dump
dump(filename: str) -> None
Dump the parsed pipeline to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file where the pipeline will be saved |
required |
Source code in orbital/ast.py
load
classmethod
load(filename: str) -> ParsedPipeline
Load a parsed pipeline from a file.
Returns a orbital.ast.ParsedPipeline object loaded from the specified file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the file containing the saved pipeline |
required |
Source code in orbital/ast.py
orbital.ast.UnsupportedFormatVersion
Bases: Exception
Format of loaded pipeline is not supported.
This usually happens when trying to load a newer format version with an older version of the framework.
orbital.types
Data types of the features processed by models.
orbital.types.FeaturesTypes
module-attribute
FeaturesTypes = typing.Dict[str, ColumnType]
Mapping of feature names to their types.
orbital.types.ColumnType
Bases: ABC
A base class representing the type of a column of data.
Source code in orbital/types.py
__init__
__init__(passthrough: bool = False) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
passthrough
|
bool
|
If True, the column is ignored by the pipeline and is only available to SQL generator. You will still need to project those columns for them to be included in the SQL query. |
False
|
Source code in orbital/types.py
orbital.types.FloatColumnType
orbital.types.Float16ColumnType
orbital.types.DoubleColumnType
orbital.types.StringColumnType
orbital.types.Int64ColumnType
Bases: ColumnType
Mark a column as containing signed 64bit integer values
Source code in orbital/types.py
orbital.types.UInt64ColumnType
Bases: ColumnType
Mark a column as containing unsigned 64bit integer values
Source code in orbital/types.py
orbital.types.Int32ColumnType
Bases: ColumnType
Mark a column as containing signed 32bit integer values
Source code in orbital/types.py
orbital.types.UInt32ColumnType
Bases: ColumnType
Mark a column as containing unsigned 32bit integer values
Source code in orbital/types.py
orbital.types.Int16ColumnType
Bases: ColumnType
Mark a column as containing signed 16bit integer values
Source code in orbital/types.py
orbital.types.UInt16ColumnType
Bases: ColumnType
Mark a column as containing unsigned 16bit integer values
Source code in orbital/types.py
orbital.types.Int8ColumnType
Bases: ColumnType
Mark a column as containing signed 8bit integer values
Source code in orbital/types.py
orbital.types.UInt8ColumnType
Bases: ColumnType
Mark a column as containing unsigned 8bit integer values
Source code in orbital/types.py
orbital.types.BooleanColumnType
orbital.types.guess_datatypes
guess_datatypes(dataframe: Any) -> FeaturesTypes
Given a DataFrame, try to guess the types of each feature in it.
This procudes a orbital.types.FeaturesTypes dictionary that can be used by parse_pipeline to generate the SQL queries from the sklearn pipeline.
In most cases this shouldn't be necessary as the user should know on what data the pipeline was trained on, but it can be convenient when experimenting or writing tests.