Skip to content

Commit

Permalink
sql: avoid invalid SQL in raw_config_sql in crdb_internal.zones
Browse files Browse the repository at this point in the history
Previously, we could include an invalid `ALTER ... CONFIGURE ZONE
USING;` stmt in the `raw_config_sql` column of `crdb_internal.zones`.
This was the case when some of the config was inherited and we didn't
want to include it for the corresponding object. The contract of this
virtual tables allows NULLs in this column, so this commit returns NULL
in such cases. Note that I spent non-trivial amount of time trying to
figure out how we could end up in this situation but was unsuccessful.

This invalid stmt was included into the output of SHOW CREATE TABLE
which, among other things, we use when recreating the bundle, so we
should now have easier time recreating multi-region bundles.

Release note: None
  • Loading branch information
yuzefovich committed Dec 17, 2024
1 parent b8209ca commit 647438a
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pkg/sql/show_zone_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,34 +158,35 @@ func getShowZoneConfigRow(
}

// zoneConfigToSQL pretty prints a zone configuration as a SQL string.
func zoneConfigToSQL(zs *tree.ZoneSpecifier, zone *zonepb.ZoneConfig) (string, error) {
func zoneConfigToSQL(zs *tree.ZoneSpecifier, zone *zonepb.ZoneConfig) (tree.Datum, error) {
constraints, err := yamlMarshalFlow(zonepb.ConstraintsList{
Constraints: zone.Constraints,
Inherited: zone.InheritedConstraints})
if err != nil {
return "", err
return tree.DNull, err
}
constraints = strings.TrimSpace(constraints)
voterConstraints, err := yamlMarshalFlow(zonepb.ConstraintsList{
Constraints: zone.VoterConstraints,
Inherited: zone.InheritedVoterConstraints(),
})
if err != nil {
return "", err
return tree.DNull, err
}
voterConstraints = strings.TrimSpace(voterConstraints)
prefs, err := yamlMarshalFlow(zone.LeasePreferences)
if err != nil {
return "", err
return tree.DNull, err
}
prefs = strings.TrimSpace(prefs)

useComma := false
first := true
maybeWriteComma := func(f *tree.FmtCtx) {
if useComma {
if !first {
f.Printf(",\n")
} else {
first = false
}
useComma = true
}

f := tree.NewFmtCtx(tree.FmtParsable)
Expand Down Expand Up @@ -228,7 +229,13 @@ func zoneConfigToSQL(zs *tree.ZoneSpecifier, zone *zonepb.ZoneConfig) (string, e
maybeWriteComma(f)
f.Printf("\tlease_preferences = %s", lexbase.EscapeSQLString(prefs))
}
return f.String(), nil
if first {
// We didn't include any zone config parameters, so rather than
// returning an invalid 'ALTER ... CONFIGURE ZONE USING;' stmt we'll
// return NULL.
return tree.DNull, nil
}
return tree.NewDString(f.String()), nil
}

// generateZoneConfigIntrospectionValues creates a result row
Expand Down Expand Up @@ -293,11 +300,12 @@ func generateZoneConfigIntrospectionValues(
if zs == nil {
values[rawConfigSQLCol] = tree.DNull
} else {
sqlStr, err := zoneConfigToSQL(zs, zone)
var d tree.Datum
d, err = zoneConfigToSQL(zs, zone)
if err != nil {
return err
}
values[rawConfigSQLCol] = tree.NewDString(sqlStr)
values[rawConfigSQLCol] = d
}

// Populate the protobuf column.
Expand Down

0 comments on commit 647438a

Please sign in to comment.