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

Add OnlineDDL show support #13738

Merged
merged 26 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
119 changes: 119 additions & 0 deletions go/cmd/vtctldclient/command/onlineddl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package command

import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"

"vitess.io/vitess/go/cmd/vtctldclient/cli"
"vitess.io/vitess/go/protoutil"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/vtctl/schematools"

vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)

var (
OnlineDDL = &cobra.Command{
Use: "OnlineDDL <cmd> <keyspace> [args]",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately cobra won't support a positional argument before the subcommand name (or at least i couldn't see an easy/obvious way)

DisableFlagsInUseLine: true,
Args: cobra.MinimumNArgs(2),
}
OnlineDDLShow = &cobra.Command{
Use: "show",
ajm188 marked this conversation as resolved.
Show resolved Hide resolved
DisableFlagsInUseLine: true,
Args: cobra.RangeArgs(0, 1),
RunE: commandOnlineDDLShow,
}
)

var onlineDDLShowArgs = struct {
JSON bool
OrderStr string
Limit uint64
Skip uint64
}{
OrderStr: "ascending",
}

func commandOnlineDDLShow(cmd *cobra.Command, args []string) error {
var order vtctldatapb.QueryOrdering
switch strings.ToLower(onlineDDLShowArgs.OrderStr) {
case "":
order = vtctldatapb.QueryOrdering_NONE
case "asc", "ascending":
order = vtctldatapb.QueryOrdering_ASCENDING
case "desc", "descending":
order = vtctldatapb.QueryOrdering_DESCENDING
default:
return fmt.Errorf("invalid ordering %s (choices are 'asc', 'ascending', 'desc', 'descending')", onlineDDLShowArgs.OrderStr)
}

cli.FinishedParsing(cmd)

req := &vtctldatapb.GetSchemaMigrationsRequest{
Keyspace: cmd.Flags().Arg(0),
Order: order,
Limit: onlineDDLShowArgs.Limit,
Skip: onlineDDLShowArgs.Skip,
}

switch arg := cmd.Flags().Arg(1); arg {
case "", "all":
case "recent":
req.Recent = protoutil.DurationToProto(7 * 24 * time.Hour)
default:
if status, err := schematools.ParseSchemaMigrationStatus(arg); err == nil {
// Argument is a status name.
req.Status = status
} else if schema.IsOnlineDDLUUID(arg) {
req.Uuid = arg
} else {
req.MigrationContext = arg
}
}

resp, err := client.GetSchemaMigrations(commandCtx, req)
if err != nil {
return err
}

switch {
case onlineDDLShowArgs.JSON:
data, err := cli.MarshalJSON(resp)
if err != nil {
return err
}
fmt.Printf("%s\n", data)
default:
// TODO: support tabular/textual format
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to at least best-effort match the format that a mysql query provides, which we have currently lost in the new client

}
return nil
}

func init() {
OnlineDDLShow.Flags().BoolVar(&onlineDDLShowArgs.JSON, "json", false, "TODO")
OnlineDDLShow.Flags().StringVar(&onlineDDLShowArgs.OrderStr, "order", "asc", "TODO")
OnlineDDLShow.Flags().Uint64Var(&onlineDDLShowArgs.Limit, "limit", 0, "TODO")
OnlineDDLShow.Flags().Uint64Var(&onlineDDLShowArgs.Skip, "skip", 0, "TODO")
ajm188 marked this conversation as resolved.
Show resolved Hide resolved

OnlineDDL.AddCommand(OnlineDDLShow)
Root.AddCommand(OnlineDDL)
}
11 changes: 11 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ func (v Value) ToInt() (int, error) {
return strconv.Atoi(v.RawStr())
}

// ToFloat32 first parses the value as MySQL would return it as a float64, then
// downcasts it to a float32. It may overflow if the value exceeds 32 bits.
func (v Value) ToFloat32() (float32, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be a bit clunkier for me to do this inline in grpcvtctldserver, but this is also very much not a safe cast to do in general so i'm happy to move it there and grumble about the clunkiness :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinions. If we keep this here, though, people may start using the function without realizing it loses precision. So perhaps better off moved to vtctld

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, using float32(.AsFloat64()) works just as well for me

f64, err := v.ToFloat64()
if err != nil {
return 0, err
}

return float32(f64), nil
}

// ToFloat64 returns the value as MySQL would return it as a float64.
func (v Value) ToFloat64() (float64, error) {
if !IsNumber(v.typ) {
Expand Down
Loading
Loading