/** * Performs three final parts of the analysis: 1. Checks type compatibility between all * expressions and their targets * * <p>2. Populates partitionKeyExprs with type-compatible expressions, in Hive partition-column * order, for all partition columns * * <p>3. Populates resultExprs_ with type-compatible expressions, in Hive column order, for all * expressions in the select-list. Unmentioned columns are assigned NULL literal expressions. * * <p>If necessary, adds casts to the expressions to make them compatible with the type of the * corresponding column. * * @throws AnalysisException If an expression is not compatible with its target column */ private void prepareExpressions( List<Column> selectExprTargetColumns, List<Expr> selectListExprs, Table tbl, Analyzer analyzer) throws AnalysisException { // Temporary lists of partition key exprs and names in an arbitrary order. List<Expr> tmpPartitionKeyExprs = new ArrayList<Expr>(); List<String> tmpPartitionKeyNames = new ArrayList<String>(); int numClusteringCols = (tbl instanceof HBaseTable) ? 0 : tbl.getNumClusteringCols(); // Check dynamic partition columns for type compatibility. for (int i = 0; i < selectListExprs.size(); ++i) { Column targetColumn = selectExprTargetColumns.get(i); Expr compatibleExpr = checkTypeCompatibility(targetColumn, selectListExprs.get(i)); if (targetColumn.getPosition() < numClusteringCols) { // This is a dynamic clustering column tmpPartitionKeyExprs.add(compatibleExpr); tmpPartitionKeyNames.add(targetColumn.getName()); } selectListExprs.set(i, compatibleExpr); } // Check static partition columns, dynamic entries in partitionKeyValues will already // be in selectExprTargetColumns and therefore are ignored in this loop if (partitionKeyValues_ != null) { for (PartitionKeyValue pkv : partitionKeyValues_) { if (pkv.isStatic()) { // tableColumns is guaranteed to exist after the earlier analysis checks Column tableColumn = table_.getColumn(pkv.getColName()); Expr compatibleExpr = checkTypeCompatibility(tableColumn, pkv.getValue()); tmpPartitionKeyExprs.add(compatibleExpr); tmpPartitionKeyNames.add(pkv.getColName()); } } } // Reorder the partition key exprs and names to be consistent with the target table // declaration. We need those exprs in the original order to create the corresponding // Hdfs folder structure correctly. for (Column c : table_.getColumns()) { for (int j = 0; j < tmpPartitionKeyNames.size(); ++j) { if (c.getName().equals(tmpPartitionKeyNames.get(j))) { partitionKeyExprs_.add(tmpPartitionKeyExprs.get(j)); break; } } } Preconditions.checkState(partitionKeyExprs_.size() == numClusteringCols); // Make sure we have stats for partitionKeyExprs for (Expr expr : partitionKeyExprs_) { expr.analyze(analyzer); } // Finally, 'undo' the permutation so that the selectListExprs are in Hive column // order, and add NULL expressions to all missing columns. for (Column tblColumn : table_.getColumnsInHiveOrder()) { boolean matchFound = false; for (int i = 0; i < selectListExprs.size(); ++i) { if (selectExprTargetColumns.get(i).getName().equals(tblColumn.getName())) { resultExprs_.add(selectListExprs.get(i)); matchFound = true; break; } } // If no match is found, either the column is a clustering column with a static // value, or it was unmentioned and therefore should have a NULL select-list // expression. if (!matchFound) { if (tblColumn.getPosition() >= numClusteringCols) { // Unmentioned non-clustering columns get NULL literals with the appropriate // target type because Parquet cannot handle NULL_TYPE (IMPALA-617). resultExprs_.add(NullLiteral.create(tblColumn.getType())); } } } // TODO: Check that HBase row-key columns are not NULL? See IMPALA-406 if (needsGeneratedQueryStatement_) { // Build a query statement that returns NULL for every column List<SelectListItem> selectListItems = Lists.newArrayList(); for (Expr e : resultExprs_) { selectListItems.add(new SelectListItem(e, null)); } SelectList selectList = new SelectList(selectListItems); queryStmt_ = new SelectStmt(selectList, null, null, null, null, null, null); queryStmt_.analyze(analyzer); } }
private void analyze() throws AnalysisException { targetType_.analyze(); if (targetType_.isComplexType()) { throw new AnalysisException("Unsupported cast to complex type: " + targetType_.toSql()); } boolean readyForCharCast = children_.get(0).getType().getPrimitiveType() == PrimitiveType.STRING || children_.get(0).getType().getPrimitiveType() == PrimitiveType.CHAR; if (targetType_.getPrimitiveType() == PrimitiveType.CHAR && !readyForCharCast) { // Back end functions only exist to cast string types to CHAR, there is not a cast // for every type since it is redundant with STRING. Casts to go through 2 casts: // (1) cast to string, to stringify the value // (2) cast to CHAR, to truncate or pad with spaces CastExpr tostring = new CastExpr(ScalarType.STRING, children_.get(0), true); tostring.analyze(); children_.set(0, tostring); } if (children_.get(0) instanceof NumericLiteral && targetType_.isFloatingPointType()) { // Special case casting a decimal literal to a floating point number. The // decimal literal can be interpreted as either and we want to avoid casts // since that can result in loss of accuracy. ((NumericLiteral) children_.get(0)).explicitlyCastToFloat(targetType_); } if (children_.get(0).getType().isNull()) { // Make sure BE never sees TYPE_NULL uncheckedCastChild(targetType_, 0); } // Ensure child has non-null type (even if it's a null literal). This is required // for the UDF interface. if (children_.get(0) instanceof NullLiteral) { NullLiteral nullChild = (NullLiteral) (children_.get(0)); nullChild.uncheckedCastTo(targetType_); } Type childType = children_.get(0).type_; Preconditions.checkState(!childType.isNull()); if (childType.equals(targetType_)) { noOp_ = true; type_ = targetType_; return; } FunctionName fnName = new FunctionName(Catalog.BUILTINS_DB, getFnName(targetType_)); Type[] args = {childType}; Function searchDesc = new Function(fnName, args, Type.INVALID, false); if (isImplicit_) { fn_ = Catalog.getBuiltin(searchDesc, CompareMode.IS_SUPERTYPE_OF); Preconditions.checkState(fn_ != null); } else { fn_ = Catalog.getBuiltin(searchDesc, CompareMode.IS_IDENTICAL); if (fn_ == null) { // allow for promotion from CHAR to STRING; only if no exact match is found fn_ = Catalog.getBuiltin(searchDesc.promoteCharsToStrings(), CompareMode.IS_IDENTICAL); } } if (fn_ == null) { throw new AnalysisException( "Invalid type cast of " + getChild(0).toSql() + " from " + childType + " to " + targetType_); } Preconditions.checkState( targetType_.matchesType(fn_.getReturnType()), targetType_ + " != " + fn_.getReturnType()); type_ = targetType_; }