Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c3fe4d1
merge:
spiridonov Oct 6, 2025
605933c
math expressions
spiridonov Oct 7, 2025
3b5d6a4
merge
spiridonov Oct 7, 2025
7978ff5
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 7, 2025
e2e6e87
fix merge
spiridonov Oct 7, 2025
5a238b8
format
spiridonov Oct 7, 2025
d88a7e0
format
spiridonov Oct 7, 2025
a1bf376
memory leaks
spiridonov Oct 7, 2025
eedbfaf
lint
spiridonov Oct 7, 2025
36f3540
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 8, 2025
1ef3a95
merge
spiridonov Oct 8, 2025
49c5215
wip
spiridonov Oct 8, 2025
88892c7
revert go.mod
spiridonov Oct 8, 2025
a2147e1
revert go.mod
spiridonov Oct 8, 2025
a10228a
revert go.mod
spiridonov Oct 8, 2025
14265a2
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 9, 2025
ffce0da
merge
spiridonov Oct 9, 2025
357450a
release
spiridonov Oct 9, 2025
1ce7c78
comment
spiridonov Oct 9, 2025
75d4ab4
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 9, 2025
0ae79aa
Merge branch 'main' into spiridonov-rate-aggregate
spiridonov Oct 9, 2025
7c556fb
do.mod
spiridonov Oct 9, 2025
206ac82
Merge branch 'spiridonov-rate-aggregate' of github.com:grafana/loki i…
spiridonov Oct 9, 2025
ef1e804
do.mod
spiridonov Oct 9, 2025
a7181c2
semconv
spiridonov Oct 9, 2025
1f03fba
Merge branch 'main' into spiridonov-rate-aggregate
spiridonov Oct 9, 2025
dd74932
Merge branch 'main' into spiridonov-rate-aggregate
spiridonov Oct 9, 2025
fbad3d0
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 10, 2025
0c1453f
Merge branch 'spiridonov-rate-aggregate' of github.com:grafana/loki i…
spiridonov Oct 10, 2025
2fede5f
Merge branch 'main' into spiridonov-rate-aggregate
spiridonov Oct 10, 2025
de1851c
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 14, 2025
42219bb
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 14, 2025
efcb3e8
removed rate
spiridonov Oct 14, 2025
51e100e
Merge branch 'spiridonov-rate-aggregate' of github.com:grafana/loki i…
spiridonov Oct 14, 2025
f78d9f1
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 15, 2025
7cae847
pr comments
spiridonov Oct 15, 2025
a8b5c3c
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 15, 2025
87c1ee0
format
spiridonov Oct 15, 2025
3cce17d
Merge branch 'main' into spiridonov-rate-aggregate
spiridonov Oct 15, 2025
7216e04
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 16, 2025
f57498f
input columns
spiridonov Oct 16, 2025
32bcbc6
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 16, 2025
beebaba
Merge branch 'spiridonov-rate-aggregate' of github.com:grafana/loki i…
spiridonov Oct 16, 2025
bba1e87
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 17, 2025
23f20c8
separate join and math expressions
spiridonov Oct 17, 2025
dec254e
Merge branch 'main' of github.com:grafana/loki into spiridonov-rate-a…
spiridonov Oct 17, 2025
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
14 changes: 14 additions & 0 deletions pkg/engine/internal/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (c *Context) execute(ctx context.Context, node physical.Node) Pipeline {
return tracePipeline("physical.VectorAggregation", c.executeVectorAggregation(ctx, n, inputs))
case *physical.ParseNode:
return tracePipeline("physical.ParseNode", c.executeParse(ctx, n, inputs))
case *physical.MathExpression:
return tracePipeline("physical.MathExpression", c.executeMathExpression(ctx, n, inputs))
case *physical.ColumnCompat:
return tracePipeline("physical.ColumnCompat", c.executeColumnCompat(ctx, n, inputs))
case *physical.Parallelize:
Expand Down Expand Up @@ -376,6 +378,18 @@ func (c *Context) executeParse(ctx context.Context, parse *physical.ParseNode, i
return NewParsePipeline(parse, inputs[0], allocator)
}

func (c *Context) executeMathExpression(ctx context.Context, expr *physical.MathExpression, inputs []Pipeline) Pipeline {
if len(inputs) == 0 {
return emptyPipeline()
}

if len(inputs) > 1 {
return errorPipeline(ctx, fmt.Errorf("math expression expects exactly one input, got %d", len(inputs)))
}

return NewMathExpressionPipeline(expr, inputs[0], c.evaluator)
}

func (c *Context) executeColumnCompat(ctx context.Context, compat *physical.ColumnCompat, inputs []Pipeline) Pipeline {
if len(inputs) == 0 {
return emptyPipeline()
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/internal/executor/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ var _ ColumnVector = (*Array)(nil)

// ToArray implements ColumnVector.
func (a *Array) ToArray() arrow.Array {
a.array.Retain()
// this should already has 1 ref counter
return a.array
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/engine/internal/executor/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ func TestEvaluateBinaryExpression(t *testing.T) {
func collectBooleanColumnVector(vec ColumnVector) []bool {
res := make([]bool, 0, vec.Len())
arr := vec.ToArray().(*array.Boolean)
defer arr.Release()
for i := range int(vec.Len()) {
res = append(res, arr.Value(i))
}
Expand Down Expand Up @@ -327,10 +326,8 @@ null,null,null`
colVec, err := e.eval(colExpr, record)
require.NoError(t, err)
require.IsType(t, &CoalesceVector{}, colVec)
defer colVec.Release()

arr := colVec.ToArray()
defer arr.Release()
require.IsType(t, &array.String{}, arr)
stringArr := arr.(*array.String)

Expand Down
2 changes: 0 additions & 2 deletions pkg/engine/internal/executor/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ func NewFilterPipeline(filter *physical.Filter, input Pipeline, evaluator expres
if err != nil {
return nil, err
}
defer vec.Release()

arr := vec.ToArray()
// boolean filters are only used for filtering; they're not returned
// and must be released
Expand Down
142 changes: 100 additions & 42 deletions pkg/engine/internal/executor/functions.go

Large diffs are not rendered by default.

29 changes: 26 additions & 3 deletions pkg/engine/internal/executor/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func createFloat64Array(mem memory.Allocator, values []float64, nulls []bool) *A
// Helper function to extract boolean values from result
func extractBoolValues(result ColumnVector) []bool {
arr := result.ToArray().(*array.Boolean)
defer arr.Release()

values := make([]bool, arr.Len())
for i := 0; i < arr.Len(); i++ {
Expand Down Expand Up @@ -182,10 +181,34 @@ func TestBinaryFunctionRegistry_GetForSignature(t *testing.T) {
dataType: arrow.BinaryTypes.String,
expectError: false,
},
{
name: "valid div operation",
op: types.BinaryOpDiv,
dataType: arrow.PrimitiveTypes.Float64,
expectError: false,
},
{
name: "valid add operation",
op: types.BinaryOpAdd,
dataType: arrow.PrimitiveTypes.Float64,
expectError: false,
},
{
name: "valid Mul operation",
op: types.BinaryOpMul,
dataType: arrow.PrimitiveTypes.Float64,
expectError: false,
},
{
name: "valid sub operation",
op: types.BinaryOpSub,
dataType: arrow.PrimitiveTypes.Float64,
expectError: false,
},
{
name: "invalid operation",
op: types.BinaryOpAdd, // Not registered
dataType: arrow.PrimitiveTypes.Int64,
op: types.BinaryOpAnd, // Not registered
dataType: arrow.FixedWidthTypes.Boolean,
expectError: true,
},
{
Expand Down
81 changes: 81 additions & 0 deletions pkg/engine/internal/executor/math_expression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package executor

import (
"context"
"fmt"
"slices"

"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"

"github.com/grafana/loki/v3/pkg/engine/internal/types"

"github.com/grafana/loki/v3/pkg/engine/internal/semconv"

"github.com/grafana/loki/v3/pkg/engine/internal/planner/physical"
)

func NewMathExpressionPipeline(expr *physical.MathExpression, input Pipeline, evaluator expressionEvaluator) *GenericPipeline {
return newGenericPipeline(func(ctx context.Context, inputs []Pipeline) (arrow.Record, error) {
input := inputs[0]
batch, err := input.Read(ctx)
if err != nil {
return nil, err
}
defer batch.Release()

res, err := evaluator.eval(expr.Expression, batch)
if err != nil {
return nil, err
}
data := res.ToArray()
if data.DataType().ID() != arrow.FLOAT64 {
return nil, fmt.Errorf("expression returned non-float64 type %s", data.DataType())
}
valCol := data.(*array.Float64)
defer valCol.Release()

// Build output by moving all columns from the input except columns that were used for expression evaluation
// and add the result `value` column. In case of simple math expressions this will replace `value` with `value`.
// In case of math expressions that go after Joins this will replace `value_left` and `value_right` with `value`.
allColumnRefs := getAllColumnRefs(expr.Expression)
schema := batch.Schema()
outputFields := make([]arrow.Field, 0, schema.NumFields()+1)
outputCols := make([]arrow.Array, 0, schema.NumFields()+1)
for i := 0; i < schema.NumFields(); i++ {
if !slices.ContainsFunc(allColumnRefs, func(ref *types.ColumnRef) bool {
ident, err := semconv.ParseFQN(schema.Field(i).Name)
if err != nil {
return false
}
return ref.Column == ident.ShortName()
}) {
outputFields = append(outputFields, schema.Field(i))
outputCols = append(outputCols, batch.Column(i))
}
}

// Add `values` column
outputFields = append(outputFields, semconv.FieldFromIdent(semconv.ColumnIdentValue, false))
outputCols = append(outputCols, valCol)

outputSchema := arrow.NewSchema(outputFields, nil)
evaluatedRecord := array.NewRecord(outputSchema, outputCols, batch.NumRows())

return evaluatedRecord, nil
}, input)
}

// getAllColumnRefs finds all column ref from the expression. Should 1 or 2, but might be deep in the expression tree.
func getAllColumnRefs(expr physical.Expression) []*types.ColumnRef {
switch expr := expr.(type) {
case *physical.BinaryExpr:
return append(getAllColumnRefs(expr.Left), getAllColumnRefs(expr.Right)...)
case *physical.UnaryExpr:
return getAllColumnRefs(expr.Left)
case *physical.ColumnExpr:
return []*types.ColumnRef{&expr.Ref}
}

return nil
}
Loading