Search in sources :

Example 1 with IdentityProjection

use of herddb.model.planner.ProjectOp.IdentityProjection in project herddb by diennea.

the class SortOp method optimize.

@Override
public PlannerOp optimize() {
    if (input instanceof BindableTableScanOp) {
        BindableTableScanOp op = (BindableTableScanOp) input;
        // we can change the statement, this node will be lost and the tablescan too
        ScanStatement statement = op.getStatement();
        statement.setComparator(this);
        if (fields.length == 1 && directions[0]) {
            Table tableDef = statement.getTableDef();
            if (tableDef.getPrimaryKey().length == 1) {
                if (statement.getProjection() != null && statement.getProjection() instanceof ZeroCopyProjection) {
                    ZeroCopyProjection zeroCopyProjection = (ZeroCopyProjection) statement.getProjection();
                    int index = zeroCopyProjection.mapPosition(fields[0]);
                    Column col = tableDef.resolveColumName(index);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                } else if (statement.getProjection() != null && statement.getProjection() instanceof IdentityProjection) {
                    Column col = tableDef.resolveColumName(fields[0]);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                }
            }
        }
        return new SortedBindableTableScanOp(statement);
    } else if (input instanceof TableScanOp) {
        TableScanOp op = (TableScanOp) input;
        // we can change the statement, this node will be lost and the tablescan too
        ScanStatement statement = op.getStatement();
        statement.setComparator(this);
        if (fields.length == 1 && directions[0]) {
            Table tableDef = statement.getTableDef();
            if (tableDef.getPrimaryKey().length == 1) {
                if (statement.getProjection() != null && statement.getProjection() instanceof ZeroCopyProjection) {
                    ZeroCopyProjection zeroCopyProjection = (ZeroCopyProjection) statement.getProjection();
                    int index = zeroCopyProjection.mapPosition(fields[0]);
                    Column col = tableDef.resolveColumName(index);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                } else if (statement.getProjection() != null && statement.getProjection() instanceof IdentityProjection) {
                    Column col = tableDef.resolveColumName(fields[0]);
                    if (col.name.equals(tableDef.getPrimaryKey()[0])) {
                        this.onlyPrimaryKeyAndAscending = true;
                    }
                }
            }
        }
        return new SortedTableScanOp(statement);
    }
    return this;
}
Also used : Table(herddb.model.Table) ZeroCopyProjection(herddb.model.planner.ProjectOp.ZeroCopyProjection) Column(herddb.model.Column) IdentityProjection(herddb.model.planner.ProjectOp.IdentityProjection) ScanStatement(herddb.model.commands.ScanStatement)

Example 2 with IdentityProjection

use of herddb.model.planner.ProjectOp.IdentityProjection in project herddb by diennea.

the class CalcitePlannerTest method simplePlansTests.

@Test
public void simplePlansTests() 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) values(?,?)", Arrays.asList("mykey", 1234), TransactionContext.NO_TRANSACTION);
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql", Collections.emptyList())) {
            assertEquals(1, scan.consume().size());
        }
        try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql", Collections.emptyList())) {
            assertEquals(1, scan.consume().size());
        }
        try (DataScanner scan = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql", Collections.emptyList())) {
            assertEquals(1, scan.consume().size());
        }
        try (DataScanner scan = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql where k1='mykey'", Collections.emptyList())) {
            assertEquals(1, scan.consume().size());
        }
        {
            // test ReduceExpressionsRule.FILTER_INSTANCE
            // "n1 = 1 and n1 is not null" -> this must be simplified to "n1 = 1"
            BindableTableScanOp plan = assertInstanceOf(plan(manager, "select * from tblspace1.tsql where n1 = 1 and n1 is not null"), BindableTableScanOp.class);
            ScanStatement statement = (ScanStatement) plan.getStatement();
            Projection projection = statement.getProjection();
            assertThat(projection, instanceOf(IdentityProjection.class));
            SQLRecordPredicate predicate = (SQLRecordPredicate) statement.getPredicate();
            assertThat(predicate.getWhere(), instanceOf(CompiledEqualsExpression.class));
            CompiledEqualsExpression equals = (CompiledEqualsExpression) predicate.getWhere();
            assertThat(equals.getLeft(), instanceOf(AccessCurrentRowExpression.class));
            assertThat(equals.getRight(), instanceOf(ConstantExpression.class));
        }
        if (manager.getPlanner() instanceof JSQLParserPlanner) {
            assertInstanceOf(plan(manager, "select * from tblspace1.tsql"), BindableTableScanOp.class);
        } else {
            assertInstanceOf(plan(manager, "select * from tblspace1.tsql"), TableScanOp.class);
        }
        assertInstanceOf(plan(manager, "select * from tblspace1.tsql where n1=1"), BindableTableScanOp.class);
        assertInstanceOf(plan(manager, "select n1 from tblspace1.tsql"), BindableTableScanOp.class);
        assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=? where k1=?"), SimpleUpdateOp.class);
        assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=? where k1=? and n1=?"), SimpleUpdateOp.class);
        if (manager.isFullSQLSupportEnabled()) {
            assertInstanceOf(plan(manager, "update tblspace1.tsql set n1=?" + " where n1 in (select b.n1*2 from tblspace1.tsql b)"), UpdateOp.class);
        }
        assertInstanceOf(plan(manager, "delete from tblspace1.tsql where k1=?"), SimpleDeleteOp.class);
        assertInstanceOf(plan(manager, "delete from tblspace1.tsql where k1=? and n1=?"), SimpleDeleteOp.class);
        if (manager.isFullSQLSupportEnabled()) {
            assertInstanceOf(plan(manager, "delete 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);
        if (manager.getPlanner() instanceof JSQLParserPlanner) {
            assertInstanceOf(plan(manager, "select * from tblspace1.tsql order by k1"), SortedBindableTableScanOp.class);
        } else {
            assertInstanceOf(plan(manager, "select * from tblspace1.tsql order by k1"), SortedTableScanOp.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 = 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());
        }
        {
            SortedBindableTableScanOp plan = assertInstanceOf(plan(manager, "select n1,k1 from tblspace1.tsql order by n1"), SortedBindableTableScanOp.class);
            Projection projection = plan.getStatement().getProjection();
            System.out.println("projection:" + projection);
            assertThat(projection, instanceOf(ZeroCopyProjection.class));
            ZeroCopyProjection zero = (ZeroCopyProjection) projection;
            assertArrayEquals(new String[] { "n1", "k1" }, zero.getFieldNames());
            assertEquals("n1", zero.getColumns()[0].name);
            assertEquals("k1", zero.getColumns()[1].name);
            TupleComparator comparator = plan.getStatement().getComparator();
            System.out.println("comparator:" + comparator);
            assertThat(comparator, instanceOf(SortOp.class));
            SortOp sortOp = (SortOp) comparator;
            assertFalse(sortOp.isOnlyPrimaryKeyAndAscending());
        }
        {
            SortedBindableTableScanOp plan = assertInstanceOf(plan(manager, "select n1,k1 from tblspace1.tsql order by k1"), SortedBindableTableScanOp.class);
            Projection projection = plan.getStatement().getProjection();
            System.out.println("projection:" + projection);
            assertThat(projection, instanceOf(ZeroCopyProjection.class));
            ZeroCopyProjection zero = (ZeroCopyProjection) projection;
            assertArrayEquals(new String[] { "n1", "k1" }, zero.getFieldNames());
            assertEquals("n1", zero.getColumns()[0].name);
            assertEquals("k1", zero.getColumns()[1].name);
            TupleComparator comparator = plan.getStatement().getComparator();
            System.out.println("comparator:" + comparator);
            assertThat(comparator, instanceOf(SortOp.class));
            SortOp sortOp = (SortOp) comparator;
            assertTrue(sortOp.isOnlyPrimaryKeyAndAscending());
        }
        {
            SimpleScanOp plan = (SimpleScanOp) plan(manager, "select * from tblspace1.tsql order by n1");
            if (manager.getPlanner() instanceof JSQLParserPlanner) {
                assertThat(plan, instanceOf(SortedBindableTableScanOp.class));
            } else {
                assertThat(plan, instanceOf(SortedTableScanOp.class));
            }
            Projection projection = plan.getStatement().getProjection();
            System.out.println("projection:" + projection);
            assertThat(projection, instanceOf(IdentityProjection.class));
            IdentityProjection zero = (IdentityProjection) projection;
            assertArrayEquals(new String[] { "k1", "n1", "s1" }, zero.getFieldNames());
            assertEquals("k1", zero.getColumns()[0].name);
            assertEquals("n1", zero.getColumns()[1].name);
            assertEquals("s1", zero.getColumns()[2].name);
            TupleComparator comparator = plan.getStatement().getComparator();
            System.out.println("comparator:" + comparator);
            assertThat(comparator, instanceOf(SortOp.class));
            SortOp sortOp = (SortOp) comparator;
            assertFalse(sortOp.isOnlyPrimaryKeyAndAscending());
        }
        {
            SimpleScanOp plan = (SimpleScanOp) plan(manager, "select * from tblspace1.tsql order by k1");
            if (manager.getPlanner() instanceof JSQLParserPlanner) {
                assertThat(plan, instanceOf(SortedBindableTableScanOp.class));
            } else {
                assertThat(plan, instanceOf(SortedTableScanOp.class));
            }
            Projection projection = plan.getStatement().getProjection();
            System.out.println("projection:" + projection);
            assertThat(projection, instanceOf(IdentityProjection.class));
            IdentityProjection zero = (IdentityProjection) projection;
            assertArrayEquals(new String[] { "k1", "n1", "s1" }, zero.getFieldNames());
            assertEquals("k1", zero.getColumns()[0].name);
            assertEquals("n1", zero.getColumns()[1].name);
            assertEquals("s1", zero.getColumns()[2].name);
            TupleComparator comparator = plan.getStatement().getComparator();
            System.out.println("comparator:" + comparator);
            assertThat(comparator, instanceOf(SortOp.class));
            SortOp sortOp = (SortOp) comparator;
            assertTrue(sortOp.isOnlyPrimaryKeyAndAscending());
        }
    }
}
Also used : SimpleScanOp(herddb.model.planner.SimpleScanOp) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) ZeroCopyProjection(herddb.model.planner.ProjectOp.ZeroCopyProjection) IdentityProjection(herddb.model.planner.ProjectOp.IdentityProjection) Projection(herddb.model.Projection) TupleComparator(herddb.model.TupleComparator) RawString(herddb.utils.RawString) SortOp(herddb.model.planner.SortOp) IdentityProjection(herddb.model.planner.ProjectOp.IdentityProjection) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) CompiledEqualsExpression(herddb.sql.expressions.CompiledEqualsExpression) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) ZeroCopyProjection(herddb.model.planner.ProjectOp.ZeroCopyProjection) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) SortedBindableTableScanOp(herddb.model.planner.SortedBindableTableScanOp) LimitedSortedBindableTableScanOp(herddb.model.planner.LimitedSortedBindableTableScanOp) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) BindableTableScanOp(herddb.model.planner.BindableTableScanOp) SortedBindableTableScanOp(herddb.model.planner.SortedBindableTableScanOp) LimitedSortedBindableTableScanOp(herddb.model.planner.LimitedSortedBindableTableScanOp) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

ScanStatement (herddb.model.commands.ScanStatement)2 IdentityProjection (herddb.model.planner.ProjectOp.IdentityProjection)2 ZeroCopyProjection (herddb.model.planner.ProjectOp.ZeroCopyProjection)2 DBManager (herddb.core.DBManager)1 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)1 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)1 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)1 Column (herddb.model.Column)1 DataScanner (herddb.model.DataScanner)1 Projection (herddb.model.Projection)1 Table (herddb.model.Table)1 TupleComparator (herddb.model.TupleComparator)1 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)1 BindableTableScanOp (herddb.model.planner.BindableTableScanOp)1 LimitedSortedBindableTableScanOp (herddb.model.planner.LimitedSortedBindableTableScanOp)1 SimpleScanOp (herddb.model.planner.SimpleScanOp)1 SortOp (herddb.model.planner.SortOp)1 SortedBindableTableScanOp (herddb.model.planner.SortedBindableTableScanOp)1 CompiledEqualsExpression (herddb.sql.expressions.CompiledEqualsExpression)1 RawString (herddb.utils.RawString)1