Skip to content

Conversation

@hvanhovell
Copy link
Contributor

What changes were proposed in this pull request?

We currently only allow relatively simple expressions as the input for a value based case statement. Expressions like case (a > 1) or (b = 2) when true then 1 when false then 0 end currently fail. This PR adds support for such expressions.

How was this patch tested?

Added a test to the ExpressionParserSuite.

@hvanhovell
Copy link
Contributor Author

cc @dongjoon-hyun

@dongjoon-hyun
Copy link
Member

Thank you for pinging me!

@dongjoon-hyun
Copy link
Member

dongjoon-hyun commented Oct 2, 2016

@hvanhovell , it looks great.

Most of cases are passed. I'm wondering if we can support the following, too?

sql("SELECT CASE 'a'='a' WHEN TRUE THEN 1 END").show

Since the followings are passed, the above one is a minor one.

sql("SELECT CASE ('a'='a') WHEN TRUE THEN 1 END").show
sql("SELECT CASE 1=1 WHEN TRUE THEN 1 END").show
sql("SELECT CASE 1='a' WHEN TRUE THEN 1 END").show

@SparkQA
Copy link

SparkQA commented Oct 2, 2016

Test build #66232 has finished for PR 15322 at commit b25c849.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@hvanhovell
Copy link
Contributor Author

@dongjoon-hyun I had to move a few rules around to get your example working. It was hitting the typeConstructor rule instead of the simpleCase rule; the lesson here is that generic rules should normally get lower priority then specific ones (if they both apply).

@dongjoon-hyun
Copy link
Member

Ah, that was the reason. Thank you so much, @hvanhovell .

@SparkQA
Copy link

SparkQA commented Oct 2, 2016

Test build #66246 has finished for PR 15322 at commit b6b3910.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

CaseKeyWhen('a, Seq(1, 'b, 2, 'c, 'd)))
assertEqual("case (a or b) when true then c when false then d else e end",
CaseKeyWhen('a || 'b, Seq(true, 'c, false, 'd, 'e)))
assertEqual("case 'a'='a' when true then 1 end",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: seems 'a'='a' is a valueExpression? So it should be passed even without this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought the same thing. The issue here is that case 'a' also matches the typeConstructor rule which was triggered before the simpleCase rule. So I also fixed this by moving the rules around a bit more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see. Currently the first element in typeConstructor is an identifier. Is it too loose?

@viirya
Copy link
Member

viirya commented Oct 3, 2016

LGTM except a minor comment.

@hvanhovell
Copy link
Contributor Author

Merging to master/2.0. Thanks for the reviews!

asfgit pushed a commit that referenced this pull request Oct 4, 2016
…sed case statement

## What changes were proposed in this pull request?
We currently only allow relatively simple expressions as the input for a value based case statement. Expressions like `case (a > 1) or (b = 2) when true then 1 when false then 0 end` currently fail. This PR adds support for such expressions.

## How was this patch tested?
Added a test to the ExpressionParserSuite.

Author: Herman van Hovell <hvanhovell@databricks.com>

Closes #15322 from hvanhovell/SPARK-17753.

(cherry picked from commit 2bbecde)
Signed-off-by: Herman van Hovell <hvanhovell@databricks.com>
@asfgit asfgit closed this in 2bbecde Oct 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants