remap_rewrites

remap_rewrites is a session-scoped system variable that stores query rewrite rules as a JSON object; the rules apply to every query in the session when enable_remap_hint is enabled.

Description

remap_rewrites is a session-scoped system variable that stores query rewrite rules as a JSON object. When enable_remap_hint is enabled, these rules are applied to every query in the session.

The value accepts two kinds of rules:

  • Table rewrite rules, which map a qualified database.table name to a replacement SELECT statement.

  • remapdb rules, which map a source database name to a target database name.

Setting the variable to '' clears the session rules. The value is validated when it is set, so an invalid value is rejected immediately and the previous value stays in effect.

Table Rewrite Rules

Table rewrite rules use the same payload as the /*+ {"rewrites": {...}} */ optimizer hint. Both the bare map form and the wrapped {"rewrites": {...}} form are accepted:

{"database.table": "select ... from database.table ..."}
{"rewrites": {"database.table": "select ... from database.table ..."}}

remapdb Rules

remapdb rules map a source database name to a target database name, so qualified references such as source_db.table resolve to target_db.table:

{"remapdb": {"source_database": "target_database"}}

Both rule kinds can be combined in one value, and remapdb is applied before the table rewrite rules. For details, see remapdb.

Syntax

SET remap_rewrites = '{"database.table": "select ..."}'

Arguments

remap_rewrites takes a single value: a JSON object containing rewrites and/or remapdb rules, or '' to clear all session rules.

Examples

DROP DATABASE IF EXISTS remap_rewrites_demo;
CREATE DATABASE remap_rewrites_demo;
USE remap_rewrites_demo;

CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1, 2), (3, 4);
SET enable_remap_hint = 1;
SET remap_rewrites = '{"remap_rewrites_demo.t1": "select a, b from remap_rewrites_demo.t1 where a > 1"}';
SELECT * FROM remap_rewrites_demo.t1;
SET remap_rewrites = '';
SET enable_remap_hint = 0;

DROP DATABASE remap_rewrites_demo;

See Also