You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by fc...@apache.org on 2023/02/16 09:09:28 UTC

[kyuubi] branch branch-1.6 updated: [KYUUBI #4305][Bug] Backport HIVE-15820: comment at the head of beeline -e

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

fchen pushed a commit to branch branch-1.6
in repository https://gitbox.apache.org/repos/asf/kyuubi.git


The following commit(s) were added to refs/heads/branch-1.6 by this push:
     new 31483c1dc [KYUUBI #4305][Bug] Backport HIVE-15820: comment at the head of beeline -e
31483c1dc is described below

commit 31483c1dc42fe0621c606d0038e783db3015d76c
Author: senmiaoliu <se...@trip.com>
AuthorDate: Thu Feb 16 17:05:18 2023 +0800

    [KYUUBI #4305][Bug] Backport HIVE-15820: comment at the head of beeline -e
    
    close [#4305](https://github.com/apache/kyuubi/issues/4305)
    
    ```sql
    bin/beeline -u jdbc:hive2://X:10009 -e "
    --asd
    select 1 as a
    "
    ```
    
    ![image](https://user-images.githubusercontent.com/18713676/218910222-b829d447-e5b7-4d80-842b-2ddd4f47a26d.png)
    
    - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible
    
    - [x] Add screenshots for manual tests if appropriate
    
    - [ ] [Run test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #4333 from lsm1/fix/beeline_comment_header.
    
    Closes #4305
    
    f932d4ead [senmiaoliu] reformat
    9a071fb40 [senmiaoliu] added multi line ut
    425c53631 [senmiaoliu] added ut
    d4dc21a61 [senmiaoliu] comment at the head of beeline -e
    
    Authored-by: senmiaoliu <se...@trip.com>
    Signed-off-by: Fu Chen <cf...@gmail.com>
    (cherry picked from commit c489e29697c73e3eec96aa3ff602e40f9a2c798a)
    Signed-off-by: Fu Chen <cf...@gmail.com>
---
 .../org/apache/hive/beeline/KyuubiBeeLine.java     |  6 +++
 .../org/apache/hive/beeline/KyuubiCommands.java    | 54 ++++++++++++++++++++++
 .../org/apache/hive/beeline/KyuubiBeeLineTest.java | 13 ++++++
 3 files changed, 73 insertions(+)

diff --git a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
index 7711e21b6..9c8c908a1 100644
--- a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
+++ b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java
@@ -29,6 +29,7 @@ import java.util.List;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
+import org.apache.hive.common.util.HiveStringUtils;
 
 public class KyuubiBeeLine extends BeeLine {
   public static final String KYUUBI_BEELINE_DEFAULT_JDBC_DRIVER =
@@ -191,4 +192,9 @@ public class KyuubiBeeLine extends BeeLine {
     }
     return code;
   }
+
+  @Override
+  boolean dispatch(String line) {
+    return super.dispatch(HiveStringUtils.removeComments(line));
+  }
 }
diff --git a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
index 2144b0e2a..b36a5158f 100644
--- a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
+++ b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java
@@ -21,6 +21,7 @@ import java.io.*;
 import java.sql.*;
 import java.util.*;
 import org.apache.hive.beeline.logs.KyuubiBeelineInPlaceUpdateStream;
+import org.apache.hive.common.util.HiveStringUtils;
 import org.apache.kyuubi.jdbc.hive.JdbcConnectionParams;
 import org.apache.kyuubi.jdbc.hive.KyuubiStatement;
 import org.apache.kyuubi.jdbc.hive.Utils;
@@ -505,6 +506,59 @@ public class KyuubiCommands extends Commands {
     }
   }
 
+  @Override
+  public String handleMultiLineCmd(String line) throws IOException {
+    line = HiveStringUtils.removeComments(line);
+    Character mask =
+        (System.getProperty("jline.terminal", "").equals("jline.UnsupportedTerminal"))
+            ? null
+            : jline.console.ConsoleReader.NULL_MASK;
+
+    while (isMultiLine(line) && beeLine.getOpts().isAllowMultiLineCommand()) {
+      StringBuilder prompt = new StringBuilder(beeLine.getPrompt());
+      if (!beeLine.getOpts().isSilent()) {
+        for (int i = 0; i < prompt.length() - 1; i++) {
+          if (prompt.charAt(i) != '>') {
+            prompt.setCharAt(i, i % 2 == 0 ? '.' : ' ');
+          }
+        }
+      }
+      String extra;
+      // avoid NPE below if for some reason -e argument has multi-line command
+      if (beeLine.getConsoleReader() == null) {
+        throw new RuntimeException(
+            "Console reader not initialized. This could happen when there "
+                + "is a multi-line command using -e option and which requires further reading from console");
+      }
+      if (beeLine.getOpts().isSilent() && beeLine.getOpts().getScriptFile() != null) {
+        extra = beeLine.getConsoleReader().readLine(null, mask);
+      } else {
+        extra = beeLine.getConsoleReader().readLine(prompt.toString());
+      }
+
+      if (extra == null) { // it happens when using -f and the line of cmds does not end with ;
+        break;
+      }
+      extra = HiveStringUtils.removeComments(extra);
+      if (extra != null && !extra.isEmpty()) {
+        line += "\n" + extra;
+      }
+    }
+    return line;
+  }
+
+  // returns true if statement represented by line is not complete and needs additional reading from
+  // console. Used in handleMultiLineCmd method assumes line would never be null when this method is
+  // called
+  private boolean isMultiLine(String line) {
+    if (line.endsWith(beeLine.getOpts().getDelimiter()) || beeLine.isComment(line)) {
+      return false;
+    }
+    // handles the case like line = show tables; --test comment
+    List<String> cmds = getCmdList(line, false);
+    return cmds.isEmpty() || !cmds.get(cmds.size() - 1).startsWith("--");
+  }
+
   static class KyuubiLogRunnable implements Runnable {
     private final KyuubiCommands commands;
     private final KyuubiLoggable kyuubiLoggable;
diff --git a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
index b144c95c6..d571d9362 100644
--- a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
+++ b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java
@@ -29,4 +29,17 @@ public class KyuubiBeeLineTest {
     int result = kyuubiBeeLine.initArgs(new String[0]);
     assertEquals(0, result);
   }
+
+  @Test
+  public void testKyuubiBeelineComment() {
+    KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine();
+    int result = kyuubiBeeLine.initArgsFromCliVars(new String[] {"-e", "--comment show database;"});
+    assertEquals(0, result);
+    result = kyuubiBeeLine.initArgsFromCliVars(new String[] {"-e", "--comment\n show database;"});
+    assertEquals(1, result);
+    result =
+        kyuubiBeeLine.initArgsFromCliVars(
+            new String[] {"-e", "--comment line 1 \n    --comment line 2 \n show database;"});
+    assertEquals(1, result);
+  }
 }