private String extractJoinAlias(ASTNode node, String tableName) { // ptf node form is: // ^(TOK_PTBLFUNCTION $name $alias? partitionTableFunctionSource partitioningSpec? expression*) // guaranteed to have an alias here: check done in processJoin if (node.getType() == HiveParser.TOK_PTBLFUNCTION) { return unescapeIdentifier(node.getChild(1).getText().toLowerCase()); } if (node.getChildCount() == 1) { return tableName; } for (int i = node.getChildCount() - 1; i >= 1; i--) { if (node.getChild(i).getType() == HiveParser.Identifier) { return unescapeIdentifier(node.getChild(i).getText().toLowerCase()); } } return tableName; }
private String extractJoinAlias(ASTNode node, String tableName) { // ptf node form is: // ^(TOK_PTBLFUNCTION $name $alias? partitionTableFunctionSource partitioningSpec? expression*) // guaranteed to have an alias here: check done in processJoin if (node.getType() == HiveParser.TOK_PTBLFUNCTION) { return unescapeIdentifier(node.getChild(1).getText().toLowerCase()); } if (node.getChildCount() == 1) { return tableName; } for (int i = node.getChildCount() - 1; i >= 1; i--) { if (node.getChild(i).getType() == HiveParser.Identifier) { return unescapeIdentifier(node.getChild(i).getText().toLowerCase()); } } return tableName; }
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)); } }
String findSimpleTableName(ASTNode tabref, int aliasIndex) throws SemanticException { assert tabref.getType() == HiveParser.TOK_TABREF; ASTNode tableTree = (ASTNode) (tabref.getChild(0)); String alias; if (aliasIndex != 0) { alias = unescapeIdentifier(tabref.getChild(aliasIndex).getText()); } else { alias = getUnescapedUnqualifiedTableName(tableTree); } return alias; } /**
@SuppressWarnings("rawtypes") private String findAlias(ASTNode columnRef, Map<String, Operator> aliasToOpInfo) throws SemanticException { String colName = unescapeIdentifier(columnRef.getChild(0).getText() .toLowerCase()); String tabAlias = null; if ( aliasToOpInfo != null ) { for (Map.Entry<String, Operator> opEntry : aliasToOpInfo.entrySet()) { Operator op = opEntry.getValue(); RowResolver rr = opParseCtx.get(op).getRowResolver(); ColumnInfo colInfo = rr.get(null, colName); if (colInfo != null) { if (tabAlias == null) { tabAlias = opEntry.getKey(); } else { throw new SemanticException( ErrorMsg.AMBIGUOUS_TABLE_ALIAS.getMsg(columnRef.getChild(0))); } } } } if ( tabAlias == null ) { throw new SemanticException(ErrorMsg.INVALID_TABLE_ALIAS.getMsg(columnRef .getChild(0))); } return tabAlias; }
String findSimpleTableName(ASTNode tabref, int aliasIndex) { assert tabref.getType() == HiveParser.TOK_TABREF; ASTNode tableTree = (ASTNode) (tabref.getChild(0)); String alias; if (aliasIndex != 0) { alias = unescapeIdentifier(tabref.getChild(aliasIndex).getText()); } else { alias = getUnescapedUnqualifiedTableName(tableTree); } return 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 boolean containsLeadLagUDF(ASTNode expressionTree) { int exprTokenType = expressionTree.getToken().getType(); if (exprTokenType == HiveParser.TOK_FUNCTION) { assert (expressionTree.getChildCount() != 0); if (expressionTree.getChild(0).getType() == HiveParser.Identifier) { String functionName = unescapeIdentifier(expressionTree.getChild(0) .getText()); functionName = functionName.toLowerCase(); if ( FunctionRegistry.LAG_FUNC_NAME.equals(functionName) || FunctionRegistry.LEAD_FUNC_NAME.equals(functionName) ) { return true; } } } for (int i = 0; i < expressionTree.getChildCount(); i++) { if ( containsLeadLagUDF((ASTNode) expressionTree.getChild(i))) { return true; } } return false; }
private boolean containsLeadLagUDF(ASTNode expressionTree) { int exprTokenType = expressionTree.getToken().getType(); if (exprTokenType == HiveParser.TOK_FUNCTION) { assert (expressionTree.getChildCount() != 0); if (expressionTree.getChild(0).getType() == HiveParser.Identifier) { String functionName = unescapeIdentifier(expressionTree.getChild(0) .getText()); functionName = functionName.toLowerCase(); if ( FunctionRegistry.LAG_FUNC_NAME.equals(functionName) || FunctionRegistry.LEAD_FUNC_NAME.equals(functionName) ) { return true; } } } for (int i = 0; i < expressionTree.getChildCount(); i++) { if ( containsLeadLagUDF((ASTNode) expressionTree.getChild(i))) { return true; } } return false; }
private static void getTableAliasesInSubQuery(ASTNode joinNode, List<String> aliases) throws SemanticException { if ((joinNode.getToken().getType() == HiveParser.TOK_TABREF) || (joinNode.getToken().getType() == HiveParser.TOK_SUBQUERY) || (joinNode.getToken().getType() == HiveParser.TOK_PTBLFUNCTION)) { String tableName = SemanticAnalyzer.getUnescapedUnqualifiedTableName((ASTNode) joinNode.getChild(0)) .toLowerCase(); String alias = joinNode.getChildCount() == 1 ? tableName : SemanticAnalyzer.unescapeIdentifier(joinNode.getChild(joinNode.getChildCount() - 1) .getText().toLowerCase()); alias = (joinNode.getToken().getType() == HiveParser.TOK_PTBLFUNCTION) ? SemanticAnalyzer.unescapeIdentifier(joinNode.getChild(1).getText().toLowerCase()) : alias; aliases.add(alias); } else { ASTNode left = (ASTNode) joinNode.getChild(0); ASTNode right = (ASTNode) joinNode.getChild(1); getTableAliasesInSubQuery(left, aliases); getTableAliasesInSubQuery(right, aliases); } }
private static void getTableAliasesInSubQuery(ASTNode joinNode, List<String> aliases) { if ((joinNode.getToken().getType() == HiveParser.TOK_TABREF) || (joinNode.getToken().getType() == HiveParser.TOK_SUBQUERY) || (joinNode.getToken().getType() == HiveParser.TOK_PTBLFUNCTION)) { String tableName = SemanticAnalyzer.getUnescapedUnqualifiedTableName((ASTNode) joinNode.getChild(0)) .toLowerCase(); String alias = joinNode.getChildCount() == 1 ? tableName : SemanticAnalyzer.unescapeIdentifier(joinNode.getChild(joinNode.getChildCount() - 1) .getText().toLowerCase()); alias = (joinNode.getToken().getType() == HiveParser.TOK_PTBLFUNCTION) ? SemanticAnalyzer.unescapeIdentifier(joinNode.getChild(1).getText().toLowerCase()) : alias; aliases.add(alias); } else { ASTNode left = (ASTNode) joinNode.getChild(0); ASTNode right = (ASTNode) joinNode.getChild(1); getTableAliasesInSubQuery(left, aliases); getTableAliasesInSubQuery(right, aliases); } }
String tableOrCol = SemanticAnalyzer.unescapeIdentifier(filterCondn.getChild(0).getText() .toLowerCase()); return getIndex(tabAliases, tableOrCol);
private void doPhase1GetColumnAliasesFromSelect( ASTNode selectExpr, QBParseInfo qbp) { for (int i = 0; i < selectExpr.getChildCount(); ++i) { ASTNode selExpr = (ASTNode) selectExpr.getChild(i); if ((selExpr.getToken().getType() == HiveParser.TOK_SELEXPR) && (selExpr.getChildCount() == 2)) { String columnAlias = unescapeIdentifier(selExpr.getChild(1).getText()); qbp.setExprToColumnAlias((ASTNode) selExpr.getChild(0), columnAlias); } } }
static ASTNode setQualifiedColumnReferences(ASTNode ast, String tableAlias) { int type = ast.getType(); if (type == HiveParser.DOT) { return ast; } if (type == HiveParser.TOK_TABLE_OR_COL) { if (tableAlias == null) { return null; } String colName = SemanticAnalyzer.unescapeIdentifier(ast.getChild(0).getText()); return SubQueryUtils.createColRefAST(tableAlias, colName); } for (int i = 0; i < ast.getChildCount(); i++) { ASTNode child = (ASTNode) ast.getChild(i); ASTNode c = setQualifiedColumnReferences(child, tableAlias); if (c == null) { return null; } if (c != child) { ast.setChild(i, c); } } return ast; }
static ASTNode setQualifiedColumnReferences(ASTNode ast, String tableAlias) { int type = ast.getType(); if (type == HiveParser.DOT) { return ast; } if (type == HiveParser.TOK_TABLE_OR_COL) { if (tableAlias == null) { return null; } String colName = SemanticAnalyzer.unescapeIdentifier(ast.getChild(0).getText()); return SubQueryUtils.createColRefAST(tableAlias, colName); } for (int i = 0; i < ast.getChildCount(); i++) { ASTNode child = (ASTNode) ast.getChild(i); ASTNode c = setQualifiedColumnReferences(child, tableAlias); if (c == null) { return null; } if (c != child) { ast.setChild(i, c); } } return ast; }
String functionName = SemanticAnalyzer.unescapeIdentifier(expressionTree.getChild(0) .getText()); GenericUDAFResolver udafResolver = FunctionRegistry.getGenericUDAFResolver(functionName);
static void checkForSubqueries(ASTNode node, boolean disallow) throws SemanticException { // allow NOT but throw an error for rest if(node.getType() == HiveParser.TOK_SUBQUERY_EXPR && disallow) { throw new CalciteSubquerySemanticException(ErrorMsg.UNSUPPORTED_SUBQUERY_EXPRESSION.getMsg( "Invalid subquery. Subquery in UDAF is not allowed.")); } if (node.getType() == HiveParser.TOK_FUNCTION || node.getType() == HiveParser.TOK_FUNCTIONDI || node.getType() == HiveParser.TOK_FUNCTIONSTAR) { if (node.getChild(0).getType() == HiveParser.Identifier) { String functionName = SemanticAnalyzer.unescapeIdentifier(node.getChild(0).getText()); GenericUDAFResolver udafResolver = FunctionRegistry.getGenericUDAFResolver(functionName); if (udafResolver != null) { disallow = disallow || true; } } } for(int i=0; i<node.getChildCount(); i++) { checkForSubqueries((ASTNode)node.getChild(i), disallow); } } /*
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; }
if ((selExpr.getToken().getType() == HiveParser.TOK_SELEXPR) && (selExpr.getChildCount() == 2)) { String columnAlias = unescapeIdentifier(selExpr.getChild(1).getText()); qbp.setExprToColumnAlias((ASTNode) selExpr.getChild(0), columnAlias);
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; }