Skip to content

Commit a2da77c

Browse files
committed
Change return type of EXTRACT to numeric
The previous implementation of EXTRACT mapped internally to date_part(), which returned type double precision (since it was implemented long before the numeric type existed). This can lead to imprecise output in some cases, so returning numeric would be preferrable. Changing the return type of an existing function is a bit risky, so instead we do the following: We implement a new set of functions, which are now called "extract", in parallel to the existing date_part functions. They work the same way internally but use numeric instead of float8. The EXTRACT construct is now mapped by the parser to these new extract functions. That way, dumps of views etc. from old versions (which would use date_part) continue to work unchanged, but new uses will map to the new extract functions. Additionally, the reverse compilation of EXTRACT now reproduces the original syntax, using the new mechanism introduced in 40c24bf. The following minor changes of behavior result from the new implementation: - The column name from an isolated EXTRACT call is now "extract" instead of "date_part". - Extract from date now rejects inappropriate field names such as HOUR. It was previously mapped internally to extract from timestamp, so it would silently accept everything appropriate for timestamp. - Return values when extracting fields with possibly fractional values, such as second and epoch, now have the full scale that the value has internally (so, for example, '1.000000' instead of just '1'). Reported-by: Petr Fedorov <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent f5d94e4 commit a2da77c

23 files changed

+1330
-496
lines changed

doc/src/sgml/func.sgml

+7-3
Original file line numberDiff line numberDiff line change
@@ -8872,7 +8872,7 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
88728872
<primary>extract</primary>
88738873
</indexterm>
88748874
<function>extract</function> ( <parameter>field</parameter> <literal>from</literal> <type>timestamp</type> )
8875-
<returnvalue>double precision</returnvalue>
8875+
<returnvalue>numeric</returnvalue>
88768876
</para>
88778877
<para>
88788878
Get timestamp subfield; see <xref linkend="functions-datetime-extract"/>
@@ -8886,7 +8886,7 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
88868886
<row>
88878887
<entry role="func_table_entry"><para role="func_signature">
88888888
<function>extract</function> ( <parameter>field</parameter> <literal>from</literal> <type>interval</type> )
8889-
<returnvalue>double precision</returnvalue>
8889+
<returnvalue>numeric</returnvalue>
88908890
</para>
88918891
<para>
88928892
Get interval subfield; see <xref linkend="functions-datetime-extract"/>
@@ -9401,7 +9401,7 @@ EXTRACT(<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
94019401
well.) <replaceable>field</replaceable> is an identifier or
94029402
string that selects what field to extract from the source value.
94039403
The <function>extract</function> function returns values of type
9404-
<type>double precision</type>.
9404+
<type>numeric</type>.
94059405
The following are valid field names:
94069406

94079407
<!-- alphabetical -->
@@ -9825,6 +9825,10 @@ date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
98259825
be a string value, not a name. The valid field names for
98269826
<function>date_part</function> are the same as for
98279827
<function>extract</function>.
9828+
For historical reasons, the <function>date_part</function> function
9829+
returns values of type <type>double precision</type>. This can result in
9830+
a loss of precision in certain uses. Using <function>extract</function>
9831+
is recommended instead.
98289832
</para>
98299833

98309834
<screen>

src/backend/parser/gram.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -14020,7 +14020,7 @@ func_expr_common_subexpr:
1402014020
{ $$ = makeTypeCast($3, $5, @1); }
1402114021
| EXTRACT '(' extract_list ')'
1402214022
{
14023-
$$ = (Node *) makeFuncCall(SystemFuncName("date_part"),
14023+
$$ = (Node *) makeFuncCall(SystemFuncName("extract"),
1402414024
$3,
1402514025
COERCE_SQL_SYNTAX,
1402614026
@1);

0 commit comments

Comments
 (0)