-
Notifications
You must be signed in to change notification settings - Fork 225
TRY_CONVERT #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
TRY_CONVERT #901
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a06b75
Add TRY_CONVERT extension
robozmey cb7d0c0
Remove unsuported types
robozmey 4689cbe
Fix tab indents
robozmey 9a4bc17
Make internal functions static
robozmey c4c7afa
Refactor find_typmod_conversion_function
robozmey fde465f
Add Licensies
robozmey 454bd4c
Add licence for .c file
robozmey df9a4d5
Add README for try_convert extension (#179)
robozmey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Generated subdirectories | ||
| /results/ | ||
| /sql/ | ||
| /expected/ | ||
| /input/ | ||
| /output/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # contrib/try_convert/Makefile | ||
|
|
||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| MODULE_big = try_convert | ||
| OBJS = try_convert.o $(WIN32RES) | ||
|
|
||
| EXTENSION = try_convert | ||
| DATA = try_convert--1.0.sql | ||
| PGFILEDESC = "try_convert - function for type cast" | ||
| REGRESS = try_convert | ||
|
|
||
| ifdef USE_PGXS | ||
| PG_CONFIG = pg_config | ||
| PGXS := $(shell $(PG_CONFIG) --pgxs) | ||
| include $(PGXS) | ||
| else | ||
| subdir = contrib/try_convert | ||
| top_builddir = ../.. | ||
| include $(top_builddir)/src/Makefile.global | ||
| include $(top_srcdir)/contrib/contrib-global.mk | ||
| endif | ||
|
|
||
| .PHONY: generate-tests | ||
| generate-tests: | ||
| mkdir -p input | ||
| mkdir -p output | ||
| python3 scripts/generate_test.py | ||
|
|
||
| all: generate-tests | ||
|
|
||
| .PHONY: verify | ||
| verify: | ||
| python3 scripts/verify.py | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,92 @@ | ||||||
| # TRY_CONVERT | ||||||
|
|
||||||
| TRY_CONVERT is Greenplum extension, which adds function for error-safe type cast like [TRY_CAST from SQL-Server](https://learn.microsoft.com/ru-ru/sql/t-sql/functions/try-cast-transact-sql?view=sql-server-ver16) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ## Usage | ||||||
|
|
||||||
| ``` | ||||||
| TRY_CONVERT(SOURCE_VALUE, DEFAULT_VALUE::TARGET_TYPE) | ||||||
| returns (VALUE_IN_TARGET_TYPE or DEFAULT_VALUE) | ||||||
| ``` | ||||||
|
|
||||||
| ``` | ||||||
| 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 | ||||||
| ``` | ||||||
|
|
||||||
| ### Extension's type casts | ||||||
|
|
||||||
| Casting from extensions types is able only for extensions: | ||||||
|
|
||||||
| - hstore | ||||||
| - citext | ||||||
|
|
||||||
| To enable casting from hstore and citext types use, `add_type_for_try_convert(regtype)` function | ||||||
|
|
||||||
| ## Error handling | ||||||
|
|
||||||
| To handle errors from type cast we use "soft" error handling concept, introduced Postgres 17 (https://github.com/postgres/postgres/commit/ccff2d20ed9622815df2a7deffce8a7b14830965), that concept we spreaded on data types in [21be368 | ||||||
| Preview](https://github.com/open-gpdb/gpdb/commit/21be3688729ec4468ffd083da197721860fa2cbd) and [d31f362 | ||||||
| ](https://github.com/open-gpdb/gpdb/commit/d31f362250105e456961c2c9249693e42e67eca9) commits. | ||||||
|
|
||||||
| ## Why signature is so strange? | ||||||
|
|
||||||
| Greenplum function polymorphism accept to have polymorphic functions only one any type in signature. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ## Supported casts | ||||||
|
|
||||||
| ✅ Values Cast | ||||||
| ✅ Types with typemod | ||||||
| ❌ Array-Array Cast | ||||||
| ❌ To Domain type cast | ||||||
|
|
||||||
| ## Benchmark results by pgbench | ||||||
|
|
||||||
|
|
||||||
| | | without errors | with errors | | ||||||
| | --- | --- | --- | | ||||||
| | cast | 299.346 | ❌ fails | | ||||||
| | try_convert | 984.280 | 1004.524 | | ||||||
| | sql | 1384.784 | 5787.115 | | ||||||
| | sql execute | 5843.220 | 5898.813 | | ||||||
|
|
||||||
|
|
||||||
| SQL version: | ||||||
| ``` | ||||||
| CREATE OR REPLACE FUNCTION try_convert_into_int(_in text, d int2) RETURNS int2 | ||||||
| LANGUAGE plpgsql AS | ||||||
| $func$ | ||||||
| BEGIN | ||||||
| RETURN CAST(_in AS int2); | ||||||
| EXCEPTION WHEN others THEN | ||||||
| RETURN d; | ||||||
| END | ||||||
| $func$; | ||||||
| ``` | ||||||
|
|
||||||
|
|
||||||
| SQL with execute version: | ||||||
| ``` | ||||||
| CREATE OR REPLACE FUNCTION try_convert_by_sql(_in text, INOUT _out ANYELEMENT) | ||||||
| LANGUAGE plpgsql AS | ||||||
| $func$ | ||||||
| BEGIN | ||||||
| EXECUTE format('SELECT %L::%s', $1, pg_typeof(_out)) | ||||||
| INTO _out; | ||||||
| EXCEPTION WHEN others THEN | ||||||
| -- do nothing: _out already carries default | ||||||
| END | ||||||
| $func$; | ||||||
| ``` | ||||||
|
|
||||||
| Data: | ||||||
| ``` | ||||||
| drop table if exists text_ints; create table text_ints (n text); | ||||||
| Insert into text_ints(n) select (random()*1000)::int4::text from generate_series(1,1000000); | ||||||
|
|
||||||
| drop table if exists text_error_ints; create table text_error_ints (n text); | ||||||
| Insert into text_error_ints(n) select (random()*1000000)::int8::text from generate_series(1,1000000); | ||||||
| ``` | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 13:35:45 | ||
| 13:35:90 | ||
| 13:121:45 | ||
| 2323:35:45 | ||
| 13:35:-45 | ||
| 13:-35:45 | ||
| -13:35:45 | ||
|
|
||
| 12:52:43 AM | ||
| 15:52:43 AM | ||
| 12:152:43 AM | ||
| 12:52:143 AM | ||
| 12:52:43 PM | ||
| 15:52:43 PM | ||
| 12:152:43 PM | ||
| 12:52:143 PM | ||
|
|
||
| 21:52:07 JST | ||
| 21:52:07 MSSSSSK | ||
|
|
||
| 21:52:07+03:02 | ||
| 21:52:07+33:02 | ||
| 21:52:07-33:02 | ||
| 21:52:07+03:222 | ||
|
|
||
| 1982-06-27 18:52:43 | ||
| 1982-06-32 18:52:43 | ||
| 1982-32-27 18:52:43 | ||
| 99999999999999-06-27 18:52:43 | ||
|
|
||
| 1982-12-12 01:01:59+09:00 | ||
| 1982-12-12 01:01:59+29:00 | ||
|
|
||
| 1982-06-27 | ||
| 1982-06-32 | ||
| 1982-32-27 | ||
| 999999999999999-06-27 | ||
|
|
||
| 11-30-0002 BC | ||
| 11-30-200000000 BC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1 | ||
| 2 | ||
| 333333333 | ||
| 333333333333333333333 | ||
| 5555555555555555555555555555555555555555555 | ||
| dfgdg435fw2342rf445 | ||
| 6.6332342343243242342342342342343243244324 | ||
| 354345345345435345345345345.23467567867325345345 | ||
| $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ | ||
| 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7 | ||
| Twenty-two | ||
| 12234441231.13123123 | ||
| 485728435843759345135.4324234234 | ||
| 4823849249230.f | ||
| 19203234723472-e102 | ||
| 0000000000000.00000000000000000001 | ||
| 234324+e4443 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1 | ||
| 2 | ||
| 333333333 | ||
| 333333333333333333333 | ||
| 5555555555555555555555555555555555555555555 | ||
| dfgdg435fw2342rf445 | ||
| 6.6332342343243242342342342342343243244324 | ||
| 354345345345435345345345345.23467567867325345345 | ||
| $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ | ||
| 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7 | ||
| Twenty-two | ||
| 12234441231.13123123 | ||
| 485728435843759345135.4324234234 | ||
| 4823849249230.f | ||
| 19203234723472-e102 | ||
| 0000000000000.00000000000000000001 | ||
| 234324+e4443 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1 | ||
| 2 | ||
| 333333333 | ||
| 333333333333333333333 | ||
| 5555555555555555555555555555555555555555555 | ||
| dfgdg435fw2342rf445 | ||
| 6.6332342343243242342342342342343243244324 | ||
| 354345345345435345345345345.23467567867325345345 | ||
| $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ | ||
| 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7 | ||
| Twenty-two | ||
| 12234441231.13123123 | ||
| 485728435843759345135.4324234234 | ||
| 4823849249230.f | ||
| 19203234723472-e102 | ||
| 0000000000000.00000000000000000001 | ||
| 234324+e4443 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1 | ||
| 2 | ||
| 333333333 | ||
| 333333333333333333333 | ||
| 5555555555555555555555555555555555555555555 | ||
| dfgdg435fw2342rf445 | ||
| 6.6332342343243242342342342342343243244324 | ||
| 354345345345435345345345345.23467567867325345345 | ||
| $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ | ||
| 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7 | ||
| Twenty-two | ||
| 12234441231.13123123 | ||
| 485728435843759345135.4324234234 | ||
| 4823849249230.f | ||
| 19203234723472-e102 | ||
| 0000000000000.00000000000000000001 | ||
| 234324+e4443 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1 | ||
| 2 | ||
| 333333333 | ||
| 333333333333333333333 | ||
| 5555555555555555555555555555555555555555555 | ||
| dfgdg435fw2342rf445 | ||
| 6.6332342343243242342342342342343243244324 | ||
| 354345345345435345345345345.23467567867325345345 | ||
| $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ | ||
| 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7 | ||
| Twenty-two | ||
| 12234441231.13123123 | ||
| 485728435843759345135.4324234234 | ||
| 4823849249230.f | ||
| 19203234723472-e102 | ||
| 0000000000000.00000000000000000001 | ||
| 234324+e4443 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
|
|
||
| [ | ||
| {"world":"CC", "query":"AA", "line":14, "disabled":false, "date":"CAC", "coauthors":"AB"} | ||
| {"world":"CCquery":"AA"} | ||
| {,,,,,,} | ||
| {"world":"CC",,,,, "query":"AA", "line":14, "disabled":false, "date":"CAC", "coauthors":"AB"} | ||
| {"world":"CC", "query"::"AA", "line":14, "disabled":false, "date":"CAC", "coauthors":"AB"} | ||
|
|
||
| {"world"} | ||
| {"world":"CC", "query":["AA":"BB", "sds"], "line":14, "disabled":false, "date":"CAC", "coauthors":"AB"} | ||
| }}}}}}}} | ||
| [[[[[[]]]]]] | ||
| {[]} | ||
| [{}] | ||
| ][][][][][][] | ||
| {}}{}{}}}{{{}{}{}{}}}} | ||
| kfgbidsfgadgbdfiugbhdiug |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 1 | ||
| 2 | ||
| 333333333 | ||
| 333333333333333333333 | ||
| 5555555555555555555555555555555555555555555 | ||
| dfgdg435fw2342rf445 | ||
| 6.6332342343243242342342342342343243244324 | ||
| 354345345345435345345345345.23467567867325345345 | ||
| $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ | ||
| 7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7.7 | ||
| Twenty-two | ||
| 12234441231.13123123 | ||
| 485728435843759345135.4324234234 | ||
| 4823849249230.f | ||
| 19203234723472-e102 | ||
| 0000000000000.00000000000000000001 | ||
| 234324+e4443 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| 13:35:45 | ||
| 13:35:90 | ||
| 13:121:45 | ||
| 2323:35:45 | ||
| 13:35:-45 | ||
| 13:-35:45 | ||
| -13:35:45 | ||
|
|
||
| 12:52:43 AM | ||
| 15:52:43 AM | ||
| 12:152:43 AM | ||
| 12:52:143 AM | ||
| 12:52:43 PM | ||
| 15:52:43 PM | ||
| 12:152:43 PM | ||
| 12:52:143 PM | ||
|
|
||
| 21:52:07 JST | ||
| 21:52:07 MSSSSSK | ||
|
|
||
| 21:52:07+03:02 | ||
| 21:52:07+33:02 | ||
| 21:52:07-33:02 | ||
| 21:52:07+03:222 | ||
|
|
||
| 1982-06-27 18:52:43 | ||
| 1982-06-32 18:52:43 | ||
| 1982-32-27 18:52:43 | ||
| 99999999999999-06-27 18:52:43 | ||
|
|
||
| 1982-12-12 01:01:59+09:00 | ||
| 1982-12-12 01:01:59+19:00 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.