use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.
the class QueryProcessor method executeAsync.
public static Future<UntypedResultSet> executeAsync(InetAddressAndPort address, String query, Object... values) {
Prepared prepared = prepareInternal(query);
QueryOptions options = makeInternalOptions(prepared.statement, values);
if (prepared.statement instanceof SelectStatement) {
SelectStatement select = (SelectStatement) prepared.statement;
int nowInSec = FBUtilities.nowInSeconds();
ReadQuery readQuery = select.getQuery(options, nowInSec);
List<ReadCommand> commands;
if (readQuery instanceof ReadCommand) {
commands = Collections.singletonList((ReadCommand) readQuery);
} else if (readQuery instanceof SinglePartitionReadQuery.Group) {
List<? extends SinglePartitionReadQuery> queries = ((SinglePartitionReadQuery.Group<? extends SinglePartitionReadQuery>) readQuery).queries;
queries.forEach(a -> {
if (!(a instanceof ReadCommand))
throw new IllegalArgumentException("Queries found which are not ReadCommand: " + a.getClass());
});
commands = (List<ReadCommand>) (List<?>) queries;
} else {
throw new IllegalArgumentException("Unable to handle; only expected ReadCommands but given " + readQuery.getClass());
}
Future<List<Message<ReadResponse>>> future = FutureCombiner.allOf(commands.stream().map(rc -> Message.out(rc.verb(), rc)).map(m -> MessagingService.instance().<ReadResponse>sendWithResult(m, address)).collect(Collectors.toList()));
ResultSetBuilder result = new ResultSetBuilder(select.getResultMetadata(), select.getSelection().newSelectors(options), null);
return future.map(list -> {
int i = 0;
for (Message<ReadResponse> m : list) {
ReadResponse rsp = m.payload;
try (PartitionIterator it = UnfilteredPartitionIterators.filter(rsp.makeIterator(commands.get(i++)), nowInSec)) {
while (it.hasNext()) {
try (RowIterator partition = it.next()) {
select.processPartition(partition, options, result, nowInSec);
}
}
}
}
return result.build();
}).map(UntypedResultSet::create);
}
throw new IllegalArgumentException("Unable to execute query; only SELECT supported but given: " + query);
}
use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.
the class QueryEventsTest method prepareExecuteTest.
@Test
public void prepareExecuteTest() {
createTable("create table %s (id int primary key, v int)");
MockListener listener = new MockListener(getCurrentColumnFamilyStore());
QueryEvents.instance.registerListener(listener);
Session session = sessionNet();
String query = formatQuery("select * from %s where id = 1");
PreparedStatement ps = session.prepare(query);
listener.verify("prepareSuccess", 1);
assertEquals(query, listener.query);
assertTrue(listener.statement instanceof SelectStatement);
Statement s = ps.bind();
session.execute(s);
listener.verify(newArrayList("prepareSuccess", "executeSuccess"), newArrayList(1, 1));
QueryProcessor.clearPreparedStatements(false);
s = ps.bind();
// this re-prepares the query!!
session.execute(s);
listener.verify(newArrayList("prepareSuccess", "executeSuccess", "executeFailure"), newArrayList(2, 2, 1));
query = formatQuery("select abcdef from %s where id = 1");
Exception expectedException = null;
try {
session.prepare(query);
fail("should fail");
} catch (Exception e) {
expectedException = e;
}
listener.verify(newArrayList("prepareSuccess", "prepareFailure", "executeSuccess", "executeFailure"), newArrayList(2, 1, 2, 1));
assertNotNull(listener.e);
assertNotNull(expectedException);
}
Aggregations