Principle:4paradigm OpenMLDB SQL Feature Engineering
| Knowledge Sources | |
|---|---|
| Domains | Feature_Engineering, Database |
| Last Updated | 2026-10-12 12:00 GMT |
Overview
A SQL-based feature engineering approach that uses windowed aggregations, last joins, and extended SQL syntax to compute machine learning features from time-series data.
Description
SQL feature engineering in OpenMLDB extends standard SQL with time-windowed aggregations (WINDOW), point-in-time joins (LAST JOIN), and pre-aggregation tables. This allows data scientists to express feature computation logic in SQL rather than custom code, making features declarative, reproducible, and optimizable. The same SQL runs in both offline mode (via Spark batch processing) and online mode (via the real-time SQL engine), ensuring consistency between training and serving.
The key problem solved is training-serving skew: features computed differently in offline training and online serving lead to ML model degradation. By using the same SQL engine for both, OpenMLDB eliminates this skew.
Usage
Use this principle when building ML pipelines that require time-series feature computation (e.g., aggregating user behavior over time windows, joining features from multiple tables at specific timestamps). It is ideal for real-time recommendation, fraud detection, and predictive maintenance.
Theoretical Basis
OpenMLDB's SQL feature engineering extends SQL with windowed aggregation:
Where the window specifies:
- Partition: Grouping key (e.g., user ID)
- Order: Ordering column (e.g., timestamp)
- Frame: Row range (e.g.,
ROWS BETWEEN 60 PRECEDING AND CURRENT ROW)
Pseudo-code for windowed feature computation:
-- Abstract SQL pattern (real syntax in OpenMLDB)
SELECT
user_id,
SUM(amount) OVER w1 AS total_60min,
AVG(amount) OVER w1 AS avg_60min,
COUNT(*) OVER w1 AS txn_count_60min
FROM transactions
WINDOW w1 AS (
PARTITION BY user_id
ORDER BY txn_time
ROWS BETWEEN 60 PRECEDING AND CURRENT ROW
);
The LAST JOIN provides point-in-time join semantics, ensuring no future data leaks into features:
Practical Guide
- Start with offline SQL development using
sql_clientto validate feature queries - Use
WINDOWfor time-based aggregations — specify partition, order, and frame explicitly - Use
LAST JOINto join dimension tables without data leakage - Test with small datasets first, then deploy for online serving
- The
enable_distsqlflag enables distributed SQL execution for large offline datasets
Related Pages
Implemented By
- Implementation:4paradigm_OpenMLDB_CLI_Client
- Implementation:4paradigm_OpenMLDB_Go_SDK_Driver
- Implementation:4paradigm_OpenMLDB_Go_SDK_Connection
- Implementation:4paradigm_OpenMLDB_Demo_Scenarios