carbondata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jack...@apache.org
Subject carbondata git commit: [CARBONDATA-1895] Fix issue of create table if not exists
Date Sun, 17 Dec 2017 02:52:27 GMT
Repository: carbondata
Updated Branches:
  refs/heads/master 4a7fc666c -> 9f332be24


[CARBONDATA-1895] Fix issue of create table if not exists

Fix issue of create table if not exists

This closes #1657


Project: http://git-wip-us.apache.org/repos/asf/carbondata/repo
Commit: http://git-wip-us.apache.org/repos/asf/carbondata/commit/9f332be2
Tree: http://git-wip-us.apache.org/repos/asf/carbondata/tree/9f332be2
Diff: http://git-wip-us.apache.org/repos/asf/carbondata/diff/9f332be2

Branch: refs/heads/master
Commit: 9f332be245598c73e4fe81495182293dc8aa6c32
Parents: 4a7fc66
Author: chenerlu <chenerlu@huawei.com>
Authored: Wed Dec 13 23:36:49 2017 +0800
Committer: Jacky Li <jacky.likun@qq.com>
Committed: Sun Dec 17 10:52:19 2017 +0800

----------------------------------------------------------------------
 .../TestCreateTableIfNotExists.scala            |  47 ++++++++
 .../table/CarbonCreateTableCommand.scala        | 106 +++++++++----------
 2 files changed, 100 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/carbondata/blob/9f332be2/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/createTable/TestCreateTableIfNotExists.scala
----------------------------------------------------------------------
diff --git a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/createTable/TestCreateTableIfNotExists.scala
b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/createTable/TestCreateTableIfNotExists.scala
new file mode 100644
index 0000000..a8fcf12
--- /dev/null
+++ b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/createTable/TestCreateTableIfNotExists.scala
@@ -0,0 +1,47 @@
+/*
+ * 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.carbondata.spark.testsuite.createTable
+
+import org.apache.spark.sql.test.util.QueryTest
+import org.scalatest.BeforeAndAfterAll
+
+class TestCreateTableIfNotExists extends QueryTest with BeforeAndAfterAll {
+
+  override def beforeAll {
+    sql("use default")
+    sql("drop table if exists test")
+  }
+
+  test("test create table if not exists") {
+    sql("create table test(a int, b string) stored by 'carbondata'")
+    try {
+      // table creation should be successful
+      sql("create table if not exists test(a int, b string) stored by 'carbondata'")
+      assert(true)
+    } catch {
+      case ex: Exception =>
+        assert(false)
+    }
+  }
+
+  override def afterAll {
+    sql("use default")
+    sql("drop table if exists test")
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/carbondata/blob/9f332be2/integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/table/CarbonCreateTableCommand.scala
----------------------------------------------------------------------
diff --git a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/table/CarbonCreateTableCommand.scala
b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/table/CarbonCreateTableCommand.scala
index 78b9634..314e551 100644
--- a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/table/CarbonCreateTableCommand.scala
+++ b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/table/CarbonCreateTableCommand.scala
@@ -62,66 +62,66 @@ case class CarbonCreateTableCommand(
           s"Table [$tableName] already exists under database [$dbName]")
         throw new TableAlreadyExistsException(dbName, tableName)
       }
-    }
-
-    val tablePath = tableLocation.getOrElse(
-      CarbonEnv.getTablePath(Some(dbName), tableName)(sparkSession))
-    tableInfo.setTablePath(tablePath)
-    val tableIdentifier = AbsoluteTableIdentifier.from(tablePath, dbName, tableName)
+    } else {
+      val tablePath = tableLocation.getOrElse(
+        CarbonEnv.getTablePath(Some(dbName), tableName)(sparkSession))
+      tableInfo.setTablePath(tablePath)
+      val tableIdentifier = AbsoluteTableIdentifier.from(tablePath, dbName, tableName)
 
-    // Add validation for sort scope when create table
-    val sortScope = tableInfo.getFactTable.getTableProperties.asScala
-      .getOrElse("sort_scope", CarbonCommonConstants.LOAD_SORT_SCOPE_DEFAULT)
-    if (!CarbonUtil.isValidSortOption(sortScope)) {
-      throw new InvalidConfigurationException(
-        s"Passing invalid SORT_SCOPE '$sortScope', valid SORT_SCOPE are 'NO_SORT', 'BATCH_SORT',"
+
-        s" 'LOCAL_SORT' and 'GLOBAL_SORT' ")
-    }
+      // Add validation for sort scope when create table
+      val sortScope = tableInfo.getFactTable.getTableProperties.asScala
+        .getOrElse("sort_scope", CarbonCommonConstants.LOAD_SORT_SCOPE_DEFAULT)
+      if (!CarbonUtil.isValidSortOption(sortScope)) {
+        throw new InvalidConfigurationException(
+          s"Passing invalid SORT_SCOPE '$sortScope', valid SORT_SCOPE are 'NO_SORT'," +
+          s" 'BATCH_SORT', 'LOCAL_SORT' and 'GLOBAL_SORT' ")
+      }
 
-    if (tableInfo.getFactTable.getListOfColumns.size <= 0) {
-      CarbonException.analysisException("Table should have at least one column.")
-    }
+      if (tableInfo.getFactTable.getListOfColumns.size <= 0) {
+        CarbonException.analysisException("Table should have at least one column.")
+      }
 
-    val operationContext = new OperationContext
-    val createTablePreExecutionEvent: CreateTablePreExecutionEvent =
-      CreateTablePreExecutionEvent(sparkSession, tableIdentifier, Some(tableInfo))
-    OperationListenerBus.getInstance.fireEvent(createTablePreExecutionEvent, operationContext)
-    val catalog = CarbonEnv.getInstance(sparkSession).carbonMetastore
-    val carbonSchemaString = catalog.generateTableSchemaString(tableInfo, tableIdentifier)
-    if (createDSTable) {
-      try {
-        val tablePath = tableIdentifier.getTablePath
-        val carbonRelation = CarbonSparkUtil.createCarbonRelation(tableInfo, tablePath)
-        val rawSchema = CarbonSparkUtil.getRawSchema(carbonRelation)
-        sparkSession.sparkContext.setLocalProperty(EXECUTION_ID_KEY, null)
-        sparkSession.sql(
-          s"""CREATE TABLE $dbName.$tableName
-             |(${ rawSchema })
-             |USING org.apache.spark.sql.CarbonSource
-             |OPTIONS (
-             |  tableName "$tableName",
-             |  dbName "$dbName",
-             |  tablePath "$tablePath",
-             |  path "$tablePath"
-             |  $carbonSchemaString)
+      val operationContext = new OperationContext
+      val createTablePreExecutionEvent: CreateTablePreExecutionEvent =
+        CreateTablePreExecutionEvent(sparkSession, tableIdentifier, Some(tableInfo))
+      OperationListenerBus.getInstance.fireEvent(createTablePreExecutionEvent, operationContext)
+      val catalog = CarbonEnv.getInstance(sparkSession).carbonMetastore
+      val carbonSchemaString = catalog.generateTableSchemaString(tableInfo, tableIdentifier)
+      if (createDSTable) {
+        try {
+          val tablePath = tableIdentifier.getTablePath
+          val carbonRelation = CarbonSparkUtil.createCarbonRelation(tableInfo, tablePath)
+          val rawSchema = CarbonSparkUtil.getRawSchema(carbonRelation)
+          sparkSession.sparkContext.setLocalProperty(EXECUTION_ID_KEY, null)
+          sparkSession.sql(
+            s"""CREATE TABLE $dbName.$tableName
+               |(${ rawSchema })
+               |USING org.apache.spark.sql.CarbonSource
+               |OPTIONS (
+               |  tableName "$tableName",
+               |  dbName "$dbName",
+               |  tablePath "$tablePath",
+               |  path "$tablePath"
+               |  $carbonSchemaString)
              """.stripMargin)
-      } catch {
-        case e: AnalysisException => throw e
-        case e: Exception =>
-          // call the drop table to delete the created table.
-          CarbonEnv.getInstance(sparkSession).carbonMetastore
-            .dropTable(tableIdentifier)(sparkSession)
+        } catch {
+          case e: AnalysisException => throw e
+          case e: Exception =>
+            // call the drop table to delete the created table.
+            CarbonEnv.getInstance(sparkSession).carbonMetastore
+              .dropTable(tableIdentifier)(sparkSession)
 
-          val msg = s"Create table'$tableName' in database '$dbName' failed."
-          LOGGER.audit(msg)
-          LOGGER.error(e, msg)
-          CarbonException.analysisException(msg)
+            val msg = s"Create table'$tableName' in database '$dbName' failed."
+            LOGGER.audit(msg)
+            LOGGER.error(e, msg)
+            CarbonException.analysisException(msg)
+        }
       }
+      val createTablePostExecutionEvent: CreateTablePostExecutionEvent =
+        CreateTablePostExecutionEvent(sparkSession, tableIdentifier)
+      OperationListenerBus.getInstance.fireEvent(createTablePostExecutionEvent, operationContext)
+      LOGGER.audit(s"Table created with Database name [$dbName] and Table name [$tableName]")
     }
-    val createTablePostExecutionEvent: CreateTablePostExecutionEvent =
-      CreateTablePostExecutionEvent(sparkSession, tableIdentifier)
-    OperationListenerBus.getInstance.fireEvent(createTablePostExecutionEvent, operationContext)
-    LOGGER.audit(s"Table created with Database name [$dbName] and Table name [$tableName]")
     Seq.empty
   }
 }


Mime
View raw message