github.com/dolthub/go-mysql-server@v0.18.0/enginetest/memory_engine_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 enginetest_test
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/dolthub/sqllogictest/go/logictest"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/dolthub/go-mysql-server/enginetest"
    27  	"github.com/dolthub/go-mysql-server/enginetest/queries"
    28  	"github.com/dolthub/go-mysql-server/enginetest/scriptgen/setup"
    29  	memharness "github.com/dolthub/go-mysql-server/enginetest/sqllogictest/harness"
    30  	"github.com/dolthub/go-mysql-server/memory"
    31  	"github.com/dolthub/go-mysql-server/sql"
    32  	"github.com/dolthub/go-mysql-server/sql/expression"
    33  	"github.com/dolthub/go-mysql-server/sql/plan"
    34  	"github.com/dolthub/go-mysql-server/sql/types"
    35  	_ "github.com/dolthub/go-mysql-server/sql/variables"
    36  )
    37  
    38  // This file is for validating both the engine itself and the in-memory database implementation in the memory package.
    39  // Any engine test that relies on the correct implementation of the in-memory database belongs here. All test logic and
    40  // queries are declared in the exported enginetest package to make them usable by integrators, to validate the engine
    41  // against their own implementation.
    42  
    43  type indexBehaviorTestParams struct {
    44  	name              string
    45  	driverInitializer enginetest.IndexDriverInitializer
    46  	nativeIndexes     bool
    47  }
    48  
    49  const testNumPartitions = 5
    50  
    51  var numPartitionsVals = []int{
    52  	1,
    53  	testNumPartitions,
    54  }
    55  var indexBehaviors = []*indexBehaviorTestParams{
    56  	{"none", nil, false},
    57  	{"mergableIndexes", mergableIndexDriver, false},
    58  	{"nativeIndexes", nil, true},
    59  	{"nativeAndMergable", mergableIndexDriver, true},
    60  }
    61  var parallelVals = []int{
    62  	1,
    63  	2,
    64  }
    65  
    66  // TestQueries tests the given queries on an engine under a variety of circumstances:
    67  // 1) Partitioned tables / non partitioned tables
    68  // 2) Mergeable / unmergeable / native / no indexes
    69  // 3) Parallelism on / off
    70  func TestQueries(t *testing.T) {
    71  	for _, numPartitions := range numPartitionsVals {
    72  		for _, indexBehavior := range indexBehaviors {
    73  			for _, parallelism := range parallelVals {
    74  				if parallelism == 1 && numPartitions == testNumPartitions && indexBehavior.name == "nativeIndexes" {
    75  					// This case is covered by TestQueriesSimple
    76  					continue
    77  				}
    78  				testName := fmt.Sprintf("partitions=%d,indexes=%v,parallelism=%v", numPartitions, indexBehavior.name, parallelism)
    79  				harness := enginetest.NewMemoryHarness(testName, parallelism, numPartitions, indexBehavior.nativeIndexes, indexBehavior.driverInitializer)
    80  
    81  				t.Run(testName, func(t *testing.T) {
    82  					enginetest.TestQueries(t, harness)
    83  				})
    84  			}
    85  		}
    86  	}
    87  }
    88  
    89  // TestQueriesPreparedSimple runs the canonical test queries against a single threaded index enabled harness.
    90  func TestQueriesPreparedSimple(t *testing.T) {
    91  	harness := enginetest.NewDefaultMemoryHarness()
    92  	if harness.IsUsingServer() {
    93  		t.Skip("issue: https://github.com/dolthub/dolt/issues/6904 and https://github.com/dolthub/dolt/issues/6901")
    94  	}
    95  	enginetest.TestQueriesPrepared(t, harness)
    96  }
    97  
    98  // TestQueriesSimple runs the canonical test queries against a single threaded index enabled harness.
    99  func TestQueriesSimple(t *testing.T) {
   100  	harness := enginetest.NewDefaultMemoryHarness()
   101  	enginetest.TestQueries(t, harness)
   102  }
   103  
   104  // TestJoinQueries runs the canonical test queries against a single threaded index enabled harness.
   105  func TestJoinQueries(t *testing.T) {
   106  	enginetest.TestJoinQueries(t, enginetest.NewDefaultMemoryHarness())
   107  }
   108  
   109  func TestLateralJoin(t *testing.T) {
   110  	enginetest.TestLateralJoinQueries(t, enginetest.NewDefaultMemoryHarness())
   111  }
   112  
   113  // TestJoinPlanning runs join-specific tests for merge
   114  func TestJoinPlanning(t *testing.T) {
   115  	enginetest.TestJoinPlanning(t, enginetest.NewDefaultMemoryHarness())
   116  }
   117  
   118  // TestJoinOps runs join-specific tests for merge
   119  func TestJoinOps(t *testing.T) {
   120  	enginetest.TestJoinOps(t, enginetest.NewDefaultMemoryHarness(), enginetest.DefaultJoinOpTests)
   121  }
   122  
   123  func TestJoinStats(t *testing.T) {
   124  	enginetest.TestJoinStats(t, enginetest.NewDefaultMemoryHarness())
   125  }
   126  
   127  // TestJSONTableQueries runs the canonical test queries against a single threaded index enabled harness.
   128  func TestJSONTableQueries(t *testing.T) {
   129  	enginetest.TestJSONTableQueries(t, enginetest.NewDefaultMemoryHarness())
   130  }
   131  
   132  // TestJSONTableScripts runs the canonical test queries against a single threaded index enabled harness.
   133  func TestJSONTableScripts(t *testing.T) {
   134  	enginetest.TestJSONTableScripts(t, enginetest.NewDefaultMemoryHarness())
   135  }
   136  
   137  // TestBrokenJSONTableScripts runs the canonical test queries against a single threaded index enabled harness.
   138  func TestBrokenJSONTableScripts(t *testing.T) {
   139  	t.Skip("incorrect errors and unsupported json_table functionality")
   140  	enginetest.TestBrokenJSONTableScripts(t, enginetest.NewDefaultMemoryHarness())
   141  }
   142  
   143  // Convenience test for debugging a single query. Unskip and set to the desired query.
   144  func TestSingleQuery(t *testing.T) {
   145  	t.Skip()
   146  	var test queries.QueryTest
   147  	test = queries.QueryTest{
   148  		Query:    `select a.i,a.f, b.i2 from niltable a left join niltable b on a.i = b.i2 order by a.i`,
   149  		Expected: []sql.Row{{1, nil, nil}, {2, nil, 2}, {3, nil, nil}, {4, 4.0, 4}, {5, 5.0, nil}, {6, 6.0, 6}},
   150  	}
   151  
   152  	fmt.Sprintf("%v", test)
   153  	harness := enginetest.NewMemoryHarness("", 1, testNumPartitions, true, nil)
   154  	// harness.UseServer()
   155  	harness.Setup(setup.MydbData, setup.NiltableData)
   156  	engine, err := harness.NewEngine(t)
   157  	require.NoError(t, err)
   158  
   159  	engine.EngineAnalyzer().Debug = true
   160  	engine.EngineAnalyzer().Verbose = true
   161  
   162  	enginetest.TestQueryWithEngine(t, harness, engine, test)
   163  }
   164  
   165  // Convenience test for debugging a single query. Unskip and set to the desired query.
   166  func TestSingleQueryPrepared(t *testing.T) {
   167  	t.Skip()
   168  	var test = queries.ScriptTest{
   169  		Name:        "renaming views with RENAME TABLE ... TO .. statement",
   170  		SetUpScript: []string{},
   171  		Assertions: []queries.ScriptTestAssertion{
   172  			{
   173  				// Original Issue: https://github.com/dolthub/dolt/issues/5714
   174  				Query: `select 1.0/0.0 from dual`,
   175  
   176  				Expected: []sql.Row{
   177  					{4},
   178  				},
   179  			},
   180  		},
   181  	}
   182  
   183  	fmt.Sprintf("%v", test)
   184  	harness := enginetest.NewMemoryHarness("", 1, testNumPartitions, false, nil)
   185  	harness.Setup(setup.KeylessSetup...)
   186  	engine, err := harness.NewEngine(t)
   187  	if err != nil {
   188  		panic(err)
   189  	}
   190  
   191  	engine.EngineAnalyzer().Debug = true
   192  	engine.EngineAnalyzer().Verbose = true
   193  
   194  	enginetest.TestScriptWithEnginePrepared(t, engine, harness, test)
   195  }
   196  
   197  func newUpdateResult(matched, updated int) types.OkResult {
   198  	return types.OkResult{
   199  		RowsAffected: uint64(updated),
   200  		Info:         plan.UpdateInfo{Matched: matched, Updated: updated},
   201  	}
   202  }
   203  
   204  // Convenience test for debugging a single query. Unskip and set to the desired query.
   205  func TestSingleScript(t *testing.T) {
   206  	t.Skip()
   207  	var scripts = []queries.ScriptTest{
   208  		{
   209  			Name: "physical columns added after virtual one",
   210  			SetUpScript: []string{
   211  				"create table t (pk int primary key, col1 int as (pk + 1));",
   212  				"insert into t (pk) values (1), (3)",
   213  				"alter table t add index idx1 (col1, pk);",
   214  				"alter table t add index idx2 (col1);",
   215  				"alter table t add column col2 int;",
   216  				"alter table t add column col3 int;",
   217  				"insert into t (pk, col2, col3) values (2, 4, 5);",
   218  			},
   219  			Assertions: []queries.ScriptTestAssertion{
   220  				{
   221  					Query: "select * from t order by pk",
   222  					Expected: []sql.Row{
   223  						{1, 2, nil, nil},
   224  						{2, 3, 4, 5},
   225  						{3, 4, nil, nil},
   226  					},
   227  				},
   228  				{
   229  					Query: "select * from t where col1 = 2",
   230  					Expected: []sql.Row{
   231  						{1, 2, nil, nil},
   232  					},
   233  				},
   234  				{
   235  					Query: "select * from t where col1 = 3 and pk = 2",
   236  					Expected: []sql.Row{
   237  						{2, 3, 4, 5},
   238  					},
   239  				},
   240  				{
   241  					Query: "select * from t where pk = 2",
   242  					Expected: []sql.Row{
   243  						{2, 3, 4, 5},
   244  					},
   245  				},
   246  			},
   247  		},
   248  	}
   249  
   250  	for _, test := range scripts {
   251  		harness := enginetest.NewMemoryHarness("", 1, testNumPartitions, true, nil)
   252  		harness.Setup(setup.MydbData, setup.Parent_childData)
   253  		engine, err := harness.NewEngine(t)
   254  		if err != nil {
   255  			panic(err)
   256  		}
   257  		engine.EngineAnalyzer().Debug = true
   258  		engine.EngineAnalyzer().Verbose = true
   259  
   260  		enginetest.TestScriptWithEngine(t, engine, harness, test)
   261  	}
   262  }
   263  
   264  func TestUnbuildableIndex(t *testing.T) {
   265  	var scripts = []queries.ScriptTest{
   266  		{
   267  			Name: "Failing index builder still returning correct results",
   268  			SetUpScript: []string{
   269  				"CREATE TABLE mytable2 (i BIGINT PRIMARY KEY, s VARCHAR(20))",
   270  				"CREATE UNIQUE INDEX mytable2_s ON mytable2 (s)",
   271  				fmt.Sprintf("CREATE INDEX mytable2_i_s ON mytable2 (i, s) COMMENT '%s'", memory.CommentPreventingIndexBuilding),
   272  				"INSERT INTO mytable2 VALUES (1, 'first row'), (2, 'second row'), (3, 'third row')",
   273  			},
   274  			Assertions: []queries.ScriptTestAssertion{
   275  				{
   276  					Query: "SELECT i FROM mytable2 WHERE i IN (SELECT i FROM mytable2) ORDER BY i",
   277  					Expected: []sql.Row{
   278  						{1},
   279  						{2},
   280  						{3},
   281  					},
   282  				},
   283  			},
   284  		},
   285  	}
   286  
   287  	for _, test := range scripts {
   288  		harness := enginetest.NewDefaultMemoryHarness()
   289  		enginetest.TestScript(t, harness, test)
   290  	}
   291  }
   292  
   293  func TestBrokenQueries(t *testing.T) {
   294  	enginetest.TestBrokenQueries(t, enginetest.NewSkippingMemoryHarness())
   295  }
   296  
   297  func TestQueryPlanTODOs(t *testing.T) {
   298  	harness := enginetest.NewSkippingMemoryHarness()
   299  	harness.Setup(setup.MydbData, setup.Pk_tablesData, setup.NiltableData)
   300  	e, err := harness.NewEngine(t)
   301  	if err != nil {
   302  		log.Fatal(err)
   303  	}
   304  	for _, tt := range queries.QueryPlanTODOs {
   305  		t.Run(tt.Query, func(t *testing.T) {
   306  			enginetest.TestQueryPlan(t, harness, e, tt.Query, tt.ExpectedPlan, enginetest.DebugQueryPlan)
   307  		})
   308  	}
   309  }
   310  
   311  func TestVersionedQueries(t *testing.T) {
   312  	for _, numPartitions := range numPartitionsVals {
   313  		for _, indexInit := range indexBehaviors {
   314  			for _, parallelism := range parallelVals {
   315  				testName := fmt.Sprintf("partitions=%d,indexes=%v,parallelism=%v", numPartitions, indexInit.name, parallelism)
   316  				harness := enginetest.NewMemoryHarness(testName, parallelism, numPartitions, indexInit.nativeIndexes, indexInit.driverInitializer)
   317  
   318  				t.Run(testName, func(t *testing.T) {
   319  					enginetest.TestVersionedQueries(t, harness)
   320  				})
   321  			}
   322  		}
   323  	}
   324  }
   325  
   326  func TestAnsiQuotesSqlMode(t *testing.T) {
   327  	enginetest.TestAnsiQuotesSqlMode(t, enginetest.NewDefaultMemoryHarness())
   328  }
   329  
   330  func TestAnsiQuotesSqlModePrepared(t *testing.T) {
   331  	harness := enginetest.NewDefaultMemoryHarness()
   332  	if harness.IsUsingServer() {
   333  		t.Skip("prepared test depend on context for current sql_mode information, but it does not get updated when using ServerEngine")
   334  	}
   335  	enginetest.TestAnsiQuotesSqlModePrepared(t, enginetest.NewDefaultMemoryHarness())
   336  }
   337  
   338  // Tests of choosing the correct execution plan independent of result correctness. Mostly useful for confirming that
   339  // the right indexes are being used for joining tables.
   340  func TestQueryPlans(t *testing.T) {
   341  	indexBehaviors := []*indexBehaviorTestParams{
   342  		{"nativeIndexes", nil, true},
   343  		{"nativeAndMergable", mergableIndexDriver, true},
   344  	}
   345  
   346  	for _, indexInit := range indexBehaviors {
   347  		t.Run(indexInit.name, func(t *testing.T) {
   348  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 2, indexInit.nativeIndexes, indexInit.driverInitializer)
   349  			// The IN expression requires mergeable indexes meaning that an unmergeable index returns a different result, so we skip this test
   350  			harness.QueriesToSkip("SELECT a.* FROM mytable a inner join mytable b on (a.i = b.s) WHERE a.i in (1, 2, 3, 4)")
   351  			enginetest.TestQueryPlans(t, harness, queries.PlanTests)
   352  		})
   353  	}
   354  }
   355  
   356  func TestIntegrationQueryPlans(t *testing.T) {
   357  	indexBehaviors := []*indexBehaviorTestParams{
   358  		{"nativeIndexes", nil, true},
   359  	}
   360  
   361  	for _, indexInit := range indexBehaviors {
   362  		t.Run(indexInit.name, func(t *testing.T) {
   363  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 1, indexInit.nativeIndexes, indexInit.driverInitializer)
   364  			enginetest.TestIntegrationPlans(t, harness)
   365  		})
   366  	}
   367  }
   368  
   369  func TestImdbQueryPlans(t *testing.T) {
   370  	t.Skip("tests are too slow")
   371  	indexBehaviors := []*indexBehaviorTestParams{
   372  		{"nativeIndexes", nil, true},
   373  	}
   374  
   375  	for _, indexInit := range indexBehaviors {
   376  		t.Run(indexInit.name, func(t *testing.T) {
   377  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 1, indexInit.nativeIndexes, indexInit.driverInitializer)
   378  			enginetest.TestImdbPlans(t, harness)
   379  		})
   380  	}
   381  }
   382  
   383  func TestTpccQueryPlans(t *testing.T) {
   384  	indexBehaviors := []*indexBehaviorTestParams{
   385  		{"nativeIndexes", nil, true},
   386  	}
   387  
   388  	for _, indexInit := range indexBehaviors {
   389  		t.Run(indexInit.name, func(t *testing.T) {
   390  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 1, indexInit.nativeIndexes, indexInit.driverInitializer)
   391  			enginetest.TestTpccPlans(t, harness)
   392  		})
   393  	}
   394  }
   395  
   396  func TestTpchQueryPlans(t *testing.T) {
   397  	indexBehaviors := []*indexBehaviorTestParams{
   398  		{"nativeIndexes", nil, true},
   399  	}
   400  
   401  	for _, indexInit := range indexBehaviors {
   402  		t.Run(indexInit.name, func(t *testing.T) {
   403  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 1, indexInit.nativeIndexes, indexInit.driverInitializer)
   404  			enginetest.TestTpchPlans(t, harness)
   405  		})
   406  	}
   407  }
   408  
   409  func TestTpcdsQueryPlans(t *testing.T) {
   410  	t.Skip("missing features")
   411  	indexBehaviors := []*indexBehaviorTestParams{
   412  		{"nativeIndexes", nil, true},
   413  	}
   414  
   415  	for _, indexInit := range indexBehaviors {
   416  		t.Run(indexInit.name, func(t *testing.T) {
   417  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 1, indexInit.nativeIndexes, indexInit.driverInitializer)
   418  			enginetest.TestTpcdsPlans(t, harness)
   419  		})
   420  	}
   421  }
   422  
   423  func TestIndexQueryPlans(t *testing.T) {
   424  	indexBehaviors := []*indexBehaviorTestParams{
   425  		{"nativeIndexes", nil, true},
   426  		{"nativeAndMergable", mergableIndexDriver, true},
   427  	}
   428  
   429  	for _, indexInit := range indexBehaviors {
   430  		t.Run(indexInit.name, func(t *testing.T) {
   431  			harness := enginetest.NewMemoryHarness(indexInit.name, 1, 2, indexInit.nativeIndexes, indexInit.driverInitializer)
   432  			enginetest.TestIndexQueryPlans(t, harness)
   433  		})
   434  	}
   435  }
   436  
   437  func TestParallelismQueries(t *testing.T) {
   438  	enginetest.TestParallelismQueries(t, enginetest.NewMemoryHarness("default", 2, testNumPartitions, true, nil))
   439  }
   440  
   441  func TestQueryErrors(t *testing.T) {
   442  	enginetest.TestQueryErrors(t, enginetest.NewDefaultMemoryHarness())
   443  }
   444  
   445  func TestInfoSchema(t *testing.T) {
   446  	enginetest.TestInfoSchema(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   447  }
   448  
   449  func TestMySqlDb(t *testing.T) {
   450  	enginetest.TestMySqlDb(t, enginetest.NewDefaultMemoryHarness())
   451  }
   452  
   453  func TestReadOnlyDatabases(t *testing.T) {
   454  	enginetest.TestReadOnlyDatabases(t, enginetest.NewReadOnlyMemoryHarness())
   455  }
   456  
   457  func TestReadOnlyVersionedQueries(t *testing.T) {
   458  	enginetest.TestReadOnlyVersionedQueries(t, enginetest.NewReadOnlyMemoryHarness())
   459  }
   460  
   461  func TestColumnAliases(t *testing.T) {
   462  	enginetest.TestColumnAliases(t, enginetest.NewDefaultMemoryHarness())
   463  }
   464  
   465  func TestDerivedTableOuterScopeVisibility(t *testing.T) {
   466  	harness := enginetest.NewDefaultMemoryHarness()
   467  	harness.QueriesToSkip(
   468  		"SELECT max(val), (select max(dt.a) from (SELECT val as a) as dt(a)) as a1 from numbers group by a1;",            // memoization to fix
   469  		"select 'foo' as foo, (select dt.b from (select 1 as a, foo as b) dt);",                                          // need to error
   470  		"SELECT n1.val as a1 from numbers n1, (select n1.val, n2.val * -1 from numbers n2 where n1.val = n2.val) as dt;", // different OK error
   471  	)
   472  	enginetest.TestDerivedTableOuterScopeVisibility(t, harness)
   473  }
   474  
   475  func TestOrderByGroupBy(t *testing.T) {
   476  	// TODO: window validation expecting error message
   477  	enginetest.TestOrderByGroupBy(t, enginetest.NewDefaultMemoryHarness())
   478  }
   479  
   480  func TestAmbiguousColumnResolution(t *testing.T) {
   481  	enginetest.TestAmbiguousColumnResolution(t, enginetest.NewDefaultMemoryHarness())
   482  }
   483  
   484  func TestInsertInto(t *testing.T) {
   485  	harness := enginetest.NewDefaultMemoryHarness()
   486  	harness.QueriesToSkip(
   487  		// should be column not found error
   488  		"insert into a (select * from b) on duplicate key update b.i = a.i",
   489  		"insert into a (select * from b as t) on duplicate key update a.i = b.j + 100",
   490  	)
   491  	enginetest.TestInsertInto(t, harness)
   492  }
   493  
   494  func TestInsertIgnoreInto(t *testing.T) {
   495  	enginetest.TestInsertIgnoreInto(t, enginetest.NewDefaultMemoryHarness())
   496  }
   497  
   498  func TestInsertDuplicateKeyKeyless(t *testing.T) {
   499  	enginetest.TestInsertDuplicateKeyKeyless(t, enginetest.NewDefaultMemoryHarness())
   500  }
   501  
   502  func TestIgnoreIntoWithDuplicateUniqueKeyKeyless(t *testing.T) {
   503  	enginetest.TestIgnoreIntoWithDuplicateUniqueKeyKeyless(t, enginetest.NewDefaultMemoryHarness())
   504  }
   505  
   506  func TestInsertIntoErrors(t *testing.T) {
   507  	enginetest.TestInsertIntoErrors(t, enginetest.NewDefaultMemoryHarness())
   508  }
   509  
   510  func TestBrokenInsertScripts(t *testing.T) {
   511  	enginetest.TestBrokenInsertScripts(t, enginetest.NewDefaultMemoryHarness())
   512  }
   513  
   514  func TestGeneratedColumns(t *testing.T) {
   515  	enginetest.TestGeneratedColumns(t, enginetest.NewDefaultMemoryHarness())
   516  }
   517  
   518  func TestGeneratedColumnPlans(t *testing.T) {
   519  	enginetest.TestGeneratedColumnPlans(t, enginetest.NewDefaultMemoryHarness())
   520  }
   521  
   522  func TestSysbenchPlans(t *testing.T) {
   523  	enginetest.TestSysbenchPlans(t, enginetest.NewDefaultMemoryHarness())
   524  }
   525  
   526  func TestStatistics(t *testing.T) {
   527  	enginetest.TestStatistics(t, enginetest.NewDefaultMemoryHarness())
   528  }
   529  
   530  func TestStatisticIndexFilters(t *testing.T) {
   531  	enginetest.TestStatisticIndexFilters(t, enginetest.NewDefaultMemoryHarness())
   532  }
   533  
   534  func TestSpatialInsertInto(t *testing.T) {
   535  	enginetest.TestSpatialInsertInto(t, enginetest.NewDefaultMemoryHarness())
   536  }
   537  
   538  func TestLoadData(t *testing.T) {
   539  	enginetest.TestLoadData(t, enginetest.NewDefaultMemoryHarness())
   540  }
   541  
   542  func TestLoadDataErrors(t *testing.T) {
   543  	enginetest.TestLoadDataErrors(t, enginetest.NewDefaultMemoryHarness())
   544  }
   545  
   546  func TestLoadDataFailing(t *testing.T) {
   547  	enginetest.TestLoadDataFailing(t, enginetest.NewDefaultMemoryHarness())
   548  }
   549  
   550  func TestSelectIntoFile(t *testing.T) {
   551  	enginetest.TestSelectIntoFile(t, enginetest.NewDefaultMemoryHarness())
   552  }
   553  
   554  func TestReplaceInto(t *testing.T) {
   555  	enginetest.TestReplaceInto(t, enginetest.NewDefaultMemoryHarness())
   556  }
   557  
   558  func TestReplaceIntoErrors(t *testing.T) {
   559  	enginetest.TestReplaceIntoErrors(t, enginetest.NewDefaultMemoryHarness())
   560  }
   561  
   562  func TestUpdate(t *testing.T) {
   563  	enginetest.TestUpdate(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   564  }
   565  
   566  func TestUpdateIgnore(t *testing.T) {
   567  	enginetest.TestUpdateIgnore(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   568  }
   569  
   570  func TestUpdateErrors(t *testing.T) {
   571  	// TODO different errors
   572  	enginetest.TestUpdateErrors(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   573  }
   574  
   575  func TestOnUpdateExprScripts(t *testing.T) {
   576  	enginetest.TestOnUpdateExprScripts(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   577  }
   578  
   579  func TestSpatialUpdate(t *testing.T) {
   580  	enginetest.TestSpatialUpdate(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   581  }
   582  
   583  func TestDeleteFromErrors(t *testing.T) {
   584  	enginetest.TestDeleteErrors(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   585  }
   586  
   587  func TestSpatialDeleteFrom(t *testing.T) {
   588  	enginetest.TestSpatialDelete(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   589  }
   590  
   591  func TestTruncate(t *testing.T) {
   592  	enginetest.TestTruncate(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   593  }
   594  
   595  func TestDeleteFrom(t *testing.T) {
   596  	enginetest.TestDelete(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   597  }
   598  
   599  func TestConvert(t *testing.T) {
   600  	enginetest.TestConvert(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   601  }
   602  
   603  func TestScripts(t *testing.T) {
   604  	enginetest.TestScripts(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   605  }
   606  
   607  func TestSpatialScripts(t *testing.T) {
   608  	enginetest.TestSpatialScripts(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   609  }
   610  
   611  func TestSpatialIndexScripts(t *testing.T) {
   612  	enginetest.TestSpatialIndexScripts(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   613  }
   614  
   615  func TestSpatialIndexPlans(t *testing.T) {
   616  	enginetest.TestSpatialIndexPlans(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   617  }
   618  
   619  func TestUserPrivileges(t *testing.T) {
   620  	harness := enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver)
   621  	if harness.IsUsingServer() {
   622  		t.Skip("TestUserPrivileges test depend on Context to switch the user to run test queries")
   623  	}
   624  	enginetest.TestUserPrivileges(t, harness)
   625  }
   626  
   627  func TestUserAuthentication(t *testing.T) {
   628  	harness := enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver)
   629  	if harness.IsUsingServer() {
   630  		t.Skip("TestUserPrivileges test depend on Context to switch the user to run test queries")
   631  	}
   632  	enginetest.TestUserAuthentication(t, harness)
   633  }
   634  
   635  func TestPrivilegePersistence(t *testing.T) {
   636  	enginetest.TestPrivilegePersistence(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   637  }
   638  
   639  func TestComplexIndexQueries(t *testing.T) {
   640  	harness := enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver)
   641  	enginetest.TestComplexIndexQueries(t, harness)
   642  }
   643  
   644  func TestTriggers(t *testing.T) {
   645  	enginetest.TestTriggers(t, enginetest.NewDefaultMemoryHarness())
   646  }
   647  
   648  func TestShowTriggers(t *testing.T) {
   649  	enginetest.TestShowTriggers(t, enginetest.NewDefaultMemoryHarness())
   650  }
   651  
   652  func TestBrokenTriggers(t *testing.T) {
   653  	h := enginetest.NewSkippingMemoryHarness()
   654  	for _, script := range queries.BrokenTriggerQueries {
   655  		enginetest.TestScript(t, h, script)
   656  	}
   657  }
   658  
   659  func TestStoredProcedures(t *testing.T) {
   660  	for i, test := range queries.ProcedureLogicTests {
   661  		//TODO: the RowIter returned from a SELECT should not take future changes into account
   662  		if test.Name == "FETCH captures state at OPEN" {
   663  			queries.ProcedureLogicTests[0], queries.ProcedureLogicTests[i] = queries.ProcedureLogicTests[i], queries.ProcedureLogicTests[0]
   664  			queries.ProcedureLogicTests = queries.ProcedureLogicTests[1:]
   665  		}
   666  	}
   667  	enginetest.TestStoredProcedures(t, enginetest.NewDefaultMemoryHarness())
   668  }
   669  
   670  func TestEvents(t *testing.T) {
   671  	enginetest.TestEvents(t, enginetest.NewDefaultMemoryHarness())
   672  }
   673  
   674  func TestTriggersErrors(t *testing.T) {
   675  	enginetest.TestTriggerErrors(t, enginetest.NewDefaultMemoryHarness())
   676  }
   677  
   678  func TestCreateTable(t *testing.T) {
   679  	enginetest.TestCreateTable(t, enginetest.NewDefaultMemoryHarness())
   680  }
   681  
   682  func TestRowLimit(t *testing.T) {
   683  	enginetest.TestRowLimit(t, enginetest.NewDefaultMemoryHarness())
   684  }
   685  
   686  func TestDropTable(t *testing.T) {
   687  	enginetest.TestDropTable(t, enginetest.NewDefaultMemoryHarness())
   688  }
   689  
   690  func TestRenameTable(t *testing.T) {
   691  	enginetest.TestRenameTable(t, enginetest.NewDefaultMemoryHarness())
   692  }
   693  
   694  func TestRenameColumn(t *testing.T) {
   695  	enginetest.TestRenameColumn(t, enginetest.NewDefaultMemoryHarness())
   696  }
   697  
   698  func TestAddColumn(t *testing.T) {
   699  	enginetest.TestAddColumn(t, enginetest.NewDefaultMemoryHarness())
   700  }
   701  
   702  func TestModifyColumn(t *testing.T) {
   703  	enginetest.TestModifyColumn(t, enginetest.NewDefaultMemoryHarness())
   704  }
   705  
   706  func TestDropColumn(t *testing.T) {
   707  	enginetest.TestDropColumn(t, enginetest.NewDefaultMemoryHarness())
   708  }
   709  
   710  func TestDropColumnKeylessTables(t *testing.T) {
   711  	enginetest.TestDropColumnKeylessTables(t, enginetest.NewDefaultMemoryHarness())
   712  }
   713  
   714  func TestCreateDatabase(t *testing.T) {
   715  	enginetest.TestCreateDatabase(t, enginetest.NewDefaultMemoryHarness())
   716  }
   717  
   718  func TestPkOrdinalsDDL(t *testing.T) {
   719  	enginetest.TestPkOrdinalsDDL(t, enginetest.NewDefaultMemoryHarness())
   720  }
   721  
   722  func TestPkOrdinalsDML(t *testing.T) {
   723  	enginetest.TestPkOrdinalsDML(t, enginetest.NewDefaultMemoryHarness())
   724  }
   725  
   726  func TestDropDatabase(t *testing.T) {
   727  	enginetest.TestDropDatabase(t, enginetest.NewDefaultMemoryHarness())
   728  }
   729  
   730  func TestCreateForeignKeys(t *testing.T) {
   731  	enginetest.TestCreateForeignKeys(t, enginetest.NewDefaultMemoryHarness())
   732  }
   733  
   734  func TestDropForeignKeys(t *testing.T) {
   735  	enginetest.TestDropForeignKeys(t, enginetest.NewDefaultMemoryHarness())
   736  }
   737  
   738  func TestForeignKeys(t *testing.T) {
   739  	enginetest.TestForeignKeys(t, enginetest.NewDefaultMemoryHarness())
   740  }
   741  
   742  func TestFulltextIndexes(t *testing.T) {
   743  	enginetest.TestFulltextIndexes(t, enginetest.NewDefaultMemoryHarness())
   744  }
   745  
   746  func TestCreateCheckConstraints(t *testing.T) {
   747  	enginetest.TestCreateCheckConstraints(t, enginetest.NewDefaultMemoryHarness())
   748  }
   749  
   750  func TestChecksOnInsert(t *testing.T) {
   751  	enginetest.TestChecksOnInsert(t, enginetest.NewDefaultMemoryHarness())
   752  }
   753  
   754  func TestChecksOnUpdate(t *testing.T) {
   755  	enginetest.TestChecksOnUpdate(t, enginetest.NewDefaultMemoryHarness())
   756  }
   757  
   758  func TestDisallowedCheckConstraints(t *testing.T) {
   759  	enginetest.TestDisallowedCheckConstraints(t, enginetest.NewDefaultMemoryHarness())
   760  }
   761  
   762  func TestDropCheckConstraints(t *testing.T) {
   763  	enginetest.TestDropCheckConstraints(t, enginetest.NewDefaultMemoryHarness())
   764  }
   765  
   766  func TestReadOnly(t *testing.T) {
   767  	enginetest.TestReadOnly(t, enginetest.NewDefaultMemoryHarness(), true /* testStoredProcedures */)
   768  }
   769  
   770  func TestViews(t *testing.T) {
   771  	enginetest.TestViews(t, enginetest.NewDefaultMemoryHarness())
   772  }
   773  
   774  func TestVersionedViews(t *testing.T) {
   775  	enginetest.TestVersionedViews(t, enginetest.NewDefaultMemoryHarness())
   776  }
   777  
   778  func TestNaturalJoin(t *testing.T) {
   779  	enginetest.TestNaturalJoin(t, enginetest.NewDefaultMemoryHarness())
   780  }
   781  
   782  func TestWindowFunctions(t *testing.T) {
   783  	enginetest.TestWindowFunctions(t, enginetest.NewDefaultMemoryHarness())
   784  }
   785  
   786  func TestWindowRangeFrames(t *testing.T) {
   787  	enginetest.TestWindowRangeFrames(t, enginetest.NewDefaultMemoryHarness())
   788  }
   789  
   790  func TestNamedWindows(t *testing.T) {
   791  	enginetest.TestNamedWindows(t, enginetest.NewDefaultMemoryHarness())
   792  }
   793  
   794  func TestNaturalJoinEqual(t *testing.T) {
   795  	enginetest.TestNaturalJoinEqual(t, enginetest.NewDefaultMemoryHarness())
   796  }
   797  
   798  func TestNaturalJoinDisjoint(t *testing.T) {
   799  	enginetest.TestNaturalJoinDisjoint(t, enginetest.NewDefaultMemoryHarness())
   800  }
   801  
   802  func TestInnerNestedInNaturalJoins(t *testing.T) {
   803  	enginetest.TestInnerNestedInNaturalJoins(t, enginetest.NewDefaultMemoryHarness())
   804  }
   805  
   806  func TestColumnDefaults(t *testing.T) {
   807  	enginetest.TestColumnDefaults(t, enginetest.NewDefaultMemoryHarness())
   808  }
   809  
   810  func TestAlterTable(t *testing.T) {
   811  	enginetest.TestAlterTable(t, enginetest.NewDefaultMemoryHarness())
   812  }
   813  
   814  func TestDateParse(t *testing.T) {
   815  	harness := enginetest.NewDefaultMemoryHarness()
   816  	if harness.IsUsingServer() {
   817  		t.Skip("issue: https://github.com/dolthub/dolt/issues/6901")
   818  	}
   819  	enginetest.TestDateParse(t, enginetest.NewDefaultMemoryHarness())
   820  }
   821  
   822  func TestJsonScripts(t *testing.T) {
   823  	// TODO different error messages
   824  	enginetest.TestJsonScripts(t, enginetest.NewDefaultMemoryHarness())
   825  }
   826  
   827  func TestShowTableStatus(t *testing.T) {
   828  	enginetest.TestShowTableStatus(t, enginetest.NewDefaultMemoryHarness())
   829  }
   830  
   831  func TestAddDropPks(t *testing.T) {
   832  	enginetest.TestAddDropPks(t, enginetest.NewDefaultMemoryHarness())
   833  }
   834  
   835  func TestAddAutoIncrementColumn(t *testing.T) {
   836  	for _, script := range queries.AlterTableAddAutoIncrementScripts {
   837  		enginetest.TestScript(t, enginetest.NewDefaultMemoryHarness(), script)
   838  	}
   839  }
   840  
   841  func TestNullRanges(t *testing.T) {
   842  	enginetest.TestNullRanges(t, enginetest.NewDefaultMemoryHarness())
   843  }
   844  
   845  func TestBlobs(t *testing.T) {
   846  	enginetest.TestBlobs(t, enginetest.NewDefaultMemoryHarness())
   847  }
   848  
   849  func TestIndexes(t *testing.T) {
   850  	enginetest.TestIndexes(t, enginetest.NewDefaultMemoryHarness())
   851  }
   852  
   853  func TestIndexPrefix(t *testing.T) {
   854  	enginetest.TestIndexPrefix(t, enginetest.NewDefaultMemoryHarness())
   855  }
   856  
   857  func TestPersist(t *testing.T) {
   858  	harness := enginetest.NewDefaultMemoryHarness()
   859  	if harness.IsUsingServer() {
   860  		t.Skip("this test depends on Context, which ServerEngine does not depend on or update the current context")
   861  	}
   862  	newSess := func(_ *sql.Context) sql.PersistableSession {
   863  		ctx := harness.NewSession()
   864  		persistedGlobals := memory.GlobalsMap{}
   865  		memSession := ctx.Session.(*memory.Session).SetGlobals(persistedGlobals)
   866  		return memSession
   867  	}
   868  	enginetest.TestPersist(t, harness, newSess)
   869  }
   870  
   871  func TestValidateSession(t *testing.T) {
   872  	count := 0
   873  	incrementValidateCb := func() {
   874  		count++
   875  	}
   876  
   877  	harness := enginetest.NewDefaultMemoryHarness()
   878  	if harness.IsUsingServer() {
   879  		t.Skip("It depends on ValidateSession() method call on context")
   880  	}
   881  	newSess := func(ctx *sql.Context) sql.PersistableSession {
   882  		memSession := ctx.Session.(*memory.Session)
   883  		memSession.SetValidationCallback(incrementValidateCb)
   884  		return memSession
   885  	}
   886  	enginetest.TestValidateSession(t, harness, newSess, &count)
   887  }
   888  
   889  func TestPrepared(t *testing.T) {
   890  	enginetest.TestPrepared(t, enginetest.NewDefaultMemoryHarness())
   891  }
   892  
   893  func TestPreparedInsert(t *testing.T) {
   894  	enginetest.TestPreparedInsert(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
   895  }
   896  
   897  func TestPreparedStatements(t *testing.T) {
   898  	enginetest.TestPreparedStatements(t, enginetest.NewDefaultMemoryHarness())
   899  }
   900  
   901  func TestCharsetCollationEngine(t *testing.T) {
   902  	harness := enginetest.NewDefaultMemoryHarness()
   903  	if harness.IsUsingServer() {
   904  		// Note: charset introducer needs to be handled with the SQLVal when preparing
   905  		//  e.g. what we do currently for `_utf16'hi'` is `_utf16 :v1` with v1 = "hi", instead of `:v1` with v1 = "_utf16'hi'".
   906  		t.Skip("way we prepare the queries with injectBindVarsAndPrepare() method does not work for ServerEngine test")
   907  	}
   908  	enginetest.TestCharsetCollationEngine(t, harness)
   909  }
   910  
   911  func TestCharsetCollationWire(t *testing.T) {
   912  	if _, ok := os.LookupEnv("CI_TEST"); !ok {
   913  		t.Skip("Skipping test that requires CI_TEST=true")
   914  	}
   915  	harness := enginetest.NewDefaultMemoryHarness()
   916  	enginetest.TestCharsetCollationWire(t, harness, harness.SessionBuilder())
   917  }
   918  
   919  func TestDatabaseCollationWire(t *testing.T) {
   920  	if _, ok := os.LookupEnv("CI_TEST"); !ok {
   921  		t.Skip("Skipping test that requires CI_TEST=true")
   922  	}
   923  	harness := enginetest.NewDefaultMemoryHarness()
   924  	enginetest.TestDatabaseCollationWire(t, harness, harness.SessionBuilder())
   925  }
   926  
   927  func TestTypesOverWire(t *testing.T) {
   928  	if _, ok := os.LookupEnv("CI_TEST"); !ok {
   929  		t.Skip("Skipping test that requires CI_TEST=true")
   930  	}
   931  	harness := enginetest.NewDefaultMemoryHarness()
   932  	enginetest.TestTypesOverWire(t, harness, harness.SessionBuilder())
   933  }
   934  
   935  func mergableIndexDriver(dbs []sql.Database) sql.IndexDriver {
   936  	return memory.NewIndexDriver("mydb", map[string][]sql.DriverIndex{
   937  		"mytable": {
   938  			newMergableIndex(dbs, "mytable",
   939  				expression.NewGetFieldWithTable(0, 1, types.Int64, "db", "mytable", "i", false)),
   940  			newMergableIndex(dbs, "mytable",
   941  				expression.NewGetFieldWithTable(1, 1, types.Text, "db", "mytable", "s", false)),
   942  			newMergableIndex(dbs, "mytable",
   943  				expression.NewGetFieldWithTable(0, 1, types.Int64, "db", "mytable", "i", false),
   944  				expression.NewGetFieldWithTable(1, 1, types.Text, "db", "mytable", "s", false)),
   945  		},
   946  		"othertable": {
   947  			newMergableIndex(dbs, "othertable",
   948  				expression.NewGetFieldWithTable(0, 1, types.Text, "db", "othertable", "s2", false)),
   949  			newMergableIndex(dbs, "othertable",
   950  				expression.NewGetFieldWithTable(1, 1, types.Text, "db", "othertable", "i2", false)),
   951  			newMergableIndex(dbs, "othertable",
   952  				expression.NewGetFieldWithTable(0, 1, types.Text, "db", "othertable", "s2", false),
   953  				expression.NewGetFieldWithTable(1, 1, types.Text, "db", "othertable", "i2", false)),
   954  		},
   955  		"bigtable": {
   956  			newMergableIndex(dbs, "bigtable",
   957  				expression.NewGetFieldWithTable(0, 1, types.Text, "db", "bigtable", "t", false)),
   958  		},
   959  		"floattable": {
   960  			newMergableIndex(dbs, "floattable",
   961  				expression.NewGetFieldWithTable(2, 1, types.Text, "db", "floattable", "f64", false)),
   962  		},
   963  		"niltable": {
   964  			newMergableIndex(dbs, "niltable",
   965  				expression.NewGetFieldWithTable(0, 1, types.Int64, "db", "niltable", "i", false)),
   966  			newMergableIndex(dbs, "niltable",
   967  				expression.NewGetFieldWithTable(1, 1, types.Int64, "db", "niltable", "i2", true)),
   968  		},
   969  		"one_pk": {
   970  			newMergableIndex(dbs, "one_pk",
   971  				expression.NewGetFieldWithTable(0, 1, types.Int8, "db", "one_pk", "pk", false)),
   972  		},
   973  		"two_pk": {
   974  			newMergableIndex(dbs, "two_pk",
   975  				expression.NewGetFieldWithTable(0, 1, types.Int8, "db", "two_pk", "pk1", false),
   976  				expression.NewGetFieldWithTable(1, 1, types.Int8, "db", "two_pk", "pk2", false),
   977  			),
   978  		},
   979  	})
   980  }
   981  
   982  func newMergableIndex(dbs []sql.Database, tableName string, exprs ...sql.Expression) *memory.Index {
   983  	db, table := findTable(dbs, tableName)
   984  	if db == nil {
   985  		return nil
   986  	}
   987  	return &memory.Index{
   988  		DB:         db.Name(),
   989  		DriverName: memory.IndexDriverId,
   990  		TableName:  tableName,
   991  		Tbl:        table.(*memory.Table),
   992  		Exprs:      exprs,
   993  	}
   994  }
   995  
   996  func findTable(dbs []sql.Database, tableName string) (sql.Database, sql.Table) {
   997  	for _, db := range dbs {
   998  		names, err := db.GetTableNames(sql.NewEmptyContext())
   999  		if err != nil {
  1000  			panic(err)
  1001  		}
  1002  		for _, name := range names {
  1003  			if name == tableName {
  1004  				table, _, _ := db.GetTableInsensitive(sql.NewEmptyContext(), name)
  1005  				return db, table
  1006  			}
  1007  		}
  1008  	}
  1009  	return nil, nil
  1010  }
  1011  
  1012  func mergeSetupScripts(scripts ...setup.SetupScript) []string {
  1013  	var all []string
  1014  	for _, s := range scripts {
  1015  		all = append(all, s...)
  1016  	}
  1017  	return all
  1018  }
  1019  
  1020  func TestSQLLogicTests(t *testing.T) {
  1021  	enginetest.TestSQLLogicTests(t, enginetest.NewMemoryHarness("default", 1, testNumPartitions, true, mergableIndexDriver))
  1022  }
  1023  
  1024  func TestSQLLogicTestFiles(t *testing.T) {
  1025  	t.Skip()
  1026  	h := memharness.NewMemoryHarness(enginetest.NewDefaultMemoryHarness())
  1027  	paths := []string{
  1028  		"./sqllogictest/testdata/join/join.txt",
  1029  		"./sqllogictest/testdata/join/subquery_correlated.txt",
  1030  	}
  1031  	logictest.RunTestFiles(h, paths...)
  1032  }