danny0405 commented on a change in pull request #12649: URL: https://github.com/apache/flink/pull/12649#discussion_r443528280
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/calcite/sql/fun/SqlDotOperator.java ########## @@ -0,0 +1,194 @@ +/* + * 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.calcite.sql.fun; + +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperandCountRange; +import org.apache.calcite.sql.SqlOperatorBinding; +import org.apache.calcite.sql.SqlSpecialOperator; +import org.apache.calcite.sql.SqlUtil; +import org.apache.calcite.sql.SqlWriter; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.OperandTypes; +import org.apache.calcite.sql.type.SqlOperandCountRanges; +import org.apache.calcite.sql.type.SqlSingleOperandTypeChecker; +import org.apache.calcite.sql.type.SqlTypeFamily; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.sql.util.SqlBasicVisitor; +import org.apache.calcite.sql.util.SqlVisitor; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.sql.validate.SqlValidatorScope; +import org.apache.calcite.sql.validate.SqlValidatorUtil; +import org.apache.calcite.util.Litmus; +import org.apache.calcite.util.Static; + +import java.util.Arrays; + +/** + * The dot operator {@code .}, used to access a field of a + * record. For example, {@code a.b}. + * + * <p>This class was copied over from Calcite to fix the derived type. + * If the ROW is nullable force the accessed field to be nullable as well. + */ +public class SqlDotOperator extends SqlSpecialOperator { + SqlDotOperator() { + super("DOT", SqlKind.DOT, 100, true, null, null, null); + } + + @Override public ReduceResult reduceExpr(int ordinal, TokenSequence list) { + SqlNode left = list.node(ordinal - 1); + SqlNode right = list.node(ordinal + 1); + return new ReduceResult(ordinal - 1, + ordinal + 2, + createCall( + SqlParserPos.sum( + Arrays.asList(left.getParserPosition(), + right.getParserPosition(), + list.pos(ordinal))), + left, + right)); + } + + @Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec, + int rightPrec) { + final SqlWriter.Frame frame = + writer.startList(SqlWriter.FrameTypeEnum.IDENTIFIER); + call.operand(0).unparse(writer, leftPrec, 0); + writer.sep("."); + call.operand(1).unparse(writer, 0, 0); + writer.endList(frame); + } + + @Override public SqlOperandCountRange getOperandCountRange() { + return SqlOperandCountRanges.of(2); + } + + @Override public <R> void acceptCall(SqlVisitor<R> visitor, SqlCall call, + boolean onlyExpressions, SqlBasicVisitor.ArgHandler<R> argHandler) { + if (onlyExpressions) { + // Do not visit operands[1] -- it is not an expression. + argHandler.visitChild(visitor, call, 0, call.operand(0)); + } else { + super.acceptCall(visitor, call, onlyExpressions, argHandler); + } + } + + @Override public RelDataType deriveType(SqlValidator validator, + SqlValidatorScope scope, SqlCall call) { + final SqlNode operand = call.getOperandList().get(0); + final RelDataType nodeType = + validator.deriveType(scope, operand); + assert nodeType != null; + if (!nodeType.isStruct()) { + throw SqlUtil.newContextException(operand.getParserPosition(), + Static.RESOURCE.incompatibleTypes()); + } + + final SqlNode fieldId = call.operand(1); + final String fieldName = fieldId.toString(); + final RelDataTypeField field = + nodeType.getField(fieldName, false, false); + if (field == null) { + throw SqlUtil.newContextException(fieldId.getParserPosition(), + Static.RESOURCE.unknownField(fieldName)); + } + RelDataType type = field.getType(); + if (nodeType.isNullable() && !type.isNullable()) { + type = validator.getTypeFactory().createTypeWithNullability(type, true); + } Review comment: In Calcite, when the record type is nullable, the attributes are always nullable, so, this fix is not that necessary from the Calcite side. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
