use of herddb.core.DBManager in project herddb by diennea.
the class RawSQLTest method simpleSumAvgTest.
@Test
public void simpleSumAvgTest() 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,s1) values(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1), "a")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,?)", Arrays.asList("mykey2", Integer.valueOf(2), "a")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,?)", Arrays.asList("mykey3", Integer.valueOf(5), "b")).getUpdateCount());
assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as cc FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Long.valueOf(8), result.get(0).get(0));
assertEquals(Long.valueOf(8), result.get(0).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT AVG(n1) as cc,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
// AVG between integers is an integer, Calcite actually does SUM/COUNT cast to Integer
// EnumerableProject(CC=[CAST(/(CASE(=($2, 0), null:INTEGER, $1), $2)):INTEGER], S1=[$0]): rowcount = 1.0, cumulative cost = {4.25 rows, 4.0 cpu, 0.0 io}, id = 241
// EnumerableAggregate(group=[{0}], agg#0=[$SUM0($1)], agg#1=[COUNT($1)]): rowcount = 1.0, cumulative cost = {3.25 rows, 2.0 cpu, 0.0 io}, id = 240
assertEquals(Integer.valueOf(1), result.get(0).get(0));
assertEquals(Integer.valueOf(1), result.get(0).get("cc"));
assertEquals(RawString.of("a"), result.get(0).get(1));
assertEquals(RawString.of("a"), result.get(0).get("s1"));
assertEquals(Integer.valueOf(5), result.get(1).get(0));
assertEquals(Integer.valueOf(5), result.get(1).get("cc"));
assertEquals(RawString.of("b"), result.get(1).get(1));
assertEquals(RawString.of("b"), result.get(1).get("s1"));
assertEquals(Integer.valueOf(0), result.get(2).get(0));
assertEquals(Integer.valueOf(0), result.get(2).get("cc"));
assertEquals(null, result.get(2).get(1));
assertEquals(null, result.get(2).get("s1"));
}
try (DataScanner scan1 = scan(manager, "SELECT CAST(AVG(n1) as DOUBLE) as cc,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
// you can require it as DOUBLE, then Calcite uses real AVG aggregator
// LogicalProject(CC=[CAST($1):DOUBLE], S1=[$0]): rowcount = 1.0, cumulative cost = {10.125 rows, 15.0 cpu, 0.0 io}, id = 254
// LogicalAggregate(group=[{0}], agg#0=[AVG($1)]): rowcount = 1.0, cumulative cost = {9.125 rows, 13.0 cpu, 0.0 io}, id = 252
assertEquals(Double.valueOf(1.5), result.get(0).get(0));
assertEquals(Double.valueOf(1.5), result.get(0).get("cc"));
assertEquals(RawString.of("a"), result.get(0).get(1));
assertEquals(RawString.of("a"), result.get(0).get("s1"));
assertEquals(Double.valueOf(5), result.get(1).get(0));
assertEquals(Double.valueOf(5), result.get(1).get("cc"));
assertEquals(RawString.of("b"), result.get(1).get(1));
assertEquals(RawString.of("b"), result.get(1).get("s1"));
assertEquals(Double.valueOf(0), result.get(2).get(0));
assertEquals(Double.valueOf(0), result.get(2).get("cc"));
assertEquals(null, result.get(2).get(1));
assertEquals(null, result.get(2).get("s1"));
}
try (DataScanner scan1 = scan(manager, "SELECT AVG(n1) as cc FROM tblspace1.tsql ", Collections.emptyList())) {
// AVG between integers is an integer
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Integer.valueOf(2), result.get(0).get(0));
assertEquals(Integer.valueOf(2), result.get(0).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT AVG(CAST(n1 as DOUBLE)) as cc FROM tblspace1.tsql ", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Double.valueOf(2.0), result.get(0).get(0));
assertEquals(Double.valueOf(2.0), result.get(0).get("cc"));
}
try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as asum,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
assertEquals(Long.valueOf(3), result.get(0).get(0));
assertEquals(Long.valueOf(3), result.get(0).get("asum"));
assertEquals(RawString.of("a"), result.get(0).get(1));
assertEquals(RawString.of("a"), result.get(0).get("s1"));
assertEquals(Long.valueOf(5), result.get(1).get(0));
assertEquals(Long.valueOf(5), result.get(1).get("asum"));
assertEquals(RawString.of("b"), result.get(1).get(1));
assertEquals(RawString.of("b"), result.get(1).get("s1"));
assertEquals(Long.valueOf(0), result.get(2).get(0));
assertEquals(Long.valueOf(0), result.get(2).get("asum"));
assertEquals(null, result.get(2).get(1));
assertEquals(null, result.get(2).get("s1"));
}
try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as asum,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY asum", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(3, result.size());
assertEquals(Long.valueOf(0), result.get(0).get(0));
assertEquals(Long.valueOf(0), result.get(0).get("asum"));
assertEquals(null, result.get(0).get(1));
assertEquals(null, result.get(0).get("s1"));
assertEquals(Long.valueOf(3), result.get(1).get(0));
assertEquals(Long.valueOf(3), result.get(1).get("asum"));
assertEquals(RawString.of("a"), result.get(1).get(1));
assertEquals(RawString.of("a"), result.get(1).get("s1"));
assertEquals(Long.valueOf(5), result.get(2).get(0));
assertEquals(Long.valueOf(5), result.get(2).get("asum"));
assertEquals(RawString.of("b"), result.get(2).get(1));
assertEquals(RawString.of("b"), result.get(2).get("s1"));
}
// test "modulo" operator
try (DataScanner scan1 = scan(manager, "SELECT n1 % 4 as cc FROM tblspace1.tsql where n1 % 4 = 1 and k1='mykey3'", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Long.valueOf(1), result.get(0).get(0));
assertEquals(Long.valueOf(1), result.get(0).get("cc"));
}
if (manager.isFullSQLSupportEnabled()) {
try (DataScanner scan1 = scan(manager, "SELECT SUM(1) as cc FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> result = scan1.consume();
assertEquals(1, result.size());
assertEquals(Long.valueOf(4), result.get(0).get(0));
assertEquals(Long.valueOf(4), result.get(0).get("cc"));
}
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class SimpleScanZeroCopyTest method testPkNotFirst.
@Test
public void testPkNotFirst() 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 primary key, 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);
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 SimpleScanZeroCopyTest method testMultiPkFirst.
@Test
public void testMultiPkFirst() 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 primary key,k1 string primary key, 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);
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 plannerUpdateTest.
@Test
public void plannerUpdateTest() 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, "UPDATE tblspace1.tsql set n1=1111", Collections.emptyList()).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1111", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
}
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=1112 where k1='mykey'", Collections.emptyList()).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1112", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
}
assertEquals(0, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=1112 where k1='no'", Collections.emptyList()).getUpdateCount());
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=n1+1 where k1='mykey'", Collections.emptyList()).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1113", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
}
assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1=?", Arrays.asList(1114, "mykey")).getUpdateCount());
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql where n1=1114", Collections.emptyList())) {
List<DataAccessor> results = scan.consume();
assertEquals(1, results.size());
}
}
}
use of herddb.core.DBManager in project herddb by diennea.
the class TruncateTableSQLTest method truncateTableTransactionTest.
@Test
public void truncateTableTransactionTest() 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, "CREATE BRIN INDEX test1 ON tblspace1.tsql (k1)", Collections.emptyList());
execute(manager, "CREATE HASH INDEX test2 ON tblspace1.tsql (k1)", Collections.emptyList());
long tx1 = TestUtils.beginTransaction(manager, "tblspace1");
try {
// forbidden, transactions not allowed
execute(manager, "truncate TABLE tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx1));
fail();
} catch (StatementExecutionException ok) {
assertEquals("TRUNCATE TABLE cannot be executed within the context of a Transaction", ok.getMessage());
}
long txId = execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), TransactionContext.AUTOTRANSACTION_TRANSACTION).transactionId;
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList(), new TransactionContext(txId))) {
assertEquals(1, scan.consume().size());
}
try {
// forbidden, a transaction is running on table
execute(manager, "TRUNCATE TABLE tblspace1.tsql", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
fail();
} catch (StatementExecutionException ok) {
assertEquals("TRUNCATE TABLE cannot be executed table tblspace1.tsql: at least one transaction is pending on it", ok.getCause().getMessage());
}
TestUtils.commitTransaction(manager, "tblspace1", txId);
execute(manager, "TRUNCATE TABLE tblspace1.tsql", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
assertEquals(0, scan.consume().size());
}
}
}
Aggregations