您好,登錄后才能下訂單哦!
這篇文章主要介紹“PostgreSQL的數(shù)據(jù)類型轉(zhuǎn)換規(guī)則有哪些”,在日常操作中,相信很多人在PostgreSQL的數(shù)據(jù)類型轉(zhuǎn)換規(guī)則有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL的數(shù)據(jù)類型轉(zhuǎn)換規(guī)則有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
函數(shù)調(diào)用中依賴的指定函數(shù)使用以下過程確定.
Function Type Resolution
1.從pg_proc中選擇函數(shù).通常情況下,如果不指定schema,則在當前搜索路徑中選擇名稱&參數(shù)個數(shù)匹配的,否則將選擇指定schema的函數(shù).
a.如在該搜索路徑中存在多個相同參數(shù)類型的操作符,則選擇最早出現(xiàn)的那個.對于具有不同參數(shù)類型的操作符,無論搜索路徑如何設置,都被認為是平等的.
b.如果函數(shù)使用VARIADIC數(shù)組參數(shù)聲明,但調(diào)用沒有使用VARIADIC關鍵字,那么數(shù)組參數(shù)會被替換為數(shù)組元素類型的一個或多個值以匹配函數(shù)調(diào)用.在這樣展開后,可能與NON-VARIADIC的函數(shù)有相同的參數(shù),在這種情況下,使用搜索路徑最終出現(xiàn)的那個,或者使用同一個schema中NON-VARIADIC的那個.
c.有默認參數(shù)值的函數(shù)會匹配任何省略了零個或多個默認參數(shù)位置的函數(shù)調(diào)用.如果有多個函數(shù)匹配,搜索路徑中最終出現(xiàn)的那個會被選用.如果在同一個schema中存在兩個或以上這樣的函數(shù),這時候PG無法選擇使用哪個,因此會報錯:”ambiguous function call”.
2.檢查是否接受輸入的參數(shù)類型.如存在,則使用此函數(shù).與操作符類似,會有安全上的漏洞.
3.如果沒有完全匹配的函數(shù),檢查函數(shù)調(diào)用是否需要類型轉(zhuǎn)換.這會出現(xiàn)在函數(shù)調(diào)用只有一個參數(shù)并且函數(shù)名稱與內(nèi)部函數(shù)名稱一樣.此外,函數(shù)參數(shù)必須是unknown-type literal,或者可binary-coercible為命名數(shù)據(jù)類型,或者通過I/O函數(shù)轉(zhuǎn)換為命名數(shù)據(jù)類型.如果滿足這些條件,那么函數(shù)調(diào)用會被視為CAST的形式.
4.尋找最佳匹配
參照operator操作符的說明.
下面是一些例子:
Round
round函數(shù)在pg_proc中的定義如下:
testdb=# select oid,proname,provariadic,proargtypes,prorettype,prosrc from pg_proc where proname = 'round'; oid | proname | provariadic | proargtypes | prorettype | prosrc ------+---------+-------------+-------------+------------+------------------------------- 1342 | round | 0 | 701 | 701 | dround 1707 | round | 0 | 1700 23 | 1700 | numeric_round 1708 | round | 0 | 1700 | 1700 | select pg_catalog.round($1,0) (3 rows)
其中proargtypes是參數(shù)類型,prorettype是返回類型,prosrc是函數(shù)實現(xiàn)”源碼”.
類型23/701/1700定義如下
testdb=# select oid,typname,typalign,typstorage from pg_type where oid in (23,701,1700); oid | typname | typalign | typstorage ------+---------+----------+------------ 23 | int4 | i | p 701 | float8 | d | p 1700 | numeric | i | m (3 rows)
執(zhí)行SQL
testdb=# SELECT round(4, 4); round -------- 4.0000 (1 row)
在pg_proc中,只有一個函數(shù)(oid = 1707)有兩個參數(shù),第一個參數(shù)類型視為numeric,第二個為integer,那么第一個參數(shù)4會自動轉(zhuǎn)換為numeric類型,該SQL語句與”SELECT round(CAST (4 AS numeric), 4);”無異.
Variadic
首先定義一個函數(shù)variadic_example
testdb=# CREATE FUNCTION public.variadic_example(VARIADIC numeric[]) RETURNS int testdb-# LANGUAGE sql AS 'SELECT 1'; CREATE FUNCTION
函數(shù)定義
testdb=# select oid,proname,provariadic,proargtypes,prorettype,prosrc from pg_proc where proname = 'variadic_example'; oid | proname | provariadic | proargtypes | prorettype | prosrc -------+------------------+-------------+-------------+------------+---------- 32787 | variadic_example | 1700 | 1231 | 23 | SELECT 1 (1 row) testdb=# select oid,typname,typalign,typstorage from pg_type where oid in (1231,1700); oid | typname | typalign | typstorage ------+----------+----------+------------ 1231 | _numeric | i | x 1700 | numeric | i | m (2 rows)
執(zhí)行函數(shù),該函數(shù)接受可變參數(shù)關鍵字,但不需要指定,可允許整數(shù)和數(shù)值參數(shù):
testdb=# SELECT public.variadic_example(0), testdb-# public.variadic_example(0.0), testdb-# public.variadic_example(VARIADIC array[0.0]); variadic_example | variadic_example | variadic_example ------------------+------------------+------------------ 1 | 1 | 1 (1 row)
上述第一次和第二次調(diào)用將更傾向明確定義的函數(shù):
testdb=# CREATE FUNCTION public.variadic_example(numeric) RETURNS int LANGUAGE sql AS 'SELECT 2'; testdb=# CREATE FUNCTION testdb=# CREATE FUNCTION public.variadic_example(int) RETURNS int LANGUAGE sql AS 'SELECT 3'; testdb=# CREATE FUNCTION testdb=# SELECT public.variadic_example(0), testdb-# public.variadic_example(0.0), testdb-# public.variadic_example(VARIADIC array[0.0]); variadic_example | variadic_example | variadic_example ------------------+------------------+------------------ 3 | 2 | 1 (1 row)
Substring
有多個substr函數(shù):
testdb=# select oid,proname,provariadic,proargtypes,prorettype,prosrc from pg_proc where proname = 'substr'; oid | proname | provariadic | proargtypes | prorettype | prosrc ------+---------+-------------+-------------+------------+--------------------- 877 | substr | 0 | 25 23 23 | 25 | text_substr 883 | substr | 0 | 25 23 | 25 | text_substr_no_len 2085 | substr | 0 | 17 23 23 | 17 | bytea_substr 2086 | substr | 0 | 17 23 | 17 | bytea_substr_no_len (4 rows) testdb=# select oid,typname,typalign,typstorage from pg_type where oid in (17,23,25); oid | typname | typalign | typstorage -----+---------+----------+------------ 17 | bytea | i | x 23 | int4 | i | p 25 | text | i | x (3 rows)
如未指定參數(shù)類型調(diào)用函數(shù),系統(tǒng)會優(yōu)先選擇參數(shù)為text + int4的函數(shù):
testdb=# SELECT substr('1234', 3); substr -------- 34 (1 row)
如指定類型為varchar,則轉(zhuǎn)換為text
testdb=# SELECT substr(varchar '1234', 3); substr -------- 34 (1 row) testdb=# SELECT substr(CAST (varchar '1234' AS text), 3); substr -------- 34 (1 row)
如第一個參數(shù)為integer,系統(tǒng)無轉(zhuǎn)換函數(shù),則會報錯
testdb=# SELECT substr(1234, 3); psql: ERROR: function substr(integer, integer) does not exist LINE 1: SELECT substr(1234, 3); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. testdb=#
到此,關于“PostgreSQL的數(shù)據(jù)類型轉(zhuǎn)換規(guī)則有哪些”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。