forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11080][SDK] Transform SQL supports DATE_SUB function (apache#…
…11193) Co-authored-by: ZKpLo <[email protected]>
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
...m-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/DateSubFunction.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,72 @@ | ||
/* | ||
* 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 org.apache.commons.lang3.tuple.Pair; | ||
|
||
import java.time.temporal.ChronoField; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* DateAddFunction | ||
* Description: | ||
* - return NULL if date is NULL. | ||
* - return DATE if the date argument is a DATE value and your calculations involve only YEAR, MONTH, and DAY parts | ||
* (that is, no time parts). | ||
* - return TIME if the date argument is a TIME value and the calculations involve only HOURS, MINUTES, | ||
* and SECONDS parts (that is, no date parts). | ||
* - return DATETIME if the first argument is a DATETIME (or TIMESTAMP) value, or if the first argument is a DATE | ||
* and the unit value uses HOURS, MINUTES, or SECONDS, or if the first argument is of type TIME and the | ||
* unit value uses YEAR, MONTH, or DAY. | ||
* - return If the first argument is a dynamic parameter (for example, of a prepared statement), its resolved type | ||
* is DATE if the second argument is an interval that contains some combination of YEAR, MONTH, or DAY values | ||
* only; otherwise, its type is DATETIME. | ||
* - return String otherwise (type VARCHAR). | ||
*/ | ||
@TransformFunction(names = {"date_sub", "datesub"}) | ||
public class DateSubFunction implements ValueParser { | ||
|
||
private ValueParser datetimeParser; | ||
private ValueParser intervalParser; | ||
|
||
public DateSubFunction(Function expr) { | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
datetimeParser = OperatorTools.buildParser(expressions.get(0)); | ||
intervalParser = OperatorTools.buildParser(expressions.get(1)); | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object intervalPairObj = intervalParser.parse(sourceData, rowIndex, context); | ||
Object dateObj = datetimeParser.parse(sourceData, rowIndex, context); | ||
if (intervalPairObj == null || dateObj == null) { | ||
return null; | ||
} | ||
return DateUtil.dateAdd(OperatorTools.parseString(dateObj), | ||
(Pair<Integer, Map<ChronoField, Long>>) intervalPairObj, -1); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...t/java/org/apache/inlong/sdk/transform/process/function/temporal/TestDateSubFunction.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,76 @@ | ||
/* | ||
* 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 TestDateSubFunction extends AbstractFunctionTemporalTestBase { | ||
|
||
@Test | ||
public void testDateSubFunction() throws Exception { | ||
String transformSql = null; | ||
TransformConfig config = null; | ||
TransformProcessor<String, String> processor = null; | ||
List<String> output = null; | ||
|
||
transformSql = "select DATE_SUB(string1, INTERVAL string2 DAY) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case1: DATE_SUB('2020-12-31 23:59:59',INTERVAL 999 DAY) | ||
output = processor.transform("2020-12-31 23:59:59|999", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2018-04-07 23:59:59", output.get(0)); | ||
|
||
// case2: DATE_SUB('2020-12-31 23:59:59',INTERVAL -999 DAY) | ||
output = processor.transform("2020-12-31 23:59:59|-999", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2023-09-26 23:59:59", output.get(0)); | ||
|
||
transformSql = "select DATE_SUB(string1, INTERVAL string2 MINUTE_SECOND) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case3: DATE_SUB('2020-12-31',INTERVAL '-1:1' MINUTE_SECOND) | ||
output = processor.transform("2020-12-31|-1:1", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=2020-12-31 00:01:01", output.get(0)); | ||
|
||
transformSql = "select DATE_SUB(string1, INTERVAL string2 SECOND_MICROSECOND) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case4: DATE_SUB('1992-12-31 23:59:59', INTERVAL '-1.999999' SECOND_MICROSECOND) | ||
output = processor.transform("1992-12-31 23:59:59|-1.999999", new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result=1993-01-01 00:00:00.999999", output.get(0)); | ||
|
||
} | ||
} |