You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "FurcyPin (via GitHub)" <gi...@apache.org> on 2023/03/03 17:31:17 UTC

[GitHub] [spark] FurcyPin opened a new pull request, #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

FurcyPin opened a new pull request, #40271:
URL: https://github.com/apache/spark/pull/40271

   
   
   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   
   In the `pyspark.sql.functions`, we replaced `from typing import foo, bar, etc` with `import typing` and all
   uses of `foo`, `bar` and `etc` in that module were replaced with `typing.foo`, `typing.bar` and `typing.etc`.
   
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   
   Exposing methods from `typing` inside `spark.sql.functions` could lead to confusing errors on the user side, like in this example:
   ```
   from pyspark.sql import SparkSession
   from pyspark.sql import functions as f
   
   spark = SparkSession.builder.getOrCreate()
   df = spark.sql("""SELECT 1 as a""")
   df.withColumn("a", f.cast("STRING", f.col("a"))).printSchema()  
   ```
   The code above runs without raising any error but yields an incorrect result:
   ```
   root
   |-- a: integer (nullable = false)
   ```
   
   This is because `f.cast` is in fact `typing.cast` and we should have used `f.col("a").cast("STRING")` instead.
   
   
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   Yes, if a user has made the mistake described in the example above, their code that did run (with an incorrect behaviour) will instead break after they upgrade to a version containing this change. 
   But one could argue that it would be a good thing, since it will expose a mistake in their code.
   
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   No new test were added.
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] itholic commented on a diff in pull request #40271: SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1130262880


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   I don't think there's a concern for the name collision with `typing.cast` because `functions.cast` is not a function specified in the [PySpark API Reference](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/functions.html).
   
   However, I also sort of agree that users might get confused and expect a `functions.cast` to exist since it's a function name that is generally expected to be supported (and anyway it is also supported in the Column API...) based on the example you provided in the PR description.
   
   At the moment, I am not 100% sure which direction is the right based on my own thoughts, so let's gather some more opinions from others.
   
   WDYT, @HyukjinKwon @ueshin @zhenglaizhang ??



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] itholic commented on a diff in pull request #40271: SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1127452614


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   Oh,, yeah I thought we have `functions.cast`.
   
   Hmm... If `functions.cast` does not exist, I'm not sure if removing the duplication of `typing.cast` in advance would be appropriate.
   
   WDYT, @HyukjinKwon ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] github-actions[bot] closed pull request #40271: SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #40271: SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast
URL: https://github.com/apache/spark/pull/40271


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] FurcyPin commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "FurcyPin (via GitHub)" <gi...@apache.org>.
FurcyPin commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1126999170


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   I apologize, but I am not sure I understand what you mean... it sounds like you think that `functions.cast` already exists. It does not, there is only `Column.cast`. This is what this test does: it checks that `functions.cast` does not exists.
   
   We _could_ add a `functions.cast` method, but I don't think we should because 
   1. the need is already covered by `Column.cast`
   2. that would require to add it to the Scala/Java API to be consistent



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] itholic commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1124915899


##########
python/pyspark/sql/functions.py:
##########
@@ -22,20 +22,10 @@
 import sys
 import functools
 import warnings
-from typing import (
-    Any,
-    cast,

Review Comment:
   How about just fix the `cast` instead of fixing all imports and add a test to verify it ??
   
   IMHO most of the other functions except `cast` seem to be names that are very unlikely to be used repeatedly.
   
   Another concern is that we're using the typing module most of our PySpark files this way, so I don't know maybe if we need to fix all the other files for consistency(but which seems to be too much work).
   
   ```shell
   spark$ git grep "from typing import" python/pyspark | wc -l
        202
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] github-actions[bot] commented on pull request #40271: SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40271:
URL: https://github.com/apache/spark/pull/40271#issuecomment-1595904320

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] itholic commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1125771590


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   How about add a `test_cast` with practical cases?
   
   Seems like the test for `functions.cast` is missing (And that's also the root reason why we haven't noticed this is wrong until now)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] itholic commented on pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on PR #40271:
URL: https://github.com/apache/spark/pull/40271#issuecomment-1455275958

   Looks good otherwise.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] itholic commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "itholic (via GitHub)" <gi...@apache.org>.
itholic commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1125771590


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   How about add a `test_cast`?
   
   Seems like the test for `functions.cast` is missing (And that's also the root reason why we haven't noticed this is wrong until now)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] FurcyPin commented on a diff in pull request #40271: SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "FurcyPin (via GitHub)" <gi...@apache.org>.
FurcyPin commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1127557744


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   From what I could test, the current behavior when using `typing.cast` is that it has no effect. The SQL cast does not happen, obviously, but no exception is raised either.
   Removing `typing.cast` will break jobs that are currently using it. This can be seen as a regression, but since it breaks a buggy behaviour, it could also be seen as a bugfix ?
   
   I see 4 possible alternatives here:
   1. Remove `typing.cast` (the current commit)
   2. Add a Python version of `functions.cast` with the right behaviour that uses `Column.cast`
   3. Replace the `typing.cast` call with a dummy `functions.cast` that logs a warning
   4. Add `functions.cast` to the Scala/Java API then the Python one (and probably R too ?)
   
   All options have their upsides and downsides:
   
   1. Already implemented, consistent with Scala API, but will crash jobs that mistakenly used `functions.cast`
   2. Simple to implement, inconsistent with Scala API, might silently change the behaviour of jobs that mistakenly used `functions.cast` (because the behaviour will change from "no effect" to "actual cast")
   3. Simple to implement, consistent with Scala API, will not crash jobs, only issue warnings (that users might or might not see)
   4. Longest to implement, consistent with Scala API, might silently change the behaviour of jobs that mistakenly used `functions.cast` (because the behaviour will change from "no effect" to "actual cast")
   
   Personally, I think 1. is okay, but it's your choice.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] FurcyPin commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "FurcyPin (via GitHub)" <gi...@apache.org>.
FurcyPin commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1126999170


##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -1268,6 +1268,12 @@ def test_bucket(self):
             message_parameters={"arg_name": "numBuckets", "arg_type": "str"},
         )
 
+    def test_no_cast(self):

Review Comment:
   I apologize, but I am not sure I understand what you mean... it sounds like you think that `functions.cast` already exists.
   It does not, there is only `Column.cast`.
   
   We _could_ add a `functions.cast` method, but I don't think we should because 
   1. the need is already covered by `Column.cast`
   2. that would require to add it to the Scala/Java API to be consistent



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] FurcyPin commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "FurcyPin (via GitHub)" <gi...@apache.org>.
FurcyPin commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1125698656


##########
python/pyspark/sql/functions.py:
##########
@@ -22,20 +22,10 @@
 import sys
 import functools
 import warnings
-from typing import (
-    Any,
-    cast,

Review Comment:
   In the end, I went for `from typing import cast as _cast` which makes the intent even more explicit, I think.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] FurcyPin commented on a diff in pull request #40271: [WIP][SPARK-42258][PYTHON] pyspark.sql.functions should not expose typing.cast

Posted by "FurcyPin (via GitHub)" <gi...@apache.org>.
FurcyPin commented on code in PR #40271:
URL: https://github.com/apache/spark/pull/40271#discussion_r1125695676


##########
python/pyspark/sql/functions.py:
##########
@@ -22,20 +22,10 @@
 import sys
 import functools
 import warnings
-from typing import (
-    Any,
-    cast,

Review Comment:
   I agree that the change seemed quite cumbersome. It made me wish Python had some kind of "private import" keyword to handle such cases more easily.
   
   I agree with you that `cast` is the only name that might be confusing (perhaps `overload` too, but all the other names start with an uppercase). The `functions` module feels a little special to me because it is the module I use the most as a Spark user, it's definitely a public API. The 201 other modules don't require such change.
   
   It's out of the scope of this MR, but perhaps for the long term you could consider reorganizing this module [the same way as I did in one of my projects](https://github.com/FurcyPin/spark-frame/blob/main/spark_frame/transformations.py), which had two advantages:
   - the code of each method was isolated in a separate file (that would prevent having a 10 000-lines files)
   - there was no import pollution
   
   For now, I'll do as you suggest: only handle `typing.cast` as a special case and add a unit test to make sure it does not get imported again.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org