Skip to content

Commit

Permalink
[feature][runtime] Add dateSubFun
Browse files Browse the repository at this point in the history
  • Loading branch information
guojn1 authored and ketor committed Oct 15, 2024
1 parent c06f26b commit c1c6f5b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c1c6f5b

Please sign in to comment.