From b7d8f36febda69ebc657906754ce1d2c5b4dfb54 Mon Sep 17 00:00:00 2001 From: saketbairoliya2 Date: Wed, 16 Nov 2022 12:04:49 +0530 Subject: [PATCH 1/2] Added new changes for aggregations. --- builder/aggregation/aggregation.go | 2 + builder/aggregation/expression.go | 69 ++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 builder/aggregation/expression.go diff --git a/builder/aggregation/aggregation.go b/builder/aggregation/aggregation.go index a9ec226..7341273 100644 --- a/builder/aggregation/aggregation.go +++ b/builder/aggregation/aggregation.go @@ -108,6 +108,8 @@ func Load(data []byte) (builder.Aggregator, error) { a = NewQuantilesDoublesSketch() case "thetaSketch": a = NewThetaSketch() + case "expression": + a = NewExpression() default: return nil, errors.New("unsupported aggregation type") } diff --git a/builder/aggregation/expression.go b/builder/aggregation/expression.go new file mode 100644 index 0000000..ed9f5e0 --- /dev/null +++ b/builder/aggregation/expression.go @@ -0,0 +1,69 @@ +package aggregation + +// Expression holds the expression type of aggregator. +// TODO: Implement the backend changes. +type Expression struct { + Base + FieldsName []string `json:"fields,omitempty"` + AccumulatorIdentifier string `json:"accumulatorIdentifier,omitempty"` + InitialValue string `json:"initialValue,omitempty"` + InitialCombineValue string `json:"initialCombineValue,omitempty"` + Fold string `json:"fold,omitempty"` + Combine string `json:"combine,omitempty"` + MaxSizeBytes int64 `json:"maxSizeBytes,omitempty"` +} + +// NewExpression create a new instance of Expression +func NewExpression() *Expression { + t := &Expression{} + t.Base.SetType("expression") + return t +} + +// SetName set name +func (t *Expression) SetName(name string) *Expression { + t.Base.SetName(name) + return t +} + +// SetFieldName set fields +func (t *Expression) SetFieldsName(fieldsName []string) *Expression { + t.FieldsName = fieldsName + return t +} + +// SetAccumulatorIdentifier set accumulatorIdentifier +func (t *Expression) SetAccumulatorIdentifier(identifier string) *Expression { + t.AccumulatorIdentifier = identifier + return t +} + +// SetInitialValue set initialValue +func (t *Expression) SetInitialValue(value string) *Expression { + t.InitialValue = value + return t +} + +// SetInitialCombineValue set initialCombineValue +func (t *Expression) SetInitialCombineValue(value string) *Expression { + t.InitialCombineValue = value + return t +} + +// SetFold set fold +func (t *Expression) SetFold(value string) *Expression { + t.Fold = value + return t +} + +// SetCombine set combine +func (t *Expression) SetCombine(value string) *Expression { + t.Combine = value + return t +} + +// SetMaxSizeBytes set caxSizeBytes +func (t *Expression) SetMaxSizeBytes(size int64) *Expression { + t.MaxSizeBytes = size + return t +} From 9c78a90c7a4b0704b4538d4649d839b5f8ede496 Mon Sep 17 00:00:00 2001 From: saketbairoliya2 Date: Tue, 4 Jul 2023 18:39:18 +0530 Subject: [PATCH 2/2] Added a function to by pass the Load function and directly use REST based implementation. --- druid.go | 29 ++++++++++++++++++++++++++ go.mod | 10 +++++---- go.sum | 63 ++++++++++++++++++++++++-------------------------------- query.go | 14 +++++++++++++ 4 files changed, 76 insertions(+), 40 deletions(-) mode change 100644 => 100755 go.mod diff --git a/druid.go b/druid.go index 4a76e71..d723976 100644 --- a/druid.go +++ b/druid.go @@ -1,6 +1,7 @@ package druid import ( + "bytes" "context" "crypto/tls" "crypto/x509" @@ -158,6 +159,34 @@ func (c *Client) NewRequest(method, path string, opt interface{}) (*retryablehtt return r, nil } +func (c *Client) NewRequestOnMarshalledRequest(method, path string, jsonData []byte) (*retryablehttp.Request, error) { + u := *c.baseURL + unescaped, err := url.PathUnescape(path) + if err != nil { + return nil, err + } + + u.RawPath = c.baseURL.Path + path + u.Path = c.baseURL.Path + unescaped + + reqHeaders := make(http.Header) + reqHeaders.Set("Accept", "application/json") + + if method == "POST" || method == "PUT" { + reqHeaders.Set("Content-Type", "application/json") + } + + r, err := retryablehttp.NewRequest(method, u.String(), bytes.NewBuffer(jsonData)) + if err != nil { + return nil, err + } + r.Header = reqHeaders + if c.basicAuth { + r.SetBasicAuth(c.username, c.password) + } + return r, nil +} + func (c *Client) Do(r *retryablehttp.Request, result interface{}) (*Response, error) { resp, err := c.http.Do(r) if err != nil { diff --git a/go.mod b/go.mod old mode 100644 new mode 100755 index 53facbf..1143650 --- a/go.mod +++ b/go.mod @@ -4,8 +4,10 @@ go 1.14 require ( github.com/davecgh/go-spew v1.1.1 - github.com/google/go-querystring v1.0.0 - github.com/hashicorp/go-retryablehttp v0.6.7 - github.com/magefile/mage v1.11.0 - github.com/stretchr/testify v1.2.2 + github.com/google/go-querystring v1.1.0 + github.com/hashicorp/go-cleanhttp v0.5.2 // indirect + github.com/hashicorp/go-retryablehttp v0.7.1 + github.com/magefile/mage v1.14.0 + github.com/stretchr/testify v1.8.1 + github.com/fatih/structtag v1.2.0 ) diff --git a/go.sum b/go.sum index 93813bb..7ea5900 100644 --- a/go.sum +++ b/go.sum @@ -1,44 +1,35 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= -github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= -github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM= +github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4= +github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= +github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= +github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI= github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ= -github.com/hashicorp/go-retryablehttp v0.6.7 h1:8/CAEZt/+F7kR7GevNHulKkUjLht3CPmn7egmhieNKo= -github.com/hashicorp/go-retryablehttp v0.6.7/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= -github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= -github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ= +github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY= +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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 h1:2M3HP5CCK1Si9FQhwnzYhXdG6DXeebvUHFpre8QvbyI= -golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY= -golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/query.go b/query.go index 5997190..f8e5a97 100644 --- a/query.go +++ b/query.go @@ -40,3 +40,17 @@ func (q *QueryService) Execute(qry builder.Query, result interface{}) (*Response func (q *QueryService) Load(data []byte) (builder.Query, error) { return query.Load(data) } + +// TODO: (Add support for SQL query type, currently only REST path supported. ) +func (q *QueryService) ExecuteRaw(requestBody []byte, result interface{}) (*Response, error) { + path := NativeQueryEndpoint + r, err := q.client.NewRequestOnMarshalledRequest("POST", path, requestBody) + if err != nil { + return nil, err + } + resp, err := q.client.Do(r, result) + if err != nil { + return nil, err + } + return resp, nil +}