use of org.apache.flink.table.operations.Operation in project zeppelin by apache.
the class Flink114Shims method parseBySqlParser.
private SqlCommandCall parseBySqlParser(Parser sqlParser, String stmt) throws Exception {
List<Operation> operations;
try {
operations = sqlParser.parse(stmt);
} catch (Throwable e) {
throw new Exception("Invalidate SQL statement.", e);
}
if (operations.size() != 1) {
throw new Exception("Only single statement is supported now.");
}
final SqlCommand cmd;
String[] operands = new String[] { stmt };
Operation operation = operations.get(0);
if (operation instanceof CatalogSinkModifyOperation) {
boolean overwrite = ((CatalogSinkModifyOperation) operation).isOverwrite();
cmd = overwrite ? SqlCommand.INSERT_OVERWRITE : SqlCommand.INSERT_INTO;
} else if (operation instanceof CreateTableOperation) {
cmd = SqlCommand.CREATE_TABLE;
} else if (operation instanceof DropTableOperation) {
cmd = SqlCommand.DROP_TABLE;
} else if (operation instanceof AlterTableOperation) {
cmd = SqlCommand.ALTER_TABLE;
} else if (operation instanceof CreateViewOperation) {
cmd = SqlCommand.CREATE_VIEW;
} else if (operation instanceof DropViewOperation) {
cmd = SqlCommand.DROP_VIEW;
} else if (operation instanceof CreateDatabaseOperation) {
cmd = SqlCommand.CREATE_DATABASE;
} else if (operation instanceof DropDatabaseOperation) {
cmd = SqlCommand.DROP_DATABASE;
} else if (operation instanceof AlterDatabaseOperation) {
cmd = SqlCommand.ALTER_DATABASE;
} else if (operation instanceof CreateCatalogOperation) {
cmd = SqlCommand.CREATE_CATALOG;
} else if (operation instanceof DropCatalogOperation) {
cmd = SqlCommand.DROP_CATALOG;
} else if (operation instanceof UseCatalogOperation) {
cmd = SqlCommand.USE_CATALOG;
operands = new String[] { ((UseCatalogOperation) operation).getCatalogName() };
} else if (operation instanceof UseDatabaseOperation) {
cmd = SqlCommand.USE;
operands = new String[] { ((UseDatabaseOperation) operation).getDatabaseName() };
} else if (operation instanceof ShowCatalogsOperation) {
cmd = SqlCommand.SHOW_CATALOGS;
operands = new String[0];
} else if (operation instanceof ShowDatabasesOperation) {
cmd = SqlCommand.SHOW_DATABASES;
operands = new String[0];
} else if (operation instanceof ShowTablesOperation) {
cmd = SqlCommand.SHOW_TABLES;
operands = new String[0];
} else if (operation instanceof ShowFunctionsOperation) {
cmd = SqlCommand.SHOW_FUNCTIONS;
operands = new String[0];
} else if (operation instanceof CreateCatalogFunctionOperation || operation instanceof CreateTempSystemFunctionOperation) {
cmd = SqlCommand.CREATE_FUNCTION;
} else if (operation instanceof DropCatalogFunctionOperation || operation instanceof DropTempSystemFunctionOperation) {
cmd = SqlCommand.DROP_FUNCTION;
} else if (operation instanceof AlterCatalogFunctionOperation) {
cmd = SqlCommand.ALTER_FUNCTION;
} else if (operation instanceof ExplainOperation) {
cmd = SqlCommand.EXPLAIN;
} else if (operation instanceof DescribeTableOperation) {
cmd = SqlCommand.DESCRIBE;
operands = new String[] { ((DescribeTableOperation) operation).getSqlIdentifier().asSerializableString() };
} else if (operation instanceof QueryOperation) {
cmd = SqlCommand.SELECT;
} else {
throw new Exception("Unknown operation: " + operation.asSummaryString());
}
return new SqlCommandCall(cmd, operands, stmt);
}
use of org.apache.flink.table.operations.Operation in project zeppelin by apache.
the class Flink112Shims method parseBySqlParser.
private SqlCommandCall parseBySqlParser(Parser sqlParser, String stmt) throws Exception {
List<Operation> operations;
try {
operations = sqlParser.parse(stmt);
} catch (Throwable e) {
throw new Exception("Invalidate SQL statement.", e);
}
if (operations.size() != 1) {
throw new Exception("Only single statement is supported now.");
}
final SqlCommand cmd;
String[] operands = new String[] { stmt };
Operation operation = operations.get(0);
if (operation instanceof CatalogSinkModifyOperation) {
boolean overwrite = ((CatalogSinkModifyOperation) operation).isOverwrite();
cmd = overwrite ? SqlCommand.INSERT_OVERWRITE : SqlCommand.INSERT_INTO;
} else if (operation instanceof CreateTableOperation) {
cmd = SqlCommand.CREATE_TABLE;
} else if (operation instanceof DropTableOperation) {
cmd = SqlCommand.DROP_TABLE;
} else if (operation instanceof AlterTableOperation) {
cmd = SqlCommand.ALTER_TABLE;
} else if (operation instanceof CreateViewOperation) {
cmd = SqlCommand.CREATE_VIEW;
} else if (operation instanceof DropViewOperation) {
cmd = SqlCommand.DROP_VIEW;
} else if (operation instanceof CreateDatabaseOperation) {
cmd = SqlCommand.CREATE_DATABASE;
} else if (operation instanceof DropDatabaseOperation) {
cmd = SqlCommand.DROP_DATABASE;
} else if (operation instanceof AlterDatabaseOperation) {
cmd = SqlCommand.ALTER_DATABASE;
} else if (operation instanceof CreateCatalogOperation) {
cmd = SqlCommand.CREATE_CATALOG;
} else if (operation instanceof DropCatalogOperation) {
cmd = SqlCommand.DROP_CATALOG;
} else if (operation instanceof UseCatalogOperation) {
cmd = SqlCommand.USE_CATALOG;
operands = new String[] { ((UseCatalogOperation) operation).getCatalogName() };
} else if (operation instanceof UseDatabaseOperation) {
cmd = SqlCommand.USE;
operands = new String[] { ((UseDatabaseOperation) operation).getDatabaseName() };
} else if (operation instanceof ShowCatalogsOperation) {
cmd = SqlCommand.SHOW_CATALOGS;
operands = new String[0];
} else if (operation instanceof ShowDatabasesOperation) {
cmd = SqlCommand.SHOW_DATABASES;
operands = new String[0];
} else if (operation instanceof ShowTablesOperation) {
cmd = SqlCommand.SHOW_TABLES;
operands = new String[0];
} else if (operation instanceof ShowFunctionsOperation) {
cmd = SqlCommand.SHOW_FUNCTIONS;
operands = new String[0];
} else if (operation instanceof CreateCatalogFunctionOperation || operation instanceof CreateTempSystemFunctionOperation) {
cmd = SqlCommand.CREATE_FUNCTION;
} else if (operation instanceof DropCatalogFunctionOperation || operation instanceof DropTempSystemFunctionOperation) {
cmd = SqlCommand.DROP_FUNCTION;
} else if (operation instanceof AlterCatalogFunctionOperation) {
cmd = SqlCommand.ALTER_FUNCTION;
} else if (operation instanceof ExplainOperation) {
cmd = SqlCommand.EXPLAIN;
} else if (operation instanceof DescribeTableOperation) {
cmd = SqlCommand.DESCRIBE;
operands = new String[] { ((DescribeTableOperation) operation).getSqlIdentifier().asSerializableString() };
} else if (operation instanceof QueryOperation) {
cmd = SqlCommand.SELECT;
} else {
throw new Exception("Unknown operation: " + operation.asSummaryString());
}
return new SqlCommandCall(cmd, operands, stmt);
}
use of org.apache.flink.table.operations.Operation in project flink by apache.
the class CliClient method getAndExecuteStatements.
private boolean getAndExecuteStatements(LineReader lineReader, ExecutionMode mode) {
// begin reading loop
boolean exitOnFailure = !mode.equals(ExecutionMode.INTERACTIVE_EXECUTION);
isRunning = true;
while (isRunning) {
// make some space to previous command
terminal.writer().append("\n");
terminal.flush();
Optional<Operation> parsedOperation = Optional.empty();
try {
// read a statement from terminal and parse it
String line = lineReader.readLine(prompt, null, inputTransformer, null);
if (line.trim().isEmpty()) {
continue;
}
// get the parsed operation.
// if the command is invalid, the exception caught from parser would be thrown.
parsedOperation = parser.getParsedOperation();
Preconditions.checkArgument(line.equals(parser.getCommand()), String.format("This is a bug, please report to the flink community. Statement read[%s] isn't the same as statement parsed[%s]", line, parser.getCommand()));
} catch (SqlExecutionException e) {
// print the detailed information on about the parse errors in the terminal.
printExecutionException(e);
if (exitOnFailure) {
return false;
}
} catch (UserInterruptException e) {
// user cancelled line with Ctrl+C
continue;
} catch (EndOfFileException | IOError e) {
// user cancelled application with Ctrl+D or kill
break;
} catch (Throwable t) {
throw new SqlClientException("Could not read from command line.", t);
}
// no operation available, read next command
if (!parsedOperation.isPresent()) {
continue;
}
// execute the operation
boolean success = executeOperation(parsedOperation.get(), mode);
if (exitOnFailure && !success) {
return false;
}
}
// finish all statements.
return true;
}
use of org.apache.flink.table.operations.Operation in project flink by apache.
the class LocalExecutor method parseStatement.
@Override
public Operation parseStatement(String sessionId, String statement) throws SqlExecutionException {
final ExecutionContext context = getExecutionContext(sessionId);
final TableEnvironment tableEnv = context.getTableEnvironment();
Parser parser = ((TableEnvironmentInternal) tableEnv).getParser();
List<Operation> operations;
try {
operations = context.wrapClassLoader(() -> parser.parse(statement));
} catch (Exception e) {
throw new SqlExecutionException("Failed to parse statement: " + statement, e);
}
if (operations.isEmpty()) {
throw new SqlExecutionException("Failed to parse statement: " + statement);
}
return operations.get(0);
}
use of org.apache.flink.table.operations.Operation in project flink by apache.
the class TableEnvironmentImpl method sqlQuery.
@Override
public Table sqlQuery(String query) {
List<Operation> operations = getParser().parse(query);
if (operations.size() != 1) {
throw new ValidationException("Unsupported SQL query! sqlQuery() only accepts a single SQL query.");
}
Operation operation = operations.get(0);
if (operation instanceof QueryOperation && !(operation instanceof ModifyOperation)) {
return createTable((QueryOperation) operation);
} else {
throw new ValidationException("Unsupported SQL query! sqlQuery() only accepts a single SQL query of type " + "SELECT, UNION, INTERSECT, EXCEPT, VALUES, and ORDER_BY.");
}
}
Aggregations