github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/constant_eval_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 tree_test
    12  
    13  import (
    14  	"context"
    15  	"reflect"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    22  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    23  )
    24  
    25  func TestConstantEvalArrayComparison(t *testing.T) {
    26  	defer leaktest.AfterTest(t)()
    27  	defer tree.MockNameTypes(map[string]*types.T{"a": types.MakeArray(types.Int)})()
    28  
    29  	expr, err := parser.ParseExpr("a = ARRAY[1:::INT,2:::INT]")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	semaCtx := tree.MakeSemaContext()
    35  	typedExpr, err := expr.TypeCheck(context.Background(), &semaCtx, types.Any)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	ctx := tree.NewTestingEvalContext(cluster.MakeTestingClusterSettings())
    41  	defer ctx.Mon.Stop(context.Background())
    42  	c := tree.MakeConstantEvalVisitor(ctx)
    43  	expr, _ = tree.WalkExpr(&c, typedExpr)
    44  	if err := c.Err(); err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	left := tree.ColumnItem{
    49  		ColumnName: "a",
    50  	}
    51  	right := tree.DArray{
    52  		ParamTyp:    types.Int,
    53  		Array:       tree.Datums{tree.NewDInt(1), tree.NewDInt(2)},
    54  		HasNonNulls: true,
    55  	}
    56  	expected := tree.NewTypedComparisonExpr(tree.EQ, &left, &right)
    57  	if !reflect.DeepEqual(expr, expected) {
    58  		t.Errorf("invalid expr '%v', expected '%v'", expr, expected)
    59  	}
    60  }