Skip to content

Commit fbe38de

Browse files
jiaoqingboturboFei
authored andcommitted
[KYUUBI #2478] Backport HIVE-19018 to Kyuubi Beeline
### _Why are the changes needed?_ fix #2478 ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [x] 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 #2489 from jiaoqingbo/2478. Closes #2478 3565e58 [jiaoqingbo] invoke CI 0e127c6 [jiaoqingbo] [KYUUBI #2478] Backport HIVE-19018 to Kyuubi Beeline Authored-by: jiaoqingbo <1178404354@qq.com> Signed-off-by: Fei Wang <fwang12@ebay.com> (cherry picked from commit 268db01) Signed-off-by: Fei Wang <fwang12@ebay.com>
1 parent 87f81e3 commit fbe38de

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.lang.reflect.Field;
23+
import java.lang.reflect.Method;
2324
import java.sql.Driver;
25+
import java.util.Arrays;
26+
import java.util.Collections;
27+
import java.util.Iterator;
28+
import java.util.List;
29+
import org.apache.commons.cli.CommandLine;
30+
import org.apache.commons.cli.Options;
31+
import org.apache.commons.cli.ParseException;
2432

2533
public class KyuubiBeeLine extends BeeLine {
2634
public static final String KYUUBI_BEELINE_DEFAULT_JDBC_DRIVER =
@@ -97,4 +105,97 @@ String getApplicationTitle() {
97105
"Apache Kyuubi (Incubating)",
98106
});
99107
}
108+
109+
@Override
110+
int initArgs(String[] args) {
111+
List<String> commands = Collections.emptyList();
112+
113+
CommandLine cl;
114+
BeelineParser beelineParser;
115+
boolean connSuccessful;
116+
boolean exit;
117+
Field exitField;
118+
119+
try {
120+
Field optionsField = BeeLine.class.getDeclaredField("options");
121+
optionsField.setAccessible(true);
122+
Options options = (Options) optionsField.get(this);
123+
124+
beelineParser = new BeelineParser();
125+
cl = beelineParser.parse(options, args);
126+
127+
Method connectUsingArgsMethod =
128+
BeeLine.class.getDeclaredMethod(
129+
"connectUsingArgs", BeelineParser.class, CommandLine.class);
130+
connectUsingArgsMethod.setAccessible(true);
131+
connSuccessful = (boolean) connectUsingArgsMethod.invoke(this, beelineParser, cl);
132+
133+
exitField = BeeLine.class.getDeclaredField("exit");
134+
exitField.setAccessible(true);
135+
exit = (boolean) exitField.get(this);
136+
137+
} catch (ParseException e1) {
138+
output(e1.getMessage());
139+
usage();
140+
return -1;
141+
} catch (Exception t) {
142+
error(t.getMessage());
143+
return 1;
144+
}
145+
146+
// checks if default hs2 connection configuration file is present
147+
// and uses it to connect if found
148+
// no-op if the file is not present
149+
if (!connSuccessful && !exit) {
150+
try {
151+
Method defaultBeelineConnectMethod =
152+
BeeLine.class.getDeclaredMethod("defaultBeelineConnect");
153+
defaultBeelineConnectMethod.setAccessible(true);
154+
connSuccessful = (boolean) defaultBeelineConnectMethod.invoke(this);
155+
156+
} catch (Exception t) {
157+
error(t.getMessage());
158+
return 1;
159+
}
160+
}
161+
162+
int code = 0;
163+
if (cl.getOptionValues('e') != null) {
164+
commands = Arrays.asList(cl.getOptionValues('e'));
165+
try {
166+
Field optsField = BeeLine.class.getDeclaredField("opts");
167+
optsField.setAccessible(true);
168+
BeeLineOpts opts = (BeeLineOpts) optsField.get(this);
169+
opts.setAllowMultiLineCommand(false);
170+
} catch (Exception t) {
171+
error(t.getMessage());
172+
return 1;
173+
}
174+
}
175+
176+
if (!commands.isEmpty() && getOpts().getScriptFile() != null) {
177+
error("The '-e' and '-f' options cannot be specified simultaneously");
178+
return 1;
179+
} else if (!commands.isEmpty() && !connSuccessful) {
180+
error("Cannot run commands specified using -e. No current connection");
181+
return 1;
182+
}
183+
if (!commands.isEmpty()) {
184+
for (Iterator<String> i = commands.iterator(); i.hasNext(); ) {
185+
String command = i.next().toString();
186+
debug(loc("executing-command", command));
187+
if (!dispatch(command)) {
188+
code++;
189+
}
190+
}
191+
try {
192+
exit = true;
193+
exitField.set(this, exit);
194+
} catch (Exception e) {
195+
error(e.getMessage());
196+
return 1;
197+
}
198+
}
199+
return code;
200+
}
100201
}

0 commit comments

Comments
 (0)