Skip to content

Commit 95345f7

Browse files
Ajit Pratap Singhclaude
authored andcommitted
feat(parser): add Oracle CONNECT BY / START WITH hierarchical query support (#450)
Extend the MariaDB CONNECT BY / START WITH hierarchical query parser to also activate for Oracle dialect. Update isMariaDBClauseStart() to guard table-alias parsing for both dialects, preventing START WITH / CONNECT BY from being misidentified as aliases. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e09be65 commit 95345f7

3 files changed

Lines changed: 112 additions & 5 deletions

File tree

pkg/sql/parser/mariadb.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func (p *Parser) isMariaDB() bool {
2929
}
3030

3131
// isMariaDBClauseStart returns true when the current token is the start of a
32-
// MariaDB hierarchical-query clause (CONNECT BY or START WITH) rather than a
33-
// table alias. Used to guard alias parsing in FROM and JOIN table references.
32+
// MariaDB or Oracle hierarchical-query clause (CONNECT BY or START WITH) rather
33+
// than a table alias. Used to guard alias parsing in FROM and JOIN table references.
3434
func (p *Parser) isMariaDBClauseStart() bool {
35-
if !p.isMariaDB() {
35+
if !p.isMariaDB() && p.dialect != string(keywords.DialectOracle) {
3636
return false
3737
}
3838
val := strings.ToUpper(p.currentToken.Token.Value)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2026 GoSQLX Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package parser_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/ajitpratap0/GoSQLX/pkg/gosqlx"
21+
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
22+
"github.com/ajitpratap0/GoSQLX/pkg/sql/keywords"
23+
)
24+
25+
// TestConnectBy_Oracle_Basic tests basic Oracle CONNECT BY syntax.
26+
func TestConnectBy_Oracle_Basic(t *testing.T) {
27+
sql := `SELECT employee_id, manager_id, name
28+
FROM employees
29+
START WITH manager_id IS NULL
30+
CONNECT BY PRIOR employee_id = manager_id`
31+
tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectOracle)
32+
if err != nil {
33+
t.Fatalf("Parse() error: %v", err)
34+
}
35+
sel, ok := tree.Statements[0].(*ast.SelectStatement)
36+
if !ok {
37+
t.Fatalf("expected SelectStatement, got %T", tree.Statements[0])
38+
}
39+
if sel.ConnectBy == nil {
40+
t.Error("expected ConnectBy clause to be populated")
41+
}
42+
if sel.StartWith == nil {
43+
t.Error("expected StartWith expression to be populated")
44+
}
45+
}
46+
47+
// TestConnectBy_Oracle_NoCycle tests Oracle CONNECT BY with NOCYCLE modifier.
48+
func TestConnectBy_Oracle_NoCycle(t *testing.T) {
49+
sql := `SELECT id, parent_id FROM categories
50+
START WITH parent_id IS NULL
51+
CONNECT BY NOCYCLE PRIOR id = parent_id`
52+
tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectOracle)
53+
if err != nil {
54+
t.Fatalf("Parse() error: %v", err)
55+
}
56+
sel, ok := tree.Statements[0].(*ast.SelectStatement)
57+
if !ok {
58+
t.Fatalf("expected SelectStatement, got %T", tree.Statements[0])
59+
}
60+
if sel.ConnectBy == nil {
61+
t.Fatal("expected ConnectBy")
62+
}
63+
if !sel.ConnectBy.NoCycle {
64+
t.Error("expected NoCycle = true")
65+
}
66+
}
67+
68+
// TestConnectBy_Oracle_ConnectByOnly tests CONNECT BY without START WITH.
69+
func TestConnectBy_Oracle_ConnectByOnly(t *testing.T) {
70+
sql := `SELECT id, parent_id FROM categories
71+
CONNECT BY PRIOR id = parent_id`
72+
tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectOracle)
73+
if err != nil {
74+
t.Fatalf("Parse() error: %v", err)
75+
}
76+
sel, ok := tree.Statements[0].(*ast.SelectStatement)
77+
if !ok {
78+
t.Fatalf("expected SelectStatement, got %T", tree.Statements[0])
79+
}
80+
if sel.ConnectBy == nil {
81+
t.Error("expected ConnectBy clause")
82+
}
83+
if sel.StartWith != nil {
84+
t.Error("expected StartWith to be nil when not specified")
85+
}
86+
}
87+
88+
// TestConnectBy_MariaDB_StillWorks ensures existing MariaDB CONNECT BY parsing remains intact.
89+
func TestConnectBy_MariaDB_StillWorks(t *testing.T) {
90+
sql := `SELECT id, parent_id FROM categories
91+
START WITH parent_id IS NULL
92+
CONNECT BY PRIOR id = parent_id`
93+
tree, err := gosqlx.ParseWithDialect(sql, keywords.DialectMariaDB)
94+
if err != nil {
95+
t.Fatalf("Parse() error: %v", err)
96+
}
97+
sel, ok := tree.Statements[0].(*ast.SelectStatement)
98+
if !ok {
99+
t.Fatalf("expected SelectStatement, got %T", tree.Statements[0])
100+
}
101+
if sel.ConnectBy == nil {
102+
t.Error("expected ConnectBy clause")
103+
}
104+
if sel.StartWith == nil {
105+
t.Error("expected StartWith expression")
106+
}
107+
}

pkg/sql/parser/select.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func (p *Parser) parseSelectStatement() (ast.Statement, error) {
109109
return nil, err
110110
}
111111

112-
// MariaDB: START WITH ... CONNECT BY hierarchical queries (10.2+)
113-
if p.isMariaDB() {
112+
// Oracle/MariaDB: START WITH ... CONNECT BY hierarchical queries
113+
if p.isMariaDB() || p.dialect == string(keywords.DialectOracle) {
114114
if strings.EqualFold(p.currentToken.Token.Value, "START") {
115115
p.advance() // Consume START
116116
if !strings.EqualFold(p.currentToken.Token.Value, "WITH") {

0 commit comments

Comments
 (0)