/
field_validator.go
70 lines (60 loc) · 1.85 KB
/
field_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package tsdb
import (
"bytes"
"fmt"
"github.com/pineda89/influxdb/models"
"github.com/influxdata/influxql"
)
// FieldValidator should return a PartialWriteError if the point should not be written.
type FieldValidator interface {
Validate(mf *MeasurementFields, point models.Point) error
}
// defaultFieldValidator ensures that points do not use different types for fields that already exist.
type defaultFieldValidator struct{}
// Validate will return a PartialWriteError if the point has inconsistent fields.
func (defaultFieldValidator) Validate(mf *MeasurementFields, point models.Point) error {
iter := point.FieldIterator()
for iter.Next() {
// Skip fields name "time", they are illegal.
if bytes.Equal(iter.FieldKey(), timeBytes) {
continue
}
// If the fields is not present, there cannot be a conflict.
f := mf.FieldBytes(iter.FieldKey())
if f == nil {
continue
}
dataType := dataTypeFromModelsFieldType(iter.Type())
if dataType == influxql.Unknown {
continue
}
// If the types are not the same, there is a conflict.
if f.Type != dataType {
return PartialWriteError{
Reason: fmt.Sprintf(
"%s: input field \"%s\" on measurement \"%s\" is type %s, already exists as type %s",
ErrFieldTypeConflict, iter.FieldKey(), point.Name(), dataType, f.Type),
Dropped: 1,
}
}
}
return nil
}
// dataTypeFromModelsFieldType returns the influxql.DataType that corresponds to the
// passed in field type. If there is no good match, it returns Unknown.
func dataTypeFromModelsFieldType(fieldType models.FieldType) influxql.DataType {
switch fieldType {
case models.Float:
return influxql.Float
case models.Integer:
return influxql.Integer
case models.Unsigned:
return influxql.Unsigned
case models.Boolean:
return influxql.Boolean
case models.String:
return influxql.String
default:
return influxql.Unknown
}
}