wangyum commented on code in PR #40838:
URL: https://github.com/apache/spark/pull/40838#discussion_r1169800478


##########
sql/hive-thriftserver/src/main/java/org/apache/spark/sql/hive/thriftserver/CliDriverUtils.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.hive.thriftserver;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import jline.console.completer.ArgumentCompleter;
+import jline.console.completer.ArgumentCompleter.ArgumentDelimiter;
+import jline.console.completer.ArgumentCompleter.AbstractArgumentDelimiter;
+import jline.console.completer.Completer;
+import jline.console.completer.StringsCompleter;
+
+import org.apache.spark.sql.catalyst.util.SQLKeywordUtils;
+
+public class CliDriverUtils {
+
+  public static Completer[] getCommandCompleter(List<String> functions, 
List<String> confs) {
+    // StringsCompleter matches against a pre-defined wordlist
+    // We start with an empty wordlist and build it up
+    List<String> candidateStrings = new ArrayList<String>();
+    // We add Spark SQL function names
+    // For functions that aren't infix operators, we add an open
+    // parenthesis at the end.
+    functions.forEach(s -> {
+      if (s.matches("[a-z_]+")) {
+        candidateStrings.add(s + "(");
+      } else {
+        candidateStrings.add(s);
+      }
+    });
+
+    // We add Spark SQL keywords, including lower-cased versions
+    SQLKeywordUtils.keywords().foreach(s -> {
+      candidateStrings.add(s);
+      candidateStrings.add(s.toLowerCase(Locale.ROOT));
+      return null;
+    });
+
+    StringsCompleter strCompleter = new StringsCompleter(candidateStrings);
+
+    // Because we use parentheses in addition to whitespace
+    // as a keyword delimiter, we need to define a new ArgumentDelimiter
+    // that recognizes parenthesis as a delimiter.
+    ArgumentDelimiter delim = new AbstractArgumentDelimiter() {
+      @Override
+      public boolean isDelimiterChar(CharSequence buffer, int pos) {
+        char c = buffer.charAt(pos);
+        return (Character.isWhitespace(c) || c == '(' || c == ')' ||
+            c == '[' || c == ']');
+      }
+    };
+
+    // The ArgumentCompletor allows us to match multiple tokens
+    // in the same line.
+    final ArgumentCompleter argCompleter = new ArgumentCompleter(delim, 
strCompleter);
+    // By default ArgumentCompletor is in "strict" mode meaning
+    // a token is only auto-completed if all prior tokens
+    // match. We don't want that since there are valid tokens
+    // that are not in our wordlist (eg. table and column names)
+    argCompleter.setStrict(false);
+
+    // ArgumentCompletor always adds a space after a matched token.
+    // This is undesirable for function names because a space after
+    // the opening parenthesis is unnecessary (and uncommon) in Hive.
+    // We stack a custom Completor on top of our ArgumentCompletor
+    // to reverse this.
+    Completer customCompletor = new Completer () {
+      @Override
+      public int complete (String buffer, int offset, List completions) {
+        List<String> comp = completions;
+        int ret = argCompleter.complete(buffer, offset, completions);
+        // ConsoleReader will do the substitution if and only if there
+        // is exactly one valid completion, so we ignore other cases.
+        if (completions.size() == 1) {
+          if (comp.get(0).endsWith("( ")) {
+            comp.set(0, comp.get(0).trim());
+          }
+        }
+        return ret;
+      }
+    };
+
+    StringsCompleter confCompleter = new StringsCompleter(confs) {
+      @Override
+      public int complete(final String buffer, final int cursor, final 
List<CharSequence> clist) {
+        return super.complete(buffer, cursor, clist);
+      }
+    };
+
+    StringsCompleter setCompleter = new StringsCompleter("set") {
+      @Override
+      public int complete(String buffer, int cursor, List<CharSequence> clist) 
{
+        return buffer != null && buffer.equals("set") ? super.complete(buffer, 
cursor, clist) : -1;
+      }
+    };
+
+    ArgumentCompleter propCompleter = new ArgumentCompleter(setCompleter, 
confCompleter) {
+      @Override
+      public int complete(String buffer, int offset, List<CharSequence> 
completions) {
+        int ret = super.complete(buffer, offset, completions);
+        if (completions.size() == 1) {
+          completions.set(0, ((String)completions.get(0)).trim());
+        }
+        return ret;
+      }
+    };
+    return new Completer[] {propCompleter, customCompletor};
+  }
+
+}

Review Comment:
   Origin Hive's `getCommandCompleter`: 
https://github.com/apache/hive/blob/rel/release-2.3.9/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java#L554-L764



-- 
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]

Reply via email to