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

Default/tok #1274

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions cmd/dgraph/debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ grpc_port: 9080
# Port used by worker for internal communication.
workerport: 12345

# If RAM usage exceeds this, we evict LRU cache till its below the value.
max_memory_mb: 1024

# The ratio of queries to trace.
trace: 1.0

Expand Down
28 changes: 14 additions & 14 deletions cmd/dgraph/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ func TestDeletePredicate(t *testing.T) {
mutation {
schema {
friend: uid @reverse .
name: string @index .
name: string @index(term) .
}
}
`

var s2 = `
mutation {
schema {
friend: string @index .
friend: string @index(term) .
name: uid @reverse .
}
}
Expand Down Expand Up @@ -301,18 +301,18 @@ func TestSchemaMutation(t *testing.T) {
var m = `
mutation {
schema {
name:string @index(term, exact) .
name:string @index(term, exact) .
alias:string @index(exact, term) .
dob:dateTime @index .
film.film.initial_release_date:dateTime @index .
loc:geo @index .
dob:dateTime @index(year) .
film.film.initial_release_date:dateTime @index(year) .
loc:geo @index(geo) .
genre:uid @reverse .
survival_rate : float .
alive : bool .
age : int .
shadow_deep : int .
friend:uid @reverse .
geometry:geo @index .
geometry:geo @index(geo) .
}
}

Expand Down Expand Up @@ -395,7 +395,7 @@ func TestSchemaMutationIndexAdd(t *testing.T) {
var s = `
mutation {
schema {
name:string @index .
name:string @index(term) .
}
}
`
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestSchemaMutationIndexRemove(t *testing.T) {
var s1 = `
mutation {
schema {
name:string @index .
name:string @index(term) .
}
}
`
Expand Down Expand Up @@ -615,7 +615,7 @@ func TestDeleteAll(t *testing.T) {
mutation {
schema {
friend:uid @reverse .
name: string @index .
name: string @index(term) .
}
}
`
Expand Down Expand Up @@ -715,7 +715,7 @@ func TestDeleteAllSP(t *testing.T) {
mutation {
schema {
friend:uid @reverse .
name: string @index .
name: string @index(term) .
}
}
`
Expand Down Expand Up @@ -939,7 +939,7 @@ func TestListPred(t *testing.T) {
var s = `
mutation {
schema {
name:string @index .
name:string @index(term) .
}
}
`
Expand Down Expand Up @@ -983,7 +983,7 @@ func TestExpandPredError(t *testing.T) {
var s = `
mutation {
schema {
name:string @index .
name:string @index(term) .
}
}
`
Expand Down Expand Up @@ -1025,7 +1025,7 @@ func TestExpandPred(t *testing.T) {
var s = `
mutation {
schema {
name:string @index .
name:string @index(term) .
}
}
`
Expand Down
4 changes: 2 additions & 2 deletions contrib/loader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ sleep 15
#Set Schema
curl -X POST -d 'mutation {
schema {
name: string @index .
name: string @index(term) .
_xid_: string @index(exact,term) .
initial_release_date: datetime @index .
initial_release_date: datetime @index(year) .
}
}' "http://localhost:8080/query"

Expand Down
21 changes: 11 additions & 10 deletions posting/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

const schemaStr = `
name:string @index .
name:string @index(term) .
`

func uids(pl *protos.PostingList) []uint64 {
Expand All @@ -48,35 +48,35 @@ func uids(pl *protos.PostingList) []uint64 {
}

func TestIndexingInt(t *testing.T) {
schema.ParseBytes([]byte("age:int @index ."), 1)
schema.ParseBytes([]byte("age:int @index(int) ."), 1)
a, err := IndexTokens("age", "", types.Val{types.StringID, []byte("10")})
require.NoError(t, err)
require.EqualValues(t, []byte{0x6, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, []byte(a[0]))
}

func TestIndexingIntNegative(t *testing.T) {
schema.ParseBytes([]byte("age:int @index ."), 1)
schema.ParseBytes([]byte("age:int @index(int) ."), 1)
a, err := IndexTokens("age", "", types.Val{types.StringID, []byte("-10")})
require.NoError(t, err)
require.EqualValues(t, []byte{0x6, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf6}, []byte(a[0]))
}

func TestIndexingFloat(t *testing.T) {
schema.ParseBytes([]byte("age:float @index ."), 1)
schema.ParseBytes([]byte("age:float @index(float) ."), 1)
a, err := IndexTokens("age", "", types.Val{types.StringID, []byte("10.43")})
require.NoError(t, err)
require.EqualValues(t, []byte{0x7, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa}, []byte(a[0]))
}

func TestIndexingTime(t *testing.T) {
schema.ParseBytes([]byte("age:dateTime @index ."), 1)
schema.ParseBytes([]byte("age:dateTime @index(year) ."), 1)
a, err := IndexTokens("age", "", types.Val{types.StringID, []byte("0010-01-01T01:01:01.000000001")})
require.NoError(t, err)
require.EqualValues(t, []byte{0x4, 0x0, 0xa}, []byte(a[0]))
}

func TestIndexing(t *testing.T) {
schema.ParseBytes([]byte("name:string @index ."), 1)
schema.ParseBytes([]byte("name:string @index(term) ."), 1)
a, err := IndexTokens("name", "", types.Val{types.StringID, []byte("abc")})
require.NoError(t, err)
require.EqualValues(t, "\x01abc", string(a[0]))
Expand Down Expand Up @@ -126,13 +126,14 @@ func addMutationWithIndex(t *testing.T, l *List, edge *protos.DirectedEdge, op u
}

const schemaVal = `
name:string @index .
dob:dateTime @index .
name:string @index(term) .
dob:dateTime @index(year) .
friend:uid @reverse .
`

func TestTokensTable(t *testing.T) {
schema.ParseBytes([]byte(schemaVal), 1)
err := schema.ParseBytes([]byte(schemaVal), 1)
require.NoError(t, err)

key := x.DataKey("name", 1)
l := getNew(key, ps)
Expand All @@ -145,7 +146,7 @@ func TestTokensTable(t *testing.T) {
Entity: 157,
}
addMutationWithIndex(t, l, edge, Set)
_, err := l.SyncIfDirty(false)
_, err = l.SyncIfDirty(false)
x.Check(err)

key = x.IndexKey("name", "david")
Expand Down
12 changes: 6 additions & 6 deletions query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6658,17 +6658,17 @@ func TestSchemaBlock5(t *testing.T) {
const schemaStr = `
name : string @index(term, exact, trigram) @count .
alias : string @index(exact, term, fulltext) .
dob : dateTime @index .
dob : dateTime @index(year) .
dob_day : dateTime @index(day) .
film.film.initial_release_date : dateTime @index .
loc : geo @index .
film.film.initial_release_date : dateTime @index(year) .
loc : geo @index(geo) .
genre : uid @reverse .
survival_rate : float .
alive : bool @index .
age : int @index .
alive : bool @index(bool) .
age : int @index(int) .
shadow_deep : int .
friend : uid @reverse @count .
geometry : geo @index .
geometry : geo @index(geo) .
value : string @index(trigram) .
full_name : string @index(hash) .
noindex_name : string .
Expand Down
8 changes: 5 additions & 3 deletions schema/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,13 @@ func parseIndexDirective(it *lex.ItemIterator, predicate string,
}
if !it.Next() {
// Nothing to read.
return []string{tok.Default(typ).Name()}, nil
return []string{}, x.Errorf("Invalid ending.")
}
next := it.Item()
if next.Typ != itemLeftRound {
it.Prev() // Backup.
return []string{tok.Default(typ).Name()}, nil
return []string{}, x.Errorf("Require type of tokenizer for pred: %s for indexing.",
predicate)
}

expectArg := true
Expand Down Expand Up @@ -228,7 +229,8 @@ func resolveTokenizers(updates []*protos.SchemaUpdate) error {
}

if len(schema.Tokenizer) == 0 && schema.Directive == protos.SchemaUpdate_INDEX {
schema.Tokenizer = []string{tok.Default(typ).Name()}
return x.Errorf("Require type of tokenizer for pred: %s of type: %s for indexing.",
schema.Predicate, typ.Name())
} else if len(schema.Tokenizer) > 0 && schema.Directive != protos.SchemaUpdate_INDEX {
return x.Errorf("Tokenizers present without indexing on attr %s", schema.Predicate)
}
Expand Down
20 changes: 17 additions & 3 deletions schema/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ func TestSchema3_Error(t *testing.T) {
}

var schemaIndexVal1 = `
age:int @index .
age:int @index(int) .

name: string .
address: string @index .`
address: string @index(term) .`

func TestSchemaIndex(t *testing.T) {
require.NoError(t, ParseBytes([]byte(schemaIndexVal1), 1))
Expand Down Expand Up @@ -248,6 +248,20 @@ func TestParse6_Error(t *testing.T) {
require.Nil(t, schemas)
}

func TestParse7_Error(t *testing.T) {
reset()
schemas, err := Parse("name:string @index .")
require.Error(t, err)
require.Nil(t, schemas)
}

func TestParse8_Error(t *testing.T) {
reset()
schemas, err := Parse("dob:dateTime @index .")
require.Error(t, err)
require.Nil(t, schemas)
}

var ps *badger.KV

func TestMain(m *testing.M) {
Expand All @@ -271,6 +285,6 @@ func TestMain(m *testing.M) {

func TestParseUnderscore(t *testing.T) {
reset()
_, err := Parse("_share_:string @index .")
_, err := Parse("_share_:string @index(term) .")
require.NoError(t, err)
}
39 changes: 8 additions & 31 deletions tok/tok.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func init() {
RegisterTokenizer(GeoTokenizer{})
RegisterTokenizer(IntTokenizer{})
RegisterTokenizer(FloatTokenizer{})
RegisterTokenizer(DateTimeTokenizer{})
RegisterTokenizer(YearTokenizer{})
RegisterTokenizer(HourTokenizer{})
RegisterTokenizer(MonthTokenizer{})
RegisterTokenizer(DayTokenizer{})
Expand All @@ -71,12 +71,6 @@ func init() {
RegisterTokenizer(BoolTokenizer{})
RegisterTokenizer(TrigramTokenizer{})
RegisterTokenizer(HashTokenizer{})
SetDefault(types.GeoID, "geo")
SetDefault(types.IntID, "int")
SetDefault(types.FloatID, "float")
SetDefault(types.DateTimeID, "datetime")
SetDefault(types.StringID, "term")
SetDefault(types.BoolID, "bool")

// Check for duplicate prefix bytes.
usedIds := make(map[byte]struct{})
Expand All @@ -97,23 +91,6 @@ func GetTokenizer(name string) (Tokenizer, bool) {
return t, found
}

// Default returns the default tokenizer for a given type.
func Default(typ types.TypeID) Tokenizer {
t, found := defaults[typ]
x.AssertTruef(found, "No default tokenizer set for type %v", typ)
return t
}

// SetDefault sets the default tokenizer for given typeID.
func SetDefault(typ types.TypeID, name string) {
if defaults == nil {
defaults = make(map[types.TypeID]Tokenizer)
}
t, has := GetTokenizer(name)
x.AssertTruef(has && t.Type() == typ, "Type mismatch %v vs %v", t.Type(), typ)
defaults[typ] = t
}

// RegisterTokenizer adds your tokenizer to our list.
func RegisterTokenizer(t Tokenizer) {
if tokenizers == nil {
Expand Down Expand Up @@ -160,19 +137,19 @@ func (t FloatTokenizer) Identifier() byte { return 0x7 }
func (t FloatTokenizer) IsSortable() bool { return true }
func (t FloatTokenizer) IsLossy() bool { return true }

type DateTimeTokenizer struct{}
type YearTokenizer struct{}

func (t DateTimeTokenizer) Name() string { return "datetime" }
func (t DateTimeTokenizer) Type() types.TypeID { return types.DateTimeID }
func (t DateTimeTokenizer) Tokens(sv types.Val) ([]string, error) {
func (t YearTokenizer) Name() string { return "year" }
func (t YearTokenizer) Type() types.TypeID { return types.DateTimeID }
func (t YearTokenizer) Tokens(sv types.Val) ([]string, error) {
tval := sv.Value.(time.Time)
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf[0:2], uint16(tval.Year()))
return []string{encodeToken(string(buf), t.Identifier())}, nil
}
func (t DateTimeTokenizer) Identifier() byte { return 0x4 }
func (t DateTimeTokenizer) IsSortable() bool { return true }
func (t DateTimeTokenizer) IsLossy() bool { return true }
func (t YearTokenizer) Identifier() byte { return 0x4 }
func (t YearTokenizer) IsSortable() bool { return true }
func (t YearTokenizer) IsLossy() bool { return true }

type MonthTokenizer struct{}

Expand Down
2 changes: 1 addition & 1 deletion tok/tok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestMonthTokenizer(t *testing.T) {

func TestDateTimeTokenizer(t *testing.T) {
var err error
tokenizer, has := GetTokenizer("datetime")
tokenizer, has := GetTokenizer("year")
require.True(t, has)
require.NotNil(t, tokenizer)
val := types.ValueForType(types.DateTimeID)
Expand Down
4 changes: 2 additions & 2 deletions worker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func newQuery(attr string, uids []uint64, srcFunc []string) *protos.Query {
// at the end. In other words, everything is happening only in mutation layers,
// and not committed to RocksDB until near the end.
func TestProcessTaskIndexMLayer(t *testing.T) {
dir, ps := initTest(t, `friend:string @index .`)
dir, ps := initTest(t, `friend:string @index(term) .`)
defer os.RemoveAll(dir)
defer ps.Close()

Expand Down Expand Up @@ -233,7 +233,7 @@ func TestProcessTaskIndexMLayer(t *testing.T) {
// Index-related test. Similar to TestProcessTaskIndeMLayer except we call
// MergeLists in between a lot of updates.
func TestProcessTaskIndex(t *testing.T) {
dir, ps := initTest(t, `friend:string @index .`)
dir, ps := initTest(t, `friend:string @index(term) .`)
defer os.RemoveAll(dir)
defer ps.Close()

Expand Down