org.apache.calcite.rel.core.Project Java Examples
The following examples show how to use
org.apache.calcite.rel.core.Project.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DrillRelOptUtil.java From Bats with Apache License 2.0 | 6 votes |
/** * Find whether the given project rel has unknown output schema. This would happen if the * project has CONVERT_FROMJSON which can only derive the schema after evaluation is performed * @param project : The project rel * @return : Return true if the project output schema is unknown. Otherwise, false. */ public static boolean isProjectOutputSchemaUnknown(Project project) { try { RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) { @Override public Void visitCall(RexCall call) { if ("convert_fromjson".equals(call.getOperator().getName().toLowerCase())) { throw new Util.FoundOne(call); /* throw exception to interrupt tree walk (this is similar to other utility methods in RexUtil.java */ } return super.visitCall(call); } }; for (RexNode rex : project.getProjects()) { rex.accept(visitor); } } catch (Util.FoundOne e) { Util.swallow(e, null); return true; } return false; }
Example #2
Source File: RelToSqlConverter.java From calcite with Apache License 2.0 | 6 votes |
/** @see #dispatch */ public Result visit(Project e) { e.getVariablesSet(); Result x = visitChild(0, e.getInput()); parseCorrelTable(e, x); if (isStar(e.getProjects(), e.getInput().getRowType(), e.getRowType())) { return x; } final Builder builder = x.builder(e, Clause.SELECT); final List<SqlNode> selectList = new ArrayList<>(); for (RexNode ref : e.getProjects()) { SqlNode sqlExpr = builder.context.toSql(null, ref); if (SqlUtil.isNullLiteral(sqlExpr, false)) { sqlExpr = castNullType(sqlExpr, e.getRowType().getFieldList().get(selectList.size())); } addSelect(selectList, sqlExpr, e.getRowType()); } builder.setSelect(new SqlNodeList(selectList, POS)); return builder.result(); }
Example #3
Source File: RelBuilder.java From Bats with Apache License 2.0 | 6 votes |
/** Creates a {@link Project} of the given * expressions and field names, and optionally optimizing. * * <p>If {@code fieldNames} is null, or if a particular entry in * {@code fieldNames} is null, derives field names from the input * expressions. * * <p>If {@code force} is false, * and the input is a {@code Project}, * and the expressions make the trivial projection ($0, $1, ...), * modifies the input. * * @param nodes Expressions * @param fieldNames Suggested field names, or null to generate * @param force Whether to create a renaming Project if the * projections are trivial */ public RelBuilder projectNamed(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) { @SuppressWarnings({ "unchecked", "rawtypes" }) final List<? extends RexNode> nodeList = nodes instanceof List ? (List) nodes : ImmutableList.copyOf(nodes); final List<String> fieldNameList = fieldNames == null ? null : fieldNames instanceof List ? (List<String>) fieldNames : ImmutableNullableList.copyOf(fieldNames); final RelNode input = peek(); final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), nodeList, fieldNameList, SqlValidatorUtil.F_SUGGESTER); if (!force && RexUtil.isIdentity(nodeList, input.getRowType())) { if (input instanceof Project && fieldNames != null) { // Rename columns of child projection if desired field names are given. final Frame frame = stack.pop(); final Project childProject = (Project) frame.rel; final Project newInput = childProject.copy(childProject.getTraitSet(), childProject.getInput(), childProject.getProjects(), rowType); stack.push(new Frame(newInput, frame.fields)); } } else { project(nodeList, rowType.getFieldNames(), force); } return this; }
Example #4
Source File: ProjectSortTransposeRule.java From Bats with Apache License 2.0 | 6 votes |
public void onMatch(RelOptRuleCall call) { final Project project = call.rel(0); final Sort sort = call.rel(1); if (sort.getClass() != Sort.class) { return; } RelNode newProject = project.copy( project.getTraitSet(), ImmutableList.of(sort.getInput())); final Sort newSort = sort.copy( sort.getTraitSet(), newProject, sort.getCollation(), sort.offset, sort.fetch); call.transformTo(newSort); }
Example #5
Source File: RelOptUtil.java From calcite with Apache License 2.0 | 6 votes |
/** As {@link #pushPastProject}, but returns null if the resulting expressions * are significantly more complex. * * @param bloat Maximum allowable increase in complexity */ public static @Nullable List<RexNode> pushPastProjectUnlessBloat( List<? extends RexNode> nodes, Project project, int bloat) { if (bloat < 0) { // If bloat is negative never merge. return null; } if (RexOver.containsOver(nodes, null) && RexOver.containsOver(project.getProjects(), null)) { // Is it valid relational algebra to apply windowed function to a windowed // function? Possibly. But it's invalid SQL, so don't go there. return null; } final List<RexNode> list = pushPastProject(nodes, project); final int bottomCount = RexUtil.nodeCount(project.getProjects()); final int topCount = RexUtil.nodeCount(nodes); final int mergedCount = RexUtil.nodeCount(list); if (mergedCount > bottomCount + topCount + bloat) { // The merged expression is more complex than the input expressions. // Do not merge. return null; } return list; }
Example #6
Source File: PermutationTestCase.java From calcite with Apache License 2.0 | 6 votes |
@Test void testProjectPermutation() { final RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl(); final RexBuilder builder = new RexBuilder(typeFactory); final RelDataType doubleType = typeFactory.createSqlType(SqlTypeName.DOUBLE); // A project with [1, 1] is not a permutation, so should return null final Permutation perm = Project.getPermutation(2, ImmutableList.of(builder.makeInputRef(doubleType, 1), builder.makeInputRef(doubleType, 1))); assertThat(perm, nullValue()); // A project with [0, 1, 0] is not a permutation, so should return null final Permutation perm1 = Project.getPermutation(2, ImmutableList.of(builder.makeInputRef(doubleType, 0), builder.makeInputRef(doubleType, 1), builder.makeInputRef(doubleType, 0))); assertThat(perm1, nullValue()); // A project of [1, 0] is a valid permutation! final Permutation perm2 = Project.getPermutation(2, ImmutableList.of(builder.makeInputRef(doubleType, 1), builder.makeInputRef(doubleType, 0))); assertThat(perm2, is(new Permutation(new int[]{1, 0}))); }
Example #7
Source File: DrillMergeProjectRule.java From Bats with Apache License 2.0 | 5 votes |
@Override public boolean matches(RelOptRuleCall call) { Project topProject = call.rel(0); Project bottomProject = call.rel(1); // We have a complex output type do not fire the merge project rule if (checkComplexOutput(topProject) || checkComplexOutput(bottomProject)) { return false; } return true; }
Example #8
Source File: ProjectRemoveRule.java From calcite with Apache License 2.0 | 5 votes |
/** * Creates a ProjectRemoveRule. * * @param relBuilderFactory Builder for relational expressions */ public ProjectRemoveRule(RelBuilderFactory relBuilderFactory) { // Create a specialized operand to detect non-matches early. This keeps // the rule queue short. super(operandJ(Project.class, null, ProjectRemoveRule::isTrivial, any()), relBuilderFactory, null); }
Example #9
Source File: RelDecorrelator.java From flink with Apache License 2.0 | 5 votes |
/** Returns a literal output field, or null if it is not literal. */ private static RexLiteral projectedLiteral(RelNode rel, int i) { if (rel instanceof Project) { final Project project = (Project) rel; final RexNode node = project.getProjects().get(i); if (node instanceof RexLiteral) { return (RexLiteral) node; } } return null; }
Example #10
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testNodeTypeCountAggregate() { final String sql = "select deptno from emp group by deptno"; final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>(); expected.put(TableScan.class, 1); expected.put(Project.class, 1); expected.put(Aggregate.class, 1); checkNodeTypeCount(sql, expected); }
Example #11
Source File: ProjectRule.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public boolean matches(RelOptRuleCall call) { // this cannot operate on a project that has a flatten in it. final Project project = call.rel(0); for (RexNode e : project.getChildExps()) { if (FlattenVisitors.hasFlatten(e)) { return false; } } return true; }
Example #12
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testNodeTypeCountRightJoinEmptyFinite() { final String sql = "select * from (select * from emp limit 0) as emp\n" + "right join (select * from dept limit 4) as dept\n" + "on emp.deptno = dept.deptno"; final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>(); expected.put(TableScan.class, 2); expected.put(Join.class, 1); expected.put(Project.class, 3); expected.put(Sort.class, 2); checkNodeTypeCount(sql, expected); }
Example #13
Source File: ProjectSetOpTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { LogicalProject origProj = call.rel(0); SetOp setOp = call.rel(1); // cannot push project past a distinct if (!setOp.all) { return; } // locate all fields referenced in the projection PushProjector pushProject = new PushProjector( origProj, null, setOp, preserveExprCondition, call.builder()); pushProject.locateAllRefs(); List<RelNode> newSetOpInputs = new ArrayList<>(); int[] adjustments = pushProject.getAdjustments(); // push the projects completely below the setop; this // is different from pushing below a join, where we decompose // to try to keep expensive expressions above the join, // because UNION ALL does not have any filtering effect, // and it is the only operator this rule currently acts on for (RelNode input : setOp.getInputs()) { // be lazy: produce two ProjectRels, and let another rule // merge them (could probably just clone origProj instead?) Project p = pushProject.createProjectRefsAndExprs(input, true, false); newSetOpInputs.add(pushProject.createNewProject(p, adjustments)); } // create a new setop whose children are the ProjectRels created above SetOp newSetOp = setOp.copy(setOp.getTraitSet(), newSetOpInputs); call.transformTo(newSetOp); }
Example #14
Source File: FilterProjectTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { final Filter filter = call.rel(0); final Project project = call.rel(1); if (RexOver.containsOver(project.getProjects(), null)) { // In general a filter cannot be pushed below a windowing calculation. // Applying the filter before the aggregation function changes // the results of the windowing invocation. // // When the filter is on the PARTITION BY expression of the OVER clause // it can be pushed down. For now we don't support this. return; } // convert the filter to one that references the child of the project RexNode newCondition = RelOptUtil.pushPastProject(filter.getCondition(), project); final RelBuilder relBuilder = call.builder(); RelNode newFilterRel; if (copyFilter) { newFilterRel = filter.copy(filter.getTraitSet(), project.getInput(), simplifyFilterCondition(newCondition, call)); } else { newFilterRel = relBuilder.push(project.getInput()).filter(newCondition).build(); } RelNode newProjRel = copyProject ? project.copy(project.getTraitSet(), newFilterRel, project.getProjects(), project.getRowType()) : relBuilder.push(newFilterRel) .project(project.getProjects(), project.getRowType().getFieldNames()) .build(); call.transformTo(newProjRel); }
Example #15
Source File: RelBuilder.java From calcite with Apache License 2.0 | 5 votes |
/** Creates a {@link Project} of the given * expressions and field names, and optionally optimizing. * * <p>If {@code fieldNames} is null, or if a particular entry in * {@code fieldNames} is null, derives field names from the input * expressions. * * <p>If {@code force} is false, * and the input is a {@code Project}, * and the expressions make the trivial projection ($0, $1, ...), * modifies the input. * * @param nodes Expressions * @param fieldNames Suggested field names, or null to generate * @param force Whether to create a renaming Project if the * projections are trivial */ public RelBuilder projectNamed(Iterable<? extends RexNode> nodes, Iterable<String> fieldNames, boolean force) { @SuppressWarnings("unchecked") final List<? extends RexNode> nodeList = nodes instanceof List ? (List) nodes : ImmutableList.copyOf(nodes); final List<String> fieldNameList = fieldNames == null ? null : fieldNames instanceof List ? (List<String>) fieldNames : ImmutableNullableList.copyOf(fieldNames); final RelNode input = peek(); final RelDataType rowType = RexUtil.createStructType(cluster.getTypeFactory(), nodeList, fieldNameList, SqlValidatorUtil.F_SUGGESTER); if (!force && RexUtil.isIdentity(nodeList, input.getRowType())) { if (input instanceof Project && fieldNames != null) { // Rename columns of child projection if desired field names are given. final Frame frame = stack.pop(); final Project childProject = (Project) frame.rel; final Project newInput = childProject.copy(childProject.getTraitSet(), childProject.getInput(), childProject.getProjects(), rowType); stack.push(new Frame(newInput.attachHints(childProject.getHints()), frame.fields)); } } else { project(nodeList, rowType.getFieldNames(), force); } return this; }
Example #16
Source File: ProjectJoinTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
/** * Creates a ProjectJoinTransposeRule with an explicit condition. * * @param preserveExprCondition Condition for expressions that should be * preserved in the projection */ public ProjectJoinTransposeRule( PushProjector.ExprCondition preserveExprCondition, RelBuilderFactory relFactory) { super( operand(Project.class, operand(Join.class, any())), relFactory, null); this.preserveExprCondition = preserveExprCondition; }
Example #17
Source File: FilterProjectTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
/** * Creates a FilterProjectTransposeRule. * * <p>If {@code copyFilter} is true, creates the same kind of Filter as * matched in the rule, otherwise it creates a Filter using the RelBuilder * obtained by the {@code relBuilderFactory}. * Similarly for {@code copyProject}. * * <p>Defining predicates for the Filter (using {@code filterPredicate}) * and/or the Project (using {@code projectPredicate} allows making the rule * more restrictive. */ public <F extends Filter, P extends Project> FilterProjectTransposeRule( Class<F> filterClass, Predicate<? super F> filterPredicate, Class<P> projectClass, Predicate<? super P> projectPredicate, boolean copyFilter, boolean copyProject, RelBuilderFactory relBuilderFactory) { this( operandJ(filterClass, null, filterPredicate, operandJ(projectClass, null, projectPredicate, any())), copyFilter, copyProject, relBuilderFactory); }
Example #18
Source File: RelToSqlConverter.java From calcite with Apache License 2.0 | 5 votes |
private Result visitAggregate(Aggregate e, List<Integer> groupKeyList) { // "select a, b, sum(x) from ( ... ) group by a, b" final Result x = visitChild(0, e.getInput()); final Builder builder; if (e.getInput() instanceof Project) { builder = x.builder(e); builder.clauses.add(Clause.GROUP_BY); } else { builder = x.builder(e, Clause.GROUP_BY); } final List<SqlNode> selectList = new ArrayList<>(); final List<SqlNode> groupByList = generateGroupList(builder, selectList, e, groupKeyList); return buildAggregate(e, builder, selectList, groupByList); }
Example #19
Source File: JoinUtils.java From Bats with Apache License 2.0 | 5 votes |
@Override public RelNode visit(RelNode other) { // RelShuttleImpl doesn't have visit methods for Project and RelSubset. if (other instanceof RelSubset) { return visit((RelSubset) other); } else if (other instanceof Project) { return visit((Project) other); } return super.visit(other); }
Example #20
Source File: ProjectToWindowRule.java From calcite with Apache License 2.0 | 5 votes |
/** * Creates a ProjectToWindowRule. * * @param relBuilderFactory Builder for relational expressions */ public ProjectToLogicalProjectAndWindowRule( RelBuilderFactory relBuilderFactory) { super( operandJ(Project.class, null, project -> RexOver.containsOver(project.getProjects(), null), any()), relBuilderFactory, "ProjectToWindowRule:project"); }
Example #21
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testNodeTypeCountSortLimitOffsetOnFinite() { final String sql = "select * from (select * from emp limit 12)\n" + "order by ename limit 20 offset 5"; final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>(); expected.put(TableScan.class, 1); expected.put(Project.class, 2); expected.put(Sort.class, 2); checkNodeTypeCount(sql, expected); }
Example #22
Source File: FilterAggStarRule.java From quark with Apache License 2.0 | 5 votes |
@Override public void onMatch(RelOptRuleCall call) { final LogicalFilter filter = call.rel(0); Aggregate aggregate2 = call.rel(1); final Project project = call.rel(2); final StarTable.StarTableScan scan = call.rel(3); apply(call, null, filter, aggregate2, project, scan); }
Example #23
Source File: AbstractMaterializedViewRule.java From Bats with Apache License 2.0 | 5 votes |
@Override public Pair<RelNode, RelNode> pushFilterToOriginalViewPlan(RelBuilder builder, RelNode topViewProject, RelNode viewNode, RexNode cond) { // We add (and push) the filter to the view plan before triggering the rewriting. // This is useful in case some of the columns can be folded to same value after // filter is added. HepProgramBuilder pushFiltersProgram = new HepProgramBuilder(); if (topViewProject != null) { pushFiltersProgram.addRuleInstance(filterProjectTransposeRule); } pushFiltersProgram.addRuleInstance(this.filterAggregateTransposeRule) .addRuleInstance(this.aggregateProjectPullUpConstantsRule).addRuleInstance(this.projectMergeRule); final HepPlanner tmpPlanner = new HepPlanner(pushFiltersProgram.build()); // Now that the planner is created, push the node RelNode topNode = builder.push(topViewProject != null ? topViewProject : viewNode).filter(cond).build(); tmpPlanner.setRoot(topNode); topNode = tmpPlanner.findBestExp(); RelNode resultTopViewProject = null; RelNode resultViewNode = null; while (topNode != null) { if (topNode instanceof Project) { if (resultTopViewProject != null) { // Both projects could not be merged, we will bail out return Pair.of(topViewProject, viewNode); } resultTopViewProject = topNode; topNode = topNode.getInput(0); } else if (topNode instanceof Aggregate) { resultViewNode = topNode; topNode = null; } else { // We move to the child topNode = topNode.getInput(0); } } return Pair.of(resultTopViewProject, resultViewNode); }
Example #24
Source File: RelOptUtil.java From calcite with Apache License 2.0 | 5 votes |
private static RexShuttle pushShuttle(final Project project) { return new RexShuttle() { @Override public RexNode visitInputRef(RexInputRef ref) { return project.getProjects().get(ref.getIndex()); } }; }
Example #25
Source File: RelMetadataTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testNodeTypeCountAggregateEmptyKeyOnEmptyTable() { final String sql = "select count(*) from (select * from emp limit 0)"; final Map<Class<? extends RelNode>, Integer> expected = new HashMap<>(); expected.put(TableScan.class, 1); expected.put(Project.class, 2); expected.put(Aggregate.class, 1); expected.put(Sort.class, 1); checkNodeTypeCount(sql, expected); }
Example #26
Source File: AbstractMaterializedViewRule.java From Bats with Apache License 2.0 | 5 votes |
@Override protected boolean isValidPlan(Project topProject, RelNode node, RelMetadataQuery mq) { if (!(node instanceof Aggregate)) { return false; } Aggregate aggregate = (Aggregate) node; if (aggregate.getGroupType() != Aggregate.Group.SIMPLE) { // TODO: Rewriting with grouping sets not supported yet return false; } return isValidRelNodePlan(aggregate.getInput(), mq); }
Example #27
Source File: SubQueryDecorrelator.java From flink with Apache License 2.0 | 5 votes |
/** Returns a literal output field, or null if it is not literal. */ private static RexLiteral projectedLiteral(RelNode rel, int i) { if (rel instanceof Project) { final Project project = (Project) rel; final RexNode node = project.getProjects().get(i); if (node instanceof RexLiteral) { return (RexLiteral) node; } } return null; }
Example #28
Source File: RelDecorrelator.java From calcite with Apache License 2.0 | 5 votes |
@Override public RelNode visit(RelNode other) { if (other instanceof Join) { Join join = (Join) other; try { stack.push(join); join.getCondition().accept(rexVisitor(join)); } finally { stack.pop(); } return visitJoin(join); } else if (other instanceof Correlate) { Correlate correlate = (Correlate) other; mapCorToCorRel.put(correlate.getCorrelationId(), correlate); return visitJoin(correlate); } else if (other instanceof Filter) { Filter filter = (Filter) other; try { stack.push(filter); filter.getCondition().accept(rexVisitor(filter)); } finally { stack.pop(); } } else if (other instanceof Project) { Project project = (Project) other; try { stack.push(project); for (RexNode node : project.getProjects()) { node.accept(rexVisitor(project)); } } finally { stack.pop(); } } return super.visit(other); }
Example #29
Source File: RelMdPopulationSize.java From calcite with Apache License 2.0 | 5 votes |
public Double getPopulationSize(Project rel, RelMetadataQuery mq, ImmutableBitSet groupKey) { ImmutableBitSet.Builder baseCols = ImmutableBitSet.builder(); ImmutableBitSet.Builder projCols = ImmutableBitSet.builder(); List<RexNode> projExprs = rel.getProjects(); RelMdUtil.splitCols(projExprs, groupKey, baseCols, projCols); Double population = mq.getPopulationSize(rel.getInput(), baseCols.build()); if (population == null) { return null; } // No further computation required if the projection expressions are // all column references if (projCols.cardinality() == 0) { return population; } for (int bit : projCols.build()) { Double subRowCount = RelMdUtil.cardOfProjExpr(mq, rel, projExprs.get(bit)); if (subRowCount == null) { return null; } population *= subRowCount; } // REVIEW zfong 6/22/06 - Broadbase did not have the call to // numDistinctVals. This is needed; otherwise, population can be // larger than the number of rows in the RelNode. return RelMdUtil.numDistinctVals(population, mq.getRowCount(rel)); }
Example #30
Source File: ProjectMultiJoinMergeRule.java From calcite with Apache License 2.0 | 5 votes |
/** * Creates a ProjectMultiJoinMergeRule that uses a generic * {@link Project} * @param projectClass project class * @param relBuilderFactory builder factory for relational expressions */ public ProjectMultiJoinMergeRule(Class<? extends Project> projectClass, RelBuilderFactory relBuilderFactory) { super( operand(projectClass, operand(MultiJoin.class, any())), relBuilderFactory, null); }