github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/join_test.go (about) 1 // Copyright 2017 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 sql 12 13 import ( 14 "fmt" 15 16 "github.com/cockroachdb/cockroach/pkg/keys" 17 "github.com/cockroachdb/cockroach/pkg/kv" 18 "github.com/cockroachdb/cockroach/pkg/sql/opt/exec" 19 "github.com/cockroachdb/cockroach/pkg/sql/span" 20 "github.com/cockroachdb/cockroach/pkg/sql/sqlbase" 21 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 22 ) 23 24 func newTestScanNode(kvDB *kv.DB, tableName string) (*scanNode, error) { 25 desc := sqlbase.GetImmutableTableDescriptor(kvDB, keys.SystemSQLCodec, sqlutils.TestDB, tableName) 26 27 p := planner{alloc: &sqlbase.DatumAlloc{}} 28 scan := p.Scan() 29 scan.desc = desc 30 err := scan.initDescDefaults(publicColumnsCfg) 31 if err != nil { 32 return nil, err 33 } 34 // Initialize the required ordering. 35 columnIDs, dirs := scan.index.FullColumnIDs() 36 ordering := make(sqlbase.ColumnOrdering, len(columnIDs)) 37 for i, colID := range columnIDs { 38 idx, ok := scan.colIdxMap[colID] 39 if !ok { 40 panic(fmt.Sprintf("index refers to unknown column id %d", colID)) 41 } 42 ordering[i].ColIdx = idx 43 ordering[i].Direction, err = dirs[i].ToEncodingDirection() 44 if err != nil { 45 return nil, err 46 } 47 } 48 scan.reqOrdering = ordering 49 sb := span.MakeBuilder(keys.SystemSQLCodec, desc.TableDesc(), &desc.PrimaryIndex) 50 scan.spans, err = sb.SpansFromConstraint(nil /* constraint */, exec.TableColumnOrdinalSet{}, false /* forDelete */) 51 if err != nil { 52 return nil, err 53 } 54 return scan, nil 55 } 56 57 func newTestJoinNode(kvDB *kv.DB, leftName, rightName string) (*joinNode, error) { 58 left, err := newTestScanNode(kvDB, leftName) 59 if err != nil { 60 return nil, err 61 } 62 right, err := newTestScanNode(kvDB, rightName) 63 if err != nil { 64 return nil, err 65 } 66 return &joinNode{ 67 left: planDataSource{plan: left}, 68 right: planDataSource{plan: right}, 69 }, nil 70 }