diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java new file mode 100644 index 00000000000..a3072901a30 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java @@ -0,0 +1,55 @@ +/* + * 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; + +import java.time.LocalTime; +import java.time.ZoneId; + +/** + * LocalTimeFunction + * description: + * localTime([string1]) returns the current time in the specified time zone. + * (by default: the current time in the system time zone) + */ +public class LocalTimeFunction implements ValueParser { + + private ValueParser stringParser; + + public LocalTimeFunction(Function expr) { + if (expr.getParameters() != null) { + stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0)); + } + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + if (stringParser != null) { + String zoneString = OperatorTools.parseString(stringParser.parse(sourceData, rowIndex, context)); + return LocalTime.now(ZoneId.of(zoneString)).withNano(0); + } else { + return LocalTime.now(ZoneId.systemDefault()).withNano(0); + } + } +} 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 00000000000..e6aa83c58ce --- /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/RandFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RandFunction.java new file mode 100644 index 00000000000..2baebe038a3 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/RandFunction.java @@ -0,0 +1,61 @@ +/* + * 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.math.BigDecimal; +import java.util.List; +import java.util.Random; + +/** + * RandFunction + * description: Rand()--Returns a pseudo-random double precision value in the range [0.0, 1.0) + * Rand(Integer)--Returns a pseudo-random double precision value in the range [0.0, 1.0) with an initial seed of Integer. + */ +public class RandFunction implements ValueParser { + + private ValueParser seedParser; + + private Random random; + + public RandFunction(Function expr) { + random = new Random(); + if (expr.getParameters() != null) { + List expressions = expr.getParameters().getExpressions(); + if (expressions != null && expressions.size() == 1) { + seedParser = OperatorTools.buildParser(expressions.get(0)); + } + } + } + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + if (seedParser != null) { + Object seedObj = seedParser.parse(sourceData, rowIndex, context); + BigDecimal seedValue = OperatorTools.parseBigDecimal(seedObj); + random.setSeed(seedValue.intValue()); + } + return random.nextDouble(); + } +} 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 00000000000..393b1abde45 --- /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 c00d10b00f1..b2b2a73c0eb 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 @@ -31,13 +31,16 @@ import org.apache.inlong.sdk.transform.process.function.FromUnixTimeFunction; 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; 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; +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; @@ -54,6 +57,7 @@ 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; @@ -116,6 +120,8 @@ public class OperatorTools { static { functionMap.put("concat", ConcatFunction::new); functionMap.put("now", NowFunction::new); + functionMap.put("localtime", LocalTimeFunction::new); + functionMap.put("currenttime", LocalTimeFunction::new); functionMap.put("power", PowerFunction::new); functionMap.put("abs", AbsFunction::new); functionMap.put("sqrt", SqrtFunction::new); @@ -132,6 +138,7 @@ public class OperatorTools { functionMap.put("to_date", ToDateFunction::new); functionMap.put("date_format", DateFormatFunction::new); functionMap.put("ceil", CeilFunction::new); + functionMap.put("rand", RandFunction::new); functionMap.put("floor", FloorFunction::new); functionMap.put("sin", SinFunction::new); functionMap.put("sinh", SinhFunction::new); @@ -160,6 +167,8 @@ public class OperatorTools { functionMap.put("mod", ModuloFunction::new); functionMap.put("to_base64", ToBase64Function::new); functionMap.put("sign", SignFunction::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/TestTransformArithmeticFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java index 6cf9190d6c0..b1c2232f15b 100644 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Random; /** * TestArithmeticFunctionsTransformProcessor @@ -558,4 +559,31 @@ public void testBinFunction() throws Exception { Assert.assertEquals(output2.get(0), "result=null"); } + @Test + public void testRandFunction() throws Exception { + String transformSql1 = "select rand(numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: rand(1) + List output1 = processor1.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=" + new Random(1).nextDouble()); + // case: rand(2) + List output2 = processor1.transform("2|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=" + new Random(2).nextDouble()); + String transformSql2 = "select rand() from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: rand() + List output3 = processor2.transform("|||", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + double result = Double.parseDouble(output3.get(0).substring(7)); + Assert.assertTrue(result >= 0.0 && result < 1.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 b489a892184..97d0074d23d 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 processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: lower("ApPlE") + List 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 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 processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List 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 processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: upper("ApPlE") + List 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 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 processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List 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"; diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java index 7a40411bc6f..67027c4b25d 100644 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java @@ -28,6 +28,10 @@ import org.junit.Before; import org.junit.Test; +import java.time.Duration; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -362,4 +366,47 @@ public void testToTimestampFunction() throws Exception { Assert.assertEquals(1, output4.size()); Assert.assertEquals(output4.get(0), "result=1970-01-01 00:00:00.0"); } + + @Test + public void testLocalTimeFunction() throws Exception { + String transformSql1 = "select localTime() from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + // case1: localTime() + List output1 = processor1.transform("", new HashMap<>()); + LocalTime expectedTime1 = LocalTime.now().withNano(0); + LocalTime actualTime1 = LocalTime.parse(output1.get(0).split("=")[1], fomatter); + Duration duration1 = Duration.between(expectedTime1, actualTime1); + Assert.assertEquals(1, output1.size()); + Assert.assertTrue(duration1.getSeconds() < 1); + + // case2: currentTime("UTC") + String transformSql2 = "select currentTime('UTC') from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output2 = processor2.transform("", new HashMap<>()); + LocalTime expectedTime2 = LocalTime.now(ZoneId.of("UTC")).withNano(0); + LocalTime actualTime2 = LocalTime.parse(output2.get(0).split("=")[1], fomatter); + Duration duration2 = Duration.between(expectedTime2, actualTime2); + Assert.assertEquals(1, output2.size()); + Assert.assertTrue(duration2.getSeconds() < 1); + + // case 3: localTime("America/New_York") + String transformSql3 = "select localTime('America/New_York') from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor3.transform("", new HashMap<>()); + LocalTime expectedTime3 = LocalTime.now(ZoneId.of("America/New_York")).withNano(0); + LocalTime actualTime3 = LocalTime.parse(output3.get(0).split("=")[1], fomatter); + Duration duration3 = Duration.between(expectedTime3, actualTime3); + Assert.assertEquals(1, output3.size()); + Assert.assertTrue(duration3.getSeconds() < 1); + } }