Skip to content

Commit 70b69a5

Browse files
committed
update
1 parent c39e3cf commit 70b69a5

File tree

4 files changed

+37
-90
lines changed

4 files changed

+37
-90
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2924,7 +2924,7 @@ object SQLConf {
29242924
.doc("Command wrapper for executor to execute transformation script.")
29252925
.version("3.2.0")
29262926
.stringConf
2927-
.createWithDefault("/bin/bash -c")
2927+
.createWithDefault("")
29282928

29292929
val SCRIPT_TRANSFORMATION_EXIT_TIMEOUT =
29302930
buildConf("spark.sql.scriptTransformation.exitTimeoutInSeconds")

sql/core/src/main/scala/org/apache/spark/sql/execution/BaseScriptTransformationExec.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ trait BaseScriptTransformationExec extends UnaryExecNode {
7777

7878
protected def initProc(hadoopConf: Configuration): ProcParameters = {
7979
val wrapper = splitArgs(hadoopConf.get(SQLConf.SCRIPT_TRANSFORMATION_COMMAND_WRAPPER.key))
80-
val cmd = wrapper.toList ++ List(script)
80+
val cmdArgs = splitArgs(script)
81+
val prog = cmdArgs(0)
82+
if(!new File(prog).isAbsolute) {
83+
val progFile = new File(SparkFiles.get(prog))
84+
if (progFile.exists()) {
85+
cmdArgs(0) = progFile.getAbsolutePath
86+
}
87+
}
88+
val cmd = wrapper.toList ++ cmdArgs.toList
8189
val builder = new ProcessBuilder(cmd.asJava)
8290
.directory(new File(SparkFiles.getRootDirectory()))
8391

sql/core/src/test/resources/test_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#! /usr/bin/python
22

33
# Licensed to the Apache Software Foundation (ASF) under one or more
44
# contributor license agreements. See the NOTICE file distributed with

sql/core/src/test/scala/org/apache/spark/sql/execution/BaseScriptTransformationSuite.scala

Lines changed: 26 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ abstract class BaseScriptTransformationSuite extends SparkPlanTest with SQLTestU
482482
).toDF("a", "b", "c", "d", "e") // Note column d's data type is Decimal(38, 18)
483483
df.createTempView("v")
484484

485-
// test '/bin/bash -c python /path/to/script.py'
485+
// test 'python /path/to/script.py' with local file
486486
checkAnswer(
487487
sql(
488488
s"""
@@ -501,7 +501,7 @@ abstract class BaseScriptTransformationSuite extends SparkPlanTest with SQLTestU
501501
'd.cast("string"),
502502
'e.cast("string")).collect())
503503

504-
// test '/bin/bash -c /path/to/script.py' with script not executable
504+
// test '/path/to/script.py' with script not executable
505505
val e1 = intercept[TestFailedException] {
506506
checkAnswer(
507507
sql(
@@ -523,7 +523,7 @@ abstract class BaseScriptTransformationSuite extends SparkPlanTest with SQLTestU
523523
}.getMessage
524524
assert(e1.contains("Permission denied"))
525525

526-
// test '/bin/bash -c /path/to/script.py' with script executable
526+
// test `/path/to/script.py' with script executable
527527
scriptFilePath.setExecutable(true)
528528
checkAnswer(
529529
sql(
@@ -546,102 +546,41 @@ abstract class BaseScriptTransformationSuite extends SparkPlanTest with SQLTestU
546546
scriptFilePath.setExecutable(false)
547547
sql(s"ADD FILE ${scriptFilePath.getAbsolutePath}")
548548

549-
// test '/bin/bash -c script.py'
550-
val e2 = intercept[TestFailedException] {
551-
checkAnswer(
552-
sql(
553-
s"""
554-
|SELECT TRANSFORM(a, b, c, d, e)
555-
| ROW FORMAT DELIMITED
556-
| FIELDS TERMINATED BY '\t'
557-
| USING '${scriptFilePath.getName}' AS (a, b, c, d, e)
558-
| ROW FORMAT DELIMITED
559-
| FIELDS TERMINATED BY '\t'
560-
|FROM v
561-
""".stripMargin), identity, df.select(
562-
'a.cast("string"),
563-
'b.cast("string"),
564-
'c.cast("string"),
565-
'd.cast("string"),
566-
'e.cast("string")).collect())
567-
}.getMessage()
568-
assert(e2.contains("command not found"))
569-
}
570-
}
571-
572-
test("SPARK-33934: Check execute command wrapper is empty") {
573-
assume(TestUtils.testCommandAvailable("python"))
574-
val scriptFilePath = copyAndGetResourceFile(
575-
"test_script.py", "_test_empty.py").getAbsoluteFile
576-
withTempView("v") {
577-
withSQLConf(SQLConf.SCRIPT_TRANSFORMATION_COMMAND_WRAPPER.key -> "") {
578-
val df = Seq(
579-
(1, "1", 1.0, BigDecimal(1.0), new Timestamp(1)),
580-
(2, "2", 2.0, BigDecimal(2.0), new Timestamp(2)),
581-
(3, "3", 3.0, BigDecimal(3.0), new Timestamp(3))
582-
).toDF("a", "b", "c", "d", "e") // Note column d's data type is Decimal(38, 18)
583-
df.createTempView("v")
584-
585-
scriptFilePath.setExecutable(true)
586-
sql(s"ADD FILE ${scriptFilePath.getAbsolutePath}")
549+
// test `script.py` when file added
550+
checkAnswer(
587551
sql(
588552
s"""
589-
|SELECT TRANSFORM(a)
553+
|SELECT TRANSFORM(a, b, c, d, e)
590554
| ROW FORMAT DELIMITED
591555
| FIELDS TERMINATED BY '\t'
592-
| USING 'pwd' AS (a)
556+
| USING '${scriptFilePath.getName}' AS (a, b, c, d, e)
593557
| ROW FORMAT DELIMITED
594-
| FIELDS TERMINATED BY '&'
595-
|FROM (SELECT 1 AS a) TEMP
596-
""".stripMargin).show(false)
558+
| FIELDS TERMINATED BY '\t'
559+
|FROM v
560+
""".stripMargin), identity, df.select(
561+
'a.cast("string"),
562+
'b.cast("string"),
563+
'c.cast("string"),
564+
'd.cast("string"),
565+
'e.cast("string")).collect())
597566

567+
// test `python script.py` when file added
568+
checkAnswer(
598569
sql(
599570
s"""
600-
|SELECT TRANSFORM(a)
571+
|SELECT TRANSFORM(a, b, c, d, e)
601572
| ROW FORMAT DELIMITED
602573
| FIELDS TERMINATED BY '\t'
603-
| USING 'ls' AS (a)
574+
| USING 'python ${scriptFilePath.getName}' AS (a, b, c, d, e)
604575
| ROW FORMAT DELIMITED
605-
| FIELDS TERMINATED BY '&'
606-
|FROM (SELECT 1 AS a) TEMP
607-
""".stripMargin).show(false)
608-
609-
// test 'python script.py'
610-
checkAnswer(
611-
sql(
612-
s"""
613-
|SELECT TRANSFORM(a, b, c, d, e)
614-
| ROW FORMAT DELIMITED
615-
| FIELDS TERMINATED BY '\t'
616-
| USING 'python ${scriptFilePath.getName}' AS (a, b, c, d, e)
617-
| ROW FORMAT DELIMITED
618-
| FIELDS TERMINATED BY '\t'
619-
|FROM v
620-
""".stripMargin), identity, df.select(
621-
'a.cast("string"),
622-
'b.cast("string"),
623-
'c.cast("string"),
624-
'd.cast("string"),
625-
'e.cast("string")).collect())
626-
627-
// test 'script.py'
628-
checkAnswer(
629-
sql(
630-
s"""
631-
|SELECT TRANSFORM(a, b, c, d, e)
632-
| ROW FORMAT DELIMITED
633-
| FIELDS TERMINATED BY '\t'
634-
| USING '${scriptFilePath.getName}' AS (a, b, c, d, e)
635-
| ROW FORMAT DELIMITED
636-
| FIELDS TERMINATED BY '\t'
637-
|FROM v
576+
| FIELDS TERMINATED BY '\t'
577+
|FROM v
638578
""".stripMargin), identity, df.select(
639-
'a.cast("string"),
640-
'b.cast("string"),
641-
'c.cast("string"),
642-
'd.cast("string"),
643-
'e.cast("string")).collect())
644-
}
579+
'a.cast("string"),
580+
'b.cast("string"),
581+
'c.cast("string"),
582+
'd.cast("string"),
583+
'e.cast("string")).collect())
645584
}
646585
}
647586
}

0 commit comments

Comments
 (0)