Skip to content

Commit 104ac7a

Browse files
authored
Merge branch 'dolthub:main' into lowercase-sqlmode
2 parents a0a63dd + 54bd6d6 commit 104ac7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1043
-593
lines changed

engine.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func New(a *analyzer.Analyzer, cfg *Config) *Engine {
192192
PreparedDataCache: NewPreparedDataCache(),
193193
mu: &sync.Mutex{},
194194
EventScheduler: nil,
195-
Parser: sql.NewMysqlParser(),
195+
Parser: sql.GlobalParser,
196196
}
197197
ret.ReadOnly.Store(cfg.IsReadOnly)
198198
return ret
@@ -210,7 +210,7 @@ func (e *Engine) AnalyzeQuery(
210210
query string,
211211
) (sql.Node, error) {
212212
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
213-
parsed, _, _, qFlags, err := binder.Parse(query, false)
213+
parsed, _, _, qFlags, err := binder.Parse(query, nil, false)
214214
if err != nil {
215215
return nil, err
216216
}
@@ -238,7 +238,7 @@ func (e *Engine) PrepareParsedQuery(
238238
stmt sqlparser.Statement,
239239
) (sql.Node, error) {
240240
binder := planbuilder.New(ctx, e.Analyzer.Catalog, e.Parser)
241-
node, _, err := binder.BindOnly(stmt, query)
241+
node, _, err := binder.BindOnly(stmt, query, nil)
242242

243243
if err != nil {
244244
return nil, err
@@ -253,6 +253,23 @@ func (e *Engine) Query(ctx *sql.Context, query string) (sql.Schema, sql.RowIter,
253253
return e.QueryWithBindings(ctx, query, nil, nil, nil)
254254
}
255255

256+
func clearWarnings(ctx *sql.Context, node sql.Node) {
257+
if ctx == nil || ctx.Session == nil {
258+
return
259+
}
260+
261+
switch n := node.(type) {
262+
case *plan.Offset, *plan.Limit:
263+
// `show warning limit x offset y` is valid, so we need to recurse
264+
clearWarnings(ctx, n.Children()[0])
265+
case plan.ShowWarnings:
266+
// ShowWarnings should not clear the warnings, but should still reset the warning count.
267+
ctx.ClearWarningCount()
268+
default:
269+
ctx.ClearWarnings()
270+
}
271+
}
272+
256273
func bindingsToExprs(bindings map[string]*querypb.BindVariable) (map[string]sql.Expression, error) {
257274
res := make(map[string]sql.Expression, len(bindings))
258275
for k, v := range bindings {
@@ -387,10 +404,18 @@ func (e *Engine) QueryWithBindings(ctx *sql.Context, query string, parsed sqlpar
387404
return nil, nil, nil, err
388405
}
389406

407+
// planbuilding can produce warnings, so we need to preserve them
408+
numPrevWarnings := len(ctx.Session.Warnings())
390409
bound, qFlags, err := e.bindQuery(ctx, query, parsed, bindings, binder, qFlags)
391410
if err != nil {
392411
return nil, nil, nil, err
393412
}
413+
newWarnings := ctx.Session.Warnings()[numPrevWarnings:]
414+
clearWarnings(ctx, bound)
415+
// restore new warnings (backwards because they are in reverse order)
416+
for i := len(newWarnings) - 1; i >= 0; i-- {
417+
ctx.Session.Warn(newWarnings[i])
418+
}
394419

395420
analyzed, err := e.analyzeNode(ctx, query, bound, qFlags)
396421
if err != nil {
@@ -586,7 +611,7 @@ func (e *Engine) bindQuery(ctx *sql.Context, query string, parsed sqlparser.Stat
586611
var bound sql.Node
587612
var err error
588613
if parsed == nil {
589-
bound, _, _, qFlags, err = binder.Parse(query, false)
614+
bound, _, _, qFlags, err = binder.Parse(query, qFlags, false)
590615
if err != nil {
591616
clearAutocommitErr := clearAutocommitTransaction(ctx)
592617
if clearAutocommitErr != nil {
@@ -595,7 +620,7 @@ func (e *Engine) bindQuery(ctx *sql.Context, query string, parsed sqlparser.Stat
595620
return nil, nil, err
596621
}
597622
} else {
598-
bound, qFlags, err = binder.BindOnly(parsed, query)
623+
bound, qFlags, err = binder.BindOnly(parsed, query, qFlags)
599624
if err != nil {
600625
return nil, nil, err
601626
}
@@ -651,7 +676,7 @@ func (e *Engine) bindExecuteQueryNode(ctx *sql.Context, query string, eq *plan.E
651676
binder.SetBindingsWithExpr(tempBindings)
652677
}
653678

654-
bound, _, err := binder.BindOnly(prep, query)
679+
bound, _, err := binder.BindOnly(prep, query, nil)
655680
if err != nil {
656681
clearAutocommitErr := clearAutocommitTransaction(ctx)
657682
if clearAutocommitErr != nil {

enginetest/engine_only_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ func TestAnalyzer_Exp(t *testing.T) {
511511

512512
ctx := enginetest.NewContext(harness)
513513
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, sql.NewMysqlParser())
514-
parsed, _, _, _, err := b.Parse(tt.query, false)
514+
parsed, _, _, _, err := b.Parse(tt.query, nil, false)
515515
require.NoError(t, err)
516516

517517
analyzed, err := e.EngineAnalyzer().Analyze(ctx, parsed, nil, nil)

enginetest/enginetests.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5653,11 +5653,11 @@ func TestTypesOverWire(t *testing.T, harness ClientHarness, sessionBuilder serve
56535653
break
56545654
}
56555655
expectedEngineRow := make([]*string, len(engineRow))
5656-
for i := range engineRow {
5657-
sqlVal, err := sch[i].Type.SQL(ctx, nil, engineRow[i])
5658-
if !assert.NoError(t, err) {
5659-
break
5660-
}
5656+
row, err := server.RowToSQL(ctx, sch, engineRow, nil)
5657+
if !assert.NoError(t, err) {
5658+
break
5659+
}
5660+
for i, sqlVal := range row {
56615661
if !sqlVal.IsNull() {
56625662
str := sqlVal.ToString()
56635663
expectedEngineRow[i] = &str

enginetest/evaluation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func injectBindVarsAndPrepare(
528528

529529
b := planbuilder.New(ctx, e.EngineAnalyzer().Catalog, sql.NewMysqlParser())
530530
b.SetParserOptions(sql.LoadSqlMode(ctx).ParserOptions())
531-
resPlan, _, err := b.BindOnly(parsed, q)
531+
resPlan, _, err := b.BindOnly(parsed, q, nil)
532532
if err != nil {
533533
return q, nil, err
534534
}

enginetest/plangen/cmd/plangen/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func generatePlansForSuite(spec PlanSpec, w *bytes.Buffer) error {
166166
if !tt.Skip {
167167
ctx := enginetest.NewContextWithEngine(harness, engine)
168168
binder := planbuilder.New(ctx, engine.EngineAnalyzer().Catalog, sql.NewMysqlParser())
169-
parsed, _, _, qFlags, err := binder.Parse(tt.Query, false)
169+
parsed, _, _, qFlags, err := binder.Parse(tt.Query, nil, false)
170170
if err != nil {
171171
exit(fmt.Errorf("%w\nfailed to parse query: %s", err, tt.Query))
172172
}

enginetest/queries/check_scripts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ CREATE TABLE t4
293293
Assertions: []ScriptTestAssertion{
294294
{
295295
Query: "ALTER TABLE test ADD CONSTRAINT bad_check CHECK (pk < 5)",
296-
ExpectedErr: plan.ErrCheckViolated,
296+
ExpectedErr: sql.ErrCheckConstraintViolated,
297297
},
298298
},
299299
},

enginetest/queries/foreign_key_queries.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,41 @@ var ForeignKeyTests = []ScriptTest{
25812581
},
25822582
},
25832583
},
2584+
{
2585+
// https://github.com/dolthub/dolt/issues/7857
2586+
Name: "partial foreign key update",
2587+
SetUpScript: []string{
2588+
"create table parent1 (i int primary key);",
2589+
"create table child1 (" +
2590+
"i int primary key, " +
2591+
"j int, " +
2592+
"k int, " +
2593+
"index(j), " +
2594+
"index(k), " +
2595+
"foreign key (j) references parent1 (i)," +
2596+
"foreign key (k) references parent1 (i));",
2597+
"insert into parent1 values (1);",
2598+
"insert into child1 values (100, 1, 1);",
2599+
"set foreign_key_checks = 0;",
2600+
"insert into child1 values (101, 2, 2);",
2601+
"set foreign_key_checks = 1;",
2602+
},
2603+
Assertions: []ScriptTestAssertion{
2604+
{
2605+
Query: "update child1 set j = 1 where i = 101;",
2606+
Expected: []sql.Row{
2607+
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
2608+
},
2609+
},
2610+
{
2611+
Query: "select * from child1",
2612+
Expected: []sql.Row{
2613+
{100, 1, 1},
2614+
{101, 1, 2},
2615+
},
2616+
},
2617+
},
2618+
},
25842619
}
25852620

25862621
var CreateForeignKeyTests = []ScriptTest{

enginetest/queries/information_schema_queries.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,27 @@ FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'mydb'`,
11581158
SetUpScript: []string{
11591159
"USE foo",
11601160
"drop table othertable",
1161-
"CREATE TABLE t (i int)",
1162-
"CREATE VIEW v as select * from t",
1161+
"CREATE TABLE t (i int primary key, j int default (uuid_to_bin(uuid())));",
1162+
"CREATE VIEW v as select i + 1, j, j * 2, mod(i, j) from t;",
1163+
"create table tt (ii int primary key, jj int default (pow(1, 2)));",
1164+
"create view vv as select * from t join tt where i = ii;",
11631165
},
11641166
Assertions: []ScriptTestAssertion{
11651167
{
11661168
Query: "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'foo'",
11671169
Expected: []sql.Row{
1168-
{"def", "foo", "t", "i", uint32(1), nil, "YES", "int", nil, nil, int64(10), int64(0), nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1169-
{"def", "foo", "v", "", uint32(0), nil, "", nil, nil, nil, nil, nil, nil, "", "", "", "", "", "select", "", "", nil},
1170+
{"def", "foo", "t", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1171+
{"def", "foo", "t", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1172+
{"def", "foo", "tt", "ii", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "PRI", "", "insert,references,select,update", "", "", nil},
1173+
{"def", "foo", "tt", "jj", uint32(2), "power(1, 2)", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1174+
{"def", "foo", "v", "i + 1", uint32(1), nil, "NO", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1175+
{"def", "foo", "v", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1176+
{"def", "foo", "v", "j * 2", uint32(3), nil, "YES", "bigint", nil, nil, 19, 0, nil, nil, nil, "bigint", "", "", "insert,references,select,update", "", "", nil},
1177+
{"def", "foo", "v", "mod(i, j)", uint32(4), nil, "YES", "decimal", nil, nil, 10, 0, nil, nil, nil, "decimal(10,0)", "", "", "insert,references,select,update", "", "", nil},
1178+
{"def", "foo", "vv", "i", uint32(1), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1179+
{"def", "foo", "vv", "j", uint32(2), "UUID_TO_BIN(uuid())", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
1180+
{"def", "foo", "vv", "ii", uint32(3), nil, "NO", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "", "insert,references,select,update", "", "", nil},
1181+
{"def", "foo", "vv", "jj", uint32(4), "power(1, 2)", "YES", "int", nil, nil, 10, 0, nil, nil, nil, "int", "", "DEFAULT_GENERATED", "insert,references,select,update", "", "", nil},
11701182
},
11711183
},
11721184
},

enginetest/queries/script_queries.go

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7448,6 +7448,73 @@ where
74487448
},
74497449
},
74507450
},
7451+
{
7452+
Name: "preserve enums through alter statements",
7453+
SetUpScript: []string{
7454+
"create table t (i int primary key, e enum('a', 'b', 'c'));",
7455+
"insert into t values (1, 'a');",
7456+
"insert into t values (2, 'b');",
7457+
"insert into t values (3, 'c');",
7458+
},
7459+
Assertions: []ScriptTestAssertion{
7460+
{
7461+
Query: "select i, e, e + 0 from t;",
7462+
Expected: []sql.Row{
7463+
{1, "a", float64(1)},
7464+
{2, "b", float64(2)},
7465+
{3, "c", float64(3)},
7466+
},
7467+
},
7468+
{
7469+
Query: "alter table t modify column e enum('c', 'a', 'b');",
7470+
Expected: []sql.Row{
7471+
{types.NewOkResult(0)},
7472+
},
7473+
},
7474+
{
7475+
Query: "select i, e, e + 0 from t;",
7476+
Expected: []sql.Row{
7477+
{1, "a", float64(2)},
7478+
{2, "b", float64(3)},
7479+
{3, "c", float64(1)},
7480+
},
7481+
},
7482+
{
7483+
Query: "alter table t modify column e enum('asdf', 'a', 'b', 'c');",
7484+
Expected: []sql.Row{
7485+
{types.NewOkResult(0)},
7486+
},
7487+
},
7488+
{
7489+
Query: "select i, e, e + 0 from t;",
7490+
Expected: []sql.Row{
7491+
{1, "a", float64(2)},
7492+
{2, "b", float64(3)},
7493+
{3, "c", float64(4)},
7494+
},
7495+
},
7496+
{
7497+
Query: "alter table t modify column e enum('abc');",
7498+
ExpectedErrStr: "value 2 is not valid for this Enum",
7499+
},
7500+
},
7501+
},
7502+
{
7503+
Name: "coalesce with system types",
7504+
SetUpScript: []string{
7505+
"create table t as select @@admin_port as port1, @@port as port2, COALESCE(@@admin_port, @@port) as\n port3;",
7506+
},
7507+
Assertions: []ScriptTestAssertion{
7508+
{
7509+
Query: "describe t;",
7510+
Expected: []sql.Row{
7511+
{"port1", "bigint", "NO", "", nil, ""},
7512+
{"port2", "bigint", "NO", "", nil, ""},
7513+
{"port3", "bigint", "NO", "", nil, ""},
7514+
},
7515+
},
7516+
},
7517+
},
74517518
}
74527519

74537520
var SpatialScriptTests = []ScriptTest{
@@ -8758,21 +8825,17 @@ var CreateDatabaseScripts = []ScriptTest{
87588825
Expected: []sql.Row{{types.NewOkResult(1)}},
87598826
},
87608827
{
8761-
SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921. Also for when run with prepares, the warning is added twice
8762-
Query: "SHOW WARNINGS /* 1 */",
8763-
Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}},
8828+
Query: "SHOW WARNINGS /* 1 */",
8829+
Expected: []sql.Row{{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"}},
87648830
},
87658831
{
87668832
Query: "CREATE DATABASE newtest1db DEFAULT COLLATE binary ENCRYPTION='Y'",
87678833
Expected: []sql.Row{{types.NewOkResult(1)}},
87688834
},
87698835
{
8770-
SkipResultCheckOnServerEngine: true, // tracking issue here, https://github.com/dolthub/dolt/issues/6921.
8771-
// TODO: There should only be one warning (the warnings are not clearing for create database query) AND 'PREPARE' statements should not create warning from its query
87728836
Query: "SHOW WARNINGS /* 2 */",
87738837
Expected: []sql.Row{
87748838
{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"},
8775-
{"Warning", 1235, "Setting CHARACTER SET, COLLATION and ENCRYPTION are not supported yet"},
87768839
},
87778840
},
87788841
{
@@ -8888,10 +8951,8 @@ var DropDatabaseScripts = []ScriptTest{
88888951
Expected: []sql.Row{{types.OkResult{RowsAffected: 1}}},
88898952
},
88908953
{
8891-
// TODO: there should not be warning
8892-
// https://github.com/dolthub/dolt/issues/6921
88938954
Query: "SHOW WARNINGS",
8894-
Expected: []sql.Row{{"Note", 1008, "Can't drop database mydb; database doesn't exist "}},
8955+
Expected: []sql.Row{},
88958956
},
88968957
{
88978958
Query: "SELECT DATABASE()",

0 commit comments

Comments
 (0)