Skip to content

Commit

Permalink
HIVE-4577: hive CLI can't handle hadoop dfs command with space and qu…
Browse files Browse the repository at this point in the history
…otes (Bing Li reviewed by Vaibhav Gumashta)
  • Loading branch information
Vaibhav Gumashta committed Jul 14, 2017
1 parent 4af4624 commit adca35a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.PrintStream;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;

import org.slf4j.Logger;
Expand All @@ -30,6 +31,7 @@
import org.apache.hadoop.hive.conf.VariableSubstitution;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Schema;
import org.apache.hadoop.hive.ql.CommandNeedRetryException;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
import org.apache.hadoop.hive.ql.session.SessionState;
import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
Expand Down Expand Up @@ -74,7 +76,7 @@ public Map<String, String> getHiveVariable() {
}
}).substitute(ss.getConf(), command);

String[] tokens = command.split("\\s+");
String[] tokens = splitCmd(command);
CommandProcessorResponse authErrResp =
CommandUtil.authorizeCommand(ss, HiveOperationType.DFS, Arrays.asList(tokens));
if(authErrResp != null){
Expand Down Expand Up @@ -104,4 +106,60 @@ public Map<String, String> getHiveVariable() {
}
}

private String[] splitCmd(String command) throws CommandNeedRetryException {

ArrayList<String> paras = new ArrayList<String>();
int cmdLng = command.length();
char y = 0;
int start = 0;

for (int i = 0; i < cmdLng; i++) {
char x = command.charAt(i);

switch(x) {
case ' ':
if ((int) y == 0) {
String str = command.substring(start, i).trim();
if (!str.equals("")) {
paras.add(str);
start = i + 1;
}
}
break;
case '"':
if ((int) y == 0) {
y = x;
start = i + 1;
} else if ('"' == y) {
paras.add(command.substring(start, i).trim());
y = 0;
start = i + 1;
}
break;
case '\'':
if ((int) y == 0) {
y = x;
start = i + 1;
} else if ('\'' == y) {
paras.add(command.substring(start, i).trim());
y = 0;
start = i + 1;
}
break;
default:
if (i == cmdLng-1 && start < cmdLng) {
paras.add(command.substring(start, cmdLng).trim());
}
break;
}
}

if ((int) y != 0) {
console.printError("Syntax error on hadoop options: dfs " + command);
throw new CommandNeedRetryException();
}

return paras.toArray(new String[paras.size()]);
}

}
7 changes: 7 additions & 0 deletions ql/src/test/queries/clientpositive/dfscmd.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dfs -mkdir "hello";
dfs -mkdir 'world';
dfs -mkdir "bei jing";
dfs -rmr 'hello';
dfs -rmr "world";
dfs -rmr 'bei jing';

1 change: 1 addition & 0 deletions ql/src/test/results/clientpositive/dfscmd.q.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#### A masked pattern was here ####
2 changes: 1 addition & 1 deletion ql/src/test/results/clientpositive/perf/query14.q.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Warning: Shuffle Join MERGEJOIN[890][tables = [$hdt$_1, $hdt$_2, $hdt$_0]] in Stage 'Reducer 5' is a cross product
Warning: Shuffle Join MERGEJOIN[891][tables = [$hdt$_1, $hdt$_2, $hdt$_0]] in Stage 'Reducer 12' is a cross product
Warning: Shuffle Join MERGEJOIN[892][tables = [$hdt$_2, $hdt$_3, $hdt$_1]] in Stage 'Reducer 16' is a cross product
Warning: Shuffle Join MERGEJOIN[890][tables = [$hdt$_1, $hdt$_2, $hdt$_0]] in Stage 'Reducer 5' is a cross product
PREHOOK: query: explain
with cross_items as
(select i_item_sk ss_item_sk
Expand Down

0 comments on commit adca35a

Please sign in to comment.