Search in sources :

Example 51 with StringSink

use of io.questdb.std.str.StringSink in project questdb by bluestreak01.

the class PGJobContextTest method testUnsupportedParameterType.

@Test
public void testUnsupportedParameterType() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, false);
            final PreparedStatement statement = connection.prepareStatement("select x, ? from long_sequence(5)")) {
            // TIME is passed over protocol as UNSPECIFIED type
            // it will rely on date parser to work out what it is
            // for now date parser does not parse just time, it could i guess if required.
            statement.setTime(1, new Time(100L));
            try (ResultSet rs = statement.executeQuery()) {
                StringSink sink = new StringSink();
                // dump metadata
                assertResultSet("x[BIGINT],$1[VARCHAR]\n" + "1,00:00:00.1+00\n" + "2,00:00:00.1+00\n" + "3,00:00:00.1+00\n" + "4,00:00:00.1+00\n" + "5,00:00:00.1+00\n", sink, rs);
            }
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 52 with StringSink

use of io.questdb.std.str.StringSink in project questdb by bluestreak01.

the class PGJobContextTest method testRegularBatchInsertMethod.

@Test
public void testRegularBatchInsertMethod() throws Exception {
    assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, true)) {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("create table test_batch(id long,val int)");
            }
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into test_batch(id,val) values(?,?)")) {
                batchInsert.setLong(1, 0L);
                batchInsert.setInt(2, 1);
                batchInsert.addBatch();
                batchInsert.clearParameters();
                batchInsert.setLong(1, 1L);
                batchInsert.setInt(2, 2);
                batchInsert.addBatch();
                batchInsert.clearParameters();
                batchInsert.setLong(1, 2L);
                batchInsert.setInt(2, 3);
                batchInsert.addBatch();
                int[] a = batchInsert.executeBatch();
                Assert.assertEquals(3, a.length);
                Assert.assertEquals(1, a[0]);
                Assert.assertEquals(1, a[1]);
                Assert.assertEquals(1, a[2]);
            }
            StringSink sink = new StringSink();
            String expected = "id[BIGINT],val[INTEGER]\n" + "0,1\n" + "1,2\n" + "2,3\n";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select * from test_batch");
            assertResultSet(expected, sink, rs);
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 53 with StringSink

use of io.questdb.std.str.StringSink in project questdb by bluestreak01.

the class PGJobContextTest method testBatchInsertWithTransaction.

@Test
public void testBatchInsertWithTransaction() throws Exception {
    assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, true)) {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("create table test (id long,val int)");
                statement.executeUpdate("create table test2(id long,val int)");
            }
            connection.setAutoCommit(false);
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into test(id,val) values(?,?)")) {
                batchInsert.setLong(1, 0L);
                batchInsert.setInt(2, 1);
                batchInsert.addBatch();
                batchInsert.setLong(1, 1L);
                batchInsert.setInt(2, 2);
                batchInsert.addBatch();
                batchInsert.setLong(1, 2L);
                batchInsert.setInt(2, 3);
                batchInsert.addBatch();
                batchInsert.clearParameters();
                batchInsert.executeLargeBatch();
            }
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into test2(id,val) values(?,?)")) {
                batchInsert.setLong(1, 0L);
                batchInsert.setInt(2, 1);
                batchInsert.addBatch();
                batchInsert.setLong(1, 1L);
                batchInsert.setInt(2, 2);
                batchInsert.addBatch();
                batchInsert.setLong(1, 2L);
                batchInsert.setInt(2, 3);
                batchInsert.addBatch();
                batchInsert.clearParameters();
                batchInsert.executeLargeBatch();
            }
            connection.commit();
            connection.setAutoCommit(true);
            StringSink sink = new StringSink();
            String expected = "id[BIGINT],val[INTEGER]\n" + "0,1\n" + "1,2\n" + "2,3\n";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select * from test");
            assertResultSet(expected, sink, rs);
            sink.clear();
            Statement statement2 = connection.createStatement();
            ResultSet rs2 = statement2.executeQuery("select * from test2");
            assertResultSet(expected, sink, rs2);
            // now switch on autocommit and check that data is inserted without explicitly calling commit()
            connection.setAutoCommit(true);
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into test(id,val) values(?,?)")) {
                batchInsert.setLong(1, 3L);
                batchInsert.setInt(2, 4);
                batchInsert.addBatch();
                batchInsert.setLong(1, 4L);
                batchInsert.setInt(2, 5);
                batchInsert.addBatch();
                batchInsert.setLong(1, 5L);
                batchInsert.setInt(2, 6);
                batchInsert.addBatch();
                batchInsert.clearParameters();
                batchInsert.executeLargeBatch();
            }
            sink.clear();
            expected = "id[BIGINT],val[INTEGER]\n" + "0,1\n" + "1,2\n" + "2,3\n" + "3,4\n" + "4,5\n" + "5,6\n";
            Statement statement3 = connection.createStatement();
            ResultSet rs3 = statement3.executeQuery("select * from test");
            assertResultSet(expected, sink, rs3);
            // now fail insertion during transaction
            try (Statement statement4 = connection.createStatement()) {
                statement4.executeUpdate("create table anothertab(id long, val int, k timestamp) timestamp(k) ");
            }
            connection.setAutoCommit(false);
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into anothertab(id, val, k) values(?,?,?)")) {
                batchInsert.setLong(1, 3L);
                batchInsert.setInt(2, 4);
                batchInsert.setLong(3, 1_000L);
                batchInsert.addBatch();
                batchInsert.setLong(1, 4L);
                batchInsert.setInt(2, 5);
                batchInsert.setLong(3, 0L);
                batchInsert.addBatch();
                batchInsert.setLong(1, 5L);
                batchInsert.setInt(2, 6);
                batchInsert.setLong(3, 2_000L);
                batchInsert.addBatch();
                batchInsert.clearParameters();
                batchInsert.executeLargeBatch();
                Assert.fail();
            } catch (Exception e) {
                LOG.error().$(e).$();
            }
            // now transaction fail, we should rollback transaction
            connection.rollback();
            connection.setAutoCommit(true);
            sink.clear();
            expected = "id[BIGINT],val[INTEGER],k[TIMESTAMP]\n";
            Statement statement4 = connection.createStatement();
            ResultSet rs4 = statement4.executeQuery("select * from anothertab");
            assertResultSet(expected, sink, rs4);
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) PSQLException(org.postgresql.util.PSQLException) IOException(java.io.IOException) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 54 with StringSink

use of io.questdb.std.str.StringSink in project questdb by bluestreak01.

the class PGJobContextTest method testMultiplePreparedStatements.

@Test
public void testMultiplePreparedStatements() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, false)) {
            PreparedStatement ps1 = connection.prepareStatement("select 1,2,3 from long_sequence(1)");
            PreparedStatement ps2 = connection.prepareStatement("select 4,5,6 from long_sequence(1)");
            PreparedStatement ps3 = connection.prepareStatement("select 7,8,9 from long_sequence(2)");
            final String expected = "1[INTEGER],2[INTEGER],3[INTEGER]\n" + "1,2,3\n";
            StringSink sink = new StringSink();
            for (int i = 0; i < 10; i++) {
                sink.clear();
                ResultSet rs1 = ps1.executeQuery();
                ResultSet rs2 = ps2.executeQuery();
                ResultSet rs3 = ps3.executeQuery();
                assertResultSet(expected, sink, rs1);
                rs1.close();
                rs2.close();
                rs3.close();
            }
            Statement statement1 = connection.createStatement();
            for (int i = 0; i < 10; i++) {
                PreparedStatement s = connection.prepareStatement("select 2,2,2,2 from long_sequence(1)");
                s.executeQuery();
                statement1.executeQuery("select 1 from long_sequence(2)");
            }
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 55 with StringSink

use of io.questdb.std.str.StringSink in project questdb by bluestreak01.

the class PGJobContextTest method testLargeBatchInsertMethod.

@Test
public void testLargeBatchInsertMethod() throws Exception {
    assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(4);
            final Connection connection = getConnection(false, true)) {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate("create table test_large_batch(id long,val int)");
            }
            connection.setAutoCommit(false);
            try (PreparedStatement batchInsert = connection.prepareStatement("insert into test_large_batch(id,val) values(?,?)")) {
                for (int i = 0; i < 50_000; i++) {
                    batchInsert.clearParameters();
                    batchInsert.setLong(1, 0L);
                    batchInsert.setInt(2, 1);
                    batchInsert.addBatch();
                    batchInsert.clearParameters();
                    batchInsert.setLong(1, 1L);
                    batchInsert.setInt(2, 2);
                    batchInsert.addBatch();
                    batchInsert.clearParameters();
                    batchInsert.setLong(1, 2L);
                    batchInsert.setInt(2, 3);
                    batchInsert.addBatch();
                }
                batchInsert.executeBatch();
                connection.commit();
            }
            StringSink sink = new StringSink();
            String expected = "count[BIGINT]\n" + "150000\n";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select count(*) from test_large_batch");
            assertResultSet(expected, sink, rs);
        }
    });
}
Also used : BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Aggregations

StringSink (io.questdb.std.str.StringSink)284 Test (org.junit.Test)167 Function (io.questdb.cairo.sql.Function)38 BaseConnection (org.postgresql.core.BaseConnection)36 UnaryFunction (io.questdb.griffin.engine.functions.UnaryFunction)28 StrConstant (io.questdb.griffin.engine.functions.constants.StrConstant)22 StrFunction (io.questdb.griffin.engine.functions.StrFunction)20 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)16 Path (io.questdb.std.str.Path)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 CyclicBarrier (java.util.concurrent.CyclicBarrier)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 SqlCompiler (io.questdb.griffin.SqlCompiler)10 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)10 SqlException (io.questdb.griffin.SqlException)9 Metrics (io.questdb.Metrics)8 io.questdb.cairo (io.questdb.cairo)8 AllowAllCairoSecurityContext (io.questdb.cairo.security.AllowAllCairoSecurityContext)8 NetUtils (io.questdb.cutlass.NetUtils)8