{
  "comments": [
    "Tests covering the use of the SUBSTRING function.",
    "NOTE: At present the SUBSTRING function has two modes: current and legacy.",
    " v5.0 and earlier only have legacy mode.",
    " v5.1 and later have both current and legacy. New queries default to the current mode"
  ],
  "tests": [
    {
      "name": "do substring with just pos",
      "format": ["JSON"],
      "properties": {
        "ksql.functions.substring.legacy.args": false
      },
      "statements": [
        "CREATE STREAM TEST (source VARCHAR) WITH (kafka_topic='test_topic', value_format='JSON');",
        "CREATE STREAM OUTPUT AS SELECT SUBSTRING(source, 6) AS SUBSTRING, SUBSTRING(null, 1) AS NULL_STR, SUBSTRING(source, null) AS NULL_POS FROM TEST;"
      ],
      "inputs": [
        {"topic": "test_topic", "value": {"source": "some_string"}},
        {"topic": "test_topic", "value": {"source": "another"}},
        {"topic": "test_topic", "value": {"source": "short"}}
      ],
      "outputs": [
        {"topic": "OUTPUT", "value": {"SUBSTRING":"string", "NULL_STR":null, "NULL_POS":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"er", "NULL_STR":null, "NULL_POS":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"", "NULL_STR":null, "NULL_POS":null}}
      ]
    },
    {
      "name": "do substring with pos and length",
      "format": ["JSON"],
      "properties": {
        "ksql.functions.substring.legacy.args": false
      },"statements": [
        "CREATE STREAM TEST (source VARCHAR) WITH (kafka_topic='test_topic', value_format='JSON');",
        "CREATE STREAM OUTPUT AS SELECT SUBSTRING(source, 6, 3) AS SUBSTRING, SUBSTRING(null, 1) AS NULL_STR, SUBSTRING(source, null) AS NULL_POS, SUBSTRING(source, 6, null) AS NULL_LEN FROM TEST;"
      ],
      "inputs": [
        {"topic": "test_topic", "value": {"source": "some_string"}},
        {"topic": "test_topic", "value": {"source": "another"}},
        {"topic": "test_topic", "value": {"source": "short"}}
      ],
      "outputs": [
        {"topic": "OUTPUT", "value": {"SUBSTRING":"str", "NULL_STR":null, "NULL_POS":null, "NULL_LEN":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"er", "NULL_STR":null, "NULL_POS":null, "NULL_LEN":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"", "NULL_STR":null, "NULL_POS":null, "NULL_LEN":null}}
      ]
    },
    {
      "name": "do substring in legacy mode with just start index",
      "format": ["JSON"],
      "properties": {
        "ksql.functions.substring.legacy.args": true
      },
      "statements": [
        "CREATE STREAM TEST (source VARCHAR) WITH (kafka_topic='test_topic', value_format='JSON');",
        "CREATE STREAM OUTPUT AS SELECT SUBSTRING(source, 5) AS SUBSTRING, SUBSTRING(null, 1) AS NULL_STR, SUBSTRING(source, null) AS NULL_START FROM TEST;"
      ],
      "inputs": [
        {"topic": "test_topic", "value": {"source": "some_string"}},
        {"topic": "test_topic", "value": {"source": "another"}},
        {"topic": "test_topic", "value": {"source": "short"}}
      ],
      "outputs": [
        {"topic": "OUTPUT", "value": {"SUBSTRING":"string", "NULL_STR":null, "NULL_START":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"er", "NULL_STR":null, "NULL_START":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"", "NULL_STR":null, "NULL_START":null}}
      ]
    },
    {
      "name": "do substring in legacy mode with start and end indexes",
      "format": ["JSON"],
      "properties": {
        "ksql.functions.substring.legacy.args": true
      },
      "statements": [
        "CREATE STREAM TEST (source VARCHAR) WITH (kafka_topic='test_topic', value_format='JSON');",
        "CREATE STREAM OUTPUT AS SELECT SUBSTRING(source, 1, 6) AS SUBSTRING, SUBSTRING(null, 1) AS NULL_STR, SUBSTRING(source, null) AS NULL_START, SUBSTRING(source, 6, null) AS NULL_END FROM TEST;"
      ],
      "inputs": [
        {"topic": "test_topic", "value": {"source": "some_string"}},
        {"topic": "test_topic", "value": {"source": "another"}},
        {"topic": "test_topic", "value": {"source": "short"}}
      ],
      "outputs": [
        {"topic": "OUTPUT", "value": {"SUBSTRING":"ome_s", "NULL_STR":null, "NULL_START":null, "NULL_END":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":"nothe", "NULL_STR":null, "NULL_START":null, "NULL_END":null}},
        {"topic": "OUTPUT", "value": {"SUBSTRING":null, "NULL_STR":null, "NULL_START":null, "NULL_END":null}}
      ]
    },
    {
      "name": "should default to current mode for new queries",
      "format": ["JSON"],
      "statements": [
        "CREATE STREAM TEST (source VARCHAR) WITH (kafka_topic='test_topic', value_format='JSON');",
        "CREATE STREAM OUTPUT AS SELECT SUBSTRING(source, 6) AS SUBSTRING FROM TEST;"
      ],
      "inputs": [
        {"topic": "test_topic", "value": {"source": "some_string"}}
      ],
      "outputs": [
        {"topic": "OUTPUT", "value": {"SUBSTRING":"string"}}
      ]
    },
    {
      "name": "in group by",
      "statements": [
        "CREATE STREAM TEST (source VARCHAR) WITH (kafka_topic='test_topic', value_format='DELIMITED');",
        "CREATE TABLE OUTPUT AS SELECT SUBSTRING(source, 0, 2) AS Thing, COUNT(*) AS SUBSTRING FROM TEST GROUP BY SUBSTRING(source, 0, 2);"
      ],
      "inputs": [
        {"topic": "test_topic", "value": "some string"},
        {"topic": "test_topic", "value": "another string"},
        {"topic": "test_topic", "value": "some string again"},
        {"topic": "test_topic", "value": "another string again"},
        {"topic": "test_topic", "value": "some other string"},
        {"topic": "test_topic", "value": "the final string"}
      ],
      "outputs": [
        {"topic": "OUTPUT", "key": "so", "value": "so,1"},
        {"topic": "OUTPUT", "key": "an", "value": "an,1"},
        {"topic": "OUTPUT", "key": "so", "value": "so,2"},
        {"topic": "OUTPUT", "key": "an", "value": "an,2"},
        {"topic": "OUTPUT", "key": "so", "value": "so,3"},
        {"topic": "OUTPUT", "key": "th", "value": "th,1"}
      ]
    }
  ]
}