-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-26502][SQL] Move hiveResultString() from QueryExecution to Hiv…
…eResult ## What changes were proposed in this pull request? In the PR, I propose to move `hiveResultString()` out of `QueryExecution` and put it to a separate object. Closes #23409 from MaxGekk/hive-result-string. Lead-authored-by: Maxim Gekk <maxim.gekk@databricks.com> Co-authored-by: Maxim Gekk <max.gekk@gmail.com> Signed-off-by: Herman van Hovell <hvanhovell@databricks.com>
- Loading branch information
1 parent
56967b7
commit 2a30deb
Showing
6 changed files
with
126 additions
and
95 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
sql/core/src/main/scala/org/apache/spark/sql/execution/HiveResult.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,116 @@ | ||
/* | ||
* 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 | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.sql.{Date, Timestamp} | ||
|
||
import org.apache.spark.sql.Row | ||
import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
import org.apache.spark.sql.execution.command.{DescribeTableCommand, ExecutedCommandExec, ShowTablesCommand} | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types._ | ||
|
||
/** | ||
* Runs a query returning the result in Hive compatible form. | ||
*/ | ||
object HiveResult { | ||
/** | ||
* Returns the result as a hive compatible sequence of strings. This is used in tests and | ||
* `SparkSQLDriver` for CLI applications. | ||
*/ | ||
def hiveResultString(executedPlan: SparkPlan): Seq[String] = executedPlan match { | ||
case ExecutedCommandExec(desc: DescribeTableCommand) => | ||
// If it is a describe command for a Hive table, we want to have the output format | ||
// be similar with Hive. | ||
executedPlan.executeCollectPublic().map { | ||
case Row(name: String, dataType: String, comment) => | ||
Seq(name, dataType, | ||
Option(comment.asInstanceOf[String]).getOrElse("")) | ||
.map(s => String.format(s"%-20s", s)) | ||
.mkString("\t") | ||
} | ||
// SHOW TABLES in Hive only output table names, while ours output database, table name, isTemp. | ||
case command @ ExecutedCommandExec(s: ShowTablesCommand) if !s.isExtended => | ||
command.executeCollect().map(_.getString(1)) | ||
case other => | ||
val result: Seq[Seq[Any]] = other.executeCollectPublic().map(_.toSeq).toSeq | ||
// We need the types so we can output struct field names | ||
val types = executedPlan.output.map(_.dataType) | ||
// Reformat to match hive tab delimited output. | ||
result.map(_.zip(types).map(toHiveString)).map(_.mkString("\t")) | ||
} | ||
|
||
/** Formats a datum (based on the given data type) and returns the string representation. */ | ||
private def toHiveString(a: (Any, DataType)): String = { | ||
val primitiveTypes = Seq(StringType, IntegerType, LongType, DoubleType, FloatType, | ||
BooleanType, ByteType, ShortType, DateType, TimestampType, BinaryType) | ||
val timeZone = DateTimeUtils.getTimeZone(SQLConf.get.sessionLocalTimeZone) | ||
|
||
def formatDecimal(d: java.math.BigDecimal): String = { | ||
if (d.compareTo(java.math.BigDecimal.ZERO) == 0) { | ||
java.math.BigDecimal.ZERO.toPlainString | ||
} else { | ||
d.stripTrailingZeros().toPlainString | ||
} | ||
} | ||
|
||
/** Hive outputs fields of structs slightly differently than top level attributes. */ | ||
def toHiveStructString(a: (Any, DataType)): String = a match { | ||
case (struct: Row, StructType(fields)) => | ||
struct.toSeq.zip(fields).map { | ||
case (v, t) => s""""${t.name}":${toHiveStructString((v, t.dataType))}""" | ||
}.mkString("{", ",", "}") | ||
case (seq: Seq[_], ArrayType(typ, _)) => | ||
seq.map(v => (v, typ)).map(toHiveStructString).mkString("[", ",", "]") | ||
case (map: Map[_, _], MapType(kType, vType, _)) => | ||
map.map { | ||
case (key, value) => | ||
toHiveStructString((key, kType)) + ":" + toHiveStructString((value, vType)) | ||
}.toSeq.sorted.mkString("{", ",", "}") | ||
case (null, _) => "null" | ||
case (s: String, StringType) => "\"" + s + "\"" | ||
case (decimal, DecimalType()) => decimal.toString | ||
case (interval, CalendarIntervalType) => interval.toString | ||
case (other, tpe) if primitiveTypes contains tpe => other.toString | ||
} | ||
|
||
a match { | ||
case (struct: Row, StructType(fields)) => | ||
struct.toSeq.zip(fields).map { | ||
case (v, t) => s""""${t.name}":${toHiveStructString((v, t.dataType))}""" | ||
}.mkString("{", ",", "}") | ||
case (seq: Seq[_], ArrayType(typ, _)) => | ||
seq.map(v => (v, typ)).map(toHiveStructString).mkString("[", ",", "]") | ||
case (map: Map[_, _], MapType(kType, vType, _)) => | ||
map.map { | ||
case (key, value) => | ||
toHiveStructString((key, kType)) + ":" + toHiveStructString((value, vType)) | ||
}.toSeq.sorted.mkString("{", ",", "}") | ||
case (null, _) => "NULL" | ||
case (d: Date, DateType) => | ||
DateTimeUtils.dateToString(DateTimeUtils.fromJavaDate(d)) | ||
case (t: Timestamp, TimestampType) => | ||
DateTimeUtils.timestampToString(DateTimeUtils.fromJavaTimestamp(t), timeZone) | ||
case (bin: Array[Byte], BinaryType) => new String(bin, StandardCharsets.UTF_8) | ||
case (decimal: java.math.BigDecimal, DecimalType()) => formatDecimal(decimal) | ||
case (interval, CalendarIntervalType) => interval.toString | ||
case (other, tpe) if primitiveTypes.contains(tpe) => other.toString | ||
} | ||
} | ||
} |
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
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