Search in sources :

Example 1 with SQLRecordPredicate

use of herddb.sql.SQLRecordPredicate in project herddb by diennea.

the class SimpleIndexAccessTest method whereOnPkScanTest.

@Test
public void whereOnPkScanTest() 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," + "n1 int primary key," + "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, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(1235))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(1236))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey4", Integer.valueOf(1237))).getUpdateCount());
        {
            TranslatedQuery translate = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 as theKey," + "'one' as theStringConstant,3  LongConstant FROM tblspace1.tsql where n1 >= ?", Arrays.asList(1235), true, true, false, -1);
            ScanStatement scan = translate.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate() instanceof SQLRecordPredicate);
            SQLRecordPredicate sqlPred = (SQLRecordPredicate) scan.getPredicate();
            assertTrue(sqlPred.getPrimaryKeyFilter() != null);
            System.out.println("pkFilter:" + sqlPred.getPrimaryKeyFilter());
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translate.plan, translate.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                assertEquals(3, scan1.consume().size());
            }
        }
        {
            TranslatedQuery translate = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 as theKey," + "'one' as theStringConstant,3  LongConstant FROM tblspace1.tsql where n1 >= 30", Arrays.asList(1235), true, true, false, -1);
            ScanStatement scan = translate.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate() instanceof SQLRecordPredicate);
            SQLRecordPredicate sqlPred = (SQLRecordPredicate) scan.getPredicate();
            assertTrue(sqlPred.getPrimaryKeyFilter() != null);
            System.out.println("pkFilter:" + sqlPred.getPrimaryKeyFilter());
            try (DataScanner scan1 = manager.scan(scan, translate.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(4, scan1.consume().size());
            }
        }
        {
            TranslatedQuery translate = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 as theKey," + "'one' as theStringConstant,3  LongConstant FROM tblspace1.tsql" + " where n1 >= 30 and n1 >= 1235", Arrays.asList(1235), true, true, false, -1);
            ScanStatement scan = translate.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate() instanceof SQLRecordPredicate);
            SQLRecordPredicate sqlPred = (SQLRecordPredicate) scan.getPredicate();
            assertTrue(sqlPred.getPrimaryKeyFilter() != null);
            System.out.println("pkFilter:" + sqlPred.getPrimaryKeyFilter());
            try (DataScanner scan1 = manager.scan(scan, translate.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(3, scan1.consume().size());
            }
        }
        {
            TranslatedQuery translate = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 as theKey," + "'one' as theStringConstant,3  LongConstant FROM tblspace1.tsql where n1 >= 10000 or n1 >= ?", Arrays.asList(1235), true, true, false, -1);
            ScanStatement scan = translate.plan.mainStatement.unwrap(ScanStatement.class);
            assertTrue(scan.getPredicate() instanceof SQLRecordPredicate);
            SQLRecordPredicate sqlPred = (SQLRecordPredicate) scan.getPredicate();
            assertNull(sqlPred.getPrimaryKeyFilter());
            System.out.println("pkFilter:" + sqlPred.getPrimaryKeyFilter());
            try (DataScanner scan1 = manager.scan(scan, translate.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(3, scan1.consume().size());
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) TranslatedQuery(herddb.sql.TranslatedQuery) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) SQLRecordPredicate(herddb.sql.SQLRecordPredicate) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)1 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)1 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)1 DataScanner (herddb.model.DataScanner)1 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)1 ScanStatement (herddb.model.commands.ScanStatement)1 SQLRecordPredicate (herddb.sql.SQLRecordPredicate)1 TranslatedQuery (herddb.sql.TranslatedQuery)1 Test (org.junit.Test)1