Add hints for enum names

add test
This commit is contained in:
feng lv 2021-05-06 13:07:36 +00:00
parent 6f08f945e8
commit 59f5f2d03c
4 changed files with 26 additions and 3 deletions

View File

@ -66,11 +66,22 @@ T EnumValues<T>::getValue(StringRef field_name, bool try_treat_as_id) const
if (tmp_buf.eof() && value_to_name_map.find(x) != value_to_name_map.end())
return x;
}
throw Exception{"Unknown element '" + field_name.toString() + "' for enum", ErrorCodes::BAD_ARGUMENTS};
auto hints = this->getHints(field_name.toString());
auto hints_string = !hints.empty() ? ", may be you meant: " + toString(hints) : "";
throw Exception{"Unknown element '" + field_name.toString() + "' for enum" + hints_string, ErrorCodes::BAD_ARGUMENTS};
}
return it->getMapped();
}
template <typename T>
Names EnumValues<T>::getAllRegisteredNames() const
{
Names result;
for (const auto & value : values)
result.emplace_back(value.first);
return result;
}
template class EnumValues<Int8>;
template class EnumValues<Int16>;

View File

@ -1,7 +1,8 @@
#pragma once
#include <Common/HashTable/HashMap.h>
#include <unordered_map>
#include <Common/HashTable/HashMap.h>
#include <Common/NamePrompter.h>
namespace DB
{
@ -12,7 +13,7 @@ namespace ErrorCodes
}
template <typename T>
class EnumValues
class EnumValues : public IHints<1, EnumValues<T>>
{
public:
using Value = std::pair<std::string, T>;
@ -65,6 +66,8 @@ public:
return std::all_of(rhs_values.begin(), rhs_values.end(), check);
}
Names getAllRegisteredNames() const override;
};
}

View File

@ -0,0 +1 @@
OK

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh
$CLICKHOUSE_CLIENT --query="SELECT CAST('Helo', 'Enum(\'Hello\' = 1, \'World\' = 2)')" 2>&1 | grep -q "may be you meant: \['Hello'\]" && echo 'OK' || echo 'FAIL'