Introduction
In modern enterprise environments, data typically resides within Database Management Systems (DBMSs), yet machine learning (ML) inference often occurs in external environments. This separation necessitates exporting data—often millions of rows—into formats like CSV to be processed by external prediction services or Python containers. This "data movement" introduces significant performance overhead, increases technical debt through fragmented infrastructure, and complicates data governance and security.
This paper introduces MASQ (Machine learning AS Query), a library designed to bridge this gap by translating trained ML pipelines directly into standard SQL. By executing inference within the DBMS, MASQ eliminates the need for data movement, leveraging the database's native processing power for end-to-end prediction.
The MASQ Framework
MASQ operates as a compiler that converts complex ML pipelines—including both featurization and model scoring—into SQL queries. The architecture consists of three primary stages:
- Parsing: MASQ extracts fitted parameters, such as weights from linear models or thresholds from decision trees, from standard frameworks like Scikit-learn or ML.NET.
- Analysis: The system evaluates database-specific constraints. For instance, if a model exceeds the maximum column limit of a table, MASQ employs a "triplet-based representation" (
ID, Attribute, Value) to manage high-dimensional data. - Conversion: Logic is mapped to SQL constructs. Linear models and scalers become arithmetic expressions in
SELECTstatements, while decision trees and one-hot encodings are transformed into nestedCASEstatements.
A critical optimization in MASQ is operator fusion, which merges featurization and model evaluation into a single SQL statement. This reduces redundant computations and has been shown to improve performance by up to 5x on high-dimensional datasets.
Evaluation and Results
The authors evaluated MASQ across 10 pipelines and 7 diverse datasets, ranging from classic benchmarks like Iris to large-scale regression tasks like Criteo (4 million rows) and Flight Delay (21 million rows).
Performance and Accuracy
MASQ achieves high numerical precision, with prediction errors relative to original frameworks ranging only between $10^{-5}$ and $10^{-8}$. More importantly, it demonstrates significant performance gains:
- Throughput: For linear models, MASQ often outperforms Sklearn and ML.NET when data is already resident in the database, as it avoids the "data movement tax."
- Latency: In single-record "online" prediction scenarios, MASQ was up to 3x faster than external frameworks by bypassing connector and serialization overhead.
Insights into SQL as an ML Engine
The research reveals that while SQL is surprisingly expressive—capable of handling complex tree ensembles via unrolled CASE statements—it faces limitations. Standard DBMS optimizers are not currently "ML-aware"; for example, they do not push CASE conditions into index lookups. Furthermore, SQL struggles with heavy text manipulation and the matrix-heavy computations required for neural networks, which lack the BLAS optimizations found in dedicated ML runtimes.
Conclusion
MASQ demonstrates that for many common ML tasks, "plain" SQL is a formidable competitor for inference. By keeping data within the database, organizations can achieve better performance, simpler infrastructure, and tighter security. Future work points toward enhancing DBMSs with ML-aware cost-based optimizers and better support for complex User-Defined Functions (UDFs) to further close the gap for more intensive model types.
Summary updated