Skip to content
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-8420][SQL] Fix comparision of timestamps/dates with strings #6888

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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 @@ -277,15 +277,26 @@ trait HiveTypeCoercion {
case a @ BinaryArithmetic(left, right @ StringType()) =>
a.makeCopy(Array(left, Cast(right, DoubleType)))

// we should cast all timestamp/date/string compare into string compare
// For equality between string and timestamp we cast the string to a timestamp
// so that things like rounding of subsecond precision does not affect the comparison.
case p @ Equality(left @ StringType(), right @ TimestampType()) =>
p.makeCopy(Array(Cast(left, TimestampType), right))
case p @ Equality(left @ TimestampType(), right @ StringType()) =>
p.makeCopy(Array(left, Cast(right, TimestampType)))

// We should cast all relative timestamp/date/string comparison into string comparisions
// This behaves as a user would expect because timestamp strings sort lexicographically.
// i.e. TimeStamp(2013-01-01 00:00 ...) < "2014" = true
case p @ BinaryComparison(left @ StringType(), right @ DateType()) =>
p.makeCopy(Array(left, Cast(right, StringType)))
case p @ BinaryComparison(left @ DateType(), right @ StringType()) =>
p.makeCopy(Array(Cast(left, StringType), right))
case p @ BinaryComparison(left @ StringType(), right @ TimestampType()) =>
p.makeCopy(Array(Cast(left, TimestampType), right))
p.makeCopy(Array(left, Cast(right, StringType)))
case p @ BinaryComparison(left @ TimestampType(), right @ StringType()) =>
p.makeCopy(Array(left, Cast(right, TimestampType)))
p.makeCopy(Array(Cast(left, StringType), right))

// Comparisons between dates and timestamps.
case p @ BinaryComparison(left @ TimestampType(), right @ DateType()) =>
p.makeCopy(Array(Cast(left, StringType), Cast(right, StringType)))
case p @ BinaryComparison(left @ DateType(), right @ TimestampType()) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ private[sql] object BinaryComparison {
def unapply(e: BinaryComparison): Option[(Expression, Expression)] = Some((e.left, e.right))
}

/** An extractor that matches both standard 3VL equality and null-safe equality. */
private[sql] object Equality {
def unapply(e: BinaryComparison): Option[(Expression, Expression)] = e match {
case EqualTo(l, r) => Some((l, r))
case EqualNullSafe(l, r) => Some((l, r))
case _ => None
}
}

case class EqualTo(left: Expression, right: Expression) extends BinaryComparison {
override def symbol: String = "="

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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

import java.sql.{Date, Timestamp}

class DataFrameDateTimeSuite extends QueryTest {

private lazy val ctx = org.apache.spark.sql.test.TestSQLContext
import ctx.implicits._

test("timestamp comparison with date strings") {
val df = Seq(
(1, Timestamp.valueOf("2015-01-01 00:00:00")),
(2, Timestamp.valueOf("2014-01-01 00:00:00"))).toDF("i", "t")

checkAnswer(
df.select("t").filter($"t" <= "2014-06-01"),
Row(Timestamp.valueOf("2014-01-01 00:00:00")) :: Nil)


checkAnswer(
df.select("t").filter($"t" >= "2014-06-01"),
Row(Timestamp.valueOf("2015-01-01 00:00:00")) :: Nil)
}

test("date comparison with date strings") {
val df = Seq(
(1, Date.valueOf("2015-01-01")),
(2, Date.valueOf("2014-01-01"))).toDF("i", "t")

checkAnswer(
df.select("t").filter($"t" <= "2014-06-01"),
Row(Date.valueOf("2014-01-01")) :: Nil)


checkAnswer(
df.select("t").filter($"t" >= "2014-06-01"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor: change it to $"t" >= "2014"?

Row(Date.valueOf("2015-01-01")) :: Nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package org.apache.spark.sql

import org.scalatest.BeforeAndAfterAll

import java.sql.Timestamp

import org.apache.spark.sql.catalyst.DefaultParserDialect
import org.apache.spark.sql.catalyst.errors.DialectException
import org.apache.spark.sql.execution.GeneratedAggregate
Expand Down Expand Up @@ -345,6 +347,8 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll with SQLTestUtils {
}

test("SPARK-3173 Timestamp support in the parser") {
(0 to 3).map(i => Tuple1(new Timestamp(i))).toDF("time").registerTempTable("timestamps")

checkAnswer(sql(
"SELECT time FROM timestamps WHERE time='1969-12-31 16:00:00.0'"),
Row(java.sql.Timestamp.valueOf("1969-12-31 16:00:00")))
Expand Down
6 changes: 0 additions & 6 deletions sql/core/src/test/scala/org/apache/spark/sql/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ object TestData {
"3, C3, true, null" ::
"4, D4, true, 2147483644" :: Nil)

case class TimestampField(time: Timestamp)
val timestamps = TestSQLContext.sparkContext.parallelize((0 to 3).map { i =>
TimestampField(new Timestamp(i))
})
timestamps.toDF().registerTempTable("timestamps")

case class IntField(i: Int)
// An RDD with 4 elements and 8 partitions
val withEmptyParts = TestSQLContext.sparkContext.parallelize((1 to 4).map(IntField), 8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,18 @@ class InMemoryColumnarQuerySuite extends QueryTest {
}

test("SPARK-2729 regression: timestamp data type") {
val timestamps = (0 to 3).map(i => Tuple1(new Timestamp(i))).toDF("time")
timestamps.registerTempTable("timestamps")

checkAnswer(
sql("SELECT time FROM timestamps"),
timestamps.collect().toSeq.map(Row.fromTuple))
timestamps.collect().toSeq)

ctx.cacheTable("timestamps")

checkAnswer(
sql("SELECT time FROM timestamps"),
timestamps.collect().toSeq.map(Row.fromTuple))
timestamps.collect().toSeq)
}

test("SPARK-3320 regression: batched column buffer building should work with empty partitions") {
Expand Down