Skip to content

Commit

Permalink
Merge pull request #6713 from influxdata/er-query-string
Browse files Browse the repository at this point in the history
Reduce allocations on Query's io.Stringer implementation
  • Loading branch information
e-dard committed May 24, 2016
2 parents 0752ca8 + 853a95e commit 4a36530
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [#6655](https://github.com/influxdata/influxdb/issues/6655): Add HTTP(s) based subscriptions.
- [#5906](https://github.com/influxdata/influxdb/issues/5906): Dynamically update the documentation link in the admin UI.
- [#6686](https://github.com/influxdata/influxdb/pull/6686): Optimize timestamp run-length decoding
- [#6713](https://github.com/influxdata/influxdb/pull/6713): Reduce allocations during query parsing.

### Bugfixes

Expand Down
8 changes: 8 additions & 0 deletions influxql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
"github.com/influxdata/influxdb/influxql"
)

func BenchmarkQuery_String(b *testing.B) {
p := influxql.NewParser(strings.NewReader(`SELECT foo AS zoo, a AS b FROM bar WHERE value > 10 AND q = 'hello'`))
q, _ := p.ParseStatement()
for i := 0; i < b.N; i++ {
_ = q.String()
}
}

// Ensure a value's data type can be retrieved.
func TestInspectDataType(t *testing.T) {
for i, tt := range []struct {
Expand Down
11 changes: 7 additions & 4 deletions influxql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2680,15 +2680,18 @@ func (p *Parser) parseTokenMaybe(expected Token) bool {
return true
}

var (
qsReplacer = strings.NewReplacer("\n", `\n`, `\`, `\\`, `'`, `\'`)
qiReplacer = strings.NewReplacer("\n", `\n`, `\`, `\\`, `"`, `\"`)
)

// QuoteString returns a quoted string.
func QuoteString(s string) string {
return `'` + strings.NewReplacer("\n", `\n`, `\`, `\\`, `'`, `\'`).Replace(s) + `'`
return `'` + qsReplacer.Replace(s) + `'`
}

// QuoteIdent returns a quoted identifier from multiple bare identifiers.
func QuoteIdent(segments ...string) string {
r := strings.NewReplacer("\n", `\n`, `\`, `\\`, `"`, `\"`)

var buf bytes.Buffer
for i, segment := range segments {
needQuote := IdentNeedsQuotes(segment) ||
Expand All @@ -2698,7 +2701,7 @@ func QuoteIdent(segments ...string) string {
_ = buf.WriteByte('"')
}

_, _ = buf.WriteString(r.Replace(segment))
_, _ = buf.WriteString(qiReplacer.Replace(segment))

if needQuote {
_ = buf.WriteByte('"')
Expand Down

0 comments on commit 4a36530

Please sign in to comment.