forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48549][SQL][PYTHON] Improve SQL function
sentences
### What changes were proposed in this pull request? The pr aims to improve SQL function `sentences`, includes: - update the description of the `sentences` expression to make it more realistic. - add `def sentences(string: Column, language: Column): Column` to SQL functions - `codegen` support for `sentences` ### Why are the changes needed? Fix inconsistency in using the function `sentences` in the following scenarios <img width="1051" alt="image" src="https://github.com/apache/spark/assets/15246973/033c731d-5a2f-455f-8517-ed95bd6c1f6e"> - According to the definition of function `sentences`, we should only allow the following two kinds of parameter calls: A.sentences(str) B.sentences(str, language, country) - the parameters `language` and `country` either coexist or do not exist at the same time **In file `sql/core/src/main/scala/org/apache/spark/sql/functions.scala`, only the following two functions are defined**: https://github.com/apache/spark/blob/f4434c36cc4f7b0147e0e8fe26ac0f177a5199cd/sql/core/src/main/scala/org/apache/spark/sql/functions.scala#L4273-L4282 - When we directly call the expression `sentences`, it actually supports the following: A.`df.select(sentences($"str", $"language", $"country"))`; B.`df.select(sentences($"str", $"language"))`; C.`df.select(sentences($"str"))`; ## Let's align it ### Does this PR introduce _any_ user-facing change? Yes, allow calling SQL function `sentences` as parameters (`str`, `language`). ### How was this patch tested? - Add new UT & Update existed UT. - Pass GA. - Manually check ```scala scala> val df = Seq(("Hi there! The price was $1,234.56.... But, not now.", "en", "US")).toDF("str", "language", "country"); val df: org.apache.spark.sql.DataFrame = [str: string, language: string ... 1 more field] scala> df.select(sentences($"str", $"language", $"country")); val res0: org.apache.spark.sql.DataFrame = [sentences(str, language, country): array<array<string>>] scala> df.select(sentences($"str", $"language")); val res1: org.apache.spark.sql.DataFrame = [sentences(str, language, ): array<array<string>>] scala> df.select(sentences($"str")); val res2: org.apache.spark.sql.DataFrame = [sentences(str, , ): array<array<string>>] scala> df.selectExpr("sentences(str, language, country)"); val res3: org.apache.spark.sql.DataFrame = [sentences(str, language, country): array<array<string>>] scala> df.selectExpr("sentences(str, language)"); val res4: org.apache.spark.sql.DataFrame = [sentences(str, language, ): array<array<string>>] scala> df.selectExpr("sentences(str)"); val res5: org.apache.spark.sql.DataFrame = [sentences(str, , ): array<array<string>>] ``` ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#46880 from panbingkun/sentences_improve. Authored-by: panbingkun <panbingkun@baidu.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
- Loading branch information
1 parent
a9502d4
commit e037953
Showing
15 changed files
with
187 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
2 changes: 1 addition & 1 deletion
2
sql/connect/common/src/test/resources/query-tests/explain-results/function_sentences.explain
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Project [sentences(g#0, , ) AS sentences(g, , )#0] | ||
Project [static_invoke(ExpressionImplUtils.getSentences(g#0, , )) AS sentences(g, , )#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |
2 changes: 2 additions & 0 deletions
2
...n/src/test/resources/query-tests/explain-results/function_sentences_with_language.explain
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Project [static_invoke(ExpressionImplUtils.getSentences(g#0, en, )) AS sentences(g, en, )#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |
2 changes: 2 additions & 0 deletions
2
...esources/query-tests/explain-results/function_sentences_with_language_and_country.explain
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Project [static_invoke(ExpressionImplUtils.getSentences(g#0, en, US)) AS sentences(g, en, US)#0] | ||
+- LocalRelation <empty>, [id#0L, a#0, b#0, d#0, e#0, f#0, g#0] |
2 changes: 0 additions & 2 deletions
2
...mon/src/test/resources/query-tests/explain-results/function_sentences_with_locale.explain
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
...nnect/common/src/test/resources/query-tests/queries/function_sentences_with_language.json
This file contains 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"common": { | ||
"planId": "1" | ||
}, | ||
"project": { | ||
"input": { | ||
"common": { | ||
"planId": "0" | ||
}, | ||
"localRelation": { | ||
"schema": "struct\u003cid:bigint,a:int,b:double,d:struct\u003cid:bigint,a:int,b:double\u003e,e:array\u003cint\u003e,f:map\u003cstring,struct\u003cid:bigint,a:int,b:double\u003e\u003e,g:string\u003e" | ||
} | ||
}, | ||
"expressions": [{ | ||
"unresolvedFunction": { | ||
"functionName": "sentences", | ||
"arguments": [{ | ||
"unresolvedAttribute": { | ||
"unparsedIdentifier": "g" | ||
} | ||
}, { | ||
"literal": { | ||
"string": "en" | ||
} | ||
}] | ||
} | ||
}] | ||
} | ||
} |
Binary file added
BIN
+186 Bytes
.../common/src/test/resources/query-tests/queries/function_sentences_with_language.proto.bin
Binary file not shown.
File renamed without changes.
File renamed without changes.
This file contains 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