Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/sql/opt/norm/inline_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ func (c *CustomFuncs) InlineConstVar(f memo.FiltersExpr) memo.FiltersExpr {
replace = func(nd opt.Expr) opt.Expr {
if t, ok := nd.(*memo.VariableExpr); ok {
if e, ok := vals[t.Col]; ok {
// The constant was matched against the variable using Equivalent
// (not Identical) above, so its type may differ from the
// variable's (e.g. a STRING constant for a NAME column).
// Substituting the constant directly would change the result of
// type-sensitive expressions such as pg_typeof, so cast the
// constant to the variable's type when the two are not identical.
colType := c.mem.Metadata().ColumnMeta(t.Col).Type
if !e.DataType().Identical(colType) {
return c.f.ConstructCast(e, colType)
}
return e
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/opt/norm/testdata/rules/inline
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ project
│ └── true [as=column14:14]
└── false

# Regression test for #170544. InlineConstVar must not replace a variable with a
# constant whose type is equivalent but not identical to the variable's type
# without a cast, since that changes the result of type-sensitive expressions
# such as pg_typeof. Here n is NAME and the constant 'hello' is STRING; the
# inlined constant is cast back to NAME so pg_typeof(n) still reports 'name' and
# the row is not incorrectly filtered out.
exec-ddl
CREATE TABLE t_name (n NAME)
----

norm expect=InlineConstVar
SELECT * FROM t_name WHERE n = 'hello' AND pg_typeof(n)::TEXT = 'name'
----
select
├── columns: n:1!null
├── fd: ()-->(1)
├── scan t_name
│ └── columns: n:1
└── filters
└── n:1 = 'hello' [outer=(1), constraints=(/1: [/'hello' - /'hello']; tight), fd=()-->(1)]

# --------------------------------------------------
# InlineProjectConstants
# --------------------------------------------------
Expand Down