Skip to content

fix(query): raise type for an if with a temporal arm and a numeric arm - #1487

Open
singaraiona wants to merge 3 commits into
devfrom
fix/if-temporal-numeric-arms
Open

singaraiona wants to merge 3 commits into
devfrom
fix/if-temporal-numeric-arms

Conversation

@singaraiona

@singaraiona singaraiona commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Bug

In a query, an if with one temporal arm (DATE, TIME, TIMESTAMP or TIMESPAN) and one numeric arm (BOOL, U8, I16, I32, I64, F32 or F64) wrote the temporal arm's raw count beside the numbers: days for DATE, ms for TIME, ns for TIMESTAMP and TIMESPAN. A computed arm boxed the cells into a LIST instead (#1485). On dev:

(select {r: (if (> g 0) d i) from: T})        ;; I32 [10 8767 8768]: days among integers
(select {r: (if (> g 0) p f) from: T})        ;; F64: ns among floats
(select {r: (if (> g 0) (+ d 1) i) from: T})  ;; LIST [1 2000.01.04 2000.01.05 4]

anton decided on the board (T-843, A) that this raises type, as #1481 made it for two different temporal arms.

Fix

  • promote_if_type (src/ops/graph.c) now returns fix(query): raise type for an if over two temporal types #1481's "no common type" code for a temporal arm beside a numeric one, in either order.
  • ray_if applies the null-literal exception first, so (if c d 0N) stays DATE, then refuses the pair at plan time: if: branches mix DATE and I32. That is one check per if node.
  • fix(query): raise type for an if over two temporal types #1481's routes carry the refusal, so every route raises it: column or atom arms, a computed arm, a one-value condition, nesting, cond, a call around the if, select (keyless, where:, by:, a by: key, an aggregate's argument, a where: predicate), update (keyless, where:, by:), and parted tables.
  • Some arms don't compile in the planner, for example (at v 0), (count (distinct k)), a TIMESPAN from (- p p), or an if over aggregates under by:. These run one arm per row or per group, and boxed the cells into a LIST (two different temporal types did the same after fix(query): raise type for an if over two temporal types #1481). eval_expr_per_row_impl and nonagg_eval_per_group_core in src/ops/query.c now check that LIST when the expression is itself an if. An if inside a call, such as (at (enlist d 7) (if c 0 1)), doesn't own the call's values, so the dev LIST stays.
    • The if's branches, including those of nested ifs in branch position, are scanned for null literals. A null cell of a null literal's type is that literal. Any other cell counts by its type, including a computed null such as (at [0Nl] 0).
    • A temporal cell beside a numeric cell, or beside a different temporal type, raises the same if: branches mix ....
    • One temporal type, with null-literal cells, becomes that type's column, as ray_if makes it. (if c (+ t 1000) 0Nd) is now a TIME column with 0Nt instead of a LIST holding 0Nd.
    • Limit: these routes see values, not arm types. When every row or group takes the same arm, there is no mix to see, and the column is that arm's. Before this PR the same data gave a typed column too.
  • Outside a query, if is control flow and evaluates one arm, which is unchanged.

Answers that change: every temporal × numeric pair in a query now raises type. To get the raw count, cast the arm: (if c (as 'I64 d) i).

Tests

  • test/rfl/query/if_temporal_numeric.rfl (new, @also-unfused) covers:
    • all 28 temporal × numeric pairs, each in both orders, with atom arms, under where:, under by: and in update;
    • computed arms, an atom condition, nesting, cond, a call, keyless and per-group aggregates, a by: key, a where: predicate, update with where: and by:, and overwriting a column;
    • null literals in both orders, the as forms, a parted table, and control flow outside a query;
    • the per-row and per-group routes: (at V 0), (count (distinct k)), (- p p), (- t t), an if over aggregates under by: (with groups on both arms), each on a parted table too; the null-literal cases there, including a chain and a nested if; a computed null, which raises; and an if inside at, which keeps dev's LIST.
    • It fails on dev with got "[10 8767 8768]", and without the query.c check with a LIST at (if (> g 0) d (at V 0)).
  • These pinned the old blend and are repinned to type, each keeping its intent through the as form or a null literal: test/rfl/query/grouped_if_rowwise.rfl, if_temporal_mix.rfl, if_temporal_null.rfl and test/rfl/ops/pivot_coverage.rfl (the empty-branch cases). test/rfl/arith/temporal_int_arith.rfl pinned the LIST for (if c (+ t 1000) 0Nd) and now pins the TIME column.
  • Full suite on the head: 5590 of 5594 passed, 4 skipped, 0 failed.

Docs

  • docs/docs/language/control-flow.md, docs/docs/guides/types.md, docs/docs/language/functions.md and docs/docs/reference/all-functions.md: a temporal arm beside a number raises type, with the (as 'I64 d) form for the raw count. mkdocs build --strict passes.

Fixes #1485

In a query, an if with one temporal arm (DATE, TIME, TIMESTAMP, TIMESPAN)
and one numeric arm (BOOL, U8, I16, I32, I64, F32, F64) wrote the temporal
arm's raw count (days, ms, ns) beside the numbers, and a computed arm boxed
the cells into a LIST. promote_if_type now gives such a pair no common
type, as #1481 did for two temporal types, so ray_if refuses it at plan
time and every route raises `if: branches mix DATE and I32`. A temporal arm
beside a null literal keeps its type; outside a query if is control flow.
…er-group routes

An if whose arm the planner cannot compile, such as (at v 0), a TIMESPAN
from (- p p), or an if over aggregates under by:, runs one arm per row or
per group and boxed the cells into a LIST. eval_expr_per_row_impl and
nonagg_eval_per_group_core now check such a LIST for an expression holding
an if: a temporal cell beside a numeric or another temporal one raises
`if: branches mix ...`, and one temporal type beside null cells only is
that type's column, as ray_if makes it for a null literal.
… per-row routes

rw_if_cells_check judged any expression holding an if, so an if that only
picks an index, (at (enlist d 7) (if c 0 1)), was blamed for the call's
mixed cells; it now judges only an expression that is the if.  A null cell
was taken for a null literal whatever its arm; now only a null of the type
of one of the if's null literal arms (through nested ifs in arm position)
takes the other arms' type, and a computed null, (at [0Nl] 0), counts as
its arm's I64 and raises beside a DATE arm.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An if with a temporal arm and a numeric arm blends raw counts in a query

1 participant