Repository navigation
refactor(streaming): resolve customer_id with map - #3259
Conversation
📝 WalkthroughWalkthroughDecouples subject and customer filters, replaces CASE-based subject→customer_id with a WITH map, and shifts helper functions to mutate the sqlbuilder.SelectBuilder directly; applies these changes across event and meter ClickHouse query builders and their tests. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
1995d33 to
3add88c
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
openmeter/streaming/clickhouse/queryhelper.go (2)
19-30: Consider validating empty subject keys.While the code handles customers with subject keys correctly, it might be worth adding validation or filtering for customers with empty SubjectKeys arrays to avoid unnecessary map entries.
for _, customer := range customers { + if len(customer.GetUsageAttribution().SubjectKeys) == 0 { + continue + } // Add each subject key to the map and map it to the customer id for _, subjectKey := range customer.GetUsageAttribution().SubjectKeys { subjectSQL := fmt.Sprintf("'%s'", sqlbuilder.Escape(subjectKey)) customerIDSQL := fmt.Sprintf("'%s'", sqlbuilder.Escape(customer.GetUsageAttribution().ID)) values = append(values, subjectSQL, customerIDSQL) } }
32-37: Consider handling edge case of empty customer list.When
customersis empty, the map will be created with no entries. While this should work, it might be cleaner to skip the WITH clause entirely in such cases.+// Return early if no customers provided +if len(customers) == 0 { + return query +} + mapAs := "subject_to_customer_id" mapSQL := fmt.Sprintf("WITH map(%s) as %s", strings.Join(values, ", "), mapAs)openmeter/streaming/clickhouse/event_query.go (2)
161-163: Use the same qualified tableName here as well; also confirm count-estimate intent with customersWhere.For consistency with
FROM, passtableNameinto both helpers. Additionally, the comment abovetoCountRowSQLmentions filtering only by columns in table order for estimation efficiency. EnsurecustomersWhere(...)constrainssubject(which is in the ORDER BY tuple) rather than introducing computed predicates that could hurt the estimate performance.Apply this diff:
- query = subjectWhere(d.EventsTableName, subjects, query) - query = customersWhere(d.EventsTableName, customers, query) + query = subjectWhere(tableName, subjects, query) + query = customersWhere(tableName, customers, query)You can quickly verify the generated WHERE parts for both functions using the existing tests or by logging the built SQL for representative inputs (subject-only, customers-only, and both).
97-99: Use the sametableNamein helper filters to keep column qualifications in syncPassing
d.EventsTableNametosubjectWhere/customersWherecan misqualify columns when theFROMclause uses a different identifier (e.g. fully-qualified name or alias). Since both helpers build column references viacolumnFactory(eventsTableName), it’s safer to pass the exacttableNameused inFROM.• File:
openmeter/streaming/clickhouse/event_query.go
Location: around lines 97–99Suggested refactor (optional):
- query = subjectWhere(d.EventsTableName, subjects, query) - query = customersWhere(d.EventsTableName, customers, query) + query = subjectWhere(tableName, subjects, query) + query = customersWhere(tableName, customers, query)This ensures that columns in filters always align with the table identifier in the
FROMclause.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (6)
openmeter/streaming/clickhouse/event_query.go(3 hunks)openmeter/streaming/clickhouse/event_query_v2.go(3 hunks)openmeter/streaming/clickhouse/event_query_v2_test.go(1 hunks)openmeter/streaming/clickhouse/meter_query.go(2 hunks)openmeter/streaming/clickhouse/meter_query_test.go(2 hunks)openmeter/streaming/clickhouse/queryhelper.go(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
openmeter/streaming/clickhouse/queryhelper.go (3)
api/client/javascript/src/client/schemas.ts (1)
Customer(10856-10856)api/api.gen.go (1)
Customer(2165-2215)api/client/go/client.gen.go (1)
Customer(1995-2045)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
- GitHub Check: Artifacts / Container image
- GitHub Check: Migration Checks
- GitHub Check: Lint
- GitHub Check: Test
- GitHub Check: Code Generators
- GitHub Check: Build
- GitHub Check: Analyze (go)
🔇 Additional comments (11)
openmeter/streaming/clickhouse/event_query_v2_test.go (1)
150-150: LGTM! Map-based customer_id derivation is more efficient.The change from CASE-based to map-based customer_id derivation is a solid improvement. The map approach simplifies the SQL and should perform better, especially with many subject-to-customer mappings.
openmeter/streaming/clickhouse/meter_query.go (2)
200-204: Good refactoring: customer_id selection moved to query mutation phase.Moving the customer_id column selection from the pre-built selectColumns array to a direct query mutation is cleaner and more consistent with the new helper pattern.
267-268: Consistent use of top-level filter helpers.The migration from method-based filtering to top-level helper functions (
customersWhereandsubjectWhere) improves code organization and reusability across different query builders.openmeter/streaming/clickhouse/meter_query_test.go (2)
294-294: Test correctly reflects the new map-based approach.The test expectation properly validates the new SQL generation with the WITH map clause for customer_id derivation.
363-364: Filter combination test is properly updated.The test correctly includes both subject filters in the WHERE clause when both FilterCustomer and FilterSubject are specified, maintaining the AND relationship between them.
openmeter/streaming/clickhouse/event_query_v2.go (3)
26-31: Clean column selection with conditional customer_id inclusion.The explicit column selection followed by conditional customer_id inclusion via
selectCustomerIdColumnis more readable than the previous dynamic array approach.
60-61: Consistent filter application pattern.Using
customersWhereto mutate the query directly aligns well with the overall refactoring pattern and eliminates the need for intermediate expression strings.
139-140: Count query correctly applies customer filter.The count query maintains consistency with the main query by using the same
customersWherehelper.openmeter/streaming/clickhouse/queryhelper.go (3)
13-43: Efficient map-based customer_id derivation implementation.The new implementation using ClickHouse's map function is elegant and efficient. The WITH clause approach properly isolates the mapping logic.
45-61: Clean separation of customer filtering logic.The
customersWherefunction properly encapsulates the customer filtering logic and returns the mutated query builder, following the fluent interface pattern.
63-81: Subject filtering correctly decoupled from customer filtering.The removal of the
customersparameter fromsubjectWhereproperly separates concerns, making the function's purpose clearer.
Refactor customer_id select.
Summary by CodeRabbit