Skip to content

Commit 802d7e6

Browse files
committed
sql: fix placeholder type checking
Previously, an binary expression with two placeholders could be incorrectly typed in a prepared statement. The issue could occur when one placeholder had an `UNKNOWN` type hint and one with a non-ambiguous type hint, e.g., `INT`, `DECIMAL`, etc. The `UNKNOWN` type hint could take precedence over the non-ambiguous type. This could cause incorrect results in some cases. The bug has been fixed by preferring non-ambiguous type hints over ambiguous ones. Fixes #152664 Release note (bug fix): A bug in type-checking placeholders with `UNKNOWN` types has been fixed. It could cause incorrect results in some cases.
1 parent 287999c commit 802d7e6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pkg/sql/logictest/testdata/logic_test/prepare

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,3 +1741,24 @@ statement ok
17411741
EXECUTE p(120)
17421742

17431743
subtest end
1744+
1745+
# Regression test for #152664. Correctly type placeholders with UNKNOWN type
1746+
# hints.
1747+
statement ok
1748+
PREPARE p152664 (BOOL, UNKNOWN, DECIMAL) AS SELECT IF($1, $2, $3) IS NOT NAN
1749+
1750+
query B
1751+
EXECUTE p152664(false, NULL, 4)
1752+
----
1753+
true
1754+
1755+
statement ok
1756+
DEALLOCATE p152664
1757+
1758+
statement ok
1759+
PREPARE p152664 (BOOL, DECIMAL, UNKNOWN) AS SELECT IF($1, $2, $3) IS NOT NAN
1760+
1761+
query B
1762+
EXECUTE p152664(true, 4, NULL)
1763+
----
1764+
true

pkg/sql/sem/tree/type_check.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,9 @@ func typeCheckSameTypedPlaceholders(s typeCheckExprsState, typ *types.T) (*types
29912991
return nil, err
29922992
}
29932993
s.typedExprs[i] = typedExpr
2994-
typ = typedExpr.ResolvedType()
2994+
if t := typedExpr.ResolvedType(); !t.IsAmbiguous() {
2995+
typ = t
2996+
}
29952997
}
29962998
return typ, nil
29972999
}

0 commit comments

Comments
 (0)