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

Support 'case' conditional function #2356

Draft
wants to merge 1 commit into
base: release-7.x
Choose a base branch
from
Draft

Support 'case' conditional function #2356

wants to merge 1 commit into from

Conversation

xuzhg
Copy link
Member

@xuzhg xuzhg commented Mar 18, 2022

Issues

This pull request fixes #2309.

Description

Support 'case' conditional function parsing.

Here's the information for the "case" function:

5.1.1.12 Conditional Functions
5.1.1.12.1 case
The case function has the following signature:

expression case(Edm.Boolean:expression, ..., Edm.Boolean:expression)

Each parameter is a pair of expressions separated by a colon (:), where the first expression – the condition – MUST be a Boolean expression, and the second expression – the result – may evaluate to any type.

The case function evaluates the condition in each pair, starting with the leftmost pair, and stops as soon as a condition evaluates to true. It then returns the value of the result of this pair. It returns null if none of the conditions in any pair evaluates to true. Clients can specify a last pair whose condition is true to get a non-null “default/else/otherwise” result.

Clients SHOULD ensure that the results in all pairs are compatible. If all results are of the same type, the type of the case expression is of that type. If all results are of numeric type, then the type of the case expression is a numeric type capable of representing any of these expressions according to standard type promotion rules.

Services MAY support case expressions containing parameters of incompatible types, in which case the case expression is treated as Edm.Untyped and its value has the type of the parameter expression selected by the case statement.

Example 97: compute signum(X)

$compute=case(X gt 0 :1,X lt 0 :-1,true :0) as SignumX

[Discussion]

We should ask whitespace before the ':', otherwise we have the following problem:, (However, it sounds ridiculous)
That's because ':' is a valid character both for separate or char in time value.

  1. case(Id eq 2:1) => should 'case(Id eq 2 :1)`

  2. case(CreatedTime eq 10:2:1) ==> We can't distinguish it from TimeOfDay value

    But, this does: case(CreatedTime eq 10:2 :1)

  3. case(CreatedTime eq 10:2:1:2:3)
    It can be: 10:2:1 : 2:3 or 10:2 : 1:2:3

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

@pull-request-quantifier-deprecated

This PR has 130 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Medium
Size       : +128 -2
Percentile : 46%

Total files changed: 15

Change summary by file extension:
.txt : +16 -0
.cs : +112 -2

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@sliekens
Copy link
Contributor

Can this be picked up again? 🙏 I want to start using $compute as introduced in https://devblogs.microsoft.com/odata/compute-and-search-in-asp-net-core-odata-8/

but $compute without case() is very, very limited

@xuzhg
Copy link
Member Author

xuzhg commented Nov 27, 2023

Can this be picked up again? 🙏 I want to start using $compute as introduced in https://devblogs.microsoft.com/odata/compute-and-search-in-asp-net-core-odata-8/

but $compute without case() is very, very limited

Would you please share a little bit more detail about your usage/sceneries and yourself to us so we can priority it carefully? Thanks to: [email protected]

@sliekens
Copy link
Contributor

Right now I only have one use case which is null coalescing:

case(field1 ne null:field1, true:field2)

Which can be read as:

coalesce(field1, field2)

But I expect to find more ways to use this in the future.

@sliekens
Copy link
Contributor

Also see this related issue OData/AspNetCoreOData#684

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CASE not working in $compute and $filter
2 participants