org.apache.calcite.rel.core.CorrelationId Java Examples

The following examples show how to use org.apache.calcite.rel.core.CorrelationId. 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: EnumerableCorrelate.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates an EnumerableCorrelate. */
public static EnumerableCorrelate create(
    RelNode left,
    RelNode right,
    CorrelationId correlationId,
    ImmutableBitSet requiredColumns,
    JoinRelType joinType) {
  final RelOptCluster cluster = left.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet =
      cluster.traitSetOf(EnumerableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE,
              () -> RelMdCollation.enumerableCorrelate(mq, left, right, joinType));
  return new EnumerableCorrelate(
      cluster,
      traitSet,
      left,
      right,
      correlationId,
      requiredColumns,
      joinType);
}
 
Example #2
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public RelNode visit(LogicalFilter filter) {
	final boolean hasSubQuery = RexUtil.SubQueryFinder.find(filter.getCondition()) != null;
	try {
		if (!corNodeStack.isEmpty()) {
			mapSubQueryNodeToCorSet.put(filter, corNodeStack.peek().getVariablesSet());
		}
		if (hasSubQuery) {
			corNodeStack.push(filter);
		}
		checkCorCondition(filter);
		filter.getCondition().accept(rexVisitor(filter));
		for (CorrelationId correlationId : filter.getVariablesSet()) {
			mapCorToCorRel.put(correlationId, filter);
		}
	} finally {
		if (hasSubQuery) {
			corNodeStack.pop();
		}
	}
	return super.visit(filter);
}
 
Example #3
Source File: LogicalCorrelate.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a LogicalCorrelate.
 * @param cluster      cluster this relational expression belongs to
 * @param left         left input relational expression
 * @param right        right input relational expression
 * @param correlationId variable name for the row of left input
 * @param requiredColumns Required columns
 * @param joinType     join type
 */
public LogicalCorrelate(
    RelOptCluster cluster,
    RelTraitSet traitSet,
    RelNode left,
    RelNode right,
    CorrelationId correlationId,
    ImmutableBitSet requiredColumns,
    JoinRelType joinType) {
  super(
      cluster,
      traitSet,
      left,
      right,
      correlationId,
      requiredColumns,
      joinType);
  assert !CalciteSystemProperty.DEBUG.value() || isValid(Litmus.THROW, null);
}
 
Example #4
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Function2<RelNode, RelNode, Void> createCopyHook() {
  return (oldNode, newNode) -> {
    if (cm.mapRefRelToCorRef.containsKey(oldNode)) {
      cm.mapRefRelToCorRef.putAll(newNode,
          cm.mapRefRelToCorRef.get(oldNode));
    }
    if (oldNode instanceof Correlate
        && newNode instanceof Correlate) {
      Correlate oldCor = (Correlate) oldNode;
      CorrelationId c = oldCor.getCorrelationId();
      if (cm.mapCorToCorRel.get(c) == oldNode) {
        cm.mapCorToCorRel.put(c, newNode);
      }

      if (generatedCorRels.contains(oldNode)) {
        generatedCorRels.add((Correlate) newNode);
      }
    }
    return null;
  };
}
 
Example #5
Source File: EnumerableNestedLoopJoin.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Creates an EnumerableNestedLoopJoin. */
public static EnumerableNestedLoopJoin create(
    RelNode left,
    RelNode right,
    RexNode condition,
    Set<CorrelationId> variablesSet,
    JoinRelType joinType) {
  final RelOptCluster cluster = left.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet =
      cluster.traitSetOf(EnumerableConvention.INSTANCE)
          .replaceIfs(RelCollationTraitDef.INSTANCE,
              () -> RelMdCollation.enumerableNestedLoopJoin(mq, left, right, joinType));
  return new EnumerableNestedLoopJoin(cluster, traitSet, left, right, condition,
      variablesSet, joinType);
}
 
Example #6
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Tests {@link RexCopier#visitCorrelVariable(RexCorrelVariable)} */
@Test void testCopyCorrelVariable() {
  final RelDataTypeFactory sourceTypeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  RelDataType type = sourceTypeFactory.createSqlType(SqlTypeName.VARCHAR, 65536);

  final RelDataTypeFactory targetTypeFactory =
      new MySqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RexBuilder builder = new RexBuilder(targetTypeFactory);

  final RexCorrelVariable node =
      (RexCorrelVariable) builder.makeCorrel(type, new CorrelationId(0));
  final RexNode copy = builder.copy(node);
  assertTrue(copy instanceof RexCorrelVariable);

  final RexCorrelVariable result = (RexCorrelVariable) copy;
  assertThat(result.id, is(node.id));
  assertThat(result.getType().getSqlTypeName(), is(SqlTypeName.VARCHAR));
  assertThat(result.getType().getPrecision(), is(PRECISION));
}
 
Example #7
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
private Function2<RelNode, RelNode, Void> createCopyHook() {
  return (oldNode, newNode) -> {
    if (cm.mapRefRelToCorRef.containsKey(oldNode)) {
      cm.mapRefRelToCorRef.putAll(newNode,
          cm.mapRefRelToCorRef.get(oldNode));
    }
    if (oldNode instanceof LogicalCorrelate
        && newNode instanceof LogicalCorrelate) {
      LogicalCorrelate oldCor = (LogicalCorrelate) oldNode;
      CorrelationId c = oldCor.getCorrelationId();
      if (cm.mapCorToCorRel.get(c) == oldNode) {
        cm.mapCorToCorRel.put(c, newNode);
      }

      if (generatedCorRels.contains(oldNode)) {
        generatedCorRels.add((LogicalCorrelate) newNode);
      }
    }
    return null;
  };
}
 
Example #8
Source File: RelBuilderTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testFilterEmpty() {
  final RelBuilder builder = RelBuilder.create(config().build());
  final RelNode root =
      builder.scan("EMP")
          // We intend to call
          //   filter(Iterable<CorrelationId>, RexNode...)
          // with zero varargs, not
          //   filter(Iterable<RexNode>)
          // Let's hope they're distinct after type erasure.
          .filter(ImmutableSet.<CorrelationId>of())
          .build();
  assertThat(root, hasTree("LogicalTableScan(table=[[scott, EMP]])\n"));
}
 
Example #9
Source File: OLAPJoinRel.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public OLAPJoinRel(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, //
        RexNode condition, ImmutableIntList leftKeys, ImmutableIntList rightKeys, //
        Set<CorrelationId> variablesSet, JoinRelType joinType) throws InvalidRelException {
    super(cluster, traits, left, right, condition, leftKeys, rightKeys, variablesSet, joinType);
    Preconditions.checkArgument(getConvention() == OLAPRel.CONVENTION);
    this.rowType = getRowType();
    this.isTopJoin = false;
}
 
Example #10
Source File: DrillLateralJoinRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Correlate copy(RelTraitSet traitSet,
      RelNode left, RelNode right, CorrelationId correlationId,
      ImmutableBitSet requiredColumns, SemiJoinType joinType) {
  return new DrillLateralJoinRel(this.getCluster(), this.getTraitSet(), left, right, this.excludeCorrelateColumn, correlationId, requiredColumns,
      this.getJoinType());
}
 
Example #11
Source File: ProjectCorrelateTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
public RexFieldAccessReplacer(CorrelationId rexCorrelVariableToReplace, RexCorrelVariable rexCorrelVariable,
        RexBuilder builder, Map<Integer, Integer> requiredColsMap) {
    this.rexCorrelVariableToReplace = rexCorrelVariableToReplace;
    this.rexCorrelVariable = rexCorrelVariable;
    this.builder = builder;
    this.requiredColsMap = requiredColsMap;
}
 
Example #12
Source File: DremioRelFactories.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode createJoin(RelNode left, RelNode right,
                          RexNode condition,
                          Set<CorrelationId> variablesSet,
                          JoinRelType joinType, boolean semiJoinDone) {
  return JoinRel.create(left.getCluster(), left.getTraitSet(), left, right, condition, joinType);
}
 
Example #13
Source File: LogicalFilter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a LogicalFilter. */
public static LogicalFilter create(final RelNode input, RexNode condition,
    ImmutableSet<CorrelationId> variablesSet) {
  final RelOptCluster cluster = input.getCluster();
  final RelMetadataQuery mq = cluster.getMetadataQuery();
  final RelTraitSet traitSet = cluster.traitSetOf(Convention.NONE)
      .replaceIfs(RelCollationTraitDef.INSTANCE,
          () -> RelMdCollation.filter(mq, input))
      .replaceIf(RelDistributionTraitDef.INSTANCE,
          () -> RelMdDistribution.filter(mq, input));
  return new LogicalFilter(cluster, traitSet, input, condition, variablesSet);
}
 
Example #14
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/** Creates a CorelMap with given contents. */
public static CorelMap of(
    SortedSetMultimap<RelNode, CorRef> mapRefRelToCorVar,
    SortedMap<CorrelationId, RelNode> mapCorToCorRel,
    Map<RexFieldAccess, CorRef> mapFieldAccessToCorVar) {
  return new CorelMap(mapRefRelToCorVar, mapCorToCorRel,
      mapFieldAccessToCorVar);
}
 
Example #15
Source File: DeduplicateCorrelateVariables.java    From calcite with Apache License 2.0 5 votes vote down vote up
private DeduplicateCorrelateVariablesShuttle(RexBuilder builder,
    CorrelationId canonicalId, ImmutableSet<CorrelationId> alternateIds,
    DeduplicateCorrelateVariables shuttle) {
  this.builder = builder;
  this.canonicalId = canonicalId;
  this.alternateIds = alternateIds;
  this.shuttle = shuttle;
}
 
Example #16
Source File: LogicalJoin.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalJoin(RelOptCluster cluster, RelTraitSet traitSet, RelNode left,
    RelNode right, RexNode condition, JoinRelType joinType,
    Set<String> variablesStopped, boolean semiJoinDone,
    ImmutableList<RelDataTypeField> systemFieldList) {
  this(cluster, traitSet, left, right, condition,
      CorrelationId.setOf(variablesStopped), joinType, semiJoinDone,
      systemFieldList);
}
 
Example #17
Source File: DremioRelFactories.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode createFilter(RelNode child, RexNode condition, Set<CorrelationId> correlVariables) {
  Preconditions.checkArgument(correlVariables.isEmpty());
  return FilterRel.create(
      RelOptRule.convert(child, child.getTraitSet().plus(Rel.LOGICAL).simplify()),
      condition);
}
 
Example #18
Source File: LogicalJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalJoin(RelOptCluster cluster, RelTraitSet traitSet, RelNode left,
    RelNode right, RexNode condition, JoinRelType joinType,
    Set<String> variablesStopped, boolean semiJoinDone,
    ImmutableList<RelDataTypeField> systemFieldList) {
  this(cluster, traitSet, ImmutableList.of(), left, right, condition,
      CorrelationId.setOf(variablesStopped), joinType, semiJoinDone,
      systemFieldList);
}
 
Example #19
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void updateRelInMap(
    SortedMap<CorrelationId, LogicalCorrelate> mapCorVarToCorRel) {
  for (CorrelationId corVar : mapCorVarToCorRel.keySet()) {
    LogicalCorrelate oldRel = mapCorVarToCorRel.get(corVar);
    if (oldToNewRelMap.containsKey(oldRel)) {
      RelNode newRel = oldToNewRelMap.get(oldRel);
      assert newRel instanceof LogicalCorrelate;
      mapCorVarToCorRel.put(corVar, (LogicalCorrelate) newRel);
    }
  }
}
 
Example #20
Source File: EquiJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
public EquiJoin(RelOptCluster cluster, RelTraitSet traits, RelNode left,
    RelNode right, RexNode condition, ImmutableIntList leftKeys,
    ImmutableIntList rightKeys, JoinRelType joinType,
    Set<String> variablesStopped) {
  super(cluster, traits, left, right, condition, leftKeys, rightKeys,
      CorrelationId.setOf(variablesStopped), joinType);
}
 
Example #21
Source File: QueryOperationConverter.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RelNode visit(JoinQueryOperation join) {
	final Set<CorrelationId> corSet;
	if (join.isCorrelated()) {
		corSet = Collections.singleton(relBuilder.peek().getCluster().createCorrel());
	} else {
		corSet = Collections.emptySet();
	}

	return relBuilder.join(
		convertJoinType(join.getJoinType()),
		join.getCondition().accept(joinExpressionVisitor),
		corSet)
		.build();
}
 
Example #22
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private DecorrelateRexShuttle(
		RelDataType leftRowType,
		RelDataType rightRowType,
		Set<CorrelationId> variableSet) {
	this.leftRowType = leftRowType;
	this.rightRowType = rightRowType;
	this.variableSet = variableSet;
}
 
Example #23
Source File: EnumerableCorrelate.java    From calcite with Apache License 2.0 5 votes vote down vote up
public EnumerableCorrelate(RelOptCluster cluster, RelTraitSet traits,
    RelNode left, RelNode right,
    CorrelationId correlationId,
    ImmutableBitSet requiredColumns, JoinRelType joinType) {
  super(cluster, traits, left, right, correlationId, requiredColumns,
      joinType);
}
 
Example #24
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void updateRelInMap(
    SortedSetMultimap<RelNode, CorrelationId> mapRefRelToCorVar) {
  for (RelNode rel : Lists.newArrayList(mapRefRelToCorVar.keySet())) {
    if (oldToNewRelMap.containsKey(rel)) {
      SortedSet<CorrelationId> corVarSet =
          mapRefRelToCorVar.removeAll(rel);
      mapRefRelToCorVar.putAll(oldToNewRelMap.get(rel), corVarSet);
    }
  }
}
 
Example #25
Source File: RelStructuredTypeFlattener.java    From Bats with Apache License 2.0 5 votes vote down vote up
public void updateRelInMap(SortedSetMultimap<RelNode, CorrelationId> mapRefRelToCorVar) {
    for (RelNode rel : Lists.newArrayList(mapRefRelToCorVar.keySet())) {
        if (oldToNewRelMap.containsKey(rel)) {
            SortedSet<CorrelationId> corVarSet = mapRefRelToCorVar.removeAll(rel);
            mapRefRelToCorVar.putAll(oldToNewRelMap.get(rel), corVarSet);
        }
    }
}
 
Example #26
Source File: RelFieldTrimmer.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Trims the fields of an input relational expression.
 *
 * @param rel        Relational expression
 * @param input      Input relational expression, whose fields to trim
 * @param fieldsUsed Bitmap of fields needed by the consumer
 * @return New relational expression and its field mapping
 */
protected TrimResult trimChild(RelNode rel, RelNode input, final ImmutableBitSet fieldsUsed,
        Set<RelDataTypeField> extraFields) {
    final ImmutableBitSet.Builder fieldsUsedBuilder = fieldsUsed.rebuild();

    // Fields that define the collation cannot be discarded.
    final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
    final ImmutableList<RelCollation> collations = mq.collations(input);
    for (RelCollation collation : collations) {
        for (RelFieldCollation fieldCollation : collation.getFieldCollations()) {
            fieldsUsedBuilder.set(fieldCollation.getFieldIndex());
        }
    }

    // Correlating variables are a means for other relational expressions to use
    // fields.
    for (final CorrelationId correlation : rel.getVariablesSet()) {
        rel.accept(new CorrelationReferenceFinder() {
            @Override
            protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.getCorrelationId().equals(correlation)) {
                    fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
                }
                return fieldAccess;
            }
        });
    }

    return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
}
 
Example #27
Source File: LogicalJoin.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Deprecated // to be removed before 2.0
public LogicalJoin(RelOptCluster cluster, RelTraitSet traitSet,
    RelNode left, RelNode right, RexNode condition, Set<CorrelationId> variablesSet,
    JoinRelType joinType, boolean semiJoinDone,
    ImmutableList<RelDataTypeField> systemFieldList) {
  this(cluster, traitSet, ImmutableList.of(), left, right, condition,
      variablesSet, joinType, semiJoinDone, systemFieldList);
}
 
Example #28
Source File: OLAPJoinRel.java    From kylin with Apache License 2.0 5 votes vote down vote up
public OLAPJoinRel(RelOptCluster cluster, RelTraitSet traits, RelNode left, RelNode right, //
        RexNode condition, ImmutableIntList leftKeys, ImmutableIntList rightKeys, //
        Set<CorrelationId> variablesSet, JoinRelType joinType) throws InvalidRelException {
    super(cluster, traits, left, right, condition, leftKeys, rightKeys, variablesSet, joinType);
    Preconditions.checkArgument(getConvention() == OLAPRel.CONVENTION);
    this.rowType = getRowType();
    this.isTopJoin = false;
}
 
Example #29
Source File: DeduplicateCorrelateVariables.java    From Bats with Apache License 2.0 5 votes vote down vote up
private DeduplicateCorrelateVariablesShuttle(RexBuilder builder, CorrelationId canonicalId,
        ImmutableSet<CorrelationId> alternateIds, DeduplicateCorrelateVariables shuttle) {
    this.builder = builder;
    this.canonicalId = canonicalId;
    this.alternateIds = alternateIds;
    this.shuttle = shuttle;
}
 
Example #30
Source File: JdbcRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a JdbcJoin. */
public JdbcJoin(RelOptCluster cluster, RelTraitSet traitSet,
    RelNode left, RelNode right, RexNode condition,
    Set<CorrelationId> variablesSet, JoinRelType joinType)
    throws InvalidRelException {
  super(cluster, traitSet, ImmutableList.of(), left, right, condition, variablesSet, joinType);
}