@@ -226,35 +226,42 @@ public actor DataFrame: Sendable {
226
226
return result
227
227
}
228
228
229
- /// Execute the plan and show the result .
229
+ /// Displays the top 20 rows of ``DataFrame`` in a tabular form .
230
230
public func show( ) async throws {
231
- try await execute ( )
231
+ try await show ( 20 )
232
+ }
232
233
233
- if let schema = self . _schema {
234
- var columns : [ TextTableColumn ] = [ ]
235
- for f in schema. struct. fields {
236
- columns. append ( TextTableColumn ( header: f. name) )
237
- }
238
- var table = TextTable ( columns: columns)
239
- for batch in self . batches {
240
- for i in 0 ..< batch. length {
241
- var values : [ String ] = [ ]
242
- for column in batch. columns {
243
- let str = column. array as! AsString
244
- if column. data. isNull ( i) {
245
- values. append ( " NULL " )
246
- } else if column. data. type. info == ArrowType . ArrowBinary {
247
- let binary = str. asString ( i) . utf8. map { String ( format: " %02x " , $0) } . joined ( separator: " " )
248
- values. append ( " [ \( binary) ] " )
249
- } else {
250
- values. append ( str. asString ( i) )
251
- }
252
- }
253
- table. addRow ( values: values)
254
- }
255
- }
256
- print ( table. render ( ) )
257
- }
234
+ /// Displays the top 20 rows of ``DataFrame`` in a tabular form.
235
+ /// - Parameter truncate: Whether truncate long strings. If true, strings more than 20 characters will be truncated
236
+ /// and all cells will be aligned right
237
+ public func show( _ truncate: Bool ) async throws {
238
+ try await show ( 20 , truncate)
239
+ }
240
+
241
+ /// Displays the ``DataFrame`` in a tabular form.
242
+ /// - Parameters:
243
+ /// - numRows: Number of rows to show
244
+ /// - truncate: Whether truncate long strings. If true, strings more than 20 characters will be truncated
245
+ /// and all cells will be aligned right
246
+ public func show( _ numRows: Int32 = 20 , _ truncate: Bool = true ) async throws {
247
+ try await show ( numRows, truncate ? 20 : 0 )
248
+ }
249
+
250
+ /// Displays the ``DataFrame`` in a tabular form.
251
+ /// - Parameters:
252
+ /// - numRows: Number of rows to show
253
+ /// - truncate: If set to more than 0, truncates strings to `truncate` characters and all cells will be aligned right.
254
+ /// - vertical: If set to true, prints output rows vertically (one line per column value).
255
+ public func show( _ numRows: Int32 , _ truncate: Int32 , _ vertical: Bool = false ) async throws {
256
+ let rows = try await showString ( numRows, truncate, vertical) . collect ( )
257
+ assert ( rows. count == 1 )
258
+ assert ( rows [ 0 ] . length == 1 )
259
+ print ( try rows [ 0 ] . get ( 0 ) as! String )
260
+ }
261
+
262
+ func showString( _ numRows: Int32 , _ truncate: Int32 , _ vertical: Bool ) -> DataFrame {
263
+ let plan = SparkConnectClient . getShowString ( self . plan. root, numRows, truncate, vertical)
264
+ return DataFrame ( spark: self . spark, plan: plan)
258
265
}
259
266
260
267
/// Projects a set of expressions and returns a new ``DataFrame``.
0 commit comments