github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/view_queries.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/types"
    20  )
    21  
    22  var ViewScripts = []ScriptTest{
    23  	{
    24  		Name: "multi database view",
    25  		SetUpScript: []string{
    26  			"Create database base;",
    27  			"Create table base.xy (x int primary key, y int);",
    28  			"Insert into base.xy values (1, 2);",
    29  			"Create database live;",
    30  			"create view live.xy as select base.xy.x AS x, base.xy.y AS y from base.xy;",
    31  		},
    32  		Assertions: []ScriptTestAssertion{
    33  			{
    34  				Query:    "SELECT database()",
    35  				Expected: []sql.Row{{"mydb"}},
    36  			},
    37  			{
    38  				Query:    "SELECT * from live.xy;",
    39  				Expected: []sql.Row{{1, 2}},
    40  			},
    41  		},
    42  	},
    43  	{
    44  		Name: "view of join with projections",
    45  		SetUpScript: []string{
    46  			`
    47  CREATE TABLE tab1 (
    48    pk int NOT NULL,
    49    col0 int,
    50    col1 float,
    51    col2 text,
    52    col3 int,
    53    col4 float,
    54    col5 text,
    55    PRIMARY KEY (pk),
    56    KEY idx_tab1_0 (col0),
    57    KEY idx_tab1_1 (col1),
    58    KEY idx_tab1_3 (col3),
    59    KEY idx_tab1_4 (col4)
    60  )`,
    61  			"insert into tab1 values (6, 0, 52.14, 'jxmel', 22, 2.27, 'pzxbn')",
    62  		},
    63  		Assertions: []ScriptTestAssertion{
    64  			{
    65  				Query:    "CREATE VIEW view_2_tab1_157 AS SELECT pk, col0 FROM tab1 WHERE NOT ((col0 IN (SELECT col3 FROM tab1 WHERE ((col0 IS NULL) OR col3 > 5 OR col3 <= 50 OR col1 < 83.11))) OR col0 > 75)",
    66  				Expected: []sql.Row{{types.OkResult{}}},
    67  			},
    68  			{
    69  				Query:    "select pk, col0 from view_2_tab1_157",
    70  				Expected: []sql.Row{{6, 0}},
    71  			},
    72  		},
    73  	},
    74  	{
    75  		Name: "view with expression name",
    76  		SetUpScript: []string{
    77  			`create view v as select 2+2`,
    78  		},
    79  		Assertions: []ScriptTestAssertion{
    80  			{
    81  				Query:    "SELECT * from v;",
    82  				Expected: []sql.Row{{4}},
    83  				ExpectedColumns: sql.Schema{
    84  					{
    85  						Name: "2+2",
    86  						Type: types.Int64,
    87  					},
    88  				},
    89  			},
    90  		},
    91  	},
    92  	{
    93  		Name: "view with column names",
    94  		SetUpScript: []string{
    95  			`CREATE TABLE xy (x int primary key, y int);`,
    96  			`create view v_today(today) as select CURRENT_DATE()`,
    97  			`CREATE VIEW xyv (u,v) AS SELECT * from xy;`,
    98  		},
    99  		Assertions: []ScriptTestAssertion{
   100  			{
   101  				Query:    "SELECT * from xyv;",
   102  				Expected: []sql.Row{},
   103  				ExpectedColumns: sql.Schema{
   104  					{
   105  						Name: "u",
   106  						Type: types.Int32,
   107  					},
   108  					{
   109  						Name: "v",
   110  						Type: types.Int32,
   111  					},
   112  				},
   113  			},
   114  			{
   115  				Query: "SELECT * from v_today;",
   116  				ExpectedColumns: sql.Schema{
   117  					{
   118  						Name: "today",
   119  						Type: types.LongText,
   120  					},
   121  				},
   122  			},
   123  			{
   124  				Query:       "CREATE VIEW xyv (u) AS SELECT * from xy;",
   125  				ExpectedErr: sql.ErrInvalidColumnNumber,
   126  			},
   127  		},
   128  	},
   129  	{
   130  		Name: "view columns retain original case",
   131  		SetUpScript: []string{
   132  			`CREATE TABLE strs ( id int NOT NULL AUTO_INCREMENT,
   133                                   str  varchar(15) NOT NULL,
   134                                   PRIMARY KEY (id));`,
   135  			`CREATE VIEW caseSensitive AS SELECT id as AbCdEfG FROM strs;`,
   136  		},
   137  		Assertions: []ScriptTestAssertion{
   138  			{
   139  				Query:    "SELECT * from caseSensitive;",
   140  				Expected: []sql.Row{},
   141  				ExpectedColumns: sql.Schema{
   142  					{
   143  						Name: "AbCdEfG",
   144  						Type: types.Int32,
   145  					},
   146  				},
   147  			},
   148  		},
   149  	},
   150  	{
   151  		Name: "check view with escaped strings",
   152  		SetUpScript: []string{
   153  			`CREATE TABLE strs ( id int NOT NULL AUTO_INCREMENT,
   154                                   str  varchar(15) NOT NULL,
   155                                   PRIMARY KEY (id));`,
   156  			`CREATE VIEW quotes AS SELECT * FROM strs WHERE str IN ('joe''s',
   157                                                                      "jan's",
   158                                                                      'mia\\''s',
   159                                                                      'bob\'s'
   160                                                                     );`,
   161  			`INSERT INTO strs VALUES (0,"joe's");`,
   162  			`INSERT INTO strs VALUES (0,"mia\\'s");`,
   163  			`INSERT INTO strs VALUES (0,"bob's");`,
   164  			`INSERT INTO strs VALUES (0,"joe's");`,
   165  			`INSERT INTO strs VALUES (0,"notInView");`,
   166  			`INSERT INTO strs VALUES (0,"jan's");`,
   167  		},
   168  		Assertions: []ScriptTestAssertion{
   169  			{
   170  				Query: "SELECT * from quotes order by id",
   171  				Expected: []sql.Row{
   172  					{1, "joe's"},
   173  					{2, "mia\\'s"},
   174  					{3, "bob's"},
   175  					{4, "joe's"},
   176  					{6, "jan's"}},
   177  			},
   178  		},
   179  	},
   180  	{
   181  		Name: "show view",
   182  		SetUpScript: []string{
   183  			"create table xy (x int primary key, y int)",
   184  			"create view v as select * from xy",
   185  		},
   186  		Assertions: []ScriptTestAssertion{
   187  			{
   188  				Query:    "show keys from v",
   189  				Expected: []sql.Row{},
   190  			},
   191  			{
   192  				Query:    "show index from v from mydb",
   193  				Expected: []sql.Row{},
   194  			},
   195  			{
   196  				Query:    "show index from v where Column_name = 'x'",
   197  				Expected: []sql.Row{},
   198  			},
   199  		},
   200  	},
   201  }