-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-26502][SQL] Move hiveResultString() from QueryExecution to HiveResult #23409
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb045dd
Move HiveResult out of QueryExecution
MaxGekk 1c1b4df
Avoid using of hiveResultString
MaxGekk 92c04ab
Use SQLConf.get.sessionLocalTimeZone
MaxGekk 92e40fa
Put timeZone into a val in the scope of the toHiveString
MaxGekk fadfd5b
Use SparkPlan instead of QueryExecution
MaxGekk 7aba95b
Added doc for HiveResult
MaxGekk e91aee4
Merge remote-tracking branch 'origin/master' into hive-result-string
MaxGekk 948414a
Fix merge
MaxGekk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
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,112 @@ | ||
/* | ||
* 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.types._ | ||
|
||
object HiveResult { | ||
/** | ||
* Returns the result as a hive compatible sequence of strings. This is used in tests and | ||
* `SparkSQLDriver` for CLI applications. | ||
*/ | ||
def hiveResultString(qe: QueryExecution): Seq[String] = qe.executedPlan match { | ||
MaxGekk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
desc.run(qe.sparkSession).map { | ||
MaxGekk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)) | ||
MaxGekk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = qe.analyzed.output.map(_.dataType) | ||
MaxGekk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Reformat to match hive tab delimited output. | ||
result.map(_.zip(types).map(toHiveString(qe, _))).map(_.mkString("\t")) | ||
} | ||
|
||
/** Formats a datum (based on the given data type) and returns the string representation. */ | ||
private def toHiveString(qe: QueryExecution, a: (Any, DataType)): String = { | ||
val primitiveTypes = Seq(StringType, IntegerType, LongType, DoubleType, FloatType, | ||
BooleanType, ByteType, ShortType, DateType, TimestampType, BinaryType) | ||
|
||
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), | ||
DateTimeUtils.getTimeZone(qe.sparkSession.sessionState.conf.sessionLocalTimeZone)) | ||
MaxGekk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HiveResult.hiveResultString seems could used for other(eg. MySql).So I suggest modify the name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be used from different places. The main purpose of these methods is to return results in Hive-like form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, let's stick with HiveResult.