# Aggregation for complex structures

**URL:** <https://forum.confluent.io/t/aggregation-for-complex-structures/1844>\
**Category:** ksqlDB\
**Created:** [21 May 2021 07:20 UTC](https://forum.confluent.io/t/aggregation-for-complex-structures/1844 "2021-05-21T07:20:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel.cortinovis](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@daniel.cortinovis](https://forum.confluent.io/u/daniel.cortinovis)\
**Post date:** [21 May 2021 07:20 UTC](https://forum.confluent.io/t/aggregation-for-complex-structures/1844/1 "2021-05-21T07:20:50Z")

</div>

Hi all,  
working with ksqldb 0.17.0,  
We’re asking how we could achieve aggregation of lines with nested ARRAY AND STRUCT.  
An example below :

```auto
CREATE STREAM STREAM1 (
  NUMBER VARCHAR KEY,
  TIME VARCHAR,
  PRIORITY VARCHAR
) WITH (
  kafka_topic = 'stream1',
  partitions = 1,
  value_format = 'avro'
);

```

```auto
DESCRIBE STREAM1;

Name : STREAM1
 Field | Type
------------------------------------------------
 NUMBER | VARCHAR(STRING) (key)
 TIME | VARCHAR(STRING)
 PRIORITY | VARCHAR(STRING)

INSERT INTO STREAM1 (NUMBER, TIME, PRIORITY) VALUES ('NUMBER1', '24-09-2020 17:29:57', 'High');
INSERT INTO STREAM1 (NUMBER, TIME, PRIORITY) VALUES ('NUMBER2', '25-10-2021 18:30:58 ', 'Low');

SELECT * FROM STREAM1 EMIT CHANGES;

+------------------------------------------------------+------------------------------------------------------+------------------------------------------------------+
|NUMBER |TIME |PRIORITE |
+------------------------------------------------------+------------------------------------------------------+------------------------------------------------------+
|NUMBER1 |24-09-2020 17:29:57 |High |
|NUMBER2 |25-10-2021 18:30:58 |Low |

SELECT NUMBER, 
 ARRAY[
   STRUCT(columnId := 0, text := NUMBER), 
   STRUCT(columnId := 1, text := TIME ), 
   STRUCT(columnId := 2, text := PRIORITY ) 
] AS dataArray FROM STREAM1 EMIT CHANGES;

```

The DATAARRAY produced for each line looks like :

```auto
[{COLUMNID=0, TEXT=NUMBER1}, {COLUMNID=1, TEXT=24-09-2020 17:29:57}, {COLUMNID=2, TEXT=High}]

```

Would it be possible to aggregate the content of all lines in an additional ARRAY, based on some defined criteria (number of unread lines from topic, windowing frames, …)  
to obtain this final structure (ARRAY\<ARRAY\<STRUCT\<columnID INT, text VARCHAR\>\>\> ) ?

```auto
[
[{COLUMNID=0, TEXT=NUMBER1}, {COLUMNID=1, TEXT=24-09-2020 17:29:57}, {COLUMNID=2, TEXT=High}], 
[{COLUMNID=0, TEXT=NUMBER2}, {COLUMNID=1, TEXT=25-10-2021 18:30:58}, {COLUMNID=2, TEXT=Low}]
]

```

Unfortunately, the COLLECT\_LIST function only works with basic types.

Regards,

---

<div class="post-metadata">

**Author:** ![daveklein](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/daveklein/32/140_2.png) [@daveklein](https://forum.confluent.io/u/daveklein)\
**Post date:** [21 May 2021 16:32 UTC](https://forum.confluent.io/t/aggregation-for-complex-structures/1844/2 "2021-05-21T16:32:48Z")

</div>

Hi Daniel,

I wonder if ksqlDB’s Lambda functions might do what you need. Take a look at this guide: [How to transform columns with structured data. - ksqlDB Documentation](https://docs.ksqldb.io/en/latest/how-to-guides/use-lambda-functions/)

If you’re already tried that and it’s not what you need, post back and we’ll keep the conversation going.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![daniel.cortinovis](https://avatars.discourse-cdn.com/v4/letter/d/f07891/32.png) [@daniel.cortinovis](https://forum.confluent.io/u/daniel.cortinovis)\
**Post date:** [24 May 2021 06:44 UTC](https://forum.confluent.io/t/aggregation-for-complex-structures/1844/3 "2021-05-24T06:44:12Z")

</div>

Thanks Dave,  
I’ve looked to lambdas, but for the moment couldn’t achieve simply what I wanted to do, since lambdas work on complex structures on each line, while I’d need to aggregate data from the same column (pretty much like COLLECT\_LIST works)  
Regards,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [20 June 2021 07:21 UTC](https://forum.confluent.io/t/aggregation-for-complex-structures/1844/4 "2021-06-20T07:21:36Z")

</div>

Closed on 20 June 2021 07:21 UTC

This topic was automatically closed after 30 days. New replies are no longer allowed.
