简体   繁体   中英

How to add group_concat to hsqldb with distinct?

I am trying to add the group_concat function to hsqldb so that I can properly test a query as a unit/integration test. The query works fine in mysql, so I need it to work in hsqldb (hopefully).

        // GROUP_CONCAT
        jdbcTemplate.update("DROP FUNCTION GROUP_CONCAT IF EXISTS;");
        jdbcTemplate.update(
            "create aggregate function group_concat(in val varchar(100), in flag boolean, inout buffer varchar(1000), inout counter int) " +
            "   returns varchar(1000) " +
            "   contains sql " +
            "begin atomic " +
            "   if flag then" +
            "      return buffer;" +
            "   else" +
            "      if val is null then return null; end if;" +
            "      if buffer is null then set buffer = ''; end if;" +
            "      if counter is null then set counter = 0; end if;" +
            "      if counter > 0 then set buffer = buffer || ','; end if;" +
            "      set buffer = buffer + val;" +
            "      set counter = counter + 1;" +
            "      return null;" +
            "   end if;" +
            "end;"
        );

Adding this aggregation function solves most of the problem. It will correctly behave like mysql's group_concat. However, what it won't do is let me use the distinct keyword like this:

group_concat(distinct column)

Is there any way to factor in the distinct keyword? Or do I rewrite the query to avoid the distinct keyword altogether?

HSQLDB has built-in GROUP_CONCAT and accepts DISTINCT.

http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_aggregate_funcs

At the moment you cannot add DISTINCT to a user-defined aggregate function, but this looks like an interesting feature to allow in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM