use of herddb.core.DBManager in project herddb by diennea.
the class SimpleScanZeroCopyTest method test.
@Test
public void test() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1 ,s1) values (?,?,?)", Arrays.asList("a", 1, "b"));
try (DataScanner scan = TestUtils.scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> data = scan.consume();
assertEquals(1, data.size());
// read from the full record
assertTrue(data.get(0) instanceof DataAccessorForFullRecord);
String[] fieldNames = scan.getFieldNames();
for (String fName : fieldNames) {
Object value = data.get(0).get(fName);
System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
}
IntHolder current = new IntHolder();
data.get(0).forEach((fName, value) -> {
System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
assertEquals(fName, fieldNames[current.value++]);
});
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT k1 FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> data = scan.consume();
assertEquals(1, data.size());
// read from the full record, keeping only some field
assertTrue(data.get(0) instanceof ProjectedDataAccessor || data.get(0) instanceof ZeroCopyProjection.RuntimeProjectedDataAccessor);
assertEquals(RawString.of("a"), data.get(0).get("k1"));
assertEquals(RawString.of("a"), data.get(0).get(0));
String[] fieldNames = scan.getFieldNames();
for (String fName : fieldNames) {
Object value = data.get(0).get(fName);
System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
}
IntHolder current = new IntHolder();
data.get(0).forEach((fName, value) -> {
System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
assertEquals(fName, fieldNames[current.value++]);
});
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> data = scan.consume();
assertEquals(1, data.size());
assertTrue(data.get(0) instanceof Tuple);
String[] fieldNames = scan.getFieldNames();
for (String fName : fieldNames) {
Object value = data.get(0).get(fName);
System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
}
IntHolder current = new IntHolder();
data.get(0).forEach((fName, value) -> {
System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
assertEquals(fName, fieldNames[current.value++]);
});
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class SimpleScanZeroCopyTest method testMultiPkFirstButBadPkOrder.
@Test
public void testMultiPkFirstButBadPkOrder() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (n1 int ,k1 string, s1 string," + "primary key(k1, n1))", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1 ,s1) values (?,?,?)", Arrays.asList("a", 1, "b"));
try (DataScanner scan = TestUtils.scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> data = scan.consume();
assertEquals(1, data.size());
// read from the full record
assertTrue(data.get(0) instanceof DataAccessorForFullRecord);
assertEquals(1, data.get(0).get("n1"));
assertEquals(1, data.get(0).get(0));
assertEquals(RawString.of("a"), data.get(0).get("k1"));
assertEquals(RawString.of("a"), data.get(0).get(1));
assertEquals(RawString.of("b"), data.get(0).get("s1"));
assertEquals(RawString.of("b"), data.get(0).get(2));
String[] fieldNames = scan.getFieldNames();
for (String fName : fieldNames) {
Object value = data.get(0).get(fName);
System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
}
IntHolder current = new IntHolder();
data.get(0).forEach((fName, value) -> {
System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
assertEquals(fName, fieldNames[current.value++]);
});
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT k1 FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> data = scan.consume();
assertEquals(1, data.size());
// read from the full record, keeping only some field
assertTrue(data.get(0) instanceof ProjectedDataAccessor || data.get(0) instanceof ZeroCopyProjection.RuntimeProjectedDataAccessor);
assertEquals(RawString.of("a"), data.get(0).get("k1"));
assertEquals(RawString.of("a"), data.get(0).get(0));
String[] fieldNames = scan.getFieldNames();
for (String fName : fieldNames) {
Object value = data.get(0).get(fName);
System.out.println("FIELD " + fName + " -> " + value + " (" + value.getClass() + ")");
}
IntHolder current = new IntHolder();
data.get(0).forEach((fName, value) -> {
System.out.println("FIELD2 " + fName + " -> " + value + " (" + value.getClass() + ")");
assertEquals(fName, fieldNames[current.value++]);
});
}
try (DataScanner scan = TestUtils.scan(manager, "SELECT COUNT(*) FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> data = scan.consume();
assertEquals(1, data.size());
assertTrue(data.get(0) instanceof Tuple);
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class SimplerPlannerTest method simpleInsertTests.
@Test
public void simpleInsertTests() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?)", Arrays.asList("mykey", 1234), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql where k1='mykey'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql where k1='a'", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql where k1='mykey' and n1=1234", Collections.emptyList())) {
assertEquals(1, scan.consume().size());
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class SimplerPlannerTest method plannerBasicStatementsTest.
@Test
public void plannerBasicStatementsTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
assertEquals(RawString.of("mykey"), results.get(0).get("k1"));
assertEquals(1234, results.get(0).get(1));
assertEquals(1234, results.get(0).get("n1"));
}
try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(2, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(1));
assertEquals(RawString.of("mykey"), results.get(0).get("k1"));
assertEquals(1234, results.get(0).get(0));
assertEquals(1234, results.get(0).get("n1"));
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(1235))).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * " + " FROM tblspace1.tsql" + " ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
assertEquals(RawString.of("mykey2"), results.get(1).get(0));
assertEquals(1234, results.get(0).get(1));
assertEquals(1235, results.get(1).get(1));
}
try (DataScanner scan = scan(manager, "SELECT * " + " FROM tblspace1.tsql" + " ORDER BY k1 desc", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(1).get(0));
assertEquals(RawString.of("mykey2"), results.get(0).get(0));
assertEquals(1234, results.get(1).get(1));
assertEquals(1235, results.get(0).get(1));
}
try (DataScanner scan = scan(manager, "SELECT k1,n1 " + " FROM tblspace1.tsql" + " ORDER BY k1 desc", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(2, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(1).get(0));
assertEquals(RawString.of("mykey2"), results.get(0).get(0));
assertEquals(1234, results.get(1).get(1));
assertEquals(1235, results.get(0).get(1));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " ORDER BY n1 desc", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(1).get(0));
assertEquals(RawString.of("mykey2"), results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " ORDER BY n1 desc limit 1", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey2"), results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " ORDER BY n1 desc limit 1,1", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " WHERE k1='mykey'", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " WHERE k1='mykey' aNd n1>3", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " WHERE k1>='mykey' aNd n1>3 and n1<=1234", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT k1 " + " FROM tblspace1.tsql" + " WHERE k1='mykey' or not n1<3" + " order by k1", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(RawString.of("mykey"), results.get(0).get(0));
assertEquals(RawString.of("mykey2"), results.get(1).get(0));
}
try (DataScanner scan = scan(manager, "SELECT count(*) " + " FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(1, results.get(0).getFieldNames().length);
assertEquals(2L, results.get(0).get(0));
}
try (DataScanner scan = scan(manager, "SELECT count(*), n1, k1 " + " FROM tblspace1.tsql" + " GROUP by k1, n1" + " ORDER BY k1", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
assertEquals(1L, results.get(0).get(0));
assertEquals(RawString.of("mykey"), results.get(0).get(2));
assertEquals(1234, results.get(0).get(1));
assertEquals(1L, results.get(1).get(0));
assertEquals(RawString.of("mykey2"), results.get(1).get(2));
assertEquals(1235, results.get(1).get(1));
}
try (DataScanner scan = scan(manager, "SELECT n1, count(*) as cc " + " FROM tblspace1.tsql" + " GROUP by n1" + " ORDER BY n1", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(2, results.get(0).getFieldNames().length);
assertEquals(1L, results.get(0).get(1));
assertEquals(1234, results.get(0).get(0));
assertEquals(1L, results.get(1).get(1));
assertEquals(1235, results.get(1).get(0));
}
if (manager.getPlanner() instanceof CalcitePlanner) {
try (DataScanner scan = scan(manager, "SELECT sum(n1), count(*) as cc, k1 " + " FROM tblspace1.tsql" + " GROUP by k1" + " ORDER BY sum(n1)", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(2, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
// see https://issues.apache.org/jira/browse/CALCITE-3998
// we are summing integers but the final datatype is not bigint
// but still INTEGER because we are actually summing only one value per row
assertEquals(1234, results.get(0).get(0));
assertEquals(1L, results.get(0).get(1));
assertEquals(RawString.of("mykey"), results.get(0).get(2));
assertEquals(1235, results.get(1).get(0));
assertEquals(1L, results.get(1).get(1));
assertEquals(RawString.of("mykey2"), results.get(1).get(2));
}
}
try (DataScanner scan = scan(manager, "SELECT sum(n1) as ss, min(n1) as mi, max(n1) as ma" + " FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
System.out.println("map:" + results.get(0).toMap());
assertEquals(1234L + 1235L, results.get(0).get("ss"));
assertEquals(1234, results.get(0).get("mi"));
assertEquals(1235, results.get(0).get("ma"));
}
if (manager.getPlanner() instanceof CalcitePlanner) {
try (DataScanner scan = scan(manager, "SELECT sum(n1), count(*) as cc, k1 " + " FROM tblspace1.tsql" + " GROUP by k1" + " HAVING sum(n1) <= 1234", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
assertEquals(3, results.get(0).getFieldNames().length);
assertEquals(1234, results.get(0).get(0));
assertEquals(1L, results.get(0).get(1));
assertEquals(RawString.of("mykey"), results.get(0).get(2));
}
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class SimplerPlannerTest method plannerDeleteTest.
@Test
public void plannerDeleteTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
manager.waitForTablespace("tblspace1", 10000);
execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql", Collections.emptyList()).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(0, results.size());
}
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql WHERE k1=?", Arrays.asList("mykey")).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(0, results.size());
}
}
}
Aggregations