Skip to content

Commit 3337ca8

Browse files
committed
options
python options save save remove comments for non python mode save ut
1 parent 01320c4 commit 3337ca8

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
import java.io.IOException;
2121
import java.io.InputStream;
22+
import java.lang.reflect.Field;
23+
import java.lang.reflect.Modifier;
2224
import java.sql.Driver;
23-
import java.util.Arrays;
24-
import java.util.Collections;
25-
import java.util.List;
25+
import java.util.*;
2626
import org.apache.commons.cli.CommandLine;
2727
import org.apache.commons.cli.Options;
2828
import org.apache.commons.cli.ParseException;
@@ -41,6 +41,12 @@ public class KyuubiBeeLine extends BeeLine {
4141
private static final int ERRNO_ARGS = 1;
4242
private static final int ERRNO_OTHER = 2;
4343

44+
private static final ResourceBundle beelineResourceBundle =
45+
ResourceBundle.getBundle(BeeLine.class.getSimpleName());
46+
private static final ResourceBundle kyuubiResourceBundle = new KyuubiBeelineResourceBundle();
47+
private static final String PYTHON_MODE_PREFIX = "--python-mode";
48+
private boolean pythonMode = false;
49+
4450
public KyuubiBeeLine() {
4551
this(true);
4652
}
@@ -50,6 +56,13 @@ public KyuubiBeeLine(boolean isBeeLine) {
5056
super(isBeeLine);
5157
try {
5258
DynFields.builder().hiddenImpl(BeeLine.class, "commands").buildChecked(this).set(commands);
59+
60+
Field resourceBundleField = BeeLine.class.getDeclaredField("resourceBundle");
61+
resourceBundleField.setAccessible(true);
62+
Field modifiers = Field.class.getDeclaredField("modifiers");
63+
modifiers.setAccessible(true);
64+
modifiers.setInt(resourceBundleField, resourceBundleField.getModifiers() & ~Modifier.FINAL);
65+
resourceBundleField.set(null, kyuubiResourceBundle);
5366
} catch (Throwable t) {
5467
throw new ExceptionInInitializerError("Failed to inject kyuubi commands");
5568
}
@@ -64,6 +77,15 @@ public KyuubiBeeLine(boolean isBeeLine) {
6477
}
6578
}
6679

80+
public boolean isPythonMode() {
81+
return pythonMode;
82+
}
83+
84+
// Visible for testing
85+
public void setPythonMode(boolean pythonMode) {
86+
this.pythonMode = pythonMode;
87+
}
88+
6789
/** Starts the program. */
6890
public static void main(String[] args) throws IOException {
6991
mainWithInputRedirection(args, null);
@@ -125,7 +147,20 @@ int initArgs(String[] args) {
125147
.<Options>buildStaticChecked()
126148
.get();
127149

128-
beelineParser = new BeelineParser();
150+
beelineParser =
151+
new BeelineParser() {
152+
@Override
153+
protected void processOption(String arg, ListIterator iter) throws ParseException {
154+
if (PYTHON_MODE_PREFIX.equals(arg)) {
155+
String stripped = arg.substring(2, arg.length());
156+
String[] parts = split(stripped, "=");
157+
String value = parts.length >= 2 ? parts[1] : "true";
158+
pythonMode = Boolean.parseBoolean(value);
159+
} else {
160+
super.processOption(arg, iter);
161+
}
162+
}
163+
};
129164
cl = beelineParser.parse(options, args);
130165

131166
connSuccessful =
@@ -248,4 +283,34 @@ int runInit() {
248283
}
249284
return executionResult;
250285
}
286+
287+
static class KyuubiBeelineResourceBundle extends ListResourceBundle {
288+
static String CMD_USAGE = "cmd-usage";
289+
290+
private Object[][] contents = new Object[beelineResourceBundle.keySet().size()][];
291+
292+
public KyuubiBeelineResourceBundle() {
293+
int i = 0;
294+
for (String key : beelineResourceBundle.keySet()) {
295+
String value = beelineResourceBundle.getString(key);
296+
if (key.equals(CMD_USAGE)) {
297+
StringBuilder stringBuilder = new StringBuilder();
298+
stringBuilder.append(value).append("\n");
299+
stringBuilder
300+
.append("Usage: java " + KyuubiBeeLine.class.getCanonicalName())
301+
.append("\n");
302+
stringBuilder.append(
303+
" --python-mode=[true/false] Execute python code/script.");
304+
value = stringBuilder.toString();
305+
}
306+
contents[i] = new Object[] {key, value};
307+
i++;
308+
}
309+
}
310+
311+
@Override
312+
protected Object[][] getContents() {
313+
return contents;
314+
}
315+
}
251316
}

kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.sql.*;
2525
import java.util.*;
2626
import org.apache.hive.beeline.logs.KyuubiBeelineInPlaceUpdateStream;
27+
import org.apache.hive.common.util.HiveStringUtils;
2728
import org.apache.kyuubi.jdbc.hive.KyuubiStatement;
2829
import org.apache.kyuubi.jdbc.hive.Utils;
2930
import org.apache.kyuubi.jdbc.hive.logs.InPlaceUpdateStream;
@@ -522,6 +523,10 @@ public String handleMultiLineCmd(String line) throws IOException {
522523
? null
523524
: jline.console.ConsoleReader.NULL_MASK;
524525

526+
int[] startQuote = {-1};
527+
if (!beeLine.isPythonMode()) {
528+
line = HiveStringUtils.removeComments(line, startQuote);
529+
}
525530
while (isMultiLine(line) && beeLine.getOpts().isAllowMultiLineCommand()) {
526531
StringBuilder prompt = new StringBuilder(beeLine.getPrompt());
527532
if (!beeLine.getOpts().isSilent()) {
@@ -547,6 +552,9 @@ public String handleMultiLineCmd(String line) throws IOException {
547552
if (extra == null) { // it happens when using -f and the line of cmds does not end with ;
548553
break;
549554
}
555+
if (!beeLine.isPythonMode()) {
556+
extra = HiveStringUtils.removeComments(extra, startQuote);
557+
}
550558
if (!extra.isEmpty()) {
551559
line += "\n" + extra;
552560
}

kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiCommandsTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void testParsePythonSnippets() throws IOException {
3434
Mockito.when(reader.readLine()).thenReturn(pythonSnippets);
3535

3636
KyuubiBeeLine beeline = new KyuubiBeeLine();
37+
beeline.setPythonMode(true);
3738
beeline.setConsoleReader(reader);
3839
KyuubiCommands commands = new KyuubiCommands(beeline);
3940
String line = commands.handleMultiLineCmd(pythonSnippets);
@@ -42,4 +43,21 @@ public void testParsePythonSnippets() throws IOException {
4243
assertEquals(cmdList.size(), 1);
4344
assertEquals(cmdList.get(0), pythonSnippets);
4445
}
46+
47+
@Test
48+
public void testHandleMultiLineCmd() throws IOException {
49+
ConsoleReader reader = Mockito.mock(ConsoleReader.class);
50+
String snippets = "select 1;--comments1\nselect 2;--comments2";
51+
Mockito.when(reader.readLine()).thenReturn(snippets);
52+
53+
KyuubiBeeLine beeline = new KyuubiBeeLine();
54+
beeline.setConsoleReader(reader);
55+
beeline.setPythonMode(false);
56+
KyuubiCommands commands = new KyuubiCommands(beeline);
57+
String line = commands.handleMultiLineCmd(snippets);
58+
List<String> cmdList = commands.getCmdList(line, false);
59+
assertEquals(cmdList.size(), 2);
60+
assertEquals(cmdList.get(0), "select 1");
61+
assertEquals(cmdList.get(1), "\nselect 2");
62+
}
4563
}

0 commit comments

Comments
 (0)