CREATE SCALEOUT ROUTING FUNCTION
Use CREATE SCALEOUT ROUTING FUNCTION to define how a routing key places rows in the collocations of a database cluster group for Aurora.
Description and syntax
Creates a routing function: the rule that maps a routing key value to a collocation, and so to the member cluster that holds that collocation. A scaleout table names a routing function to place its rows. Because the function maps each key to a collocation and not to a member cluster, you can move a collocation to another member cluster without changing the function. Tables whose rows stay together share one function. For the concepts, see Scaleout tables and routing functions. To choose a function, see Choosing a routing key and routing function.
CREATE SCALEOUT ROUTING FUNCTIONfunction_name(paramtype[,paramtype...]) AS PARTITION BYmethod(arguments) IN TARGET COLLOCATION (collocation_group{ VALUES IN (value[, ...]) | VALUES IN RANGE (low, {high| MAXVALUE }) } [, ...] );method(arguments) ::= MOD(param,divisor) | HASH('algorithm',param[,param...]) | PREFIX(param, 'delimiter',depth) | COLUMN(param)
Aurora provides the MOD, HASH, PREFIX, and COLUMN functions, and the routing layer refuses any other method. User-defined routing functions aren't supported. For a placement scheme that doesn't map to one of these functions, map each database with a data mapping, or connect with session pinning.
The following rules apply to CREATE SCALEOUT ROUTING FUNCTION:
Each declared key parameter appears exactly once in
PARTITION BY. The divisor, algorithm, delimiter, and depth must be literals.All collocations in one function use
VALUES IN, or all useVALUES IN RANGE. You can't mix the two forms.VALUES IN RANGE (includes both bounds. Ranges can't overlap. Only the last range can uselow,high)MAXVALUE, and only as its upper bound.A value can't appear in two collocations. Each value or bound must fit the key's type and length, and the routing layer doesn't clamp or truncate it.
PREFIX: use a non-empty delimiter and a depth of 1 or more. With an empty delimiter, the routing layer rejects every statement on the table.COLUMNon aTIMESTAMPkey: the routing layer reads the bounds in your session's time zone, and SHOW CREATE SCALEOUT ROUTING FUNCTION shows them in your session's time zone.Every collocation name must already exist on a data mapping. Collocation names are case-sensitive.
MOD: give the divisor as an integer literal of 1 or more. A collocation can take a list of remainders withVALUES INor a range of remainders withVALUES IN RANGE.HASH: the algorithm is one of the six algorithm names. Ranges cover 0 to 9223372036854775807, and the last range can end atMAXVALUE. The routing layer hashes key columns in signature order.DROP SCALEOUT ROUTING FUNCTIONon a function that a scaleout table uses returns error 64042,Cannot drop routing function '<name>' referenced by scaleout tables.
Parameters and options
The statement takes the following parameters and options.
function_name-
The routing function name, unique in the group.
paramtype-
A key parameter with the name and type of the routing key column of each table that uses the function, for example
tenant_rf(tenant_id BIGINT).HASHtakes one or more parameters.MOD,PREFIX, andCOLUMNtake one. Use every parameter in the method arguments exactly once.The arguments, key data types, and collocation values depend on the function:
Function Arguments Key data types (Aurora MySQL) Collocation values MODKey, integer divisor TINYINT,SMALLINT,MEDIUMINT,INT,BIGINTVALUES INremainders, orVALUES IN RANGEof remaindersHASHAlgorithm string, one or more keys Integer types, CHAR,VARCHAR,TEXT,DATE,DATETIME,TIMESTAMPVALUES IN RANGEover 0 to 9223372036854775807PREFIXKey, delimiter string, integer depth CHAR,VARCHAR,TEXTVALUES INprefixes, orVALUES IN RANGEof prefixesCOLUMNKey only Integer types, CHAR,VARCHAR,DATE,DATETIME,TIMESTAMPVALUES INvalues, orVALUES IN RANGE - MOD(
param,divisor) -
The remainder of the key,
key % n, which keeps the sign of the key, from-(n-1)ton-1, as the engineMOD()function does. A negative key gives a negative remainder: underMOD(k, 6), -7 and -1 are both in bucket -1. Give the divisor as an integer literal of 1 or more. List the negative remainders in a collocation if your keys can be negative. Map the buckets withVALUES INlists. - HASH('
algorithm',param[,param...]) -
A hash of the key, as an integer from 0 to 9223372036854775807. Each collocation takes one range of that interval, so the ranges divide hash values, not key values. Several parameters give a composite key; keep the
ROUTE BY KEYcolumn order of each table the same as the parameter order. Map the ranges withVALUES IN RANGEfrom 0 toMAXVALUE.The
algorithmis a quoted string, the first argument, written in lowercase. It is one of the following:-
cityhash,xxhash, ormd5– Hashes a character key under the collation of the routing key, so two values that the collation compares as equal get the same hash. -
cityhash_normalized,xxhash_normalized, ormd5_normalized– Ignores the collation of the routing key. Lowercases a character key, removes all whitespace, and removes accents that Unicode can separate from the base letter, then hashes the result. Doesn't turnßintoss,øintoo, or full-width letters into ASCII letters. Two values that the key's collation compares as equal can go to different member clusters. For a character key, use these algorithms only when the key has a binary collation, such asutf8mb4_bin.
Both forms of an algorithm hash integer and date and time keys the same way. The routing layer computes the hash itself; no statement returns the hash or the collocation of a key value.
-
- PREFIX(
param, 'delimiter',depth) -
The key up to the end of its
depth-th segment, cut atdelimiter.PREFIXkeeps the key up to that point and matches the result against the collocations. UnderPREFIX(k, '/', 1),'us/nyc'routes by'us', and underPREFIX(k, '/', 2),'us/east/nyc'routes by'us/east'. A leading delimiter stays in the result: at depth 1,'/us/nyc'routes by'/us'.PREFIXtakes one key parameter. Map the prefixes withVALUES INlists, or map one inclusive range of prefixes withVALUES IN RANGE.Give a non-empty
delimiterand adepthof 1 or more. The routing layer matches the delimiter as plain text, compares the result under the routing key's collation, and rejects a key that has fewer segments than the depth or a prefix that no collocation lists.PREFIXisn't supported on a routing key with a Japanese collation. - COLUMN(
param) -
The key value itself.
COLUMNtakes one key parameter. Map exact values withVALUES INlists, or map one inclusive range withVALUES IN RANGE. One function uses lists or ranges, not both, and ranges can't overlap. The last range can end atMAXVALUE.COLUMNis the only routing function that routes a range predicate, and only withVALUES IN RANGEcollocations on an integer,DATE,DATETIME, orTIMESTAMPkey. On aTIMESTAMPkey, the routing layer reads the bounds in your session's time zone. collocation_group-
A collocation that a data mapping already declares in
router_collocation_group. Each name appears once in the map. - VALUES IN (
value[, ...]) -
The values of the collocation:
MODbuckets, such as(0, 3),PREFIXprefixes, such as('us', 'ca'), orCOLUMNkey values. A value belongs to one collocation only. - VALUES IN RANGE (
low, {high| MAXVALUE }) -
A range of
HASHvalues,MODremainders,PREFIXprefixes, orCOLUMNkey values, both bounds included:(0, 99)holds 0 and 99.MAXVALUEleaves the top open. All entries in one map use the same form. Ranges can't share a bound, so start each integer range at the previous high value plus 1. For how to write adjacent date, time, and string ranges, see COLUMN.
Where it runs and required privileges
Run the statement at the routing endpoint, in a session that isn't pinned, outside a transaction. A pinned session passes the statement through to the member cluster unchanged. On Aurora MySQL, inside a transaction the routing layer refuses the statement with error 1179. For the details, see Where these statements run.
The router user needs dbclustergroup_metadata_admin or dbclustergroup_super_admin. For all privileges for database cluster groups for Aurora, see Privileges.
Usage notes
Keep the following in mind when you use the statement.
-
Map the member tables first. Each collocation that the function targets must already exist in a data mapping.
-
The routing layer doesn't check that every key has a route. Under
MOD, put every bucket from 0 to divisor - 1, and from -1 to -(divisor - 1) if keys can be negative, in exactly one collocation. UnderHASH, start the ranges at 0, end them atMAXVALUE, and leave no gap. UnderPREFIXandCOLUMN, list every prefix or key value that your rows use, or cover the keys with ranges. The routing layer refuses a statement whose key maps to no collocation. -
A scaleout table spreads across at most as many member clusters as its function has collocations. Create more collocations than you have member clusters today; two or more collocations can be on one member cluster.
-
Under
MOD,PREFIX, andCOLUMN, your application can compute the collocation of a key, for example to load each member table with its own rows. UnderMOD, use the engineMOD()function, which keeps the sign of the key. UnderHASH, no statement returns the hash, so load the rows through routedINSERTstatements that carry the routing key. See Loading data into member clusters. -
A routing function has no
ALTERstatement. To change one, create a new function and switch the table to it. Change the routing function only of an empty scaleout table. The change moves no rows, so you can't read a row by its routing key if the new function sends that key to another collocation. See Changing a routing function. -
Keep your own record of the statement. You read a definition back only with SHOW CREATE SCALEOUT ROUTING FUNCTION.
-
For
COLUMN,PREFIX, andHASHwithcityhash,xxhash, ormd5, the routing layer compares string routing keys under the collation of the scaleout table. For more information, see Collations and string routing keys.
Examples
The following example creates a MOD function with twelve buckets of tenant_id, one collocation for each bucket.
CREATE SCALEOUT ROUTING FUNCTION tenant_rf(tenant_id BIGINT) AS PARTITION BY MOD(tenant_id, 12) IN TARGET COLLOCATION ( cg_00 VALUES IN (0), cg_01 VALUES IN (1), cg_02 VALUES IN (2), cg_03 VALUES IN (3), cg_04 VALUES IN (4), cg_05 VALUES IN (5), cg_06 VALUES IN (6), cg_07 VALUES IN (7), cg_08 VALUES IN (8), cg_09 VALUES IN (9), cg_10 VALUES IN (10), cg_11 VALUES IN (11) );
The following example creates two HASH functions with two equal ranges: one on a string key, and one on a composite key.
CREATE SCALEOUT ROUTING FUNCTION device_rf(device_id VARCHAR(64)) AS PARTITION BY HASH('cityhash', device_id) IN TARGET COLLOCATION ( cg_h0 VALUES IN RANGE (0, 4611686018427387903), cg_h1 VALUES IN RANGE (4611686018427387904, MAXVALUE) ); CREATE SCALEOUT ROUTING FUNCTION region_id_rf(region INT, id BIGINT) AS PARTITION BY HASH('cityhash', region, id) IN TARGET COLLOCATION ( cg_r0 VALUES IN RANGE (0, 4611686018427387903), cg_r1 VALUES IN RANGE (4611686018427387904, MAXVALUE) );
The following example creates a PREFIX function that places rows by the first segment of a tenant path, cut at a slash. This example assumes that data mappings already created the collocations C1 and C2.
CREATE SCALEOUT ROUTING FUNCTION tenant_path_fn(tenant_path VARCHAR(128)) AS PARTITION BY PREFIX(tenant_path, '/', 1) IN TARGET COLLOCATION ( C1 VALUES IN ('/us', '/ca'), C2 VALUES IN ('/eu') );
With this function, '/us/acme/42' and '/ca/beta/7' go to C1, '/eu/gamma/1' goes to C2, and '/jp/x' is rejected.
The following example creates a COLUMN function that places rows by inclusive ranges of order_id. This example assumes that data mappings already created the collocations C1, C2, and C3.
CREATE SCALEOUT ROUTING FUNCTION orders_range_fn(order_id BIGINT) AS PARTITION BY COLUMN(order_id) IN TARGET COLLOCATION ( C1 VALUES IN RANGE (1, 999999), C2 VALUES IN RANGE (1000000, 1999999), C3 VALUES IN RANGE (2000000, MAXVALUE) );
Errors
A refused statement creates nothing. For all errors from the routing layer, see Error codes for database cluster groups for Aurora.
| Aurora MySQL | Meaning |
|---|---|
64005 TARGET COLLOCATION '<group>' not found: no data mapping declares such a router_collocation_group (names are case-sensitive) (42000) |
No data mapping declares the collocation. The message lists every missing name. |
64022 Routing function '<function_name>' already exists (42000) |
The name is in use. |
1305 FUNCTION <name> does not exist. Use MOD, PREFIX, COLUMN or HASH. |
The statement names a function other than MOD, HASH, PREFIX, or COLUMN. |
1582 Incorrect parameter count in the call to routing function '<function>' |
A missing divisor, algorithm, delimiter, or depth, or more than one parameter in COLUMN. |
1064 You have an error in your SQL syntax; hash algorithm '<name>' is not supported |
The HASH algorithm isn't one of the six names, or it isn't in lowercase. |
1235 HASH algorithm name must be a string literal, found '<value>' |
The algorithm isn't a quoted string, or it isn't the first argument. |
1491 The routing function MOD returns the wrong type for parameter '<param>', 1502 for a BLOB, TEXT, or JSON key, or 1235 Parameter data type is incompatible with the routing function type (42000) |
The function doesn't accept the key type. |
1235 Parameter '<param>' is declared but not used as a routing key |
A parameter is missing from the method arguments. |
1495 Multiple definition of same value <N> in TARGET COLLOCATION mappings |
A value is in two collocations. |
1517 Duplicate TARGET COLLOCATION name <collocation> |
A collocation is listed twice. |
1480 All TARGET COLLOCATION entries must use the same mapping type |
The map mixes VALUES IN and VALUES IN RANGE. |
1493 TARGET COLLOCATION '<a>' and '<b>' have overlapping VALUES IN RANGE bounds |
Ranges overlap or share a bound. |
1493 TARGET COLLOCATION '<group>' has an inverted VALUES IN RANGE; … |
The low bound is above the high bound. |
1654 for COLUMN or PREFIX, 1697 for MOD or HASH: TARGET COLLOCATION '<group>' has an invalid VALUES IN RANGE bound; each bound must have the type this routing function returns |
A bound of the wrong type, or a HASH bound above the interval. |
1235 MOD divisor must be an integer literal, found '<n>' |
A negative or non-literal divisor. |
| 1064 | Syntax error: a form outside the grammar, such as IF NOT EXISTS, an empty parameter list, or MAXVALUE in a VALUES IN list. |
1235 (42000) This operation is not supported in query routing mode. Use the cluster endpoint directly or pin the session to a member cluster. |
CREATE OR REPLACE SCALEOUT ROUTING FUNCTION. |
1179 CREATE SCALEOUT ROUTING FUNCTION is not allowed inside a transaction … |
The statement ran inside a transaction. |
1227 Access denied; you need the '<PRIVILEGE>' privilege for this operation |
The router user lacks the privilege for database cluster groups for Aurora. |
Related statements
For more information, see the following: