-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[FLINK-24577][table-planner] Implement Cast from BINARY to RAW #19409
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
| */ | ||
| package org.apache.calcite.rex; | ||
|
|
||
| import org.apache.flink.table.planner.plan.schema.RawRelDataType; | ||
|
|
||
| import com.google.common.collect.ArrayListMultimap; | ||
| import com.google.common.collect.BoundType; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
@@ -232,6 +234,9 @@ public RexNode simplifyPreservingType( | |
| rexBuilder.typeFactory, e2.getType(), e.getType())) { | ||
| return e2; | ||
| } | ||
| if (e.getType().getSqlTypeName() == SqlTypeName.OTHER) { | ||
| return e; | ||
| } | ||
| final RexNode e3 = rexBuilder.makeCast(e.getType(), e2, matchNullability); | ||
| if (e3.equals(e)) { | ||
| return e; | ||
|
|
@@ -2024,7 +2029,9 @@ private RexNode simplifySearch(RexCall call, RexUnknownAs unknownAs) { | |
| private RexNode simplifyCast(RexCall e) { | ||
| RexNode operand = e.getOperands().get(0); | ||
| operand = simplify(operand, UNKNOWN); | ||
| if (sameTypeOrNarrowsNullability(e.getType(), operand.getType())) { | ||
| if (sameTypeOrNarrowsNullability(e.getType(), operand.getType()) | ||
| || e.getType() instanceof RawRelDataType) { | ||
| // || e.getType().getSqlTypeName() == SqlTypeName.OTHER) { | ||
|
Contributor
Author
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. Maybe we need this instead?
Contributor
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. Not sure, OTHER catches other types in our type system, right?
Contributor
Author
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. For us, currently, only |
||
| return operand; | ||
| } | ||
| if (RexUtil.isLosslessCast(operand)) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.flink.table.planner.functions.casting; | ||
|
|
||
| import org.apache.flink.table.data.binary.BinaryRawValueData; | ||
| import org.apache.flink.table.types.logical.LogicalType; | ||
| import org.apache.flink.table.types.logical.LogicalTypeFamily; | ||
| import org.apache.flink.table.types.logical.LogicalTypeRoot; | ||
|
|
||
| import static org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall; | ||
|
|
||
| /** {@link LogicalTypeFamily#BINARY_STRING} to {@link LogicalTypeRoot#RAW} to cast rule. */ | ||
| class BinaryToRawCastRule extends AbstractNullAwareCodeGeneratorCastRule<byte[], Object> { | ||
|
|
||
| static final BinaryToRawCastRule INSTANCE = new BinaryToRawCastRule(); | ||
|
|
||
| private BinaryToRawCastRule() { | ||
| super( | ||
| CastRulePredicate.builder() | ||
| .input(LogicalTypeFamily.BINARY_STRING) | ||
| .target(LogicalTypeRoot.RAW) | ||
| .build()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canFail(LogicalType inputLogicalType, LogicalType targetLogicalType) { | ||
| return true; | ||
| } | ||
|
|
||
| /* Example generated code for BINARY(3): | ||
|
|
||
| isNull$0 = _myInputIsNull; | ||
| if (!isNull$0) { | ||
| result$1 = org.apache.flink.table.data.binary.BinaryRawValueData.fromBytes(_myInput); | ||
| isNull$0 = result$1 == null; | ||
| } else { | ||
| result$1 = null; | ||
| } | ||
|
|
||
| */ | ||
|
|
||
| @Override | ||
| protected String generateCodeBlockInternal( | ||
| CodeGeneratorCastRule.Context context, | ||
| String inputTerm, | ||
| String returnVariable, | ||
| LogicalType inputLogicalType, | ||
| LogicalType targetLogicalType) { | ||
| // Get serializer for RAW type | ||
| return new CastRuleUtils.CodeWriter() | ||
| .assignStmt( | ||
| returnVariable, | ||
| staticCall(BinaryRawValueData.class, "fromBytes", inputTerm)) | ||
| .toString(); | ||
| } | ||
| } |
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.
The change in this line and in
RexLiteralis a blocker for this PR in my opinion. We should fix these things in Calcite first and only port the changes to the Flink code base, once merged in Calcite.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've spent quite some time here, and couldn't find a workaround.
I could modify the PR and only allow it for TableAPI for the moment, and try to fix this in Calcite first.
If it's accepted, have a follow up PR to enable this for SQL in flink.