From f6bac9adda741b8afc74e833951db57752a8f61d Mon Sep 17 00:00:00 2001 From: Ulrich Kramer Date: Mon, 7 Oct 2024 09:30:18 +0200 Subject: [PATCH] EXPB-2885 Fix SqlFunctions.customDateAdd --- .../apache/calcite/runtime/SqlFunctions.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java index 8fbdfb11a487..1314abf1da47 100644 --- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java +++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java @@ -4978,6 +4978,21 @@ public static int customDateAdd(DataContext root, return timeFrameSet.addDate(date, interval, timeFrame); } + /** SQL {@code DATEADD} function applied to a custom time frame. + * + *

Custom time frames are created as part of a {@link TimeFrameSet}. + * This method retrieves the session's time frame set from the + * {@link DataContext.Variable#TIME_FRAME_SET} variable, then looks up the + * time frame by name. */ + public static int customDateAdd(DataContext root, + TimeUnitRange timeFrameName, int interval, int date) { + final TimeFrameSet timeFrameSet = + requireNonNull(DataContext.Variable.TIME_FRAME_SET.get(root)); + final TimeFrame timeFrame = timeFrameSet.get(timeFrameName.startUnit); + return timeFrameSet.addDate(date, interval, timeFrame); + } + + /** SQL {@code TIMESTAMPADD} function applied to a custom time frame. * *

Custom time frames are created and accessed as described in @@ -4990,6 +5005,19 @@ public static long customTimestampAdd(DataContext root, return timeFrameSet.addTimestamp(timestamp, interval, timeFrame); } + /** SQL {@code TIMESTAMPADD} function applied to a custom time frame. + * + *

Custom time frames are created and accessed as described in + * {@link #customDateAdd}. */ + public static long customTimestampAdd(DataContext root, + TimeUnitRange timeFrameName, long interval, long timestamp) { + final TimeFrameSet timeFrameSet = + requireNonNull(DataContext.Variable.TIME_FRAME_SET.get(root)); + final TimeFrame timeFrame = timeFrameSet.get(timeFrameName.startUnit); + return timeFrameSet.addTimestamp(timestamp, interval, timeFrame); + } + + /** SQL {@code DATEDIFF} function applied to a custom time frame. * *

Custom time frames are created and accessed as described in