You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "PetarVasiljevic-DB (via GitHub)" <gi...@apache.org> on 2024/01/16 17:51:35 UTC

[PR] [SPARK-46725] Add DAYNAME function [spark]

PetarVasiljevic-DB opened a new pull request, #44758:
URL: https://github.com/apache/spark/pull/44758

   <!--
   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?
   Added DAYNAME function that returns three letter abbreviation day name for the specified date to:
   - Scala API
   - Python API
   - R API
   - Spark Connect Scala Client
   - Spark Connect Python Client
   
   
   ### Why are the changes needed?
   <!--
   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.
   -->
   For parity with Snowflake
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   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'.
   -->
   Yes, since new function DAYNAME is added
   
   ### How was this patch tested?
   <!--
   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.
   -->
   Tested on new unit tests
   
   ### Was this patch authored or co-authored using generative AI tooling?
   <!--
   If generative AI tooling has been used in the process of authoring this patch, please include the
   phrase: 'Generated-by: ' followed by the name of the tool and its version.
   If no, write 'No'.
   Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
   -->
   No
   


-- 
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


Re: [PR] [WIP][SPARK-46725][SQL] Add DAYNAME function [spark]

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


##########
R/pkg/R/functions.R:
##########
@@ -1105,6 +1105,20 @@ setMethod("monthname",
             column(jc)
           })
 
+#' @details
+#' \code{dayname}: Extracts the three-letter abbreviated month name from a

Review Comment:
   ```suggestion
   #' \code{dayname}: Extracts the three-letter abbreviated day name from a
   ```



##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -421,6 +421,12 @@ def test_monthname(self):
         row = df.select(F.monthname(df.date)).first()
         self.assertEqual(row[0], "Nov")
 
+    def test_dayname(self):
+            dt = datetime.datetime(2017, 11, 6)
+            df = self.spark.createDataFrame([Row(date=dt)])
+            row = df.select(F.dayname(df.date)).first()
+            self.assertEqual(row[0], "Mon")

Review Comment:
   Please, fix indentation here.



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/DateExpressionsSuite.scala:
##########
@@ -269,6 +269,17 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
     checkConsistencyBetweenInterpretedAndCodegen(MonthName, DateType)
   }
 
+  test("DayName") {
+    checkEvaluation(DayName(Literal.create(null, DateType)), null)
+    checkEvaluation(DayName(Literal(d)), "Wed")
+    checkEvaluation(DayName(Cast(Literal(date), DateType, UTC_OPT)), "Wed")
+    checkEvaluation(DayName(Cast(Literal(ts), DateType, UTC_OPT)), "Fri")
+    checkEvaluation(DayName(Cast(Literal("2011-05-06"), DateType, UTC_OPT)), "Fri")
+    checkEvaluation(DayName(Literal(new Date(toMillis("2017-05-27 13:10:15")))), "Sat")
+    checkEvaluation(DayName(Literal(new Date(toMillis("1582-10-15 13:10:15")))), "Fri")

Review Comment:
   Could you use Java 8+ datetime API, and avoud `new Date`.



##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/sql/functions.scala:
##########
@@ -5960,6 +5960,15 @@ object functions {
   def monthname(timeExp: Column): Column =
     Column.fn("monthname", timeExp)
 
+  /**
+   * Extracts the three-letter abbreviated month name from a given date/timestamp/string.

Review Comment:
   `month`? Please, replace it by `day`.



##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -7303,6 +7303,36 @@ def monthname(col: "ColumnOrName") -> Column:
     return _invoke_function_over_columns("monthname", col)
 
 
+@_try_remote_functions
+def dayname(col: "ColumnOrName") -> Column:
+    """
+    Returns the three-letter abbreviated day name from the given date.
+
+    .. versionadded:: 4.0.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or str
+        target date/timestamp column to work on.
+
+    Returns
+    -------
+    :class:`~pyspark.sql.Column`
+        the three-letter abbreviation of day name for date/timestamp (Mon, Tue, Wed...)
+
+    Examples
+    --------
+    >>> df = spark.createDataFrame([('2015-04-08',)], ['dt'])
+    >>> df.select(dayname('dt').alias('day')).show()
+    +-----+
+    |day|

Review Comment:
   Is it actual output? Seems like something wrong with the table formatting.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala:
##########
@@ -909,6 +909,24 @@ case class MonthName(child: Expression) extends GetDateField {
     copy(child = newChild)
 }
 
+// scalastyle:off line.size.limit

Review Comment:
   If you disable the check, could you enable it back when it is not needed.



-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

Posted by "PetarVasiljevic-DB (via GitHub)" <gi...@apache.org>.
PetarVasiljevic-DB commented on code in PR #44758:
URL: https://github.com/apache/spark/pull/44758#discussion_r1460935150


##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -7303,6 +7303,36 @@ def monthname(col: "ColumnOrName") -> Column:
     return _invoke_function_over_columns("monthname", col)
 
 
+@_try_remote_functions
+def dayname(col: "ColumnOrName") -> Column:
+    """
+    Returns the three-letter abbreviated day name from the given date.
+
+    .. versionadded:: 4.0.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or str
+        target date/timestamp column to work on.
+
+    Returns
+    -------
+    :class:`~pyspark.sql.Column`
+        the three-letter abbreviation of day name for date/timestamp (Mon, Tue, Wed...)
+
+    Examples
+    --------

Review Comment:
   Done



-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

Posted by "MaxGekk (via GitHub)" <gi...@apache.org>.
MaxGekk closed pull request #44758: [SPARK-46725][SQL] Add DAYNAME function
URL: https://github.com/apache/spark/pull/44758


-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #44758:
URL: https://github.com/apache/spark/pull/44758#discussion_r1464253727


##########
R/pkg/R/functions.R:
##########
@@ -1105,6 +1105,20 @@ setMethod("monthname",
             column(jc)
           })
 
+#' @details
+#' \code{dayname}: Extracts the three-letter abbreviated day name from a

Review Comment:
   I'm fine with the function name DAYNAME, but shall we make it clearer in the doc? `day-of-week name` instead of `day name`. This is also what snowflake does: https://docs.snowflake.com/en/sql-reference/functions/dayname



-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

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


##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -7303,6 +7303,36 @@ def monthname(col: "ColumnOrName") -> Column:
     return _invoke_function_over_columns("monthname", col)
 
 
+@_try_remote_functions
+def dayname(col: "ColumnOrName") -> Column:
+    """
+    Returns the three-letter abbreviated day name from the given date.
+
+    .. versionadded:: 4.0.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or str
+        target date/timestamp column to work on.
+
+    Returns
+    -------
+    :class:`~pyspark.sql.Column`
+        the three-letter abbreviation of day name for date/timestamp (Mon, Tue, Wed...)
+
+    Examples
+    --------

Review Comment:
   @PetarVasiljevic-DB Please, address @LuciferYang's comments.



-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

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


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoderSuite.scala:
##########
@@ -248,7 +248,7 @@ class ExpressionEncoderSuite extends CodegenInterpretedPlanTest with AnalysisTes
       useFallback = true)
   }
 
-  object OuterLevelWithVeryVeryVeryLongClassName1 {
+  /* object OuterLevelWithVeryVeryVeryLongClassName1 {

Review Comment:
   Why did you comment it?



-- 
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


Re: [PR] [WIP][SPARK-46725][SQL] Add DAYNAME function [spark]

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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala:
##########
@@ -909,6 +909,23 @@ case class MonthName(child: Expression) extends GetDateField {
     copy(child = newChild)
 }
 
+@ExpressionDescription(
+  usage = "_FUNC_(date) - Returns the three-letter abbreviated day name from the given date.",
+  examples = """
+    Examples:
+      > SELECT _FUNC_('2008-02-20');

Review Comment:
   Let's use a DATE literal explicitly, and don't assume some implicit conversions from strings.
   ```suggestion
         > SELECT _FUNC_(date'2008-02-20');
   ```



-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

Posted by "PetarVasiljevic-DB (via GitHub)" <gi...@apache.org>.
PetarVasiljevic-DB commented on code in PR #44758:
URL: https://github.com/apache/spark/pull/44758#discussion_r1460934891


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoderSuite.scala:
##########
@@ -248,7 +248,7 @@ class ExpressionEncoderSuite extends CodegenInterpretedPlanTest with AnalysisTes
       useFallback = true)
   }
 
-  object OuterLevelWithVeryVeryVeryLongClassName1 {
+  /* object OuterLevelWithVeryVeryVeryLongClassName1 {

Review Comment:
   Building on my machine was failing because of this. Pushed it by accident, resolved 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


Re: [PR] [WIP][SPARK-46725][SQL] Add DAYNAME function [spark]

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


##########
sql/core/src/test/resources/sql-functions/sql-expression-schema.md:
##########
@@ -117,6 +117,7 @@
 | org.apache.spark.sql.catalyst.expressions.DatePartExpressionBuilder | datepart | SELECT datepart('YEAR', TIMESTAMP '2019-08-12 01:00:00.123456') | struct<datepart(YEAR FROM TIMESTAMP '2019-08-12 01:00:00.123456'):int> |
 | org.apache.spark.sql.catalyst.expressions.DateSub | date_sub | SELECT date_sub('2016-07-30', 1) | struct<date_sub(2016-07-30, 1):date> |
 | org.apache.spark.sql.catalyst.expressions.DayOfMonth | day | SELECT day('2009-07-30') | struct<day(2009-07-30):int> |
+| org.apache.spark.sql.catalyst.expressions.DayName | dayname | SELECT dayname(‘2008-02-20') | struct<dayname(2008-02-20):string> |

Review Comment:
   Could you regenerate the file using:
   ```
   $ SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/testOnly *ExpressionsSchemaSuite"
   ```
   seems the test failure is related:
   ```
   sbt.ForkMain$ForkError: org.scalatest.exceptions.TestFailedException: "SELECT day[('2009-07-3]0')" did not equal "SELECT day[name('2008-02-2]0')" SQL query did not match
   	at org.scalatest.Assertions.newAssertionFailedException(Assertions.scala:472)
   ```



-- 
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


Re: [PR] [WIP][SPARK-46725][SQL] Add DAYNAME function [spark]

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


##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -7303,6 +7303,36 @@ def monthname(col: "ColumnOrName") -> Column:
     return _invoke_function_over_columns("monthname", col)
 
 
+@_try_remote_functions
+def dayname(col: "ColumnOrName") -> Column:
+    """
+    Returns the three-letter abbreviated day name from the given date.

Review Comment:
   `Date and Timestamp Function: Returns the three-letter ...`



##########
python/pyspark/sql/tests/test_functions.py:
##########
@@ -421,6 +421,12 @@ def test_monthname(self):
         row = df.select(F.monthname(df.date)).first()
         self.assertEqual(row[0], "Nov")
 
+    def test_dayname(self):
+            dt = datetime.datetime(2017, 11, 6)

Review Comment:
   Indentation



##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -7303,6 +7303,36 @@ def monthname(col: "ColumnOrName") -> Column:
     return _invoke_function_over_columns("monthname", col)
 
 
+@_try_remote_functions
+def dayname(col: "ColumnOrName") -> Column:
+    """
+    Returns the three-letter abbreviated day name from the given date.
+
+    .. versionadded:: 4.0.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or str
+        target date/timestamp column to work on.
+
+    Returns
+    -------
+    :class:`~pyspark.sql.Column`
+        the three-letter abbreviation of day name for date/timestamp (Mon, Tue, Wed...)
+
+    Examples
+    --------

Review Comment:
   1. Please give a name to Example.
   2. Try not to use `.alias('day')` as much as possible.
   3. Please use the `from pyspark.sql import functions as sf/import pyspark.sql.functions as sf` ... `sf.dayname` approach to write the example. This is to maintain consistency with the already refined examples.
   
   
   
   



-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

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

   @PetarVasiljevic-DB Do you have an account at https://issues.apache.org/jira/browse/SPARK-46725. If so, please, leave a comment in the ticket.


-- 
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


Re: [PR] [SPARK-46725][SQL] Add DAYNAME function [spark]

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

   +1, LGTM. Merging to master.
   Thank you, @PetarVasiljevic-DB and @LuciferYang for review.


-- 
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