Java Code Examples for org.apache.calcite.linq4j.Linq4j#asEnumerable()

The following examples show how to use org.apache.calcite.linq4j.Linq4j#asEnumerable() . 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: HashJoinPlan.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Scanner scan(DataContext dataContext, long flags) {
    Scanner left = this.left.scan(dataContext, flags);
    Scanner right = this.right.scan(dataContext, flags);

    Enumerable<DataAccessor> outer = Linq4j.asEnumerable(() -> left);
    Enumerable<DataAccessor> inner = Linq4j.asEnumerable(() -> right);
    Function1<DataAccessor, JoinKey> leftJoinKey = a0 ->  JoinKey.keyExtractor(leftKeys).apply(a0);
    Function1<DataAccessor, JoinKey> rightJoinKey = a0 -> JoinKey.keyExtractor(rightKeys).apply(a0);
    Function2<DataAccessor, DataAccessor, DataAccessor> resultSelector = new Function2<DataAccessor, DataAccessor, DataAccessor>() {
        @Override
        public DataAccessor apply(DataAccessor v0, DataAccessor v1) {
            return null;
        }
    };
    return Scanner.of(EnumerableDefaults.hashJoin(outer,
            inner,
            leftJoinKey,
            rightJoinKey,
            resultSelector,
            null,generateNullsOnLeft,generateNullsOnRight).iterator());
}
 
Example 2
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 6 votes vote down vote up
protected MetaResultSet createResultSet(
    Map<String, Object> internalParameters, List<ColumnMetaData> columns,
    CursorFactory cursorFactory, final Frame firstFrame) {
  try {
    final QuarkConnectionImpl connection = getConnection();
    final AvaticaStatement statement = connection.createStatement();
    final CalcitePrepare.CalciteSignature<Object> signature =
        new CalcitePrepare.CalciteSignature<Object>("",
            ImmutableList.<AvaticaParameter>of(), internalParameters, null,
            columns, cursorFactory, null, ImmutableList.<RelCollation>of(), -1,
            null, Meta.StatementType.SELECT) {
          @Override public Enumerable<Object> enumerable(
              DataContext dataContext) {
            return Linq4j.asEnumerable(firstFrame.rows);
          }
        };
    return MetaResultSet.create(connection.id, statement.getId(), true,
        signature, firstFrame);
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example 3
Source File: Smalls.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static QueryableTable oneThreePlus(String s) {
  List<Integer> items;
  // Argument is null in case SQL contains function call with expression.
  // Then the engine calls a function with null arguments to get getRowType.
  if (s == null) {
    items = ImmutableList.of();
  } else {
    Integer latest = Integer.parseInt(s.substring(1, s.length() - 1));
    items = ImmutableList.of(1, 3, latest);
  }
  final Enumerable<Integer> enumerable = Linq4j.asEnumerable(items);
  return new AbstractQueryableTable(Integer.class) {
    public <E> Queryable<E> asQueryable(
        QueryProvider queryProvider, SchemaPlus schema, String tableName) {
      //noinspection unchecked
      return (Queryable<E>) enumerable.asQueryable();
    }

    public RelDataType getRowType(RelDataTypeFactory typeFactory) {
      return typeFactory.builder().add("c", SqlTypeName.INTEGER).build();
    }
  };
}
 
Example 4
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
protected MetaResultSet createResultSet(
    Map<String, Object> internalParameters, List<ColumnMetaData> columns,
    CursorFactory cursorFactory, final Frame firstFrame) {
  try {
    final CalciteConnectionImpl connection = getConnection();
    final AvaticaStatement statement = connection.createStatement();
    final CalcitePrepare.CalciteSignature<Object> signature =
        new CalcitePrepare.CalciteSignature<Object>("",
            ImmutableList.of(), internalParameters, null,
            columns, cursorFactory, null, ImmutableList.of(), -1,
            null, Meta.StatementType.SELECT) {
          @Override public Enumerable<Object> enumerable(
              DataContext dataContext) {
            return Linq4j.asEnumerable(firstFrame.rows);
          }
        };
    return MetaResultSet.create(connection.id, statement.getId(), true,
        signature, firstFrame);
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: Linq4jTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testZipLengthNotMatch() {
  final Enumerable<String> e1 = Linq4j.asEnumerable(Arrays.asList("a", "b"));
  final Enumerable<String> e2 = Linq4j.asEnumerable(Arrays.asList("1", "2", "3"));

  final Function2<String, String, String> resultSelector = (v0, v1) -> v0 + v1;

  final Enumerable<String> zipped1 = e1.zip(e2, resultSelector);
  assertEquals(2, zipped1.count());
  assertEquals(2, count(zipped1.enumerator()));
  zipped1.enumerator().reset();
  for (int i = 0; i < 2; i++) {
    assertEquals("" + (char) ('a' + i) + (char) ('1' + i), zipped1.elementAt(i));
  }

  final Enumerable<String> zipped2 = e2.zip(e1, resultSelector);
  assertEquals(2, zipped2.count());
  assertEquals(2, count(zipped2.enumerator()));
  zipped2.enumerator().reset();
  for (int i = 0; i < 2; i++) {
    assertEquals("" + (char) ('1' + i) + (char) ('a' + i), zipped2.elementAt(i));
  }
}
 
Example 6
Source File: Linq4jTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTakeWhileEnumerableFunction() {
  final Enumerable<Department> enumerableDepts =
      Linq4j.asEnumerable(depts);
  final List<Department> deptList =
      EnumerableDefaults.takeWhile(
          enumerableDepts,
          new Predicate2<Department, Integer>() {
            int index = 0;

            public boolean apply(Department v1, Integer v2) {
              // Make sure we're passed the correct indices
              assertEquals(index++, (int) v2, "Invalid index passed to function");
              return 20 != v1.deptno;
            }
          }).toList();

  assertEquals(1, deptList.size());
  assertEquals(depts[0], deptList.get(0));
}
 
Example 7
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTakeEnumerableGreaterThanLength() {
  final Enumerable<Department> enumerableDepts =
      Linq4j.asEnumerable(depts);
  final List<Department> depList =
      EnumerableDefaults.take(enumerableDepts, 5).toList();
  assertEquals(3, depList.size());
  assertEquals(depts[0], depList.get(0));
  assertEquals(depts[1], depList.get(1));
  assertEquals(depts[2], depList.get(2));
}
 
Example 8
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLastOrDefaultWithPredicate() {
  final Enumerable<String> enumerable =
      Linq4j.asEnumerable(Arrays.asList("jimi", "mitch", "ming"));
  assertEquals("mitch", enumerable.lastOrDefault(x -> x.startsWith("mit")));
  assertNull(enumerable.lastOrDefault(x -> false));

  @SuppressWarnings("unchecked")
  final Enumerable<String> emptyEnumerable = Linq4j.asEnumerable(Collections.EMPTY_LIST);
  assertNull(
      emptyEnumerable.lastOrDefault(x -> {
        fail();
        return false;
      }));
}
 
Example 9
Source File: ArrayTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** As {@link #testLoadSorted()} but column #1 is the unique column, not
 * column #0. The algorithm needs to go back and permute the values of
 * column #0 after it discovers that column #1 is unique and sorts by it. */
@Test void testLoadSorted2() {
  final JavaTypeFactoryImpl typeFactory =
      new JavaTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType rowType =
      typeFactory.builder()
          .add("deptno", typeFactory.createType(int.class))
          .add("empid", typeFactory.createType(int.class))
          .add("name", typeFactory.createType(String.class))
          .build();
  final Enumerable<Object[]> enumerable =
      Linq4j.asEnumerable(
          Arrays.asList(
              new Object[]{10, 100, "Bill"},
              new Object[]{20, 200, "Eric"},
              new Object[]{30, 150, "Sebastian"},
              new Object[]{10, 160, "Theodore"}));
  final ColumnLoader<Object[]> loader =
      new ColumnLoader<Object[]>(typeFactory, enumerable,
          RelDataTypeImpl.proto(rowType), null);
  // Note that values have been sorted with {20, 200, Eric} last because the
  // value 200 is the highest value of empid, the unique column.
  checkColumn(
      loader.representationValues.get(0),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=0, bitCount=5, primitive=INT, signed=false), value=[10, 30, 10, 20, 0, 0, 0, 0, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(1),
      ArrayTable.RepresentationType.BIT_SLICED_PRIMITIVE_ARRAY,
      "Column(representation=BitSlicedPrimitiveArray(ordinal=1, bitCount=8, primitive=INT, signed=false), value=[100, 150, 160, 200, 0, 0, 0, 0])");
  checkColumn(
      loader.representationValues.get(2),
      ArrayTable.RepresentationType.OBJECT_ARRAY,
      "Column(representation=ObjectArray(ordinal=2), value=[Bill, Sebastian, Theodore, Eric])");
}
 
Example 10
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLast() {
  final Enumerable<String> enumerable = Linq4j.asEnumerable(Arrays.asList("jimi", "mitch"));
  assertEquals("mitch", enumerable.last());

  final Enumerable<?> emptyEnumerable = Linq4j.asEnumerable(Collections.EMPTY_LIST);
  try {
    emptyEnumerable.last();
    fail();
  } catch (Exception ignored) {
    // ok
  }
}
 
Example 11
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testZip() {
  final Enumerable<String> e1 = Linq4j.asEnumerable(Arrays.asList("a", "b", "c"));
  final Enumerable<String> e2 = Linq4j.asEnumerable(Arrays.asList("1", "2", "3"));

  final Enumerable<String> zipped = e1.zip(e2, (v0, v1) -> v0 + v1);
  assertEquals(3, zipped.count());
  zipped.enumerator().reset();
  for (int i = 0; i < 3; i++) {
    assertEquals("" + (char) ('a' + i) + (char) ('1' + i), zipped.elementAt(i));
  }
}
 
Example 12
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTakeWhileEnumerablePredicate() {
  final Enumerable<Department> enumerableDepts =
      Linq4j.asEnumerable(depts);
  final List<Department> deptList =
      EnumerableDefaults.takeWhile(
          enumerableDepts, v1 -> v1.name.contains("e")).toList();

  // Only one department:
  // 0: Sales --> true
  // 1: HR --> false
  // 2: Marketing --> never get to it (we stop after false)
  assertEquals(1, deptList.size());
  assertEquals(depts[0], deptList.get(0));
}
 
Example 13
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTakeQueryableGreaterThanLength() {
  final Enumerable<Department> enumerableDepts =
      Linq4j.asEnumerable(depts);
  final List<Department> depList =
      EnumerableDefaults.take(enumerableDepts, 5).toList();
  assertEquals(3, depList.size());
  assertEquals(depts[0], depList.get(0));
  assertEquals(depts[1], depList.get(1));
  assertEquals(depts[2], depList.get(2));
}
 
Example 14
Source File: MycatReflectiveSchema.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private static Enumerable toEnumerable(final Object o) {
  if (o.getClass().isArray()) {
    if (o instanceof Object[]) {
      return Linq4j.asEnumerable((Object[]) o);
    } else {
      return Linq4j.asEnumerable(Primitive.asList(o));
    }
  }
  if (o instanceof Iterable) {
    return Linq4j.asEnumerable((Iterable) o);
  }
  throw new RuntimeException(
      "Cannot convert " + o.getClass() + " into a Enumerable");
}
 
Example 15
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testTakeEnumerable() {
  final Enumerable<Department> enumerableDepts =
      Linq4j.asEnumerable(depts);
  final List<Department> enumerableDeptsResult =
      EnumerableDefaults.take(enumerableDepts, 2).toList();
  assertEquals(2, enumerableDeptsResult.size());
  assertEquals(depts[0], enumerableDeptsResult.get(0));
  assertEquals(depts[1], enumerableDeptsResult.get(1));

  final List<Department> enumerableDeptsResult5 =
      EnumerableDefaults.take(enumerableDepts, 5).toList();
  assertEquals(3, enumerableDeptsResult5.size());
}
 
Example 16
Source File: NestedLoopPlan.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Scanner scan(DataContext dataContext, long flags) {
    final Enumerable<DataAccessor> outer = Linq4j.asEnumerable(() -> left.scan(dataContext, flags));
    final Enumerable<DataAccessor> inner = Linq4j.asEnumerable(() -> right.scan(dataContext, flags));
    final Predicate2<DataAccessor, DataAccessor> predicate = null;
    final Function2<DataAccessor, DataAccessor, DataAccessor> resultSelector = null;
    final JoinType joinType = this.joinRelType;
    return Scanner.of(EnumerableDefaults.nestedLoopJoin(outer, inner, predicate, resultSelector, joinType).iterator());
}
 
Example 17
Source File: Linq4jTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testElementAtOrDefault() {
  final Enumerable<String> enumerable = Linq4j.asEnumerable(Arrays.asList("jimi", "mitch"));
  assertEquals("jimi", enumerable.elementAtOrDefault(0));
  assertNull(enumerable.elementAtOrDefault(2));
  assertNull(enumerable.elementAtOrDefault(-1));
}
 
Example 18
Source File: SparkRuntime.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Converts an RDD into an enumerable. */
public static <T> Enumerable<T> asEnumerable(JavaRDD<T> rdd) {
  return Linq4j.asEnumerable(rdd.collect());
}
 
Example 19
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 4 votes vote down vote up
protected Enumerable<MetaTypeInfo> allTypeInfo() {
  return Linq4j.asEnumerable(getAllDefaultType());
}
 
Example 20
Source File: EnumerablesTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test @Disabled // TODO fix this
public void testMatch() {
  final Enumerable<Emp> emps = Linq4j.asEnumerable(
      Arrays.asList(
          new Emp(20, "Theodore"),
          new Emp(10, "Fred"),
          new Emp(20, "Sebastian"),
          new Emp(30, "Joe")));

  final Pattern p =
      Pattern.builder()
          .symbol("A")
          .symbol("B").seq()
          .build();

  final Matcher<Emp> matcher =
      Matcher.<Emp>builder(p.toAutomaton())
          .add("A", s -> s.get().deptno == 20)
          .add("B", s -> s.get().deptno != 20)
          .build();

  final Enumerables.Emitter<Emp, String> emitter =
      (rows, rowStates, rowSymbols, match, consumer) -> {
        for (int i = 0; i < rows.size(); i++) {
          if (rowSymbols == null) {
            continue;
          }
          if ("A".equals(rowSymbols.get(i))) {
            consumer.accept(
                String.format(Locale.ENGLISH, "%s %s %d", rows, rowStates,
                    match));
          }
        }
      };

  final Enumerable<String> matches =
      Enumerables.match(emps, emp -> 0L, matcher, emitter, 1, 1);
  assertThat(matches.toList().toString(),
      equalTo("[[Emp(20, Theodore), Emp(10, Fred)] null 1, "
          + "[Emp(20, Sebastian), Emp(30, Joe)] null 2]"));
}