gortiz commented on code in PR #12475: URL: https://github.com/apache/pinot/pull/12475#discussion_r1507194978
########## pinot-query-planner/src/main/java/org/apache/pinot/query/validate/BytesCastVisitor.java: ########## @@ -0,0 +1,73 @@ +/** + * 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.pinot.query.validate; + +import com.google.common.base.Preconditions; +import java.util.List; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlCharStringLiteral; +import org.apache.calcite.sql.SqlDataTypeSpec; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.fun.SqlCastFunction; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.SqlTypeUtil; +import org.apache.calcite.sql.util.SqlBasicVisitor; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.util.Static; + + +public class BytesCastVisitor extends SqlBasicVisitor<Void> { + + private final SqlValidator _originalValidator; + + public BytesCastVisitor(SqlValidator originalValidator) { + _originalValidator = originalValidator; + } + + @SuppressWarnings("checkstyle:WhitespaceAround") + @Override + public Void visit(SqlCall call) { + if (call.getOperator() instanceof SqlCastFunction) { + List<SqlNode> operands = call.getOperandList(); + SqlNode sqlNode = operands.get(1); + Preconditions.checkState(sqlNode instanceof SqlDataTypeSpec); + RelDataType toType = ((SqlDataTypeSpec) sqlNode).deriveType(_originalValidator); + if (!SqlTypeUtil.isBinary(toType)) { + return super.visit(call); + } + SqlNode srcNode = operands.get(0); + RelDataType fromType = _originalValidator.getValidatedNodeTypeIfKnown(srcNode); + if (fromType != null && SqlTypeUtil.isBinary(fromType)) { + return super.visit(call); + } + String message = "Cannot cast " + srcNode + " as " + toType + "."; + if (srcNode instanceof SqlCharStringLiteral) { + message += " Try to use binary literal instead (like x" + srcNode + ")"; + } else if (fromType != null && SqlTypeUtil.isCharacter(fromType)) { + message += " Try to wrap the expression in hexToBytes (like hexToBytes(" + srcNode + "))"; + } + SqlParserPos pos = call.getParserPosition(); + RuntimeException ex = new RuntimeException(message); Review Comment: Changed to throw a new runtime exception called `InvalidCastException` -- 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. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org