From 597ef62236e226fe3ffd2e7ab5f8decf3462fb5c Mon Sep 17 00:00:00 2001 From: guojn1 Date: Tue, 15 Oct 2024 11:50:14 +0800 Subject: [PATCH] [feature][runtime] Add dateSubFun --- .../io/dingodb/expr/runtime/op/OpKeys.java | 1 + .../expr/runtime/op/time/DateSubFun.java | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 runtime/src/main/java/io/dingodb/expr/runtime/op/time/DateSubFun.java diff --git a/runtime/src/main/java/io/dingodb/expr/runtime/op/OpKeys.java b/runtime/src/main/java/io/dingodb/expr/runtime/op/OpKeys.java index 4189a54e..dbc7c03c 100644 --- a/runtime/src/main/java/io/dingodb/expr/runtime/op/OpKeys.java +++ b/runtime/src/main/java/io/dingodb/expr/runtime/op/OpKeys.java @@ -37,6 +37,7 @@ public final class OpKeys { public static final BinaryOpKeys STRING_INT = new BinaryOpKeys(Types.STRING, Types.INT); public static final BinaryOpKeys DECIMAL_INT = new BinaryOpKeys(Types.DECIMAL, Types.INT); public static final BinaryOpKeys DATE_STRING = new BinaryOpKeys(Types.DATE, Types.STRING); + public static final BinaryOpKeys DATE_LONG = new BinaryOpKeys(Types.DATE, Types.LONG); public static final BinaryOpKeys TIME_STRING = new BinaryOpKeys(Types.TIME, Types.STRING); public static final BinaryOpKeys TIMESTAMP_STRING = new BinaryOpKeys(Types.TIMESTAMP, Types.STRING); public static final TertiaryOpKeys STRING_INT_INT = new TertiaryOpKeys(Types.STRING, Types.INT, Types.INT); diff --git a/runtime/src/main/java/io/dingodb/expr/runtime/op/time/DateSubFun.java b/runtime/src/main/java/io/dingodb/expr/runtime/op/time/DateSubFun.java new file mode 100644 index 00000000..e2e99315 --- /dev/null +++ b/runtime/src/main/java/io/dingodb/expr/runtime/op/time/DateSubFun.java @@ -0,0 +1,63 @@ +/* + * Copyright 2021 DataCanvas + * + * Licensed 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 io.dingodb.expr.runtime.op.time; + +import io.dingodb.expr.annotations.Operators; +import io.dingodb.expr.runtime.ExprConfig; +import io.dingodb.expr.runtime.op.BinaryOp; +import io.dingodb.expr.runtime.op.OpKey; +import io.dingodb.expr.runtime.op.OpKeys; +import io.dingodb.expr.runtime.type.Type; +import io.dingodb.expr.runtime.type.Types; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.sql.Date; + +@Operators +public class DateSubFun extends BinaryOp { + public static final String NAME = "DATE_SUB"; + private static final long serialVersionUID = 3693816042300916311L; + + @Override + protected Date evalNonNullValue(@NonNull Object value0, @NonNull Object value1, + ExprConfig config) { + Date dateVal = (Date) value0; + Long val1 = (Long) value1; + long newDateLongVal = dateVal.getTime() - val1; + return new Date(newDateLongVal); + } + + @Override + public final OpKey bestKeyOf(@NonNull Type @NonNull [] types) { + return OpKeys.DATE_LONG.bestKeyOf(types); + } + + @Override + public @NonNull String getName() { + return NAME; + } + + @Override + public Type getType() { + return Types.DATE; + } + + @Override + public OpKey getKey() { + return keyOf(Types.DATE, Types.LONG); + } +}