|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet |
| 21 | + |
| 22 | +import scala.util.Random |
| 23 | + |
| 24 | +import org.apache.spark.sql.{CometTestBase, SaveMode} |
| 25 | +import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper |
| 26 | +import org.apache.spark.sql.internal.SQLConf |
| 27 | +import org.apache.spark.sql.types.{DataTypes, StructField, StructType} |
| 28 | + |
| 29 | +import org.apache.comet.serde.{CometTruncDate, CometTruncTimestamp} |
| 30 | +import org.apache.comet.testing.{DataGenOptions, FuzzDataGenerator} |
| 31 | + |
| 32 | +class CometTemporalExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper { |
| 33 | + |
| 34 | + test("trunc (TruncDate)") { |
| 35 | + val supportedFormats = CometTruncDate.supportedFormats |
| 36 | + val unsupportedFormats = Seq("invalid") |
| 37 | + |
| 38 | + val r = new Random(42) |
| 39 | + val schema = StructType( |
| 40 | + Seq( |
| 41 | + StructField("c0", DataTypes.DateType, true), |
| 42 | + StructField("c1", DataTypes.StringType, true))) |
| 43 | + val df = FuzzDataGenerator.generateDataFrame(r, spark, schema, 1000, DataGenOptions()) |
| 44 | + |
| 45 | + df.createOrReplaceTempView("tbl") |
| 46 | + |
| 47 | + for (format <- supportedFormats) { |
| 48 | + checkSparkAnswerAndOperator(s"SELECT c0, trunc(c0, '$format') from tbl order by c0, c1") |
| 49 | + } |
| 50 | + for (format <- unsupportedFormats) { |
| 51 | + // Comet should fall back to Spark for unsupported or invalid formats |
| 52 | + checkSparkAnswerAndFallbackReason( |
| 53 | + s"SELECT c0, trunc(c0, '$format') from tbl order by c0, c1", |
| 54 | + s"Format $format is not supported") |
| 55 | + } |
| 56 | + |
| 57 | + // Comet should fall back to Spark if format is not a literal |
| 58 | + checkSparkAnswerAndFallbackReason( |
| 59 | + "SELECT c0, trunc(c0, c1) from tbl order by c0, c1", |
| 60 | + "Invalid format strings will throw an exception instead of returning NULL") |
| 61 | + } |
| 62 | + |
| 63 | + test("date_trunc (TruncTimestamp) - reading from DataFrame") { |
| 64 | + val supportedFormats = CometTruncTimestamp.supportedFormats |
| 65 | + val unsupportedFormats = Seq("invalid") |
| 66 | + |
| 67 | + createTimestampTestData.createOrReplaceTempView("tbl") |
| 68 | + |
| 69 | + // TODO test fails with non-UTC timezone |
| 70 | + // https://github.com/apache/datafusion-comet/issues/2649 |
| 71 | + withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") { |
| 72 | + for (format <- supportedFormats) { |
| 73 | + checkSparkAnswerAndOperator(s"SELECT c0, date_trunc('$format', c0) from tbl order by c0") |
| 74 | + } |
| 75 | + for (format <- unsupportedFormats) { |
| 76 | + // Comet should fall back to Spark for unsupported or invalid formats |
| 77 | + checkSparkAnswerAndFallbackReason( |
| 78 | + s"SELECT c0, date_trunc('$format', c0) from tbl order by c0", |
| 79 | + s"Format $format is not supported") |
| 80 | + } |
| 81 | + // Comet should fall back to Spark if format is not a literal |
| 82 | + checkSparkAnswerAndFallbackReason( |
| 83 | + "SELECT c0, date_trunc(fmt, c0) from tbl order by c0, fmt", |
| 84 | + "Invalid format strings will throw an exception instead of returning NULL") |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + test("date_trunc (TruncTimestamp) - reading from Parquet") { |
| 89 | + val supportedFormats = CometTruncTimestamp.supportedFormats |
| 90 | + val unsupportedFormats = Seq("invalid") |
| 91 | + |
| 92 | + withTempDir { path => |
| 93 | + createTimestampTestData.write.mode(SaveMode.Overwrite).parquet(path.toString) |
| 94 | + spark.read.parquet(path.toString).createOrReplaceTempView("tbl") |
| 95 | + |
| 96 | + // TODO test fails with non-UTC timezone |
| 97 | + // https://github.com/apache/datafusion-comet/issues/2649 |
| 98 | + withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") { |
| 99 | + for (format <- supportedFormats) { |
| 100 | + checkSparkAnswerAndOperator( |
| 101 | + s"SELECT c0, date_trunc('$format', c0) from tbl order by c0") |
| 102 | + } |
| 103 | + for (format <- unsupportedFormats) { |
| 104 | + // Comet should fall back to Spark for unsupported or invalid formats |
| 105 | + checkSparkAnswerAndFallbackReason( |
| 106 | + s"SELECT c0, date_trunc('$format', c0) from tbl order by c0", |
| 107 | + s"Format $format is not supported") |
| 108 | + } |
| 109 | + // Comet should fall back to Spark if format is not a literal |
| 110 | + checkSparkAnswerAndFallbackReason( |
| 111 | + "SELECT c0, date_trunc(fmt, c0) from tbl order by c0, fmt", |
| 112 | + "Invalid format strings will throw an exception instead of returning NULL") |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private def createTimestampTestData = { |
| 118 | + val r = new Random(42) |
| 119 | + val schema = StructType( |
| 120 | + Seq( |
| 121 | + StructField("c0", DataTypes.TimestampType, true), |
| 122 | + StructField("fmt", DataTypes.StringType, true))) |
| 123 | + FuzzDataGenerator.generateDataFrame(r, spark, schema, 1000, DataGenOptions()) |
| 124 | + } |
| 125 | +} |
0 commit comments