Skip to content

PostgreSQL: add support for CREATE AGGREGATE#2316

Open
fmguerreiro wants to merge 14 commits into
apache:mainfrom
fmguerreiro:upstream/create-aggregate
Open

PostgreSQL: add support for CREATE AGGREGATE#2316
fmguerreiro wants to merge 14 commits into
apache:mainfrom
fmguerreiro:upstream/create-aggregate

Conversation

@fmguerreiro

@fmguerreiro fmguerreiro commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

Parses PostgreSQL CREATE [OR REPLACE] AGGREGATE, both the modern name (arg_types) (options) form and the legacy BASETYPE = ... form.

The full option set from the docs is covered: state and final functions, the moving-aggregate variants (MSFUNC, MINVFUNC, ...), PARALLEL, SORTOP, HYPOTHETICAL, and the FINALFUNC_MODIFY kinds (READ_ONLY / SHAREABLE / READ_WRITE).

parse_create_aggregate dispatches modern vs legacy on whether the option list opens with BASETYPE, building a new CreateAggregate node behind Statement::CreateAggregate. Round-trip tests cover both syntaxes, star and variadic args, the moving-aggregate options, and an unknown-option error.

Ordered-set aggregates (ORDER BY in the argument list) are out of scope to keep this reviewable.

Ported from the pgmold-sqlparser fork, where it was tested against PostgreSQL 13-17 schemas in pgmold.

Add Statement::CreateAggregate, CreateAggregate struct, CreateAggregateOption
enum, and AggregateModifyKind enum to represent PostgreSQL CREATE AGGREGATE
DDL. Options are stored as a typed enum covering all documented parameters
(SFUNC, STYPE, FINALFUNC, PARALLEL, moving-aggregate variants, etc.).
Wire AGGREGATE into the CREATE dispatch (before the or_replace error branch
so CREATE OR REPLACE AGGREGATE is accepted). parse_create_aggregate parses
the name, argument-type list, and the options block. Each recognised option
keyword dispatches to parse_create_aggregate_option which produces the typed
CreateAggregateOption variant.
Three tests covering: basic old-style aggregate (SFUNC/STYPE/FINALFUNC/INITCOND),
CREATE OR REPLACE with PARALLEL = SAFE, and moving-aggregate options
(MSFUNC/MINVFUNC/MSTYPE/MFINALFUNC_EXTRA/MFINALFUNC_MODIFY). All use
pg().verified_stmt() to assert parse-then-display round-trips identically.
PR #7 added the Statement::CreateAggregate variant but omitted the
corresponding match arm in the Spanned impl for Statement. Fork CI
never ran on the PR so this was not caught before merge.
- SORTOP now parses via parse_operator_name so bare operators
  (SORTOP = <) work correctly.
- INITCOND / MINITCOND now store ValueWithSpan, preserving source
  location and matching the rest of the DDL layer.
- Replace the hand-rolled option loop with parse_comma_separated,
  rejecting leading and doubled commas.
- Simplify empty arg-list detection (no more prev_token dance).
- Replace the PARALLEL match-with-fallthrough with the
  if/else-if shape used elsewhere in the parser.
- Extend the 'after CREATE OR REPLACE' error message to mention
  AGGREGATE.
Return the name's span instead of Span::empty() to match the
sibling Create* arms.
- Rename abbreviated CreateAggregateOption variants to full words
- Preserve (*) wildcard arg list via CreateAggregate::star_args
- Implement Spanned for CreateAggregate and delegate from Statement
- Parse aggregate args as OperateFunctionArg so VARIADIC/named args
  round-trip instead of being dropped
- Add verified_stmt coverage for HYPOTHETICAL, SORTOP, schema-qualified
  SORTOP, FINALFUNC_MODIFY, SSPACE, COMBINEFUNC, SERIALFUNC/DESERIALFUNC,
  star args, and named/variadic args
- Switch CREATE AGGREGATE tests to pg_and_generic(), use
  display_comma_separated for options, extract parse_function_parallel
- Replace em-dashes in CreateAggregateOption doc comments
- Add FunctionParallel::as_str and reuse it in Display and the PARALLEL option
- Support old-syntax BASETYPE option (parse and Display round-trip)
- Clarify the CreateAggregate args field doc
- Test the unknown-option error path and the old BASETYPE syntax
…gate

# Conflicts:
#	src/ast/mod.rs
#	tests/sqlparser_postgres.rs
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.

1 participant