Skip to content

Commit e15b5bc

Browse files
craig[bot]mgartnerDrewKimball
committed
107133: opt: add EliminateConstValueSubquery normalization rule r=mgartner a=mgartner The EliminateConstValueSubquery normalization rule replaces a subquery with a constant value when the subquery's input is a single-rows, single-column Values expression with a constant value. This enables further optimization of the query. Fixes cockroachdb#104218 Release note (performance improvement): The optimizer now produces more efficient query plans in some cases for queries with subqueries and user-defined functions. 107312: server: increase timeout for application_api tests r=DrewKimball a=DrewKimball This patch increase the timeout for the tests in the `application_api` package from `5m` to `15m`. Several of these tests have been flaking lately with timeouts. Fixes cockroachdb#107225 Fixes cockroachdb#107263 Fixes cockroachdb#107308 Release note: None Co-authored-by: Marcus Gartner <[email protected]> Co-authored-by: Drew Kimball <[email protected]>
3 parents e0235d0 + bbe36ee + 8424f93 commit e15b5bc

File tree

10 files changed

+227
-118
lines changed

10 files changed

+227
-118
lines changed

pkg/server/application_api/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99

1010
go_test(
1111
name = "application_api_test",
12+
size = "large",
1213
srcs = [
1314
"activity_test.go",
1415
"config_test.go",
@@ -30,7 +31,7 @@ go_test(
3031
"util_test.go",
3132
"zcfg_test.go",
3233
],
33-
args = ["-test.timeout=295s"],
34+
args = ["-test.timeout=895s"],
3435
deps = [
3536
"//pkg/base",
3637
"//pkg/ccl",

pkg/sql/explain_bundle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
348348
}
349349
return nil
350350
}, false /* expectErrors */, base, plans,
351-
"distsql-1-subquery.html distsql-2-main-query.html vec-1-subquery-v.txt vec-1-subquery.txt vec-2-main-query-v.txt vec-2-main-query.txt")
351+
"distsql.html vec-v.txt vec.txt")
352352
})
353353

354354
t.Run("permission error", func(t *testing.T) {

pkg/sql/logictest/testdata/logic_test/apply_join

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ VALUES
467467
FROM
468468
(VALUES (tab_54747.col_95055)) AS tab_54752 (col_95061)
469469
WHERE
470-
(SELECT 0) < tab_54752.col_95061
470+
(SELECT random()::INT) < tab_54752.col_95061
471471
)
472472
FROM
473473
(VALUES (0:::OID), (3790322641:::OID)) AS tab_54747 (col_95055)

pkg/sql/opt/exec/execbuilder/testdata/subquery

Lines changed: 47 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Uncorrelated subqueries.
55
# ------------------------------------------------------------------------------
66
statement ok
7-
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT);
7+
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT, FAMILY (a, b, c));
88
CREATE TABLE abc2 (a INT PRIMARY KEY, b INT, c INT)
99

1010
query T
@@ -13,22 +13,12 @@ EXPLAIN ALTER TABLE abc SPLIT AT VALUES ((SELECT 42))
1313
distribution: local
1414
vectorized: true
1515
·
16-
• root
17-
18-
├── • split
19-
│ │ index: abc@abc_pkey
20-
│ │ expiry: CAST(NULL AS STRING)
21-
│ │
22-
│ └── • values
23-
│ size: 1 column, 1 row
16+
• split
17+
│ index: abc@abc_pkey
18+
│ expiry: CAST(NULL AS STRING)
2419
25-
└── • subquery
26-
│ id: @S1
27-
│ original sql: (SELECT 42)
28-
│ exec mode: one row
29-
30-
└── • values
31-
size: 1 column, 1 row
20+
└── • values
21+
size: 1 column, 1 row
3222

3323
statement ok
3424
ALTER TABLE abc SPLIT AT VALUES ((SELECT 1))
@@ -39,55 +29,27 @@ EXPLAIN ALTER RANGE RELOCATE FROM 11 TO 22 FOR VALUES ((SELECT 1))
3929
distribution: local
4030
vectorized: true
4131
·
42-
• root
43-
44-
├── • relocate range
45-
│ │ replicas: VOTERS
46-
│ │ to: 22
47-
│ │ from: 11
48-
│ │
49-
│ └── • values
50-
│ size: 1 column, 1 row
32+
• relocate range
33+
│ replicas: VOTERS
34+
│ to: 22
35+
│ from: 11
5136
52-
└── • subquery
53-
│ id: @S1
54-
│ original sql: (SELECT 1)
55-
│ exec mode: one row
56-
57-
└── • values
58-
size: 1 column, 1 row
37+
└── • values
38+
size: 1 column, 1 row
5939

6040
query T
6141
EXPLAIN ALTER RANGE 22 RELOCATE FROM ((SELECT 1)) TO ((SELECT 2))
6242
----
6343
distribution: local
6444
vectorized: true
6545
·
66-
• root
67-
68-
├── • relocate range
69-
│ │ replicas: VOTERS
70-
│ │ to: @S1
71-
│ │ from: @S2
72-
│ │
73-
│ └── • values
74-
│ size: 1 column, 1 row
75-
76-
├── • subquery
77-
│ │ id: @S1
78-
│ │ original sql: ((SELECT 2))
79-
│ │ exec mode: one row
80-
│ │
81-
│ └── • values
82-
│ size: 1 column, 1 row
46+
• relocate range
47+
│ replicas: VOTERS
48+
│ to: 2
49+
│ from: 1
8350
84-
└── • subquery
85-
│ id: @S2
86-
│ original sql: ((SELECT 1))
87-
│ exec mode: one row
88-
89-
└── • values
90-
size: 1 column, 1 row
51+
└── • values
52+
size: 1 column, 1 row
9153

9254
query T
9355
EXPLAIN SELECT EXISTS (SELECT a FROM abc)
@@ -228,7 +190,7 @@ vectorized: true
228190

229191
# the subquery's plan must be visible in EXPLAIN
230192
query T
231-
EXPLAIN VALUES (1), ((SELECT 2))
193+
EXPLAIN VALUES (1), ((SELECT random()::INT))
232194
----
233195
distribution: local
234196
vectorized: true
@@ -240,7 +202,7 @@ vectorized: true
240202
241203
└── • subquery
242204
│ id: @S1
243-
│ original sql: (SELECT 2)
205+
│ original sql: (SELECT random()::INT8)
244206
│ exec mode: one row
245207
246208
└── • values
@@ -291,6 +253,33 @@ vectorized: true
291253
table: tab4@tab4_pkey
292254
spans: FULL SCAN
293255

256+
# Subqueries with single, constant values can be inlined for index-acceleration.
257+
query T
258+
EXPLAIN (VERBOSE)
259+
SELECT * FROM abc WHERE a = (SELECT 1)
260+
----
261+
distribution: local
262+
vectorized: true
263+
·
264+
• scan
265+
columns: (a, b, c)
266+
estimated row count: 1 (missing stats)
267+
table: abc@abc_pkey
268+
spans: /1/0
269+
270+
query T
271+
EXPLAIN (VERBOSE)
272+
SELECT * FROM abc WHERE a >= (SELECT 1)
273+
----
274+
distribution: local
275+
vectorized: true
276+
·
277+
• scan
278+
columns: (a, b, c)
279+
estimated row count: 333 (missing stats)
280+
table: abc@abc_pkey
281+
spans: /1-
282+
294283
# ------------------------------------------------------------------------------
295284
# Correlated subqueries.
296285
# ------------------------------------------------------------------------------

pkg/sql/opt/exec/execbuilder/testdata/udf

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ CREATE TABLE t (
99
INSERT INTO t VALUES (1, 1), (2, 2), (3, 3), (4, 1), (5, 1);
1010

1111
statement ok
12-
CREATE FUNCTION one() RETURNS INT LANGUAGE SQL AS 'SELECT 1';
12+
CREATE FUNCTION one() RETURNS INT IMMUTABLE LANGUAGE SQL AS 'SELECT 1';
1313

1414
query T
1515
EXPLAIN SELECT one()
@@ -27,7 +27,7 @@ distribution: local
2727
vectorized: true
2828
·
2929
• filter
30-
│ filter: a = one()
30+
│ filter: a = 1
3131
3232
└── • scan
3333
missing stats
@@ -259,6 +259,51 @@ vectorized: true
259259
└── • emptyrow
260260
columns: ()
261261

262+
# Immutable UDFs can be inlined for index-acceleration.
263+
query T
264+
EXPLAIN (VERBOSE)
265+
SELECT * FROM t WHERE k = one()
266+
----
267+
distribution: local
268+
vectorized: true
269+
·
270+
• scan
271+
columns: (k, a)
272+
estimated row count: 1 (missing stats)
273+
table: t@t_pkey
274+
spans: /1/0
275+
276+
statement ok
277+
CREATE FUNCTION num(n INT) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$
278+
SELECT n
279+
$$
280+
281+
query T
282+
EXPLAIN (VERBOSE)
283+
SELECT * FROM t WHERE k = num(2)
284+
----
285+
distribution: local
286+
vectorized: true
287+
·
288+
• scan
289+
columns: (k, a)
290+
estimated row count: 1 (missing stats)
291+
table: t@t_pkey
292+
spans: /2/0
293+
294+
query T
295+
EXPLAIN (VERBOSE)
296+
SELECT * FROM t WHERE k >= num(2)
297+
----
298+
distribution: local
299+
vectorized: true
300+
·
301+
• scan
302+
columns: (k, a)
303+
estimated row count: 333 (missing stats)
304+
table: t@t_pkey
305+
spans: /2-
306+
262307

263308
subtest regressions
264309

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ $input
299299
=>
300300
(Exists $input $subqueryPrivate)
301301

302+
# EliminateConstValuesSubquery replaces a subquery with a constant value if the
303+
# subquery's input is a single-row, single-column Values expression with a
304+
# constant value.
305+
[EliminateConstValueSubquery, Normalize]
306+
(Subquery (Values [ (Tuple [ $value:(Const) ]) ]))
307+
=>
308+
$value
309+
302310
# SimplifyCaseWhenConstValue removes branches known to not match. Any
303311
# branch known to match is used as the ELSE and further WHEN conditions
304312
# are skipped. If all WHEN conditions have been removed, the ELSE

pkg/sql/opt/norm/testdata/rules/inline

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,21 +1251,16 @@ norm expect=InlineUDF
12511251
SELECT one_strict() FROM (VALUES (1), (2), (3)) v(i)
12521252
----
12531253
project
1254-
├── columns: one_strict:3
1254+
├── columns: one_strict:3!null
12551255
├── cardinality: [3 - 3]
1256+
├── fd: ()-->(3)
12561257
├── values
12571258
│ ├── cardinality: [3 - 3]
12581259
│ ├── ()
12591260
│ ├── ()
12601261
│ └── ()
12611262
└── projections
1262-
└── subquery [as=one_strict:3, subquery]
1263-
└── values
1264-
├── columns: "?column?":2!null
1265-
├── cardinality: [1 - 1]
1266-
├── key: ()
1267-
├── fd: ()-->(2)
1268-
└── (1,)
1263+
└── 1 [as=one_strict:3]
12691264

12701265
# A UDF is not inlined when the arguments are not constants or either Variable
12711266
# or Const expressions.

0 commit comments

Comments
 (0)