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);
}
}
}
Aggregations