-
Notifications
You must be signed in to change notification settings - Fork 153
Add datetime functions #3473
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 datetime functions #3473
Conversation
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: Xinyuan Lu <[email protected]>
Map opensearch math functions to calcite implementations
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
@@ -118,6 +121,22 @@ public RexNode visitLiteral(Literal node, CalcitePlanContext context) { | |||
} | |||
} | |||
|
|||
private SpanUnit intervalUnitToSpanUnit(IntervalUnit unit) { |
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.
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.
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.
Already done.
@@ -0,0 +1,25 @@ | |||
package org.opensearch.sql.calcite.udf.datetimeUDF; |
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.
copyright. please check all new added files.
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.
IDE: settings -> Editor -> Copyright -> Copyright Profile -> +
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.
Done
return context.relBuilder.call(op, left, right); | ||
} | ||
|
||
private RexNode transferCompareForDateRelated( | ||
RexNode candidate, CalcitePlanContext context, boolean wrapper) { | ||
if (wrapper) { |
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.
wrapper -> whetherCompareByTime
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.
do we still need this DateToString
call?
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.
Do you mean PostprocessDateToStringFunction?
@@ -320,11 +363,20 @@ public RexNode visitLet(Let node, CalcitePlanContext context) { | |||
|
|||
@Override | |||
public RexNode visitFunction(Function node, CalcitePlanContext context) { | |||
// Re-org the code structure |
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.
remove this comment
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.
Done
@@ -5,6 +5,9 @@ | |||
|
|||
package org.opensearch.sql.calcite.udf; | |||
|
|||
/** | |||
* TO DO. support init with constant arguments https://github.com/opensearch-project/sql/issues/3490 |
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.
TODO, not TO DO
@@ -0,0 +1,37 @@ | |||
package org.opensearch.sql.calcite.udf.datetimeUDF; |
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.
copyright
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.
Done
@@ -35,10 +97,15 @@ | |||
import org.opensearch.sql.calcite.udf.textUDF.LocateFunction; | |||
import org.opensearch.sql.calcite.udf.textUDF.ReplaceFunction; | |||
|
|||
/** TO DO: We need to refactor code to make all */ |
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.
TODO
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.
Done
@@ -204,6 +331,82 @@ static SqlOperator translate(String op) { | |||
return TransferUserDefinedFunction( | |||
TypeOfFunction.class, "typeof", ReturnTypes.VARCHAR_2000_NULLABLE); | |||
// TODO Add more, ref RexImpTable |
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.
move this line before default:
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.
Done
// try { | ||
// return exprDate(transferInputToExprTimestampValue(args[0], (SqlTypeName) args[1], restored)) | ||
// .valueForCalcite(); | ||
// } catch (SemanticCheckException e) { | ||
// return null; | ||
// } |
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.
remove them
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.
Done
// Instant base = InstantUtils.convertToInstant(argBase, baseType, false); | ||
// Instant interval = InstantUtils.convertToInstant(argInterval, argIntervalType, false); | ||
// LocalTime time = interval.atZone(ZoneOffset.UTC).toLocalTime(); | ||
// Duration duration = Duration.between(LocalTime.MIN, time); | ||
|
||
// Instant newInstant = DateTimeApplyUtils.applyInterval(base, duration, isAdd); |
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.
ditto
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.
Done
boolean isAdd = (Boolean) args[4]; | ||
SqlTypeName returnSqlType = (SqlTypeName) args[5]; | ||
ExprValue base = transferInputToExprValue(argBase, sqlTypeName); | ||
// Instant base = InstantUtils.convertToInstant(argBase, sqlTypeName, false); |
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.
Remove it
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.
Done
return null; | ||
} | ||
|
||
if (UserDefinedFunctionUtils.containsNull(args)) { |
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.
Duplicated
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.
Already remove it.
TimeUnit unit = (TimeUnit) args[0]; | ||
long interval = ((Number) args[1]).longValue(); | ||
Object argBase = args[2]; | ||
SqlTypeName sqlTypeName = (SqlTypeName) args[3]; |
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.
Why using SqlTypeName here? I think sql type is a subset of all types we use and date, time, timestamp of Sql type is no longer used by us after introducing UDT
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.
For our current UDF, all time/timestamp/time input would be the UDT. So we directly transfer the type to corresponding sqltypetime that we can call fromObjectValue
easily. See function transferDateRelatedTimeName
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.
These UDT's real SqlType is VARCHAR, the more appropriate way is pass in the RelDataType here and get the correct exprtype by using ExprUDT.getExprCoreType
.
It works here because we have a incorrect mapping in convertSqlTypeNameToExprType
(the mapping of time,timestamp,date will never be used since they are blocked now by the previous logic of handling UDT), and you pass in SqlTypeName.TIMESTAMP
directly from the place where you call this funciton.
return opBinding -> { | ||
RelDataType operandType0 = opBinding.getOperandType(6); | ||
SqlTypeName typeName = operandType0.getSqlTypeName(); | ||
if (typeName == SqlTypeName.TIMESTAMP) { |
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.
In which case will we use SqlTypeName.TIMESTAMP?
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.
Same as above.
SqlTypeName returnSqlType = (SqlTypeName) args[5]; | ||
ExprValue base = transferInputToExprValue(argBase, sqlTypeName); | ||
// Instant base = InstantUtils.convertToInstant(argBase, sqlTypeName, false); | ||
FunctionProperties restored = restoreFunctionProperties(args[args.length - 1]); |
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.
This properties should be set when constructing the function and I think it supports pass in FunctionProperties directly, please add a TODO here to make change in the future.
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.
Got it. Add in function restoreFunctionProperties
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
|
||
@Test | ||
public void testYearWeekInvalid() { | ||
assertThrows( |
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.
Please add assert for error message as well.
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.
Already add.
@@ -949,6 +949,7 @@ public void testMakeDateWithNullIO() { | |||
* date value given the day number N. DATETIME: (TIMESTAMP, STRING) -> TIMESTAMP (TIMESTAMP) -> | |||
* TIMESTAMP Converts the datetime to a new timezone | |||
*/ | |||
@Ignore |
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.
why this test flaky?
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.
Already fix the bug and enable IT.
Signed-off-by: xinyual <[email protected]>
…cal digits) Signed-off-by: Yuanchun Shen <[email protected]>
Fix date / time formatting
Signed-off-by: xinyual <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Reuse PlanUtils.intervalUnitToSpanUnit
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[email protected]>
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.
Description
We have several tasks in this PR:
For this PR,
we miss
Related Issues
Resolves #[Issue number to be closed when this PR is merged]
#3400
I have tested the nonFallbackITs in the #3400 locally. There are some issues:
NonFallbackCalciteNowLikeFunctionIT: pass all
NonFallbackCalciteDateTimeFunctionIT: pass all
NonFallbackCalciteDateTimeImplementationIT: pass
NonFallbackCalciteDateTimeComparisonIT: pass all
NonFallbackCalciteConvertTZFunctionIT: pass
Check List
--signoff
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.