Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/12362#discussion_r59761027
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/command/tables.scala ---
@@ -17,9 +17,45 @@
package org.apache.spark.sql.execution.command
-import org.apache.spark.sql.{Row, SQLContext}
+import org.apache.spark.sql.{AnalysisException, Row, SQLContext}
import org.apache.spark.sql.catalyst.TableIdentifier
-import org.apache.spark.sql.catalyst.catalog.CatalogTable
+import org.apache.spark.sql.catalyst.catalog.{CatalogTable,
CatalogTableType}
+
+/**
+ * A command to create a table with the same definition of the given
existing table.
+ *
+ * The syntax of using this command in SQL is:
+ * {{{
+ * CREATE TABLE [IF NOT EXISTS] [db_name.]table_name
+ * LIKE [other_db_name.]existing_table_name
+ * }}}
+ */
+case class CreateTableLike(
+ targetTable: TableIdentifier,
+ sourceTable: TableIdentifier,
+ ifNotExists: Boolean) extends RunnableCommand {
+
+ override def run(sqlContext: SQLContext): Seq[Row] = {
+ val catalog = sqlContext.sessionState.catalog
+ if (!catalog.tableExists(sourceTable)) {
+ throw new AnalysisException(
+ s"Source table in CREATE TABLE LIKE does not exist:
'$sourceTable'")
+ }
+ if (catalog.isTemporaryTable(sourceTable)) {
+ throw new AnalysisException(
+ s"Source table in CREATE TABLE LIKE cannot be temporary:
'$sourceTable'")
+ }
+
+ val tableToCreate = catalog.getTableMetadata(sourceTable).copy(
--- End diff --
I think things like `lastAccessTime` are exceptions. Otherwise if we add a
new field to `CatalogTable` in the future you might forget to copy it here.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]