Java Code Examples for org.apache.calcite.linq4j.Enumerator

The following examples show how to use org.apache.calcite.linq4j.Enumerator. These examples are extracted from open source projects. 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 Project: Mycat2   Source File: MycatPhysicalTable.java    License: 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 2
@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 3
@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 Project: calcite   Source File: MazeTable.java    License: 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 5
Source Project: calcite   Source File: RangeTable.java    License: 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 6
Source Project: calcite   Source File: ListTable.java    License: 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 7
Source Project: calcite   Source File: Linq4jTest.java    License: 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 8
Source Project: calcite   Source File: UtilTest.java    License: 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 9
Source Project: calcite   Source File: Linq4jTest.java    License: 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 10
Source Project: calcite   Source File: Linq4jTest.java    License: 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 11
Source Project: calcite   Source File: Linq4jTest.java    License: 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 12
Source Project: calcite   Source File: Linq4jTest.java    License: 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 13
Source Project: calcite   Source File: CorrelateJoinTest.java    License: 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 14
Source Project: calcite   Source File: MongoTable.java    License: 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 15
Source Project: marble   Source File: DataTable.java    License: Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new DataTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    public Enumerator<T> enumerator() {
      //noinspection unchecked
      return (Enumerator<T>) Linq4j.enumerator(rows);
    }

    public List getRows() {
      return rows;
    }
  };
}
 
Example 16
Source Project: nifi   Source File: FlowFileTable.java    License: 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
        @SuppressWarnings({"unchecked", "rawtypes"})
        public Enumerator<Object> enumerator() {
            final FlowFileEnumerator flowFileEnumerator = new FlowFileEnumerator(session, flowFile, logger, recordReaderFactory, 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(flowFileEnumerator);
            }

            return flowFileEnumerator;
        }
    };
}
 
Example 17
Source Project: nifi   Source File: JvmMetricsTable.java    License: 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
        @SuppressWarnings({"unchecked", "rawtypes"})
        public Enumerator<Object> enumerator() {
            final JvmMetricsEnumerator jvmMetricsEnumerator = new JvmMetricsEnumerator(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(jvmMetricsEnumerator);
            }

            return jvmMetricsEnumerator;
        }
    };
}
 
Example 18
Source Project: kafka-eagle   Source File: JSqlTable.java    License: Apache License 2.0 5 votes vote down vote up
public Enumerable<Object[]> scan(DataContext root) {
	final List<String> types = new ArrayList<String>(sourceTable.columns.size());
	for (JSqlMapData.Column column : sourceTable.columns) {
		types.add(column.type);
	}
	final int[] fields = identityList(this.dataType.getFieldCount());
	return new AbstractEnumerable<Object[]>() {
		public Enumerator<Object[]> enumerator() {
			return new JSqlEnumerator<Object[]>(fields, types, sourceTable.data);
		}
	};
}
 
Example 19
Source Project: calcite   Source File: ScannableTableTest.java    License: Apache License 2.0 5 votes vote down vote up
private static Enumerator<Object[]> tens() {
  return new Enumerator<Object[]>() {
    int row = -1;
    Object[] current;

    public Object[] current() {
      return current;
    }

    public boolean moveNext() {
      if (++row < 4) {
        current = new Object[] {row * 10};
        return true;
      } else {
        return false;
      }
    }

    public void reset() {
      row = -1;
    }

    public void close() {
      current = null;
    }
  };
}
 
Example 20
Source Project: nifi   Source File: ConnectionStatusPredictionsTable.java    License: 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
        @SuppressWarnings({"unchecked", "rawtypes"})
        public Enumerator<Object> enumerator() {
            final ConnectionStatusPredictionsEnumerator connectionStatusPredictionsEnumerator = new ConnectionStatusPredictionsEnumerator(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(connectionStatusPredictionsEnumerator);
            }

            return connectionStatusPredictionsEnumerator;
        }
    };
}
 
Example 21
Source Project: calcite   Source File: ArrayTable.java    License: 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 Project: herddb   Source File: DataScanner.java    License: Apache License 2.0 5 votes vote down vote up
private Enumerable<DataAccessor> createEnumerable(boolean rewindOnClose) {
    return new AbstractEnumerable<DataAccessor>() {
        @Override
        public Enumerator<DataAccessor> enumerator() {
            return asEnumerator(rewindOnClose);
        }
    };
}
 
Example 23
Source Project: sql-gremlin   Source File: GremlinTraversalScan.java    License: 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 24
Source Project: quark   Source File: QuarkResultSet.java    License: 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 25
Source Project: quark   Source File: QuarkViewTable.java    License: Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
                                    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      return null;
    }
  };
}
 
Example 26
Source Project: quark   Source File: QuarkViewTable.java    License: 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 null;
    }
  };
}
 
Example 27
Source Project: calcite   Source File: CollectionTypeTest.java    License: 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 nestedRecordsEnumerator();
    }
  };
}
 
Example 28
Source Project: calcite   Source File: ScannableTableTest.java    License: 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 29
Source Project: calcite   Source File: ArrayTable.java    License: Apache License 2.0 5 votes vote down vote up
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider,
    SchemaPlus schema, String tableName) {
  return new AbstractTableQueryable<T>(queryProvider, schema, this,
      tableName) {
    @SuppressWarnings("unchecked")
    public Enumerator<T> enumerator() {
      final Content content = supplier.get();
      return content.enumerator();
    }
  };
}
 
Example 30
Source Project: mat-calcite-plugin   Source File: TableFunctions.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> Queryable<T> asQueryable(QueryProvider queryProvider, SchemaPlus schemaPlus, String s) {
    BaseQueryable<RowType> queryable = new BaseQueryable<RowType>(null, getElementType(), null) {
        @Override
        public Enumerator<RowType> enumerator() {
            return Linq4j.enumerator(values);
        }
    };
    return (Queryable<T>) queryable;
}