Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public class FunctionMappings {
s(SqlLibraryOperators.LEFT, "left"),
s(SqlLibraryOperators.RIGHT, "right"),
s(SqlLibraryOperators.LPAD, "lpad"),
s(SqlLibraryOperators.RPAD, "rpad"))
s(SqlLibraryOperators.RPAD, "rpad"),
s(SqlLibraryOperators.PARSE_TIME, "strptime_time"),
s(SqlLibraryOperators.PARSE_TIMESTAMP, "strptime_timestamp"),
s(SqlLibraryOperators.PARSE_DATE, "strptime_date"))
.build();

public static final ImmutableList<Sig> AGGREGATE_SIGS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public ScalarFunctionConverter(
new TrimFunctionMapper(functions),
new SqrtFunctionMapper(functions),
new ExtractDateFunctionMapper(functions),
new PositionFunctionMapper(functions));
new PositionFunctionMapper(functions),
new StrptimeDateFunctionMapper(functions),
new StrptimeTimeFunctionMapper(functions),
new StrptimeTimestampFunctionMapper(functions));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.substrait.isthmus.expression;

import io.substrait.expression.Expression.ScalarFunctionInvocation;
import io.substrait.expression.FunctionArg;
import io.substrait.extension.SimpleExtension.ScalarFunctionVariant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.fun.SqlLibraryOperators;

/**
* Custom mapping for the Calcite {@code PARSE_DATE} function to the Substrait {@code strptime_date}
* function.
*
* <p>Calcite {@code PARSE_DATE} has <em>format</em> followed by <em>date_string</em> parameters,
* while Substrait {@code strptime_date} has <em>date_string</em> followed by <em>format</em>. When
* mapping between Calcite and Substrait, the parameters need to be reversed.
*
* <p>{@code PARSE_DATE(format, date_string)} maps to {@code strptime_date(date_string, format)}.
*/
public final class StrptimeDateFunctionMapper implements ScalarFunctionMapper {
private static final String STRPTIME_DATE_FUNCTION_NAME = "strptime_date";
private final List<ScalarFunctionVariant> strptimeDateFunctions;

public StrptimeDateFunctionMapper(List<ScalarFunctionVariant> functions) {
strptimeDateFunctions =
functions.stream()
.filter(f -> STRPTIME_DATE_FUNCTION_NAME.equals(f.name()))
.collect(Collectors.toUnmodifiableList());
}

@Override
public Optional<SubstraitFunctionMapping> toSubstrait(RexCall call) {
if (!SqlLibraryOperators.PARSE_DATE.equals(call.op)) {
return Optional.empty();
}

List<RexNode> operands = new ArrayList<>(call.getOperands());
Collections.swap(operands, 0, 1);

return Optional.of(
new SubstraitFunctionMapping(STRPTIME_DATE_FUNCTION_NAME, operands, strptimeDateFunctions));
}

@Override
public Optional<List<FunctionArg>> getExpressionArguments(ScalarFunctionInvocation expression) {
if (!STRPTIME_DATE_FUNCTION_NAME.equals(expression.declaration().name())) {
return Optional.empty();
}

List<FunctionArg> arguments = new ArrayList<>(expression.arguments());
Collections.swap(arguments, 0, 1);

return Optional.of(arguments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.substrait.isthmus.expression;

import io.substrait.expression.Expression.ScalarFunctionInvocation;
import io.substrait.expression.FunctionArg;
import io.substrait.extension.SimpleExtension.ScalarFunctionVariant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.fun.SqlLibraryOperators;

/**
* Custom mapping for the Calcite {@code PARSE_TIME} function to the Substrait {@code strptime_time}
* function.
*
* <p>Calcite {@code PARSE_TIME} has <em>format</em> followed by <em>time_string</em> parameters,
* while Substrait {@code strptime_time} has <em>time_string</em> followed by <em>format</em>. When
* mapping between Calcite and Substrait, the parameters need to be reversed.
*
* <p>{@code PARSE_TIME(format, time_string)} maps to {@code strptime_time(time_string, format)}.
*/
public final class StrptimeTimeFunctionMapper implements ScalarFunctionMapper {
private static final String STRPTIME_TIME_FUNCTION_NAME = "strptime_time";
private final List<ScalarFunctionVariant> strptimeTimeFunctions;

public StrptimeTimeFunctionMapper(List<ScalarFunctionVariant> functions) {
strptimeTimeFunctions =
functions.stream()
.filter(f -> STRPTIME_TIME_FUNCTION_NAME.equals(f.name()))
.collect(Collectors.toUnmodifiableList());
}

@Override
public Optional<SubstraitFunctionMapping> toSubstrait(RexCall call) {
if (!SqlLibraryOperators.PARSE_TIME.equals(call.op)) {
return Optional.empty();
}

List<RexNode> operands = new ArrayList<>(call.getOperands());
Collections.swap(operands, 0, 1);

return Optional.of(
new SubstraitFunctionMapping(STRPTIME_TIME_FUNCTION_NAME, operands, strptimeTimeFunctions));
}

@Override
public Optional<List<FunctionArg>> getExpressionArguments(ScalarFunctionInvocation expression) {
if (!STRPTIME_TIME_FUNCTION_NAME.equals(expression.declaration().name())) {
return Optional.empty();
}

List<FunctionArg> arguments = new ArrayList<>(expression.arguments());
Collections.swap(arguments, 0, 1);

return Optional.of(arguments);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.substrait.isthmus.expression;

import io.substrait.expression.Expression.ScalarFunctionInvocation;
import io.substrait.expression.FunctionArg;
import io.substrait.extension.SimpleExtension.ScalarFunctionVariant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.fun.SqlLibraryOperators;

/**
* Custom mapping for the Calcite {@code PARSE_TIMESTAMP} function to the Substrait {@code
* strptime_timestamp} function.
*
* <p>Calcite {@code PARSE_TIMESTAMP} has <em>format</em> followed by <em>timestamp_string</em>
* parameters, while Substrait {@code strptime_timestamp} has <em>timestamp_string</em> followed by
* <em>format</em>. When mapping between Calcite and Substrait, the parameters need to be reversed.
*
* <p>{@code PARSE_TIMESTAMP(format, timestamp_string)} maps to {@code
* strptime_timestamp(timestamp_string, format)}.
*/
public final class StrptimeTimestampFunctionMapper implements ScalarFunctionMapper {
private static final String STRPTIME_TIMESTAMP_FUNCTION_NAME = "strptime_timestamp";
private final List<ScalarFunctionVariant> strptimeTimestampFunctions;

public StrptimeTimestampFunctionMapper(List<ScalarFunctionVariant> functions) {
strptimeTimestampFunctions =
functions.stream()
.filter(f -> STRPTIME_TIMESTAMP_FUNCTION_NAME.equals(f.name()))
.collect(Collectors.toUnmodifiableList());
}

@Override
public Optional<SubstraitFunctionMapping> toSubstrait(RexCall call) {
if (!SqlLibraryOperators.PARSE_TIMESTAMP.equals(call.op)) {
return Optional.empty();
}

List<RexNode> operands = new ArrayList<>(call.getOperands());
Collections.swap(operands, 0, 1);

return Optional.of(
new SubstraitFunctionMapping(
STRPTIME_TIMESTAMP_FUNCTION_NAME, operands, strptimeTimestampFunctions));
}

@Override
public Optional<List<FunctionArg>> getExpressionArguments(ScalarFunctionInvocation expression) {
if (!STRPTIME_TIMESTAMP_FUNCTION_NAME.equals(expression.declaration().name())) {
return Optional.empty();
}

List<FunctionArg> arguments = new ArrayList<>(expression.arguments());
Collections.swap(arguments, 0, 1);

return Optional.of(arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.substrait.isthmus.expression.ScalarFunctionConverter;
import io.substrait.isthmus.expression.WindowFunctionConverter;
import io.substrait.type.TypeCreator;
import java.util.stream.Stream;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.SqlKind;
Expand All @@ -25,21 +26,25 @@
* Verify that "problematic" Substrait functions can be converted to Calcite and back successfully
*/
class FunctionConversionTest extends PlanTestBase {
final ScalarFunctionConverter scalarFnConverter =
new ScalarFunctionConverter(extensions.scalarFunctions(), typeFactory);

final WindowFunctionConverter windowFnConverter =
new WindowFunctionConverter(extensions.windowFunctions(), typeFactory);

final ExpressionRexConverter expressionRexConverter =
new ExpressionRexConverter(
typeFactory,
new ScalarFunctionConverter(extensions.scalarFunctions(), typeFactory),
new WindowFunctionConverter(extensions.windowFunctions(), typeFactory),
TypeConverter.DEFAULT);
typeFactory, scalarFnConverter, windowFnConverter, TypeConverter.DEFAULT);

final RexExpressionConverter rexExpressionConverter =
new RexExpressionConverter(
// a SubstraitRelVisitor is not needed for these tests
null,
CallConverters.defaults(TypeConverter.DEFAULT),
// TODO: set WindowFunctionConverter if/when tests for window functions are added
null,
Stream.concat(
CallConverters.defaults(TypeConverter.DEFAULT).stream(),
Stream.of(scalarFnConverter))
.toList(),
windowFnConverter,
TypeConverter.DEFAULT);

@Test
Expand All @@ -61,8 +66,8 @@ void subtractDateIDay() {
TypeConverter.DEFAULT.toCalcite(typeFactory, TypeCreator.REQUIRED.DATE),
calciteExpr.getType());

// TODO: remove once this can be converted back to Substrait
assertThrows(IllegalArgumentException.class, () -> calciteExpr.accept(rexExpressionConverter));
Expression reverse = calciteExpr.accept(rexExpressionConverter);
assertEquals(expr, reverse);
}

@Test
Expand Down Expand Up @@ -278,4 +283,81 @@ void concatCharAndVarchar() throws Exception {
void concatStringLiteralAndChar() throws Exception {
assertProtoPlanRoundrip("select 'brand_'||P_BRAND from PART");
}

@Test
void testStrptimeTime() {
Expression.StrLiteral timeString = Expression.StrLiteral.builder().value("12:34:56").build();
Expression.StrLiteral formatString = Expression.StrLiteral.builder().value("%H:%M:%S").build();
ScalarFunctionInvocation strptimeTimeFn =
sb.scalarFn(
DefaultExtensionCatalog.FUNCTIONS_DATETIME,
"strptime_time:str_str",
TypeCreator.REQUIRED.TIME,
timeString,
formatString);

// tests Substrait -> Calcite
RexNode calciteExpr = strptimeTimeFn.accept(expressionRexConverter, Context.newContext());
assertEquals(SqlKind.OTHER_FUNCTION, calciteExpr.getKind());
assertInstanceOf(RexCall.class, calciteExpr);

assertEquals("PARSE_TIME('%H:%M:%S':VARCHAR, '12:34:56':VARCHAR)", calciteExpr.toString());

// tests the reverse Calcite -> Substrait
Expression reverse = calciteExpr.accept(rexExpressionConverter);
assertEquals(strptimeTimeFn, reverse);
}

@Test
void testStrptimeTimestamp() {
Expression.StrLiteral timestampString =
Expression.StrLiteral.builder().value("2026-01-29T12:34:56").build();
Expression.StrLiteral formatString =
Expression.StrLiteral.builder().value("%Y:%m:%dT%H:%M:%S").build();
ScalarFunctionInvocation strptimeTimestampFn =
sb.scalarFn(
DefaultExtensionCatalog.FUNCTIONS_DATETIME,
"strptime_timestamp:str_str",
// using precision 6 here to be compatible with Calcite
TypeCreator.REQUIRED.precisionTimestamp(6),
timestampString,
formatString);

// tests Substrait -> Calcite
RexNode calciteExpr = strptimeTimestampFn.accept(expressionRexConverter, Context.newContext());
assertEquals(SqlKind.OTHER_FUNCTION, calciteExpr.getKind());
assertInstanceOf(RexCall.class, calciteExpr);

assertEquals(
"PARSE_TIMESTAMP('%Y:%m:%dT%H:%M:%S':VARCHAR, '2026-01-29T12:34:56':VARCHAR)",
calciteExpr.toString());

// tests the reverse Calcite -> Substrait
Expression reverse = calciteExpr.accept(rexExpressionConverter);
assertEquals(strptimeTimestampFn, reverse);
}

@Test
void testStrptimeDate() {
Expression.StrLiteral dateString = Expression.StrLiteral.builder().value("2026-01-29").build();
Expression.StrLiteral formatString = Expression.StrLiteral.builder().value("%Y:%m:%d").build();
ScalarFunctionInvocation strptimeDateFn =
sb.scalarFn(
DefaultExtensionCatalog.FUNCTIONS_DATETIME,
"strptime_date:str_str",
TypeCreator.REQUIRED.DATE,
dateString,
formatString);

// tests Substrait -> Calcite
RexNode calciteExpr = strptimeDateFn.accept(expressionRexConverter, Context.newContext());
assertEquals(SqlKind.OTHER_FUNCTION, calciteExpr.getKind());
assertInstanceOf(RexCall.class, calciteExpr);

assertEquals("PARSE_DATE('%Y:%m:%d':VARCHAR, '2026-01-29':VARCHAR)", calciteExpr.toString());

// tests the reverse Calcite -> Substrait
Expression reverse = calciteExpr.accept(rexExpressionConverter);
assertEquals(strptimeDateFn, reverse);
}
}