|
8 | 8 | ) |
9 | 9 |
|
10 | 10 | // reshape rewrites a parsed tree so that decorative table aliases are |
11 | | -// stripped and sort AND conditions |
| 11 | +// stripped and commutative AND/OR conditions are ordered |
12 | 12 | func reshape(tree *pg_query.ParseResult) error { |
13 | 13 | if tree == nil { |
14 | 14 | return nil |
@@ -487,8 +487,8 @@ func reshapeSelect(s *pg_query.SelectStmt) { |
487 | 487 | } |
488 | 488 | } |
489 | 489 |
|
490 | | - sortAndTree(s.WhereClause) |
491 | | - sortAndTree(s.HavingClause) |
| 490 | + sortBoolTree(s.WhereClause) |
| 491 | + sortBoolTree(s.HavingClause) |
492 | 492 | } |
493 | 493 |
|
494 | 494 | func reshapeUpdate(u *pg_query.UpdateStmt) { |
@@ -533,7 +533,7 @@ func reshapeUpdate(u *pg_query.UpdateStmt) { |
533 | 533 | } |
534 | 534 | } |
535 | 535 |
|
536 | | - sortAndTree(u.WhereClause) |
| 536 | + sortBoolTree(u.WhereClause) |
537 | 537 | } |
538 | 538 |
|
539 | 539 | func reshapeDelete(d *pg_query.DeleteStmt) { |
@@ -572,7 +572,7 @@ func reshapeDelete(d *pg_query.DeleteStmt) { |
572 | 572 | } |
573 | 573 | } |
574 | 574 |
|
575 | | - sortAndTree(d.WhereClause) |
| 575 | + sortBoolTree(d.WhereClause) |
576 | 576 | } |
577 | 577 |
|
578 | 578 | func reshapeWithClause(w *pg_query.WithClause) { |
@@ -897,23 +897,24 @@ func rewriteInSelect(s *pg_query.SelectStmt, dec map[string][]*pg_query.Node) { |
897 | 897 | } |
898 | 898 | } |
899 | 899 |
|
900 | | -func sortAndTree(n *pg_query.Node) { |
| 900 | +func sortBoolTree(n *pg_query.Node) { |
901 | 901 | if n == nil { |
902 | 902 | return |
903 | 903 | } |
904 | 904 | switch v := n.Node.(type) { |
905 | 905 | case *pg_query.Node_BoolExpr: |
906 | 906 | for _, a := range v.BoolExpr.Args { |
907 | | - sortAndTree(a) |
| 907 | + sortBoolTree(a) |
908 | 908 | } |
909 | | - if v.BoolExpr.Boolop == pg_query.BoolExprType_AND_EXPR { |
| 909 | + switch v.BoolExpr.Boolop { |
| 910 | + case pg_query.BoolExprType_AND_EXPR, pg_query.BoolExprType_OR_EXPR: |
910 | 911 | sort.SliceStable(v.BoolExpr.Args, func(i, j int) bool { |
911 | 912 | return argSortKey(v.BoolExpr.Args[i]) < argSortKey(v.BoolExpr.Args[j]) |
912 | 913 | }) |
913 | 914 | } |
914 | 915 | case *pg_query.Node_AExpr: |
915 | | - sortAndTree(v.AExpr.Lexpr) |
916 | | - sortAndTree(v.AExpr.Rexpr) |
| 916 | + sortBoolTree(v.AExpr.Lexpr) |
| 917 | + sortBoolTree(v.AExpr.Rexpr) |
917 | 918 | } |
918 | 919 | } |
919 | 920 |
|
|
0 commit comments