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));
}
}
}
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;
}
}
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;
}
});
}
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());
}
}
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);
}
Aggregations