github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/metadata_test.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package opt_test 12 13 import ( 14 "context" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/sql/opt" 18 "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" 19 "github.com/cockroachdb/cockroach/pkg/sql/opt/testutils/testcat" 20 "github.com/cockroachdb/cockroach/pkg/sql/privilege" 21 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 22 "github.com/cockroachdb/cockroach/pkg/sql/types" 23 ) 24 25 func TestMetadata(t *testing.T) { 26 var md opt.Metadata 27 schID := md.AddSchema(&testcat.Schema{}) 28 colID := md.AddColumn("col", types.Int) 29 tabID := md.AddTable(&testcat.Table{}, &tree.TableName{}) 30 seqID := md.AddSequence(&testcat.Sequence{}) 31 md.AddView(&testcat.View{}) 32 33 // Call Init and add objects from catalog, verifying that IDs have been reset. 34 testCat := testcat.New() 35 tab := &testcat.Table{Revoked: true} 36 testCat.AddTable(tab) 37 38 md.Init() 39 if md.AddSchema(testCat.Schema()) != schID { 40 t.Fatalf("unexpected schema id") 41 } 42 if md.AddColumn("col2", types.Int) != colID { 43 t.Fatalf("unexpected column id") 44 } 45 if md.AddTable(tab, &tree.TableName{}) != tabID { 46 t.Fatalf("unexpected table id") 47 } 48 if md.AddSequence(&testcat.Sequence{SeqID: 100}) != seqID { 49 t.Fatalf("unexpected sequence id") 50 } 51 52 md.AddView(&testcat.View{ViewID: 101}) 53 if len(md.AllViews()) != 1 { 54 t.Fatalf("unexpected views") 55 } 56 57 md.AddDependency(opt.DepByName(&tab.TabName), tab, privilege.CREATE) 58 depsUpToDate, err := md.CheckDependencies(context.Background(), testCat) 59 if err == nil || depsUpToDate { 60 t.Fatalf("expected table privilege to be revoked") 61 } 62 63 // Call CopyFrom and verify that same objects are present in new metadata. 64 var mdNew opt.Metadata 65 mdNew.CopyFrom(&md) 66 if mdNew.Schema(schID) != testCat.Schema() { 67 t.Fatalf("unexpected schema") 68 } 69 if mdNew.ColumnMeta(colID).Alias != "col2" { 70 t.Fatalf("unexpected column") 71 } 72 73 if mdNew.TableMeta(tabID).Table != tab { 74 t.Fatalf("unexpected table") 75 } 76 77 if mdNew.Sequence(seqID).(*testcat.Sequence).SeqID != 100 { 78 t.Fatalf("unexpected sequence") 79 } 80 81 if v := mdNew.AllViews(); len(v) != 1 && v[0].(*testcat.View).ViewID != 101 { 82 t.Fatalf("unexpected view") 83 } 84 85 depsUpToDate, err = md.CheckDependencies(context.Background(), testCat) 86 if err == nil || depsUpToDate { 87 t.Fatalf("expected table privilege to be revoked in metadata copy") 88 } 89 } 90 91 func TestMetadataSchemas(t *testing.T) { 92 var md opt.Metadata 93 94 sch := &testcat.Schema{ 95 SchemaID: 1, 96 SchemaName: cat.SchemaName{CatalogName: "db", SchemaName: "schema"}, 97 } 98 99 schID := md.AddSchema(sch) 100 lookup := md.Schema(schID) 101 if lookup.ID() != 1 { 102 t.Fatalf("unexpected schema id: %d", lookup.ID()) 103 } 104 if lookup.Name().String() != sch.SchemaName.String() { 105 t.Fatalf("unexpected schema name: %s", lookup.Name()) 106 } 107 } 108 109 func TestMetadataColumns(t *testing.T) { 110 var md opt.Metadata 111 112 // Add standalone column. 113 colID := md.AddColumn("alias", types.Int) 114 if colID != 1 { 115 t.Fatalf("unexpected column id: %d", colID) 116 } 117 118 colMeta := md.ColumnMeta(colID) 119 if colMeta.Alias != "alias" { 120 t.Fatalf("unexpected column alias: %s", colMeta.Alias) 121 } 122 123 if colMeta.Type.Family() != types.IntFamily { 124 t.Fatalf("unexpected column type: %s", colMeta.Type) 125 } 126 127 if n := md.NumColumns(); n != 1 { 128 t.Fatalf("unexpected num columns: %d", n) 129 } 130 131 // Add another column. 132 colID = md.AddColumn("alias2", types.String) 133 if colID != 2 { 134 t.Fatalf("unexpected column id: %d", colID) 135 } 136 137 colMeta = md.ColumnMeta(colID) 138 if colMeta.Alias != "alias2" { 139 t.Fatalf("unexpected column alias: %s", colMeta.Alias) 140 } 141 142 if colMeta.Type.Family() != types.StringFamily { 143 t.Fatalf("unexpected column type: %s", colMeta.Type) 144 } 145 146 if n := md.NumColumns(); n != 2 { 147 t.Fatalf("unexpected num columns: %d", n) 148 } 149 } 150 151 func TestMetadataTables(t *testing.T) { 152 var md opt.Metadata 153 154 // Add a table reference to the metadata. 155 a := &testcat.Table{TabID: 1} 156 a.TabName = tree.MakeUnqualifiedTableName(tree.Name("a")) 157 x := &testcat.Column{Name: "x"} 158 y := &testcat.Column{Name: "y"} 159 a.Columns = append(a.Columns, x, y) 160 161 tabID := md.AddTable(a, &tree.TableName{}) 162 if tabID == 0 { 163 t.Fatalf("unexpected table id: %d", tabID) 164 } 165 166 tab := md.Table(tabID) 167 if tab != a { 168 t.Fatal("table didn't match table added to metadata") 169 } 170 171 colID := tabID.ColumnID(0) 172 if colID == 0 { 173 t.Fatalf("unexpected column id: %d", colID) 174 } 175 176 colMeta := md.ColumnMeta(colID) 177 if colMeta.Alias != "x" { 178 t.Fatalf("unexpected column alias: %s", colMeta.Alias) 179 } 180 181 // Add another table reference to the metadata. 182 b := &testcat.Table{TabID: 1} 183 b.TabName = tree.MakeUnqualifiedTableName(tree.Name("b")) 184 b.Columns = append(b.Columns, &testcat.Column{Name: "x"}) 185 186 otherTabID := md.AddTable(b, &tree.TableName{}) 187 if otherTabID == tabID { 188 t.Fatalf("unexpected table id: %d", tabID) 189 } 190 191 alias := md.ColumnMeta(otherTabID.ColumnID(0)).Alias 192 if alias != "x" { 193 t.Fatalf("unexpected column alias: %s", alias) 194 } 195 } 196 197 // TestIndexColumns tests that we can extract a set of columns from an index ordinal. 198 func TestIndexColumns(t *testing.T) { 199 cat := testcat.New() 200 _, err := cat.ExecuteDDL( 201 "CREATE TABLE a (" + 202 "k INT PRIMARY KEY, " + 203 "i INT, " + 204 "s STRING, " + 205 "f FLOAT, " + 206 "INDEX (i, k), " + 207 "INDEX (s DESC) STORING(f))") 208 if err != nil { 209 t.Fatal(err) 210 } 211 212 var md opt.Metadata 213 tn := tree.NewUnqualifiedTableName("a") 214 a := md.AddTable(cat.Table(tn), tn) 215 216 k := a.ColumnID(0) 217 i := a.ColumnID(1) 218 s := a.ColumnID(2) 219 f := a.ColumnID(3) 220 221 testCases := []struct { 222 index int 223 expectedCols opt.ColSet 224 }{ 225 {1, opt.MakeColSet(k, i)}, 226 {2, opt.MakeColSet(s, f, k)}, 227 } 228 229 for _, tc := range testCases { 230 actual := md.TableMeta(a).IndexColumns(tc.index) 231 if !tc.expectedCols.Equals(actual) { 232 t.Errorf("expected %v, got %v", tc.expectedCols, actual) 233 } 234 } 235 }