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-10764][SDK] Transform SQL support temporal functions(Includin…
…g year, quarter, month, week, dayofyear and dayofmonth) (apache#10766)
- Loading branch information
Showing
4 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...k/src/main/java/org/apache/inlong/sdk/transform/process/function/DateExtractFunction.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 net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.expression.Function; | ||
|
||
import java.sql.Date; | ||
import java.time.LocalDate; | ||
import java.time.temporal.TemporalField; | ||
import java.time.temporal.WeekFields; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
/** | ||
* DateExtractFunction | ||
* description: | ||
* - year(date)--returns the year from SQL date date | ||
* - quarter(date)--returns the quarter of a year (an integer between 1 and 4) from SQL date date | ||
* - month(date)--returns the month of a year (an integer between 1 and 12) from SQL date date | ||
* - week(date)--returns the week of a year (an integer between 1 and 53) from SQL date date | ||
* - dayofyear(date)--returns the day of a year (an integer between 1 and 366) from SQL date date | ||
* - dayofmonth(date)--returns the day of a month (an integer between 1 and 31) from SQL date date | ||
*/ | ||
public class DateExtractFunction implements ValueParser { | ||
|
||
private DateExtractFunctionType type; | ||
private ValueParser dateParser; | ||
private static final TemporalField weekOfYearField = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear(); | ||
|
||
public enum DateExtractFunctionType { | ||
YEAR, QUARTER, MONTH, WEEK, DAY_OF_YEAR, DAY_OF_MONTH | ||
} | ||
|
||
public DateExtractFunction(DateExtractFunctionType type, Function expr) { | ||
this.type = type; | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
dateParser = OperatorTools.buildParser(expressions.get(0)); | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object dateObj = dateParser.parse(sourceData, rowIndex, context); | ||
Date date = OperatorTools.parseDate(dateObj); | ||
LocalDate localDate = date.toLocalDate(); | ||
switch (type) { | ||
// year | ||
case YEAR: | ||
return localDate.getYear(); | ||
// quarter(between 1 and 4) | ||
case QUARTER: | ||
return (localDate.getMonthValue() - 1) / 3 + 1; | ||
// month(between 1 and 12) | ||
case MONTH: | ||
return localDate.getMonthValue(); | ||
// week(between 1 and 53) | ||
case WEEK: | ||
return localDate.get(weekOfYearField); | ||
// dayofyear(between 1 and 366) | ||
case DAY_OF_YEAR: | ||
return localDate.getDayOfYear(); | ||
// dayofmonth(between 1 and 31) | ||
case DAY_OF_MONTH: | ||
return localDate.getDayOfMonth(); | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
...ransform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/parser/DateParser.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,43 @@ | ||
/* | ||
* 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.parser; | ||
|
||
import org.apache.inlong.sdk.transform.decode.SourceData; | ||
import org.apache.inlong.sdk.transform.process.Context; | ||
|
||
import net.sf.jsqlparser.expression.DateValue; | ||
|
||
import java.sql.Date; | ||
|
||
/** | ||
* DateParser | ||
* description: parse the sql expression to a java.sql.Date object | ||
*/ | ||
public class DateParser implements ValueParser { | ||
|
||
private final Date dateValue; | ||
|
||
public DateParser(DateValue expr) { | ||
this.dateValue = Date.valueOf(expr.getValue().toLocalDate()); | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
return dateValue; | ||
} | ||
} |
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