in odps-console-resource/src/main/java/com/aliyun/openservices/odps/console/resource/AddResourceCommand.java [339:459]
public static AddResourceCommand parse(String commandString, ExecutionContext sessionContext)
throws ODPSConsoleException {
/*
* !HACK: filter ADD <object type> <object name> TO PACKAGE <package name>
* [WITH PRIVILEGES privileges];
*/
if (isSecurityCommand(commandString)) {
return null;
}
String oldString = commandString;
// file/py/jar/archive
if (commandString.toUpperCase().matches(
"\\s*ADD\\s+((FILE)|(PY)|(JAR)|(ARCHIVE)|(TABLE)|(VOLUMEFILE)|(VOLUMEARCHIVE))[\\s\\S]*")) {
commandString = commandString.replaceAll("\\s+", " ").trim();
// 把add去掉
commandString = commandString.substring(4);
boolean isUpdate = false;
if (commandString.toUpperCase().endsWith(" -F")) {
isUpdate = true;
commandString = commandString.substring(0, (commandString.length() - 2)).trim();
}
int commandIndex = commandString.indexOf(" ");
if (commandIndex == -1) {
throwUsageError(System.err, "bad command");
}
// 取出命令file/py/jar/archive
String command = commandString.substring(0, commandIndex).trim().toUpperCase();
commandString = commandString.substring(commandIndex).trim();
int fileIndex = commandString.indexOf(" ");
String refName;
if (fileIndex == -1) {
// 在此,可能有一些非法字符, 要判断localFile的正确性
refName = commandString;
} else {
refName = commandString.substring(0, fileIndex);
}
commandString = commandString.substring(refName.length()).trim();
String partitionSpec = "";
// 如果command是table,则要先处理partition_spec
if ("TABLE".equals(command)) {
if (commandString.toUpperCase().indexOf("PARTITION") == 0 && commandString.indexOf("(") > 0
&& commandString.indexOf(")") > 0) {
partitionSpec = commandString.substring(commandString.indexOf("(") + 1,
commandString.indexOf(")"));
commandString = commandString.substring(commandString.indexOf(")") + 1).trim();
}
}
// as alias
int asIndex = commandString.toUpperCase().indexOf("AS ");
String alias = "";
if (asIndex == 0) {
// 说明有as,不是add table的情况
commandString = commandString.substring(2).trim();
int asIndexSpace = commandString.indexOf(" ");
if (asIndexSpace > 0) {
alias = commandString.substring(0, asIndexSpace);
} else {
alias = commandString;
}
}
commandString = commandString.substring(alias.length()).trim();
// comment
int commentIndex = commandString.toUpperCase().indexOf("COMMENT ");
String comment = "";
if (commentIndex == 0) {
// 说明有as,不是add table的情况
commandString = commandString.substring("COMMENT ".length()).trim();
comment = commandString;
}
commandString = commandString.substring(comment.length()).trim();
if (!commandString.isEmpty()) {
String warningName = "AddResourceCommandAsMissing";
Long count = OdpsDeprecatedLogger.getDeprecatedCalls().get(warningName);
if (count == null) {
count = 1L;
} else {
count += 1L;
}
OdpsDeprecatedLogger.getDeprecatedCalls().put(warningName, count);
System.err.println(
"Warning: ignore part \"" + commandString + "\" in command, maybe 'AS' is missing");
}
if ("TABLE".equals(command) && StringUtils.isNullOrEmpty(alias)) {
alias = refName;
}
AddResourceCommand addResourceCommand = new AddResourceCommand(oldString, sessionContext);
addResourceCommand.type = command;
// refName和alias都可以引号括引来
addResourceCommand.refName = refName.replaceAll("'", "").replaceAll("\"", "");
if ("TABLE".equals(command)) {
addResourceCommand.tableCoordinate = Coordinate.getCoordinateABC(addResourceCommand.refName);
}
addResourceCommand.alias = alias.replaceAll("'", "").replaceAll("\"", "");
addResourceCommand.comment = comment;
addResourceCommand.partitionSpec = partitionSpec.replaceAll(" ", "");
addResourceCommand.isUpdate = isUpdate;
return addResourceCommand;
}
return null;
}