Skip to content

Commit

Permalink
GetAttributeStatistics callback
Browse files Browse the repository at this point in the history
  • Loading branch information
RekGRpth committed May 24, 2024
1 parent 9812a2c commit 81953c1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
20 changes: 6 additions & 14 deletions backup/queries_statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/greenplum-db/gp-common-go-libs/dbconn"
"github.com/greenplum-db/gp-common-go-libs/gplog"
"github.com/greenplum-db/gpbackup/utils"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
)

Expand Down Expand Up @@ -54,7 +53,7 @@ type AttributeStatistic struct {
Values5 pq.StringArray `db:"stavalues5"`
}

func getAttributeStatisticsRows(connectionPool *dbconn.DBConn, tables []Table) (*sqlx.Rows, error) {
func GetAttributeStatistics(connectionPool *dbconn.DBConn, tables []Table, processRow func(attStat *AttributeStatistic)) {
inheritClause := ""
statSlotClause := ""
if connectionPool.Version.AtLeast("6") {
Expand Down Expand Up @@ -120,21 +119,14 @@ func getAttributeStatisticsRows(connectionPool *dbconn.DBConn, tables []Table) (
inheritClause, statSlotClause, statCollationClause,
SchemaFilterClause("n"), utils.SliceToQuotedString(tablenames))

return connectionPool.Query(query)
}

func GetAttributeStatistics(connectionPool *dbconn.DBConn, tables []Table) map[uint32][]AttributeStatistic {
results, err := getAttributeStatisticsRows(connectionPool, tables)
gplog.FatalOnError(err)
stats := make(map[uint32][]AttributeStatistic)
for results.Next() {
rows, err := connectionPool.Query(query)
for rows.Next() {
var attStat AttributeStatistic
err = results.StructScan(&attStat)
err = rows.StructScan(&attStat)
gplog.FatalOnError(err)
stats[attStat.Oid] = append(stats[attStat.Oid], attStat)
processRow(&attStat)
}
gplog.FatalOnError(results.Err())
return stats
gplog.FatalOnError(rows.Err())
}

type TupleStatistic struct {
Expand Down
12 changes: 3 additions & 9 deletions backup/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,9 @@ func backupTableStatistics(statisticsFile *utils.FileWithByteCount, tables []Tab
GetTupleStatistics(connectionPool, tables, func(tupleStat *TupleStatistic) {
printTupleStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[tupleStat.Oid], *tupleStat)
})
attStats, err := getAttributeStatisticsRows(connectionPool, tables)
gplog.FatalOnError(err)
for attStats.Next() {
var attStat AttributeStatistic
err = attStats.StructScan(&attStat)
gplog.FatalOnError(err)
printAttributeStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[attStat.Oid], attStat)
}
gplog.FatalOnError(attStats.Err())
GetAttributeStatistics(connectionPool, tables, func(attStat *AttributeStatistic) {
printAttributeStatisticsStatementForTable(statisticsFile, globalTOC, tablesMap[attStat.Oid], *attStat)
})
}

func backupIncrementalMetadata() {
Expand Down
20 changes: 16 additions & 4 deletions integration/statistics_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ var _ = Describe("backup integration tests", func() {
oldTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION)
tables[0].Oid = oldTableOid

beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables)
beforeAttStats := make(map[uint32][]backup.AttributeStatistic)
backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) {
beforeAttStats[attStat.Oid] = append(beforeAttStats[attStat.Oid], *attStat)
})
beforeTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
beforeTupleStats[tupleStat.Oid] = *tupleStat
Expand All @@ -47,7 +50,10 @@ var _ = Describe("backup integration tests", func() {

newTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION)
tables[0].Oid = newTableOid
afterAttStats := backup.GetAttributeStatistics(connectionPool, tables)
afterAttStats := make(map[uint32][]backup.AttributeStatistic)
backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) {
afterAttStats[attStat.Oid] = append(afterAttStats[attStat.Oid], *attStat)
})
afterTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
afterTupleStats[tupleStat.Oid] = *tupleStat
Expand Down Expand Up @@ -80,7 +86,10 @@ var _ = Describe("backup integration tests", func() {
oldTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo''\"''''bar", backup.TYPE_RELATION)
tables[0].Oid = oldTableOid

beforeAttStats := backup.GetAttributeStatistics(connectionPool, tables)
beforeAttStats := make(map[uint32][]backup.AttributeStatistic)
backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) {
beforeAttStats[attStat.Oid] = append(beforeAttStats[attStat.Oid], *attStat)
})
beforeTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
beforeTupleStats[tupleStat.Oid] = *tupleStat
Expand All @@ -97,7 +106,10 @@ var _ = Describe("backup integration tests", func() {

newTableOid := testutils.OidFromObjectName(connectionPool, "public", "foo''\"''''bar", backup.TYPE_RELATION)
tables[0].Oid = newTableOid
afterAttStats := backup.GetAttributeStatistics(connectionPool, tables)
afterAttStats := make(map[uint32][]backup.AttributeStatistic)
backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) {
afterAttStats[attStat.Oid] = append(afterAttStats[attStat.Oid], *attStat)
})
afterTupleStats := make(map[uint32]backup.TupleStatistic)
backup.GetTupleStatistics(connectionPool, tables, func(tupleStat *backup.TupleStatistic) {
afterTupleStats[tupleStat.Oid] = *tupleStat
Expand Down
5 changes: 4 additions & 1 deletion integration/statistics_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ var _ = Describe("backup integration tests", func() {
})
Describe("GetAttributeStatistics", func() {
It("returns attribute statistics for a table", func() {
attStats := backup.GetAttributeStatistics(connectionPool, tables)
attStats := make(map[uint32][]backup.AttributeStatistic)
backup.GetAttributeStatistics(connectionPool, tables, func(attStat *backup.AttributeStatistic) {
attStats[attStat.Oid] = append(attStats[attStat.Oid], *attStat)
})
Expect(attStats).To(HaveLen(1))
Expect(attStats[tableOid]).To(HaveLen(3))
tableAttStatsI := attStats[tableOid][0]
Expand Down

0 comments on commit 81953c1

Please sign in to comment.