This is an automated email from the ASF dual-hosted git repository. dockerzhang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push: new 7cfbf8a74b [INLONG-10939][SDK] Transform SQL supports STRCMP function (#10941) 7cfbf8a74b is described below commit 7cfbf8a74bb9570daa5171ed9300420fb37e1489 Author: Zkplo <87751516+zk...@users.noreply.github.com> AuthorDate: Fri Aug 30 09:53:02 2024 +0800 [INLONG-10939][SDK] Transform SQL supports STRCMP function (#10941) Co-authored-by: ZKpLo <14148880+zk...@user.noreply.gitee.com> --- .../transform/process/function/StrcmpFunction.java | 67 ++++++++++++++++++++++ .../TestTransformStringFunctionsProcessor.java | 42 ++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/StrcmpFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/StrcmpFunction.java new file mode 100644 index 0000000000..ebbcc94ae9 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/StrcmpFunction.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.inlong.sdk.transform.process.function; + +import org.apache.inlong.sdk.transform.decode.SourceData; +import org.apache.inlong.sdk.transform.process.Context; +import org.apache.inlong.sdk.transform.process.operator.OperatorTools; +import org.apache.inlong.sdk.transform.process.parser.ValueParser; + +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.Function; + +import java.util.List; + +/** + * StrcmpFunction + * description: strcmp(s1,s2) + * return NULL if either argument is NULL + * return 0 if the strings are the same + * return -1 if the first argument is smaller than the second according to the current sort order + * return 1 otherwise + */ +@TransformFunction(names = {"strcmp"}) +public class StrcmpFunction implements ValueParser { + + private final ValueParser leftStringParser; + private final ValueParser rightStringParser; + + public StrcmpFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + leftStringParser = OperatorTools.buildParser(expressions.get(0)); + rightStringParser = OperatorTools.buildParser(expressions.get(1)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object leftStringObj = leftStringParser.parse(sourceData, rowIndex, context); + Object rightStringObj = rightStringParser.parse(sourceData, rowIndex, context); + if (leftStringObj == null || rightStringObj == null) { + return null; + } + String leftString = OperatorTools.parseString(leftStringObj); + String rightString = OperatorTools.parseString(rightStringObj); + int cmp = OperatorTools.compareValue(leftString, rightString); + if (cmp > 0) { + return 1; + } else if (cmp < 0) { + return -1; + } + return 0; + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java index d8576d12fc..10cfa740c9 100644 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java @@ -386,6 +386,48 @@ public class TestTransformStringFunctionsProcessor { Assert.assertEquals(output7.get(0), "result=da"); } + @Test + public void testStrcmpFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor<String, String> processor = null; + List<String> output = null; + + transformSql = "select strcmp(string1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: strcmp('hello world','banana') + data = "hello world|banana|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=1", output.get(0)); + + // case2: strcmp('hello world','hello world') + data = "hello world|hello world|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + + // case3: strcmp('hello world','zzzzz') + data = "hello world|zzzzz|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=-1", output.get(0)); + + // case4: strcmp('hello world',null) + transformSql = "select strcmp(string1,xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|zzzzz|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } + @Test public void testRightFunction() throws Exception { String transformSql = "select right(string1,numeric1) from source";