From 4547ed370a4bbe20260ccdd6cd020b4c5d8ba55a Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Wed, 30 Mar 2022 20:54:33 +0800 Subject: [PATCH] add hints for column description --- src/Common/NamePrompter.h | 7 +++++++ src/Storages/ColumnsDescription.cpp | 21 ++++++++++++++++++--- src/Storages/ColumnsDescription.h | 7 +++++-- src/Storages/IndicesDescription.h | 1 - 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Common/NamePrompter.h b/src/Common/NamePrompter.h index a88d4bdea8e..8e301dec8b7 100644 --- a/src/Common/NamePrompter.h +++ b/src/Common/NamePrompter.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -102,6 +103,12 @@ public: return prompter.getHints(name, getAllRegisteredNames()); } + String getHintsString(const String & name) const + { + const auto hints = getHints(name); + return !hints.empty() ? ", may be you meant: " + toString(hints) : ""; + } + IHints() = default; IHints(const IHints &) = default; diff --git a/src/Storages/ColumnsDescription.cpp b/src/Storages/ColumnsDescription.cpp index 69ca6002c22..a694405665b 100644 --- a/src/Storages/ColumnsDescription.cpp +++ b/src/Storages/ColumnsDescription.cpp @@ -230,8 +230,8 @@ void ColumnsDescription::remove(const String & column_name) { auto range = getNameRange(columns, column_name); if (range.first == range.second) - throw Exception("There is no column " + column_name + " in table.", - ErrorCodes::NO_SUCH_COLUMN_IN_TABLE); + throw Exception( + "There is no column " + column_name + " in table" + getHintsString(column_name), ErrorCodes::NO_SUCH_COLUMN_IN_TABLE); for (auto list_it = range.first; list_it != range.second;) { @@ -244,7 +244,10 @@ void ColumnsDescription::rename(const String & column_from, const String & colum { auto it = columns.get<1>().find(column_from); if (it == columns.get<1>().end()) - throw Exception("Cannot find column " + column_from + " in ColumnsDescription", ErrorCodes::LOGICAL_ERROR); + { + throw Exception( + "Cannot find column " + column_from + " in ColumnsDescription" + getHintsString(column_from), ErrorCodes::LOGICAL_ERROR); + } columns.get<1>().modify_key(it, [&column_to] (String & old_name) { @@ -745,6 +748,18 @@ void ColumnsDescription::removeSubcolumns(const String & name_in_storage) subcolumns.get<1>().erase(range.first, range.second); } +std::vector ColumnsDescription::getAllRegisteredNames() const +{ + std::vector names; + names.reserve(columns.size()); + for (const auto & column : columns) + { + if (column.name.find('.') == std::string::npos) + names.push_back(column.name); + } + return names; +} + Block validateColumnsDefaultsAndGetSampleBlock(ASTPtr default_expr_list, const NamesAndTypesList & all_columns, ContextPtr context) { for (const auto & child : default_expr_list->children) diff --git a/src/Storages/ColumnsDescription.h b/src/Storages/ColumnsDescription.h index 4ae1dcfc2cd..affe2ef5a56 100644 --- a/src/Storages/ColumnsDescription.h +++ b/src/Storages/ColumnsDescription.h @@ -91,7 +91,7 @@ struct ColumnDescription /// Description of multiple table columns (in CREATE TABLE for example). -class ColumnsDescription +class ColumnsDescription : public IHints<2, ColumnsDescription> { public: ColumnsDescription() = default; @@ -149,7 +149,8 @@ public: { auto it = columns.get<1>().find(column_name); if (it == columns.get<1>().end()) - throw Exception("Cannot find column " + column_name + " in ColumnsDescription", ErrorCodes::LOGICAL_ERROR); + throw Exception( + "Cannot find column " + column_name + " in ColumnsDescription" + getHintsString(column_name), ErrorCodes::LOGICAL_ERROR); removeSubcolumns(it->name); if (!columns.get<1>().modify(it, std::forward(f))) @@ -196,6 +197,8 @@ public: return columns.empty(); } + std::vector getAllRegisteredNames() const override; + /// Keep the sequence of columns and allow to lookup by name. using ColumnsContainer = boost::multi_index_container< ColumnDescription, diff --git a/src/Storages/IndicesDescription.h b/src/Storages/IndicesDescription.h index 72e0748778f..862df6fe23c 100644 --- a/src/Storages/IndicesDescription.h +++ b/src/Storages/IndicesDescription.h @@ -74,7 +74,6 @@ struct IndicesDescription : public std::vector, IHints<1, Indi /// Return common expression for all stored indices ExpressionActionsPtr getSingleExpressionForIndices(const ColumnsDescription & columns, ContextPtr context) const; -public: Names getAllRegisteredNames() const override; };