@@ -659,7 +659,7 @@ func TestInsertIntoStmt(t *testing.T) {
659659 },
660660 targets : nil ,
661661 where : & BinBoolExp {
662- op : AND ,
662+ op : And ,
663663 left : & CmpBoolExp {op : GE , left : & ColSelector {col : "balance" }, right : & Integer {val : 0 }},
664664 right : & CmpBoolExp {op : EQ , left : & ColSelector {col : "deleted_at" }, right : & NullValue {t : AnyType }},
665665 },
@@ -1009,9 +1009,9 @@ func TestSelectStmt(t *testing.T) {
10091009 },
10101010 ds : & tableRef {table : "table1" },
10111011 where : & BinBoolExp {
1012- op : AND ,
1012+ op : And ,
10131013 left : & BinBoolExp {
1014- op : AND ,
1014+ op : And ,
10151015 left : & CmpBoolExp {
10161016 op : EQ ,
10171017 left : & ColSelector {
@@ -1208,7 +1208,7 @@ func TestSelectStmt(t *testing.T) {
12081208 },
12091209 ds : & tableRef {table : "table1" },
12101210 where : & BinBoolExp {
1211- op : AND ,
1211+ op : And ,
12121212 left : & CmpBoolExp {
12131213 op : GE ,
12141214 left : & ColSelector {
@@ -1421,7 +1421,7 @@ func TestParseExp(t *testing.T) {
14211421 },
14221422 ds : & tableRef {table : "table1" },
14231423 where : & BinBoolExp {
1424- op : AND ,
1424+ op : And ,
14251425 left : & NotBoolExp {
14261426 exp : & CmpBoolExp {
14271427 op : GT ,
@@ -1453,7 +1453,7 @@ func TestParseExp(t *testing.T) {
14531453 ds : & tableRef {table : "table1" },
14541454 where : & NotBoolExp {
14551455 exp : & BinBoolExp {
1456- op : AND ,
1456+ op : And ,
14571457 left : & CmpBoolExp {
14581458 op : GT ,
14591459 left : & ColSelector {
@@ -1483,7 +1483,7 @@ func TestParseExp(t *testing.T) {
14831483 },
14841484 ds : & tableRef {table : "table1" },
14851485 where : & BinBoolExp {
1486- op : OR ,
1486+ op : Or ,
14871487 left : & NotBoolExp {
14881488 exp : & ColSelector {col : "active" },
14891489 },
@@ -1502,7 +1502,7 @@ func TestParseExp(t *testing.T) {
15021502 },
15031503 ds : & tableRef {table : "table1" },
15041504 where : & BinBoolExp {
1505- op : AND ,
1505+ op : And ,
15061506 left : & CmpBoolExp {
15071507 op : GT ,
15081508 left : & ColSelector {
@@ -1572,9 +1572,9 @@ func TestParseExp(t *testing.T) {
15721572 },
15731573 ds : & tableRef {table : "table1" },
15741574 where : & BinBoolExp {
1575- op : OR ,
1575+ op : Or ,
15761576 left : & BinBoolExp {
1577- op : AND ,
1577+ op : And ,
15781578 left : & CmpBoolExp {
15791579 op : GT ,
15801580 left : & ColSelector {
@@ -1711,7 +1711,7 @@ func TestParseExp(t *testing.T) {
17111711 whenThen : []whenThenClause {
17121712 {
17131713 when : & BinBoolExp {
1714- op : OR ,
1714+ op : Or ,
17151715 left : & ColSelector {col : "is_deleted" },
17161716 right : & ColSelector {col : "is_expired" },
17171717 },
@@ -1736,7 +1736,7 @@ func TestParseExp(t *testing.T) {
17361736 whenThen : []whenThenClause {
17371737 {
17381738 when : & BinBoolExp {
1739- op : OR ,
1739+ op : Or ,
17401740 left : & ColSelector {col : "is_deleted" },
17411741 right : & ColSelector {col : "is_expired" },
17421742 },
@@ -1798,7 +1798,7 @@ func TestParseExp(t *testing.T) {
17981798 },
17991799 {
18001800 when : & BinBoolExp {
1801- op : AND ,
1801+ op : And ,
18021802 left : & CmpBoolExp {op : GE , left : & ColSelector {col : "stock" }, right : & Integer {10 }},
18031803 right : & CmpBoolExp {op : LE , left : & ColSelector {col : "stock" }, right : & Integer {50 }},
18041804 },
@@ -1898,7 +1898,7 @@ func TestMultiLineStmts(t *testing.T) {
18981898 },
18991899 ds : & tableRef {table : "table1" },
19001900 where : & BinBoolExp {
1901- op : AND ,
1901+ op : And ,
19021902 left : & CmpBoolExp {
19031903 op : GE ,
19041904 left : & ColSelector {
@@ -2033,7 +2033,7 @@ func TestGrantRevokeStmt(t *testing.T) {
20332033 }
20342034}
20352035
2036- func TestExprString (t * testing.T ) {
2036+ func TestExpString (t * testing.T ) {
20372037 exps := []string {
20382038 "(1 + 1) / (2 * 5 - 10) % 2" ,
20392039 "@param LIKE 'pattern'" ,
@@ -2043,6 +2043,8 @@ func TestExprString(t *testing.T) {
20432043 "CASE WHEN in_stock THEN 'In Stock' END" ,
20442044 "CASE WHEN 1 > 0 THEN 1 ELSE 0 END" ,
20452045 "CASE WHEN is_active THEN 'active' WHEN is_expired THEN 'expired' ELSE 'active' END" ,
2046+ "'text' LIKE 'pattern'" ,
2047+ "'text' NOT LIKE 'pattern'" ,
20462048 }
20472049
20482050 for i , e := range exps {
@@ -2056,3 +2058,46 @@ func TestExprString(t *testing.T) {
20562058 })
20572059 }
20582060}
2061+
2062+ func TestLogicOperatorPrecedence (t * testing.T ) {
2063+ type testCase struct {
2064+ input string
2065+ expected string
2066+ }
2067+
2068+ testCases := []testCase {
2069+ // simple precedence
2070+ {input : "NOT true" , expected : "(NOT true)" },
2071+ {input : "true AND false OR true" , expected : "((true AND false) OR true)" },
2072+ {input : "NOT true AND false" , expected : "((NOT true) AND false)" },
2073+ {input : "NOT true OR false" , expected : "((NOT true) OR false)" },
2074+
2075+ // parentheses override precedence
2076+ {input : "(true OR false) AND true" , expected : "((true OR false) AND true)" },
2077+
2078+ // multiple NOTs
2079+ {input : "NOT NOT true AND false" , expected : "((NOT (NOT true)) AND false)" },
2080+
2081+ // complex nesting
2082+ {input : "true AND (false OR (NOT false))" , expected : "(true AND (false OR (NOT false)))" },
2083+ {input : "NOT (true AND false) OR true" , expected : "((NOT (true AND false)) OR true)" },
2084+
2085+ // AND/OR with nested groups
2086+ {input : "(true AND false) AND (true OR false)" , expected : "((true AND false) AND (true OR false))" },
2087+ {input : "(true OR false) OR (NOT (true AND false))" , expected : "((true OR false) OR (NOT (true AND false)))" },
2088+
2089+ // deep nesting
2090+ {input : "(true AND (false OR (NOT true))) OR (NOT false)" , expected : "((true AND (false OR (NOT true))) OR (NOT false))" },
2091+
2092+ // chain of operators
2093+ {input : "true AND false OR true AND NOT false OR true" , expected : "(((true AND false) OR (true AND (NOT false))) OR true)" },
2094+ }
2095+
2096+ for _ , tc := range testCases {
2097+ t .Run (tc .input , func (t * testing.T ) {
2098+ e , err := ParseExpFromString (tc .input )
2099+ require .NoError (t , err )
2100+ require .Equal (t , tc .expected , e .String ())
2101+ })
2102+ }
2103+ }
0 commit comments