Skip to content

Fix: Dremio AT BRANCH/TAG/REF/COMMIT dropped in CTAS, CREATE VIEW, INSERT#5

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-version-clause-handling
Closed

Fix: Dremio AT BRANCH/TAG/REF/COMMIT dropped in CTAS, CREATE VIEW, INSERT#5
Copilot wants to merge 2 commits intomainfrom
copilot/fix-version-clause-handling

Conversation

Copy link

Copilot AI commented Mar 3, 2026

format_statement extracted the Dremio version clause correctly but only forwarded it to the Statement::Query arm. The CTAS, CREATE VIEW, and INSERT arms all called their inner query formatters with version: None, silently dropping the clause and changing query semantics.

Changes

  • src/format/sql/mod.rs: Thread version: Option<DremioVersionClause> through format_create_table_with_query, format_create_view, and format_insert; pass it to the inner format_query/format_query_with_layout_preference call instead of None; update all three call sites in format_statement
  • fixtures/dremio/out/12.sql: Correct expected output to preserve AT COMMIT 'a1b2c3d4' in the CTAS statement
  • fixtures/dremio/in/17.sql / out/17.sql: New regression fixtures covering all four affected statement types
  • Tests: Three new unit tests — preserves_version_clause_in_ctas, preserves_version_clause_in_create_view, preserves_version_clause_in_insert_select

Example

Before:

-- Input
CREATE TABLE snap AS SELECT * FROM sales.orders AT COMMIT 'a1b2c3d4';

-- Output (wrong — clause silently dropped)
CREATE TABLE snap AS
SELECT *
FROM sales.orders;

After:

-- Output (correct)
CREATE TABLE snap AS
SELECT *
FROM sales.orders
AT COMMIT 'a1b2c3d4';
Original prompt

Summary

Fixes #2.

sqlchisel drops Dremio versioned table-reference clauses (AT BRANCH|TAG|REF|COMMIT, optional AS OF TIMESTAMP) when the source query is nested inside a CREATE TABLE ... AS, CREATE VIEW ... AS, or INSERT INTO ... SELECT statement. Top-level SELECT already works correctly.

This changes query semantics and violates the expected behavior that formatting should never change what SQL does.


Root Cause

In src/parser.rs, strip_dremio_version_clause correctly extracts the version clause and stores it in ParsedStatement::Sql { version: Some(...), .. }. However, in src/format/sql/mod.rs, format_statement only threads version through to format_query for the top-level Statement::Query arm. The CTAS, CREATE VIEW, and INSERT arms all call their inner query formatters with version: None, silently dropping the clause.

Broken call sites (all pass version: None today):

  • format_create_table_with_queryformat_query_with_layout_preference(..., None, ...)
  • format_create_viewformat_query(..., None, ...)
  • format_insertformat_query(..., None, ...)

Required Changes

1. src/format/sql/mod.rs

Thread the version: Option<DremioVersionClause> parameter through the three broken formatter functions:

  • format_create_table_with_query: Add version: Option<DremioVersionClause> parameter. Pass it to format_query_with_layout_preference instead of None. Update its call site in format_statement to pass version.
  • format_create_view: Add version: Option<DremioVersionClause> parameter. Pass it to format_query instead of None. Update its call site in format_statement to pass version.
  • format_insert: Add version: Option<DremioVersionClause> parameter. Pass it to format_query instead of None. Update its call site in format_statement to pass version.

The version value is already available in format_statement — it is extracted from ParsedStatement::Sql { version, .. } and currently only used for the Statement::Query arm. It must also be passed to the above three arms.

2. fixtures/dremio/out/12.sql

The existing CTAS fixture (fixtures/dremio/in/12.sql) contains AT COMMIT 'a1b2c3d4' but fixtures/dremio/out/12.sql drops it. Update the expected output to preserve the clause:

Current (wrong):

CREATE TABLE demoCatalog.sales.snapshot_orders AS
SELECT * FROM demoCatalog.sales."orders_history";

Expected (correct):

CREATE TABLE demoCatalog.sales.snapshot_orders AS
SELECT *
FROM demoCatalog.sales."orders_history"
AT COMMIT 'a1b2c3d4';

3. New regression fixtures

Add fixtures/dremio/in/13.sql and fixtures/dremio/out/13.sql covering all four statement types.

fixtures/dremio/in/13.sql:

CREATE TABLE a AS SELECT * FROM s.t AT TAG v1;
CREATE VIEW v AS SELECT * FROM s.t AT TAG v1;
INSERT INTO a SELECT * FROM s.t AT TAG v1;
SELECT * FROM s.t AT TAG v1;

fixtures/dremio/out/13.sql: The correctly-formatted version preserving all version clauses.

4. Tests

Add regression tests (unit or integration) verifying that format_sql with DialectKind::Dremio preserves the version clause in:

  • CTAS (CREATE TABLE ... AS SELECT ... AT TAG)
  • CREATE VIEW AS (with AT TAG)
  • INSERT INTO ... SELECT (with AT TAG)
  • Top-level SELECT (already passing — add a test to prevent regression)

Acceptance Criteria

  • AT BRANCH|TAG|REF|COMMIT and AS OF TIMESTAMP are preserved in: CTAS, CREATE VIEW AS, INSERT ... SELECT, and top-level SELECT.
  • fixtures/dremio/out/12.sql is corrected.
  • New regression fixtures fixtures/dremio/in/13.sql and fixtures/dremio/out/13.sql are added.
  • Tests pass for all four statement types.
  • No existing tests are broken.

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…statements

Co-authored-by: jhklarcher <1714393+jhklarcher@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dropping of Dremio version clauses in nested queries Fix: Dremio AT BRANCH/TAG/REF/COMMIT dropped in CTAS, CREATE VIEW, INSERT Mar 3, 2026
@jhklarcher jhklarcher closed this Mar 12, 2026
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.

[BUG] Preserve Dremio AT BRANCH|TAG|REF|COMMIT clauses in CTAS/CREATE VIEW/INSERT

2 participants