Skip to content

Commit

Permalink
[KYUUBI #2478] Backport HIVE-19018 to Kyuubi Beeline
Browse files Browse the repository at this point in the history
### _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>
  • Loading branch information
jiaoqingbo authored and turboFei committed Apr 27, 2022
1 parent 87f81e3 commit fbe38de
Showing 1 changed file with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Driver;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

public class KyuubiBeeLine extends BeeLine {
public static final String KYUUBI_BEELINE_DEFAULT_JDBC_DRIVER =
Expand Down Expand Up @@ -97,4 +105,97 @@ String getApplicationTitle() {
"Apache Kyuubi (Incubating)",
});
}

@Override
int initArgs(String[] args) {
List<String> commands = Collections.emptyList();

CommandLine cl;
BeelineParser beelineParser;
boolean connSuccessful;
boolean exit;
Field exitField;

try {
Field optionsField = BeeLine.class.getDeclaredField("options");
optionsField.setAccessible(true);
Options options = (Options) optionsField.get(this);

beelineParser = new BeelineParser();
cl = beelineParser.parse(options, args);

Method connectUsingArgsMethod =
BeeLine.class.getDeclaredMethod(
"connectUsingArgs", BeelineParser.class, CommandLine.class);
connectUsingArgsMethod.setAccessible(true);
connSuccessful = (boolean) connectUsingArgsMethod.invoke(this, beelineParser, cl);

exitField = BeeLine.class.getDeclaredField("exit");
exitField.setAccessible(true);
exit = (boolean) exitField.get(this);

} catch (ParseException e1) {
output(e1.getMessage());
usage();
return -1;
} catch (Exception t) {
error(t.getMessage());
return 1;
}

// checks if default hs2 connection configuration file is present
// and uses it to connect if found
// no-op if the file is not present
if (!connSuccessful && !exit) {
try {
Method defaultBeelineConnectMethod =
BeeLine.class.getDeclaredMethod("defaultBeelineConnect");
defaultBeelineConnectMethod.setAccessible(true);
connSuccessful = (boolean) defaultBeelineConnectMethod.invoke(this);

} catch (Exception t) {
error(t.getMessage());
return 1;
}
}

int code = 0;
if (cl.getOptionValues('e') != null) {
commands = Arrays.asList(cl.getOptionValues('e'));
try {
Field optsField = BeeLine.class.getDeclaredField("opts");
optsField.setAccessible(true);
BeeLineOpts opts = (BeeLineOpts) optsField.get(this);
opts.setAllowMultiLineCommand(false);
} catch (Exception t) {
error(t.getMessage());
return 1;
}
}

if (!commands.isEmpty() && getOpts().getScriptFile() != null) {
error("The '-e' and '-f' options cannot be specified simultaneously");
return 1;
} else if (!commands.isEmpty() && !connSuccessful) {
error("Cannot run commands specified using -e. No current connection");
return 1;
}
if (!commands.isEmpty()) {
for (Iterator<String> i = commands.iterator(); i.hasNext(); ) {
String command = i.next().toString();
debug(loc("executing-command", command));
if (!dispatch(command)) {
code++;
}
}
try {
exit = true;
exitField.set(this, exit);
} catch (Exception e) {
error(e.getMessage());
return 1;
}
}
return code;
}
}

0 comments on commit fbe38de

Please sign in to comment.