-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[FLINK-19796][table] Explicit casting should be made if the type of a… #15906
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
Merged
Changes from all commits
Commits
Show all changes
5 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
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
48 changes: 48 additions & 0 deletions
48
...blink/src/main/java/org/apache/flink/table/planner/functions/sql/SqlArrayConstructor.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,48 @@ | ||
| /* | ||
| * 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.sql; | ||
|
|
||
| import org.apache.flink.table.planner.functions.utils.SqlValidatorUtils; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.SqlOperatorBinding; | ||
| import org.apache.calcite.sql.fun.SqlArrayValueConstructor; | ||
| import org.apache.calcite.sql.type.SqlTypeUtil; | ||
|
|
||
| /** | ||
| * {@link SqlOperator} for <code>ARRAY</code>, which makes explicit casting if the element type not | ||
| * equals the derived component type. | ||
| */ | ||
| public class SqlArrayConstructor extends SqlArrayValueConstructor { | ||
|
|
||
| @Override | ||
| public RelDataType inferReturnType(SqlOperatorBinding opBinding) { | ||
| RelDataType type = | ||
| getComponentType(opBinding.getTypeFactory(), opBinding.collectOperandTypes()); | ||
| if (null == type) { | ||
| return null; | ||
| } | ||
|
|
||
| // explicit cast elements to component type if they are not same | ||
| SqlValidatorUtils.adjustTypeForArrayConstructor(type, opBinding); | ||
|
|
||
| return SqlTypeUtil.createArrayType(opBinding.getTypeFactory(), type, false); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
...r-blink/src/main/java/org/apache/flink/table/planner/functions/sql/SqlMapConstructor.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,60 @@ | ||
| /* | ||
| * 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.sql; | ||
|
|
||
| import org.apache.flink.table.planner.functions.utils.SqlValidatorUtils; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.rel.type.RelDataTypeFactory; | ||
| import org.apache.calcite.sql.SqlOperator; | ||
| import org.apache.calcite.sql.SqlOperatorBinding; | ||
| import org.apache.calcite.sql.fun.SqlMapValueConstructor; | ||
| import org.apache.calcite.sql.type.SqlTypeUtil; | ||
| import org.apache.calcite.util.Pair; | ||
| import org.apache.calcite.util.Util; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * {@link SqlOperator} for <code>MAP</code>, which makes explicit casting if the element type not | ||
| * equals the derived component type. | ||
| */ | ||
| public class SqlMapConstructor extends SqlMapValueConstructor { | ||
|
|
||
| @Override | ||
| public RelDataType inferReturnType(SqlOperatorBinding opBinding) { | ||
| Pair<RelDataType, RelDataType> type = | ||
| getComponentTypes(opBinding.getTypeFactory(), opBinding.collectOperandTypes()); | ||
| if (null == type) { | ||
| return null; | ||
| } | ||
|
|
||
| // explicit cast elements to component type if they are not same | ||
| SqlValidatorUtils.adjustTypeForMapConstructor(type, opBinding); | ||
|
|
||
| return SqlTypeUtil.createMapType(opBinding.getTypeFactory(), type.left, type.right, false); | ||
| } | ||
|
|
||
| private Pair<RelDataType, RelDataType> getComponentTypes( | ||
| RelDataTypeFactory typeFactory, List<RelDataType> argTypes) { | ||
| return Pair.of( | ||
| typeFactory.leastRestrictive(Util.quotientList(argTypes, 2, 0)), | ||
| typeFactory.leastRestrictive(Util.quotientList(argTypes, 2, 1))); | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
...blink/src/main/java/org/apache/flink/table/planner/functions/utils/SqlValidatorUtils.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,84 @@ | ||
| /* | ||
| * 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.utils; | ||
|
|
||
| import org.apache.calcite.rel.type.RelDataType; | ||
| import org.apache.calcite.sql.SqlCall; | ||
| import org.apache.calcite.sql.SqlCallBinding; | ||
| import org.apache.calcite.sql.SqlNode; | ||
| import org.apache.calcite.sql.SqlOperatorBinding; | ||
| import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
| import org.apache.calcite.sql.parser.SqlParserPos; | ||
| import org.apache.calcite.sql.type.SqlTypeUtil; | ||
| import org.apache.calcite.util.Pair; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** Utility methods related to SQL validation. */ | ||
| public class SqlValidatorUtils { | ||
|
|
||
| public static void adjustTypeForArrayConstructor( | ||
| RelDataType componentType, SqlOperatorBinding opBinding) { | ||
| if (opBinding instanceof SqlCallBinding) { | ||
| adjustTypeForMultisetConstructor( | ||
| componentType, componentType, (SqlCallBinding) opBinding); | ||
| } | ||
| } | ||
|
|
||
| public static void adjustTypeForMapConstructor( | ||
| Pair<RelDataType, RelDataType> componentType, SqlOperatorBinding opBinding) { | ||
| if (opBinding instanceof SqlCallBinding) { | ||
| adjustTypeForMultisetConstructor( | ||
| componentType.getKey(), componentType.getValue(), (SqlCallBinding) opBinding); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * When the element element does not equal with the component type, making explicit casting. | ||
| * | ||
| * @param evenType derived type for element with even index | ||
| * @param oddType derived type for element with odd index | ||
| * @param sqlCallBinding description of call | ||
| */ | ||
| private static void adjustTypeForMultisetConstructor( | ||
| RelDataType evenType, RelDataType oddType, SqlCallBinding sqlCallBinding) { | ||
| SqlCall call = sqlCallBinding.getCall(); | ||
| List<RelDataType> operandTypes = sqlCallBinding.collectOperandTypes(); | ||
| List<SqlNode> operands = call.getOperandList(); | ||
| RelDataType elementType; | ||
| for (int i = 0; i < operands.size(); i++) { | ||
| if (i % 2 == 0) { | ||
| elementType = evenType; | ||
| } else { | ||
| elementType = oddType; | ||
| } | ||
| if (operandTypes.get(i).equalsSansFieldNames(elementType)) { | ||
| continue; | ||
| } | ||
| call.setOperand(i, castTo(operands.get(i), elementType)); | ||
| } | ||
| } | ||
|
|
||
| private static SqlNode castTo(SqlNode node, RelDataType type) { | ||
| return SqlStdOperatorTable.CAST.createCall( | ||
| SqlParserPos.ZERO, | ||
| node, | ||
| SqlTypeUtil.convertTypeToSpec(type).withNullable(type.isNullable())); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.