Skip to content

Commit

Permalink
Skip the /* comments */ SELECT @@variables ... from mysql-connector…
Browse files Browse the repository at this point in the history
…-java setup for MySQL Handler ClickHouse#9336

mysql-connector setup query:
/* mysql-connector-java-5.1.38 ( Revision: ${revinfo.commit} ) */SELECT  @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout...

ClickHouse side Error:
{} <Error> executeQuery: Code: 62, e.displayText() = DB::Exception: Syntax error: failed at position 74: @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_conn. Expected one of: CAST, NULL...

Client side Exception:
java.sql.SQLException: Syntax error: failed at position 74: @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_conn. Expected one of: CAST...
  • Loading branch information
BohuTANG committed Apr 2, 2020
1 parent a87de76 commit ff16bdc
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions dbms/programs/server/MySQLHandler.cpp
Expand Up @@ -16,6 +16,7 @@
#include <IO/WriteBufferFromPocoSocket.h>
#include <Storages/IStorage.h>
#include <boost/algorithm/string/replace.hpp>
#include <boost/regex.hpp>

#if USE_POCO_NETSSL
#include <Poco/Net/SecureStreamSocket.h>
Expand Down Expand Up @@ -268,15 +269,16 @@ void MySQLHandler::comPing()
packet_sender->sendPacket(OK_Packet(0x0, client_capability_flags, 0, 0, 0), true);
}

static bool isFederatedServerSetupCommand(const String & query);
static bool isFederatedServerSetupSetCommand(const String & query);
static bool isFederatedServerSetupSelectVarCommand(const String & query);

void MySQLHandler::comQuery(ReadBuffer & payload)
{
String query = String(payload.position(), payload.buffer().end());

// This is a workaround in order to support adding ClickHouse to MySQL using federated server.
// As Clickhouse doesn't support these statements, we just send OK packet in response.
if (isFederatedServerSetupCommand(query))
if (isFederatedServerSetupSetCommand(query))
{
packet_sender->sendPacket(OK_Packet(0x00, client_capability_flags, 0, 0, 0), true);
}
Expand All @@ -288,10 +290,11 @@ void MySQLHandler::comQuery(ReadBuffer & payload)

// Translate query from MySQL to ClickHouse.
// This is a temporary workaround until ClickHouse supports the syntax "@@var_name".
if (query == "select @@version_comment limit 1") // MariaDB client starts session with that query
if (isFederatedServerSetupSelectVarCommand(query))
{
should_replace = true;
}

// This is a workaround in order to support adding ClickHouse to MySQL using federated server.
if (0 == strncasecmp("SHOW TABLE STATUS LIKE", query.c_str(), 22))
{
Expand Down Expand Up @@ -358,11 +361,26 @@ void MySQLHandlerSSL::finishHandshakeSSL(size_t packet_size, char * buf, size_t

#endif

static bool isFederatedServerSetupCommand(const String & query)
static bool isFederatedServerSetupSetCommand(const String & query)
{
static const boost::regex expr{
"(^(SET NAMES(.*)))"\
"|(^(SET character_set_results(.*)))"\
"|(^(SET FOREIGN_KEY_CHECKS(.*)))"\
"|(^(SET AUTOCOMMIT(.*)))"\
"|(^(SET sql_mode(.*)))"\
"|(^(SET SESSION TRANSACTION ISOLATION LEVEL(.*)))"\
, boost::regex::icase};
return 1 == boost::regex_match(query, expr);
}

static bool isFederatedServerSetupSelectVarCommand(const String & query)
{
return 0 == strncasecmp("SET NAMES", query.c_str(), 9) || 0 == strncasecmp("SET character_set_results", query.c_str(), 25)
|| 0 == strncasecmp("SET FOREIGN_KEY_CHECKS", query.c_str(), 22) || 0 == strncasecmp("SET AUTOCOMMIT", query.c_str(), 14)
|| 0 == strncasecmp("SET SESSION TRANSACTION ISOLATION LEVEL", query.c_str(), 39);
static const boost::regex expr{
"|(^(SELECT @@(.*)))"\
"|(^((/\\*(.*)\\*/)([ \t]*)(SELECT([ \t]*)@@(.*))))"
, boost::regex::icase};
return 1 == boost::regex_match(query, expr);
}

const String MySQLHandler::show_table_status_replacement_query("SELECT"
Expand Down

0 comments on commit ff16bdc

Please sign in to comment.