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
6 changes: 5 additions & 1 deletion python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,11 @@ def slice(x, start, length):
[Row(sliced=[2, 3]), Row(sliced=[5])]
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.slice(_to_java_column(x), start, length))
return Column(sc._jvm.functions.slice(
_to_java_column(x),
start._jc if isinstance(start, Column) else start,
length._jc if isinstance(length, Column) else length
))


@since(2.4)
Expand Down
10 changes: 10 additions & 0 deletions python/pyspark/sql/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ def test_input_file_name_reset_for_rdd(self):
for result in results:
self.assertEqual(result[0], '')

def test_slice(self):
from pyspark.sql.functions import slice, lit

df = self.spark.createDataFrame([([1, 2, 3],), ([4, 5],)], ['x'])

self.assertEquals(
df.select(slice(df.x, 2, 2).alias("sliced")).collect(),
df.select(slice(df.x, lit(2), lit(2)).alias("sliced")).collect(),
)

def test_array_repeat(self):
from pyspark.sql.functions import array_repeat, lit

Expand Down