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

Add subquery test code #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions builder/datasource/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package datasource

import (
"encoding/json"

"github.com/grafadruid/go-druid/builder"
)

type Query struct {
Base
Query builder.Query `json:"-,omitempty"`
Query builder.Query `json:"query,omitempty"`
jbguerraz marked this conversation as resolved.
Show resolved Hide resolved
}

func NewQuery() *Query {
Expand All @@ -17,8 +16,9 @@ func NewQuery() *Query {
return q
}

func (q *Query) SetQuery(qry builder.Query) {
func (q *Query) SetQuery(qry builder.Query) *Query {
q.Query = qry
return q
}

func (q *Query) UnmarshalJSONWithQueryLoader(data []byte, loader func(data []byte) (builder.Query, error)) error {
Expand Down
6 changes: 3 additions & 3 deletions builder/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ func (b *Base) UnmarshalJSON(data []byte) error {
}
if b.Type() != "sql" {
d, err := datasource.Load(tmp.DataSource)
if err != nil && d.Type() == "query" {
err = d.(*datasource.Query).UnmarshalJSONWithQueryLoader(tmp.DataSource, Load) // cycle import
}
if err != nil {
return err
}
if d.Type() == "query" {
d.(*datasource.Query).UnmarshalJSONWithQueryLoader(tmp.DataSource, Load)
}
b.DataSource = d
var i builder.Intervals
if tmp.Intervals != nil {
Expand Down
22 changes: 22 additions & 0 deletions builder/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,25 @@ func TestAllSetterSQL(t *testing.T) {
assert.JSONEq(t, expected, string(expressionJSON))

}

func TestScanUnmarshalJson(t *testing.T) {
expected := `{
"queryType": "scan",
"dataSource": {
"type": "table",
"name": "A"
},
"columns": [
"AT"
],
"intervals": {
"type": "intervals",
"intervals": [
"1980-06-12T22:30:00.000Z/2020-01-26T23:00:00.000Z"
]
}
}`
scan := NewScan()
err := scan.UnmarshalJSON([]byte(expected))
assert.Nil(t, err)
}
11 changes: 4 additions & 7 deletions examples/fromjson/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
//go:build mage
// +build mage

package main

import (
"fmt"
"github.com/grafadruid/go-druid"
"github.com/grafadruid/go-druid/builder"
"log"

"github.com/davecgh/go-spew/spew"
"github.com/grafadruid/go-druid"
"github.com/grafadruid/go-druid/builder"
)

func loadAndExecute(d *druid.Client, qry []byte) {
Expand Down Expand Up @@ -54,8 +51,8 @@ func main() {
loadAndExecute(d, []byte(qry))

// https://github.com/grafadruid/go-druid/issues/15
qry = `{"batchSize":20480,"columns":["__time","channel","cityName","comment","count","countryIsoCode","diffUrl","flags","isAnonymous","isMinor","isNew","isRobot","isUnpatrolled","metroCode","namespace","page","regionIsoCode","regionName","sum_added","sum_commentLength","sum_deleted","sum_delta","sum_deltaBucket","user"],"dataSource":{"type":"query","query":{"queryType":"scan","dataSource":{"type":"table","name":"A"},"columns":["AT"],"intervals":{"type":"intervals","intervals":["1980-06-12T22:30:00.000Z/2020-01-26T23:00:00.000Z"]}}},"filter":{"dimension":"countryName","extractionFn":{"locale":"","type":"lower"},"type":"selector","value":"france"},"intervals":{"type":"intervals","intervals":["1980-06-12T22:30:00.000Z/2020-01-26T23:00:00.000Z"]},"limit":10,"order":"descending","queryType":"scan"}`
//loadAndExecute(d, []byte(qry))
qry = `{"batchSize":20480,"columns":["__time","channel","cityName","comment","count","countryIsoCode","diffUrl","flags","isAnonymous","isMinor","isNew","isRobot","isUnpatrolled","metroCode","namespace","page","regionIsoCode","regionName","sum_added","sum_commentLength","sum_deleted","sum_delta","sum_deltaBucket","user"],"dataSource":{"type":"query","query":{"queryType":"scan","dataSource":{"type":"table","name":"A"},"columns":["AT"],"intervals":{"type":"intervals","intervals":["1980-06-12T22:30:00.000Z/2020-01-26T23:00:00.000Z"]}}},"filter":{"dimension":"countryName","extractionFn":{"locale":"","type":"lower"},"type":"selector","value":"france"},"intervals":{"type":"intervals","intervals":["1980-06-12T22:30:00.000Z/2020-01-26T23:00:00.000Z"]},"limit":10,"order":"none","queryType":"scan"}`
loadAndExecute(d, []byte(qry))

qry = `{"context":{"con":"text"},"query":"SELECT * \nFROM \"wikipedia\"\nWHERE \"countryName\" = 'France'\nLIMIT 10","queryType":"sql", "resultFormat":"array", "header": true}`
loadAndExecute(d, []byte(qry))
Expand Down
99 changes: 99 additions & 0 deletions examples/subqueryExample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"github.com/davecgh/go-spew/spew"
"github.com/grafadruid/go-druid"
"github.com/grafadruid/go-druid/builder"
"github.com/grafadruid/go-druid/builder/aggregation"
datasource2 "github.com/grafadruid/go-druid/builder/datasource"
"github.com/grafadruid/go-druid/builder/dimension"
"github.com/grafadruid/go-druid/builder/extractionfn"
"github.com/grafadruid/go-druid/builder/filter"
"github.com/grafadruid/go-druid/builder/granularity"
"github.com/grafadruid/go-druid/builder/intervals"
"github.com/grafadruid/go-druid/builder/query"
"github.com/grafadruid/go-druid/builder/topnmetric"
"log"
"time"
)

func main() {

d, err := druid.NewClient("http://localhost:8888")
if err != nil {
log.Fatal(err)
}

var results []map[string]interface{}

_, err = d.Query().Execute(MakeScanSubQuery(), &results)
if err != nil {
log.Fatal(err)
}
spew.Dump(results)

_, err = d.Query().Execute(MakeTimeSeriesSubQuery(), &results)
if err != nil {
log.Fatal(err)
}
spew.Dump(results)

}

// MakeScanSubQuery Result is nil
func MakeScanSubQuery() *query.Scan {
location, _ := time.LoadLocation("UTC")

start, _ := time.ParseInLocation(time.RFC3339Nano,
"1980-06-12T22:30:00.000Z",
location)
end, _ := time.ParseInLocation(time.RFC3339Nano,
"2023-01-26T23:00:00.000Z",
location)
i := intervals.NewInterval()
i.SetInterval(start, end)
interval := intervals.NewIntervals().SetIntervals([]*intervals.Interval{i})

selectorExtractionFn := extractionfn.NewLower().SetLocale("")
a := filter.NewSelector().SetDimension("countryName").SetValue("france").
SetExtractionFn(selectorExtractionFn)
query.NewSegmentMetadata().SetIntervals(interval)
datasourceSubQuery := query.NewScan().
SetDataSource(datasource2.NewTable().SetName("A")).SetColumns([]string{"AT"}).
SetIntervals(interval)
datasource := datasource2.NewQuery().SetQuery(datasourceSubQuery)
scan := query.NewScan()
queryTest := scan.SetLimit(10).SetIntervals(interval).SetFilter(a).
SetColumns([]string{"__time", "channel", "cityName", "comment", "count", "countryIsoCode",
"diffUrl", "flags", "isAnonymous", "isMinor", "isNew", "isRobot", "isUnpatrolled",
"metroCode", "namespace", "page", "regionIsoCode", "regionName", "sum_added",
"sum_commentLength", "sum_deleted", "sum_delta", "sum_deltaBucket", "user"}).
SetBatchSize(20480).
SetDataSource(datasource).SetOrder(query.None)
return queryTest
}

// MakeTimeSeriesSubQuery Results are timestamp 2022-10-07 ~ 13
func MakeTimeSeriesSubQuery() *query.Timeseries {
location, _ := time.LoadLocation("UTC")
start, _ := time.ParseInLocation(time.RFC3339,
"2022-10-07T00:00:00Z",
location)
i := intervals.NewInterval()
i.SetInterval(start, start.Add(time.Hour*24*7))
interval := intervals.NewIntervals().SetIntervals([]*intervals.Interval{i})
topN := query.NewTopN().
SetThreshold(100).
SetMetric(topnmetric.NewNumeric().SetMetric("count")).
SetIntervals(interval).
SetGranularity(granularity.NewSimple().SetGranularity(granularity.Day)).
SetDimension(dimension.NewDefault().SetDimension("string_value")).
SetDataSource(datasource2.NewTable().SetName("dc_94b4f5fdfde940979b79c50539d8322a_b42fde98efed4e638a0016b34b3c10cf_dataset_pre")).
SetAggregations([]builder.Aggregator{aggregation.NewLongSum().SetName("count").SetFieldName("count")}).
SetFilter(filter.NewAnd().SetFields([]builder.Filter{filter.NewSelector().SetDimension("_split_name_").SetValue("train"),
filter.NewSelector().SetDimension("column_name").SetValue("addressState")}))
subQuery := datasource2.NewQuery().SetQuery(topN)
queryTest := query.NewTimeseries().SetGranularity(granularity.NewSimple().SetGranularity(granularity.Day)).
SetIntervals(interval).SetDataSource(subQuery)
return queryTest
}
Loading