Skip to content

Commit

Permalink
[INLONG-10930][SDK] Transform support DAYOFWEEK function (#10933)
Browse files Browse the repository at this point in the history
  • Loading branch information
ying-hua authored Aug 28, 2024
1 parent eca3e64 commit 2409da5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* - week(date)--returns the week of a year (an integer between 1 and 53) from SQL date
* - dayofyear(date)--returns the day of a year (an integer between 1 and 366) from SQL date
* - dayofmonth(date)--returns the day of a month (an integer between 1 and 31) from SQL date
* - dayofweek(date)--returns the day of a week (an integer between 1(Sunday) and 7(Saturday)) from SQL date
*/
public class DateExtractFunction implements ValueParser {

Expand All @@ -49,7 +50,7 @@ public class DateExtractFunction implements ValueParser {
private static final TemporalField weekOfYearField = WeekFields.of(Locale.getDefault()).weekOfWeekBasedYear();

public enum DateExtractFunctionType {
YEAR, QUARTER, MONTH, WEEK, DAY_OF_YEAR, DAY_OF_MONTH
YEAR, QUARTER, MONTH, WEEK, DAY_OF_YEAR, DAY_OF_MONTH, DAY_OF_WEEK
}

public DateExtractFunction(DateExtractFunctionType type, Function expr) {
Expand Down Expand Up @@ -82,6 +83,9 @@ public Object parse(SourceData sourceData, int rowIndex, Context context) {
// dayofmonth(between 1 and 31)
case DAY_OF_MONTH:
return localDate.getDayOfMonth();
// dayofweek(between 1 and 7)
case DAY_OF_WEEK:
return localDate.getDayOfWeek().getValue() % 7 + 1;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public class OperatorTools {
functionMap.put("week", func -> new DateExtractFunction(DateExtractFunctionType.WEEK, func));
functionMap.put("dayofyear", func -> new DateExtractFunction(DateExtractFunctionType.DAY_OF_YEAR, func));
functionMap.put("dayofmonth", func -> new DateExtractFunction(DateExtractFunctionType.DAY_OF_MONTH, func));
functionMap.put("dayofweek", func -> new DateExtractFunction(DateExtractFunctionType.DAY_OF_WEEK, func));
functionMap.put("hour",
func -> new TimestampExtractFunction(TimestampExtractFunction.TimestampExtractFunctionType.HOUR, func));
functionMap.put("minute",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ public void testDateExtractFunction() throws Exception {
List<String> output6 = processor6.transform("2024-02-29", new HashMap<>());
Assert.assertEquals(1, output6.size());
Assert.assertEquals(output6.get(0), "result=29");

String transformSql7 = "select dayofweek(string1) from source";
TransformConfig config7 = new TransformConfig(transformSql7);
TransformProcessor<String, String> processor7 = TransformProcessor
.create(config7, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case7: dayofweek(2024-02-29) (Thursday)
List<String> output7 = processor7.transform("2024-02-29", new HashMap<>());
Assert.assertEquals(1, output7.size());
Assert.assertEquals(output7.get(0), "result=5");
}

@Test
Expand Down

0 comments on commit 2409da5

Please sign in to comment.