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 Hour_Of_Day Function As An Alias Of Hour #195

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -346,6 +346,10 @@ public static FunctionExpression hour(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.HOUR, expressions);
}

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

public static FunctionExpression microsecond(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MICROSECOND, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(dayOfYear(BuiltinFunctionName.DAY_OF_YEAR));
repository.register(from_days());
repository.register(from_unixtime());
repository.register(hour());
repository.register(hour(BuiltinFunctionName.HOUR));
repository.register(hour(BuiltinFunctionName.HOUR_OF_DAY));
repository.register(localtime());
repository.register(localtimestamp());
repository.register(makedate());
Expand Down Expand Up @@ -389,8 +390,8 @@ private FunctionResolver from_unixtime() {
/**
* HOUR(STRING/TIME/DATETIME/TIMESTAMP). return the hour value for time.
*/
private DefaultFunctionResolver hour() {
return define(BuiltinFunctionName.HOUR.getName(),
private DefaultFunctionResolver hour(BuiltinFunctionName name) {
return define(name.getName(),
impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, STRING),
impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, TIME),
impl(nullMissingHandling(DateTimeFunction::exprHour), INTEGER, DATETIME),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public enum BuiltinFunctionName {
FROM_DAYS(FunctionName.of("from_days")),
FROM_UNIXTIME(FunctionName.of("from_unixtime")),
HOUR(FunctionName.of("hour")),
HOUR_OF_DAY(FunctionName.of("hour_of_day")),
MAKEDATE(FunctionName.of("makedate")),
MAKETIME(FunctionName.of("maketime")),
MICROSECOND(FunctionName.of("microsecond")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,67 @@ public void hour() {
assertEquals("hour(\"2020-08-17 01:02:03\")", expression.toString());
}

public void testHourOfDay(FunctionExpression dateExpression, int hour) {
margarit-h marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(INTEGER, dateExpression.type());
assertEquals(integerValue(hour), eval(dateExpression));
}

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

FunctionExpression expression1 = DSL.hour_of_day(DSL.literal(new ExprTimeValue("01:02:03")));
FunctionExpression expression2 = DSL.hour_of_day(DSL.literal("01:02:03"));
FunctionExpression expression3 = DSL.hour_of_day(
DSL.literal(new ExprTimestampValue("2020-08-17 01:02:03")));
FunctionExpression expression4 = DSL.hour_of_day(
DSL.literal(new ExprDatetimeValue("2020-08-17 01:02:03")));
FunctionExpression expression5 = DSL.hour_of_day(DSL.literal("2020-08-17 01:02:03"));

assertAll(
() -> testHourOfDay(expression1, 1),
() -> assertEquals("hour_of_day(TIME '01:02:03')", expression1.toString()),

() -> testHourOfDay(expression2, 1),
() -> assertEquals("hour_of_day(\"01:02:03\")", expression2.toString()),

() -> testHourOfDay(expression3, 1),
() -> assertEquals("hour_of_day(TIMESTAMP '2020-08-17 01:02:03')", expression3.toString()),

() -> testHourOfDay(expression4, 1),
() -> assertEquals("hour_of_day(DATETIME '2020-08-17 01:02:03')", expression4.toString()),

() -> testHourOfDay(expression5, 1),
() -> assertEquals("hour_of_day(\"2020-08-17 01:02:03\")", expression5.toString())
);
}

public void testInvalidHourOfDay(String time) {
FunctionExpression expression = DSL.hour_of_day(DSL.literal(new ExprTimeValue(time)));
eval(expression);
}

@Test
public void hourOfDayInvalidArguments() {
when(nullRef.type()).thenReturn(TIME);
when(missingRef.type()).thenReturn(TIME);
assertEquals(nullValue(), eval(DSL.hour(nullRef)));
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(missingValue(), eval(DSL.hour(missingRef)));

//Invalid Seconds
assertThrows(SemanticCheckException.class, () -> testInvalidHourOfDay("12:23:61"));

//Invalid Minutes
assertThrows(SemanticCheckException.class, () -> testInvalidHourOfDay("12:61:34"));

//Invalid Hours
assertThrows(SemanticCheckException.class, () -> testInvalidHourOfDay("25:23:34"));

//incorrect format
assertThrows(SemanticCheckException.class, () -> testInvalidHourOfDay("asdfasdf"));
}

@Test
public void microsecond() {
when(nullRef.type()).thenReturn(TIME);
Expand Down
58 changes: 58 additions & 0 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1597,13 +1597,22 @@ Description
>>>>>>>>>>>

Usage: hour(time) extracts the hour value for time. Different from the time of day value, the time value has a large range and can be greater than 23, so the return value of hour(time) can be also greater than 23.
The function `hour_of_day` is also provided as an alias.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need an underscore to use it as a link?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we do, but it does not have its own entry in the doc so I don't believe it would be linking to anything. Should I just remove the backticks altogether?


Argument type: STRING/TIME/DATETIME/TIMESTAMP

Return type: INTEGER

Example::

os> SELECT HOUR('01:02:03')
fetched rows / total rows = 1/1
+--------------------+
| HOUR('01:02:03') |
|--------------------|
| 1 |
+--------------------+

os> SELECT HOUR((TIME '01:02:03'))
fetched rows / total rows = 1/1
+---------------------------+
Expand All @@ -1612,6 +1621,55 @@ Example::
| 1 |
+---------------------------+

os> SELECT HOUR((TIMESTAMP '2022-12-19 01:02:03'))
fetched rows / total rows = 1/1
+-------------------------------------------+
| HOUR((TIMESTAMP '2022-12-19 01:02:03')) |
|-------------------------------------------|
| 1 |
+-------------------------------------------+

os> SELECT HOUR(datetime('2022-12-19 01:02:03'))
fetched rows / total rows = 1/1
+-----------------------------------------+
| HOUR(datetime('2022-12-19 01:02:03')) |
|-----------------------------------------|
| 1 |
+-----------------------------------------+

os> SELECT HOUR_OF_DAY('01:02:03')
fetched rows / total rows = 1/1
+---------------------------+
| HOUR_OF_DAY('01:02:03') |
|---------------------------|
| 1 |
+---------------------------+

os> SELECT HOUR_OF_DAY((TIME '01:02:03'))
fetched rows / total rows = 1/1
+----------------------------------+
| HOUR_OF_DAY((TIME '01:02:03')) |
|----------------------------------|
| 1 |
+----------------------------------+

os> SELECT HOUR_OF_DAY((TIMESTAMP '2022-12-19 01:02:03'))
fetched rows / total rows = 1/1
+--------------------------------------------------+
| HOUR_OF_DAY((TIMESTAMP '2022-12-19 01:02:03')) |
|--------------------------------------------------|
| 1 |
+--------------------------------------------------+

os> SELECT HOUR_OF_DAY(datetime('2022-12-19 01:02:03'))
fetched rows / total rows = 1/1
+------------------------------------------------+
| HOUR_OF_DAY(datetime('2022-12-19 01:02:03')) |
|------------------------------------------------|
| 1 |
+------------------------------------------------+



LOCALTIMESTAMP
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,63 @@ public void testHour() throws IOException {
verifyDataRows(result, rows(17));
}

@Test
public void testHourOfDayWithUnderscores() throws IOException {
JSONObject result = executeQuery("select hour_of_day(timestamp('2020-09-16 17:30:00'))");
verifySchema(result, schema(
"hour_of_day(timestamp('2020-09-16 17:30:00'))", null, "integer"));
verifyDataRows(result, rows(17));

result = executeQuery("select hour_of_day(datetime('2020-09-16 17:30:00'))");
verifySchema(result, schema(
"hour_of_day(datetime('2020-09-16 17:30:00'))", null, "integer"));
verifyDataRows(result, rows(17));

result = executeQuery("select hour_of_day(time('17:30:00'))");
verifySchema(result, schema("hour_of_day(time('17:30:00'))", null, "integer"));
verifyDataRows(result, rows(17));

result = executeQuery("select hour_of_day('2020-09-16 17:30:00')");
verifySchema(result, schema("hour_of_day('2020-09-16 17:30:00')", null, "integer"));
verifyDataRows(result, rows(17));

result = executeQuery("select hour_of_day('17:30:00')");
verifySchema(result, schema("hour_of_day('17:30:00')", null, "integer"));
verifyDataRows(result, rows(17));
}

@Test
public void testHourFunctionAliasesReturnTheSameResults() throws IOException {
JSONObject result1 = executeQuery("SELECT hour('11:30:00')");
JSONObject result2 = executeQuery("SELECT hour_of_day(date('11:30:00')");
verifyDataRows(result1, rows(11));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));

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

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

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

result1 = executeQuery(String.format(
"SELECT hour(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS));
result2 = executeQuery(String.format(
"SELECT hour_of_day(CAST(datetime0 AS timestamp)) FROM %s", TEST_INDEX_CALCS));
result1.getJSONArray("datarows").similar(result2.getJSONArray("datarows"));
}

@Test
public void testMicrosecond() throws IOException {
JSONObject result = executeQuery("select microsecond(timestamp('2020-09-16 17:30:00.123456'))");
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 @@ -245,6 +245,7 @@ datetimeConstantLiteral
| CURRENT_TIME
| CURRENT_TIMESTAMP
| DAY_OF_YEAR
| HOUR_OF_DAY

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datetimeConstantLiteral is a function which could be called without () too, e.g.

select HOUR_OF_DAY, HOUR_OF_DAY();

HOUR_OF_DAY should be in this list. DAY_OF_YEAR, WEEK_OF_YEAR and MONTH_OF_YEAR too, actually. See dateTimeFunctionName.

Copy link
Author

@GabeFernandez310 GabeFernandez310 Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HOUR_OF_DAY should be in this list. DAY_OF_YEAR, WEEK_OF_YEAR and MONTH_OF_YEAR too, actually. See dateTimeFunctionName.

They are currently in this list. Did you mean to say that they shouldn't be in this list?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, sorry. My mistake, but you understand everything correct.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Thanks Yury! Have a good vacation!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4255ec9

| LOCALTIME
| LOCALTIMESTAMP
| MONTH_OF_YEAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ public void can_parse_now_like_functions(String name, Boolean hasFsp, Boolean ha
assertNotNull(parser.parse("SELECT id FROM test WHERE " + String.join(" AND ", calls)));
}

@Test
public void can_parse_hour_functions() {
assertNotNull(parser.parse("SELECT hour('2022-11-18 12:23:34')"));
assertNotNull(parser.parse("SELECT hour_of_day('12:23:34')"));
}

@Test
public void can_parse_week_of_year_functions() {
Expand Down