Abstract
The purpose of predictive modeling on relational data is to predict future or missing values in a relational database, such as future purchases, patient readmission risk, or fraudulent transactions. Typically powered by machine learning (ML), these models are essential for modern recommendation systems and supply chain optimization. However, training these models requires manual work to extract training examples—prediction entities and target labels—from the database, a process that is slow, laborious, and prone to mistakes like temporal data leakage. We present the Predictive Query Language (PQL), an SQL-inspired declarative language for defining predictive tasks. PQL allows specifying a task in a single query, enabling the automatic computation of training labels for regression, classification, time-series forecasting, and recommender systems. PQL is integrated into a predictive AI platform and demonstrated through implementations for both small-scale, low-latency use and large-scale batch processing.
Introduction
Real-world databases are dynamic: new records are appended, entities evolve, and historical values are updated. To train a machine learning model to predict a future event (e.g., "how much will this customer spend in the next 30 days?"), engineers must extract historical snapshots of the database. This requires "time travel"—aggregating future data to create the target label while strictly filtering out any input features that occurred after the prediction's anchor time.
Failing to enforce this strict temporal separation leads to data leakage, where a model inadvertently learns from the future, resulting in overly optimistic performance that collapses in production. Doing this manually with SQL or Pandas is notoriously slow and susceptible to subtle bugs. While existing declarative ML tools simplify model training, they assume the training table already exists. PQL fills this gap by providing a language specifically designed to safely and automatically generate these training tables directly from raw relational data.
The PQL Framework
PQL uses a concise, declarative syntax centered around two main clauses: PREDICT (what to predict) and FOR EACH (for which entities).
Core Components
- Temporal Safety: Users specify timeframes directly in the query (e.g.,
0, 30, days). The PQL engine handles the complex logic of sampling historical anchor times and ensuring strict point-in-time correctness for feature generation. - Counterfactuals via the
ASSUMINGClause: PQL introduces anASSUMINGoperator that acts as a forward-looking filter during training. This allows models to learn conditional outcomes, such as predicting a user's spend assuming they receive a push notification, unlocking causal inference. - Dual Execution Engines: The PQL compiler translates queries into logical plans for two distinct backends:
- Batch Processing (Spark): Designed for Relational Deep Learning (RDL), scaling to billions of rows by avoiding expensive cross-joins.
- Interactive (Relational Foundation Models): Designed for low-latency inference using a custom GraphSAGE-based sampler to fetch relevant subgraphs for in-context learning.
Automatic Task Inference
One of PQL's most powerful features is its ability to automatically infer the appropriate machine learning task based on the data types and aggregations used in the PREDICT clause. By tightly coupling the query language to the database schema, PQL removes the need for users to manually configure ML pipelines.
As shown in the figure above, predicting a boolean condition triggers binary classification, while predicting a LIST_DISTINCT of foreign keys triggers a link prediction (recommendation) task. This implicit definition allows the same query structure to serve diverse business needs without changing the underlying logic.
Evaluation and Results
The authors benchmarked PQL on three relational datasets: rel-Amazon (small), Fannie Mae (medium), and H&M (large, with 6.3B transactions).
- Massive Batch Scalability: On the H&M dataset, a naive Spark execution timed out after 10 hours. The optimized PQL Spark engine—which avoids entity-timestamp cross-joins and applies early filtering—completed the training table generation in just 96 minutes.
- Sub-Second Interactive Latency: For interactive queries, the custom graph-sampler implementation was up to 40x faster than a naive Pandas approach. It consistently executed in a fraction of a second, making it viable for real-time applications.
Limitations and Future Work
PQL currently relies on accurate database schemas and timestamps; retroactive modifications without proper annotations can break temporal leakage guarantees. The language also has syntax constraints, such as lacking support for arbitrary joins (inferred from schema) and operations between two columns. Future work aims to address these constraints and expand support for predictions involving sets of entities rather than single primary keys.
Summary updated