[GitHub] [carbondata] ravipesala commented on a change in pull request #3392: [CARBONDATA-3516] Supporting mixed formats in carbon

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] [carbondata] ravipesala commented on a change in pull request #3392: [CARBONDATA-3516] Supporting mixed formats in carbon

GitBox
ravipesala commented on a change in pull request #3392: [CARBONDATA-3516] Supporting mixed formats in carbon
URL: https://github.com/apache/carbondata/pull/3392#discussion_r335795606
 
 

 ##########
 File path: integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/MixedFormatHandler.scala
 ##########
 @@ -0,0 +1,280 @@
+/*
+ * 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.strategy
+
+import java.util
+
+import scala.collection.JavaConverters._
+
+import org.apache.hadoop.fs.{Path, PathFilter}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.execution
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.carbondata.execution.datasources.SparkCarbonFileFormat
+import org.apache.spark.sql.catalyst.expressions
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference, AttributeSet, Expression, ExpressionSet, NamedExpression, SubqueryExpression}
+import org.apache.spark.sql.execution.FileSourceScanExec
+import org.apache.spark.sql.execution.datasources.{FileFormat, HadoopFsRelation, InMemoryFileIndex, LogicalRelation}
+import org.apache.spark.sql.execution.datasources.csv.CSVFileFormat
+import org.apache.spark.sql.execution.datasources.json.JsonFileFormat
+import org.apache.spark.sql.execution.datasources.parquet.ParquetFileFormat
+import org.apache.spark.sql.execution.datasources.text.TextFileFormat
+import org.apache.spark.sql.hive.orc.OrcFileFormat
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.SparkSQLUtil
+
+import org.apache.carbondata.common.logging.LogServiceFactory
+import org.apache.carbondata.core.constants.CarbonCommonConstants
+import org.apache.carbondata.core.datastore.impl.FileFactory
+import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore}
+import org.apache.carbondata.core.readcommitter.ReadCommittedScope
+import org.apache.carbondata.core.statusmanager.{FileFormat => CarbonFileFormat, SegmentStatus}
+import org.apache.carbondata.core.util.{CarbonProperties, CarbonSessionInfo, SessionParams, ThreadLocalSessionInfo}
+import org.apache.carbondata.core.util.path.CarbonTablePath
+
+object MixedFormatHandler {
+
+  val LOGGER = LogServiceFactory.getLogService(this.getClass.getName)
+
+  val supportedFormats: Seq[String] =
+    Seq("carbon", "carbondata", "parquet", "orc", "json", "csv", "text")
+
+  def validateFormat(format: String): Boolean = {
+    supportedFormats.exists(_.equalsIgnoreCase(format))
+  }
+
+  def getSchema(sparkSession: SparkSession,
+      options: Map[String, String],
+      segPath: String): StructType = {
+    val format = options.getOrElse("format", "carbondata")
+    if ((format.equals("carbondata") || format.equals("carbon"))) {
+      new SparkCarbonFileFormat().inferSchema(sparkSession, options, Seq.empty).get
+    } else {
+      val filePath = FileFactory.addSchemeIfNotExists(segPath.replace("\\", "/"))
+      val path = new Path(filePath)
+      val fs = path.getFileSystem(SparkSQLUtil.sessionState(sparkSession).newHadoopConf())
+      val status = fs.listStatus(path, new PathFilter {
+        override def accept(path: Path): Boolean = {
+          !path.getName.equals("_SUCCESS") && !path.getName.endsWith(".crc")
+        }
+      })
+      getFileFormat(new CarbonFileFormat(format)).inferSchema(sparkSession, options, status).get
+    }
+  }
+
+  def extraRDD(l: LogicalRelation,
+      projects: Seq[NamedExpression],
+      filters: Seq[Expression],
+      readCommittedScope: ReadCommittedScope,
+      identier: AbsoluteTableIdentifier,
+      supportBatch: Boolean = true): Option[(RDD[InternalRow], Boolean)] = {
+    val loadMetadataDetails = readCommittedScope.getSegmentList
+    val segsToAccess = getSegmentsToAccess(identier)
+    val rdds = loadMetadataDetails.filterNot(l =>
+      l.getFileFormat.equals(CarbonFileFormat.COLUMNAR_V3) ||
+      l.getFileFormat.equals(CarbonFileFormat.ROW_V1) &&
+      (!(l.getSegmentStatus.equals(SegmentStatus.SUCCESS) &&
+         l.getSegmentStatus.equals(SegmentStatus.LOAD_PARTIAL_SUCCESS))))
+      .filter(l => segsToAccess.isEmpty || segsToAccess.contains(l.getLoadName))
+      .groupBy(_.getFileFormat)
+      .map { case (format, detailses) =>
+        val paths = detailses.flatMap { d =>
+          SegmentFileStore.readSegmentFile(CarbonTablePath.getSegmentFilePath(readCommittedScope
+            .getFilePath, d.getSegmentFile)).getLocationMap.asScala.flatMap { case (p, f) =>
+            f.getFiles.asScala.map { ef =>
+              p + CarbonCommonConstants.FILE_SEPARATOR + ef
+            }.toSeq
+          }.toSeq
+        }.map(new Path(_))
+
+        val fileFormat = getFileFormat(format, supportBatch)
+        getRDDForExternalSegments(l, projects, filters, fileFormat, paths)
+      }
+    if (rdds.nonEmpty) {
+      if (rdds.size == 1) {
+        Some(rdds.head)
+      } else {
+        if (supportBatch && rdds.exists(!_._2)) {
+          extraRDD(l, projects, filters, readCommittedScope, identier, false)
+        } else {
+          var rdd: RDD[InternalRow] = null
+          rdds.foreach { r =>
+            if (rdd == null) {
+              rdd = r._1
+            } else {
+              rdd = rdd.union(r._1)
+            }
+          }
+          Some(rdd, !rdds.exists(!_._2))
+        }
+      }
+    } else {
+      None
+    }
+  }
+
+  def getFileFormat(fileFormat: CarbonFileFormat, supportBatch: Boolean = true): FileFormat = {
+    if (fileFormat.equals(new CarbonFileFormat("parquet"))) {
+      new ExtendedParquetFileFormat(supportBatch)
+    } else if (fileFormat.equals(new CarbonFileFormat("orc"))) {
+      new ExtendedOrcFileFormat(supportBatch)
+    } else if (fileFormat.equals(new CarbonFileFormat("json"))) {
+      new JsonFileFormat
+    } else if (fileFormat.equals(new CarbonFileFormat("csv"))) {
+      new CSVFileFormat
+    } else if (fileFormat.equals(new CarbonFileFormat("text"))) {
+      new TextFileFormat
+    } else {
+      throw new UnsupportedOperationException("Format not supported " + fileFormat)
+    }
+  }
+
+  class ExtendedParquetFileFormat(supportBatch: Boolean) extends ParquetFileFormat {
+    override def supportBatch(sparkSession: SparkSession, schema: StructType): Boolean = {
+      super.supportBatch(sparkSession, schema) && supportBatch
+    }
+  }
+
+  class ExtendedOrcFileFormat(supportBatch: Boolean) extends OrcFileFormat {
+    override def supportBatch(sparkSession: SparkSession, schema: StructType): Boolean = {
+      super.supportBatch(sparkSession, schema) && supportBatch
+    }
+  }
+
+
+  def getRDDForExternalSegments(l: LogicalRelation,
 
 Review comment:
   ok

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services