⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typeconv.sgml

📁 PostgreSQL7.4.6 for Linux
💻 SGML
📖 第 1 页 / 共 2 页
字号:
 2432902008176640000(1 row)</screen></para></example></sect1><sect1 id="typeconv-func"><title>Functions</title><indexterm zone="typeconv-func"> <primary>function</primary> <secondary>type resolution in an invocation</secondary></indexterm>  <para>   The specific function to be used in a function invocation is determined   according to the following steps.  </para><procedure><title>Function Type Resolution</title><step performance="required"><para>Select the functions to be considered from the<classname>pg_proc</classname> system catalog.  If an unqualifiedfunction name was used, the functionsconsidered are those of the right name and argument count that arevisible in the current search path (see <xref linkend="ddl-schemas-path">).If a qualified function name was given, only functions in the specifiedschema are considered.</para><substeps><step performance="optional"><para>If the search path finds multiple functions of identical argument types,only the one appearing earliest in the path is considered.  But functions ofdifferent argument types are considered on an equal footing regardless ofsearch path position.</para></step></substeps></step><step performance="required"><para>Check for a function accepting exactly the input argument types.If one exists (there can be only one exact match in the set offunctions considered), use it.(Cases involving <type>unknown</type> will never find a match atthis step.)</para></step><step performance="required"><para>If no exact match is found, see whether the function call appearsto be a trivial type conversion request.  This happens if the function callhas just one argument and the function name is the same as the (internal)name of some data type.  Furthermore, the function argument must be eitheran unknown-type literal or a type that is binary-compatible with the nameddata type.  When these conditions are met, the function argument is convertedto the named data type without any actual function call.</para></step><step performance="required"><para>Look for the best match.</para><substeps><step performance="required"><para>Discard candidate functions for which the input types do not matchand cannot be converted (using an implicit conversion) to match.<type>unknown</type> literals areassumed to be convertible to anything for this purpose.  If only onecandidate remains, use it; else continue to the next step.</para></step><step performance="required"><para>Run through all candidates and keep those with the most exact matcheson input types.  (Domains are considered the same as their base typefor this purpose.)  Keep all candidates if none have any exact matches.If only one candidate remains, use it; else continue to the next step.</para></step><step performance="required"><para>Run through all candidates and keep those that accept preferred types (of theinput data type's type category) at the most positions where type conversionwill be required.Keep all candidates if none accept preferred types.If only one candidate remains, use it; else continue to the next step.</para></step><step performance="required"><para>If any input arguments are <type>unknown</type>, check the type categoriesaccepted at those argument positions by the remaining candidates.  At each position,select the <type>string</type> category if any candidate accepts that category.(This bias towards stringis appropriate since an unknown-type literal does look like a string.)Otherwise, if all the remaining candidates accept the same type category,select that category; otherwise fail becausethe correct choice cannot be deduced without more clues.Now discard candidates that do not accept the selected type category.Furthermore, if any candidate accepts a preferred type at a given argumentposition, discard candidates that accept non-preferred types for thatargument.</para></step><step performance="required"><para>If only one candidate remains, use it.  If no candidate or more than onecandidate remains,then fail.</para></step></substeps></step></procedure><para>Note that the <quote>best match</> rules are identical for operator andfunction type resolution.Some examples follow.</para><example><title>Rounding Function Argument Type Resolution</title><para>There is only one <function>round</function> function with twoarguments.  (The first is <type>numeric</type>, the second is<type>integer</type>.)  So the following query automatically convertsthe first argument of type <type>integer</type> to<type>numeric</type>:<screen>SELECT round(4, 4); round-------- 4.0000(1 row)</screen>That query is actually transformed by the parser to<screen>SELECT round(CAST (4 AS numeric), 4);</screen></para><para>Since numeric constants with decimal points are initially assigned thetype <type>numeric</type>, the following query will require no typeconversion and may therefore be slightly more efficient:<screen>SELECT round(4.0, 4);</screen></para></example><example><title>Substring Function Type Resolution</title><para>There are several <function>substr</function> functions, one of whichtakes types <type>text</type> and <type>integer</type>.  If calledwith a string constant of unspecified type, the system chooses thecandidate function that accepts an argument of the preferred category<literal>string</literal> (namely of type <type>text</type>).<screen>SELECT substr('1234', 3); substr--------     34(1 row)</screen></para><para>If the string is declared to be of type <type>varchar</type>, as might be the caseif it comes from a table, then the parser will try to convert it to become <type>text</type>:<screen>SELECT substr(varchar '1234', 3); substr--------     34(1 row)</screen>This is transformed by the parser to effectively become<screen>SELECT substr(CAST (varchar '1234' AS text), 3);</screen></para><para><note><para>The parser learns from the <structname>pg_cast</> catalog that<type>text</type> and <type>varchar</type>are binary-compatible, meaning that one can be passed to a function thataccepts the other without doing any physical conversion.  Therefore, noexplicit type conversion call is really inserted in this case.</para></note></para><para>And, if the function is called with an argument of type <type>integer</type>, the parser willtry to convert that to <type>text</type>:<screen>SELECT substr(1234, 3); substr--------     34(1 row)</screen>This actually executes as<screen>SELECT substr(CAST (1234 AS text), 3);</screen>This automatic transformation can succeed because there is animplicitly invocable cast from <type>integer</type> to<type>text</type>.</para></example></sect1><sect1 id="typeconv-query"><title>Value Storage</title>  <para>   Values to be inserted into a table are converted to the destination   column's data type according to the   following steps.  </para><procedure><title>Value Storage Type Conversion</title><step performance="required"><para>Check for an exact match with the target.</para></step><step performance="required"><para>Otherwise, try to convert the expression to the target type.  This will succeedif there is a registered cast between the two types.If the expression is an unknown-type literal, the contents ofthe literal string will be fed to the input conversion routine for the targettype.</para></step><step performance="required"><para>If the target is a fixed-length type (e.g., <type>char</type> or <type>varchar</type>declared with a length) then try to find a sizing function for the targettype.  A sizing function is a function of the same name as the type,taking two arguments of which the first is that type and the second is of type<type>integer</type>, and returning the same type.  If one is found, it is applied,passing the column's declared length as the second parameter.</para></step></procedure><example><title><type>character</type> Storage Type Conversion</title><para>For a target column declared as <type>character(20)</type> the following statementensures that the stored value is sized correctly:<screen>CREATE TABLE vv (v character(20));INSERT INTO vv SELECT 'abc' || 'def';SELECT v, length(v) FROM vv;          v           | length----------------------+-------- abcdef               |     20(1 row)</screen></para><para>What has really happened here is that the two unknown literals are resolvedto <type>text</type> by default, allowing the <literal>||</literal> operatorto be resolved as <type>text</type> concatenation.  Then the <type>text</type>result of the operator is converted to <type>bpchar</type> (<quote>blank-paddedchar</>, the internal name of the <type>character</type> data type) to match the targetcolumn type.  (Since the types <type>text</type> and<type>bpchar</type> are binary-compatible, this conversion doesnot insert any real function call.)  Finally, the sizing function<literal>bpchar(bpchar, integer)</literal> is found in the system catalogand applied to the operator's result and the stored column length.  Thistype-specific function performs the required length check and addition ofpadding spaces.</para></example></sect1><sect1 id="typeconv-union-case"><title><literal>UNION</literal>, <literal>CASE</literal>, and <literal>ARRAY</literal> Constructs</title><indexterm zone="typeconv-union-case"> <primary>UNION</primary> <secondary>determination of result type</secondary></indexterm><indexterm zone="typeconv-union-case"> <primary>CASE</primary> <secondary>determination of result type</secondary></indexterm><indexterm zone="typeconv-union-case"> <primary>ARRAY</primary> <secondary>determination of result type</secondary></indexterm><para>SQL <literal>UNION</> constructs must match up possibly dissimilartypes to become a single result set.  The resolution algorithm isapplied separately to each output column of a union query.  The<literal>INTERSECT</> and <literal>EXCEPT</> constructs resolvedissimilar types in the same way as <literal>UNION</>.  The<literal>CASE</> and <literal>ARRAY</> constructs use the identicalalgorithm to match up their component expressions and select a resultdata type.</para><procedure><title><literal>UNION</literal>, <literal>CASE</literal>, and<literal>ARRAY</literal> Type Resolution</title><step performance="required"><para>If all inputs are of type <type>unknown</type>, resolve as type<type>text</type> (the preferred type of the string category).Otherwise, ignore the <type>unknown</type> inputs while choosing the result type.</para></step><step performance="required"><para>If the non-unknown inputs are not all of the same type category, fail.</para></step><step performance="required"><para>Choose the first non-unknown input type which is a preferred type inthat category or allows all the non-unknown inputs to be implicitlyconverted to it.</para></step><step performance="required"><para>Convert all inputs to the selected type.</para></step></procedure><para>Some examples follow.</para><example><title>Type Resolution with Underspecified Types in a Union</title><para><screen>SELECT text 'a' AS "text" UNION SELECT 'b'; text------ a b(2 rows)</screen>Here, the unknown-type literal <literal>'b'</literal> will be resolved as type <type>text</type>.</para></example><example><title>Type Resolution in a Simple Union</title><para><screen>SELECT 1.2 AS "numeric" UNION SELECT 1; numeric---------       1     1.2(2 rows)</screen>The literal <literal>1.2</> is of type <type>numeric</>,and the <type>integer</type> value <literal>1</> can be cast implicitly to<type>numeric</>, so that type is used.</para></example><example><title>Type Resolution in a Transposed Union</title><para><screen>SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL); real------    1  2.2(2 rows)</screen>Here, since type <type>real</> cannot be implicitly cast to <type>integer</>,but <type>integer</> can be implicitly cast to <type>real</>, the unionresult type is resolved as <type>real</>.</para></example></sect1></chapter><!-- Keep this comment at the end of the fileLocal variables:mode:sgmlsgml-omittag:tsgml-shorttag:tsgml-minimize-attributes:nilsgml-always-quote-attributes:tsgml-indent-step:1sgml-indent-data:tsgml-parent-document:nilsgml-default-dtd-file:"./reference.ced"sgml-exposed-tags:nilsgml-local-catalogs:("/usr/lib/sgml/catalog")sgml-local-ecat-files:nilEnd:-->

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -