Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -201,9 +201,9 @@ statement
| (DESC | DESCRIBE) FUNCTION EXTENDED? describeFuncName #describeFunction
| (DESC | DESCRIBE) database EXTENDED? db=errorCapturingIdentifier #describeDatabase
| (DESC | DESCRIBE) TABLE? option=(EXTENDED | FORMATTED)?
multipartIdentifier partitionSpec? describeColName? #describeTable
multipartIdentifier partitionSpec? describeColName? #describeTable
| (DESC | DESCRIBE) QUERY? query #describeQuery
| REFRESH TABLE tableIdentifier #refreshTable
| REFRESH TABLE multipartIdentifier #refreshTable
| REFRESH (STRING | .*?) #refreshResource
| CACHE LAZY? TABLE tableIdentifier
(OPTIONS options=tablePropertyList)? (AS? query)? #cacheTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
writeOptions = c.options.filterKeys(_ != "path"),
ignoreIfExists = c.ifNotExists)

case RefreshTableStatement(NonSessionCatalog(catalog, tableName)) =>
RefreshTable(catalog.asTableCatalog, tableName.asIdentifier)

case c @ ReplaceTableStatement(
NonSessionCatalog(catalog, tableName), _, _, _, _, _, _, _, _, _) =>
ReplaceTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2799,4 +2799,16 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
val partitionKeys = Option(ctx.partitionSpec).map(visitNonOptionalPartitionSpec)
ShowPartitionsStatement(table, partitionKeys)
}

/**
* Create a [[RefreshTableStatement]].
*
* For example:
* {{{
* REFRESH TABLE multi_part_name
* }}}
*/
override def visitRefreshTable(ctx: RefreshTableContext): LogicalPlan = withOrigin(ctx) {
RefreshTableStatement(visitMultipartIdentifier(ctx.multipartIdentifier()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,11 @@ case class TruncateTableStatement(
/**
* A SHOW PARTITIONS statement, as parsed from SQL
*/
case class ShowPartitionsStatement(tableName: Seq[String],
case class ShowPartitionsStatement(
tableName: Seq[String],
partitionSpec: Option[TablePartitionSpec]) extends ParsedStatement

/**
* A REFRESH TABLE statement, as parsed from SQL
*/
case class RefreshTableStatement(tableName: Seq[String]) extends ParsedStatement
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,10 @@ case class SetCatalogAndNamespace(
catalogManager: CatalogManager,
catalogName: Option[String],
namespace: Option[Seq[String]]) extends Command

/**
* The logical plan of the REFRESH TABLE command that works for v2 catalogs.
*/
case class RefreshTable(
catalog: TableCatalog,
ident: Identifier) extends Command
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,12 @@ class DDLParserSuite extends AnalysisTest {
comparePlans(parsed5, expected5)
}

test("REFRESH TABLE table") {
comparePlans(
parsePlan("REFRESH TABLE a.b.c"),
RefreshTableStatement(Seq("a", "b", "c")))
}

private case class TableSpec(
name: Seq[String],
schema: Option[StructType],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BasicInMemoryTableCatalog extends TableCatalog {
protected val tables: util.Map[Identifier, InMemoryTable] =
new ConcurrentHashMap[Identifier, InMemoryTable]()

private val invalidatedTables: util.Set[Identifier] = ConcurrentHashMap.newKeySet()

private var _name: Option[String] = None

override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = {
Expand All @@ -55,6 +57,10 @@ class BasicInMemoryTableCatalog extends TableCatalog {
}
}

override def invalidateTable(ident: Identifier): Unit = {
invalidatedTables.add(ident)
}

override def createTable(
ident: Identifier,
schema: StructType,
Expand Down Expand Up @@ -104,6 +110,10 @@ class BasicInMemoryTableCatalog extends TableCatalog {
}
}

def isTableInvalidated(ident: Identifier): Boolean = {
invalidatedTables.contains(ident)
}

def clearTables(): Unit = {
tables.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.connector.catalog.{CatalogManager, CatalogPlugin, LookupCatalog, TableChange, V1Table}
import org.apache.spark.sql.connector.expressions.Transform
import org.apache.spark.sql.execution.command.{AlterTableAddColumnsCommand, AlterTableRecoverPartitionsCommand, AlterTableSetLocationCommand, AlterTableSetPropertiesCommand, AlterTableUnsetPropertiesCommand, AnalyzeColumnCommand, AnalyzePartitionCommand, AnalyzeTableCommand, CreateDatabaseCommand, DescribeColumnCommand, DescribeTableCommand, DropTableCommand, ShowPartitionsCommand, ShowTablesCommand, TruncateTableCommand}
import org.apache.spark.sql.execution.datasources.{CreateTable, DataSource}
import org.apache.spark.sql.execution.datasources.{CreateTable, DataSource, RefreshTable}
import org.apache.spark.sql.execution.datasources.v2.FileDataSourceV2
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{HIVE_TYPE_STRING, HiveStringType, MetadataBuilder, StructField, StructType}
Expand Down Expand Up @@ -216,6 +216,9 @@ class ResolveSessionCatalog(
ignoreIfExists = c.ifNotExists)
}

case RefreshTableStatement(SessionCatalog(_, tableName)) =>
RefreshTable(tableName.asTableIdentifier)

// For REPLACE TABLE [AS SELECT], we should fail if the catalog is resolved to the
// session catalog and the table provider is not v2.
case c @ ReplaceTableStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,6 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder(conf) {
ShowCreateTableCommand(table)
}

/**
* Create a [[RefreshTable]] logical plan.
*/
override def visitRefreshTable(ctx: RefreshTableContext): LogicalPlan = withOrigin(ctx) {
RefreshTable(visitTableIdentifier(ctx.tableIdentifier))
}

/**
* Create a [[RefreshResource]] logical plan.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import scala.collection.mutable
import org.apache.spark.sql.{AnalysisException, Strategy}
import org.apache.spark.sql.catalyst.expressions.{And, AttributeReference, AttributeSet, Expression, PredicateHelper, SubqueryExpression}
import org.apache.spark.sql.catalyst.planning.PhysicalOperation
import org.apache.spark.sql.catalyst.plans.logical.{AlterTable, AppendData, CreateNamespace, CreateTableAsSelect, CreateV2Table, DeleteFromTable, DescribeTable, DropTable, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, Repartition, ReplaceTable, ReplaceTableAsSelect, SetCatalogAndNamespace, ShowNamespaces, ShowTables}
import org.apache.spark.sql.catalyst.plans.logical.{AlterTable, AppendData, CreateNamespace, CreateTableAsSelect, CreateV2Table, DeleteFromTable, DescribeTable, DropTable, LogicalPlan, OverwriteByExpression, OverwritePartitionsDynamic, RefreshTable, Repartition, ReplaceTable, ReplaceTableAsSelect, SetCatalogAndNamespace, ShowNamespaces, ShowTables}
import org.apache.spark.sql.connector.catalog.{StagingTableCatalog, TableCapability}
import org.apache.spark.sql.connector.read.{Scan, ScanBuilder, SupportsPushDownFilters, SupportsPushDownRequiredColumns}
import org.apache.spark.sql.connector.read.streaming.{ContinuousStream, MicroBatchStream}
Expand Down Expand Up @@ -193,6 +193,9 @@ object DataSourceV2Strategy extends Strategy with PredicateHelper {
catalog, ident, parts, query, planLater(query), props, writeOptions, ifNotExists) :: Nil
}

case RefreshTable(catalog, ident) =>
RefreshTableExec(catalog, ident) :: Nil

case ReplaceTable(catalog, ident, schema, parts, props, orCreate) =>
catalog match {
case staging: StagingTableCatalog =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.v2

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.connector.catalog.{Identifier, TableCatalog}

case class RefreshTableExec(
catalog: TableCatalog,
ident: Identifier) extends V2CommandExec {
override protected def run(): Seq[InternalRow] = {
catalog.invalidateTable(ident)
Seq.empty
}

override def output: Seq[Attribute] = Seq.empty
}
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,20 @@ class DataSourceV2SQLSuite
}
}

test("REFRESH TABLE: v2 table") {
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
sql(s"CREATE TABLE $t (id bigint, data string) USING foo")

val testCatalog = catalog("testcat").asTableCatalog.asInstanceOf[InMemoryTableCatalog]
val identifier = Identifier.of(Array("ns1", "ns2"), "tbl")

assert(!testCatalog.isTableInvalidated(identifier))
sql(s"REFRESH TABLE $t")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any suggestion for a reasonable test here?

ALTER TABLE doesn't require REFRESH TABLE in the following existing test, correct?

      sql(s"CREATE TABLE $t (id int, data string) USING $v2Format")
      sql(s"ALTER TABLE $t DROP COLUMN data")
      val table = getTableMetadata(t) // <- this calls TableCatalog.loadTable

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a reasonable test is to create a custom v2 implementation which implements invalidateTable.

Copy link
Contributor

@cloud-fan cloud-fan Oct 22, 2019

Choose a reason for hiding this comment

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

We can implement InMemoryTable.invalidateTable which simply set a flag on a global object to indicate it has been called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! Exactly what I was planning to do. :)

assert(testCatalog.isTableInvalidated(identifier))
}
}

test("REPLACE TABLE: v1 table") {
val e = intercept[AnalysisException] {
sql(s"CREATE OR REPLACE TABLE tbl (a int) USING ${classOf[SimpleScanSource].getName}")
Expand Down Expand Up @@ -1211,28 +1225,16 @@ class DataSourceV2SQLSuite
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
spark.sql(s"CREATE TABLE $t (id bigint, data string) USING foo")

val e = intercept[AnalysisException] {
sql(s"ANALYZE TABLE $t COMPUTE STATISTICS")
}
assert(e.message.contains("ANALYZE TABLE is only supported with v1 tables"))

val e2 = intercept[AnalysisException] {
sql(s"ANALYZE TABLE $t COMPUTE STATISTICS FOR ALL COLUMNS")
}
assert(e2.message.contains("ANALYZE TABLE is only supported with v1 tables"))
testV1Command("ANALYZE TABLE", s"$t COMPUTE STATISTICS")
testV1Command("ANALYZE TABLE", s"$t COMPUTE STATISTICS FOR ALL COLUMNS")
}
}

test("MSCK REPAIR TABLE") {
val t = "testcat.ns1.ns2.tbl"
withTable(t) {
spark.sql(s"CREATE TABLE $t (id bigint, data string) USING foo")

val e = intercept[AnalysisException] {
sql(s"MSCK REPAIR TABLE $t")
}
assert(e.message.contains("MSCK REPAIR TABLE is only supported with v1 tables"))
testV1Command("MSCK REPAIR TABLE", t)
}
}

Expand All @@ -1246,15 +1248,8 @@ class DataSourceV2SQLSuite
|PARTITIONED BY (id)
""".stripMargin)

val e1 = intercept[AnalysisException] {
sql(s"TRUNCATE TABLE $t")
}
assert(e1.message.contains("TRUNCATE TABLE is only supported with v1 tables"))

val e2 = intercept[AnalysisException] {
sql(s"TRUNCATE TABLE $t PARTITION(id='1')")
}
assert(e2.message.contains("TRUNCATE TABLE is only supported with v1 tables"))
testV1Command("TRUNCATE TABLE", t)
testV1Command("TRUNCATE TABLE", s"$t PARTITION(id='1')")
}
}

Expand All @@ -1268,16 +1263,16 @@ class DataSourceV2SQLSuite
|PARTITIONED BY (id)
""".stripMargin)

val e1 = intercept[AnalysisException] {
val partition = sql(s"SHOW PARTITIONS $t")
}
assert(e1.message.contains("SHOW PARTITIONS is only supported with v1 tables"))
testV1Command("SHOW PARTITIONS", t)
testV1Command("SHOW PARTITIONS", s"$t PARTITION(id='1')")
}
}

val e2 = intercept[AnalysisException] {
val partition2 = sql(s"SHOW PARTITIONS $t PARTITION(id='1')")
}
assert(e2.message.contains("SHOW PARTITIONS is only supported with v1 tables"))
private def testV1Command(sqlCommand: String, sqlParams: String): Unit = {
val e = intercept[AnalysisException] {
sql(s"$sqlCommand $sqlParams")
}
assert(e.message.contains(s"$sqlCommand is only supported with v1 tables"))
}

private def assertAnalysisError(sqlStatement: String, expectedError: String): Unit = {
Expand Down