Skip to content

Commit

Permalink
pgsql missing some groupby's (some like title are unnecessary, but it…
Browse files Browse the repository at this point in the history
… works for now)
  • Loading branch information
NodudeWasTaken committed Oct 21, 2024
1 parent db0cbc2 commit 1ab44eb
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pkg/sqlite/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,11 +941,11 @@ func (qb *FileStore) setQuerySort(query *queryBuilder, findFilter *models.FindFi
case "path":
// special handling for path
query.sortAndPagination += fmt.Sprintf(" ORDER BY folders.path %s, files.basename %[1]s", direction)
query.addGroupBy([]string{"folders.path", "files.basename"})
query.addGroupBy("folders.path", "files.basename")
default:
add, agg := getSort(sort, direction, "files")
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlite/gallery.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,26 +838,26 @@ func (qb *GalleryStore) setGallerySort(query *queryBuilder, findFilter *models.F
addFileTable()
addFolderTable()
query.sortAndPagination += fmt.Sprintf(" ORDER BY COALESCE(folders.path, '') || COALESCE(file_folder.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI %s", direction)
query.addGroupBy([]string{"folders.path", "file_folder.path", "files.basename"})
query.addGroupBy("folders.path", "file_folder.path", "files.basename")
case "file_mod_time":
sort = "mod_time"
addFileTable()
add, agg := getSort(sort, direction, fileTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "title":
addFileTable()
addFolderTable()
query.sortAndPagination += " ORDER BY COALESCE(galleries.title, files.basename, basename(COALESCE(folders.path, ''))) COLLATE NATURAL_CI " + direction + ", file_folder.path COLLATE NATURAL_CI " + direction
default:
add, agg := getSort(sort, direction, "galleries")
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
}

// Whatever the sorting, always use title/id as a final sort
query.sortAndPagination += ", COALESCE(galleries.title, CAST(galleries.id as text)) COLLATE NATURAL_CI ASC"
query.addGroupBy([]string{"galleries.title", "galleries.id"})
query.addGroupBy("galleries.title", "galleries.id")

return nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlite/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,14 @@ func (qb *GroupStore) setGroupSort(query *queryBuilder, findFilter *models.FindF
if query.hasJoin("groups_parents") {
add, agg := getSort("order_index", direction, "groups_parents")
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
} else {
// this will give unexpected results if the query is not filtered by a parent group and
// the group has multiple parents and order indexes
query.join(groupRelationsTable, "", "groups.id = groups_relations.sub_id")
add, agg := getSort("order_index", direction, groupRelationsTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
}
case "tag_count":
query.sortAndPagination += getCountSort(groupTable, groupsTagsTable, groupIDColumn, direction)
Expand All @@ -531,12 +531,12 @@ func (qb *GroupStore) setGroupSort(query *queryBuilder, findFilter *models.FindF
default:
add, agg := getSort(sort, direction, "groups")
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
}

// Whatever the sorting, always use name/id as a final sort
query.sortAndPagination += ", COALESCE(groups.name, CAST(groups.id as text)) COLLATE NATURAL_CI ASC"
query.addGroupBy([]string{"groups.name", "groups.id"})
query.addGroupBy("groups.name", "groups.id")
return nil
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/sqlite/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ func (qb *ImageStore) queryGroupedFields(ctx context.Context, options models.Ima
)
query.addColumn("COALESCE(image_files.width, 0) * COALESCE(image_files.height, 0) as megapixels")
aggregateQuery.addColumn("COALESCE(SUM(temp.megapixels), 0) / 1000000 as megapixels")
query.addGroupBy("image_files.width", "image_files.height")
}

if options.TotalSize {
Expand All @@ -880,6 +881,7 @@ func (qb *ImageStore) queryGroupedFields(ctx context.Context, options models.Ima
)
query.addColumn("COALESCE(files.size, 0) as size")
aggregateQuery.addColumn("SUM(temp.size) as size")
query.addGroupBy("files.size")
}

const includeSortPagination = false
Expand Down Expand Up @@ -969,7 +971,7 @@ func (qb *ImageStore) setImageSortAndPagination(q *queryBuilder, findFilter *mod
addFilesJoin()
addFolderJoin()
sortClause = " ORDER BY COALESCE(folders.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI " + direction
q.addGroupBy([]string{"folders.path", "files.basename"})
q.addGroupBy("folders.path", "files.basename")
case "file_count":
sortClause = getCountSort(imageTable, imagesFilesTable, imageIDColumn, direction)
case "tag_count":
Expand All @@ -980,21 +982,21 @@ func (qb *ImageStore) setImageSortAndPagination(q *queryBuilder, findFilter *mod
addFilesJoin()
add, agg := getSort(sort, direction, "files")
sortClause = add
q.addGroupBy(agg)
q.addGroupBy(agg...)
case "title":
addFilesJoin()
addFolderJoin()
sortClause = " ORDER BY COALESCE(images.title, files.basename) COLLATE NATURAL_CI " + direction + ", folders.path COLLATE NATURAL_CI " + direction
q.addGroupBy([]string{"images.title", "files.basename", "folders.path"})
q.addGroupBy("images.title", "files.basename", "folders.path")
default:
add, agg := getSort(sort, direction, "images")
sortClause = add
q.addGroupBy(agg)
q.addGroupBy(agg...)
}

// Whatever the sorting, always use title/id as a final sort
sortClause += ", COALESCE(images.title, CAST(images.id as text)) COLLATE NATURAL_CI ASC"
q.addGroupBy([]string{"images.title", "images.id"})
q.addGroupBy("images.title", "images.id")
}

q.sortAndPagination = sortClause + getPagination(findFilter)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/performer.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (qb *PerformerStore) makeQuery(ctx context.Context, performerFilter *models
return nil, err
}
query.sortAndPagination += getPagination(findFilter)
query.addGroupBy(agg)
query.addGroupBy(agg...)

return &query, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (qb *queryBuilder) addColumn(column string) {
qb.columns = append(qb.columns, column)
}

func (qb *queryBuilder) addGroupBy(columns []string) {
func (qb *queryBuilder) addGroupBy(columns ...string) {
if len(columns) > 0 {
qb.groupByClauses = sliceutil.AppendUniques(qb.groupByClauses, columns)
}
Expand Down
28 changes: 15 additions & 13 deletions pkg/sqlite/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ func (qb *SceneStore) queryGroupedFields(ctx context.Context, options models.Sce
)
query.addColumn("COALESCE(video_files.duration, 0) as duration")
aggregateQuery.addColumn("SUM(temp.duration) as duration")
query.addGroupBy("video_files.duration")
}

if options.TotalSize {
Expand All @@ -1022,6 +1023,7 @@ func (qb *SceneStore) queryGroupedFields(ctx context.Context, options models.Sce
)
query.addColumn("COALESCE(files.size, 0) as size")
aggregateQuery.addColumn("SUM(temp.size) as size")
query.addGroupBy("files.size")
}

const includeSortPagination = false
Expand Down Expand Up @@ -1132,12 +1134,12 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF
query.join(groupsScenesTable, "", "scenes.id = groups_scenes.scene_id")
add, group := getSort("scene_index", direction, groupsScenesTable)
query.sortAndPagination += add
query.addGroupBy(group)
query.addGroupBy(group...)
case "group_scene_number":
query.join(groupsScenesTable, "scene_group", "scenes.id = scene_group.scene_id")
add, group := getSort("scene_index", direction, "scene_group")
query.sortAndPagination += add
query.addGroupBy(group)
query.addGroupBy(group...)
case "tag_count":
query.sortAndPagination += getCountSort(sceneTable, scenesTagsTable, sceneIDColumn, direction)
case "performer_count":
Expand All @@ -1149,7 +1151,7 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF
addFileTable()
addFolderTable()
query.sortAndPagination += fmt.Sprintf(" ORDER BY COALESCE(folders.path, '') || COALESCE(files.basename, '') COLLATE NATURAL_CI %s", direction)
query.addGroupBy([]string{"folders.path", "files.basename"})
query.addGroupBy("folders.path", "files.basename")
case "perceptual_similarity":
// special handling for phash
addFileTable()
Expand All @@ -1162,45 +1164,45 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF
)

query.sortAndPagination += " ORDER BY fingerprints_phash.fingerprint " + direction + ", files.size DESC"
query.addGroupBy([]string{"fingerprints_phash.fingerprint", "files.size"})
query.addGroupBy("fingerprints_phash.fingerprint", "files.size")
case "bitrate":
sort = "bit_rate"
addVideoFileTable()
add, group := getSort(sort, direction, videoFileTable)
query.sortAndPagination += add
query.addGroupBy(group)
query.addGroupBy(group...)
case "file_mod_time":
sort = "mod_time"
addFileTable()
add, agg := getSort(sort, direction, fileTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "framerate":
sort = "frame_rate"
addVideoFileTable()
add, agg := getSort(sort, direction, videoFileTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "filesize":
addFileTable()
add, agg := getSort(sort, direction, fileTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "duration":
addVideoFileTable()
add, agg := getSort(sort, direction, videoFileTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "interactive", "interactive_speed":
addVideoFileTable()
add, agg := getSort(sort, direction, videoFileTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "title":
addFileTable()
addFolderTable()
query.sortAndPagination += " ORDER BY COALESCE(scenes.title, files.basename) COLLATE NATURAL_CI " + direction + ", folders.path COLLATE NATURAL_CI " + direction
query.addGroupBy([]string{"scenes.title", "files.basename", "folders.path"})
query.addGroupBy("scenes.title", "files.basename", "folders.path")
case "play_count":
query.sortAndPagination += getCountSort(sceneTable, scenesViewDatesTable, sceneIDColumn, direction)
case "last_played_at":
Expand All @@ -1212,12 +1214,12 @@ func (qb *SceneStore) setSceneSort(query *queryBuilder, findFilter *models.FindF
default:
add, agg := getSort(sort, direction, "scenes")
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
}

// Whatever the sorting, always use title/id as a final sort
query.sortAndPagination += ", COALESCE(scenes.title, CAST(scenes.id as text)) COLLATE NATURAL_CI ASC"
query.addGroupBy([]string{"scenes.title", "scenes.id"})
query.addGroupBy("scenes.title", "scenes.id")

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlite/scene_marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,14 @@ func (qb *SceneMarkerStore) setSceneMarkerSort(query *queryBuilder, findFilter *
query.join(sceneTable, "", "scenes.id = scene_markers.scene_id")
add, agg := getSort(sort, direction, sceneTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
case "title":
query.join(tagTable, "", "scene_markers.primary_tag_id = tags.id")
query.sortAndPagination += " ORDER BY COALESCE(NULLIF(scene_markers.title,''), tags.name) COLLATE NATURAL_CI " + direction
default:
add, agg := getSort(sort, direction, sceneMarkerTable)
query.sortAndPagination += add
query.addGroupBy(agg)
query.addGroupBy(agg...)
}

query.sortAndPagination += ", scene_markers.scene_id ASC, scene_markers.seconds ASC"
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func selectAll(tableName string) string {
func distinctIDs(qb *queryBuilder, tableName string) {
columnId := getColumn(tableName, "id")
qb.addColumn(columnId)
qb.addGroupBy([]string{columnId})
qb.addGroupBy(columnId)
qb.from = tableName
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/studio.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func (qb *StudioStore) makeQuery(ctx context.Context, studioFilter *models.Studi
return nil, err
}
query.sortAndPagination += getPagination(findFilter)
query.addGroupBy(group)
query.addGroupBy(group...)

return &query, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func (qb *TagStore) Query(ctx context.Context, tagFilter *models.TagFilterType,
return nil, 0, err
}
query.sortAndPagination += getPagination(findFilter)
query.addGroupBy(group)
query.addGroupBy(group...)
idsResult, countResult, err := query.executeFind(ctx)
if err != nil {
return nil, 0, err
Expand Down

0 comments on commit 1ab44eb

Please sign in to comment.