@@ -20,10 +20,10 @@ package org.apache.spark.sql.hive.execution
20
20
import scala .util .control .NonFatal
21
21
22
22
import org .apache .spark .sql .{AnalysisException , Row , SaveMode , SparkSession }
23
- import org .apache .spark .sql .catalyst .catalog .{CatalogTable , SimpleCatalogRelation }
24
- import org .apache .spark .sql .catalyst .plans .logical .{InsertIntoTable , LogicalPlan , SubqueryAlias }
23
+ import org .apache .spark .sql .catalyst .analysis .UnresolvedRelation
24
+ import org .apache .spark .sql .catalyst .catalog .CatalogTable
25
+ import org .apache .spark .sql .catalyst .plans .logical .{InsertIntoTable , LogicalPlan }
25
26
import org .apache .spark .sql .execution .command .RunnableCommand
26
- import org .apache .spark .sql .hive .MetastoreRelation
27
27
28
28
29
29
/**
@@ -44,40 +44,6 @@ case class CreateHiveTableAsSelectCommand(
44
44
override def innerChildren : Seq [LogicalPlan ] = Seq (query)
45
45
46
46
override def run (sparkSession : SparkSession ): Seq [Row ] = {
47
- lazy val metastoreRelation : MetastoreRelation = {
48
- import org .apache .hadoop .hive .ql .io .HiveIgnoreKeyTextOutputFormat
49
- import org .apache .hadoop .hive .serde2 .`lazy` .LazySimpleSerDe
50
- import org .apache .hadoop .io .Text
51
- import org .apache .hadoop .mapred .TextInputFormat
52
-
53
- val withFormat =
54
- tableDesc.withNewStorage(
55
- inputFormat =
56
- tableDesc.storage.inputFormat.orElse(Some (classOf [TextInputFormat ].getName)),
57
- outputFormat =
58
- tableDesc.storage.outputFormat
59
- .orElse(Some (classOf [HiveIgnoreKeyTextOutputFormat [Text , Text ]].getName)),
60
- serde = tableDesc.storage.serde.orElse(Some (classOf [LazySimpleSerDe ].getName)),
61
- compressed = tableDesc.storage.compressed)
62
-
63
- val withSchema = if (withFormat.schema.isEmpty) {
64
- tableDesc.copy(schema = query.schema)
65
- } else {
66
- withFormat
67
- }
68
-
69
- sparkSession.sessionState.catalog.createTable(withSchema, ignoreIfExists = true )
70
-
71
- // Get the Metastore Relation
72
- sparkSession.sessionState.catalog.lookupRelation(tableIdentifier) match {
73
- case SubqueryAlias (_, r : SimpleCatalogRelation , _) =>
74
- val tableMeta = r.metadata
75
- MetastoreRelation (tableMeta.database, tableMeta.identifier.table)(tableMeta, sparkSession)
76
- }
77
- }
78
- // TODO ideally, we should get the output data ready first and then
79
- // add the relation into catalog, just in case of failure occurs while data
80
- // processing.
81
47
if (sparkSession.sessionState.catalog.tableExists(tableIdentifier)) {
82
48
assert(mode != SaveMode .Overwrite ,
83
49
s " Expect the table $tableIdentifier has been dropped when the save mode is Overwrite " )
@@ -89,12 +55,30 @@ case class CreateHiveTableAsSelectCommand(
89
55
// Since the table already exists and the save mode is Ignore, we will just return.
90
56
return Seq .empty
91
57
}
92
- sparkSession.sessionState.executePlan(InsertIntoTable (
93
- metastoreRelation, Map (), query, overwrite = false , ifNotExists = false )).toRdd
58
+
59
+ sparkSession.sessionState.executePlan(
60
+ InsertIntoTable (
61
+ UnresolvedRelation (tableIdentifier),
62
+ Map (),
63
+ query,
64
+ overwrite = false ,
65
+ ifNotExists = false )).toRdd
94
66
} else {
67
+ // TODO ideally, we should get the output data ready first and then
68
+ // add the relation into catalog, just in case of failure occurs while data
69
+ // processing.
70
+ assert(tableDesc.schema.isEmpty)
71
+ sparkSession.sessionState.catalog.createTable(
72
+ tableDesc.copy(schema = query.schema), ignoreIfExists = false )
73
+
95
74
try {
96
- sparkSession.sessionState.executePlan(InsertIntoTable (
97
- metastoreRelation, Map (), query, overwrite = true , ifNotExists = false )).toRdd
75
+ sparkSession.sessionState.executePlan(
76
+ InsertIntoTable (
77
+ UnresolvedRelation (tableIdentifier),
78
+ Map (),
79
+ query,
80
+ overwrite = true ,
81
+ ifNotExists = false )).toRdd
98
82
} catch {
99
83
case NonFatal (e) =>
100
84
// drop the created table.
0 commit comments