mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-26 10:37:04 -06:00
Add in for filtered table (#1826)
Let's you do an IN clause with the dolt diff table
This commit is contained in:
@@ -100,6 +100,8 @@ func getExpFunc(nbf *types.NomsBinFormat, sch schema.Schema, exp sql.Expression)
|
||||
}
|
||||
|
||||
return newAndFunc(leftFunc, rightFunc), nil
|
||||
case *expression.InTuple:
|
||||
return newComparisonFunc(EqualsOp{}, typedExpr.BinaryExpression, sch)
|
||||
}
|
||||
|
||||
return nil, errNotImplemented.New(exp.Type().String())
|
||||
@@ -273,7 +275,49 @@ func newComparisonFunc(op CompareOp, exp expression.BinaryExpression, sch schema
|
||||
}
|
||||
}, nil
|
||||
} else if compType == VariableInLiteralList {
|
||||
return nil, errUnsupportedComparisonType.New()
|
||||
colName := vars[0].Name()
|
||||
col, ok := sch.GetAllCols().GetByNameCaseInsensitive(colName)
|
||||
|
||||
if !ok {
|
||||
return nil, errUnknownColumn.New(colName)
|
||||
}
|
||||
|
||||
tag := col.Tag
|
||||
|
||||
// Get all the noms values
|
||||
nomsVals := make([]types.Value, len(consts))
|
||||
for i, c := range consts {
|
||||
nomsVal, err := LiteralToNomsValue(col.Kind, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nomsVals[i] = nomsVal
|
||||
}
|
||||
|
||||
compareNomsValues := op.CompareNomsValues
|
||||
compareToNil := op.CompareToNil
|
||||
|
||||
return func(ctx context.Context, vals map[uint64]types.Value) (b bool, err error) {
|
||||
colVal, ok := vals[tag]
|
||||
|
||||
for _, nv := range nomsVals {
|
||||
var lb bool
|
||||
if ok && !types.IsNull(colVal) {
|
||||
lb, err = compareNomsValues(colVal, nv)
|
||||
} else {
|
||||
lb, err = compareToNil(nv)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if lb {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}, nil
|
||||
} else {
|
||||
return nil, errUnsupportedComparisonType.New()
|
||||
}
|
||||
|
||||
@@ -1200,3 +1200,30 @@ SQL
|
||||
[[ "$output" =~ "| FOUND_ROWS() |" ]] || false
|
||||
[[ "$output" =~ "| 2 |" ]] || false
|
||||
}
|
||||
|
||||
@test "sql: dolt diff table correctly works with IN" {
|
||||
dolt sql -q "CREATE TABLE mytable(pk int primary key);"
|
||||
dolt sql -q "INSERT INTO mytable VALUES (1), (2)"
|
||||
dolt commit -am "Commit 1"
|
||||
|
||||
head_commit=$(get_head_commit)
|
||||
|
||||
run dolt sql -q "SELECT COUNT(*) from dolt_diff_mytable where dolt_diff_mytable.to_commit IN ('$head_commit', '00200202')"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "| COUNT(*) |" ]] || false
|
||||
[[ "$output" =~ "| 2 |" ]] || false
|
||||
|
||||
dolt sql -q "INSERT INTO mytable VALUES (3)"
|
||||
dolt commit -am "Commit 2"
|
||||
|
||||
head_commit2=$(get_head_commit)
|
||||
|
||||
run dolt sql -q "SELECT COUNT(*) from dolt_diff_mytable where dolt_diff_mytable.to_commit IN ('$head_commit', '$head_commit2', 'fake-val')"
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "| COUNT(*) |" ]] || false
|
||||
[[ "$output" =~ "| 3 |" ]] || false
|
||||
}
|
||||
|
||||
get_head_commit() {
|
||||
dolt log -n 1 | grep -m 1 commit | cut -c 8-
|
||||
}
|
||||
Reference in New Issue
Block a user