diff --git a/enginetest/queries/script_queries.go b/enginetest/queries/script_queries.go index ad4a3e82a1..de434528de 100644 --- a/enginetest/queries/script_queries.go +++ b/enginetest/queries/script_queries.go @@ -121,6 +121,60 @@ type ScriptTestAssertion struct { // Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of // the tests. var ScriptTests = []ScriptTest{ + { + // https://github.com/dolthub/go-mysql-server/issues/3216 + Name: "UNION ALL with BLOB columns", + SetUpScript: []string{ + "CREATE TABLE a(name VARCHAR(255), data BLOB)", + "CREATE TABLE b(name VARCHAR(255), data BLOB)", + "INSERT INTO a VALUES ('a-data', UNHEX('deadbeef'))", + "INSERT INTO b VALUES ('b-nodata', NULL)", + }, + Assertions: []ScriptTestAssertion{ + { + Query: "SELECT name, data FROM a UNION ALL SELECT name, data FROM b", + Expected: []sql.Row{ + {"a-data", []byte{0xde, 0xad, 0xbe, 0xef}}, + {"b-nodata", nil}, + }, + }, + { + Query: "SELECT name, HEX(data) as data_hex FROM a UNION ALL SELECT name, HEX(data) as data_hex FROM b", + Expected: []sql.Row{ + {"a-data", "DEADBEEF"}, + {"b-nodata", nil}, + }, + }, + { + Query: "SELECT name, data FROM a UNION ALL SELECT name, NULL FROM b", + Expected: []sql.Row{ + {"a-data", []byte{0xde, 0xad, 0xbe, 0xef}}, + {"b-nodata", nil}, + }, + }, + { + Query: "SELECT name, HEX(data) as data_hex FROM a UNION ALL SELECT name, HEX(NULL) as data_hex FROM b", + Expected: []sql.Row{ + {"a-data", "DEADBEEF"}, + {"b-nodata", nil}, + }, + }, + { + Query: "SELECT name, data FROM a UNION ALL SELECT name, UNHEX('') FROM b", + Expected: []sql.Row{ + {"a-data", []byte{0xde, 0xad, 0xbe, 0xef}}, + {"b-nodata", []byte{}}, + }, + }, + { + Query: "SELECT name, HEX(data) as data_hex FROM a UNION ALL SELECT name, HEX(UNHEX('')) as data_hex FROM b", + Expected: []sql.Row{ + {"a-data", "DEADBEEF"}, + {"b-nodata", ""}, + }, + }, + }, + }, { // https://github.com/dolthub/dolt/issues/9836 Skip: true, diff --git a/sql/expression/convert.go b/sql/expression/convert.go index 09f948aa9d..b15548cd67 100644 --- a/sql/expression/convert.go +++ b/sql/expression/convert.go @@ -120,6 +120,10 @@ func GetConvertToType(l, r sql.Type) string { } if !types.IsNumber(l) || !types.IsNumber(r) { + // Special handling for BLOB types - preserve binary data + if types.IsBlobType(l) || types.IsBlobType(r) { + return ConvertToBinary + } return ConvertToChar }