Skip to content

Support all Aggregate Functions as parameter for columns of type AggregateFunction #1232

Description

@tom-clickhouse

Currently, only groupBitmap is supported as parameter for a table column of type AggregateFunction.

It would be good if all aggregate functions are supported.

Activity

  1. changed the title [-]Support Aggregate Functions as parameter for columns of type AggregateFunction[/-] [+]Support all Aggregate Functions as parameter for columns of type AggregateFunction[/+] on Feb 10, 2023
  2. zhicwu commented on Feb 10, 2023

    @zhicwu
    Contributor

    It's in roadmap but we haven't completed refactoring yet. In v0.4.0, serialization/deserialization was refactored with some performance improvement but still not easy to extend. On the other hand, in order to support more AggregateFunction types, since there's no document about the data structures for read and write, we have to dig into ClickHouse code to figure it out one by one, which is going to take a while. Would be great if someone from the server team can document all the details, so that not only Java but all other clients will benefit from that.

  3. Zarathustra2 commented on Apr 28, 2023

    @Zarathustra2

    I just ran into this issue as well. I have a AggregatingMergeTree table with a column of type AggregateFunction(argMin, Decimal(18, 4), DateTime).

    The working around for now seems to be:
    Create a new table with Engine = MergeTree and create a materialized view on top of the table if one has any AggregateFunction functions and wants to insert those columns.

  4. Zarathustra2 commented on May 1, 2023

    @Zarathustra2

    @den-crane Thanks for the neat trick that is cool but unfortunately that does not work for me as I use the JDBC driver in a Flink pipeline, I am getting the error:

    Caused by: java.sql.SQLException: External table, input function, and query parameter cannot be used together in PreparedStatement.
    	at com.clickhouse.jdbc.SqlExceptionUtils.clientError(SqlExceptionUtils.java:73)
    	at com.clickhouse.jdbc.internal.ClickHouseConnectionImpl.prepareStatement(ClickHouseConnectionImpl.java:743)
    	at com.clickhouse.jdbc.ClickHouseConnection.prepareStatement(ClickHouseConnection.java:121)
    	at org.apache.flink.connector.jdbc.internal.executor.SimpleBatchStatementExecutor.prepareStatements(SimpleBatchStatementExecutor.java:58)
    	at org.apache.flink.connector.jdbc.internal.JdbcBatchingOutputFormat.createAndOpenStatementExecutor(JdbcBatchingOutputFormat.java:144)
    	... 12 more
    

    So I assume I have to go with 2 tables for now or write my own sink function

  5. zhicwu commented on May 2, 2023

    @zhicwu
    Contributor

    Hi @Zarathustra2, you don't have to specify parameters when using input function. Just added below test case for demonstration.

    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());
            }
        }
    }
  6. Zarathustra2 commented on May 2, 2023

    @Zarathustra2

    oh wow, @zhicwu, I thought I had to specify the params as well. Now it works!!! :)

    Thanks so much!

  7. github-actions commented on Jan 8, 2025

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last year. It will be closed in 30 days if no further activity occurs. Please feel free to leave a comment if you believe the issue is still relevant. Thank you for your contributions!

  8. added
    data-types
    on Jan 10, 2025
  9. added
    jdbc
    on Nov 14, 2025
  10. github-actions commented on Nov 15, 2026

    @github-actions

    This issue has been automatically marked as stale because it has not had activity in the last year. It will be closed in 30 days if no further activity occurs. Please feel free to leave a comment if you believe the issue is still relevant. Thank you for your contributions!

  11. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Close — duplicate of #2599
    #2599 asks for the same in client-v2 and jdbc-v2, with a newer analysis on main. Keep #2599 open.

    • Category: Feature
    • Classification: impact: none · area: Data types & formats (AggregateFunction) · native: format
    • Status on main: Not supported on main — at 8480ca001, ClickHouse 26.11.4.11, a read of a column AggregateFunction(argMin, Decimal(18, 4), DateTime) (the type in the thread) fails in jdbc-v2, and in client-v2 with RowBinaryWithNamesAndTypes and with Native: Failed to read value for column m.
    • Breaking change: No — can be done without a break, if groupBitmap states still read as ClickHouseBitmap.
    • Duplicates / related: Related: In the JDBC query result set, columns of type AggregateFunction cannot be parsed normally #2662 — some other states are read as groupBitmap with no error.

    Analysis: The client-v2 reader decodes each AggregateFunction state as groupBitmap (BinaryStreamReader.java:292), and the writer rejects all other functions (SerializerUtils.java:1052). The v1 code of the issue link has the same limit on main (ClickHouseRowBinaryProcessor.java:574 and :734).

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  12. added
    client
    native-format
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clientdata-typesduplicateenhancementjdbcnative-formatstale

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions