Skip to content

fix: Supporting two argument flavor of REPLACE function in Snowflake and Presto dialects #5289

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

Merged

Conversation

rahulj51
Copy link
Contributor

Both Snwoflake and Presto support a two argument version of REPLACE with the third argument being optional. This breaks during transpilation to other dialects and needs to be handled. This fix adds the default value for the third argument for these two dialects.

Also added unit tests for validations.

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

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

This looks good, thank you @rahulj51! Just a suggestion & a question.

@@ -2223,6 +2223,35 @@ def test_regexp_replace(self, logger):
},
)

@mock.patch("sqlglot.generator.logger")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why was this needed?

@@ -166,6 +166,15 @@ def _build_regexp_replace(args: t.List) -> exp.RegexpReplace:
return regexp_replace


def _build_replace(args: t.List) -> exp.Replace:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't the logic the same as Presto? Why do something different for Snowflake? I'd suggest extracting the Presto logic out into a helper within dialect.py and then importing it in both dialects, so that it can be reused elsewhere if needed.

@georgesittas
Copy link
Collaborator

I'll get this in and take it to the finish line, thanks!

@georgesittas georgesittas merged commit 0ff95c5 into tobymao:main Jun 26, 2025
7 checks passed
@rahulj51
Copy link
Contributor Author

I'll get this in and take it to the finish line, thanks!

Perfect. Thanks.

@georgesittas
Copy link
Collaborator

aa5f806

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