Skip to content

Commit

Permalink
single columns tested
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Nov 16, 2020
1 parent 412334c commit 2ad28e6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
65 changes: 64 additions & 1 deletion go/vt/vtgate/engine/distinct.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package engine
import (
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/vtgate/evalengine"
)

// Distinct Primitive is used to uniqueify results
Expand All @@ -29,9 +30,71 @@ type Distinct struct {
Source Primitive
}

type row = []sqltypes.Value

type probeTable struct {
m map[int64][]row
}

func (pt *probeTable) exists(r row) (bool, error) {
hashcode, err := evalengine.NullsafeHashcode(r[0])
if err != nil {
return false, err
}

existingRows, found := pt.m[hashcode]
if !found {
pt.m[hashcode] = []row{r}
return false, nil
}

for _, existingRow := range existingRows {
cmp, err := evalengine.NullsafeCompare(r[0], existingRow[0])
if err != nil {
return false, err
}
if cmp == 0 /*equal*/ {
return true, nil
}
}

pt.m[hashcode] = append(existingRows, r)

return false, nil
}

func newProbetable() *probeTable {
return &probeTable{m: map[int64][]row{}}
}

// Execute implements the Primitive interface
func (d *Distinct) Execute(vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
panic("implement me")
input, err := d.Source.Execute(vcursor, bindVars, wantfields)
if err != nil {
return nil, err
}

result := &sqltypes.Result{
Fields: input.Fields,
InsertID: input.InsertID,
SessionStateChanges: input.SessionStateChanges,
}

pt := newProbetable()

for _, row := range input.Rows {
exists, err := pt.exists(row)
if err != nil {
return nil, err
}
if !exists {
result.Rows = append(result.Rows, row)
}
}

result.RowsAffected = uint64(len(result.Rows))

return result, err
}

// StreamExecute implements the Primitive interface
Expand Down
3 changes: 1 addition & 2 deletions go/vt/vtgate/engine/distinct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDistinct(t *testing.T) {
}, {
testName: "int64 numbers",
inputs: r("myid", "int64", "0", "1", "1", "null", "null"),
expectedResult: r("myid", "int64", "11|m1|n1", "22|m2|n2", "1|a1|b1", "2|a2|b2"),
expectedResult: r("myid", "int64", "0", "1", "null"),
}, {
testName: "varchar columns",
inputs: r("myid", "varchar", "monkey", "horse"),
Expand All @@ -48,7 +48,6 @@ func TestDistinct(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.testName+"-Execute", func(t *testing.T) {

distinct := &Distinct{Source: &fakePrimitive{results: []*sqltypes.Result{tc.inputs}}}

qr, err := distinct.Execute(&noopVCursor{ctx: context.Background()}, nil, true)
Expand Down

0 comments on commit 2ad28e6

Please sign in to comment.