Skip to content
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

feat(flow): get time window from datafusion logical plan #5570

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

Conversation

discord9
Copy link
Contributor

@discord9 discord9 commented Feb 19, 2025

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

as title, get time window from datafusion logical plan, exposing a find_plan_time_window_lower_bound for adjust time winodw of certain query, this is part of a larger feature and split for better reviewability

PR Checklist

Please convert it to a draft if some of the following conditions are not met.

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.
  • API changes are backward compatible.
  • Schema or data changes are backward compatible.

Summary by CodeRabbit

  • New Features

    • Enhanced error reporting by capturing additional context for external library issues.
    • Introduced new query processing logic to support recording rules and improve time-window based operations.
  • Tests

    • Expanded test coverage to ensure robust handling of time-window expressions in queries.
  • Chores

    • Adjusted test schemas to correctly designate time index columns.

Copy link
Contributor

coderabbitai bot commented Feb 19, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request introduces a new variant in the flow module’s error handling to encapsulate Arrow library errors, updating the corresponding status mapping. It enhances test coverage by adding a new test case for evaluating time window lower bounds. Additionally, a new module named recording_rules is added, which contains functions for converting SQL queries into logical plans, determining time window lower bounds, and rewriting plans with added filters. Finally, the test utilities are updated to designate the "ts" column as a time index.

Changes

File(s) Change Summary
src/flow/src/error.rs Added new Arrow variant to the Error enum with fields raw: ArrowError and context: String; updated status_code to map this variant to StatusCode::Internal.
src/flow/src/expr/utils.rs Introduced a new test case in test_plan_time_window_lower_bound with an added expected variable for various time window scenarios.
src/flow/src/lib.rs
src/flow/src/recording_rules.rs
Added new module recording_rules which provides:
- sql_to_df_plan for SQL-to-logical plan conversion
- find_plan_time_window_lower_bound and find_expr_time_window_lower_bound for time window evaluation
- eval_ts_to_ts for timestamp conversion
- df_plan_to_sql to translate logical plans back to SQL
- AddFilterRewriter struct with associated methods for plan rewrites.
src/flow/src/test_utils.rs Updated the "ts" column schema by appending .with_time_index(true) to mark it explicitly as a time index.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant RR as RecordingRules
    participant QE as QueryEngine
    participant QC as QueryContext

    User->>RR: Call sql_to_df_plan(query, optimize)
    RR->>QE: Parse SQL query
    QE-->>RR: Return LogicalPlan
    RR-->>User: Return LogicalPlan

    User->>RR: Call find_plan_time_window_lower_bound(plan, current)
    RR->>RR: Locate time window expression
    RR->>QE: Execute binary search for lower bound
    QE-->>RR: Return lower bound timestamp
    RR-->>User: Return Optional Timestamp
Loading

Possibly related PRs

  • feat: flow recreate on reboot #4509: Altered the Error enum in the same file by introducing a new variant (ListFlows), which has a close code-level connection to the modifications in this PR.

Suggested reviewers

  • waynexia
  • zhongzc
  • WenyXu

Poem

I'm a rabbit hopping through lines of code,
Discovering errors where new variants explode.
From SQL queries to plans so tight,
Every test case shines with carrot light.
With modules and tweaks all fresh and grand,
I celebrate these changes across our land!


🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

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.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Feb 19, 2025
@discord9 discord9 changed the title feat(flow): get time window from logical plan feat(flow): get time window from datafusion logical plan Feb 19, 2025
@discord9 discord9 marked this pull request as ready for review February 19, 2025 11:32
@waynexia waynexia requested a review from Copilot February 19, 2025 18:00

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)
@discord9
Copy link
Contributor Author

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Feb 20, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@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: 0

🧹 Nitpick comments (3)
src/flow/src/recording_rules.rs (3)

48-70: Validate SQL parse failures further.

Consider appending more detailed context or user-friendly hints when parsing fails—e.g., logging the exact failing SQL to assist in debugging parse errors in production.


72-198: Handle multiple time window expressions more gracefully.

Right now, the code returns on the first matching group expression referencing the timestamp column. If there's more than one matching expression, only the first is considered. Although this may be the intended behavior today, consider clarifying or adding a fallback strategy for future expansions that might handle multiple time window expressions in a single plan.


308-391: Single-row evaluation overhead.

eval_ts_to_ts creates a single-row record batch on each call. If many calls are needed (e.g., wide searches or repeated usage), this can be somewhat inefficient. Caching an in-memory query context or batching multiple timestamps might improve performance.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81da18e and 727403c.

📒 Files selected for processing (5)
  • src/flow/src/error.rs (3 hunks)
  • src/flow/src/expr/utils.rs (1 hunks)
  • src/flow/src/lib.rs (1 hunks)
  • src/flow/src/recording_rules.rs (1 hunks)
  • src/flow/src/test_utils.rs (1 hunks)
🔇 Additional comments (9)
src/flow/src/recording_rules.rs (3)

1-14: License header check.

No issues found in the license headers. Good job including Apache 2.0 references and disclaimers.


200-306: Exponential + binary search performance consideration.

The exponential fallback and subsequent binary search for finding the lower bound is correct for monotonic time expressions. However, for extremely large time offsets, you could exceed typical time ranges or quickly jump to None. This is acceptable if you expect moderate ranges, but if large intervals become common, consider bounding the search or using a more direct approach (e.g., specialized domain knowledge for particular date bin intervals).


445-556: Solid test coverage.

Great use of table-driven tests that exercise different group by expressions and time windows. This approach is easy to extend or adapt for future expansions.

src/flow/src/lib.rs (1)

36-36: Module import looks good.

The new recording_rules module integrates seamlessly with the existing crate structure.

src/flow/src/test_utils.rs (1)

89-90: Time index schema update.

Marking the "ts" column as a time index aligns with the logic in find_plan_time_window_lower_bound, enabling correct detection of time-window-based queries.

src/flow/src/error.rs (3)

19-19: LGTM!

The import of ArrowError is correctly added to support the new error variant.


160-167: LGTM!

The new Arrow variant is well-structured and follows the same pattern as the existing Datafusion variant, with appropriate fields for error source, context, and location.


243-243: LGTM!

The Arrow variant is correctly integrated into the status_code method, mapping to StatusCode::Internal which is appropriate for this type of error.

src/flow/src/expr/utils.rs (1)

241-241: LGTM!

The test case modification maintains the existing test structure and coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant