関数

関数の評価

  1. Check for an exact match in the pg_proc system catalog.

    pg_proc システムカタログ内で一致するかどうかを点検します。

  2. Look for the best match.

    最も良く合うものを検索します。

    1. Make a list of all functions of the same name with the same number of arguments.

      同じ名前かつ引数の数が同じ関数の全ての一覧を作成します。

    2. If only one function is in the list, use it if the input types can be coerced, and throw an error if the types cannot be coerced.

      一覧に関数が 1 つだけ存在する場合、入力の型を強制できるのであれば その関数を使用し、できなければエラーを発生します。

    3. Keep all functions with the most explicit matches for types. Keep all if there are no explicit matches and move to the next step. If only one candidate remains, use it if the type can be coerced.

      型が最も明確に合う関数を全て保持します。明確に合うものがなければ、 全てを保持して次の段階に進みます。候補が 1 つだけ残った場合は、型 を強制できるのであればその関数を使用します。

    4. If any input arguments are "unknown", categorize the input candidate arguments as boolean, numeric, string, geometric, or user-defined. If there is a mix of categories, or more than one user-defined type, throw an error because the correct choice cannot be deduced without more clues. If only one category is present, then assign the "preferred type" to the input column which had been previously "unknown".

      入力引数全てが "unknown" であった場合、入力候補の引数をブール値、数 値、文字列、地理的情報、ユーザ定義というカテゴリに分類します。カテゴ リが混在する、または、1 つ以上ユーザ定義型が存在する場合、正しい選択 を推論するための手がかりがもうありませんので、エラーを発生します。カ テゴリが 1 つだけ存在する場合、"好ましい型" を以前 "unknown" であった 入力カラムに代入します。

    5. Choose the candidate with the most exact type matches, and which matches the "preferred type" for each column category from the previous step. If there is still more than one candidate, or if there are none, then throw an error.

      最も正しく型に合う候補で、前の段階の各カラムカテゴリの "好ましい型" に合うものを選択します。複数の候補が存在する場合や候補が無くなった 場合は、エラーを発生します。

階乗関数

There is only one factorial function defined in the pg_proc catalog. So the following query automatically converts the int2 argument to int4:

pg_proc カタログには、階乗関数が 1 つのみ定義されています。ですから 次の問い合わせは自動的に int2 引数を int4 に変換します。

tgl=> select int4fac(int2 '4');
int4fac
-------
     24
(1 row)
and is actually transformed by the parser to そして、実際にはパーサによって次のように変換されます。
tgl=> select int4fac(int4(int2 '4'));
int4fac
-------
     24
(1 row)

Substring 関数

There are two substr functions declared in pg_proc. However, only one takes two arguments, of types text and int4.

pg_proc には2つの substr 関数が宣言されています。 しかし、text 型と int4 型という 2 つの型を 持つものは 1 つしかありません。

If called with a string constant of unspecified type, the type is matched up directly with the only candidate function type:

型指定の無い文字列定数を与えて呼び出した場合、その型は、1 つの候補関 数の型に直ちに合わされます。

tgl=> select substr('1234', 3);
substr
------
    34
(1 row)

If the string is declared to be of type varchar, as might be the case if it comes from a table, then the parser will try to coerce it to become text:

あるテーブルから文字列を得るといった場合にあり得るものとして、文字 列が varchar 型として宣言されていたとすると、パーサは 文字列を text 型に強制しようとします。

tgl=> select substr(varchar '1234', 3);
substr
------
    34
(1 row)
which is transformed by the parser to become は、パーサによって次のように変換されます。
tgl=> select substr(text(varchar '1234'), 3);
substr
------
    34
(1 row)

Note: There are some heuristics in the parser to optimize the relationship between the char, varchar, and text types. For this case, substr is called directly with the varchar string rather than inserting an explicit conversion call.

charvarchartext 型の間の関 係を最適化するために、パーサ内部には特別に用意されたものがあります。こ の場合、substr は、明示的な変換呼び出しを挿入する のでは無く、直接 varrchar 文字列を使って呼び出されます。

And, if the function is called with an int4, the parser will try to convert that to text:

int4 型を使って呼び出された場合、パーサは text に変換しようとします。

tgl=> select substr(1234, 3);
substr
------
    34
(1 row)
actually executes as は、実際には次のように実行します。
tgl=> select substr(text(1234), 3);
substr
------
    34
(1 row)