Skip to content

Commit 6cd27b8

Browse files
author
Mark Sirek
committed
execbuilder: use lookup join lookup table distribution in home region checking
This commit updates enforce_home_region checking to build the lookup join lookup table's distribution the same as is done in the coster. This can prevent some lookup joins with a home region from being errored out as not having a home region. Informs: cockroachdb#105942 Release note: None
1 parent c7ecbe2 commit 6cd27b8

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

pkg/ccl/logictestccl/testdata/logic_test/multi_region_remote_access_error

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ CREATE TABLE customers (
246246
name STRING NOT NULL
247247
) LOCALITY REGIONAL BY ROW;
248248

249+
statement ok
250+
INSERT INTO customers VALUES ('69a1c2c2-5b18-459e-94d2-079dc53a4dd0', 'ACME Sprockets')
251+
249252
statement ok
250253
ALTER TABLE customers INJECT STATISTICS '[
251254
{
@@ -277,6 +280,9 @@ CREATE TABLE orders (
277280
FOREIGN KEY (cust_id, crdb_region) REFERENCES customers (id, crdb_region) ON UPDATE CASCADE
278281
) LOCALITY REGIONAL BY ROW;
279282

283+
statement ok
284+
INSERT INTO orders VALUES ('69a1c2c2-5b18-459e-94d2-079dc53a4dd0', '69a1c2c2-5b18-459e-94d2-079dc53a4dd0', 'Super Deluxe Sprocket')
285+
280286
statement ok
281287
ALTER TABLE orders INJECT STATISTICS '[
282288
{
@@ -636,11 +642,33 @@ locality-optimized-search
636642
├── constraint: /11/10: [/'ca-central-1' - /'us-east-1']
637643
└── limit: 1
638644

639-
# Locality optimized search with lookup join will be supported in phase 2 or 3
640-
# when we can dynamically determine if the lookup will access a remote region.
641-
retry
645+
# Static checking should not error this query out, because we now generate a
646+
# plan with a home region for it.
647+
query T retry
648+
EXPLAIN (OPT) SELECT * FROM customers c JOIN orders o ON c.id = o.cust_id AND
649+
(c.crdb_region = o.crdb_region) WHERE c.id = '69a1c2c2-5b18-459e-94d2-079dc53a4dd0'
650+
----
651+
project
652+
└── inner-join (lookup orders [as=o])
653+
├── lookup columns are key
654+
├── inner-join (lookup orders@orders_cust_id_idx [as=o])
655+
│ ├── locality-optimized-search
656+
│ │ ├── scan customers [as=c]
657+
│ │ │ └── constraint: /15/13: [/'ap-southeast-2'/'69a1c2c2-5b18-459e-94d2-079dc53a4dd0' - /'ap-southeast-2'/'69a1c2c2-5b18-459e-94d2-079dc53a4dd0']
658+
│ │ └── scan customers [as=c]
659+
│ │ └── constraint: /20/18
660+
│ │ ├── [/'ca-central-1'/'69a1c2c2-5b18-459e-94d2-079dc53a4dd0' - /'ca-central-1'/'69a1c2c2-5b18-459e-94d2-079dc53a4dd0']
661+
│ │ └── [/'us-east-1'/'69a1c2c2-5b18-459e-94d2-079dc53a4dd0' - /'us-east-1'/'69a1c2c2-5b18-459e-94d2-079dc53a4dd0']
662+
│ └── filters
663+
│ └── cust_id = '69a1c2c2-5b18-459e-94d2-079dc53a4dd0'
664+
└── filters (true)
665+
666+
# This query should error out dynamically during execution, but
667+
# `GetLookupJoinLookupTableDistribution` doesn't currently work in
668+
# the execbuilder phase because `Optimizer.stateMap` is empty.
669+
# TODO(msirek): Fix this test case to not error out statically.
642670
statement error pq: Query has no home region\. Try adding a filter on o\.crdb_region and/or on key column \(o\.cust_id\)\. For more information, see https://www.cockroachlabs.com/docs/stable/cost-based-optimizer.html#control-whether-queries-are-limited-to-a-single-region
643-
SELECT * FROM customers c JOIN orders o ON c.id = o.cust_id AND
671+
SELECT 'a' FROM customers c JOIN orders o ON c.id = o.cust_id AND
644672
(c.crdb_region = o.crdb_region) WHERE c.id = '69a1c2c2-5b18-459e-94d2-079dc53a4dd0'
645673

646674
# Locality optimized lookup join is allowed.

pkg/sql/opt/exec/execbuilder/relational.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,8 @@ func (b *Builder) handleRemoteLookupJoinError(join *memo.LookupJoinExpr) (err er
23112311
if lookupTable.IsGlobalTable() || join.LocalityOptimized || join.ChildOfLocalityOptimizedSearch {
23122312
// HomeRegion() does not automatically fill in the home region of a global
23132313
// table as the gateway region, so let's manually set it here.
2314-
// Locality optimized joins are considered local in phase 1.
2314+
// Locality optimized joins are considered local for static
2315+
// enforce_home_region checks.
23152316
homeRegion = gatewayRegion
23162317
} else {
23172318
homeRegion, _ = lookupTable.HomeRegion()
@@ -2343,7 +2344,19 @@ func (b *Builder) handleRemoteLookupJoinError(join *memo.LookupJoinExpr) (err er
23432344
}
23442345
}
23452346
}
2346-
2347+
// If the specialized methods of setting the home region above don't succeed,
2348+
// see if `GetLookupJoinLookupTableDistribution` can find the lookup table's
2349+
// distribution.
2350+
// TODO(msirek): Check if the specialized methods above are even needed any
2351+
// more.
2352+
if homeRegion == "" && b.optimizer != nil && b.optimizer.Coster() != nil {
2353+
_, physicalDistribution := distribution.BuildLookupJoinLookupTableDistribution(
2354+
b.ctx, b.evalCtx, join, join.RequiredPhysical(), b.optimizer.MaybeGetBestCostRelation,
2355+
)
2356+
if len(physicalDistribution.Regions) == 1 {
2357+
homeRegion = physicalDistribution.Regions[0]
2358+
}
2359+
}
23472360
if homeRegion != "" {
23482361
if foundLocalRegion {
23492362
if queryHasHomeRegion {

0 commit comments

Comments
 (0)