Skip to content

Commit

Permalink
[INLONG-10935][SDK] Transform support DAYNAME function (#10937)
Browse files Browse the repository at this point in the history
* [INLONG-10935][SDK] Transform support DAYNAME function

* fix the style

* fix the failed UT
  • Loading branch information
ying-hua authored Sep 2, 2024
1 parent 9f9acbd commit 6234b9e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* - 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
* - dayname(date)--returns the name of the day of the week from SQL date
*/
public abstract class DateExtractFunction implements ValueParser {

Expand All @@ -50,7 +51,7 @@ public abstract 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, DAY_OF_WEEK
YEAR, QUARTER, MONTH, WEEK, DAY_OF_YEAR, DAY_OF_MONTH, DAY_OF_WEEK, DAY_NAME
}

@TransformFunction(names = {"year"})
Expand Down Expand Up @@ -109,6 +110,14 @@ public DayOfWeekExtractFunction(Function expr) {
}
}

@TransformFunction(names = {"day_name", "dayname"})
public static class DayNameExtractFunction extends DateExtractFunction {

public DayNameExtractFunction(Function expr) {
super(DateExtractFunctionType.DAY_NAME, expr);
}
}

public DateExtractFunction(DateExtractFunctionType type, Function expr) {
this.type = type;
List<Expression> expressions = expr.getParameters().getExpressions();
Expand Down Expand Up @@ -142,6 +151,9 @@ public Object parse(SourceData sourceData, int rowIndex, Context context) {
// dayofweek(between 1 and 7)
case DAY_OF_WEEK:
return localDate.getDayOfWeek().getValue() % 7 + 1;
// dayname(between Sunday and Saturday)
case DAY_NAME:
return localDate.getDayOfWeek().name();
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public class OperatorTools {
public static final String ROOT_KEY = "$root";

public static final String CHILD_KEY = "$child";

public static ExpressionOperator buildOperator(Expression expr) {
if (expr instanceof AndExpression) {
return new AndOperator((AndExpression) expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ public void testDateExtractFunction() throws Exception {
List<String> output7 = processor7.transform("2024-02-29", new HashMap<>());
Assert.assertEquals(1, output7.size());
Assert.assertEquals(output7.get(0), "result=5");

String transformSql8 = "select dayname(string1) from source";
TransformConfig config8 = new TransformConfig(transformSql8);
TransformProcessor<String, String> processor8 = TransformProcessor
.create(config8, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case8: dayname(2024-02-29) (Thursday)
List<String> output8 = processor8.transform("2024-02-29", new HashMap<>());
Assert.assertEquals(1, output8.size());
Assert.assertEquals(output8.get(0), "result=THURSDAY");
}

@Test
Expand Down

0 comments on commit 6234b9e

Please sign in to comment.