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

adding query-diff cli command #6514

Merged
merged 36 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2127172
starting on reviving console command
Aug 10, 2023
b96fc55
Merge branch 'main' into james/diff
Aug 10, 2023
7236a20
this does something
Aug 10, 2023
55d2c27
working, definitely not jank
Aug 14, 2023
c69e5e7
simple same table diff working
Aug 14, 2023
16a0e82
implementation for different tables
Aug 14, 2023
b7d900e
remove old code
Aug 14, 2023
ef05baa
adding tests and TODO
Aug 16, 2023
026c496
[ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/upda…
jcor11599 Aug 16, 2023
0c3a15a
working as a table function
Aug 16, 2023
0cc116d
Merge branch 'james/view-diff' into james/diff
Aug 16, 2023
9e7b0a4
fix space
Aug 16, 2023
456a309
Merge branch 'main' into james/diff
Aug 17, 2023
fcb820f
better schema
Aug 17, 2023
c0a2189
handle different schemas
Aug 17, 2023
9ebf94c
asdf
Aug 17, 2023
9642f77
bump
Aug 17, 2023
54c3d53
merge with main
Aug 18, 2023
c1902a6
bump
Aug 18, 2023
2245fd9
merge with main
Aug 18, 2023
fceaa89
bump
Aug 18, 2023
6ddcd3d
point CLI to table function
Aug 18, 2023
df33517
cleaning up code
Aug 19, 2023
654d7b2
[ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/upda…
jcor11599 Aug 19, 2023
5b6a23c
queries
Aug 20, 2023
922e0f0
Merge branch 'james/diff' of /~https://github.com/dolthub/dolt into jam…
Aug 20, 2023
98168a7
adding enginetests
Aug 20, 2023
79e002d
more test
Aug 20, 2023
050d397
bats tests
Aug 21, 2023
eb3ed28
[ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/upda…
jcor11599 Aug 21, 2023
ff07039
merge with main
Aug 21, 2023
d846c8b
bump
Aug 21, 2023
9226313
merge and rebump
Aug 22, 2023
2b1c2af
merge
Aug 22, 2023
908ab51
bump again
Aug 23, 2023
0cfe6ef
mergem ain
max-hoffman Aug 23, 2023
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
141 changes: 141 additions & 0 deletions go/cmd/dolt/commands/query_diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2023 Dolthub, Inc.
//
// 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 commands

import (
"context"
"fmt"
"io"

"github.com/dolthub/go-mysql-server/sql"
gmstypes "github.com/dolthub/go-mysql-server/sql/types"
"github.com/gocraft/dbr/v2"
"github.com/gocraft/dbr/v2/dialect"

"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/doltcore/table/untyped/tabular"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/dolt/go/libraries/utils/iohelp"
)

var queryDiffDocs = cli.CommandDocumentationContent{
ShortDesc: "Shows table diff between two queries",
LongDesc: "Shows table diff between two queries",
Synopsis: []string{
`[options] [{{.LessThan}}query1{{.GreaterThan}}] [{{.LessThan}}query2{{.GreaterThan}}...]`,
},
}

type QueryDiff struct{}

var _ cli.Command = QueryDiff{}

func (q QueryDiff) Name() string {
return "query-diff"
}

func (q QueryDiff) Description() string {
return "description"
}

func (q QueryDiff) Docs() *cli.CommandDocumentation {
ap := q.ArgParser()
return cli.NewCommandDocumentation(queryDiffDocs, ap)
}

func (q QueryDiff) ArgParser() *argparser.ArgParser {
return argparser.NewArgParserWithVariableArgs(q.Name())
}

func (q QueryDiff) compareRows(pkOrds []int, row1, row2 sql.Row) (int, bool) {
var cmp int
for _, pkOrd := range pkOrds {
pk1, _ := gmstypes.ConvertToString(row1[pkOrd], gmstypes.Text)
pk2, _ := gmstypes.ConvertToString(row2[pkOrd], gmstypes.Text)
if pk1 < pk2 {
cmp = -1
} else if pk1 > pk2 {
cmp = 1
} else {
cmp = 0
}
}
var diff bool
for i := 0; i < len(row1); i++ {
a, _ := gmstypes.ConvertToString(row1[i], gmstypes.Text)
b, _ := gmstypes.ConvertToString(row2[i], gmstypes.Text)
if a != b {
diff = true
break
}
}
return cmp, diff
}

func (q QueryDiff) validateArgs(apr *argparser.ArgParseResults) error {
if apr.NArg() != 2 {
return fmt.Errorf("please provide exactly two queries")
}
return nil
}

func (q QueryDiff) Exec(ctx context.Context, commandStr string, args []string, dEnv *env.DoltEnv, cliCtx cli.CliContext) int {
ap := q.ArgParser()
help, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, queryDiffDocs, ap))
apr := cli.ParseArgsOrDie(ap, args, help)
if err := q.validateArgs(apr); err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}

queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}
if closeFunc != nil {
defer closeFunc()
}

// TODO: prevent create, insert, update, delete, etc. queries
query1, query2 := apr.Arg(0), apr.Arg(1)
query, err := dbr.InterpolateForDialect("select * from dolt_query_diff(?, ?)", []interface{}{query1, query2}, dialect.MySQL)
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}
schema, rowIter, err := queryist.Query(sqlCtx, query)
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
}

cliWR := iohelp.NopWrCloser(cli.OutStream)
wr := tabular.NewFixedWidthTableWriter(schema, cliWR, 100)
defer wr.Close(ctx)

for {
row, rerr := rowIter.Next(sqlCtx)
if rerr == io.EOF {
break
}
if rerr != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(rerr), usage)
}
if werr := wr.WriteSqlRow(ctx, row); werr != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(werr), usage)
}
}

return 0
}
1 change: 1 addition & 0 deletions go/cmd/dolt/dolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ var doltSubCommands = []cli.Command{
stashcmds.StashCommands,
&commands.Assist{},
commands.ProfileCmd{},
commands.QueryDiff{},
}

var commandsWithoutCliCtx = []cli.Command{
Expand Down
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ require (
github.com/cespare/xxhash v1.1.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.16.1-0.20230823165146-a356b9a4bbdb
github.com/dolthub/go-mysql-server v0.16.1-0.20230823191256-1aac38366429
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e h1:kPsT4a47cw1+y/N5SSCkma7FhAPw7KeGmD6c9PBZW9Y=
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.16.1-0.20230823165146-a356b9a4bbdb h1:62W7vbU0d88ydYvEUGRaAhD2e+7bHh6U8rcrlfTExbo=
github.com/dolthub/go-mysql-server v0.16.1-0.20230823165146-a356b9a4bbdb/go.mod h1:G05kv0oaccoUORzpk7B45EpL2fwhO1WETObKslLmra8=
github.com/dolthub/go-mysql-server v0.16.1-0.20230823191256-1aac38366429 h1:un6RYp8UJUXMoi2YBq2vQmTfj3gxFMCyJglIt9D7zGQ=
github.com/dolthub/go-mysql-server v0.16.1-0.20230823191256-1aac38366429/go.mod h1:G05kv0oaccoUORzpk7B45EpL2fwhO1WETObKslLmra8=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488 h1:0HHu0GWJH0N6a6keStrHhUAK5/o9LVfkh44pvsV4514=
github.com/dolthub/ishell v0.0.0-20221214210346-d7db0b066488/go.mod h1:ehexgi1mPxRTk0Mok/pADALuHbvATulTh6gzr7NzZto=
github.com/dolthub/jsonpath v0.0.2-0.20230525180605-8dc13778fd72 h1:NfWmngMi1CYUWU4Ix8wM+USEhjc+mhPlT9JUR/anvbQ=
Expand Down
3 changes: 3 additions & 0 deletions go/libraries/doltcore/sqle/database_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,9 @@ func (p DoltDatabaseProvider) TableFunction(_ *sql.Context, name string) (sql.Ta
case "dolt_schema_diff":
dtf := &SchemaDiffTableFunction{}
return dtf, nil
case "dolt_query_diff":
dtf := &QueryDiffTableFunction{}
return dtf, nil
}

return nil, sql.ErrTableFunctionNotFound.New(name)
Expand Down
Loading