github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colexec/limit_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 colexec
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/colexecbase"
    17  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    18  )
    19  
    20  func TestLimit(t *testing.T) {
    21  	defer leaktest.AfterTest(t)()
    22  	tcs := []struct {
    23  		limit    int
    24  		tuples   []tuple
    25  		expected []tuple
    26  	}{
    27  		{
    28  			limit:    2,
    29  			tuples:   tuples{{1}},
    30  			expected: tuples{{1}},
    31  		},
    32  		{
    33  			limit:    1,
    34  			tuples:   tuples{{1}},
    35  			expected: tuples{{1}},
    36  		},
    37  		{
    38  			limit:    0,
    39  			tuples:   tuples{{1}},
    40  			expected: tuples{},
    41  		},
    42  		{
    43  			limit:    100000,
    44  			tuples:   tuples{{1}, {2}, {3}, {4}},
    45  			expected: tuples{{1}, {2}, {3}, {4}},
    46  		},
    47  		{
    48  			limit:    2,
    49  			tuples:   tuples{{1}, {2}, {3}, {4}},
    50  			expected: tuples{{1}, {2}},
    51  		},
    52  		{
    53  			limit:    1,
    54  			tuples:   tuples{{1}, {2}, {3}, {4}},
    55  			expected: tuples{{1}},
    56  		},
    57  		{
    58  			limit:    0,
    59  			tuples:   tuples{{1}, {2}, {3}, {4}},
    60  			expected: tuples{},
    61  		},
    62  	}
    63  
    64  	for _, tc := range tcs {
    65  		// The tuples consisting of all nulls still count as separate rows, so if
    66  		// we replace all values with nulls, we should get the same output.
    67  		runTestsWithoutAllNullsInjection(t, []tuples{tc.tuples}, nil /* typs */, tc.expected, orderedVerifier, func(input []colexecbase.Operator) (colexecbase.Operator, error) {
    68  			return NewLimitOp(input[0], tc.limit), nil
    69  		})
    70  	}
    71  }