This is an automated email from the ASF dual-hosted git repository. dockerzhang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push: new 18b9905c9d [INLONG-11218][SDK] Transform support CONVERT_TZ() function (#11251) 18b9905c9d is described below commit 18b9905c9dd739726afa1fcfdf7b5521480633d2 Author: emptyOVO <118812562+empty...@users.noreply.github.com> AuthorDate: Tue Oct 8 12:55:15 2024 +0800 [INLONG-11218][SDK] Transform support CONVERT_TZ() function (#11251) --- .../process/function/ConvertTzFunction.java | 80 ++++++++++++++++++ .../function/temporal/TestConvertTzFunction.java | 96 ++++++++++++++++++++++ 2 files changed, 176 insertions(+) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ConvertTzFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ConvertTzFunction.java new file mode 100644 index 0000000000..5e2327d030 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ConvertTzFunction.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; + +import org.apache.inlong.sdk.transform.decode.SourceData; +import org.apache.inlong.sdk.transform.process.Context; +import org.apache.inlong.sdk.transform.process.operator.OperatorTools; +import org.apache.inlong.sdk.transform.process.parser.ValueParser; + +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.Function; + +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +/** + * ConvertTzFunction + * Description: CONVERT_TZ(string1, string2, string3)--Converts a datetime string1 (with default ISO timestamp format + * ‘yyyy-MM-dd HH:mm:ss’) from time zone string2 to time zone string3. The format of time zone should be + * either an abbreviation such as “PST”, a full name such as “America/Los_Angeles”, or a custom ID such + * as “GMT-08:00”. + * for example: CONVERT_TZ(‘1970-01-01 00:00:00’, ‘UTC’, ‘America/Los_Angeles’) returns ‘1969-12-31 16:00:00’. + */ +@TransformFunction(names = {"convert_tz"}) +public class ConvertTzFunction implements ValueParser { + + private ValueParser dateTimeParser; + + private ValueParser fromTimeZoneParser; + + private ValueParser toTimeZoneParser; + + public ConvertTzFunction(Function expr) { + List<Expression> expressions = expr.getParameters().getExpressions(); + dateTimeParser = OperatorTools.buildParser(expressions.get(0)); + fromTimeZoneParser = OperatorTools.buildParser(expressions.get(1)); + toTimeZoneParser = OperatorTools.buildParser(expressions.get(2)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object datetimeObj = dateTimeParser.parse(sourceData, rowIndex, context); + Object fromTimeZoneObj = fromTimeZoneParser.parse(sourceData, rowIndex, context); + Object toTimeZoneObj = toTimeZoneParser.parse(sourceData, rowIndex, context); + if (datetimeObj == null || fromTimeZoneObj == null || toTimeZoneObj == null) { + return null; + } + String dateTime = OperatorTools.parseString(datetimeObj); + String fromTimeZone = OperatorTools.parseString(fromTimeZoneObj); + String toTimeZone = OperatorTools.parseString(toTimeZoneObj); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + LocalDateTime localDateTime = LocalDateTime.parse(dateTime, formatter); + + ZoneId fromZoneId = ZoneId.of(fromTimeZone); + ZoneId toZoneId = ZoneId.of(toTimeZone); + + ZonedDateTime fromZonedDateTime = ZonedDateTime.of(localDateTime, fromZoneId); + ZonedDateTime toZonedDateTime = fromZonedDateTime.withZoneSameInstant(toZoneId); + + return toZonedDateTime.format(formatter); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestConvertTzFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestConvertTzFunction.java new file mode 100644 index 0000000000..4e05fdd1da --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestConvertTzFunction.java @@ -0,0 +1,96 @@ +/* + * 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 TestConvertTzFunction extends AbstractFunctionTemporalTestBase { + + @Test + public void testConvertTzFunction() throws Exception { + String transformSql = null; + TransformConfig config = null; + TransformProcessor<String, String> processor = null; + List<String> output = null; + + transformSql = "select convert_tz(string1,string2,string3) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: convert_tz('1970-01-01 00:00:00', 'UTC', 'America/Los_Angeles') + output = processor.transform("1970-01-01 00:00:00|UTC|America/Los_Angeles", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=1969-12-31 16:00:00", output.get(0)); + + // case1: convert_tz('1970-01-01 00:00:00', 'UTC', 'America/Los_Angeles') + output = processor.transform("2024-09-29 12:00:00|Asia/Tokyo|UTC", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-09-29 03:00:00", output.get(0)); + + transformSql = "select convert_tz(string1,string2,string3) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: convert_tz('2024-12-31 23:59:59', 'Europe/London', 'America/New_York') + output = processor.transform("2024-12-31 23:59:59|Europe/London|America/New_York", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-12-31 18:59:59", output.get(0)); + + transformSql = "select convert_tz(string1,string2,string3) from source"; + config = new TransformConfig(transformSql); + processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: convert_tz('2024-07-15 10:30:00', 'America/New_York', 'Australia/Sydney') + output = processor.transform("2024-07-15 10:30:00|America/New_York|Australia/Sydney", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-07-16 00:30:00", output.get(0)); + + // case1: convert_tz('2024-03-31 02:00:00', 'Europe/Berlin', 'Asia/Kolkata') + output = processor.transform("2024-03-31 02:00:00|Europe/Berlin|Asia/Kolkata", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-03-31 06:30:00", output.get(0)); + + // case1: convert_tz('2024-11-01 15:45:00', 'Australia/Sydney', 'UTC') + output = processor.transform("2024-11-01 15:45:00|Australia/Sydney|UTC", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-11-01 04:45:00", output.get(0)); + + // case1: convert_tz('2024-06-21 00:00:00', 'America/Los_Angeles', 'Europe/Paris') + output = processor.transform("2024-06-21 00:00:00|America/Los_Angeles|Europe/Paris", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-06-21 09:00:00", output.get(0)); + + // case1: convert_tz('2024-01-01 10:00:00', 'GMT-03:00', 'Asia/Shanghai') + output = processor.transform("2024-01-01 10:00:00|GMT-03:00|Asia/Shanghai", new HashMap<>()); + Assert.assertEquals(1, output.size()); + Assert.assertEquals("result=2024-01-01 21:00:00", output.get(0)); + + } +}