Add PostgreSQL partial-column inserts with composable CTE support - #827
Draft
kushagarr wants to merge 12 commits into
Draft
Add PostgreSQL partial-column inserts with composable CTE support#827kushagarr wants to merge 12 commits into
kushagarr wants to merge 12 commits into
Conversation
- Introduced `pgWithSyntax` to prefix PostgreSQL statements with CTEs. - Added new types for data-modifying CTEs: `PgDataModifyingCommonTableExpressionSyntax` and `PgCommonTableExpressionSyntax`. - Implemented type classes for handling CTEs in SQL queries, ensuring correct placement of data-modifying CTEs. - Created unit and integration tests for rendering and type safety of CTEs, including negative tests for invalid placements. - Updated documentation to reflect new CTE capabilities and usage examples. - Modified test setup to use PostgreSQL version 18.4 for better compatibility.
- Updated CTE handling in tests to reflect PostgreSQL's behavior with zero-column CTEs and side-effect-only CTEs. - Added new tests for materialization execution and rendering, ensuring compliance with PostgreSQL 12+ features. - Refined error handling for invalid CTE placements, particularly for side-effect-only operations. - Improved documentation on PostgreSQL-specific CTE usage, including materialization options and nested CTEs. - Adjusted dependencies in the SQLite library for compatibility. - Ensured reproducibility of CI runs by pinning PostgreSQL server version.
…mn inserts in PostgreSQL
Contributor
Author
|
@LaurentRDC could you check this please. |
Member
|
I haven't forgotten about this; I'm just swamped at work right now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds PostgreSQL-specific partial-column inserts that preserve
ON CONFLICTsupport and compose cleanly withRETURNINGand data-modifying CTEs.Motivation
The existing PostgreSQL insert helpers support
insertValues,insertExpressions, andinsertFrom, but require values for every table column. This prevents CTE inserts from omitting generated, identity, or database-defaulted columns.Beam’s backend-independent
insertOnlysupports partial-column inserts, but cannot express PostgreSQL’sON CONFLICTbehavior.Changes
pgInsertOnlyfor partial-column PostgreSQL inserts withPgInsertOnConflict.pgCteInsertto lift anySqlInsert Postgres tableinto a side-effect-only CTE.pgCteInsertReturningto attach aRETURNINGprojection and expose the inserted rows as a reusable CTE.insert,insertReturning,cteInsert, andcteInsertReturningAPIs.beam-postgres-0.6.3.1.BEAM_POSTGRES_TEST_CONNSTR.Design
Partial-column selection is treated as a property of insert construction, while conflict handling,
RETURNING, and CTE placement remain independently composable:pgInsertOnlydelegates target-column projection and base SQL generation to Beam’s existinginsertOnlyimplementation, then adds PostgreSQL’s conflict clause. This avoids maintaining a second insert renderer and keeps column ordering, quoting, empty-input behavior, and type checking aligned with the backend-independent implementation.The command-level CTE adapters also avoid introducing narrowly specialized APIs such as
pgCteInsertOnly.Compatibility
This is additive and does not require changes to existing user code. Existing PostgreSQL insert and CTE helpers retain their signatures and behavior.
pgCteInsertReturningreturnsNothingonly forSqlInsertNoRows. A validON CONFLICT DO NOTHINGcommand still returns a reusable relation, which may contain no rows after execution.Validation
beam-postgrestest suite: 149 tests passed against PostgreSQL 18.4.insertDataandinsertFrom;ON CONFLICT DO NOTHING;ON CONFLICT DO UPDATEandexcludedvalues;git diff --checkpasses.