Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 05a2456

Browse files
Improve the efficiency of readIdentList (HT @erizocosmico)
Before, this function read the string containing all the identifiers *and* the separators between them, splitting the string afterwards to retrieve the list. Now, the list of identifiers is built on the go. Signed-off-by: Alejandro García Montoro <alejandro.garciamontoro@gmail.com>
1 parent 16413c9 commit 05a2456

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

sql/parse/util.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,22 @@ func readValidIdentRune(r *bufio.Reader, buf *bytes.Buffer) error {
179179
// readValidScopedIdentRune parses a single rune from the reader and consumes
180180
// it, copying it to the buffer, if is a letter, a digit, an underscore or the
181181
// specified separator.
182-
func readValidScopedIdentRune(r *bufio.Reader, separator rune, buf *bytes.Buffer) error {
182+
// If the returned error is not nil, the returned rune equals the null
183+
// character.
184+
func readValidScopedIdentRune(r *bufio.Reader, separator rune) (rune, error) {
183185
ru, _, err := r.ReadRune()
184186
if err != nil {
185-
return err
187+
return 0, err
186188
}
187189

188190
if !unicode.IsLetter(ru) && !unicode.IsDigit(ru) && ru != '_' && ru != separator {
189191
if err := r.UnreadRune(); err != nil {
190-
return err
192+
return 0, err
191193
}
192-
return io.EOF
194+
return 0, io.EOF
193195
}
194196

195-
buf.WriteRune(ru)
196-
return nil
197+
return ru, nil
197198
}
198199

199200
func readValidQuotedIdentRune(r *bufio.Reader, buf *bytes.Buffer) error {
@@ -266,18 +267,24 @@ func readIdentList(separator rune, idents *[]string) parseFunc {
266267
}
267268

268269
for {
269-
if err := readValidScopedIdentRune(r, separator, &buf); err == io.EOF {
270-
break
271-
} else if err != nil {
270+
currentRune, err := readValidScopedIdentRune(r, separator)
271+
if err != nil {
272+
if err == io.EOF {
273+
break
274+
}
272275
return err
273276
}
277+
278+
if currentRune == separator {
279+
*idents = append(*idents, buf.String())
280+
buf.Reset()
281+
} else {
282+
buf.WriteRune(currentRune)
283+
}
274284
}
275285

276286
if readString := buf.String(); len(readString) > 0 {
277-
*idents = append(
278-
*idents,
279-
strings.Split(strings.ToLower(readString), string(separator))...,
280-
)
287+
*idents = append(*idents, readString)
281288
}
282289
return nil
283290
}

sql/parse/util_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,14 @@ func TestReadValidScopedIdentRune(t *testing.T) {
133133
reader := bufio.NewReader(strings.NewReader(fixture.string))
134134
var buffer bytes.Buffer
135135

136+
var rune rune
136137
var err error
137138
for i := 0; i < len(fixture.string); i++ {
138-
err = readValidScopedIdentRune(reader, fixture.separator, &buffer)
139+
if rune, err = readValidScopedIdentRune(reader, fixture.separator); err != nil {
140+
break
141+
}
142+
143+
buffer.WriteRune(rune)
139144
}
140145
if fixture.expectedError {
141146
require.Error(err)
@@ -146,7 +151,7 @@ func TestReadValidScopedIdentRune(t *testing.T) {
146151
remaining, _ := reader.ReadString('\n')
147152
require.Equal(remaining, fixture.expectedRemaining)
148153

149-
require.Equal(buffer.String(), fixture.expectedBuffer)
154+
require.Equal(fixture.expectedBuffer, buffer.String())
150155
}
151156
}
152157

0 commit comments

Comments
 (0)