From 59f5f2d03cb9896928fbf8daa5be77806d3c5226 Mon Sep 17 00:00:00 2001 From: feng lv Date: Thu, 6 May 2021 13:07:36 +0000 Subject: [PATCH] Add hints for enum names add test --- src/DataTypes/EnumValues.cpp | 13 ++++++++++++- src/DataTypes/EnumValues.h | 7 +++++-- .../0_stateless/01852_hints_enum_name.reference | 1 + tests/queries/0_stateless/01852_hints_enum_name.sh | 8 ++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/01852_hints_enum_name.reference create mode 100755 tests/queries/0_stateless/01852_hints_enum_name.sh diff --git a/src/DataTypes/EnumValues.cpp b/src/DataTypes/EnumValues.cpp index d4ca7e4dfdd..39c24bf1122 100644 --- a/src/DataTypes/EnumValues.cpp +++ b/src/DataTypes/EnumValues.cpp @@ -66,11 +66,22 @@ T EnumValues::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 +Names EnumValues::getAllRegisteredNames() const +{ + Names result; + for (const auto & value : values) + result.emplace_back(value.first); + return result; +} + template class EnumValues; template class EnumValues; diff --git a/src/DataTypes/EnumValues.h b/src/DataTypes/EnumValues.h index 45ac30f9cd7..d03a8867e42 100644 --- a/src/DataTypes/EnumValues.h +++ b/src/DataTypes/EnumValues.h @@ -1,7 +1,8 @@ #pragma once -#include #include +#include +#include namespace DB { @@ -12,7 +13,7 @@ namespace ErrorCodes } template -class EnumValues +class EnumValues : public IHints<1, EnumValues> { public: using Value = std::pair; @@ -65,6 +66,8 @@ public: return std::all_of(rhs_values.begin(), rhs_values.end(), check); } + + Names getAllRegisteredNames() const override; }; } diff --git a/tests/queries/0_stateless/01852_hints_enum_name.reference b/tests/queries/0_stateless/01852_hints_enum_name.reference new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/tests/queries/0_stateless/01852_hints_enum_name.reference @@ -0,0 +1 @@ +OK diff --git a/tests/queries/0_stateless/01852_hints_enum_name.sh b/tests/queries/0_stateless/01852_hints_enum_name.sh new file mode 100755 index 00000000000..bffde6e6c8c --- /dev/null +++ b/tests/queries/0_stateless/01852_hints_enum_name.sh @@ -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' +