Search in sources :

Example 1 with UnparseTranslator

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

the class ConstraintsUtils method validateCheckConstraint.

public static void validateCheckConstraint(List<FieldSchema> columns, List<SQLCheckConstraint> checkConstraints, Configuration conf) throws SemanticException {
    // create colinfo and then row resolver
    RowResolver rr = new RowResolver();
    for (FieldSchema column : columns) {
        ColumnInfo ci = new ColumnInfo(column.getName(), TypeInfoUtils.getTypeInfoFromTypeString(column.getType()), null, false);
        rr.put(null, column.getName(), ci);
    }
    TypeCheckCtx typeCheckCtx = new TypeCheckCtx(rr);
    // TypeCheckProcFactor expects typecheckctx to have unparse translator
    UnparseTranslator unparseTranslator = new UnparseTranslator(conf);
    typeCheckCtx.setUnparseTranslator(unparseTranslator);
    for (SQLCheckConstraint cc : checkConstraints) {
        try {
            ParseDriver parseDriver = new ParseDriver();
            ASTNode checkExprAST = parseDriver.parseExpression(cc.getCheck_expression());
            validateCheckExprAST(checkExprAST);
            Map<ASTNode, ExprNodeDesc> genExprs = ExprNodeTypeCheck.genExprNode(checkExprAST, typeCheckCtx);
            ExprNodeDesc checkExpr = genExprs.get(checkExprAST);
            if (checkExpr == null) {
                throw new SemanticException(ErrorMsg.INVALID_CSTR_SYNTAX.getMsg("Invalid type for CHECK constraint: ") + cc.getCheck_expression());
            }
            if (checkExpr.getTypeInfo().getTypeName() != serdeConstants.BOOLEAN_TYPE_NAME) {
                throw new SemanticException(ErrorMsg.INVALID_CSTR_SYNTAX.getMsg("Only boolean type is supported for CHECK constraint: ") + cc.getCheck_expression() + ". Found: " + checkExpr.getTypeInfo().getTypeName());
            }
            validateCheckExpr(checkExpr);
        } catch (Exception e) {
            throw new SemanticException(ErrorMsg.INVALID_CSTR_SYNTAX.getMsg("Invalid CHECK constraint expression: ") + cc.getCheck_expression() + ". " + e.getMessage(), e);
        }
    }
}
Also used : TypeCheckCtx(org.apache.hadoop.hive.ql.parse.type.TypeCheckCtx) SQLCheckConstraint(org.apache.hadoop.hive.metastore.api.SQLCheckConstraint) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) ParseDriver(org.apache.hadoop.hive.ql.parse.ParseDriver) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) ColumnInfo(org.apache.hadoop.hive.ql.exec.ColumnInfo) RowResolver(org.apache.hadoop.hive.ql.parse.RowResolver) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) UnparseTranslator(org.apache.hadoop.hive.ql.parse.UnparseTranslator) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Aggregations

FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)1 SQLCheckConstraint (org.apache.hadoop.hive.metastore.api.SQLCheckConstraint)1 ColumnInfo (org.apache.hadoop.hive.ql.exec.ColumnInfo)1 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)1 ParseDriver (org.apache.hadoop.hive.ql.parse.ParseDriver)1 RowResolver (org.apache.hadoop.hive.ql.parse.RowResolver)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1 UnparseTranslator (org.apache.hadoop.hive.ql.parse.UnparseTranslator)1 TypeCheckCtx (org.apache.hadoop.hive.ql.parse.type.TypeCheckCtx)1 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)1