Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.calcite.udf.datetimeUDF;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import org.apache.calcite.sql.type.SqlTypeName;
import org.opensearch.sql.calcite.udf.UserDefinedFunction;
import org.opensearch.sql.calcite.utils.datetime.InstantUtils;
import org.opensearch.sql.data.model.ExprTimestampValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.expression.datetime.DateTimeFunctions;
import org.opensearch.sql.expression.function.FunctionProperties;

/**
* Calculates the difference of date parts of given values. If the first argument is time, today's
* date is used.
*
* <p>(DATE/TIMESTAMP/TIME, DATE/TIMESTAMP/TIME) -> LONG
*/
public class DateDiffFunction implements UserDefinedFunction {
@Override
public Object eval(Object... args) {
SqlTypeName sqlTypeName1 = (SqlTypeName) args[1];
Instant timestamp1 = InstantUtils.convertToInstant(args[0], sqlTypeName1);
SqlTypeName sqlTypeName2 = (SqlTypeName) args[3];
Instant timestamp2 = InstantUtils.convertToInstant(args[2], sqlTypeName2);
LocalDateTime localDateTime1 = LocalDateTime.ofInstant(timestamp1, ZoneOffset.UTC);
LocalDateTime localDateTime2 = LocalDateTime.ofInstant(timestamp2, ZoneOffset.UTC);
ExprValue diffResult =
DateTimeFunctions.exprDateDiff(
new FunctionProperties(),
new ExprTimestampValue(localDateTime1),
new ExprTimestampValue(localDateTime2));
return diffResult.longValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.expression.datetime.DateTimeFunctions;

/**
* DATETIME(timestamp)/ DATETIME(date, to_timezone) Converts the datetime to a new timezone. If not
* specified, the timestamp is regarded to be in system time zone.
*
* <p>(TIMESTAMP, STRING) -> TIMESTAMP <br>
* (TIMESTAMP) -> TIMESTAMP
*
* <p>Converting timestamp with timezone to the second argument timezone.
*/
public class DatetimeFunction implements UserDefinedFunction {
@Override
public Object eval(Object... args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.calcite.udf.datetimeUDF;

import static org.opensearch.sql.expression.function.FunctionDSL.nullMissingHandling;

import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import org.apache.calcite.runtime.SqlFunctions;
import org.opensearch.sql.calcite.udf.UserDefinedFunction;
import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils;
import org.opensearch.sql.data.model.ExprDoubleValue;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.expression.datetime.DateTimeFunctions;

/**
* Returns a date, given year and day-of-year values. dayofyear must be greater than 0 or the result
* is NULL. The result is also NULL if either argument is NULL. Arguments are rounded to an integer.
*
* <p>Limitations: - Zero year interpreted as 2000; - Negative year is not accepted; - day-of-year
* should be greater than zero; - day-of-year could be greater than 365/366, calculation switches to
* the next year(s)
*/
public class MakeDateFunction implements UserDefinedFunction {
@Override
public Object eval(Object... args) {
UserDefinedFunctionUtils.validateArgumentCount("MAKE_DATE", 2, args.length, false);
UserDefinedFunctionUtils.validateArgumentTypes(
Arrays.asList(args),
ImmutableList.of(Number.class, Number.class),
ImmutableList.of(true, true));

ExprDoubleValue v1 = new ExprDoubleValue((Number) args[0]);
ExprDoubleValue v2 = new ExprDoubleValue((Number) args[1]);
ExprValue date = nullMissingHandling(DateTimeFunctions::exprMakeDate).apply(v1, v2);

if (date.isNull()) {
return null;
}
return SqlFunctions.toInt(java.sql.Date.valueOf(date.dateValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Objects;
import org.apache.calcite.sql.type.SqlTypeName;
import org.opensearch.sql.calcite.udf.UserDefinedFunction;
import org.opensearch.sql.calcite.utils.datetime.DateTimeParser;
import org.opensearch.sql.calcite.utils.datetime.InstantUtils;

/**
Expand All @@ -28,7 +29,7 @@ public Object eval(Object... args) {
LocalDate localDate;
if (candiate instanceof String) {
// First transfer it to LocalDate
localDate = LocalDate.parse((String) candiate);
localDate = DateTimeParser.parse(candiate.toString()).toLocalDate();
} else if (argumentType == SqlTypeName.DATE) { // date
localDate =
LocalDate.ofInstant(InstantUtils.fromInternalDate((int) candiate), ZoneOffset.UTC);
Expand All @@ -40,6 +41,7 @@ public Object eval(Object... args) {
throw new IllegalArgumentException("something wrong");
}
String nameType = (String) type;
// TODO: Double-check whether it is ok to always return US week & month names
if (Objects.equals(nameType, "MONTHNAME")) {
return localDate.getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault());
} else if (Objects.equals(nameType, "DAYNAME")) {
Expand Down
Loading
Loading