From ebd6969995169a408daa5b2ca872a917d8c1dfc5 Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Tue, 25 Jul 2023 00:59:25 +0000 Subject: [PATCH] sql: fix crdb_internal.encode_key in some contexts Previously, we forgot to set `CatalogBuiltins` for the internal planner which is used by `crdb_internal.encode_key` (which, in turn, is used to power `SHOW RANGE ... FOR ROW`), so evaluating it would lead to an error. For example, the internal planner is used in the backfill. This is now fixed. Release note (bug fix): CockroachDB would previously return an error when using `SHOW RANGE ... FOR ROW ...` in `CREATE TABLE ... AS ...` construct, and this is now fixed. --- pkg/sql/create_as_test.go | 3 --- pkg/sql/delegate/show_range_for_row.go | 8 ++++++++ pkg/sql/planner.go | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/sql/create_as_test.go b/pkg/sql/create_as_test.go index bf509a1c1ab6..1c79d9a85f22 100644 --- a/pkg/sql/create_as_test.go +++ b/pkg/sql/create_as_test.go @@ -198,9 +198,6 @@ func TestCreateAsShow(t *testing.T) { { sql: "SHOW RANGE FROM TABLE show_ranges_tbl FOR ROW (0)", setup: "CREATE TABLE show_ranges_tbl (id int PRIMARY KEY)", - // TODO(sql-foundations): Fix `invalid memory address or nil pointer dereference` error in job. - // See https://github.com/cockroachdb/cockroach/issues/106397. - skip: true, }, { sql: "SHOW SURVIVAL GOAL FROM DATABASE", diff --git a/pkg/sql/delegate/show_range_for_row.go b/pkg/sql/delegate/show_range_for_row.go index 3f91f45319cc..20dc2b981fd0 100644 --- a/pkg/sql/delegate/show_range_for_row.go +++ b/pkg/sql/delegate/show_range_for_row.go @@ -34,6 +34,14 @@ func (d *delegator) delegateShowRangeForRow(n *tree.ShowRangeForRow) (tree.State if idx.Table().IsVirtualTable() { return nil, errors.New("SHOW RANGE FOR ROW may not be called on a virtual table") } + // Use qualifyDataSourceNamesInAST similarly to the Builder so that + // CREATE TABLE AS can source from a delegated expression. + // For example: CREATE TABLE t2 AS SELECT * FROM [SHOW RANGE FROM TABLE t1 FOR ROW (0)]; + if d.qualifyDataSourceNamesInAST { + resName.ExplicitSchema = true + resName.ExplicitCatalog = true + (n.TableOrIndex).Table = resName.ToUnresolvedObjectName().ToTableName() + } span := idx.Span() table := idx.Table() idxSpanStart := hex.EncodeToString(span.Key) diff --git a/pkg/sql/planner.go b/pkg/sql/planner.go index 70ec51a5f9e8..45b85358262f 100644 --- a/pkg/sql/planner.go +++ b/pkg/sql/planner.go @@ -469,6 +469,7 @@ func newInternalPlanner( p.schemaResolver.txn = p.txn p.schemaResolver.authAccessor = p p.evalCatalogBuiltins.Init(execCfg.Codec, p.txn, p.Descriptors()) + p.extendedEvalCtx.CatalogBuiltins = &p.evalCatalogBuiltins return p, func() { // Note that we capture ctx here. This is only valid as long as we create