Skip to content

Commit 6f7f6ee

Browse files
committed
[#28708] YSQL: fix tuple IN condition check logic
Summary: Commit 71f5dcc fixes a bug in YbIsTupleInRange, but it fails to apply the same changes to YbNeedTupleRangeCheck. This leaves a small, hard-to-hit hole in the original bug #22800, leading to tserver crash. Fix that. Also clean up an unused variable from YbIsTupleInRange. Jira: DB-18411 Test Plan: On Almalinux 8: ./yb_build.sh fastdebug --gcc13 daemons initdb \ --java-test 'org.yb.pgsql.TestPgRegressJoin' \ --tp 1 -n 20 --stop-at-failure Close: #28708 Reviewers: mtakahara Reviewed By: mtakahara Subscribers: yql Differential Revision: https://phorge.dev.yugabyte.com/D46994
1 parent dd9c110 commit 6f7f6ee

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/postgres/src/backend/access/yb_access/yb_scan.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,10 +1438,10 @@ YbShouldRecheckEquality(Oid column_typid, Oid value_typid)
14381438

14391439
static bool
14401440
YbNeedTupleRangeCheck(Datum value, TupleDesc bind_desc,
1441-
int key_length, ScanKey keys[])
1441+
int key_length, AttrNumber bind_key_attnums[])
14421442
{
14431443
/* Move past header key. */
1444-
++keys;
1444+
++bind_key_attnums;
14451445
--key_length;
14461446

14471447
Oid tupType = HeapTupleHeaderGetTypeId(DatumGetHeapTupleHeader(value));
@@ -1452,7 +1452,8 @@ YbNeedTupleRangeCheck(Datum value, TupleDesc bind_desc,
14521452
for (int i = 0; i < key_length; i++)
14531453
{
14541454
Oid val_type = ybc_get_atttypid(val_tupdesc, i + 1);
1455-
Oid column_type = ybc_get_atttypid(bind_desc, keys[i]->sk_attno);
1455+
Oid column_type = ybc_get_atttypid(bind_desc,
1456+
bind_key_attnums[i]);
14561457

14571458
if (YbShouldRecheckEquality(column_type, val_type))
14581459
{
@@ -1466,11 +1467,9 @@ YbNeedTupleRangeCheck(Datum value, TupleDesc bind_desc,
14661467

14671468
static bool
14681469
YbIsTupleInRange(Datum value, TupleDesc bind_desc,
1469-
int key_length, ScanKey keys[],
1470-
AttrNumber bind_key_attnums[])
1470+
int key_length, AttrNumber bind_key_attnums[])
14711471
{
14721472
/* Move past header key. */
1473-
++keys;
14741473
++bind_key_attnums;
14751474
--key_length;
14761475

@@ -1496,7 +1495,8 @@ YbIsTupleInRange(Datum value, TupleDesc bind_desc,
14961495
{
14971496
Datum val = datum_values[i];
14981497
Oid val_type = ybc_get_atttypid(val_tupdesc, i + 1);
1499-
Oid column_type = ybc_get_atttypid(bind_desc, bind_key_attnums[i]);
1498+
Oid column_type = ybc_get_atttypid(bind_desc,
1499+
bind_key_attnums[i]);
15001500

15011501
if (!YbShouldRecheckEquality(column_type, val_type))
15021502
continue;
@@ -1840,7 +1840,7 @@ YbBindSearchArray(YbScanDesc ybScan, YbScanPlan scan_plan,
18401840
YbNeedTupleRangeCheck(elem_values[0],
18411841
scan_plan->bind_desc,
18421842
length_of_key,
1843-
&ybScan->keys[i]));
1843+
&scan_plan->bind_key_attnums[i]));
18441844

18451845
bool retain_nulls = YbSearchArrayRetainNulls(key);
18461846
bool bind_to_null = false;
@@ -1883,7 +1883,6 @@ YbBindSearchArray(YbScanDesc ybScan, YbScanPlan scan_plan,
18831883
if (!YbIsTupleInRange(elem_values[j],
18841884
scan_plan->bind_desc,
18851885
length_of_key,
1886-
&ybScan->keys[i],
18871886
&scan_plan->bind_key_attnums[i]))
18881887
continue;
18891888
}

src/postgres/src/test/regress/expected/yb.orig.join_batching.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ explain (costs off) /*+Leading((f1 f2)) YbBatchedNL(f1 f2)*/ select * from float
904904

905905
drop table floattable1;
906906
drop table floattable2;
907-
create table ss1(a bigint, b int, primary key(a asc, b asc));
907+
create table ss1(a bigint, b int, primary key(b asc, a asc));
908908
create table ss2(a int, b bigint, primary key(a asc, b asc));
909909
-- 0x8000000000000001 should not join with 1 and 0x8000000000000002 should not join with 2
910910
insert into ss1 values (1,2), (3,4), (x'8000000000000001'::bigint, 1);
@@ -933,7 +933,7 @@ explain (costs off) /*+Leading((ss2 ss1))*/ select * from ss1, ss2 where ss1.a =
933933
Join Filter: ((ss1.a = ss2.a) AND (ss1.b = ss2.b))
934934
-> Index Scan using ss2_pkey on ss2
935935
-> Index Scan using ss1_pkey on ss1
936-
Index Cond: (ROW(a, b) = ANY (ARRAY[ROW(ss2.a, ss2.b), ROW($1, $4), ROW($2, $5)]))
936+
Index Cond: (ROW(b, a) = ANY (ARRAY[ROW(ss2.b, ss2.a), ROW($1, $4), ROW($2, $5)]))
937937
(5 rows)
938938

939939
/*+Leading((ss2 ss1))*/ select * from ss1, ss2 where ss1.a = ss2.a and ss1.b = ss2.b;
@@ -950,7 +950,7 @@ explain (costs off) /*+Leading((ss2 ss1))*/ select * from ss1, ss2 where ss1.a =
950950
Join Filter: ((ss1.a = ss2.a) AND (ss1.b = ss2.b))
951951
-> Index Scan using ss2_pkey on ss2
952952
-> Index Scan using ss1_pkey on ss1
953-
Index Cond: ((ROW(a, b) = ANY (ARRAY[ROW(ss2.a, ss2.b), ROW($1, $4), ROW($2, $5)])) AND (b < 10))
953+
Index Cond: ((ROW(b, a) = ANY (ARRAY[ROW(ss2.b, ss2.a), ROW($1, $4), ROW($2, $5)])) AND (b < 10))
954954
(5 rows)
955955

956956
/*+Leading((ss2 ss1))*/ select * from ss1, ss2 where ss1.a = ss2.a and ss1.b = ss2.b and ss1.b < 10;

src/postgres/src/test/regress/sql/yb.orig.join_batching.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ explain (costs off) /*+Leading((f1 f2)) YbBatchedNL(f1 f2)*/ select * from float
224224
drop table floattable1;
225225
drop table floattable2;
226226

227-
create table ss1(a bigint, b int, primary key(a asc, b asc));
227+
create table ss1(a bigint, b int, primary key(b asc, a asc));
228228
create table ss2(a int, b bigint, primary key(a asc, b asc));
229229
-- 0x8000000000000001 should not join with 1 and 0x8000000000000002 should not join with 2
230230
insert into ss1 values (1,2), (3,4), (x'8000000000000001'::bigint, 1);

0 commit comments

Comments
 (0)