Search in sources :

Example 41 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class UpgradeFrom050WithBrinIndexesTest method test.

private void test(TestCase tcase, final DBManager manager, final String tableSpace) throws StatementExecutionException, DataScannerException {
    System.out.println("QUERY: " + tcase.query);
    TranslatedQuery translated = manager.getPlanner().translate(tableSpace, tcase.query, Collections.emptyList(), true, true, false, -1);
    ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
    int size = 0;
    try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
        for (DataAccessor r : scan1.consume()) {
            System.out.println("FOUND " + r.toMap());
            size++;
        }
    }
    System.out.println("SIZE: " + size);
    IndexOperation indexOperation = scan.getPredicate().getIndexOperation();
    System.out.println("OPERATION: " + indexOperation);
    assertTrue(tcase.expectedIndexAccessType.isAssignableFrom(scan.getPredicate().getIndexOperation().getClass()));
    assertEquals(tcase.expectedRecordCount, size);
}
Also used : IndexOperation(herddb.index.IndexOperation) TranslatedQuery(herddb.sql.TranslatedQuery) DataScanner(herddb.model.DataScanner) DataAccessor(herddb.utils.DataAccessor) ScanStatement(herddb.model.commands.ScanStatement)

Example 42 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class RawSQLTest method selectFromDualTest.

@Test
public void selectFromDualTest() 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);
        try (DataScanner scan = TestUtils.scan(manager, "SELECT 'one' as theStringConstant,3 LongConstant FROM DUAL", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(1, records.size());
            assertEquals("one", records.get(0).get("theStringConstant").toString());
            assertEquals(3L, records.get(0).get("LongConstant"));
        }
        try (DataScanner scan = TestUtils.scan(manager, "SELECT 'one' as theStringConstant,3  LongConstant FROM tblspace1.Dual", Collections.emptyList())) {
            List<DataAccessor> records = scan.consume();
            assertEquals(1, records.size());
            assertEquals("one", records.get(0).get("theStringConstant").toString());
            assertEquals(3L, records.get(0).get("LongConstant"));
        }
    }
}
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 43 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class RawSQLTest method simpleCountTest.

@Test
public void simpleCountTest() 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());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1) values(?)", Arrays.asList("mykey4")).getUpdateCount());
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) 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"));
            }
        }
        try (DataScanner scan1 = scan(manager, "SELECT COUNT(*)  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("expr$0"));
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc FROM tblspace1.tsql WHERE k1='mykey3'", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
                assertEquals(Long.valueOf(1), result.get(0).get(0));
                assertEquals(Long.valueOf(1), result.get(0).get("cc"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*),k1 FROM tblspace1.tsql", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                Assert.fail();
            } catch (StatementExecutionException error) {
                assertTrue("field k1 MUST appear in GROUP BY clause".equals(error.getMessage()) || error.getMessage().equals("From line 1, column 17 to line 1, column 18: Expression 'K1' is not being grouped"));
            }
        }
        {
            try (DataScanner scan1 = scan(manager, "SELECT COUNT(*) as cc,k1 FROM tblspace1.tsql GROUP BY k1", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(4, result.size());
                for (DataAccessor t : result) {
                    assertEquals(Long.valueOf(1), t.get("cc"));
                    switch(t.get("k1") + "") {
                        case "mykey":
                        case "mykey2":
                        case "mykey3":
                        case "mykey4":
                            break;
                        default:
                            fail();
                    }
                }
            }
        }
    }
}
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) List(java.util.List) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 44 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class RawSQLTest method orderByAlias2Test.

@Test
public void orderByAlias2Test() 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) 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 45 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class RawSQLTest method simpleSumAvgTest.

@Test
public void simpleSumAvgTest() 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 AVG(n1) as cc,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
            // AVG between integers is an integer, Calcite actually does SUM/COUNT cast to Integer
            // EnumerableProject(CC=[CAST(/(CASE(=($2, 0), null:INTEGER, $1), $2)):INTEGER], S1=[$0]): rowcount = 1.0, cumulative cost = {4.25 rows, 4.0 cpu, 0.0 io}, id = 241
            // EnumerableAggregate(group=[{0}], agg#0=[$SUM0($1)], agg#1=[COUNT($1)]): rowcount = 1.0, cumulative cost = {3.25 rows, 2.0 cpu, 0.0 io}, id = 240
            assertEquals(Integer.valueOf(1), result.get(0).get(0));
            assertEquals(Integer.valueOf(1), result.get(0).get("cc"));
            assertEquals(RawString.of("a"), result.get(0).get(1));
            assertEquals(RawString.of("a"), result.get(0).get("s1"));
            assertEquals(Integer.valueOf(5), result.get(1).get(0));
            assertEquals(Integer.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(Integer.valueOf(0), result.get(2).get(0));
            assertEquals(Integer.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 CAST(AVG(n1) as DOUBLE) as cc,s1 FROM tblspace1.tsql GROUP BY s1 ORDER BY s1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
            // you can require it as DOUBLE, then Calcite uses real AVG aggregator
            // LogicalProject(CC=[CAST($1):DOUBLE], S1=[$0]): rowcount = 1.0, cumulative cost = {10.125 rows, 15.0 cpu, 0.0 io}, id = 254
            // LogicalAggregate(group=[{0}], agg#0=[AVG($1)]): rowcount = 1.0, cumulative cost = {9.125 rows, 13.0 cpu, 0.0 io}, id = 252
            assertEquals(Double.valueOf(1.5), result.get(0).get(0));
            assertEquals(Double.valueOf(1.5), result.get(0).get("cc"));
            assertEquals(RawString.of("a"), result.get(0).get(1));
            assertEquals(RawString.of("a"), result.get(0).get("s1"));
            assertEquals(Double.valueOf(5), result.get(1).get(0));
            assertEquals(Double.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(Double.valueOf(0), result.get(2).get(0));
            assertEquals(Double.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 AVG(n1) as cc FROM tblspace1.tsql ", Collections.emptyList())) {
            // AVG between integers is an integer
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(Integer.valueOf(2), result.get(0).get(0));
            assertEquals(Integer.valueOf(2), result.get(0).get("cc"));
        }
        try (DataScanner scan1 = scan(manager, "SELECT AVG(CAST(n1 as DOUBLE)) as cc FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(Double.valueOf(2.0), result.get(0).get(0));
            assertEquals(Double.valueOf(2.0), result.get(0).get("cc"));
        }
        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"));
        }
        // test "modulo" operator
        try (DataScanner scan1 = scan(manager, "SELECT n1 % 4 as cc FROM tblspace1.tsql where n1 % 4 = 1 and k1='mykey3'", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(Long.valueOf(1), result.get(0).get(0));
            assertEquals(Long.valueOf(1), result.get(0).get("cc"));
        }
        if (manager.isFullSQLSupportEnabled()) {
            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) 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)

Aggregations

DataAccessor (herddb.utils.DataAccessor)164 DataScanner (herddb.model.DataScanner)118 Test (org.junit.Test)110 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)83 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)82 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)82 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)76 RawString (herddb.utils.RawString)70 DBManager (herddb.core.DBManager)52 TransactionContext (herddb.model.TransactionContext)44 StatementExecutionException (herddb.model.StatementExecutionException)38 Table (herddb.model.Table)34 List (java.util.List)31 DataScannerException (herddb.model.DataScannerException)28 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)27 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)27 Column (herddb.model.Column)22 ScanResult (herddb.model.ScanResult)22 ScanStatement (herddb.model.commands.ScanStatement)22 Bytes (herddb.utils.Bytes)22