mirror of
https://github.com/dolthub/dolt.git
synced 2026-04-21 11:29:51 -05:00
Merge pull request #254 from liquidata-inc/bh/diff-where-fixes
Fix diff where clause
This commit is contained in:
@@ -144,3 +144,84 @@ teardown() {
|
||||
[[ "$output" =~ "--- a/employees @" ]] || false
|
||||
[[ "$output" =~ "+++ b/employees @" ]] || false
|
||||
}
|
||||
|
||||
@test "diff with where clause" {
|
||||
dolt table create -s=`batshelper 1pk5col-ints.schema` test
|
||||
dolt table put-row test pk:0 c1:0 c2:0 c3:0 c4:0 c5:0
|
||||
dolt table put-row test pk:1 c1:1 c2:1 c3:1 c4:1 c5:1
|
||||
dolt add test
|
||||
dolt commit -m "table created"
|
||||
dolt table put-row test pk:2 c1:22 c2:0 c3:0 c4:0 c5:0
|
||||
dolt table put-row test pk:3 c1:33 c2:0 c3:0 c4:0 c5:0
|
||||
run dolt diff --where "pk=2"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "22" ]] || false
|
||||
! [[ "$output" =~ "33" ]] || false
|
||||
|
||||
dolt add test
|
||||
dolt commit -m "added two rows"
|
||||
|
||||
dolt checkout -b test1
|
||||
dolt table put-row test pk:4 c1:44 c2:0 c3:0 c4:0 c5:0
|
||||
dolt add .
|
||||
dolt commit -m "committed to branch test1"
|
||||
|
||||
dolt checkout master
|
||||
dolt checkout -b test2
|
||||
dolt table put-row test pk:5 c1:55 c2:0 c3:0 c4:0 c5:0
|
||||
dolt add .
|
||||
dolt commit -m "committed to branch test2"
|
||||
|
||||
dolt checkout master
|
||||
run dolt diff test1 test2
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "44" ]] || false
|
||||
[[ "$output" =~ "55" ]] || false
|
||||
|
||||
run dolt diff test1 test2 --where "pk=4"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "44" ]] || false
|
||||
! [[ "$output" =~ "55" ]] || false
|
||||
|
||||
run dolt diff test1 test2 --where "to_pk=4"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "44" ]] || false
|
||||
! [[ "$output" =~ "55" ]] || false
|
||||
|
||||
run dolt diff test1 test2 --where "to_pk=5"
|
||||
[ "$status" -eq 0 ]
|
||||
! [[ "$output" =~ "44" ]] || false
|
||||
! [[ "$output" =~ "55" ]] || false
|
||||
|
||||
run dolt diff test1 test2 --where "from_pk=5"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "55" ]] || false
|
||||
! [[ "$output" =~ "44" ]] || false
|
||||
|
||||
run dolt diff test1 test2 --where "from_pk=4"
|
||||
[ "$status" -eq 0 ]
|
||||
! [[ "$output" =~ "44" ]] || false
|
||||
! [[ "$output" =~ "55" ]] || false
|
||||
}
|
||||
|
||||
@test "diff with where clause errors" {
|
||||
dolt table create -s=`batshelper 1pk5col-ints.schema` test
|
||||
dolt table put-row test pk:0 c1:0 c2:0 c3:0 c4:0 c5:0
|
||||
dolt table put-row test pk:1 c1:1 c2:1 c3:1 c4:1 c5:1
|
||||
dolt add test
|
||||
dolt commit -m "table created"
|
||||
dolt table put-row test pk:2 c1:22 c2:0 c3:0 c4:0 c5:0
|
||||
dolt table put-row test pk:3 c1:33 c2:0 c3:0 c4:0 c5:0
|
||||
|
||||
run dolt diff --where "poop=0"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" =~ "failed to parse where clause" ]] || false
|
||||
|
||||
dolt add test
|
||||
dolt commit -m "added two rows"
|
||||
|
||||
run dolt diff --where "poop=0"
|
||||
skip "Bad where clause not found because the argument parsing logic is only triggered on existance of a diff"
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" =~ "failed to parse where clause" ]] || false
|
||||
}
|
||||
@@ -655,7 +655,7 @@ func buildPipeline(dArgs *diffArgs, joiner *rowconv.Joiner, ds *diff.DiffSplitte
|
||||
where, err := ParseWhere(joiner.GetSchema(), dArgs.where)
|
||||
|
||||
if err != nil {
|
||||
return nil, errhand.BuildDError("error: failed to parse where cause").AddCause(err).SetPrintUsage().Build()
|
||||
return nil, errhand.BuildDError("error: failed to parse where clause").AddCause(err).SetPrintUsage().Build()
|
||||
}
|
||||
|
||||
transforms := pipeline.NewTransformCollection()
|
||||
|
||||
@@ -44,12 +44,30 @@ func ParseWhere(sch schema.Schema, whereClause string) (FilterFn, error) {
|
||||
|
||||
col, ok := sch.GetAllCols().GetByName(key)
|
||||
|
||||
var cols []schema.Column
|
||||
if !ok {
|
||||
return nil, errors.New("where clause is invalid. '" + key + "' is not a known column.")
|
||||
toCol, toOk := sch.GetAllCols().GetByName("to_" + key)
|
||||
fromCol, fromOk := sch.GetAllCols().GetByName("from_" + key)
|
||||
|
||||
if !(toOk && fromOk) {
|
||||
return nil, errors.New("where clause is invalid. '" + key + "' is not a known column.")
|
||||
}
|
||||
|
||||
if fromCol.Kind != toCol.Kind {
|
||||
panic("to col and from col are different types.")
|
||||
}
|
||||
|
||||
cols = []schema.Column{toCol, fromCol}
|
||||
} else {
|
||||
cols = []schema.Column{col}
|
||||
}
|
||||
|
||||
tag := col.Tag
|
||||
convFunc, err := doltcore.GetConvFunc(types.StringKind, col.Kind)
|
||||
var tags []uint64
|
||||
for _, curr := range cols {
|
||||
tags = append(tags, curr.Tag)
|
||||
}
|
||||
|
||||
convFunc, err := doltcore.GetConvFunc(types.StringKind, cols[0].Kind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -60,13 +78,19 @@ func ParseWhere(sch schema.Schema, whereClause string) (FilterFn, error) {
|
||||
}
|
||||
|
||||
return func(r row.Row) bool {
|
||||
rowVal, ok := r.GetColVal(tag)
|
||||
for _, tag := range tags {
|
||||
rowVal, ok := r.GetColVal(tag)
|
||||
|
||||
if !ok {
|
||||
return false
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if val.Equals(rowVal) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return val.Equals(rowVal)
|
||||
return false
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user