From 00080a0828b13fdc15c8d1484e6c88616b48dc6f Mon Sep 17 00:00:00 2001 From: guojn1 Date: Wed, 5 Mar 2025 09:44:34 +0800 Subject: [PATCH] [fix][dingo-exec] Support length function --- .../calcite/fun/DingoOperatorTable.java | 8 ++++ .../io/dingodb/exec/fun/DingoFunFactory.java | 1 + .../java/io/dingodb/exec/fun/LengthFun.java | 39 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 dingo-exec/src/main/java/io/dingodb/exec/fun/LengthFun.java diff --git a/dingo-calcite/src/main/java/io/dingodb/calcite/fun/DingoOperatorTable.java b/dingo-calcite/src/main/java/io/dingodb/calcite/fun/DingoOperatorTable.java index 9c8652f2b..8cba2b6c0 100644 --- a/dingo-calcite/src/main/java/io/dingodb/calcite/fun/DingoOperatorTable.java +++ b/dingo-calcite/src/main/java/io/dingodb/calcite/fun/DingoOperatorTable.java @@ -19,6 +19,7 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import io.dingodb.exec.fun.AutoIncrementFun; +import io.dingodb.exec.fun.LengthFun; import io.dingodb.exec.fun.PowFunFactory; import io.dingodb.exec.fun.mysql.InstrFun; import io.dingodb.exec.fun.mysql.JsonExtractFun; @@ -415,6 +416,13 @@ private void init() { OperandTypes.STRING, SqlFunctionCategory.NUMERIC ); + registerFunction( + LengthFun.NAME, + ReturnTypes.INTEGER, + InferTypes.VARCHAR_1024, + OperandTypes.STRING, + SqlFunctionCategory.NUMERIC + ); } public void registerFunction( diff --git a/dingo-exec/src/main/java/io/dingodb/exec/fun/DingoFunFactory.java b/dingo-exec/src/main/java/io/dingodb/exec/fun/DingoFunFactory.java index 0516cc412..4a70b094e 100644 --- a/dingo-exec/src/main/java/io/dingodb/exec/fun/DingoFunFactory.java +++ b/dingo-exec/src/main/java/io/dingodb/exec/fun/DingoFunFactory.java @@ -69,6 +69,7 @@ private DingoFunFactory() { registerBinaryFun(SchemaFun.NAME, SchemaFun.INSTANCE); registerUnaryFun(NextValFun.NAME, NextValFun.INSTANCE); registerUnaryFun(CurrValFun.NAME, CurrValFun.INSTANCE); + registerUnaryFun(LengthFun.NAME, LengthFun.INSTANCE); } public static synchronized DingoFunFactory getInstance() { diff --git a/dingo-exec/src/main/java/io/dingodb/exec/fun/LengthFun.java b/dingo-exec/src/main/java/io/dingodb/exec/fun/LengthFun.java new file mode 100644 index 000000000..2ca7a12ab --- /dev/null +++ b/dingo-exec/src/main/java/io/dingodb/exec/fun/LengthFun.java @@ -0,0 +1,39 @@ +/* + * 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.exec.fun; + +import io.dingodb.expr.runtime.ExprConfig; +import io.dingodb.expr.runtime.op.UnaryOp; + +import java.io.Serial; + +public class LengthFun extends UnaryOp { + public static final LengthFun INSTANCE = new LengthFun(); + + public static final String NAME = "length"; + @Serial + private static final long serialVersionUID = 1518305554642713272L; + + @Override + public Object evalValue(Object value, ExprConfig config) { + if (value == null) { + return null; + } else { + return value.toString().length(); + } + } +}