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

 ##########
 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"
+  }
+
+  private def registerSegmentRowView(
+      sparkSession: SparkSession,
+      tempViewName: String,
+      carbonTable: CarbonTable,
+      segments: Array[LoadMetadataDetails]): Unit = {
+
+    // populate a dataframe containing all segment information
+    val tablePath = carbonTable.getTablePath
+    val segmentRows = segments.toSeq.map { segment =>
+      val mergedToId = CarbonStore.getMergeTo(segment)
+      val path = CarbonStore.getSegmentPath(segment)
+      val startTime = CarbonStore.getLoadStartTime(segment)
+      val endTime = CarbonStore.getLoadEndTime(segment)
+      val spentTime = CarbonStore.getSpentTimeAsMillis(segment)
+      val (dataSize, indexSize) = CarbonStore.getDataAndIndexSize(tablePath, 
segment)
+      val partitions = CarbonStore.getPartitions(tablePath, segment)
+      SegmentRow(
+        segment.getLoadName,
+        segment.getSegmentStatus.toString,
+        startTime,
+        spentTime,
+        partitions,
+        dataSize,
+        indexSize,
+        mergedToId,
+        segment.getFileFormat.toString,
+        path,
+        endTime,
+        if (segment.getSegmentFile == null) "NA" else segment.getSegmentFile)
+    }
+
+    // create a temp view using the populated dataframe and execute the query 
on it
+    val df = sparkSession.createDataFrame(segmentRows)
+    checkIfTableExist(sparkSession, tempViewName)
 
 Review comment:
   please move this check to Line#74

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to