Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <database> query or it was set at connection time.
  • Loading branch information
markus456 committed Jul 4, 2017
1 parent 5ed6483 commit 0c33572
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion server/modules/routing/schemarouter/schemarouter.c
Expand Up @@ -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++)
Expand Down

0 comments on commit 0c33572

Please sign in to comment.