Java Code Examples for org.apache.calcite.util.Util#FoundOne

The following examples show how to use org.apache.calcite.util.Util#FoundOne . 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: AbstractMaterializedViewRule.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces all the input references by the position in the
 * input column set. If a reference index cannot be found in
 * the input set, then we return null.
 */
private static RexNode shuttleReferences(final RexBuilder rexBuilder, final RexNode node, final Mapping mapping) {
    try {
        RexShuttle visitor = new RexShuttle() {
            @Override
            public RexNode visitInputRef(RexInputRef inputRef) {
                int pos = mapping.getTargetOpt(inputRef.getIndex());
                if (pos != -1) {
                    // Found it
                    return rexBuilder.makeInputRef(inputRef.getType(), pos);
                }
                throw Util.FoundOne.NULL;
            }
        };
        return visitor.apply(node);
    } catch (Util.FoundOne ex) {
        Util.swallow(ex, null);
        return null;
    }
}
 
Example 2
Source File: MutableRels.java    From Bats with Apache License 2.0 6 votes vote down vote up
public static boolean contains(MutableRel ancestor,
    final MutableRel target) {
  if (ancestor.equals(target)) {
    // Short-cut common case.
    return true;
  }
  try {
    new MutableRelVisitor() {
      @Override public void visit(MutableRel node) {
        if (node.equals(target)) {
          throw Util.FoundOne.NULL;
        }
        super.visit(node);
      }
      // CHECKSTYLE: IGNORE 1
    }.go(ancestor);
    return false;
  } catch (Util.FoundOne e) {
    return true;
  }
}
 
Example 3
Source File: RelOptUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static boolean containsGet(RexNode node) {
  try {
    node.accept(
        new RexVisitorImpl<Void>(true) {
          @Override public Void visitCall(RexCall call) {
            if (call.getOperator() == RexBuilder.GET_OPERATOR) {
              throw Util.FoundOne.NULL;
            }
            return super.visitCall(call);
          }
        });
    return false;
  } catch (Util.FoundOne e) {
    return true;
  }
}
 
Example 4
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any
 * {@link org.apache.calcite.rex.RexFieldAccess} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsFieldAccess(RexNode node) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitFieldAccess(RexFieldAccess fieldAccess) {
                throw new Util.FoundOne(fieldAccess);
            }
        };
        node.accept(visitor);
        return false;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 5
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexInputRef} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsInputRef(RexNode node) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitInputRef(RexInputRef inputRef) {
                throw new Util.FoundOne(inputRef);
            }
        };
        node.accept(visitor);
        return false;
    } catch (Util.FoundOne e) {
        Util.swallow(e, null);
        return true;
    }
}
 
Example 6
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given expression is deterministic.
 *
 * @param e Expression
 * @return true if tree result is deterministic, false otherwise
 */
public static boolean isDeterministic(RexNode e) {
    try {
        RexVisitor<Void> visitor = new RexVisitorImpl<Void>(true) {
            @Override
            public Void visitCall(RexCall call) {
                if (!call.getOperator().isDeterministic()) {
                    throw Util.FoundOne.NULL;
                }
                return super.visitCall(call);
            }
        };
        e.accept(visitor);
        return true;
    } catch (Util.FoundOne ex) {
        Util.swallow(ex, null);
        return false;
    }
}
 
Example 7
Source File: RelDecorrelator.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Finds a {@link RexInputRef} that is equivalent to a {@link CorRef},
 * and if found, throws a {@link org.apache.calcite.util.Util.FoundOne}. */
private void findCorrelationEquivalent(CorRef correlation, RexNode e)
    throws Util.FoundOne {
  switch (e.getKind()) {
  case EQUALS:
    final RexCall call = (RexCall) e;
    final List<RexNode> operands = call.getOperands();
    if (references(operands.get(0), correlation)) {
      throw new Util.FoundOne(operands.get(1));
    }
    if (references(operands.get(1), correlation)) {
      throw new Util.FoundOne(operands.get(0));
    }
    break;
  case AND:
    for (RexNode operand : ((RexCall) e).getOperands()) {
      findCorrelationEquivalent(correlation, operand);
    }
  }
}
 
Example 8
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any
 * {@link org.apache.calcite.rex.RexFieldAccess} nodes.
 *
 * @param node a RexNode tree
 */
public static boolean containsFieldAccess(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitFieldAccess(RexFieldAccess fieldAccess) {
            throw new Util.FoundOne(fieldAccess);
          }
        };
    node.accept(visitor);
    return false;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return true;
  }
}
 
Example 9
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether a given tree contains any {link RexTableInputRef} nodes.
 *
 * @param node a RexNode tree
 * @return first such node found or null if it there is no such node
 */
public static RexTableInputRef containsTableInputRef(RexNode node) {
  try {
    RexVisitor<Void> visitor =
        new RexVisitorImpl<Void>(true) {
          public Void visitTableInputRef(RexTableInputRef inputRef) {
            throw new Util.FoundOne(inputRef);
          }
        };
    node.accept(visitor);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (RexTableInputRef) e.getNode();
  }
}
 
Example 10
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether a {@link Project} contains a sub-query. */
public static boolean containsSubQuery(Project project) {
    for (RexNode node : project.getProjects()) {
        try {
            node.accept(INSTANCE);
        } catch (Util.FoundOne e) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns whether an expression contains a {@link RexCorrelVariable}. */
public static boolean containsCorrelation(RexNode condition) {
    try {
        condition.accept(CorrelationFinder.INSTANCE);
        return false;
    } catch (Util.FoundOne e) {
        return true;
    }
}
 
Example 12
Source File: RexUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static RexSubQuery find(Iterable<RexNode> nodes) {
    for (RexNode node : nodes) {
        try {
            node.accept(INSTANCE);
        } catch (Util.FoundOne e) {
            return (RexSubQuery) e.getNode();
        }
    }
    return null;
}
 
Example 13
Source File: SqlUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns a list of ancestors of {@code predicate} within a given
 * {@code SqlNode} tree.
 *
 * <p>The first element of the list is {@code root}, and the last is
 * the node that matched {@code predicate}. Throws if no node matches.
 */
public static ImmutableList<SqlNode> getAncestry(SqlNode root,
    Predicate<SqlNode> predicate, Predicate<SqlNode> postPredicate) {
  try {
    new Genealogist(predicate, postPredicate).visitChild(root);
    throw new AssertionError("not found: " + predicate + " in " + root);
  } catch (Util.FoundOne e) {
    //noinspection unchecked
    return (ImmutableList<SqlNode>) e.getNode();
  }
}
 
Example 14
Source File: SqlBetweenOperator.java    From Bats with Apache License 2.0 5 votes vote down vote up
boolean containsAnd(SqlNode node) {
  try {
    node.accept(this);
    return false;
  } catch (Util.FoundOne e) {
    return true;
  }
}
 
Example 15
Source File: SqlUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns a list of ancestors of {@code predicate} within a given
 * {@code SqlNode} tree.
 *
 * <p>The first element of the list is {@code root}, and the last is
 * the node that matched {@code predicate}. Throws if no node matches.
 */
public static ImmutableList<SqlNode> getAncestry(SqlNode root,
    Predicate<SqlNode> predicate, Predicate<SqlNode> postPredicate) {
  try {
    new Genealogist(predicate, postPredicate).visitChild(root);
    throw new AssertionError("not found: " + predicate + " in " + root);
  } catch (Util.FoundOne e) {
    //noinspection unchecked
    return (ImmutableList<SqlNode>) e.getNode();
  }
}
 
Example 16
Source File: RexUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static RexSubQuery find(Iterable<RexNode> nodes) {
  for (RexNode node : nodes) {
    try {
      node.accept(INSTANCE);
    } catch (Util.FoundOne e) {
      return (RexSubQuery) e.getNode();
    }
  }
  return null;
}
 
Example 17
Source File: AggFinder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Finds an aggregate.
 *
 * @param node Parse tree to search
 * @return First aggregate function in parse tree, or null if not found
 */
public SqlCall findAgg(SqlNode node) {
  try {
    node.accept(this);
    return null;
  } catch (Util.FoundOne e) {
    Util.swallow(e, null);
    return (SqlCall) e.getNode();
  }
}
 
Example 18
Source File: PushFilterPastProjectRule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitSubQuery(RexSubQuery subQuery) {
  throw new Util.FoundOne(subQuery);
}
 
Example 19
Source File: RexUtil.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public Void visitSubQuery(RexSubQuery subQuery) {
  throw new Util.FoundOne(subQuery);
}
 
Example 20
Source File: RexUtil.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public Void visitSubQuery(RexSubQuery subQuery) {
    throw new Util.FoundOne(subQuery);
}