org.apache.calcite.rex.RexFieldAccess Java Examples

The following examples show how to use org.apache.calcite.rex.RexFieldAccess. 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: RelDecorrelator.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
    if (cm.mapFieldAccessToCorRef.containsKey(fieldAccess)) {
        // if it is a corVar, change it to be input ref.
        CorRef corVar = cm.mapFieldAccessToCorRef.get(fieldAccess);

        // corVar offset should point to the leftInput of currentRel,
        // which is the Correlate.
        RexNode newRexNode = RexBuilder.getRexFactory().makeInputRef(corVar.field, fieldAccess.getType());

        if (projectPulledAboveLeftCorrelator && (nullIndicator != null)) {
            // need to enforce nullability by applying an additional
            // cast operator over the transformed expression.
            newRexNode = createCaseExpression(nullIndicator, rexBuilder.constantNull(), newRexNode);
        }
        return newRexNode;
    }
    return fieldAccess;
}
 
Example #2
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 6 votes vote down vote up
private ScalarFunctionSplitter createScalarFunctionSplitter(
	int primitiveLeftFieldCount,
	ArrayBuffer<RexNode> extractedRexNodes,
	RexNode tableFunctionNode) {
	return new ScalarFunctionSplitter(
		primitiveLeftFieldCount,
		extractedRexNodes,
		node -> {
			if (PythonUtil.isNonPythonCall(tableFunctionNode)) {
				// splits the RexCalls which contain Python functions into separate node
				return PythonUtil.isPythonCall(node, null);
			} else if (PythonUtil.containsNonPythonCall(node)) {
				// splits the RexCalls which contain non-Python functions into separate node
				return PythonUtil.isNonPythonCall(node);
			} else {
				// splits the RexFieldAccesses which contain non-Python functions into separate node
				return node instanceof RexFieldAccess;
			}
		}
	);
}
 
Example #3
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 #4
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  if (cm.mapFieldAccessToCorRef.containsKey(fieldAccess)) {
    // if it is a corVar, change it to be input ref.
    CorRef corVar = cm.mapFieldAccessToCorRef.get(fieldAccess);

    // corVar offset should point to the leftInput of currentRel,
    // which is the Correlate.
    RexNode newRexNode =
        new RexInputRef(corVar.field, fieldAccess.getType());

    if (projectPulledAboveLeftCorrelator
        && (nullIndicator != null)) {
      // need to enforce nullability by applying an additional
      // cast operator over the transformed expression.
      newRexNode =
          createCaseExpression(nullIndicator, null, newRexNode);
    }
    return newRexNode;
  }
  return fieldAccess;
}
 
Example #5
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  if (cm.mapFieldAccessToCorRef.containsKey(fieldAccess)) {
    // if it is a corVar, change it to be input ref.
    CorRef corVar = cm.mapFieldAccessToCorRef.get(fieldAccess);

    // corVar offset should point to the leftInput of currentRel,
    // which is the Correlate.
    RexNode newRexNode =
        new RexInputRef(corVar.field, fieldAccess.getType());

    if (projectPulledAboveLeftCorrelator
        && (nullIndicator != null)) {
      // need to enforce nullability by applying an additional
      // cast operator over the transformed expression.
      newRexNode =
          createCaseExpression(
              nullIndicator,
              rexBuilder.constantNull(),
              newRexNode);
    }
    return newRexNode;
  }
  return fieldAccess;
}
 
Example #6
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean references(RexNode e, CorRef correlation) {
  switch (e.getKind()) {
  case CAST:
    final RexNode operand = ((RexCall) e).getOperands().get(0);
    if (isWidening(e.getType(), operand.getType())) {
      return references(operand, correlation);
    }
    return false;
  case FIELD_ACCESS:
    final RexFieldAccess f = (RexFieldAccess) e;
    if (f.getField().getIndex() == correlation.field
        && f.getReferenceExpr() instanceof RexCorrelVariable) {
      if (((RexCorrelVariable) f.getReferenceExpr()).id == correlation.corr) {
        return true;
      }
    }
    // fall through
  default:
    return false;
  }
}
 
Example #7
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 6 votes vote down vote up
private boolean references(RexNode e, CorRef correlation) {
    switch (e.getKind()) {
    case CAST:
        final RexNode operand = ((RexCall) e).getOperands().get(0);
        if (isWidening(e.getType(), operand.getType())) {
            return references(operand, correlation);
        }
        return false;
    case FIELD_ACCESS:
        final RexFieldAccess f = (RexFieldAccess) e;
        if (f.getField().getIndex() == correlation.field && f.getReferenceExpr() instanceof RexCorrelVariable) {
            if (((RexCorrelVariable) f.getReferenceExpr()).getCorrelationId() == correlation.corr) {
                return true;
            }
        }
        // fall through
    default:
        return false;
    }
}
 
Example #8
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
private boolean references(RexNode e, CorRef correlation) {
  switch (e.getKind()) {
  case CAST:
    final RexNode operand = ((RexCall) e).getOperands().get(0);
    if (isWidening(e.getType(), operand.getType())) {
      return references(operand, correlation);
    }
    return false;
  case FIELD_ACCESS:
    final RexFieldAccess f = (RexFieldAccess) e;
    if (f.getField().getIndex() == correlation.field
        && f.getReferenceExpr() instanceof RexCorrelVariable) {
      if (((RexCorrelVariable) f.getReferenceExpr()).id == correlation.corr) {
        return true;
      }
    }
    // fall through
  default:
    return false;
  }
}
 
Example #9
Source File: RelDecorrelator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  if (cm.mapFieldAccessToCorRef.containsKey(fieldAccess)) {
    // if it is a corVar, change it to be input ref.
    CorRef corVar = cm.mapFieldAccessToCorRef.get(fieldAccess);

    // corVar offset should point to the leftInput of currentRel,
    // which is the Correlate.
    RexNode newRexNode =
        new RexInputRef(corVar.field, fieldAccess.getType());

    if (projectPulledAboveLeftCorrelator
        && (nullIndicator != null)) {
      // need to enforce nullability by applying an additional
      // cast operator over the transformed expression.
      newRexNode =
          createCaseExpression(
              nullIndicator,
              rexBuilder.constantNull(),
              newRexNode);
    }
    return newRexNode;
  }
  return fieldAccess;
}
 
Example #10
Source File: RelDecorrelator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
    int newInputOutputOffset = 0;
    for (RelNode input : currentRel.getInputs()) {
        final Frame frame = map.get(input);

        if (frame != null) {
            // try to find in this input rel the position of corVar
            final CorRef corRef = cm.mapFieldAccessToCorRef.get(fieldAccess);

            if (corRef != null) {
                Integer newInputPos = frame.corDefOutputs.get(corRef.def());
                if (newInputPos != null) {
                    // This input does produce the corVar referenced.
                    return RexBuilder.getRexFactory().makeInputRef(newInputPos + newInputOutputOffset,
                            frame.r.getRowType().getFieldList().get(newInputPos).getType());
                }
            }

            // this input does not produce the corVar needed
            newInputOutputOffset += frame.r.getRowType().getFieldCount();
        } else {
            // this input is not rewritten
            newInputOutputOffset += input.getRowType().getFieldCount();
        }
    }
    return fieldAccess;
}
 
Example #11
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
	final RexNode ref = fieldAccess.getReferenceExpr();
	if (ref instanceof RexCorrelVariable) {
		final RexCorrelVariable var = (RexCorrelVariable) ref;
		assert variableSet.contains(var.id);
		final RelDataTypeField field = fieldAccess.getField();
		return new RexInputRef(field.getIndex(), field.getType());
	} else {
		return super.visitFieldAccess(fieldAccess);
	}
}
 
Example #12
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
	final RexNode ref = fieldAccess.getReferenceExpr();
	if (ref instanceof RexCorrelVariable) {
		final RexCorrelVariable var = (RexCorrelVariable) ref;
		assert variableSet.contains(var.id);
		final RelDataTypeField field = fieldAccess.getField();
		return new RexInputRef(field.getIndex(), field.getType());
	} else {
		return super.visitFieldAccess(fieldAccess);
	}
}
 
Example #13
Source File: CorrelationReferenceFinder.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
    if (fieldAccess.getReferenceExpr() instanceof RexCorrelVariable) {
        return finder.handle(fieldAccess);
    }
    return super.visitFieldAccess(fieldAccess);
}
 
Example #14
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  if (fieldAccess.getReferenceExpr() instanceof RexCorrelVariable) {
    final RexCorrelVariable v =
        (RexCorrelVariable) fieldAccess.getReferenceExpr();
    variableFields.put(v.id, fieldAccess.getField().getIndex());
  }
  return super.visitFieldAccess(fieldAccess);
}
 
Example #15
Source File: RelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
    if (fieldAccess.getReferenceExpr() instanceof RexCorrelVariable) {
        final RexCorrelVariable v = (RexCorrelVariable) fieldAccess.getReferenceExpr();
        variableFields.put(v.getCorrelationId(), fieldAccess.getField().getIndex());
    }
    return super.visitFieldAccess(fieldAccess);
}
 
Example #16
Source File: RexToTestCodeShuttle.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public String visitFieldAccess(RexFieldAccess fieldAccess) {
  StringBuilder sb = new StringBuilder();
  sb.append("v");
  RelDataType type = fieldAccess.getType();
  appendSqlType(sb, type);
  if (!type.isNullable()) {
    sb.append("NotNull");
  }
  sb.append("(");
  sb.append(fieldAccess.getField().getIndex() % 10);
  sb.append(")");
  return sb.toString();
}
 
Example #17
Source File: ProjectCorrelateTransposeRule.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
    RexNode refExpr = fieldAccess.getReferenceExpr().accept(this);
    // creates new RexFieldAccess instance for the case when referenceExpr was replaced.
    // Otherwise calls super method.
    if (refExpr == rexCorrelVariable) {
        return builder.makeFieldAccess(refExpr, requiredColsMap.get(fieldAccess.getField().getIndex()));
    }
    return super.visitFieldAccess(fieldAccess);
}
 
Example #18
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
  return new RexVisitorImpl<Void>(true) {
    @Override public Void visitFieldAccess(RexFieldAccess fieldAccess) {
      final RexNode ref = fieldAccess.getReferenceExpr();
      if (ref instanceof RexCorrelVariable) {
        final RexCorrelVariable var = (RexCorrelVariable) ref;
        if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
          // for cases where different Rel nodes are referring to
          // same correlation var (e.g. in case of NOT IN)
          // avoid generating another correlation var
          // and record the 'rel' is using the same correlation
          mapRefRelToCorRef.put(rel,
              mapFieldAccessToCorVar.get(fieldAccess));
        } else {
          final CorRef correlation =
              new CorRef(var.id, fieldAccess.getField().getIndex(),
                  corrIdGenerator++);
          mapFieldAccessToCorVar.put(fieldAccess, correlation);
          mapRefRelToCorRef.put(rel, correlation);
        }
      }
      return super.visitFieldAccess(fieldAccess);
    }

    @Override public Void visitSubQuery(RexSubQuery subQuery) {
      subQuery.rel.accept(CorelMapBuilder.this);
      return super.visitSubQuery(subQuery);
    }
  };
}
 
Example #19
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 #20
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 #21
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> rexVisitor(final RelNode rel) {
  return new RexVisitorImpl<Void>(true) {
    @Override public Void visitFieldAccess(RexFieldAccess fieldAccess) {
      final RexNode ref = fieldAccess.getReferenceExpr();
      if (ref instanceof RexCorrelVariable) {
        final RexCorrelVariable var = (RexCorrelVariable) ref;
        if (mapFieldAccessToCorVar.containsKey(fieldAccess)) {
          // for cases where different Rel nodes are referring to
          // same correlation var (e.g. in case of NOT IN)
          // avoid generating another correlation var
          // and record the 'rel' is using the same correlation
          mapRefRelToCorRef.put(rel,
              mapFieldAccessToCorVar.get(fieldAccess));
        } else {
          final CorRef correlation =
              new CorRef(var.id, fieldAccess.getField().getIndex(),
                  corrIdGenerator++);
          mapFieldAccessToCorVar.put(fieldAccess, correlation);
          mapRefRelToCorRef.put(rel, correlation);
        }
      }
      return super.visitFieldAccess(fieldAccess);
    }

    @Override public Void visitSubQuery(RexSubQuery subQuery) {
      subQuery.rel.accept(CorelMapBuilder.this);
      return super.visitSubQuery(subQuery);
    }
  };
}
 
Example #22
Source File: DrillUnnestRel.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public LogicalOperator implement(DrillImplementor implementor) {
  if(getRef() instanceof RexFieldAccess) {
    final RexFieldAccess fldAccess = (RexFieldAccess)getRef();
    return new Unnest(SchemaPath.getSimplePath(fldAccess.getField().getName()));
  }

  return null;
}
 
Example #23
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 #24
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private CorelMap(Multimap<RelNode, CorRef> mapRefRelToCorRef,
    SortedMap<CorrelationId, RelNode> mapCorToCorRel,
    Map<RexFieldAccess, CorRef> mapFieldAccessToCorRef) {
  this.mapRefRelToCorRef = mapRefRelToCorRef;
  this.mapCorToCorRel = mapCorToCorRel;
  this.mapFieldAccessToCorRef = ImmutableMap.copyOf(mapFieldAccessToCorRef);
}
 
Example #25
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
private CorelMap(Multimap<RelNode, CorRef> mapRefRelToCorRef,
    SortedMap<CorrelationId, RelNode> mapCorToCorRel,
    Map<RexFieldAccess, CorRef> mapFieldAccessToCorRef) {
  this.mapRefRelToCorRef = mapRefRelToCorRef;
  this.mapCorToCorRel = mapCorToCorRel;
  this.mapFieldAccessToCorRef = ImmutableMap.copyOf(mapFieldAccessToCorRef);
}
 
Example #26
Source File: RelDecorrelator.java    From calcite 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 #27
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  int newInputOutputOffset = 0;
  for (RelNode input : currentRel.getInputs()) {
    final Frame frame = map.get(input);

    if (frame != null) {
      // try to find in this input rel the position of corVar
      final CorRef corRef = cm.mapFieldAccessToCorRef.get(fieldAccess);

      if (corRef != null) {
        Integer newInputPos = frame.corDefOutputs.get(corRef.def());
        if (newInputPos != null) {
          // This input does produce the corVar referenced.
          return new RexInputRef(newInputPos + newInputOutputOffset,
              frame.r.getRowType().getFieldList().get(newInputPos)
                  .getType());
        }
      }

      // this input does not produce the corVar needed
      newInputOutputOffset += frame.r.getRowType().getFieldCount();
    } else {
      // this input is not rewritten
      newInputOutputOffset += input.getRowType().getFieldCount();
    }
  }
  return fieldAccess;
}
 
Example #28
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 #29
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override public RexNode visitFieldAccess(RexFieldAccess fieldAccess) {
  int newInputOutputOffset = 0;
  for (RelNode input : currentRel.getInputs()) {
    final Frame frame = map.get(input);

    if (frame != null) {
      // try to find in this input rel the position of corVar
      final CorRef corRef = cm.mapFieldAccessToCorRef.get(fieldAccess);

      if (corRef != null) {
        Integer newInputPos = frame.corDefOutputs.get(corRef.def());
        if (newInputPos != null) {
          // This input does produce the corVar referenced.
          return new RexInputRef(newInputPos + newInputOutputOffset,
              frame.r.getRowType().getFieldList().get(newInputPos)
                  .getType());
        }
      }

      // this input does not produce the corVar needed
      newInputOutputOffset += frame.r.getRowType().getFieldCount();
    } else {
      // this input is not rewritten
      newInputOutputOffset += input.getRowType().getFieldCount();
    }
  }
  return fieldAccess;
}
 
Example #30
Source File: RelDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private CorelMap(Multimap<RelNode, CorRef> mapRefRelToCorRef,
    SortedMap<CorrelationId, RelNode> mapCorToCorRel,
    Map<RexFieldAccess, CorRef> mapFieldAccessToCorRef) {
  this.mapRefRelToCorRef = mapRefRelToCorRef;
  this.mapCorToCorRel = mapCorToCorRel;
  this.mapFieldAccessToCorRef = ImmutableMap.copyOf(mapFieldAccessToCorRef);
}