@VisibleForTesting public LogicalPlan createPlan(OverridableConf queryContext, Expr expr, boolean debug) throws TajoException { LogicalPlan plan = new LogicalPlan(); QueryBlock rootBlock = plan.newAndGetBlock(LogicalPlan.ROOT_BLOCK); PlanContext context = new PlanContext(queryContext, plan, rootBlock, evalOptimizer, debug); preprocessor.process(context, expr); plan.resetGeneratedId(); LogicalNode topMostNode = this.visit(context, new Stack<Expr>(), expr); // Add Root Node LogicalRootNode root = plan.createNode(LogicalRootNode.class); root.setInSchema(topMostNode.getOutSchema()); root.setChild(topMostNode); root.setOutSchema(topMostNode.getOutSchema()); plan.getRootBlock().setRoot(root); return plan; }
@VisibleForTesting public LogicalPlan createPlan(OverridableConf queryContext, Expr expr, boolean debug) throws TajoException { LogicalPlan plan = new LogicalPlan(); QueryBlock rootBlock = plan.newAndGetBlock(LogicalPlan.ROOT_BLOCK); PlanContext context = new PlanContext(queryContext, plan, rootBlock, evalOptimizer, debug); preprocessor.process(context, expr); plan.resetGeneratedId(); LogicalNode topMostNode = this.visit(context, new Stack<>(), expr); // Add Root Node LogicalRootNode root = plan.createNode(LogicalRootNode.class); root.setInSchema(topMostNode.getOutSchema()); root.setChild(topMostNode); root.setOutSchema(topMostNode.getOutSchema()); plan.getRootBlock().setRoot(root); return plan; }
PlanContext leftContext = new PlanContext(context, leftBlock); stack.push(setOperation); LogicalNode leftChild = visit(leftContext, new Stack<Expr>(), setOperation.getLeft()); PlanContext rightContext = new PlanContext(context, rightBlock); stack.push(setOperation); LogicalNode rightChild = visit(rightContext, new Stack<Expr>(), setOperation.getRight());
PlanContext leftContext = new PlanContext(context, leftBlock); stack.push(setOperation); LogicalNode leftChild = visit(leftContext, new Stack<>(), setOperation.getLeft()); PlanContext rightContext = new PlanContext(context, rightBlock); stack.push(setOperation); LogicalNode rightChild = visit(rightContext, new Stack<>(), setOperation.getRight());
@Override public LogicalNode visitSimpleTableSubquery(LogicalPlanner.PlanContext ctx, Stack<Expr> stack, SimpleTableSubquery expr) throws TajoException { LogicalPlanner.PlanContext newContext; // Note: TableSubQuery always has a table name. // SELECT .... FROM (SELECT ...) TB_NAME <- QueryBlock queryBlock = ctx.getPlan().newQueryBlock(); newContext = new LogicalPlanner.PlanContext(ctx, queryBlock); LogicalNode child = super.visitSimpleTableSubquery(newContext, stack, expr); queryBlock.setRoot(child); // a table subquery should be dealt as a relation. TableSubQueryNode node = ctx.getPlan().createNode(TableSubQueryNode.class); node.init(CatalogUtil.buildFQName(ctx.getQueryContext().get(SessionVars.CURRENT_DATABASE), ctx.generateUniqueSubQueryName()), child); ctx.getQueryBlock().addRelation(node); if (stack.peek().getType() == OpType.InPredicate) { // In-subquery and scalar subquery cannot be the base for name resolution. node.setNameResolveBase(false); } return node; }
@Override public LogicalNode visitUnion(LogicalPlanner.PlanContext ctx, Stack<Expr> stack, SetOperation expr) throws TajoException { LogicalPlan.QueryBlock leftBlock = ctx.getPlan().newQueryBlock(); LogicalPlanner.PlanContext leftContext = new LogicalPlanner.PlanContext(ctx, leftBlock); LogicalNode leftChild = visit(leftContext, new Stack<Expr>(), expr.getLeft()); leftBlock.setRoot(leftChild); ctx.getQueryBlock().registerExprWithNode(expr.getLeft(), leftChild); LogicalPlan.QueryBlock rightBlock = ctx.getPlan().newQueryBlock(); LogicalPlanner.PlanContext rightContext = new LogicalPlanner.PlanContext(ctx, rightBlock); LogicalNode rightChild = visit(rightContext, new Stack<Expr>(), expr.getRight()); rightBlock.setRoot(rightChild); ctx.getQueryBlock().registerExprWithNode(expr.getRight(), rightChild); UnionNode unionNode = new UnionNode(ctx.getPlan().newPID()); unionNode.setLeftChild(leftChild); unionNode.setRightChild(rightChild); unionNode.setInSchema(leftChild.getOutSchema()); unionNode.setOutSchema(leftChild.getOutSchema()); unionNode.setDistinct(expr.isDistinct()); return unionNode; }
@Override public LogicalNode visitSimpleTableSubquery(LogicalPlanner.PlanContext ctx, Stack<Expr> stack, SimpleTableSubquery expr) throws TajoException { LogicalPlanner.PlanContext newContext; // Note: TableSubQuery always has a table name. // SELECT .... FROM (SELECT ...) TB_NAME <- QueryBlock queryBlock = ctx.getPlan().newQueryBlock(); newContext = new LogicalPlanner.PlanContext(ctx, queryBlock); LogicalNode child = super.visitSimpleTableSubquery(newContext, stack, expr); queryBlock.setRoot(child); // a table subquery should be dealt as a relation. TableSubQueryNode node = ctx.getPlan().createNode(TableSubQueryNode.class); node.init(IdentifierUtil.buildFQName(ctx.getQueryContext().get(SessionVars.CURRENT_DATABASE), ctx.generateUniqueSubQueryName()), child); ctx.getQueryBlock().addRelation(node); if (stack.peek().getType() == OpType.InPredicate) { // In-subquery and scalar subquery cannot be the base for name resolution. node.setNameResolveBase(false); } return node; }
@Override public LogicalNode visitUnion(LogicalPlanner.PlanContext ctx, Stack<Expr> stack, SetOperation expr) throws TajoException { LogicalPlan.QueryBlock leftBlock = ctx.getPlan().newQueryBlock(); LogicalPlanner.PlanContext leftContext = new LogicalPlanner.PlanContext(ctx, leftBlock); LogicalNode leftChild = visit(leftContext, new Stack<>(), expr.getLeft()); leftBlock.setRoot(leftChild); ctx.getQueryBlock().registerExprWithNode(expr.getLeft(), leftChild); LogicalPlan.QueryBlock rightBlock = ctx.getPlan().newQueryBlock(); LogicalPlanner.PlanContext rightContext = new LogicalPlanner.PlanContext(ctx, rightBlock); LogicalNode rightChild = visit(rightContext, new Stack<>(), expr.getRight()); rightBlock.setRoot(rightChild); ctx.getQueryBlock().registerExprWithNode(expr.getRight(), rightChild); UnionNode unionNode = new UnionNode(ctx.getPlan().newPID()); unionNode.setLeftChild(leftChild); unionNode.setRightChild(rightChild); unionNode.setInSchema(leftChild.getOutSchema()); unionNode.setOutSchema(leftChild.getOutSchema()); unionNode.setDistinct(expr.isDistinct()); return unionNode; }
@Override public LogicalNode visitTableSubQuery(LogicalPlanner.PlanContext ctx, Stack<Expr> stack, TablePrimarySubQuery expr) throws TajoException { LogicalPlanner.PlanContext newContext; // Note: TableSubQuery always has a table name. // SELECT .... FROM (SELECT ...) TB_NAME <- QueryBlock queryBlock = ctx.getPlan().newQueryBlock(); newContext = new LogicalPlanner.PlanContext(ctx, queryBlock); LogicalNode child = super.visitTableSubQuery(newContext, stack, expr); queryBlock.setRoot(child); // a table subquery should be dealt as a relation. TableSubQueryNode node = ctx.getPlan().createNode(TableSubQueryNode.class); node.init(IdentifierUtil.buildFQName(ctx.getQueryContext().get(SessionVars.CURRENT_DATABASE), expr.getName()), child); ctx.getQueryBlock().addRelation(node); return node; }
@Override public LogicalNode visitTableSubQuery(LogicalPlanner.PlanContext ctx, Stack<Expr> stack, TablePrimarySubQuery expr) throws TajoException { LogicalPlanner.PlanContext newContext; // Note: TableSubQuery always has a table name. // SELECT .... FROM (SELECT ...) TB_NAME <- QueryBlock queryBlock = ctx.getPlan().newQueryBlock(); newContext = new LogicalPlanner.PlanContext(ctx, queryBlock); LogicalNode child = super.visitTableSubQuery(newContext, stack, expr); queryBlock.setRoot(child); // a table subquery should be dealt as a relation. TableSubQueryNode node = ctx.getPlan().createNode(TableSubQueryNode.class); node.init(CatalogUtil.buildFQName(ctx.getQueryContext().get(SessionVars.CURRENT_DATABASE), expr.getName()), child); ctx.getQueryBlock().addRelation(node); 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; }
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 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 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 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; }
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 static EvalNode getRootSelection(String query) throws TajoException { Expr block = analyzer.parse(query); LogicalPlan plan = planner.createPlan(defaultContext, block); LogicalPlanner.PlanContext context = new LogicalPlanner.PlanContext(defaultContext, plan, plan.getRootBlock(), new EvalTreeOptimizer(), true); Selection selection = plan.getRootBlock().getSingletonExpr(OpType.Filter); return planner.getExprAnnotator().createEvalNode(context, selection.getQual(), NameResolvingMode.RELS_AND_SUBEXPRS); }