Java Code Examples for org.apache.calcite.rel.RelNode#getVariablesSet()

The following examples show how to use org.apache.calcite.rel.RelNode#getVariablesSet() . 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: RelFieldTrimmer.java    From Bats with Apache License 2.0 6 votes vote down vote up
protected TrimResult result(RelNode r, final Mapping mapping) {
    final RexBuilder rexBuilder = relBuilder.getRexBuilder();
    for (final CorrelationId correlation : r.getVariablesSet()) {
        r = r.accept(new CorrelationReferenceFinder() {
            @Override
            protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.getCorrelationId().equals(correlation)
                        && v.getType().getFieldCount() == mapping.getSourceCount()) {
                    final int old = fieldAccess.getField().getIndex();
                    final int new_ = mapping.getTarget(old);
                    final RelDataTypeFactory.Builder typeBuilder = relBuilder.getTypeFactory().builder();
                    for (int target : Util.range(mapping.getTargetCount())) {
                        typeBuilder.add(v.getType().getFieldList().get(mapping.getSource(target)));
                    }
                    final RexNode newV = rexBuilder.makeCorrel(typeBuilder.build(), v.getCorrelationId());
                    if (old != new_) {
                        return rexBuilder.makeFieldAccess(newV, new_);
                    }
                }
                return fieldAccess;
            }
        });
    }
    return new TrimResult(r, mapping);
}
 
Example 2
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 3
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> handleSubQuery(final RelNode rel) {
	return new RexVisitorImpl<Void>(true) {

		@Override
		public Void visitSubQuery(RexSubQuery subQuery) {
			RelNode newRel = subQuery.rel;
			if (subQuery.getKind() == SqlKind.IN) {
				newRel = addProjectionForIn(subQuery.rel);
			}
			final Frame frame = decorrelator.getInvoke(newRel);
			if (frame != null && frame.c != null) {

				Frame target = frame;
				if (subQuery.getKind() == SqlKind.EXISTS) {
					target = addProjectionForExists(frame);
				}

				final DecorrelateRexShuttle shuttle = new DecorrelateRexShuttle(
						rel.getRowType(),
						target.r.getRowType(),
						rel.getVariablesSet());

				final RexNode newCondition = target.c.accept(shuttle);
				Pair<RelNode, RexNode> newNodeAndCondition = new Pair<>(target.r, newCondition);
				subQueryMap.put(subQuery, newNodeAndCondition);
			}
			return null;
		}
	};
}
 
Example 4
Source File: DremioFieldTrimmer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected TrimResult result(RelNode r, final Mapping mapping) {
  final RexBuilder rexBuilder = builder.getRexBuilder();
  for (final CorrelationId correlation : r.getVariablesSet()) {
    r = r.accept(
            new CorrelationReferenceFinder() {
              protected RexNode handle(RexFieldAccess fieldAccess) {
                final RexCorrelVariable v =
                        (RexCorrelVariable) fieldAccess.getReferenceExpr();
                if (v.id.equals(correlation)) {
                  final int old = fieldAccess.getField().getIndex();
                  final int new_ = mapping.getTarget(old);
                  final RelDataTypeFactory.Builder typeBuilder =
                          builder.getTypeFactory().builder();
                  for (IntPair pair : mapping) {
                    if (pair.source < v.getType().getFieldCount()) {
                      typeBuilder.add(v.getType().getFieldList().get(pair.source));
                    }
                  }
                  final RexNode newV =
                          rexBuilder.makeCorrel(typeBuilder.build(), v.id);
                  if (old != new_) {
                    return rexBuilder.makeFieldAccess(newV, new_);
                  }
                }
                return fieldAccess;
              }
            });
  }
  return new TrimResult(r, mapping);
}
 
Example 5
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> handleSubQuery(final RelNode rel) {
	return new RexVisitorImpl<Void>(true) {

		@Override
		public Void visitSubQuery(RexSubQuery subQuery) {
			RelNode newRel = subQuery.rel;
			if (subQuery.getKind() == SqlKind.IN) {
				newRel = addProjectionForIn(subQuery.rel);
			}
			final Frame frame = decorrelator.getInvoke(newRel);
			if (frame != null && frame.c != null) {

				Frame target = frame;
				if (subQuery.getKind() == SqlKind.EXISTS) {
					target = addProjectionForExists(frame);
				}

				final DecorrelateRexShuttle shuttle = new DecorrelateRexShuttle(
						rel.getRowType(),
						target.r.getRowType(),
						rel.getVariablesSet());

				final RexNode newCondition = target.c.accept(shuttle);
				Pair<RelNode, RexNode> newNodeAndCondition = new Pair<>(target.r, newCondition);
				subQueryMap.put(subQuery, newNodeAndCondition);
			}
			return null;
		}
	};
}
 
Example 6
Source File: RelFieldTrimmer.java    From calcite 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() {
          protected RexNode handle(RexFieldAccess fieldAccess) {
            final RexCorrelVariable v =
                (RexCorrelVariable) fieldAccess.getReferenceExpr();
            if (v.id.equals(correlation)) {
              fieldsUsedBuilder.set(fieldAccess.getField().getIndex());
            }
            return fieldAccess;
          }
        });
  }

  return dispatchTrimFields(input, fieldsUsedBuilder.build(), extraFields);
}
 
Example 7
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 5 votes vote down vote up
protected TrimResult result(RelNode r, final Mapping mapping) {
  final RexBuilder rexBuilder = relBuilder.getRexBuilder();
  for (final CorrelationId correlation : r.getVariablesSet()) {
    r = r.accept(
        new CorrelationReferenceFinder() {
          protected RexNode handle(RexFieldAccess fieldAccess) {
            final RexCorrelVariable v =
                (RexCorrelVariable) fieldAccess.getReferenceExpr();
            if (v.id.equals(correlation)
                && v.getType().getFieldCount() == mapping.getSourceCount()) {
              final int old = fieldAccess.getField().getIndex();
              final int new_ = mapping.getTarget(old);
              final RelDataTypeFactory.Builder typeBuilder =
                  relBuilder.getTypeFactory().builder();
              for (int target : Util.range(mapping.getTargetCount())) {
                typeBuilder.add(
                    v.getType().getFieldList().get(mapping.getSource(target)));
              }
              final RexNode newV =
                  rexBuilder.makeCorrel(typeBuilder.build(), v.id);
              if (old != new_) {
                return rexBuilder.makeFieldAccess(newV, new_);
              }
            }
            return fieldAccess;
          }
        });
  }
  return new TrimResult(r, mapping);
}
 
Example 8
Source File: RelToSqlConverter.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void parseCorrelTable(RelNode relNode, Result x) {
  for (CorrelationId id : relNode.getVariablesSet()) {
    correlTableMap.put(id, x.qualifiedContext());
  }
}
 
Example 9
Source File: RelNodeConvertor.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public static Schema convertRelNode(RelNode relNode) {

        List<RelNode> inputs = relNode.getInputs();
        String relTypeName = relNode.getRelTypeName();
        String correlVariable = relNode.getCorrelVariable();
        RelOptTable table = relNode.getTable();
        Set<CorrelationId> variablesSet = relNode.getVariablesSet();
        switch (relTypeName) {
            case "LogicalValues": {
                return logicValues(relNode);
            }
            case "LogicalProject": {
                return logicProject(relNode);
            }
            case "LogicalAggregate": {
                return logicalAggregate(relNode);
            }
            case "LogicalTableScan": {
                return logicalTableScan(relNode);
            }
            case "LogicalIntersect":
            case "LogicalMinus":
            case "LogicalUnion": {
                return logicalSetOp(relNode);
            }
            case "LogicalSort": {
                return logicalSort(relNode);
            }
            case "LogicalFilter": {
                return logicalFilter(relNode);
            }
            case "LogicalJoin": {
                return logicalJoin(relNode);
            }
            case "LogicalCorrelate": {
                return logicalCorrelate(relNode);
            }
        }
        if (relNode instanceof TableScan) {
            List<FieldType> fields = getFields(relNode);
            TableScan relNode1 = (TableScan) relNode;
            MycatSQLTableScan unwrap = relNode1.getTable().unwrap(MycatSQLTableScan.class);
            if (unwrap != null) {
                return new FromSqlSchema(fields, unwrap.getTargetName(), unwrap.getSql());
            }
        }
        throw new UnsupportedOperationException();
    }
 
Example 10
Source File: RelToSqlConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private void parseCorrelTable(RelNode relNode, Result x) {
  for (CorrelationId id : relNode.getVariablesSet()) {
    correlTableMap.put(id, x.qualifiedContext());
  }
}
 
Example 11
Source File: RelToSqlConverter.java    From calcite with Apache License 2.0 4 votes vote down vote up
private void parseCorrelTable(RelNode relNode, Result x) {
  for (CorrelationId id : relNode.getVariablesSet()) {
    correlTableMap.put(id, x.qualifiedContext());
  }
}