public static List<Target> schemaToTargets(Schema schema) { List<Target> targets = new ArrayList<>(); FieldEval eval; for (int i = 0; i < schema.size(); i++) { eval = new FieldEval(schema.getColumn(i)); targets.add(new Target(eval)); } return targets; }
public static Target [] buildGroupByTarget(GroupbyNode groupbyNode, @Nullable List<Target> groupingKeyTargets, String [] aggEvalNames) { final int groupingKeyNum = groupingKeyTargets == null ? groupbyNode.getGroupingColumns().length : groupingKeyTargets.size(); final int aggrFuncNum = aggEvalNames != null ? aggEvalNames.length : 0; EvalNode [] aggEvalNodes = groupbyNode.getAggFunctions(); Target [] targets = new Target[groupingKeyNum + aggrFuncNum]; if (groupingKeyTargets != null) { for (int groupingKeyIdx = 0; groupingKeyIdx < groupingKeyNum; groupingKeyIdx++) { targets[groupingKeyIdx] = groupingKeyTargets.get(groupingKeyIdx); } } else { for (int groupingKeyIdx = 0; groupingKeyIdx < groupingKeyNum; groupingKeyIdx++) { targets[groupingKeyIdx] = new Target(new FieldEval(groupbyNode.getGroupingColumns()[groupingKeyIdx])); } } if (aggEvalNames != null) { for (int aggrFuncIdx = 0, targetIdx = groupingKeyNum; aggrFuncIdx < aggrFuncNum; aggrFuncIdx++, targetIdx++) { targets[targetIdx] = new Target(new FieldEval(aggEvalNames[aggrFuncIdx], aggEvalNodes[aggrFuncIdx].getValueType())); } } return targets; }
private static LinkedHashSet<Target> createFieldTargetsFromRelation(QueryBlock block, RelationNode relationNode, Set<String> newlyEvaluatedRefNames) { LinkedHashSet<Target> targets = Sets.newLinkedHashSet(); for (Column column : relationNode.getLogicalSchema().getAllColumns()) { // TODO - Currently, EvalNode has DataType as a return type. So, RECORD cannot be used for any target. // The following line is a kind of hack, preventing RECORD to be used for target in the logical planning phase. // This problem should be resolved after TAJO-1402. if (column.getTypeDesc().getDataType().getType() == TajoDataTypes.Type.RECORD) { continue; } String aliasName = block.namedExprsMgr.checkAndGetIfAliasedColumn(column.getQualifiedName()); if (aliasName != null) { targets.add(new Target(new FieldEval(column), aliasName)); newlyEvaluatedRefNames.remove(aliasName); } else { targets.add(new Target(new FieldEval(column))); } } return targets; }
private static LinkedHashSet<Target> createFieldTargetsFromRelation(QueryBlock block, RelationNode relationNode, Set<String> newlyEvaluatedRefNames) { LinkedHashSet<Target> targets = Sets.newLinkedHashSet(); for (Column column : relationNode.getLogicalSchema().getAllColumns()) { // TODO - Currently, EvalNode has DataType as a return type. So, RECORD cannot be used for any target. // The following line is a kind of hack, preventing RECORD to be used for target in the logical planning phase. // This problem should be resolved after TAJO-1402. if (column.getTypeDesc().getDataType().getType() == TajoDataTypes.Type.RECORD) { continue; } String aliasName = block.namedExprsMgr.checkAndGetIfAliasedColumn(column.getQualifiedName()); if (aliasName != null) { targets.add(new Target(new FieldEval(column), aliasName)); newlyEvaluatedRefNames.remove(aliasName); } else { targets.add(new Target(new FieldEval(column))); } } return targets; }
private EvalNode buildJoinCondition(EvalNode leftField, TableSubQueryNode subQueryNode) { FieldEval rightField = new FieldEval(subQueryNode.getOutSchema().getColumn(0)); return new BinaryEval(EvalType.EQUAL, leftField, rightField); }
private EvalNode buildJoinCondition(EvalNode leftField, TableSubQueryNode subQueryNode) { FieldEval rightField = new FieldEval(subQueryNode.getOutSchema().getColumn(0)); return new BinaryEval(EvalType.EQUAL, leftField, rightField); }
@Override public EvalNode visitColumnReference(Context ctx, Stack<Expr> stack, ColumnReferenceExpr expr) throws TajoException { Column column; switch (ctx.columnRsvLevel) { case LEGACY: case RELS_ONLY: case RELS_AND_SUBEXPRS: case SUBEXPRS_AND_RELS: column = NameResolver.resolve(ctx.plan, ctx.currentBlock, expr, ctx.columnRsvLevel, ctx.includeSelfDescTable); break; default: throw new TajoInternalError("Unsupported column resolving level: " + ctx.columnRsvLevel.name()); } return new FieldEval(column); }
@Override public EvalNode visitColumnReference(Context ctx, Stack<Expr> stack, ColumnReferenceExpr expr) throws TajoException { Column column; switch (ctx.columnRsvLevel) { case LEGACY: case RELS_ONLY: case RELS_AND_SUBEXPRS: case SUBEXPRS_AND_RELS: column = NameResolver.resolve(ctx.plan, ctx.currentBlock, expr, ctx.columnRsvLevel, ctx.includeSelfDescTable); break; default: throw new TajoInternalError("Unsupported column resolving level: " + ctx.columnRsvLevel.name()); } return new FieldEval(column); }
public static Target[] schemaToTargets(Schema schema) { Target[] targets = new Target[schema.size()]; FieldEval eval; for (int i = 0; i < schema.size(); i++) { eval = new FieldEval(schema.getColumn(i)); targets[i] = new Target(eval); } return targets; }
private static EvalNode getNaturalJoinCondition(JoinNode joinNode) { Schema leftSchema = joinNode.getLeftChild().getInSchema(); Schema rightSchema = joinNode.getRightChild().getInSchema(); Schema commons = SchemaUtil.getNaturalJoinColumns(leftSchema, rightSchema); EvalNode njQual = null; EvalNode equiQual; Column leftJoinKey; Column rightJoinKey; for (Column common : commons.getRootColumns()) { leftJoinKey = leftSchema.getColumn(common.getQualifiedName()); rightJoinKey = rightSchema.getColumn(common.getQualifiedName()); equiQual = new BinaryEval(EvalType.EQUAL, new FieldEval(leftJoinKey), new FieldEval(rightJoinKey)); if (njQual == null) { njQual = equiQual; } else { njQual = new BinaryEval(EvalType.AND, njQual, equiQual); } } return njQual; }
private List<Target> buildTargets(LogicalPlanner.PlanContext context, List<NamedExpr> exprs) throws TajoException { List<Target> targets = new ArrayList<>(); for (NamedExpr namedExpr : exprs) { TajoDataTypes.DataType dataType = typeDeterminant.determineDataType(context, namedExpr.getExpr()); if (dataType.getType() == RECORD) { throw new NotImplementedException("record projection"); } if (namedExpr.hasAlias()) { targets.add(new Target(new FieldEval(new Column(namedExpr.getAlias(), dataType)))); } else { String generatedName = context.getPlan().generateUniqueColumnName(namedExpr.getExpr()); targets.add(new Target(new FieldEval(new Column(generatedName, dataType)))); } } return targets; }
private static EvalNode getNaturalJoinCondition(JoinNode joinNode) { Schema leftSchema = joinNode.getLeftChild().getInSchema(); Schema rightSchema = joinNode.getRightChild().getInSchema(); Schema commons = SchemaUtil.getNaturalJoinColumns(leftSchema, rightSchema); EvalNode njQual = null; EvalNode equiQual; Column leftJoinKey; Column rightJoinKey; for (Column common : commons.getRootColumns()) { leftJoinKey = leftSchema.getColumn(common.getQualifiedName()); rightJoinKey = rightSchema.getColumn(common.getQualifiedName()); equiQual = new BinaryEval(EvalType.EQUAL, new FieldEval(leftJoinKey), new FieldEval(rightJoinKey)); if (njQual == null) { njQual = equiQual; } else { njQual = new BinaryEval(EvalType.AND, njQual, equiQual); } } return njQual; }
private Target [] buildTargets(LogicalPlanner.PlanContext context, NamedExpr [] exprs) throws TajoException { Target [] targets = new Target[exprs.length]; for (int i = 0; i < exprs.length; i++) { NamedExpr namedExpr = exprs[i]; TajoDataTypes.DataType dataType = typeDeterminant.determineDataType(context, namedExpr.getExpr()); if (namedExpr.hasAlias()) { targets[i] = new Target(new FieldEval(new Column(namedExpr.getAlias(), dataType))); } else { String generatedName = context.getPlan().generateUniqueColumnName(namedExpr.getExpr()); targets[i] = new Target(new FieldEval(new Column(generatedName, dataType))); } } return targets; }
private static void pushDownIfComplexTermInJoinCondition(Context ctx, EvalNode cnf, EvalNode term) throws TajoException { // If one of both terms in a binary operator is a complex expression, the binary operator will require // multiple phases. In this case, join cannot evaluate a binary operator. // So, we should prevent dividing the binary operator into more subexpressions. if (term.getType() != EvalType.FIELD && !(term instanceof BinaryEval) && term.getType() != EvalType.ROW_CONSTANT && term.getType() != EvalType.CONST) { String refName = ctx.addExpr(term); EvalTreeUtil.replace(cnf, term, new FieldEval(refName, term.getValueType())); } }
private static void pushDownIfComplexTermInJoinCondition(Context ctx, EvalNode cnf, EvalNode term) throws TajoException { // If one of both terms in a binary operator is a complex expression, the binary operator will require // multiple phases. In this case, join cannot evaluate a binary operator. // So, we should prevent dividing the binary operator into more subexpressions. if (term.getType() != EvalType.FIELD && !(term instanceof BinaryEval) && term.getType() != EvalType.ROW_CONSTANT && term.getType() != EvalType.CONST) { String refName = ctx.addExpr(term); EvalTreeUtil.replace(cnf, term, new FieldEval(refName, term.getValueType())); } }
public LogicalNode visitFilter(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, SelectionNode node, Stack<LogicalNode> stack) throws TajoException { Context newContext = new Context(context); String referenceName = newContext.targetListMgr.add(node.getQual()); newContext.addNecessaryReferences(node.getQual()); LogicalNode child = super.visitFilter(newContext, plan, block, node, stack); node.setInSchema(child.getOutSchema()); node.setOutSchema(child.getOutSchema()); Target target = context.targetListMgr.getTarget(referenceName); if (newContext.targetListMgr.isEvaluated(referenceName)) { node.setQual(new FieldEval(target.getNamedColumn())); } else { node.setQual(target.getEvalTree()); newContext.targetListMgr.markAsEvaluated(target); } return node; }
@Override public LogicalNode visitHaving(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, HavingNode node, Stack<LogicalNode> stack) throws TajoException { Context newContext = new Context(context); String referenceName = newContext.targetListMgr.add(node.getQual()); newContext.addNecessaryReferences(node.getQual()); LogicalNode child = super.visitHaving(newContext, plan, block, node, stack); node.setInSchema(child.getOutSchema()); node.setOutSchema(child.getOutSchema()); Target target = context.targetListMgr.getTarget(referenceName); if (newContext.targetListMgr.isEvaluated(referenceName)) { node.setQual(new FieldEval(target.getNamedColumn())); } else { node.setQual(target.getEvalTree()); newContext.targetListMgr.markAsEvaluated(target); } return node; }
public LogicalNode visitFilter(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, SelectionNode node, Stack<LogicalNode> stack) throws TajoException { Context newContext = new Context(context); String referenceName = newContext.targetListMgr.add(node.getQual()); newContext.addNecessaryReferences(node.getQual()); LogicalNode child = super.visitFilter(newContext, plan, block, node, stack); node.setInSchema(child.getOutSchema()); node.setOutSchema(child.getOutSchema()); Target target = context.targetListMgr.getTarget(referenceName); if (newContext.targetListMgr.isEvaluated(referenceName)) { node.setQual(new FieldEval(target.getNamedColumn())); } else { node.setQual(target.getEvalTree()); newContext.targetListMgr.markAsEvaluated(target); } return node; }
@Override public LogicalNode visitHaving(Context context, LogicalPlan plan, LogicalPlan.QueryBlock block, HavingNode node, Stack<LogicalNode> stack) throws TajoException { Context newContext = new Context(context); String referenceName = newContext.targetListMgr.add(node.getQual()); newContext.addNecessaryReferences(node.getQual()); LogicalNode child = super.visitHaving(newContext, plan, block, node, stack); node.setInSchema(child.getOutSchema()); node.setOutSchema(child.getOutSchema()); Target target = context.targetListMgr.getTarget(referenceName); if (newContext.targetListMgr.isEvaluated(referenceName)) { node.setQual(new FieldEval(target.getNamedColumn())); } else { node.setQual(target.getEvalTree()); newContext.targetListMgr.markAsEvaluated(target); } return node; }
@Test public void testTupleEval() throws CloneNotSupportedException { ConstEval e1 = new ConstEval(DatumFactory.createInt4(1)); assertCloneEqual(e1); FieldEval e2 = new FieldEval("table1.score", CatalogUtil.newSimpleDataType(INT4)); // it indicates assertCloneEqual(e2); Schema schema1 = SchemaBuilder.builder() .add("table1.id", INT4) .add("table1.score", INT4) .build(); BinaryEval expr = new BinaryEval(EvalType.PLUS, e1, e2); expr.bind(null, schema1); assertCloneEqual(expr); VTuple tuple = new VTuple(2); tuple.put(0, DatumFactory.createInt4(1)); // put 0th field tuple.put(1, DatumFactory.createInt4(99)); // put 1th field // the result of evaluation must be 100. assertEquals(expr.eval(tuple).asInt4(), 100); }