public void setLogicalPlan(LogicalNode plan) { this.plan = plan; LogicalNode node = plan; ArrayList<LogicalNode> s = new ArrayList<LogicalNode>(); s.add(node); while (!s.isEmpty()) { node = s.remove(s.size()-1); if (node instanceof UnaryNode) { UnaryNode unary = (UnaryNode) node; s.add(s.size(), unary.getChild()); } else if (node instanceof BinaryNode) { BinaryNode binary = (BinaryNode) node; s.add(s.size(), binary.getLeftChild()); s.add(s.size(), binary.getRightChild()); } else if (node instanceof ScanNode) { scan.add((ScanNode)node); } else if (node instanceof TableSubQueryNode) { s.add(((TableSubQueryNode) node).getSubQuery()); } } }
public void setSubQuery(LogicalNode node) { this.subQuery = node; setInSchema(SchemaUtil.clone(this.subQuery.getOutSchema())); getInSchema().setQualifier(this.tableName); if (hasTargets()) { setOutSchema(PlannerUtil.targetToSchema(targets)); } else { setOutSchema(SchemaUtil.clone(this.subQuery.getOutSchema())); } }
@Override public LogicalNode visitTableSubQuery(Context upperContext, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { Context childContext = new Context(plan, upperContext.requiredSet); stack.push(node); LogicalNode child = super.visitTableSubQuery(childContext, plan, block, node, stack); node.setSubQuery(child); stack.pop(); List<Target> targets; if (node.hasTargets()) { targets = node.getTargets(); } else { targets = PlannerUtil.schemaToTargets(node.getOutSchema()); } LinkedHashSet<Target> projectedTargets = Sets.newLinkedHashSet(); for (Iterator<Target> it = getFilteredTarget(targets, upperContext.requiredSet); it.hasNext();) { Target target = it.next(); upperContext.addExpr(target); } for (Iterator<Target> it = upperContext.targetListMgr.getFilteredTargets(upperContext.requiredSet); it.hasNext();) { Target target = it.next(); if (LogicalPlanner.checkIfBeEvaluatedAtRelation(block, target.getEvalTree(), node)) { projectedTargets.add(target); upperContext.targetListMgr.markAsEvaluated(target); } } node.setTargets(new ArrayList<>(projectedTargets)); LogicalPlanner.verifyProjectedFields(block, node); return node; }
public void visitDerivedSubquery(SQLBuilderContext ctx, TableSubQueryNode derivedSubquery, Stack<LogicalNode> stack) { ctx.sb.append(" ("); visit(ctx, derivedSubquery.getSubQuery(), stack); ctx.sb.append(" ) ").append(derivedSubquery.getTableName()); }
@Override public LogicalNode visitTableSubQuery(CompilationContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { stack.push(node); visit(context, plan, null, node.getSubQuery(), stack); stack.pop(); if (node.hasTargets()) { for (Target target : node.getTargets()) { compileIfAbsent(context, node.getLogicalSchema(), target.getEvalTree()); } } return node; }
@Override public LogicalNode visitTableSubQuery(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { context.depth++; stack.push(node); visit(context, plan, block, node.getSubQuery(), new Stack<LogicalNode>()); stack.pop(); context.depth--; context.add(context.depth, node.getPlanString()); return node; }
TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { LogicalNode child = super.visitTableSubQuery(context, plan, queryBlock, node, stack); node.setSubQuery(child); for (ExecutionBlock childBlock : context.plan.getChilds(currentBlock.getId())) { TableSubQueryNode copy = PlannerUtil.clone(plan, node); copy.setSubQuery(childBlock.getPlan()); childBlock.setPlan(copy); addedTableSubQueries.add(copy); if (copy.getInSchema().containsAll(copy.getOutSchema().getRootColumns())) { for (Target eachTarget : copy.getTargets()) { Set<Column> columns = EvalTreeUtil.findUniqueColumns(eachTarget.getEvalTree()); if (copy.getInSchema().containsAll(columns)) { leftMostSubQueryNode = copy; break; List<Target> targets = leftMostSubQueryNode.getTargets(); int[] targetMappings = new int[targets.size()]; for (int i = 0; i < targets.size(); i++) { int index = leftMostSubQueryNode.getInSchema().getColumnId(targets.get(i).getNamedColumn().getQualifiedName()); if (index < 0) { index = leftMostSubQueryNode.getInSchema().getColumnId(column.getQualifiedName()); + "->" + leftMostSubQueryNode.getInSchema()); if (eachNode.getPID() == leftMostSubQueryNode.getPID()) {
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; }
@Override public LogicalNode visitTableSubQuery(SerializeContext context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { super.visitTableSubQuery(context, plan, block, node, stack); int [] childIds = registerGetChildIds(context, node); PlanProto.TableSubQueryNode.Builder builder = PlanProto.TableSubQueryNode.newBuilder(); builder.setChildSeq(childIds[0]); builder.setTableName(node.getTableName()); if (node.hasTargets()) { builder.addAllTargets(ProtoUtil.<PlanProto.Target>toProtoObjects(node.getTargets().toArray(new ProtoObject[node.getTargets().size()]))); } builder.setNameResolveBase(node.isNameResolveBase()); PlanProto.LogicalNode.Builder nodeBuilder = createNodeBuilder(context, node); nodeBuilder.setTableSubQuery(builder); context.treeBuilder.addNodes(nodeBuilder); return node; }
public void init(String tableName, LogicalNode subQuery) { this.tableName = tableName; if (subQuery != null) { this.subQuery = subQuery; setOutSchema(SchemaUtil.clone(this.subQuery.getOutSchema())); setInSchema(SchemaUtil.clone(this.subQuery.getOutSchema())); getInSchema().setQualifier(this.tableName); getOutSchema().setQualifier(this.tableName); } }
@Override public PlanString getPlanString() { PlanString planStr = new PlanString(this); planStr.appendTitle(" as ").appendTitle(tableName); if (hasTargets()) { StringBuilder sb = new StringBuilder("Targets: "); for (int i = 0; i < targets.length; i++) { sb.append(targets[i]); if( i < targets.length - 1) { sb.append(", "); } } planStr.addExplan(sb.toString()); if (getOutSchema() != null) { planStr.addExplan("out schema: " + getOutSchema().toString()); } if (getInSchema() != null) { planStr.addExplan("in schema: " + getInSchema().toString()); } } return planStr; }
@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 static TableSubQueryNode convertTableSubQuery(OverridableConf context, EvalContext evalContext, Map<Integer, LogicalNode> nodeMap, PlanProto.LogicalNode protoNode) { PlanProto.TableSubQueryNode proto = protoNode.getTableSubQuery(); TableSubQueryNode tableSubQuery = new TableSubQueryNode(protoNode.getNodeId()); tableSubQuery.init(proto.getTableName(), nodeMap.get(proto.getChildSeq())); tableSubQuery.setInSchema(convertSchema(protoNode.getInSchema())); if (proto.getTargetsCount() > 0) { tableSubQuery.setTargets(convertTargets(context, evalContext, proto.getTargetsList())); } tableSubQuery.setNameResolveBase(proto.getNameResolveBase()); return tableSubQuery; }
setOpTableSubQueryNode.init(CatalogUtil.buildFQName(context.queryContext.get(SessionVars.CURRENT_DATABASE), context.generateUniqueSubQueryName()), setOperationNode); setTargetOfTableSubQuery(context, currentBlock, setOpTableSubQueryNode); currentBlock.addRelation(setOpTableSubQueryNode); Schema setOpSchema = setOpTableSubQueryNode.getOutSchema(); Target[] setOpTarget = setOpTableSubQueryNode.getTargets();
@Override public LogicalNode visitTableSubQuery(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) throws TajoException { super.visitTableSubQuery(context, plan, block, node, stack); if (node.hasTargets()) { for (Target target : node.getTargets()) { ExprsVerifier.verify(context.state, node, target.getEvalTree()); } } verifyProjectableOutputSchema(context, node); return node; }
@Override public LogicalNode visitTableSubQuery(StringBuilder sb, LogicalPlan plan, LogicalPlan.QueryBlock block, TableSubQueryNode node, Stack<LogicalNode> stack) { sb.append(node.getTableName()); 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 DataType getValueType() { return subQueryNode.getOutSchema().getColumn(0).getDataType(); }
@Override public List<Target> getTargets() { if (hasTargets()) { return targets; } else { return null; } }
@Override public Schema getLogicalSchema() { // an output schema can be determined by targets. So, an input schema of // TableSubQueryNode is only eligible for table schema. // // TODO - but, a derived table can have column alias. For that, we should improve here. // // example) select * from (select col1, col2, col3 from t1) view (c1, c2); return getInSchema(); }