-
Notifications
You must be signed in to change notification settings - Fork 0
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
Update TIME and DATE functions #134
Changes from 16 commits
65c34ac
4a4e425
da22234
e421349
26c1165
be3ea22
4f7edd1
a00863c
99c98b0
f9e38e9
2016222
4c0928d
95c5e5c
61289bb
310f653
484b28c
157059f
77bd46e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ | |
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; | ||
|
@@ -651,7 +652,11 @@ private ExprValue exprConvertTZ(ExprValue startingDateTime, ExprValue fromTz, Ex | |
*/ | ||
private ExprValue exprDate(ExprValue exprValue) { | ||
if (exprValue instanceof ExprStringValue) { | ||
return new ExprDateValue(exprValue.stringValue()); | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we still need it because it can be a stringValue or a dateValue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a case where an ExprStringValue doesn't have a stringValue()? |
||
return new ExprDateValue(exprValue.stringValue()); | ||
} catch (SemanticCheckException e) { | ||
return ExprNullValue.of(); | ||
} | ||
} else { | ||
return new ExprDateValue(exprValue.dateValue()); | ||
} | ||
|
@@ -943,8 +948,15 @@ private ExprValue exprSubDateInterval(ExprValue date, ExprValue expr) { | |
* @return ExprValue. | ||
*/ | ||
private ExprValue exprTime(ExprValue exprValue) { | ||
if (exprValue.type() == DATE) { | ||
return new ExprTimeValue("00:00:00"); | ||
} | ||
if (exprValue instanceof ExprStringValue) { | ||
return new ExprTimeValue(exprValue.stringValue()); | ||
try { | ||
return new ExprTimeValue(exprValue.stringValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use a switch on type() here. For TIME, STRING, DATE, and TIMESTAMP? |
||
} catch (SemanticCheckException e) { | ||
return ExprNullValue.of(); | ||
} | ||
} else { | ||
return new ExprTimeValue(exprValue.timeValue()); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
import static org.opensearch.sql.data.type.ExprCoreType.TIMESTAMP; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -240,6 +241,19 @@ public void date() { | |
assertEquals(DATE, expr.type()); | ||
assertEquals(new ExprDateValue("2020-08-17"), eval(expr)); | ||
assertEquals("date(DATE '2020-08-17')", expr.toString()); | ||
|
||
expr = dsl.date(DSL.literal(new ExprDateValue("2020-08-17 12:12:00"))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should test with nano seconds too |
||
assertEquals(DATE, expr.type()); | ||
assertEquals(new ExprDateValue("2020-08-17 12:12:00"), eval(expr)); | ||
assertEquals("date(DATE '2020-08-17')", expr.toString()); | ||
|
||
expr = dsl.date(DSL.literal(new ExprDateValue("2020-08-17 12:12"))); | ||
assertEquals(DATE, expr.type()); | ||
assertEquals(new ExprDateValue("2020-08-17 12:12"), eval(expr)); | ||
assertEquals("date(DATE '2020-08-17')", expr.toString()); | ||
|
||
expr = dsl.date(DSL.literal("2020-02-30")); | ||
assertEquals(nullValue(), expr.valueOf(null)); | ||
} | ||
|
||
@Test | ||
|
@@ -795,6 +809,33 @@ public void time() { | |
assertEquals(TIME, expr.type()); | ||
assertEquals(new ExprTimeValue("01:01:01"), eval(expr)); | ||
assertEquals("time(TIME '01:01:01')", expr.toString()); | ||
|
||
expr = dsl.time(DSL.literal(new ExprTimeValue("01:01"))); | ||
assertEquals(TIME, expr.type()); | ||
assertEquals(new ExprTimeValue("01:01"), eval(expr)); | ||
assertEquals("time(TIME '01:01:00')", expr.toString()); | ||
|
||
expr = dsl.time(DSL.literal(new ExprTimeValue("2019-04-19 01:01:01"))); | ||
assertEquals(TIME, expr.type()); | ||
assertEquals(new ExprTimeValue("2019-04-19 01:01:01"), eval(expr)); | ||
assertEquals("time(TIME '01:01:01')", expr.toString()); | ||
|
||
expr = dsl.time(DSL.literal(new ExprTimeValue("2019-04-19 01:01"))); | ||
assertEquals(TIME, expr.type()); | ||
assertEquals(new ExprTimeValue("2019-04-19 01:01"), eval(expr)); | ||
assertEquals("time(TIME '01:01:00')", expr.toString()); | ||
|
||
expr = dsl.time(DSL.literal(new ExprTimeValue("01:01:01.0123"))); | ||
assertEquals(TIME, expr.type()); | ||
assertEquals(new ExprTimeValue("01:01:01.0123"), eval(expr)); | ||
assertEquals("time(TIME '01:01:01.0123')", expr.toString()); | ||
|
||
expr = dsl.time(dsl.date(DSL.literal("2020-01-02"))); | ||
assertEquals(TIME, expr.type()); | ||
assertEquals(new ExprTimeValue("00:00:00"), expr.valueOf(null)); | ||
|
||
expr = dsl.time(DSL.literal("01:01:01:01")); | ||
assertEquals(nullValue(), expr.valueOf(null)); | ||
} | ||
|
||
@Test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last minor change - you need to revert this as well ... or ... update test to use
UTC
too.