Search in sources :

Example 1 with SQLPlanner

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

the class RawSQLTest method jdbcWrongParameterCountTest.

@Test
public void jdbcWrongParameterCountTest() 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());
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql where k1=?", Collections.emptyList());
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(1, ok.getIndex());
        }
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql where k1=1 and n1=?", Collections.emptyList());
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(1, ok.getIndex());
        }
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql where k1=1 or n1=?", Collections.emptyList());
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(1, ok.getIndex());
        }
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql order by k1 limit ?", Collections.emptyList());
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(1, ok.getIndex());
        }
        if (manager.getPlanner() instanceof SQLPlanner) {
            try {
                scan(manager, "SELECT * FROM tblspace1.tsql where n1 = 1234 and k1 in " + "(SELECT k1 FROM tblspace1.tsql order by k1 limit ?) and n1 = ?", Arrays.asList(1));
                fail();
            } catch (MissingJDBCParameterException ok) {
                assertEquals(2, ok.getIndex());
            }
            scan(manager, "SELECT * FROM tblspace1.tsql where n1 = ? and k1 in " + "(SELECT k1 FROM tblspace1.tsql order by k1 limit ?)", Arrays.asList(1));
            try {
                scan(manager, "SELECT * FROM tblspace1.tsql where k1 in " + "(SELECT k1 FROM tblspace1.tsql order by k1 limit ?)", Collections.emptyList());
                fail();
            } catch (MissingJDBCParameterException ok) {
                assertEquals(1, ok.getIndex());
            }
            try {
                scan(manager, "SELECT * FROM tblspace1.tsql where k1 in (SELECT k1+? FROM tblspace1.tsql)", Collections.emptyList());
                fail();
            } catch (MissingJDBCParameterException ok) {
                assertEquals(1, ok.getIndex());
            }
        }
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql where k1 in (SELECT k1 FROM tblspace1.tsql where n1=?)", Collections.emptyList());
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(1, ok.getIndex());
        }
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql where n1=? and k1 in (SELECT k1 FROM tblspace1.tsql where n1=?)", Arrays.asList(1));
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(2, ok.getIndex());
        }
        try {
            scan(manager, "SELECT * FROM tblspace1.tsql where k1=1 and n1=? and n1=?", Arrays.asList(1));
            fail();
        } catch (MissingJDBCParameterException ok) {
            assertEquals(2, ok.getIndex());
        }
        try {
            scan(manager, "SELECT n1+? FROM tblspace1.tsql", Collections.emptyList());
        } catch (MissingJDBCParameterException ok) {
            assertEquals(1, ok.getIndex());
        }
        if (manager.getPlanner() instanceof SQLPlanner) {
            try {
                scan(manager, "SELECT sum(n1), sum(?) FROM tblspace1.tsql", Collections.emptyList());
            } catch (MissingJDBCParameterException ok) {
                assertEquals(1, ok.getIndex());
            }
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) RawString(herddb.utils.RawString) SQLPlanner(herddb.sql.SQLPlanner) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 2 with SQLPlanner

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

the class RawSQLTest method betweenTest.

@Test
public void betweenTest() 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,ts timestamp,l1 bigint)", Collections.emptyList());
        long now = System.currentTimeMillis();
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,ts,l1) values(?,?,?,?)", Arrays.asList("mykey", Integer.valueOf(1234), new java.sql.Timestamp(now), Long.valueOf(2234))).getUpdateCount());
        // integer
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where n1 between 1234 and 1234", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where n1 between 1234 and 1235", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where n1 between 1233 and 1234", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where n1 between 1200 and 1239", Collections.emptyList()).consume().size());
        assertEquals(0, scan(manager, "SELECT * FROM tblspace1.tsql where n1 between 0 and -1", Collections.emptyList()).consume().size());
        // long
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where l1 between 2234 and 2234", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where l1 between 2234 and 2235", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where l1 between 2233 and 2234", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where l1 between 2200 and 2239", Collections.emptyList()).consume().size());
        assertEquals(0, scan(manager, "SELECT * FROM tblspace1.tsql where l1 between 0 and -1", Collections.emptyList()).consume().size());
        // string
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where k1 between 'mykey' and 'mykey'", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where k1 between 'mykey' and 'mykfy'", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where k1 between 'mykdy' and 'mykey'", Collections.emptyList()).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where k1 between 'mykay' and 'mykqy'", Collections.emptyList()).consume().size());
        assertEquals(0, scan(manager, "SELECT * FROM tblspace1.tsql where k1 between 'mykfy' and 'mykgy'", Collections.emptyList()).consume().size());
        // timestamp
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where ts between ? and ?", Arrays.asList(new java.sql.Timestamp(now), new java.sql.Timestamp(now))).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where ts between ? and ?", Arrays.asList(new java.sql.Timestamp(now), new java.sql.Timestamp(now + 60000))).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where ts between ? and ?", Arrays.asList(new java.sql.Timestamp(now - 1000), new java.sql.Timestamp(now))).consume().size());
        assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where ts between ? and ?", Arrays.asList(new java.sql.Timestamp(now - 1000), new java.sql.Timestamp(now + 60000))).consume().size());
        assertEquals(0, scan(manager, "SELECT * FROM tblspace1.tsql where ts between ? and ?", Arrays.asList(new java.sql.Timestamp(0), new java.sql.Timestamp(1000))).consume().size());
        assertEquals(0, scan(manager, "SELECT * FROM tblspace1.tsql where ts between ? and ?", Arrays.asList(new java.sql.Timestamp(now + 1000), new java.sql.Timestamp(now - 1000))).consume().size());
        if (manager.getPlanner() instanceof SQLPlanner) {
            System.out.println("now:" + new java.sql.Timestamp(now));
            assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where ts >= {ts '" + new java.sql.Timestamp(now) + "'}", Collections.emptyList()).consume().size());
            // timestamp with jdbc literals
            assertEquals(1, scan(manager, "SELECT * FROM tblspace1.tsql where ts between {ts '" + new java.sql.Timestamp(now) + "'} and {ts '" + new java.sql.Timestamp(now) + "'}", Collections.emptyList()).consume().size());
        } else {
        // Calcite interprets JDBC syntax as in UTC Timezone
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Timestamp(java.sql.Timestamp) RawString(herddb.utils.RawString) SQLPlanner(herddb.sql.SQLPlanner) Timestamp(java.sql.Timestamp) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 3 with SQLPlanner

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

the class DropTableSQLTest method dropTableWithTransaction.

@Test
public void dropTableWithTransaction() 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);
        long tx = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 string primary key,n1 int,s1 string)", Collections.emptyList(), new TransactionContext(tx));
        execute(manager, "INSERT INTO tblspace1.tsql (k1) values('a')", Collections.emptyList(), new TransactionContext(tx));
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            fail();
        } catch (TableDoesNotExistException ok) {
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(0, all.size());
        }
        execute(manager, "EXECUTE committransaction 'tblspace1'," + tx, Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        long tx2 = ((TransactionResult) execute(manager, "EXECUTE begintransaction 'tblspace1'", Collections.emptyList())).getTransactionId();
        execute(manager, "DROP TABLE tblspace1.tsql", Collections.emptyList(), new TransactionContext(tx2));
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(1, all.size());
        }
        execute(manager, "EXECUTE committransaction 'tblspace1'," + tx2, Collections.emptyList());
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.systables where table_name='tsql'", Collections.emptyList())) {
            List<DataAccessor> all = scan.consume();
            assertEquals(0, all.size());
        }
        try (DataScanner scan = scan(manager, "SELECT * FROM tblspace1.tsql ", Collections.emptyList())) {
            fail();
        } catch (TableDoesNotExistException ok) {
            assertTrue(manager.getPlanner() instanceof SQLPlanner);
        } catch (StatementExecutionException ok) {
            assertEquals("From line 1, column 15 to line 1, column 28: Object 'TSQL' not found within 'tblspace1'", ok.getMessage());
            assertTrue(manager.getPlanner() instanceof CalcitePlanner);
        }
    }
}
Also used : TransactionResult(herddb.model.TransactionResult) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) StatementExecutionException(herddb.model.StatementExecutionException) TableDoesNotExistException(herddb.model.TableDoesNotExistException) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CalcitePlanner(herddb.sql.CalcitePlanner) SQLPlanner(herddb.sql.SQLPlanner) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 4 with SQLPlanner

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

the class RawSQLTest method limitsTest.

@Test
public void limitsTest() 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());
        // scan performed at "scan time"
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
        }
        if (manager.getPlanner() instanceof SQLPlanner) {
            try (DataScanner scan1 = scan(manager, "SELECT TOP 1 * FROM tblspace1.tsql ORDER BY k1", Collections.emptyList())) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(1, result.size());
                assertEquals(RawString.of("mykey"), result.get(0).get("k1"));
            }
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 1,1", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(RawString.of("mykey2"), result.get(0).get("k1"));
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 1,2", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(2, result.size());
            assertEquals(RawString.of("mykey2"), result.get(0).get("k1"));
            assertEquals(RawString.of("mykey3"), result.get(1).get("k1"));
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 10", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(4, 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"));
            assertEquals(RawString.of("mykey4"), result.get(3).get("k1"));
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 10,10", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(0, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 4,10", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(0, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql ORDER BY k1 LIMIT 3,10", Collections.emptyList())) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(1, result.size());
            assertEquals(RawString.of("mykey4"), result.get(0).get("k1"));
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 3", Collections.emptyList(), 2, TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(2, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?", Arrays.asList(3), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT 1,2", Collections.emptyList(), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(2, result.size());
        }
        if (manager.getPlanner() instanceof CalcitePlanner) {
            try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?,?", Arrays.asList(1, 2), TransactionContext.NO_TRANSACTION)) {
                List<DataAccessor> result = scan1.consume();
                assertEquals(2, result.size());
            }
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "WHERE k1 <> ? LIMIT ?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "WHERE k1 <> ? LIMIT 2,?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(2, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "WHERE k1 <> ? ORDER BY k1 LIMIT ?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT k1, count(*) FROM tblspace1.tsql " + "WHERE k1 <> ? GROUP BY k1 ORDER BY k1 LIMIT ?", Arrays.asList("aaa", 3), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?", Arrays.asList(0), TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(4, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql LIMIT ?", Collections.emptyList(), TransactionContext.NO_TRANSACTION)) {
            fail();
        } catch (MissingJDBCParameterException err) {
            assertEquals(1, err.getIndex());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "ORDER BY k1 LIMIT 3", Collections.emptyList(), 5, TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(3, result.size());
        }
        try (DataScanner scan1 = scan(manager, "SELECT * FROM tblspace1.tsql " + "ORDER BY k1 ", Collections.emptyList(), 2, TransactionContext.NO_TRANSACTION)) {
            List<DataAccessor> result = scan1.consume();
            assertEquals(2, result.size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DataScanner(herddb.model.DataScanner) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) DataAccessor(herddb.utils.DataAccessor) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CalcitePlanner(herddb.sql.CalcitePlanner) RawString(herddb.utils.RawString) SQLPlanner(herddb.sql.SQLPlanner) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 5 with SQLPlanner

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

the class RawSQLTest method currentTimestampTest.

@Test
public void currentTimestampTest() 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,s1 string,t1 timestamp, primary key (t1) )", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,CURRENT_TIMESTAMP)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        Thread.sleep(500);
        assertEquals(1234, scan(manager, "SELECT n1 FROM tblspace1.tsql WHERE t1<CURRENT_TIMESTAMP", Collections.emptyList()).consume().get(0).get("n1"));
        java.sql.Timestamp now = new java.sql.Timestamp(System.currentTimeMillis());
        if (manager.getPlanner() instanceof SQLPlanner) {
            // non standard syntax, needs a decoding
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,'" + RecordSerializer.getUTCTimestampFormatter().format(now.toInstant()) + "')", Arrays.asList("mykey2", Integer.valueOf(1234))).getUpdateCount());
        }
        java.sql.Timestamp now2 = new java.sql.Timestamp(now.getTime() + 1000);
        // standard syntax, but timezone dependant
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1,t1) values(?,?,{ts '" + now2 + "'})", Arrays.asList("mykey3", Integer.valueOf(1234))).getUpdateCount());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) Timestamp(java.sql.Timestamp) RawString(herddb.utils.RawString) SQLPlanner(herddb.sql.SQLPlanner) Timestamp(java.sql.Timestamp) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

SQLPlanner (herddb.sql.SQLPlanner)10 Test (org.junit.Test)10 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)9 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)9 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)9 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)9 RawString (herddb.utils.RawString)7 DataScanner (herddb.model.DataScanner)6 DataAccessor (herddb.utils.DataAccessor)5 Timestamp (java.sql.Timestamp)5 CalcitePlanner (herddb.sql.CalcitePlanner)3 MissingJDBCParameterException (herddb.model.MissingJDBCParameterException)2 StatementExecutionException (herddb.model.StatementExecutionException)2 TransactionContext (herddb.model.TransactionContext)2 ClientConfiguration (herddb.client.ClientConfiguration)1 HDBClient (herddb.client.HDBClient)1 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)1 TableDoesNotExistException (herddb.model.TableDoesNotExistException)1 TransactionResult (herddb.model.TransactionResult)1 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)1