use of org.apache.calcite.linq4j.AbstractEnumerable in project calcite by apache.
the class DruidConnectionImpl method enumerable.
/**
* Executes a request and returns the resulting rows as an
* {@link Enumerable}, running the parser in a thread provided by
* {@code service}.
*/
public Enumerable<Row> enumerable(final QueryType queryType, final String request, final List<String> fieldNames, final ExecutorService service) throws IOException {
return new AbstractEnumerable<Row>() {
public Enumerator<Row> enumerator() {
final BlockingQueueEnumerator<Row> enumerator = new BlockingQueueEnumerator<>();
final RunnableQueueSink sink = new RunnableQueueSink() {
public void send(Row row) throws InterruptedException {
enumerator.queue.put(row);
}
public void end() {
enumerator.done.set(true);
}
@SuppressWarnings("deprecation")
public void setSourceEnumerable(Enumerable<Row> enumerable) throws InterruptedException {
for (Row row : enumerable) {
send(row);
}
end();
}
public void run() {
try {
final Page page = new Page();
final List<ColumnMetaData.Rep> fieldTypes = Collections.nCopies(fieldNames.size(), null);
request(queryType, request, this, fieldNames, fieldTypes, page);
enumerator.done.set(true);
} catch (Throwable e) {
enumerator.throwableHolder.set(e);
enumerator.done.set(true);
}
}
};
service.execute(sink);
return enumerator;
}
};
}
use of org.apache.calcite.linq4j.AbstractEnumerable in project calcite by apache.
the class ScannableTableTest method testPrepared2.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1031">[CALCITE-1031]
* In prepared statement, CsvScannableTable.scan is called twice</a>.
*/
@Test
public void testPrepared2() throws SQLException {
final Properties properties = new Properties();
properties.setProperty("caseSensitive", "true");
try (final Connection connection = DriverManager.getConnection("jdbc:calcite:", properties)) {
final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
final AtomicInteger scanCount = new AtomicInteger();
final AtomicInteger enumerateCount = new AtomicInteger();
final Schema schema = new AbstractSchema() {
@Override
protected Map<String, Table> getTableMap() {
return ImmutableMap.<String, Table>of("TENS", new SimpleTable() {
private Enumerable<Object[]> superScan(DataContext root) {
return super.scan(root);
}
@Override
public Enumerable<Object[]> scan(final DataContext root) {
scanCount.incrementAndGet();
return new AbstractEnumerable<Object[]>() {
public Enumerator<Object[]> enumerator() {
enumerateCount.incrementAndGet();
return superScan(root).enumerator();
}
};
}
});
}
};
calciteConnection.getRootSchema().add("TEST", schema);
final String sql = "select * from \"TEST\".\"TENS\" where \"i\" < ?";
final PreparedStatement statement = calciteConnection.prepareStatement(sql);
assertThat(scanCount.get(), is(0));
assertThat(enumerateCount.get(), is(0));
// First execute
statement.setInt(1, 20);
assertThat(scanCount.get(), is(0));
ResultSet resultSet = statement.executeQuery();
assertThat(scanCount.get(), is(1));
assertThat(enumerateCount.get(), is(1));
assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10"));
assertThat(scanCount.get(), is(1));
assertThat(enumerateCount.get(), is(1));
// Second execute
resultSet = statement.executeQuery();
assertThat(scanCount.get(), is(2));
assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10"));
assertThat(scanCount.get(), is(2));
// Third execute
statement.setInt(1, 30);
resultSet = statement.executeQuery();
assertThat(scanCount.get(), is(3));
assertThat(resultSet, Matchers.returnsUnordered("i=0", "i=10", "i=20"));
assertThat(scanCount.get(), is(3));
}
}
use of org.apache.calcite.linq4j.AbstractEnumerable in project calcite by apache.
the class MongoTable method find.
/**
* 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);
}
};
}
use of org.apache.calcite.linq4j.AbstractEnumerable in project calcite by apache.
the class Elasticsearch5Table method find.
@Override
protected Enumerable<Object> find(String index, List<String> ops, List<Map.Entry<String, Class>> fields) {
final String dbName = index;
final SearchSourceBuilder searchSourceBuilder;
if (ops.isEmpty()) {
searchSourceBuilder = new SearchSourceBuilder();
} else {
String queryString = "{" + Util.toString(ops, "", ", ", "") + "}";
NamedXContentRegistry xContentRegistry = NamedXContentRegistry.EMPTY;
XContent xContent = JsonXContent.jsonXContent;
try (XContentParser parser = xContent.createParser(xContentRegistry, queryString)) {
final QueryParseContext queryParseContext = new QueryParseContext(parser);
searchSourceBuilder = SearchSourceBuilder.fromXContent(queryParseContext);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
final Function1<SearchHit, Object> getter = Elasticsearch5Enumerator.getter(fields);
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
final Iterator<SearchHit> cursor = client.prepareSearch(dbName).setTypes(typeName).setSource(searchSourceBuilder).execute().actionGet().getHits().iterator();
return new Elasticsearch5Enumerator(cursor, getter);
}
};
}
use of org.apache.calcite.linq4j.AbstractEnumerable in project calcite by apache.
the class GeodeTable method query.
/**
* Executes an OQL query on the underlying table.
*
* <p>Called by the {@link GeodeQueryable} which in turn is
* called via the generated code.
*
* @param clientCache Geode client cache
* @param fields List of fields to project
* @param predicates A list of predicates which should be used in the query
* @return Enumerator of results
*/
public Enumerable<Object> query(final ClientCache clientCache, final List<Map.Entry<String, Class>> fields, final List<Map.Entry<String, String>> selectFields, final List<Map.Entry<String, String>> aggregateFunctions, final List<String> groupByFields, List<String> predicates, List<String> orderByFields, String limit) {
final RelDataTypeFactory typeFactory = new JavaTypeFactoryExtImpl();
final RelDataTypeFactory.Builder fieldInfo = typeFactory.builder();
for (Map.Entry<String, Class> field : fields) {
SqlTypeName typeName = typeFactory.createJavaType(field.getValue()).getSqlTypeName();
fieldInfo.add(field.getKey(), typeFactory.createSqlType(typeName)).nullable(true);
}
final RelProtoDataType resultRowType = RelDataTypeImpl.proto(fieldInfo.build());
ImmutableMap<String, String> aggFuncMap = ImmutableMap.of();
if (!aggregateFunctions.isEmpty()) {
ImmutableMap.Builder<String, String> aggFuncMapBuilder = ImmutableMap.builder();
for (Map.Entry<String, String> e : aggregateFunctions) {
aggFuncMapBuilder.put(e.getKey(), e.getValue());
}
aggFuncMap = aggFuncMapBuilder.build();
}
// Construct the list of fields to project
Builder<String> selectBuilder = ImmutableList.builder();
if (!groupByFields.isEmpty()) {
for (String groupByField : groupByFields) {
selectBuilder.add(groupByField + " AS " + groupByField);
}
if (!aggFuncMap.isEmpty()) {
for (Map.Entry<String, String> e : aggFuncMap.entrySet()) {
selectBuilder.add(e.getValue() + " AS " + e.getKey());
}
}
} else {
if (selectFields.isEmpty()) {
if (!aggFuncMap.isEmpty()) {
for (Map.Entry<String, String> e : aggFuncMap.entrySet()) {
selectBuilder.add(e.getValue() + " AS " + e.getKey());
}
} else {
selectBuilder.add("*");
}
} else {
for (Map.Entry<String, String> field : selectFields) {
selectBuilder.add(field.getKey() + " AS " + field.getValue());
}
}
}
final String oqlSelectStatement = Util.toString(selectBuilder.build(), " ", ", ", "");
// Combine all predicates conjunctively
String whereClause = "";
if (!predicates.isEmpty()) {
whereClause = " WHERE ";
whereClause += Util.toString(predicates, "", " AND ", "");
}
// Build and issue the query and return an Enumerator over the results
StringBuilder queryBuilder = new StringBuilder("SELECT ");
queryBuilder.append(oqlSelectStatement);
queryBuilder.append(" FROM /" + regionName);
queryBuilder.append(whereClause);
if (!groupByFields.isEmpty()) {
queryBuilder.append(Util.toString(groupByFields, " GROUP BY ", ", ", ""));
}
if (!orderByFields.isEmpty()) {
queryBuilder.append(Util.toString(orderByFields, " ORDER BY ", ", ", ""));
}
if (limit != null) {
queryBuilder.append(" LIMIT " + limit);
}
final String oqlQuery = queryBuilder.toString();
LOGGER.info("OQL: " + oqlQuery);
return new AbstractEnumerable<Object>() {
public Enumerator<Object> enumerator() {
SelectResults results = null;
QueryService queryService = clientCache.getQueryService();
try {
results = (SelectResults) queryService.newQuery(oqlQuery).execute();
} catch (Exception e) {
e.printStackTrace();
}
return new GeodeEnumerator(results, resultRowType);
}
};
}
Aggregations