Skip to content
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

fix(spark): Custom annotation for SUBSTRING() #4004

Merged
merged 3 commits into from
Aug 29, 2024

Conversation

VaggelisD
Copy link
Collaborator

@VaggelisD VaggelisD commented Aug 29, 2024

Fixes #4002

Could not test HiveQL but the docs mention only STRING as the return type, so this PR is limited to Spark2+:

  • Spark 2:
scala> spark.sql("select substring(cast('thing' as binary), 1, 2), substring('thing', 1, 2)")
res4: org.apache.spark.sql.DataFrame = [substring(CAST(thing AS BINARY), 1, 2): binary, substring(thing, 1, 2): string]
  • Spark 3:
spark-sql (default)> select typeof(substring('thing', 1, 2)), typeof(substring(cast('thing' as binary), 1, 2));
string  binary
  • Databricks
select typeof(substring(cast('thing' as binary), 1, 2)) as bin, typeof(substring('thing', 1, 2)) as str; 

bin	str
binary	string

Docs

Databricks SUBSTRING | HiveQL Builtin Functions

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.

LGTM, nice 👍

sqlglot/dialects/spark2.py Outdated Show resolved Hide resolved
tests/test_optimizer.py Outdated Show resolved Hide resolved
@georgesittas georgesittas merged commit 4b7ca2b into main Aug 29, 2024
6 checks passed
@georgesittas georgesittas deleted the vaggelisd/spark_substr branch August 29, 2024 13:09
hellozepp pushed a commit to hellozepp/sqlglot-doris that referenced this pull request Sep 3, 2024
* fix(spark): Custom annotation for SUBSTRING()

* Remove empty line

* PR Feedback 1
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.

Substring type inference missing binary case
2 participants