Skip to content

Commit

Permalink
feat: querier uniq function supports multi params
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaochaoren1 committed Sep 25, 2024
1 parent 581ca82 commit 6f1bb32
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
53 changes: 51 additions & 2 deletions server/querier/engine/clickhouse/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func GetAggFunc(name string, args []string, alias string, derivativeArgs []strin
derivativeGroupBy := e.DerivativeGroupBy
if name == view.FUNCTION_TOPK || name == view.FUNCTION_ANY {
return GetTopKTrans(name, args, alias, e)
} else if name == view.FUNCTION_UNIQ || name == view.FUNCTION_UNIQ_EXACT {
return GetUniqTrans(name, args, alias, e)
}

var levelFlag int
Expand Down Expand Up @@ -242,6 +244,53 @@ func GetTopKTrans(name string, args []string, alias string, e *CHEngine) (Statem
}, levelFlag, unit, nil
}

func GetUniqTrans(name string, args []string, alias string, e *CHEngine) (Statement, int, string, error) {
db := e.DB
fields := args

function, ok := metrics.METRICS_FUNCTIONS_MAP[name]
if !ok {
return nil, 0, "", nil
}

levelFlag := view.MODEL_METRICS_LEVEL_FLAG_UNLAY
fieldsLen := len(fields)
dbFields := make([]string, fieldsLen)

var metricStruct *metrics.Metrics
for i, field := range fields {
field = strings.Trim(field, "`")
metricStruct, ok = metrics.GetAggMetrics(field, e.DB, e.Table, e.ORGID)
if !ok || metricStruct.Type == metrics.METRICS_TYPE_ARRAY {
return nil, 0, "", nil
}
dbFields[i] = metricStruct.DBField

// judge whether the operator supports single layer
if levelFlag == view.MODEL_METRICS_LEVEL_FLAG_UNLAY && db != chCommon.DB_NAME_FLOW_LOG {
unlayFuns := metrics.METRICS_TYPE_UNLAY_FUNCTIONS[metricStruct.Type]
if !common.IsValueInSliceString(name, unlayFuns) {
levelFlag = view.MODEL_METRICS_LEVEL_FLAG_LAYERED
}
}
}

metricStructCopy := *metricStruct
metricStructCopy.DBField = strings.Join(dbFields, ", ")
if fieldsLen > 1 {
metricStructCopy.DBField = "(" + metricStructCopy.DBField + ")"
}

unit := strings.ReplaceAll(function.UnitOverwrite, "$unit", metricStruct.Unit)

return &AggFunction{
Metrics: &metricStructCopy,
Name: name,
Args: args,
Alias: alias,
}, levelFlag, unit, nil
}

func GetBinaryFunc(name string, args []Function) (*BinaryFunction, error) {
return &BinaryFunction{
Name: name,
Expand Down Expand Up @@ -915,8 +964,8 @@ func (f *TagFunction) Trans(m *view.Model) view.Node {
if len(fields) > 1 {
if f.Name == "if" {
withValue = fmt.Sprintf("%s(%s)", f.Name, strings.Join(values, ","))
} else if strings.HasPrefix(f.Name, "topK") || strings.HasPrefix(f.Name, "any") {
withValue = fmt.Sprintf("%s((%s))", f.Name, strings.Join(values, ","))
} else if strings.HasPrefix(f.Name, "topK") || strings.HasPrefix(f.Name, "any") || strings.HasPrefix(f.Name, "uniq") {
withValue = fmt.Sprintf("%s(%s)", f.Name, strings.Join(values, ","))
} else {
withValue = fmt.Sprintf("%s([%s])", f.Name, strings.Join(values, ","))
}
Expand Down
2 changes: 1 addition & 1 deletion server/querier/engine/clickhouse/view/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (f *DefaultFunction) WriteTo(buf *bytes.Buffer) {
args := f.Args
if f.Name == FUNCTION_TOPK {
args = f.Args[len(f.Args)-1:]
} else if f.Name == FUNCTION_ANY {
} else if f.Name == FUNCTION_ANY || f.Name == FUNCTION_UNIQ || f.Name == FUNCTION_UNIQ_EXACT {
args = nil
}
if len(args) > 0 {
Expand Down

0 comments on commit 6f1bb32

Please sign in to comment.