From 0c33572a96eb9c296efa567e0028f853fb3bc09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 4 Jul 2017 23:15:23 +0300 Subject: [PATCH] MXS-1310: Detect implicit database usage When the current database is implicitly used in a query that also uses an explicit database, it must be routed to the shard which has the current database. As cross-shard joins are not supported, the safest, and possibly the most expected course of action to take, is to route it to the so-called default shard. The default shard is the shard that contains the database that is currently set as the active database with a COM_INIT_DB, a text protocol USE query or it was set at connection time. --- .../routing/schemarouter/schemarouter.c | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/server/modules/routing/schemarouter/schemarouter.c b/server/modules/routing/schemarouter/schemarouter.c index bc7811a41..74972ed28 100644 --- a/server/modules/routing/schemarouter/schemarouter.c +++ b/server/modules/routing/schemarouter/schemarouter.c @@ -459,11 +459,30 @@ char* get_shard_target_name(ROUTER_INSTANCE* router, char** dbnms = NULL; char* rval = NULL, *query, *tmp = NULL; bool has_dbs = false; /**If the query targets any database other than the current one*/ + bool uses_implicit_databases = false; - dbnms = qc_get_database_names(buffer, &sz); + dbnms = qc_get_table_names(buffer, &sz, true); + + for (i = 0; i < sz; i++) + { + if (strchr(dbnms[i], '.') == NULL) + { + uses_implicit_databases = true; + } + MXS_FREE(dbnms[i]); + } + MXS_FREE(dbnms); HASHTABLE* ht = client->shardmap->hash; + if (uses_implicit_databases) + { + MXS_INFO("Query implicitly uses the current database"); + return (char*)hashtable_fetch(ht, client->current_db); + } + + dbnms = qc_get_database_names(buffer, &sz); + if (sz > 0) { for (i = 0; i < sz; i++)