Skip to content

Rule engine, resolves #96#97

Merged
jeroenrinzema merged 2 commits intomainfrom
feat/rule-engine
Dec 4, 2025
Merged

Rule engine, resolves #96#97
jeroenrinzema merged 2 commits intomainfrom
feat/rule-engine

Conversation

@jeroenrinzema
Copy link
Copy Markdown
Contributor

This pull request introduces a new SQL query builder system for user and event segmentation rules in the Nexus service. The changes add a robust, type-safe way to define, compose, and generate PostgreSQL queries from a tree of rule definitions, supporting complex user and event attribute filtering, frequency/event-based rules, and JSONB path access. The system is modular, with clear separation for rule types, operators, and query construction logic.

Core Rule Model and Types:

  • Introduced a comprehensive rules package defining Rule, RuleSet, Frequency, Period, and all supported operators, types, and groups, with methods for SQL type mapping and rule tree navigation.

Query Builder Architecture:

  • Added the QueryBuilder struct with methods to generate SQL and arguments from a RuleSet, supporting both full SELECT queries and WHERE clause fragments.

Rule Parsing and SQL Construction:

  • Implemented recursive rule parsing in rule.go, including logic for logical wrappers (AND/OR), user attribute rules, and flexible JSONB/dot/bracket path resolution for nested attributes, with correct PostgreSQL type casting.
  • Provided robust path parsing using regular expressions to support both dot and bracket notation for JSONB columns.

Comparison and Event Rule Handling:

  • Added a flexible comparison builder supporting all defined operators (equality, existence, string, array, date, etc.), with special handling for arrays and type casting.
  • Implemented event rule SQL generation, including support for event attribute filters, project scoping, and frequency-based event conditions with rolling time window logic.

These changes lay the foundation for advanced, composable rule-based segmentation with strong type safety and SQL correctness.

References:
[1] [2] [3] [4] [5]

@jeroenrinzema jeroenrinzema requested a review from Copilot December 2, 2025 23:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a comprehensive SQL query builder system for the Nexus service, enabling type-safe construction of PostgreSQL queries from rule definitions. The implementation supports user attribute filtering, event-based rules, frequency conditions, and complex JSONB path navigation with proper type casting.

Key Changes

  • Added a rule model with type definitions, operators, and tree navigation methods
  • Implemented a query builder that recursively constructs SQL from rule trees
  • Added support for nested JSONB paths using dot and bracket notation

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 16 comments.

Show a summary per file
File Description
services/nexus/internal/rules/rules.go Defines core rule types, operators, and period configurations with SQL mapping methods
services/nexus/internal/rules/query/query.go Implements the QueryBuilder for generating SELECT queries and WHERE clauses
services/nexus/internal/rules/query/rule.go Handles recursive rule parsing and JSONB path resolution for nested attributes
services/nexus/internal/rules/query/event.go Builds event-based SQL with frequency conditions and time window logic
services/nexus/internal/rules/query/comparison.go Constructs SQL comparison expressions for various operators and types
services/nexus/internal/rules/query/query_test.go Comprehensive test coverage for all query builder functionality

Comment thread services/nexus/internal/rules/query/comparison.go Outdated
Comment thread services/nexus/internal/rules/query/comparison.go Outdated
Comment thread services/nexus/internal/rules/query/rule.go Outdated
Comment thread services/nexus/internal/rules/query/query_test.go
Comment thread services/nexus/internal/rules/query/query_test.go
Comment thread services/nexus/internal/rules/query/query_test.go
Comment thread services/nexus/internal/rules/query/query_test.go
Comment thread services/nexus/internal/rules/query/query_test.go
Comment thread services/nexus/internal/rules/query/query_test.go
Comment thread services/nexus/internal/rules/query/query_test.go
@jeroenrinzema jeroenrinzema merged commit 0a8c0a4 into main Dec 4, 2025
3 checks passed
@jeroenrinzema jeroenrinzema deleted the feat/rule-engine branch December 4, 2025 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants