Skip to content

[pkg/ottl]: add bitwise AND (&) and OR (|) operators#46906

Open
57Ajay wants to merge 1 commit intoopen-telemetry:mainfrom
57Ajay:ottl-bitwise-operators
Open

[pkg/ottl]: add bitwise AND (&) and OR (|) operators#46906
57Ajay wants to merge 1 commit intoopen-telemetry:mainfrom
57Ajay:ottl-bitwise-operators

Conversation

@57Ajay
Copy link
Copy Markdown
Contributor

@57Ajay 57Ajay commented Mar 14, 2026

Description

This PR adds bitwise AND (&) and OR (|) as native operators in OTTL math expressions, addressing the first part of #45560.

These operators enable bitwise flag manipulation directly in OTTL statements without requiring custom converter functions:

transform:
  log_statements:
    - context: log
      statements:
        - set(attributes["masked"], attributes["flags"] & 0xFF)
        - set(attributes["combined"], attributes["a"] | attributes["b"])

Implementation

Bitwise operators are implemented as grammar-level operators, following the same architecture as the existing arithmetic operators (+, -, *, /).

Grammar changes (grammar.go):

  • Added OpBitwiseAnd and OpBitwiseOr lexer tokens
  • Added bitwiseAndTerm, opBitwiseAndTerm, bitwiseOrTerm, opBitwiseOrTerm AST nodes
  • Rewired mathExpression to route through the new precedence levels
  • Updated value lookahead to include new tokens

Evaluation changes (math.go):

  • Added evaluateBitwiseOrTerm and evaluateBitwiseAndTerm evaluation functions
  • Added performBitwiseOp for int64-only bitwise execution
  • Bitwise ops on non-int64 operands return a clear error

Operator precedence (highest → lowest):

Precedence Operators Description
1 * / Multiplication, Division
2 + - Addition, Subtraction
3 & Bitwise AND
4 | Bitwise OR

This matches the precedence in Go, C, Java, and Python.

Type constraints

  • Operands must be int64. Using float64, time.Time, or time.Duration returns an error: "bitwise operations require int64 operands".

Testing

  • Unit tests (math_test.go): 11 new passing tests covering AND, OR, precedence, paths, parentheses, mixed arithmetic+bitwise
  • Error tests (math_test.go): 2 new tests verifying float operand rejection
  • Lexer tests (lexer_test.go): 3 new tests for &, | tokens and combined expressions
  • Parser tests (parser_test.go): existing ASTs updated for new grammar structure
  • E2E tests (e2e/e2e_test.go): 5 new integration tests through the full parser --> evaluator pipeline
  • Manual verification: built otelcontribcol, tested with transform processor + OTLP HTTP receiver + debug exporter, all 6 expressions produced correct results

Scope

This PR implements & and | only. Remaining bitwise operators (^, ~, <<, >>) can follow in subsequent PRs once this approach is validated by code owners.

Copy link
Copy Markdown
Member

@florianl florianl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works fine here and enables interesting options

Signed-off-by: 57Ajay <57ajay.u@gmail.com>
@57Ajay 57Ajay force-pushed the ottl-bitwise-operators branch from 0b6d66e to 617331f Compare March 30, 2026 15:39
@github-actions
Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Apr 14, 2026
@57Ajay
Copy link
Copy Markdown
Contributor Author

57Ajay commented Apr 14, 2026

Hey, @edmocosta @TylerHelmuth @evan-bradley @bogdandrutu, a gentle reminder if you have some time can you please review this PR. I would love to hear your feedback on this..

@github-actions github-actions bot removed the Stale label Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants