Skip to content

IR: global PromQL min/max are declared nullable, but never output NULL #1351

Open

Description

@zzylol

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 as sum(m).
  • A SQL SELECT max(x) FROM t WHERE false has one NULL row, so a global SQL MAX is 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 is Min/Max, and the input's evaluated_rows_kind is Relation;
  • 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 checking Sum, 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) and min(m) lower to a non-null value column and that SELECT max(x) FROM t lowers to a nullable one. The first assertion fails on main today. The corpus plan diff should show only the changed nullability.

Related

Activity

  1. added
    bugSomething isn't working
    irPre-ASAP or post-ASAP intermediate representation
    promqlPromQL front-end lowering (L1→L2)
    schemaSchema, type, key, and nullability contracts
    on Nov 15, 2026
  2. zzylol commented on Nov 15, 2026

    @zzylol
    ContributorAuthor

    A related latent IR gap in the same area. It surfaced while reviewing #1350, and no plan reaches it today.

    source_kind in crates/types/src/ir/operator/asap.rs maps a non-ASAP node whose result kind is State to Relation when that node is not a filter, time range or time shift. It does not follow the node's input. The case that matters is a Project that keeps state columns over states built from PromQL rows. The IR would call that Relation, 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 is State, recurse into its first child instead of returning Relation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingirPre-ASAP or post-ASAP intermediate representationpromqlPromQL front-end lowering (L1→L2)schemaSchema, type, key, and nullability contracts

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions