Skip to content

Commit

Permalink
Merge pull request #2857 from influxdb/jw-commas
Browse files Browse the repository at this point in the history
Fix parsing commas in string field values
  • Loading branch information
jwilder committed Jun 9, 2015
2 parents 22e6cbc + 9f444a6 commit cd3e758
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [2829](https://github.com/influxdb/influxdb/pull/2829) -- Re-enable Graphite support as a new Service-style component.
- [2814](https://github.com/influxdb/influxdb/issues/2814) -- Convert collectd to a service.
- [2852](https://github.com/influxdb/influxdb/pull/2852) -- Don't panic when altering retention policies. Thanks for the report @huhongbo
- [2857](https://github.com/influxdb/influxdb/issues/2857) -- Fix parsing commas in string field values.

## v0.9.0-rc32 [2015-06-07]

Expand Down
29 changes: 28 additions & 1 deletion tsdb/points.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ func scanTagValue(buf []byte, i int) (int, []byte) {
return i, buf[start:i]
}

func scanFieldValue(buf []byte, i int) (int, []byte) {
start := i
quoted := false
for {
if i >= len(buf) {
break
}

if buf[i] == '"' {
i += 1
quoted = !quoted
continue
}

if buf[i] == '\\' {
i += 2
continue
}

if buf[i] == ',' && !quoted {
break
}
i += 1
}
return i, buf[start:i]
}

func escape(in []byte) []byte {
for b, esc := range escapeCodes {
in = bytes.Replace(in, []byte{b}, esc, -1)
Expand Down Expand Up @@ -700,7 +727,7 @@ func newFieldsFromBinary(buf []byte) Fields {
continue
}

i, valueBuf = scanTo(buf, i+1, ',')
i, valueBuf = scanFieldValue(buf, i+1)
if len(valueBuf) == 0 {
fields[string(name)] = nil
continue
Expand Down
33 changes: 33 additions & 0 deletions tsdb/points_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,39 @@ func TestParsePointWithStringWithSpaces(t *testing.T) {
)
}

func TestParsePointWithStringWithCommas(t *testing.T) {
// escaped comma
test(t, `cpu,host=serverA,region=us-east value=1.0,str="foo\,bar" 1000000000`,
NewPoint(
"cpu",
Tags{
"host": "serverA",
"region": "us-east",
},
Fields{
"value": 1.0,
"str": "foo,bar", // commas in string value
},
time.Unix(1, 0)),
)

// non-escaped comma
test(t, `cpu,host=serverA,region=us-east value=1.0,str="foo,bar" 1000000000`,
NewPoint(
"cpu",
Tags{
"host": "serverA",
"region": "us-east",
},
Fields{
"value": 1.0,
"str": "foo,bar", // commas in string value
},
time.Unix(1, 0)),
)

}

func TestParsePointWithStringWithEquals(t *testing.T) {
test(t, `cpu,host=serverA,region=us-east str="foo=bar",value=1.0, 1000000000`,
NewPoint(
Expand Down

0 comments on commit cd3e758

Please sign in to comment.