ueshin commented on code in PR #54014:
URL: https://github.com/apache/spark/pull/54014#discussion_r2734156824
##########
sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala:
##########
@@ -3012,6 +3012,36 @@ object CustomPathEncoder {
Seq(realClassDataEnc.fields.head.copy(enc = custStringEnc),
realClassDataEnc.fields.last)
)
+
+ test("zipWithIndex should append consecutive 0-based indices") {
Review Comment:
This is not in a test class but in `CustomPathEncoder`.
##########
sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala:
##########
@@ -3012,6 +3012,36 @@ object CustomPathEncoder {
Seq(realClassDataEnc.fields.head.copy(enc = custStringEnc),
realClassDataEnc.fields.last)
)
+
+ test("zipWithIndex should append consecutive 0-based indices") {
+ val ds = Seq(("a", 1), ("b", 2), ("c", 3), ("d", 4), ("e",
5)).toDS().repartition(3)
+ val result = ds.zipWithIndex()
+
+ // Index column should be the last column
+ assert(result.columns === Array("_1", "_2", "index"))
+ assert(result.schema.last.dataType === LongType)
+
+ // Indices should be consecutive 0-based
+ val indices = result.collect().map(_.getLong(2)).sorted
+ assert(indices === (0L until 5L).toArray)
+ }
+
+ test("zipWithIndex with custom column name") {
+ val ds = Seq(1, 2, 3, 4, 5).toDS()
+ val result = ds.zipWithIndex("row_num")
+
+ assert(result.columns === Array("value", "row_num"))
+ val indices = result.collect().map(_.getLong(1)).sorted
+ assert(indices === (0L until 5L).toArray)
+ }
+
+ test("zipWithIndex should throw if column name already exists") {
+ val ds = Seq(("a", 1), ("b", 2)).toDS().withColumnRenamed("_1", "index")
Review Comment:
nit:
```suggestion
val ds = Seq(("a", 1), ("b", 2)).toDF("_1", "index")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]