vitess.io/vitess@v0.16.2/go/vt/sqlparser/rewriter_test.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func BenchmarkVisitLargeExpression(b *testing.B) {
    28  	gen := newGenerator(1, 5)
    29  	exp := gen.expression()
    30  
    31  	depth := 0
    32  	for i := 0; i < b.N; i++ {
    33  		_ = Rewrite(exp, func(cursor *Cursor) bool {
    34  			depth++
    35  			return true
    36  		}, func(cursor *Cursor) bool {
    37  			depth--
    38  			return true
    39  		})
    40  	}
    41  }
    42  
    43  func TestReplaceWorksInLaterCalls(t *testing.T) {
    44  	q := "select * from tbl1"
    45  	stmt, err := Parse(q)
    46  	require.NoError(t, err)
    47  	count := 0
    48  	Rewrite(stmt, func(cursor *Cursor) bool {
    49  		switch node := cursor.Node().(type) {
    50  		case *Select:
    51  			node.SelectExprs[0] = &AliasedExpr{
    52  				Expr: NewStrLiteral("apa"),
    53  			}
    54  			node.SelectExprs = append(node.SelectExprs, &AliasedExpr{
    55  				Expr: NewStrLiteral("foobar"),
    56  			})
    57  		case *StarExpr:
    58  			t.Errorf("should not have seen the star")
    59  		case *Literal:
    60  			count++
    61  		}
    62  		return true
    63  	}, nil)
    64  	assert.Equal(t, 2, count)
    65  }
    66  
    67  func TestReplaceAndRevisitWorksInLaterCalls(t *testing.T) {
    68  	q := "select * from tbl1"
    69  	stmt, err := Parse(q)
    70  	require.NoError(t, err)
    71  	count := 0
    72  	Rewrite(stmt, func(cursor *Cursor) bool {
    73  		switch node := cursor.Node().(type) {
    74  		case SelectExprs:
    75  			if len(node) != 1 {
    76  				return true
    77  			}
    78  			expr1 := &AliasedExpr{
    79  				Expr: NewStrLiteral("apa"),
    80  			}
    81  			expr2 := &AliasedExpr{
    82  				Expr: NewStrLiteral("foobar"),
    83  			}
    84  			cursor.ReplaceAndRevisit(SelectExprs{expr1, expr2})
    85  		case *StarExpr:
    86  			t.Errorf("should not have seen the star")
    87  		case *Literal:
    88  			count++
    89  		}
    90  		return true
    91  	}, nil)
    92  	assert.Equal(t, 2, count)
    93  }
    94  
    95  func TestChangeValueTypeGivesError(t *testing.T) {
    96  	parse, err := Parse("select * from a join b on a.id = b.id")
    97  	require.NoError(t, err)
    98  
    99  	defer func() {
   100  		if r := recover(); r != nil {
   101  			require.Equal(t, "[BUG] tried to replace 'On' on 'JoinCondition'", r)
   102  		}
   103  	}()
   104  	_ = Rewrite(parse, func(cursor *Cursor) bool {
   105  		_, ok := cursor.Node().(*ComparisonExpr)
   106  		if ok {
   107  			cursor.Replace(&NullVal{}) // this is not a valid replacement because the container is a value type
   108  		}
   109  		return true
   110  	}, nil)
   111  
   112  }