github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/span_builder_test.go (about)

     1  // Copyright 2019 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  	"context"
    15  	"fmt"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/keys"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/span"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/tests"
    22  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    23  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    24  )
    25  
    26  // These tests are in the sql package rather than span package
    27  // so that we have easy access to table descriptor creation.
    28  
    29  func TestSpanBuilderCanSplitSpan(t *testing.T) {
    30  	defer leaktest.AfterTest(t)()
    31  	ctx := context.Background()
    32  	params, _ := tests.CreateTestServerParams()
    33  	s, sqlDB, kvDB := serverutils.StartServer(t, params)
    34  	defer s.Stopper().Stop(ctx)
    35  	execCfg := s.ExecutorConfig().(ExecutorConfig)
    36  	tcs := []struct {
    37  		sql               string
    38  		index             string
    39  		prefixLen         int
    40  		numNeededFamilies int
    41  		containsNull      bool
    42  		canSplit          bool
    43  	}{
    44  		{
    45  			sql:               "a INT, b INT, c INT, d INT, PRIMARY KEY (a, b), FAMILY (a, b, c), FAMILY (d)",
    46  			index:             "primary",
    47  			prefixLen:         2,
    48  			numNeededFamilies: 1,
    49  			canSplit:          true,
    50  		},
    51  		{
    52  			sql:               "a INT, b INT, c INT, d INT, PRIMARY KEY (a, b), FAMILY (a, b, c), FAMILY (d)",
    53  			index:             "primary",
    54  			prefixLen:         1,
    55  			numNeededFamilies: 1,
    56  			canSplit:          false,
    57  		},
    58  		{
    59  			sql:               "a INT, b INT, c INT, d INT, PRIMARY KEY (a, b), FAMILY (a, b, c, d)",
    60  			index:             "primary",
    61  			prefixLen:         2,
    62  			numNeededFamilies: 1,
    63  			canSplit:          false,
    64  		},
    65  		{
    66  			sql:               "a INT, b INT, c INT, INDEX i (b) STORING (a, c), FAMILY (a), FAMILY (b), FAMILY (c)",
    67  			index:             "i",
    68  			prefixLen:         1,
    69  			numNeededFamilies: 1,
    70  			canSplit:          false,
    71  		},
    72  		{
    73  			sql:               "a INT, b INT, c INT, UNIQUE INDEX i (b) STORING (a, c), FAMILY (a), FAMILY (b), FAMILY (c)",
    74  			index:             "i",
    75  			prefixLen:         1,
    76  			numNeededFamilies: 1,
    77  			containsNull:      true,
    78  			canSplit:          false,
    79  		},
    80  		{
    81  			sql:               "a INT, b INT, c INT, UNIQUE INDEX i (b) STORING (a, c), FAMILY (a), FAMILY (b), FAMILY (c)",
    82  			index:             "i",
    83  			prefixLen:         1,
    84  			numNeededFamilies: 1,
    85  			containsNull:      false,
    86  			canSplit:          true,
    87  		},
    88  	}
    89  	if _, err := sqlDB.Exec("CREATE DATABASE t"); err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	for _, tc := range tcs {
    93  		t.Run(tc.sql, func(t *testing.T) {
    94  			if _, err := sqlDB.Exec("DROP TABLE IF EXISTS t.t"); err != nil {
    95  				t.Fatal(err)
    96  			}
    97  			sql := fmt.Sprintf("CREATE TABLE t.t (%s)", tc.sql)
    98  			if _, err := sqlDB.Exec(sql); err != nil {
    99  				t.Fatal(err)
   100  			}
   101  			desc := sqlbase.GetTableDescriptor(kvDB, keys.SystemSQLCodec, "t", "t")
   102  			idx, _, err := desc.FindIndexByName(tc.index)
   103  			if err != nil {
   104  				t.Fatal(err)
   105  			}
   106  			builder := span.MakeBuilder(execCfg.Codec, desc, idx)
   107  			if res := builder.CanSplitSpanIntoSeparateFamilies(
   108  				tc.numNeededFamilies, tc.prefixLen, tc.containsNull); res != tc.canSplit {
   109  				t.Errorf("expected result to be %v, but found %v", tc.canSplit, res)
   110  			}
   111  		})
   112  	}
   113  }