This is an automated email from the ASF dual-hosted git repository. aloyszhang 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 fab6756556 [INLONG-10906][SDK] Transform supports the truncation of left and right strings (#10923) fab6756556 is described below commit fab6756556abf56bef71c7b164463dbb08af33d2 Author: Zkplo <87751516+zk...@users.noreply.github.com> AuthorDate: Wed Aug 28 11:04:34 2024 +0800 [INLONG-10906][SDK] Transform supports the truncation of left and right strings (#10923) --- .../transform/process/function/LeftFunction.java | 62 ++++++++++++++ .../transform/process/function/RightFunction.java | 62 ++++++++++++++ .../transform/process/operator/OperatorTools.java | 6 +- .../TestTransformStringFunctionsProcessor.java | 97 +++++++++++++++++++++- 4 files changed, 225 insertions(+), 2 deletions(-) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LeftFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LeftFunction.java new file mode 100644 index 0000000000..e86d770f92 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LeftFunction.java @@ -0,0 +1,62 @@ +/* + * 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; + +/** + * LeftFunction + * description: left(string,length) + * - return null if either string or length is null + * - return "" if it is less than or equal to zero + * - return a substring of length starting from the right side of the string. + */ +public class LeftFunction implements ValueParser { + + private final ValueParser stringParser; + private final ValueParser lengthParser; + + public LeftFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + stringParser = OperatorTools.buildParser(expressions.get(0)); + lengthParser = OperatorTools.buildParser(expressions.get(1)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object stringObj = stringParser.parse(sourceData, rowIndex, context); + Object lengthObj = lengthParser.parse(sourceData, rowIndex, context); + if (stringObj == null || lengthObj == null) { + return null; + } + String str = OperatorTools.parseString(stringObj); + int len = Integer.parseInt(OperatorTools.parseString(lengthObj)); + if (len <= 0) { + return ""; + } + return str.substring(0, Math.min(str.length(), len)); + } +} diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RightFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RightFunction.java new file mode 100644 index 0000000000..8260d3f299 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RightFunction.java @@ -0,0 +1,62 @@ +/* + * 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; + +/** + * RightFunction + * description: right(string,length) + * - return null if either string or length is null + * - return "" if it is less than or equal to zero + * - return a substring of length starting from the right side of the string. + */ +public class RightFunction implements ValueParser { + + private final ValueParser stringParser; + private final ValueParser lengthParser; + + public RightFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + stringParser = OperatorTools.buildParser(expressions.get(0)); + lengthParser = OperatorTools.buildParser(expressions.get(1)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object stringObj = stringParser.parse(sourceData, rowIndex, context); + Object lengthObj = lengthParser.parse(sourceData, rowIndex, context); + if (stringObj == null || lengthObj == null) { + return null; + } + String str = OperatorTools.parseString(stringObj); + int len = Integer.parseInt(OperatorTools.parseString(lengthObj)); + if (len <= 0) { + return ""; + } + return str.substring(Math.max(str.length() - len, 0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java index 5dc282130b..977883f021 100644 --- a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java @@ -29,6 +29,7 @@ import org.apache.inlong.sdk.transform.process.function.DateFormatFunction; import org.apache.inlong.sdk.transform.process.function.ExpFunction; import org.apache.inlong.sdk.transform.process.function.FloorFunction; import org.apache.inlong.sdk.transform.process.function.FromUnixTimeFunction; +import org.apache.inlong.sdk.transform.process.function.LeftFunction; import org.apache.inlong.sdk.transform.process.function.LengthFunction; import org.apache.inlong.sdk.transform.process.function.LnFunction; import org.apache.inlong.sdk.transform.process.function.LocalTimeFunction; @@ -44,6 +45,7 @@ import org.apache.inlong.sdk.transform.process.function.RandFunction; import org.apache.inlong.sdk.transform.process.function.ReplaceFunction; import org.apache.inlong.sdk.transform.process.function.ReplicateFunction; import org.apache.inlong.sdk.transform.process.function.ReverseFunction; +import org.apache.inlong.sdk.transform.process.function.RightFunction; import org.apache.inlong.sdk.transform.process.function.RoundFunction; import org.apache.inlong.sdk.transform.process.function.SinFunction; import org.apache.inlong.sdk.transform.process.function.SinhFunction; @@ -106,7 +108,7 @@ import java.util.Map; /** * OperatorTools - * + * */ public class OperatorTools { @@ -169,6 +171,8 @@ public class OperatorTools { functionMap.put("upper", UpperFunction::new); functionMap.put("length", LengthFunction::new); functionMap.put("replace", ReplaceFunction::new); + functionMap.put("left", LeftFunction::new); + functionMap.put("right", RightFunction::new); } public static ExpressionOperator buildOperator(Expression expr) { 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 97d0074d23..f28e9ac50e 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 @@ -175,7 +175,6 @@ public class TestTransformStringFunctionsProcessor { Assert.assertEquals(1, output5.size()); Assert.assertEquals(output5.get(0), "result=null"); } - @Test public void testReplicateFunction() throws Exception { String transformSql1 = "select replicate(string1, numeric1) from source"; @@ -347,4 +346,100 @@ public class TestTransformStringFunctionsProcessor { Assert.assertEquals(1, output7.size()); Assert.assertEquals(output7.get(0), "result=da"); } + + @Test + public void testRightFunction() throws Exception { + String transformSql = "select right(string1,numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: right('hello world',5) + String data = "hello world|banana|cloud|5|3|3"; + List<String> output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=world", output1.get(0)); + + // case2: right('hello world',-15) + data = "hello world|banana|cloud|-15|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=", output1.get(0)); + + // case3: right('hello world',100) + data = "hello world|banana|cloud|100|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=hello world", output1.get(0)); + + // case4: right(null,5) + transformSql = "select right(xxd,numeric1) from source"; + config = new TransformConfig(transformSql); + processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|banana|cloud|5|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=null", output1.get(0)); + + // case5: right('hello world',null) + transformSql = "select right(string1,xxd) from source"; + config = new TransformConfig(transformSql); + processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|banana|cloud|5|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=null", output1.get(0)); + } + + @Test + public void testLeftFunction() throws Exception { + String transformSql = "select left(string1,numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: left('hello world',5) + String data = "hello world|banana|cloud|5|3|3"; + List<String> output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=hello", output1.get(0)); + + // case2: left('hello world',-15) + data = "hello world|banana|cloud|-15|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=", output1.get(0)); + + // case3: left('hello world',100) + data = "hello world|banana|cloud|100|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=hello world", output1.get(0)); + + // case4: left(null,5) + transformSql = "select left(xxd,numeric1) from source"; + config = new TransformConfig(transformSql); + processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|banana|cloud|5|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=null", output1.get(0)); + + // case5: left('hello world',null) + transformSql = "select left(string1,xxd) from source"; + config = new TransformConfig(transformSql); + processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|banana|cloud|5|3|3"; + output1 = processor1.transform(data, new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=null", output1.get(0)); + } }