github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/table_func_scripts.go (about)

     1  // Copyright 2023 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 queries
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  	"github.com/dolthub/go-mysql-server/sql/plan"
    20  )
    21  
    22  var TableFunctionScriptTests = []ScriptTest{
    23  	{
    24  		SetUpScript: []string{
    25  			"create database if not exists mydb",
    26  			"use mydb",
    27  			"create table xy (x int primary key, y int)",
    28  			"insert into xy values (0,1), (1,2), (2,3)",
    29  		},
    30  		Query:    "select y from table_func('z',2) join xy t on y = z",
    31  		Expected: []sql.Row{{2}},
    32  	},
    33  	{
    34  		Query:    "select * from sequence_table('y',2) seq1 where y in (select SEQ2.x from table_func('x', 1) seq2)",
    35  		Expected: []sql.Row{{1}},
    36  	},
    37  	{
    38  		Name:        "undefined table function",
    39  		Query:       "SELECT * from does_not_exist('q', 123);",
    40  		ExpectedErr: sql.ErrTableFunctionNotFound,
    41  	},
    42  	{
    43  		Name:        "projection of non-existent column from table function",
    44  		Query:       "SELECT none from simple_TABLE_function(123);",
    45  		ExpectedErr: sql.ErrColumnNotFound,
    46  	},
    47  	{
    48  		Name:        "projection of non-existent qualified column from table function",
    49  		Query:       "SELECT simple_TABLE_function.none from simple_TABLE_function(123);",
    50  		ExpectedErr: sql.ErrTableColumnNotFound,
    51  	},
    52  	{
    53  		Name:        "projection of non-existent aliased qualified column from table function",
    54  		Query:       "SELECT stf.none from simple_TABLE_function(123) as stf;",
    55  		ExpectedErr: sql.ErrTableColumnNotFound,
    56  	},
    57  	{
    58  		Name:        "projection of non-existent aliased qualified column from table function in join",
    59  		Query:       "SELECT stf1.none from simple_TABLE_function(123) as stf1 join simple_TABLE_function(123) stf2;",
    60  		ExpectedErr: sql.ErrTableColumnNotFound,
    61  	},
    62  	{
    63  		Name:        "alias overwrites original name",
    64  		Query:       "SELECT simple_table_function.none from simple_TABLE_function(123) stf;",
    65  		ExpectedErr: sql.ErrTableNotFound,
    66  	},
    67  	{
    68  		Name:        "projection of aliased non-existent qualified column from table function",
    69  		Query:       "SELECT stf.none as none from simple_TABLE_function(123) as stf;",
    70  		ExpectedErr: sql.ErrTableColumnNotFound,
    71  	},
    72  	{
    73  		Name:     "basic table function",
    74  		Query:    "SELECT * from simple_table_function(123);",
    75  		Expected: []sql.Row{{"foo", 123}},
    76  	},
    77  	{
    78  		Name:     "basic table function",
    79  		Query:    "SELECT * from simple_TABLE_function(123);",
    80  		Expected: []sql.Row{{"foo", 123}},
    81  	},
    82  	{
    83  		Name:     "aggregate function applied to a table function",
    84  		Query:    "SELECT count(*) from simple_TABLE_function(123);",
    85  		Expected: []sql.Row{{1}},
    86  	},
    87  	{
    88  		Name:     "projection of table function",
    89  		Query:    "SELECT one from simple_TABLE_function(123);",
    90  		Expected: []sql.Row{{"foo"}},
    91  	},
    92  	{
    93  		Name:     "nested expressions in table function arguments",
    94  		Query:    "SELECT * from simple_TABLE_function(concat('f', 'o', 'o'));",
    95  		Expected: []sql.Row{{"foo", 123}},
    96  	},
    97  	{
    98  		Name:     "filtering table function results",
    99  		Query:    "SELECT * from simple_TABLE_function(123) where one='foo';",
   100  		Expected: []sql.Row{{"foo", 123}},
   101  	},
   102  	{
   103  		Name:     "filtering table function results to no results",
   104  		Query:    "SELECT * from simple_TABLE_function(123) where one='none';",
   105  		Expected: []sql.Row{},
   106  	},
   107  	{
   108  		Name:     "grouping table function results",
   109  		Query:    "SELECT count(one) from simple_TABLE_function(123) group by one;",
   110  		Expected: []sql.Row{{1}},
   111  	},
   112  	{
   113  		Name:     "table function as subquery",
   114  		Query:    "SELECT * from (select * from simple_TABLE_function(123)) as tf;",
   115  		Expected: []sql.Row{{"foo", 123}},
   116  	},
   117  	{
   118  		Query:    "select * from sequence_table('x', 5)",
   119  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   120  	},
   121  	{
   122  		Query:    "select sequence_table.x from sequence_table('x', 5)",
   123  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   124  	},
   125  	{
   126  		Query:    "select sequence_table.x from sequence_table('x', 5)",
   127  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   128  	},
   129  	{
   130  		Query:       "select * from sequence_table('x', 5) join sequence_table('y', 5) on x = y",
   131  		ExpectedErr: sql.ErrDuplicateAliasOrTable,
   132  	},
   133  	{
   134  		Query:       "select * from sequence_table('x', 5) join sequence_table('y', 5) on x = 0",
   135  		ExpectedErr: sql.ErrDuplicateAliasOrTable,
   136  	},
   137  	{
   138  		Query:    "select * from sequence_table('x', 2) where x is not null",
   139  		Expected: []sql.Row{{0}, {1}},
   140  	},
   141  	{
   142  		Query:    "select seq.x from sequence_table('x', 5) as seq",
   143  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   144  	},
   145  	{
   146  		Query:    "select seq.x from sequence_table('x', 5) seq",
   147  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   148  	},
   149  	{
   150  		Query:       "select not_seq.x from sequence_table('x', 5) as seq",
   151  		ExpectedErr: sql.ErrTableNotFound,
   152  	},
   153  	{
   154  		Query:           "select /*+ MERGE_JOIN(seq1,seq2) JOIN_ORDER(seq2,seq1) */ seq1.x, seq2.y from sequence_table('x', 5) seq1 join sequence_table('y', 5) seq2 on seq1.x = seq2.y",
   155  		Expected:        []sql.Row{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}},
   156  		ExpectedIndexes: []string{"y", "x"},
   157  	},
   158  	{
   159  		Query:           "select /*+ LOOKUP_JOIN(seq1,seq2) JOIN_ORDER(seq2,seq1) */ seq1.x, seq2.y from sequence_table('x', 5) seq1 join sequence_table('y', 5) seq2 on seq1.x = seq2.y",
   160  		Expected:        []sql.Row{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}},
   161  		ExpectedIndexes: []string{"x"},
   162  	},
   163  	{
   164  		Query:           "select /*+ MERGE_JOIN(seq1,seq2) JOIN_ORDER(seq2,seq1) */ * from sequence_table('x', 5) seq1 join sequence_table('y', 5) seq2 on x = 0",
   165  		Expected:        []sql.Row{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}},
   166  		ExpectedIndexes: []string{"x"},
   167  	},
   168  	{
   169  		Query:           "select /*+ LOOKUP_JOIN(seq1,seq2) */ * from sequence_table('x', 5) seq1 join sequence_table('y', 5) seq2 on x = 0",
   170  		Expected:        []sql.Row{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}},
   171  		ExpectedIndexes: []string{"x"},
   172  	},
   173  	{
   174  		Query:    "with cte as (select seq.x from sequence_table('x', 5) seq) select cte.x from cte",
   175  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   176  	},
   177  	{
   178  		Query:    "select sq.x from (select seq.x from sequence_table('x', 5) seq) sq",
   179  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   180  	},
   181  	{
   182  		Query:       "select seq.x from (select seq.x from sequence_table('x', 5) seq) sq",
   183  		ExpectedErr: sql.ErrTableNotFound,
   184  	},
   185  	{
   186  		Query:    "select sq.xx from (select seq.x as xx from sequence_table('x', 5) seq) sq",
   187  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   188  	},
   189  	{
   190  		Name:            "sequence_table allows point lookups",
   191  		Query:           "select * from sequence_table('x', 5) where x = 2",
   192  		Expected:        []sql.Row{{2}},
   193  		ExpectedIndexes: []string{"x"},
   194  	},
   195  	{
   196  		Name:            "sequence_table allows range lookups",
   197  		Query:           "select * from sequence_table('x', 5) where x >= 1 and x <= 3",
   198  		Expected:        []sql.Row{{1}, {2}, {3}},
   199  		ExpectedIndexes: []string{"x"},
   200  	},
   201  	{
   202  		Name:     "basic behavior of point_lookup_table",
   203  		Query:    "select seq.x from point_lookup_table('x', 5) seq",
   204  		Expected: []sql.Row{{0}, {1}, {2}, {3}, {4}},
   205  	},
   206  	{
   207  		Name:            "point_lookup_table allows point lookups",
   208  		Query:           "select * from point_lookup_table('x', 5) where x = 2",
   209  		Expected:        []sql.Row{{2}},
   210  		ExpectedIndexes: []string{"x"},
   211  	},
   212  	{
   213  		Name:            "point_lookup_table disallows range lookups",
   214  		Query:           "select * from point_lookup_table('x', 5) where x >= 1 and x <= 3",
   215  		Expected:        []sql.Row{{1}, {2}, {3}},
   216  		ExpectedIndexes: []string{},
   217  	},
   218  	{
   219  		Name:            "point_lookup_table disallows merge join",
   220  		Query:           "select /*+ MERGE_JOIN(l,r) */ * from point_lookup_table('x', 5) l join point_lookup_table('y', 5) r where x = y",
   221  		Expected:        []sql.Row{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}},
   222  		JoinTypes:       []plan.JoinType{plan.JoinTypeLookup},
   223  		ExpectedIndexes: []string{"y"},
   224  	},
   225  }