You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@carbondata.apache.org by GitBox <gi...@apache.org> on 2020/03/23 06:39:31 UTC

[GitHub] [carbondata] kunal642 commented on a change in pull request #3657: [CARBONDATA-3736] Improve show segment

kunal642 commented on a change in pull request #3657: [CARBONDATA-3736] Improve show segment
URL: https://github.com/apache/carbondata/pull/3657#discussion_r396237032
 
 

 ##########
 File path: integration/spark/src/main/scala/org/apache/spark/sql/execution/command/management/CarbonShowSegmentsByQueryCommand.scala
 ##########
 @@ -0,0 +1,135 @@
+/*
+ * 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.command.management
+
+import org.apache.spark.sql.{CarbonEnv, DataFrame, Row, SparkSession}
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference}
+import org.apache.spark.sql.execution.command.{Checker, DataCommand}
+
+import org.apache.carbondata.api.CarbonStore
+import org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException
+import org.apache.carbondata.core.metadata.schema.table.CarbonTable
+import org.apache.carbondata.core.statusmanager.LoadMetadataDetails
+
+case class SegmentRow(
+    id: String, status: String, loadStartTime: String, spentTimeMs: Long, partitions: Seq[String],
+    dataSize: Long, indexSize: Long, mergedToId: String, format: String, path: String,
+    loadEndTime: String, segmentFileName: String)
+
+case class CarbonShowSegmentsByQueryCommand(
+    databaseNameOp: Option[String],
+    tableName: String,
+    query: String,
+    showHistory: Boolean = false)
+  extends DataCommand {
+
+  private lazy val df = createDataFrame
+
+  override def output: Seq[Attribute] = {
+    df.queryExecution.analyzed.output.map { attr =>
+      AttributeReference(attr.name, attr.dataType, nullable = false)()
+    }
+  }
+
+  override def processData(sparkSession: SparkSession): Seq[Row] = {
+    Checker.validateTableExists(databaseNameOp, tableName, sparkSession)
+    val carbonTable = CarbonEnv.getCarbonTable(databaseNameOp, tableName)(sparkSession)
+    setAuditTable(carbonTable)
+    if (!carbonTable.getTableInfo.isTransactionalTable) {
+      throw new MalformedCarbonCommandException("Unsupported operation on non transactional table")
+    }
+    try {
+      df.collect()
+    } catch {
+      case ex: Throwable =>
+        throw new MalformedCarbonCommandException("failed to run query: " + ex.getMessage)
+    } finally {
+      sparkSession.catalog.dropTempView(makeTempViewName(carbonTable))
+    }
+  }
+
+  override protected def opName: String = "SHOW SEGMENTS"
+
+  private def createDataFrame: DataFrame = {
+    val sparkSession = SparkSession.getActiveSession.get
+    val carbonTable = CarbonEnv.getCarbonTable(databaseNameOp, tableName)(sparkSession)
+    val tablePath = carbonTable.getTablePath
+    val segments = CarbonStore.readSegments(tablePath, showHistory)
+    val tempViewName = makeTempViewName(carbonTable)
+    registerSegmentRowView(sparkSession, tempViewName, carbonTable, segments)
+    try {
+      sparkSession.sql(query)
+    } catch {
+      case t: Throwable =>
+        sparkSession.catalog.dropTempView(tempViewName)
+        throw t
+    }
+  }
+
+  /**
+   * Generate temp view name for the query to execute
+   */
+  private def makeTempViewName(carbonTable: CarbonTable): String = {
+    s"${carbonTable.getTableName}_segments"
 
 Review comment:
   using tableUniqueId would be better

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services