# KSQL - Add new field to existing struct

**URL:** https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030
**Category:** ksqlDB
**Created:** [25 February 2021 10:28 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030 "2021-02-25T10:28:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![harinath-sirigiri](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/harinath-sirigiri/32/541_2.png) [@harinath-sirigiri](https://forum.confluent.io/u/harinath-sirigiri)
#### Post date: [25 February 2021 10:28 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030/1 "2021-02-25T10:28:38Z")

</div>

A stream contains struct and a string field

```auto
ksql> select * from t_accnt emit changes;
+----------------------------------------------+----------------------------------------------+
|ACCOUNT |OPERATION |
+----------------------------------------------+----------------------------------------------+
|{ACCNT_ID=1234, A1=10, A2=20, A3=30, A4=40} |null |
|{ACCNT_ID=1234, A1=10, A2=20, A3=30, A4=40} |update |

```

I want to achieve the below output in KSQL, basically adding a new field to existing structure (or create new structure)

```auto
{ACCNT_ID=1234, A1=10, A2=20, A3=30, A4=40,OPERATION=update}

```

---

<div class="post-metadata">

### Author: ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)
#### Post date: [25 February 2021 13:32 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030/2 "2021-02-25T13:32:58Z")

</div>

Here’s one way to do it, although it’s not particularly elegant:

```sql
-- Set up test stream
CREATE STREAM TMP (ACCOUNT STRUCT<ID INT, A1 INT, A2 INT>,
                   OPERATION VARCHAR) 
  WITH (KAFKA_TOPIC='test', FORMAT='JSON', PARTITIONS=1);

INSERT INTO TMP VALUES (STRUCT(ID:=1,A1:=2,A2:=3),'UPDATE');
INSERT INTO TMP VALUES (STRUCT(ID:=2,A1:=22,A2:=23),'INSERT');

-- Query data

ksql> SELECT * FROM TMP EMIT CHANGES LIMIT 2;
+----------------------+----------+
|ACCOUNT |OPERATION |
+----------------------+----------+
|{ID=1, A1=2, A2=3} |UPDATE |
|{ID=2, A1=22, A2=23} |INSERT |
Limit Reached
Query terminated

-- Add `OPERATION` into `ACCOUNT` struct
ksql> SELECT STRUCT("ID":=ACCOUNT->ID,
                    "A1":=ACCOUNT->A1,
                    "A2":=ACCOUNT->A2,
                    "OPERATION":=OPERATION) AS ACCOUNT 
         FROM TMP 
       EMIT CHANGES LIMIT 2;
+---------------------------------------+
|ACCOUNT |
+---------------------------------------+
|{ID=1, A1=2, A2=3, OPERATION=UPDATE} |
|{ID=2, A1=22, A2=23, OPERATION=INSERT} |
Limit Reached
Query terminated

```

---

<div class="post-metadata">

### Author: ![harinath-sirigiri](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/harinath-sirigiri/32/541_2.png) [@harinath-sirigiri](https://forum.confluent.io/u/harinath-sirigiri)
#### Post date: [25 February 2021 14:25 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030/3 "2021-02-25T14:25:28Z")

</div>

Hi Robin,

Thank you very much for this , Actually I already tried this forming new struct with limited source struct fields but in my real case i have 50+ fields in source struct and may schema change in future.

Is there any way to explode source struct like ACCNT-\>\* which helps easy to from new struct

Any other alternative /suggestion apart from UDF for this.

---

<div class="post-metadata">

### Author: ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)
#### Post date: [25 February 2021 15:57 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030/4 "2021-02-25T15:57:16Z")

</div>

You can follow this issue: [Add capability to alter a struct in query · Issue #6743 · confluentinc/ksql · GitHub](https://github.com/confluentinc/ksql/issues/6743)

---

<div class="post-metadata">

### Author: ![harinath-sirigiri](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/harinath-sirigiri/32/541_2.png) [@harinath-sirigiri](https://forum.confluent.io/u/harinath-sirigiri)
#### Post date: [25 February 2021 16:09 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030/5 "2021-02-25T16:09:13Z")

</div>

Sure Many Thanks Robin !!

---

<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: [4 March 2021 16:09 UTC](https://forum.confluent.io/t/ksql-add-new-field-to-existing-struct/1030/6 "2021-03-04T16:09:20Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
