github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/ordering/project_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 ordering 12 13 import ( 14 "fmt" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/sql/opt" 18 "github.com/cockroachdb/cockroach/pkg/sql/opt/norm" 19 "github.com/cockroachdb/cockroach/pkg/sql/opt/props" 20 "github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" 21 "github.com/cockroachdb/cockroach/pkg/sql/opt/testutils/testcat" 22 "github.com/cockroachdb/cockroach/pkg/sql/opt/testutils/testexpr" 23 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 24 "github.com/cockroachdb/cockroach/pkg/sql/types" 25 ) 26 27 func TestProject(t *testing.T) { 28 evalCtx := tree.NewTestingEvalContext(nil /* st */) 29 var f norm.Factory 30 f.Init(evalCtx, testcat.New()) 31 md := f.Metadata() 32 for i := 1; i <= 4; i++ { 33 md.AddColumn(fmt.Sprintf("col%d", i), types.Int) 34 } 35 36 var fds props.FuncDepSet 37 fds.AddEquivalency(2, 3) 38 fds.AddConstants(opt.MakeColSet(4)) 39 40 input := &testexpr.Instance{ 41 Rel: &props.Relational{ 42 OutputCols: opt.MakeColSet(1, 2, 3, 4), 43 FuncDeps: fds, 44 }, 45 } 46 47 type testCase struct { 48 req string 49 exp string 50 } 51 testCases := []testCase{ 52 { 53 req: "", 54 exp: "", 55 }, 56 { 57 req: "+1,+2,+3,+4", 58 exp: "+1,+(2|3) opt(4)", 59 }, 60 { 61 req: "+3 opt(5)", 62 exp: "+(2|3) opt(4)", 63 }, 64 { 65 req: "+1 opt(2,5)", 66 exp: "+1 opt(2-4)", 67 }, 68 { 69 req: "+1,+2,+3,+4,+5", 70 exp: "no", 71 }, 72 { 73 req: "+5", 74 exp: "no", 75 }, 76 } 77 for _, tc := range testCases { 78 req := physical.ParseOrderingChoice(tc.req) 79 project := f.Memo().MemoizeProject(input, nil /* projections */, opt.MakeColSet(1, 2, 3, 4)) 80 81 res := "no" 82 if projectCanProvideOrdering(project, &req) { 83 res = projectBuildChildReqOrdering(project, &req, 0).String() 84 } 85 if res != tc.exp { 86 t.Errorf("req: %s expected: %s got: %s\n", tc.req, tc.exp, res) 87 } 88 } 89 }