From 7f2d78329e4c44e87939d8788e8e58c1add46ecd Mon Sep 17 00:00:00 2001 From: Rafi Shamim Date: Tue, 17 Sep 2024 18:46:22 +0000 Subject: [PATCH] tree: fix walkStmt for SHOW EXPERIMENTAL_FINGERPRINTS If SHOW EXPERIMENTAL_FINGERPRINTS FROM TABLE appeared in certain kinds of subqueries, the CRDB node could panic while walking the statement due to a missing nil check. No release note since this only affected an experimental feature. Release note: None --- pkg/sql/sem/tree/walk.go | 12 +++++++----- pkg/testutils/sqlutils/pretty.go | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/sql/sem/tree/walk.go b/pkg/sql/sem/tree/walk.go index c7eed182d673..118e7409cc4f 100644 --- a/pkg/sql/sem/tree/walk.go +++ b/pkg/sql/sem/tree/walk.go @@ -1184,12 +1184,14 @@ func (n *ShowFingerprints) copyNode() *ShowFingerprints { // walkStmt is part of the walkableStmt interface. func (n *ShowFingerprints) walkStmt(v Visitor) Statement { ret := n - ts, changed := walkTenantSpec(v, n.TenantSpec) - if changed { - if ret == n { - ret = n.copyNode() + if n.TenantSpec != nil { + ts, changed := walkTenantSpec(v, n.TenantSpec) + if changed { + if ret == n { + ret = n.copyNode() + } + ret.TenantSpec = ts } - ret.TenantSpec = ts } if n.Options.StartTimestamp != nil { e, changed := WalkExpr(v, n.Options.StartTimestamp) diff --git a/pkg/testutils/sqlutils/pretty.go b/pkg/testutils/sqlutils/pretty.go index 3c4e7b716a64..17d5a471cac4 100644 --- a/pkg/testutils/sqlutils/pretty.go +++ b/pkg/testutils/sqlutils/pretty.go @@ -29,6 +29,15 @@ func VerifyStatementPrettyRoundtrip(t *testing.T, sql string) { for i := range stmts { origStmt := stmts[i].AST verifyStatementPrettyRoundTrip(t, sql, origStmt, false /* plpgsql */) + + // Verify that the AST can be walked. + if _, err := tree.SimpleStmtVisit( + origStmt, + func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) { return }, + ); err != nil { + t.Fatalf("cannot walk stmt %s %v", stmts[i].SQL, err) + } + } }