Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {

// Decode and input/output format.
type Format = (Seq[(String, String)], Option[String], Seq[(String, String)], Option[String])
def format(fmt: RowFormatContext, configKey: String): Format = fmt match {
def format(fmt: RowFormatContext, configKey: String, configValue: String): Format = fmt match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name should show that this value is default value, right?

case c: RowFormatDelimitedContext =>
// TODO we should use the visitRowFormatDelimited function here. However HiveScriptIOSchema
// expects a seq of pairs in which the old parsers' token names are used as keys.
Expand All @@ -1329,7 +1329,7 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {

// SPARK-10310: Special cases LazySimpleSerDe
val recordHandler = if (name == "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe") {
Try(conf.getConfString(configKey)).toOption
Try(conf.getConfString(configKey, configValue)).toOption
Copy link
Contributor

@yhuai yhuai Jul 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just use getConfString(key: String, defaultValue: String)? That defaultRecordHandler method seems unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is different for different key, you mean to inline the defaultRecordHandler function?

} else {
None
}
Expand All @@ -1340,15 +1340,18 @@ class SparkSqlAstBuilder(conf: SQLConf) extends AstBuilder {
val name = conf.getConfString("hive.script.serde",
"org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe")
val props = Seq("field.delim" -> "\t")
val recordHandler = Try(conf.getConfString(configKey)).toOption
val recordHandler = Try(conf.getConfString(configKey, configValue)).toOption
(Nil, Option(name), props, recordHandler)
}

val (inFormat, inSerdeClass, inSerdeProps, reader) =
format(inRowFormat, "hive.script.recordreader")
format(
inRowFormat, "hive.script.recordreader", "org.apache.hadoop.hive.ql.exec.TextRecordReader")

val (outFormat, outSerdeClass, outSerdeProps, writer) =
format(outRowFormat, "hive.script.recordwriter")
format(
outRowFormat, "hive.script.recordwriter",
"org.apache.hadoop.hive.ql.exec.TextRecordWriter")

ScriptInputOutputSchema(
inFormat, outFormat,
Expand Down
23 changes: 23 additions & 0 deletions sql/hive/src/test/resources/test_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

#
# 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.
#

while read line
do
echo "$line" | sed 's/\t/_/'
done < /dev/stdin
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ class SQLQuerySuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
import hiveContext._
import spark.implicits._

test("script") {
val df = Seq(("x1", "y1", "z1"), ("x2", "y2", "z2")).toDF("c1", "c2", "c3")
df.createOrReplaceTempView("script_table")
val query1 = sql(
"""
|SELECT col1 FROM (from(SELECT c1, c2, c3 FROM script_table) tempt_table
|REDUCE c1, c2, c3 USING 'bash src/test/resources/test_script.sh' AS
|(col1 STRING, col2 STRING)) script_test_table""".stripMargin)
checkAnswer(query1, Row("x1_y1") :: Row("x2_y2") :: Nil)
}

test("UDTF") {
withUserDefinedFunction("udtf_count2" -> true) {
sql(s"ADD JAR ${hiveContext.getHiveFile("TestUDTF.jar").getCanonicalPath()}")
Expand Down