-
Notifications
You must be signed in to change notification settings - Fork 95
fix(isthmus): use schema types for virtual table literals #1065
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
alexandrefimov
wants to merge
1
commit into
substrait-io:main
Choose a base branch
from
alexandrefimov:codex/isthmus-values-schema-literals
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.
+70
−16
Open
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
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import java.util.Optional; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rex.RexLiteral; | ||
| import org.apache.calcite.util.DateString; | ||
|
|
@@ -91,9 +92,7 @@ private static BigDecimal bd(RexLiteral literal) { | |
| * @throws UnsupportedOperationException if the literal type/value cannot be handled | ||
| */ | ||
| public Expression.Literal convert(RexLiteral literal) { | ||
| // convert type first to guarantee we can handle the value. | ||
| final Type type = typeConverter.toSubstrait(literal.getType()); | ||
| return convert(literal, type.nullable()); | ||
| return convert(literal, literal.getType()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -108,14 +107,34 @@ public Expression.Literal convert(RexLiteral literal) { | |
| * @return the converted Substrait Literal | ||
| */ | ||
| public Expression.Literal convert(RexLiteral literal, boolean nullable) { | ||
| return convert(literal, literal.getType(), nullable); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a RexLiteral to a Substrait Literal using the specified result type. | ||
| * | ||
| * <p>This overload is useful when the target type comes from a containing schema rather than the | ||
| * literal itself. Calcite may infer a narrower type for a value in a LogicalValues tuple than for | ||
| * the corresponding row field. | ||
| * | ||
| * @param literal the RexLiteral to convert | ||
| * @param resultType the Calcite type required by the containing schema | ||
| * @return the converted Substrait Literal | ||
| */ | ||
| public Expression.Literal convert(RexLiteral literal, RelDataType resultType) { | ||
| Type type = typeConverter.toSubstrait(resultType); | ||
| return convert(literal, resultType, type.nullable()); | ||
| } | ||
|
|
||
| private Expression.Literal convert(RexLiteral literal, RelDataType resultType, boolean nullable) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Getting rid of the |
||
| if (literal.isNull()) { | ||
| final Type type = typeConverter.toSubstrait(literal.getType()); | ||
| final Type type = typeConverter.toSubstrait(resultType); | ||
| final Type typeWithNullability = | ||
| nullable ? TypeCreator.asNullable(type) : TypeCreator.asNotNullable(type); | ||
| return ExpressionCreator.typedNull(typeWithNullability); | ||
| } | ||
|
|
||
| switch (literal.getType().getSqlTypeName()) { | ||
| switch (resultType.getSqlTypeName()) { | ||
| case TINYINT: | ||
| return ExpressionCreator.i8(nullable, i(literal).intValue()); | ||
| case SMALLINT: | ||
|
|
@@ -145,23 +164,23 @@ public Expression.Literal convert(RexLiteral literal, boolean nullable) { | |
| { | ||
| BigDecimal bd = bd(literal); | ||
| return ExpressionCreator.decimal( | ||
| nullable, bd, literal.getType().getPrecision(), literal.getType().getScale()); | ||
| nullable, bd, resultType.getPrecision(), resultType.getScale()); | ||
| } | ||
| case VARCHAR: | ||
| { | ||
| if (literal.getType().getPrecision() == RelDataType.PRECISION_NOT_SPECIFIED) { | ||
| if (resultType.getPrecision() == RelDataType.PRECISION_NOT_SPECIFIED) { | ||
| return ExpressionCreator.string(nullable, s(literal)); | ||
| } | ||
|
|
||
| return ExpressionCreator.varChar(nullable, s(literal), literal.getType().getPrecision()); | ||
| return ExpressionCreator.varChar(nullable, s(literal), resultType.getPrecision()); | ||
| } | ||
| case BINARY: | ||
| return ExpressionCreator.fixedBinary( | ||
| nullable, | ||
| ByteString.copyFrom( | ||
| padRightIfNeeded( | ||
| literal.getValueAs(org.apache.calcite.avatica.util.ByteString.class), | ||
| literal.getType().getPrecision()))); | ||
| resultType.getPrecision()))); | ||
| case VARBINARY: | ||
| return ExpressionCreator.binary( | ||
| nullable, ByteString.copyFrom(literal.getValueAs(byte[].class))); | ||
|
|
@@ -246,21 +265,29 @@ public Expression.Literal convert(RexLiteral literal, boolean nullable) { | |
| { | ||
| List<RexLiteral> literals = (List<RexLiteral>) literal.getValue(); | ||
| return ExpressionCreator.struct( | ||
| nullable, literals.stream().map(this::convert).collect(Collectors.toList())); | ||
| nullable, | ||
| IntStream.range(0, literals.size()) | ||
| .mapToObj( | ||
| i -> convert(literals.get(i), resultType.getFieldList().get(i).getType())) | ||
| .collect(Collectors.toList())); | ||
| } | ||
|
|
||
| case ARRAY: | ||
| { | ||
| List<RexLiteral> literals = (List<RexLiteral>) literal.getValue(); | ||
| RelDataType componentType = Objects.requireNonNull(resultType.getComponentType()); | ||
| return ExpressionCreator.list( | ||
| nullable, literals.stream().map(this::convert).collect(Collectors.toList())); | ||
| nullable, | ||
| literals.stream() | ||
| .map(nestedLiteral -> convert(nestedLiteral, componentType)) | ||
| .collect(Collectors.toList())); | ||
| } | ||
|
|
||
| default: | ||
| throw new UnsupportedOperationException( | ||
| String.format( | ||
| "Unable to convert the value of %s of type %s to a literal.", | ||
| literal, literal.getType().getSqlTypeName())); | ||
| literal, resultType.getSqlTypeName())); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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
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.
I would advocate for getting rid of this method entirely actually.
It's not used internally after your changes, and any user that wants to set the nullability explicitly on the Calcite type can use the
RelDataTypeFactory#createTypeWithNullabilityto set it, and then pass it to the new method below.