Skip to content

Commit

Permalink
Fix parsing commas in string field values
Browse files Browse the repository at this point in the history
Fixes a panic on writes because the field value was not parse correctly.

panic: unsupported value type during encode fields: <nil>

goroutine 117 [running]:
github.com/influxdb/influxdb/tsdb.(*FieldCodec).EncodeFields(0xc2081c4020, 0xc2081dc180, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/jason/go/src/github.com/influxdb/influxdb/tsdb/shard.go:573 +0x8e3
  • Loading branch information
jwilder committed Jun 9, 2015
1 parent 22e6cbc commit 3e96916
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
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 3e96916

Please sign in to comment.