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

CASE statement parse issue #6

Open
lcoldiron opened this issue Aug 10, 2011 · 2 comments
Open

CASE statement parse issue #6

lcoldiron opened this issue Aug 10, 2011 · 2 comments
Assignees

Comments

@lcoldiron
Copy link
Contributor

IF (CASE
WHEN x IS NULL THEN FALSE
WHEN x = 'abc' THEN FALSE
ELSE TRUE
END)
THEN
RAISE EXCEPTION 'error message';
END IF;

The parser seem to have issues with FALSE. It appears that the parser is assuming there needs to be a assignment that occurs after the FALSE.

@kputnam
Copy link
Owner

kputnam commented Aug 11, 2011

What's going on is the parser reads IF and skips ahead to the next occurrence of THEN, after which it expects the consequent (in this case it should be RAISE EXCEPTION). Instead it stops at the THEN which is part of the CASE expression. This is clearly a faulty assumption and I'll need to actually parse the conditional as an expression.

However, this raises several issues. First, this will need to add state to the parser to track the parentheses nesting level -- something I was fortunately able to avoid until now. Second, it makes sense that we would want to get coverage on each branch within the CASE statement! This is a fairly complicated issue that I would like to address, but perhaps after some careful consideration.

In the meantime, there are several alternate syntaxes that express the same control flow. I realize this may be a simplified example, but consider the following suggestions.

  • IF ((x = 'abc') is not true) THEN ...
  • IF (x IS NOT NULL AND x <> 'abc') THEN ...

Probably more acceptable would be to assign the result of the entire CASE expression to a temporary variable. This preserves the CASE statement entirely, allows for code coverage on each WHEN branch, and most importantly will be accepted by the parser.

condition := CASE WHEN x IS NULL THEN false
                  WHEN x = 'abc' THEN false
                  ELSE true
                  END;

IF (condition)
THEN
  RAISE EXCEPTION 'error message';
END IF;

@lcoldiron
Copy link
Contributor Author

I changed to use the later workaround and everything worked as expected.

@ghost ghost assigned kputnam Aug 13, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants