org.apache.calcite.rex.RexVisitor Java Examples

The following examples show how to use org.apache.calcite.rex.RexVisitor. 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: DrillRelMdSelectivity.java    From Bats with Apache License 2.0 6 votes vote down vote up
private static RexInputRef findRexInputRef(final RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitCall(RexCall call) {
            for (RexNode child : call.getOperands()) {
              child.accept(this);
            }
            return super.visitCall(call);
          }

          public Void visitInputRef(RexInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (RexInputRef) e.getNode();
  }
}
 
Example #2
Source File: SubstitutionVisitor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Check if join condition only references RexInputRef. */
private static boolean referenceByMapping(
    RexNode joinCondition, List<RexNode>... projectsOfInputs) {
  List<RexNode> projects = new ArrayList<>();
  for (List<RexNode> projectsOfInput: projectsOfInputs) {
    projects.addAll(projectsOfInput);
  }

  try {
    RexVisitor rexVisitor = new RexVisitorImpl<Void>(true) {
      @Override public Void visitInputRef(RexInputRef inputRef) {
        if (!(projects.get(inputRef.getIndex()) instanceof RexInputRef)) {
          throw Util.FoundOne.NULL;
        }
        return super.visitInputRef(inputRef);
      }
    };
    joinCondition.accept(rexVisitor);
  } catch (Util.FoundOne e) {
    return false;
  }
  return true;
}
 
Example #3
Source File: RelMdColumnOrigins.java    From calcite with Apache License 2.0 6 votes vote down vote up
private Set<RelColumnOrigin> getMultipleColumns(RexNode rexNode, RelNode input,
    final RelMetadataQuery mq) {
  final Set<RelColumnOrigin> set = new HashSet<>();
  final RexVisitor<Void> visitor =
      new RexVisitorImpl<Void>(true) {
        public Void visitInputRef(RexInputRef inputRef) {
          Set<RelColumnOrigin> inputSet =
              mq.getColumnOrigins(input, inputRef.getIndex());
          if (inputSet != null) {
            set.addAll(inputSet);
          }
          return null;
        }
      };
  rexNode.accept(visitor);
  return set;
}
 
Example #4
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #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: FunctionRenderer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public FunctionRenderer(
    boolean supportsV5Features,
    boolean scriptsEnabled,
    RenderMode mode,
    RexVisitor<FunctionRender> visitor) {
  super();
  this.supportsV5Features = supportsV5Features;
  this.scriptsEnabled = scriptsEnabled;
  this.mode = mode;
  this.visitor = visitor;
}
 
Example #7
Source File: RelFieldTrimmer.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
 * {@link org.apache.calcite.rel.logical.LogicalFilter}.
 */
public TrimResult trimFields(Filter filter, ImmutableBitSet fieldsUsed, Set<RelDataTypeField> extraFields) {
    final RelDataType rowType = filter.getRowType();
    final int fieldCount = rowType.getFieldCount();
    final RexNode conditionExpr = filter.getCondition();
    final RelNode input = filter.getInput();

    // We use the fields used by the consumer, plus any fields used in the
    // filter.
    final Set<RelDataTypeField> inputExtraFields = new LinkedHashSet<>(extraFields);
    RelOptUtil.InputFinder inputFinder = new RelOptUtil.InputFinder(inputExtraFields);
    inputFinder.inputBitSet.addAll(fieldsUsed);
    conditionExpr.accept(inputFinder);
    final ImmutableBitSet inputFieldsUsed = inputFinder.inputBitSet.build();

    // Create input with trimmed columns.
    TrimResult trimResult = trimChild(filter, input, inputFieldsUsed, inputExtraFields);
    RelNode newInput = trimResult.left;
    final Mapping inputMapping = trimResult.right;

    // If the input is unchanged, and we need to project all columns,
    // there's nothing we can do.
    if (newInput == input && fieldsUsed.cardinality() == fieldCount) {
        return result(filter, Mappings.createIdentity(fieldCount));
    }

    // Build new project expressions, and populate the mapping.
    final RexVisitor<RexNode> shuttle = new RexPermuteInputsShuttle(inputMapping, newInput);
    RexNode newConditionExpr = conditionExpr.accept(shuttle);

    // Use copy rather than relBuilder so that correlating variables get set.
    relBuilder.push(filter.copy(filter.getTraitSet(), newInput, newConditionExpr));

    // The result has the same mapping as the input gave us. Sometimes we
    // return fields that the consumer didn't ask for, because the filter
    // needs them for its condition.
    return result(relBuilder.build(), inputMapping);
}
 
Example #8
Source File: DrillRelMdSelectivity.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static List<RexInputRef> findAllRexInputRefs(final RexNode node) {
    List<RexInputRef> rexRefs = new ArrayList<>();
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitInputRef(RexInputRef inputRef) {
            rexRefs.add(inputRef);
            return super.visitInputRef(inputRef);
          }
        };
    node.accept(visitor);
    return rexRefs;
}
 
Example #9
Source File: DrillRelOptUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Travesal RexNode to find at least one operator in the given collection. Continue search if RexNode has a
 * RexInputRef which refers to a RexNode in project expressions.
 *
 * @param node : RexNode to search
 * @param projExprs : the list of project expressions. Empty list means there is No project operator underneath.
 * @param operators collection of operators to find
 * @return : Return null if there is NONE; return the first appearance of item/flatten RexCall.
 */
public static RexCall findOperators(final RexNode node, final List<RexNode> projExprs,
        final Collection<String> operators) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitCall(RexCall call) {
                if (operators.contains(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);
            }

            @Override
            public Void visitInputRef(RexInputRef inputRef) {
                if (projExprs.size() == 0) {
                    return super.visitInputRef(inputRef);
                } else {
                    final int index = inputRef.getIndex();
                    RexNode n = projExprs.get(index);
                    if (n instanceof RexCall) {
                        RexCall r = (RexCall) n;
                        if (operators.contains(r.getOperator().getName().toLowerCase())) {
                            throw new Util.FoundOne(r);
                        }
                    }

                    return super.visitInputRef(inputRef);
                }
            }
        };
        node.accept(visitor);
        return null;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return (RexCall) e.getNode();
    }
}
 
Example #10
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 #11
Source File: RexDynamicParamImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitDynamicParam(this);
}
 
Example #12
Source File: RexCorrelVariableImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitCorrelVariable(this);
}
 
Example #13
Source File: RelFieldTrimmer.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Variant of {@link #trimFields(RelNode, ImmutableBitSet, Set)} for
 * {@link org.apache.calcite.rel.logical.LogicalFilter}.
 */
public TrimResult trimFields(
    Filter filter,
    ImmutableBitSet fieldsUsed,
    Set<RelDataTypeField> extraFields) {
  final RelDataType rowType = filter.getRowType();
  final int fieldCount = rowType.getFieldCount();
  final RexNode conditionExpr = filter.getCondition();
  final RelNode input = filter.getInput();

  // We use the fields used by the consumer, plus any fields used in the
  // filter.
  final Set<RelDataTypeField> inputExtraFields =
      new LinkedHashSet<>(extraFields);
  RelOptUtil.InputFinder inputFinder =
      new RelOptUtil.InputFinder(inputExtraFields, fieldsUsed);
  conditionExpr.accept(inputFinder);
  final ImmutableBitSet inputFieldsUsed = inputFinder.build();

  // Create input with trimmed columns.
  TrimResult trimResult =
      trimChild(filter, input, inputFieldsUsed, inputExtraFields);
  RelNode newInput = trimResult.left;
  final Mapping inputMapping = trimResult.right;

  // If the input is unchanged, and we need to project all columns,
  // there's nothing we can do.
  if (newInput == input
      && fieldsUsed.cardinality() == fieldCount) {
    return result(filter, Mappings.createIdentity(fieldCount));
  }

  // Build new project expressions, and populate the mapping.
  final RexVisitor<RexNode> shuttle =
      new RexPermuteInputsShuttle(inputMapping, newInput);
  RexNode newConditionExpr =
      conditionExpr.accept(shuttle);

  // Build new filter with trimmed input and condition.
  relBuilder.push(newInput)
      .filter(filter.getVariablesSet(), newConditionExpr);

  // The result has the same mapping as the input gave us. Sometimes we
  // return fields that the consumer didn't ask for, because the filter
  // needs them for its condition.
  return result(relBuilder.build(), inputMapping);
}
 
Example #14
Source File: RexInputRefImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitInputRef(this);
}
 
Example #15
Source File: FunctionRenderer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public RexVisitor<FunctionRender> getVisitor() {
  return visitor;
}
 
Example #16
Source File: RexCallImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitCall(this);
}
 
Example #17
Source File: RexOverImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitOver(this);
}
 
Example #18
Source File: RexLiteralImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitLiteral(this);
}
 
Example #19
Source File: RexLocalRefImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitLocalRef(this);
}
 
Example #20
Source File: RexSubQueryImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitSubQuery(this);
}
 
Example #21
Source File: RexFieldAccessImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitFieldAccess(this);
}
 
Example #22
Source File: RexRangeRefImpl.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public <R> R accept(RexVisitor<R> visitor) {
    return visitor.visitRangeRef(this);
}