-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-27924][SQL] Support ANSI SQL Boolean-Predicate syntax #25074
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
Closed
Closed
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4a9ac98
implement boolean test.
beliefer 3963455
Add UT.
beliefer 00cc822
support boolean test.
beliefer 5e72437
support boolean test.
beliefer 349d865
update code.
beliefer 4ef1c12
update code.
beliefer 3013cee
update code.
beliefer 6629b33
update UT.
beliefer a0d2d07
update UT.
beliefer 05d36f9
update code.
beliefer bd05be2
update ut.
beliefer 97e7812
update ut.
beliefer 782b75e
update ut.
beliefer b2fc258
update ut.
beliefer fa148db
Supplement Boolean test.
beliefer c22f12d
Enhance UT.
beliefer fc207e1
Optimize code.
beliefer 3f36c0c
Optimize code.
beliefer 189e805
Optimize code.
beliefer 26c8658
Optimize code.
beliefer d6abc38
Add tests.
beliefer a9cc3ce
Optimize code.
beliefer 2719224
Optimize code.
beliefer efe5a51
change to private
beliefer bd71665
reference maropu
beliefer 67db695
merge maropu suggestion.
beliefer 65d4100
avoid no valid constructor
beliefer ebd2dcf
Remove import AnalysisException
beliefer 4794b67
Optimize code.
beliefer ae126e7
Fix ambiguous variable.
beliefer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...atalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/booleanExpressions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} | ||
| import org.apache.spark.sql.types.BooleanType | ||
|
|
||
| /** | ||
| * String to indicate which boolean test selected. | ||
| */ | ||
| object BooleanTest { | ||
| val TRUE = "TRUE" | ||
| val FALSE = "FALSE" | ||
| val UNKNOWN = "UNKNOWN" | ||
|
|
||
| def calculate(input: Any, booleanValue: String): Boolean = { | ||
| booleanValue match { | ||
| case TRUE => input == true | ||
| case FALSE => input == false | ||
| case UNKNOWN => input == null | ||
| case _ => throw new AnalysisException("Boolean test value must be one of TRUE, " + | ||
| "FALSE and UNKNOWN.") | ||
| } | ||
| } | ||
| } | ||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Test the value of an expression is true, false, or unknown. | ||
| */ | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(expr, booleanValue) - Returns true if `expr` equals booleanValue, " + | ||
| "or false otherwise.", | ||
| arguments = """ | ||
| Arguments: | ||
| * expr - a boolean expression | ||
| * booleanValue - a boolean value represented by a string. booleanValue must be one | ||
| of TRUE, FALSE and UNKNOWN. | ||
| """, | ||
| examples = """ | ||
| Examples: | ||
| > SELECT _FUNC_(1 > 2, true); | ||
| false | ||
| > SELECT _FUNC_(2 > 1, true); | ||
| true | ||
| """) | ||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
| case class BooleanTest(child: Expression, booleanValue: String) | ||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
| extends UnaryExpression with Predicate { | ||
|
|
||
| override def nullable: Boolean = false | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| BooleanTest.calculate(child.eval(input), booleanValue) | ||
| } | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| defineCodeGen(ctx, ev, input => | ||
| s""" | ||
| org.apache.spark.sql.catalyst.expressions.BooleanTest.calculate($input, "$booleanValue") | ||
| """ | ||
| ) | ||
| } | ||
|
|
||
| override def sql: String = s"(${child.sql} IS $booleanValue)" | ||
| } | ||
|
|
||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...st/src/test/scala/org/apache/spark/sql/catalyst/expressions/BooleanExpressionsSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.expressions | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.expressions.BooleanTest._ | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| class BooleanExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
|
|
||
| val row0 = create_row(null) | ||
| val row1 = create_row(false) | ||
| val row2 = create_row(true) | ||
|
|
||
| test("istrue and isnottrue") { | ||
| checkEvaluation(BooleanTest(Literal.create(null, NullType), TRUE), false, row0) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(null, NullType), TRUE)), true, row0) | ||
| checkEvaluation(BooleanTest(Literal.create(false, BooleanType), TRUE), false, row1) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(false, BooleanType), TRUE)), true, row1) | ||
| checkEvaluation(BooleanTest(Literal.create(true, BooleanType), TRUE), true, row2) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(true, BooleanType), TRUE)), false, row2) | ||
| } | ||
|
|
||
| test("isfalse and isnotfalse") { | ||
| checkEvaluation(BooleanTest(Literal.create(null, NullType), FALSE), false, row0) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(null, NullType), FALSE)), true, row0) | ||
| checkEvaluation(BooleanTest(Literal.create(false, BooleanType), FALSE), true, row1) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(false, BooleanType), FALSE)), false, row1) | ||
| checkEvaluation(BooleanTest(Literal.create(true, BooleanType), FALSE), false, row2) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(true, BooleanType), FALSE)), true, row2) | ||
| } | ||
|
|
||
| test("isunknown and isnotunknown") { | ||
| checkEvaluation(BooleanTest(Literal.create(null, NullType), UNKNOWN), true, row0) | ||
| checkEvaluation(Not(BooleanTest(Literal.create(null, NullType), UNKNOWN)), false, row0) | ||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
|
dongjoon-hyun marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.