-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
SPARK-2407: Added internal implementation of SQL SUBSTR() #1359
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,11 @@ package org.apache.spark.sql.catalyst.expressions | |
|
||
import java.util.regex.Pattern | ||
|
||
import org.apache.spark.sql.catalyst.types.DataType | ||
import org.apache.spark.sql.catalyst.types.StringType | ||
import org.apache.spark.sql.catalyst.types.BooleanType | ||
import scala.collection.IndexedSeqOptimized | ||
|
||
|
||
import org.apache.spark.sql.catalyst.analysis.UnresolvedException | ||
import org.apache.spark.sql.catalyst.types.{BinaryType, BooleanType, DataType, StringType} | ||
|
||
trait StringRegexExpression { | ||
self: BinaryExpression => | ||
|
@@ -205,3 +207,72 @@ case class EndsWith(left: Expression, right: Expression) | |
extends BinaryExpression with StringComparison { | ||
def compare(l: String, r: String) = l.endsWith(r) | ||
} | ||
|
||
/** | ||
* A function that takes a substring of its first argument starting at a given position. | ||
* Defined for String and Binary types. | ||
*/ | ||
case class Substring(str: Expression, pos: Expression, len: Expression) extends Expression { | ||
|
||
type EvaluatedType = Any | ||
|
||
def nullable: Boolean = true | ||
def dataType: DataType = { | ||
if (!resolved) { | ||
throw new UnresolvedException(this, s"Cannot resolve since $children are not resolved") | ||
} | ||
if (str.dataType == BinaryType) str.dataType else StringType | ||
} | ||
|
||
def references = children.flatMap(_.references).toSet | ||
|
||
override def children = str :: pos :: len :: Nil | ||
|
||
@inline | ||
def slice[T, C <: Any](str: C, startPos: Int, sliceLen: Int) | ||
(implicit ev: (C=>IndexedSeqOptimized[T,_])): Any = { | ||
val len = str.length | ||
// Hive and SQL use one-based indexing for SUBSTR arguments but also accept zero and | ||
// negative indices for start positions. If a start index i is greater than 0, it | ||
// refers to element i-1 in the sequence. If a start index i is less than 0, it refers | ||
// to the -ith element before the end of the sequence. If a start index i is 0, it | ||
// refers to the first element. | ||
|
||
val start = startPos match { | ||
case pos if pos > 0 => pos - 1 | ||
case neg if neg < 0 => len + neg | ||
case _ => 0 | ||
} | ||
|
||
val end = sliceLen match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The MySQL doc mentions that "If len is less than 1, the result is the empty string." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the behavior of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, missed this before, sorry. |
||
case max if max == Integer.MAX_VALUE => max | ||
case x => start + x | ||
} | ||
|
||
str.slice(start, end) | ||
} | ||
|
||
override def eval(input: Row): Any = { | ||
val string = str.eval(input) | ||
|
||
val po = pos.eval(input) | ||
val ln = len.eval(input) | ||
|
||
if ((string == null) || (po == null) || (ln == null)) { | ||
null | ||
} else { | ||
val start = po.asInstanceOf[Int] | ||
val length = ln.asInstanceOf[Int] | ||
|
||
string match { | ||
case ba: Array[Byte] => slice(ba, start, length) | ||
case other => slice(other.toString, start, length) | ||
} | ||
} | ||
} | ||
|
||
override def toString = len match { | ||
case max if max == Integer.MAX_VALUE => s"SUBSTR($str, $pos)" | ||
case _ => s"SUBSTR($str, $pos, $len)" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nullable
could bestr.nullable || pos.nullable || len.nullable
?