Constant mz_sql::plan::typeconv::STRING_REG_CAST_TEMPLATE
source · const STRING_REG_CAST_TEMPLATE: &str = "
(SELECT
CASE
WHEN $1 IS NULL THEN NULL
-- Handle OID-like input, if available via {2}
WHEN {2} AND pg_catalog.substring($1, 1, 1) BETWEEN '0' AND '9' THEN
$1::pg_catalog.oid::pg_catalog.{0}
ELSE (
-- String case; look up that the item exists
SELECT o.oid
FROM mz_unsafe.mz_error_if_null(
(
-- We need to ensure a distinct here in the case of e.g. functions,
-- where multiple items share a GlobalId.
SELECT DISTINCT id AS name_id
FROM mz_internal.mz_resolve_object_name('{0}', $1)
),
-- TODO: Support the correct error code for does not exist (42883).
'{1} \"' || $1 || '\" does not exist'
) AS i (name_id),
-- Lateral lets us error separately from DNE case
LATERAL (
SELECT
CASE
-- Handle too many OIDs
WHEN mz_catalog.list_length(mz_catalog.list_agg(oid)) > 1 THEN
mz_unsafe.mz_error_if_null(
NULL::pg_catalog.{0},
'more than one {1} named \"' || $1 || '\"'
)
-- Resolve object name's OID if we know there is only one
ELSE
CAST(mz_catalog.list_agg(oid)[1] AS pg_catalog.{0})
END
FROM mz_catalog.mz_objects
WHERE id = name_id
GROUP BY id
) AS o (oid)
)
END)";
Expand description
STRING to REG*
A reg* type represents a specific type of object by oid.
Casting from a string to a reg*:
- Accepts a string that looks like an OID and converts the value to the
specified reg* type. This is available in all cases except explicitly
casting text values to regclass (e.g.
SELECT '2'::text::regclass
) - Resolves non-OID-appearing strings to objects. If this string resolves to more than one OID (e.g. functions), it errors.
The below code provides a template to accomplish this for various reg* types. Arguments in order are:
- 0: type catalog name this is casting to
- 1: the category of this reg for the error message
- 2: Whether or not to permit passing through numeric values as OIDs