This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new f0305fdab9 [spark] Match COPY INTO file statistics by the URL-encoded
file name (#10930)
f0305fdab9 is described below
commit f0305fdab9f93920eb2d3631abd243b8d4cea01e
Author: YangJie <[email protected]>
AuthorDate: Mon Nov 2 01:14:09 2026 -0500
[spark] Match COPY INTO file statistics by the URL-encoded file name
(#10930)
---
.../spark/copyinto/CopyIntoResultBuilder.scala | 11 ++--
.../spark/execution/CopyIntoErrorHandler.scala | 13 ++++-
.../paimon/spark/execution/CopyIntoUtils.scala | 6 ++
.../paimon/spark/sql/CopyIntoOnErrorTest.scala | 65 ++++++++++++++++++++++
.../apache/paimon/spark/sql/CopyIntoTestBase.scala | 24 ++++++++
5 files changed, 112 insertions(+), 7 deletions(-)
diff --git
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/copyinto/CopyIntoResultBuilder.scala
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/copyinto/CopyIntoResultBuilder.scala
index a165a45233..823fda930b 100644
---
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/copyinto/CopyIntoResultBuilder.scala
+++
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/copyinto/CopyIntoResultBuilder.scala
@@ -18,6 +18,7 @@
package org.apache.paimon.spark.copyinto
+import org.apache.paimon.spark.execution.CopyIntoUtils
import org.apache.paimon.table.FileStoreTable
import org.apache.hadoop.fs.FileStatus
@@ -66,7 +67,8 @@ object CopyIntoResultBuilder {
val loadedResults = filesToLoad.map {
fileStatus =>
- val baseName = fileStatus.getPath.getName
+ val baseName = CopyIntoUtils.encodedBaseName(fileStatus)
+ val displayName = fileStatus.getPath.getName
val fullPath = fileStatus.getPath.toString
val parsedCount = totalRowsPerFile.getOrElse(baseName, 0L)
val fileParseErrors = parseErrors.getOrElse(baseName, 0L)
@@ -93,7 +95,7 @@ object CopyIntoResultBuilder {
val fileFirstError =
firstParseErrorPerFile.get(baseName).orElse(firstCastErrorPerFile.get(baseName))
buildResultRow(
- baseName,
+ displayName,
status,
rowsLoaded,
parsedCount,
@@ -120,7 +122,8 @@ object CopyIntoResultBuilder {
val loadedResults = filesToLoad.map {
fileStatus =>
- val baseName = fileStatus.getPath.getName
+ val baseName = CopyIntoUtils.encodedBaseName(fileStatus)
+ val displayName = fileStatus.getPath.getName
val rowCount = rowCountsPerFile.getOrElse(baseName, 0L)
historyManager.recordLoaded(
@@ -133,7 +136,7 @@ object CopyIntoResultBuilder {
rowsLoaded = rowCount
))
- buildResultRow(baseName, "LOADED", rowCount, rowCount, 0L, null)
+ buildResultRow(displayName, "LOADED", rowCount, rowCount, 0L, null)
}.toSeq
loadedResults ++ buildSkippedResults(skippedFiles)
diff --git
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoErrorHandler.scala
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoErrorHandler.scala
index 466134900f..9f56b0f9ba 100644
---
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoErrorHandler.scala
+++
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoErrorHandler.scala
@@ -145,7 +145,8 @@ private[execution] class CopyIntoErrorHandler(
val results = filesToLoad.map {
fileStatus =>
- val baseName = fileStatus.getPath.getName
+ val baseName = CopyIntoUtils.encodedBaseName(fileStatus)
+ val displayName = fileStatus.getPath.getName
val fullPath = fileStatus.getPath.toString
if (filesWithErrors.contains(baseName)) {
@@ -158,7 +159,7 @@ private[execution] class CopyIntoErrorHandler(
.orElse(errorResult.firstCastError.get(baseName))
CopyIntoResultBuilder.buildResultRow(
- baseName,
+ displayName,
"LOAD_FAILED",
0L,
errorResult.totalRowsPerFile.getOrElse(baseName, 0L),
@@ -177,7 +178,13 @@ private[execution] class CopyIntoErrorHandler(
rowsLoaded = rowCount
))
- CopyIntoResultBuilder.buildResultRow(baseName, "LOADED",
rowCount, rowCount, 0L, null)
+ CopyIntoResultBuilder.buildResultRow(
+ displayName,
+ "LOADED",
+ rowCount,
+ rowCount,
+ 0L,
+ null)
}
}.toSeq
diff --git
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoUtils.scala
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoUtils.scala
index 32aa730780..e51a169d40 100644
---
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoUtils.scala
+++
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/execution/CopyIntoUtils.scala
@@ -18,6 +18,7 @@
package org.apache.paimon.spark.execution
+import org.apache.hadoop.fs.FileStatus
import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.sql.catalyst.parser.ParseException
import org.apache.spark.sql.catalyst.plans.logical.{Command, InsertIntoDir,
LogicalPlan, ParsedStatement}
@@ -85,6 +86,11 @@ object CopyIntoUtils {
fullPath.substring(fullPath.lastIndexOf('/') + 1)
}
+ /** The base name of a file, URL-encoded like the values of
`input_file_name()`. */
+ def encodedBaseName(fileStatus: FileStatus): String = {
+ extractBaseName(fileStatus.getPath.toUri.toString)
+ }
+
/** Count rows per file, keyed by base file name. */
def countPerFile(df: DataFrame, fileCol: String): Map[String, Long] = {
df.groupBy(col(fileCol))
diff --git
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoOnErrorTest.scala
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoOnErrorTest.scala
index 11942c7dfd..493dc00ddb 100644
---
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoOnErrorTest.scala
+++
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoOnErrorTest.scala
@@ -142,6 +142,71 @@ trait CopyIntoOnErrorTest { self: CopyIntoTestBase =>
spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_skipfile_mixed")
}
+ test("COPY INTO: ON_ERROR = SKIP_FILE with file names that contain spaces") {
+ spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_skipfile_spaces")
+ spark.sql(s"CREATE TABLE $dbName0.copy_skipfile_spaces (id INT, name
STRING)")
+
+ withCsvDir {
+ dir =>
+ createCsvFile(dir, "good data.csv", "1,Alice\n2,Bob\n")
+ createCsvFile(dir, "bad data.csv", "abc,Charlie\n")
+
+ def copyInto(): Array[Row] =
+ spark
+ .sql(s"""COPY INTO $dbName0.copy_skipfile_spaces
+ |FROM '${dir.getAbsolutePath}'
+ |FILE_FORMAT = (TYPE = CSV)
+ |ON_ERROR = SKIP_FILE
+ |""".stripMargin)
+ .collect()
+
+ val rows = copyInto()
+ val loaded = rows.filter(_.getString(1) == "LOADED")
+ val failed = rows.filter(_.getString(1) == "LOAD_FAILED")
+ assert(loaded.map(_.getString(0)).toSeq == Seq("good data.csv"),
rows.toSeq)
+ assert(loaded(0).getLong(2) == 2L, rows.toSeq)
+ assert(failed.map(_.getString(0)).toSeq == Seq("bad data.csv"),
rows.toSeq)
+
+ // the failed file is not recorded as loaded, so a rerun tries it again
+ val rerun = copyInto()
+ assert(
+ rerun.filter(_.getString(0) == "bad
data.csv").map(_.getString(1)).toSeq ==
+ Seq("LOAD_FAILED"))
+ assert(
+ rerun.filter(_.getString(0) == "good
data.csv").map(_.getString(1)).toSeq ==
+ Seq("SKIPPED"))
+ }
+
+ spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_skipfile_spaces")
+ }
+
+ test("COPY INTO: ON_ERROR = CONTINUE with a failed file name that contains a
space") {
+ spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_continue_spaces")
+ spark.sql(s"CREATE TABLE $dbName0.copy_continue_spaces (id INT, name
STRING)")
+
+ withCsvDir {
+ dir =>
+ createCsvFile(dir, "bad data.csv", "abc,Alice\nxyz,Bob\n")
+
+ def copyInto(): Array[Row] =
+ spark
+ .sql(s"""COPY INTO $dbName0.copy_continue_spaces
+ |FROM '${dir.getAbsolutePath}'
+ |FILE_FORMAT = (TYPE = CSV)
+ |ON_ERROR = CONTINUE
+ |""".stripMargin)
+ .collect()
+
+ val rows = copyInto()
+ assert(rows.length == 1)
+ assert(rows(0).getString(1) == "LOAD_FAILED", rows.toSeq)
+ assert(rows(0).getLong(4) == 2L, rows.toSeq)
+ assert(copyInto()(0).getString(1) == "LOAD_FAILED")
+ }
+
+ spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_continue_spaces")
+ }
+
test("COPY INTO: ON_ERROR = SKIP_FILE with all good files") {
spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_skipfile_ok")
spark.sql(s"CREATE TABLE $dbName0.copy_skipfile_ok (id INT, name STRING)")
diff --git
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoTestBase.scala
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoTestBase.scala
index bb127854d7..3a39f976dd 100644
---
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoTestBase.scala
+++
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/CopyIntoTestBase.scala
@@ -628,6 +628,30 @@ class CopyIntoTestBase extends PaimonSparkTestBase {
spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_count")
}
+ test("COPY INTO: rows_loaded count of a file name with a space") {
+ spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_count_space")
+ spark.sql(s"CREATE TABLE $dbName0.copy_count_space (id INT, name STRING)")
+
+ withCsvDir {
+ dir =>
+ createCsvFile(dir, "my data.csv", "1,Alice\n2,Bob\n3,Charlie\n")
+
+ val rows = spark
+ .sql(s"""COPY INTO $dbName0.copy_count_space
+ |FROM '${dir.getAbsolutePath}'
+ |FILE_FORMAT = (TYPE = CSV)
+ |""".stripMargin)
+ .collect()
+ assert(rows.length == 1)
+ assert(rows(0).getString(0) == "my data.csv")
+ assert(rows(0).getString(1) == "LOADED")
+ assert(rows(0).getLong(2) == 3L)
+ assert(rows(0).getLong(3) == 3L)
+ }
+
+ spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_count_space")
+ }
+
test("COPY INTO: duplicate column list errors") {
spark.sql(s"DROP TABLE IF EXISTS $dbName0.copy_dup_col")
spark.sql(s"CREATE TABLE $dbName0.copy_dup_col (id INT, name STRING)")