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
12 changes: 6 additions & 6 deletions pkg/sql/opt/exec/execbuilder/testdata/join
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ vectorized: true
# Query similar to the one above, but the filter refers to a rendered
# expression and can't "break through".
query T
EXPLAIN (VERBOSE) SELECT a, b, n, sq FROM (SELECT a, b, a * b / 2 AS div, n, sq FROM pairs, square) WHERE div = sq
EXPLAIN (VERBOSE) SELECT a, b, n, sq FROM (SELECT a, b, a & 1 AS b_and, n, sq FROM pairs, square) WHERE b_and = sq
----
distribution: local
vectorized: true
Expand All @@ -614,13 +614,13 @@ vectorized: true
│ columns: (a, b, n, sq)
└── • filter
│ columns: (div, a, b, n, sq)
│ estimated row count: 990 (missing stats)
│ filter: div = sq
│ columns: (b_and, a, b, n, sq)
│ estimated row count: 9,900 (missing stats)
│ filter: b_and = sq
└── • render
│ columns: (div, a, b, n, sq)
│ render div: (a * b) / 2
│ columns: (b_and, a, b, n, sq)
│ render b_and: a & 1
│ render a: a
│ render b: b
│ render n: n
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/norm/inline_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (c *CustomFuncs) CanInline(scalar opt.ScalarExpr) bool {
opt.EqOp, opt.NeOp, opt.LeOp, opt.LtOp, opt.GeOp, opt.GtOp,
opt.IsOp, opt.IsNotOp, opt.InOp, opt.NotInOp,
opt.VariableOp, opt.ConstOp, opt.NullOp,
opt.PlusOp, opt.MinusOp, opt.MultOp,
opt.PlusOp, opt.MinusOp, opt.MultOp, opt.DivOp, opt.ModOp,
opt.CaseOp, opt.WhenOp, opt.ScalarListOp,
opt.FetchValOp, opt.FetchTextOp, opt.FetchValPathOp, opt.FetchTextPathOp:

Expand Down
76 changes: 42 additions & 34 deletions pkg/sql/opt/norm/testdata/rules/decorrelate
Original file line number Diff line number Diff line change
Expand Up @@ -1127,44 +1127,52 @@ WHERE EXISTS
SELECT * FROM xy INNER JOIN (SELECT u, u/1.1 AS div FROM uv WHERE i=5) ON x=div
)
----
distinct-on
project
├── columns: k:1!null
├── grouping columns: k:1!null
├── key: (1)
└── select
├── columns: k:1!null x:8!null div:16!null
├── fd: (8)==(16), (16)==(8)
├── project
│ ├── columns: div:16!null k:1!null x:8!null
│ ├── inner-join (cross)
│ │ ├── columns: k:1!null i:2!null x:8!null u:12!null
│ │ ├── key: (1,8,12)
│ │ ├── fd: ()-->(2)
│ │ ├── select
│ │ │ ├── columns: k:1!null i:2!null
│ │ │ ├── key: (1)
│ │ │ ├── fd: ()-->(2)
│ │ │ ├── scan a
│ │ │ │ ├── columns: k:1!null i:2
│ │ │ │ ├── key: (1)
│ │ │ │ └── fd: (1)-->(2)
│ │ │ └── filters
│ │ │ └── i:2 = 5 [outer=(2), constraints=(/2: [/5 - /5]; tight), fd=()-->(2)]
│ │ ├── inner-join (cross)
│ │ │ ├── columns: x:8!null u:12!null
│ │ │ ├── key: (8,12)
│ │ │ ├── scan xy
│ │ │ │ ├── columns: x:8!null
│ │ │ │ └── key: (8)
│ │ │ ├── scan uv
│ │ │ │ ├── columns: u:12!null
│ │ │ │ └── key: (12)
│ │ │ └── filters (true)
│ │ └── filters (true)
│ └── projections
│ └── u:12 / 1.1 [as=div:16, outer=(12)]
├── columns: k:1!null i:2!null
├── key: (1)
├── fd: ()-->(2)
├── scan a
│ ├── columns: k:1!null i:2
│ ├── key: (1)
│ └── fd: (1)-->(2)
└── filters
└── x:8 = div:16 [outer=(8,16), constraints=(/8: (/NULL - ]; /16: (/NULL - ]), fd=(8)==(16), (16)==(8)]
├── coalesce [subquery]
│ ├── subquery
│ │ └── project
│ │ ├── columns: column19:19!null
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ ├── fd: ()-->(19)
│ │ ├── limit
│ │ │ ├── columns: x:8!null column17:17!null
│ │ │ ├── cardinality: [0 - 1]
│ │ │ ├── key: ()
│ │ │ ├── fd: ()-->(8,17), (8)==(17), (17)==(8)
│ │ │ ├── inner-join (cross)
│ │ │ │ ├── columns: x:8!null column17:17!null
│ │ │ │ ├── multiplicity: left-rows(zero-or-more), right-rows(zero-or-one)
│ │ │ │ ├── fd: (8)==(17), (17)==(8)
│ │ │ │ ├── limit hint: 1.00
│ │ │ │ ├── scan xy
│ │ │ │ │ ├── columns: x:8!null
│ │ │ │ │ └── key: (8)
│ │ │ │ ├── project
│ │ │ │ │ ├── columns: column17:17!null
│ │ │ │ │ ├── scan uv
│ │ │ │ │ │ ├── columns: u:12!null
│ │ │ │ │ │ └── key: (12)
│ │ │ │ │ └── projections
│ │ │ │ │ └── u:12 / 1.1 [as=column17:17, outer=(12)]
│ │ │ │ └── filters
│ │ │ │ └── x:8 = column17:17 [outer=(8,17), constraints=(/8: (/NULL - ]; /17: (/NULL - ]), fd=(8)==(17), (17)==(8)]
│ │ │ └── 1
│ │ └── projections
│ │ └── true [as=column19:19]
│ └── false
└── i:2 = 5 [outer=(2), constraints=(/2: [/5 - /5]; tight), fd=()-->(2)]

# Don't hoist Project operator in right join case.
norm
Expand Down
29 changes: 8 additions & 21 deletions pkg/sql/opt/norm/testdata/rules/prune_cols
Original file line number Diff line number Diff line change
Expand Up @@ -431,31 +431,18 @@ project
├── key: ()
├── fd: ()-->(2)
└── select
├── columns: i:2 f:7!null
├── columns: k:1!null i:2 a.f:3
├── cardinality: [0 - 1]
├── immutable
├── key: ()
├── fd: ()-->(2,7)
├── project
│ ├── columns: f:7 i:2
│ ├── cardinality: [0 - 1]
│ ├── key: ()
│ ├── fd: ()-->(2,7)
│ ├── select
│ │ ├── columns: k:1!null i:2 a.f:3
│ │ ├── cardinality: [0 - 1]
│ │ ├── key: ()
│ │ ├── fd: ()-->(1-3)
│ │ ├── scan a
│ │ │ ├── columns: k:1!null i:2 a.f:3
│ │ │ ├── key: (1)
│ │ │ └── fd: (1)-->(2,3)
│ │ └── filters
│ │ └── k:1 = 5 [outer=(1), constraints=(/1: [/5 - /5]; tight), fd=()-->(1)]
│ └── projections
│ └── a.f:3 / 2.0 [as=f:7, outer=(3)]
├── fd: ()-->(1-3)
├── scan a
│ ├── columns: k:1!null i:2 a.f:3
│ ├── key: (1)
│ └── fd: (1)-->(2,3)
└── filters
└── f:7 = i:2::FLOAT8 [outer=(2,7), immutable, constraints=(/7: (/NULL - ]), fd=(2)-->(7)]
├── k:1 = 5 [outer=(1), constraints=(/1: [/5 - /5]; tight), fd=()-->(1)]
└── (a.f:3 / 2.0) = i:2::FLOAT8 [outer=(2,3), immutable]

# Detect PruneSelectCols and PushSelectIntoProject dependency cycle.
norm
Expand Down
40 changes: 19 additions & 21 deletions pkg/sql/opt/norm/testdata/rules/reject_nulls
Original file line number Diff line number Diff line change
Expand Up @@ -1444,30 +1444,28 @@ SELECT * FROM
) f(p)
WHERE p > 5
----
select
project
├── columns: p:11!null
├── immutable
├── project
│ ├── columns: "?column?":11!null
├── inner-join (hash)
│ ├── columns: k:1!null i:2!null x:7!null y:8!null
│ ├── immutable
│ ├── inner-join (hash)
│ │ ├── columns: k:1!null i:2!null x:7!null y:8!null
│ │ ├── key: (1,7)
│ │ ├── fd: (1)-->(2), (7)-->(8), (2)==(8), (8)==(2)
│ │ ├── scan a
│ │ │ ├── columns: k:1!null i:2
│ │ │ ├── key: (1)
│ │ │ └── fd: (1)-->(2)
│ │ ├── scan xy
│ │ │ ├── columns: x:7!null y:8
│ │ │ ├── key: (7)
│ │ │ └── fd: (7)-->(8)
│ │ └── filters
│ │ └── i:2 = y:8 [outer=(2,8), constraints=(/2: (/NULL - ]; /8: (/NULL - ]), fd=(2)==(8), (8)==(2)]
│ └── projections
│ └── k:1 * (3 - (4 / x:7)) [as="?column?":11, outer=(1,7), immutable]
└── filters
└── "?column?":11 > 5 [outer=(11), immutable, constraints=(/11: (/5 - ]; tight)]
│ ├── key: (1,7)
│ ├── fd: (1)-->(2), (7)-->(8), (2)==(8), (8)==(2)
│ ├── scan a
│ │ ├── columns: k:1!null i:2
│ │ ├── key: (1)
│ │ └── fd: (1)-->(2)
│ ├── scan xy
│ │ ├── columns: x:7!null y:8
│ │ ├── key: (7)
│ │ └── fd: (7)-->(8)
│ └── filters
│ ├── i:2 = y:8 [outer=(2,8), constraints=(/2: (/NULL - ]; /8: (/NULL - ]), fd=(2)==(8), (8)==(2)]
│ └── (k:1 * (3 - (4 / x:7))) > 5 [outer=(1,7), immutable]
└── projections
└── k:1 * (3 - (4 / x:7)) [as="?column?":11, outer=(1,7), immutable]


# Case with one projection that transmits NULLs, and one that doesn't.
norm expect=RejectNullsProject
Expand Down
28 changes: 16 additions & 12 deletions pkg/sql/opt/norm/testdata/rules/select
Original file line number Diff line number Diff line change
Expand Up @@ -691,33 +691,35 @@ project

# Don't push down select if it depends on computed column that can't be inlined.
norm expect-not=PushSelectIntoProject
SELECT * FROM (SELECT i, i/2 div, f FROM a) a WHERE div=2
SELECT * FROM (SELECT i, i&1 b_and, f FROM a) a WHERE b_and=0
----
select
├── columns: i:2 div:8!null f:3
├── columns: i:2 b_and:8!null f:3
├── immutable
├── fd: ()-->(8)
├── project
│ ├── columns: div:8 i:2 f:3
│ ├── columns: b_and:8 i:2 f:3
│ ├── immutable
│ ├── fd: (2)-->(8)
│ ├── scan a
│ │ └── columns: i:2 f:3
│ └── projections
│ └── i:2 / 2 [as=div:8, outer=(2)]
│ └── i:2 & 1 [as=b_and:8, outer=(2), immutable]
└── filters
└── div:8 = 2 [outer=(8), immutable, constraints=(/8: [/2 - /2]; tight), fd=()-->(8)]
└── b_and:8 = 0 [outer=(8), constraints=(/8: [/0 - /0]; tight), fd=()-->(8)]

# Push down some conjuncts, but not others.
norm expect=PushSelectIntoProject
SELECT * FROM (SELECT i, i/2 div, f FROM a) a WHERE 10.0=f AND 2=div AND i=1
SELECT * FROM (SELECT i, i/2 div, i&1 b_and, f FROM a) a WHERE 10.0=f AND 2=div AND 1=b_and AND i=1
----
select
├── columns: i:2!null div:8!null f:3!null
├── columns: i:2!null div:8!null b_and:9!null f:3!null
├── immutable
├── fd: ()-->(2,3,8)
├── fd: ()-->(2,3,8,9)
├── project
│ ├── columns: div:8!null i:2!null f:3!null
│ ├── fd: ()-->(2,3,8)
│ ├── columns: div:8!null b_and:9!null i:2!null f:3!null
│ ├── immutable
│ ├── fd: ()-->(2,3,8,9)
│ ├── select
│ │ ├── columns: i:2!null f:3!null
│ │ ├── fd: ()-->(2,3)
Expand All @@ -727,9 +729,11 @@ select
│ │ ├── f:3 = 10.0 [outer=(3), constraints=(/3: [/10.0 - /10.0]; tight), fd=()-->(3)]
│ │ └── i:2 = 1 [outer=(2), constraints=(/2: [/1 - /1]; tight), fd=()-->(2)]
│ └── projections
│ └── i:2 / 2 [as=div:8, outer=(2)]
│ ├── i:2 / 2 [as=div:8, outer=(2)]
│ └── i:2 & 1 [as=b_and:9, outer=(2), immutable]
└── filters
└── div:8 = 2 [outer=(8), immutable, constraints=(/8: [/2 - /2]; tight), fd=()-->(8)]
├── div:8 = 2 [outer=(8), immutable, constraints=(/8: [/2 - /2]; tight), fd=()-->(8)]
└── b_and:9 = 1 [outer=(9), constraints=(/9: [/1 - /1]; tight), fd=()-->(9)]

# Detect PushSelectIntoProject and FilterUnusedSelectCols dependency cycle.
norm
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/opt/optbuilder/partial_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ func (b *Builder) buildPartialIndexPredicate(
filters,
)

for {
s, ok := selExpr.(*memo.ProjectExpr)
if !ok {
break
}
selExpr = s.Input
}

switch t := selExpr.(type) {
case *memo.SelectExpr:
// If the expression remains a Select, return the normalized filters.
Expand Down
Loading
Loading