Java Code Examples for org.apache.calcite.linq4j.Enumerable#enumerator()

The following examples show how to use org.apache.calcite.linq4j.Enumerable#enumerator() . 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 testList2() {
  final List<String> experience = Arrays.asList("jimi", "mitch", "noel");
  final Enumerator<String> enumerator = Linq4j.enumerator(experience);
  assertThat(enumerator.getClass().getName(), endsWith("ListEnumerator"));
  assertThat(count(enumerator), equalTo(3));

  final Enumerable<String> listEnumerable = Linq4j.asEnumerable(experience);
  final Enumerator<String> listEnumerator = listEnumerable.enumerator();
  assertThat(listEnumerator.getClass().getName(),
      endsWith("ListEnumerator"));
  assertThat(count(listEnumerator), equalTo(3));

  final Enumerable<String> linkedListEnumerable =
      Linq4j.asEnumerable(Lists.newLinkedList(experience));
  final Enumerator<String> iterableEnumerator =
      linkedListEnumerable.enumerator();
  assertThat(iterableEnumerator.getClass().getName(),
      endsWith("IterableEnumerator"));
  assertThat(count(iterableEnumerator), equalTo(3));
}
 
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: CorrelateJoinTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void testJoin(JoinType joinType, Integer[][] expected) {
  Enumerable<Integer[]> join =
      Linq4j.asEnumerable(ImmutableList.of(1, 2, 3, 10, 20, 30))
          .correlateJoin(joinType, a0 -> {
            if (a0 == 1 || a0 == 10) {
              return Linq4j.emptyEnumerable();
            }
            if (a0 == 2 || a0 == 20) {
              return Linq4j.singletonEnumerable(a0 * 10);
            }
            if (a0 == 3 || a0 == 30) {
              return Linq4j.asEnumerable(
                  ImmutableList.of(-a0 * 10, -a0 * 20));
            }
            throw new IllegalArgumentException(
                "Unexpected input " + a0);
          }, SELECT_BOTH);
  for (int i = 0; i < 2; i++) {
    Enumerator<Integer[]> e = join.enumerator();
    checkResults(e, expected);
    e.close();
  }
}
 
Example 5
Source File: EnumerableDataScanner.java    From herddb with Apache License 2.0 5 votes vote down vote up
public EnumerableDataScanner(
        Transaction transaction, String[] fieldNames, Column[] schema,
        Enumerable<DataAccessor> wrapped,
        DataScanner originalLeft, DataScanner originalRight
) {
    super(transaction, fieldNames, schema);
    this.originalLeft = originalLeft;
    this.originalRight = originalRight;
    this.wrapped = wrapped.enumerator();
    fetchNext();
}
 
Example 6
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testEmptyEnumerable() {
  final Enumerable<Object> enumerable = Linq4j.emptyEnumerable();
  assertThat(enumerable.any(), is(false));
  assertThat(enumerable.longCount(), equalTo(0L));
  final Enumerator<Object> enumerator = enumerable.enumerator();
  assertThat(enumerator.moveNext(), is(false));
}
 
Example 7
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testSingletonEnumerable() {
  final Enumerable<String> enumerable = Linq4j.singletonEnumerable("foo");
  assertThat(enumerable.any(), is(true));
  assertThat(enumerable.longCount(), equalTo(1L));
  final Enumerator<String> enumerator = enumerable.enumerator();
  assertThat(enumerator.moveNext(), is(true));
  assertThat(enumerator.current(), equalTo("foo"));
  assertThat(enumerator.moveNext(), is(false));
}
 
Example 8
Source File: JdbcTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerator<T> enumerator() {
  final JavaTypeFactory typeFactory =
      ((CalciteConnection) queryProvider).getTypeFactory();
  final SqlString sql = generateSql();
  //noinspection unchecked
  final Enumerable<T> enumerable = (Enumerable<T>) ResultSetEnumerable.of(
      jdbcSchema.getDataSource(),
      sql.getSql(),
      JdbcUtils.ObjectArrayRowBuilder.factory(fieldClasses(typeFactory)));
  return enumerable.enumerator();
}
 
Example 9
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 10
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Apply tumbling per row from the enumerable input.
 */
public static <TSource, TResult> Enumerable<TResult> tumbling(
    Enumerable<TSource> inputEnumerable,
    Function1<TSource, TResult> outSelector) {
  return new AbstractEnumerable<TResult>() {
    // Applies tumbling on each element from the input enumerator and produces
    // exactly one element for each input element.
    @Override public Enumerator<TResult> enumerator() {
      return new Enumerator<TResult>() {
        Enumerator<TSource> inputs = inputEnumerable.enumerator();

        public TResult current() {
          return outSelector.apply(inputs.current());
        }

        public boolean moveNext() {
          return inputs.moveNext();
        }

        public void reset() {
          inputs.reset();
        }

        public void close() {
        }
      };
    }
  };
}
 
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();
}
 
Example 13
Source File: CalciteRunners.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@SneakyThrows
    public static RowBaseIterator run(String sql, MycatCalciteDataContext calciteDataContext, RelNode relNode) {
        SqlRecorder recorder = SqlRecorderRuntime.INSTANCE.getCurrentRecorder();
        Map<String, List<SingeTargetSQLTable>> map = new HashMap<>();
        relNode.accept(new RelShuttleImpl() {
            @Override
            public RelNode visit(TableScan scan) {
                SingeTargetSQLTable unwrap = scan.getTable().unwrap(SingeTargetSQLTable.class);
                if (unwrap != null && !unwrap.existsEnumerable()) {
                    List<SingeTargetSQLTable> tables = map.computeIfAbsent(unwrap.getTargetName(), s -> new ArrayList<>(2));
                    tables.add(unwrap);
                }
                return super.visit(scan);
            }
        });

        HepProgramBuilder hepProgramBuilder = new HepProgramBuilder();
        hepProgramBuilder.addMatchLimit(64);

        hepProgramBuilder.addRuleInstance(StreamUnionRule.INSTANCE);
        final HepPlanner planner2 = new HepPlanner(hepProgramBuilder.build());
        planner2.setRoot(relNode);
        relNode = planner2.findBestExp();

        //check
        relNode.accept(new RelShuttleImpl() {
            @Override
            public RelNode visit(LogicalUnion union) {
                if (union.getInputs().size() > 2) {
                    throw new AssertionError("union input more 2");
                }
                return super.visit(union);
            }
        });
        long startGetConnectionTime = TimeProvider.INSTANCE.now();
        fork(sql, calciteDataContext, map);
        long cbo = TimeProvider.INSTANCE.now();
        recorder.addRecord(SqlRecorderType.GET_CONNECTION, sql, cbo - startGetConnectionTime);
        ArrayBindable bindable1 = Interpreters.bindable(relNode);
        long execution_start = TimeProvider.INSTANCE.now();
        recorder.addRecord(SqlRecorderType.CBO, sql, execution_start - cbo);
//        EnumerableInterpretable.toBindable()
        Enumerable<Object[]> bind = bindable1.bind(calciteDataContext);
        Enumerator<Object[]> enumerator = bind.enumerator();

        return new EnumeratorRowIterator(CalciteConvertors.getMycatRowMetaData(relNode.getRowType()), enumerator,
                () -> {
                    recorder.addRecord(SqlRecorderType.EXECUTION_TIME, sql, TimeProvider.INSTANCE.now()-execution_start);
                    recorder.addRecord(SqlRecorderType.AT_END, sql, TimeProvider.INSTANCE.now());
                });
    }
 
Example 14
Source File: CassandraTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Enumerator<T> enumerator() {
  //noinspection unchecked
  final Enumerable<T> enumerable =
      (Enumerable<T>) getTable().query(getSession());
  return enumerable.enumerator();
}
 
Example 15
Source File: MongoTable.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Enumerator<T> enumerator() {
  //noinspection unchecked
  final Enumerable<T> enumerable =
      (Enumerable<T>) getTable().find(getMongoDb(), null, null, null);
  return enumerable.enumerator();
}