diff --git a/sql/sql_base.cc b/sql/sql_base.cc --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -6566,4 +6566,133 @@ } /* + SELECT aliases live in a different namespace than the FROM columns. + Keep the same visibility distinction as find_item_in_list(): a qualified + lookup cannot use a SELECT alias, but may match an underlying field. +*/ +static void collect_unknown_item_list_candidates( + THD *thd, Item *missing, List &items, uint limit, + Unknown_column_candidates *out) +{ + const Item_ident *requested= NULL; + if (missing->type() == Item::FIELD_ITEM || + missing->type() == Item::REF_ITEM) + requested= static_cast(missing); + const bool qualified= requested && requested->table_name.str && + requested->table_name.length; + const uint count= limit && limit < items.elements ? limit : items.elements; + List_iterator it(items); + + for (uint i= 0; i < count; ++i) + { + Item *candidate= it++; + if (!candidate) + break; + bool is_field= candidate->type() == Item::FIELD_ITEM; + bool is_view_ref= candidate->type() == Item::REF_ITEM && + static_cast(candidate)->ref_type() == + Item_ref::VIEW_REF; + if (qualified) + { + if (!is_field && !is_view_ref) + continue; + Item_ident *field= static_cast(candidate); + if (!field->table_name.str || + !field->table_name.streq(requested->table_name) || + (requested->db_name.str && + (!field->db_name.str || + !field->db_name.streq(requested->db_name)))) + continue; + if (field->field_name.str) + out->add(thd, field->field_name); + } + else + { + /* An unnamed expression is not a column name the user can type. */ + if (candidate->name.str && + (candidate->is_explicit_name() || is_field || is_view_ref)) + out->add(thd, candidate->name); + /* find_item_in_list also checks fields hidden by SELECT aliases. */ + if (is_field || is_view_ref) + { + Item_ident *field= static_cast(candidate); + if (field->field_name.str && + (!candidate->name.str || + !field->field_name.streq(candidate->name))) + out->add(thd, field->field_name); + } + } + if (out->failed) + break; + } +} + +static void report_unknown_column_candidates(THD *thd, + const char *missing, + Unknown_column_candidates *out) +{ + if (!out->failed && out->short_list.length()) + { + if (out->truncated) + out->emit_note(thd); + my_printf_error(ER_BAD_FIELD_ERROR, + "Unknown column '%s' in '%s'. Available columns: %s%s", + MYF(0), missing, thd_where(thd), + out->short_list.c_ptr_safe(), + out->truncated ? "; see SHOW WARNINGS" : ""); + } + else + my_error(ER_BAD_FIELD_ERROR, MYF(0), missing, thd_where(thd)); +} + +/* Used by find_item_in_list() and by Item_ref in sql/item.cc. */ +void report_unknown_column_from_item_list(THD *thd, Item *missing, + List &items, uint limit) +{ + Unknown_column_candidates candidates; + collect_unknown_item_list_candidates(thd, missing, items, limit, + &candidates); + report_unknown_column_candidates(thd, missing->full_name(), &candidates); +} + +/* + Called only after all normal lookups have failed. Iterate over the same + local/outer name-resolution contexts, without changing the resolution itself. +*/ +void report_unknown_column_from_context(THD *thd, Item_ident *missing, + Name_resolution_context *ctx, + bool include_outer) +{ + Unknown_column_candidates candidates; + const char *table_name= missing->table_name.str; + LEX_CSTRING db= table_name ? missing->db_name : Lex_cstring(); + if (table_name && !table_name[0]) + { + table_name= NULL; + db= Lex_cstring(); + } + bool first_context= true; + for (; ctx; ctx= include_outer ? ctx->outer_context : NULL) + { + if (ctx->select_lex && (first_context || ctx->resolve_in_select_list)) + collect_unknown_item_list_candidates(thd, missing, + *(ctx->select_lex->get_item_list()), + 0, &candidates); + + TABLE_LIST *last= ctx->last_name_resolution_table; + if (last) + last= last->next_name_resolution_table; + for (TABLE_LIST *tl= ctx->first_name_resolution_table; + tl && tl != last; tl= tl->next_name_resolution_table) + collect_unknown_column_candidates(thd, tl, db, table_name, + ctx->ignored_tables, true, &candidates); + + if (candidates.failed) + break; + first_context= false; + } + report_unknown_column_candidates(thd, missing->full_name(), &candidates); +} + +/* Find field in table list. @@ -7060,7 +7060,6 @@ if (report_error != REPORT_EXCEPT_NOT_FOUND) { if (report_error == REPORT_ALL_ERRORS) - my_error(ER_BAD_FIELD_ERROR, MYF(0), - find->full_name(), thd_where(current_thd)); + report_unknown_column_from_item_list(current_thd, find, items, limit); return (Item **) 0; } @@ -7512,7 +7512,12 @@ if (!(common_field= it++)) { - my_error(ER_BAD_FIELD_ERROR, MYF(0), - ErrConvString(using_field_name).ptr(), - thd_where(current_thd)); + /* Only columns common to BOTH operands are valid USING names. */ + Unknown_column_candidates candidates; + List_iterator_fast common(*join_columns); + Natural_join_column *column; + while ((column= common++)) + candidates.add(thd, column->name()); + report_unknown_column_candidates( + thd, ErrConvString(using_field_name).ptr(), &candidates); goto err; } diff --git a/sql/sql_base.h b/sql/sql_base.h --- a/sql/sql_base.h +++ b/sql/sql_base.h @@ -210,4 +210,10 @@ Item ** find_item_in_list(Item *item, List &items, uint *counter, find_item_error_report_type report_error, enum_resolution_type *resolution, uint limit= 0); +/* Error-only helpers: no additional work on a successful name lookup. */ +void report_unknown_column_from_item_list(THD *thd, Item *missing, + List &items, uint limit); +void report_unknown_column_from_context(THD *thd, Item_ident *missing, + Name_resolution_context *context, + bool include_outer); bool setup_tables(THD *thd, Name_resolution_context *context, diff --git a/sql/item.cc b/sql/item.cc --- a/sql/item.cc +++ b/sql/item.cc @@ -6021,5 +6021,5 @@ if (upward_lookup) { - // We can't say exactly what absent table or field - my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd_where(thd)); + /* All ordinary lookups, including the outer contexts, failed. */ + report_unknown_column_from_context(thd, this, context, true); } @@ -6292,5 +6292,5 @@ if (unlikely(!select)) { - my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd_where(thd)); + report_unknown_column_from_context(thd, this, context, true); goto error; } @@ -8278,6 +8278,6 @@ if (unlikely(!outer_context)) { - /* The current reference cannot be resolved in this query. */ - my_error(ER_BAD_FIELD_ERROR,MYF(0), full_name(), thd_where(thd)); + /* SELECT aliases and visible FROM names in the current context. */ + report_unknown_column_from_context(thd, this, context, false); goto error; } @@ -8422,7 +8422,6 @@ if (unlikely(ref == 0)) { - /* The item was not a table field and not a reference */ - my_error(ER_BAD_FIELD_ERROR, MYF(0), - this->full_name(), thd_where(thd)); + /* Neither local nor outer SELECT aliases / FROM names matched. */ + report_unknown_column_from_context(thd, this, context, true); goto error; }