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

Fix parsing commas in string field values #2857

Merged
merged 2 commits into from
Jun 9, 2015
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
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