From 19c093467d9fbc058b05cd9957bf7fc5e5889a62 Mon Sep 17 00:00:00 2001 From: Prafulla Mahindrakar Date: Thu, 7 Apr 2022 19:42:34 +0530 Subject: [PATCH 1/5] Fix sorting bug for named entities Signed-off-by: Prafulla Mahindrakar --- pkg/common/sorting.go | 7 + pkg/common/sorting_test.go | 2 + .../gormimpl/named_entity_repo.go | 14 +- .../gormimpl/named_entity_repo_test.go | 129 +++++++++++++++++- 4 files changed, 142 insertions(+), 10 deletions(-) diff --git a/pkg/common/sorting.go b/pkg/common/sorting.go index c4922d0b1..8bdaf5dd3 100644 --- a/pkg/common/sorting.go +++ b/pkg/common/sorting.go @@ -13,16 +13,22 @@ const gormAscending = "%s asc" type SortParameter interface { GetGormOrderExpr() string + GetSortKey() string } type sortParamImpl struct { gormOrderExpression string + sortKey string } func (s *sortParamImpl) GetGormOrderExpr() string { return s.gormOrderExpression } +func (s *sortParamImpl) GetSortKey() string { + return s.sortKey +} + func NewSortParameter(sort admin.Sort) (SortParameter, error) { var gormOrderExpression string switch sort.Direction { @@ -35,5 +41,6 @@ func NewSortParameter(sort admin.Sort) (SortParameter, error) { } return &sortParamImpl{ gormOrderExpression: gormOrderExpression, + sortKey: sort.Key, }, nil } diff --git a/pkg/common/sorting_test.go b/pkg/common/sorting_test.go index 20cb69f6d..04a3b88d4 100644 --- a/pkg/common/sorting_test.go +++ b/pkg/common/sorting_test.go @@ -14,6 +14,7 @@ func TestSortParameter_Ascending(t *testing.T) { }) assert.Nil(t, err) assert.Equal(t, "name asc", sortParameter.GetGormOrderExpr()) + assert.Equal(t, "name", sortParameter.GetSortKey()) } func TestSortParameter_Descending(t *testing.T) { @@ -23,4 +24,5 @@ func TestSortParameter_Descending(t *testing.T) { }) assert.Nil(t, err) assert.Equal(t, "project desc", sortParameter.GetGormOrderExpr()) + assert.Equal(t, "project", sortParameter.GetSortKey()) } diff --git a/pkg/repositories/gormimpl/named_entity_repo.go b/pkg/repositories/gormimpl/named_entity_repo.go index 8e02390dd..1cca6b227 100644 --- a/pkg/repositories/gormimpl/named_entity_repo.go +++ b/pkg/repositories/gormimpl/named_entity_repo.go @@ -4,15 +4,15 @@ import ( "context" "fmt" - "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core" - "google.golang.org/grpc/codes" - "github.com/flyteorg/flyteadmin/pkg/common" adminErrors "github.com/flyteorg/flyteadmin/pkg/errors" "github.com/flyteorg/flyteadmin/pkg/repositories/errors" "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces" "github.com/flyteorg/flyteadmin/pkg/repositories/models" + "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core" "github.com/flyteorg/flytestdlib/promutils" + + "google.golang.org/grpc/codes" "gorm.io/gorm" ) @@ -33,12 +33,15 @@ func getSubQueryJoin(db *gorm.DB, tableName string, input interfaces.ListNamedEn Table(tableName). Where(map[string]interface{}{Project: input.Project, Domain: input.Domain}). Limit(input.Limit). - Offset(input.Offset). - Group(identifierGroupBy) + Offset(input.Offset) // Apply consistent sort ordering. if input.SortParameter != nil { + identifierGroupByWithOrderKey := fmt.Sprintf("%s, %s, %s, %s", Project, Domain, Name, input.SortParameter.GetSortKey()) + tx = tx.Group(identifierGroupByWithOrderKey) tx = tx.Order(input.SortParameter.GetGormOrderExpr()) + } else { + tx = tx.Group(identifierGroupBy) } return db.Joins(fmt.Sprintf(joinString, input.ResourceType), tx) @@ -191,6 +194,7 @@ func (r *NamedEntityRepo) List(ctx context.Context, input interfaces.ListNamedEn } // Apply sort ordering. if input.SortParameter != nil { + tx = tx.Group(input.SortParameter.GetSortKey()) tx = tx.Order(input.SortParameter.GetGormOrderExpr()) } diff --git a/pkg/repositories/gormimpl/named_entity_repo_test.go b/pkg/repositories/gormimpl/named_entity_repo_test.go index d1867eca0..128cc462c 100644 --- a/pkg/repositories/gormimpl/named_entity_repo_test.go +++ b/pkg/repositories/gormimpl/named_entity_repo_test.go @@ -2,18 +2,20 @@ package gormimpl import ( "context" + "fmt" "testing" "github.com/flyteorg/flyteadmin/pkg/common" - - "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" - - mocket "github.com/Selvatico/go-mocket" + adminErrors "github.com/flyteorg/flyteadmin/pkg/errors" "github.com/flyteorg/flyteadmin/pkg/repositories/errors" "github.com/flyteorg/flyteadmin/pkg/repositories/interfaces" "github.com/flyteorg/flyteadmin/pkg/repositories/models" + "github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin" mockScope "github.com/flyteorg/flytestdlib/promutils" + + mocket "github.com/Selvatico/go-mocket" "github.com/stretchr/testify/assert" + "google.golang.org/grpc/codes" ) func getMockNamedEntityResponseFromDb(expected models.NamedEntity) map[string]interface{} { @@ -155,7 +157,7 @@ func TestListNamedEntity(t *testing.T) { mockQuery := GlobalMock.NewMock() mockQuery.WithQuery( - `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state ORDER BY name desc`).WithReply(results) + `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name, name ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY "name",entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state ORDER BY name desc`).WithReply(results) sortParameter, _ := common.NewSortParameter(admin.Sort{ Direction: admin.Sort_DESCENDING, @@ -173,3 +175,120 @@ func TestListNamedEntity(t *testing.T) { assert.NoError(t, err) assert.Len(t, output.Entities, 1) } + +func TestListNamedEntityTxErrorCases(t *testing.T) { + metadataRepo := NewNamedEntityRepo(GetDbForTest(t), errors.NewTestErrorTransformer(), mockScope.NewTestScope()) + + results := make([]map[string]interface{}, 0) + metadata := getMockNamedEntityResponseFromDb(models.NamedEntity{ + NamedEntityKey: models.NamedEntityKey{ + ResourceType: resourceType, + Project: project, + Domain: domain, + Name: name, + }, + NamedEntityMetadataFields: models.NamedEntityMetadataFields{ + Description: description, + }, + }) + results = append(results, metadata) + + GlobalMock := mocket.Catcher.Reset() + GlobalMock.Logging = true + mockQuery := GlobalMock.NewMock() + + mockQuery.WithQuery( + `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name, name ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY "name",entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state ORDER BY name desc`).WithError(fmt.Errorf("failed")) + + sortParameter, _ := common.NewSortParameter(admin.Sort{ + Direction: admin.Sort_DESCENDING, + Key: "name", + }) + output, err := metadataRepo.List(context.Background(), interfaces.ListNamedEntityInput{ + ResourceType: resourceType, + Project: "admintests", + Domain: "development", + ListResourceInput: interfaces.ListResourceInput{ + Limit: 20, + SortParameter: sortParameter, + }, + }) + assert.Equal(t, "Test transformer failed to find transformation to apply", err.Error()) + assert.Len(t, output.Entities, 0) +} + +func TestListNamedEntityInputErrorCases(t *testing.T) { + type test struct { + input interfaces.ListNamedEntityInput + wantedError error + wantedLength int + } + + sortParameter, _ := common.NewSortParameter(admin.Sort{ + Direction: admin.Sort_DESCENDING, + Key: "name", + }) + + tests := []test{ + { + input: interfaces.ListNamedEntityInput{ + ResourceType: resourceType, + Project: "", + Domain: "development", + ListResourceInput: interfaces.ListResourceInput{ + Limit: 20, + SortParameter: sortParameter, + }, + }, + wantedError: errors.GetInvalidInputError(Project), + wantedLength: 0, + }, + { + input: interfaces.ListNamedEntityInput{ + ResourceType: resourceType, + Project: "project", + Domain: "", + ListResourceInput: interfaces.ListResourceInput{ + Limit: 20, + SortParameter: sortParameter, + }, + }, + wantedError: errors.GetInvalidInputError(Domain), + wantedLength: 0, + }, + { + input: interfaces.ListNamedEntityInput{ + ResourceType: resourceType, + Project: "project", + Domain: "development", + ListResourceInput: interfaces.ListResourceInput{ + Limit: 0, + SortParameter: sortParameter, + }, + }, + wantedError: errors.GetInvalidInputError(limit), + wantedLength: 0, + }, + { + input: interfaces.ListNamedEntityInput{ + ResourceType: -1, + Project: "project", + Domain: "development", + ListResourceInput: interfaces.ListResourceInput{ + Limit: 20, + SortParameter: sortParameter, + }, + }, + wantedError: adminErrors.NewFlyteAdminErrorf(codes.InvalidArgument, + "Cannot list entity names for resource type: %v", -1), + wantedLength: 0, + }, + } + + metadataRepo := NewNamedEntityRepo(GetDbForTest(t), errors.NewTestErrorTransformer(), mockScope.NewTestScope()) + for _, tc := range tests { + output, err := metadataRepo.List(context.Background(), tc.input) + assert.Len(t, output.Entities, tc.wantedLength) + assert.Equal(t, tc.wantedError, err) + } +} From 7462000b6e9132ee1e90f393961aa58a81f62468 Mon Sep 17 00:00:00 2001 From: Prafulla Mahindrakar Date: Thu, 7 Apr 2022 20:14:41 +0530 Subject: [PATCH 2/5] lint fixes Signed-off-by: Prafulla Mahindrakar --- .../gormimpl/named_entity_repo_test.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/pkg/repositories/gormimpl/named_entity_repo_test.go b/pkg/repositories/gormimpl/named_entity_repo_test.go index 128cc462c..84277fd8c 100644 --- a/pkg/repositories/gormimpl/named_entity_repo_test.go +++ b/pkg/repositories/gormimpl/named_entity_repo_test.go @@ -178,21 +178,6 @@ func TestListNamedEntity(t *testing.T) { func TestListNamedEntityTxErrorCases(t *testing.T) { metadataRepo := NewNamedEntityRepo(GetDbForTest(t), errors.NewTestErrorTransformer(), mockScope.NewTestScope()) - - results := make([]map[string]interface{}, 0) - metadata := getMockNamedEntityResponseFromDb(models.NamedEntity{ - NamedEntityKey: models.NamedEntityKey{ - ResourceType: resourceType, - Project: project, - Domain: domain, - Name: name, - }, - NamedEntityMetadataFields: models.NamedEntityMetadataFields{ - Description: description, - }, - }) - results = append(results, metadata) - GlobalMock := mocket.Catcher.Reset() GlobalMock.Logging = true mockQuery := GlobalMock.NewMock() From b0cde31972d9c0bd8ff85a7793635821ea2d394f Mon Sep 17 00:00:00 2001 From: Prafulla Mahindrakar Date: Fri, 8 Apr 2022 18:16:38 +0530 Subject: [PATCH 3/5] Fixed filtering Signed-off-by: Prafulla Mahindrakar --- pkg/common/sorting.go | 6 ------ pkg/common/sorting_test.go | 2 -- pkg/repositories/gormimpl/common.go | 3 ++- pkg/repositories/gormimpl/named_entity_repo.go | 15 ++++++++------- .../gormimpl/named_entity_repo_test.go | 4 ++-- 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/pkg/common/sorting.go b/pkg/common/sorting.go index 8bdaf5dd3..3b71d7427 100644 --- a/pkg/common/sorting.go +++ b/pkg/common/sorting.go @@ -13,7 +13,6 @@ const gormAscending = "%s asc" type SortParameter interface { GetGormOrderExpr() string - GetSortKey() string } type sortParamImpl struct { @@ -25,10 +24,6 @@ func (s *sortParamImpl) GetGormOrderExpr() string { return s.gormOrderExpression } -func (s *sortParamImpl) GetSortKey() string { - return s.sortKey -} - func NewSortParameter(sort admin.Sort) (SortParameter, error) { var gormOrderExpression string switch sort.Direction { @@ -41,6 +36,5 @@ func NewSortParameter(sort admin.Sort) (SortParameter, error) { } return &sortParamImpl{ gormOrderExpression: gormOrderExpression, - sortKey: sort.Key, }, nil } diff --git a/pkg/common/sorting_test.go b/pkg/common/sorting_test.go index 04a3b88d4..20cb69f6d 100644 --- a/pkg/common/sorting_test.go +++ b/pkg/common/sorting_test.go @@ -14,7 +14,6 @@ func TestSortParameter_Ascending(t *testing.T) { }) assert.Nil(t, err) assert.Equal(t, "name asc", sortParameter.GetGormOrderExpr()) - assert.Equal(t, "name", sortParameter.GetSortKey()) } func TestSortParameter_Descending(t *testing.T) { @@ -24,5 +23,4 @@ func TestSortParameter_Descending(t *testing.T) { }) assert.Nil(t, err) assert.Equal(t, "project desc", sortParameter.GetGormOrderExpr()) - assert.Equal(t, "project", sortParameter.GetSortKey()) } diff --git a/pkg/repositories/gormimpl/common.go b/pkg/repositories/gormimpl/common.go index bd0a65f75..737b83664 100644 --- a/pkg/repositories/gormimpl/common.go +++ b/pkg/repositories/gormimpl/common.go @@ -19,6 +19,7 @@ const Description = "description" const ResourceType = "resource_type" const State = "state" const ID = "id" +const CreatedAt = "created_at" const executionTableName = "executions" const namedEntityMetadataTableName = "named_entity_metadata" @@ -30,7 +31,7 @@ const taskTableName = "tasks" const limit = "limit" const filters = "filters" -var identifierGroupBy = fmt.Sprintf("%s, %s, %s", Project, Domain, Name) +var identifierGroupBy = fmt.Sprintf("%s, %s, %s, %s", Project, Domain, Name, CreatedAt) var entityToTableName = map[common.Entity]string{ common.Execution: "executions", diff --git a/pkg/repositories/gormimpl/named_entity_repo.go b/pkg/repositories/gormimpl/named_entity_repo.go index 1cca6b227..cef158fa5 100644 --- a/pkg/repositories/gormimpl/named_entity_repo.go +++ b/pkg/repositories/gormimpl/named_entity_repo.go @@ -33,16 +33,18 @@ func getSubQueryJoin(db *gorm.DB, tableName string, input interfaces.ListNamedEn Table(tableName). Where(map[string]interface{}{Project: input.Project, Domain: input.Domain}). Limit(input.Limit). + Group(identifierGroupBy). Offset(input.Offset) // Apply consistent sort ordering. if input.SortParameter != nil { - identifierGroupByWithOrderKey := fmt.Sprintf("%s, %s, %s, %s", Project, Domain, Name, input.SortParameter.GetSortKey()) - tx = tx.Group(identifierGroupByWithOrderKey) + //identifierGroupByWithOrderKey := fmt.Sprintf("%s, %s, %s, %s", Project, Domain, Name, input.SortParameter.GetSortKey()) + //tx = tx.Group(identifierGroupByWithOrderKey) tx = tx.Order(input.SortParameter.GetGormOrderExpr()) - } else { - tx = tx.Group(identifierGroupBy) } + //else { + // tx = tx.Group(identifierGroupBy) + //} return db.Joins(fmt.Sprintf(joinString, input.ResourceType), tx) } @@ -68,10 +70,10 @@ var resourceTypeToMetadataJoin = map[core.ResourceType]string{ core.ResourceType_TASK: leftJoinTaskNameToMetadata, } -var getGroupByForNamedEntity = fmt.Sprintf("%s.%s, %s.%s, %s.%s, %s.%s, %s.%s", +var getGroupByForNamedEntity = fmt.Sprintf("%s.%s, %s.%s, %s.%s, %s.%s, %s.%s, %s.%s", innerJoinTableAlias, Project, innerJoinTableAlias, Domain, innerJoinTableAlias, Name, namedEntityMetadataTableName, Description, - namedEntityMetadataTableName, State) + namedEntityMetadataTableName, State, namedEntityMetadataTableName, CreatedAt) func getSelectForNamedEntity(tableName string, resourceType core.ResourceType) []string { return []string{ @@ -194,7 +196,6 @@ func (r *NamedEntityRepo) List(ctx context.Context, input interfaces.ListNamedEn } // Apply sort ordering. if input.SortParameter != nil { - tx = tx.Group(input.SortParameter.GetSortKey()) tx = tx.Order(input.SortParameter.GetGormOrderExpr()) } diff --git a/pkg/repositories/gormimpl/named_entity_repo_test.go b/pkg/repositories/gormimpl/named_entity_repo_test.go index 84277fd8c..7824b3db8 100644 --- a/pkg/repositories/gormimpl/named_entity_repo_test.go +++ b/pkg/repositories/gormimpl/named_entity_repo_test.go @@ -157,7 +157,7 @@ func TestListNamedEntity(t *testing.T) { mockQuery := GlobalMock.NewMock() mockQuery.WithQuery( - `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name, name ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY "name",entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state ORDER BY name desc`).WithReply(results) + `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name, created_at ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state, named_entity_metadata.created_at ORDER BY name desc`).WithReply(results) sortParameter, _ := common.NewSortParameter(admin.Sort{ Direction: admin.Sort_DESCENDING, @@ -183,7 +183,7 @@ func TestListNamedEntityTxErrorCases(t *testing.T) { mockQuery := GlobalMock.NewMock() mockQuery.WithQuery( - `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name, name ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY "name",entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state ORDER BY name desc`).WithError(fmt.Errorf("failed")) + `SELECT entities.project,entities.domain,entities.name,'2' AS resource_type,named_entity_metadata.description,named_entity_metadata.state FROM "named_entity_metadata" RIGHT JOIN (SELECT project,domain,name FROM "workflows" WHERE "domain" = $1 AND "project" = $2 GROUP BY project, domain, name, created_at ORDER BY name desc LIMIT 20) AS entities ON named_entity_metadata.resource_type = 2 AND named_entity_metadata.project = entities.project AND named_entity_metadata.domain = entities.domain AND named_entity_metadata.name = entities.name GROUP BY entities.project, entities.domain, entities.name, named_entity_metadata.description, named_entity_metadata.state, named_entity_metadata.created_at ORDER BY name desc%`).WithError(fmt.Errorf("failed")) sortParameter, _ := common.NewSortParameter(admin.Sort{ Direction: admin.Sort_DESCENDING, From 47bb2eccbad00a61ea10a58f75bea9e82fa2cfab Mon Sep 17 00:00:00 2001 From: Prafulla Mahindrakar Date: Fri, 8 Apr 2022 18:32:20 +0530 Subject: [PATCH 4/5] removed unused Signed-off-by: Prafulla Mahindrakar --- pkg/common/sorting.go | 1 - pkg/repositories/gormimpl/named_entity_repo.go | 5 ----- 2 files changed, 6 deletions(-) diff --git a/pkg/common/sorting.go b/pkg/common/sorting.go index 3b71d7427..c4922d0b1 100644 --- a/pkg/common/sorting.go +++ b/pkg/common/sorting.go @@ -17,7 +17,6 @@ type SortParameter interface { type sortParamImpl struct { gormOrderExpression string - sortKey string } func (s *sortParamImpl) GetGormOrderExpr() string { diff --git a/pkg/repositories/gormimpl/named_entity_repo.go b/pkg/repositories/gormimpl/named_entity_repo.go index cef158fa5..0e698601e 100644 --- a/pkg/repositories/gormimpl/named_entity_repo.go +++ b/pkg/repositories/gormimpl/named_entity_repo.go @@ -38,13 +38,8 @@ func getSubQueryJoin(db *gorm.DB, tableName string, input interfaces.ListNamedEn // Apply consistent sort ordering. if input.SortParameter != nil { - //identifierGroupByWithOrderKey := fmt.Sprintf("%s, %s, %s, %s", Project, Domain, Name, input.SortParameter.GetSortKey()) - //tx = tx.Group(identifierGroupByWithOrderKey) tx = tx.Order(input.SortParameter.GetGormOrderExpr()) } - //else { - // tx = tx.Group(identifierGroupBy) - //} return db.Joins(fmt.Sprintf(joinString, input.ResourceType), tx) } From 0e3915ca2b273795f3f1138d0fefda7407797b64 Mon Sep 17 00:00:00 2001 From: Prafulla Mahindrakar Date: Fri, 8 Apr 2022 19:18:53 +0530 Subject: [PATCH 5/5] using distinct to filter results Signed-off-by: Prafulla Mahindrakar --- pkg/repositories/gormimpl/launch_plan_repo.go | 2 +- pkg/repositories/gormimpl/named_entity_repo.go | 6 +++--- pkg/repositories/gormimpl/task_repo.go | 2 +- pkg/repositories/gormimpl/workflow_repo.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/repositories/gormimpl/launch_plan_repo.go b/pkg/repositories/gormimpl/launch_plan_repo.go index dc379ed03..a1462feca 100644 --- a/pkg/repositories/gormimpl/launch_plan_repo.go +++ b/pkg/repositories/gormimpl/launch_plan_repo.go @@ -166,7 +166,7 @@ func (r *LaunchPlanRepo) ListLaunchPlanIdentifiers(ctx context.Context, input in // Scan the results into a list of launch plans var launchPlans []models.LaunchPlan timer := r.metrics.ListIdentifiersDuration.Start() - tx.Select([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&launchPlans) + tx.Distinct([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&launchPlans) timer.Stop() if tx.Error != nil { return interfaces.LaunchPlanCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error) diff --git a/pkg/repositories/gormimpl/named_entity_repo.go b/pkg/repositories/gormimpl/named_entity_repo.go index 0e698601e..3e7813284 100644 --- a/pkg/repositories/gormimpl/named_entity_repo.go +++ b/pkg/repositories/gormimpl/named_entity_repo.go @@ -33,8 +33,8 @@ func getSubQueryJoin(db *gorm.DB, tableName string, input interfaces.ListNamedEn Table(tableName). Where(map[string]interface{}{Project: input.Project, Domain: input.Domain}). Limit(input.Limit). - Group(identifierGroupBy). - Offset(input.Offset) + Offset(input.Offset). + Group(identifierGroupBy) // Apply consistent sort ordering. if input.SortParameter != nil { @@ -198,7 +198,7 @@ func (r *NamedEntityRepo) List(ctx context.Context, input interfaces.ListNamedEn var entities []models.NamedEntity timer := r.metrics.ListDuration.Start() - tx.Select(getSelectForNamedEntity(innerJoinTableAlias, input.ResourceType)).Table(namedEntityMetadataTableName).Group(getGroupByForNamedEntity).Scan(&entities) + tx.Distinct(getSelectForNamedEntity(innerJoinTableAlias, input.ResourceType)).Table(namedEntityMetadataTableName).Group(getGroupByForNamedEntity).Scan(&entities) timer.Stop() diff --git a/pkg/repositories/gormimpl/task_repo.go b/pkg/repositories/gormimpl/task_repo.go index f48c6ca11..ecfbc8a28 100644 --- a/pkg/repositories/gormimpl/task_repo.go +++ b/pkg/repositories/gormimpl/task_repo.go @@ -113,7 +113,7 @@ func (r *TaskRepo) ListTaskIdentifiers(ctx context.Context, input interfaces.Lis // Scan the results into a list of tasks var tasks []models.Task timer := r.metrics.ListIdentifiersDuration.Start() - tx.Select([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&tasks) + tx.Distinct([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&tasks) timer.Stop() if tx.Error != nil { return interfaces.TaskCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error) diff --git a/pkg/repositories/gormimpl/workflow_repo.go b/pkg/repositories/gormimpl/workflow_repo.go index 2c78cb2c4..69405b367 100644 --- a/pkg/repositories/gormimpl/workflow_repo.go +++ b/pkg/repositories/gormimpl/workflow_repo.go @@ -110,7 +110,7 @@ func (r *WorkflowRepo) ListIdentifiers(ctx context.Context, input interfaces.Lis // Scan the results into a list of workflows var workflows []models.Workflow timer := r.metrics.ListIdentifiersDuration.Start() - tx.Select([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&workflows) + tx.Distinct([]string{Project, Domain, Name}).Group(identifierGroupBy).Scan(&workflows) timer.Stop() if tx.Error != nil { return interfaces.WorkflowCollectionOutput{}, r.errorTransformer.ToFlyteAdminError(tx.Error)