private CTEClause findCTEFromName(QB qb, String cteName) { StringBuilder qId = new StringBuilder(); if (qb.getId() != null) { qId.append(qb.getId()); } while (qId.length() > 0) { String nm = qId + ":" + cteName; CTEClause cte = aliasToCTEs.get(nm); if (cte != null) { return cte; } int lastIndex = qId.lastIndexOf(":"); lastIndex = lastIndex < 0 ? 0 : lastIndex; qId.setLength(lastIndex); } return aliasToCTEs.get(cteName); }
private CTEClause findCTEFromName(QB qb, String cteName) { StringBuilder qId = new StringBuilder(); if (qb.getId() != null) { qId.append(qb.getId()); } while (qId.length() > 0) { String nm = qId + ":" + cteName; CTEClause cte = aliasToCTEs.get(nm); if (cte != null) { return cte; } int lastIndex = qId.lastIndexOf(":"); lastIndex = lastIndex < 0 ? 0 : lastIndex; qId.setLength(lastIndex); } return aliasToCTEs.get(cteName); }
private String getModifiedAlias(QB qb, String alias) { return QB.getAppendedAliasFromId(qb.getId(), alias); }
private String getModifiedAlias(QB qb, String alias) { return QB.getAppendedAliasFromId(qb.getId(), alias); }
private void processCTE(QB qb, ASTNode ctes) throws SemanticException { int numCTEs = ctes.getChildCount(); for(int i=0; i <numCTEs; i++) { ASTNode cte = (ASTNode) ctes.getChild(i); ASTNode cteQry = (ASTNode) cte.getChild(0); String alias = unescapeIdentifier(cte.getChild(1).getText()); String qName = qb.getId() == null ? "" : qb.getId() + ":"; qName += alias.toLowerCase(); if ( aliasToCTEs.containsKey(qName)) { throw new SemanticException(ErrorMsg.AMBIGUOUS_TABLE_ALIAS.getMsg(cte.getChild(1))); } aliasToCTEs.put(qName, new CTEClause(qName, cteQry)); } }
private void processCTE(QB qb, ASTNode ctes) throws SemanticException { int numCTEs = ctes.getChildCount(); for(int i=0; i <numCTEs; i++) { ASTNode cte = (ASTNode) ctes.getChild(i); ASTNode cteQry = (ASTNode) cte.getChild(0); String alias = unescapeIdentifier(cte.getChild(1).getText()); String qName = qb.getId() == null ? "" : qb.getId() + ":"; qName += alias.toLowerCase(); if ( aliasToCTEs.containsKey(qName)) { throw new SemanticException(ErrorMsg.AMBIGUOUS_TABLE_ALIAS.getMsg(cte.getChild(1))); } aliasToCTEs.put(qName, new CTEClause(qName, cteQry)); } }
private void addCTEAsSubQuery(QB qb, String cteName, String cteAlias) throws SemanticException { cteAlias = cteAlias == null ? cteName : cteAlias; CTEClause cte = findCTEFromName(qb, cteName); ASTNode cteQryNode = cte.cteNode; QBExpr cteQBExpr = new QBExpr(cteAlias); doPhase1QBExpr(cteQryNode, cteQBExpr, qb.getId(), cteAlias); qb.rewriteCTEToSubq(cteAlias, cteName, cteQBExpr); }
private void addCTEAsSubQuery(QB qb, String cteName, String cteAlias) throws SemanticException { cteAlias = cteAlias == null ? cteName : cteAlias; CTEClause cte = findCTEFromName(qb, cteName); ASTNode cteQryNode = cte.cteNode; QBExpr cteQBExpr = new QBExpr(cteAlias); doPhase1QBExpr(cteQryNode, cteQBExpr, qb.getId(), cteAlias); qb.rewriteCTEToSubq(cteAlias, cteName, cteQBExpr); }
private void gatherCTEReferences(QB qb, CTEClause current) throws HiveException { for (String alias : qb.getTabAliases()) { String tabName = qb.getTabNameForAlias(alias); String cteName = tabName.toLowerCase(); CTEClause cte = findCTEFromName(qb, cteName); if (cte != null) { if (ctesExpanded.contains(cteName)) { throw new SemanticException("Recursive cte " + cteName + " detected (cycle: " + StringUtils.join(ctesExpanded, " -> ") + " -> " + cteName + ")."); } cte.reference++; current.parents.add(cte); if (cte.qbExpr != null) { continue; } cte.qbExpr = new QBExpr(cteName); doPhase1QBExpr(cte.cteNode, cte.qbExpr, qb.getId(), cteName); ctesExpanded.add(cteName); gatherCTEReferences(cte.qbExpr, cte); ctesExpanded.remove(ctesExpanded.size() - 1); } } for (String alias : qb.getSubqAliases()) { gatherCTEReferences(qb.getSubqForAlias(alias), current); } }
private void gatherCTEReferences(QB qb, CTEClause current) throws HiveException { for (String alias : qb.getTabAliases()) { String tabName = qb.getTabNameForAlias(alias); String cteName = tabName.toLowerCase(); CTEClause cte = findCTEFromName(qb, cteName); if (cte != null) { if (ctesExpanded.contains(cteName)) { throw new SemanticException("Recursive cte " + cteName + " detected (cycle: " + StringUtils.join(ctesExpanded, " -> ") + " -> " + cteName + ")."); } cte.reference++; current.parents.add(cte); if (cte.qbExpr != null) { continue; } cte.qbExpr = new QBExpr(cteName); doPhase1QBExpr(cte.cteNode, cte.qbExpr, qb.getId(), cteName); ctesExpanded.add(cteName); gatherCTEReferences(cte.qbExpr, cte); ctesExpanded.remove(ctesExpanded.size() - 1); } } for (String alias : qb.getSubqAliases()) { gatherCTEReferences(qb.getSubqForAlias(alias), current); } }
QBSubQuery subQuery = SubQueryUtils.buildSubQuery(qb.getId(), sqIdx, subQueryAST, originalSubQueryAST, ctx);
QB qbSQ = new QB(qb.getId(), sbQueryAlias, true); qbSQ.setInsideView(qb.isInsideView()); Phase1Ctx ctx1 = initPhase1Ctx();
void addRewrites(TokenRewriteStream stream, QB qb, String program, PrintStream out) { QBSubQuery sqW = qb.getWhereClauseSubQueryPredicate(); QBSubQuery sqH = qb.getHavingClauseSubQueryPredicate(); if (sqW != null || sqH != null) { ASTNode sqNode = sqW != null ? sqW.getOriginalSubQueryASTForRewrite() : sqH.getOriginalSubQueryASTForRewrite(); ASTNode tokQry = getQueryASTNode(sqNode); ASTNode tokFrom = (ASTNode) tokQry.getChild(0); StringBuilder addedJoins = new StringBuilder(); if (sqW != null) { addRewrites(stream, sqW, program, out, qb.getId(), true, addedJoins); } if (sqH != null) { addRewrites(stream, sqH, program, out, qb.getId(), false, addedJoins); } stream.insertAfter(program, tokFrom.getTokenStopIndex(), addedJoins); } Set<String> sqAliases = qb.getSubqAliases(); for(String sqAlias : sqAliases) { addRewrites(stream, qb.getSubqForAlias(sqAlias).getQB(), program, out); } }
LOG.debug("Created Filter Plan for " + qb.getId() + " row schema: " + inputRR.toString());
void addRewrites(TokenRewriteStream stream, QB qb, String program, PrintStream out) { QBSubQuery sqW = qb.getWhereClauseSubQueryPredicate(); QBSubQuery sqH = qb.getHavingClauseSubQueryPredicate(); if (sqW != null || sqH != null) { ASTNode sqNode = sqW != null ? sqW.getOriginalSubQueryASTForRewrite() : sqH.getOriginalSubQueryASTForRewrite(); ASTNode tokQry = getQueryASTNode(sqNode); ASTNode tokFrom = (ASTNode) tokQry.getChild(0); StringBuilder addedJoins = new StringBuilder(); if (sqW != null) { addRewrites(stream, sqW, program, out, qb.getId(), true, addedJoins); } if (sqH != null) { addRewrites(stream, sqH, program, out, qb.getId(), false, addedJoins); } stream.insertAfter(program, tokFrom.getTokenStopIndex(), addedJoins); } Set<String> sqAliases = qb.getSubqAliases(); for(String sqAlias : sqAliases) { addRewrites(stream, qb.getSubqForAlias(sqAlias).getQB(), program, out); } }
doPhase1QBExpr(viewTree, qbexpr, qb.getId(), alias, true);
private String processSubQuery(QB qb, ASTNode subq) throws SemanticException { // This is a subquery and must have an alias if (subq.getChildCount() != 2) { throw new SemanticException(ErrorMsg.NO_SUBQUERY_ALIAS.getMsg(subq)); } ASTNode subqref = (ASTNode) subq.getChild(0); String alias = unescapeIdentifier(subq.getChild(1).getText()); // Recursively do the first phase of semantic analysis for the subquery QBExpr qbexpr = new QBExpr(alias); doPhase1QBExpr(subqref, qbexpr, qb.getId(), alias, qb.isInsideView()); // If the alias is already there then we have a conflict if (qb.exists(alias)) { throw new SemanticException(ErrorMsg.AMBIGUOUS_TABLE_ALIAS.getMsg(subq .getChild(1))); } // Insert this map into the stats qb.setSubqAlias(alias, qbexpr); qb.addAlias(alias); unparseTranslator.addIdentifierTranslation((ASTNode) subq.getChild(1)); return alias; }
private String processSubQuery(QB qb, ASTNode subq) throws SemanticException { // This is a subquery and must have an alias if (subq.getChildCount() != 2) { throw new SemanticException(ErrorMsg.NO_SUBQUERY_ALIAS.getMsg(subq)); } ASTNode subqref = (ASTNode) subq.getChild(0); String alias = unescapeIdentifier(subq.getChild(1).getText()); // Recursively do the first phase of semantic analysis for the subquery QBExpr qbexpr = new QBExpr(alias); doPhase1QBExpr(subqref, qbexpr, qb.getId(), alias, qb.isInsideView()); // If the alias is already there then we have a conflict if (qb.exists(alias)) { throw new SemanticException(ErrorMsg.AMBIGUOUS_TABLE_ALIAS.getMsg(subq .getChild(1))); } // Insert this map into the stats qb.setSubqAlias(alias, qbexpr); qb.addAlias(alias); unparseTranslator.addIdentifierTranslation((ASTNode) subq.getChild(1)); return alias; }