Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkg/sql/parser/clickhouse_array_literal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2026 GoSQLX Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");

package parser_test

import (
"testing"

"github.com/ajitpratap0/GoSQLX/pkg/gosqlx"
"github.com/ajitpratap0/GoSQLX/pkg/sql/keywords"
)

// TestClickHouseArrayLiteral verifies that the bare-bracket array literal
// `[1, 2, 3]` parses in the ClickHouse dialect. ClickHouse supports this as
// a shorthand for `array(1, 2, 3)`. Regression for #482.
func TestClickHouseArrayLiteral(t *testing.T) {
queries := map[string]string{
"int_literal": `SELECT [1, 2, 3] AS nums`,
"string_literal": `SELECT ['a', 'b', 'c'] AS words`,
"empty": `SELECT [] AS empty`,
"nested": `SELECT [[1, 2], [3, 4]] AS matrix`,
"in_function": `SELECT arrayJoin([10, 20, 30]) AS x`,
"mixed_with_col": `SELECT id, [status, type] AS labels FROM events`,
}
for name, q := range queries {
q := q
t.Run(name, func(t *testing.T) {
if _, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse); err != nil {
t.Fatalf("ParseWithDialect failed: %v", err)
}
})
}
}
33 changes: 33 additions & 0 deletions pkg/sql/parser/expressions_complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,36 @@ func (p *Parser) parseArrayConstructor() (*ast.ArrayConstructorExpression, error

return nil, p.expectedError("[ or (")
}

// parseBracketArrayLiteral parses a ClickHouse-style bare bracket array
// literal: [expr, expr, ...]. The opening '[' is the current token.
func (p *Parser) parseBracketArrayLiteral() (*ast.ArrayConstructorExpression, error) {
p.advance() // Consume [

arrayExpr := ast.GetArrayConstructor()

if !p.isType(models.TokenTypeRBracket) {
for {
elem, err := p.parseExpression()
if err != nil {
return nil, err
}
arrayExpr.Elements = append(arrayExpr.Elements, elem)

if p.isType(models.TokenTypeComma) {
p.advance()
} else if p.isType(models.TokenTypeRBracket) {
break
} else {
return nil, p.expectedError(", or ]")
}
}
}

if !p.isType(models.TokenTypeRBracket) {
return nil, p.expectedError("]")
}
p.advance() // Consume ]

return arrayExpr, nil
}
7 changes: 7 additions & 0 deletions pkg/sql/parser/expressions_literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ func (p *Parser) parsePrimaryExpression() (ast.Expression, error) {
return p.parseArrayConstructor()
}

// ClickHouse array literal: [expr, expr, ...] without the ARRAY keyword.
// Gated to ClickHouse to avoid colliding with subscript-style usage in other
// dialects.
if p.isType(models.TokenTypeLBracket) && p.dialect == string(keywords.DialectClickHouse) {
return p.parseBracketArrayLiteral()
}

// Handle MySQL VALUES(column) helper in ON DUPLICATE KEY UPDATE.
// VALUES is normally a DML keyword, but inside ON DUPLICATE KEY UPDATE it acts
// as a scalar function that returns the value that was attempted for insertion.
Expand Down
Loading