add hints for column description

This commit is contained in:
taiyang-li 2022-03-30 20:54:33 +08:00 committed by Antonio Andelic
parent 44293e6f94
commit 4547ed370a
4 changed files with 30 additions and 6 deletions

View File

@ -2,6 +2,7 @@
#include <base/types.h>
#include <Common/PODArray.h>
#include <IO/WriteHelpers.h>
#include <algorithm>
#include <cctype>
@ -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;

View File

@ -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<String> ColumnsDescription::getAllRegisteredNames() const
{
std::vector<String> 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)

View File

@ -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>(f)))
@ -196,6 +197,8 @@ public:
return columns.empty();
}
std::vector<String> getAllRegisteredNames() const override;
/// Keep the sequence of columns and allow to lookup by name.
using ColumnsContainer = boost::multi_index_container<
ColumnDescription,

View File

@ -74,7 +74,6 @@ struct IndicesDescription : public std::vector<IndexDescription>, IHints<1, Indi
/// Return common expression for all stored indices
ExpressionActionsPtr getSingleExpressionForIndices(const ColumnsDescription & columns, ContextPtr context) const;
public:
Names getAllRegisteredNames() const override;
};