Java Code Examples for org.apache.calcite.rel.logical.LogicalProject#getProjects()

The following examples show how to use org.apache.calcite.rel.logical.LogicalProject#getProjects() . 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: OLAPProjectRule.java    From kylin with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode convert(final RelNode rel) {

    //  KYLIN-3281
    //  OLAPProjectRule can't normal working with projectRel[input=sortRel]
    final LogicalProject project = (LogicalProject) rel;
    final RelNode convertChild = convert(project.getInput(),
            project.getInput().getTraitSet().replace(OLAPRel.CONVENTION));
    final RelOptCluster cluster = convertChild.getCluster();
    final RelTraitSet traitSet = cluster.traitSet().replace(OLAPRel.CONVENTION)
            .replaceIfs(RelCollationTraitDef.INSTANCE, new Supplier<List<RelCollation>>() {
                public List<RelCollation> get() {
                    //  CALCITE-88
                    return RelMdCollation.project(cluster.getMetadataQuery(), convertChild, project.getProjects());
                }
            });
    return new OLAPProjectRel(convertChild.getCluster(), traitSet, convertChild, project.getProjects(),
            project.getRowType());
}
 
Example 2
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalProject project) {
	hasOverNode = RexOver.containsOver(project.getProjects(), null);
	final boolean hasSubQuery = RexUtil.SubQueryFinder.find(project.getProjects()) != null;
	try {
		if (!corNodeStack.isEmpty()) {
			mapSubQueryNodeToCorSet.put(project, corNodeStack.peek().getVariablesSet());
		}
		if (hasSubQuery) {
			corNodeStack.push(project);
		}
		checkCorCondition(project);
		for (RexNode node : project.getProjects()) {
			node.accept(rexVisitor(project));
		}
	} finally {
		if (hasSubQuery) {
			corNodeStack.pop();
		}
	}
	return super.visit(project);
}
 
Example 3
Source File: FlinkSemiAntiJoinProjectTransposeRule.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matches(RelOptRuleCall call) {
	LogicalJoin join = call.rel(0);
	LogicalProject project = call.rel(1);

	// only accept SEMI/ANTI join
	JoinRelType joinType = join.getJoinType();
	if (joinType != JoinRelType.SEMI && joinType != JoinRelType.ANTI) {
		return false;
	}
	// all expressions in Project should be RexInputRef
	for (RexNode p : project.getProjects()) {
		if (!(p instanceof RexInputRef)) {
			return false;
		}
	}
	return true;
}
 
Example 4
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalProject project) {
	hasOverNode = RexOver.containsOver(project.getProjects(), null);
	final boolean hasSubQuery = RexUtil.SubQueryFinder.find(project.getProjects()) != null;
	try {
		if (!corNodeStack.isEmpty()) {
			mapSubQueryNodeToCorSet.put(project, corNodeStack.peek().getVariablesSet());
		}
		if (hasSubQuery) {
			corNodeStack.push(project);
		}
		checkCorCondition(project);
		for (RexNode node : project.getProjects()) {
			node.accept(rexVisitor(project));
		}
	} finally {
		if (hasSubQuery) {
			corNodeStack.pop();
		}
	}
	return super.visit(project);
}
 
Example 5
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * check whether a node has some input which have correlation condition.
 * e.g. SELECT * FROM l WHERE EXISTS (SELECT * FROM r LEFT JOIN (SELECT * FROM t WHERE t.j=l.b) t1 ON r.f=t1.k)
 * the above sql can not be converted to semi-join plan,
 * because the right input of Left-Join has the correlation condition(t.j=l.b).
 */
private void checkCorConditionOfInput(final RelNode input) {
	final RelShuttleImpl shuttle = new RelShuttleImpl() {
		final RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
			@Override
			public Void visitCorrelVariable(RexCorrelVariable correlVariable) {
				hasUnsupportedCorCondition = true;
				return super.visitCorrelVariable(correlVariable);
			}
		};

		@Override
		public RelNode visit(LogicalFilter filter) {
			filter.getCondition().accept(visitor);
			return super.visit(filter);
		}

		@Override
		public RelNode visit(LogicalProject project) {
			for (RexNode rex : project.getProjects()) {
				rex.accept(visitor);
			}
			return super.visit(project);
		}

		@Override
		public RelNode visit(LogicalJoin join) {
			join.getCondition().accept(visitor);
			return super.visit(join);
		}
	};
	input.accept(shuttle);
}
 
Example 6
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void rewriteRel(LogicalProject rel) {
  RewriteRexShuttle shuttle = new RewriteRexShuttle();
  List<RexNode> oldProjects = rel.getProjects();
  List<String> oldNames = rel.getRowType().getFieldNames();
  List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
  flattenProjections(shuttle, oldProjects, oldNames, "", flattenedExpList);
  RelNode newInput = getNewForOldRel(rel.getInput());
  List<RexNode> newProjects = Pair.left(flattenedExpList);
  List<String> newNames = Pair.right(flattenedExpList);
  final RelNode newRel = relBuilder.push(newInput)
      .projectNamed(newProjects, newNames, true)
      .hints(rel.getHints())
      .build();
  setNewForOldRel(rel, newRel);
}
 
Example 7
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(LogicalProject project) {
    try {
        stack.push(project);
        for (RexNode node : project.getProjects()) {
            node.accept(rexVisitor(project));
        }
    } finally {
        stack.pop();
    }
    return super.visit(project);
}
 
Example 8
Source File: SemiJoinProjectTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  SemiJoin semiJoin = call.rel(0);
  LogicalProject project = call.rel(1);

  // Convert the LHS semi-join keys to reference the child projection
  // expression; all projection expressions must be RexInputRefs,
  // otherwise, we wouldn't have created this semi-join.
  final List<Integer> newLeftKeys = new ArrayList<>();
  final List<Integer> leftKeys = semiJoin.getLeftKeys();
  final List<RexNode> projExprs = project.getProjects();
  for (int leftKey : leftKeys) {
    RexInputRef inputRef = (RexInputRef) projExprs.get(leftKey);
    newLeftKeys.add(inputRef.getIndex());
  }

  // convert the semijoin condition to reflect the LHS with the project
  // pulled up
  RexNode newCondition = adjustCondition(project, semiJoin);

  SemiJoin newSemiJoin =
      SemiJoin.create(project.getInput(), semiJoin.getRight(), newCondition,
          ImmutableIntList.copyOf(newLeftKeys), semiJoin.getRightKeys());

  // Create the new projection.  Note that the projection expressions
  // are the same as the original because they only reference the LHS
  // of the semijoin and the semijoin only projects out the LHS
  final RelBuilder relBuilder = call.builder();
  relBuilder.push(newSemiJoin);
  relBuilder.project(projExprs, project.getRowType().getFieldNames());

  call.transformTo(relBuilder.build());
}
 
Example 9
Source File: MongoRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalProject project = (LogicalProject) rel;
  final RelTraitSet traitSet = project.getTraitSet().replace(out);
  return new MongoProject(project.getCluster(), traitSet,
      convert(project.getInput(), out), project.getProjects(),
      project.getRowType());
}
 
Example 10
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RelNode visit(LogicalProject project) {
  try {
    stack.push(project);
    for (RexNode node : project.getProjects()) {
      node.accept(rexVisitor(project));
    }
  } finally {
    stack.pop();
  }
  return super.visit(project);
}
 
Example 11
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * check whether a node has some input which have correlation condition.
 * e.g. SELECT * FROM l WHERE EXISTS (SELECT * FROM r LEFT JOIN (SELECT * FROM t WHERE t.j=l.b) t1 ON r.f=t1.k)
 * the above sql can not be converted to semi-join plan,
 * because the right input of Left-Join has the correlation condition(t.j=l.b).
 */
private void checkCorConditionOfInput(final RelNode input) {
	final RelShuttleImpl shuttle = new RelShuttleImpl() {
		final RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
			@Override
			public Void visitCorrelVariable(RexCorrelVariable correlVariable) {
				hasUnsupportedCorCondition = true;
				return super.visitCorrelVariable(correlVariable);
			}
		};

		@Override
		public RelNode visit(LogicalFilter filter) {
			filter.getCondition().accept(visitor);
			return super.visit(filter);
		}

		@Override
		public RelNode visit(LogicalProject project) {
			for (RexNode rex : project.getProjects()) {
				rex.accept(visitor);
			}
			return super.visit(project);
		}

		@Override
		public RelNode visit(LogicalJoin join) {
			join.getCondition().accept(visitor);
			return super.visit(join);
		}
	};
	input.accept(shuttle);
}
 
Example 12
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  LogicalAggregate singleAggregate = call.rel(0);
  LogicalProject project = call.rel(1);
  LogicalAggregate aggregate = call.rel(2);

  // check singleAggRel is single_value agg
  if ((!singleAggregate.getGroupSet().isEmpty())
      || (singleAggregate.getAggCallList().size() != 1)
      || !(singleAggregate.getAggCallList().get(0).getAggregation()
      instanceof SqlSingleValueAggFunction)) {
    return;
  }

  // check projRel only projects one expression
  // check this project only projects one expression, i.e. scalar
  // sub-queries.
  List<RexNode> projExprs = project.getProjects();
  if (projExprs.size() != 1) {
    return;
  }

  // check the input to project is an aggregate on the entire input
  if (!aggregate.getGroupSet().isEmpty()) {
    return;
  }

  // singleAggRel produces a nullable type, so create the new
  // projection that casts proj expr to a nullable type.
  final RelBuilder relBuilder = call.builder();
  final RelDataType type =
      relBuilder.getTypeFactory()
          .createTypeWithNullability(projExprs.get(0).getType(), true);
  final RexNode cast =
      relBuilder.getRexBuilder().makeCast(type, projExprs.get(0));
  relBuilder.push(aggregate)
      .project(cast);
  call.transformTo(relBuilder.build());
}
 
Example 13
Source File: GeodeRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public boolean matches(RelOptRuleCall call) {
  LogicalProject project = call.rel(0);
  for (RexNode e : project.getProjects()) {
    if (e.getType().getSqlTypeName() == SqlTypeName.GEOMETRY) {
      // For spatial Functions Drop to Calcite Enumerable
      return false;
    }
  }

  return true;
}
 
Example 14
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RelNode visit(LogicalProject project) {
  try {
    stack.push(project);
    for (RexNode node : project.getProjects()) {
      node.accept(rexVisitor(project));
    }
  } finally {
    stack.pop();
  }
  return super.visit(project);
}
 
Example 15
Source File: ElasticsearchProjectRule.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
@Override public RelNode convert(RelNode relNode) {
    final LogicalProject project = (LogicalProject) relNode;
    final RelTraitSet traitSet = project.getTraitSet().replace(getOutTrait());
    return new ElasticsearchProject(project.getCluster(), traitSet,
            convert(project.getInput(), getOutTrait()), project.getProjects(), project.getRowType());
}
 
Example 16
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public RelNode convert(RelNode relNode) {
  final LogicalProject project = (LogicalProject) relNode;
  final RelTraitSet traitSet = project.getTraitSet().replace(out);
  return new ElasticsearchProject(project.getCluster(), traitSet,
    convert(project.getInput(), out), project.getProjects(), project.getRowType());
}
 
Example 17
Source File: PigRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
public RelNode convert(RelNode rel) {
  final LogicalProject project = (LogicalProject) rel;
  final RelTraitSet traitSet = project.getTraitSet().replace(PigRel.CONVENTION);
  return new PigProject(project.getCluster(), traitSet, project.getInput(),
      project.getProjects(), project.getRowType());
}
 
Example 18
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Rewrite LogicalProject.
 *
 * @param rel the project rel to rewrite
 */
public Frame decorrelateRel(LogicalProject rel) {
  //
  // Rewrite logic:
  //
  // 1. Pass along any correlated variables coming from the input.
  //

  final RelNode oldInput = rel.getInput();
  Frame frame = getInvoke(oldInput, rel);
  if (frame == null) {
    // If input has not been rewritten, do not rewrite this rel.
    return null;
  }
  final List<RexNode> oldProjects = rel.getProjects();
  final List<RelDataTypeField> relOutput = rel.getRowType().getFieldList();

  // Project projects the original expressions,
  // plus any correlated variables the input wants to pass along.
  final List<Pair<RexNode, String>> projects = new ArrayList<>();

  // If this Project has correlated reference, create value generator
  // and produce the correlated variables in the new output.
  if (cm.mapRefRelToCorRef.containsKey(rel)) {
    frame = decorrelateInputWithValueGenerator(rel, frame);
  }

  // Project projects the original expressions
  final Map<Integer, Integer> mapOldToNewOutputs = new HashMap<>();
  int newPos;
  for (newPos = 0; newPos < oldProjects.size(); newPos++) {
    projects.add(
        newPos,
        Pair.of(
            decorrelateExpr(currentRel, map, cm, oldProjects.get(newPos)),
            relOutput.get(newPos).getName()));
    mapOldToNewOutputs.put(newPos, newPos);
  }

  // Project any correlated variables the input wants to pass along.
  final SortedMap<CorDef, Integer> corDefOutputs = new TreeMap<>();
  for (Map.Entry<CorDef, Integer> entry : frame.corDefOutputs.entrySet()) {
    projects.add(
        RexInputRef.of2(entry.getValue(),
            frame.r.getRowType().getFieldList()));
    corDefOutputs.put(entry.getKey(), newPos);
    newPos++;
  }

  RelNode newProject = relBuilder.push(frame.r)
      .projectNamed(Pair.left(projects), Pair.right(projects), true)
      .build();

  return register(rel, newProject, mapOldToNewOutputs, corDefOutputs);
}
 
Example 19
Source File: RelDecorrelator.java    From flink with Apache License 2.0 4 votes vote down vote up
private void onMatch2(
    RelOptRuleCall call,
    LogicalCorrelate correlate,
    RelNode leftInput,
    LogicalProject aggOutputProject,
    LogicalAggregate aggregate) {
  if (generatedCorRels.contains(correlate)) {
    // This Correlate was generated by a previous invocation of
    // this rule. No further work to do.
    return;
  }

  setCurrent(call.getPlanner().getRoot(), correlate);

  // check for this pattern
  // The pattern matching could be simplified if rules can be applied
  // during decorrelation,
  //
  // CorrelateRel(left correlation, condition = true)
  //   leftInput
  //   Project-A (a RexNode)
  //     Aggregate (groupby (0), agg0(), agg1()...)

  // check aggOutputProj projects only one expression
  List<RexNode> aggOutputProjExprs = aggOutputProject.getProjects();
  if (aggOutputProjExprs.size() != 1) {
    return;
  }

  JoinRelType joinType = correlate.getJoinType();
  // corRel.getCondition was here, however Correlate was updated so it
  // never includes a join condition. The code was not modified for brevity.
  RexNode joinCond = relBuilder.literal(true);
  if ((joinType != JoinRelType.LEFT)
      || (joinCond != relBuilder.literal(true))) {
    return;
  }

  // check that the agg is on the entire input
  if (!aggregate.getGroupSet().isEmpty()) {
    return;
  }

  List<AggregateCall> aggCalls = aggregate.getAggCallList();
  Set<Integer> isCount = new HashSet<>();

  // remember the count() positions
  int i = -1;
  for (AggregateCall aggCall : aggCalls) {
    ++i;
    if (aggCall.getAggregation() instanceof SqlCountAggFunction) {
      isCount.add(i);
    }
  }

  // now rewrite the plan to
  //
  // Project-A' (all LHS plus transformed original projections,
  //             replacing references to count() with case statement)
  //   Correlate(left correlation, condition = true)
  //     leftInput
  //     Aggregate(groupby (0), agg0(), agg1()...)
  //
  LogicalCorrelate newCorrelate =
      LogicalCorrelate.create(leftInput, aggregate,
          correlate.getCorrelationId(), correlate.getRequiredColumns(),
          correlate.getJoinType());

  // remember this rel so we don't fire rule on it again
  // REVIEW jhyde 29-Oct-2007: rules should not save state; rule
  // should recognize patterns where it does or does not need to do
  // work
  generatedCorRels.add(newCorrelate);

  // need to update the mapCorToCorRel Update the output position
  // for the corVars: only pass on the corVars that are not used in
  // the join key.
  if (cm.mapCorToCorRel.get(correlate.getCorrelationId()) == correlate) {
    cm.mapCorToCorRel.put(correlate.getCorrelationId(), newCorrelate);
  }

  RelNode newOutput =
      aggregateCorrelatorOutput(newCorrelate, aggOutputProject, isCount);

  call.transformTo(newOutput);
}
 
Example 20
Source File: ElasticsearchProjectRule.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
@Override public RelNode convert(RelNode relNode) {
    final LogicalProject project = (LogicalProject) relNode;
    final RelTraitSet traitSet = project.getTraitSet().replace(getOutTrait());
    return new ElasticsearchProject(project.getCluster(), traitSet,
            convert(project.getInput(), getOutTrait()), project.getProjects(), project.getRowType());
}