Skip to content

Commit d9d856a

Browse files
githubgxllguojn1
authored andcommitted
[fix][runtime] Add binary function
1 parent e36a3dd commit d9d856a

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

parser/src/main/java/io/dingodb/expr/parser/DefaultFunFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import io.dingodb.expr.runtime.op.aggregation.SingleValueAgg;
4343
import io.dingodb.expr.runtime.op.aggregation.Sum0Agg;
4444
import io.dingodb.expr.runtime.op.aggregation.SumAgg;
45+
import io.dingodb.expr.runtime.op.binary.BinaryFunFactory;
4546
import io.dingodb.expr.runtime.op.collection.ArrayConstructorOpFactory;
4647
import io.dingodb.expr.runtime.op.collection.ListConstructorOpFactory;
4748
import io.dingodb.expr.runtime.op.collection.MapConstructorOpFactory;
@@ -259,6 +260,7 @@ public DefaultFunFactory(@NonNull ExprConfig config) {
259260
registerBinaryFun(Locate2FunFactory.NAME, Exprs.LOCATE2);
260261
registerTertiaryFun(Locate3FunFactory.NAME, Exprs.LOCATE3);
261262
registerUnaryFun(HexFunFactory.NAME, Exprs.HEX);
263+
registerUnaryFun(BinaryFunFactory.NAME, Exprs.BINARY);
262264
registerBinaryFun(NumberFormatFunFactory.NAME, Exprs.FORMAT);
263265
registerBinaryFun(MatchesFunFactory.NAME, Exprs.MATCHES);
264266
registerBinaryFun(MatchesIgnoreCaseFunFactory.NAME, Exprs.MATCHES_NC);

runtime/src/main/java/io/dingodb/expr/runtime/expr/Exprs.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import io.dingodb.expr.runtime.op.arithmetic.NegOpFactory;
5656
import io.dingodb.expr.runtime.op.arithmetic.PosOpFactory;
5757
import io.dingodb.expr.runtime.op.arithmetic.SubOpFactory;
58+
import io.dingodb.expr.runtime.op.binary.BinaryFunFactory;
5859
import io.dingodb.expr.runtime.op.cast.BoolCastOpFactory;
5960
import io.dingodb.expr.runtime.op.cast.BytesCastOpFactory;
6061
import io.dingodb.expr.runtime.op.cast.DateCastOpFactory;
@@ -271,6 +272,7 @@ public final class Exprs {
271272
public static final Locate2FunFactory LOCATE2 = Locate2FunFactory.INSTANCE;
272273
public static final Locate3FunFactory LOCATE3 = Locate3FunFactory.INSTANCE;
273274
public static final HexFunFactory HEX = HexFunFactory.INSTANCE;
275+
public static final BinaryFunFactory BINARY = BinaryFunFactory.INSTANCE;
274276
public static final NumberFormatFunFactory FORMAT = NumberFormatFunFactory.INSTANCE;
275277
public static final MatchesFunFactory MATCHES = MatchesFunFactory.INSTANCE;
276278
public static final MatchesIgnoreCaseFunFactory MATCHES_NC = MatchesIgnoreCaseFunFactory.INSTANCE;

runtime/src/main/java/io/dingodb/expr/runtime/expr/Val.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.dingodb.expr.runtime.EvalContext;
3030
import io.dingodb.expr.runtime.ExprCompiler;
3131
import io.dingodb.expr.runtime.ExprConfig;
32+
import io.dingodb.expr.runtime.op.binary.BinaryFunFactory;
3233
import io.dingodb.expr.runtime.op.string.HexFunFactory;
3334
import io.dingodb.expr.runtime.utils.CodecUtils;
3435
import lombok.EqualsAndHashCode;
@@ -169,7 +170,7 @@ private String smartToString(Object obj) {
169170
return wrapByFun(TimestampType.NAME, toSecond(((Timestamp) obj).getTime(), 3).toString());
170171
}
171172
if (obj instanceof byte[]) {
172-
return wrapByFun(HexFunFactory.NAME, "'" + CodecUtils.bytesToHexString((byte[]) obj) + "'");
173+
return wrapByFun(BinaryFunFactory.NAME, "'" + CodecUtils.bytesToHexString((byte[]) obj) + "'");
173174
}
174175
return obj.toString();
175176
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2021 DataCanvas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.dingodb.expr.runtime.op.binary;
18+
19+
import io.dingodb.expr.annotations.Operators;
20+
import io.dingodb.expr.common.type.Type;
21+
import io.dingodb.expr.common.type.Types;
22+
import io.dingodb.expr.runtime.op.UnaryOp;
23+
import io.dingodb.expr.runtime.utils.CodecUtils;
24+
import org.checkerframework.checker.nullness.qual.NonNull;
25+
26+
@Operators
27+
abstract class BinaryFun extends UnaryOp {
28+
public static final String NAME = "BINARY";
29+
30+
private static final long serialVersionUID = 7550168473179742157L;
31+
32+
static byte @NonNull [] binary(@NonNull String value) {
33+
return CodecUtils.hexStringToBytes(value);
34+
}
35+
36+
@Override
37+
public final Type getType() {
38+
return Types.BYTES;
39+
}
40+
41+
@Override
42+
public @NonNull String getName() {
43+
return NAME;
44+
}
45+
}

test/src/test/java/io/dingodb/expr/test/TestToString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public class TestToString {
122122
arguments("or(false, null, false)", "BOOL(NULL)"),
123123
arguments("or(null, c, null)", "BOOL(NULL) || $[2]"),
124124
arguments("or(false, null, a, b, c)", "OR(BOOL(NULL), CASTBOOL($[0]), CASTBOOL($[1]), $[2])"),
125-
arguments("bytes('abc')", "HEX('616263')"),
125+
arguments("bytes('abc')", "BINARY('616263')"),
126126
arguments("case(100)", "100"),
127127
arguments("case(true, 1, 100)", "1"),
128128
arguments("case(false, 1, true, 2, 100)", "2")

0 commit comments

Comments
 (0)