提交 44d4c948 编写于 作者: M Mel Kiyama 提交者: David Yozie

docs: update cast information for GPDB 5/6 (#4969)

* docs: update cast information for GPDB 5/6

Update cast information. Add information about limited text casts.
See section "Type Casts"

* docs: review comments for GPDB 5/6 updated cast information.

* docs: fix typos in updated CAST info
上级 c299405a
......@@ -23,8 +23,10 @@
engine understands. SQL queries consist of a sequence of commands. Commands consist
of a sequence of valid tokens in correct syntax order, terminated by a semicolon
(<codeph>;</codeph>). </p>
<p>For more information about SQL commands, see the <cite>Greenplum Database Reference
Guide</cite>.</p>
<p>For more information about SQL commands, see <xref
href="../../../ref_guide/sql_commands/sql_ref.xml">SQL Command
Reference</xref><ph otherprops="op-print"> in the <cite>Greenplum Database
Reference Guide</cite></ph>.</p>
<p>Greenplum Database uses PostgreSQL's structure and syntax, with some exceptions. For
more information about SQL rules and concepts in PostgreSQL, see "SQL Syntax" in the
PostgreSQL documentation.</p>
......@@ -186,8 +188,10 @@
<p>
<codeblock>sqrt(2)</codeblock>
</p>
<p>For information about using and creating functions, see <xref
href="functions-operators.xml#topic26"/>.</p>
<p>See <xref href="../../../ref_guide/function-summary.xml#topic1">Summary of
Built-in Functions</xref><ph otherprops="op-print"> in the <cite>Greenplum
Database Reference Guide</cite></ph> for lists of the built-in functions
by category. You can add custom functions, too.</p>
</body>
</topic>
<topic id="topic11" xml:lang="en">
......@@ -355,27 +359,106 @@
<topic id="topic14" xml:lang="en">
<title>Type Casts</title>
<body>
<p>A type cast specifies a conversion from one data type to another. Greenplum
Database accepts two equivalent syntaxes for type casts:</p>
<p>
<codeblock>CAST ( expression AS type )
expression::type
</codeblock>
</p>
<p>The <codeph>CAST</codeph> syntax conforms to SQL; the syntax with
<codeph>::</codeph> is historical PostgreSQL usage.</p>
<p>A cast applied to a value expression of a known type is a run-time type
conversion. The cast succeeds only if a suitable type conversion function is
defined. This differs from the use of casts with constants. A cast applied to a
string literal represents the initial assignment of a type to a literal constant
value, so it succeeds for any type if the contents of the string literal are
acceptable input syntax for the data type.</p>
<p>A type cast specifies a conversion from one data type to another. A cast applied
to a value expression of a known type is a run-time type conversion. The cast
succeeds only if a suitable type conversion function is defined. This differs
from the use of casts with constants. A cast applied to a string literal
represents the initial assignment of a type to a literal constant value, so it
succeeds for any type if the contents of the string literal are acceptable input
syntax for the data type.</p>
<p>Greenplum Database supports three types of casts applied to a value expression:
<ul id="ul_f4w_svt_rdb">
<li><i>Explicit cast</i> - Greenplum Database applies a cast when you
explicitly specify a cast between two data types. Greenplum Database
accepts two equivalent syntaxes for explicit type casts:<p>
<codeblock>CAST ( expression AS type )
expression::type</codeblock>
</p><p>The <codeph>CAST</codeph> syntax conforms to SQL; the syntax with
<codeph>::</codeph> is historical PostgreSQL usage.</p></li>
<li><i>Assignment cast</i> - Greenplum Database implicitly invokes a cast in
assignment contexts, when assigning a value to a column of the target
data type. For example, a <codeph><xref
href="../../../ref_guide/sql_commands/CREATE_CAST.xml">CREATE
CAST</xref></codeph> command with the <codeph>AS
ASSIGNMENT</codeph> clause creates a cast that is applied implicitly
in the assignment context. This example assignment cast assumes that
<codeph>tbl1.f1</codeph> is a column of type <codeph>text</codeph>.
The <codeph>INSERT</codeph> command is allowed because the value is
implicitly cast from the <codeph>integer</codeph> to
<codeph>text</codeph> type.
<codeblock>INSERT INTO tbl1 (f1) VALUES (42);</codeblock></li>
<li><i>Implicit cast</i> - Greenplum Database implicitly invokes a cast in
assignment or expression contexts. For example, a <codeph>CREATE
CAST</codeph> command with the <codeph>AS IMPLICIT</codeph> clause
creates an implicit cast, a cast that is applied implicitly in both the
assignment and expression context. This example implicit cast assumes
that <codeph>tbl1.c1</codeph> is a column of type <codeph>int</codeph>.
For the calculation in the predicate, the value of <codeph>c1</codeph>
is implicitly cast from <codeph>int</codeph> to a
<codeph>decimal</codeph>
type.<codeblock>SELECT * FROM tbl1 WHERE tbl1.c2 = (4.3 + tbl1.c1) ;</codeblock></li>
</ul></p>
<p>You can usually omit an explicit type cast if there is no ambiguity about the
type a value expression must produce; for example, when it is assigned to a
table column, the system automatically applies a type cast. The system applies
automatic casting only to casts marked "OK to apply implicitly" in system
catalogs. Other casts must be invoked with explicit casting syntax to prevent
unexpected conversions from being applied without the user's knowledge.</p>
table column, the system automatically applies a type cast. Greenplum Database
implicitly applies casts only to casts where the is defined with the cast
context of assignment or implicit in the system catalogs. Other casts must be
invoked with explicit casting syntax to prevent unexpected conversions from
being applied without the user's knowledge. </p>
<p>You can display cast information with the <codeph>psql</codeph> metacommand
<codeph>\dC</codeph>. Cast information is stored in the catalog table
<codeph>pg_cast</codeph>, and type information is stored in the catalog
table <codeph>pg_type</codeph>. </p>
<p>Greenplum Database 5.x limits implicit casts between <codeph>text</codeph> and
other non-text data types to minimize unexpected results when performing casts
to <codeph>text</codeph>. The following examples are types of queries that
contain expressions that require an explicit cast.</p>
<p>The examples assume these table
definitions.<codeblock>CREATE TABLE tbl1 (a int) DISTRIBUTED RANDOMLY ;
CREATE TABLE tbl2 (b text) DISTRIBUTED RANDOMLY ;</codeblock></p>
<ul id="ul_rgx_3xt_rdb">
<li>Queries that reference text type and non-text type columns in an
expression.<p>In this example query, the comparison expression causes a
cast
error:<codeblock>SELECT * FROM tbl1, tbl2 WHERE tbl1.a = tbl2.b ;
ERROR: operator does not exist: integer = text
LINE 1: SELECT * FROM tbl1, tbl2 WHERE tbl1.a = tbl2.b ;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.</codeblock></p><p>The
updated example casts the <codeph>text</codeph> type to an
<codeph>integer</codeph>
type.<codeblock>SELECT * FROM tbl1, tbl2 WHERE tbl1.a = tbl2.b::int;</codeblock></p></li>
<li>Queries that perform comparisons between a text type column and a non-quoted
literal such as an <codeph>integer</codeph>, <codeph>number</codeph>,
<codeph>float</codeph>, or <codeph>oid</codeph>.<p>This example query
that compares text and non-quoted integer returns an
error.<codeblock>SELECT * FROM tbl2 WHERE b = 123;</codeblock></p><p>Adding
an explicit cast to text fixes the
issue.<codeblock>SELECT * FROM tbl2 WHERE b = 123::text;</codeblock></p></li>
<li>Queries that perform comparisons between a <codeph>date</codeph> type column
or literal of type <codeph>date</codeph> and an integer-like column.<p> This
example query compares an <codeph>integer</codeph> column with a literal
of type <codeph>date</codeph> returns an
error.<codeblock>SELECT a FROM tbl1 WHERE a = '20130101'::date;</codeblock></p><p>There
is no built-in cast from integer type to <codeph>date</codeph> type.
However, you can explicitly cast an <codeph>integer</codeph> to
<codeph>text</codeph> and then to <codeph>date</codeph>. The updated
examples use the <codeph>cast</codeph> and <codeph>::</codeph>
syntax.<codeblock>SELECT * FROM tbl1 WHERE cast(cast(a AS text) AS date) = '20130101'::date;
SELECT * FROM tbl1 WHERE (a::text)::date = '20130101'::date;</codeblock></p></li>
<li>Queries that mix the <codeph>text</codeph> type and a non-text data type in
function and aggregate arguments.<p>In this example, the query that executes
the example function <codeph>concat</codeph> returns a cast
error.<codeblock>CREATE FUNCTION concat(text, text)
RETURNS text AS $$
SELECT $1 || $2
$$ STRICT LANGUAGE SQL;
SELECT concat('a', 2);</codeblock></p><p>Adding
an explicit cast from <codeph>integer</codeph> to <codeph>text</codeph>
fixes the
issue.<codeblock>SELECT concat('a', 2::text);</codeblock></p></li>
</ul>
</body>
</topic>
<topic id="topic15" xml:lang="en">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册