From 916e6c1532ac4f740abb7b4c0babf76ef855baee Mon Sep 17 00:00:00 2001 From: Xincheng Huang <60057611+ying-hua@users.noreply.github.com> Date: Tue, 27 Aug 2024 19:35:32 +0800 Subject: [PATCH] [INLONG-10920][SDK] Transform support LocalTime function (#10922) --- .../process/function/LocalTimeFunction.java | 55 +++++++++++++++++++ .../process/operator/OperatorTools.java | 3 + ...stTransformTemporalFunctionsProcessor.java | 47 ++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java new file mode 100644 index 00000000000..a3072901a30 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalTimeFunction.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.inlong.sdk.transform.process.function; + +import org.apache.inlong.sdk.transform.decode.SourceData; +import org.apache.inlong.sdk.transform.process.Context; +import org.apache.inlong.sdk.transform.process.operator.OperatorTools; +import org.apache.inlong.sdk.transform.process.parser.ValueParser; + +import net.sf.jsqlparser.expression.Function; + +import java.time.LocalTime; +import java.time.ZoneId; + +/** + * LocalTimeFunction + * description: + * localTime([string1]) returns the current time in the specified time zone. + * (by default: the current time in the system time zone) + */ +public class LocalTimeFunction implements ValueParser { + + private ValueParser stringParser; + + public LocalTimeFunction(Function expr) { + if (expr.getParameters() != null) { + stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0)); + } + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + if (stringParser != null) { + String zoneString = OperatorTools.parseString(stringParser.parse(sourceData, rowIndex, context)); + return LocalTime.now(ZoneId.of(zoneString)).withNano(0); + } else { + return LocalTime.now(ZoneId.systemDefault()).withNano(0); + } + } +} diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java index 9c6f0d07312..5dc282130ba 100644 --- a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java @@ -31,6 +31,7 @@ import org.apache.inlong.sdk.transform.process.function.FromUnixTimeFunction; import org.apache.inlong.sdk.transform.process.function.LengthFunction; import org.apache.inlong.sdk.transform.process.function.LnFunction; +import org.apache.inlong.sdk.transform.process.function.LocalTimeFunction; import org.apache.inlong.sdk.transform.process.function.LocateFunction; import org.apache.inlong.sdk.transform.process.function.Log10Function; import org.apache.inlong.sdk.transform.process.function.Log2Function; @@ -118,6 +119,8 @@ public class OperatorTools { static { functionMap.put("concat", ConcatFunction::new); functionMap.put("now", NowFunction::new); + functionMap.put("localtime", LocalTimeFunction::new); + functionMap.put("currenttime", LocalTimeFunction::new); functionMap.put("power", PowerFunction::new); functionMap.put("abs", AbsFunction::new); functionMap.put("sqrt", SqrtFunction::new); diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java index 7a40411bc6f..67027c4b25d 100644 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformTemporalFunctionsProcessor.java @@ -28,6 +28,10 @@ import org.junit.Before; import org.junit.Test; +import java.time.Duration; +import java.time.LocalTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -362,4 +366,47 @@ public void testToTimestampFunction() throws Exception { Assert.assertEquals(1, output4.size()); Assert.assertEquals(output4.get(0), "result=1970-01-01 00:00:00.0"); } + + @Test + public void testLocalTimeFunction() throws Exception { + String transformSql1 = "select localTime() from source"; + TransformConfig config1 = new TransformConfig(transformSql1); + TransformProcessor processor1 = TransformProcessor + .create(config1, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + DateTimeFormatter fomatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + // case1: localTime() + List output1 = processor1.transform("", new HashMap<>()); + LocalTime expectedTime1 = LocalTime.now().withNano(0); + LocalTime actualTime1 = LocalTime.parse(output1.get(0).split("=")[1], fomatter); + Duration duration1 = Duration.between(expectedTime1, actualTime1); + Assert.assertEquals(1, output1.size()); + Assert.assertTrue(duration1.getSeconds() < 1); + + // case2: currentTime("UTC") + String transformSql2 = "select currentTime('UTC') from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output2 = processor2.transform("", new HashMap<>()); + LocalTime expectedTime2 = LocalTime.now(ZoneId.of("UTC")).withNano(0); + LocalTime actualTime2 = LocalTime.parse(output2.get(0).split("=")[1], fomatter); + Duration duration2 = Duration.between(expectedTime2, actualTime2); + Assert.assertEquals(1, output2.size()); + Assert.assertTrue(duration2.getSeconds() < 1); + + // case 3: localTime("America/New_York") + String transformSql3 = "select localTime('America/New_York') from source"; + TransformConfig config3 = new TransformConfig(transformSql3); + TransformProcessor processor3 = TransformProcessor + .create(config3, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + List output3 = processor3.transform("", new HashMap<>()); + LocalTime expectedTime3 = LocalTime.now(ZoneId.of("America/New_York")).withNano(0); + LocalTime actualTime3 = LocalTime.parse(output3.get(0).split("=")[1], fomatter); + Duration duration3 = Duration.between(expectedTime3, actualTime3); + Assert.assertEquals(1, output3.size()); + Assert.assertTrue(duration3.getSeconds() < 1); + } }