Skip to content

Commit

Permalink
Merge branch 'main' into integ-arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewryanwells authored Mar 15, 2023
2 parents 462a4ee + 4f9f5ca commit ebbeb25
Show file tree
Hide file tree
Showing 20 changed files with 957 additions and 181 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies {
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
events "skipped", "failed"
exceptionFormat "full"
}
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/org/opensearch/sql/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ public LogicalPlan visitTableFunction(TableFunction node, AnalysisContext contex
.collect(Collectors.toList());
TableFunctionImplementation tableFunctionImplementation
= (TableFunctionImplementation) repository.compile(context.getFunctionProperties(),
dataSourceSchemaIdentifierNameResolver.getDataSourceName(), functionName, arguments);
dataSourceService
.getDataSource(dataSourceSchemaIdentifierNameResolver.getDataSourceName())
.getStorageEngine().getFunctions(), functionName, arguments);
context.push();
TypeEnvironment curEnv = context.peek();
Table table = tableFunctionImplementation.applyArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
public enum DataSourceType {
PROMETHEUS,
OPENSEARCH,
FILESYSTEM
JDBC
}
29 changes: 29 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 @@ -250,6 +250,10 @@ public static FunctionExpression rand(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.RAND, expressions);
}

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

public static FunctionExpression round(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.ROUND, expressions);
}
Expand All @@ -258,6 +262,10 @@ public static FunctionExpression sign(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SIGN, expressions);
}

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

public static FunctionExpression sqrt(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SQRT, expressions);
}
Expand Down Expand Up @@ -294,6 +302,10 @@ public static FunctionExpression cos(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.COS, expressions);
}

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

public static FunctionExpression cot(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.COT, expressions);
}
Expand Down Expand Up @@ -467,6 +479,11 @@ public static FunctionExpression week(
return compile(functionProperties, BuiltinFunctionName.WEEK, expressions);
}

public static FunctionExpression weekday(FunctionProperties functionProperties,
Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEKDAY, expressions);
}

public static FunctionExpression weekofyear(
FunctionProperties functionProperties, Expression... expressions) {
return compile(functionProperties, BuiltinFunctionName.WEEKOFYEAR, expressions);
Expand All @@ -481,6 +498,18 @@ public static FunctionExpression year(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.YEAR, expressions);
}

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

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

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

public static FunctionExpression substr(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SUBSTR, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.opensearch.sql.data.type.ExprCoreType.DATE;
import static org.opensearch.sql.data.type.ExprCoreType.DATETIME;
import static org.opensearch.sql.data.type.ExprCoreType.DOUBLE;
import static org.opensearch.sql.data.type.ExprCoreType.FLOAT;
import static org.opensearch.sql.data.type.ExprCoreType.INTEGER;
import static org.opensearch.sql.data.type.ExprCoreType.INTERVAL;
import static org.opensearch.sql.data.type.ExprCoreType.LONG;
Expand Down Expand Up @@ -59,6 +60,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.opensearch.sql.data.model.ExprDateValue;
import org.opensearch.sql.data.model.ExprDatetimeValue;
Expand Down Expand Up @@ -176,6 +178,7 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(period_add());
repository.register(period_diff());
repository.register(quarter());
repository.register(sec_to_time());
repository.register(second(BuiltinFunctionName.SECOND));
repository.register(second(BuiltinFunctionName.SECOND_OF_MINUTE));
repository.register(subdate());
Expand All @@ -195,6 +198,7 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(week(BuiltinFunctionName.WEEK));
repository.register(week(BuiltinFunctionName.WEEKOFYEAR));
repository.register(week(BuiltinFunctionName.WEEK_OF_YEAR));
repository.register(weekday());
repository.register(year());
}

Expand Down Expand Up @@ -688,6 +692,15 @@ private DefaultFunctionResolver quarter() {
);
}

private DefaultFunctionResolver sec_to_time() {
return define(BuiltinFunctionName.SEC_TO_TIME.getName(),
impl((nullMissingHandling(DateTimeFunction::exprSecToTime)), TIME, INTEGER),
impl((nullMissingHandling(DateTimeFunction::exprSecToTime)), TIME, LONG),
impl((nullMissingHandling(DateTimeFunction::exprSecToTimeWithNanos)), TIME, DOUBLE),
impl((nullMissingHandling(DateTimeFunction::exprSecToTimeWithNanos)), TIME, FLOAT)
);
}

/**
* SECOND(STRING/TIME/DATETIME/TIMESTAMP). return the second value for time.
*/
Expand Down Expand Up @@ -888,6 +901,19 @@ private DefaultFunctionResolver week(BuiltinFunctionName week) {
);
}

private DefaultFunctionResolver weekday() {
return define(BuiltinFunctionName.WEEKDAY.getName(),
implWithProperties(nullMissingHandlingWithProperties(
(functionProperties, arg) -> new ExprIntegerValue(
formatNow(functionProperties.getQueryStartClock()).getDayOfWeek().getValue() - 1)),
INTEGER, TIME),
impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, DATE),
impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, DATETIME),
impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, TIMESTAMP),
impl(nullMissingHandling(DateTimeFunction::exprWeekday), INTEGER, STRING)
);
}

/**
* YEAR(STRING/DATE/DATETIME/TIMESTAMP). return the year for date (1000-9999).
*/
Expand Down Expand Up @@ -1498,6 +1524,44 @@ private ExprValue exprQuarter(ExprValue date) {
return new ExprIntegerValue((month / 3) + ((month % 3) == 0 ? 0 : 1));
}

/**
* Returns TIME value of sec_to_time function for an INTEGER or LONG arguments.
* @param totalSeconds The total number of seconds
* @return A TIME value
*/
private ExprValue exprSecToTime(ExprValue totalSeconds) {
return new ExprTimeValue(LocalTime.MIN.plus(Duration.ofSeconds(totalSeconds.longValue())));
}

/**
* Helper function which obtains the decimal portion of the seconds value passed in.
* Uses BigDecimal to prevent issues with math on floating point numbers.
* Return is formatted to be used with Duration.ofSeconds();
*
* @param seconds and ExprDoubleValue or ExprFloatValue for the seconds
* @return A LONG representing the nanoseconds portion
*/
private long formatNanos(ExprValue seconds) {
//Convert ExprValue to BigDecimal
BigDecimal formattedNanos = BigDecimal.valueOf(seconds.doubleValue());
//Extract only the nanosecond part
formattedNanos = formattedNanos.subtract(BigDecimal.valueOf(formattedNanos.intValue()));

return formattedNanos.scaleByPowerOfTen(9).longValue();
}

/**
* Returns TIME value of sec_to_time function for FLOAT or DOUBLE arguments.
* @param totalSeconds The total number of seconds
* @return A TIME value
*/
private ExprValue exprSecToTimeWithNanos(ExprValue totalSeconds) {
long nanos = formatNanos(totalSeconds);

return new ExprTimeValue(
LocalTime.MIN.plus(Duration.ofSeconds(totalSeconds.longValue(), nanos)));
}

/**
* Second implementation for ExprValue.
*
Expand Down Expand Up @@ -1637,6 +1701,16 @@ private ExprValue exprWeek(ExprValue date, ExprValue mode) {
CalendarLookup.getWeekNumber(mode.integerValue(), date.dateValue()));
}

/**
* Weekday implementation for ExprValue.
*
* @param date ExprValue of Date/Datetime/String/Timstamp type.
* @return ExprValue.
*/
private ExprValue exprWeekday(ExprValue date) {
return new ExprIntegerValue(date.dateValue().getDayOfWeek().getValue() - 1);
}

private ExprValue unixTimeStamp(Clock clock) {
return new ExprLongValue(Instant.now(clock).getEpochSecond());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public enum BuiltinFunctionName {
POW(FunctionName.of("pow")),
POWER(FunctionName.of("power")),
RAND(FunctionName.of("rand")),
RINT(FunctionName.of("rint")),
ROUND(FunctionName.of("round")),
SIGN(FunctionName.of("sign")),
SINH(FunctionName.of("sinh")),
SQRT(FunctionName.of("sqrt")),
CBRT(FunctionName.of("cbrt")),
TRUNCATE(FunctionName.of("truncate")),
Expand All @@ -49,6 +51,7 @@ public enum BuiltinFunctionName {
ATAN(FunctionName.of("atan")),
ATAN2(FunctionName.of("atan2")),
COS(FunctionName.of("cos")),
COSH(FunctionName.of("cosh")),
COT(FunctionName.of("cot")),
DEGREES(FunctionName.of("degrees")),
RADIANS(FunctionName.of("radians")),
Expand Down Expand Up @@ -93,6 +96,7 @@ public enum BuiltinFunctionName {
PERIOD_ADD(FunctionName.of("period_add")),
PERIOD_DIFF(FunctionName.of("period_diff")),
QUARTER(FunctionName.of("quarter")),
SEC_TO_TIME(FunctionName.of("sec_to_time")),
SECOND(FunctionName.of("second")),
SECOND_OF_MINUTE(FunctionName.of("second_of_minute")),
SUBDATE(FunctionName.of("subdate")),
Expand All @@ -108,6 +112,7 @@ public enum BuiltinFunctionName {
UTC_TIMESTAMP(FunctionName.of("utc_timestamp")),
UNIX_TIMESTAMP(FunctionName.of("unix_timestamp")),
WEEK(FunctionName.of("week")),
WEEKDAY(FunctionName.of("weekday")),
WEEKOFYEAR(FunctionName.of("weekofyear")),
WEEK_OF_YEAR(FunctionName.of("week_of_year")),
YEAR(FunctionName.of("year")),
Expand Down
Loading

0 comments on commit ebbeb25

Please sign in to comment.