Skip to content

Commit 0ae9ed2

Browse files
branch-4.0: [Feature](func) Support function PREVIOUS_DAY #60680 (#60726)
Cherry-picked from #60680 Co-authored-by: linrrarity <linzhenqi@selectdb.com>
1 parent 00ffd1c commit 0ae9ed2

File tree

7 files changed

+401
-11
lines changed

7 files changed

+401
-11
lines changed

be/src/vec/functions/function_date_or_datetime_computation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ using FunctionDatetimeToWeekTwoArgs =
203203

204204
void register_function_date_time_computation(SimpleFunctionFactory& factory) {
205205
factory.register_function<FunctionNextDay>();
206+
factory.register_function<FunctionPreviousDay>();
206207
factory.register_function<FunctionNow>();
207208
factory.register_function<FunctionNowWithPrecision>();
208209
factory.register_function(CurDateFunctionName::name, &createCurDateFunctionBuilderFunction);

be/src/vec/functions/function_date_or_datetime_computation.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,11 @@ class FunctionMonthsBetween : public IFunction {
14841484
}
14851485
};
14861486

1487-
class FunctionNextDay : public IFunction {
1487+
template <bool IsPrevious>
1488+
class FunctionRelativeDay : public IFunction {
14881489
public:
1489-
static constexpr auto name = "next_day";
1490-
static FunctionPtr create() { return std::make_shared<FunctionNextDay>(); }
1490+
static constexpr auto name = IsPrevious ? "previous_day" : "next_day";
1491+
static FunctionPtr create() { return std::make_shared<FunctionRelativeDay<IsPrevious>>(); }
14911492
String get_name() const override { return name; }
14921493
size_t get_number_of_arguments() const override { return 2; }
14931494
DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
@@ -1506,9 +1507,7 @@ class FunctionNextDay : public IFunction {
15061507
const auto& date_col = *assert_cast<const ColumnDateV2*>(left_col.get());
15071508
const auto& week_col = *assert_cast<const ColumnString*>(right_col.get());
15081509
Status status;
1509-
if (left_const && right_const) {
1510-
status = execute_vector<true, true>(input_rows_count, date_col, week_col, *res);
1511-
} else if (left_const) {
1510+
if (left_const) {
15121511
status = execute_vector<true, false>(input_rows_count, date_col, week_col, *res);
15131512
} else if (right_const) {
15141513
status = execute_vector<false, true>(input_rows_count, date_col, week_col, *res);
@@ -1538,10 +1537,11 @@ class FunctionNextDay : public IFunction {
15381537
}
15391538
return it->second;
15401539
}
1541-
static Status compute_next_day(DateV2Value<DateV2ValueType>& dtv, const int week_day) {
1542-
auto days_to_add = (week_day - (dtv.weekday() + 1) + 7) % 7;
1543-
days_to_add = days_to_add == 0 ? 7 : days_to_add;
1544-
dtv.date_add_interval<TimeUnit::DAY>(TimeInterval(TimeUnit::DAY, days_to_add, false));
1540+
static Status compute_relative_day(DateV2Value<DateV2ValueType>& dtv, const int week_day) {
1541+
auto delta_days = IsPrevious ? ((dtv.weekday() + 1) - week_day + 7) % 7
1542+
: (week_day - (dtv.weekday() + 1) + 7) % 7;
1543+
delta_days = delta_days == 0 ? 7 : delta_days;
1544+
dtv.date_add_interval<TimeUnit::DAY>(TimeInterval(TimeUnit::DAY, delta_days, IsPrevious));
15451545
return Status::OK();
15461546
}
15471547

@@ -1574,13 +1574,16 @@ class FunctionNextDay : public IFunction {
15741574
week);
15751575
}
15761576
}
1577-
RETURN_IF_ERROR(compute_next_day(dtv, week_day));
1577+
RETURN_IF_ERROR(compute_relative_day(dtv, week_day));
15781578
res_col.insert_value(dtv);
15791579
}
15801580
return Status::OK();
15811581
}
15821582
};
15831583

1584+
using FunctionNextDay = FunctionRelativeDay<true>;
1585+
using FunctionPreviousDay = FunctionRelativeDay<false>;
1586+
15841587
class FunctionTime : public IFunction {
15851588
public:
15861589
static constexpr auto name = "time";

fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
import org.apache.doris.nereids.trees.expressions.functions.scalar.Positive;
384384
import org.apache.doris.nereids.trees.expressions.functions.scalar.Pow;
385385
import org.apache.doris.nereids.trees.expressions.functions.scalar.Power;
386+
import org.apache.doris.nereids.trees.expressions.functions.scalar.PreviousDay;
386387
import org.apache.doris.nereids.trees.expressions.functions.scalar.Protocol;
387388
import org.apache.doris.nereids.trees.expressions.functions.scalar.QuantilePercent;
388389
import org.apache.doris.nereids.trees.expressions.functions.scalar.QuantileStateEmpty;
@@ -926,6 +927,7 @@ public class BuiltinScalarFunctions implements FunctionHelper {
926927
scalar(ParseDataSize.class, "parse_data_size"),
927928
scalar(PeriodAdd.class, "period_add"),
928929
scalar(PeriodDiff.class, "period_diff"),
930+
scalar(PreviousDay.class, "previous_day"),
929931
scalar(Pi.class, "pi"),
930932
scalar(Pmod.class, "pmod"),
931933
scalar(Positive.class, "positive"),
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
package org.apache.doris.nereids.trees.expressions.functions.scalar;
19+
20+
import org.apache.doris.catalog.FunctionSignature;
21+
import org.apache.doris.nereids.trees.expressions.Expression;
22+
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
23+
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullLiteral;
24+
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
25+
import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
26+
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
27+
import org.apache.doris.nereids.types.DateV2Type;
28+
import org.apache.doris.nereids.types.StringType;
29+
30+
import com.google.common.base.Preconditions;
31+
import com.google.common.collect.ImmutableList;
32+
33+
import java.util.List;
34+
35+
/**
36+
* ScalarFunction 'previous_day'.
37+
*/
38+
public class PreviousDay extends ScalarFunction
39+
implements BinaryExpression, ExplicitlyCastableSignature, PropagateNullLiteral, PropagateNullable {
40+
private static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
41+
FunctionSignature.ret(DateV2Type.INSTANCE).args(DateV2Type.INSTANCE, StringType.INSTANCE));
42+
43+
public PreviousDay(Expression arg0, Expression arg1) {
44+
super("previous_day", arg0, arg1);
45+
}
46+
47+
/** constructor for withChildren and reuse signature */
48+
private PreviousDay(ScalarFunctionParams functionParams) {
49+
super(functionParams);
50+
}
51+
52+
@Override
53+
public PreviousDay withChildren(List<Expression> children) {
54+
Preconditions.checkArgument(children.size() == 2);
55+
return new PreviousDay(getFunctionParams(children));
56+
}
57+
58+
@Override
59+
public List<FunctionSignature> getSignatures() {
60+
return SIGNATURES;
61+
}
62+
63+
@Override
64+
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
65+
return visitor.visitPreviousDay(this, context);
66+
}
67+
}

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@
389389
import org.apache.doris.nereids.trees.expressions.functions.scalar.Positive;
390390
import org.apache.doris.nereids.trees.expressions.functions.scalar.Pow;
391391
import org.apache.doris.nereids.trees.expressions.functions.scalar.Power;
392+
import org.apache.doris.nereids.trees.expressions.functions.scalar.PreviousDay;
392393
import org.apache.doris.nereids.trees.expressions.functions.scalar.Protocol;
393394
import org.apache.doris.nereids.trees.expressions.functions.scalar.QuantilePercent;
394395
import org.apache.doris.nereids.trees.expressions.functions.scalar.QuantileStateEmpty;
@@ -2737,6 +2738,10 @@ default R visitPeriodDiff(PeriodDiff periodDiff, C context) {
27372738
return visitScalarFunction(periodDiff, context);
27382739
}
27392740

2741+
default R visitPreviousDay(PreviousDay previousDay, C context) {
2742+
return visitScalarFunction(previousDay, context);
2743+
}
2744+
27402745
default R visitUnicodeNormalize(UnicodeNormalize func, C context) {
27412746
return visitScalarFunction(func, context);
27422747
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !empty_nullable --
3+
4+
-- !empty_not_nullable --
5+
6+
-- !empty_partial_nullable --
7+
8+
-- !all_null --
9+
\N \N
10+
2024-12-31 2024-12-31
11+
12+
-- !nullable --
13+
2024-03-11 2024-03-11
14+
0000-01-01 0000-01-01
15+
0000-12-29 0000-12-29
16+
0000-02-27 0000-02-27
17+
2024-02-21 2024-02-21
18+
2024-02-22 2024-02-22
19+
2023-02-21 2023-02-21
20+
2023-02-22 2023-02-22
21+
1900-02-21 1900-02-21
22+
1900-02-22 1900-02-22
23+
9999-12-24 9999-12-24
24+
1969-12-24 1969-12-24
25+
1969-12-25 1969-12-25
26+
27+
-- !not_nullable --
28+
2024-03-11 2024-03-11
29+
0000-01-01 0000-01-01
30+
0000-12-29 0000-12-29
31+
0000-02-27 0000-02-27
32+
2024-02-21 2024-02-21
33+
2024-02-22 2024-02-22
34+
2023-02-21 2023-02-21
35+
2023-02-22 2023-02-22
36+
1900-02-21 1900-02-21
37+
1900-02-22 1900-02-22
38+
9999-12-24 9999-12-24
39+
1969-12-24 1969-12-24
40+
1969-12-25 1969-12-25
41+
42+
-- !partial_nullable --
43+
2024-03-11 2024-03-11 2024-03-11 2024-03-11
44+
0000-01-01 0000-01-01 0000-01-01 0000-01-01
45+
0000-12-29 0000-12-29 0000-12-29 0000-12-29
46+
0000-02-27 0000-02-27 0000-02-27 0000-02-27
47+
2024-02-21 2024-02-21 2024-02-21 2024-02-21
48+
2024-02-22 2024-02-22 2024-02-22 2024-02-22
49+
2023-02-21 2023-02-21 2023-02-21 2023-02-21
50+
2023-02-22 2023-02-22 2023-02-22 2023-02-22
51+
1900-02-21 1900-02-21 1900-02-21 1900-02-21
52+
1900-02-22 1900-02-22 1900-02-22 1900-02-22
53+
9999-12-24 9999-12-24 9999-12-24 9999-12-24
54+
1969-12-24 1969-12-24 1969-12-24 1969-12-24
55+
1969-12-25 1969-12-25 1969-12-25 1969-12-25
56+
57+
-- !nullable_no_null --
58+
2024-03-11 2024-03-11
59+
0000-01-01 0000-01-01
60+
0000-12-29 0000-12-29
61+
0000-02-27 0000-02-27
62+
2024-02-21 2024-02-21
63+
2024-02-22 2024-02-22
64+
2023-02-21 2023-02-21
65+
2023-02-22 2023-02-22
66+
1900-02-21 1900-02-21
67+
1900-02-22 1900-02-22
68+
9999-12-24 9999-12-24
69+
1969-12-24 1969-12-24
70+
1969-12-25 1969-12-25
71+
72+
-- !timestamptz_literal --
73+
2025-10-03 2025-10-04
74+
75+
-- !timestamptz_mixed --
76+
2025-10-04 2025-10-04
77+
78+
-- !const_nullable --
79+
\N
80+
\N
81+
\N
82+
\N
83+
\N
84+
\N
85+
\N
86+
\N
87+
\N
88+
\N
89+
\N
90+
\N
91+
\N
92+
93+
-- !partial_const_nullable --
94+
\N
95+
\N
96+
\N
97+
\N
98+
\N
99+
\N
100+
\N
101+
\N
102+
\N
103+
\N
104+
\N
105+
\N
106+
\N
107+
108+
-- !const_not_nullable --
109+
2024-12-30
110+
2024-12-30
111+
2024-12-30
112+
2024-12-30
113+
2024-12-30
114+
2024-12-30
115+
2024-12-30
116+
2024-12-30
117+
2024-12-30
118+
2024-12-30
119+
2024-12-30
120+
2024-12-30
121+
2024-12-30
122+
123+
-- !const_other_nullable --
124+
2024-12-30
125+
2024-12-29
126+
2024-12-27
127+
2024-12-30
128+
2024-12-25
129+
2024-12-26
130+
2024-12-24
131+
2024-12-25
132+
2024-12-25
133+
2024-12-26
134+
2024-12-27
135+
2024-12-25
136+
2024-12-26
137+
138+
-- !const_other_not_nullable --
139+
2024-03-08
140+
0000-01-01
141+
0000-12-29
142+
0000-02-24
143+
2024-02-23
144+
2024-02-23
145+
2023-02-24
146+
2023-02-24
147+
1900-02-23
148+
1900-02-23
149+
9999-12-24
150+
1969-12-26
151+
1969-12-26
152+
153+
-- !const_nullable_no_null --
154+
2024-12-30
155+
156+
-- !const_nullable_no_null_multirows --
157+
2024-03-11
158+
0000-01-01
159+
0000-12-29
160+
0000-02-27
161+
2024-02-21
162+
2024-02-22
163+
2023-02-21
164+
2023-02-22
165+
1900-02-21
166+
1900-02-22
167+
9999-12-24
168+
1969-12-24
169+
1969-12-25
170+
171+
-- !const_partial_nullable_no_null --
172+
2024-12-30
173+
2024-12-29
174+
2024-12-27
175+
2024-12-30
176+
2024-12-25
177+
2024-12-26
178+
2024-12-24
179+
2024-12-25
180+
2024-12-25
181+
2024-12-26
182+
2024-12-27
183+
2024-12-25
184+
2024-12-26
185+

0 commit comments

Comments
 (0)