Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package org.apache.spark.sql.catalyst.expressions
* by `hashCode`.
* - [[EqualTo]] and [[EqualNullSafe]] are reordered by `hashCode`.
* - Other comparisons ([[GreaterThan]], [[LessThan]]) are reversed by `hashCode`.
* - Elements in [[In]] are reordered by `hashCode`.
*/
object Canonicalize {
def execute(e: Expression): Expression = {
Expand Down Expand Up @@ -85,6 +86,11 @@ object Canonicalize {
case Not(GreaterThanOrEqual(l, r)) => LessThan(l, r)
case Not(LessThanOrEqual(l, r)) => GreaterThan(l, r)

// order the list in the In operator
// In subqueries contain only one element of type ListQuery. So checking that the length > 1
// we are not reordering In subqueries.
case In(value, list) if list.length > 1 => In(value, list.sortBy(_.hashCode()))

case _ => e
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.dsl.plans._
import org.apache.spark.sql.catalyst.plans.logical.Range

class CanonicalizeSuite extends SparkFunSuite {

test("SPARK-24276: IN expression with different order are semantically equal") {
val range = Range(1, 1, 1, 1)
val idAttr = range.output.head

val in1 = In(idAttr, Seq(Literal(1), Literal(2)))
val in2 = In(idAttr, Seq(Literal(2), Literal(1)))
val in3 = In(idAttr, Seq(Literal(1), Literal(2), Literal(3)))

assert(in1.canonicalized.semanticHash() == in2.canonicalized.semanticHash())
assert(in1.canonicalized.semanticHash() != in3.canonicalized.semanticHash())

assert(range.where(in1).sameResult(range.where(in2)))
assert(!range.where(in1).sameResult(range.where(in3)))

val arrays1 = In(idAttr, Seq(CreateArray(Seq(Literal(1), Literal(2))),
CreateArray(Seq(Literal(2), Literal(1)))))
val arrays2 = In(idAttr, Seq(CreateArray(Seq(Literal(2), Literal(1))),
CreateArray(Seq(Literal(1), Literal(2)))))
val arrays3 = In(idAttr, Seq(CreateArray(Seq(Literal(1), Literal(2))),
CreateArray(Seq(Literal(3), Literal(1)))))

assert(arrays1.canonicalized.semanticHash() == arrays2.canonicalized.semanticHash())
assert(arrays1.canonicalized.semanticHash() != arrays3.canonicalized.semanticHash())

assert(range.where(arrays1).sameResult(range.where(arrays2)))
assert(!range.where(arrays1).sameResult(range.where(arrays3)))
}
}