Overview of the Issue
When vtexplain is used to generate the query plan for a UNION query against a sharded keyspace, vtexplain can fail with errors like:
coercion should not try to coerce this value to a text: INT32(4)
This is caused by the fact that the query planner might inject WEIGHT_STRING calls into the per-shard queries. Unfortunately vtexplain treats all function calls as returning integer values (even if WEIGHT_STRING actually returns a string).
Later, UNION queries are handled via the Distinct primitive that builds a probe table. The probe table uses the NullsafeHashcode function to generate hash codes for each column, falling back to WEIGHT_STRING values if a column has an unsupported collation. When NullsafeHashcode is called for the WEIGHT_STRING column, the original column type is passed in, which causes the coercion should not try to coerce this value to a text as the WEIGHT_STRING function is assumed to return an integer, but the original columns type (VARCHAR in this case) is passed into the NullsafeHashcode function.
Reproduction Steps
Use the SQL schema from go/vt/vtexplain/testdata/test-schema.sql, and the VSchema from go/vt/vtexplain/testdata/test-vschema.json to explain a query like:
(SELECT user.id, user.name FROM user WHERE user.id = 1) UNION (SELECT user.id, user.name FROM user WHERE user.id = 2)
Binary Version
v17 and earlier seem to be affected. v18 and later are not affected.
Operating System and Environment details
Log Fragments
Overview of the Issue
When
vtexplainis used to generate the query plan for aUNIONquery against a sharded keyspace,vtexplaincan fail with errors like:This is caused by the fact that the query planner might inject
WEIGHT_STRINGcalls into the per-shard queries. Unfortunatelyvtexplaintreats all function calls as returning integer values (even ifWEIGHT_STRINGactually returns a string).Later,
UNIONqueries are handled via theDistinctprimitive that builds a probe table. The probe table uses theNullsafeHashcodefunction to generate hash codes for each column, falling back toWEIGHT_STRINGvalues if a column has an unsupported collation. WhenNullsafeHashcodeis called for theWEIGHT_STRINGcolumn, the original column type is passed in, which causes thecoercion should not try to coerce this value to a textas theWEIGHT_STRINGfunction is assumed to return an integer, but the original columns type (VARCHARin this case) is passed into theNullsafeHashcodefunction.Reproduction Steps
Use the SQL schema from
go/vt/vtexplain/testdata/test-schema.sql, and the VSchema fromgo/vt/vtexplain/testdata/test-vschema.jsonto explain a query like:Binary Version
Operating System and Environment details
Log Fragments