github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/ordering/row_number_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/memo"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/opt/norm"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
    22  	"github.com/cockroachdb/cockroach/pkg/sql/opt/testutils/testexpr"
    23  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    24  )
    25  
    26  func TestOrdinalityProvided(t *testing.T) {
    27  	emptyFD, equivFD, constFD := testFDs()
    28  	// The ordinality column is 10.
    29  	testCases := []struct {
    30  		required string
    31  		input    string
    32  		fds      props.FuncDepSet
    33  		provided string
    34  	}{
    35  		{ // case 1
    36  			required: "+10",
    37  			input:    "+1,+2",
    38  			fds:      emptyFD,
    39  			provided: "+10",
    40  		},
    41  		{ // case 2
    42  			required: "+1,+10",
    43  			input:    "+1,+2,+3",
    44  			fds:      emptyFD,
    45  			provided: "+1,+10",
    46  		},
    47  		{ // case 3
    48  			required: "+1,+10,+5",
    49  			input:    "+1,+2",
    50  			fds:      emptyFD,
    51  			provided: "+1,+10",
    52  		},
    53  		{ // case 4
    54  			required: "+(1|2),+(3|10)",
    55  			input:    "+1,+4,+5",
    56  			fds:      emptyFD,
    57  			provided: "+1,+10",
    58  		},
    59  		{ // case 5
    60  			required: "+1",
    61  			input:    "",
    62  			fds:      constFD,
    63  			provided: "",
    64  		},
    65  		{ // case 6
    66  			required: "-1,+10",
    67  			input:    "-2",
    68  			fds:      equivFD,
    69  			provided: "-2,+10",
    70  		},
    71  	}
    72  
    73  	for tcIdx, tc := range testCases {
    74  		t.Run(fmt.Sprintf("case%d", tcIdx+1), func(t *testing.T) {
    75  			evalCtx := tree.NewTestingEvalContext(nil /* st */)
    76  			var f norm.Factory
    77  			f.Init(evalCtx, nil /* catalog */)
    78  			input := &testexpr.Instance{
    79  				Rel: &props.Relational{OutputCols: opt.MakeColSet(1, 2, 3, 4, 5)},
    80  				Provided: &physical.Provided{
    81  					Ordering: physical.ParseOrdering(tc.input),
    82  				},
    83  			}
    84  			r := f.Memo().MemoizeOrdinality(input, &memo.OrdinalityPrivate{ColID: 10})
    85  			r.Relational().FuncDeps = tc.fds
    86  			req := physical.ParseOrderingChoice(tc.required)
    87  			res := ordinalityBuildProvided(r, &req).String()
    88  			if res != tc.provided {
    89  				t.Errorf("expected '%s', got '%s'", tc.provided, res)
    90  			}
    91  		})
    92  	}
    93  }