Search in sources :

Example 6 with DataScanner

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

the class RawSQLTest method simpleSumTest.

@Test
public void simpleSumTest() 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,s1) values(?,?,?)", Arrays.asList("mykey", Integer.valueOf(1), "a")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,?)", Arrays.asList("mykey2", Integer.valueOf(2), "a")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,s1) values(?,?,?)", Arrays.asList("mykey3", Integer.valueOf(5), "b")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
        {
            try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as cc FROM tblspace1.tsql", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
                assertEquals(Long.valueOf(8), result.get(0).get(0));
                assertEquals(Long.valueOf(8), result.get(0).get("cc"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as cc,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(3, result.size());
                assertEquals(Long.valueOf(3), result.get(0).get(0));
                assertEquals(Long.valueOf(3), result.get(0).get("cc"));
                assertEquals(RawString.of("a"), result.get(0).get(1));
                assertEquals(RawString.of("a"), result.get(0).get("s1"));
                assertEquals(Long.valueOf(5), result.get(1).get(0));
                assertEquals(Long.valueOf(5), result.get(1).get("cc"));
                assertEquals(RawString.of("b"), result.get(1).get(1));
                assertEquals(RawString.of("b"), result.get(1).get("s1"));
                assertEquals(Long.valueOf(0), result.get(2).get(0));
                assertEquals(Long.valueOf(0), result.get(2).get("cc"));
                assertEquals(null, result.get(2).get(1));
                assertEquals(null, result.get(2).get("s1"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as asum,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(3, result.size());
                assertEquals(Long.valueOf(3), result.get(0).get(0));
                assertEquals(Long.valueOf(3), result.get(0).get("asum"));
                assertEquals(RawString.of("a"), result.get(0).get(1));
                assertEquals(RawString.of("a"), result.get(0).get("s1"));
                assertEquals(Long.valueOf(5), result.get(1).get(0));
                assertEquals(Long.valueOf(5), result.get(1).get("asum"));
                assertEquals(RawString.of("b"), result.get(1).get(1));
                assertEquals(RawString.of("b"), result.get(1).get("s1"));
                assertEquals(Long.valueOf(0), result.get(2).get(0));
                assertEquals(Long.valueOf(0), result.get(2).get("asum"));
                assertEquals(null, result.get(2).get(1));
                assertEquals(null, result.get(2).get("s1"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT SUM(n1) as asum,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY asum", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(3, result.size());
                assertEquals(Long.valueOf(0), result.get(0).get(0));
                assertEquals(Long.valueOf(0), result.get(0).get("asum"));
                assertEquals(null, result.get(0).get(1));
                assertEquals(null, result.get(0).get("s1"));
                assertEquals(Long.valueOf(3), result.get(1).get(0));
                assertEquals(Long.valueOf(3), result.get(1).get("asum"));
                assertEquals(RawString.of("a"), result.get(1).get(1));
                assertEquals(RawString.of("a"), result.get(1).get("s1"));
                assertEquals(Long.valueOf(5), result.get(2).get(0));
                assertEquals(Long.valueOf(5), result.get(2).get("asum"));
                assertEquals(RawString.of("b"), result.get(2).get(1));
                assertEquals(RawString.of("b"), result.get(2).get("s1"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT SUM(1) as cc FROM tblspace1.tsql", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
                assertEquals(Long.valueOf(4), result.get(0).get(0));
                assertEquals(Long.valueOf(4), result.get(0).get("cc"));
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 7 with DataScanner

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

the class RawSQLTest method escapedStringTest.

@Test
public void escapedStringTest() 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," + "s1 string)", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.tsql (k1 ," + "s1) values('test','test ''escaped')", Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT k1,s1 FROM tblspace1.tsql where s1='test ''escaped'", Collections.emptyList())) {
            assertEquals(1, scan.consume().size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 8 with DataScanner

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

the class RawSQLTest method orderByAliasTest.

@Test
public void orderByAliasTest() 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(1))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql as tt ORDER BY tt.N1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey2"), result.get(1).get("k1"));
            assertEquals(RawString.of("mykey3"), result.get(2).get("k1"));
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql as tt ORDER BY tt.N1 desc", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey2"), result.get(1).get("k1"));
            assertEquals(RawString.of("mykey"), result.get(2).get("k1"));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) 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)

Example 9 with DataScanner

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

the class RawSQLTest method sortTest.

@Test
public void sortTest() 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(1))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(2))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(3))).getUpdateCount());
        // }
        try (DataScanner scan1 = scan(manager, "SELECT k1 FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertArrayEquals(new String[] { "k1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey"), result.get(0).get(0));
        }
        try (DataScanner scan1 = scan(manager, "SELECT n1 FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            System.out.println("result:" + result.get(0));
            assertArrayEquals(new String[] { "n1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(1, result.get(0).get("n1"));
            assertEquals(1, result.get(0).get(0));
        }
        try (DataScanner scan1 = scan(manager, "SELECT n1 FROM tblspace1.tsql ORDER BY n1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertArrayEquals(new String[] { "n1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(1, result.get(0).get("n1"));
            assertEquals(1, result.get(0).get(0));
        }
        try (DataScanner scan1 = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertArrayEquals(new String[] { "n1", "k1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey"), result.get(0).get(1));
            assertEquals(1, result.get(0).get("n1"));
            assertEquals(1, result.get(0).get(0));
        }
        try (DataScanner scan1 = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql ORDER BY k1 desc", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertArrayEquals(new String[] { "n1", "k1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey3"), result.get(0).get(1));
            assertEquals(3, result.get(0).get("n1"));
            assertEquals(3, result.get(0).get(0));
        }
        try (DataScanner scan1 = scan(manager, "SELECT n1,k1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertArrayEquals(new String[] { "n1", "k1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey3"), result.get(0).get(1));
            assertEquals(3, result.get(0).get("n1"));
            assertEquals(3, result.get(0).get(0));
        }
        try (DataScanner scan1 = scan(manager, "SELECT k1,n1 FROM tblspace1.tsql ORDER BY n1 desc", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertArrayEquals(new String[] { "k1", "n1" }, scan1.getFieldNames());
            assertEquals(3, result.size());
            assertEquals(RawString.of("mykey3"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey3"), result.get(0).get(0));
            assertEquals(3, result.get(0).get("n1"));
            assertEquals(3, result.get(0).get(1));
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) 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)

Example 10 with DataScanner

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

the class RawSQLTest method aliasTest.

@Test
public void aliasTest() 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());
        {
            TranslatedQuery translate1 = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT k1 theKey FROM tblspace1.tsql where k1 ='mykey2'", Collections.emptyList(), true, true, false, -1);
            ScanStatement scan = translate1.plan.mainStatement.unwrap(ScanStatement.class);
            PlannerOp plannerOp = translate1.plan.mainStatement.unwrap(PlannerOp.class);
            System.out.println("plannerOp:" + plannerOp);
            try (DataScanner scan1 = manager.scan(scan, translate1.context, TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> records = scan1.consume();
                assertEquals(0, records.size());
                assertEquals(1, scan1.getFieldNames().length);
                assertEquals("thekey", scan1.getFieldNames()[0].toLowerCase());
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) TranslatedQuery(herddb.sql.TranslatedQuery) PlannerOp(herddb.model.planner.PlannerOp) DataScanner(herddb.model.DataScanner) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) List(java.util.List) RawString(herddb.utils.RawString) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) 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