From 7c3275fee82ead6dd95e7935adfce0f9c4a3499b Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Mon, 15 Feb 2021 17:09:15 -0800 Subject: [PATCH] opt: add test constraining partial index on virtual column No code changes were necessary in order to generate a constrained scan for a partial index on a virtual column. Release note: None --- pkg/sql/opt/xform/testdata/rules/select | 64 +++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/pkg/sql/opt/xform/testdata/rules/select b/pkg/sql/opt/xform/testdata/rules/select index 4f5a201b65d4..aedac9d7e88e 100644 --- a/pkg/sql/opt/xform/testdata/rules/select +++ b/pkg/sql/opt/xform/testdata/rules/select @@ -125,7 +125,8 @@ CREATE TABLE virtual ( s STRING, lower_s STRING AS (lower(s)) VIRTUAL, INDEX (a) WHERE c IN (10, 20, 30), - INDEX (lower_s) + INDEX (lower_s), + INDEX (c) WHERE c > 100 ) ---- @@ -370,8 +371,10 @@ project │ │ └── lower_s:6 │ │ └── lower(s:5) │ ├── partial index predicates + │ │ ├── secondary: filters + │ │ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable] │ │ └── secondary: filters - │ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable] + │ │ └── b:3 > 90 [outer=(3), constraints=(/3: [/91 - ]; tight)] │ ├── key: (1) │ └── fd: (1)-->(3) └── filters @@ -1714,17 +1717,70 @@ project │ │ └── lower_s:6 │ │ └── lower(s:5) │ ├── partial index predicates + │ │ ├── secondary: filters + │ │ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable] │ │ └── secondary: filters - │ │ └── (b:3 + 10) IN (10, 20, 30) [outer=(3), immutable] + │ │ └── b:3 > 90 [outer=(3), constraints=(/3: [/91 - ]; tight)] │ ├── key: (1) │ └── fd: (1)-->(2,3) └── filters ├── (a:2 > 10) AND (a:2 < 20) [outer=(2), constraints=(/2: [/11 - /19]; tight)] └── b:3 = 10 [outer=(3), constraints=(/3: [/10 - /10]; tight), fd=()-->(3)] +# Constrained partial index scan with an indexed virtual computed column also in +# the predicate. +# TODO(mgartner): Teach indexConstraintCtx.simplifyFilter to remove b=140 from +# the remaining filters after the index constraint is built. This will eliminate +# the unnecessary index-join and select expressions. +opt +SELECT k FROM virtual WHERE c = 150 +---- +project + ├── columns: k:1!null + ├── key: (1) + └── select + ├── columns: k:1!null b:3!null + ├── key: (1) + ├── fd: ()-->(3) + ├── index-join virtual + │ ├── columns: k:1!null b:3 + │ ├── key: (1) + │ ├── fd: (1)-->(3) + │ └── scan virtual@secondary,partial + │ ├── columns: k:1!null + │ ├── constraint: /4/1: [/150 - /150] + │ └── key: (1) + └── filters + └── b:3 = 140 [outer=(3), constraints=(/3: [/140 - /140]; tight), fd=()-->(3)] + +# Constrain a scan on a partial index on virtual column c constraining the +# dependent column b. +# TODO(mgartner): Teach indexConstraintCtx.simplifyFilter to remove b=140 from +# the remaining filters after the index constraint is built. This will eliminate +# the unnecessary index-join and select expressions. +opt +SELECT k FROM virtual WHERE b = 140 +---- +project + ├── columns: k:1!null + ├── key: (1) + └── select + ├── columns: k:1!null b:3!null + ├── key: (1) + ├── fd: ()-->(3) + ├── index-join virtual + │ ├── columns: k:1!null b:3 + │ ├── key: (1) + │ ├── fd: (1)-->(3) + │ └── scan virtual@secondary,partial + │ ├── columns: k:1!null + │ ├── constraint: /4/1: [/150 - /150] + │ └── key: (1) + └── filters + └── b:3 = 140 [outer=(3), constraints=(/3: [/140 - /140]; tight), fd=()-->(3)] + # Regression test for #55387. GenerateConstrainedScans should not reduce the # input filters when proving partial index implication. - exec-ddl CREATE TABLE t55387 ( k INT PRIMARY KEY,