Skip to content

Commit

Permalink
Infer data-type of column from other rows if value in first row is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Isabella Dix committed May 12, 2023
1 parent 322c39f commit 333a8af
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 32 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,25 @@ yarn prettier --write .
```

### Backend
> **_NOTE:_** Depending on your version of go you might need `go get -u` instead of `go install`.
Optional: Use Docker for building with go
```bash
sudo docker run --rm -w /app -it -v $(pwd):/app golang:1.16 bash
go get -u github.com/magefile/mage
go install github.com/magefile/mage
```

1. Update [Grafana plugin SDK for Go](https://grafana.com/docs/grafana/latest/developers/plugins/backend/grafana-plugin-sdk-for-go/) dependency to the latest minor version:

```bash
go get -u github.com/grafana/grafana-plugin-sdk-go
go install github.com/grafana/grafana-plugin-sdk-go
go mod tidy
```

2. Build with go:

```bash
go get ./...
go install ./...
go build ./...
```

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"
services:
grafana:
image: grafana/grafana:8.3.4
image: grafana/grafana:9.4.3
environment:
- GF_DEFAULT_APP_MODE=development
- GF_SECURITY_ADMIN_USER=admin
Expand Down
2 changes: 1 addition & 1 deletion neo4j-datasource-plugin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.3.0
github.com/grafana/grafana-plugin-sdk-go v0.121.0
github.com/magefile/mage v1.12.1
github.com/magefile/mage v1.14.0
github.com/neo4j/neo4j-go-driver/v4 v4.4.0
)
11 changes: 2 additions & 9 deletions neo4j-datasource-plugin/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.1 h1:jAbXjIeW2ZSW2AwFxlGTDoc2CjI2XujLkV3ArsZFCvc=
github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
Expand Down Expand Up @@ -184,8 +183,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/magefile/mage v1.12.1 h1:oGdAbhIUd6iKamKlDGVtU6XGdy5SgNuCWn7gCTgHDtU=
github.com/magefile/mage v1.12.1/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mattetti/filebuffer v1.0.1 h1:gG7pyfnSIZCxdoKq+cPa8T0hhYtD9NxCdI4D7PTjRLM=
github.com/mattetti/filebuffer v1.0.1/go.mod h1:YdMURNDOttIiruleeVr6f56OrMc+MydEnTcXwtkxNVs=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
Expand All @@ -211,8 +210,6 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/neo4j/neo4j-go-driver v1.8.3 h1:yfuo9YBAlezdIiogu92GwEir/81RD81dNwS5mY/wAIk=
github.com/neo4j/neo4j-go-driver v1.8.3/go.mod h1:ncO5VaFWh0Nrt+4KT4mOZboaczBZcLuHrG+/sUeP8gI=
github.com/neo4j/neo4j-go-driver/v4 v4.4.0 h1:p52GnTFjT/prEHdinfDFCEaQwcIhQ7qpR8axJz8yzuE=
github.com/neo4j/neo4j-go-driver/v4 v4.4.0/go.mod h1:7mOj4ipioP0S07AyC6EPstJysKnVqeJJcK2uJkf8gQg=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand All @@ -222,11 +219,9 @@ github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQ
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.9.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.16.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down Expand Up @@ -352,7 +347,6 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand Down Expand Up @@ -415,7 +409,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40 h1:JWgyZ1qgdTaF3N3oxC+MdTV7qvEEgHo3otj+HB5CM7Q=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
46 changes: 28 additions & 18 deletions neo4j-datasource-plugin/pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,36 +159,44 @@ func toDataResponse(result neo4j.Result) (backend.DataResponse, error) {
// create data frame response.
frame := data.NewFrame("response")

var currentRecord *neo4j.Record
if result.Next() {
currentRecord = result.Record()
}
var allRecords, _ = result.Collect()

// infer data type per column and define frame for it
for columnNr, columnName := range keys {
var typ interface{}
if len(allRecords) > 0 {
typ = getTypeArray(allRecords[0], columnNr)
} else {
typ = getTypeArray(nil, columnNr)
}

if typ == nil {
log.DefaultLogger.Debug("Could not infer type from first columnNr, because value was nil. Trying next rows")

for i := 1; i < len(allRecords) && typ == nil; i++ {
typ = getTypeArray(allRecords[i], columnNr)
}
}

for i, k := range keys {
// infer datatypes of columns from first Row
typ := getTypeArray(currentRecord, i)
if typ == nil {
log.DefaultLogger.Debug("After looking at all rows, type is still nil. Assigning string-type as default")
typ = []*string{}
}

frame.Fields = append(frame.Fields,
data.NewField(k, nil, typ),
data.NewField(columnName, nil, typ),
)
}

row := 0
for currentRecord != nil {
values := result.Record().Values

// iterate through rows and append frame of values to result
for _, currentRecord := range allRecords {
values := currentRecord.Values
vals := make([]interface{}, len(frame.Fields))
for col, v := range values {
val := toValue(v)
vals[col] = val
}
frame.AppendRow(vals...)
if result.Next() {
currentRecord = result.Record()
row++
} else {
currentRecord = nil
}
}

// add the frames to the response.
Expand Down Expand Up @@ -259,6 +267,8 @@ func getTypeArray(record *neo4j.Record, idx int) interface{} {
return []*bool{}
case time.Time, dbtype.Date, dbtype.Time, dbtype.LocalTime, dbtype.LocalDateTime:
return []*time.Time{}
case nil:
return nil
default:
return []*string{}
}
Expand Down
13 changes: 13 additions & 0 deletions neo4j-datasource-plugin/pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,19 @@ func TestColumnNameWithDot(t *testing.T) {
runNeo4JIntegrationTest(t, cypher, expectedFrame)
}

func TestNullInIntColumn(t *testing.T) {
skipIfIsShort(t)
expectedFrame := data.NewFrame("response",
data.NewField("A", nil, []*int64{
nil,
ptrI(1),
}))

cypher := "With null as A return A UNION ALL With 1 as A return A"

runNeo4JIntegrationTest(t, cypher, expectedFrame)
}

func runNeo4JIntegrationTest(t *testing.T, cypher string, expected *data.Frame) {
neo4JSettings := neo4JSettings{
Url: "neo4j://localhost:7687",
Expand Down

0 comments on commit 333a8af

Please sign in to comment.