Java Code Examples for org.apache.calcite.linq4j.Enumerator#moveNext()

The following examples show how to use org.apache.calcite.linq4j.Enumerator#moveNext() . 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: Linq4jTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void cartesianProductWithReset() {
  Enumerator<List<Integer>> product =
      Linq4j.product(
          Arrays.asList(
              Linq4j.enumerator(Arrays.asList(1, 2)),
              Linq4j.enumerator(Arrays.asList(3, 4))));

  assertEquals(
      "[[1, 3], [1, 4], [2, 3], [2, 4]]",
      contentsOf(product).toString(),
      "cartesian product");
  product.reset();
  assertEquals(
      "[[1, 3], [1, 4], [2, 3], [2, 4]]",
      contentsOf(product).toString(),
      "cartesian product after .reset()");
  product.moveNext();
  product.reset();
  assertEquals(
      "[[1, 3], [1, 4], [2, 3], [2, 4]]",
      contentsOf(product).toString(),
      "cartesian product after .moveNext(); .reset()");
}
 
Example 2
Source File: Linq4jTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDefaultIfEmpty() {
  final List<String> experience = Arrays.asList("jimi", "mitch", "noel");
  final Enumerable<String> notEmptyEnumerable = Linq4j.asEnumerable(experience).defaultIfEmpty();
  final Enumerator<String> notEmptyEnumerator = notEmptyEnumerable.enumerator();
  notEmptyEnumerator.moveNext();
  assertEquals("jimi", notEmptyEnumerator.current());
  notEmptyEnumerator.moveNext();
  assertEquals("mitch", notEmptyEnumerator.current());
  notEmptyEnumerator.moveNext();
  assertEquals("noel", notEmptyEnumerator.current());

  final Enumerable<String> emptyEnumerable =
      Linq4j.asEnumerable(Linq4j.<String>emptyEnumerable()).defaultIfEmpty();
  final Enumerator<String> emptyEnumerator = emptyEnumerable.enumerator();
  assertTrue(emptyEnumerator.moveNext());
  assertNull(emptyEnumerator.current());
  assertFalse(emptyEnumerator.moveNext());
}
 
Example 3
Source File: Linq4jTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testDefaultIfEmpty2() {
  final List<String> experience = Arrays.asList("jimi", "mitch", "noel");
  final Enumerable<String> notEmptyEnumerable =
      Linq4j.asEnumerable(experience).defaultIfEmpty("dummy");
  final Enumerator<String> notEmptyEnumerator = notEmptyEnumerable.enumerator();
  notEmptyEnumerator.moveNext();
  assertEquals("jimi", notEmptyEnumerator.current());
  notEmptyEnumerator.moveNext();
  assertEquals("mitch", notEmptyEnumerator.current());
  notEmptyEnumerator.moveNext();
  assertEquals("noel", notEmptyEnumerator.current());

  final Enumerable<String> emptyEnumerable =
      Linq4j.asEnumerable(Linq4j.<String>emptyEnumerable()).defaultIfEmpty("N/A");
  final Enumerator<String> emptyEnumerator = emptyEnumerable.enumerator();
  assertTrue(emptyEnumerator.moveNext());
  assertEquals("N/A", emptyEnumerator.current());
  assertFalse(emptyEnumerator.moveNext());
}
 
Example 4
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private <T> List<T> contentsOf(Enumerator<T> enumerator) {
  List<T> result = new ArrayList<>();
  while (enumerator.moveNext()) {
    result.add(enumerator.current());
  }
  return result;
}
 
Example 5
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static int count(Enumerator<String> enumerator) {
  int n = 0;
  while (enumerator.moveNext()) {
    if (enumerator.current() != null) {
      ++n;
    }
  }
  return n;
}
 
Example 6
Source File: CorrelateJoinTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void checkResults(Enumerator<Integer[]> e, Integer[][] expected) {
  List<Integer[]> res = new ArrayList<>();
  while (e.moveNext()) {
    res.add(e.current());
  }
  Integer[][] actual = res.toArray(new Integer[res.size()][]);
  assertArrayEquals(expected, actual);
}
 
Example 7
Source File: EnumerableBindable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Node implement(final InterpreterImplementor implementor) {
  return () -> {
    final Sink sink =
        implementor.relSinks.get(EnumerableBindable.this).get(0);
    final Enumerable<Object[]> enumerable = bind(implementor.dataContext);
    final Enumerator<Object[]> enumerator = enumerable.enumerator();
    while (enumerator.moveNext()) {
      sink.send(Row.asCopy(enumerator.current()));
    }
  };
}
 
Example 8
Source File: EnumerableInterpretable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public void run() throws InterruptedException {
  final Enumerator<Object[]> enumerator = enumerable.enumerator();
  while (enumerator.moveNext()) {
    Object[] values = enumerator.current();
    sink.send(Row.of(values));
  }
}
 
Example 9
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Function that, given a certain List containing single-item structs (i.e. arrays / lists with
 * a single item), builds an Enumerable that returns those single items inside the structs.
 */
public static Function1<Object, Enumerable<Comparable>> flatList() {
  return inputObject -> {
    final List list = (List) inputObject;
    final Enumerator<List<Object>> enumerator = Linq4j.enumerator(list);
    return new AbstractEnumerable<Comparable>() {
      public Enumerator<Comparable> enumerator() {
        return new Enumerator<Comparable>() {

          @Override public boolean moveNext() {
            return enumerator.moveNext();
          }

          @Override public Comparable current() {
            final Object element = enumerator.current();
            final Comparable comparable;
            if (element.getClass().isArray()) {
              comparable = (Comparable) ((Object[]) element)[0];
            } else {
              comparable = (Comparable) ((List) element).get(0);
            }
            return comparable;
          }

          @Override public void reset() {
            enumerator.reset();
          }

          @Override public void close() {
            enumerator.close();
          }
        };
      }
    };
  };
}
 
Example 10
Source File: IntegerIntervalSet.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public int size() {
  int n = 0;
  Enumerator<Integer> e = enumerator();
  while (e.moveNext()) {
    ++n;
  }
  return n;
}
 
Example 11
Source File: Interpreter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override public void setSourceEnumerable(Enumerable<Row> enumerable)
    throws InterruptedException {
  // just copy over the source into the local list
  final Enumerator<Row> enumerator = enumerable.enumerator();
  while (enumerator.moveNext()) {
    this.send(enumerator.current());
  }
  enumerator.close();
}
 
Example 12
Source File: Interpreter.java    From calcite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override public void setSourceEnumerable(Enumerable<Row> enumerable)
    throws InterruptedException {
  // just copy over the source into the local list
  final Enumerator<Row> enumerator = enumerable.enumerator();
  while (enumerator.moveNext()) {
    this.send(enumerator.current());
  }
  enumerator.close();
}