Skip to content

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

Merged
merged 224 commits into from
Apr 3, 2025
Merged

Conversation

xinyual
Copy link
Contributor

@xinyual xinyual commented Mar 24, 2025

Description

We have several tasks in this PR:

  1. Add datetime functions.
  2. Add IT for these functions
  3. Add approximate percentile functions and IT
  4. Add comparison between time/datetime/date

For this PR,
we miss

  1. Validation for argument

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

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

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.

xinyual and others added 30 commits February 24, 2025 10:42
Signed-off-by: xinyual <[email protected]>
Signed-off-by: xinyual <[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: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
Signed-off-by: Yuanchun Shen <[email protected]>
xinyual added 2 commits April 1, 2025 17:14
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) {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

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;
Copy link
Member

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.

Copy link
Member

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 -> +

Copy link
Contributor Author

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) {
Copy link
Member

Choose a reason for hiding this comment

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

wrapper -> whetherCompareByTime

Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

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

remove this comment

Copy link
Contributor Author

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
Copy link
Member

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;
Copy link
Member

Choose a reason for hiding this comment

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

copyright

Copy link
Contributor Author

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 */
Copy link
Member

Choose a reason for hiding this comment

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

TODO

Copy link
Contributor Author

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
Copy link
Member

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:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 34 to 39
// try {
// return exprDate(transferInputToExprTimestampValue(args[0], (SqlTypeName) args[1], restored))
// .valueForCalcite();
// } catch (SemanticCheckException e) {
// return null;
// }
Copy link
Member

Choose a reason for hiding this comment

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

remove them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 42 to 47
// 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);
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove it

Copy link
Contributor Author

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)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Duplicated

Copy link
Contributor Author

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];
Copy link
Collaborator

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

Copy link
Contributor Author

@xinyual xinyual Apr 1, 2025

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

Copy link
Collaborator

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) {
Copy link
Collaborator

@qianheng-aws qianheng-aws Apr 1, 2025

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?

Copy link
Contributor Author

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]);
Copy link
Collaborator

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.

Copy link
Contributor Author

@xinyual xinyual Apr 1, 2025

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


@Test
public void testYearWeekInvalid() {
assertThrows(
Copy link
Collaborator

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.

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

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

why this test flaky?

Copy link
Contributor Author

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.

LantaoJin
LantaoJin previously approved these changes Apr 2, 2025
yuancu and others added 2 commits April 3, 2025 10:21
Reuse PlanUtils.intervalUnitToSpanUnit
Copy link
Member

@LantaoJin LantaoJin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @xinyual and @yuancu

@LantaoJin LantaoJin merged commit 920f711 into opensearch-project:main Apr 3, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
calcite calcite migration releated
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants