org.apache.calcite.linq4j.Enumerator Java Examples

The following examples show how to use org.apache.calcite.linq4j.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: ListTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractQueryable<T>() {
    public Type getElementType() {
      return elementType;
    }

    public Expression getExpression() {
      return expression;
    }

    public QueryProvider getProvider() {
      return queryProvider;
    }

    public Iterator<T> iterator() {
      //noinspection unchecked
      return list.iterator();
    }

    public Enumerator<T> enumerator() {
      //noinspection unchecked
      return Linq4j.enumerator(list);
    }
  };
}
 
Example #2
Source File: UtilTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testFlatListProduct() {
  final List<Enumerator<List<String>>> list = new ArrayList<>();
  list.add(Linq4j.enumerator(l2(l1("a"), l1("b"))));
  list.add(Linq4j.enumerator(l3(l2("x", "p"), l2("y", "q"), l2("z", "r"))));
  final Enumerable<FlatLists.ComparableList<String>> product =
      SqlFunctions.product(list, 3, false);
  int n = 0;
  FlatLists.ComparableList<String> previous = FlatLists.of();
  for (FlatLists.ComparableList<String> strings : product) {
    if (n++ == 1) {
      assertThat(strings.size(), is(3));
      assertThat(strings.get(0), is("a"));
      assertThat(strings.get(1), is("y"));
      assertThat(strings.get(2), is("q"));
    }
    if (previous != null) {
      assertTrue(previous.compareTo(strings) < 0);
    }
    previous = strings;
  }
  assertThat(n, is(6));
}
 
Example #3
Source File: OutboundReferencesTable.java    From mat-calcite-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public Queryable<Object[]> asQueryable(QueryProvider queryProvider, SchemaPlus schemaPlus, String tableName) {
    return new AbstractTableQueryable<Object[]>(queryProvider, schemaPlus, this, tableName) {
        @Override
        public Enumerator<Object[]> enumerator() {
            FluentIterable<Object[]> it = FluentIterable
                    .from(references)
                    .transform(new Function<NamedReference, Object[]>() {
                        @Nullable
                        @Override
                        public Object[] apply(@Nullable NamedReference namedReference) {
                            HeapReference ref = null;
                            try {
                                ref = HeapReference.valueOf(namedReference.getObject());
                            } catch (SnapshotException e) {
                                e.printStackTrace();
                            }
                            return new Object[]{namedReference.getName(), ref};
                        }
                    });

            return Linq4j.iterableEnumerator(it);
        }
    };
}
 
Example #4
Source File: ReflectivePredicateEvaluator.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters, int[] projects) {
	List<Object> list = REGISTRY.getIfPresent(this.operatorIdentifier).objects;

	final int[] actualProjects = resolveProjects(projects);

	Enumerator<Object[]> enumerator =  Linq4j.enumerator(list.stream()
			.filter(o -> referenceInterface.isAssignableFrom(o.getClass()))
			.map(
			m -> {
				Object[] res = new Object[actualProjects.length];
				for (int i = 0; i < actualProjects.length; i++) {
					res[i] = methodsForFields.get(actualProjects[i]).apply(m);
				}
				return res;
			}
	).collect(Collectors.toList()));

	return new AbstractEnumerable<Object[]>() {
		@Override
		public Enumerator<Object[]> enumerator() {
			return enumerator;
		}
	};
}
 
Example #5
Source File: MazeTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
  final Random random = seed >= 0 ? new Random(seed) : new Random();
  final Maze maze = new Maze(width, height);
  final PrintWriter pw = Util.printWriter(System.out);
  maze.layout(random, pw);
  if (Maze.DEBUG) {
    maze.print(pw, true);
  }
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      final Set<Integer> solutionSet;
      if (solution) {
        solutionSet = maze.solve(0, 0);
      } else {
        solutionSet = null;
      }
      return Linq4j.transform(maze.enumerator(solutionSet),
          s -> new Object[] {s});
    }
  };
}
 
Example #6
Source File: Linq4jTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
@Test void testTransformEnumerator() {
  final List<String> strings = Arrays.asList("one", "two", "three");
  final Function1<String, Integer> func = String::length;
  final Enumerator<Integer> enumerator =
      Linq4j.transform(Linq4j.enumerator(strings), func);
  assertThat(enumerator.moveNext(), is(true));
  assertThat(enumerator.current(), is(3));
  assertThat(enumerator.moveNext(), is(true));
  assertThat(enumerator.current(), is(3));
  assertThat(enumerator.moveNext(), is(true));
  assertThat(enumerator.current(), is(5));
  assertThat(enumerator.moveNext(), is(false));

  final Enumerator<Integer> enumerator2 =
      Linq4j.transform(Linq4j.emptyEnumerator(), func);
  assertThat(enumerator2.moveNext(), is(false));
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: MongoTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes an "aggregate" operation on the underlying collection.
 *
 * <p>For example:
 * <code>zipsTable.aggregate(
 * "{$filter: {state: 'OR'}",
 * "{$group: {_id: '$city', c: {$sum: 1}, p: {$sum: '$pop'}}}")
 * </code></p>
 *
 * @param mongoDb MongoDB connection
 * @param fields List of fields to project; or null to return map
 * @param operations One or more JSON strings
 * @return Enumerator of results
 */
private Enumerable<Object> aggregate(final MongoDatabase mongoDb,
    final List<Map.Entry<String, Class>> fields,
    final List<String> operations) {
  final List<Bson> list = new ArrayList<>();
  for (String operation : operations) {
    list.add(BsonDocument.parse(operation));
  }
  final Function1<Document, Object> getter =
      MongoEnumerator.getter(fields);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      final Iterator<Document> resultIterator;
      try {
        resultIterator = mongoDb.getCollection(collectionName)
            .aggregate(list).iterator();
      } catch (Exception e) {
        throw new RuntimeException("While running MongoDB query "
            + Util.toString(operations, "[", ",\n", "]"), e);
      }
      return new MongoEnumerator(resultIterator, getter);
    }
  };
}
 
Example #13
Source File: MycatPhysicalTable.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters, int[] projects) {
    String backendTaskSQL = CalciteUtls.getBackendTaskSQL(filters,
            logicTable().getColumns(),
            CalciteUtls.getColumnList(logicTable(), projects), backendTableInfo);

    MycatCalciteDataContext root1 = (MycatCalciteDataContext) root;
    MycatConnection connection = root1.getUponDBContext().getConnection(backendTableInfo.getTargetName());
    RowBaseIterator rowBaseIterator = connection.executeQuery(null, backendTaskSQL);
    return new AbstractEnumerable<Object[]>() {
        @Override
        @SneakyThrows
        public Enumerator<Object[]> enumerator() {
            return new MyCatResultSetEnumerator(root1.getCancelFlag(), rowBaseIterator);
        }
    };

}
 
Example #14
Source File: RangeTable.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Enumerator<Integer> enumerator() {
  return new Enumerator<Integer>() {
    int current = start - 1;

    public Integer current() {
      if (current >= end) {
        throw new NoSuchElementException();
      }
      return current;
    }

    public boolean moveNext() {
      ++current;
      return current < end;
    }

    public void reset() {
      current = start - 1;
    }

    public void close() {
    }
  };
}
 
Example #15
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCast() {
  final List<Number> numbers = Arrays.asList((Number) 2, null, 3.14, 5);
  final Enumerator<Integer> enumerator =
      Linq4j.asEnumerable(numbers)
          .cast(Integer.class)
          .enumerator();
  checkCast(enumerator);
}
 
Example #16
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 #17
Source File: CalciteResultSet.java    From calcite with Apache License 2.0 5 votes vote down vote up
private Cursor createCursor(ColumnMetaData.AvaticaType elementType,
    Iterable iterable) {
  final Enumerator enumerator = Linq4j.iterableEnumerator(iterable);
  //noinspection unchecked
  return !(elementType instanceof ColumnMetaData.StructType)
      || ((ColumnMetaData.StructType) elementType).columns.size() == 1
      ? new ObjectEnumeratorCursor(enumerator)
      : new ArrayEnumeratorCursor(enumerator);
}
 
Example #18
Source File: MongoTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a "find" operation on the underlying collection.
 *
 * <p>For example,
 * <code>zipsTable.find("{state: 'OR'}", "{city: 1, zipcode: 1}")</code></p>
 *
 * @param mongoDb MongoDB connection
 * @param filterJson Filter JSON string, or null
 * @param projectJson Project JSON string, or null
 * @param fields List of fields to project; or null to return map
 * @return Enumerator of results
 */
private Enumerable<Object> find(MongoDatabase mongoDb, String filterJson,
    String projectJson, List<Map.Entry<String, Class>> fields) {
  final MongoCollection collection =
      mongoDb.getCollection(collectionName);
  final Bson filter =
      filterJson == null ? null : BsonDocument.parse(filterJson);
  final Bson project =
      projectJson == null ? null : BsonDocument.parse(projectJson);
  final Function1<Document, Object> getter = MongoEnumerator.getter(fields);
  return new AbstractEnumerable<Object>() {
    public Enumerator<Object> enumerator() {
      @SuppressWarnings("unchecked") final FindIterable<Document> cursor =
          collection.find(filter).projection(project);
      return new MongoEnumerator(cursor.iterator(), getter);
    }
  };
}
 
Example #19
Source File: MetadataSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerator<MetaTable> enumerator(CalciteMetaImpl meta) {
  final String catalog;
  try {
    catalog = meta.getConnection().getCatalog();
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
  return meta.tables(catalog).enumerator();
}
 
Example #20
Source File: MetadataSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerator<MetaColumn> enumerator(
    final CalciteMetaImpl meta) {
  final String catalog;
  try {
    catalog = meta.getConnection().getCatalog();
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
  return meta.tables(catalog)
      .selectMany(meta::columns).enumerator();
}
 
Example #21
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      return (Enumerator<T>) MetadataTable.this.enumerator(
          ((CalciteConnectionImpl) queryProvider).meta());
    }
  };
}
 
Example #22
Source File: CalciteConnectionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Enumerator<T> executeQuery(Queryable<T> queryable) {
  try {
    CalciteStatement statement = (CalciteStatement) createStatement();
    CalcitePrepare.CalciteSignature<T> signature =
        statement.prepare(queryable);
    return enumerable(statement.handle, signature).enumerator();
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #23
Source File: Linq4jTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testIterableOfType() {
  final List<Number> numbers = Arrays.asList((Number) 2, null, 3.14, 5);
  final Enumerator<Integer> enumerator =
      Linq4j.ofType(numbers, Integer.class)
          .enumerator();
  checkIterable(enumerator);
}
 
Example #24
Source File: MutableArrayTable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    public Enumerator<T> enumerator() {
      //noinspection unchecked
      return (Enumerator<T>) Linq4j.enumerator(rows);
    }
  };
}
 
Example #25
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
ProductComparableListEnumerator(List<Enumerator<List<E>>> enumerators,
    int fieldCount, boolean withOrdinality) {
  super(enumerators);
  this.withOrdinality = withOrdinality;
  flatElements = (E[]) new Comparable[fieldCount];
  list = Arrays.asList(flatElements);
}
 
Example #26
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 #27
Source File: ResultSetEnumerable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerator<T> enumerator() {
  if (preparedStatementEnricher == null) {
    return enumeratorBasedOnStatement();
  } else {
    return enumeratorBasedOnPreparedStatement();
  }
}
 
Example #28
Source File: RecordEnumeratorCursor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a RecordEnumeratorCursor.
 *
 * @param enumerator Enumerator
 * @param clazz Element type
 */
public RecordEnumeratorCursor(
    Enumerator<E> enumerator,
    Class<E> clazz) {
  super(enumerator);
  this.clazz = clazz;
}
 
Example #29
Source File: EnumUtils.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Note that it only works for batch scenario. E.g. all data is known and there is no late data.
 *
 * @param inputEnumerator the enumerator to provide an array of objects as input
 * @param indexOfWatermarkedColumn the index of timestamp column upon which a watermark is built
 * @param slide sliding size
 * @param intervalSize window size
 */
HopEnumerator(Enumerator<Object[]> inputEnumerator,
    int indexOfWatermarkedColumn, long slide, long intervalSize) {
  this.inputEnumerator = inputEnumerator;
  this.indexOfWatermarkedColumn = indexOfWatermarkedColumn;
  this.emitFrequency = slide;
  this.intervalSize = intervalSize;
  list = new LinkedList<>();
}
 
Example #30
Source File: ScannableTableTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root, List<RexNode> filters) {
  final Pair<Integer, Object> filter = getFilter(cooperative, filters);
  return new AbstractEnumerable<Object[]>() {
    public Enumerator<Object[]> enumerator() {
      return beatles(buf, filter, null);
    }
  };
}