Skip to content

Commit

Permalink
feat: remove unnessary extraction operation for unnest (#486)
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <shoothzj@gmail.com>
  • Loading branch information
shoothzj committed Feb 24, 2024
1 parent de5505d commit 5eb0377
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 113 deletions.
43 changes: 0 additions & 43 deletions engine/immutable/logstore/unnest_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,6 @@ import (
"github.com/openGemini/openGemini/lib/util/lifted/vm/protoparser/influx"
)

func TestUnnestMatchAll(t *testing.T) {
unnest := &influxql.Unnest{
Expr: &influxql.Call{
Name: "match_all",
Args: []influxql.Expr{&influxql.VarRef{Val: "location:([a-z]+)"}, &influxql.VarRef{Val: "content", Type: influxql.String}},
},
Aliases: []string{"key1"},
DstType: []influxql.DataType{influxql.String},
}
match, _ := NewUnnestMatchAll(unnest)
result := match.Get("location:test")
v, ok := result["key1"]
if !ok || v != "test" {
t.Fatal("get result error")
}
result = match.Get("type:test")
v, ok = result["key1"]
if !ok || v != "" {
t.Fatal("get nil result error")
}

matchNil, _ := NewUnnestMatchAll(&influxql.Unnest{
Expr: &influxql.VarRef{},
Aliases: []string{"key1"},
DstType: []influxql.DataType{influxql.String},
})
if matchNil != nil {
t.Fatal("get nil match error")
}

_, err := NewUnnestMatchAll(&influxql.Unnest{
Expr: &influxql.Call{
Name: "match_all",
Args: []influxql.Expr{&influxql.VarRef{Val: "* | EXTRACT(content:\"type:(a-z:0-9]+\") AS(key1) | select count (key1) group by key1"}, &influxql.VarRef{Val: "content", Type: influxql.String}},
},
Aliases: []string{"key1"},
DstType: []influxql.DataType{influxql.String},
})
if err == nil {
t.Fatal("get err match")
}
}

func TestUnnestMatchAllOperator(t *testing.T) {
schema := record.Schemas{
record.Field{Type: influx.Field_Type_String, Name: "content"},
Expand Down
44 changes: 0 additions & 44 deletions engine/immutable/unnest_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,6 @@ limitations under the License.

package immutable

import (
"regexp"

"github.com/openGemini/openGemini/lib/util/lifted/influx/influxql"
)

type UnnestMatch interface {
Get([][]byte) [][]byte
}

type UnnestMatchAll struct {
unnest *influxql.Unnest
re *regexp.Regexp
}

func NewUnnestMatchAll(unnest *influxql.Unnest) (*UnnestMatchAll, error) {
unnestExpr, ok := unnest.Expr.(*influxql.Call)
if !ok {
return nil, nil
}
var err error
reg, err := regexp.Compile(unnestExpr.Args[0].(*influxql.VarRef).Val)
if err != nil {
return nil, err
}
r := &UnnestMatchAll{
unnest: unnest,
re: reg,
}

return r, nil
}

func (r *UnnestMatchAll) Get(s string) map[string]string {
result := make(map[string]string)
for _, v := range r.unnest.Aliases {
result[v] = ""
}
matchesAll := r.re.FindStringSubmatch(s)
if matchesAll == nil {
return result
}
for k, v := range r.unnest.Aliases {
result[v] = matchesAll[k+1]
}
return result
}
32 changes: 6 additions & 26 deletions lib/util/lifted/influx/httpd/handler_logstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

"github.com/influxdata/influxdb/query"
"github.com/influxdata/influxdb/uuid"
logstore2 "github.com/openGemini/openGemini/engine/immutable/logstore"
"github.com/openGemini/openGemini/lib/bufferpool"
"github.com/openGemini/openGemini/lib/config"
"github.com/openGemini/openGemini/lib/cpu"
Expand Down Expand Up @@ -1996,26 +1995,9 @@ func (h *Handler) getQueryLogResult(resp *Response, logCond *influxql.Query, par
var count int64
var logs []map[string]interface{}
var unnest *influxql.Unnest
var unnestField string
if logCond != nil {
unnest = logCond.Statements[0].(*influxql.LogPipeStatement).Unnest
}
var unnestFunc *logstore2.UnnestMatchAll
if unnest != nil {
unnestExpr, ok := unnest.Expr.(*influxql.Call)
if !ok {
return 0, nil, fmt.Errorf("the type of unnest expr error")
}
unnestField = unnestExpr.Args[1].(*influxql.VarRef).Val
if unnestField == TAGS {
unnestField = Tag
}
var err error
unnestFunc, err = logstore2.NewUnnestMatchAll(unnest)
if err != nil {
return 0, nil, err
}
}
for i := range resp.Results {
for _, s := range resp.Results[i].Series {
for j := range s.Values {
Expand All @@ -2032,12 +2014,6 @@ func (h *Handler) getQueryLogResult(resp *Response, logCond *influxql.Query, par
}
continue
}
if unnest != nil && c == unnestField {
unnestResult := unnestFunc.Get(s.Values[j][id].(string))
for k, v := range unnestResult {
rec[k] = v
}
}
switch c {
case TIME:
v, _ := s.Values[j][id].(time.Time)
Expand All @@ -2049,8 +2025,12 @@ func (h *Handler) getQueryLogResult(resp *Response, logCond *influxql.Query, par
case CONTENT:
h.setRecord(rec, CONTENT, s.Values[j][id], para.Truncate)
default:
hasMore = true
h.setRecord(recMore, c, s.Values[j][id], para.Truncate)
if unnest != nil && unnest.IsUnnestField(c) {
h.setRecord(rec, c, s.Values[j][id], para.Truncate)
} else {
hasMore = true
h.setRecord(recMore, c, s.Values[j][id], para.Truncate)
}
}
}
rec[Cursor] = base64.StdEncoding.EncodeToString([]byte(strconv.FormatInt(currT, 10) + "|" + strconv.FormatInt(currSeqID, 10) + "^^"))
Expand Down
9 changes: 9 additions & 0 deletions lib/util/lifted/influx/influxql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -7693,6 +7693,15 @@ func (u *Unnest) Clone() *Unnest {
return clone
}

func (u *Unnest) IsUnnestField(field string) bool {
for _, alias := range u.Aliases {
if alias == field {
return true
}
}
return false
}

func (u *Unnest) GetName() string {
return ""
}
Expand Down

0 comments on commit 5eb0377

Please sign in to comment.