github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/describe_test.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rowexec
    16  
    17  import (
    18  	"io"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/dolthub/go-mysql-server/memory"
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/expression"
    26  	"github.com/dolthub/go-mysql-server/sql/plan"
    27  	"github.com/dolthub/go-mysql-server/sql/types"
    28  )
    29  
    30  func TestDescribe(t *testing.T) {
    31  	require := require.New(t)
    32  	ctx := sql.NewEmptyContext()
    33  
    34  	db := memory.NewDatabase("test")
    35  	table := memory.NewTable(db.Database(), "test", sql.NewPrimaryKeySchema(sql.Schema{
    36  		{Name: "c1", Type: types.Text},
    37  		{Name: "c2", Type: types.Int32},
    38  	}), nil)
    39  
    40  	d := plan.NewDescribe(plan.NewResolvedTable(table, nil, nil))
    41  	iter, err := DefaultBuilder.Build(ctx, d, nil)
    42  	require.NoError(err)
    43  	require.NotNil(iter)
    44  
    45  	n, err := iter.Next(ctx)
    46  	require.NoError(err)
    47  	require.Equal(sql.NewRow("c1", "text"), n)
    48  
    49  	n, err = iter.Next(ctx)
    50  	require.NoError(err)
    51  	require.Equal(sql.NewRow("c2", "int"), n)
    52  
    53  	n, err = iter.Next(ctx)
    54  	require.Equal(io.EOF, err)
    55  	require.Nil(n)
    56  }
    57  
    58  func TestDescribe_Empty(t *testing.T) {
    59  	require := require.New(t)
    60  	ctx := sql.NewEmptyContext()
    61  
    62  	d := plan.NewDescribe(plan.NewUnresolvedTable("test_table", ""))
    63  
    64  	iter, err := DefaultBuilder.Build(ctx, d, nil)
    65  	require.NoError(err)
    66  	require.NotNil(iter)
    67  
    68  	n, err := iter.Next(ctx)
    69  	require.Equal(io.EOF, err)
    70  	require.Nil(n)
    71  }
    72  
    73  func TestDescribeQuery(t *testing.T) {
    74  	require := require.New(t)
    75  
    76  	db := memory.NewDatabase("foo")
    77  	table := memory.NewTable(db.BaseDatabase, "foo", sql.NewPrimaryKeySchema(sql.Schema{
    78  		{Source: "foo", Name: "a", Type: types.Text},
    79  		{Source: "foo", Name: "b", Type: types.Text},
    80  	}), nil)
    81  
    82  	format := sql.DescribeOptions{
    83  		Analyze:   false,
    84  		Estimates: false,
    85  		Debug:     false,
    86  	}
    87  	node := plan.NewDescribeQuery(format, plan.NewProject(
    88  		[]sql.Expression{
    89  			expression.NewGetFieldWithTable(1, 0, types.Text, "", "foo", "a", false),
    90  			expression.NewGetFieldWithTable(1, 1, types.Text, "", "foo", "b", false),
    91  		},
    92  		plan.NewFilter(
    93  			expression.NewEquals(
    94  				expression.NewGetFieldWithTable(1, 0, types.Text, "", "foo", "a", false),
    95  				expression.NewLiteral("foo", types.LongText),
    96  			),
    97  			plan.NewResolvedTable(table, nil, nil),
    98  		),
    99  	))
   100  
   101  	ctx := sql.NewEmptyContext()
   102  	iter, err := DefaultBuilder.Build(ctx, node, nil)
   103  	require.NoError(err)
   104  
   105  	rows, err := sql.RowIterToRows(ctx, iter)
   106  	require.NoError(err)
   107  
   108  	expected := []sql.Row{
   109  		{"Project"},
   110  		{" ├─ columns: [foo.a, foo.b]"},
   111  		{" └─ Filter"},
   112  		{"     ├─ (foo.a = 'foo')"},
   113  		{"     └─ Table"},
   114  		{"         └─ name: foo"},
   115  	}
   116  
   117  	require.Equal(expected, rows)
   118  }