public QueryBlock getBlockByExpr(Expr expr) { return getBlock(exprToBlockNameMap.get(ObjectUtils.identityToString(expr))); }
private TableSubQueryNode visitCommonTableSubquery(PlanContext context, Stack<Expr> stack, CommonSubquery expr) throws TajoException { QueryBlock currentBlock = context.queryBlock; QueryBlock childBlock = context.plan.getBlock(context.plan.getBlockNameByExpr(expr.getSubQuery())); context.plan.connectBlocks(childBlock, currentBlock, BlockType.TableSubQuery); PlanContext newContext = new PlanContext(context, childBlock); context.plan.connectBlocks(childBlock, context.queryBlock, BlockType.TableSubQuery); LogicalNode child = visit(newContext, new Stack<>(), expr.getSubQuery()); TableSubQueryNode subQueryNode = currentBlock.getNodeFromExpr(expr); subQueryNode.setSubQuery(child); setTargetOfTableSubQuery(context, currentBlock, subQueryNode); return subQueryNode; }
public QueryBlock getBlockByExpr(Expr expr) { return getBlock(exprToBlockNameMap.get(ObjectUtils.identityToString(expr))); }
private TableSubQueryNode visitCommonTableSubquery(PlanContext context, Stack<Expr> stack, CommonSubquery expr) throws TajoException { QueryBlock currentBlock = context.queryBlock; QueryBlock childBlock = context.plan.getBlock(context.plan.getBlockNameByExpr(expr.getSubQuery())); context.plan.connectBlocks(childBlock, currentBlock, BlockType.TableSubQuery); PlanContext newContext = new PlanContext(context, childBlock); context.plan.connectBlocks(childBlock, context.queryBlock, BlockType.TableSubQuery); LogicalNode child = visit(newContext, new Stack<Expr>(), expr.getSubQuery()); TableSubQueryNode subQueryNode = currentBlock.getNodeFromExpr(expr); subQueryNode.setSubQuery(child); setTargetOfTableSubQuery(context, currentBlock, subQueryNode); return subQueryNode; }
@Override public RESULT visitTableSubQuery(CONTEXT context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { stack.push(node); RESULT result = null; if (plan != null) { LogicalPlan.QueryBlock childBlock = plan.getBlock(node.getSubQuery()); result = visit(context, plan, childBlock, childBlock.getRoot(), stack); } else { result = visit(context, null, null, node.getSubQuery(), stack); } stack.pop(); return result; }
@Override public LogicalNode visitTableSubQuery(ProcessorContext ctx, Stack<Expr> stack, TablePrimarySubQuery expr) throws TajoException { QueryBlock childBlock = ctx.planContext.getPlan().getBlock( ctx.planContext.getPlan().getBlockNameByExpr(expr.getSubQuery())); ProcessorContext newContext = new ProcessorContext(new PlanContext(ctx.planContext, childBlock)); super.visitTableSubQuery(newContext, stack, expr); TableSubQueryNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getSubQuery()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
@Override public RESULT visitTableSubQuery(CONTEXT context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { stack.push(node); RESULT result = null; if (plan != null) { LogicalPlan.QueryBlock childBlock = plan.getBlock(node.getSubQuery()); result = visit(context, plan, childBlock, childBlock.getRoot(), stack); } else { result = visit(context, null, null, node.getSubQuery(), stack); } stack.pop(); return result; }
@Override public LogicalNode visitSimpleTableSubquery(ProcessorContext ctx, Stack<Expr> stack, SimpleTableSubquery expr) throws TajoException { QueryBlock childBlock = ctx.planContext.getPlan().getBlock( ctx.planContext.getPlan().getBlockNameByExpr(expr.getSubQuery())); ProcessorContext newContext = new ProcessorContext(new PlanContext(ctx.planContext, childBlock)); super.visitSimpleTableSubquery(newContext, stack, expr); TableSubQueryNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getSubQuery()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
@Override public LogicalNode visitUnion(FilterPushDownContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, UnionNode unionNode, Stack<LogicalNode> stack) throws TajoException { LogicalNode leftNode = unionNode.getLeftChild(); List<EvalNode> origins = new ArrayList<>(); origins.addAll(context.pushingDownFilters); // transformed -> pushingDownFilters Map<EvalNode, EvalNode> transformedMap = transformEvalsWidthByPassNode(origins, plan, block, unionNode, leftNode); context.setFiltersTobePushed(transformedMap.keySet()); visit(context, plan, plan.getBlock(leftNode)); if (!context.pushingDownFilters.isEmpty()) { errorFilterPushDown(plan, leftNode, context); } LogicalNode rightNode = unionNode.getRightChild(); transformedMap = transformEvalsWidthByPassNode(origins, plan, block, unionNode, rightNode); context.setFiltersTobePushed(transformedMap.keySet()); visit(context, plan, plan.getBlock(rightNode), rightNode, stack); if (!context.pushingDownFilters.isEmpty()) { errorFilterPushDown(plan, rightNode, context); } // notify all filter matched to upper context.pushingDownFilters.clear(); return unionNode; }
@Override public LogicalNode visitTableSubQuery(ProcessorContext ctx, Stack<Expr> stack, TablePrimarySubQuery expr) throws TajoException { QueryBlock childBlock = ctx.planContext.getPlan().getBlock( ctx.planContext.getPlan().getBlockNameByExpr(expr.getSubQuery())); ProcessorContext newContext = new ProcessorContext(new PlanContext(ctx.planContext, childBlock)); super.visitTableSubQuery(newContext, stack, expr); TableSubQueryNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getSubQuery()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
@Override public LogicalNode visitUnion(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, UnionNode node, Stack<LogicalNode> stack) throws TajoException { LogicalPlan.QueryBlock leftBlock = plan.getBlock(node.getLeftChild()); LogicalPlan.QueryBlock rightBlock = plan.getBlock(node.getRightChild()); Context leftContext = new Context(plan, PlannerUtil.toQualifiedFieldNames(context.requiredSet, leftBlock.getName())); Context rightContext = new Context(plan, PlannerUtil.toQualifiedFieldNames(context.requiredSet, rightBlock.getName())); stack.push(node); visit(leftContext, plan, leftBlock, leftBlock.getRoot(), new Stack<LogicalNode>()); visit(rightContext, plan, rightBlock, rightBlock.getRoot(), new Stack<LogicalNode>()); stack.pop(); return node; }
@Override public LogicalNode visitSimpleTableSubquery(ProcessorContext ctx, Stack<Expr> stack, SimpleTableSubquery expr) throws TajoException { QueryBlock childBlock = ctx.planContext.getPlan().getBlock( ctx.planContext.getPlan().getBlockNameByExpr(expr.getSubQuery())); ProcessorContext newContext = new ProcessorContext(new PlanContext(ctx.planContext, childBlock)); super.visitSimpleTableSubquery(newContext, stack, expr); TableSubQueryNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getSubQuery()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
@Override public LogicalNode visitUnion(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, UnionNode node, Stack<LogicalNode> stack) throws TajoException { LogicalPlan.QueryBlock leftBlock = plan.getBlock(node.getLeftChild()); LogicalPlan.QueryBlock rightBlock = plan.getBlock(node.getRightChild()); Context leftContext = new Context(plan, PlannerUtil.toQualifiedFieldNames(context.requiredSet, leftBlock.getName())); Context rightContext = new Context(plan, PlannerUtil.toQualifiedFieldNames(context.requiredSet, rightBlock.getName())); stack.push(node); visit(leftContext, plan, leftBlock, leftBlock.getRoot(), new Stack<>()); visit(rightContext, plan, rightBlock, rightBlock.getRoot(), new Stack<>()); stack.pop(); return node; }
@Override public LogicalNode visitTableSubQuery(FilterPushDownContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { List<EvalNode> matched = new ArrayList<>(); List<EvalNode> unmatched = new ArrayList<>(); for (EvalNode eval : context.pushingDownFilters) { if (LogicalPlanner.checkIfBeEvaluatedAtRelation(block, eval, node)) { matched.add(eval); } else { unmatched.add(eval); } } // transformed -> pushingDownFilters Map<EvalNode, EvalNode> transformedMap = transformEvalsWidthByPassNode(matched, plan, block, node, node.getSubQuery()); context.setFiltersTobePushed(transformedMap.keySet()); visit(context, plan, plan.getBlock(node.getSubQuery())); context.setToOrigin(transformedMap); context.addFiltersTobePushed(unmatched); return node; }
@Override public RESULT visitUnion(CONTEXT context, LogicalPlan plan, LogicalPlan.QueryBlock block, UnionNode node, Stack<LogicalNode> stack) throws TajoException { stack.push(node); RESULT result = null; if (plan != null) { LogicalPlan.QueryBlock leftBlock = plan.getBlock(node.getLeftChild()); result = visit(context, plan, leftBlock, leftBlock.getRoot(), stack); LogicalPlan.QueryBlock rightBlock = plan.getBlock(node.getRightChild()); visit(context, plan, rightBlock, rightBlock.getRoot(), stack); } else { result = visit(context, null, null, node.getLeftChild(), stack); visit(context, null, null, node.getRightChild(), stack); } stack.pop(); return result; }
@Override public LogicalNode visitUnion(FilterPushDownContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, UnionNode unionNode, Stack<LogicalNode> stack) throws TajoException { LogicalNode leftNode = unionNode.getLeftChild(); List<EvalNode> origins = TUtil.newList(context.pushingDownFilters); // transformed -> pushingDownFilters Map<EvalNode, EvalNode> transformedMap = transformEvalsWidthByPassNode(origins, plan, block, unionNode, leftNode); context.setFiltersTobePushed(transformedMap.keySet()); visit(context, plan, plan.getBlock(leftNode)); if (!context.pushingDownFilters.isEmpty()) { errorFilterPushDown(plan, leftNode, context); } LogicalNode rightNode = unionNode.getRightChild(); transformedMap = transformEvalsWidthByPassNode(origins, plan, block, unionNode, rightNode); context.setFiltersTobePushed(transformedMap.keySet()); visit(context, plan, plan.getBlock(rightNode), rightNode, stack); if (!context.pushingDownFilters.isEmpty()) { errorFilterPushDown(plan, rightNode, context); } // notify all filter matched to upper context.pushingDownFilters.clear(); return unionNode; }
@Override public RESULT visitUnion(CONTEXT context, LogicalPlan plan, LogicalPlan.QueryBlock block, UnionNode node, Stack<LogicalNode> stack) throws TajoException { stack.push(node); RESULT result = null; if (plan != null) { LogicalPlan.QueryBlock leftBlock = plan.getBlock(node.getLeftChild()); result = visit(context, plan, leftBlock, leftBlock.getRoot(), stack); LogicalPlan.QueryBlock rightBlock = plan.getBlock(node.getRightChild()); visit(context, plan, rightBlock, rightBlock.getRoot(), stack); } else { result = visit(context, null, null, node.getLeftChild(), stack); visit(context, null, null, node.getRightChild(), stack); } stack.pop(); return result; }
@Override public LogicalNode visitTableSubQuery(FilterPushDownContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { List<EvalNode> matched = TUtil.newList(); List<EvalNode> unmatched = TUtil.newList(); for (EvalNode eval : context.pushingDownFilters) { if (LogicalPlanner.checkIfBeEvaluatedAtRelation(block, eval, node)) { matched.add(eval); } else { unmatched.add(eval); } } // transformed -> pushingDownFilters Map<EvalNode, EvalNode> transformedMap = transformEvalsWidthByPassNode(matched, plan, block, node, node.getSubQuery()); context.setFiltersTobePushed(transformedMap.keySet()); visit(context, plan, plan.getBlock(node.getSubQuery())); context.setToOrigin(transformedMap); context.addFiltersTobePushed(unmatched); return node; }
private void optimizeJoinOrder(LogicalPlan plan, String blockName) throws TajoException { LogicalPlan.QueryBlock block = plan.getBlock(blockName); if (block.hasNode(NodeType.JOIN)) { String originalOrder = JoinOrderStringBuilder.buildJoinOrderString(plan, block); double nonOptimizedJoinCost = JoinCostComputer.computeCost(plan, block); // finding relations and filter expressions JoinGraphContext joinGraphContext = JoinGraphBuilder.buildJoinGraph(plan, block); // finding join order and restore remaining filters FoundJoinOrder order = joinOrderAlgorithm.findBestOrder(plan, block, joinGraphContext); // replace join node with FoundJoinOrder. JoinNode newJoinNode = order.getOrderedJoin(); LogicalNode newNode = handleRemainingFiltersIfNecessary(joinGraphContext, plan, block, newJoinNode); JoinNode old = PlannerUtil.findTopNode(block.getRoot(), NodeType.JOIN); JoinTargetCollector collector = new JoinTargetCollector(); Set<Target> targets = new LinkedHashSet<>(); collector.visitJoin(targets, plan, block, old, new Stack<>()); if (targets.size() == 0) { newJoinNode.setTargets(PlannerUtil.schemaToTargets(old.getOutSchema())); } else { newJoinNode.setTargets(new ArrayList<>(targets)); } PlannerUtil.replaceNode(plan, block.getRoot(), old, newNode); // End of replacement logic String optimizedOrder = JoinOrderStringBuilder.buildJoinOrderString(plan, block); block.addPlanHistory("Non-optimized join order: " + originalOrder + " (cost: " + nonOptimizedJoinCost + ")"); block.addPlanHistory("Optimized join order : " + optimizedOrder + " (cost: " + order.getCost() + ")"); joinGraphContext.clear(); } }
private ProjectionNode makeProjectionForInsertUnion(PlanContext context, InsertNode insertNode) { LogicalNode child = insertNode.getChild(); // add (projection - subquery) to RootBlock and create new QueryBlock for UnionNode TableSubQueryNode subQueryNode = context.plan.createNode(TableSubQueryNode.class); subQueryNode.init(context.queryBlock.getName(), child); subQueryNode.setTargets(PlannerUtil.schemaToTargets(subQueryNode.getOutSchema())); ProjectionNode projectionNode = context.plan.createNode(ProjectionNode.class); projectionNode.setChild(subQueryNode); projectionNode.setInSchema(subQueryNode.getInSchema()); projectionNode.setTargets(subQueryNode.getTargets()); context.queryBlock.registerNode(projectionNode); context.queryBlock.registerNode(subQueryNode); // add child QueryBlock to the UnionNode's QueryBlock UnionNode unionNode = (UnionNode)child; context.queryBlock.unregisterNode(unionNode); QueryBlock unionBlock = context.plan.newQueryBlock(); unionBlock.registerNode(unionNode); unionBlock.setRoot(unionNode); QueryBlock leftBlock = context.plan.getBlock(unionNode.getLeftChild()); QueryBlock rightBlock = context.plan.getBlock(unionNode.getRightChild()); context.plan.disconnectBlocks(leftBlock, context.queryBlock); context.plan.disconnectBlocks(rightBlock, context.queryBlock); context.plan.connectBlocks(unionBlock, context.queryBlock, BlockType.TableSubQuery); context.plan.connectBlocks(leftBlock, unionBlock, BlockType.TableSubQuery); context.plan.connectBlocks(rightBlock, unionBlock, BlockType.TableSubQuery); // set InsertNode's child with ProjectionNode which is created. insertNode.setChild(projectionNode); return projectionNode; }