This is an automated email from the ASF dual-hosted git repository.
luchunliang 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 1454cbba96 [INLONG-11152][SDK] Transform SQL supports TIMESTAMPDIFF
function (#11160)
1454cbba96 is described below
commit 1454cbba96bd7a8237a27b0487fa58ca49b81f3f
Author: Zkplo <[email protected]>
AuthorDate: Tue Sep 24 09:59:02 2024 +0800
[INLONG-11152][SDK] Transform SQL supports TIMESTAMPDIFF function (#11160)
Co-authored-by: ZKpLo <[email protected]>
---
.../process/function/TimestampDiffFunction.java | 119 +++++++++++++++++++++
.../temporal/TestTimestampDiffFunction.java | 79 ++++++++++++++
2 files changed, 198 insertions(+)
diff --git
a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TimestampDiffFunction.java
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TimestampDiffFunction.java
new file mode 100644
index 0000000000..ebc045db22
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TimestampDiffFunction.java
@@ -0,0 +1,119 @@
+/*
+ * 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.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoUnit;
+import java.util.List;
+
+/**
+ * TimestampDiffFunction -> TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
+ * Description:
+ * - returns NULL if datetime_expr1 or datetime_expr2 is NULL.
+ * - returns datetime_expr2 − datetime_expr1, where datetime_expr1 and
datetime_expr2 are date or datetime expressions.
+ * The unit parameter: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY,
WEEK, MONTH, QUARTER, or YEAR.
+ */
+@TransformFunction(names = {"timestamp_diff", "timestampdiff"})
+public class TimestampDiffFunction implements ValueParser {
+
+ private ValueParser unitParser;
+ private ValueParser firstDateTimeParser;
+ private ValueParser secondDateTimeParser;
+ private static final DateTimeFormatter DEFAULT_FORMAT_DATE_MRO_TIME =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");
+ private static final DateTimeFormatter DEFAULT_FORMAT_DATE_TIME =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+ private static final DateTimeFormatter DEFAULT_FORMAT_DATE =
DateTimeFormatter.ofPattern("yyyy-MM-dd");
+
+ public TimestampDiffFunction(Function expr) {
+ List<Expression> expressions = expr.getParameters().getExpressions();
+ unitParser = OperatorTools.buildParser(expressions.get(0));
+ firstDateTimeParser = OperatorTools.buildParser(expressions.get(1));
+ secondDateTimeParser = OperatorTools.buildParser(expressions.get(2));
+ }
+
+ @Override
+ public Object parse(SourceData sourceData, int rowIndex, Context context) {
+ Object unitObj = unitParser.parse(sourceData, rowIndex, context);
+ Object firstDateTimeObj = firstDateTimeParser.parse(sourceData,
rowIndex, context);
+ Object secondDateTimeObj = secondDateTimeParser.parse(sourceData,
rowIndex, context);
+ if (firstDateTimeObj == null || secondDateTimeObj == null || unitObj
== null) {
+ return null;
+ }
+ String unit = OperatorTools.parseString(unitObj).toUpperCase();
+ String firstDateTime = OperatorTools.parseString(firstDateTimeObj);
+ String secondDateTime = OperatorTools.parseString(secondDateTimeObj);
+ if (firstDateTime.isEmpty() || secondDateTime.isEmpty()) {
+ return null;
+ }
+ try {
+ LocalDateTime left = getLocalDate(firstDateTime);
+ LocalDateTime right = getLocalDate(secondDateTime);
+ switch (unit) {
+ case "MICROSECOND":
+ return ChronoUnit.MICROS.between(left, right);
+ case "SECOND":
+ return ChronoUnit.SECONDS.between(left, right);
+ case "MINUTE":
+ return ChronoUnit.MINUTES.between(left, right);
+ case "HOUR":
+ return ChronoUnit.HOURS.between(left, right);
+ case "DAY":
+ return ChronoUnit.DAYS.between(left, right);
+ case "WEEK":
+ return ChronoUnit.WEEKS.between(left, right);
+ case "MONTH":
+ return ChronoUnit.MONTHS.between(left, right);
+ case "QUARTER":
+ return ChronoUnit.MONTHS.between(left, right) / 3;
+ case "YEAR":
+ return ChronoUnit.YEARS.between(left, right);
+ default:
+ return null;
+ }
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ public LocalDateTime getLocalDate(String dateString) {
+ DateTimeFormatter formatter = null;
+ LocalDateTime dateTime = null;
+ if (dateString.indexOf('.') != -1) {
+ formatter = DEFAULT_FORMAT_DATE_MRO_TIME;
+ dateTime = LocalDateTime.parse(dateString, formatter);
+ } else if (dateString.indexOf(' ') != -1) {
+ formatter = DEFAULT_FORMAT_DATE_TIME;
+ dateTime = LocalDateTime.parse(dateString, formatter);
+ } else {
+ formatter = DEFAULT_FORMAT_DATE;
+ dateTime = LocalDate.parse(dateString, formatter).atStartOfDay();
+ }
+ return dateTime;
+ }
+}
diff --git
a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampDiffFunction.java
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampDiffFunction.java
new file mode 100644
index 0000000000..5c7d89954f
--- /dev/null
+++
b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampDiffFunction.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.util.HashMap;
+import java.util.List;
+
+public class TestTimestampDiffFunction extends
AbstractFunctionTemporalTestBase {
+
+ @Test
+ public void testTimestampDiffFunction() throws Exception {
+ String transformSql1 = "select timestampdiff(string1,string2,string3)
from source";
+ TransformConfig config1 = new TransformConfig(transformSql1);
+ TransformProcessor<String, String> processor1 = TransformProcessor
+ .create(config1,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case1: timestampdiff('MONTH','2003-02-01','2003-05-01')
+ List<String> output1 =
processor1.transform("MONTH|2003-02-01|2003-05-01", new HashMap<>());
+ Assert.assertEquals(1, output1.size());
+ Assert.assertEquals("result=3", output1.get(0));
+
+ // case2: timestampdiff('YEAR','2002-05-01','2001-01-01')
+ List<String> output2 =
processor1.transform("YEAR|2002-05-01|2001-01-01", new HashMap<>());
+ Assert.assertEquals(1, output2.size());
+ Assert.assertEquals("result=-1", output2.get(0));
+
+ // case3: timestampdiff('MICROSECOND','12:05:55.999999','2001-01-01')
+ List<String> output3 =
processor1.transform("MICROSECOND|12:05:55.999999|2003-05-01", new HashMap<>());
+ Assert.assertEquals(1, output3.size());
+ Assert.assertEquals("result=", output3.get(0));
+
+ // case4: timestampdiff('MICROSECOND','2003-05-01
12:05:55.999999','2003-05-01')
+ List<String> output4 =
+ processor1.transform("MICROSECOND|2003-05-01
12:05:55.999999|2003-05-01", new HashMap<>());
+ Assert.assertEquals(1, output4.size());
+ Assert.assertEquals("result=-43555999999", output4.get(0));
+
+ // case5: timestampdiff('QUARTER','2003-05-01
12:05:55.999999','2663-05-01')
+ List<String> output5 = processor1.transform("QUARTER|2003-05-01
12:05:55.999999|2663-05-01", new HashMap<>());
+ Assert.assertEquals(1, output5.size());
+ Assert.assertEquals("result=2639", output5.get(0));
+
+ String transformSql2 = "select timestampdiff(string1,stringx,string3)
from source";
+ TransformConfig config2 = new TransformConfig(transformSql2);
+ TransformProcessor<String, String> processor2 = TransformProcessor
+ .create(config2,
SourceDecoderFactory.createCsvDecoder(csvSource),
+ SinkEncoderFactory.createKvEncoder(kvSink));
+
+ // case6: timestampdiff('MONTH',null,'2003-05-01')
+ List<String> output6 =
processor2.transform("MONTH|2003-02-01|2003-05-01", new HashMap<>());
+ Assert.assertEquals(1, output6.size());
+ Assert.assertEquals("result=", output6.get(0));
+
+ }
+}