Feature: add TRY_CONVERT extension - #1872
Conversation
|
The initial PR for this feature was #901 but I didn't have push access to the original fork, and the head branch of an existing PR couldn't be changed, so instead of updating this one I opened a separate PR #1872 with the rebased version. All credit for the extension goes to @robozmey - the commit lists him as a co-author. On top of the rebase I addressed the open review comments, pulled in the follow-up fixes the extension got in open-gpdb since this PR was opened, added the module to the ic-contrib CI matrix (nothing was running its tests before), and marked the extension trusted so that a non-superuser with CREATE privilege on the database can install it. |
leborchuk
left a comment
There was a problem hiding this comment.
LGTM, we want to add it to Cloudberry
my-ship-it
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the contribution.
TRY_CONVERT(source_value, default_value) casts source_value to the type
of default_value and returns default_value whenever the cast fails,
which is the behaviour of TRY_CAST in SQL Server. Without it a single
malformed value makes the whole query fail, and the plpgsql workarounds
that catch the error per row are several times slower.
TRY_CONVERT('42'::text, NULL::int2) -- returns 42::int2
TRY_CONVERT('42d'::text, NULL::int2) -- returns NULL::int2
TRY_CONVERT('42d'::text, 1234::int2) -- returns 1234::int2
The conversion to use is resolved the same way the parser resolves an
explicit cast in coerce_type(): a pg_cast entry, an I/O conversion or a
binary-compatible relabel, followed by the length coercion of the
target type. Casts between array types and casts to domain types are
not supported and are reported as query errors, the same as a cast that
does not exist at all; only a failure caused by the converted data is
turned into the default value.
The cast itself runs inside a PG_TRY() block, since the datatype input
functions of Cloudberry cannot yet report a conversion failure without
throwing.
The extension is marked trusted, so the owner of a database can install
it without being a superuser.
Co-authored-by: Vladimir Rachkin <vova@kpnn.ru>
See: PR#901 <apache#901>
ac07b4e to
9e88271
Compare
Feature: add TRY_CONVERT extension
TRY_CONVERT(source_value, default_value) casts source_value to the type of default_value and returns default_value whenever the cast fails, which is the behaviour of TRY_CAST in SQL Server. Without it a single malformed value makes the whole query fail, and the plpgsql workarounds that catch the error per row are several times slower.
The conversion to use is resolved the same way the parser resolves an explicit cast in coerce_type(): a pg_cast entry, an I/O conversion or a binary-compatible relabel, followed by the length coercion of the target type. Casts between array types and casts to domain types are not supported and are reported as query errors, the same as a cast that does not exist at all; only a failure caused by the converted data is turned into the default value.
The cast itself runs inside a PG_TRY() block, since the datatype input functions of Cloudberry cannot yet report a conversion failure without throwing.
The extension is marked trusted, so the owner of a database can install it without being a superuser.
Co-authored-by: Vladimir Rachkin vova@kpnn.ru
See: PR#901 #901
Fixes #ISSUE_Number
What does this PR do?
Type of Change
Breaking Changes
Test Plan
make installcheckmake -C src/test installcheck-cbdb-parallelImpact
Performance:
User-facing changes:
Dependencies:
Checklist
Additional Context
CI Skip Instructions