Skip to content

Commit 10d0c8d

Browse files
andygroveclaude
andauthored
feat: add support for next_day expression (apache#3148)
* feat: add support for next_day expression Adds native Comet support for Spark's next_day function which returns the first date after a given date that falls on the specified day of the week. Supports full day names (Sunday, Monday, etc.) and abbreviations (Sun, Mon, etc.). Closes apache#3092 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs * test: migrate next_day tests to SQL file-based approach Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: use next_day implementation from datafusion-spark crate Replace our custom SparkNextDay with the upstream datafusion-spark version, which also handles LargeUtf8 and Utf8View string types. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4fe6452 commit 10d0c8d

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

native/core/src/execution/jni_api.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use datafusion_spark::function::bitwise::bitwise_not::SparkBitwiseNot;
4545
use datafusion_spark::function::datetime::date_add::SparkDateAdd;
4646
use datafusion_spark::function::datetime::date_sub::SparkDateSub;
4747
use datafusion_spark::function::datetime::last_day::SparkLastDay;
48+
use datafusion_spark::function::datetime::next_day::SparkNextDay;
4849
use datafusion_spark::function::hash::sha1::SparkSha1;
4950
use datafusion_spark::function::hash::sha2::SparkSha2;
5051
use datafusion_spark::function::map::map_from_entries::MapFromEntries;
@@ -349,6 +350,7 @@ fn register_datafusion_spark_function(session_ctx: &SessionContext) {
349350
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkDateAdd::default()));
350351
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkDateSub::default()));
351352
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkLastDay::default()));
353+
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkNextDay::default()));
352354
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkSha1::default()));
353355
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkConcat::default()));
354356
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkBitwiseNot::default()));

spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ object QueryPlanSerde extends Logging with CometExprShim {
199199
classOf[Hour] -> CometHour,
200200
classOf[MakeDate] -> CometMakeDate,
201201
classOf[Minute] -> CometMinute,
202+
classOf[NextDay] -> CometNextDay,
202203
classOf[Second] -> CometSecond,
203204
classOf[TruncDate] -> CometTruncDate,
204205
classOf[TruncTimestamp] -> CometTruncTimestamp,

spark/src/main/scala/org/apache/comet/serde/datetime.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package org.apache.comet.serde
2121

2222
import java.util.Locale
2323

24-
import org.apache.spark.sql.catalyst.expressions.{Attribute, DateAdd, DateDiff, DateFormatClass, DateSub, DayOfMonth, DayOfWeek, DayOfYear, GetDateField, Hour, LastDay, Literal, MakeDate, Minute, Month, Quarter, Second, TruncDate, TruncTimestamp, UnixDate, UnixTimestamp, WeekDay, WeekOfYear, Year}
24+
import org.apache.spark.sql.catalyst.expressions.{Attribute, DateAdd, DateDiff, DateFormatClass, DateSub, DayOfMonth, DayOfWeek, DayOfYear, GetDateField, Hour, LastDay, Literal, MakeDate, Minute, Month, NextDay, Quarter, Second, TruncDate, TruncTimestamp, UnixDate, UnixTimestamp, WeekDay, WeekOfYear, Year}
2525
import org.apache.spark.sql.types.{DateType, IntegerType, StringType, TimestampType}
2626
import org.apache.spark.unsafe.types.UTF8String
2727

@@ -310,6 +310,8 @@ object CometDateAdd extends CometScalarFunction[DateAdd]("date_add")
310310

311311
object CometDateSub extends CometScalarFunction[DateSub]("date_sub")
312312

313+
object CometNextDay extends CometScalarFunction[NextDay]("next_day")
314+
313315
object CometMakeDate extends CometScalarFunction[MakeDate]("make_date")
314316

315317
object CometLastDay extends CometScalarFunction[LastDay]("last_day")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
-- Licensed to the Apache Software Foundation (ASF) under one
2+
-- or more contributor license agreements. See the NOTICE file
3+
-- distributed with this work for additional information
4+
-- regarding copyright ownership. The ASF licenses this file
5+
-- to you under the Apache License, Version 2.0 (the
6+
-- "License"); you may not use this file except in compliance
7+
-- with the License. You may obtain a copy of the License at
8+
--
9+
-- http://www.apache.org/licenses/LICENSE-2.0
10+
--
11+
-- Unless required by applicable law or agreed to in writing,
12+
-- software distributed under the License is distributed on an
13+
-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
-- KIND, either express or implied. See the License for the
15+
-- specific language governing permissions and limitations
16+
-- under the License.
17+
18+
-- ConfigMatrix: parquet.enable.dictionary=false,true
19+
20+
statement
21+
CREATE TABLE test_next_day(d date) USING parquet
22+
23+
statement
24+
INSERT INTO test_next_day VALUES (date('2023-01-01')), (date('2024-02-29')), (date('1969-12-31')), (date('2024-06-15')), (NULL)
25+
26+
-- full day names
27+
query
28+
SELECT next_day(d, 'Sunday') FROM test_next_day
29+
30+
query
31+
SELECT next_day(d, 'Monday') FROM test_next_day
32+
33+
query
34+
SELECT next_day(d, 'Tuesday') FROM test_next_day
35+
36+
query
37+
SELECT next_day(d, 'Wednesday') FROM test_next_day
38+
39+
query
40+
SELECT next_day(d, 'Thursday') FROM test_next_day
41+
42+
query
43+
SELECT next_day(d, 'Friday') FROM test_next_day
44+
45+
query
46+
SELECT next_day(d, 'Saturday') FROM test_next_day
47+
48+
-- abbreviated day names
49+
query
50+
SELECT next_day(d, 'Sun') FROM test_next_day
51+
52+
query
53+
SELECT next_day(d, 'Mon') FROM test_next_day
54+
55+
query
56+
SELECT next_day(d, 'Tue') FROM test_next_day
57+
58+
query
59+
SELECT next_day(d, 'Wed') FROM test_next_day
60+
61+
query
62+
SELECT next_day(d, 'Thu') FROM test_next_day
63+
64+
query
65+
SELECT next_day(d, 'Fri') FROM test_next_day
66+
67+
query
68+
SELECT next_day(d, 'Sat') FROM test_next_day
69+
70+
-- literal arguments
71+
query
72+
SELECT next_day(date('2023-01-01'), 'Monday'), next_day(date('2023-01-01'), 'Sunday')
73+
74+
-- null handling
75+
query
76+
SELECT next_day(NULL, 'Monday'), next_day(date('2023-01-01'), NULL)

0 commit comments

Comments
 (0)