Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Day_Of_Month Function As An Alias Of DayOfMonth #194

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ public static FunctionExpression dayofyear(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAYOFYEAR, expressions);
}

public static FunctionExpression day_of_month(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_MONTH, expressions);
}

public static FunctionExpression day_of_year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.DAY_OF_YEAR, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.exception.ExpressionEvaluationException;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.function.BuiltinFunctionName;
import org.opensearch.sql.expression.function.BuiltinFunctionRepository;
import org.opensearch.sql.expression.function.DefaultFunctionResolver;
Expand Down Expand Up @@ -100,7 +99,8 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(date_sub());
repository.register(day());
repository.register(dayName());
repository.register(dayOfMonth());
repository.register(dayOfMonth(BuiltinFunctionName.DAYOFMONTH));
repository.register(dayOfMonth(BuiltinFunctionName.DAY_OF_MONTH));
repository.register(dayOfWeek());
repository.register(dayOfYear(BuiltinFunctionName.DAYOFYEAR));
repository.register(dayOfYear(BuiltinFunctionName.DAY_OF_YEAR));
Expand Down Expand Up @@ -336,8 +336,8 @@ private DefaultFunctionResolver dayName() {
/**
* DAYOFMONTH(STRING/DATE/DATETIME/TIMESTAMP). return the day of the month (1-31).
*/
private DefaultFunctionResolver dayOfMonth() {
return define(BuiltinFunctionName.DAYOFMONTH.getName(),
private DefaultFunctionResolver dayOfMonth(BuiltinFunctionName name) {
return define(name.getName(),
impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprDayOfMonth), INTEGER, TIMESTAMP),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public enum BuiltinFunctionName {
DAY(FunctionName.of("day")),
DAYNAME(FunctionName.of("dayname")),
DAYOFMONTH(FunctionName.of("dayofmonth")),
DAY_OF_MONTH(FunctionName.of("day_of_month")),
DAYOFWEEK(FunctionName.of("dayofweek")),
DAYOFYEAR(FunctionName.of("dayofyear")),
DAY_OF_YEAR(FunctionName.of("day_of_year")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,64 @@ public void dayOfMonth() {
assertEquals(integerValue(8), eval(expression));
}

private void testDayOfMonthWithUnderscores(FunctionExpression dateExpression, int dayOfMonth) {
assertEquals(INTEGER, dateExpression.type());
assertEquals(integerValue(dayOfMonth), eval(dateExpression));
}

@Test
public void dayOfMonthWithUnderscores() {
lenient().when(nullRef.valueOf(env)).thenReturn(nullValue());
lenient().when(missingRef.valueOf(env)).thenReturn(missingValue());


FunctionExpression expression1 = DSL.dayofmonth(DSL.literal(new ExprDateValue("2020-08-07")));
FunctionExpression expression2 = DSL.dayofmonth(DSL.literal("2020-07-08"));

assertAll(
() -> testDayOfMonthWithUnderscores(expression1, 7),
() -> assertEquals("dayofmonth(DATE '2020-08-07')", expression1.toString()),

() -> testDayOfMonthWithUnderscores(expression2, 8),
() -> assertEquals("dayofmonth(\"2020-07-08\")", expression2.toString())

);
}

private void testInvalidDayOfMonth(String date) {
FunctionExpression expression = DSL.day_of_month(DSL.literal(new ExprDateValue(date)));
eval(expression);
}

@Test
public void dayOfMonthWithUnderscoresLeapYear() {
lenient().when(nullRef.valueOf(env)).thenReturn(nullValue());
lenient().when(missingRef.valueOf(env)).thenReturn(missingValue());

//Feb. 29 of a leap year
testDayOfMonthWithUnderscores(DSL.day_of_month(DSL.literal("2020-02-29")), 29);

//Feb. 29 of a non-leap year
assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-02-29"));
}

@Test
public void dayOfMonthWithUnderscoresInvalidArguments() {
lenient().when(nullRef.type()).thenReturn(DATE);
lenient().when(missingRef.type()).thenReturn(DATE);
assertEquals(nullValue(), eval(DSL.day_of_month(nullRef)));
assertEquals(missingValue(), eval(DSL.day_of_month(missingRef)));

//40th day of the month
assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-02-40"));

//13th month of the year
assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("2021-13-40"));

//incorrect format
assertThrows(SemanticCheckException.class, () -> testInvalidDayOfMonth("asdfasdfasdf"));
}

@Test
public void dayOfWeek() {
when(nullRef.type()).thenReturn(DATE);
Expand Down
31 changes: 27 additions & 4 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1359,13 +1359,13 @@ DAY
Description
>>>>>>>>>>>

Usage: day(date) extracts the day of the month for date, in the range 1 to 31. The dates with value 0 such as '0000-00-00' or '2008-00-00' are invalid.
Usage: day(date) extracts the day of the month for date, in the range 1 to 31.

Argument type: STRING/DATE/DATETIME/TIMESTAMP

Return type: INTEGER

Synonyms: DAYOFMONTH
Synonyms: `DAYOFMONTH`_, `DAY_OF_MONTH`_

Example::

Expand Down Expand Up @@ -1407,13 +1407,13 @@ DAYOFMONTH
Description
>>>>>>>>>>>

Usage: dayofmonth(date) extracts the day of the month for date, in the range 1 to 31. The dates with value 0 such as '0000-00-00' or '2008-00-00' are invalid.
Usage: dayofmonth(date) extracts the day of the month for date, in the range 1 to 31.

Argument type: STRING/DATE/DATETIME/TIMESTAMP

Return type: INTEGER

Synonyms: DAY
Synonyms: `DAY`_, `DAY_OF_MONTH`_

Example::

Expand All @@ -1425,6 +1425,29 @@ Example::
| 26 |
+----------------------------------+

DAY_OF_MONTH
----------
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

Description
>>>>>>>>>>>

Usage: day_of_month(date) extracts the day of the month for date, in the range 1 to 31.

Argument type: STRING/DATE/DATETIME/TIMESTAMP
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

Return type: INTEGER

Synonyms: `DAY`_, `DAYOFMONTH`_

Example::

os> SELECT DAY_OF_MONTH('2020-08-26')
fetched rows / total rows = 1/1
+------------------------------+
| DAY_OF_MONTH('2020-08-26') |
|------------------------------|
| 26 |
+------------------------------+

DAYOFWEEK
---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,48 @@ public void testDayOfMonth() throws IOException {
verifyDataRows(result, rows(16));
}

@Test
public void testDayOfMonthWithUnderscores() throws IOException {
JSONObject result = executeQuery("select day_of_month(date('2020-09-16'))");
verifySchema(result, schema("day_of_month(date('2020-09-16'))", null, "integer"));
verifyDataRows(result, rows(16));

result = executeQuery("select day_of_month('2020-09-16')");
verifySchema(result, schema("day_of_month('2020-09-16')", null, "integer"));
verifyDataRows(result, rows(16));
}

@Test
public void testDayOfMonthAliasesReturnTheSameResults() throws IOException {
JSONObject result1 = executeQuery("SELECT dayofmonth(date('2022-11-22'))");
JSONObject result2 = executeQuery("SELECT day_of_month(date('2022-11-22'))");
verifyDataRows(result1, rows(22));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));

result1 = executeQuery(String.format(
"SELECT dayofmonth(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS));
result2 = executeQuery(String.format(
"SELECT day_of_month(CAST(date0 AS date)) FROM %s", TEST_INDEX_CALCS));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));

result1 = executeQuery(String.format(
"SELECT dayofmonth(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS));
result2 = executeQuery(String.format(
"SELECT day_of_month(datetime(CAST(time0 AS STRING))) FROM %s", TEST_INDEX_CALCS));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));

result1 = executeQuery(String.format(
"SELECT dayofmonth(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS));
result2 = executeQuery(String.format(
"SELECT day_of_month(CAST(time0 AS STRING)) FROM %s", TEST_INDEX_CALCS));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));

result1 = executeQuery(String.format(
"SELECT dayofmonth(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS));
result2 = executeQuery(String.format(
"SELECT day_of_month(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));
}
@Test
public void testDayOfWeek() throws IOException {
JSONObject result = executeQuery("select dayofweek(date('2020-09-16'))");
Expand Down
1 change: 1 addition & 0 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ dateTimeFunctionName
| DAY
| DAYNAME
| DAYOFMONTH
| DAY_OF_MONTH
| DAYOFWEEK
| DAYOFYEAR
| FROM_DAYS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public void can_parse_week_of_year_functions() {
assertNotNull(parser.parse("SELECT week_of_year('2022-11-18')"));
}

@Test
public void can_parse_dayofmonth_functions() {
assertNotNull(parser.parse("SELECT dayofmonth('2022-11-18')"));
assertNotNull(parser.parse("SELECT day_of_month('2022-11-18')"));
}

@Test
public void can_parse_dayofyear_functions() {
assertNotNull(parser.parse("SELECT dayofyear('2022-11-18')"));
Expand Down