Skip to content

Consider adding a has_flag option to the comparison operators #822

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
gordon-matt opened this issue Sep 23, 2019 · 12 comments
Open

Consider adding a has_flag option to the comparison operators #822

gordon-matt opened this issue Sep 23, 2019 · 12 comments
Labels
feature New feature

Comments

@gordon-matt
Copy link

Would you please consider adding a has_flag option to the comparison operators to support bitwise flags? For example, let's say one of our columns called, "ProductType" can hold a value representing one or more of the following flag values:

[Flags]
public enum ProductType
{
    Flight = 1,
    Hotel = 2,
    Tour = 4,
    Transfer = 8
    //etc 16, 32, 64...
}

So if a booking record in my database has a value of 3 for the ProductType, this means it was a Flight + Hotel booking. Now if I want to query all records where there is a hotel component (for example), then I need to produce a query as follows:

SELECT *
FROM Bookings
WHERE ProductType & 2 <> 0

Having a has_flag operator would be good here. As for UI, we could use checkboxes. If multiple values are checked, you would produce multiple WHERE clauses.

@mistic100
Copy link
Owner

mistic100 commented Sep 23, 2019

Hello,
sorry I won't implement this, it would require changes in the parser and it's basically an equal or contains operation only with a different form. Which can be easily done with a post builder transformer.

@gordon-matt
Copy link
Author

gordon-matt commented Sep 23, 2019

I'm not sure I can agree with it being basically an equals operation. It's not at all, imho.. but maybe I am missing something here?

And could you please point me in the direction of finding more info on what a "post builder transformer" is?

@mistic100
Copy link
Owner

if you use an "equal" operator you will get "ProductType = 2" then you only have to transform the output to "ProductType & 2 <> 0"

it's all to you to do this transformation, it's not an option of the builder


I forgot to say that you also have the possibility to implement you own operator check https://querybuilder.js.org/#operators and the "Operator" section of https://querybuilder.js.org/plugins.html#sql-support

@mistic100
Copy link
Owner

mistic100 commented Sep 23, 2019

QueryBuilder is a tool, not an end product, what I mean is that you will always have to generate your final query from it's output and not expect it to do all the work. The SQL output was added due to popular demand but it is not the right path when you have specific needs (while working with MongoDB I never used the Mongo output but always generated my queries from the rules tree).

@gordon-matt
Copy link
Author

@mistic100 Thank you for taking the time to respond. I can certainly appreciate your perspective. However, you're not taking into account that in some cases (like mine), we are creating queries for unknown tables. The filters are changed dynamically, because the users of my app can change tables and even choose any random database they want. So I don't know ahead of time which fields are enumerations and which are not, much less which ones are bitwise flags.

Having looked at Mongo's reference manual, I can see they also support bitwise operations:
https://docs.mongodb.com/manual/reference/operator/query-bitwise/

I believe the Mongo equivalent of what I want in SQL would be the $bitsAnySet operator. So adding a has_flag option shouldn't be a problem here if that's what you were worried about - since both SQL and Mongo can support it.

But... if you're steadfast against adding this option, I guess I'll have to try figure out something else. On the other hand, if it's just because of a lack of time to implement, then why not leave this issue open and label it as help wanted ?

@gordon-matt
Copy link
Author

@mistic100

Hi again,

I've been busy with other work, so didn't have time to look at this yet. I was planning on looking into your workaround soon - the one where you said this:

"if you use an "equal" operator you will get "ProductType = 2" then you only have to transform the output to "ProductType & 2 <> 0"

I was just thinking about this and I realized this surely can't be right at all. Because there is a difference between wanting to check if the enumeration CONTAINS a value or is ONLY that value. Let me give you an example:

WHERE ProductType & 2 <> 0
Meaning: Give me results where the ProductType is made up of (2). So, therefore, a value of 3 would also return true.. and other numbers too.

WHERE ProductType = 2
Meaning: Give me results where the ProductType is ONLY (2).

So in this real life situation, a ProductType of 2 is "Hotel". Sometimes we want to query if the booking is for hotel ONLY (=2) or sometimes we want to see all bookings that have a hotel and might also have other products like Flight or Tour.

Therefore, what you suggested about transforming the equals operation cannot be correct... and additionally, the queries are dynamic. I don't know the structure of the tables ahead of time - we build the filters dynamically based on database INFORMATION_SCHEMA.

I hope this clarifies it.

@gordon-matt
Copy link
Author

@mistic100

Hi again,

I don't like to keep nagging about this, but your reason given for not wanting to implement it is based on faulty assumptions. I have explained why in the previous post above. Could you please read it very carefully and respond? There is absolutely no way for me to implement this myself unless you add the new operator. If I automatically convert equals to a check for "has flag", then I lose the option for equals. I need BOTH options.

And this does not matter if it's using SQL output or your json filters array.. it's the exact same problem.

If I am wrong somehow, or if you do understand the problem and still don't want to implement it anyway, then could you please still respond and give me more info? Because as I said; I don't believe that the reasons you provided before were based on a proper understanding of the situation. Please correct me if I am wrong.

@mistic100
Copy link
Owner

Sorry I just don't have the time to analyse this issue. Feel free to submit a proper merge request.

@mistic100
Copy link
Owner

Preferably has a plugin.

@gordon-matt
Copy link
Author

Thanks again for the response.

It's not really that complex. The difference is basically this:

WHERE ProductType IS ONLY Hotel

vs.

WHERE ProductType CONTAINS Hotel

The first one is done with a normal equals operator and the second one is done with a bitwise operator. Mongo supports this as well, so you won't have an issue there.

As far as I can tell, this would be impossible to do without adding a new has_flag operator in the query builder. I can 100% understand if you're too busy to do it yourself. But if so, it would be much appreciated if you would still leave the issue open then. Perhaps someone else may see this issue and decide to implement it. I may give it a try, but as previously mentioned, I am not much of a frontend dev... I do have some colleagues that may be able to look at this though. I'll see if they can find time to implement it and create a pull request.

So, please reopen this.. because it's a very important operator for people who store flag values in their data.

@mistic100
Copy link
Owner

Considering nobody ever asked this I can safely say it is important for you.
I do not say it is not a valid request be you seems to take it very personnally that I don't want to address it immediately. Just remember that this is an Open Source library and provided "as it".

@mistic100 mistic100 reopened this Oct 8, 2019
@mistic100 mistic100 added the feature New feature label Oct 8, 2019
@gordon-matt
Copy link
Author

Thank you kindly for reopening this. I appreciate that.

I'm not sure why you think I am taking it personally. I just didn't want you to close the issue based on a faulty understanding of the issue. Surely that is a reasonable request?

As for wanting it done immediately, that is certainly not the case. I said I totally 100% understand if you don't have the time to do it.. and only want you to leave this open so someone (either yourself or someone else in the community) may eventually get around to it.

Again, thank you for reopening this issue and also just for making such a useful jQuery plugin as this is and sharing it with everyone in the first place.

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

No branches or pull requests

2 participants