2018-08-20 03:34:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-08 17:08:36 +00:00
|
|
|
#include <optional>
|
2023-03-03 12:40:16 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2018-08-20 03:34:10 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-03-06 13:14:03 +00:00
|
|
|
class Lexer;
|
|
|
|
|
2022-03-17 09:49:14 +00:00
|
|
|
/// Checks expected server and client error codes.
|
2021-06-01 05:54:28 +00:00
|
|
|
///
|
|
|
|
/// The following comment hints are supported:
|
|
|
|
///
|
|
|
|
/// - "-- { serverError 60 }" -- in case of you are expecting server error.
|
2023-03-08 11:13:28 +00:00
|
|
|
/// - "-- { serverError 16, 36 }" -- in case of you are expecting one of the 2 errors.
|
2021-06-01 05:54:28 +00:00
|
|
|
///
|
|
|
|
/// - "-- { clientError 20 }" -- in case of you are expecting client error.
|
2023-03-06 13:20:45 +00:00
|
|
|
/// - "-- { clientError 20, 60, 92 }" -- It's expected that the client will return one of the 3 errors.
|
2021-06-01 05:54:28 +00:00
|
|
|
///
|
2021-08-08 17:08:36 +00:00
|
|
|
/// - "-- { serverError FUNCTION_THROW_IF_VALUE_IS_NON_ZERO }" -- by error name.
|
2023-03-06 13:53:32 +00:00
|
|
|
/// - "-- { serverError NO_SUCH_COLUMN_IN_TABLE, BAD_ARGUMENTS }" -- by error name.
|
2021-08-08 17:08:36 +00:00
|
|
|
///
|
|
|
|
/// - "-- { clientError FUNCTION_THROW_IF_VALUE_IS_NON_ZERO }" -- by error name.
|
|
|
|
///
|
2021-06-01 05:54:28 +00:00
|
|
|
/// Remember that the client parse the query first (not the server), so for
|
|
|
|
/// example if you are expecting syntax error, then you should use
|
|
|
|
/// clientError not serverError.
|
|
|
|
///
|
|
|
|
/// Examples:
|
|
|
|
///
|
2022-03-17 09:49:14 +00:00
|
|
|
/// - echo 'select / -- { clientError 62 }' | clickhouse-client -nm
|
2021-06-01 05:54:28 +00:00
|
|
|
///
|
|
|
|
// Here the client parses the query but it is incorrect, so it expects
|
|
|
|
/// SYNTAX_ERROR (62).
|
|
|
|
///
|
2022-03-17 09:49:14 +00:00
|
|
|
/// - echo 'select foo -- { serverError 47 }' | clickhouse-client -nm
|
2021-06-01 05:54:28 +00:00
|
|
|
///
|
|
|
|
/// But here the query is correct, but there is no such column "foo", so it
|
|
|
|
/// is UNKNOWN_IDENTIFIER server error.
|
|
|
|
///
|
|
|
|
/// The following hints will control the query echo mode (i.e print each query):
|
|
|
|
///
|
|
|
|
/// - "-- { echo }"
|
|
|
|
/// - "-- { echoOn }"
|
|
|
|
/// - "-- { echoOff }"
|
2018-08-20 03:34:10 +00:00
|
|
|
class TestHint
|
|
|
|
{
|
|
|
|
public:
|
2023-03-08 11:13:28 +00:00
|
|
|
using ErrorVector = std::vector<int>;
|
2022-03-17 09:49:14 +00:00
|
|
|
TestHint(const String & query_);
|
2018-08-20 03:34:10 +00:00
|
|
|
|
2023-03-03 12:40:16 +00:00
|
|
|
const auto & serverErrors() const { return server_errors; }
|
|
|
|
const auto & clientErrors() const { return client_errors; }
|
2021-06-01 05:54:28 +00:00
|
|
|
std::optional<bool> echoQueries() const { return echo; }
|
2018-08-20 03:34:10 +00:00
|
|
|
|
2023-03-08 11:13:28 +00:00
|
|
|
bool hasClientErrors() { return !client_errors.empty(); }
|
|
|
|
bool hasServerErrors() { return !server_errors.empty(); }
|
|
|
|
|
2018-08-20 03:34:10 +00:00
|
|
|
private:
|
2020-07-21 14:05:30 +00:00
|
|
|
const String & query;
|
2023-03-08 11:13:28 +00:00
|
|
|
ErrorVector server_errors{};
|
|
|
|
ErrorVector client_errors{};
|
2021-06-01 05:54:28 +00:00
|
|
|
std::optional<bool> echo;
|
2018-08-20 03:34:10 +00:00
|
|
|
|
2023-03-06 13:14:03 +00:00
|
|
|
void parse(Lexer & comment_lexer, bool is_leading_hint);
|
2018-08-20 03:34:10 +00:00
|
|
|
|
|
|
|
bool allErrorsExpected(int actual_server_error, int actual_client_error) const
|
|
|
|
{
|
2023-03-03 12:40:16 +00:00
|
|
|
if (actual_server_error && std::find(server_errors.begin(), server_errors.end(), actual_server_error) == server_errors.end())
|
|
|
|
return false;
|
|
|
|
if (!actual_server_error && server_errors.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (actual_client_error && std::find(client_errors.begin(), client_errors.end(), actual_client_error) == client_errors.end())
|
|
|
|
return false;
|
|
|
|
if (!actual_client_error && client_errors.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2018-08-20 03:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool lostExpectedError(int actual_server_error, int actual_client_error) const
|
|
|
|
{
|
2023-03-03 12:40:16 +00:00
|
|
|
return (server_errors.size() && !actual_server_error) || (client_errors.size() && !actual_client_error);
|
2018-08-20 03:34:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2023-03-03 12:40:16 +00:00
|
|
|
|
|
|
|
template <>
|
2023-03-08 11:13:28 +00:00
|
|
|
struct fmt::formatter<DB::TestHint::ErrorVector>
|
2023-03-03 12:40:16 +00:00
|
|
|
{
|
|
|
|
static constexpr auto parse(format_parse_context & ctx)
|
|
|
|
{
|
|
|
|
const auto * it = ctx.begin();
|
|
|
|
const auto * end = ctx.end();
|
|
|
|
|
|
|
|
/// Only support {}.
|
|
|
|
if (it != end && *it != '}')
|
|
|
|
throw format_error("Invalid format");
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FormatContext>
|
2023-03-08 11:13:28 +00:00
|
|
|
auto format(const DB::TestHint::ErrorVector & ErrorVector, FormatContext & ctx)
|
2023-03-03 12:40:16 +00:00
|
|
|
{
|
2023-03-08 11:13:28 +00:00
|
|
|
if (ErrorVector.empty())
|
2023-03-03 12:40:16 +00:00
|
|
|
return format_to(ctx.out(), "{}", 0);
|
2023-03-08 11:13:28 +00:00
|
|
|
else if (ErrorVector.size() == 1)
|
|
|
|
return format_to(ctx.out(), "{}", ErrorVector[0]);
|
2023-03-03 12:40:16 +00:00
|
|
|
else
|
2023-03-08 11:13:28 +00:00
|
|
|
return format_to(ctx.out(), "[{}]", fmt::join(ErrorVector, ", "));
|
2023-03-03 12:40:16 +00:00
|
|
|
}
|
|
|
|
};
|