Skip to content

Snowflake: Fix COPY INTO transformation parsing for cast expressions#2328

Open
97nitt wants to merge 3 commits intoapache:mainfrom
97nitt:upstream/copy-into-dollar-col-cast
Open

Snowflake: Fix COPY INTO transformation parsing for cast expressions#2328
97nitt wants to merge 3 commits intoapache:mainfrom
97nitt:upstream/copy-into-dollar-col-cast

Conversation

@97nitt
Copy link
Copy Markdown

@97nitt 97nitt commented Apr 28, 2026

Summary

Snowflake COPY INTO ... FROM (SELECT ...) transformation lists support cast
expressions like $1:"col"::TYPE and $1::TYPE, e.g.:

COPY INTO my_company.emp_basic (a)
FROM (SELECT $1:"A"::NUMBER(38, 0) FROM @stg);

Today the Snowflake-specific stage-load-select-item parser successfully
consumes $1:"A" as a stage-load-select-item, leaves ::NUMBER(38, 0) in
the token stream, and the surrounding COPY INTO body then fails with
Expected: FROM, found: ::.

Changes

  • parse_select_item_for_data_load: bail (so maybe_parse rewinds and the
    caller falls through to the generic parse_select_item) when the next
    token is ::. The generic path already handles cast operators on $<n>
    references correctly.
  • Tests: regression cases for $1:"col"::TYPE, $1::TYPE,
    $1:SEQUENCE::TYPE, multi-column lists, and a mixed list combining a
    plain stage-load-select-item with a cast item — verifying we don't
    over-correct and break the existing shape.

Test plan

  • cargo test
  • cargo fmt --check
  • cargo clippy --all-targets --all-features -- -D warnings

Comment thread tests/sqlparser_snowflake.rs Outdated
Comment on lines +2452 to +2461
// Snowflake `COPY INTO` transformation lists support casts like
// `$1:"col"::TYPE` and `$1::TYPE`. These are distinct from the bare
// `$<n>[.<col>][:<elem>]` stage-load-select-item shape, so the parser
// must fall through to the generic select-item parser to handle the
// `::` cast operator.
//
// Regression: the Snowflake-specific parser used to successfully
// consume `$1:"col"` as a stage-load-select-item, leaving `::TYPE`
// behind and causing "Expected: FROM, found: ::" once the COPY INTO
// body tried to expect `FROM`.
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.

Suggested change
// Snowflake `COPY INTO` transformation lists support casts like
// `$1:"col"::TYPE` and `$1::TYPE`. These are distinct from the bare
// `$<n>[.<col>][:<elem>]` stage-load-select-item shape, so the parser
// must fall through to the generic select-item parser to handle the
// `::` cast operator.
//
// Regression: the Snowflake-specific parser used to successfully
// consume `$1:"col"` as a stage-load-select-item, leaving `::TYPE`
// behind and causing "Expected: FROM, found: ::" once the COPY INTO
// body tried to expect `FROM`.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread src/dialect/snowflake.rs Outdated
Comment on lines +1549 to +1551
// `$1:"col"::NUMBER(38,0)`), not a stage-load-select-item. Bail so
// `maybe_parse` rewinds and the caller falls through to
// `parse_select_item`, which handles the cast correctly.
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.

Suggested change
// `$1:"col"::NUMBER(38,0)`), not a stage-load-select-item. Bail so
// `maybe_parse` rewinds and the caller falls through to
// `parse_select_item`, which handles the cast correctly.
// `$1:"col"::NUMBER(38,0)`), not a stage-load-select-item.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread tests/sqlparser_snowflake.rs Outdated
Comment on lines +2479 to +2480
// Mix with an ordinary stage-load-select-item in the same list,
// so we don't over-correct and break the existing shape.
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.

Suggested change
// Mix with an ordinary stage-load-select-item in the same list,
// so we don't over-correct and break the existing shape.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread tests/sqlparser_snowflake.rs Outdated
),
];
for sql in variants {
snowflake().parse_sql_statements(sql).unwrap_or_else(|e| {
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.

these should use verified_stmt?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

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.

2 participants