This is an automated email from the ASF dual-hosted git repository.

aloyszhang 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 f35ab6f619 [INLONG-10934][SDK] Transform support LocalDate function 
(#10951)
f35ab6f619 is described below

commit f35ab6f619004fac58a06f79b98e681e778622f0
Author: Xincheng Huang <[email protected]>
AuthorDate: Thu Aug 29 20:19:44 2024 +0800

    [INLONG-10934][SDK] Transform support LocalDate function (#10951)
---
 .../process/function/LocalDateFunction.java        | 56 ++++++++++++++++++++++
 .../TestTransformTemporalFunctionsProcessor.java   | 42 ++++++++++++++++
 2 files changed, 98 insertions(+)

diff --git 
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalDateFunction.java
 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalDateFunction.java
new file mode 100644
index 0000000000..ba2d909d95
--- /dev/null
+++ 
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/LocalDateFunction.java
@@ -0,0 +1,56 @@
+/*
+ * 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.LocalDate;
+import java.time.ZoneId;
+
+/**
+ *  LocalDateFunction
+ *  description:
+ *  localDate([string1]) returns the current date in the specified time zone.
+ *  (by default: the current date in the system time zone)
+ */
+@TransformFunction(names = {"localdate", "currentdate", "current_date", 
"curdate"})
+public class LocalDateFunction implements ValueParser {
+
+    private ValueParser stringParser;
+
+    public LocalDateFunction(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 LocalDate.now(ZoneId.of(zoneString));
+        } else {
+            return LocalDate.now(ZoneId.systemDefault());
+        }
+    }
+}
\ No newline at end of file
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 e0830dd72e..354b8b7f18 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
@@ -29,6 +29,7 @@ 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;
@@ -421,6 +422,47 @@ public class TestTransformTemporalFunctionsProcessor {
         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<String, String> processor1 = TransformProcessor
+                .create(config1, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        List<String> 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<String, String> processor2 = TransformProcessor
+                .create(config2, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        List<String> 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<String, String> processor3 = TransformProcessor
+                .create(config3, 
SourceDecoderFactory.createCsvDecoder(csvSource),
+                        SinkEncoderFactory.createKvEncoder(kvSink));
+        List<String> 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";

Reply via email to