Repository navigation
Description
Problem
The IR declares PromQL max(m) and min(m) nullable, but the executor now produces a non-null column for them. The IR and the executor state different rules for the same output.
Lowered with the PromQL frontend (exact target), the root's value column is:
| Query | Result kind | IR value column |
|---|---|---|
max(m) |
InstantVector |
max: Float64, nullable = true |
min(m) |
InstantVector |
min: Float64, nullable = true |
max(max by (job) (m)) |
InstantVector |
max: Float64, nullable = true |
sum(m) |
InstantVector |
sum: Float64, nullable = false |
max by (job) (m) |
InstantVector |
max: Float64, nullable = false |
(From a throwaway probe test that calls the frontend's lower_promql on main plus #1350 and prints the root's last field.)
What each should be:
- A PromQL aggregate over an empty vector has no sample. A global
max(m)therefore never outputs a NULL row, so its value is non-null, the same assum(m). - A SQL
SELECT max(x) FROM t WHERE falsehas one NULL row, so a global SQLMAXis nullable.
The executor follows this rule after #1350. Over a PromQL vector it builds the global Max without emit_empty_global, so the output is non-null. Over a Relation it opts into SQL's empty row, which makes SUM, AVG, MIN, MAX and percentile nullable. Plans install anyway because Operator::with_output_schema accepts a non-null native column under a nullable declared one. So nothing fails today, but the declared schema of every global PromQL min/max is looser than what runs. Any consumer of the IR schema (Stage 0 nullability, downstream checks, the plan's output schema) sees a NULL that can never occur.
Cause
crates/types/src/ir/schema/aggregate_schema.rs (around line 197), in the aggregate's schema derivation:
// A global extremum emits NULL for an empty input, even if its input
// column is non-nullable. Grouped extrema only emit existing groups.
if by.is_empty() && matches!(intent, AggIntent::Min { .. } | AggIntent::Max { .. }) {
out.nullable = true;
}The rule is SQL's. It does not check whether the aggregate reads a Relation or a PromQL vector. The executor reads that from the IR: evaluated_rows_kind of the input in types::ir::operator::asap, and input_rows_kind in crates/executor/src/physical_planner/mod.rs.
Proposal
Make the global-extremum rule depend on the input's rows kind, as the executor's does:
- nullable when
by.is_empty(), the measure isMin/Max, and the input'sevaluated_rows_kindisRelation; - otherwise nullable only when the input column is.
The schema derivation would need the input's rows kind, which Operator::output_schema can reach through its OperatorInput child. The SUM/AVG and percentile nullability of a global SQL aggregate should follow the same rule. Today SUM/AVG is nullable from the input column in the IR and from SQL's empty row in the executor. Check that both say the same thing.
Scope and acceptance
- In scope: IR schema derivation of global aggregates (
Min,Max, and checkingSum,Avg, percentile) for PromQL vs SQL inputs. - Out of scope: the executor, which already follows the rows kind.
- Test: a frontend test asserting that
max(m)andmin(m)lower to a non-null value column and thatSELECT max(x) FROM tlowers to a nullable one. The first assertion fails onmaintoday. The corpus plan diff should show only the changed nullability.
Related
- fix(executor): global aggregates follow the input's result kind, not the identity column #1350: makes the executor take the SQL-or-PromQL rule from the IR's rows kind. This mismatch was found in its round-1 review.
- IR: set-op and aggregate nullability, and the SQL top-k rewrite's output shape #657: SQL aggregate results that should be nullable but are declared non-null (the opposite direction, for SQL).
Activity
zzylol commentedon Nov 15, 2026
A related latent IR gap in the same area. It surfaced while reviewing #1350, and no plan reaches it today.
source_kindincrates/types/src/ir/operator/asap.rsmaps a non-ASAP node whose result kind isStatetoRelationwhen that node is not a filter, time range or time shift. It does not follow the node's input. The case that matters is aProjectthat keeps state columns over states built from PromQL rows. The IR would call thatRelation, so a global merge or aggregate above it would get SQL's one row over empty input instead of PromQL's no sample.Suggested fix: in
source_kind's non-ASAP arm, when the node's kind isState, recurse into its first child instead of returningRelation.