Skip to content

Commit

Permalink
feat: union expr
Browse files Browse the repository at this point in the history
  • Loading branch information
kzaitsev committed Aug 19, 2023
1 parent e1af263 commit bb8f6a0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
47 changes: 46 additions & 1 deletion ch/query_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"github.com/uptrace/go-clickhouse/ch/internal"
)

type union struct {
expr string
query *SelectQuery
}

type SelectQuery struct {
baseQuery

Expand All @@ -26,6 +31,8 @@ type SelectQuery struct {
limit int
offset int
final bool

union []union
}

var _ Query = (*SelectQuery)(nil)
Expand Down Expand Up @@ -150,6 +157,24 @@ func (q *SelectQuery) ExcludeColumn(columns ...string) *SelectQuery {
return q
}

///------------------------------------------------------------------------------

func (q *SelectQuery) Union(other *SelectQuery) *SelectQuery {
return q.addUnion(" UNION ", other)
}

func (q *SelectQuery) UnionAll(other *SelectQuery) *SelectQuery {
return q.addUnion(" UNION ALL ", other)
}

func (q *SelectQuery) addUnion(expr string, other *SelectQuery) *SelectQuery {
q.union = append(q.union, union{
expr: expr,
query: other,
})
return q
}

//------------------------------------------------------------------------------

func (q *SelectQuery) Join(join string, args ...any) *SelectQuery {
Expand Down Expand Up @@ -329,6 +354,10 @@ func (q *SelectQuery) appendQuery(
b = append(b, `WITH "_count_wrapper" AS (`...)
}

if len(q.union) > 0 {
b = append(b, '(')
}

if len(q.with) > 0 {
b, err = q.appendWith(fmter, b)
if err != nil {
Expand Down Expand Up @@ -429,6 +458,20 @@ func (q *SelectQuery) appendQuery(
}
}

if len(q.union) > 0 {
b = append(b, ')')

for _, u := range q.union {
b = append(b, u.expr...)
b = append(b, '(')
b, err = u.query.AppendQuery(fmter, b)
if err != nil {
return nil, err
}
b = append(b, ')')
}
}

if !count {
if len(q.order) > 0 {
b = append(b, " ORDER BY "...)
Expand All @@ -451,7 +494,9 @@ func (q *SelectQuery) appendQuery(
b = append(b, " OFFSET "...)
b = strconv.AppendInt(b, int64(q.offset), 10)
}
} else if cteCount {
}

if cteCount {
b = append(b, `) SELECT `...)
b = append(b, "count()"...)
b = append(b, ` FROM "_count_wrapper"`...)
Expand Down
10 changes: 10 additions & 0 deletions ch/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ func TestQuery(t *testing.T) {
Group("id").
OrderExpr("id")
},
func(db *ch.DB) chschema.QueryAppender {
q1 := db.NewSelect().Model(new(Model)).Where("1")
q2 := db.NewSelect().Model(new(Model))
return q1.Union(q2)
},
func(db *ch.DB) chschema.QueryAppender {
q1 := db.NewSelect().Model(new(Model)).Where("1")
q2 := db.NewSelect().Model(new(Model))
return q1.UnionAll(q2)
},
}

db := chDB()
Expand Down
1 change: 1 addition & 0 deletions ch/testdata/snapshots/TestQuery-17
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" WHERE (1)) UNION (SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model")
1 change: 1 addition & 0 deletions ch/testdata/snapshots/TestQuery-18
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model" WHERE (1)) UNION ALL (SELECT "model"."id", "model"."string", "model"."bytes" FROM "models" AS "model")

0 comments on commit bb8f6a0

Please sign in to comment.