Skip to content

Commit

Permalink
Merge pull request #63 from tablelandnetwork/bcalza/fixmultiplecompound
Browse files Browse the repository at this point in the history
fixes compound query syntax
  • Loading branch information
brunocalza authored Jun 5, 2023
2 parents ecb6cfd + 1cb695c commit c0e6862
Show file tree
Hide file tree
Showing 6 changed files with 2,624 additions and 2,601 deletions.
12 changes: 4 additions & 8 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,16 @@ const (

// CompoundSelect represents a compound operation of selects.
type CompoundSelect struct {
Left *Select
Type string
Right *Select
Limit *Limit
OrderBy OrderBy
Left *Select
Type string
Right ReadStatement
}

func (node *CompoundSelect) String() string {
return nodeStringsConcat(
node.Left.String(),
node.Type,
node.Right.String(),
node.Limit.String(),
node.OrderBy.String(),
)
}

Expand All @@ -268,7 +264,7 @@ func (node *CompoundSelect) walkSubtree(visit Visit) error {
if node == nil {
return nil
}
return Walk(visit, node.Left, node.Right, node.Limit, node.OrderBy)
return Walk(visit, node.Left, node.Right)
}

// Distinct/All.
Expand Down
37 changes: 17 additions & 20 deletions diagrams.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@
<xhtml:ul>
<xhtml:li><xhtml:a href="#stmts" title="stmts">stmts</xhtml:a></xhtml:li>
</xhtml:ul>
</xhtml:p><xhtml:br xmlns:xhtml="http://www.w3.org/1999/xhtml" /><xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml" style="font-size: 14px; font-weight:bold"><xhtml:a name="select_stmt">select_stmt:</xhtml:a></xhtml:p><svg xmlns="http://www.w3.org/2000/svg" width="663" height="69">
</xhtml:p><xhtml:br xmlns:xhtml="http://www.w3.org/1999/xhtml" /><xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml" style="font-size: 14px; font-weight:bold"><xhtml:a name="select_stmt">select_stmt:</xhtml:a></xhtml:p><svg xmlns="http://www.w3.org/2000/svg" width="431" height="81">
<defs>
<style type="text/css">
@namespace "http://www.w3.org/2000/svg";
Expand Down Expand Up @@ -473,25 +473,22 @@
polygon.regexp {fill: #FFFFFF; stroke: #262626; stroke-width: 1;}
</style>
</defs>
<polygon points="9 17 1 13 1 21"></polygon>
<polygon points="17 17 9 13 9 21"></polygon><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#base_select" xlink:title="base_select">
<rect x="31" y="3" width="96" height="32"></rect>
<rect x="29" y="1" width="96" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="39" y="21">base_select</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#compound_op" xlink:title="compound_op">
<rect x="167" y="35" width="110" height="32"></rect>
<rect x="165" y="33" width="110" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="175" y="53">compound_op</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#base_select" xlink:title="base_select">
<rect x="297" y="35" width="96" height="32"></rect>
<rect x="295" y="33" width="96" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="305" y="53">base_select</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#order_by_opt" xlink:title="order_by_opt">
<rect x="433" y="3" width="106" height="32"></rect>
<rect x="431" y="1" width="106" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="441" y="21">order_by_opt</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#limit_opt" xlink:title="limit_opt">
<rect x="559" y="3" width="76" height="32"></rect>
<rect x="557" y="1" width="76" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="567" y="21">limit_opt</text></a><svg:path xmlns:svg="http://www.w3.org/2000/svg" class="line" d="m17 17 h2 m0 0 h10 m96 0 h10 m20 0 h10 m0 0 h236 m-266 0 h20 m246 0 h20 m-286 0 q10 0 10 10 m266 0 q0 -10 10 -10 m-276 10 v12 m266 0 v-12 m-266 12 q0 10 10 10 m246 0 q10 0 10 -10 m-256 10 h10 m110 0 h10 m0 0 h10 m96 0 h10 m20 -32 h10 m106 0 h10 m0 0 h10 m76 0 h10 m3 0 h-3"></svg:path>
<polygon points="653 17 661 13 661 21"></polygon>
<polygon points="653 17 645 13 645 21"></polygon></svg><xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml">referenced by:
<polygon points="9 61 1 57 1 65"></polygon>
<polygon points="17 61 9 57 9 65"></polygon><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#base_select" xlink:title="base_select">
<rect x="51" y="47" width="96" height="32"></rect>
<rect x="49" y="45" width="96" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="59" y="65">base_select</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#compound_op" xlink:title="compound_op">
<rect x="51" y="3" width="110" height="32"></rect>
<rect x="49" y="1" width="110" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="59" y="21">compound_op</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#order_by_opt" xlink:title="order_by_opt">
<rect x="201" y="47" width="106" height="32"></rect>
<rect x="199" y="45" width="106" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="209" y="65">order_by_opt</text></a><a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#limit_opt" xlink:title="limit_opt">
<rect x="327" y="47" width="76" height="32"></rect>
<rect x="325" y="45" width="76" height="32" class="nonterminal"></rect>
<text class="nonterminal" x="335" y="65">limit_opt</text></a><svg:path xmlns:svg="http://www.w3.org/2000/svg" class="line" d="m17 61 h2 m20 0 h10 m96 0 h10 m0 0 h14 m-150 0 l20 0 m-1 0 q-9 0 -9 -10 l0 -24 q0 -10 10 -10 m130 44 l20 0 m-20 0 q10 0 10 -10 l0 -24 q0 -10 -10 -10 m-130 0 h10 m110 0 h10 m20 44 h10 m106 0 h10 m0 0 h10 m76 0 h10 m3 0 h-3"></svg:path>
<polygon points="421 61 429 57 429 65"></polygon>
<polygon points="421 61 413 57 413 65"></polygon></svg><xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml">referenced by:
<xhtml:ul>
<xhtml:li><xhtml:a href="#insert_stmt" title="insert_stmt">insert_stmt</xhtml:a></xhtml:li>
<xhtml:li><xhtml:a href="#single_stmt" title="single_stmt">single_stmt</xhtml:a></xhtml:li>
Expand Down
4 changes: 2 additions & 2 deletions grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ select_stmt:
$1.Limit = $3
$$ = $1
}
| base_select compound_op base_select order_by_opt limit_opt
| base_select compound_op select_stmt
{
$$ = &CompoundSelect{Type: $2, Left: $1, Right: $3, OrderBy: $4, Limit: $5}
$$ = &CompoundSelect{Type: $2, Left: $1, Right: $3}
}
;

Expand Down
56 changes: 51 additions & 5 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2985,6 +2985,52 @@ func TestSelectStatement(t *testing.T) {
},
},
},
{
name: "select multiple union",
stmt: "SELECT a FROM t UNION SELECT a FROM t2 UNION SELECT a FROM t3",
deparsed: "select a from t union select a from t2 union select a from t3",
expectedAST: &AST{
Statements: []Statement{
&CompoundSelect{
Left: &Select{
SelectColumnList: SelectColumnList{
&AliasedSelectColumn{
Expr: &Column{Name: "a"},
},
},
From: &AliasedTableExpr{
Expr: &Table{Name: "t", IsTarget: true},
},
},
Type: CompoundUnionStr,
Right: &CompoundSelect{
Left: &Select{
SelectColumnList: SelectColumnList{
&AliasedSelectColumn{
Expr: &Column{Name: "a"},
},
},
From: &AliasedTableExpr{
Expr: &Table{Name: "t2", IsTarget: true},
},
},
Type: CompoundUnionStr,
Right: &Select{
SelectColumnList: SelectColumnList{
&AliasedSelectColumn{
Expr: &Column{Name: "a"},
},
},
From: &AliasedTableExpr{
Expr: &Table{Name: "t3", IsTarget: true},
},
},
},
},
},
},
},

{
name: "select union all",
stmt: "SELECT a FROM t UNION ALL SELECT a FROM t2",
Expand Down Expand Up @@ -3076,11 +3122,11 @@ func TestSelectStatement(t *testing.T) {
From: &AliasedTableExpr{
Expr: &Table{Name: "t2", IsTarget: true},
},
},
OrderBy: []*OrderingTerm{
{
Expr: &Column{Name: "a"},
Direction: AscStr,
OrderBy: []*OrderingTerm{
{
Expr: &Column{Name: "a"},
Direction: AscStr,
},
},
},
},
Expand Down
Loading

0 comments on commit c0e6862

Please sign in to comment.