use of herddb.mem.MemoryCommitLogManager in project herddb by diennea.
the class SecondaryIndexAccessSuite method createIndexOnTableWithData.
@Test
public void createIndexOnTableWithData() 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);
Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
Index index = Index.builder().onTable(table).type(indexType).column("name", ColumnTypes.STRING).build();
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('a','n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('b','n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('c','n1')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('d','n2')", Collections.emptyList());
TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('e','n2')", Collections.emptyList());
// create index, it will be built using existing data
CreateIndexStatement st3 = new CreateIndexStatement(index);
manager.executeStatement(st3, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
assertEquals(3, scan1.consume().size());
}
}
}
use of herddb.mem.MemoryCommitLogManager in project herddb by diennea.
the class CalcitePlannerTest method explainPlanTest.
@Test
public void explainPlanTest() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
assumeTrue(manager.getPlanner() instanceof CalcitePlanner);
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," + "s1 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1 ," + "s1) values('testk1','tests1')", Collections.emptyList());
try (DataScanner scan = scan(manager, "EXPLAIN SELECT k1 FROM tblspace1.tsql", Collections.emptyList())) {
List<DataAccessor> consume = scan.consume();
assertEquals(4, consume.size());
consume.get(0).forEach((k, b) -> {
System.out.println("k:" + k + " -> " + b);
});
consume.get(1).forEach((k, b) -> {
System.out.println("k:" + k + " -> " + b);
});
consume.get(2).forEach((k, b) -> {
System.out.println("k:" + k + " -> " + b);
});
consume.get(3).forEach((k, b) -> {
System.out.println("k:" + k + " -> " + b);
});
}
}
}
use of herddb.mem.MemoryCommitLogManager in project herddb by diennea.
the class CalcitePlannerTest method joinsPlansTests.
@Test
public void joinsPlansTests() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
assumeThat(manager.getPlanner(), instanceOf(CalcitePlanner.class));
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 TABLE tblspace1.tsql2 (k2 string primary key,n2 int,s2 string)", Collections.emptyList());
execute(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?)", Arrays.asList("mykey", 1234), TransactionContext.NO_TRANSACTION);
execute(manager, "INSERT INTO tblspace1.tsql2 (k2,n2) values(?,?)", Arrays.asList("mykey", 1234), TransactionContext.NO_TRANSACTION);
execute(manager, "INSERT INTO tblspace1.tsql2 (k2,n2) values(?,?)", Arrays.asList("mykey2", 1234), TransactionContext.NO_TRANSACTION);
{
List<DataAccessor> tuples = scan(manager, "select k2,n2,n1 " + " from tblspace1.tsql2 join tblspace1.tsql on k2<>k1 " + " where n2 > 1 " + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(3, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
assertEquals("n1", t.getFieldNames()[2]);
}
assertEquals(1, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey2", "n2", 1234, "n1", 1234))));
}
{
// theta join
List<DataAccessor> tuples = scan(manager, "select k2,n2,n1 " + " from tblspace1.tsql2" + " left join tblspace1.tsql on n2 < n1-1000" + " " + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(3, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
assertEquals("n1", t.getFieldNames()[2]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey2", "n2", 1234, "n1", null))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey", "n2", 1234, "n1", null))));
}
{
List<DataAccessor> tuples = scan(manager, "select k2,n2,round(n1/2) as halfn1" + " from tblspace1.tsql2" + " left join tblspace1.tsql on n2 < n1-?" + " " + " ", Arrays.asList(-1000)).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(3, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
assertEquals("halfn1", t.getFieldNames()[2]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey2", "n2", 1234, "halfn1", 617.0))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey", "n2", 1234, "halfn1", 617.0))));
}
{
List<DataAccessor> tuples = scan(manager, "select k2,n2 from tblspace1.tsql2" + " where k2 not in (select k1 from tblspace1.tsql)" + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(2, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
}
assertEquals(1, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey2", "n2", 1234))));
}
{
List<DataAccessor> tuples = scan(manager, "select k2,n2 from tblspace1.tsql2" + " where n2 in (select n1 from tblspace1.tsql" + " )" + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(2, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey2", "n2", 1234))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey", "n2", 1234))));
}
{
List<DataAccessor> tuples = scan(manager, "select k2,n2 from tblspace1.tsql2" + " where exists (select * from tblspace1.tsql" + " where n1=n2)" + " ", Collections.emptyList()).consume();
for (DataAccessor t : tuples) {
System.out.println("tuple -: " + t.toMap());
assertEquals(2, t.getFieldNames().length);
assertEquals("k2", t.getFieldNames()[0]);
assertEquals("n2", t.getFieldNames()[1]);
}
assertEquals(2, tuples.size());
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey2", "n2", 1234))));
assertTrue(tuples.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k2", "mykey", "n2", 1234))));
}
}
}
use of herddb.mem.MemoryCommitLogManager in project herddb by diennea.
the class CalcitePlannerTest method dirtySQLPlansTests.
@Test
public void dirtySQLPlansTests() throws Exception {
String nodeId = "localhost";
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
assumeThat(manager.getPlanner(), instanceOf(CalcitePlanner.class));
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, "\n\nCREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
execute(manager, "\n\nINSERT 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());
}
assertInstanceOf(plan(manager, "-- comment\nselect * from tblspace1.tsql"), TableScanOp.class);
assertInstanceOf(plan(manager, "/* multiline\ncomment */\nselect * from tblspace1.tsql where n1=1"), BindableTableScanOp.class);
assertInstanceOf(plan(manager, "\n\nselect n1 from tblspace1.tsql"), BindableTableScanOp.class);
assertInstanceOf(plan(manager, "-- comment\nupdate tblspace1.tsql set n1=? where k1=?"), SimpleUpdateOp.class);
assertInstanceOf(plan(manager, "/* multiline\ncomment */\nupdate tblspace1.tsql set n1=? where k1=? and n1=?"), SimpleUpdateOp.class);
assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=?" + " where n1 in (select b.n1*2 from tblspace1.tsql b)"), UpdateOp.class);
assertInstanceOf(plan(manager, "-- comment\ndelete from tblspace1.tsql where k1=?"), SimpleDeleteOp.class);
assertInstanceOf(plan(manager, "/* multiline\ncomment */\ndelete from tblspace1.tsql where k1=? and n1=?"), SimpleDeleteOp.class);
assertInstanceOf(plan(manager, "\n\ndelete from tblspace1.tsql where n1 in (select b.n1*2 from tblspace1.tsql b)"), DeleteOp.class);
assertInstanceOf(plan(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?)"), SimpleInsertOp.class);
assertInstanceOf(plan(manager, "INSERT INTO tblspace1.tsql (k1,n1) values(?,?),(?,?)"), InsertOp.class);
assertInstanceOf(plan(manager, "select k1 from tblspace1.tsql order by k1"), SortedBindableTableScanOp.class);
assertInstanceOf(plan(manager, "select k1 from tblspace1.tsql order by k1 limit 10"), LimitedSortedBindableTableScanOp.class);
BindableTableScanOp plan = (BindableTableScanOp) assertInstanceOf(plan(manager, "select * from tblspace1.tsql where k1=?"), BindableTableScanOp.class);
Projection projection = plan.getStatement().getProjection();
System.out.println("projection:" + projection);
assertThat(projection, instanceOf(IdentityProjection.class));
assertNull(plan.getStatement().getComparator());
}
}
use of herddb.mem.MemoryCommitLogManager in project herddb by diennea.
the class SQLRecordPredicateTest method testEvaluateExpression.
@Test
public void testEvaluateExpression() throws Exception {
try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
manager.start();
assertTrue(manager.waitForTablespace(TableSpace.DEFAULT, 10000));
Table table = Table.builder().name("t1").column("pk", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("pk").build();
CreateTableStatement st2 = new CreateTableStatement(table);
manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
{
TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * " + "FROM t1 " + "where pk >= ?", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
assertTrue(scan.getPredicate() instanceof SQLRecordPredicate);
SQLRecordPredicate pred = (SQLRecordPredicate) scan.getPredicate();
assertTrue(pred.getPrimaryKeyFilter() != null && (pred.getPrimaryKeyFilter() instanceof CompiledSQLExpression));
assertTrue(pred.getWhere() != null && (pred.getWhere() instanceof CompiledSQLExpression));
StatementEvaluationContext ctx = new SQLStatementEvaluationContext("the-query", Arrays.asList("my-string"));
Record record = RecordSerializer.makeRecord(table, "pk", "test", "name", "myname");
assertEquals(Boolean.TRUE, pred.evaluate(record, ctx));
long _start = System.currentTimeMillis();
int SIZE = 20_000_000;
for (int i = 0; i < SIZE; i++) {
pred.evaluate(record, ctx);
}
long _end = System.currentTimeMillis();
double speed = (int) (SIZE * 1000d / (_end - _start));
System.out.println("speed: " + speed + " eval/s" + " per " + SIZE + " records");
}
}
}
Aggregations