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

Sort alphabets of languages for non indexed fields. #4260

Merged
merged 6 commits into from
Nov 21, 2019
Merged
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
8 changes: 8 additions & 0 deletions gql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,10 @@ func getRoot(it *lex.ItemIterator) (gq *GraphQuery, rerr error) {
return nil, it.Errorf("Sorting by an attribute: [%s] can only be done once", val)
}
attr, langs := attrAndLang(val)
if len(langs) > 1 {
return nil, it.Errorf("Sorting by an attribute: [%s] "+
"can only be done on one language", val)
}
gq.Order = append(gq.Order,
&pb.Order{Attr: attr, Desc: key == "orderdesc", Langs: langs})
order[val] = true
Expand Down Expand Up @@ -3011,6 +3015,10 @@ func godeep(it *lex.ItemIterator, gq *GraphQuery) error {
"can only be done once", p.Val)
}
attr, langs := attrAndLang(p.Val)
if len(langs) > 1 {
return it.Errorf("Sorting by an attribute: [%s] "+
"can only be done on one language", p.Val)
}
curp.Order = append(curp.Order,
&pb.Order{Attr: attr, Desc: p.Key == "orderdesc", Langs: langs})
order[p.Val] = true
Expand Down
21 changes: 17 additions & 4 deletions gql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3753,7 +3753,7 @@ func TestParseQueryWithAttrLang(t *testing.T) {
{
me(func: uid(0x1)) {
name
friend(first:5, orderasc: name@en:fr) {
friend(first:5, orderasc: name@en) {
name@en
}
}
Expand All @@ -3764,7 +3764,7 @@ func TestParseQueryWithAttrLang(t *testing.T) {
require.NotNil(t, res.Query)
require.Equal(t, 1, len(res.Query))
require.Equal(t, "name", res.Query[0].Children[1].Order[0].Attr)
require.Equal(t, []string{"en", "fr"}, res.Query[0].Children[1].Order[0].Langs)
require.Equal(t, []string{"en"}, res.Query[0].Children[1].Order[0].Langs)
}

func TestParseQueryWithAttrLang2(t *testing.T) {
Expand Down Expand Up @@ -4476,10 +4476,23 @@ func TestInvalidValUsage(t *testing.T) {
require.Contains(t, err.Error(), "Query syntax invalid.")
}

func TestOrderWithMultipleLangFail(t *testing.T) {
query := `
{
me(func: uid(0x1), orderasc: name@en:fr, orderdesc: lastname@ci, orderasc: salary) {
name
}
}
`
_, err := Parse(Request{Str: query})
require.Error(t, err)
require.Contains(t, err.Error(), "Sorting by an attribute: [name@en:fr] can only be done on one language")
}

func TestOrderWithLang(t *testing.T) {
query := `
{
me(func: uid(0x1), orderasc: name@en:fr:., orderdesc: lastname@ci, orderasc: salary) {
me(func: uid(0x1), orderasc: name@en, orderdesc: lastname@ci, orderasc: salary) {
name
}
}
Expand All @@ -4490,7 +4503,7 @@ func TestOrderWithLang(t *testing.T) {
require.Equal(t, 1, len(res.Query))
orders := res.Query[0].Order
require.Equal(t, "name", orders[0].Attr)
require.Equal(t, []string{"en", "fr", "."}, orders[0].Langs)
require.Equal(t, []string{"en"}, orders[0].Langs)
require.Equal(t, "lastname", orders[1].Attr)
require.Equal(t, []string{"ci"}, orders[1].Langs)
require.Equal(t, "salary", orders[2].Attr)
Expand Down
9 changes: 8 additions & 1 deletion query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ type Node {
}

name : string @index(term, exact, trigram) @count @lang .
name_lang : string @lang .
lang_type : string @index(exact) .
alt_name : [string] @index(term, exact, trigram) @count .
alias : string @index(exact, term, fulltext) .
abbr : string .
Expand Down Expand Up @@ -348,7 +350,12 @@ func populateCluster() {
<10005> <name> "Bob" .
<10006> <name> "Colin" .
<10007> <name> "Elizabeth" .

<10101> <name_lang> "zon"@sv .
<10101> <name_lang> "öffnen"@de .
<10101> <lang_type> "Test" .
<10102> <name_lang> "öppna"@sv .
<10102> <name_lang> "zumachen"@de .
<10102> <lang_type> "Test" .
<11000> <name> "Baz Luhrmann"@en .
<11001> <name> "Strictly Ballroom"@en .
<11002> <name> "Puccini: La boheme (Sydney Opera)"@en .
Expand Down
5 changes: 3 additions & 2 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,7 @@ func (sg *SubGraph) sortAndPaginateUsingFacet(ctx context.Context) error {
continue
}
if err := types.SortWithFacet(values, &pb.List{Uids: uids},
facetList, []bool{sg.Params.FacetOrderDesc}); err != nil {
facetList, []bool{sg.Params.FacetOrderDesc}, ""); err != nil {
return err
}
sg.uidMatrix[i].Uids = uids
Expand Down Expand Up @@ -2372,7 +2372,8 @@ func (sg *SubGraph) sortAndPaginateUsingVar(ctx context.Context) error {
if len(values) == 0 {
continue
}
if err := types.Sort(values, &pb.List{Uids: uids}, []bool{sg.Params.Order[0].Desc}); err != nil {
if err := types.Sort(values, &pb.List{Uids: uids},
[]bool{sg.Params.Order[0].Desc}, ""); err != nil {
return err
}
sg.uidMatrix[i].Uids = uids
Expand Down
2 changes: 1 addition & 1 deletion query/query1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestToFastJSONOrderLang(t *testing.T) {
query := `
{
me(func: uid(0x01)) {
friend(first:2, orderdesc: alias@en:de:.) {
friend(first:2, orderdesc: alias@en) {
alias
}
}
Expand Down
52 changes: 52 additions & 0 deletions query/query2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,58 @@ func TestToFastJSONOrderDesc2(t *testing.T) {
js)
}

func TestLanguageOrderNonIndexed1(t *testing.T) {
query := `
{
q(func:eq(lang_type, "Test"), orderasc: name_lang@de) {
name_lang@de
name_lang@sv
}
}
`

js := processQueryNoErr(t, query)
require.JSONEq(t,
`{
"data": {
"q": [{
"name_lang@de": "öffnen",
"name_lang@sv": "zon"
}, {
"name_lang@de": "zumachen",
"name_lang@sv": "öppna"
}]
}
}`,
js)
}

func TestLanguageOrderNonIndexed2(t *testing.T) {
query := `
{
q(func:eq(lang_type, "Test"), orderasc: name_lang@sv) {
name_lang@de
name_lang@sv
}
}
`

js := processQueryNoErr(t, query)
require.JSONEq(t,
`{
"data": {
"q": [{
"name_lang@de": "öffnen",
"name_lang@sv": "zon"
}, {
"name_lang@de": "zumachen",
"name_lang@sv": "öppna"
}]
}
}`,
js)
}

// Test sorting / ordering by dob.
func TestToFastJSONOrderDesc_pawan(t *testing.T) {

Expand Down
12 changes: 6 additions & 6 deletions systest/21million/queries/query-016
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
]
},
{
"name@en": "Short Film",
"name@en": "Drama",
"~genre": [
{
"name@en": "\"Crossover \"- NYU Student Film"
"name@en": "-30-"
}
]
},
{
"name@en": "Drama",
"name@en": "Comedy",
"~genre": [
{
"name@en": "#Stuck"
"name@en": "¡ El Presidente !"
}
]
},
{
"name@en": "Comedy",
"name@en": "Short Film",
"~genre": [
{
"name@en": "\"FF.SS.\" – Cioè: \"...che mi hai portato a fare sopra a Posillipo se non mi vuoi più bene?\""
"name@en": ":08 Min. Core Workouts"
}
]
}
Expand Down
Loading