★ wanayoo — archive 1999 https://github.com/JSQLParser/JSqlParser/issues/926Nouvelle recherche | Portail wanayoo
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

TRUE and FALSE in WHERE clause are parsed as columns #926

Open
sivaraam opened this issue Jan 16, 2020 · 6 comments
Open

TRUE and FALSE in WHERE clause are parsed as columns #926

sivaraam opened this issue Jan 16, 2020 · 6 comments

Comments

@sivaraam
Copy link
Contributor

@sivaraam sivaraam commented Jan 16, 2020

Describe the bug

Boolean values in WHERE clause are identified as columns rather than being identified as primitive values like numbers.

To Reproduce

Steps to reproduce the behavior:

  1. Example SQL
SELECT * FROM tableA WHERE true
  1. Parsing this SQL using JSqlParser with this statements
    public static void main(String[] args) throws JSQLParserException {
        Select sel = (Select) CCJSqlParserUtil.parse("SELECT * FROM tableA WHERE true");
        PlainSelect plainSelect = (PlainSelect) sel.getSelectBody();
        System.out.println(plainSelect.getWhere() instanceof Column);
    }

Expected behavior

Prints false as true in the WHERE clause is not supposed to be considered as a column name.

Actual behavior

Prints true.

System

  • Database you are using: PostgreSQL
  • Java Version: 11.0.3
  • JSqlParser version: 3.1
@wumpz
Copy link
Member

@wumpz wumpz commented Jan 21, 2020

That is true. Because JSqlParser is not only a PostgreSQL parser and other DBMS do not have this true / false keywords, this is by design. How should we differ between TRUE as a value and TRUE as a column name?

@sivaraam
Copy link
Contributor Author

@sivaraam sivaraam commented Jan 21, 2020

Because JSqlParser is not only a PostgreSQL parser and other DBMS do not have this true / false keywords, this is by design.

Oh! I didn't realise that. I thought most databases have TRUE/FALSE as keywords. On cursory exploration it does seem MS SQL Server doesn't treat them as keywords.

How should we differ between TRUE as a value and TRUE as a column name?

Just assume we always interpret TRUE/FALSE as a value. AFAICT, the only consequence would be that we cannot use TRUE and FALSE as database identifiers without quoting. Would that be such a bad thing? I'm asking this as I'm not sure.

@SerialVelocity
Copy link

@SerialVelocity SerialVelocity commented Jan 23, 2020

Just assume we always interpret TRUE/FALSE as a value. AFAICT, the only consequence would be that we cannot use TRUE and FALSE as database identifiers without quoting. Would that be such a bad thing? I'm asking this as I'm not sure.

I think if a query is valid in another database, it should be parsed by JSQLParser (without having to add extra quotes, etc).

@wumpz I think we had a small discussion on the Postgres array syntax issue a while ago that it might be a good idea to have a flag indicating what kind of database syntax is being parsed. It would help here as well because the parser could interpret TRUE/FALSE differently based on the flag (e.g. Postgres has TRUE/FALSE as a keyword but MSSQL has it as an expression).

@wumpz
Copy link
Member

@wumpz wumpz commented Jan 24, 2020

I don't see the benefit here. The user just need to check the column names. The same would apply to some rownum, sysdate, now functions that could be used without brackets. So it makes things more complicated. Do you suggest a flag for all of these?

@SerialVelocity
Copy link

@SerialVelocity SerialVelocity commented Jan 24, 2020

The benefit is that users don't need to special case things like this, it gets parsed according to the dialect. I would expect a flag to cover all of these (unrelated thing: rownum is a pseudo-column afaik so maybe it would still be a column?).

For example, if you are creating a visitor that visits all functions, you need to remember to visit column names too and make sure "sysdate", etc aren't being used.

If values are parsed as a function by the dialect, it would be good for JSQLParser to recognise them as functions too.

@sivaraam
Copy link
Contributor Author

@sivaraam sivaraam commented Jan 27, 2020

Making JsqlParser accept the SQL dialect of the query being parsed would be a good feature to have. It would help with flagging usages such as the usage of NULLS FIRST in MySQL queries and what not. But this seems to be more involved change.

For fixing this issue, I think a short-term solution would be to just always interpret TRUE/FALSE as a value. I think it would be a nice solution for the mean time as I hope that not many would have TRUE/FALSE as table/column names.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.