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 de1189f732 [INLONG-10869][SDK] Transform SQL supports case conversion of strings (#10878) de1189f732 is described below commit de1189f732f221e6e8e88abf3f48cc2cf7a4ae04 Author: Zkplo <87751516+zk...@users.noreply.github.com> AuthorDate: Tue Aug 27 19:35:14 2024 +0800 [INLONG-10869][SDK] Transform SQL supports case conversion of strings (#10878) * [INLONG-10869][SDK] Transform SQL supports case conversion of strings * [INLONG-10864][SDK] Increase the judgment of null pointers * [INLONG-10869][SDK] Modified the handling logic for null and added relevant testing code --------- Co-authored-by: ZKpLo <14148880+zk...@user.noreply.gitee.com> --- .../transform/process/function/LowerFunction.java | 46 ++++++++++++++++++ .../transform/process/function/UpperFunction.java | 46 ++++++++++++++++++ .../transform/process/operator/OperatorTools.java | 4 ++ .../TestTransformStringFunctionsProcessor.java | 56 ++++++++++++++++++++++ 4 files changed, 152 insertions(+) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LowerFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LowerFunction.java new file mode 100644 index 0000000000..e6aa83c58c --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LowerFunction.java @@ -0,0 +1,46 @@ +/* + * 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; + +/** + * LowerFunction + * description: LOWER(s): Convert all letters of the string s to lowercase letters + */ +public class LowerFunction implements ValueParser { + + private ValueParser stringParser; + + public LowerFunction(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); + if (stringObj == null) + return null; + return stringObj.toString().toLowerCase(); + } +} diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UpperFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UpperFunction.java new file mode 100644 index 0000000000..393b1abde4 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UpperFunction.java @@ -0,0 +1,46 @@ +/* + * 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; + +/** + * UpperFunction + * description: UPPER(s): Convert a string to uppercase + */ +public class UpperFunction implements ValueParser { + + private ValueParser stringParser; + + public UpperFunction(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); + if (stringObj == null) + return null; + return stringObj.toString().toUpperCase(); + } +} 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 ed7ba57d4b..9c6f0d0731 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 @@ -35,6 +35,7 @@ import org.apache.inlong.sdk.transform.process.function.LocateFunction; import org.apache.inlong.sdk.transform.process.function.Log10Function; 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.LowerFunction; import org.apache.inlong.sdk.transform.process.function.ModuloFunction; import org.apache.inlong.sdk.transform.process.function.NowFunction; import org.apache.inlong.sdk.transform.process.function.PowerFunction; @@ -54,6 +55,7 @@ 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.function.UpperFunction; import org.apache.inlong.sdk.transform.process.parser.AdditionParser; import org.apache.inlong.sdk.transform.process.parser.ColumnParser; import org.apache.inlong.sdk.transform.process.parser.DateParser; @@ -160,6 +162,8 @@ public class OperatorTools { functionMap.put("to_timestamp", ToTimestampFunction::new); functionMap.put("mod", ModuloFunction::new); functionMap.put("to_base64", ToBase64Function::new); + functionMap.put("lower", LowerFunction::new); + functionMap.put("upper", UpperFunction::new); functionMap.put("length", LengthFunction::new); functionMap.put("replace", ReplaceFunction::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 b489a89218..97d0074d23 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 @@ -60,6 +60,62 @@ public class TestTransformStringFunctionsProcessor { kvSink = new KvSinkInfo("UTF-8", dstFields); } + @Test + public void testLowerFunction() throws Exception { + String transformSql1 = "select lower(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: lower("ApPlE") + List<String> output1 = processor1.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=apple"); + + // case2: lower("") + List<String> output2 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result="); + + // case3: lower(null) + String transformSql2 = "select lower(xxd) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor<String, String> processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List<String> output3 = processor2.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=null"); + } + + @Test + public void testUpperFunction() throws Exception { + String transformSql1 = "select upper(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: upper("ApPlE") + List<String> output1 = processor1.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=APPLE"); + + // case2: upper("") + List<String> output2 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result="); + + // case3: upper(null) + String transformSql2 = "select upper(xxd) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor<String, String> processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List<String> output3 = processor2.transform("ApPlE|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=null"); + } + @Test public void testSubstringFunction() throws Exception { String transformSql1 = "select substring(string2, numeric1) from source";