Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb26dfb9

Browse files
committed
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity inoperator/function resolution. Introduce a well-defined promotion hierarchyfor numeric datatypes (int2->int4->int8->numeric->float4->float8).Change make_const to initially label numeric literals as int4, int8, ornumeric (never float8 anymore).Explicitly mark Func and RelabelType nodes to indicate whether they camefrom a function call, explicit cast, or implicit cast; use this to doreverse-listing more accurately and without so many heuristics.Explicit casts to char, varchar, bit, varbit will truncate or pad withoutraising an error (the pre-7.2 behavior), while assigning to a column withoutany explicit cast will still raise an error for wrong-length data like 7.3.This more nearly follows the SQL spec than 7.2 behavior (we should bereporting a 'completion condition' in the explicit-cast cases, but we haveno mechanism for that, so just do silent truncation).Fix some problems with enforcement of typmod for array elements;it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.Provide a generalized array_length_coerce() function to replace thespecialized per-array-type functions that used to be needed (and weremissing for NUMERIC as well as all the datetime types).Add missing conversions int8<->float4, text<->numeric, oid<->int8.initdb forced.
1 parentcc70ba2 commitb26dfb9

File tree

70 files changed

+1641
-1527
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1641
-1527
lines changed

‎doc/src/sgml/catalogs.sgml

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--
22
Documentation of the system catalogs, directed toward PostgreSQL developers
3-
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.59 2002/09/03 01:04:40 tgl Exp $
3+
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.60 2002/09/18 21:35:20 tgl Exp $
44
-->
55

66
<chapter id="catalogs">
@@ -841,9 +841,8 @@
841841
<title>pg_cast</title>
842842

843843
<para>
844-
<structname>pg_cast</structname> stores data type conversion paths
845-
defined with <command>CREATE CAST</command> plus the built-in
846-
conversions.
844+
<structname>pg_cast</structname> stores data type conversion paths,
845+
both built-in paths and those defined with <command>CREATE CAST</command>.
847846
</para>
848847

849848
<table>
@@ -879,17 +878,25 @@
879878
<entry><type>oid</type></entry>
880879
<entry>pg_proc.oid</entry>
881880
<entry>
882-
The OID of the function to use to perform this cast.A 0 is
883-
stored if the data types are binarycompatible (that is, no
884-
function is needed to perform the cast).
881+
The OID of the function to use to perform this cast.Zero is
882+
stored if the data types are binarycoercible (that is, no
883+
run-time operation is needed to perform the cast).
885884
</entry>
886885
</row>
887886

888887
<row>
889-
<entry>castimplicit</entry>
890-
<entry><type>bool</type></entry>
888+
<entry>castcontext</entry>
889+
<entry><type>char</type></entry>
891890
<entry></entry>
892-
<entry>Indication whether this cast can be invoked implicitly</entry>
891+
<entry>
892+
Indicates what contexts the cast may be invoked in.
893+
<literal>e</> means only as an explicit cast (using
894+
<literal>CAST</>, <literal>::</>, or function-call syntax).
895+
<literal>a</> means implicitly in assignment
896+
to a target column, as well as explicitly.
897+
<literal>i</> means implicitly in expressions, as well as the
898+
other cases.
899+
</entry>
893900
</row>
894901
</tbody>
895902
</tgroup>

‎doc/src/sgml/datatype.sgml

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.102 2002/08/23 02:54:18 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.103 2002/09/18 21:35:20 tgl Exp $
33
-->
44

55
<chapter id="datatype">
@@ -823,8 +823,19 @@ CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
823823

824824
<note>
825825
<para>
826-
Prior to <productname>PostgreSQL</> 7.2, strings that were too long were silently
827-
truncated, no error was raised.
826+
If one explicitly casts a value to
827+
<type>character(<replaceable>n</>)</type> or <type>character
828+
varying(<replaceable>n</>)</type>, then an overlength value will
829+
be truncated to <replaceable>n</> characters without raising an
830+
error. (This too is required by the SQL standard.)
831+
</para>
832+
</note>
833+
834+
<note>
835+
<para>
836+
Prior to <productname>PostgreSQL</> 7.2, strings that were too long were
837+
always truncated without raising an error, in either explicit or
838+
implicit casting contexts.
828839
</para>
829840
</note>
830841

@@ -897,12 +908,14 @@ INSERT INTO test2 VALUES ('ok');
897908
INSERT INTO test2 VALUES ('good ');
898909
INSERT INTO test2 VALUES ('too long');
899910
<computeroutput>ERROR: value too long for type character varying(5)</computeroutput>
911+
INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
900912
SELECT b, char_length(b) FROM test2;
901913
<computeroutput>
902914
b | char_length
903915
-------+-------------
904916
ok | 2
905917
good | 5
918+
too l | 5
906919
</computeroutput>
907920
</programlisting>
908921
<calloutlist>
@@ -932,7 +945,7 @@ SELECT b, char_length(b) FROM test2;
932945
</para>
933946

934947
<table tocentry="1">
935-
<title>Specialty CharacterType</title>
948+
<title>Specialty CharacterTypes</title>
936949
<tgroup cols="3">
937950
<thead>
938951
<row>
@@ -2832,29 +2845,39 @@ SELECT * FROM test1 WHERE a;
28322845
<para>
28332846
Bit strings are strings of 1's and 0's. They can be used to store
28342847
or visualize bit masks. There are two SQL bit types:
2835-
<type>BIT(<replaceable>x</replaceable>)</type> and <type>BIT
2836-
VARYING(<replaceable>x</replaceable>)</type>; where
2837-
<replaceable>x</replaceable> is a positive integer.
2848+
<type>BIT(<replaceable>n</replaceable>)</type> and <type>BIT
2849+
VARYING(<replaceable>n</replaceable>)</type>, where
2850+
<replaceable>n</replaceable> is a positive integer.
28382851
</para>
28392852

28402853
<para>
28412854
<type>BIT</type> type data must match the length
2842-
<replaceable>x</replaceable> exactly; it is an error to attempt to
2843-
store shorter or longer bit strings. <type>BIT VARYING</type> is
2855+
<replaceable>n</replaceable> exactly; it is an error to attempt to
2856+
store shorter or longer bit strings. <type>BIT VARYING</type>datais
28442857
of variable length up to the maximum length
2845-
<replaceable>x</replaceable>; longer strings will be rejected.
2846-
<type>BIT</type> without length is equivalent to
2847-
<literal>BIT(1)</literal>, <type>BIT VARYING</type> without length
2858+
<replaceable>n</replaceable>; longer strings will be rejected.
2859+
Writing<type>BIT</type> without a length is equivalent to
2860+
<literal>BIT(1)</literal>,while<type>BIT VARYING</type> without a length
28482861
specification means unlimited length.
28492862
</para>
28502863

28512864
<note>
28522865
<para>
2853-
Prior to <productname>PostgreSQL</> 7.2, <type>BIT</type> type data was
2854-
zero-padded on the right. This was changed to comply with the
2855-
SQL standard. To implement zero-padded bit strings, a
2856-
combination of the concatenation operator and the
2857-
<function>substring</function> function can be used.
2866+
If one explicitly casts a bit-string value to
2867+
<type>BIT(<replaceable>n</>)</type>, it will be truncated or
2868+
zero-padded on the right to be exactly <replaceable>n</> bits,
2869+
without raising an error. Similarly,
2870+
if one explicitly casts a bit-string value to
2871+
<type>BIT VARYING(<replaceable>n</>)</type>, it will be truncated
2872+
on the right if it is more than <replaceable>n</> bits.
2873+
</para>
2874+
</note>
2875+
2876+
<note>
2877+
<para>
2878+
Prior to <productname>PostgreSQL</> 7.2, <type>BIT</type> data was
2879+
always silently truncated or zero-padded on the right, with or without an
2880+
explicit cast. This was changed to comply with the SQL standard.
28582881
</para>
28592882
</note>
28602883

@@ -2874,9 +2897,16 @@ CREATE TABLE test (a BIT(3), b BIT VARYING(5));
28742897
INSERT INTO test VALUES (B'101', B'00');
28752898
INSERT INTO test VALUES (B'10', B'101');
28762899
<computeroutput>
2877-
ERROR: bit string length does not match type bit(3)
2900+
ERROR: Bit string length 2 does not match type BIT(3)
2901+
</computeroutput>
2902+
INSERT INTO test VALUES (B'10'::bit(3), B'101');
2903+
SELECT * FROM test;
2904+
<computeroutput>
2905+
a | b
2906+
-----+-----
2907+
101 | 00
2908+
100 | 101
28782909
</computeroutput>
2879-
SELECT SUBSTRING(b FROM 1 FOR 2) FROM test;
28802910
</programlisting>
28812911
</example>
28822912

‎doc/src/sgml/ref/create_cast.sgml

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_cast.sgml,v 1.4 2002/09/15 13:04:16 petere Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_cast.sgml,v 1.5 2002/09/18 21:35:20 tgl Exp $ -->
22

33
<refentry id="SQL-CREATECAST">
44
<refmeta>
@@ -15,11 +15,11 @@
1515
<synopsis>
1616
CREATE CAST (<replaceable>sourcetype</replaceable> AS <replaceable>targettype</replaceable>)
1717
WITH FUNCTION <replaceable>funcname</replaceable> (<replaceable>argtype</replaceable>)
18-
[AS ASSIGNMENT]
18+
[AS ASSIGNMENT | AS IMPLICIT]
1919

2020
CREATE CAST (<replaceable>sourcetype</replaceable> AS <replaceable>targettype</replaceable>)
2121
WITHOUT FUNCTION
22-
[AS ASSIGNMENT]
22+
[AS ASSIGNMENT | AS IMPLICIT]
2323
</synopsis>
2424
</refsynopsisdiv>
2525

@@ -49,20 +49,44 @@ SELECT CAST(42 AS text);
4949
</para>
5050

5151
<para>
52-
A cast can be marked <literal>AS ASSIGNMENT</>, which means that it
53-
can be invoked implicitly in any context where the conversion it
54-
defines is required. Cast functions not so marked can be invoked
55-
only by explicit <literal>CAST</>,
52+
By default, a cast can be invoked only by an explicit cast request,
53+
that is an explicit <literal>CAST(<replaceable>x</> AS
54+
<replaceable>typename</>)</literal>,
5655
<replaceable>x</><literal>::</><replaceable>typename</>, or
57-
<replaceable>typename</>(<replaceable>x</>) constructs. For
58-
example, supposing that <literal>foo.f1</literal> is a column of
56+
<replaceable>typename</>(<replaceable>x</>) construct.
57+
</para>
58+
59+
<para>
60+
If the cast is marked <literal>AS ASSIGNMENT</> then it can be invoked
61+
implicitly when assigning to a column of the target data type.
62+
For example, supposing that <literal>foo.f1</literal> is a column of
5963
type <type>text</type>, then
6064
<programlisting>
6165
INSERT INTO foo(f1) VALUES(42);
6266
</programlisting>
6367
will be allowed if the cast from type <type>integer</type> to type
6468
<type>text</type> is marked <literal>AS ASSIGNMENT</>, otherwise
65-
not. (We generally use the term <firstterm>implicit
69+
not.
70+
(We generally use the term <firstterm>assignment
71+
cast</firstterm> to describe this kind of cast.)
72+
</para>
73+
74+
<para>
75+
If the cast is marked <literal>AS IMPLICIT</> then it can be invoked
76+
implicitly in any context, whether assignment or internally in an
77+
expression. For example, since <literal>||</> takes <type>text</>
78+
arguments,
79+
<programlisting>
80+
SELECT 'The time is ' || now();
81+
</programlisting>
82+
will be allowed only if the cast from type <type>timestamp</> to
83+
<type>text</type> is marked <literal>AS IMPLICIT</>. Otherwise it
84+
will be necessary to write one of
85+
<programlisting>
86+
SELECT 'The time is ' || CAST(now() AS text);
87+
SELECT 'The time is ' || now()::text;
88+
</programlisting>
89+
(We generally use the term <firstterm>implicit
6690
cast</firstterm> to describe this kind of cast.)
6791
</para>
6892

@@ -74,10 +98,11 @@ INSERT INTO foo(f1) VALUES(42);
7498
all because there are multiple possible interpretations. A good
7599
rule of thumb is to make a cast implicitly invokable only for
76100
information-preserving transformations between types in the same
77-
general type category. For example, <type>int2</type> to
78-
<type>int4</type> casts can reasonably be implicit, but be wary of
79-
marking <type>int4</type> to <type>text</type> or
80-
<type>float8</type> to <type>int4</type> as implicit casts.
101+
general type category. For example, the cast from <type>int2</type> to
102+
<type>int4</type> can reasonably be implicit, but the cast from
103+
<type>float8</type> to <type>int4</type> should probably be
104+
assignment-only. Cross-type-category casts, such as <type>text</>
105+
to <type>int4</>, are best made explicit-only.
81106
</para>
82107

83108
<para>
@@ -138,7 +163,18 @@ INSERT INTO foo(f1) VALUES(42);
138163

139164
<listitem>
140165
<para>
141-
Indicates that the cast may be invoked implicitly.
166+
Indicates that the cast may be invoked implicitly in assignment
167+
contexts.
168+
</para>
169+
</listitem>
170+
</varlistentry>
171+
172+
<varlistentry>
173+
<term><literal>AS IMPLICIT</literal></term>
174+
175+
<listitem>
176+
<para>
177+
Indicates that the cast may be invoked implicitly in any context.
142178
</para>
143179
</listitem>
144180
</varlistentry>
@@ -163,10 +199,10 @@ INSERT INTO foo(f1) VALUES(42);
163199
data type, returned that data type, and took one argument of a
164200
different type was automatically a cast function. This convention has
165201
been abandoned in face of the introduction of schemas and to be
166-
able to represent binary compatible casts in the catalogs. The built-in
202+
able to represent binary compatible casts in the catalogs.(The built-in
167203
cast functions
168-
still follow this naming scheme, but they have to bedeclared as
169-
castsexplicitlynow.
204+
still follow this naming scheme, but they have to beshown as
205+
castsin <literal>pg_cast</>now.)
170206
</para>
171207
</refsect1>
172208

@@ -191,7 +227,8 @@ CREATE CAST (text AS int4) WITH FUNCTION int4(text);
191227
<para>
192228
The <command>CREATE CAST</command> command conforms to SQL99,
193229
except that SQL99 does not make provisions for binary compatible
194-
types.
230+
types. <literal>AS IMPLICIT</> is a <productname>PostgreSQL</productname>
231+
extension, too.
195232
</para>
196233
</refsect1>
197234

‎doc/src/sgml/release.sgml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.158 2002/09/04 07:16:32 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/release.sgml,v 1.159 2002/09/18 21:35:20 tgl Exp $
33
-->
44

55
<appendix id="release">
@@ -24,6 +24,8 @@ CDATA means the content is "SGML-free", so you can write without
2424
worries about funny characters.
2525
-->
2626
<literallayout><![CDATA[
27+
Mixed numeric-and-float expressions are evaluated as float, per SQL spec
28+
Explicit casts to char, varchar, bit, varbit will truncate or pad without error
2729
CREATE OR REPLACE VIEW, CREATE OR REPLACE RULE are available
2830
No-autocommit mode is available (set autocommit to off)
2931
Substantial improvements in functionality for functions returning sets

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp