Skip to content

LogQL Binary Operations and Vector Matching #16

@frisbeeman

Description

@frisbeeman

Overview

Implement LogQL binary operations between vectors, including arithmetic, comparison, and logical operators with vector matching modifiers.

Parent Issue

Part of #6 (Query Service Implementation), delegated from #8 (LogQL Query Compiler).

Context

Binary operations allow combining two vector expressions. Currently, all binary operations return NotImplemented error. Only scalar literals work in simple contexts.

Requirements

Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
^ Exponentiation

Comparison Operators

Operator Description
== Equal (filter or bool)
!= Not equal
> Greater than
>= Greater or equal
< Less than
<= Less or equal

Logical/Set Operators

Operator Description
and Intersection
or Union
unless Difference

Vector Matching Modifiers

Modifier Description
on(labels...) Match only on specified labels
ignoring(labels...) Ignore specified labels when matching
group_left(labels...) Many-to-one matching
group_right(labels...) One-to-many matching

Example Queries

rate({job="app"}[5m]) * 60
rate({job="app"}[5m]) / rate({job="db"}[5m])
rate({job="app"}[5m]) > 100
sum by (service) (rate({job="app"}[5m])) / on(service) sum by (service) (rate({job="db"}[5m]))
vector1 and vector2
vector1 unless vector2

Implementation Approach

  1. Implement binary expression planning in planner.rs:

    • Handle BinOpExpr in query planning
    • Support vector-scalar and vector-vector operations
  2. Vector matching logic:

    • Default: match on all labels
    • on(): JOIN on specified labels only
    • ignoring(): JOIN on all labels except specified
    • group_left/right: handle one-to-many relationships
  3. Translation to DataFusion:

    • Arithmetic: col("left.value") + col("right.value")
    • Comparison: filter() or CASE WHEN for bool modifier
    • Logical: INNER/LEFT/ANTI JOINs on label columns

Acceptance Criteria

  • All arithmetic operators work between vectors and scalars
  • Comparison operators filter or return 0/1 with bool modifier
  • Logical operators perform correct set operations
  • on() and ignoring() modifiers work correctly
  • group_left() and group_right() handle cardinality correctly
  • Proper error messages for cardinality violations
  • Unit tests for each operator
  • Integration tests with complex expressions

Technical References

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions