Skip to content

Commit

Permalink
Add SHOW WARNINGS support in MySQL interface
Browse files Browse the repository at this point in the history
  • Loading branch information
slvrtrn committed Jan 17, 2024
1 parent 7731297 commit cb8372a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Server/MySQLHandler.cpp
Expand Up @@ -61,6 +61,8 @@ namespace ErrorCodes
static const size_t PACKET_HEADER_SIZE = 4;
static const size_t SSL_REQUEST_PAYLOAD_SIZE = 32;

static String showWarningsReplacementQuery(const String & query);
static String showCountWarningsReplacementQuery(const String & query);
static String selectEmptyReplacementQuery(const String & query);
static String showTableStatusReplacementQuery(const String & query);
static String killConnectionIdReplacementQuery(const String & query);
Expand All @@ -86,6 +88,8 @@ MySQLHandler::MySQLHandler(
if (ssl_enabled)
server_capabilities |= CLIENT_SSL;

replacements.emplace("SHOW WARNINGS", showWarningsReplacementQuery);
replacements.emplace("SHOW COUNT(*) WARNINGS", showCountWarningsReplacementQuery);
replacements.emplace("KILL QUERY", killConnectionIdReplacementQuery);
replacements.emplace("SHOW TABLE STATUS LIKE", showTableStatusReplacementQuery);
replacements.emplace("SHOW VARIABLES", selectEmptyReplacementQuery);
Expand Down Expand Up @@ -544,6 +548,18 @@ static bool isFederatedServerSetupSetCommand(const String & query)
return re2::RE2::FullMatch(query, expr);
}

/// Always return an empty set with appropriate column definitions for SHOW WARNINGS queries
/// See also: https://dev.mysql.com/doc/refman/8.0/en/show-warnings.html
static String showWarningsReplacementQuery([[maybe_unused]] const String & query)
{
return "SELECT '' AS Level, 0::UInt32 AS Code, '' AS Message WHERE false";
}

static String showCountWarningsReplacementQuery([[maybe_unused]] const String & query)
{
return "SELECT 0::UInt64 AS `@@session.warning_count`";
}

/// Replace "[query(such as SHOW VARIABLES...)]" into "".
static String selectEmptyReplacementQuery(const String & query)
{
Expand Down
6 changes: 6 additions & 0 deletions tests/queries/0_stateless/02968_mysql_show_warnings.reference
@@ -0,0 +1,6 @@
@@session.warning_count
0
@@session.warning_count
0
@@warning_count
0
19 changes: 19 additions & 0 deletions tests/queries/0_stateless/02968_mysql_show_warnings.sh
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Tags: no-fasttest
# Tag no-fasttest: requires mysql client

# Tests the override of certain MySQL proprietary settings to ClickHouse native settings

CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CURDIR"/../shell_config.sh

${MYSQL_CLIENT} --execute "SHOW WARNINGS;"
${MYSQL_CLIENT} --execute "show warnings;"
${MYSQL_CLIENT} --execute "SHOW WARNINGS LIMIT 100;"
${MYSQL_CLIENT} --execute "show warnings limit 100;"
${MYSQL_CLIENT} --execute "SHOW WARNINGS LIMIT 100 OFFSET 100;"
${MYSQL_CLIENT} --execute "show warnings limit 100 offset 100;"
${MYSQL_CLIENT} --execute "SHOW COUNT(*) WARNINGS;"
${MYSQL_CLIENT} --execute "show count(*) warnings;"
${MYSQL_CLIENT} --execute "SELECT @@session.warning_count;"

0 comments on commit cb8372a

Please sign in to comment.