-
Notifications
You must be signed in to change notification settings - Fork 95
refactor(isthmus): strptime function mapping #701
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
Open
bestbeforetoday
wants to merge
2
commits into
substrait-io:main
Choose a base branch
from
bestbeforetoday:strptime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
isthmus/src/main/java/io/substrait/isthmus/expression/AbstractStrptimeFunctionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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); | ||
| } | ||
| } | ||
50 changes: 4 additions & 46 deletions
50
isthmus/src/main/java/io/substrait/isthmus/expression/StrptimeDateFunctionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
50 changes: 4 additions & 46 deletions
50
isthmus/src/main/java/io/substrait/isthmus/expression/StrptimeTimeFunctionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
50 changes: 3 additions & 47 deletions
50
isthmus/src/main/java/io/substrait/isthmus/expression/StrptimeTimestampFunctionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
PositionFunctionMapperalso just swaps the position of the first two arguments of its function callUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.