Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-9546][SQL] Centralize orderable data type checking.
This pull request creates two isOrderable functions in RowOrdering that can be used to check whether a data type or a sequence of expressions can be used in sorting. Author: Reynold Xin <rxin@databricks.com> Closes #7880 from rxin/SPARK-9546 and squashes the following commits: f9e322d [Reynold Xin] Fixed tests. 0439b43 [Reynold Xin] [SPARK-9546][SQL] Centralize orderable data type checking.
- Loading branch information
Showing
15 changed files
with
173 additions
and
144 deletions.
There are no files selected for viewing
This file contains 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 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 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
93 changes: 93 additions & 0 deletions
93
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/RowOrdering.scala
This file contains 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,93 @@ | ||
/* | ||
* 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.catalyst.InternalRow | ||
import org.apache.spark.sql.types._ | ||
|
||
|
||
/** | ||
* An interpreted row ordering comparator. | ||
*/ | ||
class RowOrdering(ordering: Seq[SortOrder]) extends Ordering[InternalRow] { | ||
|
||
def this(ordering: Seq[SortOrder], inputSchema: Seq[Attribute]) = | ||
this(ordering.map(BindReferences.bindReference(_, inputSchema))) | ||
|
||
def compare(a: InternalRow, b: InternalRow): Int = { | ||
var i = 0 | ||
while (i < ordering.size) { | ||
val order = ordering(i) | ||
val left = order.child.eval(a) | ||
val right = order.child.eval(b) | ||
|
||
if (left == null && right == null) { | ||
// Both null, continue looking. | ||
} else if (left == null) { | ||
return if (order.direction == Ascending) -1 else 1 | ||
} else if (right == null) { | ||
return if (order.direction == Ascending) 1 else -1 | ||
} else { | ||
val comparison = order.dataType match { | ||
case dt: AtomicType if order.direction == Ascending => | ||
dt.ordering.asInstanceOf[Ordering[Any]].compare(left, right) | ||
case dt: AtomicType if order.direction == Descending => | ||
dt.ordering.asInstanceOf[Ordering[Any]].reverse.compare(left, right) | ||
case s: StructType if order.direction == Ascending => | ||
s.ordering.asInstanceOf[Ordering[Any]].compare(left, right) | ||
case s: StructType if order.direction == Descending => | ||
s.ordering.asInstanceOf[Ordering[Any]].reverse.compare(left, right) | ||
case other => | ||
throw new IllegalArgumentException(s"Type $other does not support ordered operations") | ||
} | ||
if (comparison != 0) { | ||
return comparison | ||
} | ||
} | ||
i += 1 | ||
} | ||
return 0 | ||
} | ||
} | ||
|
||
object RowOrdering { | ||
|
||
/** | ||
* Returns true iff the data type can be ordered (i.e. can be sorted). | ||
*/ | ||
def isOrderable(dataType: DataType): Boolean = dataType match { | ||
case NullType => true | ||
case dt: AtomicType => true | ||
case struct: StructType => struct.fields.forall(f => isOrderable(f.dataType)) | ||
case _ => false | ||
} | ||
|
||
/** | ||
* Returns true iff outputs from the expressions can be ordered. | ||
*/ | ||
def isOrderable(exprs: Seq[Expression]): Boolean = exprs.forall(e => isOrderable(e.dataType)) | ||
|
||
/** | ||
* Creates a [[RowOrdering]] for the given schema, in natural ascending order. | ||
*/ | ||
def forSchema(dataTypes: Seq[DataType]): RowOrdering = { | ||
new RowOrdering(dataTypes.zipWithIndex.map { | ||
case (dt, index) => new SortOrder(BoundReference(index, dt, nullable = true), Ascending) | ||
}) | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.