public ASTNode copyASTNode() throws LensException { return MetastoreUtil.copyAST(getASTNode()); } @Override
public ASTNode copyASTNode() throws LensException { return MetastoreUtil.copyAST(getASTNode()); } @Override
public static ASTNode copyAST(ASTNode original) { return copyAST(original, x -> Pair.of(new ASTNode(x), true)); }
public static ASTNode copyAST(ASTNode original) { return copyAST(original, x -> Pair.of(new ASTNode(x), true)); }
public static ASTNode copyAST(ASTNode original, Function<ASTNode, Pair<ASTNode, Boolean>> overrideCopyFunction) { Pair<ASTNode, Boolean> copy1 = overrideCopyFunction.apply(original); ASTNode copy = copy1.getLeft(); if (copy1.getRight()) { if (original.getChildren() != null) { for (Node o : original.getChildren()) { copy.addChild(copyAST((ASTNode) o, overrideCopyFunction)); } } } return copy; }
public static ASTNode copyAST(ASTNode original, Function<ASTNode, Pair<ASTNode, Boolean>> overrideCopyFunction) { Pair<ASTNode, Boolean> copy1 = overrideCopyFunction.apply(original); ASTNode copy = copy1.getLeft(); if (copy1.getRight()) { if (original.getChildren() != null) { for (Node o : original.getChildren()) { copy.addChild(copyAST((ASTNode) o, overrideCopyFunction)); } } } return copy; }
void initRewrittenAST(boolean copyFinal) { if (copyFinal) { reWrittenAST = MetastoreUtil.copyAST(pickedCtx.getFinalAST()); } else { reWrittenAST = pickedCtx.getFinalAST(); } } ASTNode getRewrittenAST() {
void initRewrittenAST(boolean copyFinal) { if (copyFinal) { reWrittenAST = MetastoreUtil.copyAST(pickedCtx.getFinalAST()); } else { reWrittenAST = pickedCtx.getFinalAST(); } } ASTNode getRewrittenAST() {
private void addAllNestedExpressions(CubeQueryContext cubeql, ExprSpecContext baseEsc, AbstractBaseTable baseTable, Set<ExprSpecContext> nestedExpressions, Set<String> exprCols) throws LensException { for (String col : exprCols) { Set<ExprSpecContext> replacedExpressions = new LinkedHashSet<ExprSpecContext>(); for (ExprSpec es : baseTable.getExpressionByName(col).getExpressionSpecs()) { ASTNode finalAST = MetastoreUtil.copyAST(baseEsc.getFinalAST()); replaceColumnInAST(finalAST, col, es.copyASTNode()); ExprSpecContext replacedESC = new ExprSpecContext(baseEsc, es, finalAST, cubeql); nestedExpressions.add(replacedESC); replacedExpressions.add(replacedESC); } Set<String> remaining = new LinkedHashSet<String>(exprCols); remaining.remove(col); for (ExprSpecContext replacedESC : replacedExpressions) { addAllNestedExpressions(cubeql, replacedESC, baseTable, nestedExpressions, remaining); } } }
private void addAllNestedExpressions(CubeQueryContext cubeql, ExprSpecContext baseEsc, AbstractBaseTable baseTable, Set<ExprSpecContext> nestedExpressions, Set<String> exprCols) throws LensException { for (String col : exprCols) { Set<ExprSpecContext> replacedExpressions = new LinkedHashSet<ExprSpecContext>(); for (ExprSpec es : baseTable.getExpressionByName(col).getExpressionSpecs()) { ASTNode finalAST = MetastoreUtil.copyAST(baseEsc.getFinalAST()); replaceColumnInAST(finalAST, col, es.copyASTNode()); ExprSpecContext replacedESC = new ExprSpecContext(baseEsc, es, finalAST, cubeql); nestedExpressions.add(replacedESC); replacedExpressions.add(replacedESC); } Set<String> remaining = new LinkedHashSet<String>(exprCols); remaining.remove(col); for (ExprSpecContext replacedESC : replacedExpressions) { addAllNestedExpressions(cubeql, replacedESC, baseTable, nestedExpressions, remaining); } } }
private ASTNode getExprAst(ExpressionContext expressionContext) { Set<StorageCandidate> scSet = CandidateUtil.getStorageCandidates(cubeql.getCandidates()); Set<String> storageTableNames = new HashSet<>(); Set<ExpressionContext> expressionContexts = Sets.newLinkedHashSet(); expressionContexts.add(expressionContext); for (StorageCandidate sc : scSet) { storageTableNames.add(sc.getStorageTable()); ExpressionContext ex = sc.getCubeQueryContext().getExprCtx().getExpressionContext(expressionContext. getExprCol().getName(), expressionContext.getSrcAlias()); if (ex != null) { expressionContexts.add(ex); } } for (ExpressionContext ec : expressionContexts) { for (CandidateTable table : ec.evaluableExpressions.keySet()) { if (storageTableNames.contains(table.getStorageTable())) { return MetastoreUtil.copyAST(ec.evaluableExpressions.get(table).iterator().next().finalAST); } } } return null; }
private ASTNode getExprAst(ExpressionContext expressionContext) { Set<StorageCandidate> scSet = CandidateUtil.getStorageCandidates(cubeql.getCandidates()); Set<String> storageTableNames = new HashSet<>(); Set<ExpressionContext> expressionContexts = Sets.newLinkedHashSet(); expressionContexts.add(expressionContext); for (StorageCandidate sc : scSet) { storageTableNames.add(sc.getStorageTable()); ExpressionContext ex = sc.getCubeQueryContext().getExprCtx().getExpressionContext(expressionContext. getExprCol().getName(), expressionContext.getSrcAlias()); if (ex != null) { expressionContexts.add(ex); } } for (ExpressionContext ec : expressionContexts) { for (CandidateTable table : ec.evaluableExpressions.keySet()) { if (storageTableNames.contains(table.getStorageTable())) { return MetastoreUtil.copyAST(ec.evaluableExpressions.get(table).iterator().next().finalAST); } } } return null; }
/** * Get the aggregate node for the SelectPhrase index. A given measure might not be answerable * for a StorageCanddate. In that case get the non default aggregate node wcich ideally not "0.0", * from otherStorage candidate. * * @param position * @return */ private ASTNode getAggregateNodesExpression(int position) { ASTNode node = null; for (StorageCandidateHQLContext sc : storageCandidates) { node = (ASTNode) sc.getQueryAst().getSelectAST().getChild(position).getChild(0); if (HQLParser.isAggregateAST(node) || HQLParser.hasAggregate(node)) { if (!node.getChild(1).toString().equals(DEFAULT_MEASURE)) { return MetastoreUtil.copyAST(node); } } } return MetastoreUtil.copyAST(node); }
/** * Get the aggregate node for the SelectPhrase index. A given measure might not be answerable * for a StorageCanddate. In that case get the non default aggregate node wcich ideally not "0", * from otherStorage candidate. * * @param position * @return */ private ASTNode getAggregateNodesExpression(int position) { ASTNode node = null; for (StorageCandidateHQLContext sc : storageCandidates) { node = (ASTNode) sc.getQueryAst().getSelectAST().getChild(position).getChild(0); if (HQLParser.isAggregateAST(node) || HQLParser.hasAggregate(node)) { if (!node.getChild(1).toString().equals(DEFAULT_MEASURE)) { return MetastoreUtil.copyAST(node); } } } return MetastoreUtil.copyAST(node); }
default void copyFrom(QueryAST sourceAst) { setSelectAST(MetastoreUtil.copyAST(sourceAst.getSelectAST())); setWhereAST(MetastoreUtil.copyAST(sourceAst.getWhereAST())); if (sourceAst.getJoinAST() != null) { setJoinAST(MetastoreUtil.copyAST(sourceAst.getJoinAST())); } if (sourceAst.getGroupByAST() != null) { setGroupByAST(MetastoreUtil.copyAST(sourceAst.getGroupByAST())); } if (sourceAst.getHavingAST() != null) { setHavingAST(MetastoreUtil.copyAST(sourceAst.getHavingAST())); } if (sourceAst.getOrderByAST() != null) { setOrderByAST(MetastoreUtil.copyAST(sourceAst.getOrderByAST())); } setLimitValue(sourceAst.getLimitValue()); setFromString(sourceAst.getFromString()); setWhereString(sourceAst.getWhereString()); } }
default void copyFrom(QueryAST sourceAst) { setSelectAST(MetastoreUtil.copyAST(sourceAst.getSelectAST())); setWhereAST(MetastoreUtil.copyAST(sourceAst.getWhereAST())); if (sourceAst.getJoinAST() != null) { setJoinAST(MetastoreUtil.copyAST(sourceAst.getJoinAST())); } if (sourceAst.getGroupByAST() != null) { setGroupByAST(MetastoreUtil.copyAST(sourceAst.getGroupByAST())); } if (sourceAst.getHavingAST() != null) { setHavingAST(MetastoreUtil.copyAST(sourceAst.getHavingAST())); } if (sourceAst.getOrderByAST() != null) { setOrderByAST(MetastoreUtil.copyAST(sourceAst.getOrderByAST())); } setLimitValue(sourceAst.getLimitValue()); setFromString(sourceAst.getFromString()); setWhereString(sourceAst.getWhereString()); } }
private ASTNode processAggregate(ASTNode astNode, ASTNode innerSelectAST, AliasDecider aliasDecider, boolean isSelectAst) { ASTNode innerSelectASTWithoutAlias = MetastoreUtil.copyAST(astNode); ASTNode innerSelectExprAST = new ASTNode(new CommonToken(HiveParser.TOK_SELEXPR, "TOK_SELEXPR")); innerSelectExprAST.addChild(innerSelectASTWithoutAlias); String alias = aliasDecider.decideAlias(astNode); ASTNode aliasNode = new ASTNode(new CommonToken(Identifier, alias)); innerSelectExprAST.addChild(aliasNode); innerSelectAST.addChild(innerSelectExprAST); ASTNode dotAST = getDotAST(cubeql.getCube().getName(), alias); ASTNode outerAST = new ASTNode(new CommonToken(TOK_FUNCTION, "TOK_FUNCTION")); //TODO: take care or non-transitive aggregate functions outerAST.addChild(new ASTNode(new CommonToken(Identifier, astNode.getChild(0).getText()))); outerAST.addChild(dotAST); HashableASTNode innerAST = new HashableASTNode(innerSelectASTWithoutAlias); if (isSelectAst && !innerToOuterSelectASTs.containsKey(innerAST)) { innerToOuterSelectASTs.put(innerAST, outerAST); } else if (!isSelectAst && !innerToOuterHavingASTs.containsKey(innerAST)) { innerToOuterHavingASTs.put(innerAST, outerAST); } return outerAST; }
private ASTNode processAggregate(ASTNode astNode, ASTNode innerSelectAST, AliasDecider aliasDecider, boolean isSelectAst) { ASTNode innerSelectASTWithoutAlias = MetastoreUtil.copyAST(astNode); ASTNode innerSelectExprAST = new ASTNode(new CommonToken(HiveParser.TOK_SELEXPR, "TOK_SELEXPR")); innerSelectExprAST.addChild(innerSelectASTWithoutAlias); String alias = aliasDecider.decideAlias(astNode); ASTNode aliasNode = new ASTNode(new CommonToken(Identifier, alias)); innerSelectExprAST.addChild(aliasNode); innerSelectAST.addChild(innerSelectExprAST); ASTNode dotAST = getDotAST(cubeql.getCube().getName(), alias); ASTNode outerAST = new ASTNode(new CommonToken(TOK_FUNCTION, "TOK_FUNCTION")); //TODO: take care or non-transitive aggregate functions outerAST.addChild(new ASTNode(new CommonToken(Identifier, astNode.getChild(0).getText()))); outerAST.addChild(dotAST); HashableASTNode innerAST = new HashableASTNode(innerSelectASTWithoutAlias); if (isSelectAst && !innerToOuterSelectASTs.containsKey(innerAST)) { innerToOuterSelectASTs.put(innerAST, outerAST); } else if (!isSelectAst && !innerToOuterHavingASTs.containsKey(innerAST)) { innerToOuterHavingASTs.put(innerAST, outerAST); } return outerAST; }
private static ASTNode replaceAlias(final ASTNode expr, final CubeQueryContext cubeql) throws LensException { final ASTNode finalAST = MetastoreUtil.copyAST(expr); HQLParser.bft(finalAST, visited -> { ASTNode node = visited.getNode(); ASTNode parent = null; if (visited.getParent() != null) { parent = visited.getParent().getNode(); } if (node.getToken().getType() == TOK_TABLE_OR_COL && (parent != null && parent.getToken().getType() == DOT)) { ASTNode current = (ASTNode) node.getChild(0); if (current.getToken().getType() == Identifier) { String tableName = current.getToken().getText().toLowerCase(); String alias = cubeql.getAliasForTableName(tableName); if (!alias.equalsIgnoreCase(tableName)) { node.setChild(0, new ASTNode(new CommonToken(HiveParser.Identifier, alias))); } } } }); return finalAST; }
private static ASTNode replaceAlias(final ASTNode expr, final CubeQueryContext cubeql) throws LensException { final ASTNode finalAST = MetastoreUtil.copyAST(expr); HQLParser.bft(finalAST, visited -> { ASTNode node = visited.getNode(); ASTNode parent = null; if (visited.getParent() != null) { parent = visited.getParent().getNode(); } if (node.getToken().getType() == TOK_TABLE_OR_COL && (parent != null && parent.getToken().getType() == DOT)) { ASTNode current = (ASTNode) node.getChild(0); if (current.getToken().getType() == Identifier) { String tableName = current.getToken().getText().toLowerCase(); String alias = cubeql.getAliasForTableName(tableName); if (!alias.equalsIgnoreCase(tableName)) { node.setChild(0, new ASTNode(new CommonToken(HiveParser.Identifier, alias))); } } } }); return finalAST; }