From 7802393d01519f2e64537dfd1c8b9900ad4db9b5 Mon Sep 17 00:00:00 2001 From: finelog Date: Tue, 25 Jan 2022 11:09:41 +0800 Subject: [PATCH 1/2] fix reset colmap when counting distinct cols (#2096) when using distinct cols with FindAndCount, reset statement.ColumnMap will make the counting sql an syntax error, this pr try fix this. is this pr ok for merge? error sql logging: ```sql [SQL] SELECT DISTINCT `Fid` FROM `table_demo` WHERE Fkey = ? [val] [SQL] SELECT count(DISTINCT ) FROM `table_demo` WHERE Fkey = ? [val] ``` after fix: ```sql [SQL] SELECT DISTINCT `Fid` FROM `table_demo` WHERE Fkey = ? [val] [SQL] SELECT count(DISTINCT `Fid`) FROM `table_demo` WHERE Fkey = ? [val] ``` Co-authored-by: finelog Reviewed-on: https://gitea.com/xorm/xorm/pulls/2096 Reviewed-by: Lunny Xiao Co-authored-by: finelog Co-committed-by: finelog --- integrations/session_find_test.go | 30 ++++++++++++++++++++++++++++++ session_find.go | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/integrations/session_find_test.go b/integrations/session_find_test.go index f7af45fa..0e0c7bb4 100644 --- a/integrations/session_find_test.go +++ b/integrations/session_find_test.go @@ -711,6 +711,36 @@ func TestFindAndCountWithGroupBy(t *testing.T) { assert.EqualValues(t, 2, len(results)) } +func TestFindAndCountWithDistinct(t *testing.T) { + assert.NoError(t, PrepareEngine()) + + type FindAndCountWithDistinct struct { + Id int64 + Age int `xorm:"index"` + Name string + } + + assert.NoError(t, testEngine.Sync(new(FindAndCountWithDistinct))) + + _, err := testEngine.Insert([]FindAndCountWithDistinct{ + { + Name: "test1", + Age: 10, + }, + { + Name: "test2", + Age: 20, + }, + }) + assert.NoError(t, err) + + var results []FindAndCountWithDistinct + cnt, err := testEngine.Distinct("`age`").FindAndCount(&results) + assert.NoError(t, err) + assert.EqualValues(t, 2, cnt) + assert.EqualValues(t, 2, len(results)) +} + type FindMapDevice struct { Deviceid string `xorm:"pk"` Status int diff --git a/session_find.go b/session_find.go index 57ac7bb7..caf79ee3 100644 --- a/session_find.go +++ b/session_find.go @@ -57,7 +57,7 @@ func (session *Session) FindAndCount(rowsSlicePtr interface{}, condiBean ...inte if session.statement.SelectStr != "" { session.statement.SelectStr = "" } - if len(session.statement.ColumnMap) > 0 { + if len(session.statement.ColumnMap) > 0 && !session.statement.IsDistinct { session.statement.ColumnMap = []string{} } if session.statement.OrderStr != "" { From 3180c418c2454771181690a360a88204a2514826 Mon Sep 17 00:00:00 2001 From: fuge Date: Tue, 25 Jan 2022 13:28:46 +0800 Subject: [PATCH 2/2] bugfix :Oid It's a special index. You can't put it in (#2105) Co-authored-by: fuge <8342337@qq.com> Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/xorm/xorm/pulls/2105 Co-authored-by: fuge Co-committed-by: fuge --- dialects/postgres.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/dialects/postgres.go b/dialects/postgres.go index 76279d32..a5b080aa 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -1300,6 +1300,19 @@ func (db *postgres) GetIndexes(queryer core.Queryer, ctx context.Context, tableN indexType = schemas.IndexType } colNames = getIndexColName(indexdef) + + isSkip := false + //Oid It's a special index. You can't put it in + for _, element := range colNames { + if "oid" == element { + isSkip = true + break + } + } + if isSkip { + continue + } + var isRegular bool if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) { newIdxName := indexName[5+len(tableName):]