docs/content/stable/releases/techadvisories/ta-20827.md
| Product | Affected Versions | Related Issues | Fixed In |
|---|---|---|---|
| {{<product "ysql">}} | {{<release "2.20.1.x">}} | {{<issue 20827>}} | {{<release "2.20.2.0, 2.21.1.0">}} |
The impacted releases contain a correctness issue for queries using the SELECT DISTINCT clause. The queries may return incorrect results when the following conditions are true.
The issue does not apply to hash or merge joins.
Upgrade to a release with the fix.
SELECT DISTINCT t2.k FROM t1 JOIN t2 ON t1.k = t2.k; where a distinct index scan is chosen on table t2 (inner relation).t2.k as a constant during the distinct index scan because the condition t2.k = <some terms not related to t2> made it appear constant. However, this assumption is incorrect in batch nested loop joins because a single scan on t2 can return multiple values for t2.k matching different values from t1.k.t2.k in the distinct prefix (the columns used to eliminate duplicates) unless the condition is of the form t2.k = C, where C is a true constant value.foreach $1 in t1.k do; distinct index scan on t2 where t2.k = $1 , it is correct to treat t2.k as a constant and exclude it from the distinct prefix. But for a batch nested loop join with foreach ($1, ..., $1024) in t1.k do; distinct index scan on t2 where t2.k in ($1, ..., $1024), it is incorrect to treat t2.k as a constant because the scan can return multiple values for t2.k.