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: allow reordering of annotations in flux query result #134

Merged
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 @@ -4,6 +4,7 @@

### Bug fixes
1. [#132](https://github.com/influxdata/influxdb-client-go/pull/132) Properly handle errors instead of panics
1. [#134](https://github.com/influxdata/influxdb-client-go/pull/134) FluxQueryResult: support reordering of annotations

## 1.2.0 [2020-05-15]
### Breaking Changes
Expand Down
82 changes: 46 additions & 36 deletions api/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ type QueryTableResult struct {
}

// TablePosition returns actual flux table position in the result, or -1 if no table was found yet
// Each new table is introduced by the #dataType annotation in csv
// Each new table is introduced by an annotation in csv
func (q *QueryTableResult) TablePosition() int {
if q.table != nil {
return q.table.Position()
Expand Down Expand Up @@ -208,6 +208,7 @@ type parsingState int

const (
parsingStateNormal parsingState = iota
parsingStateAnnotation
parsingStateNameRow
parsingStateError
)
Expand All @@ -233,6 +234,7 @@ func (q *QueryTableResult) Next() bool {
}()
parsingState := parsingStateNormal
q.tableChanged = false
dataTypeAnnotationFound := false
readRow:
row, q.err = q.csvReader.Read()
if q.err == io.EOF {
Expand All @@ -246,23 +248,36 @@ readRow:
if len(row) <= 1 {
goto readRow
}

if len(row[0]) > 0 && row[0][0] == '#' {
if parsingState == parsingStateNormal {
q.table = query.NewFluxTableMetadata(q.tablePosition)
q.tablePosition++
q.tableChanged = true
for i := range row[1:] {
q.table.AddColumn(query.NewFluxColumn(i))
}
parsingState = parsingStateAnnotation
}
}
if q.table == nil {
q.err = errors.New("parsing error, annotations not found")
return false
}
if len(row)-1 != len(q.table.Columns()) {
q.err = fmt.Errorf("parsing error, row has different number of columns than the table: %d vs %d", len(row)-1, len(q.table.Columns()))
return false
}
switch row[0] {
case "":
if parsingState == parsingStateError {
var message string
if len(row) > 1 && len(row[1]) > 0 {
message = row[1]
} else {
message = "unknown query error"
}
reference := ""
if len(row) > 2 && len(row[2]) > 0 {
reference = fmt.Sprintf(",%s", row[2])
switch parsingState {
case parsingStateAnnotation:
if !dataTypeAnnotationFound {
q.err = errors.New("parsing error, datatype annotation not found")
return false
}
q.err = fmt.Errorf("%s%s", message, reference)
return false
} else if parsingState == parsingStateNameRow {
parsingState = parsingStateNameRow
fallthrough
case parsingStateNameRow:
if row[1] == "error" {
parsingState = parsingStateError
} else {
Expand All @@ -274,13 +289,18 @@ readRow:
parsingState = parsingStateNormal
}
goto readRow
}
if q.table == nil {
q.err = errors.New("parsing error, datatype annotation not found")
return false
}
if len(row)-1 != len(q.table.Columns()) {
q.err = fmt.Errorf("parsing error, row has different number of columns than table: %d vs %d", len(row)-1, len(q.table.Columns()))
case parsingStateError:
var message string
if len(row) > 1 && len(row[1]) > 0 {
message = row[1]
} else {
message = "unknown query error"
}
reference := ""
if len(row) > 2 && len(row[2]) > 0 {
reference = fmt.Sprintf(",%s", row[2])
}
q.err = fmt.Errorf("%s%s", message, reference)
return false
}
values := make(map[string]interface{})
Expand All @@ -294,36 +314,26 @@ readRow:
}
q.record = query.NewFluxRecord(q.table.Position(), values)
case "#datatype":
q.table = query.NewFluxTableMetadata(q.tablePosition)
q.tablePosition++
q.tableChanged = true
dataTypeAnnotationFound = true
for i, d := range row[1:] {
q.table.AddColumn(query.NewFluxColumn(i, d))
if q.table.Column(i) != nil {
q.table.Column(i).SetDataType(d)
}
}
goto readRow
case "#group":
if q.table == nil {
q.err = errors.New("parsing error, datatype annotation not found")
return false
}
for i, g := range row[1:] {
if q.table.Column(i) != nil {
q.table.Column(i).SetGroup(g == "true")
}
}
goto readRow
case "#default":
if q.table == nil {
q.err = errors.New("parsing error, datatype annotation not found")
return false
}
for i, c := range row[1:] {
if q.table.Column(i) != nil {
q.table.Column(i).SetDefaultValue(c)
}
}
// there comes column names after defaults
parsingState = parsingStateNameRow
goto readRow
}
// don't close query
Expand Down
6 changes: 3 additions & 3 deletions api/query/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func (f *FluxTableMetadata) String() string {
return buffer.String()
}

// newFluxColumn creates FluxColumn for position and data type
func NewFluxColumn(index int, dataType string) *FluxColumn {
return &FluxColumn{index: index, dataType: dataType}
// newFluxColumn creates FluxColumn for position
func NewFluxColumn(index int) *FluxColumn {
return &FluxColumn{index: index}
}

// newFluxColumn creates FluxColumn
Expand Down
Loading