-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11154][SDK] Transform SQL supports TIMESTAMP function
- Loading branch information
ZKpLo
committed
Sep 21, 2024
1 parent
260a12d
commit d77dc08
Showing
3 changed files
with
233 additions
and
21 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/TimestampFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 org.apache.inlong.sdk.transform.process.utils.DateUtil; | ||
|
||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.expression.Function; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
|
||
/** | ||
* TimestampFunction -> timestamp(expr1[, expr2]) | ||
* description: | ||
* - return NULL if expr1 or expr2 is NULL. | ||
* - return the date or datetime expression expr as a datetime value if there is only one parameter | ||
* - return the result of the date or date time expression expr1 plus the time expression expr2 if there are two parameters | ||
*/ | ||
@TransformFunction(names = {"timestamp"}) | ||
public class TimestampFunction implements ValueParser { | ||
|
||
private ValueParser dateTimeExprParser; | ||
private ValueParser timeExprParser; | ||
|
||
public TimestampFunction(Function expr) { | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
dateTimeExprParser = OperatorTools.buildParser(expressions.get(0)); | ||
if (expressions.size() == 2) { | ||
timeExprParser = OperatorTools.buildParser(expressions.get(1)); | ||
} | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object dateTimeExprObj = dateTimeExprParser.parse(sourceData, rowIndex, context); | ||
if (dateTimeExprObj == null) { | ||
return null; | ||
} | ||
String dateTimeStr = dateTimeExprObj.toString(); | ||
LocalDateTime localDateTime = DateUtil.parseLocalDateTime(dateTimeExprObj.toString()); | ||
if (localDateTime == null) { | ||
// Not meeting the format requirements | ||
return null; | ||
} | ||
boolean hasMicroSecond = dateTimeStr.indexOf('.') != -1; | ||
String formatStr = DateUtil.YEAR_TO_SECOND; | ||
// Support the second parameter | ||
if (timeExprParser != null) { | ||
Object timeExprObj = timeExprParser.parse(sourceData, rowIndex, context); | ||
if (timeExprObj != null) { | ||
String timeStr = timeExprObj.toString(); | ||
LocalTime localTime = DateUtil.parseLocalTime(timeStr); | ||
if (localTime == null) { | ||
// Not meeting the format requirements | ||
return null; | ||
} | ||
hasMicroSecond |= timeStr.indexOf('.') != -1; | ||
localDateTime = DateUtil.dateAdd(localDateTime, localTime); | ||
} else { | ||
return null; | ||
} | ||
} | ||
if (hasMicroSecond) { | ||
formatStr = DateUtil.YEAR_TO_MICRO; | ||
} | ||
return localDateTime.format(DateUtil.getDateTimeFormatter(formatStr)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...java/org/apache/inlong/sdk/transform/process/function/temporal/TestTimestampFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 TestTimestampFunction extends AbstractFunctionTemporalTestBase { | ||
|
||
@Test | ||
public void testTimestamp() throws Exception { | ||
String transformSql = null; | ||
TransformConfig config = null; | ||
TransformProcessor<String, String> processor = null; | ||
List<String> output = null; | ||
|
||
transformSql = "select timestamp(string1,string2) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case1: timestamp('2003-12-31 12:00:00.600000','12:00:00') | ||
output = processor.transform("2003-12-31 12:00:00.600000|12:00:00", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2004-01-01 00:00:00.600000", output.get(0)); | ||
|
||
// case2: timestamp('2003-12-31 12:00:00','12:00:00.600000') | ||
output = processor.transform("2003-12-31 12:00:00|12:00:00.600000", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2004-01-01 00:00:00.600000", output.get(0)); | ||
|
||
// case3: timestamp('2003-12-31','12:00:00.600000') | ||
output = processor.transform("2003-12-31|12:00:00.600000", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2003-12-31 12:00:00.600000", output.get(0)); | ||
|
||
transformSql = "select timestamp(string1,stringx) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case4: timestamp('2003-12-31 12:00:00.600000',null) | ||
output = processor.transform("2003-12-31 12:00:00.600000|12:00:00", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=", output.get(0)); | ||
|
||
transformSql = "select timestamp(string1) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case5: timestamp('2003-12-31 12:00:00') | ||
output = processor.transform("2003-12-31 12:00:00", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2003-12-31 12:00:00", output.get(0)); | ||
|
||
// case6: timestamp('2003-12-31') | ||
output = processor.transform("2003-12-31", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2003-12-31 00:00:00", output.get(0)); | ||
|
||
} | ||
} |