Skip to content
Open
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
@@ -0,0 +1,71 @@
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.SqlOperator;

/**
* Abstract base class for custom mappings between Calcite PARSE_* functions and Substrait
* strptime_* functions.
*
* <p>Calcite PARSE_* functions have <em>format</em> followed by <em>string</em> parameters, while
* Substrait strptime_* functions have <em>string</em> followed by <em>format</em>. When mapping
* between Calcite and Substrait, the parameters need to be reversed.
*/
abstract class AbstractStrptimeFunctionMapper implements ScalarFunctionMapper {
private final String substraitFunctionName;
private final SqlOperator calciteOperator;
private final List<ScalarFunctionVariant> strptimeFunctions;

/**
* Constructs an abstract strptime function mapper.
*
* @param substraitFunctionName the name of the Substrait function (e.g., "strptime_date")
* @param calciteOperator the Calcite operator to map from (e.g., SqlLibraryOperators.PARSE_DATE)
* @param functions the list of all available scalar function variants
*/
protected AbstractStrptimeFunctionMapper(
Copy link
Member

Choose a reason for hiding this comment

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

if you want to have even more reuse then PositionFunctionMapper also just swaps the position of the first two arguments of its function call

Copy link
Member Author

@bestbeforetoday bestbeforetoday Feb 13, 2026

Choose a reason for hiding this comment

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

This refactor consolidates a family of closely related date/time parsing functions that require the same argument mapping and differ only by the type they return. POSITION deals with string searching so, while it also requires the same argument mapping, the relationship is not obvious and could be confusing. I would prefer to keep this PR focused on removing duplication in the closely related date/time parsing functions.

String substraitFunctionName,
SqlOperator calciteOperator,
List<ScalarFunctionVariant> functions) {
this.substraitFunctionName = substraitFunctionName;
this.calciteOperator = calciteOperator;
this.strptimeFunctions =
functions.stream()
.filter(f -> substraitFunctionName.equals(f.name()))
.collect(Collectors.toUnmodifiableList());
}

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

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

return Optional.of(
new SubstraitFunctionMapping(substraitFunctionName, operands, strptimeFunctions));
}

@Override
public Optional<List<FunctionArg>> getExpressionArguments(ScalarFunctionInvocation expression) {
if (!substraitFunctionName.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
@@ -1,60 +1,18 @@
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)}.
* function. {@code PARSE_DATE(format, date_string)} maps to {@code strptime_date(date_string,
* format)}.
*/
public final class StrptimeDateFunctionMapper implements ScalarFunctionMapper {
public final class StrptimeDateFunctionMapper extends AbstractStrptimeFunctionMapper {
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);
super(STRPTIME_DATE_FUNCTION_NAME, SqlLibraryOperators.PARSE_DATE, functions);
}
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,18 @@
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)}.
* function. {@code PARSE_TIME(format, time_string)} maps to {@code strptime_time(time_string,
* format)}.
*/
public final class StrptimeTimeFunctionMapper implements ScalarFunctionMapper {
public final class StrptimeTimeFunctionMapper extends AbstractStrptimeFunctionMapper {
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);
super(STRPTIME_TIME_FUNCTION_NAME, SqlLibraryOperators.PARSE_TIME, functions);
}
}
Original file line number Diff line number Diff line change
@@ -1,62 +1,18 @@
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} function. {@code PARSE_TIMESTAMP(format, timestamp_string)} maps to {@code
* strptime_timestamp(timestamp_string, format)}.
*/
public final class StrptimeTimestampFunctionMapper implements ScalarFunctionMapper {
public final class StrptimeTimestampFunctionMapper extends AbstractStrptimeFunctionMapper {
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);
super(STRPTIME_TIMESTAMP_FUNCTION_NAME, SqlLibraryOperators.PARSE_TIMESTAMP, functions);
}
}
Loading