Skip to content

Commit

Permalink
fix projection aliasing in lytics backend
Browse files Browse the repository at this point in the history
  • Loading branch information
araddon committed May 1, 2018
1 parent 54d260e commit f1b5639
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion backends/lytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ We are going to add an additional source (Lytics) via a `SQL CREATE` statement.
```sh

docker pull gcr.io/dataux-io/dataux:latest
docker run --rm -e "LOGGING=debug" -p 4000:4000 --name dataux gcr.io/dataux-io/dataux:latest
docker run --rm -e "LOGGING=debug" -p 4000:4000 --network host --name dataux gcr.io/dataux-io/dataux:latest

```

Expand Down
19 changes: 18 additions & 1 deletion backends/lytics/lytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func RunTestServer(t *testing.T) func() {
reg := schema.DefaultRegistry()

by := []byte(fmt.Sprintf(`{
"name": "lytics_test",
"name": "lyticsx",
"schema":"lyticsx",
"type": "lytics",
"settings" :{
Expand Down Expand Up @@ -188,6 +188,23 @@ func TestSimpleRowSelect(t *testing.T) {
})
}

func TestSelectProjection(t *testing.T) {
data := struct {
UserId string `db:"user_id"`
LoginCt int `db:"login_ct"`
}{}
validateQuerySpec(t, QuerySpec{
Sql: `select toint(json.jmespath(events,"login")) AS login_ct, user_id from user WHERE user_id = "user123";`,
ExpectRowCt: 1,
ValidateRowData: func() {
//u.Infof("%v", data)
assert.Equal(t, "user123", data.UserId, "%v", data)
assert.Equal(t, 1, data.LoginCt, "%v", data)
},
RowData: &data,
})
}

/*
func TestSelectAggs(t *testing.T) {
Expand Down
26 changes: 15 additions & 11 deletions backends/lytics/resultreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ type ResultReader struct {
Req *Generator
}

// A wrapper, allowing us to implement sql/driver Next() interface
// which is different than qlbridge/datasource Next()
// ResultReaderNext is wrapper, allowing us to implement sql/driver Next() interface
// which is different than qlbridge/datasource Next()
type ResultReaderNext struct {
*ResultReader
}

// NewResultReader create a new lytics reader that will handle
// translation of lytics rest api content into dataux/qlbridge types.
func NewResultReader(req *Generator) *ResultReader {
m := &ResultReader{}
m.TaskBase = exec.NewTaskBase(req.p.Context())
Expand All @@ -46,19 +48,21 @@ func NewResultReader(req *Generator) *ResultReader {

func (m *ResultReader) Close() error { return nil }

// Run() Fetch api response
// Run() Fetch api response, page through result continuing to
// send messages.
func (m *ResultReader) Run() error {

sigChan := m.SigChan()
outCh := m.MessageOut()
m.finalized = true

// projection Columns
cols := m.Req.p.Proj.Columns
client := lytics.NewLytics(m.Req.apiKey, "", nil)

colNames := make(map[string]int, len(m.Req.p.Proj.Columns))
for i, col := range m.Req.p.Proj.Columns {
colNames[col.As] = i
colNames[col.SourceName()] = i
}

defer func() {
Expand All @@ -80,28 +84,28 @@ func (m *ResultReader) Run() error {
for i, col := range cols {
switch col.Type {
case value.BoolType:
row[i] = eh.Bool(col.As)
row[i] = eh.Bool(col.SourceName())
case value.StringType:
row[i] = eh.String(col.As)
row[i] = eh.String(col.SourceName())
case value.TimeType:
t, err := dateparse.ParseAny(eh.String(col.As))
t, err := dateparse.ParseAny(eh.String(col.SourceName()))
if err == nil {
row[i] = t
}
case value.IntType:
iv, ok := eh.Int64Safe(col.As)
iv, ok := eh.Int64Safe(col.SourceName())
if ok {
row[i] = iv
}
case value.NumberType:
fv, ok := eh.Float64Safe(col.As)
fv, ok := eh.Float64Safe(col.SourceName())
if ok {
row[i] = fv
}
case value.StringsType:
row[i] = eh.Strings(col.As)
row[i] = eh.Strings(col.SourceName())
case value.JsonType:
by, _ := json.Marshal(eh[col.As])
by, _ := json.Marshal(eh[col.SourceName()])
row[i] = by
default:
u.Warnf("unhandled %s", col.Type)
Expand Down
8 changes: 5 additions & 3 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ dockerbuild() {
# if you get auth issues
#
# rm ~/.docker/config.json
# gcloud docker --authorize-only
# gcloud auth configure-docker


docker build -t gcr.io/dataux-io/dataux:$TAG .
gcloud docker -- push gcr.io/dataux-io/dataux:$TAG
#gcloud docker -- push gcr.io/dataux-io/dataux:$TAG
docker push gcr.io/dataux-io/dataux:$TAG

docker tag gcr.io/dataux-io/dataux:$TAG gcr.io/dataux-io/dataux:latest
#docker build -t gcr.io/dataux-io/dataux:latest .
gcloud docker -- push gcr.io/dataux-io/dataux:latest
docker push gcr.io/dataux-io/dataux:latest

# now lets allow anyone to read these gcr images
# https://cloud.google.com/container-registry/docs/access-control
Expand Down

0 comments on commit f1b5639

Please sign in to comment.