Search in sources :

Example 1 with SelectStatement

use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.

the class CassandraAuthorizer method addPermissionsForRole.

// Add every permission on the resource granted to the role
private void addPermissionsForRole(Set<Permission> permissions, IResource resource, RoleResource role) throws RequestExecutionException, RequestValidationException {
    QueryOptions options = QueryOptions.forInternalCalls(ConsistencyLevel.LOCAL_ONE, Lists.newArrayList(ByteBufferUtil.bytes(role.getRoleName()), ByteBufferUtil.bytes(resource.getName())));
    SelectStatement statement;
    // is being upgraded and so is running with mixed versions of the authz schema
    if (Schema.instance.getTableMetadata(SchemaConstants.AUTH_KEYSPACE_NAME, USER_PERMISSIONS) == null)
        statement = authorizeRoleStatement;
    else {
        // If the permissions table was initialised only after the statement got prepared, re-prepare (CASSANDRA-12813)
        if (legacyAuthorizeRoleStatement == null)
            legacyAuthorizeRoleStatement = prepare(USERNAME, USER_PERMISSIONS);
        statement = legacyAuthorizeRoleStatement;
    }
    ResultMessage.Rows rows = statement.execute(QueryState.forInternalCalls(), options, System.nanoTime());
    UntypedResultSet result = UntypedResultSet.create(rows.result);
    if (!result.isEmpty() && result.one().has(PERMISSIONS)) {
        for (String perm : result.one().getSet(PERMISSIONS, UTF8Type.instance)) {
            permissions.add(Permission.valueOf(perm));
        }
    }
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) QueryOptions(org.apache.cassandra.cql3.QueryOptions)

Example 2 with SelectStatement

use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.

the class SinglePartitionSliceCommandTest method getIteratorFromSinglePartition.

public static UnfilteredRowIterator getIteratorFromSinglePartition(String q) {
    SelectStatement stmt = (SelectStatement) QueryProcessor.parseStatement(q).prepare(ClientState.forInternalCalls());
    SinglePartitionReadQuery.Group<SinglePartitionReadCommand> query = (SinglePartitionReadQuery.Group<SinglePartitionReadCommand>) stmt.getQuery(QueryOptions.DEFAULT, 0);
    Assert.assertEquals(1, query.queries.size());
    SinglePartitionReadCommand command = Iterables.getOnlyElement(query.queries);
    try (ReadExecutionController controller = ReadExecutionController.forCommand(command, false);
        UnfilteredPartitionIterator partitions = command.executeLocally(controller)) {
        assert partitions.hasNext();
        UnfilteredRowIterator partition = partitions.next();
        assert !partitions.hasNext();
        return partition;
    }
}
Also used : UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) UnfilteredPartitionIterator(org.apache.cassandra.db.partitions.UnfilteredPartitionIterator)

Example 3 with SelectStatement

use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.

the class CompactionAllocationTest method testTinyPartitions.

private static void testTinyPartitions(String name, int numSSTable, int sstablePartitions, boolean overlap) throws Throwable {
    String ksname = "ks_" + name.toLowerCase();
    SchemaLoader.createKeyspace(ksname, KeyspaceParams.simple(1), CreateTableStatement.parse("CREATE TABLE tbl (k INT PRIMARY KEY, v INT)", ksname).build());
    ColumnFamilyStore cfs = Schema.instance.getColumnFamilyStoreInstance(Schema.instance.getTableMetadata(ksname, "tbl").id);
    Assert.assertNotNull(cfs);
    cfs.disableAutoCompaction();
    List<Runnable> reads = new ArrayList<>(numSSTable * (overlap ? 1 : sstablePartitions));
    measure(new Workload() {

        public void setup() {
            cfs.disableAutoCompaction();
            String insert = String.format("INSERT INTO %s.%s (k, v) VALUES (?,?)", ksname, "tbl");
            String read = String.format("SELECT * FROM %s.%s WHERE k = ?", ksname, "tbl");
            SelectStatement select = (SelectStatement) QueryProcessor.parseStatement(read).prepare(ClientState.forInternalCalls());
            QueryState queryState = QueryState.forInternalCalls();
            for (int f = 0; f < numSSTable; f++) {
                for (int p = 0; p < sstablePartitions; p++) {
                    int key = overlap ? p : (f * sstablePartitions) + p;
                    QueryProcessor.executeInternal(insert, key, key);
                    if (!overlap || f == 0) {
                        QueryOptions options = QueryProcessor.makeInternalOptions(select, new Object[] { f });
                        ReadQuery query = select.getQuery(options, queryState.getNowInSeconds());
                        reads.add(() -> runQuery(query, cfs.metadata.get()));
                    }
                }
                cfs.forceBlockingFlush();
            }
            Assert.assertEquals(numSSTable, cfs.getLiveSSTables().size());
        }

        public List<Runnable> getReads() {
            return reads;
        }

        public ColumnFamilyStore getCfs() {
            return cfs;
        }

        public String name() {
            return name;
        }
    });
}
Also used : SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) QueryState(org.apache.cassandra.service.QueryState) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ReadQuery(org.apache.cassandra.db.ReadQuery)

Example 4 with SelectStatement

use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.

the class AbstractReadQueryToCQLStringTest method toCQLString.

private List<String> toCQLString(String query) {
    String fullQuery = formatQuery(query);
    ClientState state = ClientState.forInternalCalls();
    CQLStatement statement = QueryProcessor.getStatement(fullQuery, state);
    assertTrue(statement instanceof SelectStatement);
    SelectStatement select = (SelectStatement) statement;
    QueryOptions options = QueryOptions.forInternalCalls(Collections.emptyList());
    ReadQuery readQuery = select.getQuery(options, FBUtilities.nowInSeconds());
    if (readQuery instanceof SinglePartitionReadCommand.Group) {
        SinglePartitionReadCommand.Group group = (SinglePartitionReadCommand.Group) readQuery;
        return group.queries.stream().map(AbstractReadQuery::toCQLString).collect(Collectors.toList());
    } else {
        assertTrue(readQuery instanceof AbstractReadQuery);
        return Collections.singletonList(((AbstractReadQuery) readQuery).toCQLString());
    }
}
Also used : ClientState(org.apache.cassandra.service.ClientState) CQLStatement(org.apache.cassandra.cql3.CQLStatement) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) QueryOptions(org.apache.cassandra.cql3.QueryOptions)

Example 5 with SelectStatement

use of org.apache.cassandra.cql3.statements.SelectStatement in project cassandra by apache.

the class SelectionColumnMappingTest method testMultipleArgumentFunction.

@Test
public void testMultipleArgumentFunction() throws Throwable {
    // demonstrate behaviour of token() with composite partition key
    tableName = createTable("CREATE TABLE %s (a int, b text, PRIMARY KEY ((a, b)))");
    ColumnSpecification tokenSpec = columnSpecification("system.token(a, b)", BytesType.instance);
    SelectionColumnMapping expected = SelectionColumnMapping.newMapping().addMapping(tokenSpec, columnDefinitions("a", "b"));
    // we don't use verify like with the other tests because this query will produce no results
    SelectStatement statement = getSelect("SELECT token(a,b) FROM %s");
    verifyColumnMapping(expected, statement);
    statement.executeLocally(QueryState.forInternalCalls(), QueryOptions.DEFAULT);
}
Also used : SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) 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