Search in sources :

Example 76 with DataScanner

use of herddb.model.DataScanner in project herddb by diennea.

the class RawSQLTest method testDMLOutOfOrderThanCreateTable.

@Test
public void testDMLOutOfOrderThanCreateTable() 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.IP (\n" + "   field0             INT          NOT NULL PRIMARY KEY,\n" + "   field1          VARCHAR(15)  NOT NULL,\n" + "   field2         VARCHAR(255) NOT NULL,\n" + "   field3           INT          NOT NULL,\n" + "   field4           INT          NOT NULL,\n" + "   field5            NCLOB        /* NOT NULL */\n" + ")", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.IP (field0,field1,field3,field4,field2,field5) values(?,?,?,?,?,?)", Arrays.asList(1, "1.2.3.4", 2, 5, "localhost", "aaa")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.IP set field3=?, field2=?, field4=?, field5=? WHERE field0=?", Arrays.asList(1, "localhost", 8, "aaa", 1)).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.IP set field3=?, field4=?, field2=?, field5=? WHERE field0=?", Arrays.asList(1, 8, "localhost", "aaa", 1)).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT field5,field4,field2,field3 FROM tblspace1.IP where field0=?", Arrays.asList(1))) {
            while (scan.hasNext()) {
                DataAccessor next = scan.next();
                assertThat(next, instanceOf(RuntimeProjectedDataAccessor.class));
                assertArrayEquals(new String[] { "field5", "field4", "field2", "field3" }, next.getFieldNames());
                assertArrayEquals(new Object[] { RawString.of("aaa"), 8, RawString.of("localhost"), 1 }, next.getValues());
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) 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 77 with DataScanner

use of herddb.model.DataScanner in project herddb by diennea.

the class RawSQLTest method caseWhenTest.

@Test
public void caseWhenTest() 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,t1 timestamp)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey2", Integer.valueOf(1235))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey3", Integer.valueOf(1236))).getUpdateCount());
        try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey'  THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "ELSE 'c'  " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> res = scan.consume();
            for (DataAccessor t : res) {
                System.out.println("t:" + t);
            }
            assertEquals(3, res.size());
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", "c"))));
        }
        try (DataScanner scan = scan(manager, "SELECT k1, " + "CASE " + "WHEN k1='mykey'  THEN 'a' " + "WHEN k1='mykey2' THEN 'b' " + "END as mycase " + "FROM tblspace1.tsql " + "ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> res = scan.consume();
            for (DataAccessor t : res) {
                System.out.println("t:" + t);
            }
            assertEquals(3, res.size());
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mycase", "a"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mycase", "b"))));
            assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mycase", null))));
        }
        if (manager.isFullSQLSupportEnabled()) {
            try (DataScanner scan = scan(manager, "SELECT k1, " + "SUM(CASE " + "WHEN k1='mykey'  THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3  " + "END) as mysum " + "FROM tblspace1.tsql " + "GROUP BY k1", Collections.emptyList())) {
                List<DataAccessor> res = scan.consume();
                for (DataAccessor t : res) {
                    System.out.println("t2:" + t);
                }
                assertEquals(3, res.size());
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey", "mysum", 1L))));
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey2", "mysum", 2L))));
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("k1", "mykey3", "mysum", 3L))));
            }
            try (DataScanner scan = scan(manager, "SELECT " + "SUM(CASE " + "WHEN k1='mykey'  THEN 1 " + "WHEN k1='mykey2' THEN 2 " + "ELSE 3  " + "END) as mysum " + "FROM tblspace1.tsql " + "", Collections.emptyList())) {
                List<DataAccessor> res = scan.consume();
                for (DataAccessor t : res) {
                    System.out.println("t:" + t);
                }
                assertEquals(1, res.size());
                assertTrue(res.stream().anyMatch(t -> t.toMap().equals(MapUtils.map("mysum", 6L))));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) RuntimeProjectedDataAccessor(herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor) CommitTransactionStatement(herddb.model.commands.CommitTransactionStatement) Date(java.util.Date) Assert.assertNotSame(org.junit.Assert.assertNotSame) RollbackTransactionStatement(herddb.model.commands.RollbackTransactionStatement) Table(herddb.model.Table) TransactionResult(herddb.model.TransactionResult) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) TestUtils.scan(herddb.core.TestUtils.scan) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) MapUtils(herddb.utils.MapUtils) RawString(herddb.utils.RawString) Index(herddb.model.Index) ScanResult(herddb.model.ScanResult) DataAccessor(herddb.utils.DataAccessor) DataAccessorForFullRecord(herddb.codec.DataAccessorForFullRecord) TableDoesNotExistException(herddb.model.TableDoesNotExistException) DataScanner(herddb.model.DataScanner) StatementExecutionException(herddb.model.StatementExecutionException) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) RecordSerializer(herddb.codec.RecordSerializer) ScanStatement(herddb.model.commands.ScanStatement) List(java.util.List) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) Assert.assertFalse(org.junit.Assert.assertFalse) GetStatement(herddb.model.commands.GetStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) Assume.assumeTrue(org.junit.Assume.assumeTrue) TableSpace(herddb.model.TableSpace) Bytes(herddb.utils.Bytes) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) GetResult(herddb.model.GetResult) SimpleDateFormat(java.text.SimpleDateFormat) PrimaryIndexSeek(herddb.index.PrimaryIndexSeek) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Assert.assertSame(org.junit.Assert.assertSame) TestUtils(herddb.core.TestUtils) Calendar(java.util.Calendar) TransactionContext(herddb.model.TransactionContext) Transaction(herddb.model.Transaction) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestUtils.scanKeepReadLocks(herddb.core.TestUtils.scanKeepReadLocks) TestUtils.commitTransaction(herddb.core.TestUtils.commitTransaction) IndexDoesNotExistException(herddb.model.IndexDoesNotExistException) TestUtils.executeUpdate(herddb.core.TestUtils.executeUpdate) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) DBManager(herddb.core.DBManager) Assert.assertNull(org.junit.Assert.assertNull) StatementEvaluationContext(herddb.model.StatementEvaluationContext) AbstractIndexManager(herddb.core.AbstractIndexManager) TestUtils.execute(herddb.core.TestUtils.execute) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) 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 78 with DataScanner

use of herddb.model.DataScanner in project herddb by diennea.

the class RawSQLTest method selectWithParameters.

@Test
public void selectWithParameters() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        assumeTrue(manager.getPlanner() instanceof JSQLParserPlanner);
        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());
        try (DataScanner scan = scan(manager, "SELECT ? as foo, k1, n1 FROM tblspace1.tsql", Arrays.asList("test"))) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
            assertEquals(RawString.of("test"), all.get(0).get("foo"));
            assertEquals(RawString.of("mykey"), all.get(0).get("k1"));
            assertEquals(Integer.valueOf(1234), all.get(0).get("n1"));
        }
        Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
        try (DataScanner scan = scan(manager, "SELECT ? as foo, ? as bar  FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
            assertEquals(Long.valueOf(1), all.get(0).get("foo"));
            assertEquals(timestamp, all.get(0).get("bar"));
        }
        if (manager.isFullSQLSupportEnabled()) {
            try (DataScanner scan = scan(manager, "SELECT MAX(?) as foo, MIN(?) as bar  FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
                List<DataAccessor> all = scan.consume();
                assertEquals(1, all.size());
                assertEquals(Long.valueOf(1), all.get(0).get("foo"));
                assertEquals(timestamp, all.get(0).get("bar"));
            }
        }
        executeUpdate(manager, "DELETE FROM tblspace1.tsql", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT ? as foo, ? as bar  FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
            List<DataAccessor> all = scan.consume();
            assertEquals(0, all.size());
        }
        if (manager.isFullSQLSupportEnabled()) {
            try (DataScanner scan = scan(manager, "SELECT MAX(?) as foo, MIN(?) as bar  FROM tblspace1.tsql", Arrays.asList(Long.valueOf(1), timestamp))) {
                List<DataAccessor> all = scan.consume();
                assertEquals(1, all.size());
                assertNull(all.get(0).get("foo"));
                assertNull(all.get(0).get("bar"));
            }
        }
    }
}
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) Timestamp(java.sql.Timestamp) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 79 with DataScanner

use of herddb.model.DataScanner in project herddb by diennea.

the class RawSQLTest method basicTest.

@Test
public void basicTest() 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());
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(1234), finalRecord.get("n1"));
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ?", Arrays.asList(Integer.valueOf(999), "mykey")).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and n1 = ?", Arrays.asList(Integer.valueOf(100), "mykey", Integer.valueOf(999))).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and (n1 = ? or n1 <> ?)", Arrays.asList(Integer.valueOf(999), "mykey", Integer.valueOf(100), Integer.valueOf(1000))).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and (n1 <> ?)", Arrays.asList(Integer.valueOf(34), "mykey", Integer.valueOf(15))).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=? where k1 = ? and not (n1 <> ?)", Arrays.asList(Integer.valueOf(999), "mykey", Integer.valueOf(34))).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(999), finalRecord.get("n1"));
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 = ?", Arrays.asList("mykey"), false, true, false, -1);
            GetStatement st_get = (GetStatement) translate1.plan.mainStatement;
            GetResult result = manager.get(st_get, translate1.context, TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(999), finalRecord.get("n1"));
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 = ? and n1=?", Arrays.asList("mykey", 999), false, true, false, -1);
            GetStatement st_get_with_condition = (GetStatement) translate1.plan.mainStatement;
            GetResult result = manager.get(st_get_with_condition, translate1.context, TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
            assertEquals(result.getRecord().key, Bytes.from_string("mykey"));
            Map<String, Object> finalRecord = result.getRecord().toBean(manager.getTableSpaceManager("tblspace1").getTableManager("tsql").getTable());
            assertEquals(RawString.of("mykey"), finalRecord.get("k1"));
            assertEquals(Integer.valueOf(999), finalRecord.get("n1"));
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 = ? and n1=?", Arrays.asList("mykey", 9992), false, true, false, -1);
            GetStatement st_get_with_wrong_condition = (GetStatement) translate1.plan.mainStatement;
            GetResult result = manager.get(st_get_with_wrong_condition, translate1.context, TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            assertEquals(0, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 = ? and n1 = ?", Arrays.asList("mykey", 123)).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 = ?", Arrays.asList("mykey")).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(result.found());
        }
        {
            assertEquals(1, executeUpdate(manager, "DELETE FROM tblspace1.tsql where k1 = ? and n1=?", Arrays.asList("mykey", 1234)).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values('mykey2',1234)", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2135 where k1 = 'mykey2'", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='foo' where k1 = 'mykey2' and s1 is null", Collections.emptyList()).getUpdateCount());
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(1, scan1.consume().size());
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translate1.plan, translate1.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(1, records.size());
                System.out.println("records:" + records);
                assertEquals(1, records.get(0).getFieldNames().length);
                assertEquals(1, records.get(0).toMap().size());
                assertEquals("k1", records.get(0).getFieldNames()[0]);
                assertEquals(RawString.of("mykey2"), records.get(0).get(0));
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 theKey FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translate1.plan, translate1.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(1, records.size());
                assertEquals(1, records.get(0).getFieldNames().length);
                assertEquals(1, records.get(0).toMap().size());
                assertEquals("thekey", records.get(0).getFieldNames()[0].toLowerCase());
                System.out.println("type: " + records.get(0).getClass());
                assertEquals(RawString.of("mykey2"), records.get(0).get(0));
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 as theKey,'one' as theStringConstant,3  LongConstant FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            try (DataScanner scan1 = ((ScanResult) manager.executePlan(translate1.plan, translate1.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(1, records.size());
                assertEquals(3, records.get(0).getFieldNames().length);
                assertEquals(3, records.get(0).toMap().size());
                assertEquals("thekey", records.get(0).getFieldNames()[0].toLowerCase());
                assertEquals(RawString.of("mykey2"), records.get(0).get(0));
                assertEquals("thestringconstant", records.get(0).getFieldNames()[1].toLowerCase());
                assertEquals(RawString.of("one"), records.get(0).get(1));
                assertEquals("longconstant", records.get(0).getFieldNames()[2].toLowerCase());
                assertEquals(3, ((Number) records.get(0).get(2)).longValue());
            }
        }
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.tsql where k1 ='mykey2' and s1 is not null", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                assertEquals(1, scan1.consume().size());
            }
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and s1 is not null", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(0, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and s1 is null", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(0, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and not (s1 is not null)", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "UPDATE tblspace1.tsql set n1=2138,s1='bar' where k1 = 'mykey2' and not (s1 is null)", Collections.emptyList()).getUpdateCount());
        }
        {
            assertEquals(1, executeUpdate(manager, "DELETE FROM  tblspace1.tsql where k1 = 'mykey2' and s1 is not null", Collections.emptyList()).getUpdateCount());
            assertEquals(0, executeUpdate(manager, "DELETE FROM  tblspace1.tsql where k1 = 'mykey2' and s1 is not null", Collections.emptyList()).getUpdateCount());
        }
        {
            GetResult result = manager.get(new GetStatement("tblspace1", "tsql", Bytes.from_string("mykey2"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertFalse(result.found());
        }
        {
            TransactionResult result = (TransactionResult) execute(manager, "EXECUTE BEGINTRANSACTION 'tblspace1'", Collections.emptyList());
            long tx = result.getTransactionId();
            execute(manager, "EXECUTE COMMITTRANSACTION 'tblspace1'," + tx, Collections.emptyList());
        }
        {
            TransactionResult result = (TransactionResult) execute(manager, "EXECUTE BEGINTRANSACTION 'tblspace1'", Collections.emptyList());
            long tx = result.getTransactionId();
            execute(manager, "EXECUTE ROLLBACKTRANSACTION 'tblspace1'," + tx, Collections.emptyList());
        }
    }
}
Also used : TransactionResult(herddb.model.TransactionResult) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) GetResult(herddb.model.GetResult) RawString(herddb.utils.RawString) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) GetStatement(herddb.model.commands.GetStatement) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) Map(java.util.Map) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 80 with DataScanner

use of herddb.model.DataScanner in project herddb by diennea.

the class SimpleJoinTest method testExists.

@Test
public void testExists() throws Exception {
    String nodeId = "localhost";
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    // reproduces an error due to the order or elements returned during the scan
    try (DBManager manager = new DBManager(nodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        assumeThat(manager.getPlanner(), instanceOf(CalcitePlanner.class));
        manager.start();
        manager.waitForTablespace(TableSpace.DEFAULT, 10000);
        execute(manager, "CREATE TABLE a (k1 int primary key, v1 int)", Collections.emptyList());
        execute(manager, "CREATE TABLE b (k2 int primary key, v2 int)", Collections.emptyList());
        execute(manager, "INSERT INTO a (k1,v1) values(1,1)", Collections.emptyList());
        execute(manager, "INSERT INTO b (k2,v2) values(1,1)", Collections.emptyList());
        {
            try (DataScanner scan1 = scan(manager, " select k1 as kk, v1 as vv from a where 1=1 and ( NOT EXISTS (SELECT NULL FROM b WHERE b.v2=a.k1 AND  1=1  AND b.k2 = 2 ))", Collections.emptyList())) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
                assertEquals(1, consume.size());
            }
        }
        {
            try (DataScanner scan1 = scan(manager, " select k1 as kk, v1 as vv from a where 1=1 and ( NOT EXISTS (SELECT NULL FROM b WHERE b.v2=a.k1 AND  1=1  AND b.k2 = 1 ))", Collections.emptyList())) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
                assertEquals(0, consume.size());
            }
        }
        // joins during transactions must release transaction resources properly
        {
            long tx = TestUtils.beginTransaction(manager, TableSpace.DEFAULT);
            try (DataScanner scan1 = scan(manager, " select k1 as kk, v1 as vv from a where 1=1 and ( NOT EXISTS (SELECT NULL FROM b WHERE b.v2=a.k1 AND  1=1  AND b.k2 = 2 ))", Collections.emptyList(), new TransactionContext(tx))) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
                assertEquals(1, consume.size());
            }
            TestUtils.commitTransaction(manager, TableSpace.DEFAULT, tx);
        }
        {
            long tx = TestUtils.beginTransaction(manager, TableSpace.DEFAULT);
            try (DataScanner scan1 = scan(manager, " select k1 as kk, v1 as vv from a where 1=1 and ( EXISTS (SELECT 1 FROM b WHERE b.v2=a.k1 AND b.k2 = 1 ))", Collections.emptyList(), new TransactionContext(tx))) {
                List<DataAccessor> consume = scan1.consume();
                System.out.println("NUM " + consume.size());
                for (DataAccessor r : consume) {
                    System.out.println("RECORD " + r.toMap());
                }
                assertEquals(1, consume.size());
            }
            TestUtils.commitTransaction(manager, TableSpace.DEFAULT, tx);
        }
    }
}
Also used : Path(java.nio.file.Path) FileCommitLogManager(herddb.file.FileCommitLogManager) DBManager(herddb.core.DBManager) DataScanner(herddb.model.DataScanner) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) DataAccessor(herddb.utils.DataAccessor) TransactionContext(herddb.model.TransactionContext) FileDataStorageManager(herddb.file.FileDataStorageManager) List(java.util.List) RawString(herddb.utils.RawString) Test(org.junit.Test)

Aggregations

DataScanner (herddb.model.DataScanner)247 Test (org.junit.Test)195 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)163 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)155 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)155 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)151 DataAccessor (herddb.utils.DataAccessor)116 Table (herddb.model.Table)101 ScanStatement (herddb.model.commands.ScanStatement)100 RawString (herddb.utils.RawString)91 DBManager (herddb.core.DBManager)90 CreateTableStatement (herddb.model.commands.CreateTableStatement)81 TranslatedQuery (herddb.sql.TranslatedQuery)79 Index (herddb.model.Index)66 TransactionContext (herddb.model.TransactionContext)59 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)59 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)51 StatementExecutionException (herddb.model.StatementExecutionException)47 List (java.util.List)47 InsertStatement (herddb.model.commands.InsertStatement)40