You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ul...@apache.org on 2022/07/29 06:48:42 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #3158] Fix npe issue when formatting the kyuubi-ctl output

This is an automated email from the ASF dual-hosted git repository.

ulyssesyou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ddf7e384 [KYUUBI #3158] Fix npe issue when formatting the kyuubi-ctl output
0ddf7e384 is described below

commit 0ddf7e384914e81b3d8c9de6419fcb1499277592
Author: Fei Wang <fw...@ebay.com>
AuthorDate: Fri Jul 29 14:48:31 2022 +0800

    [KYUUBI #3158] Fix npe issue when formatting the kyuubi-ctl output
    
    ### _Why are the changes needed?_
    
    Fix npe issue:
    
    ```
    Exception in thread "main" java.lang.NullPointerException
            at com.jakewharton.fliptables.FlipTable.<init>(FlipTable.java:42)
            at com.jakewharton.fliptables.FlipTable.of(FlipTable.java:20)
            at org.apache.kyuubi.ctl.util.Tabulator$.formatTextTable(Tabulator.scala:37)
            at org.apache.kyuubi.ctl.util.Tabulator$.format(Tabulator.scala:25)
            at org.apache.kyuubi.ctl.util.Render$.renderBatchListInfo(Render.scala:39)
            at org.apache.kyuubi.ctl.cmd.list.ListBatchCommand.$anonfun$render$1(ListBatchCommand.scala:58)
            at org.apache.kyuubi.ctl.cmd.Command.info(Command.scala:86)
            at org.apache.kyuubi.ctl.cmd.list.ListBatchCommand.render(ListBatchCommand.scala:58)
            at org.apache.kyuubi.ctl.cmd.list.ListBatchCommand.render(ListBatchCommand.scala:26)
            at org.apache.kyuubi.ctl.cmd.Command.$anonfun$run$1(Command.scala:47)
            at org.apache.kyuubi.ctl.cmd.Command.$anonfun$run$1$adapted(Command.scala:47)
            at scala.Option.foreach(Option.scala:407)
            at org.apache.kyuubi.ctl.cmd.Command.run(Command.scala:47)
            at org.apache.kyuubi.ctl.ControlCli.doAction(ControlCli.scala:45)
            at org.apache.kyuubi.ctl.ControlCli$$anon$1.doAction(ControlCli.scala:78)
            at org.apache.kyuubi.ctl.ControlCli$.main(ControlCli.scala:86)
            at org.apache.kyuubi.ctl.ControlCli.main(ControlCli.scala)
    ```
    
    ### _How was this patch tested?_
    - [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #3158 from turboFei/batch_npe.
    
    Closes #3158
    
    fa9b53ec [Fei Wang] refactor
    eed61515 [Fei Wang] nit
    b54bff84 [Fei Wang] fix npe
    
    Authored-by: Fei Wang <fw...@ebay.com>
    Signed-off-by: ulysses-you <ul...@apache.org>
---
 .../org/apache/kyuubi/ctl/util/Tabulator.scala     |  3 +-
 .../apache/kyuubi/ctl/util/TabulatorSuite.scala}   | 35 +++++++++++-----------
 2 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala b/kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala
index 97dc6dd2e..c3792efa9 100644
--- a/kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala
+++ b/kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala
@@ -34,6 +34,7 @@ private[kyuubi] object Tabulator {
   }
 
   private def formatTextTable(header: Array[String], rows: Array[Array[String]]): String = {
-    FlipTable.of(header, rows)
+    val normalizedRows = rows.map(row => row.map(Option(_).getOrElse("N/A")))
+    FlipTable.of(header, normalizedRows)
   }
 }
diff --git a/kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala b/kyuubi-ctl/src/test/scala/org/apache/kyuubi/ctl/util/TabulatorSuite.scala
similarity index 55%
copy from kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala
copy to kyuubi-ctl/src/test/scala/org/apache/kyuubi/ctl/util/TabulatorSuite.scala
index 97dc6dd2e..c57c4de5a 100644
--- a/kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/util/Tabulator.scala
+++ b/kyuubi-ctl/src/test/scala/org/apache/kyuubi/ctl/util/TabulatorSuite.scala
@@ -17,23 +17,24 @@
 
 package org.apache.kyuubi.ctl.util
 
-import com.jakewharton.fliptables.FlipTable
-import org.apache.commons.lang3.StringUtils
+import org.apache.kyuubi.KyuubiFunSuite
 
-private[kyuubi] object Tabulator {
-  def format(title: String, header: Array[String], rows: Array[Array[String]]): String = {
-    val textTable = formatTextTable(header, rows)
-    val footer = s"${rows.size} row(s)\n"
-    if (StringUtils.isBlank(title)) {
-      textTable + footer
-    } else {
-      val rowWidth = textTable.split("\n").head.size
-      val titleNewLine = "\n" + StringUtils.center(title, rowWidth) + "\n"
-      titleNewLine + textTable + footer
-    }
-  }
-
-  private def formatTextTable(header: Array[String], rows: Array[Array[String]]): String = {
-    FlipTable.of(header, rows)
+class TabulatorSuite extends KyuubiFunSuite {
+  test("format rows have null") {
+    val rows: Array[Array[String]] = Array(Array("1", ""), Array(null, "2"))
+    // scalastyle:off
+    val expected =
+      """
+        |╔═════╤════╗
+        |║ c1  │ c2 ║
+        |╠═════╪════╣
+        |║ 1   │    ║
+        |╟─────┼────╢
+        |║ N/A │ 2  ║
+        |╚═════╧════╝
+        |""".stripMargin
+    // scalastyle:on
+    val result = Tabulator.format("test", Array("c1", "c2"), rows)
+    assert(result.contains(expected))
   }
 }