{
  "comments": [
    "Tests around session windows."
  ],
  "tests": [
    {
      "name": "max session",
      "statements": [
        "CREATE STREAM TEST (ID bigint, NAME varchar, VALUE bigint) WITH (kafka_topic='test_topic', value_format='DELIMITED', key='ID');",
        "CREATE TABLE S2 as SELECT id, max(value) FROM test WINDOW SESSION (30 SECONDS) group by id;"
      ],
      "inputs": [
        {"topic": "test_topic", "key": 0, "value": "0,zero,0", "timestamp": 0},
        {"topic": "test_topic", "key": 0, "value": "0,100,5", "timestamp": 10000},
        {"topic": "test_topic", "key": 1, "value": "1,100,100", "timestamp": 10000},
        {"topic": "test_topic", "key": 1, "value": "1,100,200", "timestamp": 40000}
      ],
      "outputs": [
        {"topic": "S2", "key": 0, "value": "0,0", "timestamp": 0, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "S2", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "S2", "key": 0, "value": "0,5", "timestamp": 10000, "window": {"start": 0, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": "1,100", "timestamp": 10000, "window": {"start": 10000, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": null, "timestamp": 40000, "window": {"start": 10000, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": "1,200", "timestamp": 40000, "window": {"start": 10000, "end": 40000, "type": "session"}}
      ],
      "properties": {
        "ksql.windowed.session.key.legacy": "false"
      }
    },
    {
      "name": "import session table",
      "statements": [
        "CREATE TABLE TEST (ID bigint, VALUE bigint) WITH (kafka_topic='test_topic', value_format='DELIMITED', KEY='ID', WINDOW_TYPE='SeSSion');",
        "CREATE TABLE S2 as SELECT *, ROWKEY as KEY FROM test;"
      ],
      "inputs": [
        {"topic": "test_topic", "key": 0, "value": "0,0", "timestamp": 10, "window": {"start": 10, "end": 10, "type": "session"}},
        {"topic": "test_topic", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 10, "end": 10, "type": "session"}},
        {"topic": "test_topic", "key": 0, "value": "0,5", "timestamp": 10000, "window": {"start": 10, "end": 10000, "type": "session"}},
        {"topic": "test_topic", "key": 1, "value": "1,100", "timestamp": 10000, "window": {"start": 800, "end": 10000, "type": "session"}},
        {"topic": "test_topic", "key": 1, "value": "1,200", "timestamp": 40000, "window": {"start": 800, "end": 40000, "type": "session"}}
      ],
      "outputs": [
        {"topic": "S2", "key": 0, "value": "0,0,0 : Window{start=10 end=10}", "timestamp": 10, "window": {"start": 10, "end": 10, "type": "session"}},
        {"topic": "S2", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 10, "end": 10, "type": "session"}},
        {"topic": "S2", "key": 0, "value": "0,5,0 : Window{start=10 end=10000}", "timestamp": 10000, "window": {"start": 10, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": "1,100,1 : Window{start=800 end=10000}", "timestamp": 10000, "window": {"start": 800, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": "1,200,1 : Window{start=800 end=40000}", "timestamp": 40000, "window": {"start": 800, "end": 40000, "type": "session"}}
      ]
    },
    {
      "name": "import session stream",
      "statements": [
        "CREATE STREAM TEST (ID bigint, VALUE bigint) WITH (kafka_topic='test_topic', value_format='DELIMITED', WINDOW_TYPE='SESSION');",
        "CREATE STREAM S2 as SELECT *, ROWKEY as Key FROM test;"
      ],
      "inputs": [
        {"topic": "test_topic", "key": 0, "value": "0,0", "timestamp": 0, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "test_topic", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "test_topic", "key": 0, "value": "0,5", "timestamp": 10000, "window": {"start": 0, "end": 10000, "type": "session"}},
        {"topic": "test_topic", "key": 1, "value": "1,100", "timestamp": 10000, "window": {"start": 10000, "end": 10000, "type": "session"}},
        {"topic": "test_topic", "key": 1, "value": "1,200", "timestamp": 40000, "window": {"start": 10000, "end": 40000, "type": "session"}}
      ],
      "outputs": [
        {"topic": "S2", "key": 0, "value": "0,0,0 : Window{start=0 end=0}", "timestamp": 0, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "S2", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "S2", "key": 0, "value": "0,5,0 : Window{start=0 end=10000}", "timestamp": 10000, "window": {"start": 0, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": "1,100,1 : Window{start=10000 end=10000}", "timestamp": 10000, "window": {"start": 10000, "end": 10000, "type": "session"}},
        {"topic": "S2", "key": 1, "value": "1,200,1 : Window{start=10000 end=40000}", "timestamp": 40000, "window": {"start": 10000, "end": 40000, "type": "session"}}
      ]
    },
    {
      "name": "legacy session",
      "comment": [
        "in version 5.1 and before the key of session windowed data was actually a TimeWindow, ",
        "i.e. it serialized with no end time. This was fixed in later versions, but this breaks ",
        "compatibility with data running in older queries."
      ],
      "properties": {
        "ksql.windowed.session.key.legacy": "true"
      },
      "statements": [
        "CREATE STREAM TEST (ID bigint, NAME varchar, VALUE bigint) WITH (kafka_topic='test_topic', value_format='DELIMITED', key='ID');",
        "CREATE TABLE S2 as SELECT id, max(value) FROM test WINDOW SESSION (30 SECONDS) group by id;"
      ],
      "inputs": [
        {"topic": "test_topic", "key": 0, "value": "0,zero,0", "timestamp": 0},
        {"topic": "test_topic", "key": 0, "value": "0,100,5", "timestamp": 10000},
        {"topic": "test_topic", "key": 1, "value": "1,100,100", "timestamp": 10000},
        {"topic": "test_topic", "key": 1, "value": "1,100,200", "timestamp": 40000}
      ],
      "outputs": [
        {"topic": "S2", "key": 0, "value": "0,0", "timestamp": 0, "window": {"start": 0, "end": 30000, "type": "time"}},
        {"topic": "S2", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 0, "end": 30000, "type": "time"}},
        {"topic": "S2", "key": 0, "value": "0,5", "timestamp": 10000, "window": {"start": 0, "end": 40000, "type": "time"}},
        {"topic": "S2", "key": 1, "value": "1,100", "timestamp": 10000, "window": {"start": 10000, "end": 40000, "type": "time"}},
        {"topic": "S2", "key": 1, "value": null, "timestamp": 40000, "window": {"start": 10000, "end": 40000, "type": "time"}},
        {"topic": "S2", "key": 1, "value": "1,200", "timestamp": 40000, "window": {"start": 10000, "end": 70000, "type": "time"}}
      ]
    },
    {
      "name": "import session stream and group by",
      "statements": [
        "CREATE STREAM TEST (ID bigint, VALUE bigint) WITH (kafka_topic='test_topic', value_format='DELIMITED', WINDOW_TYPE='SESSION');",
        "CREATE TABLE S2 as SELECT ROWKEY as Id, count(*) FROM test GROUP BY ROWKEY;"
      ],
      "inputs": [
        {"topic": "test_topic", "key": 0, "value": "0,0", "timestamp": 0, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "test_topic", "key": 0, "value": null, "timestamp": 10000, "window": {"start": 0, "end": 0, "type": "session"}},
        {"topic": "test_topic", "key": 0, "value": "0,5", "timestamp": 10000, "window": {"start": 0, "end": 10000, "type": "session"}},
        {"topic": "test_topic", "key": 1, "value": "1,100", "timestamp": 10000, "window": {"start": 10000, "end": 10000, "type": "session"}},
        {"topic": "test_topic", "key": 1, "value": "1,200", "timestamp": 10000, "window": {"start": 10000, "end": 10000, "type": "session"}}
      ],
      "outputs": [
        {"topic": "S2", "key": "0 : Window{start=0 end=0}", "value": "0 : Window{start=0 end=0},1", "timestamp": 0},
        {"topic": "S2", "key": "0 : Window{start=0 end=10000}", "value": "0 : Window{start=0 end=10000},1", "timestamp": 10000},
        {"topic": "S2", "key": "1 : Window{start=10000 end=10000}", "value": "1 : Window{start=10000 end=10000},1", "timestamp": 10000},
        {"topic": "S2", "key": "1 : Window{start=10000 end=10000}", "value": "1 : Window{start=10000 end=10000},2", "timestamp": 10000}
      ]
    }
  ]
}