github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/colflow/vectorized_meta_propagation_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 colflow_test
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/colexec"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    22  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    23  	"github.com/cockroachdb/cockroach/pkg/testutils/distsqlutils"
    24  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    25  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    26  )
    27  
    28  // TestVectorizedMetaPropagation tests whether metadata is correctly propagated
    29  // alongside columnar operators. It sets up the following "flow":
    30  // RowSource -> metadataTestSender -> columnarizer -> noopOperator ->
    31  // -> materializer -> metadataTestReceiver. Metadata propagation is hooked up
    32  // manually from the columnarizer into the materializer similar to how it is
    33  // done in setupVectorizedFlow.
    34  func TestVectorizedMetaPropagation(t *testing.T) {
    35  	defer leaktest.AfterTest(t)()
    36  	ctx := context.Background()
    37  	st := cluster.MakeTestingClusterSettings()
    38  	evalCtx := tree.MakeTestingEvalContext(st)
    39  	defer evalCtx.Stop(ctx)
    40  
    41  	flowCtx := execinfra.FlowCtx{
    42  		EvalCtx: &evalCtx,
    43  		Cfg:     &execinfra.ServerConfig{Settings: cluster.MakeTestingClusterSettings()},
    44  	}
    45  
    46  	nRows := 10
    47  	nCols := 1
    48  	types := sqlbase.OneIntCol
    49  
    50  	input := distsqlutils.NewRowBuffer(types, sqlbase.MakeIntRows(nRows, nCols), distsqlutils.RowBufferArgs{})
    51  	mtsSpec := execinfrapb.ProcessorCoreUnion{
    52  		MetadataTestSender: &execinfrapb.MetadataTestSenderSpec{
    53  			ID: uuid.MakeV4().String(),
    54  		},
    55  	}
    56  	mts, err := execinfra.NewMetadataTestSender(
    57  		&flowCtx,
    58  		0,
    59  		input,
    60  		&execinfrapb.PostProcessSpec{},
    61  		nil,
    62  		uuid.MakeV4().String(),
    63  	)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	col, err := colexec.NewColumnarizer(ctx, testAllocator, &flowCtx, 1, mts)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	noop := colexec.NewNoop(col)
    74  	mat, err := colexec.NewMaterializer(
    75  		&flowCtx,
    76  		2, /* processorID */
    77  		noop,
    78  		types,
    79  		nil, /* output */
    80  		[]execinfrapb.MetadataSource{col},
    81  		nil, /* toClose */
    82  		nil, /* outputStatsToTrace */
    83  		nil, /* cancelFlow */
    84  	)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	mtr, err := execinfra.NewMetadataTestReceiver(
    90  		&flowCtx,
    91  		3,
    92  		mat,
    93  		&execinfrapb.PostProcessSpec{},
    94  		nil,
    95  		[]string{mtsSpec.MetadataTestSender.ID},
    96  	)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	mtr.Start(ctx)
   101  
   102  	rowCount, metaCount := 0, 0
   103  	for {
   104  		row, meta := mtr.Next()
   105  		if row == nil && meta == nil {
   106  			break
   107  		}
   108  		if row != nil {
   109  			rowCount++
   110  		} else if meta.Err != nil {
   111  			t.Fatal(meta.Err)
   112  		} else {
   113  			metaCount++
   114  		}
   115  	}
   116  	if rowCount != nRows {
   117  		t.Fatalf("expected %d rows but %d received", nRows, rowCount)
   118  	}
   119  	if metaCount != nRows+1 {
   120  		// metadataTestSender sends a meta after each row plus an additional one to
   121  		// indicate the last meta.
   122  		t.Fatalf("expected %d meta but %d received", nRows+1, metaCount)
   123  	}
   124  }