-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(duckdb): fix for Snowflake transpilation issue related to PIVOT and string literal column names #7660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fivetran-kwoodbeck
wants to merge
6
commits into
main
Choose a base branch
from
transpile/snowflake-pivot-string-literal-column-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−4
Open
fix(duckdb): fix for Snowflake transpilation issue related to PIVOT and string literal column names #7660
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f8e69c8
fix for pivot-string-literal-column-names bug
fivetran-kwoodbeck c0fdbfb
updated implementation to set column aliases
fivetran-kwoodbeck 02a828d
switched fix over to optimizer/qualify
fivetran-kwoodbeck e083b31
hooked into pivot_sql in generator
fivetran-kwoodbeck a9f6778
revert makefile changes
fivetran-kwoodbeck 2919fab
inject IN-list aliases
fivetran-kwoodbeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule sqlglot-integration-tests
updated
from b18cff to 273424
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2459,6 +2459,77 @@ def tablesample_sql( | |
|
|
||
| return f" {tablesample_keyword or self.TABLESAMPLE_KEYWORDS} {method}{expr}{seed}" | ||
|
|
||
| def _pivot_in_value_aliases(self, expression: exp.Pivot) -> list[exp.Expression] | None: | ||
| # Returns the rewritten field.expressions list with PivotAlias wrappers injected where | ||
| # the stored column name differs from the target dialect's natural output. | ||
| columns = expression.args.get("columns") | ||
| if not columns or len(expression.fields) != 1: | ||
| return None | ||
|
|
||
| parser_cls = self.dialect.parser_class | ||
| # if the source and target emit identical values, exit early | ||
| if ( | ||
| expression.args.get("identify_pivot_strings") == parser_cls.IDENTIFY_PIVOT_STRINGS | ||
| and expression.args.get("prefixed_pivot_columns") == parser_cls.PREFIXED_PIVOT_COLUMNS | ||
| and expression.args.get("pivot_column_naming") == parser_cls.PIVOT_COLUMN_NAMING | ||
| ): | ||
| return None | ||
|
|
||
| in_exprs = expression.fields[0].expressions | ||
| step = len(columns) // len(in_exprs) | ||
|
|
||
| # Derive the per-value suffix from the first stored column vs the first IN-list value. | ||
| # This correctly handles dialects (e.g. Spark single-agg) that ignore agg aliases. | ||
| source_identify = expression.args.get("identify_pivot_strings", False) | ||
| first_base = in_exprs[0].sql() if source_identify else in_exprs[0].alias_or_name | ||
| first_stored = columns[0].name | ||
|
|
||
| # exit if only suffix matches, not prefix. (e.g. BigQuery, which cannot be fixed) | ||
| if not first_stored.lower().startswith(first_base.lower()): | ||
| # Should we emit an unsupported here? | ||
| return None | ||
| suffix = first_stored[len(first_base) :] | ||
|
|
||
| target_identify = parser_cls.IDENTIFY_PIVOT_STRINGS | ||
| target_naming = parser_cls.PIVOT_COLUMN_NAMING | ||
|
|
||
| # Whether the target dialect would append an agg-name suffix for this pivot. | ||
| # Spark single-agg uniquely drops the agg alias entirely. | ||
| target_has_suffix = (len(expression.expressions) > 1 or target_naming != "spark") and any( | ||
| getattr(a, "alias", None) for a in expression.expressions | ||
| ) | ||
| source_has_suffix = suffix != "" | ||
|
|
||
| new_exprs: list[exp.Expression] = [] | ||
| modified = False | ||
| for val_idx, e in enumerate(in_exprs): | ||
| i = val_idx * step | ||
| stored_full = columns[i].name | ||
| stored_value = stored_full[: -len(suffix)] if suffix else stored_full | ||
| target_value = e.sql() if target_identify else e.alias_or_name | ||
|
|
||
| if isinstance(e, exp.PivotAlias): | ||
| new_exprs.append(e) | ||
| continue | ||
|
|
||
| # Source had a suffix, target won't apply one (e.g. DuckDB→Spark single-agg | ||
| # aliased): inject the full stored column name as the IN-list alias so the | ||
| # target uses it verbatim as the column name. | ||
| if source_has_suffix and not target_has_suffix: | ||
| new_exprs.append( | ||
| exp.PivotAlias(this=e, alias=exp.to_identifier(stored_full, quoted=True)) | ||
| ) | ||
| modified = True | ||
| # Value-part mismatch (e.g. Snowflake's literal-style values vs others). | ||
| elif stored_value.lower() != target_value.lower(): | ||
| new_exprs.append( | ||
| exp.PivotAlias(this=e, alias=exp.to_identifier(stored_value, quoted=True)) | ||
| ) | ||
| modified = True | ||
|
Comment on lines
+2519
to
+2528
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is |
||
| else: | ||
| new_exprs.append(e) | ||
| return new_exprs if modified else None | ||
|
|
||
| def pivot_sql(self, expression: exp.Pivot) -> str: | ||
| expressions = self.expressions(expression, flat=True) | ||
| direction = "UNPIVOT" if expression.unpivot else "PIVOT" | ||
|
|
@@ -2478,6 +2549,12 @@ def pivot_sql(self, expression: exp.Pivot) -> str: | |
| sql = f"{direction} {this}{on}{into}{using}{group}" | ||
| return self.prepend_ctes(expression, sql) | ||
|
|
||
| if not expression.unpivot: | ||
| # Wrap IN-list values with explicit aliases where the target dialect would differ | ||
| new_field_exprs = self._pivot_in_value_aliases(expression) | ||
| if new_field_exprs is not None: | ||
| expression.fields[0].set("expressions", new_field_exprs) | ||
|
|
||
| alias = self.sql(expression, "alias") | ||
| alias = f" AS {alias}" if alias else "" | ||
|
|
||
|
|
||
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.