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: databend-meta transaction support generic bool-expression and else-if chain #17064

Merged
merged 1 commit into from
Dec 20, 2024

Conversation

drmingdrmer
Copy link
Member

@drmingdrmer drmingdrmer commented Dec 17, 2024

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

feat: databend-meta transaction support generic bool-expression and else-if chain

Since this commit, application is allowed to specify a complex bool
expressions as the transaction predicate.

For example, the transaction will execute as if running the following
pseudo codes:

if (a == 1 || b == 2) && (x == 3 || y == 4) { ops1 }
else if (x == 2 || y == 1) { ops2 }
else if (y == 3 && z == 4) { ops3 }
else { ops4 }
let eq = |key: &str, val: &str| TxnCondition::eq_value(sample(key), b(val));

TxnRequest{
    operations: vec![
                BoolExpression::new(
                    Some(eq("a", 1).or(eq("b", 2))
                         .and(eq("x", 3).or(eq("y", 4)))),
                    ops1),
                BoolExpression::new(
                    Some(eq("x", 2).or(eq("y", 1))),
                    ops2),
            ],

    condition: vec![eq("y", 3), eq("z", 4)],
    if_then: ops3,
    else_then: ops4, 
}

For backward compatibility, both already existing condition and the new
operations will be evaluated: transaction handler evaluate the
operations first. If there is a met condition, execute and return.
Otherwise, it evaluate condition and then execute if_then branch or
else_then branch.

TxnReply changes:

Add field execution_path to indicate the executed branch, which is one
of:

  • "operation:<index>", operation at index is executed.
  • "then": if_then is executed.
  • "else": else_then is executed.

TxnReply.success is set to false only when else is executed.

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • New Feature (non-breaking change which adds functionality)

Related Issues


This change is Reviewable

@github-actions github-actions bot added the pr-feature this PR introduces a new feature to the codebase label Dec 17, 2024
@drmingdrmer drmingdrmer marked this pull request as ready for review December 17, 2024 07:41
Copy link
Member

@zhang2014 zhang2014 left a comment

Choose a reason for hiding this comment

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

👍

@drmingdrmer drmingdrmer force-pushed the 185-bool-expr branch 2 times, most recently from 675e496 to 6076324 Compare December 17, 2024 10:39
@drmingdrmer drmingdrmer marked this pull request as draft December 17, 2024 10:40
@drmingdrmer drmingdrmer changed the title feat: databend-meta transaction support generic bool-expression feat: databend-meta transaction support generic bool-expression and else-if chain Dec 19, 2024
@drmingdrmer drmingdrmer marked this pull request as ready for review December 19, 2024 03:34
@drmingdrmer drmingdrmer force-pushed the 185-bool-expr branch 8 times, most recently from 4a63bca to e092f8e Compare December 20, 2024 09:00
…lse-if chain

Since this commit, application is allowed to specify a complex bool
expressions as the transaction predicate.

For example, the transaction will execute as if running the following
pseudo codes:
```
if (a == 1 || b == 2) && (x == 3 || y == 4) { ops1 }
else if (x == 2 || y == 1) { ops2 }
else if (y == 3 && z == 4) { ops3 }
else { ops4 }
```

```rust
let eq = |key: &str, val: &str| TxnCondition::eq_value(sample(key), b(val));

TxnRequest{
    operations: vec![
                BoolExpression::new(
                    Some(eq("a", 1).or(eq("b", 2))
                         .and(eq("x", 3).or(eq("y", 4)))),
                    ops1),
                BoolExpression::new(
                    Some(eq("x", 2).or(eq("y", 1))),
                    ops2),
            ],

    condition: vec![eq("y", 3), eq("z", 4)],
    if_then: ops3,
    else_then: ops4,
}
```

For backward compatibility, both already existing `condition` and the new
`operations` will be evaluated: transaction handler evaluate the
`operations` first. If there is a met condition, execute and return.
Otherwise, it evaluate `condition` and then execute `if_then` branch or
`else_then` branch.

TxnReply changes:

Add field `execution_path` to indicate the executed branch, which is one
of:
- `"operation:<index>"`, operation at `index` is executed.
- `"then"`: `if_then` is executed.
- `"else"`: `else_then` is executed.

`TxnReply.success` is set to `false` only when `else` is executed.
@drmingdrmer drmingdrmer merged commit 22f9ba2 into databendlabs:main Dec 20, 2024
72 of 73 checks passed
@drmingdrmer drmingdrmer deleted the 185-bool-expr branch December 20, 2024 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-feature this PR introduces a new feature to the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants