diff --git a/sql/sql_base.cc b/sql/sql_base.cc --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -6438,4 +6438,131 @@ } /* + Add the names visible to a failed column lookup to its diagnostics. + This is deliberately a second, read-only walk over the same name-resolution + objects; successful lookups incur no additional work. +*/ +struct Unknown_column_candidates +{ + String short_list; + String note_list; + bool truncated; + bool failed; + ulong note_part; + + Unknown_column_candidates() + : truncated(false), failed(false), note_part(0) + { + short_list.set_charset(system_charset_info); + note_list.set_charset(system_charset_info); + } + + bool append_name(String *buffer, const Lex_ident_column &name) + { + return (buffer->length() && buffer->append(", ", 2)) || + buffer->append(name.str, name.length); + } + + void emit_note(THD *thd) + { + if (note_list.length()) + { + push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE, + ER_BAD_FIELD_ERROR, + "Available columns (part %lu): %s", + ++note_part, note_list.c_ptr_safe()); + note_list.length(0); + } + } + + void add(THD *thd, const Lex_ident_column &name) + { + if (failed) + return; + + size_t separator= short_list.length() ? 2 : 0; + if (!truncated && short_list.length() + separator + name.length <= 230) + failed= append_name(&short_list, name); + else + truncated= true; + + if (failed) + return; + + separator= note_list.length() ? 2 : 0; + if (note_list.length() + separator + name.length > 350) + emit_note(thd); + failed= append_name(¬e_list, name); + } +}; + +/* + Walk exactly the table references from the failed name-resolution scope. + A qualified lookup in a NATURAL/USING join searches its operands, whereas + an unqualified lookup sees the coalesced result columns of that join. +*/ +static void collect_unknown_column_candidates( + THD *thd, TABLE_LIST *table_ref, const LEX_CSTRING &db, + const char *table_name, ignored_tables_list_t ignored_tables, + bool check_privileges, Unknown_column_candidates *out) +{ + if (out->failed || table_ref->sequence || + (table_ref->table && + ignored_list_includes_table(ignored_tables, table_ref))) + return; + + if (table_name && table_ref->nested_join && + !table_ref->field_translation) + { + List_iterator it(table_ref->nested_join->join_list); + TABLE_LIST *child; + while ((child= it++)) + collect_unknown_column_candidates(thd, child, db, table_name, + ignored_tables, check_privileges, out); + return; + } + + if (table_name && + (!table_ref->alias.streq(Lex_cstring_strlen(table_name)) || + (db.str && (!table_ref->db.str || !table_ref->db.str[0])) || + (db.str && table_ref->db.str && table_ref->db.str[0] && + (table_ref->schema_table ? + !Lex_ident_i_s_table(Lex_cstring_strlen(db.str)).streq(table_ref->db) : + strcmp(db.str, table_ref->db.str))))) + return; + + /* Incomplete join-column lists cannot safely be iterated yet. */ + if (table_ref->nested_join && !table_ref->is_join_columns_complete && + !table_ref->field_translation) + return; + + Field_iterator_table_ref fields; + fields.set(table_ref); + for (; !fields.end_of_fields(); fields.next()) + { + Field *field= fields.field(); + if (field && field->invisible == INVISIBLE_FULL) + continue; + +#ifndef NO_EMBEDDED_ACCESS_CHECKS + /* Do not disclose columns that cannot be seen with column privileges. */ + if (check_privileges && !table_ref->is_derived() && + !table_ref->is_with_table() && !table_ref->schema_table && + !is_temporary_table(table_ref)) + { + const Lex_ident_db column_db= fields.get_db_name(); + const Lex_ident_table column_table= fields.get_table_name(); + if (column_db.str && column_table.str && + !(get_column_grant(thd, fields.grant(), column_db.str, + column_table.str, fields.name()) & VIEW_ANY_ACL)) + continue; + } +#endif + out->add(thd, fields.name()); + if (out->failed) + return; + } +} + +/* Find field in table list. @@ -6720,5 +6720,26 @@ #endif - my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd_where(thd)); + Unknown_column_candidates candidates; + for (TABLE_LIST *tl= first_table; tl != last_table; + tl= tl->next_name_resolution_table) + collect_unknown_column_candidates(thd, tl, db, table_name, + ignored_tables, + check_privileges && + thd->lex->sql_command != SQLCOM_SHOW_FIELDS, + &candidates); + + if (!candidates.failed && candidates.short_list.length()) + { + if (candidates.truncated) + candidates.emit_note(thd); + my_printf_error(ER_BAD_FIELD_ERROR, + "Unknown column '%s' in '%s'. Available columns: %s%s", + MYF(0), item->full_name(), thd_where(thd), + candidates.short_list.c_ptr_safe(), + candidates.truncated ? "; see SHOW WARNINGS" : ""); + } + else + my_error(ER_BAD_FIELD_ERROR, MYF(0), + item->full_name(), thd_where(thd)); } else found= not_found_field;