|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.hive |
19 | 19 |
|
| 20 | +import java.io.File |
| 21 | + |
20 | 22 | import org.apache.spark.sql.columnar.{InMemoryColumnarTableScan, InMemoryRelation} |
21 | 23 | import org.apache.spark.sql.hive.test.TestHive |
22 | 24 | import org.apache.spark.sql.hive.test.TestHive._ |
23 | | -import org.apache.spark.sql.{AnalysisException, DataFrame, QueryTest} |
| 25 | +import org.apache.spark.sql.{SaveMode, AnalysisException, DataFrame, QueryTest} |
24 | 26 | import org.apache.spark.storage.RDDBlockId |
| 27 | +import org.apache.spark.util.Utils |
25 | 28 |
|
26 | 29 | class CachedTableSuite extends QueryTest { |
27 | 30 |
|
@@ -155,4 +158,48 @@ class CachedTableSuite extends QueryTest { |
155 | 158 | assertCached(table("udfTest")) |
156 | 159 | uncacheTable("udfTest") |
157 | 160 | } |
| 161 | + |
| 162 | + test("REFRESH TABLE also needs to recache the data") { |
| 163 | + val tempPath: File = Utils.createTempDir() |
| 164 | + tempPath.delete() |
| 165 | + table("src").save(tempPath.toString, "parquet", SaveMode.Overwrite) |
| 166 | + createExternalTable("refreshTable", tempPath.toString, "parquet") |
| 167 | + checkAnswer( |
| 168 | + table("refreshTable"), |
| 169 | + table("src").collect()) |
| 170 | + // Cache the table. |
| 171 | + sql("CACHE TABLE refreshTable") |
| 172 | + assertCached(table("refreshTable")) |
| 173 | + // Append new data. |
| 174 | + table("src").save(tempPath.toString, "parquet", SaveMode.Append) |
| 175 | + // We are still using the old data. |
| 176 | + assertCached(table("refreshTable")) |
| 177 | + checkAnswer( |
| 178 | + table("refreshTable"), |
| 179 | + table("src").collect()) |
| 180 | + // Refresh the table. |
| 181 | + sql("REFRESH TABLE refreshTable") |
| 182 | + // We are using the new data. |
| 183 | + assertCached(table("refreshTable")) |
| 184 | + checkAnswer( |
| 185 | + table("refreshTable"), |
| 186 | + table("src").unionAll(table("src")).collect()) |
| 187 | + |
| 188 | + // Drop the table and create it again. |
| 189 | + sql("DROP TABLE refreshTable") |
| 190 | + createExternalTable("refreshTable", tempPath.toString, "parquet") |
| 191 | + // It is not cached. |
| 192 | + assert(!isCached("refreshTable"), "refreshTable should not be cached.") |
| 193 | + // Refresh the table. REFRESH TABLE command should not make a uncached |
| 194 | + // table cached. |
| 195 | + sql("REFRESH TABLE refreshTable") |
| 196 | + checkAnswer( |
| 197 | + table("refreshTable"), |
| 198 | + table("src").unionAll(table("src")).collect()) |
| 199 | + // It is not cached. |
| 200 | + assert(!isCached("refreshTable"), "refreshTable should not be cached.") |
| 201 | + |
| 202 | + sql("DROP TABLE refreshTable") |
| 203 | + Utils.deleteRecursively(tempPath) |
| 204 | + } |
158 | 205 | } |
0 commit comments