Search in sources :

Example 11 with SelectStatement

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);
}
Also used : ScheduledExecutors(org.apache.cassandra.concurrent.ScheduledExecutors) org.apache.cassandra.cql3.statements(org.apache.cassandra.cql3.statements) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) AbstractType(org.apache.cassandra.db.marshal.AbstractType) org.apache.cassandra.utils(org.apache.cassandra.utils) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) ByteBuffer(java.nio.ByteBuffer) Gossiper(org.apache.cassandra.gms.Gossiper) Function(org.apache.cassandra.cql3.functions.Function) ClientRequestMetrics(org.apache.cassandra.metrics.ClientRequestMetrics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ENABLE_NODELOCAL_QUERIES(org.apache.cassandra.config.CassandraRelevantProperties.ENABLE_NODELOCAL_QUERIES) FunctionName(org.apache.cassandra.cql3.functions.FunctionName) RowIterator(org.apache.cassandra.db.rows.RowIterator) PartitionIterators(org.apache.cassandra.db.partitions.PartitionIterators) com.google.common.collect(com.google.common.collect) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) RequestValidations.checkTrue(org.apache.cassandra.cql3.statements.RequestValidations.checkTrue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) QueryPager(org.apache.cassandra.service.pager.QueryPager) Predicate(com.google.common.base.Predicate) org.apache.cassandra.exceptions(org.apache.cassandra.exceptions) Future(org.apache.cassandra.utils.concurrent.Future) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) org.antlr.runtime(org.antlr.runtime) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) org.apache.cassandra.service(org.apache.cassandra.service) InetAddressAndPort(org.apache.cassandra.locator.InetAddressAndPort) java.util(java.util) CQLMetrics(org.apache.cassandra.metrics.CQLMetrics) Message(org.apache.cassandra.net.Message) Cache(com.github.benmanes.caffeine.cache.Cache) ConcurrentMap(java.util.concurrent.ConcurrentMap) Schema(org.apache.cassandra.schema.Schema) SchemaChangeListener(org.apache.cassandra.schema.SchemaChangeListener) ResultSetBuilder(org.apache.cassandra.cql3.selection.ResultSetBuilder) ClientRequestsMetricsHolder(org.apache.cassandra.metrics.ClientRequestsMetricsHolder) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) MessagingService(org.apache.cassandra.net.MessagingService) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) FutureCombiner(org.apache.cassandra.utils.concurrent.FutureCombiner) Logger(org.slf4j.Logger) Tracing(org.apache.cassandra.tracing.Tracing) Ints(com.google.common.primitives.Ints) TimeUnit(java.util.concurrent.TimeUnit) UnfilteredPartitionIterators(org.apache.cassandra.db.partitions.UnfilteredPartitionIterators) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ImmediateExecutor(org.apache.cassandra.concurrent.ImmediateExecutor) ResultSetBuilder(org.apache.cassandra.cql3.selection.ResultSetBuilder) Message(org.apache.cassandra.net.Message) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) PartitionIterator(org.apache.cassandra.db.partitions.PartitionIterator) RowIterator(org.apache.cassandra.db.rows.RowIterator)

Example 12 with SelectStatement

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);
}
Also used : SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) SimpleStatement(com.datastax.driver.core.SimpleStatement) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ModificationStatement(org.apache.cassandra.cql3.statements.ModificationStatement) Statement(com.datastax.driver.core.Statement) PreparedStatement(com.datastax.driver.core.PreparedStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

SelectStatement (org.apache.cassandra.cql3.statements.SelectStatement)9 QueryOptions (org.apache.cassandra.cql3.QueryOptions)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)3 ReadQuery (org.apache.cassandra.db.ReadQuery)3 ClientState (org.apache.cassandra.service.ClientState)3 QueryState (org.apache.cassandra.service.QueryState)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 Session (com.datastax.driver.core.Session)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1 Statement (com.datastax.driver.core.Statement)1 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Predicate (com.google.common.base.Predicate)1 com.google.common.collect (com.google.common.collect)1 Ints (com.google.common.primitives.Ints)1 ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1