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

Commit716ea62

Browse files
committed
Make construct_[md_]array return a valid empty array for zero-size input.
If construct_array() or construct_md_array() were given a dimension ofzero, they'd produce an array that contains no elements but has positivedimension. This violates a general expectation that empty arrays shouldhave ndims = 0; in particular, while arrays like this print as empty,they don't compare equal to other empty arrays.Up to now we've expected callers to avoid making such calls and insteadbe careful to call construct_empty_array() if there would be no elements.But this has always been an easily missed case, and we've repeatedly had tofix callers to do it right. In bug #14826, Erwin Brandstetter pointed outyet another such oversight, in ts_lexize(); and a bit of examination ofother call sites found at least two more with similar issues. So let'sfix the problem centrally and permanently by changing these two functionsto construct a proper zero-D empty array whenever the array would be empty.This renders a few explicit calls of construct_empty_array() redundant,but the only such place I found that really seemed worth changing was inExecEvalArrayExpr().Although this fixes some very old bugs, no back-patch: the problem ispretty minor and the risk of changing behavior seems to outweigh thebenefit in stable branches.Discussion:https://postgr.es/m/20170923125723.1448.39412@wrigleys.postgresql.orgDiscussion:https://postgr.es/m/20570.1506198383@sss.pgh.pa.us
1 parentf2ab389 commit716ea62

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

‎src/backend/executor/execExprInterp.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,14 +2131,6 @@ ExecEvalArrayExpr(ExprState *state, ExprEvalStep *op)
21312131
Datum*dvalues=op->d.arrayexpr.elemvalues;
21322132
bool*dnulls=op->d.arrayexpr.elemnulls;
21332133

2134-
/* Shouldn't happen here, but if length is 0, return empty array */
2135-
if (nelems==0)
2136-
{
2137-
*op->resvalue=
2138-
PointerGetDatum(construct_empty_array(element_type));
2139-
return;
2140-
}
2141-
21422134
/* setup for 1-D array of the given length */
21432135
ndims=1;
21442136
dims[0]=nelems;

‎src/backend/utils/adt/arrayfuncs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,6 +3297,7 @@ array_map(FunctionCallInfo fcinfo, Oid retType, ArrayMapState *amstate)
32973297
*
32983298
* A palloc'd 1-D array object is constructed and returned. Note that
32993299
* elem values will be copied into the object even if pass-by-ref type.
3300+
* Also note the result will be 0-D not 1-D if nelems = 0.
33003301
*
33013302
* NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
33023303
* from the system catalogs, given the elmtype. However, the caller is
@@ -3331,6 +3332,7 @@ construct_array(Datum *elems, int nelems,
33313332
*
33323333
* A palloc'd ndims-D array object is constructed and returned. Note that
33333334
* elem values will be copied into the object even if pass-by-ref type.
3335+
* Also note the result will be 0-D not ndims-D if any dims[i] = 0.
33343336
*
33353337
* NOTE: it would be cleaner to look up the elmlen/elmbval/elmalign info
33363338
* from the system catalogs, given the elmtype. However, the caller is
@@ -3362,12 +3364,12 @@ construct_md_array(Datum *elems,
33623364
errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
33633365
ndims,MAXDIM)));
33643366

3365-
/* fast track for empty array */
3366-
if (ndims==0)
3367-
returnconstruct_empty_array(elmtype);
3368-
33693367
nelems=ArrayGetNItems(ndims,dims);
33703368

3369+
/* if ndims <= 0 or any dims[i] == 0, return empty array */
3370+
if (nelems <=0)
3371+
returnconstruct_empty_array(elmtype);
3372+
33713373
/* compute required space */
33723374
nbytes=0;
33733375
hasnulls= false;

‎src/test/regress/expected/tsearch.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,17 @@ SELECT * from ts_debug('english', '5aew.werc.ewr:8100/?xx');
618618
url_path | URL path | /?xx | {simple} | simple | {/?xx}
619619
(3 rows)
620620

621+
SELECT token, alias,
622+
dictionaries, dictionaries is null as dnull, array_dims(dictionaries) as ddims,
623+
lexemes, lexemes is null as lnull, array_dims(lexemes) as ldims
624+
from ts_debug('english', 'a title');
625+
token | alias | dictionaries | dnull | ddims | lexemes | lnull | ldims
626+
-------+-----------+----------------+-------+-------+---------+-------+-------
627+
a | asciiword | {english_stem} | f | [1:1] | {} | f |
628+
| blank | {} | f | | | t |
629+
title | asciiword | {english_stem} | f | [1:1] | {titl} | f | [1:1]
630+
(3 rows)
631+
621632
-- to_tsquery
622633
SELECT to_tsquery('english', 'qwe & sKies ');
623634
to_tsquery

‎src/test/regress/sql/tsearch.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ SELECT * from ts_debug('english', 'http://www.harewoodsolutions.co.uk/press.aspx
145145
SELECT*from ts_debug('english','http://aew.wer0c.ewr/id?ad=qwe&dw<span>');
146146
SELECT*from ts_debug('english','http://5aew.werc.ewr:8100/?');
147147
SELECT*from ts_debug('english','5aew.werc.ewr:8100/?xx');
148+
SELECT token, alias,
149+
dictionaries, dictionaries isnullas dnull, array_dims(dictionaries)as ddims,
150+
lexemes, lexemes isnullas lnull, array_dims(lexemes)as ldims
151+
from ts_debug('english','a title');
148152

149153
-- to_tsquery
150154

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp