public ASTNode depthFirstSearch(ASTNode ast, int token) { searchQueue.clear(); searchQueue.add(ast); while (!searchQueue.isEmpty()) { ASTNode next = searchQueue.poll(); if (next.getType() == token) return next; for (int j = 0; j < next.getChildCount(); ++j) { searchQueue.add((ASTNode) next.getChild(j)); } } return null; }
private String buildTriggerActionExpression(ASTNode ast) throws SemanticException { switch (ast.getType()) { case HiveParser.KW_KILL: return "KILL"; case HiveParser.KW_MOVE: if (ast.getChildCount() != 1) { throw new SemanticException("Invalid move to clause in trigger action."); } String poolPath = poolPath(ast.getChild(0)); return "MOVE TO " + poolPath; default: throw new SemanticException("Unknown token in action clause: " + ast.getType()); } }
static QBSubQuery buildSubQuery(String outerQueryId, int sqIdx, ASTNode sqAST, ASTNode originalSQAST, Context ctx) throws SemanticException { ASTNode sqOp = (ASTNode) sqAST.getChild(0); ASTNode sq = (ASTNode) sqAST.getChild(1); ASTNode outerQueryExpr = (ASTNode) sqAST.getChild(2); /* * Restriction.8.m :: We allow only 1 SubQuery expression per Query. */ if (outerQueryExpr != null && outerQueryExpr.getType() == HiveParser.TOK_SUBQUERY_EXPR ) { throw new SemanticException(ErrorMsg.UNSUPPORTED_SUBQUERY_EXPRESSION.getMsg( originalSQAST.getChild(1), "Only 1 SubQuery expression is supported.")); } return new QBSubQuery(outerQueryId, sqIdx, sq, outerQueryExpr, buildSQOperator(sqOp), originalSQAST, ctx); }
private ResourceType getResourceType(ASTNode token) throws SemanticException { switch (token.getType()) { case HiveParser.TOK_JAR: return ResourceType.JAR; case HiveParser.TOK_FILE: return ResourceType.FILE; case HiveParser.TOK_ARCHIVE: return ResourceType.ARCHIVE; default: throw new SemanticException("Unexpected token " + token.toString()); } }
private ASTNode findFunctionNode(ASTNode n) { if (n.getType() == HiveParser.TOK_FUNCTION) { if ("internal_interval".equals(n.getChild(0).getText())) { return n; } } ArrayList<Node> children = n.getChildren(); if (children != null) { for (Node c : children) { ASTNode r = findFunctionNode((ASTNode) c); if (r != null) { return r; } } } return null; } }
private void visit(ASTNode n) { if (n.getType() == HiveParser.Identifier) { if (visitedNodes.containsKey(n)) { /** * Since we are modifying the stream, it's not idempotent. Ideally, the caller would take * care to only quote Identifiers in each subtree once, but this makes it safe */ return; } visitedNodes.put(n, n); trs.insertBefore(n.getToken(), "`"); trs.insertAfter(n.getToken(), "`"); } if (n.getChildCount() <= 0) { return; } for (Node c : n.getChildren()) { visit((ASTNode)c); } } }
private boolean isAliased(ASTNode n) { switch (n.getType()) { case HiveParser.TOK_TABREF: return findTabRefIdxs(n)[0] != 0; case HiveParser.TOK_TABNAME: return false; case HiveParser.TOK_SUBQUERY: assert n.getChildCount() > 1 : "Expected Derived Table to be aliased"; return true; default: throw raiseWrongType("TOK_TABREF|TOK_TABNAME", n); } }
/** * Collect WHEN clauses from Merge statement AST. */ private List<ASTNode> findWhenClauses(ASTNode tree, int start) throws SemanticException { assert tree.getType() == HiveParser.TOK_MERGE; List<ASTNode> whenClauses = new ArrayList<>(); for (int idx = start; idx < tree.getChildCount(); idx++) { ASTNode whenClause = (ASTNode)tree.getChild(idx); assert whenClause.getType() == HiveParser.TOK_MATCHED || whenClause.getType() == HiveParser.TOK_NOT_MATCHED : "Unexpected node type found: " + whenClause.getType() + addParseInfo(whenClause); whenClauses.add(whenClause); } if (whenClauses.size() <= 0) { //Futureproofing: the parser will actually not allow this throw new SemanticException("Must have at least 1 WHEN clause in MERGE statement"); } return whenClauses; }
public ASTNode simpleBreadthFirstSearchAny(ASTNode ast, int... tokens) { searchQueue.clear(); searchQueue.add(ast); while (!searchQueue.isEmpty()) { ASTNode next = searchQueue.poll(); for (int i = 0; i < tokens.length; ++i) { if (next.getType() == tokens[i]) return next; } for (int i = 0; i < next.getChildCount(); ++i) { searchQueue.add((ASTNode) next.getChild(i)); } } return null; }
static QBSubQuery buildSubQuery(String outerQueryId, int sqIdx, ASTNode sqAST, ASTNode originalSQAST, Context ctx) throws SemanticException { ASTNode sqOp = (ASTNode) sqAST.getChild(0); ASTNode sq = (ASTNode) sqAST.getChild(1); ASTNode outerQueryExpr = (ASTNode) sqAST.getChild(2); /* * Restriction.8.m :: We allow only 1 SubQuery expression per Query. */ if (outerQueryExpr != null && outerQueryExpr.getType() == HiveParser.TOK_SUBQUERY_EXPR ) { throw new SemanticException(ErrorMsg.UNSUPPORTED_SUBQUERY_EXPRESSION.getMsg( originalSQAST.getChild(1), "Only 1 SubQuery expression is supported.")); } return new QBSubQuery(outerQueryId, sqIdx, sq, outerQueryExpr, buildSQOperator(sqOp), originalSQAST, ctx); }
private ResourceType getResourceType(ASTNode token) throws SemanticException { switch (token.getType()) { case HiveParser.TOK_JAR: return ResourceType.JAR; case HiveParser.TOK_FILE: return ResourceType.FILE; case HiveParser.TOK_ARCHIVE: return ResourceType.ARCHIVE; default: throw new SemanticException("Unexpected token " + token.toString()); } }
private boolean isAliased(ASTNode n) { switch (n.getType()) { case HiveParser.TOK_TABREF: return findTabRefIdxs(n)[0] != 0; case HiveParser.TOK_TABNAME: return false; case HiveParser.TOK_SUBQUERY: assert n.getChildCount() > 1 : "Expected Derived Table to be aliased"; return true; default: throw raiseWrongType("TOK_TABREF|TOK_TABNAME", n); } } /**
private String buildTriggerExpression(ASTNode ast) throws SemanticException { if (ast.getType() != HiveParser.TOK_TRIGGER_EXPRESSION || ast.getChildCount() == 0) { throw new SemanticException("Invalid trigger expression."); } StringBuilder builder = new StringBuilder(); for (int i = 0; i < ast.getChildCount(); ++i) { builder.append(ast.getChild(i).getText()); // Don't strip quotes. builder.append(' '); } builder.deleteCharAt(builder.length() - 1); return builder.toString(); }
public ASTNode depthFirstSearch(ASTNode ast, int token) { searchQueue.clear(); searchQueue.add(ast); while (!searchQueue.isEmpty()) { ASTNode next = searchQueue.poll(); if (next.getType() == token) return next; for (int j = 0; j < next.getChildCount(); ++j) { searchQueue.add((ASTNode) next.getChild(j)); } } return null; }
private BoundarySpec processBoundary(int frameType, ASTNode node) throws SemanticException { BoundarySpec bs = new BoundarySpec(); int type = node.getType(); boolean hasAmt = true; ASTNode amtNode = (ASTNode) node.getChild(0); if ( amtNode.getType() == HiveParser.KW_UNBOUNDED) throw new SemanticException( "Window Frame Boundary Amount must be a positive integer, provided amount is: " + amt);
if (expr.getType() == HiveParser.TOK_DATELITERAL) { PrimitiveTypeInfo typeInfo = TypeInfoFactory.dateTypeInfo; return new ExprNodeConstantDesc(typeInfo, Date.valueOf(timeString)); if (expr.getType() == HiveParser.TOK_TIMESTAMPLITERAL) { return new ExprNodeConstantDesc(TypeInfoFactory.timestampTypeInfo, Timestamp.valueOf(timeString)); throw new IllegalArgumentException("Invalid time literal type " + expr.getType()); } catch (Exception err) { throw new SemanticException( "Unable to convert time literal '" + timeString + "' to time value.", err);
/** * Collect WHEN clauses from Merge statement AST */ private List<ASTNode> findWhenClauses(ASTNode tree) throws SemanticException { assert tree.getType() == HiveParser.TOK_MERGE; List<ASTNode> whenClauses = new ArrayList<>(); for(int idx = 3; idx < tree.getChildCount(); idx++) { ASTNode whenClause = (ASTNode)tree.getChild(idx); assert whenClause.getType() == HiveParser.TOK_MATCHED || whenClause.getType() == HiveParser.TOK_NOT_MATCHED : "Unexpected node type found: " + whenClause.getType() + addParseInfo(whenClause); whenClauses.add(whenClause); } if(whenClauses.size() <= 0) { //Futureproofing: the parser will actually not allow this throw new SemanticException("Must have at least 1 WHEN clause in MERGE statement"); } return whenClauses; } private ASTNode getWhenClauseOperation(ASTNode whenClause) {