Skip to content

Commit

Permalink
Merge pull request #137655 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-24.1-137584

release-24.1: sql: avoid invalid SQL in `raw_config_sql` in `crdb_internal.zones`
  • Loading branch information
yuzefovich authored Dec 19, 2024
2 parents 0a06cc0 + 45fdf60 commit eaf1601
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions pkg/sql/show_zone_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,34 +157,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 @@ -227,7 +228,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 @@ -292,11 +299,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 All @@ -321,11 +329,12 @@ func generateZoneConfigIntrospectionValues(
if zs == nil {
values[fullConfigSQLCol] = tree.DNull
} else {
sqlStr, err := zoneConfigToSQL(zs, inheritedConfig)
var d tree.Datum
d, err = zoneConfigToSQL(zs, inheritedConfig)
if err != nil {
return err
}
values[fullConfigSQLCol] = tree.NewDString(sqlStr)
values[fullConfigSQLCol] = d
}
return nil
}
Expand Down

0 comments on commit eaf1601

Please sign in to comment.