Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -354,6 +354,10 @@ public static FunctionExpression minute(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MINUTE, expressions);
}

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

public static FunctionExpression month(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.MONTH, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package org.opensearch.sql.expression.datetime;

import static java.time.temporal.ChronoUnit.MINUTES;
import static java.time.temporal.ChronoUnit.MONTHS;
import static org.opensearch.sql.data.type.ExprCoreType.DATE;
import static org.opensearch.sql.data.type.ExprCoreType.DATETIME;
Expand Down Expand Up @@ -112,7 +113,8 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(makedate());
repository.register(maketime());
repository.register(microsecond());
repository.register(minute());
repository.register(minute(BuiltinFunctionName.MINUTE));
repository.register(minute(BuiltinFunctionName.MINUTE_OF_HOUR));
repository.register(month(BuiltinFunctionName.MONTH));
repository.register(month(BuiltinFunctionName.MONTH_OF_YEAR));
repository.register(monthName());
Expand Down Expand Up @@ -423,11 +425,12 @@ private DefaultFunctionResolver microsecond() {
/**
* MINUTE(STRING/TIME/DATETIME/TIMESTAMP). return the minute value for time.
*/
private DefaultFunctionResolver minute() {
return define(BuiltinFunctionName.MINUTE.getName(),
private DefaultFunctionResolver minute(BuiltinFunctionName name) {
return define(name.getName(),
impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, STRING),
impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, TIME),
impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprMinute), INTEGER, TIMESTAMP)
);
}
Expand Down Expand Up @@ -899,7 +902,8 @@ private ExprValue exprMicrosecond(ExprValue time) {
* @return ExprValue.
*/
private ExprValue exprMinute(ExprValue time) {
return new ExprIntegerValue(time.timeValue().getMinute());
return new ExprIntegerValue(
(MINUTES.between(LocalTime.MIN, time.timeValue()) % 60));
Comment thread
Yury-Fridlyand marked this conversation as resolved.
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public enum BuiltinFunctionName {
MAKETIME(FunctionName.of("maketime")),
MICROSECOND(FunctionName.of("microsecond")),
MINUTE(FunctionName.of("minute")),
MINUTE_OF_HOUR(FunctionName.of("minute_of_hour")),
MONTH(FunctionName.of("month")),
MONTH_OF_YEAR(FunctionName.of("month_of_year")),
MONTHNAME(FunctionName.of("monthname")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,62 @@ public void minute() {
assertEquals("minute(\"2020-08-17 01:02:03\")", expression.toString());
}

private void testMinuteOfHour(FunctionExpression dateExpression, int minute, String testExpr) {
assertEquals(INTEGER, dateExpression.type());
Comment thread
Yury-Fridlyand marked this conversation as resolved.
Outdated
assertEquals(integerValue(minute), eval(dateExpression));
assertEquals(testExpr, dateExpression.toString());
}

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

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


assertAll(
() -> testMinuteOfHour(expression1, 2, "minute_of_hour(TIME '01:02:03')"),
() -> testMinuteOfHour(expression2, 2, "minute_of_hour(\"01:02:03\")"),
() -> testMinuteOfHour(expression3, 2, "minute_of_hour(TIMESTAMP '2020-08-17 01:02:03')"),
() -> testMinuteOfHour(expression4, 2, "minute_of_hour(DATETIME '2020-08-17 01:02:03')"),
() -> testMinuteOfHour(expression5, 2, "minute_of_hour(\"2020-08-17 01:02:03\")")
);
}

private void testInvalidMinuteOfHour(String time) {
FunctionExpression expression = DSL.minute_of_hour(DSL.literal(new ExprTimeValue(time)));
eval(expression);
}

@Test
public void minuteOfHourInvalidArguments() {
when(nullRef.type()).thenReturn(TIME);
when(missingRef.type()).thenReturn(TIME);
assertEquals(nullValue(), eval(DSL.minute_of_hour(nullRef)));
Comment thread
Yury-Fridlyand marked this conversation as resolved.
Outdated
assertEquals(missingValue(), eval(DSL.minute_of_hour(missingRef)));

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

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

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

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


@Test
public void month() {
when(nullRef.type()).thenReturn(DATE);
Expand Down
14 changes: 7 additions & 7 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1747,21 +1747,21 @@ Description
>>>>>>>>>>>

Usage: minute(time) returns the minute for time, in the range 0 to 59.
The `minute_of_hour` function is provided as an alias.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

underscore for a link

@Yury-Fridlyand Yury-Fridlyand Jan 3, 2023

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We have no section for minute_of_hour to link to


Argument type: STRING/TIME/DATETIME/TIMESTAMP

Return type: INTEGER

Example::

os> SELECT MINUTE((TIME '01:02:03'))
os> SELECT MINUTE((TIME '01:02:03')), MINUTE_OF_HOUR((TIME '01:02:03'))
Comment thread
Yury-Fridlyand marked this conversation as resolved.
Outdated
fetched rows / total rows = 1/1
+-----------------------------+
| MINUTE((TIME '01:02:03')) |
|-----------------------------|
| 2 |
+-----------------------------+

+-----------------------------+-------------------------------------+
| MINUTE((TIME '01:02:03')) | MINUTE_OF_HOUR((TIME '01:02:03')) |
|-----------------------------+-------------------------------------|
| 2 | 2 |
+-----------------------------+-------------------------------------+

MONTH
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,52 @@ public void testMinute() throws IOException {
verifyDataRows(result, rows(30));
}

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

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

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

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

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

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

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

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

@Test
public void testMonth() throws IOException {
JSONObject result = executeQuery("select month(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 @@ -435,6 +435,7 @@ dateTimeFunctionName
| MAKETIME
| MICROSECOND
| MINUTE
| MINUTE_OF_HOUR
| MONTH
| MONTHNAME
| NOW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ public void can_parse_dayofyear_functions() {
assertNotNull(parser.parse("SELECT day_of_year('2022-11-18')"));
}

@Test
public void can_parse_minute_functions() {
assertNotNull(parser.parse("SELECT minute('12:23:34')"));
assertNotNull(parser.parse("SELECT minute_of_hour('12:23:34')"));

assertNotNull(parser.parse("SELECT minute('2022-12-20 12:23:34')"));
assertNotNull(parser.parse("SELECT minute_of_hour('2022-12-20 12:23:34')"));
}

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