-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-2883] [SQL] ORC data source for Spark SQL
This PR updates PR #6135 authored by zhzhan from Hortonworks. ---- This PR implements a Spark SQL data source for accessing ORC files. > **NOTE** > > Although ORC is now an Apache TLP, the codebase is still tightly coupled with Hive. That's why the new ORC data source is under `org.apache.spark.sql.hive` package, and must be used with `HiveContext`. However, it doesn't require existing Hive installation to access ORC files. 1. Saving/loading ORC files without contacting Hive metastore 1. Support for complex data types (i.e. array, map, and struct) 1. Aware of common optimizations provided by Spark SQL: - Column pruning - Partitioning pruning - Filter push-down 1. Schema evolution support 1. Hive metastore table conversion This PR also include initial work done by scwf from Huawei (PR #3753). Author: Zhan Zhang <[email protected]> Author: Cheng Lian <[email protected]> Closes #6194 from liancheng/polishing-orc and squashes the following commits: 55ecd96 [Cheng Lian] Reorganizes ORC test suites d4afeed [Cheng Lian] Addresses comments 21ada22 [Cheng Lian] Adds @SInCE and @experimental annotations 128bd3b [Cheng Lian] ORC filter bug fix d734496 [Cheng Lian] Polishes the ORC data source 2650a42 [Zhan Zhang] resolve review comments 3c9038e [Zhan Zhang] resolve review comments 7b3c7c5 [Zhan Zhang] save mode fix f95abfd [Zhan Zhang] reuse test suite 7cc2c64 [Zhan Zhang] predicate fix 4e61c16 [Zhan Zhang] minor change 305418c [Zhan Zhang] orc data source support
- Loading branch information
Showing
14 changed files
with
1,477 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
sql/core/src/main/scala/org/apache/spark/sql/test/SQLTestUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.test | ||
|
||
import java.io.File | ||
|
||
import scala.util.Try | ||
|
||
import org.apache.spark.sql.SQLContext | ||
import org.apache.spark.util.Utils | ||
|
||
trait SQLTestUtils { | ||
val sqlContext: SQLContext | ||
|
||
import sqlContext.{conf, sparkContext} | ||
|
||
protected def configuration = sparkContext.hadoopConfiguration | ||
|
||
/** | ||
* Sets all SQL configurations specified in `pairs`, calls `f`, and then restore all SQL | ||
* configurations. | ||
* | ||
* @todo Probably this method should be moved to a more general place | ||
*/ | ||
protected def withSQLConf(pairs: (String, String)*)(f: => Unit): Unit = { | ||
val (keys, values) = pairs.unzip | ||
val currentValues = keys.map(key => Try(conf.getConf(key)).toOption) | ||
(keys, values).zipped.foreach(conf.setConf) | ||
try f finally { | ||
keys.zip(currentValues).foreach { | ||
case (key, Some(value)) => conf.setConf(key, value) | ||
case (key, None) => conf.unsetConf(key) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Generates a temporary path without creating the actual file/directory, then pass it to `f`. If | ||
* a file/directory is created there by `f`, it will be delete after `f` returns. | ||
* | ||
* @todo Probably this method should be moved to a more general place | ||
*/ | ||
protected def withTempPath(f: File => Unit): Unit = { | ||
val path = Utils.createTempDir() | ||
path.delete() | ||
try f(path) finally Utils.deleteRecursively(path) | ||
} | ||
|
||
/** | ||
* Creates a temporary directory, which is then passed to `f` and will be deleted after `f` | ||
* returns. | ||
* | ||
* @todo Probably this method should be moved to a more general place | ||
*/ | ||
protected def withTempDir(f: File => Unit): Unit = { | ||
val dir = Utils.createTempDir().getCanonicalFile | ||
try f(dir) finally Utils.deleteRecursively(dir) | ||
} | ||
|
||
/** | ||
* Drops temporary table `tableName` after calling `f`. | ||
*/ | ||
protected def withTempTable(tableName: String)(f: => Unit): Unit = { | ||
try f finally sqlContext.dropTempTable(tableName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
sql/hive/src/main/scala/org/apache/spark/sql/hive/orc/OrcFileOperator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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.hive.orc | ||
|
||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.fs.Path | ||
import org.apache.hadoop.hive.ql.io.orc.{OrcFile, Reader} | ||
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector | ||
|
||
import org.apache.spark.Logging | ||
import org.apache.spark.deploy.SparkHadoopUtil | ||
import org.apache.spark.sql.hive.HiveMetastoreTypes | ||
import org.apache.spark.sql.types.StructType | ||
|
||
private[orc] object OrcFileOperator extends Logging{ | ||
def getFileReader(pathStr: String, config: Option[Configuration] = None ): Reader = { | ||
val conf = config.getOrElse(new Configuration) | ||
val fspath = new Path(pathStr) | ||
val fs = fspath.getFileSystem(conf) | ||
val orcFiles = listOrcFiles(pathStr, conf) | ||
|
||
// TODO Need to consider all files when schema evolution is taken into account. | ||
OrcFile.createReader(fs, orcFiles.head) | ||
} | ||
|
||
def readSchema(path: String, conf: Option[Configuration]): StructType = { | ||
val reader = getFileReader(path, conf) | ||
val readerInspector = reader.getObjectInspector.asInstanceOf[StructObjectInspector] | ||
val schema = readerInspector.getTypeName | ||
HiveMetastoreTypes.toDataType(schema).asInstanceOf[StructType] | ||
} | ||
|
||
def getObjectInspector(path: String, conf: Option[Configuration]): StructObjectInspector = { | ||
getFileReader(path, conf).getObjectInspector.asInstanceOf[StructObjectInspector] | ||
} | ||
|
||
def listOrcFiles(pathStr: String, conf: Configuration): Seq[Path] = { | ||
val origPath = new Path(pathStr) | ||
val fs = origPath.getFileSystem(conf) | ||
val path = origPath.makeQualified(fs) | ||
val paths = SparkHadoopUtil.get.listLeafStatuses(fs, origPath) | ||
.filterNot(_.isDir) | ||
.map(_.getPath) | ||
.filterNot(_.getName.startsWith("_")) | ||
.filterNot(_.getName.startsWith(".")) | ||
|
||
if (paths == null || paths.size == 0) { | ||
throw new IllegalArgumentException( | ||
s"orcFileOperator: path $path does not have valid orc files matching the pattern") | ||
} | ||
|
||
paths | ||
} | ||
} |
Oops, something went wrong.