-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SELECT DISTINCT queries support (#264)
Uses hash set to make projection result unique.
- Loading branch information
Showing
10 changed files
with
417 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package planner | ||
|
||
import ( | ||
"github.com/genjidb/genji/database" | ||
"github.com/genjidb/genji/document" | ||
"github.com/genjidb/genji/sql/query/expr" | ||
) | ||
|
||
type dedupNode struct { | ||
node | ||
|
||
tableName string | ||
indexes map[string]database.Index | ||
} | ||
|
||
func NewDedupNode(n Node, tableName string) Node { | ||
return &dedupNode{ | ||
node: node{ | ||
op: Dedup, | ||
left: n, | ||
}, | ||
tableName: tableName, | ||
} | ||
} | ||
|
||
func (n *dedupNode) Bind(tx *database.Transaction, params []expr.Param) (err error) { | ||
table, err := tx.GetTable(n.tableName) | ||
if err != nil { | ||
return | ||
} | ||
|
||
n.indexes, err = table.Indexes() | ||
return | ||
} | ||
|
||
func (n *dedupNode) toStream(st document.Stream) (document.Stream, error) { | ||
set := newDocumentHashSet(nil) // use default hashing algorithm | ||
return st.Filter(set.Filter), nil | ||
} | ||
|
||
func (n *dedupNode) String() string { | ||
return "Dedup()" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package planner | ||
|
||
import ( | ||
"hash" | ||
"hash/maphash" | ||
|
||
"github.com/genjidb/genji/document" | ||
"github.com/genjidb/genji/key" | ||
) | ||
|
||
type documentHashSet struct { | ||
hash hash.Hash64 | ||
set map[uint64]struct{} | ||
} | ||
|
||
func newDocumentHashSet(hash hash.Hash64) *documentHashSet { | ||
if hash == nil { | ||
hash = &maphash.Hash{} | ||
} | ||
|
||
return &documentHashSet{ | ||
hash: hash, | ||
set: map[uint64]struct{}{}, | ||
} | ||
} | ||
|
||
func (s documentHashSet) generateKey(d document.Document) (uint64, error) { | ||
defer s.hash.Reset() | ||
|
||
fields, err := document.Fields(d) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
for _, field := range fields { | ||
value, err := d.GetByField(field) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
buf, err := key.AppendValue(nil, value) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
_, err = s.hash.Write(buf) | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
|
||
return s.hash.Sum64(), nil | ||
} | ||
|
||
func (s documentHashSet) Filter(d document.Document) (bool, error) { | ||
k, err := s.generateKey(d) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
_, ok := s.set[k] | ||
if ok { | ||
return false, nil | ||
} | ||
|
||
s.set[k] = struct{}{} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.