Skip to content

feat(streaming): clickhouse query optimization #2974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hekike
Copy link
Contributor

@hekike hekike commented Jun 9, 2025

  • use prewhere to pre-filter rows before group by filter

Summary by CodeRabbit

  • New Features

    • Added support for custom meter query settings, allowing advanced configuration and optimization of queries.
    • Enhanced query performance by introducing PREWHERE clauses and SETTINGS for more efficient filtering and execution.
  • Bug Fixes

    • Improved query construction logic for more accurate and optimized time window handling.
  • Chores

    • Updated ClickHouse server image versions in deployment configurations for Kubernetes and Docker Compose.

Copy link

coderabbitai bot commented Jun 9, 2025

📝 Walkthrough

Walkthrough

This update introduces support for custom meter query settings in the ClickHouse streaming connector by extending configuration structs and propagating these settings through the query execution logic. The ClickHouse server image versions are updated across deployment files. Query construction logic is enhanced for optimized filtering and settings injection, with corresponding updates to related tests.

Changes

Files/Paths Change Summary
.dagger/versions_pinned.go
deploy/charts/openmeter/templates/clickhouse.yaml
docker-compose.yaml
examples/collectors/database/docker-compose.yaml
Updated ClickHouse server version strings in pinned versions, Kubernetes, and Docker Compose files.
app/config/aggregation.go
openmeter/streaming/clickhouse/connector.go
Added MeterQuerySettings field to configuration structs for meter-specific query settings.
app/common/streaming.go Passed MeterQuerySettings from config to ClickHouse streaming connector initialization.
openmeter/streaming/clickhouse/meter_query.go Enhanced query logic: added custom query settings, optimized JSON filtering, inserted PREWHERE and SETTINGS clauses, and improved time window handling.
openmeter/streaming/clickhouse/meter_query_test.go Updated test cases to reflect new SQL output including PREWHERE and SETTINGS clauses and revised time window logic.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@hekike hekike added the release-note/feature Release note: Exciting New Features label Jun 9, 2025
@hekike hekike changed the title Feat/clickhouse query optimize feat(streaming): clickhouse query optimization Jun 9, 2025
@hekike hekike marked this pull request as ready for review June 9, 2025 19:58
@hekike hekike requested a review from a team as a code owner June 9, 2025 19:58
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🔭 Outside diff range comments (1)
openmeter/streaming/clickhouse/meter_query.go (1)

230-283: 🛠️ Refactor suggestion

⚠️ Potential issue

The PREWHERE implementation uses fragile string manipulation.

While the optimization approach is correct, the string manipulation method (splitting SQL and replacing keywords) is fragile and could break if the SQL builder changes its output format.

Consider using a more robust approach:

  1. Option 1: Extend the SQL builder to support PREWHERE natively
  2. Option 2: Build the PREWHERE and WHERE clauses separately:
// Build PREWHERE conditions
prewhereBuilder := sqlbuilder.ClickHouse.NewSelectBuilder()
prewhereBuilder.Select(selectColumns...)
prewhereBuilder.From(tableName)
prewhereConditions := []string{
    fmt.Sprintf("%s = ?", getColumn("namespace")),
    fmt.Sprintf("%s = ?", getColumn("type")),
}

// Build WHERE conditions for JSON filters
whereConditions := []string{}
if len(d.FilterGroupBy) > 0 {
    // Add JSON_VALUE conditions to whereConditions
}

// Combine into final SQL
sql = fmt.Sprintf("SELECT ... FROM ... PREWHERE %s WHERE %s",
    strings.Join(prewhereConditions, " AND "),
    strings.Join(whereConditions, " AND "))
  1. Option 3: Add a comment explaining the string manipulation assumptions and add tests to catch any SQL builder format changes.
🧹 Nitpick comments (1)
openmeter/streaming/clickhouse/connector.go (1)

39-39: Consider adding validation for MeterQuerySettings values.

The MeterQuerySettings field is correctly added to propagate custom query settings. However, invalid settings could cause query failures at runtime.

Consider adding validation in the Validate() method to ensure the settings are valid ClickHouse query settings:

func (c Config) Validate() error {
	if c.Logger == nil {
		return fmt.Errorf("logger is required")
	}
+
+	// Validate MeterQuerySettings if provided
+	if c.MeterQuerySettings != nil {
+		// Example: validate known settings or check format
+		for key, value := range c.MeterQuerySettings {
+			// Ensure values don't contain SQL injection risks
+			if strings.ContainsAny(value, ";'\"") {
+				return fmt.Errorf("invalid character in meter query setting value for key %s", key)
+			}
+		}
+	}

	if c.ClickHouse == nil {
		return fmt.Errorf("clickhouse connection is required")
	}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 637e267 and e996241.

📒 Files selected for processing (9)
  • .dagger/versions_pinned.go (1 hunks)
  • app/common/streaming.go (1 hunks)
  • app/config/aggregation.go (1 hunks)
  • deploy/charts/openmeter/templates/clickhouse.yaml (1 hunks)
  • docker-compose.yaml (1 hunks)
  • examples/collectors/database/docker-compose.yaml (1 hunks)
  • openmeter/streaming/clickhouse/connector.go (2 hunks)
  • openmeter/streaming/clickhouse/meter_query.go (6 hunks)
  • openmeter/streaming/clickhouse/meter_query_test.go (14 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: CI
  • GitHub Check: Analyze (go)
🔇 Additional comments (9)
.dagger/versions_pinned.go (1)

3-7: Confirm ClickHouse version bump.

The clickhouseVersion constant has been updated from "24.5.5.78" to "24.10", matching the other deployment targets. Verify that this version matches your CI/CD expectations and is compatible with the features used in the streaming connector.

deploy/charts/openmeter/templates/clickhouse.yaml (1)

23-27: Align Kubernetes image with pinned version.

The ClickHouse image tag has been updated to clickhouse/clickhouse-server:24.10, which aligns with the pinned Go constant. Confirm that the non-alpine (glibc) variant is the intended runtime for your Kubernetes clusters.

examples/collectors/database/docker-compose.yaml (1)

49-53: Verify example ClickHouse Alpine tag.

The example uses clickhouse/clickhouse-server:24.0-alpine, diverging from the pinned 24.10 and main Compose 24.10-alpine. Please confirm if the example should be updated to 24.10-alpine for consistency or if this version difference is intentional.

docker-compose.yaml (1)

29-33: Approve Alpine image update.

The ClickHouse service has been bumped to clickhouse/clickhouse-server:24.10-alpine, aligning with the new version. Ensure any client defaults (e.g., default database name) continue to work with this image.

openmeter/streaming/clickhouse/connector.go (1)

209-209: LGTM!

The meter query settings are correctly propagated to the query execution.

app/common/streaming.go (1)

38-38: LGTM!

The meter query settings are correctly passed from the aggregation configuration to the ClickHouse connector.

openmeter/streaming/clickhouse/meter_query_test.go (1)

46-47: Test updates correctly reflect the query optimization changes.

The test expectations have been properly updated to verify:

  • The new time window calculation using toStartOfMinute for non-windowed queries
  • The addition of optimization SETTINGS clause to all queries
  • The PREWHERE clause implementation for JSON filtering

Also applies to: 65-66, 83-84, 102-103, 122-123, 143-144, 165-166, 188-189, 209-210, 230-231, 251-252, 271-272, 291-292, 311-312

openmeter/streaming/clickhouse/meter_query.go (2)

30-30: LGTM!

The QuerySettings field is correctly added to support custom query optimization settings.


145-149: Clarify the TODO comment timeline.

The TODO comment mentions removing the minute rounding but doesn't specify when this should happen. This could lead to the technical debt being forgotten.

Could you provide more context about:

  1. Why is the minute rounding needed currently?
  2. What conditions need to be met before it can be removed?
  3. Should this be tracked in an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/feature Release note: Exciting New Features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants