use of org.apache.cassandra.cql3.ResultSet 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();
}
use of org.apache.cassandra.cql3.ResultSet in project cassandra by apache.
the class UpdateTest method testUpdateWithDefaultTtl.
@Test
public void testUpdateWithDefaultTtl() throws Throwable {
final int secondsPerMinute = 60;
createTable("CREATE TABLE %s (a int PRIMARY KEY, b int) WITH default_time_to_live = " + (10 * secondsPerMinute));
execute("UPDATE %s SET b = 1 WHERE a = 1");
UntypedResultSet resultSet = execute("SELECT ttl(b) FROM %s WHERE a = 1");
assertEquals(1, resultSet.size());
Row row = resultSet.one();
assertTrue(row.getInt("ttl(b)") >= (9 * secondsPerMinute));
execute("UPDATE %s USING TTL ? SET b = 3 WHERE a = 1", 0);
assertRows(execute("SELECT ttl(b) FROM %s WHERE a = 1"), row(new Object[] { null }));
execute("UPDATE %s SET b = 3 WHERE a = 1");
resultSet = execute("SELECT ttl(b) FROM %s WHERE a = 1");
assertEquals(1, resultSet.size());
row = resultSet.one();
assertTrue(row.getInt("ttl(b)") >= (9 * secondsPerMinute));
execute("UPDATE %s USING TTL ? SET b = 2 WHERE a = 2", unset());
resultSet = execute("SELECT ttl(b) FROM %s WHERE a = 2");
assertEquals(1, resultSet.size());
row = resultSet.one();
assertTrue(row.getInt("ttl(b)") >= (9 * secondsPerMinute));
execute("UPDATE %s USING TTL ? SET b = ? WHERE a = ?", null, 3, 3);
assertRows(execute("SELECT ttl(b) FROM %s WHERE a = 3"), row(new Object[] { null }));
}
use of org.apache.cassandra.cql3.ResultSet in project cassandra by apache.
the class NativeProtocolTest method withCounters.
@Test
public void withCounters() throws Throwable {
try (ICluster ignored = init(builder().withNodes(3).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).start())) {
final com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").build();
Session session = cluster.connect();
session.execute("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck counter, PRIMARY KEY (pk));");
session.execute("UPDATE " + KEYSPACE + ".tbl set ck = ck + 10 where pk = 1;");
Statement select = new SimpleStatement("select * from " + KEYSPACE + ".tbl;").setConsistencyLevel(ConsistencyLevel.ALL);
final ResultSet resultSet = session.execute(select);
assertRows(RowUtil.toObjects(resultSet), row(1, 10L));
Assert.assertEquals(3, cluster.getMetadata().getAllHosts().size());
session.close();
cluster.close();
}
}
use of org.apache.cassandra.cql3.ResultSet in project cassandra by apache.
the class CollectionsTest method testCollectionOperationResultSetMetadata.
@Test
public void testCollectionOperationResultSetMetadata() throws Throwable {
createTable("CREATE TABLE %s (k int PRIMARY KEY," + "m map<text, text>," + "fm frozen<map<text, text>>," + "s set<text>," + "fs frozen<set<text>>)");
execute("INSERT INTO %s (k, m, fm, s, fs) VALUES (?, ?, ?, ?, ?)", 0, map("1", "one", "2", "two"), map("1", "one", "2", "two"), set("1", "2", "3"), set("1", "2", "3"));
String cql = "SELECT k, " + "m, m['2'], m['2'..'3'], m[..'2'], m['3'..], " + "fm, fm['2'], fm['2'..'3'], fm[..'2'], fm['3'..], " + "s, s['2'], s['2'..'3'], s[..'2'], s['3'..], " + "fs, fs['2'], fs['2'..'3'], fs[..'2'], fs['3'..] " + "FROM " + KEYSPACE + '.' + currentTable() + " WHERE k = 0";
UntypedResultSet result = execute(cql);
Iterator<ColumnSpecification> meta = result.metadata().iterator();
meta.next();
for (int i = 0; i < 4; i++) {
// take the "full" collection selection
ColumnSpecification ref = meta.next();
ColumnSpecification selSingle = meta.next();
assertEquals(ref.toString(), UTF8Type.instance, selSingle.type);
for (int selOrSlice = 0; selOrSlice < 3; selOrSlice++) {
ColumnSpecification selSlice = meta.next();
assertEquals(ref.toString(), ref.type, selSlice.type);
}
}
assertRows(result, row(0, map("1", "one", "2", "two"), "two", map("2", "two"), map("1", "one", "2", "two"), null, map("1", "one", "2", "two"), "two", map("2", "two"), map("1", "one", "2", "two"), map(), set("1", "2", "3"), "2", set("2", "3"), set("1", "2"), set("3"), set("1", "2", "3"), "2", set("2", "3"), set("1", "2"), set("3")));
Session session = sessionNet();
ResultSet rset = session.execute(cql);
ColumnDefinitions colDefs = rset.getColumnDefinitions();
Iterator<ColumnDefinitions.Definition> colDefIter = colDefs.asList().iterator();
colDefIter.next();
for (int i = 0; i < 4; i++) {
// take the "full" collection selection
ColumnDefinitions.Definition ref = colDefIter.next();
ColumnDefinitions.Definition selSingle = colDefIter.next();
assertEquals(ref.getName(), DataType.NativeType.text(), selSingle.getType());
for (int selOrSlice = 0; selOrSlice < 3; selOrSlice++) {
ColumnDefinitions.Definition selSlice = colDefIter.next();
assertEquals(ref.getName() + ' ' + ref.getType(), ref.getType(), selSlice.getType());
}
}
}
use of org.apache.cassandra.cql3.ResultSet in project cassandra by apache.
the class SASIIndexTest method testConditionalsWithReversedType.
@Test
public void testConditionalsWithReversedType() {
final String TABLE_NAME = "reversed_clustering";
QueryProcessor.executeOnceInternal(String.format("CREATE TABLE IF NOT EXISTS %s.%s (pk text, ck int, v int, PRIMARY KEY (pk, ck)) " + "WITH CLUSTERING ORDER BY (ck DESC);", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("CREATE CUSTOM INDEX ON %s.%s (ck) USING 'org.apache.cassandra.index.sasi.SASIIndex'", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("CREATE CUSTOM INDEX ON %s.%s (v) USING 'org.apache.cassandra.index.sasi.SASIIndex'", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("INSERT INTO %s.%s (pk, ck, v) VALUES ('Alex', 1, 1);", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("INSERT INTO %s.%s (pk, ck, v) VALUES ('Alex', 2, 2);", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("INSERT INTO %s.%s (pk, ck, v) VALUES ('Alex', 3, 3);", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("INSERT INTO %s.%s (pk, ck, v) VALUES ('Tom', 1, 1);", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("INSERT INTO %s.%s (pk, ck, v) VALUES ('Tom', 2, 2);", KS_NAME, TABLE_NAME));
QueryProcessor.executeOnceInternal(String.format("INSERT INTO %s.%s (pk, ck, v) VALUES ('Tom', 3, 3);", KS_NAME, TABLE_NAME));
UntypedResultSet resultSet = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s WHERE ck <= 2;", KS_NAME, TABLE_NAME));
CQLTester.assertRowsIgnoringOrder(resultSet, CQLTester.row("Alex", 1, 1), CQLTester.row("Alex", 2, 2), CQLTester.row("Tom", 1, 1), CQLTester.row("Tom", 2, 2));
resultSet = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s WHERE ck <= 2 AND v > 1 ALLOW FILTERING;", KS_NAME, TABLE_NAME));
CQLTester.assertRowsIgnoringOrder(resultSet, CQLTester.row("Alex", 2, 2), CQLTester.row("Tom", 2, 2));
resultSet = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s WHERE ck < 2;", KS_NAME, TABLE_NAME));
CQLTester.assertRowsIgnoringOrder(resultSet, CQLTester.row("Alex", 1, 1), CQLTester.row("Tom", 1, 1));
resultSet = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s WHERE ck >= 2;", KS_NAME, TABLE_NAME));
CQLTester.assertRowsIgnoringOrder(resultSet, CQLTester.row("Alex", 2, 2), CQLTester.row("Alex", 3, 3), CQLTester.row("Tom", 2, 2), CQLTester.row("Tom", 3, 3));
resultSet = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s WHERE ck >= 2 AND v < 3 ALLOW FILTERING;", KS_NAME, TABLE_NAME));
CQLTester.assertRowsIgnoringOrder(resultSet, CQLTester.row("Alex", 2, 2), CQLTester.row("Tom", 2, 2));
resultSet = QueryProcessor.executeOnceInternal(String.format("SELECT * FROM %s.%s WHERE ck > 2;", KS_NAME, TABLE_NAME));
CQLTester.assertRowsIgnoringOrder(resultSet, CQLTester.row("Alex", 3, 3), CQLTester.row("Tom", 3, 3));
}
Aggregations