squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

[BUG] Multiple calls to Distinct() result in invalid SQL

Open pwang96 opened this issue 4 years ago • 0 comments

Calling query.Distinct().Distinct() results in the DISTINCT option being applied twice, which is invalid SQL.

See the playground snippet here: https://play.golang.org/p/9qwFGj1jRWx

builder := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)

q1 := builder.Select("foo").From("bar").Distinct()
sql, _, _ := q1.ToSql()
fmt.Println(sql)
// SELECT DISTINCT foo FROM bar

q2 := builder.Select("foo").From("bar").Distinct().Distinct()
sql, _, _ = q2.ToSql()
fmt.Println(sql)
// SELECT DISTINCT DISTINCT foo FROM bar

Duplicate options should be removed before SQL construction.

pwang96 avatar Apr 13 '21 16:04 pwang96