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 ee7f3d9732 [INLONG-10826][SDK] Transform support TRIM(), REPLICATE() function (#10827) ee7f3d9732 is described below commit ee7f3d9732fd6706f706b3988d50efb5eae1d090 Author: emptyOVO <118812562+empty...@users.noreply.github.com> AuthorDate: Wed Aug 21 21:32:53 2024 +0800 [INLONG-10826][SDK] Transform support TRIM(), REPLICATE() function (#10827) --- .../process/function/ReplicateFunction.java | 71 ++++++++++++++++++++++ .../transform/process/function/TrimFunction.java | 44 ++++++++++++++ .../transform/process/operator/OperatorTools.java | 4 ++ .../TestTransformStringFunctionsProcessor.java | 66 ++++++++++++++++++++ 4 files changed, 185 insertions(+) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplicateFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplicateFunction.java new file mode 100644 index 0000000000..5270720d44 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplicateFunction.java @@ -0,0 +1,71 @@ +/* + * 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; +/** + * ReplicateFunction + * description: replicate(string, numeric)--Repeat the string numeric times and return a new string + */ +public class ReplicateFunction implements ValueParser { + + private ValueParser stringParser; + + private ValueParser countParser; + + public ReplicateFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + stringParser = OperatorTools.buildParser(expressions.get(0)); + countParser = OperatorTools.buildParser(expressions.get(1)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object stringObj = stringParser.parse(sourceData, rowIndex, context); + Object countObj = countParser.parse(sourceData, rowIndex, context); + String str = OperatorTools.parseString(stringObj); + double count = OperatorTools.parseBigDecimal(countObj).doubleValue(); + return repeat(str, count); + } + private String repeat(String str, double count) { + if (count == 0) { + return ""; + } + if (count == 1) { + return str; + } + StringBuilder repeatedStr = new StringBuilder(); + StringBuilder originStr = new StringBuilder(str); + while (count > 0) { + if (count % 2 != 0) { + repeatedStr.append(originStr); + } + count = Math.floor(count / 2); + originStr.append(originStr); + } + return repeatedStr.toString(); + } +} diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TrimFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TrimFunction.java new file mode 100644 index 0000000000..b3fbaf26dc --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TrimFunction.java @@ -0,0 +1,44 @@ +/* + * 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.Function; + +/** + * TrimFunction + * description: trim(string)--Remove Spaces before and after the string. + */ +public class TrimFunction implements ValueParser { + + private ValueParser stringParser; + + public TrimFunction(Function expr) { + stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object stringObj = stringParser.parse(sourceData, rowIndex, context); + return OperatorTools.parseString(stringObj).trim(); + } +} 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 fe361263a4..028ef7156f 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 @@ -34,6 +34,7 @@ import org.apache.inlong.sdk.transform.process.function.Log2Function; import org.apache.inlong.sdk.transform.process.function.LogFunction; import org.apache.inlong.sdk.transform.process.function.NowFunction; import org.apache.inlong.sdk.transform.process.function.PowerFunction; +import org.apache.inlong.sdk.transform.process.function.ReplicateFunction; 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; @@ -42,6 +43,7 @@ import org.apache.inlong.sdk.transform.process.function.SubstringFunction; import org.apache.inlong.sdk.transform.process.function.TimestampExtractFunction; import org.apache.inlong.sdk.transform.process.function.ToDateFunction; import org.apache.inlong.sdk.transform.process.function.ToTimestampFunction; +import org.apache.inlong.sdk.transform.process.function.TrimFunction; import org.apache.inlong.sdk.transform.process.function.UnixTimestampFunction; import org.apache.inlong.sdk.transform.process.parser.AdditionParser; import org.apache.inlong.sdk.transform.process.parser.ColumnParser; @@ -108,6 +110,8 @@ public class OperatorTools { functionMap.put("log", LogFunction::new); functionMap.put("exp", ExpFunction::new); functionMap.put("substring", SubstringFunction::new); + functionMap.put("trim", TrimFunction::new); + functionMap.put("replicate", ReplicateFunction::new); functionMap.put("locate", LocateFunction::new); functionMap.put("to_date", ToDateFunction::new); functionMap.put("date_format", DateFormatFunction::new); 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 8dabad12f6..a56fc520b3 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 @@ -118,4 +118,70 @@ 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"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: replicate('apple', 2) + List<String> output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=appleapple"); + String transformSql2 = "select replicate(string2, numeric2) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor<String, String> processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: replicate('banana', 3) + List<String> output2 = processor2.transform("apple|banana|cloud|1|3|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=bananabananabanana"); + // case3: replicate('banana', 1) + List<String> output3 = processor2.transform("apple|banana|cloud|1|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output3.get(0), "result=banana"); + // case3: replicate('cloud', 0) + String transformSql3 = "select replicate(string3, numeric3) from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor<String, String> processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List<String> output4 = processor3.transform("apple|banana|cloud|2|1|0", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result="); + } + + @Test + public void testTrimFunction() throws Exception { + String transformSql1 = "select trim(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: trim(' in long') + List<String> output1 = processor1.transform(" in long|in long | in long ", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=in long"); + String transformSql2 = "select trim(string2) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor<String, String> processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: trim('in long ') + List<String> output2 = processor2.transform(" in long|in long | in long ", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=in long"); + String transformSql3 = "select trim(string2) from source"; + TransformConfig config3 = new TransformConfig(transformSql2); + TransformProcessor<String, String> processor3 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: trim(' in long ') + List<String> output3 = processor3.transform(" in long|in long | in long ", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=in long"); + } + }