From e3d784725fd6c2c4a434edd0660a04875f3dcf6c Mon Sep 17 00:00:00 2001 From: Zhichun Wu Date: Tue, 2 May 2023 21:07:10 +0800 Subject: [PATCH] Add tests to cover insert AggregateFunction and column with default value --- .../jdbc/ClickHousePreparedStatementTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java index a9301c4a5..d8d47b275 100644 --- a/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java +++ b/clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHousePreparedStatementTest.java @@ -1233,6 +1233,38 @@ public void testNonBatchUpdate(String mode, String query) throws SQLException { } } + @Test(groups = "integration") + public void testInsertAggregateFunction() throws SQLException { + // https://kb.altinity.com/altinity-kb-schema-design/ingestion-aggregate-function/ + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props); + Statement s = conn.createStatement(); + PreparedStatement ps = conn.prepareStatement( + "insert into test_insert_aggregate_function SELECT uid, updated, arrayReduce('argMaxState', [name], [updated]) " + + "FROM input('uid Int16, updated DateTime, name String')")) { + s.execute("drop table if exists test_insert_aggregate_function;" + + "CREATE TABLE test_insert_aggregate_function (uid Int16, updated SimpleAggregateFunction(max, DateTime), " + + "name AggregateFunction(argMax, String, DateTime)) ENGINE=AggregatingMergeTree order by uid"); + ps.setInt(1, 1); + ps.setString(2, "2020-01-02 00:00:00"); + ps.setString(3, "b"); + ps.addBatch(); + ps.setInt(1, 1); + ps.setString(2, "2020-01-01 00:00:00"); + ps.setString(3, "a"); + ps.addBatch(); + ps.executeBatch(); + try (ResultSet rs = s.executeQuery( + "select uid, max(updated) AS updated, argMaxMerge(name) from test_insert_aggregate_function group by uid")) { + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getInt(1), 1); + Assert.assertEquals(rs.getString(2), "2020-01-02 00:00:00"); + Assert.assertEquals(rs.getString(3), "b"); + Assert.assertFalse(rs.next()); + } + } + } + @Test(groups = "integration") public void testInsertByteArray() throws SQLException { Properties props = new Properties(); @@ -1257,6 +1289,34 @@ public void testInsertByteArray() throws SQLException { } } + @Test(groups = "integration") + public void testInsertDefaultValue() throws SQLException { + Properties props = new Properties(); + try (ClickHouseConnection conn = newConnection(props); + Statement s = conn.createStatement(); + PreparedStatement ps = conn.prepareStatement( + "insert into test_insert_default_value select id, name from input('id UInt32, name Nullable(String)')")) { + s.execute("drop table if exists test_insert_default_value;" + + "create table test_insert_default_value(n Int32, s String DEFAULT 'secret') engine=Memory"); + ps.setInt(1, 1); + ps.setString(2, null); + ps.addBatch(); + ps.setInt(1, -1); + ps.setNull(2, Types.ARRAY); + ps.addBatch(); + ps.executeBatch(); + try (ResultSet rs = s.executeQuery("select * from test_insert_default_value order by n")) { + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getInt(1), -1); + Assert.assertEquals(rs.getString(2), "secret"); + Assert.assertTrue(rs.next()); + Assert.assertEquals(rs.getInt(1), 1); + Assert.assertEquals(rs.getString(2), "secret"); + Assert.assertFalse(rs.next()); + } + } + } + @Test(groups = "integration") public void testOutFileAndInFile() throws SQLException { if (DEFAULT_PROTOCOL != ClickHouseProtocol.HTTP) {