Search in sources :

Example 1 with ResultSetBuilder

use of org.apache.cassandra.cql3.selection.ResultSetBuilder in project cassandra by apache.

the class SelectStatement method process.

private ResultSet process(PartitionIterator partitions, QueryOptions options, Selectors selectors, int nowInSec, int userLimit) throws InvalidRequestException {
    GroupMaker groupMaker = aggregationSpec == null ? null : aggregationSpec.newGroupMaker();
    ResultSetBuilder result = new ResultSetBuilder(getResultMetadata(), selectors, groupMaker);
    while (partitions.hasNext()) {
        try (RowIterator partition = partitions.next()) {
            processPartition(partition, options, result, nowInSec);
        }
    }
    ResultSet cqlRows = result.build();
    maybeWarn(result, options);
    orderResults(cqlRows);
    cqlRows.trim(userLimit);
    return cqlRows;
}
Also used : ResultSetBuilder(org.apache.cassandra.cql3.selection.ResultSetBuilder) RowIterator(org.apache.cassandra.db.rows.RowIterator) GroupMaker(org.apache.cassandra.db.aggregation.GroupMaker)

Example 2 with ResultSetBuilder

use of org.apache.cassandra.cql3.selection.ResultSetBuilder in project cassandra by apache.

the class ModificationStatement method buildCasFailureResultSet.

private static ResultSet buildCasFailureResultSet(RowIterator partition, Iterable<ColumnMetadata> columnsWithConditions, boolean isBatch, QueryOptions options, int nowInSeconds) {
    TableMetadata metadata = partition.metadata();
    Selection selection;
    if (columnsWithConditions == null) {
        selection = Selection.wildcard(metadata, false, false);
    } else {
        // We can have multiple conditions on the same columns (for collections) so use a set
        // to avoid duplicate, but preserve the order just to it follows the order of IF in the query in general
        Set<ColumnMetadata> defs = new LinkedHashSet<>();
        // of batches for compatibility sakes).
        if (isBatch)
            Iterables.addAll(defs, metadata.primaryKeyColumns());
        Iterables.addAll(defs, columnsWithConditions);
        selection = Selection.forColumns(metadata, new ArrayList<>(defs), false);
    }
    Selectors selectors = selection.newSelectors(options);
    ResultSetBuilder builder = new ResultSetBuilder(selection.getResultMetadata(), selectors);
    SelectStatement.forSelection(metadata, selection).processPartition(partition, options, builder, nowInSeconds);
    return builder.build();
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ResultSetBuilder(org.apache.cassandra.cql3.selection.ResultSetBuilder) Selection(org.apache.cassandra.cql3.selection.Selection) Selectors(org.apache.cassandra.cql3.selection.Selection.Selectors)

Example 3 with ResultSetBuilder

use of org.apache.cassandra.cql3.selection.ResultSetBuilder 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)

Aggregations

ResultSetBuilder (org.apache.cassandra.cql3.selection.ResultSetBuilder)3 RowIterator (org.apache.cassandra.db.rows.RowIterator)2 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 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 org.antlr.runtime (org.antlr.runtime)1 ImmediateExecutor (org.apache.cassandra.concurrent.ImmediateExecutor)1 ScheduledExecutors (org.apache.cassandra.concurrent.ScheduledExecutors)1 ENABLE_NODELOCAL_QUERIES (org.apache.cassandra.config.CassandraRelevantProperties.ENABLE_NODELOCAL_QUERIES)1 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)1