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

Commit06d9afa

Browse files
committed
Fix longstanding error in contrib/intarray's int[] & int[] operator.
The array intersection code would give wrong results if the first entry ofthe correct output array would be "1". (I think only this value could beat risk, since the previous word would always be a lower-bound entry withthat fixed value.)Problem spotted by Julien Rouhaud, initial patch by Guillaume Lelarge,cosmetic improvements by me.
1 parent4767bc8 commit06d9afa

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

‎contrib/intarray/_int_tool.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ inner_int_inter(ArrayType *a, ArrayType *b)
140140
*db,
141141
*dr;
142142
inti,
143-
j;
143+
j,
144+
k;
144145

145146
if (ARRISEMPTY(a)||ARRISEMPTY(b))
146147
returnnew_intArrayType(0);
@@ -152,29 +153,29 @@ inner_int_inter(ArrayType *a, ArrayType *b)
152153
r=new_intArrayType(Min(na,nb));
153154
dr=ARRPTR(r);
154155

155-
i=j=0;
156+
i=j=k=0;
156157
while (i<na&&j<nb)
157158
{
158159
if (da[i]<db[j])
159160
i++;
160161
elseif (da[i]==db[j])
161162
{
162-
if (i+j==0||(i+j>0&&*(dr-1)!=db[j]))
163-
*dr++=db[j];
163+
if (k==0||dr[k-1]!=db[j])
164+
dr[k++]=db[j];
164165
i++;
165166
j++;
166167
}
167168
else
168169
j++;
169170
}
170171

171-
if ((dr-ARRPTR(r))==0)
172+
if (k==0)
172173
{
173174
pfree(r);
174175
returnnew_intArrayType(0);
175176
}
176177
else
177-
returnresize_intArrayType(r,dr-ARRPTR(r));
178+
returnresize_intArrayType(r,k);
178179
}
179180

180181
void

‎contrib/intarray/expected/_int.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ SELECT '{123,623,445}'::int[] & '{1623,623}';
137137
{623}
138138
(1 row)
139139

140+
SELECT '{-1,3,1}'::int[] & '{1,2}';
141+
?column?
142+
----------
143+
{1}
144+
(1 row)
145+
140146
--test query_int
141147
SELECT '1'::query_int;
142148
query_int

‎contrib/intarray/sql/_int.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SELECT '{123,623,445}'::int[] | 623;
2424
SELECT'{123,623,445}'::int[] |1623;
2525
SELECT'{123,623,445}'::int[] |'{1623,623}';
2626
SELECT'{123,623,445}'::int[] &'{1623,623}';
27+
SELECT'{-1,3,1}'::int[] &'{1,2}';
2728

2829

2930
--test query_int

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp