github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/testutils/build.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 testutils
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/opt/xform"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    22  )
    23  
    24  // BuildQuery initializes an optimizer and builds the given sql statement.
    25  func BuildQuery(
    26  	t *testing.T, o *xform.Optimizer, catalog cat.Catalog, evalCtx *tree.EvalContext, sql string,
    27  ) {
    28  	stmt, err := parser.ParseOne(sql)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	ctx := context.Background()
    34  	semaCtx := tree.MakeSemaContext()
    35  	if err := semaCtx.Placeholders.Init(stmt.NumPlaceholders, nil /* typeHints */); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	semaCtx.Annotations = tree.MakeAnnotations(stmt.NumAnnotations)
    39  	o.Init(evalCtx, catalog)
    40  	err = optbuilder.New(ctx, &semaCtx, evalCtx, catalog, o.Factory(), stmt.AST).Build()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  }