maropu commented on a change in pull request #28841:
URL: https://github.com/apache/spark/pull/28841#discussion_r472578754



##########
File path: 
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/PathFilterSuite.scala
##########
@@ -0,0 +1,501 @@
+/*
+ * 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.execution.datasources
+
+import java.io.File
+import java.time.{LocalDateTime, ZoneOffset}
+import java.time.format.DateTimeFormatter
+
+import org.apache.hadoop.fs.Path
+
+import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
+import org.apache.spark.sql.catalyst.util.{stringToFile, CaseInsensitiveMap, 
DateTimeUtils}
+import org.apache.spark.sql.test.SharedSparkSession
+
+class PathFilterSuite extends QueryTest with SharedSparkSession {
+  import testImplicits._
+
+  test("SPARK-31962: when modifiedAfter specified with a past date") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      file.setLastModified(DateTimeUtils.currentTimestamp())
+      val df = spark.read
+        .option("modifiedAfter", "2019-05-10T01:11:00")
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 1)
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore specified with a future date") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val df = spark.read
+        .option("modifiedBefore", "2090-05-10T01:11:00")
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 1)
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore specified with a past date") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      file.setLastModified(DateTimeUtils.currentTimestamp())
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedBefore", "1984-05-01T01:00:00")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with a past date, multiple 
files, one valid") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file1 = new File(dir, "file1.csv")
+      val file2 = new File(dir, "file2.csv")
+      stringToFile(file1, "text")
+      stringToFile(file2, "text")
+      file1.setLastModified(DateTimeUtils.currentTimestamp())
+      file2.setLastModified(0)
+      val df = spark.read
+        .option("modifiedAfter", "2019-05-10T01:11:00")
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 1)
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with a past date, multiple 
files, both valid") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file1 = new File(dir, "file1.csv")
+      val file2 = new File(dir, "file2.csv")
+      stringToFile(file1, "text")
+      stringToFile(file2, "text")
+      file1.setLastModified(DateTimeUtils.currentTimestamp())
+      file2.setLastModified(DateTimeUtils.currentTimestamp())
+      val df = spark.read
+        .option("modifiedAfter", "2019-05-10T01:11:00")
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 2)
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with a past date, multiple 
files, none valid") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file1 = new File(dir, "file1.csv")
+      val file2 = new File(dir, "file2.csv")
+      stringToFile(file1, "text")
+      stringToFile(file2, "text")
+      file1.setLastModified(0)
+      file2.setLastModified(0)
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "1984-05-01T01:00:00")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore specified with a future date, " +
+    "multiple files, both valid") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file1 = new File(dir, "file1.csv")
+      val file2 = new File(dir, "file2.csv")
+      stringToFile(file1, "text")
+      stringToFile(file2, "text")
+      file1.setLastModified(0)
+      file2.setLastModified(0)
+
+      val time = LocalDateTime
+        .now()
+        .plusDays(3)
+        .format(DateTimeFormatter
+          .ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
+
+      val df = spark.read
+        .option("modifiedBefore", time)
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 2)
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore specified with a future date, 
multiple files, one valid") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file1 = new File(dir, "file1.csv")
+      val file2 = new File(dir, "file2.csv")
+      stringToFile(file1, "text")
+      stringToFile(file2, "text")
+
+      file1.setLastModified(0)
+
+      val failTime =
+        LocalDateTime.now().plusDays(3).toEpochSecond(ZoneOffset.UTC)
+      file2.setLastModified(failTime * 1000)
+
+      val time = LocalDateTime
+        .now()
+        .plusHours(10)
+        .format(DateTimeFormatter
+          .ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
+
+      val df = spark.read
+        .option("modifiedBefore", time)
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 1)
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore specified with a future date, " +
+    "multiple files, none valid") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file1 = new File(dir, "file1.csv")
+      val file2 = new File(dir, "file2.csv")
+      stringToFile(file1, "text")
+      stringToFile(file2, "text")
+
+      val time = LocalDateTime
+        .now()
+        .minusDays(1)
+        .format(DateTimeFormatter
+          .ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
+
+      file1.setLastModified(DateTimeUtils.currentTimestamp())
+      file2.setLastModified(DateTimeUtils.currentTimestamp())
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedBefore", time)
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with a past date and " +
+    "pathGlobalFilter returning results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val df = spark.read
+        .option("modifiedAfter", "1984-05-10T01:11:00")
+        .option("pathGlobFilter", "*.csv")
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 1)
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with past date " +
+    "and pathGlobFilter filtering results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "1984-05-01T01:00:00")
+          .option("pathGlobFilter", "*.txt")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with future date and " +
+    "pathGlobFilter returning results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "2050-05-01T01:00:00")
+          .option("pathGlobFilter", "*.csv")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter specified with future date and " +
+    "pathGlobFilter filtering results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "2050-05-01T01:00:00")
+          .option("pathGlobFilter", "*.txt")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore and modifiedAfter are specified out 
of range and " +
+    "pathGlobFilter returning results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "2050-05-01T01:00:00")
+          .option("modifiedBefore", "2050-05-01T01:00:00")
+          .option("pathGlobFilter", "*.csv")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore and modifiedAfter are specified in 
range and " +
+    "pathGlobFilter returning results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val df = spark.read
+        .option("modifiedAfter", "2019-05-01T01:00:00")
+        .option("modifiedBefore", "2025-05-01T01:00:00")
+        .option("pathGlobFilter", "*.csv")
+        .format("csv")
+        .load(path.toString)
+      assert(df.count() == 1)
+    }
+  }
+
+  test("SPARK-31962: when modifiedBefore and modifiedAfter are specified in 
range and " +
+    "pathGlobFilter filtering results") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "2019-05-01T01:00:00")
+          .option("modifiedBefore", "2025-05-01T01:00:00")
+          .option("pathGlobFilter", "*.txt")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(msg.contains("Unable to infer schema for CSV"))
+    }
+  }
+
+  test("SPARK-31962: when modifiedAfter is specified with an invalid date") {
+    withTempDir { dir =>
+      val path = new Path(dir.getCanonicalPath)
+      val file = new File(dir, "file1.csv")
+      stringToFile(file, "text")
+
+      val msg = intercept[AnalysisException] {
+        spark.read
+          .option("modifiedAfter", "2024-05+1 01:00:00")
+          .format("csv")
+          .load(path.toString)
+      }.getMessage
+      assert(
+        msg.contains("The timestamp provided")
+          && msg.contains("modifiedafter")
+          && msg.contains("2024-05+1 01:00:00"))
+    }
+  }
+
+  test("SPARK-31962: PathFilterStrategies - modifiedAfter option") {
+    val options = CaseInsensitiveMap[String](Map("modifiedAfter" -> 
"2010-10-01T01:01:00"))
+    val strategy = PathFilterFactory.create(options)
+    assert(strategy.head.isInstanceOf[ModifiedAfterFilter])
+    assert(strategy.size == 1)
+  }
+
+  test("SPARK-31962: PathFilterStrategies - modifiedBefore option") {
+    val options = CaseInsensitiveMap[String](Map("modifiedBefore" -> 
"2020-10-01T01:01:00"))
+    val strategy = PathFilterFactory.create(options)
+    assert(strategy.head.isInstanceOf[ModifiedBeforeFilter])
+    assert(strategy.size == 1)
+  }
+
+  test("SPARK-31962: PathFilterStrategies - pathGlobFilter option") {
+    val options = CaseInsensitiveMap[String](Map("pathGlobFilter" -> "*.txt"))
+    val strategy = PathFilterFactory.create(options)
+    assert(strategy.head.isInstanceOf[PathGlobFilter])
+    assert(strategy.size == 1)
+  }
+
+  test("SPARK-31962: PathFilterStrategies - no options") {
+    val options = CaseInsensitiveMap[String](Map.empty)
+    val strategy = PathFilterFactory.create(options)
+    assert(strategy.isEmpty)
+  }

Review comment:
       To separate fine-grained tests above from the end-2-end ones, could you 
move them into a new test suite like `PathFilterStrategySuite`?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to