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 deleted file mode 100644 index b6254eb35d5..00000000000 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformArithmeticFunctionsProcessor.java +++ /dev/null @@ -1,1140 +0,0 @@ -/* - * 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; - -import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; -import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; -import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.FieldInfo; -import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; -import org.apache.inlong.sdk.transform.pojo.TransformConfig; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Random; - -/** - * TestArithmeticFunctionsTransformProcessor - * description: test the arithmetic functions in transform processor - */ -public class TestTransformArithmeticFunctionsProcessor { - - private static final List srcFields = new ArrayList<>(); - private static final List dstFields = new ArrayList<>(); - private static final CsvSourceInfo csvSource; - private static final KvSinkInfo kvSink; - - static { - for (int i = 1; i < 5; i++) { - FieldInfo field = new FieldInfo(); - field.setName("numeric" + i); - srcFields.add(field); - } - FieldInfo field = new FieldInfo(); - field.setName("result"); - dstFields.add(field); - csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); - kvSink = new KvSinkInfo("UTF-8", dstFields); - } - - @Test - public void testSignFunction() throws Exception { - String transformSql = "select sign(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: sign(3.14159265358979323846) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1"); - // case2: sign(-3.5) - List output2 = processor.transform("-3.5|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=-1"); - // case3: sign(0) - List output3 = processor.transform("0|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=0"); - } - - @Test - public void testModuloFunction() throws Exception { - String transformFunctionSql = "select mod(numeric1,100) from source"; - String transformExpressionSql = "select numeric1 % 100 from source"; - List output1, output2; - String data; - TransformConfig functionConfig = new TransformConfig(transformFunctionSql); - TransformProcessor functionProcessor = TransformProcessor - .create(functionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - TransformConfig expressionConfig = new TransformConfig(transformExpressionSql); - TransformProcessor expressionProcessor = TransformProcessor - .create(expressionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: "mod(3.1415926,100)" and "3.1415926 % 100" - data = "3.1415926|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=3.1415926", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=3.1415926", output2.get(0)); - - // case2: "mod(-3.1415926,100)" and "-3.1415926 % 100" - data = "-3.1415926|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=-3.1415926", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=-3.1415926", output2.get(0)); - - // case3: "mod(320,100)" and "320 % 100" - data = "320|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=20", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=20", output2.get(0)); - - // case4: "mod(-320,100)" and "-320 % 100" - data = "-320|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=-20", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=-20", output2.get(0)); - - transformFunctionSql = "select mod(numeric1,-10) from source"; - transformExpressionSql = "select numeric1 % -10 from source"; - functionConfig = new TransformConfig(transformFunctionSql); - functionProcessor = TransformProcessor - .create(functionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - expressionConfig = new TransformConfig(transformExpressionSql); - expressionProcessor = TransformProcessor - .create(expressionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case5: "mod(9,-10)" and "9 % -10" - data = "9|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=9", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=9", output2.get(0)); - - // case6: "mod(-13,-10)" and "-13 % -10" - data = "-13|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=-3", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=-3", output2.get(0)); - - // case7: "mod(-13.14,-10)" and "-13.14 % -10" - data = "-13.14|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=-3.14", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=-3.14", output2.get(0)); - - // case8: "mod(13.14,-10)" and "13.14 % -10" - data = "13.14|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=3.14", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=3.14", output2.get(0)); - - transformFunctionSql = "select mod(numeric1,-3.14) from source"; - transformExpressionSql = "select numeric1 % -3.14 from source"; - functionConfig = new TransformConfig(transformFunctionSql); - functionProcessor = TransformProcessor - .create(functionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - expressionConfig = new TransformConfig(transformExpressionSql); - expressionProcessor = TransformProcessor - .create(expressionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case9: "mod(9,-3.14)" and "9 % -3.14" - data = "9|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=2.72", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=2.72", output2.get(0)); - - // case10: "mod(-9,-3.14)" and "-9 % -3.14" - data = "-9|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=-2.72", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=-2.72", output2.get(0)); - - // case11: "mod(-13.14,-3.14)" and "-13.14 % -3.14" - data = "-13.14|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=-0.58", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=-0.58", output2.get(0)); - - // case12: "mod(13.14,-3.14)" and "13.14 % -3.14" - data = "13.14|4a|4|8"; - output1 = functionProcessor.transform(data); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=0.58", output1.get(0)); - output2 = expressionProcessor.transform(data); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=0.58", output2.get(0)); - - } - - @Test - public void testIfNullFunction() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: ifnull(5, 3) - transformSql = "select ifnull(numeric1,numeric2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "5|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=5", output.get(0)); - - // case2: ifnull(null,3) - transformSql = "select ifnull(xxd,numeric2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "5|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=3", output.get(0)); - - // case3: ifnull(6 / 3,'YES') - transformSql = "select ifnull(numeric1 / numeric2,'YES') from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "6|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=2", output.get(0)); - - // case4: ifnull(6 / 0,'YES') - transformSql = "select ifnull(numeric1 / numeric2,'YES') from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "6|0|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=YES", output.get(0)); - - // case5: ifnull(6 / 0,3 / 0) - transformSql = "select ifnull(numeric1 / numeric2,numeric3 / numeric2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "6|0|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - } - - @Test - public void testShaFunction() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: sha("") - transformSql = "select sha(numeric1) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=da39a3ee5e6b4b0d3255bfef95601890afd80709", output.get(0)); - - // case2: sha("5") - data = "5|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4", output.get(0)); - - // case3: sha(null) - transformSql = "select sha(xxd) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "3|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - } - - @Test - public void testSha2Function() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: sha2("",3) - transformSql = "select sha2(numeric1,numeric2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "|3|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case2: sha2("5",224) - data = "5|224|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=b51d18b551043c1f145f22dbde6f8531faeaf68c54ed9dd79ce24d17", output.get(0)); - - // case3: sha2("5",0) - data = "5|0|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=ef2d127de37b942baad06145e54b0c619a1f22327b2ebbcfbec78f5564afe39d", output.get(0)); - - // case4: sha2(null,224) - transformSql = "select sha2(xxd,224) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "3|224|3|5"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - } - - @Test - public void testMd5Function() throws Exception { - String transformSql = "select md5(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: md5("1") - List output1 = processor.transform("1|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=c4ca4238a0b923820dcc509a6f75849b", output1.get(0)); - - // case2: md5("-1") - List output2 = processor.transform("-1|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=6bb61e3b7bce0931da574d19d1d82c88", output2.get(0)); - - // case3: md5("") - List output3 = processor.transform("|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals("result=d41d8cd98f00b204e9800998ecf8427e", output3.get(0)); - - // case4: md5(null) - transformSql = "select md5(numericxx) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output4 = processor.transform("1|4|6|8"); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals("result=null", output4.get(0)); - } - - @Test - public void testBitwiseInversionOperator() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: ~-4 - transformSql = "select ~numeric1 from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "-4|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=3", output.get(0)); - - // case2: ~4 - data = "4|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551611", output.get(0)); - - // case3: ~0 - data = "0|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551615", output.get(0)); - - // case4: ~~-4 - transformSql = "select ~(~numeric1) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "-4|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551612", output.get(0)); - - } - @Test - public void testBitwiseAndOperator() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: 18446744073709551615 & -1 - transformSql = "select numeric1 & numeric2 from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "18446744073709551615|-1|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551615", output.get(0)); - - // case2: 18446744073709551615 & 0 - data = "18446744073709551615|0|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=0", output.get(0)); - } - @Test - public void testBitwiseOrOperator() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: 18446744073709551615 | -1 - transformSql = "select numeric1 | numeric2 from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "18446744073709551615|-1|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551615", output.get(0)); - - // case2: 4 | 3 - data = "4|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=7", output.get(0)); - } - @Test - public void testBitwiseRightShiftOperator() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: 4 >> -1 - transformSql = "select numeric1 >> numeric2 from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "4|-1|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=0", output.get(0)); - - // case2: 9223372036854775808 >> 2 - data = "9223372036854775808|2|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=2305843009213693952", output.get(0)); - - // case3: 9223372036854775808 >> 9223372036854775808 - data = "9223372036854775808|9223372036854775808|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=0", output.get(0)); - } - @Test - public void testBitwiseLeftShiftOperator() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: 9223372036854775807 << 1 - transformSql = "select numeric1 << numeric2 from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "9223372036854775807|1|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551614", output.get(0)); - - // case2: 18446744073709551615 << 18446744073709551615 - data = "18446744073709551615|18446744073709551615|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=0", output.get(0)); - - // case3: 9223372036854775807 << -1 - data = "9223372036854775807|-1|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=0", output.get(0)); - } - @Test - public void testBitwiseXorOperator() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - // case1: 4 ^ 3 - transformSql = "select numeric1 ^ numeric2 from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "4|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=7", output.get(0)); - - // case2: 4 ^ -1 - data = "4|-1|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=18446744073709551611", output.get(0)); - } - - @Test - public void testRoundFunction() throws Exception { - String transformSql = "select round(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: round(3.14159265358979323846) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=3"); - // case2: round(3.5) - List output2 = processor.transform("3.5|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=4"); - - transformSql = "select round(numeric1,numeric2) from source"; - config = new TransformConfig(transformSql); - // case3: round(3.14159265358979323846,10) - processor = TransformProcessor.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|10|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=3.1415926536"); - // case4: round(13.14159265358979323846,-1) - List output4 = processor.transform("13.14159265358979323846|-1|6|8"); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=10.0"); - } - - @Test - public void testPowerFunction() throws Exception { - String transformSql = "select power(numeric1, numeric2) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: 2^4 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("2|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=16.0"); - // case2: 2^(-2) - List output2 = processor.transform("2|-2|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=0.25"); - // case3: 4^(0.5) - List output3 = processor.transform("4|0.5|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=2.0"); - } - - @Test - public void testAbsFunction() throws Exception { - String transformSql = "select abs(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: |2| - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("2|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=2"); - // case2: |-4.25| - List output2 = processor.transform("-4.25|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=4.25"); - } - - @Test - public void testSqrtFunction() throws Exception { - String transformSql = "select sqrt(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: sqrt(9) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("9|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=3.0"); - // case2: sqrt(5) - List output2 = processor.transform("5|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=2.23606797749979"); - } - - @Test - public void testLnFunction() throws Exception { - String transformSql = "select ln(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: ln(1) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - // case2: ln(10) - List output2 = processor.transform("10|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=2.302585092994046"); - } - - @Test - public void testRadiansFunction() throws Exception { - String transformSql = "select radians(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: radians(10) - List output1 = processor.transform("10|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.17453292519943295"); - // case2: radians(18.97) - List output2 = processor.transform("18.97|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=0.33108895910332425"); - } - - @Test - public void testLog10Function() throws Exception { - String transformSql = "select log10(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: log10(1) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - // case2: log10(1000) - List output2 = processor.transform("1000|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=3.0"); - } - - @Test - public void testLog2Function() throws Exception { - String transformSql = "select log2(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: log2(1) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - // case2: log2(32) - List output2 = processor.transform("32|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=5.0"); - } - - @Test - public void testLogFunction() throws Exception { - String transformSql1 = "select log(numeric1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - // case1: ln(1) - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor1.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - String transformSql2 = "select log(numeric1, numeric2) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - // case2: log2(8) - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output2 = processor2.transform("2|8|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=3.0"); - // case3: log10(100) - TransformProcessor processor3 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor3.transform("10|100|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=2.0"); - } - - @Test - public void testExpFunction() throws Exception { - String transformSql = "select exp(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: e^0 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("0|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1.0"); - // case2: e^2 - List output2 = processor.transform("2|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=7.38905609893065"); - } - - @Test - public void testCeilFunction() throws Exception { - String transformSql = "select ceil(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: ceil(1.23) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("1.23|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=2.0"); - // case2: ceil(3) - List output2 = processor.transform("3|-2|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=3.0"); - // case3: ceil(-5.67) - List output3 = processor.transform("-5.67|0.5|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-5.0"); - } - - @Test - public void testFloorFunction() throws Exception { - String transformSql = "select floor(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: floor(1.23) - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("1.23|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1.0"); - // case2: floor(3) - List output2 = processor.transform("3|-2|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=3.0"); - // case3: floor(-5.67) - List output3 = processor.transform("-5.67|0.5|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-6.0"); - } - - @Test - public void testSinFunction() throws Exception { - String transformSql = "select sin(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: sin(0) - List output1 = processor.transform("0|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - } - - @Test - public void testSinhFunction() throws Exception { - String transformSql = "select sinh(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: sinh(0) - List output1 = processor.transform("0|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - // case2: sinh(1) - List output2 = processor.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1.1752011936438014"); - // case3: sinh(2) - List output3 = processor.transform("2|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=3.626860407847019"); - } - - @Test - public void testCosFunction() throws Exception { - String transformSql = "select cos(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: cos(0) - List output1 = processor.transform("0|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1.0"); - } - - @Test - public void testAcosFunction() throws Exception { - String transformSql = "select acos(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: acos(1) - List output1 = processor.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - // case2: acos(0) - List output2 = processor.transform("0|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1.5707963267948966"); - // case3: acos(-1) - List output3 = processor.transform("-1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=3.141592653589793"); - } - - @Test - public void testTanFunction() throws Exception { - String transformSql = "select tan(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: tan(0) - List output1 = processor.transform("0|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.0"); - // case: tan(1) - List output2 = processor.transform("1|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1.5574077246549023"); - // case: tan(2) - List output3 = processor.transform("2|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-2.185039863261519"); - } - - @Test - public void testBinFunction() throws Exception { - String transformSql1 = "select bin(numeric1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: bin(4) - List output1 = processor1.transform("4|5|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=100"); - String transformSql2 = "select bin() from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: bin() - List output2 = processor2.transform("1|2|3|4", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - 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); - } - - @Test - public void testCotFunction() throws Exception { - String transformSql = "select cot(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: cot(1) - List output1 = processor.transform("1|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.6420926159343306"); - - // case2: cot(0.5) - List output2 = processor.transform("0.5|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1.830487721712452"); - - // case3: cot(-1) - List output3 = processor.transform("-1|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-0.6420926159343306"); - } - - @Test - public void testTanhFunction() throws Exception { - String transformSql = "select tanh(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: tanh(1) - List output1 = processor.transform("1|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.7615941559557649"); - - // case2: tanh(0) - List output2 = processor.transform("0|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=0.0"); - - // case3: tanh(-1) - List output3 = processor.transform("-1|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-0.7615941559557649"); - } - - @Test - public void testCoshFunction() throws Exception { - String transformSql = "select cosh(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: cosh(1) - List output1 = processor.transform("1|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1.543080634815244"); - - // case2: cosh(0) - List output2 = processor.transform("0|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1.0"); - - // case3: cosh(-1) - List output3 = processor.transform("-1|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=1.543080634815244"); - } - - @Test - public void testAsinFunction() throws Exception { - String transformSql = "select asin(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: asin(0.5) - List output1 = processor.transform("0.5|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.5235987755982989"); - - // case2: asin(0) - List output2 = processor.transform("0|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=0.0"); - - // case3: asin(-0.5) - List output3 = processor.transform("-0.5|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-0.5235987755982989"); - } - - @Test - public void testAtanFunction() throws Exception { - String transformSql = "select atan(numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: atan(1) - List output1 = processor.transform("1|4|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.7853981633974483"); - - // case2: atan(0) - List output2 = processor.transform("0|4|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=0.0"); - - // case3: atan(-1) - List output3 = processor.transform("-1|4|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=-0.7853981633974483"); - } - - @Test - public void testAtan2Function() throws Exception { - String transformSql = "select atan2(numeric1, numeric2) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: atan2(1, 1) - List output1 = processor.transform("1|1|6|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0.7853981633974483"); - - // case2: atan2(1, 0) - List output2 = processor.transform("1|0|6|8"); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1.5707963267948966"); - - // case3: atan2(0, -1) - List output3 = processor.transform("0|-1|6|8"); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=3.141592653589793"); - } - - @Test - public void testHexFunction() throws Exception { - String transformSql1 = "select hex(numeric1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: hex(1007) - List output1 = processor1.transform("1007|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=3EF"); - // case: hex(3.14) - List output2 = processor1.transform("3.14|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=3"); - // case: hex(3.141592653589793) - List output3 = processor1.transform("3.141592653589793|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=3"); - // case: hex(-9223372036854775808) - List output4 = processor1.transform("-9223372036854775808|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=8000000000000000"); - // case: hex(abc) - List output5 = processor1.transform("abc|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - Assert.assertEquals(output5.get(0), "result=616263"); - } - - @Test - public void testPiFunction() throws Exception { - String transformSql1 = "select pi() from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case: pi() - List output1 = processor1.transform("1007|4|6|8", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=3.141592653589793"); - } -} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformExpressionOperatorsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformExpressionOperatorsProcessor.java deleted file mode 100644 index 67e4e331d41..00000000000 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformExpressionOperatorsProcessor.java +++ /dev/null @@ -1,354 +0,0 @@ -/* - * 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; - -import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; -import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; -import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.FieldInfo; -import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; -import org.apache.inlong.sdk.transform.pojo.TransformConfig; -import org.apache.inlong.sdk.transform.process.converter.DoubleConverter; -import org.apache.inlong.sdk.transform.process.converter.LongConverter; -import org.apache.inlong.sdk.transform.process.converter.TypeConverter; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -/** - * TestArithmeticFunctionsTransformProcessor - * description: test the arithmetic functions in transform processor - */ -public class TestTransformExpressionOperatorsProcessor { - - private static final List srcFields = new ArrayList<>(); - private static final List dstFields = new ArrayList<>(); - private static final CsvSourceInfo csvSource; - private static final KvSinkInfo kvSink; - - static { - srcFields.add(new FieldInfo("numeric1", new DoubleConverter())); - srcFields.add(new FieldInfo("string2", TypeConverter.DefaultTypeConverter())); - srcFields.add(new FieldInfo("numeric3", new DoubleConverter())); - srcFields.add(new FieldInfo("numeric4", new LongConverter())); - - FieldInfo field = new FieldInfo(); - field.setName("result"); - dstFields.add(field); - csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); - kvSink = new KvSinkInfo("UTF-8", dstFields); - } - - @Test - public void testEqualsToOperator() throws Exception { - String transformSql = "select if(string2 = 4,1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|4a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|4a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0"); - // case2: "3.14159265358979323846|4|4|8" - List output2 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=1"); - - transformSql = "select if(numeric3 = 4,1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=1"); - // case4: "3.14159265358979323846|4|4.2|8" - List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=0"); - } - - @Test - public void testNotEqualsToOperator() throws Exception { - String transformSql = "select if(string2 != 4,1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|4a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|4a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1"); - // case2: "3.14159265358979323846|4|4|8" - List output2 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=0"); - - transformSql = "select if(numeric3 != 4,1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=0"); - // case4: "3.14159265358979323846|4|4.2|8" - List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=1"); - } - - @Test - public void testGreaterThanEqualsOperator() throws Exception { - String transformSql = "select if(string2 >= 4,1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=1"); - - transformSql = "select if(numeric3 >= 4,1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=1"); - // case4: "3.14159265358979323846|4|3.2|8" - List output4 = processor.transform("3.14159265358979323846|4|3.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=0"); - } - - @Test - public void testGreaterThanOperator() throws Exception { - String transformSql = "select if(string2 > 4.1,1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=1"); - - transformSql = "select if(numeric3 > 4.1,1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=0"); - // case4: "3.14159265358979323846|4|4.2|8" - List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=1"); - } - - @Test - public void testMinorThanEqualsOperator() throws Exception { - String transformSql = "select if(string2 <= 4,1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=0"); - - transformSql = "select if(numeric3 <= 4,1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=1"); - // case4: "3.14159265358979323846|4|4.2|8" - List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=0"); - } - - @Test - public void testMinorThanOperator() throws Exception { - String transformSql = "select if(string2 < 4.1,1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=0"); - - transformSql = "select if(numeric3 < 4,1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=0"); - // case4: "3.14159265358979323846|4|3.2|8" - List output4 = processor.transform("3.14159265358979323846|4|3.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=1"); - } - - @Test - public void testNotOperator() throws Exception { - String transformSql = "select if(!(string2 < 4),1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=1"); - - transformSql = "select if(!(numeric3 < 3.9),1,0) from source"; - config = new TransformConfig(transformSql); - // case3: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=1"); - // case4: "3.14159265358979323846|4|3.2|8" - List output4 = processor.transform("3.14159265358979323846|4|3.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=0"); - } - - @Test - public void testOrOperator() throws Exception { - String transformSql = "select if((string2 < 4) or (numeric4 > 5),1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|8" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=1"); - // case3: "3.14159265358979323846|5|4|4" - List output3 = processor.transform("3.14159265358979323846|5|4|4"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=0"); - - transformSql = "select if((numeric3 < 4) or (numeric4 > 5),1,0) from source"; - config = new TransformConfig(transformSql); - // case4: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output4 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=1"); - // case5: "3.14159265358979323846|4|3.2|8" - List output5 = processor.transform("3.14159265358979323846|4|3.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output5.get(0), "result=1"); - // case6: "3.14159265358979323846|4|4.2|5" - List output6 = processor.transform("3.14159265358979323846|4|4.2|5"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output6.get(0), "result=0"); - } - - @Test - public void testAndOperator() throws Exception { - String transformSql = "select if((string2 < 4) and (numeric4 > 5),1,0) from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1: "3.14159265358979323846|3a|4|4" - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor.transform("3.14159265358979323846|3a|4|4"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=0"); - // case2: "3.14159265358979323846|5|4|8" - List output2 = processor.transform("3.14159265358979323846|5|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output2.get(0), "result=0"); - // case3: "3.14159265358979323846|3|4|8" - List output3 = processor.transform("3.14159265358979323846|3|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output3.get(0), "result=1"); - - transformSql = "select if((numeric3 < 4) and (numeric4 > 5),1,0) from source"; - config = new TransformConfig(transformSql); - // case4: "3.14159265358979323846|4|4|8" - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output4 = processor.transform("3.14159265358979323846|4|4|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output4.get(0), "result=0"); - // case5: "3.14159265358979323846|4|3.2|4" - List output5 = processor.transform("3.14159265358979323846|4|3.2|4"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output5.get(0), "result=0"); - // case6: "3.14159265358979323846|4|3.2|8" - List output6 = processor.transform("3.14159265358979323846|4|3.2|8"); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output6.get(0), "result=1"); - } -} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformProcessor.java deleted file mode 100644 index 8448260252d..00000000000 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformProcessor.java +++ /dev/null @@ -1,429 +0,0 @@ -/* - * 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; - -import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; -import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; -import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo; -import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.FieldInfo; -import org.apache.inlong.sdk.transform.pojo.JsonSourceInfo; -import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; -import org.apache.inlong.sdk.transform.pojo.KvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.PbSourceInfo; -import org.apache.inlong.sdk.transform.pojo.TransformConfig; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Base64; -import java.util.HashMap; -import java.util.List; - -/** - * TestTransformProcessor - * - */ -public class TestTransformProcessor { - - @Test - public void testCsv2Kv() throws Exception { - List fields = this.getTestFieldList("ftime", "extinfo"); - CsvSourceInfo csvSource = new CsvSourceInfo("UTF-8", '|', '\\', fields); - KvSinkInfo kvSink = new KvSinkInfo("UTF-8", fields); - String transformSql = "select ftime,extinfo from source where extinfo='ok'"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - List output1 = processor1.transform("2024-04-28 00:00:00|ok", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "ftime=2024-04-28 00:00:00&extinfo=ok"); - // case2 - config.setTransformSql("select ftime,extinfo from source where extinfo!='ok'"); - TransformProcessor processor2 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - List output2 = processor2.transform("2024-04-28 00:00:00|ok", new HashMap<>()); - Assert.assertEquals(0, output2.size()); - } - - @Test - public void testCsv2KvNoField() throws Exception { - CsvSourceInfo csvSource = new CsvSourceInfo("UTF-8", '|', '\\', null); - KvSinkInfo kvSink = new KvSinkInfo("UTF-8", null); - String transformSql = "select $1 ftime,$2 extinfo from source where $2='ok'"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor1.transform("2024-04-28 00:00:00|ok", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "ftime=2024-04-28 00:00:00&extinfo=ok"); - // case2 - config.setTransformSql("select $1 ftime,$2 extinfo from source where $2!='ok'"); - TransformProcessor processor2 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output2 = processor2.transform("2024-04-28 00:00:00|ok", new HashMap<>()); - Assert.assertEquals(0, output2.size()); - } - - @Test - public void testKv2Csv() throws Exception { - List fields = this.getTestFieldList("ftime", "extinfo"); - KvSourceInfo kvSource = new KvSourceInfo("UTF-8", fields); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select ftime,extinfo from source where extinfo='ok'"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - List output1 = processor1.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "2024-04-28 00:00:00|ok"); - // case2 - config.setTransformSql("select ftime,extinfo from source where extinfo!='ok'"); - TransformProcessor processor2 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - List output2 = processor2.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); - Assert.assertEquals(0, output2.size()); - } - - @Test - public void testKv2CsvNoField() throws Exception { - KvSourceInfo kvSource = new KvSourceInfo("UTF-8", null); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', null); - String transformSql = "select ftime,extinfo from source where extinfo='ok'"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - List output1 = processor1.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "2024-04-28 00:00:00|ok"); - // case2 - config.setTransformSql("select ftime,extinfo from source where extinfo!='ok'"); - TransformProcessor processor2 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - List output2 = processor2.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); - Assert.assertEquals(0, output2.size()); - } - - @Test - public void testJson2Csv() throws Exception { - List fields1 = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - JsonSourceInfo jsonSource1 = new JsonSourceInfo("UTF-8", "msgs"); - CsvSinkInfo csvSink1 = new CsvSinkInfo("UTF-8", '|', '\\', fields1); - String transformSql1 = "select $root.sid,$root.packageID,$child.msgTime,$child.msg from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createJsonDecoder(jsonSource1), - SinkEncoderFactory.createCsvEncoder(csvSink1)); - String srcString1 = "{\n" - + " \"sid\":\"value1\",\n" - + " \"packageID\":\"value2\",\n" - + " \"msgs\":[\n" - + " {\"msg\":\"value4\",\"msgTime\":1713243918000},\n" - + " {\"msg\":\"v4\",\"msgTime\":1713243918000}\n" - + " ]\n" - + "}"; - List output1 = processor1.transform(srcString1, new HashMap<>()); - Assert.assertEquals(2, output1.size()); - Assert.assertEquals(output1.get(0), "value1|value2|1713243918000|value4"); - Assert.assertEquals(output1.get(1), "value1|value2|1713243918000|v4"); - // case2 - List fields2 = this.getTestFieldList("id", "itemId", "subItemId", "msg"); - JsonSourceInfo jsonSource2 = new JsonSourceInfo("UTF-8", "items"); - CsvSinkInfo csvSink2 = new CsvSinkInfo("UTF-8", '|', '\\', fields2); - String transformSql2 = - "select $root.id,$child.itemId,$child.subItems(0).subItemId,$child.subItems(1).msg from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createJsonDecoder(jsonSource2), - SinkEncoderFactory.createCsvEncoder(csvSink2)); - String srcString2 = "{\n" - + " \"id\":\"value1\",\n" - + " \"name\":\"value2\",\n" - + " \"items\":[\n" - + " {\"itemId\":\"item1\",\n" - + " \"subItems\":[\n" - + " {\"subItemId\":\"1001\", \"msg\":\"1001msg\"},\n" - + " {\"subItemId\":\"1002\", \"msg\":\"1002msg\"}\n" - + " ]\n" - + " },\n" - + " {\"itemId\":\"item2\",\n" - + " \"subItems\":[\n" - + " {\"subItemId\":\"2001\", \"msg\":\"2001msg\"},\n" - + " {\"subItemId\":\"2002\", \"msg\":\"2002msg\"}\n" - + " ]\n" - + " }\n" - + " ]\n" - + "}"; - List output2 = processor2.transform(srcString2, new HashMap<>()); - Assert.assertEquals(2, output2.size()); - Assert.assertEquals(output2.get(0), "value1|item1|1001|1002msg"); - Assert.assertEquals(output2.get(1), "value1|item2|2001|2002msg"); - } - - @Test - public void testJson2CsvForOne() throws Exception { - List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - JsonSourceInfo jsonSource = new JsonSourceInfo("UTF-8", ""); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select $root.sid,$root.packageID,$root.msgs(1).msgTime,$root.msgs(0).msg from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createJsonDecoder(jsonSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - String srcString = "{\n" - + " \"sid\":\"value1\",\n" - + " \"packageID\":\"value2\",\n" - + " \"msgs\":[\n" - + " {\"msg\":\"value4\",\"msgTime\":1713243918000},\n" - + " {\"msg\":\"v4\",\"msgTime\":1713243918000}\n" - + " ]\n" - + "}"; - List output = processor.transform(srcString, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals(output.get(0), "value1|value2|1713243918000|value4"); - } - - @Test - public void testPb2Csv() throws Exception { - List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - String transformBase64 = this.getPbTestDescription(); - PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", "msgs"); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select $root.sid,$root.packageID,$child.msgTime,$child.msg from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createPbDecoder(pbSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - byte[] srcBytes = this.getPbTestData(); - List output = processor.transform(srcBytes); - Assert.assertEquals(2, output.size()); - Assert.assertEquals(output.get(0), "sid|1|1713243918000|msgValue4"); - Assert.assertEquals(output.get(1), "sid|1|1713243918002|msgValue42"); - } - - private List getTestFieldList(String... fieldNames) { - List fields = new ArrayList<>(); - for (String fieldName : fieldNames) { - FieldInfo field = new FieldInfo(); - field.setName(fieldName); - fields.add(field); - } - return fields; - } - - private byte[] getPbTestData() { - String srcString = - "CgNzaWQSIAoJbXNnVmFsdWU0ELCdrqruMRoMCgNrZXkSBXZhbHVlEiMKCm1zZ1ZhbHVlNDIQsp2uqu4xGg4KBGtleTISBnZhbHVlMhgB"; - byte[] srcBytes = Base64.getDecoder().decode(srcString); - return srcBytes; - } - - private String getPbTestDescription() { - final String transformProto = "syntax = \"proto3\";\n" - + "package test;\n" - + "message SdkMessage {\n" - + " bytes msg = 1;\n" - + " int64 msgTime = 2;\n" - + " map extinfo = 3;\n" - + "}\n" - + "message SdkDataRequest {\n" - + " string sid = 1;\n" - + " repeated SdkMessage msgs = 2;\n" - + " uint64 packageID = 3;\n" - + "}"; - String transformBase64 = "CrcCCg90cmFuc2Zvcm0ucHJvdG8SBHRlc3QirQEKClNka01lc3NhZ2USEAoDbXNnGAEgASgMUg" - + "Ntc2cSGAoHbXNnVGltZRgCIAEoA1IHbXNnVGltZRI3CgdleHRpbmZvGAMgAygLMh0udGVzdC5TZGtNZXNzYWdlLk" - + "V4dGluZm9FbnRyeVIHZXh0aW5mbxo6CgxFeHRpbmZvRW50cnkSEAoDa2V5GAEgASgJUgNrZXkSFAoFdmFsdWUY" - + "AiABKAlSBXZhbHVlOgI4ASJmCg5TZGtEYXRhUmVxdWVzdBIQCgNzaWQYASABKAlSA3NpZBIkCgRtc2dzGAIgAygLMh" - + "AudGVzdC5TZGtNZXNzYWdlUgRtc2dzEhwKCXBhY2thZ2VJRBgDIAEoBFIJcGFja2FnZUlEYgZwcm90bzM="; - return transformBase64; - } - - @Test - public void testPb2CsvForOne() throws Exception { - List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - String transformBase64 = this.getPbTestDescription(); - PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", null); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select $root.sid,$root.packageID,$root.msgs(1).msgTime,$root.msgs(0).msg from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createPbDecoder(pbSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - byte[] srcBytes = this.getPbTestData(); - List output = processor.transform(srcBytes, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals(output.get(0), "sid|1|1713243918002|msgValue4"); - } - - @Test - public void testPb2CsvForAdd() throws Exception { - List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - String transformBase64 = this.getPbTestDescription(); - PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", null); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select $root.sid," - + "($root.msgs(1).msgTime-$root.msgs(0).msgTime)/$root.packageID field2," - + "$root.packageID*($root.msgs(0).msgTime*$root.packageID+$root.msgs(1).msgTime/$root.packageID)" - + "*$root.packageID field3," - + "$root.msgs(0).msg field4 from source " - + "where $root.packageID<($root.msgs(0).msgTime+$root.msgs(1).msgTime" - + "+$root.msgs(0).msgTime+$root.msgs(1).msgTime)"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createPbDecoder(pbSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - byte[] srcBytes = this.getPbTestData(); - List output = processor.transform(srcBytes, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals(output.get(0), "sid|2|3426487836002|msgValue4"); - } - - @Test - public void testPb2CsvForConcat() throws Exception { - List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - String transformBase64 = this.getPbTestDescription(); - PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", "msgs"); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select $root.sid,$root.packageID,$child.msgTime," - + "concat($root.sid,$root.packageID,$child.msgTime,$child.msg) msg,$root.msgs.msgTime.msg from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createPbDecoder(pbSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - byte[] srcBytes = this.getPbTestData(); - List output = processor.transform(srcBytes, new HashMap<>()); - Assert.assertTrue(output.size() == 2); - Assert.assertEquals(output.get(0), "sid|1|1713243918000|sid11713243918000msgValue4"); - Assert.assertEquals(output.get(1), "sid|1|1713243918002|sid11713243918002msgValue42"); - } - - @Test - public void testPb2CsvForNow() throws Exception { - List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); - String transformBase64 = this.getPbTestDescription(); - PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", "msgs"); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); - String transformSql = "select now() from source"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createPbDecoder(pbSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - byte[] srcBytes = this.getPbTestData(); - List output = processor.transform(srcBytes, new HashMap<>()); - Assert.assertEquals(2, output.size()); - } - @Test - public void testCsv2Star() throws Exception { - List fields = this.getTestFieldList("ftime", "extinfo"); - CsvSourceInfo csvSource = new CsvSourceInfo("UTF-8", '|', '\\', fields); - CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', new ArrayList<>()); - String transformSql = "select *"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - - List output1 = processor1.transform("2024-04-28 00:00:00|ok", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "2024-04-28 00:00:00|ok"); - // case2 - config.setTransformSql("select * from source where extinfo!='ok'"); - TransformProcessor processor2 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - - List output2 = processor2.transform("2024-04-28 00:00:00|ok", new HashMap<>()); - Assert.assertEquals(0, output2.size()); - // case3 - config.setTransformSql("select *,extinfo,ftime from source where extinfo!='ok'"); - TransformProcessor processor3 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createCsvEncoder(csvSink)); - - List output3 = processor3.transform("2024-04-28 00:00:00|nok", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "2024-04-28 00:00:00|nok|nok|2024-04-28 00:00:00"); - // case4 - CsvSourceInfo csvSourceNoField = new CsvSourceInfo("UTF-8", '|', '\\', new ArrayList<>()); - CsvSinkInfo csvSinkNoField = new CsvSinkInfo("UTF-8", '|', '\\', new ArrayList<>()); - config.setTransformSql("select *,$2,$1 from source where $2='nok'"); - TransformProcessor processor4 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSourceNoField), - SinkEncoderFactory.createCsvEncoder(csvSinkNoField)); - - List output4 = processor4.transform("2024-04-28 00:00:00|nok", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "2024-04-28 00:00:00|nok|nok|2024-04-28 00:00:00"); - } - - @Test - public void testKv2Star() throws Exception { - List fields = this.getTestFieldList("ftime", "extinfo"); - KvSourceInfo kvSource = new KvSourceInfo("UTF-8", fields); - KvSinkInfo kvSink = new KvSinkInfo("UTF-8", new ArrayList<>()); - String transformSql = "select *"; - TransformConfig config = new TransformConfig(transformSql); - // case1 - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor1.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "ftime=2024-04-28 00:00:00&extinfo=ok"); - // case2 - config.setTransformSql("select * from source where extinfo!='ok'"); - TransformProcessor processor2 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output2 = processor2.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); - Assert.assertEquals(0, output2.size()); - // case3 - config.setTransformSql("select *,extinfo e1,ftime f1 from source where extinfo!='ok'"); - TransformProcessor processor3 = TransformProcessor - .create(config, SourceDecoderFactory.createKvDecoder(kvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - List output3 = processor3.transform("ftime=2024-04-28 00:00:00&extinfo=nok", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "ftime=2024-04-28 00:00:00&extinfo=nok&e1=nok&f1=2024-04-28 00:00:00"); - } -} 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 deleted file mode 100644 index f099d728dc7..00000000000 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java +++ /dev/null @@ -1,759 +0,0 @@ -/* - * 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; - -import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; -import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; -import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.FieldInfo; -import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; -import org.apache.inlong.sdk.transform.pojo.TransformConfig; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -/** - * TestTransformStringFunctionsProcessor - * description: test the string functions in transform processor - */ -public class TestTransformStringFunctionsProcessor { - - private static final List srcFields = new ArrayList<>(); - private static final List dstFields = new ArrayList<>(); - private static final CsvSourceInfo csvSource; - private static final KvSinkInfo kvSink; - - static { - for (int i = 1; i < 4; i++) { - FieldInfo field = new FieldInfo(); - field.setName("string" + i); - srcFields.add(field); - } - for (int i = 1; i < 4; i++) { - FieldInfo field = new FieldInfo(); - field.setName("numeric" + i); - srcFields.add(field); - } - FieldInfo field = new FieldInfo(); - field.setName("result"); - dstFields.add(field); - csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); - 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"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: substring('banana', 2) - List output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=anana"); - String transformSql2 = "select substring(string1, numeric1, numeric3) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: substring('apple', 1, 3) - List output2 = processor2.transform("apple|banana|cloud|1|1|3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=app"); - // case3: substring('apple', 2, 9) - List output3 = processor2.transform("apple|banana|cloud|2|1|9", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=pple"); - } - - @Test - public void testLocateFunction() throws Exception { - String transformSql1 = "select locate(string1, string2) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: locate('app', 'apple') - List output1 = processor1.transform("app|apple|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1"); - // case2: locate('ape', 'apple') - List output2 = processor1.transform("ape|apple|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=0"); - String transformSql2 = "select locate(string1, string2, numeric1) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: locate('app', 'appapp', 2) - List output3 = processor2.transform("app|appapp|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=4"); - // case4: locate('app', 'appape', 2) - List output4 = processor2.transform("app|appape|cloud|2|1|9", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=0"); - // case5: locate('app', null) - List output5 = processor1.transform("app", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - Assert.assertEquals(output5.get(0), "result=null"); - } - - @Test - public void testSpaceFunction() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - transformSql = "select space(numeric1) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: space(5) - data = "hello world|banana|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result= ", output.get(0)); - - // case2: space(-1) - data = "hello world|banana|cloud|-1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=", output.get(0)); - - // case3: space(null) - transformSql = "select space(xxd) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "hello world|banana|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - } - - @Test - public void testReplicateFunction() throws Exception { - String transformSql1 = "select replicate(string1, numeric1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: replicate('apple', 2) - List 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 processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: replicate('banana', 3) - List 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 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 processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List 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 processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: trim(' in long') - List 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 processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: trim('in long ') - List 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 processor3 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: trim(' in long ') - List output3 = processor3.transform(" in long|in long | in long ", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=in long"); - } - - @Test - public void testReverseFunction() throws Exception { - String transformSql1 = "select reverse(string1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: reverse('apple') - List output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=elppa"); - // case2: reverse('ban ana ') - String transformSql2 = "select reverse(string2) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output2 = processor2.transform("apple|ban ana |cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result= ana nab"); - // case3: reverse(12345) - List output3 = processor1.transform("12345|banana|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=54321"); - // case4: reverse(null) - List output4 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result="); - } - - @Test - public void testToBase64Function() throws Exception { - String transformSql = "select to_base64(string1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // to_base64('app-fun') - List output1 = processor1.transform("app-fun", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=YXBwLWZ1bg=="); - // to_base64('hello world') - List output2 = processor1.transform("hello world", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=aGVsbG8gd29ybGQ="); - } - - @Test - public void testLengthFunction() throws Exception { - String transformSql = "select length(string1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: length('hello world') - List output1 = processor1.transform("hello world|apple|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=11", output1.get(0)); - - transformSql = "select length(xxd) from source"; - config = new TransformConfig(transformSql); - processor1 = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: length(null) - output1 = processor1.transform("hello world|apple|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=null", output1.get(0)); - } - - @Test - public void testReplaceFunction() throws Exception { - String transformSql = "select replace(string1, string2, string3) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: replace('hooray', 'oray', 'lly') - List output1 = processor.transform("hooray|oray|lly", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=holly"); - // case2: replace('hooray', 'hook', 'hoor') - List output2 = processor.transform("hooray|hook|hoor", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=hooray"); - // case3: replace('Hello World', 'World', '') - List output3 = processor.transform("Hello World|World|", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=Hello "); - // case4: replace('Hello World', '', 'J') - List output4 = processor.transform("Hello World||J", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=JHJeJlJlJoJ JWJoJrJlJdJ"); - // case5: replace('', '', '') - List output5 = processor.transform("||", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - Assert.assertEquals(output5.get(0), "result="); - // case6: replace('abababab', 'ab', 'cd') - List output6 = processor.transform("abababab|ab|cd", new HashMap<>()); - Assert.assertEquals(1, output6.size()); - Assert.assertEquals(output6.get(0), "result=cdcdcdcd"); - // case7: replace('aaa', 'aa', 'd') - List output7 = processor.transform("aaa|aa|d", new HashMap<>()); - Assert.assertEquals(1, output7.size()); - Assert.assertEquals(output7.get(0), "result=da"); - } - - @Test - public void testStrcmpFunction() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - transformSql = "select strcmp(string1,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: strcmp('hello world','banana') - data = "hello world|banana|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=1", output.get(0)); - - // case2: strcmp('hello world','hello world') - data = "hello world|hello world|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=0", output.get(0)); - - // case3: strcmp('hello world','zzzzz') - data = "hello world|zzzzz|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=-1", output.get(0)); - - // case4: strcmp('hello world',null) - transformSql = "select strcmp(string1,xxd) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "hello world|zzzzz|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - } - - @Test - public void testRpadFunction() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - transformSql = "select rpad(string1,numeric1,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: rpad('he',7,'xxd') - data = "he|xxd|cloud|7|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=hexxdxx", output.get(0)); - - // case2: rpad('he',1,'xxd') - data = "he|xxd|cloud|1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=h", output.get(0)); - - // case3: rpad('he',1,'') - data = "he||cloud|1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=h", output.get(0)); - - // case4: rpad('he',-1,'xxd') - data = "he|xxd|cloud|-1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case5: rpad(null,5,'xxd') - transformSql = "select rpad(xxd,numeric1,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "he|xxd|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case6: rpad('he',null,'xxd') - transformSql = "select rpad(string1,xxd,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "he|xxd|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case7: rpad('he',5,null) - transformSql = "select rpad(string1,numeric1,xxd) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "he|xxd|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - } - - @Test - public void testLpadFunction() throws Exception { - String transformSql = null, data = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - transformSql = "select lpad(string1,numeric1,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: lpad('he',7,'xxd') - data = "he|xxd|cloud|7|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=xxdxxhe", output.get(0)); - - // case2: lpad('he',1,'xxd') - data = "he|xxd|cloud|1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=h", output.get(0)); - - // case3: lpad('he',1,'') - data = "he||cloud|1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=h", output.get(0)); - - // case4: lpad('he',-1,'xxd') - data = "he|xxd|cloud|-1|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case5: lpad(null,5,'xxd') - transformSql = "select lpad(xxd,numeric1,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "he|xxd|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case6: lpad('he',null,'xxd') - transformSql = "select lpad(string1,xxd,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "he|xxd|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case7: lpad('he',5,null) - transformSql = "select lpad(string1,numeric1,xxd) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - data = "he|xxd|cloud|5|3|3"; - output = processor.transform(data, new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - } - - @Test - public void testRightFunction() throws Exception { - String transformSql = "select right(string1,numeric1) from source"; - TransformConfig config = new TransformConfig(transformSql); - TransformProcessor 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 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 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 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)); - } - - @Test - public void testTranslateFunction() throws Exception { - String transformSql1 = "select translate(string1, string2, string3) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: translate("hello word!", "el", "EL") - List output1 = processor1.transform("hello word!|el|EL|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=hELLo word!"); - String transformSql2 = "select translate(string3, string1, string2) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: translate("hello word!", "el", "EL") - List output2 = processor2.transform("el|EL|hello word!|1|1|3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=hELLo word!"); - // case3: translate('ApaCHe Inlong', CH, ch) - List output3 = processor2.transform("CH|ch|ApaCHe Inlong|2|1|9", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=Apache Inlong"); - } - - @Test - public void testInsertFunction() throws Exception { - String transformSql1 = "select insert(string1, numeric1, numeric2, string2) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case1: insert('12345678', 3, 4, 'word') -> '12word78' - List output1 = processor1.transform("12345678|word|cloud|3|4|0", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=12word78", output1.get(0)); - - // case2: insert('12345678', -1, 4, 'word') -> '12345678' - List output2 = processor1.transform("12345678|word|cloud|-1|4|0", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=12345678", output2.get(0)); - - // case3: insert('12345678', 3, 100, 'word') -> '12word' - List output3 = processor1.transform("12345678|word|cloud|3|100|0", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals("result=12word", output3.get(0)); - - // case4: insert('', 3, 4, 'word') -> '' - List output4 = processor1.transform("|word|cloud|3|4|0", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals("result=", output4.get(0)); - - // case5: insert('12345678', 3, 4, '') -> '1278' - List output5 = processor1.transform("12345678||cloud|3|4|0", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - 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 processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: contains('Transform SQL', 'SQL') - List output1 = processor.transform("Transform SQL|SQL", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=true"); - // case2: contains('', 'SQL') - List output2 = processor.transform("|SQL", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=false"); - // case3: contains('Transform SQL', '') - List 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 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 output5 = processor.transform("Transform SQL|m S", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - Assert.assertEquals(output5.get(0), "result=true"); - // case6: contains('', '') - List output6 = processor.transform("|", new HashMap<>()); - Assert.assertEquals(1, output6.size()); - Assert.assertEquals(output6.get(0), "result=true"); - } -} 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 deleted file mode 100644 index 14c1fab04ba..00000000000 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java +++ /dev/null @@ -1,562 +0,0 @@ -/* - * 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; - -import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; -import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; -import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.FieldInfo; -import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; -import org.apache.inlong.sdk.transform.pojo.TransformConfig; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.time.Duration; -import java.time.LocalDate; -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; -import java.util.TimeZone; - -/** - * TestTransformTemporalFunctionsProcessor - * description: test the temporal functions in transform processor - */ -public class TestTransformTemporalFunctionsProcessor { - - private static final List srcFields = new ArrayList<>(); - private static final List dstFields = new ArrayList<>(); - private static final CsvSourceInfo csvSource; - private static final KvSinkInfo kvSink; - - static { - for (int i = 1; i < 4; i++) { - FieldInfo field = new FieldInfo(); - field.setName("string" + i); - srcFields.add(field); - } - for (int i = 1; i < 4; i++) { - FieldInfo field = new FieldInfo(); - field.setName("numeric" + i); - srcFields.add(field); - } - FieldInfo field = new FieldInfo(); - field.setName("result"); - dstFields.add(field); - csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); - kvSink = new KvSinkInfo("UTF-8", dstFields); - } - - @Before - public void setUp() { - TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); - } - - @Test - public void testToDateFunction() throws Exception { - String transformSql1 = "select to_date(string1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: to_date('2024-08-15') - List output1 = processor1.transform("2024-08-15|apple|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=2024-08-15"); - String transformSql2 = "select to_date(string1, string2) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: to_date('20240815', 'yyyyMMdd') - List output2 = processor2.transform("20240815|yyyyMMdd|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=2024-08-15"); - // case3: to_date('08152024', 'MMddyyyy') - List output3 = processor2.transform("08152024|MMddyyyy|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=2024-08-15"); - // case4: to_date('2024/08/15', 'yyyy/MM/dd') - List output4 = processor2.transform("2024/08/15|yyyy/MM/dd|cloud|2|1|3", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=2024-08-15"); - } - - @Test - public void testDateFormatFunction() throws Exception { - String transformSql1 = "select date_format(string1, 'yyyy-MM-dd HH:mm:ss') from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: date_format('2024-08-01 22:56:56', 'yyyy-MM-dd HH:mm:ss') - List output1 = processor1.transform("2024-08-01 22:56:56", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=2024-08-01 22:56:56"); - - String transformSql2 = "select date_format(string1, 'yyyy-MM-dd') from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: date_format('2024-08-01 22:56:56', 'yyyy-MM-dd') - List output2 = processor2.transform("2024-08-01 22:56:56", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=2024-08-01"); - - String transformSql3 = "select date_format(string1, 'yyyyMMddHHmmss') from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: date_format('2024-08-01 22:56:56', 'yyyyMMddHHmmss') - List output3 = processor3.transform("2024-08-01 22:56:56", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=20240801225656"); - - String transformSql4 = "select date_format(string1, 'yyyy/MM/dd HH:mm:ss') from source"; - TransformConfig config4 = new TransformConfig(transformSql4); - TransformProcessor processor4 = TransformProcessor - .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case4: date_format('2024-08-01 22:56:56', 'yyyy/MM/dd HH:mm:ss') - List output4 = processor4.transform("2024-08-01 22:56:56", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=2024/08/01 22:56:56"); - } - - @Test - public void testDateExtractFunction() throws Exception { - String transformSql1 = "select year(string1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: year(2024-08-08) - List output1 = processor1.transform("2024-08-08", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=2024"); - - String transformSql2 = "select quarter(string1) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: quarter(2024-08-08) - List output2 = processor2.transform("2024-08-08", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=3"); - - String transformSql3 = "select month(string1) from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: month(2024-08-08) - List output3 = processor3.transform("2024-08-08", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=8"); - - String transformSql4 = "select week(string1) from source"; - TransformConfig config4 = new TransformConfig(transformSql4); - TransformProcessor processor4 = TransformProcessor - .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case4: week(2024-02-29) - List output4 = processor4.transform("2024-02-29", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=9"); - - String transformSql5 = "select day_of_year(string1) from source"; - TransformConfig config5 = new TransformConfig(transformSql5); - TransformProcessor processor5 = TransformProcessor - .create(config5, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case5: dayofyear(2024-02-29) - List output5 = processor5.transform("2024-02-29", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - Assert.assertEquals(output5.get(0), "result=60"); - - String transformSql6 = "select day_of_month(string1) from source"; - TransformConfig config6 = new TransformConfig(transformSql6); - TransformProcessor processor6 = TransformProcessor - .create(config6, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case6: dayofmonth(2024-02-29) - List output6 = processor6.transform("2024-02-29", new HashMap<>()); - Assert.assertEquals(1, output6.size()); - Assert.assertEquals(output6.get(0), "result=29"); - - String transformSql7 = "select day_of_week(string1) from source"; - TransformConfig config7 = new TransformConfig(transformSql7); - TransformProcessor processor7 = TransformProcessor - .create(config7, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case7: dayofweek(2024-02-29) (Thursday) - List output7 = processor7.transform("2024-02-29", new HashMap<>()); - Assert.assertEquals(1, output7.size()); - Assert.assertEquals(output7.get(0), "result=5"); - - String transformSql8 = "select dayname(string1) from source"; - TransformConfig config8 = new TransformConfig(transformSql8); - TransformProcessor processor8 = TransformProcessor - .create(config8, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case8: dayname(2024-02-29) (Thursday) - List output8 = processor8.transform("2024-02-29", new HashMap<>()); - Assert.assertEquals(1, output8.size()); - Assert.assertEquals(output8.get(0), "result=THURSDAY"); - } - - @Test - public void testTimestampExtractFunction() throws Exception { - String transformSql1 = "select hour(string1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: hour(2024-08-12 12:23:34) - List output1 = processor1.transform("2024-08-12 12:23:34", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=12"); - - String transformSql2 = "select minute(string1) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: minute(2024-08-12 12:23:34) - List output2 = processor2.transform("2024-08-12 12:23:34", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=23"); - - String transformSql3 = "select second(string1) from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: second(2024-08-12 12:23:34) - List output3 = processor3.transform("2024-08-12 12:23:34", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=34"); - } - - @Test - public void testFromUnixTimeFunction() throws Exception { - String transformSql1 = "select from_unix_time(numeric1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: from_unix_time(44) - List output1 = processor1.transform("can|apple|cloud|44|1|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1970-01-01 08:00:44"); - - String transformSql2 = "select from_unix_time(numeric1, 'yyyy/MM/dd HH:mm:ss') from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: from_unix_time(44, 'yyyy/MM/dd HH:mm:ss') - List output2 = processor2.transform("can|apple|cloud|44|1|3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1970/01/01 08:00:44"); - - String transformSql3 = "select from_unix_time(numeric1, 'MMdd-yyyy') from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: from_unix_time(44, 'MMdd-yyyy') - List output3 = processor3.transform("can|apple|cloud|44|1|3", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=0101-1970"); - - String transformSql4 = "select from_unix_time(numeric1, 'yyyyMMddHHss') from source"; - TransformConfig config4 = new TransformConfig(transformSql4); - TransformProcessor processor4 = TransformProcessor - .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case4: from_unix_time(44, 'yyyyMMddHHss') - List output4 = processor4.transform("can|apple|cloud|44|1|3", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=197001010844"); - } - - @Test - public void testUnixTimestampFunction() throws Exception { - String transformSql1 = "select unix_timestamp() from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: unix_timestamp() - List output1 = processor1.transform("", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - - String transformSql2 = "select unix_timestamp(string1, 'yyyy/MM/dd HH:mm:ss') from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: unix_timestamp('1970/01/01 08:00:44', 'yyyy/MM/dd HH:mm:ss') - List output2 = processor2.transform("1970/01/01 08:00:44", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=44"); - - String transformSql3 = "select unix_timestamp(string1) from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: unix_timestamp('1970/01/01 08:00:44') - List output3 = processor3.transform("1970-01-01 08:00:44", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=44"); - - String transformSql4 = "select unix_timestamp(string1, 'yyyyMMddHHmmss') from source"; - TransformConfig config4 = new TransformConfig(transformSql4); - TransformProcessor processor4 = TransformProcessor - .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case4: unix_timestamp('19700101080044', 'yyyyMMddHHss') - List output4 = processor4.transform("19700101080044", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=44"); - } - - @Test - public void testToTimestampFunction() throws Exception { - String transformSql1 = "select to_timestamp(string1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: to_timestamp('1970-01-01 00:00:44') - List output1 = processor1.transform("1970-01-01 00:00:44", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(output1.get(0), "result=1970-01-01 00:00:44.0"); - - String transformSql2 = "select to_timestamp(string1, 'yyyy/MM/dd HH:mm:ss') from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case2: to_timestamp('1970/01/01 00:00:44', 'yyyy/MM/dd HH:mm:ss') - List output2 = processor2.transform("1970/01/01 00:00:44", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(output2.get(0), "result=1970-01-01 00:00:44.0"); - - String transformSql3 = "select to_timestamp(string1, 'yyyyMMddHHmmss') from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case3: to_timestamp('19700101000044', 'yyyyMMddHHmmss') - List output3 = processor3.transform("19700101000044", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(output3.get(0), "result=1970-01-01 00:00:44.0"); - - String transformSql4 = "select to_timestamp(string1, 'yyyy-MM-dd') from source"; - TransformConfig config4 = new TransformConfig(transformSql4); - TransformProcessor processor4 = TransformProcessor - .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case4: to_timestamp('1970-01-01', 'yyyy-MM-dd') - List output4 = processor4.transform("1970-01-01", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals(output4.get(0), "result=1970-01-01 00:00:00.0"); - } - - @Test - public void testDateDiffFunction() throws Exception { - String transformSql = null; - TransformConfig config = null; - TransformProcessor processor = null; - List output = null; - - transformSql = "select datediff(string1,string2) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: datediff('1970-01-01','1970-01-02') - output = processor.transform("1970-01-01|1970-01-02", new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=-1", output.get(0)); - - // case2: datediff('1970-01-02','1970-01-01') - output = processor.transform("1970-01-02|1970-01-01", new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=1", output.get(0)); - - // case3: datediff('2018-12-10 12:30:00', '2018-12-09 13:30:00') - output = processor.transform("2018-12-10 12:30:00|2018-12-09 13:30:00", new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=1", output.get(0)); - - // case4: datediff('2018-12-10 12:30:00', '') - output = processor.transform("2018-12-10 12:30:00|", new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case5: datediff('2018-12', '2018-12-12') - output = processor.transform("2018-12|2018-12-12", new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(0)); - - // case6: datediff('1970-01-01',null) - transformSql = "select datediff(string1,xxd) from source"; - config = new TransformConfig(transformSql); - processor = TransformProcessor - .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - output = processor.transform("1970-01-01|1970-01-02", new HashMap<>()); - Assert.assertEquals(1, output.size()); - Assert.assertEquals("result=null", output.get(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 current_time('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); - } - - @Test - public void testLocalDateFunction() throws Exception { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - - // case1: localDate() - default system time zone - String transformSql1 = "select localdate() from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output1 = processor1.transform("", new HashMap<>()); - LocalDate expectedDate1 = LocalDate.now(ZoneId.systemDefault()); - LocalDate actualDate1 = LocalDate.parse(output1.get(0).split("=")[1], formatter); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals(expectedDate1, actualDate1); - - // case2: localDate("UTC") - String transformSql2 = "select localdate('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<>()); - LocalDate expectedDate2 = LocalDate.now(ZoneId.of("UTC")); - LocalDate actualDate2 = LocalDate.parse(output2.get(0).split("=")[1], formatter); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals(expectedDate2, actualDate2); - - // case3: localDate("UTC-12") - String transformSql3 = "select localdate('UTC-12') from source"; - TransformConfig config3 = new TransformConfig(transformSql3); - TransformProcessor processor3 = TransformProcessor - .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - List output3 = processor3.transform("", new HashMap<>()); - LocalDate expectedDate3 = LocalDate.now(ZoneId.of("UTC-12")); - LocalDate actualDate3 = LocalDate.parse(output3.get(0).split("=")[1], formatter); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals(expectedDate3, actualDate3); - } - - @Test - public void testTimestampAdd() throws Exception { - String transformSql1 = "select timestamp_add('day',string2,string1) from source"; - TransformConfig config1 = new TransformConfig(transformSql1); - TransformProcessor processor1 = TransformProcessor - .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - // case1: timestamp_add('day',3,'1970-01-01') - List output1 = processor1.transform("1970-01-01|3", new HashMap<>()); - Assert.assertEquals(1, output1.size()); - Assert.assertEquals("result=1970-01-04", output1.get(0)); - - // case2: timestamp_add('day',-3,'1970-01-01 00:00:44') - List output2 = processor1.transform("1970-01-01 00:00:44|-3", new HashMap<>()); - Assert.assertEquals(1, output2.size()); - Assert.assertEquals("result=1969-12-29 00:00:44", output2.get(0)); - - String transformSql2 = "select timestamp_add('MINUTE',string2,string1) from source"; - TransformConfig config2 = new TransformConfig(transformSql2); - TransformProcessor processor2 = TransformProcessor - .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), - SinkEncoderFactory.createKvEncoder(kvSink)); - - // case3: timestamp_add('MINUTE',3,'1970-01-01 00:00:44') - List output3 = processor2.transform("1970-01-01 00:00:44|3", new HashMap<>()); - Assert.assertEquals(1, output3.size()); - Assert.assertEquals("result=1970-01-01 00:03:44", output3.get(0)); - - // case4: timestamp_add('MINUTE',-3,'1970-01-01 00:00:44') - List output4 = processor2.transform("1970-01-01 00:00:44|-3", new HashMap<>()); - Assert.assertEquals(1, output4.size()); - Assert.assertEquals("result=1969-12-31 23:57:44", output4.get(0)); - - // case5: timestamp_add('MINUTE',-3,'1970-01-01') - List output5 = processor2.transform("1970-01-01|-3", new HashMap<>()); - Assert.assertEquals(1, output5.size()); - Assert.assertEquals("result=1969-12-31 23:57:00", output5.get(0)); - } -} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/AbstractFunctionArithmeticTestBase.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/AbstractFunctionArithmeticTestBase.java new file mode 100644 index 00000000000..e2963916526 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/AbstractFunctionArithmeticTestBase.java @@ -0,0 +1,50 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; + +import java.util.ArrayList; +import java.util.List; +/** + * AbstractFunctionArithmeticTestBase + * description: define static parameters for ArithmeticFunction tests + */ +public abstract class AbstractFunctionArithmeticTestBase { + + protected static final List srcFields = new ArrayList<>(); + protected static final List dstFields = new ArrayList<>(); + protected static final CsvSourceInfo csvSource; + protected static final KvSinkInfo kvSink; + + static { + for (int i = 1; i < 5; i++) { + FieldInfo field = new FieldInfo(); + field.setName("numeric" + i); + srcFields.add(field); + } + FieldInfo field = new FieldInfo(); + field.setName("result"); + dstFields.add(field); + csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); + kvSink = new KvSinkInfo("UTF-8", dstFields); + } + +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAbsFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAbsFunction.java new file mode 100644 index 00000000000..1cecd8f32f9 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAbsFunction.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestAbsFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testAbsFunction() throws Exception { + String transformSql = "select abs(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: |2| + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("2|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=2"); + // case2: |-4.25| + List output2 = processor.transform("-4.25|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=4.25"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAcosFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAcosFunction.java new file mode 100644 index 00000000000..61e24fd3dbc --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAcosFunction.java @@ -0,0 +1,52 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestAcosFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testAcosFunction() throws Exception { + String transformSql = "select acos(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: acos(1) + List output1 = processor.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + // case2: acos(0) + List output2 = processor.transform("0|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1.5707963267948966"); + // case3: acos(-1) + List output3 = processor.transform("-1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=3.141592653589793"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestArithmeticFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestArithmeticFunctionsProcessor.java new file mode 100644 index 00000000000..58faece12bb --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestArithmeticFunctionsProcessor.java @@ -0,0 +1,39 @@ +/* + * 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.arithmetic; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +/** + * TestArithmeticFunctionProcessor + * description: test all the arithmetic functions in transform processor + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + TestAbsFunction.class, TestAcosFunction.class, TestBinFunction.class, TestBinFunction.class, + TestCeilFunction.class, TestCosFunction.class, TestExpFunction.class, TestFloorFunction.class, + TestHexFunction.class, TestIfNullFunction.class, TestLnFunction.class, TestLog2Function.class, + TestLogFunction.class, TestLog10Function.class, TestMd5Function.class, TestModuloFunction.class, + TestPiFunction.class, TestPowerFunction.class, TestRadiansFunction.class, TestRandFunction.class, + TestRoundFunction.class, TestSha2Function.class, TestShaFunction.class, TestSignFunction.class, + TestSinFunction.class, TestSinhFunction.class, TestSqrtFunction.class, TestTanFunction.class, + TestAsinFunction.class, TestAtanFunction.class, TestAtan2Function.class, TestCoshFunction.class, + TestCoshFunction.class, TestTanhFunction.class +}) +public class TestArithmeticFunctionsProcessor { +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAsinFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAsinFunction.java new file mode 100644 index 00000000000..5347e1fc32f --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAsinFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestAsinFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testAsinFunction() throws Exception { + String transformSql = "select asin(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: asin(0.5) + List output1 = processor.transform("0.5|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.5235987755982989"); + + // case2: asin(0) + List output2 = processor.transform("0|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=0.0"); + + // case3: asin(-0.5) + List output3 = processor.transform("-0.5|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-0.5235987755982989"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAtan2Function.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAtan2Function.java new file mode 100644 index 00000000000..ba77363c871 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAtan2Function.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestAtan2Function extends AbstractFunctionArithmeticTestBase { + + @Test + public void testAtan2Function() throws Exception { + String transformSql = "select atan2(numeric1, numeric2) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: atan2(1, 1) + List output1 = processor.transform("1|1|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.7853981633974483"); + + // case2: atan2(1, 0) + List output2 = processor.transform("1|0|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1.5707963267948966"); + + // case3: atan2(0, -1) + List output3 = processor.transform("0|-1|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=3.141592653589793"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAtanFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAtanFunction.java new file mode 100644 index 00000000000..94710326334 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestAtanFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestAtanFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testAtanFunction() throws Exception { + String transformSql = "select atan(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: atan(1) + List output1 = processor.transform("1|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.7853981633974483"); + + // case2: atan(0) + List output2 = processor.transform("0|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=0.0"); + + // case3: atan(-1) + List output3 = processor.transform("-1|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-0.7853981633974483"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestBinFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestBinFunction.java new file mode 100644 index 00000000000..be69e35b6f4 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestBinFunction.java @@ -0,0 +1,53 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestBinFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testBinFunction() throws Exception { + String transformSql1 = "select bin(numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: bin(4) + List output1 = processor1.transform("4|5|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=100"); + String transformSql2 = "select bin() from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: bin() + List output2 = processor2.transform("1|2|3|4", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=null"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCeilFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCeilFunction.java new file mode 100644 index 00000000000..7cd946fe62c --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCeilFunction.java @@ -0,0 +1,53 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestCeilFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testCeilFunction() throws Exception { + String transformSql = "select ceil(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: ceil(1.23) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("1.23|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=2.0"); + // case2: ceil(3) + List output2 = processor.transform("3|-2|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=3.0"); + // case3: ceil(-5.67) + List output3 = processor.transform("-5.67|0.5|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-5.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCosFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCosFunction.java new file mode 100644 index 00000000000..d0d2d3d2aa2 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCosFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestCosFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testCosFunction() throws Exception { + String transformSql = "select cos(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: cos(0) + List output1 = processor.transform("0|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCoshFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCoshFunction.java new file mode 100644 index 00000000000..29c20fda372 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCoshFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestCoshFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testCoshFunction() throws Exception { + String transformSql = "select cosh(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: cosh(1) + List output1 = processor.transform("1|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1.543080634815244"); + + // case2: cosh(0) + List output2 = processor.transform("0|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1.0"); + + // case3: cosh(-1) + List output3 = processor.transform("-1|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=1.543080634815244"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCotFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCotFunction.java new file mode 100644 index 00000000000..28dbf023574 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestCotFunction.java @@ -0,0 +1,54 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; +public class TestCotFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testCotFunction() throws Exception { + String transformSql = "select cot(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: cot(1) + List output1 = processor.transform("1|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.6420926159343306"); + + // case2: cot(0.5) + List output2 = processor.transform("0.5|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1.830487721712452"); + + // case3: cot(-1) + List output3 = processor.transform("-1|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-0.6420926159343306"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestExpFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestExpFunction.java new file mode 100644 index 00000000000..0eb470a9cb6 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestExpFunction.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestExpFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testExpFunction() throws Exception { + String transformSql = "select exp(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: e^0 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("0|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1.0"); + // case2: e^2 + List output2 = processor.transform("2|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=7.38905609893065"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestFloorFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestFloorFunction.java new file mode 100644 index 00000000000..ecc5160c5b2 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestFloorFunction.java @@ -0,0 +1,53 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestFloorFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testFloorFunction() throws Exception { + String transformSql = "select floor(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: floor(1.23) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("1.23|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1.0"); + // case2: floor(3) + List output2 = processor.transform("3|-2|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=3.0"); + // case3: floor(-5.67) + List output3 = processor.transform("-5.67|0.5|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-6.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestHexFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestHexFunction.java new file mode 100644 index 00000000000..81a5ef7cb1b --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestHexFunction.java @@ -0,0 +1,60 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestHexFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testHexFunction() throws Exception { + String transformSql1 = "select hex(numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: hex(1007) + List output1 = processor1.transform("1007|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=3EF"); + // case: hex(3.14) + List output2 = processor1.transform("3.14|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=3"); + // case: hex(3.141592653589793) + List output3 = processor1.transform("3.141592653589793|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=3"); + // case: hex(-9223372036854775808) + List output4 = processor1.transform("-9223372036854775808|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=8000000000000000"); + // case: hex(abc) + List output5 = processor1.transform("abc|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result=616263"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestIfNullFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestIfNullFunction.java new file mode 100644 index 00000000000..5e86e534e17 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestIfNullFunction.java @@ -0,0 +1,95 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestIfNullFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testIfNullFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: ifnull(5, 3) + transformSql = "select ifnull(numeric1,numeric2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "5|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=5", output.get(0)); + + // case2: ifnull(null,3) + transformSql = "select ifnull(xxd,numeric2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "5|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=3", output.get(0)); + + // case3: ifnull(6 / 3,'YES') + transformSql = "select ifnull(numeric1 / numeric2,'YES') from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "6|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2", output.get(0)); + + // case4: ifnull(6 / 0,'YES') + transformSql = "select ifnull(numeric1 / numeric2,'YES') from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "6|0|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=YES", output.get(0)); + + // case5: ifnull(6 / 0,3 / 0) + transformSql = "select ifnull(numeric1 / numeric2,numeric3 / numeric2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "6|0|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLnFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLnFunction.java new file mode 100644 index 00000000000..d05ebb9ff88 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLnFunction.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLnFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testLnFunction() throws Exception { + String transformSql = "select ln(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: ln(1) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + // case2: ln(10) + List output2 = processor.transform("10|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=2.302585092994046"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLog10Function.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLog10Function.java new file mode 100644 index 00000000000..d2d3827fa67 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLog10Function.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLog10Function extends AbstractFunctionArithmeticTestBase { + + @Test + public void testLog10Function() throws Exception { + String transformSql = "select log10(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: log10(1) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + // case2: log10(1000) + List output2 = processor.transform("1000|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=3.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLog2Function.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLog2Function.java new file mode 100644 index 00000000000..67cc397921e --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLog2Function.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLog2Function extends AbstractFunctionArithmeticTestBase { + + @Test + public void testLog2Function() throws Exception { + String transformSql = "select log2(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: log2(1) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + // case2: log2(32) + List output2 = processor.transform("32|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=5.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLogFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLogFunction.java new file mode 100644 index 00000000000..d154ad8676d --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestLogFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLogFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testLogFunction() throws Exception { + String transformSql1 = "select log(numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + // case1: ln(1) + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor1.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + String transformSql2 = "select log(numeric1, numeric2) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + // case2: log2(8) + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output2 = processor2.transform("2|8|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=3.0"); + // case3: log10(100) + TransformProcessor processor3 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor3.transform("10|100|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=2.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestMd5Function.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestMd5Function.java new file mode 100644 index 00000000000..47f3c92d00f --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestMd5Function.java @@ -0,0 +1,64 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestMd5Function extends AbstractFunctionArithmeticTestBase { + + @Test + public void testMd5Function() throws Exception { + String transformSql = "select md5(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: md5("1") + List output1 = processor.transform("1|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=c4ca4238a0b923820dcc509a6f75849b", output1.get(0)); + + // case2: md5("-1") + List output2 = processor.transform("-1|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=6bb61e3b7bce0931da574d19d1d82c88", output2.get(0)); + + // case3: md5("") + List output3 = processor.transform("|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals("result=d41d8cd98f00b204e9800998ecf8427e", output3.get(0)); + + // case4: md5(null) + transformSql = "select md5(numericxx) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output4 = processor.transform("1|4|6|8"); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals("result=null", output4.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestModuloFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestModuloFunction.java new file mode 100644 index 00000000000..6ff0642d9e0 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestModuloFunction.java @@ -0,0 +1,178 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestModuloFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testModuloFunction() throws Exception { + String transformFunctionSql = "select mod(numeric1,100) from source"; + String transformExpressionSql = "select numeric1 % 100 from source"; + List output1, output2; + String data; + TransformConfig functionConfig = new TransformConfig(transformFunctionSql); + TransformProcessor functionProcessor = TransformProcessor + .create(functionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + TransformConfig expressionConfig = new TransformConfig(transformExpressionSql); + TransformProcessor expressionProcessor = TransformProcessor + .create(expressionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: "mod(3.1415926,100)" and "3.1415926 % 100" + data = "3.1415926|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=3.1415926", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=3.1415926", output2.get(0)); + + // case2: "mod(-3.1415926,100)" and "-3.1415926 % 100" + data = "-3.1415926|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=-3.1415926", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=-3.1415926", output2.get(0)); + + // case3: "mod(320,100)" and "320 % 100" + data = "320|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=20", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=20", output2.get(0)); + + // case4: "mod(-320,100)" and "-320 % 100" + data = "-320|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=-20", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=-20", output2.get(0)); + + transformFunctionSql = "select mod(numeric1,-10) from source"; + transformExpressionSql = "select numeric1 % -10 from source"; + functionConfig = new TransformConfig(transformFunctionSql); + functionProcessor = TransformProcessor + .create(functionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + expressionConfig = new TransformConfig(transformExpressionSql); + expressionProcessor = TransformProcessor + .create(expressionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case5: "mod(9,-10)" and "9 % -10" + data = "9|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=9", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=9", output2.get(0)); + + // case6: "mod(-13,-10)" and "-13 % -10" + data = "-13|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=-3", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=-3", output2.get(0)); + + // case7: "mod(-13.14,-10)" and "-13.14 % -10" + data = "-13.14|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=-3.14", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=-3.14", output2.get(0)); + + // case8: "mod(13.14,-10)" and "13.14 % -10" + data = "13.14|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=3.14", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=3.14", output2.get(0)); + + transformFunctionSql = "select mod(numeric1,-3.14) from source"; + transformExpressionSql = "select numeric1 % -3.14 from source"; + functionConfig = new TransformConfig(transformFunctionSql); + functionProcessor = TransformProcessor + .create(functionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + expressionConfig = new TransformConfig(transformExpressionSql); + expressionProcessor = TransformProcessor + .create(expressionConfig, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case9: "mod(9,-3.14)" and "9 % -3.14" + data = "9|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=2.72", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=2.72", output2.get(0)); + + // case10: "mod(-9,-3.14)" and "-9 % -3.14" + data = "-9|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=-2.72", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=-2.72", output2.get(0)); + + // case11: "mod(-13.14,-3.14)" and "-13.14 % -3.14" + data = "-13.14|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=-0.58", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=-0.58", output2.get(0)); + + // case12: "mod(13.14,-3.14)" and "13.14 % -3.14" + data = "13.14|4a|4|8"; + output1 = functionProcessor.transform(data); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=0.58", output1.get(0)); + output2 = expressionProcessor.transform(data); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=0.58", output2.get(0)); + + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestPiFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestPiFunction.java new file mode 100644 index 00000000000..90e6ab8901e --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestPiFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestPiFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testPiFunction() throws Exception { + String transformSql1 = "select pi() from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: pi() + List output1 = processor1.transform("1007|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=3.141592653589793"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestPowerFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestPowerFunction.java new file mode 100644 index 00000000000..a651aacff53 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestPowerFunction.java @@ -0,0 +1,53 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestPowerFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testPowerFunction() throws Exception { + String transformSql = "select power(numeric1, numeric2) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: 2^4 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("2|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=16.0"); + // case2: 2^(-2) + List output2 = processor.transform("2|-2|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=0.25"); + // case3: 4^(0.5) + List output3 = processor.transform("4|0.5|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=2.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRadiansFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRadiansFunction.java new file mode 100644 index 00000000000..806a4328303 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRadiansFunction.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestRadiansFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testRadiansFunction() throws Exception { + String transformSql = "select radians(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: radians(10) + List output1 = processor.transform("10|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.17453292519943295"); + // case2: radians(18.97) + List output2 = processor.transform("18.97|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=0.33108895910332425"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRandFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRandFunction.java new file mode 100644 index 00000000000..e125237d6e4 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRandFunction.java @@ -0,0 +1,60 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Random; + +public class TestRandFunction extends AbstractFunctionArithmeticTestBase { + + @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/function/arithmetic/TestRoundFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRoundFunction.java new file mode 100644 index 00000000000..241faf4fef5 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestRoundFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestRoundFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testRoundFunction() throws Exception { + String transformSql = "select round(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: round(3.14159265358979323846) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=3"); + // case2: round(3.5) + List output2 = processor.transform("3.5|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=4"); + + transformSql = "select round(numeric1,numeric2) from source"; + config = new TransformConfig(transformSql); + // case3: round(3.14159265358979323846,10) + processor = TransformProcessor.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|10|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=3.1415926536"); + // case4: round(13.14159265358979323846,-1) + List output4 = processor.transform("13.14159265358979323846|-1|6|8"); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=10.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSha2Function.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSha2Function.java new file mode 100644 index 00000000000..2007499a415 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSha2Function.java @@ -0,0 +1,74 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestSha2Function extends AbstractFunctionArithmeticTestBase { + + @Test + public void testSha2Function() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: sha2("",3) + transformSql = "select sha2(numeric1,numeric2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case2: sha2("5",224) + data = "5|224|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=b51d18b551043c1f145f22dbde6f8531faeaf68c54ed9dd79ce24d17", output.get(0)); + + // case3: sha2("5",0) + data = "5|0|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=ef2d127de37b942baad06145e54b0c619a1f22327b2ebbcfbec78f5564afe39d", output.get(0)); + + // case4: sha2(null,224) + transformSql = "select sha2(xxd,224) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "3|224|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestShaFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestShaFunction.java new file mode 100644 index 00000000000..087a8e44564 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestShaFunction.java @@ -0,0 +1,68 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestShaFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testShaFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: sha("") + transformSql = "select sha(numeric1) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=da39a3ee5e6b4b0d3255bfef95601890afd80709", output.get(0)); + + // case2: sha("5") + data = "5|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4", output.get(0)); + + // case3: sha(null) + transformSql = "select sha(xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "3|3|3|5"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSignFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSignFunction.java new file mode 100644 index 00000000000..88d021a6b0e --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSignFunction.java @@ -0,0 +1,52 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestSignFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testSignFunction() throws Exception { + String transformSql = "select sign(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: sign(3.14159265358979323846) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: sign(-3.5) + List output2 = processor.transform("-3.5|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=-1"); + // case3: sign(0) + List output3 = processor.transform("0|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSinFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSinFunction.java new file mode 100644 index 00000000000..408c978d8d6 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSinFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestSinFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testSinFunction() throws Exception { + String transformSql = "select sin(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: sin(0) + List output1 = processor.transform("0|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSinhFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSinhFunction.java new file mode 100644 index 00000000000..4c6e5941145 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSinhFunction.java @@ -0,0 +1,52 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestSinhFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testSinhFunction() throws Exception { + String transformSql = "select sinh(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: sinh(0) + List output1 = processor.transform("0|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + // case2: sinh(1) + List output2 = processor.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1.1752011936438014"); + // case3: sinh(2) + List output3 = processor.transform("2|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=3.626860407847019"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSqrtFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSqrtFunction.java new file mode 100644 index 00000000000..97bd1f1df16 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestSqrtFunction.java @@ -0,0 +1,49 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestSqrtFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testSqrtFunction() throws Exception { + String transformSql = "select sqrt(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: sqrt(9) + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("9|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=3.0"); + // case2: sqrt(5) + List output2 = processor.transform("5|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=2.23606797749979"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestTanFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestTanFunction.java new file mode 100644 index 00000000000..39b76127fb8 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestTanFunction.java @@ -0,0 +1,52 @@ +/* + * 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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; +public class TestTanFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testTanFunction() throws Exception { + String transformSql = "select tan(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case: tan(0) + List output1 = processor.transform("0|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.0"); + // case: tan(1) + List output2 = processor.transform("1|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1.5574077246549023"); + // case: tan(2) + List output3 = processor.transform("2|4|6|8", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-2.185039863261519"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestTanhFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestTanhFunction.java new file mode 100644 index 00000000000..027235951fa --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/arithmetic/TestTanhFunction.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.arithmetic; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestTanhFunction extends AbstractFunctionArithmeticTestBase { + + @Test + public void testTanhFunction() throws Exception { + String transformSql = "select tanh(numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: tanh(1) + List output1 = processor.transform("1|4|6|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0.7615941559557649"); + + // case2: tanh(0) + List output2 = processor.transform("0|4|6|8"); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=0.0"); + + // case3: tanh(-1) + List output3 = processor.transform("-1|4|6|8"); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=-0.7615941559557649"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/AbstractFunctionStringTestBase.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/AbstractFunctionStringTestBase.java new file mode 100644 index 00000000000..da4b84c5d61 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/AbstractFunctionStringTestBase.java @@ -0,0 +1,54 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; + +import java.util.ArrayList; +import java.util.List; +/** + * AbstractFunctionStringTestBase + * description: define static parameters for StringFunction tests + */ +public abstract class AbstractFunctionStringTestBase { + + protected static final List srcFields = new ArrayList<>(); + protected static final List dstFields = new ArrayList<>(); + protected static final CsvSourceInfo csvSource; + protected static final KvSinkInfo kvSink; + + static { + for (int i = 1; i < 4; i++) { + FieldInfo field = new FieldInfo(); + field.setName("string" + i); + srcFields.add(field); + } + for (int i = 1; i < 4; i++) { + FieldInfo field = new FieldInfo(); + field.setName("numeric" + i); + srcFields.add(field); + } + FieldInfo field = new FieldInfo(); + field.setName("result"); + dstFields.add(field); + csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); + kvSink = new KvSinkInfo("UTF-8", dstFields); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestContainsFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestContainsFunction.java new file mode 100644 index 00000000000..352f9c46b1b --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestContainsFunction.java @@ -0,0 +1,65 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestContainsFunction extends AbstractFunctionStringTestBase { + + @Test + public void testContainsFunction() throws Exception { + String transformSql = "select contains(string1, string2) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: contains('Transform SQL', 'SQL') + List output1 = processor.transform("Transform SQL|SQL", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=true"); + // case2: contains('', 'SQL') + List output2 = processor.transform("|SQL", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=false"); + // case3: contains('Transform SQL', '') + List 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 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 output5 = processor.transform("Transform SQL|m S", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result=true"); + // case6: contains('', '') + List output6 = processor.transform("|", new HashMap<>()); + Assert.assertEquals(1, output6.size()); + Assert.assertEquals(output6.get(0), "result=true"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformFromBase64FunctionProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestFromBase64Function.java similarity index 68% rename from inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformFromBase64FunctionProcessor.java rename to inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestFromBase64Function.java index 9684c5fe7cd..021ef381d7f 100644 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformFromBase64FunctionProcessor.java +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestFromBase64Function.java @@ -15,50 +15,21 @@ * limitations under the License. */ -package org.apache.inlong.sdk.transform.process; +package org.apache.inlong.sdk.transform.process.function.string; import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; -import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; -import org.apache.inlong.sdk.transform.pojo.FieldInfo; -import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; -/** - * TestTransformFromBase64FunctionProcessor - * description: test the from_base64 function in transform processor - */ -public class TestTransformFromBase64FunctionProcessor { - - private static final List srcFields = new ArrayList<>(); - private static final List dstFields = new ArrayList<>(); - private static final CsvSourceInfo csvSource; - private static final KvSinkInfo kvSink; +public class TestFromBase64Function extends AbstractFunctionStringTestBase { - static { - for (int i = 1; i < 4; i++) { - FieldInfo field = new FieldInfo(); - field.setName("string" + i); - srcFields.add(field); - } - for (int i = 1; i < 4; i++) { - FieldInfo field = new FieldInfo(); - field.setName("numeric" + i); - srcFields.add(field); - } - FieldInfo field = new FieldInfo(); - field.setName("result"); - dstFields.add(field); - csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); - kvSink = new KvSinkInfo("UTF-8", dstFields); - } @Test public void testFromBase64Function() throws Exception { String transformSql = "select from_base64(string1) from source"; diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestInsertFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestInsertFunction.java new file mode 100644 index 00000000000..91ce4cbc574 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestInsertFunction.java @@ -0,0 +1,66 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestInsertFunction extends AbstractFunctionStringTestBase { + + @Test + public void testInsertFunction() throws Exception { + String transformSql1 = "select insert(string1, numeric1, numeric2, string2) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: insert('12345678', 3, 4, 'word') -> '12word78' + List output1 = processor1.transform("12345678|word|cloud|3|4|0", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=12word78", output1.get(0)); + + // case2: insert('12345678', -1, 4, 'word') -> '12345678' + List output2 = processor1.transform("12345678|word|cloud|-1|4|0", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=12345678", output2.get(0)); + + // case3: insert('12345678', 3, 100, 'word') -> '12word' + List output3 = processor1.transform("12345678|word|cloud|3|100|0", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals("result=12word", output3.get(0)); + + // case4: insert('', 3, 4, 'word') -> '' + List output4 = processor1.transform("|word|cloud|3|4|0", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals("result=", output4.get(0)); + + // case5: insert('12345678', 3, 4, '') -> '1278' + List output5 = processor1.transform("12345678||cloud|3|4|0", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals("result=1278", output5.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLeftFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLeftFunction.java new file mode 100644 index 00000000000..6663c63d704 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLeftFunction.java @@ -0,0 +1,80 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLeftFunction extends AbstractFunctionStringTestBase { + + @Test + public void testLeftFunction() throws Exception { + String transformSql = "select left(string1,numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor 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 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)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLengthFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLengthFunction.java new file mode 100644 index 00000000000..1094c4f0553 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLengthFunction.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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLengthFunction extends AbstractFunctionStringTestBase { + + @Test + public void testLengthFunction() throws Exception { + String transformSql = "select length(string1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: length('hello world') + List output1 = processor1.transform("hello world|apple|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=11", output1.get(0)); + + transformSql = "select length(xxd) from source"; + config = new TransformConfig(transformSql); + processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: length(null) + output1 = processor1.transform("hello world|apple|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=null", output1.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLocateFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLocateFunction.java new file mode 100644 index 00000000000..ca21214bc44 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLocateFunction.java @@ -0,0 +1,66 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLocateFunction extends AbstractFunctionStringTestBase { + + @Test + public void testLocateFunction() throws Exception { + String transformSql1 = "select locate(string1, string2) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: locate('app', 'apple') + List output1 = processor1.transform("app|apple|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: locate('ape', 'apple') + List output2 = processor1.transform("ape|apple|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=0"); + String transformSql2 = "select locate(string1, string2, numeric1) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: locate('app', 'appapp', 2) + List output3 = processor2.transform("app|appapp|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=4"); + // case4: locate('app', 'appape', 2) + List output4 = processor2.transform("app|appape|cloud|2|1|9", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=0"); + // case5: locate('app', null) + List output5 = processor1.transform("app", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result=null"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLowerFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLowerFunction.java new file mode 100644 index 00000000000..8050372a3c7 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLowerFunction.java @@ -0,0 +1,60 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLowerFunction extends AbstractFunctionStringTestBase { + + @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"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLpadFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLpadFunction.java new file mode 100644 index 00000000000..333683aa752 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestLpadFunction.java @@ -0,0 +1,102 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestLpadFunction extends AbstractFunctionStringTestBase { + + @Test + public void testLpadFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + transformSql = "select lpad(string1,numeric1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: lpad('he',7,'xxd') + data = "he|xxd|cloud|7|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=xxdxxhe", output.get(0)); + + // case2: lpad('he',1,'xxd') + data = "he|xxd|cloud|1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=h", output.get(0)); + + // case3: lpad('he',1,'') + data = "he||cloud|1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=h", output.get(0)); + + // case4: lpad('he',-1,'xxd') + data = "he|xxd|cloud|-1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case5: lpad(null,5,'xxd') + transformSql = "select lpad(xxd,numeric1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "he|xxd|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case6: lpad('he',null,'xxd') + transformSql = "select lpad(string1,xxd,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "he|xxd|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case7: lpad('he',5,null) + transformSql = "select lpad(string1,numeric1,xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "he|xxd|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReplaceFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReplaceFunction.java new file mode 100644 index 00000000000..c82ae166c19 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReplaceFunction.java @@ -0,0 +1,69 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestReplaceFunction extends AbstractFunctionStringTestBase { + + @Test + public void testReplaceFunction() throws Exception { + String transformSql = "select replace(string1, string2, string3) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: replace('hooray', 'oray', 'lly') + List output1 = processor.transform("hooray|oray|lly", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=holly"); + // case2: replace('hooray', 'hook', 'hoor') + List output2 = processor.transform("hooray|hook|hoor", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=hooray"); + // case3: replace('Hello World', 'World', '') + List output3 = processor.transform("Hello World|World|", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=Hello "); + // case4: replace('Hello World', '', 'J') + List output4 = processor.transform("Hello World||J", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=JHJeJlJlJoJ JWJoJrJlJdJ"); + // case5: replace('', '', '') + List output5 = processor.transform("||", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result="); + // case6: replace('abababab', 'ab', 'cd') + List output6 = processor.transform("abababab|ab|cd", new HashMap<>()); + Assert.assertEquals(1, output6.size()); + Assert.assertEquals(output6.get(0), "result=cdcdcdcd"); + // case7: replace('aaa', 'aa', 'd') + List output7 = processor.transform("aaa|aa|d", new HashMap<>()); + Assert.assertEquals(1, output7.size()); + Assert.assertEquals(output7.get(0), "result=da"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReplicateFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReplicateFunction.java new file mode 100644 index 00000000000..9da3f98a6e1 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReplicateFunction.java @@ -0,0 +1,67 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestReplicateFunction extends AbstractFunctionStringTestBase { + + @Test + public void testReplicateFunction() throws Exception { + String transformSql1 = "select replicate(string1, numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: replicate('apple', 2) + List 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 processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: replicate('banana', 3) + List 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 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 processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output4 = processor3.transform("apple|banana|cloud|2|1|0", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result="); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReverseFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReverseFunction.java new file mode 100644 index 00000000000..65a9fad4251 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestReverseFunction.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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestReverseFunction extends AbstractFunctionStringTestBase { + + @Test + public void testReverseFunction() throws Exception { + String transformSql1 = "select reverse(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: reverse('apple') + List output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=elppa"); + // case2: reverse('ban ana ') + String transformSql2 = "select reverse(string2) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output2 = processor2.transform("apple|ban ana |cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result= ana nab"); + // case3: reverse(12345) + List output3 = processor1.transform("12345|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=54321"); + // case4: reverse(null) + List output4 = processor1.transform("|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result="); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestRightFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestRightFunction.java new file mode 100644 index 00000000000..9728fda896a --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestRightFunction.java @@ -0,0 +1,80 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestRightFunction extends AbstractFunctionStringTestBase { + + @Test + public void testRightFunction() throws Exception { + String transformSql = "select right(string1,numeric1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor 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 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)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestRpadFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestRpadFunction.java new file mode 100644 index 00000000000..7a58a8be439 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestRpadFunction.java @@ -0,0 +1,102 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestRpadFunction extends AbstractFunctionStringTestBase { + + @Test + public void testRpadFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + transformSql = "select rpad(string1,numeric1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: rpad('he',7,'xxd') + data = "he|xxd|cloud|7|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=hexxdxx", output.get(0)); + + // case2: rpad('he',1,'xxd') + data = "he|xxd|cloud|1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=h", output.get(0)); + + // case3: rpad('he',1,'') + data = "he||cloud|1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=h", output.get(0)); + + // case4: rpad('he',-1,'xxd') + data = "he|xxd|cloud|-1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case5: rpad(null,5,'xxd') + transformSql = "select rpad(xxd,numeric1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "he|xxd|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case6: rpad('he',null,'xxd') + transformSql = "select rpad(string1,xxd,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "he|xxd|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case7: rpad('he',5,null) + transformSql = "select rpad(string1,numeric1,xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "he|xxd|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestSpaceFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestSpaceFunction.java new file mode 100644 index 00000000000..308c54236cd --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestSpaceFunction.java @@ -0,0 +1,69 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestSpaceFunction extends AbstractFunctionStringTestBase { + + @Test + public void testSpaceFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + transformSql = "select space(numeric1) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: space(5) + data = "hello world|banana|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result= ", output.get(0)); + + // case2: space(-1) + data = "hello world|banana|cloud|-1|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=", output.get(0)); + + // case3: space(null) + transformSql = "select space(xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|banana|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestStrcmpFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestStrcmpFunction.java new file mode 100644 index 00000000000..3534da6189d --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestStrcmpFunction.java @@ -0,0 +1,74 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestStrcmpFunction extends AbstractFunctionStringTestBase { + + @Test + public void testStrcmpFunction() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + transformSql = "select strcmp(string1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: strcmp('hello world','banana') + data = "hello world|banana|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=1", output.get(0)); + + // case2: strcmp('hello world','hello world') + data = "hello world|hello world|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + + // case3: strcmp('hello world','zzzzz') + data = "hello world|zzzzz|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=-1", output.get(0)); + + // case4: strcmp('hello world',null) + transformSql = "select strcmp(string1,xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "hello world|zzzzz|cloud|5|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestStringFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestStringFunctionsProcessor.java new file mode 100644 index 00000000000..40447d9a10c --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestStringFunctionsProcessor.java @@ -0,0 +1,35 @@ +/* + * 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.string; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +/** + * TestArithmeticFunctionProcessor + * description: test all the string functions in transform processor + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({TestInsertFunction.class, TestLeftFunction.class, TestLengthFunction.class, + TestLocateFunction.class, TestLowerFunction.class, TestLpadFunction.class, TestReplaceFunction.class, + TestReplicateFunction.class, TestReverseFunction.class, TestRightFunction.class, TestRpadFunction.class, + TestSpaceFunction.class, TestStrcmpFunction.class, TestSubstringFunction.class, TestToBase64Function.class, + TestTranslateFunction.class, TestTrimFunction.class, TestUpperFunction.class, TestContainsFunction.class, + TestFromBase64Function.class +}) +public class TestStringFunctionsProcessor { +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestSubstringFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestSubstringFunction.java new file mode 100644 index 00000000000..e8931dec525 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestSubstringFunction.java @@ -0,0 +1,58 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestSubstringFunction extends AbstractFunctionStringTestBase { + + @Test + public void testSubstringFunction() throws Exception { + String transformSql1 = "select substring(string2, numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: substring('banana', 2) + List output1 = processor1.transform("apple|banana|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=anana"); + String transformSql2 = "select substring(string1, numeric1, numeric3) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: substring('apple', 1, 3) + List output2 = processor2.transform("apple|banana|cloud|1|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=app"); + // case3: substring('apple', 2, 9) + List output3 = processor2.transform("apple|banana|cloud|2|1|9", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=pple"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestToBase64Function.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestToBase64Function.java new file mode 100644 index 00000000000..04dca5f313e --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestToBase64Function.java @@ -0,0 +1,50 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestToBase64Function extends AbstractFunctionStringTestBase { + + @Test + public void testToBase64Function() throws Exception { + String transformSql = "select to_base64(string1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // to_base64('app-fun') + List output1 = processor1.transform("app-fun", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=YXBwLWZ1bg=="); + // to_base64('hello world') + List output2 = processor1.transform("hello world", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=aGVsbG8gd29ybGQ="); + } + +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestTranslateFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestTranslateFunction.java new file mode 100644 index 00000000000..26363c2b923 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestTranslateFunction.java @@ -0,0 +1,58 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestTranslateFunction extends AbstractFunctionStringTestBase { + + @Test + public void testTranslateFunction() throws Exception { + String transformSql1 = "select translate(string1, string2, string3) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: translate("hello word!", "el", "EL") + List output1 = processor1.transform("hello word!|el|EL|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=hELLo word!"); + String transformSql2 = "select translate(string3, string1, string2) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: translate("hello word!", "el", "EL") + List output2 = processor2.transform("el|EL|hello word!|1|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=hELLo word!"); + // case3: translate('ApaCHe Inlong', CH, ch) + List output3 = processor2.transform("CH|ch|ApaCHe Inlong|2|1|9", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=Apache Inlong"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestTrimFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestTrimFunction.java new file mode 100644 index 00000000000..d3e9db029d3 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestTrimFunction.java @@ -0,0 +1,63 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestTrimFunction extends AbstractFunctionStringTestBase { + + @Test + public void testTrimFunction() throws Exception { + String transformSql1 = "select trim(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: trim(' in long') + List 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 processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: trim('in long ') + List 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 processor3 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: trim(' in long ') + List output3 = processor3.transform(" in long|in long | in long ", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=in long"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUpperFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUpperFunction.java new file mode 100644 index 00000000000..b4fd923ebdd --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUpperFunction.java @@ -0,0 +1,60 @@ +/* + * 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.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestUpperFunction extends AbstractFunctionStringTestBase { + + @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"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/AbstractFunctionTemporalTestBase.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/AbstractFunctionTemporalTestBase.java new file mode 100644 index 00000000000..83fc3e625c4 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/AbstractFunctionTemporalTestBase.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.temporal; + +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; + +import org.junit.Before; + +import java.util.ArrayList; +import java.util.List; +import java.util.TimeZone; +/** + * AbstractFunctionTemporalTestBase + * description: define static parameters for TemporalFunction tests + */ +public abstract class AbstractFunctionTemporalTestBase { + + protected static final List srcFields = new ArrayList<>(); + protected static final List dstFields = new ArrayList<>(); + protected static final CsvSourceInfo csvSource; + protected static final KvSinkInfo kvSink; + + static { + for (int i = 1; i < 4; i++) { + FieldInfo field = new FieldInfo(); + field.setName("string" + i); + srcFields.add(field); + } + for (int i = 1; i < 4; i++) { + FieldInfo field = new FieldInfo(); + field.setName("numeric" + i); + srcFields.add(field); + } + FieldInfo field = new FieldInfo(); + field.setName("result"); + dstFields.add(field); + csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); + kvSink = new KvSinkInfo("UTF-8", dstFields); + } + + @Before + public void setUp() { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateDiffFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateDiffFunction.java new file mode 100644 index 00000000000..6887d855dc4 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateDiffFunction.java @@ -0,0 +1,80 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestDateDiffFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testDateDiffFunction() throws Exception { + String transformSql = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + transformSql = "select datediff(string1,string2) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: datediff('1970-01-01','1970-01-02') + output = processor.transform("1970-01-01|1970-01-02", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=-1", output.get(0)); + + // case2: datediff('1970-01-02','1970-01-01') + output = processor.transform("1970-01-02|1970-01-01", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=1", output.get(0)); + + // case3: datediff('2018-12-10 12:30:00', '2018-12-09 13:30:00') + output = processor.transform("2018-12-10 12:30:00|2018-12-09 13:30:00", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=1", output.get(0)); + + // case4: datediff('2018-12-10 12:30:00', '') + output = processor.transform("2018-12-10 12:30:00|", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case5: datediff('2018-12', '2018-12-12') + output = processor.transform("2018-12|2018-12-12", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + + // case6: datediff('1970-01-01',null) + transformSql = "select datediff(string1,xxd) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + output = processor.transform("1970-01-01|1970-01-02", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=null", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateExtractFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateExtractFunction.java new file mode 100644 index 00000000000..b7e8654ef30 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateExtractFunction.java @@ -0,0 +1,115 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestDateExtractFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testDateExtractFunction() throws Exception { + String transformSql1 = "select year(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: year(2024-08-08) + List output1 = processor1.transform("2024-08-08", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=2024"); + + String transformSql2 = "select quarter(string1) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: quarter(2024-08-08) + List output2 = processor2.transform("2024-08-08", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=3"); + + String transformSql3 = "select month(string1) from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: month(2024-08-08) + List output3 = processor3.transform("2024-08-08", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=8"); + + String transformSql4 = "select week(string1) from source"; + TransformConfig config4 = new TransformConfig(transformSql4); + TransformProcessor processor4 = TransformProcessor + .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case4: week(2024-02-29) + List output4 = processor4.transform("2024-02-29", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=9"); + + String transformSql5 = "select day_of_year(string1) from source"; + TransformConfig config5 = new TransformConfig(transformSql5); + TransformProcessor processor5 = TransformProcessor + .create(config5, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case5: dayofyear(2024-02-29) + List output5 = processor5.transform("2024-02-29", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result=60"); + + String transformSql6 = "select day_of_month(string1) from source"; + TransformConfig config6 = new TransformConfig(transformSql6); + TransformProcessor processor6 = TransformProcessor + .create(config6, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case6: dayofmonth(2024-02-29) + List output6 = processor6.transform("2024-02-29", new HashMap<>()); + Assert.assertEquals(1, output6.size()); + Assert.assertEquals(output6.get(0), "result=29"); + + String transformSql7 = "select day_of_week(string1) from source"; + TransformConfig config7 = new TransformConfig(transformSql7); + TransformProcessor processor7 = TransformProcessor + .create(config7, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case7: dayofweek(2024-02-29) (Thursday) + List output7 = processor7.transform("2024-02-29", new HashMap<>()); + Assert.assertEquals(1, output7.size()); + Assert.assertEquals(output7.get(0), "result=5"); + + String transformSql8 = "select dayname(string1) from source"; + TransformConfig config8 = new TransformConfig(transformSql8); + TransformProcessor processor8 = TransformProcessor + .create(config8, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case8: dayname(2024-02-29) (Thursday) + List output8 = processor8.transform("2024-02-29", new HashMap<>()); + Assert.assertEquals(1, output8.size()); + Assert.assertEquals(output8.get(0), "result=THURSDAY"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateFormatFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateFormatFunction.java new file mode 100644 index 00000000000..d76ac0ce01b --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateFormatFunction.java @@ -0,0 +1,75 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestDateFormatFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testDateFormatFunction() throws Exception { + String transformSql1 = "select date_format(string1, 'yyyy-MM-dd HH:mm:ss') from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: date_format('2024-08-01 22:56:56', 'yyyy-MM-dd HH:mm:ss') + List output1 = processor1.transform("2024-08-01 22:56:56", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=2024-08-01 22:56:56"); + + String transformSql2 = "select date_format(string1, 'yyyy-MM-dd') from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: date_format('2024-08-01 22:56:56', 'yyyy-MM-dd') + List output2 = processor2.transform("2024-08-01 22:56:56", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=2024-08-01"); + + String transformSql3 = "select date_format(string1, 'yyyyMMddHHmmss') from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: date_format('2024-08-01 22:56:56', 'yyyyMMddHHmmss') + List output3 = processor3.transform("2024-08-01 22:56:56", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=20240801225656"); + + String transformSql4 = "select date_format(string1, 'yyyy/MM/dd HH:mm:ss') from source"; + TransformConfig config4 = new TransformConfig(transformSql4); + TransformProcessor processor4 = TransformProcessor + .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case4: date_format('2024-08-01 22:56:56', 'yyyy/MM/dd HH:mm:ss') + List output4 = processor4.transform("2024-08-01 22:56:56", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=2024/08/01 22:56:56"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestFromUnixTimeFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestFromUnixTimeFunction.java new file mode 100644 index 00000000000..e8794247644 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestFromUnixTimeFunction.java @@ -0,0 +1,75 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestFromUnixTimeFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testFromUnixTimeFunction() throws Exception { + String transformSql1 = "select from_unix_time(numeric1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: from_unix_time(44) + List output1 = processor1.transform("can|apple|cloud|44|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1970-01-01 08:00:44"); + + String transformSql2 = "select from_unix_time(numeric1, 'yyyy/MM/dd HH:mm:ss') from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: from_unix_time(44, 'yyyy/MM/dd HH:mm:ss') + List output2 = processor2.transform("can|apple|cloud|44|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1970/01/01 08:00:44"); + + String transformSql3 = "select from_unix_time(numeric1, 'MMdd-yyyy') from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: from_unix_time(44, 'MMdd-yyyy') + List output3 = processor3.transform("can|apple|cloud|44|1|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=0101-1970"); + + String transformSql4 = "select from_unix_time(numeric1, 'yyyyMMddHHss') from source"; + TransformConfig config4 = new TransformConfig(transformSql4); + TransformProcessor processor4 = TransformProcessor + .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case4: from_unix_time(44, 'yyyyMMddHHss') + List output4 = processor4.transform("can|apple|cloud|44|1|3", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=197001010844"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestLocalDateFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestLocalDateFunction.java new file mode 100644 index 00000000000..e6a7f5a1abe --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestLocalDateFunction.java @@ -0,0 +1,76 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.List; + +public class TestLocalDateFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testLocalDateFunction() throws Exception { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + + // case1: localDate() - default system time zone + String transformSql1 = "select localdate() from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor1.transform("", new HashMap<>()); + LocalDate expectedDate1 = LocalDate.now(ZoneId.systemDefault()); + LocalDate actualDate1 = LocalDate.parse(output1.get(0).split("=")[1], formatter); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(expectedDate1, actualDate1); + + // case2: localDate("UTC") + String transformSql2 = "select localdate('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<>()); + LocalDate expectedDate2 = LocalDate.now(ZoneId.of("UTC")); + LocalDate actualDate2 = LocalDate.parse(output2.get(0).split("=")[1], formatter); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(expectedDate2, actualDate2); + + // case3: localDate("UTC-12") + String transformSql3 = "select localdate('UTC-12') from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor3.transform("", new HashMap<>()); + LocalDate expectedDate3 = LocalDate.now(ZoneId.of("UTC-12")); + LocalDate actualDate3 = LocalDate.parse(output3.get(0).split("=")[1], formatter); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(expectedDate3, actualDate3); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestLocalTimeFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestLocalTimeFunction.java new file mode 100644 index 00000000000..d69ffac7032 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestLocalTimeFunction.java @@ -0,0 +1,79 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.time.Duration; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.List; + +public class TestLocalTimeFunction extends AbstractFunctionTemporalTestBase { + + @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 current_time('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); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTemporalFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTemporalFunctionsProcessor.java new file mode 100644 index 00000000000..bbb65542bff --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTemporalFunctionsProcessor.java @@ -0,0 +1,32 @@ +/* + * 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.temporal; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +/** + * TestTemporalFunctionProcessor + * description: test all the temporal functions in transform processor + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({TestDateDiffFunction.class, TestDateExtractFunction.class, TestDateFormatFunction.class, + TestFromUnixTimeFunction.class, TestLocalDateFunction.class, TestLocalTimeFunction.class, + TestTimestampAdd.class, TestTimestampExtractFunction.class, TestToDateFunction.class, + TestToTimestampFunction.class, TestUnixTimestampFunction.class}) +public class TestTemporalFunctionsProcessor { +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampAdd.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampAdd.java new file mode 100644 index 00000000000..c2f7e1ac56c --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampAdd.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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestTimestampAdd extends AbstractFunctionTemporalTestBase { + + @Test + public void testTimestampAdd() throws Exception { + String transformSql1 = "select timestamp_add('day',string2,string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: timestamp_add('day',3,'1970-01-01') + List output1 = processor1.transform("1970-01-01|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals("result=1970-01-04", output1.get(0)); + + // case2: timestamp_add('day',-3,'1970-01-01 00:00:44') + List output2 = processor1.transform("1970-01-01 00:00:44|-3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals("result=1969-12-29 00:00:44", output2.get(0)); + + String transformSql2 = "select timestamp_add('MINUTE',string2,string1) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case3: timestamp_add('MINUTE',3,'1970-01-01 00:00:44') + List output3 = processor2.transform("1970-01-01 00:00:44|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals("result=1970-01-01 00:03:44", output3.get(0)); + + // case4: timestamp_add('MINUTE',-3,'1970-01-01 00:00:44') + List output4 = processor2.transform("1970-01-01 00:00:44|-3", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals("result=1969-12-31 23:57:44", output4.get(0)); + + // case5: timestamp_add('MINUTE',-3,'1970-01-01') + List output5 = processor2.transform("1970-01-01|-3", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals("result=1969-12-31 23:57:00", output5.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampExtractFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampExtractFunction.java new file mode 100644 index 00000000000..151c71c9a78 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampExtractFunction.java @@ -0,0 +1,65 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestTimestampExtractFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testTimestampExtractFunction() throws Exception { + String transformSql1 = "select hour(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: hour(2024-08-12 12:23:34) + List output1 = processor1.transform("2024-08-12 12:23:34", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=12"); + + String transformSql2 = "select minute(string1) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: minute(2024-08-12 12:23:34) + List output2 = processor2.transform("2024-08-12 12:23:34", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=23"); + + String transformSql3 = "select second(string1) from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: second(2024-08-12 12:23:34) + List output3 = processor3.transform("2024-08-12 12:23:34", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=34"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestToDateFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestToDateFunction.java new file mode 100644 index 00000000000..0b8141c04d3 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestToDateFunction.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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestToDateFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testToDateFunction() throws Exception { + String transformSql1 = "select to_date(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: to_date('2024-08-15') + List output1 = processor1.transform("2024-08-15|apple|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=2024-08-15"); + String transformSql2 = "select to_date(string1, string2) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: to_date('20240815', 'yyyyMMdd') + List output2 = processor2.transform("20240815|yyyyMMdd|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=2024-08-15"); + // case3: to_date('08152024', 'MMddyyyy') + List output3 = processor2.transform("08152024|MMddyyyy|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=2024-08-15"); + // case4: to_date('2024/08/15', 'yyyy/MM/dd') + List output4 = processor2.transform("2024/08/15|yyyy/MM/dd|cloud|2|1|3", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=2024-08-15"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestToTimestampFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestToTimestampFunction.java new file mode 100644 index 00000000000..a136d5a6a5f --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestToTimestampFunction.java @@ -0,0 +1,75 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestToTimestampFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testToTimestampFunction() throws Exception { + String transformSql1 = "select to_timestamp(string1) from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: to_timestamp('1970-01-01 00:00:44') + List output1 = processor1.transform("1970-01-01 00:00:44", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1970-01-01 00:00:44.0"); + + String transformSql2 = "select to_timestamp(string1, 'yyyy/MM/dd HH:mm:ss') from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: to_timestamp('1970/01/01 00:00:44', 'yyyy/MM/dd HH:mm:ss') + List output2 = processor2.transform("1970/01/01 00:00:44", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=1970-01-01 00:00:44.0"); + + String transformSql3 = "select to_timestamp(string1, 'yyyyMMddHHmmss') from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: to_timestamp('19700101000044', 'yyyyMMddHHmmss') + List output3 = processor3.transform("19700101000044", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=1970-01-01 00:00:44.0"); + + String transformSql4 = "select to_timestamp(string1, 'yyyy-MM-dd') from source"; + TransformConfig config4 = new TransformConfig(transformSql4); + TransformProcessor processor4 = TransformProcessor + .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case4: to_timestamp('1970-01-01', 'yyyy-MM-dd') + List output4 = processor4.transform("1970-01-01", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=1970-01-01 00:00:00.0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestUnixTimestampFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestUnixTimestampFunction.java new file mode 100644 index 00000000000..77e02157e39 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestUnixTimestampFunction.java @@ -0,0 +1,74 @@ +/* + * 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.temporal; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestUnixTimestampFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testUnixTimestampFunction() throws Exception { + String transformSql1 = "select unix_timestamp() from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: unix_timestamp() + List output1 = processor1.transform("", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + + String transformSql2 = "select unix_timestamp(string1, 'yyyy/MM/dd HH:mm:ss') from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: unix_timestamp('1970/01/01 08:00:44', 'yyyy/MM/dd HH:mm:ss') + List output2 = processor2.transform("1970/01/01 08:00:44", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=44"); + + String transformSql3 = "select unix_timestamp(string1) from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case3: unix_timestamp('1970/01/01 08:00:44') + List output3 = processor3.transform("1970-01-01 08:00:44", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=44"); + + String transformSql4 = "select unix_timestamp(string1, 'yyyyMMddHHmmss') from source"; + TransformConfig config4 = new TransformConfig(transformSql4); + TransformProcessor processor4 = TransformProcessor + .create(config4, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case4: unix_timestamp('19700101080044', 'yyyyMMddHHss') + List output4 = processor4.transform("19700101080044", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=44"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/AbstractOperatorTestBase.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/AbstractOperatorTestBase.java new file mode 100644 index 00000000000..0a9997c189d --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/AbstractOperatorTestBase.java @@ -0,0 +1,52 @@ +/* + * 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.operator; + +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; +import org.apache.inlong.sdk.transform.process.converter.DoubleConverter; +import org.apache.inlong.sdk.transform.process.converter.LongConverter; +import org.apache.inlong.sdk.transform.process.converter.TypeConverter; + +import java.util.ArrayList; +import java.util.List; +/** + * AbstractOperatorTestBase + * description: define static parameters for Operator tests + */ +public abstract class AbstractOperatorTestBase { + + protected static final List srcFields = new ArrayList<>(); + protected static final List dstFields = new ArrayList<>(); + protected static final CsvSourceInfo csvSource; + protected static final KvSinkInfo kvSink; + + static { + srcFields.add(new FieldInfo("numeric1", new DoubleConverter())); + srcFields.add(new FieldInfo("string2", TypeConverter.DefaultTypeConverter())); + srcFields.add(new FieldInfo("numeric3", new DoubleConverter())); + srcFields.add(new FieldInfo("numeric4", new LongConverter())); + + FieldInfo field = new FieldInfo(); + field.setName("result"); + dstFields.add(field); + csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); + kvSink = new KvSinkInfo("UTF-8", dstFields); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestAndOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestAndOperator.java new file mode 100644 index 00000000000..79dd171bf55 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestAndOperator.java @@ -0,0 +1,70 @@ +/* + * 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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestAndOperator extends AbstractOperatorTestBase { + + @Test + public void testAndOperator() throws Exception { + String transformSql = "select if((string2 < 4) and (numeric4 > 5),1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|4" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|4"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=0"); + // case3: "3.14159265358979323846|3|4|8" + List output3 = processor.transform("3.14159265358979323846|3|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=1"); + + transformSql = "select if((numeric3 < 4) and (numeric4 > 5),1,0) from source"; + config = new TransformConfig(transformSql); + // case4: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output4 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=0"); + // case5: "3.14159265358979323846|4|3.2|4" + List output5 = processor.transform("3.14159265358979323846|4|3.2|4"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output5.get(0), "result=0"); + // case6: "3.14159265358979323846|4|3.2|8" + List output6 = processor.transform("3.14159265358979323846|4|3.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output6.get(0), "result=1"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestEqualsToOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestEqualsToOperator.java new file mode 100644 index 00000000000..be7ee2c03b2 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestEqualsToOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestEqualsToOperator extends AbstractOperatorTestBase { + + @Test + public void testEqualsToOperator() throws Exception { + String transformSql = "select if(string2 = 4,1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|4a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|4a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0"); + // case2: "3.14159265358979323846|4|4|8" + List output2 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=1"); + + transformSql = "select if(numeric3 = 4,1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=1"); + // case4: "3.14159265358979323846|4|4.2|8" + List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestGreaterThanEqualsOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestGreaterThanEqualsOperator.java new file mode 100644 index 00000000000..9cc393b6278 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestGreaterThanEqualsOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestGreaterThanEqualsOperator extends AbstractOperatorTestBase { + + @Test + public void testGreaterThanEqualsOperator() throws Exception { + String transformSql = "select if(string2 >= 4,1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=1"); + + transformSql = "select if(numeric3 >= 4,1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=1"); + // case4: "3.14159265358979323846|4|3.2|8" + List output4 = processor.transform("3.14159265358979323846|4|3.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestGreaterThanOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestGreaterThanOperator.java new file mode 100644 index 00000000000..c7ab4359c63 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestGreaterThanOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestGreaterThanOperator extends AbstractOperatorTestBase { + + @Test + public void testGreaterThanOperator() throws Exception { + String transformSql = "select if(string2 > 4.1,1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=1"); + + transformSql = "select if(numeric3 > 4.1,1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=0"); + // case4: "3.14159265358979323846|4|4.2|8" + List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=1"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestMinorThanEqualsOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestMinorThanEqualsOperator.java new file mode 100644 index 00000000000..e6cbe7820f6 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestMinorThanEqualsOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestMinorThanEqualsOperator extends AbstractOperatorTestBase { + + @Test + public void testMinorThanEqualsOperator() throws Exception { + String transformSql = "select if(string2 <= 4,1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=0"); + + transformSql = "select if(numeric3 <= 4,1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=1"); + // case4: "3.14159265358979323846|4|4.2|8" + List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestMinorThanOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestMinorThanOperator.java new file mode 100644 index 00000000000..6123f8d52dd --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestMinorThanOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestMinorThanOperator extends AbstractOperatorTestBase { + + @Test + public void testMinorThanOperator() throws Exception { + String transformSql = "select if(string2 < 4.1,1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=0"); + + transformSql = "select if(numeric3 < 4,1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=0"); + // case4: "3.14159265358979323846|4|3.2|8" + List output4 = processor.transform("3.14159265358979323846|4|3.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=1"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestNotEqualsToOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestNotEqualsToOperator.java new file mode 100644 index 00000000000..6504c6f0f58 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestNotEqualsToOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestNotEqualsToOperator extends AbstractOperatorTestBase { + + @Test + public void testNotEqualsToOperator() throws Exception { + String transformSql = "select if(string2 != 4,1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|4a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|4a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: "3.14159265358979323846|4|4|8" + List output2 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=0"); + + transformSql = "select if(numeric3 != 4,1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=0"); + // case4: "3.14159265358979323846|4|4.2|8" + List output4 = processor.transform("3.14159265358979323846|4|4.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=1"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestNotOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestNotOperator.java new file mode 100644 index 00000000000..cc50eb1d009 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestNotOperator.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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestNotOperator extends AbstractOperatorTestBase { + + @Test + public void testNotOperator() throws Exception { + String transformSql = "select if(!(string2 < 4),1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=0"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=1"); + + transformSql = "select if(!(numeric3 < 3.9),1,0) from source"; + config = new TransformConfig(transformSql); + // case3: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=1"); + // case4: "3.14159265358979323846|4|3.2|8" + List output4 = processor.transform("3.14159265358979323846|4|3.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestOperatorsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestOperatorsProcessor.java new file mode 100644 index 00000000000..1fe1a8de5f2 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestOperatorsProcessor.java @@ -0,0 +1,31 @@ +/* + * 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.operator; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +/** + * TestOperatorsProcessor + * description: test all the operator transform processor + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({TestAndOperator.class, TestEqualsToOperator.class, TestGreaterThanEqualsOperator.class, + TestGreaterThanOperator.class, TestMinorThanEqualsOperator.class, TestMinorThanOperator.class, + TestNotEqualsToOperator.class, TestNotOperator.class, TestOrOperator.class}) +public class TestOperatorsProcessor { +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestOrOperator.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestOrOperator.java new file mode 100644 index 00000000000..90467f5b9f9 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/operator/TestOrOperator.java @@ -0,0 +1,70 @@ +/* + * 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.operator; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestOrOperator extends AbstractOperatorTestBase { + + @Test + public void testOrOperator() throws Exception { + String transformSql = "select if((string2 < 4) or (numeric4 > 5),1,0) from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1: "3.14159265358979323846|3a|4|8" + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor.transform("3.14159265358979323846|3a|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=1"); + // case2: "3.14159265358979323846|5|4|8" + List output2 = processor.transform("3.14159265358979323846|5|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output2.get(0), "result=1"); + // case3: "3.14159265358979323846|5|4|4" + List output3 = processor.transform("3.14159265358979323846|5|4|4"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output3.get(0), "result=0"); + + transformSql = "select if((numeric3 < 4) or (numeric4 > 5),1,0) from source"; + config = new TransformConfig(transformSql); + // case4: "3.14159265358979323846|4|4|8" + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output4 = processor.transform("3.14159265358979323846|4|4|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output4.get(0), "result=1"); + // case5: "3.14159265358979323846|4|3.2|8" + List output5 = processor.transform("3.14159265358979323846|4|3.2|8"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output5.get(0), "result=1"); + // case6: "3.14159265358979323846|4|4.2|5" + List output6 = processor.transform("3.14159265358979323846|4|4.2|5"); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output6.get(0), "result=0"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/AbstractParserTestBase.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/AbstractParserTestBase.java new file mode 100644 index 00000000000..2534bff0969 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/AbstractParserTestBase.java @@ -0,0 +1,51 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; + +import java.util.ArrayList; +import java.util.List; + +/** + * AbstractParserTestBase + * description: define static parameters for Parser tests + */ +public abstract class AbstractParserTestBase { + + protected static final List srcFields = new ArrayList<>(); + protected static final List dstFields = new ArrayList<>(); + protected static final CsvSourceInfo csvSource; + protected static final KvSinkInfo kvSink; + + static { + for (int i = 1; i < 5; i++) { + FieldInfo field = new FieldInfo(); + field.setName("numeric" + i); + srcFields.add(field); + } + FieldInfo field = new FieldInfo(); + field.setName("result"); + dstFields.add(field); + csvSource = new CsvSourceInfo("UTF-8", '|', '\\', srcFields); + kvSink = new KvSinkInfo("UTF-8", dstFields); + } + +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseAndParser.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseAndParser.java new file mode 100644 index 00000000000..3a5362d3af5 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseAndParser.java @@ -0,0 +1,57 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestBitwiseAndParser extends AbstractParserTestBase { + + @Test + public void testBitwiseAndParser() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: 18446744073709551615 & -1 + transformSql = "select numeric1 & numeric2 from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "18446744073709551615|-1|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551615", output.get(0)); + + // case2: 18446744073709551615 & 0 + data = "18446744073709551615|0|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseInversionParser.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseInversionParser.java new file mode 100644 index 00000000000..68d84369b58 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseInversionParser.java @@ -0,0 +1,75 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestBitwiseInversionParser extends AbstractParserTestBase { + + @Test + public void testBitwiseInversionParser() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: ~-4 + transformSql = "select ~numeric1 from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "-4|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=3", output.get(0)); + + // case2: ~4 + data = "4|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551611", output.get(0)); + + // case3: ~0 + data = "0|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551615", output.get(0)); + + // case4: ~~-4 + transformSql = "select ~(~numeric1) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "-4|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551612", output.get(0)); + + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseLeftShiftParser.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseLeftShiftParser.java new file mode 100644 index 00000000000..53d9570523c --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseLeftShiftParser.java @@ -0,0 +1,63 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestBitwiseLeftShiftParser extends AbstractParserTestBase { + + @Test + public void testBitwiseLeftShiftParser() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: 9223372036854775807 << 1 + transformSql = "select numeric1 << numeric2 from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "9223372036854775807|1|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551614", output.get(0)); + + // case2: 18446744073709551615 << 18446744073709551615 + data = "18446744073709551615|18446744073709551615|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + + // case3: 9223372036854775807 << -1 + data = "9223372036854775807|-1|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseOrParser.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseOrParser.java new file mode 100644 index 00000000000..a7be8b25dda --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseOrParser.java @@ -0,0 +1,57 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestBitwiseOrParser extends AbstractParserTestBase { + + @Test + public void testBitwiseOrParser() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: 18446744073709551615 | -1 + transformSql = "select numeric1 | numeric2 from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "18446744073709551615|-1|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551615", output.get(0)); + + // case2: 4 | 3 + data = "4|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=7", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseRightShiftParser.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseRightShiftParser.java new file mode 100644 index 00000000000..67c59e89dbc --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseRightShiftParser.java @@ -0,0 +1,63 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestBitwiseRightShiftParser extends AbstractParserTestBase { + + @Test + public void testBitwiseRightShiftParser() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: 4 >> -1 + transformSql = "select numeric1 >> numeric2 from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "4|-1|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + + // case2: 9223372036854775808 >> 2 + data = "9223372036854775808|2|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2305843009213693952", output.get(0)); + + // case3: 9223372036854775808 >> 9223372036854775808 + data = "9223372036854775808|9223372036854775808|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=0", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseXorParser.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseXorParser.java new file mode 100644 index 00000000000..524ba35d3d4 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestBitwiseXorParser.java @@ -0,0 +1,57 @@ +/* + * 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.parser; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestBitwiseXorParser extends AbstractParserTestBase { + + @Test + public void testBitwiseXorOperator() throws Exception { + String transformSql = null, data = null; + TransformConfig config = null; + TransformProcessor processor = null; + List output = null; + + // case1: 4 ^ 3 + transformSql = "select numeric1 ^ numeric2 from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + data = "4|3|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=7", output.get(0)); + + // case2: 4 ^ -1 + data = "4|-1|3"; + output = processor.transform(data, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=18446744073709551611", output.get(0)); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestParsersProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestParsersProcessor.java new file mode 100644 index 00000000000..52952fed95c --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/parser/TestParsersProcessor.java @@ -0,0 +1,30 @@ +/* + * 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.parser; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +/** + * TestOperatorsProcessor + * description: test all the parser transform processor + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({TestBitwiseAndParser.class, TestBitwiseInversionParser.class, TestBitwiseLeftShiftParser.class, + TestBitwiseOrParser.class, TestBitwiseRightShiftParser.class, TestBitwiseXorParser.class}) +public class TestParsersProcessor { +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/AbstractProcessorTestBase.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/AbstractProcessorTestBase.java new file mode 100644 index 00000000000..58c2393631d --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/AbstractProcessorTestBase.java @@ -0,0 +1,68 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.pojo.FieldInfo; + +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +/** + * AbstractProcessorTestBase + * description: define static parameters for Processor tests + */ +public abstract class AbstractProcessorTestBase { + + protected List getTestFieldList(String... fieldNames) { + List fields = new ArrayList<>(); + for (String fieldName : fieldNames) { + FieldInfo field = new FieldInfo(); + field.setName(fieldName); + fields.add(field); + } + return fields; + } + + protected byte[] getPbTestData() { + String srcString = + "CgNzaWQSIAoJbXNnVmFsdWU0ELCdrqruMRoMCgNrZXkSBXZhbHVlEiMKCm1zZ1ZhbHVlNDIQsp2uqu4xGg4KBGtleTISBnZhbHVlMhgB"; + byte[] srcBytes = Base64.getDecoder().decode(srcString); + return srcBytes; + } + + protected String getPbTestDescription() { + final String transformProto = "syntax = \"proto3\";\n" + + "package test;\n" + + "message SdkMessage {\n" + + " bytes msg = 1;\n" + + " int64 msgTime = 2;\n" + + " map extinfo = 3;\n" + + "}\n" + + "message SdkDataRequest {\n" + + " string sid = 1;\n" + + " repeated SdkMessage msgs = 2;\n" + + " uint64 packageID = 3;\n" + + "}"; + String transformBase64 = "CrcCCg90cmFuc2Zvcm0ucHJvdG8SBHRlc3QirQEKClNka01lc3NhZ2USEAoDbXNnGAEgASgMUg" + + "Ntc2cSGAoHbXNnVGltZRgCIAEoA1IHbXNnVGltZRI3CgdleHRpbmZvGAMgAygLMh0udGVzdC5TZGtNZXNzYWdlLk" + + "V4dGluZm9FbnRyeVIHZXh0aW5mbxo6CgxFeHRpbmZvRW50cnkSEAoDa2V5GAEgASgJUgNrZXkSFAoFdmFsdWUY" + + "AiABKAlSBXZhbHVlOgI4ASJmCg5TZGtEYXRhUmVxdWVzdBIQCgNzaWQYASABKAlSA3NpZBIkCgRtc2dzGAIgAygLMh" + + "AudGVzdC5TZGtNZXNzYWdlUgRtc2dzEhwKCXBhY2thZ2VJRBgDIAEoBFIJcGFja2FnZUlEYgZwcm90bzM="; + return transformBase64; + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestCsv2KvProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestCsv2KvProcessor.java new file mode 100644 index 00000000000..6b19acb11c8 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestCsv2KvProcessor.java @@ -0,0 +1,82 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestCsv2KvProcessor extends AbstractProcessorTestBase { + + @Test + public void testCsv2Kv() throws Exception { + List fields = this.getTestFieldList("ftime", "extinfo"); + CsvSourceInfo csvSource = new CsvSourceInfo("UTF-8", '|', '\\', fields); + KvSinkInfo kvSink = new KvSinkInfo("UTF-8", fields); + String transformSql = "select ftime,extinfo from source where extinfo='ok'"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + List output1 = processor1.transform("2024-04-28 00:00:00|ok", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "ftime=2024-04-28 00:00:00&extinfo=ok"); + // case2 + config.setTransformSql("select ftime,extinfo from source where extinfo!='ok'"); + TransformProcessor processor2 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + List output2 = processor2.transform("2024-04-28 00:00:00|ok", new HashMap<>()); + Assert.assertEquals(0, output2.size()); + } + + @Test + public void testCsv2KvNoField() throws Exception { + CsvSourceInfo csvSource = new CsvSourceInfo("UTF-8", '|', '\\', null); + KvSinkInfo kvSink = new KvSinkInfo("UTF-8", null); + String transformSql = "select $1 ftime,$2 extinfo from source where $2='ok'"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor1.transform("2024-04-28 00:00:00|ok", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "ftime=2024-04-28 00:00:00&extinfo=ok"); + // case2 + config.setTransformSql("select $1 ftime,$2 extinfo from source where $2!='ok'"); + TransformProcessor processor2 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output2 = processor2.transform("2024-04-28 00:00:00|ok", new HashMap<>()); + Assert.assertEquals(0, output2.size()); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestCsv2StarProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestCsv2StarProcessor.java new file mode 100644 index 00000000000..9fdbf0b5d01 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestCsv2StarProcessor.java @@ -0,0 +1,81 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo; +import org.apache.inlong.sdk.transform.pojo.CsvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TestCsv2StarProcessor extends AbstractProcessorTestBase { + + @Test + public void testCsv2Star() throws Exception { + List fields = this.getTestFieldList("ftime", "extinfo"); + CsvSourceInfo csvSource = new CsvSourceInfo("UTF-8", '|', '\\', fields); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', new ArrayList<>()); + String transformSql = "select *"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + + List output1 = processor1.transform("2024-04-28 00:00:00|ok", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "2024-04-28 00:00:00|ok"); + // case2 + config.setTransformSql("select * from source where extinfo!='ok'"); + TransformProcessor processor2 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + + List output2 = processor2.transform("2024-04-28 00:00:00|ok", new HashMap<>()); + Assert.assertEquals(0, output2.size()); + // case3 + config.setTransformSql("select *,extinfo,ftime from source where extinfo!='ok'"); + TransformProcessor processor3 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + + List output3 = processor3.transform("2024-04-28 00:00:00|nok", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "2024-04-28 00:00:00|nok|nok|2024-04-28 00:00:00"); + // case4 + CsvSourceInfo csvSourceNoField = new CsvSourceInfo("UTF-8", '|', '\\', new ArrayList<>()); + CsvSinkInfo csvSinkNoField = new CsvSinkInfo("UTF-8", '|', '\\', new ArrayList<>()); + config.setTransformSql("select *,$2,$1 from source where $2='nok'"); + TransformProcessor processor4 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSourceNoField), + SinkEncoderFactory.createCsvEncoder(csvSinkNoField)); + + List output4 = processor4.transform("2024-04-28 00:00:00|nok", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "2024-04-28 00:00:00|nok|nok|2024-04-28 00:00:00"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestJson2CsvProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestJson2CsvProcessor.java new file mode 100644 index 00000000000..704f2441e91 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestJson2CsvProcessor.java @@ -0,0 +1,116 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.JsonSourceInfo; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestJson2CsvProcessor extends AbstractProcessorTestBase { + + @Test + public void testJson2Csv() throws Exception { + List fields1 = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + JsonSourceInfo jsonSource1 = new JsonSourceInfo("UTF-8", "msgs"); + CsvSinkInfo csvSink1 = new CsvSinkInfo("UTF-8", '|', '\\', fields1); + String transformSql1 = "select $root.sid,$root.packageID,$child.msgTime,$child.msg from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createJsonDecoder(jsonSource1), + SinkEncoderFactory.createCsvEncoder(csvSink1)); + String srcString1 = "{\n" + + " \"sid\":\"value1\",\n" + + " \"packageID\":\"value2\",\n" + + " \"msgs\":[\n" + + " {\"msg\":\"value4\",\"msgTime\":1713243918000},\n" + + " {\"msg\":\"v4\",\"msgTime\":1713243918000}\n" + + " ]\n" + + "}"; + List output1 = processor1.transform(srcString1, new HashMap<>()); + Assert.assertEquals(2, output1.size()); + Assert.assertEquals(output1.get(0), "value1|value2|1713243918000|value4"); + Assert.assertEquals(output1.get(1), "value1|value2|1713243918000|v4"); + // case2 + List fields2 = this.getTestFieldList("id", "itemId", "subItemId", "msg"); + JsonSourceInfo jsonSource2 = new JsonSourceInfo("UTF-8", "items"); + CsvSinkInfo csvSink2 = new CsvSinkInfo("UTF-8", '|', '\\', fields2); + String transformSql2 = + "select $root.id,$child.itemId,$child.subItems(0).subItemId,$child.subItems(1).msg from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createJsonDecoder(jsonSource2), + SinkEncoderFactory.createCsvEncoder(csvSink2)); + String srcString2 = "{\n" + + " \"id\":\"value1\",\n" + + " \"name\":\"value2\",\n" + + " \"items\":[\n" + + " {\"itemId\":\"item1\",\n" + + " \"subItems\":[\n" + + " {\"subItemId\":\"1001\", \"msg\":\"1001msg\"},\n" + + " {\"subItemId\":\"1002\", \"msg\":\"1002msg\"}\n" + + " ]\n" + + " },\n" + + " {\"itemId\":\"item2\",\n" + + " \"subItems\":[\n" + + " {\"subItemId\":\"2001\", \"msg\":\"2001msg\"},\n" + + " {\"subItemId\":\"2002\", \"msg\":\"2002msg\"}\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}"; + List output2 = processor2.transform(srcString2, new HashMap<>()); + Assert.assertEquals(2, output2.size()); + Assert.assertEquals(output2.get(0), "value1|item1|1001|1002msg"); + Assert.assertEquals(output2.get(1), "value1|item2|2001|2002msg"); + } + + @Test + public void testJson2CsvForOne() throws Exception { + List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + JsonSourceInfo jsonSource = new JsonSourceInfo("UTF-8", ""); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select $root.sid,$root.packageID,$root.msgs(1).msgTime,$root.msgs(0).msg from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createJsonDecoder(jsonSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + String srcString = "{\n" + + " \"sid\":\"value1\",\n" + + " \"packageID\":\"value2\",\n" + + " \"msgs\":[\n" + + " {\"msg\":\"value4\",\"msgTime\":1713243918000},\n" + + " {\"msg\":\"v4\",\"msgTime\":1713243918000}\n" + + " ]\n" + + "}"; + List output = processor.transform(srcString, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals(output.get(0), "value1|value2|1713243918000|value4"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestKv2CsvProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestKv2CsvProcessor.java new file mode 100644 index 00000000000..82a4762f93b --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestKv2CsvProcessor.java @@ -0,0 +1,80 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestKv2CsvProcessor extends AbstractProcessorTestBase { + + @Test + public void testKv2Csv() throws Exception { + List fields = this.getTestFieldList("ftime", "extinfo"); + KvSourceInfo kvSource = new KvSourceInfo("UTF-8", fields); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select ftime,extinfo from source where extinfo='ok'"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + List output1 = processor1.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "2024-04-28 00:00:00|ok"); + // case2 + config.setTransformSql("select ftime,extinfo from source where extinfo!='ok'"); + TransformProcessor processor2 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + List output2 = processor2.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); + Assert.assertEquals(0, output2.size()); + } + + @Test + public void testKv2CsvNoField() throws Exception { + KvSourceInfo kvSource = new KvSourceInfo("UTF-8", null); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', null); + String transformSql = "select ftime,extinfo from source where extinfo='ok'"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + List output1 = processor1.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "2024-04-28 00:00:00|ok"); + // case2 + config.setTransformSql("select ftime,extinfo from source where extinfo!='ok'"); + TransformProcessor processor2 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + List output2 = processor2.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); + Assert.assertEquals(0, output2.size()); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestKv2StarProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestKv2StarProcessor.java new file mode 100644 index 00000000000..f2bedfc4927 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestKv2StarProcessor.java @@ -0,0 +1,68 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.KvSinkInfo; +import org.apache.inlong.sdk.transform.pojo.KvSourceInfo; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class TestKv2StarProcessor extends AbstractProcessorTestBase { + + @Test + public void testKv2Star() throws Exception { + List fields = this.getTestFieldList("ftime", "extinfo"); + KvSourceInfo kvSource = new KvSourceInfo("UTF-8", fields); + KvSinkInfo kvSink = new KvSinkInfo("UTF-8", new ArrayList<>()); + String transformSql = "select *"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output1 = processor1.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "ftime=2024-04-28 00:00:00&extinfo=ok"); + // case2 + config.setTransformSql("select * from source where extinfo!='ok'"); + TransformProcessor processor2 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output2 = processor2.transform("ftime=2024-04-28 00:00:00&extinfo=ok", new HashMap<>()); + Assert.assertEquals(0, output2.size()); + // case3 + config.setTransformSql("select *,extinfo e1,ftime f1 from source where extinfo!='ok'"); + TransformProcessor processor3 = TransformProcessor + .create(config, SourceDecoderFactory.createKvDecoder(kvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + List output3 = processor3.transform("ftime=2024-04-28 00:00:00&extinfo=nok", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "ftime=2024-04-28 00:00:00&extinfo=nok&e1=nok&f1=2024-04-28 00:00:00"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestPb2CsvProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestPb2CsvProcessor.java new file mode 100644 index 00000000000..ff652bff12a --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestPb2CsvProcessor.java @@ -0,0 +1,133 @@ +/* + * 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.processor; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.CsvSinkInfo; +import org.apache.inlong.sdk.transform.pojo.FieldInfo; +import org.apache.inlong.sdk.transform.pojo.PbSourceInfo; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestPb2CsvProcessor extends AbstractProcessorTestBase { + + @Test + public void testPb2Csv() throws Exception { + List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + String transformBase64 = this.getPbTestDescription(); + PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", "msgs"); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select $root.sid,$root.packageID,$child.msgTime,$child.msg from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createPbDecoder(pbSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + byte[] srcBytes = this.getPbTestData(); + List output = processor.transform(srcBytes); + Assert.assertEquals(2, output.size()); + Assert.assertEquals(output.get(0), "sid|1|1713243918000|msgValue4"); + Assert.assertEquals(output.get(1), "sid|1|1713243918002|msgValue42"); + } + + @Test + public void testPb2CsvForOne() throws Exception { + List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + String transformBase64 = this.getPbTestDescription(); + PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", null); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select $root.sid,$root.packageID,$root.msgs(1).msgTime,$root.msgs(0).msg from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createPbDecoder(pbSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + byte[] srcBytes = this.getPbTestData(); + List output = processor.transform(srcBytes, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals(output.get(0), "sid|1|1713243918002|msgValue4"); + } + + @Test + public void testPb2CsvForAdd() throws Exception { + List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + String transformBase64 = this.getPbTestDescription(); + PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", null); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select $root.sid," + + "($root.msgs(1).msgTime-$root.msgs(0).msgTime)/$root.packageID field2," + + "$root.packageID*($root.msgs(0).msgTime*$root.packageID+$root.msgs(1).msgTime/$root.packageID)" + + "*$root.packageID field3," + + "$root.msgs(0).msg field4 from source " + + "where $root.packageID<($root.msgs(0).msgTime+$root.msgs(1).msgTime" + + "+$root.msgs(0).msgTime+$root.msgs(1).msgTime)"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createPbDecoder(pbSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + byte[] srcBytes = this.getPbTestData(); + List output = processor.transform(srcBytes, new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals(output.get(0), "sid|2|3426487836002|msgValue4"); + } + + @Test + public void testPb2CsvForConcat() throws Exception { + List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + String transformBase64 = this.getPbTestDescription(); + PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", "msgs"); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select $root.sid,$root.packageID,$child.msgTime," + + "concat($root.sid,$root.packageID,$child.msgTime,$child.msg) msg,$root.msgs.msgTime.msg from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createPbDecoder(pbSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + byte[] srcBytes = this.getPbTestData(); + List output = processor.transform(srcBytes, new HashMap<>()); + Assert.assertTrue(output.size() == 2); + Assert.assertEquals(output.get(0), "sid|1|1713243918000|sid11713243918000msgValue4"); + Assert.assertEquals(output.get(1), "sid|1|1713243918002|sid11713243918002msgValue42"); + } + + @Test + public void testPb2CsvForNow() throws Exception { + List fields = this.getTestFieldList("sid", "packageID", "msgTime", "msg"); + String transformBase64 = this.getPbTestDescription(); + PbSourceInfo pbSource = new PbSourceInfo("UTF-8", transformBase64, "SdkDataRequest", "msgs"); + CsvSinkInfo csvSink = new CsvSinkInfo("UTF-8", '|', '\\', fields); + String transformSql = "select now() from source"; + TransformConfig config = new TransformConfig(transformSql); + // case1 + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createPbDecoder(pbSource), + SinkEncoderFactory.createCsvEncoder(csvSink)); + byte[] srcBytes = this.getPbTestData(); + List output = processor.transform(srcBytes, new HashMap<>()); + Assert.assertEquals(2, output.size()); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestTransformProcessors.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestTransformProcessors.java new file mode 100644 index 00000000000..e1b244176f8 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/processor/TestTransformProcessors.java @@ -0,0 +1,30 @@ +/* + * 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.processor; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +/** + * TestTransformProcessors + * description: test all the transform processors case + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({TestCsv2KvProcessor.class, TestCsv2StarProcessor.class, TestJson2CsvProcessor.class, + TestKv2CsvProcessor.class, TestKv2CsvProcessor.class, TestKv2StarProcessor.class, TestPb2CsvProcessor.class}) +public class TestTransformProcessors { +}