Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(orm): add materialization to SELECT CTE statements #1

Open
wants to merge 1 commit into
base: v10
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions orm/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ const (
allWithDeletedFlag
)

type materializationMode uint8

const (
autoMaterializationMode materializationMode = iota
asMaterializedMode
asNotMaterializedMode
)

type withQuery struct {
name string
query QueryAppender
name string
query QueryAppender
materializationMode materializationMode
}

type columnValue struct {
Expand Down Expand Up @@ -262,25 +271,34 @@ func (q *Query) AllWithDeleted() *Query {

// With adds subq as common table expression with the given name.
func (q *Query) With(name string, subq *Query) *Query {
return q._with(name, NewSelectQuery(subq))
return q._with(name, NewSelectQuery(subq), autoMaterializationMode)
}

func (q *Query) WithMaterialized(name string, subq *Query) *Query {
return q._with(name, NewSelectQuery(subq), asMaterializedMode)
}

func (q *Query) WithNotMaterialized(name string, subq *Query) *Query {
return q._with(name, NewSelectQuery(subq), asNotMaterializedMode)
}

func (q *Query) WithInsert(name string, subq *Query) *Query {
return q._with(name, NewInsertQuery(subq))
return q._with(name, NewInsertQuery(subq), autoMaterializationMode)
}

func (q *Query) WithUpdate(name string, subq *Query) *Query {
return q._with(name, NewUpdateQuery(subq, false))
return q._with(name, NewUpdateQuery(subq, false), autoMaterializationMode)
}

func (q *Query) WithDelete(name string, subq *Query) *Query {
return q._with(name, NewDeleteQuery(subq))
return q._with(name, NewDeleteQuery(subq), autoMaterializationMode)
}

func (q *Query) _with(name string, subq QueryAppender) *Query {
func (q *Query) _with(name string, subq QueryAppender, mm materializationMode) *Query {
q.with = append(q.with, withQuery{
name: name,
query: subq,
name: name,
query: subq,
materializationMode: mm,
})
return q
}
Expand Down Expand Up @@ -1533,7 +1551,14 @@ func (q *Query) appendWith(fmter QueryFormatter, b []byte) (_ []byte, err error)
b = append(b, ", "...)
}
b = types.AppendIdent(b, with.name, 1)
b = append(b, " AS ("...)
b = append(b, " AS"...)
switch with.materializationMode {
case asMaterializedMode:
b = append(b, " MATERIALIZED"...)
case asNotMaterializedMode:
b = append(b, " NOT MATERIALIZED"...)
}
b = append(b, " ("...)

b, err = with.query.AppendQuery(fmter, b)
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions orm/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,44 @@ var _ = Describe("With", func() {
Expect(s).To(Equal(`WITH "wrapper" AS (SELECT "select_model"."id", "select_model"."name", "select_model"."has_one_id" FROM "select_models" AS "select_model" WHERE (cond1)) SELECT * FROM "wrapper" WHERE (cond2)`))
})

It("generates CTE", func() {
q1 := NewQuery(nil).Table("q1")
q2 := NewQuery(nil).With("q1", q1).Table("q2", "q1")

s := selectQueryString(q2)
Expect(s).To(Equal(`WITH "q1" AS (SELECT * FROM "q1") SELECT * FROM "q2", "q1"`))
})

It("generates materialized CTE", func() {
q1 := NewQuery(nil).Table("q1")
q2 := NewQuery(nil).WithMaterialized("q1", q1).Table("q2", "q1")

s := selectQueryString(q2)
Expect(s).To(Equal(`WITH "q1" AS MATERIALIZED (SELECT * FROM "q1") SELECT * FROM "q2", "q1"`))
})

It("generates not materialized CTE", func() {
q1 := NewQuery(nil).Table("q1")
q2 := NewQuery(nil).WithNotMaterialized("q1", q1).Table("q2", "q1")

s := selectQueryString(q2)
Expect(s).To(Equal(`WITH "q1" AS NOT MATERIALIZED (SELECT * FROM "q1") SELECT * FROM "q2", "q1"`))
})

It("generates mixed materialization modes in CTE", func() {
q1 := NewQuery(nil).Table("q1")
q2 := NewQuery(nil).Table("q2")
q3 := NewQuery(nil).Table("q3")
q4 := NewQuery(nil).
With("q1", q1).
WithMaterialized("q2", q2).
WithNotMaterialized("q3", q3).
Table("q4", "q3", "q2", "q1")

s := selectQueryString(q4)
Expect(s).To(Equal(`WITH "q1" AS (SELECT * FROM "q1"), "q2" AS MATERIALIZED (SELECT * FROM "q2"), "q3" AS NOT MATERIALIZED (SELECT * FROM "q3") SELECT * FROM "q4", "q3", "q2", "q1"`))
})

It("generates nested CTE", func() {
q1 := NewQuery(nil).Table("q1")
q2 := NewQuery(nil).With("q1", q1).Table("q2", "q1")
Expand Down