Search in sources :

Example 96 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager 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);
            });
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 97 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager 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))));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 98 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager 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());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) ZeroCopyProjection(herddb.model.planner.ProjectOp.ZeroCopyProjection) IdentityProjection(herddb.model.planner.ProjectOp.IdentityProjection) Projection(herddb.model.Projection) RawString(herddb.utils.RawString) IdentityProjection(herddb.model.planner.ProjectOp.IdentityProjection) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) SortedBindableTableScanOp(herddb.model.planner.SortedBindableTableScanOp) LimitedSortedBindableTableScanOp(herddb.model.planner.LimitedSortedBindableTableScanOp) Test(org.junit.Test)

Example 99 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager 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");
        }
    }
}
Also used : Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) DBManager(herddb.core.DBManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Record(herddb.model.Record) StatementEvaluationContext(herddb.model.StatementEvaluationContext) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 100 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager 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());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)109 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)108 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)108 Test (org.junit.Test)104 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)103 DataScanner (herddb.model.DataScanner)70 RawString (herddb.utils.RawString)57 DataAccessor (herddb.utils.DataAccessor)36 ScanStatement (herddb.model.commands.ScanStatement)30 TranslatedQuery (herddb.sql.TranslatedQuery)27 Table (herddb.model.Table)26 List (java.util.List)26 CreateTableStatement (herddb.model.commands.CreateTableStatement)23 Index (herddb.model.Index)17 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)17 TransactionContext (herddb.model.TransactionContext)16 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)14 StatementExecutionException (herddb.model.StatementExecutionException)11 DBManager (herddb.core.DBManager)10 SQLPlanner (herddb.sql.SQLPlanner)10