This is an automated email from the ASF dual-hosted git repository. luchunliang 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 49b07ad683 [INLONG-10963][SDK] Transform SQL support CONTAINS function. (#10964) 49b07ad683 is described below commit 49b07ad68382cbaf50db2b6d04a799c3abff97f3 Author: rachely <124853723+ybszzz...@users.noreply.github.com> AuthorDate: Thu Sep 5 09:02:48 2024 +0800 [INLONG-10963][SDK] Transform SQL support CONTAINS function. (#10964) * [INLONG-10963][SDK] Transform SQL support CONTAINS function.(#10963) * [INLONG-10963][SDK] Transform SQL support CONTAINS function.(#10963) --- .../process/function/ContainsFunction.java | 56 ++++++++++++++++++++++ .../TestTransformStringFunctionsProcessor.java | 32 +++++++++++++ 2 files changed, 88 insertions(+) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ContainsFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ContainsFunction.java new file mode 100644 index 0000000000..e2905eea07 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ContainsFunction.java @@ -0,0 +1,56 @@ +/* + * 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; + +/** + * ContainsFunction + * description: contains(left, right) - Returns a boolean. + * The value is True if right is found inside left, otherwise, returns False. + * Both left or right must be of STRING type. + */ +@TransformFunction(names = {"contains"}) +public class ContainsFunction implements ValueParser { + + private ValueParser leftStrParser; + private ValueParser rightStrParser; + + public ContainsFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + leftStrParser = OperatorTools.buildParser(expressions.get(0)); + rightStrParser = OperatorTools.buildParser(expressions.get(1)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object leftStrObj = leftStrParser.parse(sourceData, rowIndex, context); + Object rightStrObj = rightStrParser.parse(sourceData, rowIndex, context); + String leftStr = OperatorTools.parseString(leftStrObj); + String rightStr = OperatorTools.parseString(rightStrObj); + return (leftStr == null || rightStr == null) ? null : leftStr.contains(rightStr); + } +} 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 a3099d09f2..f099d728dc 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 @@ -724,4 +724,36 @@ public class TestTransformStringFunctionsProcessor { Assert.assertEquals("result=1278", output5.get(0)); } + @Test + public void testContainsFunction() throws Exception { + String transformSql = "select contains(string1, string2) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor<String, String> processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: contains('Transform SQL', 'SQL') + List<String> output1 = processor.transform("Transform SQL|SQL", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=true"); + // case2: contains('', 'SQL') + List<String> output2 = processor.transform("|SQL", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=false"); + // case3: contains('Transform SQL', '') + List<String> output3 = processor.transform("Transform SQL|", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=true"); + // case4: contains('Transform SQL', 'Transformer') + List<String> output4 = processor.transform("Transform SQL|Transformer", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=false"); + // case5: contains('Transform SQL', 'm S') + List<String> output5 = processor.transform("Transform SQL|m S", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result=true"); + // case6: contains('', '') + List<String> output6 = processor.transform("|", new HashMap<>()); + Assert.assertEquals(1, output6.size()); + Assert.assertEquals(output6.get(0), "result=true"); + } }