From 1b56c69b5dbdc4e478003d2a060c366f0d83fadb Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Fri, 25 Aug 2023 14:52:07 +0200 Subject: [PATCH] feat: figure out the type of weightstrings added late Signed-off-by: Andres Taylor --- .../endtoend/preparestmt/stmt_methods_test.go | 21 +++++++++++++++++++ go/vt/vtgate/semantics/semantic_state.go | 8 +++++++ 2 files changed, 29 insertions(+) diff --git a/go/test/endtoend/preparestmt/stmt_methods_test.go b/go/test/endtoend/preparestmt/stmt_methods_test.go index 21369ea4d3a..24fb58bff81 100644 --- a/go/test/endtoend/preparestmt/stmt_methods_test.go +++ b/go/test/endtoend/preparestmt/stmt_methods_test.go @@ -436,3 +436,24 @@ func TestShowColumns(t *testing.T) { require.Len(t, cols, 6) require.False(t, rows.Next()) } + +func TestBinaryColumn(t *testing.T) { + defer cluster.PanicHandler(t) + dbo := Connect(t, "interpolateParams=false") + defer dbo.Close() + + _, err := dbo.Query(`SELECT DISTINCT + BINARY table_info.table_name AS table_name, + table_info.create_options AS create_options, + table_info.table_comment AS table_comment + FROM information_schema.tables AS table_info + JOIN information_schema.columns AS column_info + ON BINARY column_info.table_name = BINARY table_info.table_name + WHERE + table_info.table_schema = ? + AND column_info.table_schema = ? + -- Exclude views. + AND table_info.table_type = 'BASE TABLE' + ORDER BY BINARY table_info.table_name`, keyspaceName, keyspaceName) + require.NoError(t, err) +} diff --git a/go/vt/vtgate/semantics/semantic_state.go b/go/vt/vtgate/semantics/semantic_state.go index 24ca5db5eef..95c84217fed 100644 --- a/go/vt/vtgate/semantics/semantic_state.go +++ b/go/vt/vtgate/semantics/semantic_state.go @@ -291,6 +291,14 @@ func (st *SemTable) AddExprs(tbl *sqlparser.AliasedTableExpr, cols sqlparser.Sel // TypeForExpr returns the type of expressions in the query func (st *SemTable) TypeForExpr(e sqlparser.Expr) (sqltypes.Type, collations.ID, bool) { + // We add a lot of WeightString() expressions to queries at late stages of the planning, + // which means that they don't have any type information. We can safely assume that they + // are VarBinary, since that's the only type that WeightString() can return. + _, isWS := e.(*sqlparser.WeightStringFuncExpr) + if isWS { + return sqltypes.VarBinary, collations.CollationBinaryID, true + } + if typ, found := st.ExprTypes[e]; found { return typ.Type, typ.Collation, true }