Skip to content

Commit f2906ea

Browse files
craig[bot]normanchenn
andcommitted
Merge #143308
143308: opt: add rule to eliminate subquery wrapper around UDF calls r=normanchenn a=normanchenn This commit adds a new normalization rule `EliminateUDFCallSubquery` that removes unnecessary subquery wrappers around UDF calls when they appear in single row and single column Values expressions. Informs: #143299 Epic: None Release note: None Co-authored-by: Norman Chen <[email protected]>
2 parents c094337 + 286c980 commit f2906ea

File tree

6 files changed

+474
-285
lines changed

6 files changed

+474
-285
lines changed

pkg/bench/rttanalysis/testdata/benchmark_expectations

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ exp,benchmark
127127
15,Truncate/truncate_2_column_2_rows
128128
0,UDFResolution/select_from_udf
129129
2,UseManyRoles/use_2_roles
130-
0,UseManyRoles/use_50_roles
130+
2,UseManyRoles/use_50_roles
131131
1,VirtualTableQueries/select_crdb_internal.invalid_objects_with_1_fk
132132
1,VirtualTableQueries/select_crdb_internal.tables_with_1_fk
133133
5,VirtualTableQueries/virtual_table_cache_with_point_lookups

pkg/sql/opt/memo/testdata/logprops/tail-calls

Lines changed: 77 additions & 105 deletions
Large diffs are not rendered by default.

pkg/sql/opt/norm/rules/scalar.opt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,21 @@ $input
273273
=>
274274
(Exists $input $existsPrivate)
275275

276-
# EliminateConstValuesSubquery replaces a subquery with a constant value if the
276+
# EliminateConstValueSubquery replaces a subquery with a constant value if the
277277
# subquery's input is a single-row, single-column Values expression with a
278278
# constant value.
279279
[EliminateConstValueSubquery, Normalize]
280280
(Subquery (Values [ (Tuple [ $value:(Const) ]) ]))
281281
=>
282282
$value
283283

284+
# EliminateUDFCallSubquery replaces a subquery with a udf call if the subquery's
285+
# input is a single-row, single-column Values expression with a udf call.
286+
[EliminateUDFCallSubquery, Normalize]
287+
(Subquery (Values [ (Tuple [ $udf:(UDFCall) ]) ]))
288+
=>
289+
$udf
290+
284291
# SimplifyCaseWhenConstValue removes branches known to not match. Any
285292
# branch known to match is used as the ELSE and further WHEN conditions
286293
# are skipped. If all WHEN conditions have been removed, the ELSE

pkg/sql/opt/norm/testdata/rules/routine

Lines changed: 61 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -40,93 +40,74 @@ call
4040
│ │ ├── function: pg_sleep
4141
│ │ │ └── const: 0.1
4242
│ │ └── null
43-
│ └── subquery
43+
│ └── udf: _stmt_raise_2
4444
│ ├── tail-call
45-
│ └── values
46-
│ ├── columns: "_stmt_raise_2":7
47-
│ ├── outer: (1)
48-
│ ├── cardinality: [1 - 1]
49-
│ ├── volatile
50-
│ ├── key: ()
51-
│ ├── fd: ()-->(7)
52-
│ └── tuple
53-
│ └── udf: _stmt_raise_2
54-
│ ├── tail-call
55-
│ ├── args
56-
│ │ └── variable: x:1
57-
│ ├── params: x:4
58-
│ └── body
59-
│ ├── values
60-
│ │ ├── columns: stmt_raise_3:5
61-
│ │ ├── outer: (4)
62-
│ │ ├── cardinality: [1 - 1]
63-
│ │ ├── volatile
64-
│ │ ├── key: ()
65-
│ │ ├── fd: ()-->(5)
66-
│ │ └── tuple
67-
│ │ └── function: crdb_internal.plpgsql_raise
68-
│ │ ├── const: 'NOTICE'
69-
│ │ ├── concat
70-
│ │ │ ├── concat
71-
│ │ │ │ ├── const: 'foo '
72-
│ │ │ │ └── coalesce
73-
│ │ │ │ ├── cast: STRING
74-
│ │ │ │ │ └── variable: x:4
75-
│ │ │ │ └── const: '<NULL>'
76-
│ │ │ └── const: ''
77-
│ │ ├── const: ''
78-
│ │ ├── const: ''
79-
│ │ └── const: '00000'
45+
│ ├── args
46+
│ │ └── variable: x:1
47+
│ ├── params: x:4
48+
│ └── body
49+
│ ├── values
50+
│ │ ├── columns: stmt_raise_3:5
51+
│ │ ├── outer: (4)
52+
│ │ ├── cardinality: [1 - 1]
53+
│ │ ├── volatile
54+
│ │ ├── key: ()
55+
│ │ ├── fd: ()-->(5)
56+
│ │ └── tuple
57+
│ │ └── function: crdb_internal.plpgsql_raise
58+
│ │ ├── const: 'NOTICE'
59+
│ │ ├── concat
60+
│ │ │ ├── concat
61+
│ │ │ │ ├── const: 'foo '
62+
│ │ │ │ └── coalesce
63+
│ │ │ │ ├── cast: STRING
64+
│ │ │ │ │ └── variable: x:4
65+
│ │ │ │ └── const: '<NULL>'
66+
│ │ │ └── const: ''
67+
│ │ ├── const: ''
68+
│ │ ├── const: ''
69+
│ │ └── const: '00000'
70+
│ └── values
71+
│ ├── columns: stmt_if_1:6
72+
│ ├── cardinality: [1 - 1]
73+
│ ├── key: ()
74+
│ ├── fd: ()-->(6)
75+
│ └── tuple
76+
│ └── subquery
77+
│ ├── tail-call
8078
│ └── values
81-
│ ├── columns: stmt_if_1:6
79+
│ ├── columns: "_implicit_return":3
8280
│ ├── cardinality: [1 - 1]
8381
│ ├── key: ()
84-
│ ├── fd: ()-->(6)
82+
│ ├── fd: ()-->(3)
8583
│ └── tuple
86-
│ └── subquery
87-
│ ├── tail-call
88-
│ └── values
89-
│ ├── columns: "_implicit_return":3
90-
│ ├── cardinality: [1 - 1]
91-
│ ├── key: ()
92-
│ ├── fd: ()-->(3)
93-
│ └── tuple
94-
│ └── null
95-
└── subquery
84+
│ └── null
85+
└── udf: _stmt_exec_4
9686
├── tail-call
97-
└── values
98-
├── columns: "_stmt_exec_4":11
99-
├── outer: (1)
100-
├── cardinality: [1 - 1]
101-
├── key: ()
102-
├── fd: ()-->(11)
103-
└── tuple
104-
└── udf: _stmt_exec_4
105-
├── tail-call
106-
├── args
107-
│ └── variable: x:1
108-
├── params: x:8
109-
└── body
110-
├── values
111-
│ ├── columns: x:9
112-
│ ├── outer: (8)
113-
│ ├── cardinality: [1 - 1]
114-
│ ├── key: ()
115-
│ ├── fd: ()-->(9)
116-
│ └── tuple
117-
│ └── variable: x:8
87+
├── args
88+
│ └── variable: x:1
89+
├── params: x:8
90+
└── body
91+
├── values
92+
│ ├── columns: x:9
93+
│ ├── outer: (8)
94+
│ ├── cardinality: [1 - 1]
95+
│ ├── key: ()
96+
│ ├── fd: ()-->(9)
97+
│ └── tuple
98+
│ └── variable: x:8
99+
└── values
100+
├── columns: stmt_if_1:10
101+
├── cardinality: [1 - 1]
102+
├── key: ()
103+
├── fd: ()-->(10)
104+
└── tuple
105+
└── subquery
106+
├── tail-call
118107
└── values
119-
├── columns: stmt_if_1:10
108+
├── columns: "_implicit_return":3
120109
├── cardinality: [1 - 1]
121110
├── key: ()
122-
├── fd: ()-->(10)
111+
├── fd: ()-->(3)
123112
└── tuple
124-
└── subquery
125-
├── tail-call
126-
└── values
127-
├── columns: "_implicit_return":3
128-
├── cardinality: [1 - 1]
129-
├── key: ()
130-
├── fd: ()-->(3)
131-
└── tuple
132-
└── null
113+
└── null

0 commit comments

Comments
 (0)