/** * It transforms ColumnDefinition array to String array. * * @param columnReferenceExprs * @return */ private static String[] convertColumnsToStrings(ColumnReferenceExpr[] columnReferenceExprs) { int columnCount = columnReferenceExprs.length; String[] columns = new String[columnCount]; for(int i = 0; i < columnCount; i++) { ColumnReferenceExpr columnReferenceExpr = columnReferenceExprs[i]; columns[i] = columnReferenceExpr.getName(); } return columns; }
/** * It transforms ColumnDefinition array to String array. * * @param columnReferenceExprs * @return */ private static String[] convertColumnsToStrings(ColumnReferenceExpr[] columnReferenceExprs) { int columnCount = columnReferenceExprs.length; String[] columns = new String[columnCount]; for(int i = 0; i < columnCount; i++) { ColumnReferenceExpr columnReferenceExpr = columnReferenceExprs[i]; columns[i] = columnReferenceExpr.getName(); } return columns; }
@Override public Expr visitColumnReference(Object ctx, Stack<Expr> stack, ColumnReferenceExpr expr) throws TajoException { StringBuilder sb = new StringBuilder(); if (!isHiveCatalog) { sb.append("( ").append(tableAlias).append(".").append(CatalogConstants.COL_COLUMN_NAME) .append(" = '").append(expr.getName()).append("'") .append(" AND ").append(tableAlias).append(".").append(CatalogConstants.COL_PARTITION_VALUE); } else { sb.append(expr.getName()); } queries.push(sb.toString()); return expr; }
@Override public Object visitColumnReference(Object ctx, Stack<Expr> stack, ColumnReferenceExpr expr) throws TajoException { this.append(expr.getName()); return super.visitColumnReference(ctx, stack, expr); }
@Override public Object visitColumnReference(Object ctx, Stack<Expr> stack, ColumnReferenceExpr expr) throws TajoException { this.append(expr.getName()); return super.visitColumnReference(ctx, stack, expr); }
/** * Try to find a column from the final schema of the current block. * * @param block The current query block * @param columnRef The column reference to be found * @return The found column */ static Column resolveAliasedName(LogicalPlan.QueryBlock block, ColumnReferenceExpr columnRef) throws AmbiguousColumnException { List<Column> candidates = Lists.newArrayList(); if (block.getSchema() != null) { Column found = block.getSchema().getColumn(columnRef.getName()); if (found != null) { candidates.add(found); } } if (!candidates.isEmpty()) { return NameResolver.ensureUniqueColumn(candidates); } else { return null; } }
/** * Try to find a column from the final schema of the current block. * * @param block The current query block * @param columnRef The column reference to be found * @return The found column */ static Column resolveAliasedName(LogicalPlan.QueryBlock block, ColumnReferenceExpr columnRef) throws AmbiguousColumnException { List<Column> candidates = Lists.newArrayList(); if (block.getSchema() != null) { Column found = block.getSchema().getColumn(columnRef.getName()); if (found != null) { candidates.add(found); } } if (!candidates.isEmpty()) { return NameResolver.ensureUniqueColumn(candidates); } else { return null; } }
/** * Trying to find a column from all relations in other blocks * * @param plan The logical plan * @param columnRef The column reference to be found * @return The found column */ static Column resolveFromAllRelsInAllBlocks(LogicalPlan plan, ColumnReferenceExpr columnRef) throws AmbiguousColumnException { List<Column> candidates = Lists.newArrayList(); // from all relations of all query blocks for (LogicalPlan.QueryBlock eachBlock : plan.getQueryBlocks()) { for (RelationNode rel : eachBlock.getRelations()) { Column found = rel.getLogicalSchema().getColumn(columnRef.getName()); if (found != null) { candidates.add(found); } } } if (!candidates.isEmpty()) { return NameResolver.ensureUniqueColumn(candidates); } else { return null; } }
/** * Trying to find a column from all relations in other blocks * * @param plan The logical plan * @param columnRef The column reference to be found * @return The found column */ static Column resolveFromAllRelsInAllBlocks(LogicalPlan plan, ColumnReferenceExpr columnRef) throws AmbiguousColumnException { List<Column> candidates = Lists.newArrayList(); // from all relations of all query blocks for (LogicalPlan.QueryBlock eachBlock : plan.getQueryBlocks()) { for (RelationNode rel : eachBlock.getRelations()) { Column found = rel.getLogicalSchema().getColumn(columnRef.getName()); if (found != null) { candidates.add(found); } } } if (!candidates.isEmpty()) { return NameResolver.ensureUniqueColumn(candidates); } else { return null; } }
static Column resolveColumnWithoutQualifier(LogicalPlan plan, LogicalPlan.QueryBlock block, ColumnReferenceExpr columnRef, boolean includeSeflDescTable) throws AmbiguousColumnException, UndefinedColumnException { Column found = lookupColumnFromAllRelsInBlock(block, columnRef.getName(), includeSeflDescTable); if (found != null) { return found; } found = resolveAliasedName(block, columnRef); if (found != null) { return found; } found = resolveFromCurrentAndChildNode(block, columnRef); if (found != null) { return found; } found = resolveFromAllRelsInAllBlocks(plan, columnRef); if (found != null) { return found; } throw new UndefinedColumnException(columnRef.getCanonicalName()); } }
static Column resolveColumnWithoutQualifier(LogicalPlan plan, LogicalPlan.QueryBlock block, ColumnReferenceExpr columnRef, boolean includeSeflDescTable) throws AmbiguousColumnException, UndefinedColumnException { Column found = lookupColumnFromAllRelsInBlock(block, columnRef.getName(), includeSeflDescTable); if (found != null) { return found; } found = resolveAliasedName(block, columnRef); if (found != null) { return found; } found = resolveFromCurrentAndChildNode(block, columnRef); if (found != null) { return found; } found = resolveFromAllRelsInAllBlocks(plan, columnRef); if (found != null) { return found; } throw new UndefinedColumnException(columnRef.getCanonicalName()); } }
@Override public LogicalNode visitFilter(ProcessorContext ctx, Stack<Expr> stack, Selection expr) throws TajoException { Set<ColumnReferenceExpr> columnSet = ExprFinder.finds(expr.getQual(), OpType.Column); for (ColumnReferenceExpr col : columnSet) { if (!ctx.aliasSet.contains(col.getName())) { NameRefInSelectListNormalizer.normalize(ctx.planContext, col); TUtil.putToNestedList(ctx.projectColumns, col.getQualifier(), col); } } super.visitFilter(ctx, stack, expr); SelectionNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getChild()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
@Override public LogicalNode visitFilter(ProcessorContext ctx, Stack<Expr> stack, Selection expr) throws TajoException { Set<ColumnReferenceExpr> columnSet = ExprFinder.finds(expr.getQual(), OpType.Column); for (ColumnReferenceExpr col : columnSet) { if (!ctx.aliasSet.contains(col.getName())) { NameRefInSelectListNormalizer.normalize(ctx.planContext, col); TUtil.putToNestedList(ctx.projectColumns, col.getQualifier(), col); } } super.visitFilter(ctx, stack, expr); SelectionNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getChild()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
@Override public LogicalNode visitSort(ProcessorContext ctx, Stack<Expr> stack, Sort expr) throws TajoException { for (Sort.SortSpec sortSpec : expr.getSortSpecs()) { Set<ColumnReferenceExpr> columns = ExprFinder.finds(sortSpec.getKey(), OpType.Column); for (ColumnReferenceExpr col : columns) { if (!ctx.aliasSet.contains(col.getName())) { NameRefInSelectListNormalizer.normalize(ctx.planContext, col); TUtil.putToNestedList(ctx.projectColumns, col.getQualifier(), col); } } } super.visitSort(ctx, stack, expr); SortNode node = getNodeFromExpr(ctx.planContext.getPlan(), expr); LogicalNode child = getNonRelationListExpr(ctx.planContext.getPlan(), expr.getChild()); node.setInSchema(child.getOutSchema()); node.setOutSchema(node.getInSchema()); return node; }
/** * Try to find the column from the current node and child node. It can find subexprs generated from the optimizer. * * @param block The current query block * @param columnRef The column reference to be found * @return The found column */ static Column resolveFromCurrentAndChildNode(LogicalPlan.QueryBlock block, ColumnReferenceExpr columnRef) throws UndefinedColumnException { if (block.getCurrentNode() != null && block.getCurrentNode().getInSchema() != null) { Column found = block.getCurrentNode().getInSchema().getColumn(columnRef.getCanonicalName()); if (found != null) { return found; } else if (block.getLatestNode() != null) { found = block.getLatestNode().getOutSchema().getColumn(columnRef.getName()); if (found != null) { return found; } } } return null; }
/** * Try to find the column from the current node and child node. It can find subexprs generated from the optimizer. * * @param block The current query block * @param columnRef The column reference to be found * @return The found column */ static Column resolveFromCurrentAndChildNode(LogicalPlan.QueryBlock block, ColumnReferenceExpr columnRef) throws UndefinedColumnException { if (block.getCurrentNode() != null && block.getCurrentNode().getInSchema() != null) { Column found = block.getCurrentNode().getInSchema().getColumn(columnRef.getCanonicalName()); if (found != null) { return found; } else if (block.getLatestNode() != null) { found = block.getLatestNode().getOutSchema().getColumn(columnRef.getName()); if (found != null) { return found; } } } return null; }
qualifiedName = CatalogUtil.buildFQName(qualifier, columnRef.getName());
@Test public final void testBinaryOperator1() throws CloneNotSupportedException, TajoException { QueryContext qc = createQueryContext(); Expr expr = sqlAnalyzer.parse(QUERIES[0]); LogicalPlan plan = planner.createPlan(qc, expr); LogicalOptimizer optimizer = new LogicalOptimizer(util.getConfiguration(), catalog, TablespaceManager.getInstance()); optimizer.optimize(plan); LogicalNode node = plan.getRootBlock().getRoot(); ScanNode scanNode = PlannerUtil.findTopNode(node, NodeType.SCAN); EvalNodeToExprConverter convertor = new EvalNodeToExprConverter(scanNode.getTableName()); convertor.visit(null, scanNode.getQual(), new Stack<>()); Expr resultExpr = convertor.getResult(); BinaryOperator binaryOperator = AlgebraicUtil.findTopExpr(resultExpr, OpType.GreaterThan); assertNotNull(binaryOperator); ColumnReferenceExpr column = binaryOperator.getLeft(); assertEquals("default.lineitem", column.getQualifier()); assertEquals("l_orderkey", column.getName()); LiteralValue literalValue = binaryOperator.getRight(); assertEquals("500", literalValue.getValue()); assertEquals(LiteralValue.LiteralType.Unsigned_Integer, literalValue.getValueType()); }
@Test public final void testBinaryOperator2() throws CloneNotSupportedException, TajoException { QueryContext qc = createQueryContext(); Expr expr = sqlAnalyzer.parse(QUERIES[1]); LogicalPlan plan = planner.createPlan(qc, expr); LogicalOptimizer optimizer = new LogicalOptimizer(util.getConfiguration(), catalog, TablespaceManager.getInstance()); optimizer.optimize(plan); LogicalNode node = plan.getRootBlock().getRoot(); ScanNode scanNode = PlannerUtil.findTopNode(node, NodeType.SCAN); EvalNodeToExprConverter convertor = new EvalNodeToExprConverter(scanNode.getTableName()); convertor.visit(null, scanNode.getQual(), new Stack<>()); Expr resultExpr = convertor.getResult(); BinaryOperator equals = AlgebraicUtil.findTopExpr(resultExpr, OpType.Equals); assertNotNull(equals); ColumnReferenceExpr column = equals.getLeft(); assertEquals("default.region", column.getQualifier()); assertEquals("r_name", column.getName()); LiteralValue literalValue = equals.getRight(); assertEquals("EUROPE", literalValue.getValue()); assertEquals(LiteralValue.LiteralType.String, literalValue.getValueType()); }