Skip to content

Commit

Permalink
entc/gen: reduce the usage of DISTINCT in queries (#3305)
Browse files Browse the repository at this point in the history
Most queries are not graph traversals but rather regular table scans,
in which case the DISTINCT clause is not needed as duplicates cannot be
returned (unless query was modified by the user).
  • Loading branch information
a8m committed Feb 6, 2023
1 parent 138bd2b commit 939c7cf
Show file tree
Hide file tree
Showing 125 changed files with 508 additions and 1,474 deletions.
11 changes: 11 additions & 0 deletions dialect/sql/sqlgraph/graph.go
Expand Up @@ -577,6 +577,17 @@ type QuerySpec struct {
Assign func(columns []string, values []any) error
}

// NewQuerySpec creates a new node query spec.
func NewQuerySpec(table string, columns []string, id *FieldSpec) *QuerySpec {
return &QuerySpec{
Node: &NodeSpec{
ID: id,
Table: table,
Columns: columns,
},
}
}

// QueryNodes queries the nodes in the graph query and scans them to the given values.
func QueryNodes(ctx context.Context, drv dialect.Driver, spec *QuerySpec) error {
builder := sql.Dialect(drv.Dialect())
Expand Down
19 changes: 5 additions & 14 deletions entc/gen/template/dialect/sql/query.tmpl
Expand Up @@ -262,22 +262,13 @@ func ({{ $receiver }} *{{ $builder }}) sqlCount(ctx context.Context) (int, error
}

func ({{ $receiver }} *{{ $builder }}) querySpec() *sqlgraph.QuerySpec {
_spec := &sqlgraph.QuerySpec{
Node: &sqlgraph.NodeSpec{
Table: {{ $.Package }}.Table,
Columns: {{ $.Package }}.Columns,
{{- if $.HasOneFieldID }}
ID: &sqlgraph.FieldSpec{
Type: field.{{ $.ID.Type.ConstName }},
Column: {{ $.Package }}.{{ $.ID.Constant }},
},
{{- end }}
},
From: {{ $receiver }}.sql,
Unique: true,
}
_spec := sqlgraph.NewQuerySpec({{ $.Package }}.Table, {{ $.Package }}.Columns, {{ if $.HasOneFieldID }}sqlgraph.NewFieldSpec({{ $.Package }}.{{ $.ID.Constant }}, field.{{ $.ID.Type.ConstName }}){{ else }}nil{{ end }})
{{- /* Setup any intermediate queries if exist (traversal path). */}}
_spec.From = {{ $receiver }}.sql
if unique := {{ $receiver }}.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if {{ $receiver }}.path != nil {
_spec.Unique = true
}
if fields := {{ $receiver }}.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
Expand Down
16 changes: 4 additions & 12 deletions entc/integration/cascadelete/ent/comment_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/cascadelete/ent/post_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/cascadelete/ent/user_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/config/ent/user_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/account_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/blob_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions entc/integration/customid/ent/bloblink_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/car_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/device_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/doc_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/group_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/intsid_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/link_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions entc/integration/customid/ent/mixinid_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 939c7cf

Please sign in to comment.