Search in sources :

Example 1 with ExprNodeSubQueryDesc

use of org.apache.hadoop.hive.ql.plan.ExprNodeSubQueryDesc 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)

Aggregations

RelNode (org.apache.calcite.rel.RelNode)1 CalciteSubquerySemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSubquerySemanticException)1 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)1 QBSubQueryParseInfo (org.apache.hadoop.hive.ql.parse.QBSubQueryParseInfo)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