github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colencoding/value_encoding_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 colencoding
    12  
    13  import (
    14  	"fmt"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/col/coldata"
    18  	"github.com/cockroachdb/cockroach/pkg/col/coldataext"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    22  	"github.com/cockroachdb/cockroach/pkg/util/encoding"
    23  	"github.com/cockroachdb/cockroach/pkg/util/randutil"
    24  )
    25  
    26  func TestDecodeTableValueToCol(t *testing.T) {
    27  	rng, _ := randutil.NewPseudoRand()
    28  	var (
    29  		da           sqlbase.DatumAlloc
    30  		buf, scratch []byte
    31  	)
    32  	nCols := 1000
    33  	datums := make([]tree.Datum, nCols)
    34  	typs := make([]*types.T, nCols)
    35  	for i := 0; i < nCols; i++ {
    36  		ct := sqlbase.RandType(rng)
    37  		datum := sqlbase.RandDatum(rng, ct, false /* nullOk */)
    38  		typs[i] = ct
    39  		datums[i] = datum
    40  		var err error
    41  		fmt.Println(datum)
    42  		buf, err = sqlbase.EncodeTableValue(buf, sqlbase.ColumnID(encoding.NoColumnID), datum, scratch)
    43  		if err != nil {
    44  			t.Fatal(err)
    45  		}
    46  	}
    47  	batch := coldata.NewMemBatchWithSize(typs, 1 /* size */, coldataext.NewExtendedColumnFactory(nil /*evalCtx */))
    48  	for i := 0; i < nCols; i++ {
    49  		typeOffset, dataOffset, _, typ, err := encoding.DecodeValueTag(buf)
    50  		fmt.Println(typ)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  		buf, err = DecodeTableValueToCol(&da, batch.ColVec(i), 0 /* rowIdx */, typ,
    55  			dataOffset, typs[i], buf[typeOffset:])
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  
    60  		// TODO(jordan): should actually compare the outputs as well, but this is a
    61  		// decent enough smoke test.
    62  	}
    63  
    64  	if len(buf) != 0 {
    65  		t.Fatalf("leftover bytes %s", buf)
    66  	}
    67  }