-
Notifications
You must be signed in to change notification settings - Fork 247
fix: Fall back to Spark for trunc / date_trunc functions when format string is unsupported, or is not a literal value
#2634
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
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6524002
save
andygrove b389449
add failing tests [skip ci]
andygrove 5e8b981
tests pass
andygrove 51455b0
save [skip ci]
andygrove a00e506
check for fallbacks
andygrove 3c6c9f0
upmerge
andygrove 886c45e
fix
andygrove 21f4419
check for specific fallback reason
andygrove b3cd20f
refactor
andygrove aacb552
format
andygrove c8a3006
update signature
andygrove 8bd3287
remove tests that are no longer valid
andygrove d828a67
prep for review
andygrove d6f5fd4
Merge remote-tracking branch 'apache/main' into date-trunc
andygrove 77ccedd
address feedback
andygrove 4fe51c0
fix
andygrove c354a05
Merge remote-tracking branch 'apache/main' into date-trunc
andygrove 8d15e51
remove duplicate and reorder list
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
125 changes: 125 additions & 0 deletions
125
spark/src/test/scala/org/apache/comet/CometTemporalExpressionSuite.scala
This file contains hidden or 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,125 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.comet | ||
|
|
||
| import scala.util.Random | ||
|
|
||
| import org.apache.spark.sql.{CometTestBase, SaveMode} | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types.{DataTypes, StructField, StructType} | ||
|
|
||
| import org.apache.comet.serde.{CometTruncDate, CometTruncTimestamp} | ||
| import org.apache.comet.testing.{DataGenOptions, FuzzDataGenerator} | ||
|
|
||
| class CometTemporalExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper { | ||
|
|
||
| test("trunc (TruncDate)") { | ||
| val supportedFormats = CometTruncDate.supportedFormats | ||
| val unsupportedFormats = Seq("invalid") | ||
|
|
||
| val r = new Random(42) | ||
| val schema = StructType( | ||
| Seq( | ||
| StructField("c0", DataTypes.DateType, true), | ||
| StructField("c1", DataTypes.StringType, true))) | ||
| val df = FuzzDataGenerator.generateDataFrame(r, spark, schema, 1000, DataGenOptions()) | ||
|
|
||
| df.createOrReplaceTempView("tbl") | ||
|
|
||
| for (format <- supportedFormats) { | ||
| checkSparkAnswerAndOperator(s"SELECT c0, trunc(c0, '$format') from tbl order by c0, c1") | ||
| } | ||
| for (format <- unsupportedFormats) { | ||
| // Comet should fall back to Spark for unsupported or invalid formats | ||
| checkSparkAnswerAndFallbackReason( | ||
| s"SELECT c0, trunc(c0, '$format') from tbl order by c0, c1", | ||
| s"Format $format is not supported") | ||
| } | ||
|
|
||
| // Comet should fall back to Spark if format is not a literal | ||
| checkSparkAnswerAndFallbackReason( | ||
| "SELECT c0, trunc(c0, c1) from tbl order by c0, c1", | ||
| "Invalid format strings will throw an exception instead of returning NULL") | ||
| } | ||
|
|
||
| test("date_trunc (TruncTimestamp) - reading from DataFrame") { | ||
| val supportedFormats = CometTruncTimestamp.supportedFormats | ||
| val unsupportedFormats = Seq("invalid") | ||
|
|
||
| createTimestampTestData.createOrReplaceTempView("tbl") | ||
|
|
||
| // TODO test fails with non-UTC timezone | ||
| // https://github.com/apache/datafusion-comet/issues/2649 | ||
| withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") { | ||
| for (format <- supportedFormats) { | ||
| checkSparkAnswerAndOperator(s"SELECT c0, date_trunc('$format', c0) from tbl order by c0") | ||
| } | ||
| for (format <- unsupportedFormats) { | ||
| // Comet should fall back to Spark for unsupported or invalid formats | ||
| checkSparkAnswerAndFallbackReason( | ||
| s"SELECT c0, date_trunc('$format', c0) from tbl order by c0", | ||
| s"Format $format is not supported") | ||
| } | ||
| // Comet should fall back to Spark if format is not a literal | ||
| checkSparkAnswerAndFallbackReason( | ||
| "SELECT c0, date_trunc(fmt, c0) from tbl order by c0, fmt", | ||
| "Invalid format strings will throw an exception instead of returning NULL") | ||
| } | ||
| } | ||
|
|
||
| test("date_trunc (TruncTimestamp) - reading from Parquet") { | ||
| val supportedFormats = CometTruncTimestamp.supportedFormats | ||
| val unsupportedFormats = Seq("invalid") | ||
|
|
||
| withTempDir { path => | ||
| createTimestampTestData.write.mode(SaveMode.Overwrite).parquet(path.toString) | ||
| spark.read.parquet(path.toString).createOrReplaceTempView("tbl") | ||
|
|
||
| // TODO test fails with non-UTC timezone | ||
| // https://github.com/apache/datafusion-comet/issues/2649 | ||
| withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") { | ||
| for (format <- supportedFormats) { | ||
| checkSparkAnswerAndOperator( | ||
| s"SELECT c0, date_trunc('$format', c0) from tbl order by c0") | ||
| } | ||
| for (format <- unsupportedFormats) { | ||
| // Comet should fall back to Spark for unsupported or invalid formats | ||
| checkSparkAnswerAndFallbackReason( | ||
| s"SELECT c0, date_trunc('$format', c0) from tbl order by c0", | ||
| s"Format $format is not supported") | ||
| } | ||
| // Comet should fall back to Spark if format is not a literal | ||
| checkSparkAnswerAndFallbackReason( | ||
| "SELECT c0, date_trunc(fmt, c0) from tbl order by c0, fmt", | ||
| "Invalid format strings will throw an exception instead of returning NULL") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def createTimestampTestData = { | ||
| val r = new Random(42) | ||
| val schema = StructType( | ||
| Seq( | ||
| StructField("c0", DataTypes.TimestampType, true), | ||
| StructField("fmt", DataTypes.StringType, true))) | ||
| FuzzDataGenerator.generateDataFrame(r, spark, schema, 1000, DataGenOptions()) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list of possible formats are:
fmt - the format representing the unit to be truncated to
"YEAR", "YYYY", "YY" - truncate to the first date of the year that the ts falls in, the time part will be zero out
"QUARTER" - truncate to the first date of the quarter that the ts falls in, the time part will be zero out
"MONTH", "MM", "MON" - truncate to the first date of the month that the ts falls in, the time part will be zero out
"WEEK" - truncate to the Monday of the week that the ts falls in, the time part will be zero out
"DAY", "DD" - zero out the time part
"HOUR" - zero out the minute and second with fraction part
"MINUTE"- zero out the second with fraction part
"SECOND" - zero out the second fraction part
"MILLISECOND" - zero out the microseconds
"MICROSECOND" - everything remains
Am I missing a reason why Comet doesn't support smaller than week formats?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://spark.apache.org/docs/latest/api/sql/#date_trunc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is very confusing in Spark, but
date_truncis actuallyTruncTimestampand notTruncDate.