Predictive Queries
What Is a Predictive Query
A Predictive Query is a declarative syntax that defines a predictive modeling task in Kumo. It specifies the target variable to predict and the data context for training.
Kumo uses Predictive Query Language (PQL), a SQL-like syntax, to automate the ML pipeline, including feature engineering, training table generation, and model training.
Kumo does three things with your query:
- Build labels. Kumo picks many past points in time. At each one, it computes the true answer from your history (for example, whether each customer actually ordered in the following 30 days). Each answer becomes a training label, and together they form your training table. How labels are built depends on the query type: a temporal query computes them at many past anchor times, while a static query reads them from existing column values.
- Train. Kumo trains a model that learns to predict those labels from everything connected to the entity in your graph: the customer’s profile, past orders, the products in those orders, and so on. You do not write features.
- Predict. Kumo applies the same query at a point in time you choose, usually now, and scores each entity.
Because the same query drives labels, training, and scoring, changing the question means changing one statement, not rebuilding a pipeline.
Build Your First Predictive Query
Suppose a retail team asks: “Which of our recently active customers will order again in the next 30 days?” This translates to:
Each part of the question becomes one part of the query:
There are two ways to get from your own question to a query:
Write It Yourself
Let an Agent Write It
Answer four questions about your problem, and write each answer as a clause:
To check the query against your graph before training, refer to Use PQL in Python.
Install the kumo-data-science skill by following the Quickstart for Kumo Engine with Coding Agents.
Then ask your agent the business question in plain language, and say where your data lives:
Use PQL in Python
To use a query in practice, create a PredictiveQuery from a kumo.engine.Graph and the PQL string.
Next, turn the query into labeled rows with query.generate_training_table(snapshot) (refer to Generate or Supply Training Tables), and score entities with query.generate_prediction_table(snapshot, prediction_time=...) (refer to Make Predictions).
PQL Deep Dive
The rest of this page is for when you need more than a first query. It covers the task types PQL supports, the full syntax, and the details that decide whether a model learns the right thing: static versus temporal queries, anchor times, and data leakage.
- The Example Data: the three retail tables every example uses.
- Query Structure: the shape of every query and the forms a target can take.
- Every PQL Is Static or Temporal: how the target decides what Kumo trains on and scores.
- Supported Task Types: the business question, query, and output for each task type.
- Appendix: anchor time, data leakage, and the full PQL reference.
The Example Data
Every query on this page uses the same three tables from a retail store.
Query Structure
Every predictive query has the same shape. Square brackets mark optional parts. Angle brackets are placeholders.
The query below uses most of these parts. It predicts how much each recently active customer will spend by card in the next 30 days.
The target after PREDICT takes one of three forms:
- A column, such as
PREDICT customers.loyalty_tier. Use this to fill in values that some entities have and others do not. - An aggregation over a time window, such as
PREDICT SUM(orders.amount, 0, 30, days). Use this to predict what will happen. - A condition built from either, such as
PREDICT customers.loyalty_tier = 'gold'orPREDICT COUNT(orders.*, 0, 30, days) > 0. A condition always produces a yes or no answer.
For every clause and the rules behind it, such as which columns a target can use and where a time window can point, refer to Supported Clauses in the Appendix.
Every PQL Is Static or Temporal
The rule is simple: a query is temporal when its target has a time window, and static otherwise. The choice decides how Kumo builds training rows and what it scores.
Static: predict a value that is missing. Orders that already have a fraud label train the model. Orders without one are scored.
Temporal: predict what happens next. For each recently active customer, will they stop ordering in the next 30 days?
The following table compares static and temporal queries:
Which should you use? Ask when the answer becomes known. If it is already recorded on the row, or recorded when the row is created (a fraud label, a customer’s tier), use a static query. If it depends on events after the moment you score (future orders, churn, demand), use a temporal query.
Rules for static queries. A static query cannot use a time window anywhere, including in its entity filter.
It cannot use ASSUMING or FORECAST, and it does not accept a prediction time.
A target cannot mix a static part and a temporal part.
A temporal query is only as trustworthy as the timestamps behind it. For more information, refer to Avoid Data Leakage.
Supported Task Types
The task type is the kind of machine learning problem Kumo trains for.
You do not set it.
Kumo infers it from three things: the form of the target, the semantic type of the value it produces, and the RANK, CLASSIFY, or FORECAST modifier.
Call get_task_type() to confirm it before training.
Ask the questions in order and stop at the first match.
The following table lists each task type with its typical query shape and main output columns:
Each task type below starts with the business question it answers, then the query, then sample output. For complete queries on other datasets, refer to Predictive Query Examples.
Binary Classification
Churn. Will an active customer stop ordering in the next 30 days?
The following table shows sample output:
True_PROB is the probability that the condition is true.
Any condition is binary classification, so > 0 (will order) works the same way as = 0 (will not order).
Multiclass Classification
Next payment method. Which payment method will the customer use first in the next 30 days?
The following table shows sample output:
The output has one row per returned class for each entity. PREDICTED marks the top class.
Multilabel Classification
Payment methods in use. Which payment methods will each customer use in the next 30 days?
The following table shows sample output:
Each label is predicted independently, so several can be true.
Static version: PREDICT products.tags CLASSIFY FOR EACH products.product_id fills in missing tags.
Multilabel Ranking
Payment methods to offer first. Which two payment methods is each customer most likely to use next month?
The following table shows sample output:
Regression
Next month’s spend. How much will each customer spend in the next 30 days?
The following table shows sample output:
Static version: PREDICT products.list_price FOR EACH products.product_id.
Forecasting
Weekly revenue forecast. How much revenue will each product bring in during each of the next four weeks?
The following table shows sample output:
Each product gets four weekly figures. TIMESTAMP is the start of each window.
For more information, refer to Generate Forecast Inputs.
Temporal Link Prediction
Products to recommend. Which 10 products is each customer most likely to buy in the next 30 days?
The following table shows sample output:
CLASS holds the recommended item’s key (a product_id).
Static Link Prediction
Recommendations without timestamps. Which 10 products is each customer most likely to buy, when your data does not record when purchases happened?
Static link prediction is the only task whose target aggregation has no time window, and it only works when no table in the graph has a time column.
For this example, imagine a graph with customers, products, and a purchases table that links them, none of which have time columns:
The output has the same columns as temporal link prediction.
How many results come back. For multiclass, multilabel, and link prediction, choose how many classes or items to return per entity with num_classes_to_return at prediction time.
It defaults to k from TOP k, and link prediction requires it (at most 500) when you export predictions.
For more information, refer to Task-Specific Output Options.
Appendix
The appendix contains the reference material for the deep dive:
- Anchor Time: how Kumo separates known data from the future, and how to avoid data leakage.
- Supported Clauses: every clause in a query, with the rules for targets, entities, and modifiers.
- Aggregations: the functions that combine rows into one value.
- Time Windows: how to write a window and where it can point.
- Conditions and Operators: comparisons, text patterns, and constants.
- Filters: entity filters, target filters, and
ASSUMING.
Anchor Time
The anchor time is the “as of” moment of a prediction. Everything recorded at or before the anchor time is known and can be used as input. Everything after it is the future: the target window starts right after the anchor time, and the model never sees that data as input.
The figure shows the churn query from Every PQL Is Static or Temporal at one anchor time.
The entity filter looks back, and the target looks forward.
An order placed exactly at t counts for the filter, not for the label.
Anchor Times in Training
For a temporal query, Kumo creates training rows at many past anchor times. At each one, it hides everything after the anchor, applies the entity filter, and computes the label from the window that follows. The same customer can appear many times, once per anchor, with a different label each time.
The spacing above is illustrative. Kumo chooses the number and spacing of anchors from your data and the window length. You can change them in the training table generation plan. You need history that is much longer than the target window, so that there are enough past anchors to learn from.
Anchor Time at Prediction
When you generate predictions for a temporal query, you can pass prediction_time to set the anchor time.
If you leave it out, Kumo uses the latest timestamp found in the target’s tables.
Static queries have no anchor time at prediction.
Pass a datetime or pandas.Timestamp without a timezone. Kumo reads it as UTC, and a value with no time part means midnight at the start of that day.
Avoid Data Leakage
Anchor times only protect you if your timestamps are honest. Leakage happens when an input contains information that would not be available at the anchor time in production. Common causes: a column that is filled in after the outcome is known, a timestamp that marks the start of a period whose values were summarized at its end, and an entity column that is overwritten over time. Fix the data or remove the column. Tuning the model does not fix it.
Supported Clauses
The following table lists every clause a query can use, followed by the rules each one must follow.
The following rules apply to these clauses:
FOR EACHtakes the primary key of the table whose rows you want predictions for. WritingFORalone, or naming a column that is not the primary key, is an error.- A target column must be on the entity table or on a table the entity table points to directly through a foreign key, so that each entity has exactly one value.
PREDICT products.category FOR EACH orders.order_idworks because each order has one product.PREDICT orders.amount FOR EACH customers.customer_idfails because a customer has many orders. Aggregate them instead. RANK TOP kandCLASSIFY TOP k:kmust be at least 1.CLASSIFYonLIST_DISTINCTneeds a time window. A static list target usesRANK TOP k, or a multicategorical column withCLASSIFY.FORECAST n TIMEFRAMES:nis from 1 to 10,000, and the target must be a numerical aggregation with a time window.LIST_DISTINCTof a foreign key that points back to the entity table has nothing to predict:LIST_DISTINCT(orders.customer_id, 0, 30, days) FOR EACH customers.customer_idis an error.
Aggregations
An aggregation combines the rows linked to each entity into one value. The aggregated table must have a foreign key directly to the entity table. In a target, an aggregation looks forward over a time window.
The following rules apply to aggregations:
SUMandAVGneed integer or decimal columns. To total a boolean column, count the matching rows instead:COUNT(orders.* WHERE orders.is_fraud = true, 0, 30, days).SUM,AVG, andCOUNT_DISTINCTcannot be applied to ID, primary key, or foreign key columns.- A target cannot be a text or timestamp value, so
MIN,MAX,FIRST, andLASTof those columns only work in filters. LIST_DISTINCTreturns a set. As a target, rank it withRANK TOP kor classify it withCLASSIFY. In a filter, test it withCONTAINS.- Function names use an underscore (
COUNT_DISTINCT,LIST_DISTINCT), not a space.
Time Windows
An aggregation’s time window is written as three extra arguments: a start offset, an end offset, and a unit.
Offsets are whole numbers measured from the anchor time. Positive numbers look forward, and negative numbers look back.
- Boundaries: the start is excluded and the end is included.
(0, 30, days)covers everything after the anchor time, up to and including 30 days later. - Order: the start must be smaller than the end.
- Units:
minutes,hours,days, ormonths. If you leave the unit out, Kumo usesdays. There is noweeksoryears. Write14, daysor12, months. - All history: use
-INFas the start to look back over the full history, as inCOUNT(orders.*, -INF, 0, days).
Where a window can point depends on the clause:
A window does not have to start at the anchor time. This target sums each customer’s spending from day 10 to day 30 after the anchor time:
If the anchor time is 2025-01-01 00:00:00, the label counts orders with 2025-01-11 00:00:00 < order_time <= 2025-01-31 00:00:00.
Conditions and Operators
Conditions compare a column or aggregation with a constant. You can use them in the target (to make a yes or no question) and in filters.
The following table shows how to write each kind of constant:
Filters
A condition can appear in three places, and each place does something different:
Entity Filter
Predict whether gold or platinum customers will spend more than $500 next month:
An entity filter can use columns of the entity table, columns of tables it points to, and aggregations that look back in time.
It cannot compare a column of a table with many rows per entity directly: WHERE orders.amount > 100 fails because a customer has many orders.
Wrap it in a count instead: WHERE COUNT(orders.* WHERE orders.amount > 100, -INF, 0, days) > 0.
Target Filter
Predict whether each customer will place an order over $100 in the next 30 days:
If a target filter only mentions the entity table, Kumo warns you to move it into the entity filter, where it belongs.
Next Steps
After you write a query, continue with the following pages: