Examples · Export

export_sql

Generate SQL, execute it in SQLite against the same rows, and diff the results against the Python runtime.

Code#

export_sql.py
import sqlite3
 
from _setup import artifact, feature_names, row
 
from compileml.export import export_sql
from compileml.runtime import decide
 
# "ansi" emits GREATEST/LEAST; "sqlite" emits the two-argument MAX/MIN
# that SQLite understands. The integers either produces are the same.
sql = export_sql(artifact, table="applications", dialect="sqlite")
print(f"generated {len(sql.splitlines())} lines of SQL, no model runtime required")
print()
print("\n".join(sql.splitlines()[:10]))
print("...")
 
# Run it. One row in, one decision out, in a database that has never heard
# of scikit-learn.
connection = sqlite3.connect(":memory:")
columns = ", ".join(f'"{name}" REAL' for name in feature_names)
connection.execute(f"CREATE TABLE applications ({columns})")
connection.execute(
f"INSERT INTO applications VALUES ({', '.join('?' * len(row))})",
row,
)
 
sql_row = connection.execute(sql).fetchone()
description = [d[0] for d in connection.execute(sql).description]
result = dict(zip(description, sql_row))
 
python = decide(artifact, row, explain=False)
 
print()
print(f"{'':<14}{'SQLite':>12}{'Python':>12}")
for key, mine in (("latent_int", "latent_int"), ("band", "band")):
print(f"{key:<14}{str(result[key]):>12}{str(python[mine]):>12}")
 
print()
print("identical:", result["latent_int"] == python["latent_int"] and result["band"] == python["band"])

Output#

Captured from an actual run against compileml 0.9.0 and the UCI credit panel. If this script stops working, the build fails.

captured in CIexport_sql.py
generated 369 lines of SQL, no model runtime required
 
-- CompileML decision artifact export
-- artifact_hash: a31afc9bf476ce94714946a120a525f5b0049dcfe1461e4abf6c01cde434e66c
-- Integer-exact pipeline: score -> clamp -> display -> band -> PD (ppm).
WITH tree_scores AS (
SELECT
src.*,
(CASE WHEN "PAY_1" <= 1.5
THEN CASE WHEN "PAY_2" <= 1.5
THEN -15249
ELSE 38890 END
...
 
SQLite Python
latent_int 341 341
band G09 G09
 
identical: True

Notes#

  • The source table must expose one column per artifact feature name; NULL follows the artifact's missing policy.
  • CI runs exactly this diff on every commit.

API reference: export_sql →