Skip to content

Could not parse complex COALESCE/CAST #1607

Closed

Description

@d2a-raudenaerde

This bit of (Postgresql valid) SQL fails:

select
t0.*
,	cr.code
from
mytable t0
join 	othertable cr ON cr.code = '1234'
where	
   COALESCE (cast(to_date(to_char(t0.datum_ontslagen,'YYYYMMDD'),'YYYYMMDD')||' '||t0.tijd_ontslagen as timestamp), current_timestamp(0)) - 
      cast(to_date(to_char(t0.datum_opname,'YYYYMMDD'),'YYYYMMDD')||' '||t0.tijd_opname as timestamp)
   >= (SELECT somefunction()) * interval '1 hours' 


net.sf.jsqlparser.JSQLParserException: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "(" "("
at line 8, column 13.

Activity

  1. changed the title [-]Could not parse complex COALSECE / CAST[/-] [+]Could not parse complex CAST[/+] on Aug 2, 2022
  2. manticore-projects commented on Aug 2, 2022

    @manticore-projects
    Contributor

    Greetings.

    I think its this one: current_timestamp(0) -- current_timestamp needs to be whitelisted as keyword so it can be interpreted as a function name.

    There is a 6 month old PR regarding a proper keyword handling. But it has not gotten much interested yet.

  3. changed the title [-]Could not parse complex CAST[/-] [+]Could not parse complex COALESCE/CAST[/+] on Aug 2, 2022
  4. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    Ah (sorry for the editting)
    I'll check the PR.

  5. manticore-projects commented on Aug 2, 2022

    @manticore-projects
    Contributor

    What I can do: I can update the PR to reflect the latest release and then you can give it a try.

  6. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    That would be nice :)

  7. manticore-projects commented on Aug 2, 2022

    @manticore-projects
    Contributor

    Tomorrow first thing in the morning, I am running late here. cheers.

  8. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    No hurry! :)

  9. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    Btw: it is not a keyword according to the docs, but rather a function:

    CURRENT_TIMESTAMP(precision)

  10. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    So, maybe instead of building support, we need to remove the K_TIME_KEY_EXPR so the parser just parses a function?

  11. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    I removed it and the parse rules involving it, and now it parses fine :)
    If something like the TimeKeyExpression is needed somewhere, I think the Function parse rules could use some IF statement matching the functions name and return a TimeFunction (as that would be a more correct name)?

  12. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    Mmm. It fails the CURRENT TIME test for db2 (is that even valid SQL?)
    That can be fixed by instead of removing the K_TIME_KEY_EXPR, we keep it but only the tokens with a ' ' (so the _ will be handled by the function parsing rules)

  13. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    Btw: the sql2003standard does have a special case for these datetime functions: (but not the syntax db2 apparently uses, so that should perhapse be a capability setting?)

    <datetime value function> ::=
    		<current date value function>
    	|	<current time value function>
    	|	<current timestamp value function>
    	|	<current local time value function>
    	|	<current local timestamp value function>
    
    <current date value function> ::= CURRENT_DATE
    
    <current time value function> ::= CURRENT_TIME [ <left paren> <time precision> <right paren> ]
    
    <current local time value function> ::= LOCALTIME [ <left paren> <time precision> <right paren> ]
    
    <current timestamp value function> ::= CURRENT_TIMESTAMP [ <left paren> <timestamp precision> <right paren> ]
    
    <current local timestamp value function> ::= LOCALTIMESTAMP [ <left paren> <timestamp precision> <right paren> ]
    
  14. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    I think it would be rather straight-forward to implement these rules, and allow the tokens to be both with underscore, and without.

  15. manticore-projects commented on Aug 2, 2022

    @manticore-projects
    Contributor

    CURRENT_TIMESTAMP is defined as a Token in JSQLParser and this token has not been whitelisted yet for the use of function names.

    My PR fixes this re-occurring problem in a generic way and for ALL tokens, not only this one.

  16. manticore-projects commented on Aug 2, 2022

    @manticore-projects
    Contributor

    I think it would be rather straight-forward to implement these rules, and allow the tokens to be both with underscore, and without.

    With and without underscore is supported already.

  17. d2a-raudenaerde commented on Aug 2, 2022

    @d2a-raudenaerde
    ContributorAuthor

    Ah ok, I will be patient and wait for you PR :)

  18. manticore-projects commented on Aug 2, 2022

    @manticore-projects
    Contributor

    The PR #1382 is there already, I just need to resolve conflicts. You can read through the lengthy discussion already for understanding what it does and how it works.

  19. manticore-projects commented on Nov 12, 2022

    @manticore-projects
    Contributor

    Still relevant with JSQLParser 4.6 Snapshot.

  20. added a commit that references this issue on Nov 14, 2022
    9df3c07
  21. d2a-raudenaerde commented on Nov 14, 2022

    @d2a-raudenaerde
    ContributorAuthor

    Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions