Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: avoid invalid SQL in raw_config_sql in crdb_internal.zones #137584

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 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 All @@ -322,11 +330,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
Loading