Search in sources :

Example 1 with QBSubQueryParseInfo

use of org.apache.hadoop.hive.ql.parse.QBSubQueryParseInfo in project hive by apache.

the class ExprNodeDescExprFactory method createSubqueryExpr.

/**
 * {@inheritDoc}
 */
@Override
protected ExprNodeDesc createSubqueryExpr(TypeCheckCtx ctx, ASTNode expr, SubqueryType subqueryType, Object[] inputs) throws CalciteSubquerySemanticException {
    // subqueryToRelNode might be null if subquery expression anywhere other than
    // as expected in filter (where/having). We should throw an appropriate error
    // message
    Map<ASTNode, QBSubQueryParseInfo> subqueryToRelNode = ctx.getSubqueryToRelNode();
    if (subqueryToRelNode == null) {
        throw new CalciteSubquerySemanticException(ErrorMsg.UNSUPPORTED_SUBQUERY_EXPRESSION.getMsg(" Currently SubQuery expressions are only allowed as " + "Where and Having Clause predicates"));
    }
    ASTNode subqueryOp = (ASTNode) expr.getChild(0);
    RelNode subqueryRel = subqueryToRelNode.get(expr).getSubQueryRelNode();
    // we will create subquery expression of boolean type
    switch(subqueryType) {
        case EXISTS:
            {
                if (subqueryToRelNode.get(expr).hasFullAggregate()) {
                    return createConstantExpr(TypeInfoFactory.booleanTypeInfo, true);
                }
                return new ExprNodeSubQueryDesc(TypeInfoFactory.booleanTypeInfo, subqueryRel, SubqueryType.EXISTS);
            }
        case IN:
            {
                assert (inputs[2] != null);
                ExprNodeDesc lhs = (ExprNodeDesc) inputs[2];
                return new ExprNodeSubQueryDesc(TypeInfoFactory.booleanTypeInfo, subqueryRel, SubqueryType.IN, lhs);
            }
        case SCALAR:
            {
                // only single subquery expr is supported
                if (subqueryRel.getRowType().getFieldCount() != 1) {
                    throw new CalciteSubquerySemanticException(ErrorMsg.INVALID_SUBQUERY_EXPRESSION.getMsg("More than one column expression in subquery"));
                }
                // figure out subquery expression column's type
                TypeInfo subExprType = TypeConverter.convert(subqueryRel.getRowType().getFieldList().get(0).getType());
                return new ExprNodeSubQueryDesc(subExprType, subqueryRel, SubqueryType.SCALAR);
            }
        case SOME:
            {
                assert (inputs[2] != null);
                ExprNodeDesc lhs = (ExprNodeDesc) inputs[2];
                return new ExprNodeSubQueryDesc(TypeInfoFactory.booleanTypeInfo, subqueryRel, SubqueryType.SOME, lhs, (ASTNode) subqueryOp.getChild(1));
            }
        case ALL:
            {
                assert (inputs[2] != null);
                ExprNodeDesc lhs = (ExprNodeDesc) inputs[2];
                return new ExprNodeSubQueryDesc(TypeInfoFactory.booleanTypeInfo, subqueryRel, SubqueryType.ALL, lhs, (ASTNode) subqueryOp.getChild(1));
            }
        default:
            return null;
    }
}
Also used : ExprNodeSubQueryDesc(org.apache.hadoop.hive.ql.plan.ExprNodeSubQueryDesc) RelNode(org.apache.calcite.rel.RelNode) QBSubQueryParseInfo(org.apache.hadoop.hive.ql.parse.QBSubQueryParseInfo) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) CalciteSubquerySemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSubquerySemanticException) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)

Example 2 with QBSubQueryParseInfo

use of org.apache.hadoop.hive.ql.parse.QBSubQueryParseInfo in project hive by apache.

the class RexNodeExprFactory method createSubqueryExpr.

/**
 * {@inheritDoc}
 */
@Override
protected RexNode createSubqueryExpr(TypeCheckCtx ctx, ASTNode expr, SubqueryType subqueryType, Object[] inputs) throws SemanticException {
    // subqueryToRelNode might be null if subquery expression anywhere other than
    // as expected in filter (where/having). We should throw an appropriate error
    // message
    Map<ASTNode, QBSubQueryParseInfo> subqueryToRelNode = ctx.getSubqueryToRelNode();
    if (subqueryToRelNode == null) {
        throw new CalciteSubquerySemanticException(ErrorMsg.UNSUPPORTED_SUBQUERY_EXPRESSION.getMsg(" Currently SubQuery expressions are only allowed as " + "Where and Having Clause predicates"));
    }
    ASTNode subqueryOp = (ASTNode) expr.getChild(0);
    RelNode subqueryRel = subqueryToRelNode.get(expr).getSubQueryRelNode();
    // we will create subquery expression of boolean type
    switch(subqueryType) {
        case EXISTS:
            {
                if (subqueryToRelNode.get(expr).hasFullAggregate()) {
                    return createConstantExpr(TypeInfoFactory.booleanTypeInfo, true);
                }
                return RexSubQuery.exists(subqueryRel);
            }
        case IN:
            {
                assert (inputs[2] != null);
                /*
         * Check.5.h :: For In and Not In the SubQuery must implicitly or
         * explicitly only contain one select item.
         */
                if (subqueryRel.getRowType().getFieldCount() > 1) {
                    throw new CalciteSubquerySemanticException(ErrorMsg.INVALID_SUBQUERY_EXPRESSION.getMsg("SubQuery can contain only 1 item in Select List."));
                }
                // create RexNode for LHS
                RexNode lhs = (RexNode) inputs[2];
                // create RexSubQuery node
                return RexSubQuery.in(subqueryRel, ImmutableList.<RexNode>of(lhs));
            }
        case SCALAR:
            {
                // only single subquery expr is supported
                if (subqueryRel.getRowType().getFieldCount() != 1) {
                    throw new CalciteSubquerySemanticException(ErrorMsg.INVALID_SUBQUERY_EXPRESSION.getMsg("More than one column expression in subquery"));
                }
                if (subqueryRel.getRowType().getFieldCount() > 1) {
                    throw new CalciteSubquerySemanticException(ErrorMsg.INVALID_SUBQUERY_EXPRESSION.getMsg("SubQuery can contain only 1 item in Select List."));
                }
                // create RexSubQuery node
                return RexSubQuery.scalar(subqueryRel);
            }
        case SOME:
        case ALL:
            {
                assert (inputs[2] != null);
                // create RexNode for LHS
                RexNode lhs = (RexNode) inputs[2];
                return convertSubquerySomeAll(subqueryRel.getCluster(), (ASTNode) subqueryOp.getChild(1), subqueryType, subqueryRel, lhs);
            }
        default:
            return null;
    }
}
Also used : RelNode(org.apache.calcite.rel.RelNode) QBSubQueryParseInfo(org.apache.hadoop.hive.ql.parse.QBSubQueryParseInfo) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) CalciteSubquerySemanticException(org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSubquerySemanticException) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)2 CalciteSubquerySemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSubquerySemanticException)2 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)2 QBSubQueryParseInfo (org.apache.hadoop.hive.ql.parse.QBSubQueryParseInfo)2 RexNode (org.apache.calcite.rex.RexNode)1 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)1 ExprNodeSubQueryDesc (org.apache.hadoop.hive.ql.plan.ExprNodeSubQueryDesc)1 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)1 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)1 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)1 StructTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo)1 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)1 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)1