-
Notifications
You must be signed in to change notification settings - Fork 93
feat(isthmus): support month()-style datetime operators #643
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
Merged
bestbeforetoday
merged 3 commits into
substrait-io:main
from
mbwhite:fix-month-datetime
Dec 19, 2025
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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 |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ _apps | |
| _data | ||
| **/*/bin | ||
| build | ||
| substrait.plan | ||
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
96 changes: 96 additions & 0 deletions
96
isthmus/src/main/java/io/substrait/isthmus/expression/ExtractDateFunctionMapper.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,96 @@ | ||
| package io.substrait.isthmus.expression; | ||
|
|
||
| import io.substrait.expression.Expression; | ||
| import io.substrait.expression.FunctionArg; | ||
| import io.substrait.extension.SimpleExtension.ScalarFunctionVariant; | ||
| import java.util.Collections; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.calcite.avatica.util.TimeUnitRange; | ||
| import org.apache.calcite.rex.RexBuilder; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.rex.RexLiteral; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
|
|
||
| /** | ||
| * Custom mapping for the Calcite MONTH/DAY/QUARTER functions. | ||
| * | ||
| * <p>These come from Calcite as 2 argument functions (like YEAR) but in Substrait these functions | ||
| * are 3 arguments; the additional being if this is a 0 or 1 based value. Calcite is a 1 based value | ||
| * in this case. | ||
| * | ||
| * <p>We need to therefore map the MONTH etc functions to a different Substrait function. | ||
| */ | ||
| final class ExtractDateFunctionMapper implements ScalarFunctionMapper { | ||
|
|
||
| private final Map<String, ScalarFunctionVariant> extractFunctions; | ||
|
|
||
| public ExtractDateFunctionMapper(List<ScalarFunctionVariant> functions) { | ||
|
|
||
| Map<String, ScalarFunctionVariant> fns = | ||
| functions.stream() | ||
| .filter(f -> "extract".equals(f.name())) | ||
| .collect(Collectors.toMap(Object::toString, f -> f)); | ||
|
|
||
| this.extractFunctions = Collections.unmodifiableMap(fns); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<SubstraitFunctionMapping> toSubstrait(final RexCall call) { | ||
| if (!SqlStdOperatorTable.EXTRACT.equals(call.op)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| if (call.operandCount() < 2) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| RexNode extractType = call.operands.get(0); | ||
| if (extractType.getType().getSqlTypeName() != SqlTypeName.SYMBOL) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| final RexNode dataType = call.operands.get(1); | ||
| if (!dataType.getType().getSqlTypeName().equals(SqlTypeName.DATE)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| TimeUnitRange value = ((RexLiteral) extractType).getValueAs(TimeUnitRange.class); | ||
|
|
||
| switch (value) { | ||
| case QUARTER: | ||
| case MONTH: | ||
| case DAY: | ||
| { | ||
| final List<RexNode> newOperands = new LinkedList<>(call.operands); | ||
| newOperands.add(1, RexBuilder.DEFAULT.makeFlag(ExtractIndexing.ONE)); | ||
|
|
||
| final ScalarFunctionVariant substraitFn = | ||
| this.extractFunctions.get("extract:req_req_date"); | ||
| return Optional.of( | ||
| new SubstraitFunctionMapping("extract", newOperands, List.of(substraitFn))); | ||
| } | ||
| default: | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<List<FunctionArg>> getExpressionArguments( | ||
| final Expression.ScalarFunctionInvocation expression) { | ||
| String name = expression.declaration().toString(); | ||
|
|
||
| if ("extract:req_req_date".equals(name)) { | ||
| final List<FunctionArg> newArgs = new LinkedList<>(expression.arguments()); | ||
| newArgs.remove(1); | ||
|
|
||
| return Optional.of(newArgs); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
isthmus/src/main/java/io/substrait/isthmus/expression/ExtractIndexing.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,11 @@ | ||
| package io.substrait.isthmus.expression; | ||
|
|
||
| /** | ||
| * Enum to define the INDEXING property on the date functions. | ||
| * | ||
| * <p>Controls if the number used for example in months is 0 or 1 based. | ||
| */ | ||
| public enum ExtractIndexing { | ||
| ONE, | ||
| ZERO | ||
| } |
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
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
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
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
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.
Why do we need to ignore these?
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.
It's created for the examples, and I was used them as a test and kept adding it to the commit.