Skip to content

Commit cc965ee

Browse files
ueshinrxin
authored andcommitted
[SPARK-2518][SQL] Fix foldability of Substring expression.
This is a follow-up of #1428. Author: Takuya UESHIN <[email protected]> Closes #1432 from ueshin/issues/SPARK-2518 and squashes the following commits: 37d1ace [Takuya UESHIN] Fix foldability of Substring expression.
1 parent fc7edc9 commit cc965ee

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ case class Substring(str: Expression, pos: Expression, len: Expression) extends
216216

217217
type EvaluatedType = Any
218218

219+
override def foldable = str.foldable && pos.foldable && len.foldable
220+
219221
def nullable: Boolean = str.nullable || pos.nullable || len.nullable
220222
def dataType: DataType = {
221223
if (!resolved) {

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/ConstantFoldingSuite.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ class ConstantFoldingSuite extends PlanTest {
201201
Like(Literal(null, StringType), "abc") as 'c13,
202202
Like("abc", Literal(null, StringType)) as 'c14,
203203

204-
Upper(Literal(null, StringType)) as 'c15)
204+
Upper(Literal(null, StringType)) as 'c15,
205+
206+
Substring(Literal(null, StringType), 0, 1) as 'c16,
207+
Substring("abc", Literal(null, IntegerType), 1) as 'c17,
208+
Substring("abc", 0, Literal(null, IntegerType)) as 'c18
209+
)
205210

206211
val optimized = Optimize(originalQuery.analyze)
207212

@@ -228,8 +233,12 @@ class ConstantFoldingSuite extends PlanTest {
228233
Literal(null, BooleanType) as 'c13,
229234
Literal(null, BooleanType) as 'c14,
230235

231-
Literal(null, StringType) as 'c15)
232-
.analyze
236+
Literal(null, StringType) as 'c15,
237+
238+
Literal(null, StringType) as 'c16,
239+
Literal(null, StringType) as 'c17,
240+
Literal(null, StringType) as 'c18
241+
).analyze
233242

234243
comparePlans(optimized, correctAnswer)
235244
}

0 commit comments

Comments
 (0)