Skip to content

fix(postgres): preserve aggregate and array cast arity - #5845

Open
tensordreams wants to merge 1 commit into
mainfrom
fix/postgres-aggregate-and-array-casts
Open

fix(postgres): preserve aggregate and array cast arity#5845
tensordreams wants to merge 1 commit into
mainfrom
fix/postgres-aggregate-and-array-casts

Conversation

@tensordreams

Copy link
Copy Markdown
Contributor

Addresses the remaining PostgreSQL type-conversion gaps surfaced by MONEY aggregates in prisma/prisma#28681 and related array-cast paths.

Changes

  • Query compiler: Mark MONEY _sum and _avg inputs for result coercion so PostgreSQL evaluates SUM(column::numeric) and AVG(column::numeric).
  • Relation joins: Preserve list arity when serializing MONEY and BigInt fields through PostgreSQL JSON, producing numeric[] and text[] casts for list fields.
  • Schema engine: Migrate enum arrays through text[] while retaining scalar text casts for scalar enum columns.

Why

PostgreSQL resolves aggregate overloads from their input types, while JSON serialization and enum migrations must preserve whether the source is scalar or an array. Carrying arity through each renderer avoids locale-dependent MONEY output and prevents arrays from being coerced through scalar text representations.

Verification

  • cargo fmt -- --check
  • Quaint PostgreSQL JSON build-object tests
  • cargo test -p sql-query-builder --lib
  • PostgreSQL QC connector regression for MONEY _sum and _avg
  • Enum-array migration test compiled, but runtime execution was blocked because PostgreSQL was unavailable at localhost:5438

Signed-off-by: Alexey Orlenko's AI Agent <robot@aqrln.net>
@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Review Change Stack

Summary by CodeRabbit

  • Bug Fixes

    • Improved PostgreSQL handling of money and bigint array values when generating JSON objects.
    • Corrected aggregation selection behavior for average and sum operations.
    • Fixed PostgreSQL enum-array migration casts.
    • Improved enum migration handling for populated arrays and scalar enum columns.
  • Tests

    • Added coverage for money and bigint arrays in JSON output.
    • Added PostgreSQL money aggregation coverage.
    • Expanded enum-array and enum-column migration tests.

Walkthrough

The changes preserve PostgreSQL array typing in JSON object casts for money and bigint columns. Aggregate average and sum selections now mark their column expressions as selected, with native money aggregate coverage added. PostgreSQL enum-array migration SQL now casts through text arrays, and migration tests cover populated enum arrays plus simultaneous scalar and array enum changes.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main PostgreSQL type-casting and aggregate arity fixes in this changeset.
Description check ✅ Passed The description is directly aligned with the code changes and verification steps in the pull request.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/postgres-aggregate-and-array-casts
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/postgres-aggregate-and-array-casts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codspeed-hq

codspeed-hq Bot commented Jul 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 11 untouched benchmarks
⏩ 11 skipped benchmarks1


Comparing fix/postgres-aggregate-and-array-casts (43ca649) with main (059a7b2)

Open in CodSpeed

Footnotes

  1. 11 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@schema-engine/sql-migration-tests/tests/migrations/enums.rs`:
- Around line 545-546: Extend the migration test around the enum-array insert
and the `02remove` migration to query the seeded `Test` row afterward and assert
that `positions` remains `["First", "Second"]` in the original order. Keep the
existing migration-application assertion, and add the value-preservation check
specifically after `02remove`.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 20f7df22-25c2-4b4b-ace4-b56865ec4ab2

📥 Commits

Reviewing files that changed from the base of the PR and between 059a7b2 and 43ca649.

📒 Files selected for processing (5)
  • quaint/src/visitor/postgres.rs
  • query-compiler/query-builders/sql-query-builder/src/read.rs
  • query-engine/connector-test-kit-rs/query-engine-tests/tests/queries/data_types/native/postgres.rs
  • schema-engine/connectors/sql-schema-connector/src/flavour/postgres/renderer.rs
  • schema-engine/sql-migration-tests/tests/migrations/enums.rs

Comment on lines +545 to +546
api.raw_cmd(r#"INSERT INTO "Test" ("positions") VALUES (ARRAY['First', 'Second']::"Position"[])"#);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🔵 Trivial | ⚡ Quick win

Assert that enum-array values survive the migration.

The test seeds positions but only verifies that the migration applies. Query the row after 02remove and assert that ["First", "Second"] is unchanged; otherwise a lossy cast could pass this test.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@schema-engine/sql-migration-tests/tests/migrations/enums.rs` around lines 545
- 546, Extend the migration test around the enum-array insert and the `02remove`
migration to query the seeded `Test` row afterward and assert that `positions`
remains `["First", "Second"]` in the original order. Keep the existing
migration-application assertion, and add the value-preservation check
specifically after `02remove`.

@github-actions

Copy link
Copy Markdown
Contributor

Wasm Query Compiler File Size

Engine This PR Base branch Diff
Postgres 3.508MiB 3.508MiB 146.000B
Postgres (gzip) 1.143MiB 1.143MiB -35.000B
Postgres (size-optimized) 1.767MiB 1.767MiB 163.000B
Postgres (size-optimized, gzip) 695.636KiB 695.607KiB 30.000B
Mysql 3.461MiB 3.461MiB 25.000B
Mysql (gzip) 1.128MiB 1.128MiB -27.000B
Mysql (size-optimized) 1.737MiB 1.737MiB 23.000B
Mysql (size-optimized, gzip) 684.340KiB 684.341KiB -1.000B
Sqlite 3.378MiB 3.378MiB 25.000B
Sqlite (gzip) 1.099MiB 1.099MiB -18.000B
Sqlite (size-optimized) 1.693MiB 1.693MiB 23.000B
Sqlite (size-optimized, gzip) 667.390KiB 667.394KiB -4.000B
SQL Server 3.596MiB 3.596MiB 25.000B
SQL Server (gzip) 1.161MiB 1.161MiB -23.000B
SQL Server (size-optimized) 1.766MiB 1.766MiB 23.000B
SQL Server (size-optimized, gzip) 698.438KiB 698.453KiB -15.000B
CockroachDB 3.558MiB 3.558MiB 146.000B
CockroachDB (gzip) 1.162MiB 1.162MiB -33.000B
CockroachDB (size-optimized) 1.793MiB 1.792MiB 163.000B
CockroachDB (size-optimized, gzip) 705.682KiB 705.559KiB 126.000B

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