Skip to content

sql: use SQL-standard names for built-in regtype values - #175013

Open
Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-97456-regtype-array-names
Open

Alignyx wants to merge 1 commit into
cockroachdb:masterfrom
Alignyx:fix-97456-regtype-array-names

Conversation

@Alignyx

@Alignyx Alignyx commented Sep 10, 2026

Copy link
Copy Markdown

Fixes #97456.

Casting an array type OID to regtype currently exposes the internal catalog name instead of the SQL type name. For example:

SELECT 1016::oid::regtype;

This returns _int8; PostgreSQL returns bigint[]. The OID itself is correct: casting the result back to oid still gives 1016. The discrepancy is in the name attached to the regtype value.

performIntToOidCast resolves built-in type OIDs through types.OidToType, then uses PGName() when constructing the result. That method supplies catalog names, including the underscore-prefixed array names. The built-in branch already documents that it should return SQL-standard names, but the formatter did not implement that behavior.

This change uses SQLStandardName() at that point. The existing formatter derives an ordinary array name from its element type's SQL name and appends [], so _int8 becomes bigint[], _int4 becomes integer[], and _float8 becomes double precision[]. Reusing the type formatter also handles scalar aliases consistently: int4 displays as integer and int8 as bigint. This is preferable to stripping an underscore, which would still leave catalog spellings such as int8 in the result. The formatter already has separate cases for int2vector, oidvector, and anyarray.

There is one accompanying correction in the shared formatter. any and anyelement both belong to AnyFamily, which previously always formatted as anyelement. Checking for oid.T_any keeps their names distinct when the cast starts using this formatter.

The cast still constructs the result with the original OID and regtype semantic type. Built-in types continue to use the existing lookup, and user-defined types continue through resolveOID, including its search-path-aware qualification. No catalog entries or type identities change.

Regression coverage includes the reported array OID, integer-to-regtype casts, an OID round trip, several other array element types, scalar aliases, pseudo-types, vector types, NULL, and the zero OID. The SQL expression cases run through both the optimized and non-optimized paths of TestEval. A separate type-package test checks the exact names of any, anyelement, and anyarray.

Validation:

  • The complete //pkg/sql/sem/eval:eval_test and //pkg/sql/types:types_test targets pass locally.
  • The new regression tests fail on the unchanged parent revision 8812064a015d2faf99d3fc7e15880f94042954b0: the cast cases expose catalog names, and the type formatter returns anyelement for any.
  • Go formatting was checked with crlfmt; git diff --check passes. The package tests used --norun_validations; the repository-wide test suite and full lint/generation checks were not run.

Release note (bug fix): Fixed the names displayed when casting built-in type OIDs to regtype. Array types now use SQL names such as bigint[] instead of internal catalog names such as _int8. Scalar types use their corresponding SQL names, such as integer for int4.

Casting an array type OID to regtype exposes its catalog name: OID 1016
displays as _int8 instead of bigint[]. The OID is correct, but the
built-in branch of performIntToOidCast attaches the name from PGName().

Use SQLStandardName() to format built-in regtype values. Its existing
array handling formats the element type and appends [], so integer and
floating-point arrays receive their canonical SQL names. Scalar aliases
also use SQL names, while vector and pseudo-type names retain their
special handling. User-defined types still go through resolveOID for
search-path-aware qualification.

Distinguish oid.T_any from anyelement in the shared formatter. Both
belong to AnyFamily, but must retain distinct names when used by the
regtype cast.

Add SQL expression regressions covering array names, scalar aliases,
pseudo-types, vectors, OID preservation, NULL and zero. Add an exact-name
test for the shared pseudo-type formatter.

Fixes cockroachdb#97456

Release note (bug fix): Fixed the names displayed when casting built-in
type OIDs to regtype. Array types now use SQL names such as bigint[]
instead of internal catalog names such as _int8. Scalar types use their
corresponding SQL names, such as integer for int4.
@Alignyx
Alignyx requested a review from a team as a code owner September 10, 2026 15:52
@Alignyx
Alignyx requested review from yuzefovich and removed request for a team September 10, 2026 15:52
@blathers-crl

blathers-crl Bot commented Sep 10, 2026

Copy link
Copy Markdown

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

My owl senses detect your PR is good for review. Please keep an eye out for any test failures in CI.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added the O-community Originated from the community label Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: ::regtype cast returns wrong name for arrays

1 participant