Skip to content

Parser fails to handle extract() function with regex pattern: "unknown interval type" error #222

@Elon-WY

Description

@Elon-WY

DescriptionThe parser fails to parse valid ClickHouse SQL that uses the extract() function with a regex pattern, incorrectly treating it as an interval-related function.

Error Message

=== RUN   TestParseSQL
    parse_test.go:282: line 12:32 unknown interval type: <"instance">
            when length(extract(instance, '((\\d+\\.){3}\\d+)')) > 0 then instance
                                        ^
        
--- FAIL: TestParseSQL (5.77s)

SQL Example

SELECT
  count(1), src_type, node_class, port, client_port
FROM
  ocean.rt_stream_stat_15s_detail_distributed
WHERE
  app_id = 999118646
  AND toUnixTimestamp(timestamp) >= 1740366695
  AND toUnixTimestamp(timestamp) <= 1740377495
GROUP BY
  CASE
    WHEN length(extract(instance, '((\\d+\\.){3}\\d+)')) > 0 THEN instance
    ELSE ''
  END,
  CASE
    WHEN length(extract(client_ip, '((\\d+\\.){3}\\d+)')) > 0 THEN client_ip
    ELSE ''
  END,
  src_type,
  node_class,
  port,
  client_port
LIMIT 10000

Expected Behavior

The parser should successfully parse the SQL statement. In ClickHouse, extract(string, pattern) is used for regex extraction, not just for datetime interval extraction.
https://clickhouse.com/docs/sql-reference/functions/string-search-functions#extract

Actual Behavior

The parser throws an error: unknown interval type: <"instance">, suggesting it's trying to parse extract() as an interval/datetime function only.

Environment

  • Parser version: v0.4.17

Reproducible Test Casefunc

func TestParseSQL(t *testing.T) {
    sql := `select
  length(extract(instance, '((\\d+\\.){3}\\d+)'))
from
  rt_stream_stat_15s_detail_distributed
where
  timestamp > '2025-12-05 15:00:00'
limit
  10`
    
    parser := clickhouse.NewParser(sql)
    stmts, err := parser.ParseStmts()
    if err != nil {
        t.Error(err) // Fails with "unknown interval type" error
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions