Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 #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 #107225
Fixes #107263
Fixes #107308

Release note: None

Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com>
Co-authored-by: Drew Kimball <drewk@cockroachlabs.com>
  • Loading branch information
3 people committed Jul 21, 2023
3 parents e0235d0 + bbe36ee + 8424f93 commit e15b5bc
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 118 deletions.
3 changes: 2 additions & 1 deletion pkg/server/application_api/BUILD.bazel
Expand Up @@ -9,6 +9,7 @@ go_library(

go_test(
name = "application_api_test",
size = "large",
srcs = [
"activity_test.go",
"config_test.go",
Expand All @@ -30,7 +31,7 @@ go_test(
"util_test.go",
"zcfg_test.go",
],
args = ["-test.timeout=295s"],
args = ["-test.timeout=895s"],
deps = [
"//pkg/base",
"//pkg/ccl",
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/explain_bundle_test.go
Expand Up @@ -348,7 +348,7 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
}
return nil
}, false /* expectErrors */, base, plans,
"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")
"distsql.html vec-v.txt vec.txt")
})

t.Run("permission error", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/apply_join
Expand Up @@ -467,7 +467,7 @@ VALUES
FROM
(VALUES (tab_54747.col_95055)) AS tab_54752 (col_95061)
WHERE
(SELECT 0) < tab_54752.col_95061
(SELECT random()::INT) < tab_54752.col_95061
)
FROM
(VALUES (0:::OID), (3790322641:::OID)) AS tab_54747 (col_95055)
Expand Down
105 changes: 47 additions & 58 deletions pkg/sql/opt/exec/execbuilder/testdata/subquery
Expand Up @@ -4,7 +4,7 @@
# Uncorrelated subqueries.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT);
CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT, FAMILY (a, b, c));
CREATE TABLE abc2 (a INT PRIMARY KEY, b INT, c INT)

query T
Expand All @@ -13,22 +13,12 @@ EXPLAIN ALTER TABLE abc SPLIT AT VALUES ((SELECT 42))
distribution: local
vectorized: true
·
• root
├── • split
│ │ index: abc@abc_pkey
│ │ expiry: CAST(NULL AS STRING)
│ │
│ └── • values
│ size: 1 column, 1 row
• split
│ index: abc@abc_pkey
│ expiry: CAST(NULL AS STRING)
└── • subquery
│ id: @S1
│ original sql: (SELECT 42)
│ exec mode: one row
└── • values
size: 1 column, 1 row
└── • values
size: 1 column, 1 row

statement ok
ALTER TABLE abc SPLIT AT VALUES ((SELECT 1))
Expand All @@ -39,55 +29,27 @@ EXPLAIN ALTER RANGE RELOCATE FROM 11 TO 22 FOR VALUES ((SELECT 1))
distribution: local
vectorized: true
·
• root
├── • relocate range
│ │ replicas: VOTERS
│ │ to: 22
│ │ from: 11
│ │
│ └── • values
│ size: 1 column, 1 row
• relocate range
│ replicas: VOTERS
│ to: 22
│ from: 11
└── • subquery
│ id: @S1
│ original sql: (SELECT 1)
│ exec mode: one row
└── • values
size: 1 column, 1 row
└── • values
size: 1 column, 1 row

query T
EXPLAIN ALTER RANGE 22 RELOCATE FROM ((SELECT 1)) TO ((SELECT 2))
----
distribution: local
vectorized: true
·
• root
├── • relocate range
│ │ replicas: VOTERS
│ │ to: @S1
│ │ from: @S2
│ │
│ └── • values
│ size: 1 column, 1 row
├── • subquery
│ │ id: @S1
│ │ original sql: ((SELECT 2))
│ │ exec mode: one row
│ │
│ └── • values
│ size: 1 column, 1 row
• relocate range
│ replicas: VOTERS
│ to: 2
│ from: 1
└── • subquery
│ id: @S2
│ original sql: ((SELECT 1))
│ exec mode: one row
└── • values
size: 1 column, 1 row
└── • values
size: 1 column, 1 row

query T
EXPLAIN SELECT EXISTS (SELECT a FROM abc)
Expand Down Expand Up @@ -228,7 +190,7 @@ vectorized: true

# the subquery's plan must be visible in EXPLAIN
query T
EXPLAIN VALUES (1), ((SELECT 2))
EXPLAIN VALUES (1), ((SELECT random()::INT))
----
distribution: local
vectorized: true
Expand All @@ -240,7 +202,7 @@ vectorized: true
└── • subquery
│ id: @S1
│ original sql: (SELECT 2)
│ original sql: (SELECT random()::INT8)
│ exec mode: one row
└── • values
Expand Down Expand Up @@ -291,6 +253,33 @@ vectorized: true
table: tab4@tab4_pkey
spans: FULL SCAN

# Subqueries with single, constant values can be inlined for index-acceleration.
query T
EXPLAIN (VERBOSE)
SELECT * FROM abc WHERE a = (SELECT 1)
----
distribution: local
vectorized: true
·
• scan
columns: (a, b, c)
estimated row count: 1 (missing stats)
table: abc@abc_pkey
spans: /1/0

query T
EXPLAIN (VERBOSE)
SELECT * FROM abc WHERE a >= (SELECT 1)
----
distribution: local
vectorized: true
·
• scan
columns: (a, b, c)
estimated row count: 333 (missing stats)
table: abc@abc_pkey
spans: /1-

# ------------------------------------------------------------------------------
# Correlated subqueries.
# ------------------------------------------------------------------------------
Expand Down
49 changes: 47 additions & 2 deletions pkg/sql/opt/exec/execbuilder/testdata/udf
Expand Up @@ -9,7 +9,7 @@ CREATE TABLE t (
INSERT INTO t VALUES (1, 1), (2, 2), (3, 3), (4, 1), (5, 1);

statement ok
CREATE FUNCTION one() RETURNS INT LANGUAGE SQL AS 'SELECT 1';
CREATE FUNCTION one() RETURNS INT IMMUTABLE LANGUAGE SQL AS 'SELECT 1';

query T
EXPLAIN SELECT one()
Expand All @@ -27,7 +27,7 @@ distribution: local
vectorized: true
·
• filter
│ filter: a = one()
│ filter: a = 1
└── • scan
missing stats
Expand Down Expand Up @@ -259,6 +259,51 @@ vectorized: true
└── • emptyrow
columns: ()

# Immutable UDFs can be inlined for index-acceleration.
query T
EXPLAIN (VERBOSE)
SELECT * FROM t WHERE k = one()
----
distribution: local
vectorized: true
·
• scan
columns: (k, a)
estimated row count: 1 (missing stats)
table: t@t_pkey
spans: /1/0

statement ok
CREATE FUNCTION num(n INT) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$
SELECT n
$$

query T
EXPLAIN (VERBOSE)
SELECT * FROM t WHERE k = num(2)
----
distribution: local
vectorized: true
·
• scan
columns: (k, a)
estimated row count: 1 (missing stats)
table: t@t_pkey
spans: /2/0

query T
EXPLAIN (VERBOSE)
SELECT * FROM t WHERE k >= num(2)
----
distribution: local
vectorized: true
·
• scan
columns: (k, a)
estimated row count: 333 (missing stats)
table: t@t_pkey
spans: /2-


subtest regressions

Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/opt/norm/rules/scalar.opt
Expand Up @@ -299,6 +299,14 @@ $input
=>
(Exists $input $subqueryPrivate)

# EliminateConstValuesSubquery replaces a subquery with a constant value if the
# subquery's input is a single-row, single-column Values expression with a
# constant value.
[EliminateConstValueSubquery, Normalize]
(Subquery (Values [ (Tuple [ $value:(Const) ]) ]))
=>
$value

# SimplifyCaseWhenConstValue removes branches known to not match. Any
# branch known to match is used as the ELSE and further WHEN conditions
# are skipped. If all WHEN conditions have been removed, the ELSE
Expand Down
11 changes: 3 additions & 8 deletions pkg/sql/opt/norm/testdata/rules/inline
Expand Up @@ -1251,21 +1251,16 @@ norm expect=InlineUDF
SELECT one_strict() FROM (VALUES (1), (2), (3)) v(i)
----
project
├── columns: one_strict:3
├── columns: one_strict:3!null
├── cardinality: [3 - 3]
├── fd: ()-->(3)
├── values
│ ├── cardinality: [3 - 3]
│ ├── ()
│ ├── ()
│ └── ()
└── projections
└── subquery [as=one_strict:3, subquery]
└── values
├── columns: "?column?":2!null
├── cardinality: [1 - 1]
├── key: ()
├── fd: ()-->(2)
└── (1,)
└── 1 [as=one_strict:3]

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

0 comments on commit e15b5bc

Please sign in to comment.