org.apache.calcite.linq4j.Enumerable Java Examples

The following examples show how to use org.apache.calcite.linq4j.Enumerable. 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: JoinPreserveOrderTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void testJoin(
    JoinAlgorithm<Employee, Department, List<Integer>> joinAlgorithm,
    AssertOrder assertLeftInput, AssertOrder assertRightInput) {
  Enumerable<Employee> left =
      Linq4j.asEnumerable(EMPS)
          .orderBy(leftColumn.colSelector,
              nullsComparator(leftColumn.isNullsFirst, !leftColumn.isAscending));
  Enumerable<Department> right =
      Linq4j.asEnumerable(DEPTS)
          .orderBy(rightColumn.colSelector,
              nullsComparator(rightColumn.isNullsFirst, !rightColumn.isAscending));
  Enumerable<List<Integer>> joinResult = joinAlgorithm.join(left, right);

  List<Integer> actualIdOrderLeft = joinResult.select(joinTuple -> joinTuple.get(0)).toList();
  List<Integer> expectedIdOrderLeft = left.select(e -> e.eid).toList();
  assertLeftInput.check(expectedIdOrderLeft, actualIdOrderLeft, leftColumn.isNullsFirst);
  List<Integer> actualIdOrderRight = joinResult.select(joinTuple -> joinTuple.get(1)).toList();
  List<Integer> expectedIdOrderRight = right.select(d -> d.did).toList();
  assertRightInput.check(expectedIdOrderRight, actualIdOrderRight, rightColumn.isNullsFirst);
}
 
Example #2
Source File: SemiJoinOp.java    From herddb with Apache License 2.0 6 votes vote down vote up
@Override
public StatementExecutionResult execute(
        TableSpaceManager tableSpaceManager,
        TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite
) throws StatementExecutionException {
    ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    transactionContext = new TransactionContext(resLeft.transactionId);
    ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    final long resTransactionId = resRight.transactionId;
    Enumerable<DataAccessor> result = EnumerableDefaults.semiJoin(
            resLeft.dataScanner.createRewindOnCloseEnumerable(),
            resRight.dataScanner.createRewindOnCloseEnumerable(),
            JoinKey.keyExtractor(leftKeys),
            JoinKey.keyExtractor(rightKeys)
    );
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(resRight.dataScanner.getTransaction(), fieldNames, columns, result, resLeft.dataScanner, resRight.dataScanner);
    return new ScanResult(resTransactionId, joinedScanner);
}
 
Example #3
Source File: TableScanNode.java    From calcite with Apache License 2.0 6 votes vote down vote up
private static TableScanNode createFilterable(Compiler compiler,
    TableScan rel, ImmutableList<RexNode> filters, ImmutableIntList projects,
    FilterableTable filterableTable) {
  final DataContext root = compiler.getDataContext();
  final List<RexNode> mutableFilters = Lists.newArrayList(filters);
  final Enumerable<Object[]> enumerable =
      filterableTable.scan(root, mutableFilters);
  for (RexNode filter : mutableFilters) {
    if (!filters.contains(filter)) {
      throw RESOURCE.filterableTableInventedFilter(filter.toString()).ex();
    }
  }
  final Enumerable<Row> rowEnumerable = Enumerables.toRow(enumerable);
  return createEnumerable(compiler, rel, rowEnumerable, null,
      mutableFilters, projects);
}
 
Example #4
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 #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: MycatReflectiveSchema.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
/** Returns a table based on a particular field of this schema. If the
 * field is not of the right type to be a relation, returns null. */
private <T> Table fieldRelation(final Field field) {
  final Type elementType = getElementType(field.getType());
  if (elementType == null) {
    return null;
  }

  return new FieldTable(field, elementType, (Supplier<Enumerable<? extends Object>>) () -> {
      Object o;
      try {
          o = field.get(target);
      } catch (IllegalAccessException e) {
          throw new RuntimeException(
                  "Error while accessing field " + field, e);
      }
      @SuppressWarnings("unchecked")
      final Enumerable<T> enumerable = toEnumerable(o);
      return enumerable;
  });
}
 
Example #7
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 #8
Source File: QuarkTable.java    From quark with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an enumerable over a given projection of the fields.
 *
 * Called from generated code.
 */
public Enumerable<Object> project(final int[] fields) {
  return new AbstractEnumerable<Object>() {
    public org.apache.calcite.linq4j.Enumerator enumerator() {
      return new QuarkEnumerator();
    }
  };
}
 
Example #9
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 #10
Source File: ProvenanceTable.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an enumerable over a given projection of the fields.
 *
 * <p>
 * Called from generated code.
 */
public Enumerable<Object> project(final int[] fields) {
    return new AbstractEnumerable<Object>() {
        @Override
        public Enumerator<Object> enumerator() {
            final ProvenanceEnumerator provenanceEnumerator = new ProvenanceEnumerator(context, logger, fields) {
                @Override
                protected void onFinish() {
                    final int recordCount = getRecordsRead();
                    if (recordCount > maxRecordsRead) {
                        maxRecordsRead = recordCount;
                    }
                }

                @Override
                public void close() {
                    synchronized (enumerators) {
                        enumerators.remove(this);
                    }
                    super.close();
                }
            };

            synchronized (enumerators) {
                enumerators.add(provenanceEnumerator);
            }

            return provenanceEnumerator;
        }
    };
}
 
Example #11
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TKey, TElement, TResult> Queryable<TResult> groupBy(
    Queryable<T> source,
    FunctionExpression<Function1<T, TKey>> keySelector,
    FunctionExpression<Function1<T, TElement>> elementSelector,
    FunctionExpression<Function2<TKey, Enumerable<TElement>, TResult>>
        resultSelector,
    EqualityComparer<TKey> comparer) {
  throw new UnsupportedOperationException();
}
 
Example #12
Source File: CloneSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static <T> Table createCloneTable(final JavaTypeFactory typeFactory,
    final RelProtoDataType protoRowType, final List<RelCollation> collations,
    final List<ColumnMetaData.Rep> repList, final Enumerable<T> source) {
  final Type elementType;
  if (source instanceof QueryableTable) {
    elementType = ((QueryableTable) source).getElementType();
  } else if (protoRowType.apply(typeFactory).getFieldCount() == 1) {
    if (repList != null) {
      elementType = repList.get(0).clazz;
    } else {
      elementType = Object.class;
    }
  } else {
    elementType = Object[].class;
  }
  return new ArrayTable(
      elementType,
      protoRowType,
      Suppliers.memoize(() -> {
        final ColumnLoader loader =
            new ColumnLoader<>(typeFactory, source, protoRowType,
                repList);
        final List<RelCollation> collation2 =
            collations.isEmpty()
                && loader.sortField >= 0
                ? RelCollations.createSingleton(loader.sortField)
                : collations;
        return new ArrayTable.Content(loader.representationValues,
            loader.size(), collation2);
      }));
}
 
Example #13
Source File: FilterableTable.java    From kareldb with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters) {
    CollectionWrapper rows = (CollectionWrapper) getModifiableCollection();
    for (RexNode filter : filters) {
        rows = scanFilterForKeyFields(root, filter, rows);
    }
    final Collection coll = rows;
    final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get(root);
    return new AbstractEnumerable<Object[]>() {
        @SuppressWarnings("unchecked")
        public Enumerator<Object[]> enumerator() {
            return new TableEnumerator<>(Iterators.<Object, Object[]>transform(
                coll.iterator(), Table::toArray), cancelFlag, true);
        }
    };
}
 
Example #14
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 #15
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TInner, TKey, TResult> Queryable<TResult> groupJoin(
    Queryable<T> source,
    Enumerable<TInner> inner,
    FunctionExpression<Function1<T, TKey>> outerKeySelector,
    FunctionExpression<Function1<TInner, TKey>> innerKeySelector,
    FunctionExpression<Function2<T, Enumerable<TInner>, TResult>>
        resultSelector,
    EqualityComparer<TKey> comparer) {
  throw new UnsupportedOperationException();
}
 
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: GremlinTraversalScan.java    From sql-gremlin with Apache License 2.0 5 votes vote down vote up
public static Enumerable<Object[]> scan() {
    return new AbstractEnumerable<Object[]>() {
        public Enumerator<Object[]> enumerator() {
            return new GremlinEnumerator(rows);
        }
    };
}
 
Example #18
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 #19
Source File: EnumerableBindable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> bind(DataContext dataContext) {
  final ImmutableMap<String, Object> map = ImmutableMap.of();
  final Bindable bindable = EnumerableInterpretable.toBindable(map, null,
      (EnumerableRel) getInput(), EnumerableRel.Prefer.ARRAY);
  final ArrayBindable arrayBindable = EnumerableInterpretable.box(bindable);
  return arrayBindable.bind(dataContext);
}
 
Example #20
Source File: SnapshotThreadStacksTable.java    From mat-calcite-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Enumerable<Object[]> scan(DataContext dataContext) {
    return new AbstractEnumerable<Object[]>() {
        @Override
        public Enumerator<Object[]> enumerator() {
            return new StackFramesEnumerator(snapshot, getThreadStacks());
        }
    };
}
 
Example #21
Source File: ArrayTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      final Content content = supplier.get();
      return content.arrayEnumerator();
    }
  };
}
 
Example #22
Source File: CsvTranslatableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns an enumerable over a given projection of the fields.
 *
 * <p>Called from generated code. */
public Enumerable<Object> project(final DataContext root,
    final int[] fields) {
  final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get(root);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      return new CsvEnumerator<>(
          source,
          cancelFlag,
          getFieldTypes(root.getTypeFactory()),
          fields);
    }
  };
}
 
Example #23
Source File: JsonScannableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new JsonEnumerator(getDataList(root.getTypeFactory()));
    }
  };
}
 
Example #24
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testLastOrDefault() {
  final Enumerable<String> enumerable = Linq4j.asEnumerable(Arrays.asList("jimi", "mitch"));
  assertEquals("mitch", enumerable.lastOrDefault());

  final Enumerable<?> emptyEnumerable = Linq4j.asEnumerable(Collections.EMPTY_LIST);
  assertNull(emptyEnumerable.lastOrDefault());
}
 
Example #25
Source File: QueryableRelBuilder.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <TCollection, TResult> Queryable<TResult> selectMany(
    Queryable<T> source,
    FunctionExpression<Function2<T, Integer, Enumerable<TCollection>>>
        collectionSelector,
    FunctionExpression<Function2<T, TCollection, TResult>> resultSelector) {
  throw new UnsupportedOperationException();
}
 
Example #26
Source File: TpcdsSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    final SchemaPlus schema, final String tableName) {
  //noinspection unchecked
  return (Queryable) new AbstractTableQueryable<Object[]>(queryProvider,
      schema, this, tableName) {
    public Enumerator<Object[]> enumerator() {
      final Session session =
          Session.getDefaultSession()
              .withTable(tpcdsTable)
              .withScale(scaleFactor);
      final Results results = Results.constructResults(tpcdsTable, session);
      return Linq4j.asEnumerable(results)
          .selectMany(
              new Function1<List<List<String>>, Enumerable<Object[]>>() {
                final Column[] columns = tpcdsTable.getColumns();

                public Enumerable<Object[]> apply(
                    List<List<String>> inRows) {
                  final List<Object[]> rows = new ArrayList<>();
                  for (List<String> strings : inRows) {
                    final Object[] values = new Object[columns.length];
                    for (int i = 0; i < strings.size(); i++) {
                      values[i] = convert(strings.get(i), columns[i]);
                    }
                    rows.add(values);
                  }
                  return Linq4j.asEnumerable(rows);
                }

              })
          .enumerator();
    }
  };
}
 
Example #27
Source File: ScannableTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return tens();
    }
  };
}
 
Example #28
Source File: GeodeSimpleScannableTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override public Enumerable<Object[]> scan(DataContext root) {
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return new GeodeSimpleEnumerator<Object[]>(clientCache, regionName) {
        @Override public Object[] convert(Object obj) {
          Object values = convertToRowValues(relDataType.getFieldList(), obj);
          if (values instanceof Object[]) {
            return (Object[]) values;
          }
          return new Object[]{values};
        }
      };
    }
  };
}
 
Example #29
Source File: CassandraTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Called via code-generation.
 *
 * @see org.apache.calcite.adapter.cassandra.CassandraMethod#CASSANDRA_QUERYABLE_QUERY
 */
@SuppressWarnings("UnusedDeclaration")
public Enumerable<Object> query(List<Map.Entry<String, Class>> fields,
    List<Map.Entry<String, String>> selectFields, List<String> predicates,
    List<String> order, Integer offset, Integer fetch) {
  return getTable().query(getSession(), fields, selectFields, predicates,
      order, offset, fetch);
}
 
Example #30
Source File: FileTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Returns an enumerable over a given projection of the fields. */
public Enumerable<Object> project(final int[] fields) {
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      try {
        return new FileEnumerator(reader.iterator(), converter, fields);
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
  };
}