|
| 1 | +// Copyright 2026 GoSQLX Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + |
| 5 | +package parser_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ajitpratap0/GoSQLX/pkg/gosqlx" |
| 11 | + "github.com/ajitpratap0/GoSQLX/pkg/sql/keywords" |
| 12 | +) |
| 13 | + |
| 14 | +// TestClickHouseWithTotals verifies GROUP BY ... WITH TOTALS parses. |
| 15 | +func TestClickHouseWithTotals(t *testing.T) { |
| 16 | + queries := []string{ |
| 17 | + `SELECT status, count() FROM events GROUP BY status WITH TOTALS`, |
| 18 | + `SELECT status, count() FROM events GROUP BY status WITH TOTALS ORDER BY status`, |
| 19 | + } |
| 20 | + for _, q := range queries { |
| 21 | + t.Run(q[:40], func(t *testing.T) { |
| 22 | + if _, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse); err != nil { |
| 23 | + t.Fatalf("parse failed: %v", err) |
| 24 | + } |
| 25 | + }) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// TestClickHouseLimitBy verifies LIMIT N [OFFSET M] BY expr parses. |
| 30 | +func TestClickHouseLimitBy(t *testing.T) { |
| 31 | + queries := []string{ |
| 32 | + `SELECT user_id, event FROM events ORDER BY ts LIMIT 3 BY user_id`, |
| 33 | + `SELECT user_id, event FROM events ORDER BY ts LIMIT 3 OFFSET 1 BY user_id`, |
| 34 | + `SELECT user_id, event, ts FROM events ORDER BY ts LIMIT 5 BY user_id, event`, |
| 35 | + } |
| 36 | + for _, q := range queries { |
| 37 | + t.Run(q[:40], func(t *testing.T) { |
| 38 | + if _, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse); err != nil { |
| 39 | + t.Fatalf("parse failed: %v", err) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// TestClickHouseAnyJoin verifies the ANY/ALL join strictness prefix parses. |
| 46 | +func TestClickHouseAnyJoin(t *testing.T) { |
| 47 | + queries := map[string]string{ |
| 48 | + "any_left": `SELECT * FROM a ANY LEFT JOIN b ON a.id = b.id`, |
| 49 | + "any_inner": `SELECT * FROM a ANY INNER JOIN b ON a.id = b.id`, |
| 50 | + "all_inner": `SELECT * FROM a ALL INNER JOIN b ON a.id = b.id`, |
| 51 | + "asof": `SELECT * FROM a ASOF JOIN b ON a.id = b.id AND a.ts >= b.ts`, |
| 52 | + } |
| 53 | + for name, q := range queries { |
| 54 | + q := q |
| 55 | + t.Run(name, func(t *testing.T) { |
| 56 | + if _, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse); err != nil { |
| 57 | + t.Fatalf("parse failed: %v", err) |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestClickHouseDefaultAsIdentifier verifies DEFAULT can be used as a column |
| 64 | +// name and DATABASES as a qualified table name in ClickHouse. |
| 65 | +func TestClickHouseDefaultAsIdentifier(t *testing.T) { |
| 66 | + queries := []string{ |
| 67 | + `SELECT default FROM t`, |
| 68 | + `SELECT database, default FROM system.databases`, |
| 69 | + } |
| 70 | + for _, q := range queries { |
| 71 | + t.Run(q, func(t *testing.T) { |
| 72 | + if _, err := gosqlx.ParseWithDialect(q, keywords.DialectClickHouse); err != nil { |
| 73 | + t.Fatalf("parse failed: %v", err) |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments