This commit is contained in:
Antonio Andelic 2022-04-01 13:12:54 +00:00
parent f4772d3b8f
commit d96b682a55
8 changed files with 71 additions and 37 deletions

View File

@ -0,0 +1,15 @@
#include <IO/WriteHelpers.h>
#include <Common/NamePrompter.h>
namespace DB::detail
{
void appendHintsMessageImpl(String & message, const std::vector<String> & hints)
{
if (hints.empty())
{
return;
}
message += ". Maybe you meant: " + toString(hints);
}
}

View File

@ -1,7 +1,6 @@
#pragma once
#include <base/types.h>
#include <boost/algorithm/string/join.hpp>
#include <Common/PODArray.h>
#include <algorithm>
@ -91,6 +90,10 @@ private:
}
};
namespace detail
{
void appendHintsMessageImpl(String & message, const std::vector<String> & hints);
}
template <size_t MaxNumHints, typename Self>
class IHints
@ -103,14 +106,10 @@ public:
return prompter.getHints(name, getAllRegisteredNames());
}
String getHintsString(const String & name) const
void appendHintsMessage(String & message, const String & name) const
{
auto hints = getHints(name);
/// Note: we don't use toString because it will cause writeCString naming conflict in src/Dictionaries/MongoDBDictionarySource.cpp
for (auto & hint : hints)
hint = "'" + hint + "'";
return !hints.empty() ? ", may be you meant: [" + boost::algorithm::join(hints, ",") + "]" : "";
detail::appendHintsMessageImpl(message, hints);
}
IHints() = default;
@ -126,4 +125,6 @@ private:
NamePrompter<MaxNumHints> prompter;
};
void appendHintsString(String & message, const std::vector<String> & hints, const String & name);
}

View File

@ -1046,9 +1046,12 @@ void AlterCommands::validate(const StorageInMemoryMetadata & metadata, ContextPt
if (!all_columns.has(column_name))
{
if (!command.if_exists)
throw Exception{"Wrong column name. Cannot find column " + backQuote(column_name) + " to modify"
+ all_columns.getHintsString(column_name),
{
String exception_message = fmt::format("Wrong column. Cannot find colum {} to modify", backQuote(column_name));
all_columns.appendHintsMessage(exception_message, column_name);
throw Exception{exception_message,
ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK};
}
else
continue;
}
@ -1153,19 +1156,22 @@ void AlterCommands::validate(const StorageInMemoryMetadata & metadata, ContextPt
all_columns.remove(command.column_name);
}
else if (!command.if_exists)
throw Exception(
"Wrong column name. Cannot find column " + backQuote(command.column_name) + " to drop"
+ all_columns.getHintsString(command.column_name),
ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
{
String exception_message = fmt::format("Wrong column name. Cannot find column {} to drop", backQuote(command.column_name));
all_columns.appendHintsMessage(exception_message, command.column_name);
throw Exception(exception_message, ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
}
}
else if (command.type == AlterCommand::COMMENT_COLUMN)
{
if (!all_columns.has(command.column_name))
{
if (!command.if_exists)
throw Exception{"Wrong column name. Cannot find column " + backQuote(command.column_name) + " to comment"
+ all_columns.getHintsString(command.column_name),
ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK};
{
String exception_message = fmt::format("Wrong column name. Cannot find column {} to comment", backQuote(command.column_name));
all_columns.appendHintsMessage(exception_message, command.column_name);
throw Exception(exception_message, ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
}
}
}
else if (command.type == AlterCommand::MODIFY_SETTING || command.type == AlterCommand::RESET_SETTING)
@ -1199,9 +1205,11 @@ void AlterCommands::validate(const StorageInMemoryMetadata & metadata, ContextPt
if (!all_columns.has(command.column_name))
{
if (!command.if_exists)
throw Exception{"Wrong column name. Cannot find column " + backQuote(command.column_name) + " to rename"
+ all_columns.getHintsString(command.column_name),
ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK};
{
String exception_message = fmt::format("Wrong column name. Cannot find column {} to rename", backQuote(command.column_name));
all_columns.appendHintsMessage(exception_message, command.column_name);
throw Exception(exception_message, ErrorCodes::NOT_FOUND_COLUMN_IN_BLOCK);
}
else
continue;
}

View File

@ -230,8 +230,11 @@ 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" + getHintsString(column_name), ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
{
String exception_message = fmt::format("There is no column {} in table", column_name);
appendHintsMessage(exception_message, column_name);
throw Exception(exception_message, ErrorCodes::NO_SUCH_COLUMN_IN_TABLE);
}
for (auto list_it = range.first; list_it != range.second;)
{
@ -245,8 +248,9 @@ 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" + getHintsString(column_from), ErrorCodes::LOGICAL_ERROR);
String exception_message = fmt::format("Cannot find column {} in ColumnsDescription", column_from);
appendHintsMessage(exception_message, column_from);
throw Exception(exception_message, ErrorCodes::LOGICAL_ERROR);
}
columns.get<1>().modify_key(it, [&column_to] (String & old_name)

View File

@ -149,8 +149,11 @@ public:
{
auto it = columns.get<1>().find(column_name);
if (it == columns.get<1>().end())
throw Exception(
"Cannot find column " + column_name + " in ColumnsDescription" + getHintsString(column_name), ErrorCodes::LOGICAL_ERROR);
{
String exception_message = fmt::format("Cannot find column {} in ColumnsDescription", column_name);
appendHintsMessage(exception_message, column_name);
throw Exception(exception_message, ErrorCodes::LOGICAL_ERROR);
}
removeSubcolumns(it->name);
if (!columns.get<1>().modify(it, std::forward<F>(f)))

View File

@ -335,9 +335,11 @@ const ProjectionDescription & ProjectionsDescription::get(const String & project
{
auto it = map.find(projection_name);
if (it == map.end())
throw Exception(
"There is no projection " + projection_name + " in table" + getHintsString(projection_name),
ErrorCodes::NO_SUCH_PROJECTION_IN_TABLE);
{
String exception_message = fmt::format("There is no projection {} in table", projection_name);
appendHintsMessage(exception_message, projection_name);
throw Exception(exception_message, ErrorCodes::NO_SUCH_PROJECTION_IN_TABLE);
}
return *(it->second);
}
@ -378,9 +380,10 @@ void ProjectionsDescription::remove(const String & projection_name, bool if_exis
{
if (if_exists)
return;
throw Exception(
"There is no projection " + projection_name + " in table" + getHintsString(projection_name),
ErrorCodes::NO_SUCH_PROJECTION_IN_TABLE);
String exception_message = fmt::format("There is no projection {} in table", projection_name);
appendHintsMessage(exception_message, projection_name);
throw Exception(exception_message, ErrorCodes::NO_SUCH_PROJECTION_IN_TABLE);
}
projections.erase(it->second);

View File

@ -8,10 +8,10 @@ $CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS t"
$CLICKHOUSE_CLIENT --query="CREATE TABLE t (CounterID UInt32, StartDate Date, UserID UInt32, VisitID UInt32, NestedColumn Nested(A UInt8, S String), ToDrop UInt32) ENGINE = MergeTree(StartDate, intHash32(UserID), (CounterID, StartDate, intHash32(UserID), VisitID), 8192)"
$CLICKHOUSE_CLIENT --query="ALTER TABLE t DROP COLUMN ToDro" 2>&1 | grep -q "may be you meant: \['ToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="ALTER TABLE t DROP COLUMN ToDro" 2>&1 | grep -q "Maybe you meant: \['ToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="ALTER TABLE t MODIFY COLUMN ToDro UInt64" 2>&1 | grep -q "may be you meant: \['ToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="ALTER TABLE t MODIFY COLUMN ToDro UInt64" 2>&1 | grep -q "Maybe you meant: \['ToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="ALTER TABLE t RENAME COLUMN ToDro to ToDropp" 2>&1 | grep -q "may be you meant: \['ToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="ALTER TABLE t RENAME COLUMN ToDro to ToDropp" 2>&1 | grep -q "Maybe you meant: \['ToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="DROP TABLE t"
$CLICKHOUSE_CLIENT --query="DROP TABLE t"

View File

@ -8,6 +8,6 @@ $CLICKHOUSE_CLIENT --query="DROP TABLE IF EXISTS t"
$CLICKHOUSE_CLIENT --query="create table t (x Int32, y Int32, projection pToDrop (select x, y order by x)) engine = MergeTree order by y;"
$CLICKHOUSE_CLIENT --query="ALTER TABLE t DROP PROJECTION pToDro" 2>&1 | grep -q "may be you meant: \['pToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="ALTER TABLE t DROP PROJECTION pToDro" 2>&1 | grep -q "Maybe you meant: \['pToDrop'\]" && echo 'OK' || echo 'FAIL'
$CLICKHOUSE_CLIENT --query="DROP TABLE t"
$CLICKHOUSE_CLIENT --query="DROP TABLE t"