Improve ORDER BY COLLATE queries

This commit is contained in:
Tobie Morgan Hitchcock 2019-02-01 00:12:10 +00:00
parent ef9c73d4d7
commit 71192d2e36
2 changed files with 80 additions and 10 deletions

View file

@ -1470,6 +1470,41 @@ func TestSelect(t *testing.T) {
setupDB(1) setupDB(1)
txt := `
USE NS test DB test;
CREATE |person:1..10|;
UPDATE person:3 SET test = "ändrew";
UPDATE person:5 SET test = "Another";
UPDATE person:7 SET test = "alexander";
UPDATE person:9 SET test = "Alexander";
UPDATE person:2 SET test = "Tobie";
UPDATE person:4 SET test = "1000";
UPDATE person:6 SET test = "2";
UPDATE person:8 SET test = null;
SELECT test FROM person ORDER BY test COLLATE ASC;
`
res, err := Execute(permsKV(), txt, nil)
So(err, ShouldBeNil)
So(res, ShouldHaveLength, 11)
So(res[10].Result, ShouldHaveLength, 10)
So(data.Consume(res[10].Result[0]).Get("test").Data(), ShouldEqual, nil)
So(data.Consume(res[10].Result[1]).Get("test").Data(), ShouldEqual, nil)
So(data.Consume(res[10].Result[2]).Get("test").Data(), ShouldEqual, nil)
So(data.Consume(res[10].Result[3]).Get("test").Data(), ShouldEqual, "1000")
So(data.Consume(res[10].Result[4]).Get("test").Data(), ShouldEqual, "2")
So(data.Consume(res[10].Result[5]).Get("test").Data(), ShouldEqual, "Alexander")
So(data.Consume(res[10].Result[6]).Get("test").Data(), ShouldEqual, "alexander")
So(data.Consume(res[10].Result[7]).Get("test").Data(), ShouldEqual, "ändrew")
So(data.Consume(res[10].Result[8]).Get("test").Data(), ShouldEqual, "Another")
So(data.Consume(res[10].Result[9]).Get("test").Data(), ShouldEqual, "Tobie")
})
Convey("Order records with en-GB collation", t, func() {
setupDB(1)
txt := ` txt := `
USE NS test DB test; USE NS test DB test;
CREATE |person:1..10|; CREATE |person:1..10|;
@ -1505,6 +1540,41 @@ func TestSelect(t *testing.T) {
setupDB(1) setupDB(1)
txt := `
USE NS test DB test;
CREATE |person:1..10|;
UPDATE person:3 SET test = "ändrew";
UPDATE person:5 SET test = "Another";
UPDATE person:7 SET test = "alexander";
UPDATE person:9 SET test = "Alexander";
UPDATE person:2 SET test = "Tobie";
UPDATE person:4 SET test = "1000";
UPDATE person:6 SET test = "2";
UPDATE person:8 SET test = null;
SELECT test FROM person ORDER BY test COLLATE NUMERIC ASC;
`
res, err := Execute(permsKV(), txt, nil)
So(err, ShouldBeNil)
So(res, ShouldHaveLength, 11)
So(res[10].Result, ShouldHaveLength, 10)
So(data.Consume(res[10].Result[0]).Get("test").Data(), ShouldEqual, nil)
So(data.Consume(res[10].Result[1]).Get("test").Data(), ShouldEqual, nil)
So(data.Consume(res[10].Result[2]).Get("test").Data(), ShouldEqual, nil)
So(data.Consume(res[10].Result[3]).Get("test").Data(), ShouldEqual, "2")
So(data.Consume(res[10].Result[4]).Get("test").Data(), ShouldEqual, "1000")
So(data.Consume(res[10].Result[5]).Get("test").Data(), ShouldEqual, "Alexander")
So(data.Consume(res[10].Result[6]).Get("test").Data(), ShouldEqual, "alexander")
So(data.Consume(res[10].Result[7]).Get("test").Data(), ShouldEqual, "ändrew")
So(data.Consume(res[10].Result[8]).Get("test").Data(), ShouldEqual, "Another")
So(data.Consume(res[10].Result[9]).Get("test").Data(), ShouldEqual, "Tobie")
})
Convey("Order records with en-GB collation and numeric sorting", t, func() {
setupDB(1)
txt := ` txt := `
USE NS test DB test; USE NS test DB test;
CREATE |person:1..10|; CREATE |person:1..10|;
@ -1536,7 +1606,7 @@ func TestSelect(t *testing.T) {
}) })
Convey("Order records with collation and numeric and insensitive sorting using unicode definition", t, func() { Convey("Order records with en-GB collation and numeric and insensitive sorting using unicode definition", t, func() {
setupDB(1) setupDB(1)

View file

@ -289,23 +289,23 @@ func (p *parser) parseType() (t, k string, err error) {
} }
func (p *parser) parseLanguage() (language.Tag, error) { func (p *parser) parseLanguage() (tag language.Tag, err error) {
_, lit, err := p.shouldBe(IDENT, STRING) tag = language.English
if err != nil {
return language.English, &ParseError{Found: lit, Expected: []string{"string"}} if _, lit, exi := p.mightBe(IDENT, STRING); exi {
if tag, err = language.Parse(lit); err != nil {
return tag, &ParseError{Found: lit, Expected: []string{"BCP47 language"}}
} }
tag, err := language.Parse(lit)
if err != nil {
return language.English, &ParseError{Found: lit, Expected: []string{"BCP47 language"}}
} }
if _, _, exi := p.mightBe(NUMERIC); exi { if _, _, exi := p.mightBe(NUMERIC); exi {
tag, _ = tag.SetTypeForKey("kn", "true") tag, _ = tag.SetTypeForKey("kn", "true")
} }
return tag, err return tag, nil
} }