use of org.apache.cassandra.cql3.statements in project cassandra by apache.
the class AuditLogManager method buildEntriesForBatch.
private static List<AuditLogEntry> buildEntriesForBatch(List<? extends CQLStatement> statements, List<String> queries, QueryState state, QueryOptions options, long queryStartTimeMillis) {
List<AuditLogEntry> auditLogEntries = new ArrayList<>(statements.size() + 1);
UUID batchId = UUID.randomUUID();
String queryString = String.format("BatchId:[%s] - BATCH of [%d] statements", batchId, statements.size());
AuditLogEntry entry = new AuditLogEntry.Builder(state).setOperation(queryString).setOptions(options).setTimestamp(queryStartTimeMillis).setBatch(batchId).setType(AuditLogEntryType.BATCH).build();
auditLogEntries.add(entry);
for (int i = 0; i < statements.size(); i++) {
CQLStatement statement = statements.get(i);
entry = new AuditLogEntry.Builder(state).setType(statement.getAuditLogContext().auditLogEntryType).setOperation(queries.get(i)).setTimestamp(queryStartTimeMillis).setScope(statement).setKeyspace(state, statement).setOptions(options).setBatch(batchId).build();
auditLogEntries.add(entry);
}
return auditLogEntries;
}
use of org.apache.cassandra.cql3.statements in project cassandra by apache.
the class CassandraAuthorizer method executeLoggedBatch.
private void executeLoggedBatch(List<CQLStatement> statements) throws RequestExecutionException, RequestValidationException {
BatchStatement batch = new BatchStatement(BatchStatement.Type.LOGGED, VariableSpecifications.empty(), Lists.newArrayList(Iterables.filter(statements, ModificationStatement.class)), Attributes.none());
processBatch(batch);
}
use of org.apache.cassandra.cql3.statements in project cassandra by apache.
the class UFIdentificationTest method testBatchStatement.
@Test
public void testBatchStatement() throws Throwable {
String iFunc2 = createEchoFunction("int");
List<ModificationStatement> statements = new ArrayList<>();
statements.add(modificationStatement(cql("INSERT INTO %s (key, i_cc, t_cc) VALUES (%s, 0, 'foo')", functionCall(iFunc, "0"))));
statements.add(modificationStatement(cql("INSERT INTO %s (key, i_cc, t_cc) VALUES (1, %s, 'foo')", functionCall(iFunc2, "1"))));
statements.add(modificationStatement(cql("INSERT INTO %s (key, i_cc, t_cc) VALUES (2, 2, %s)", functionCall(tFunc, "'foo'"))));
BatchStatement batch = new BatchStatement(BatchStatement.Type.LOGGED, VariableSpecifications.empty(), statements, Attributes.none());
assertFunctions(batch, iFunc, iFunc2, tFunc);
}
use of org.apache.cassandra.cql3.statements in project cassandra by apache.
the class UFTest method testFunctionDropPreparedStatement.
@Test
public void testFunctionDropPreparedStatement() throws Throwable {
createTable("CREATE TABLE %s (key int PRIMARY KEY, d double)");
String fSin = createFunction(KEYSPACE_PER_TEST, "double", "CREATE FUNCTION %s ( input double ) " + "CALLED ON NULL INPUT " + "RETURNS double " + "LANGUAGE java " + "AS 'return Double.valueOf(Math.sin(input.doubleValue()));'");
FunctionName fSinName = parseFunctionName(fSin);
Assert.assertEquals(1, Schema.instance.getFunctions(parseFunctionName(fSin)).size());
// create a pairs of Select and Inserts. One statement in each pair uses the function so when we
// drop it those statements should be removed from the cache in QueryProcessor. The other statements
// should be unaffected.
ResultMessage.Prepared preparedSelect1 = QueryProcessor.instance.prepare(String.format("SELECT key, %s(d) FROM %s.%s", fSin, KEYSPACE, currentTable()), ClientState.forInternalCalls());
ResultMessage.Prepared preparedSelect2 = QueryProcessor.instance.prepare(String.format("SELECT key FROM %s.%s", KEYSPACE, currentTable()), ClientState.forInternalCalls());
ResultMessage.Prepared preparedInsert1 = QueryProcessor.instance.prepare(String.format("INSERT INTO %s.%s (key, d) VALUES (?, %s(?))", KEYSPACE, currentTable(), fSin), ClientState.forInternalCalls());
ResultMessage.Prepared preparedInsert2 = QueryProcessor.instance.prepare(String.format("INSERT INTO %s.%s (key, d) VALUES (?, ?)", KEYSPACE, currentTable()), ClientState.forInternalCalls());
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect2.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert2.statementId));
execute("DROP FUNCTION " + fSin + "(double);");
// the statements which use the dropped function should be removed from cache, with the others remaining
Assert.assertNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect2.statementId));
Assert.assertNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert2.statementId));
execute("CREATE FUNCTION " + fSin + " ( input double ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE java " + "AS 'return Double.valueOf(Math.sin(input));'");
Assert.assertEquals(1, Schema.instance.getFunctions(fSinName).size());
preparedSelect1 = QueryProcessor.instance.prepare(String.format("SELECT key, %s(d) FROM %s.%s", fSin, KEYSPACE, currentTable()), ClientState.forInternalCalls());
preparedInsert1 = QueryProcessor.instance.prepare(String.format("INSERT INTO %s.%s (key, d) VALUES (?, %s(?))", KEYSPACE, currentTable(), fSin), ClientState.forInternalCalls());
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
dropPerTestKeyspace();
// again, only the 2 statements referencing the function should be removed from cache
// this time because the statements select from tables in KEYSPACE, only the function
// is scoped to KEYSPACE_PER_TEST
Assert.assertNull(QueryProcessor.instance.getPrepared(preparedSelect1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedSelect2.statementId));
Assert.assertNull(QueryProcessor.instance.getPrepared(preparedInsert1.statementId));
Assert.assertNotNull(QueryProcessor.instance.getPrepared(preparedInsert2.statementId));
}
use of org.apache.cassandra.cql3.statements in project cassandra by apache.
the class FQLReplayTest method testFQLQueryBatchToStatement.
@Test
public void testFQLQueryBatchToStatement() {
List<List<ByteBuffer>> values = new ArrayList<>();
List<String> queries = new ArrayList<>();
for (int bqCount = 0; bqCount < 10; bqCount++) {
queries.add("select * from asdf where x = ? and y = " + bqCount);
List<ByteBuffer> queryValues = new ArrayList<>();
for (int i = 0; i < 10; i++) queryValues.add(ByteBufferUtil.bytes(i + ":" + bqCount));
values.add(queryValues);
}
FQLQuery.Batch batch = new FQLQuery.Batch("xyz", QueryOptions.DEFAULT.getProtocolVersion().asInt(), QueryOptions.DEFAULT, 1234, 12345, 54321, com.datastax.driver.core.BatchStatement.Type.UNLOGGED, queries, values);
Statement stmt = batch.toStatement();
assertEquals(stmt.getDefaultTimestamp(), 12345);
assertTrue(stmt instanceof com.datastax.driver.core.BatchStatement);
com.datastax.driver.core.BatchStatement batchStmt = (com.datastax.driver.core.BatchStatement) stmt;
List<Statement> statements = Lists.newArrayList(batchStmt.getStatements());
List<Statement> fromFQLQueries = batch.queries.stream().map(FQLQuery.Single::toStatement).collect(Collectors.toList());
assertEquals(statements.size(), fromFQLQueries.size());
assertEquals(12345, batchStmt.getDefaultTimestamp());
for (int i = 0; i < statements.size(); i++) compareStatements(statements.get(i), fromFQLQueries.get(i));
}
Aggregations