github.com/cloudberrydb/gpbackup@v1.0.3-0.20240118031043-5410fd45eed6/integration/statistics_queries_test.go (about) 1 package integration 2 3 import ( 4 "sort" 5 6 "github.com/cloudberrydb/gp-common-go-libs/structmatcher" 7 "github.com/cloudberrydb/gp-common-go-libs/testhelper" 8 "github.com/cloudberrydb/gpbackup/backup" 9 "github.com/cloudberrydb/gpbackup/testutils" 10 11 . "github.com/onsi/ginkgo/v2" 12 . "github.com/onsi/gomega" 13 ) 14 15 var _ = Describe("backup integration tests", func() { 16 tables := []backup.Table{ 17 {Relation: backup.Relation{Schema: "public", Name: "foo"}}, 18 } 19 var tableOid uint32 20 BeforeEach(func() { 21 testhelper.AssertQueryRuns(connectionPool, "CREATE TABLE public.foo(i int, j text, k bool)") 22 tableOid = testutils.OidFromObjectName(connectionPool, "public", "foo", backup.TYPE_RELATION) 23 testhelper.AssertQueryRuns(connectionPool, "INSERT INTO public.foo VALUES (1, 'a', 't')") 24 testhelper.AssertQueryRuns(connectionPool, "INSERT INTO public.foo VALUES (2, 'b', 'f')") 25 testhelper.AssertQueryRuns(connectionPool, "ANALYZE public.foo") 26 }) 27 AfterEach(func() { 28 testhelper.AssertQueryRuns(connectionPool, "DROP TABLE public.foo") 29 }) 30 Describe("GetAttributeStatistics", func() { 31 It("returns attribute statistics for a table", func() { 32 attStats := backup.GetAttributeStatistics(connectionPool, tables) 33 Expect(attStats).To(HaveLen(1)) 34 Expect(attStats[tableOid]).To(HaveLen(3)) 35 tableAttStatsI := attStats[tableOid][0] 36 tableAttStatsJ := attStats[tableOid][1] 37 tableAttStatsK := attStats[tableOid][2] 38 39 /* 40 * Attribute statistics will vary by GPDB version, but statistics for a 41 * certain table should always be the same in a particular version given 42 * the same schema and data. 43 */ 44 expectedStats5I := backup.AttributeStatistic{Oid: tableOid, Schema: "public", Table: "foo", AttName: "i", 45 Type: "int4", Relid: tableOid, AttNumber: 1, Inherit: false, Width: 4, Distinct: -1, Kind1: 2, Kind2: 3, Operator1: 97, 46 Operator2: 97, Numbers2: []string{"1"}, Values1: []string{"1", "2"}} 47 expectedStats5J := backup.AttributeStatistic{Oid: tableOid, Schema: "public", Table: "foo", AttName: "j", 48 Type: "text", Relid: tableOid, AttNumber: 2, Inherit: false, Width: 2, Distinct: -1, Kind1: 2, Kind2: 3, Operator1: 664, 49 Operator2: 664, Numbers2: []string{"1"}, Values1: []string{"a", "b"}} 50 expectedStats5K := backup.AttributeStatistic{Oid: tableOid, Schema: "public", Table: "foo", AttName: "k", 51 Type: "bool", Relid: tableOid, AttNumber: 3, Inherit: false, Width: 1, Distinct: -1, Kind1: 2, Kind2: 3, Operator1: 58, 52 Operator2: 58, Numbers2: []string{"-1"}, Values1: []string{"f", "t"}} 53 if true { 54 expectedStats5J.Collation1 = 100 55 expectedStats5J.Collation2 = 100 56 } 57 58 // The order in which the stavalues1 values is returned is not guaranteed to be deterministic 59 sort.Strings(tableAttStatsI.Values1) 60 sort.Strings(tableAttStatsJ.Values1) 61 sort.Strings(tableAttStatsK.Values1) 62 structmatcher.ExpectStructsToMatchExcluding(&expectedStats5I, &tableAttStatsI, "Numbers2") 63 structmatcher.ExpectStructsToMatchExcluding(&expectedStats5J, &tableAttStatsJ, "Numbers2") 64 structmatcher.ExpectStructsToMatchExcluding(&expectedStats5K, &tableAttStatsK, "Numbers2") 65 }) 66 }) 67 Describe("GetTupleStatistics", func() { 68 It("returns tuple statistics for a table", func() { 69 tupleStats := backup.GetTupleStatistics(connectionPool, tables) 70 Expect(tupleStats).To(HaveLen(1)) 71 tableTupleStats := tupleStats[tableOid] 72 73 // Tuple statistics will not vary by GPDB version. Relpages may vary based on the hardware. 74 expectedStats := backup.TupleStatistic{Oid: tableOid, Schema: "public", Table: "foo", RelTuples: 2} 75 76 structmatcher.ExpectStructsToMatchExcluding(&expectedStats, &tableTupleStats, "RelPages") 77 }) 78 }) 79 })