Skip to content

catalog: add aggregate and table functions to the builtin registry - #40912

Open
mgree wants to merge 3 commits into
builtin-registryfrom
builtin-registry-agg-table-sql
Open

mgree wants to merge 3 commits into
builtin-registryfrom
builtin-registry-agg-table-sql

Conversation

@mgree

@mgree mgree commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

PR 2 of SQL-923.

Description

The builtin registry now records every aggregate and table function variant alongside the scalar functions, so a change to their recorded properties (null handling, output types, monotonicity, WITH ORDINALITY support) fails the snapshot test. Variants are listed from their serde impls, so adding or removing one also fails it.

Unlike the scalar functions, we don't fingerprint the implementations of AggregateFunc or TableFunc, because sound coverage would need a compiler-resolved call graph that isn't worth its cost. The new table_func_inputs.slt runs every table function on edge-case inputs read from tables, so the functions actually execute instead of being constant-folded. PR 6 of SQL-923 adds an upgrade check that runs pinned plans across releases.

Verification

Rewrote the registry; new SLT passes (though we don't fingerprint).

@mgree
mgree marked this pull request as ready for review November 18, 2026 21:25
@mgree
mgree requested review from a team as code owners November 18, 2026 21:25
@mgree
mgree changed the base branch from main to builtin-registry November 18, 2026 21:27
@mgree
mgree added this pull request to stack #40921 November 18, 2026 21:28
@mgree
mgree force-pushed the builtin-registry-agg-table-sql branch from c9d83fe to 3a6b6bf Compare November 18, 2026 21:44

@ggevay ggevay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, some comments inline. The two sample gaps are the important ones.

(The description says "In PR 3" for the differential testing. SQL-923 has that as PR 5.)

SqlScalarType::String,
SqlScalarType::String,
])),
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample doesn't pin string_agg's nullability rule. The output is nullable when the value field inside the input record is nullable (the StringAgg arm of output_sql_type). But record_of makes every field non-null, and output_type_on_nullable_input only makes the outer column nullable. So both output types in the snapshot are String:NotNull, and the test still passes when that arm always returns false or reads the separator field. With a second sample whose value field is nullable, the test fails for both changes:

Suggested change
),
),
aggregate(
AggregateFunc::StringAgg {
order_by: order_by(),
},
ordered(SqlScalarType::Record {
fields: vec![
(ColumnName::from("f0"), SqlScalarType::String.nullable(true)),
(
ColumnName::from("f1"),
SqlScalarType::String.nullable(false),
),
]
.into(),
custom_id: None,
}),
)
.labeled("nullable_value"),

(Plus a REWRITE=1 run, which adds one record.)

el_typ: SqlScalarType::Int32,
})
.expect("unnest_list supports WITH ORDINALITY"),
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample doesn't pin WithOrdinality's empty_on_null_input, which comes from the inner function. unnest_list answers true, so the test still passes when empty_on_null_input is true for every WithOrdinality. abs(x) WITH ORDINALITY would then lose its NULL row, because TableFunc::eval returns no rows for a NULL argument when the property is true. With a second sample over a function that answers false, the test fails for that change:

Suggested change
),
),
table(
TableFunc::with_ordinality(TableFunc::TabletizedScalar {
name: "tabletized".to_string(),
relation: SqlRelationType::new(vec![SqlScalarType::Int32.nullable(true)]),
})
.expect("tabletized scalars support WITH ORDINALITY"),
)
.labeled("tabletized"),

(Plus a REWRITE=1 run.)

`VariadicFunc`. Run it whenever the diff touches
`src/expr/src/scalar/func.rs` or `src/expr/src/scalar/func/` (including
`impls/` and `#[sqlfunc]` bodies):
`builtin_registry` covers that gap for `UnaryFunc`, `BinaryFunc`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "New function with a payload" bullet below (line 100) still names only unary_samples(), binary_samples() and variadic_samples(). Could it also name aggregate_samples() and table_samples()?


# jsonb_each, jsonb_each_text, jsonb_object_keys

# NOTE: jsonb numbers are normalized, so `1.50` reads back as `1.5`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the jsonb number normalization, this file pins five more differences from PostgreSQL (checked on PostgreSQL 16):

  • regexp_matches on a pattern without capture groups returns {}. PostgreSQL returns the whole match, and so does our own regexp_match (DB-68).
  • The jsonb_each*, jsonb_object_keys and jsonb_array_elements* functions return no rows on the wrong kind of jsonb value. PostgreSQL errors (DB-75 covers jsonb_object_keys).
  • jsonb_each ... WITH ORDINALITY numbers the keys in sorted order. PostgreSQL numbers shorter keys first.
  • jsonb values print without spaces: [1,2], where PostgreSQL prints [1, 2] (database-issues#7085).
  • aclexplode of an empty array returns no rows. PostgreSQL errors.

Could each get a NOTE like the one here, so that the goldens don't read as intended behavior?

# their result and never runs the function. Every call here takes its
# arguments from a table column, so it runs in a dataflow, which is how a
# durably stored plan would run it. Rows whose input errors live in their own
# tables, because one erroring row fails the whole query.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is constant folding a good enough reason for a separate file, as opposed to adding these cases to table_func.slt? And is --auto-index-selects relevant here? table_func.slt is on tests_without_views, so it doesn't run with that flag in PR CI. If the file stays separate, could the header explain why, so that people adding tests later know which file to put theirs in?

mgree and others added 3 commits November 23, 2026 18:08
…yload gate's input

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nctions

Adds samples where one sample could not tell a property's branches apart:
string_agg over a nullable value field, WITH ORDINALITY over inner
functions with keys, broken monotonicity or no empty-on-NULL behavior,
fused value window functions with lead and last_value, and min/max/sum at
varchar, scaled numeric and precise timestamp inputs. ORDER BY columns and
lag/lead values now use types distinct from neighboring fields, so reading
the wrong field changes the recorded output type.

Notes PostgreSQL differences in table_func_inputs.slt, and why that file
runs separately from table_func.slt. Names the aggregate and table sample
lists in the mz-commit skill.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mgree
mgree force-pushed the builtin-registry-agg-table-sql branch from ba51ee6 to ff45564 Compare November 23, 2026 23:51
@def-

def- commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

QA LLM Review

1. MEDIUM -- Aggregate type samples bypass the planner rule that preserves type modifiers

src/expr/src/scalar/func/registry.rs:1519

The new varchar, numeric-scale, and timestamp-precision samples call the MIR aggregate typer directly. A change that makes the SQL planner return text for max(c) on a CHAR(10) column would leave every registry entry unchanged, even though a freshly planned octet_length(max(c)) would return 3 for 'def' and a pinned plan would still return 10.

Details

SQL resolves the fixed-width character overload at src/sql/src/func.rs:3722 and computes its result type in src/sql/src/plan/hir.rs:1563. That type determines the padding baked into the plan at src/sql/src/func.rs:2583. By the time MIR computes the aggregate's representation type, the input has already lost its length, scale, or precision (src/expr/src/relation/func.rs:2861). Probe the HIR result from the catalog test for these cases, using the actual CHAR(10) overload for the string sample.

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.

3 participants