org.apache.flink.table.client.gateway.SqlExecutionException Java Examples

The following examples show how to use org.apache.flink.table.client.gateway.SqlExecutionException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqlCompleter.java    From flink with Apache License 2.0 6 votes vote down vote up
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
	String statement = line.line();

	// remove ';' at the end
	if (statement.endsWith(";")) {
		statement = statement.substring(0, statement.length() - 1);
	}

	// handle SQL client specific commands
	final String statementNormalized = statement.toUpperCase().trim();
	for (String commandHint : COMMAND_HINTS) {
		if (commandHint.startsWith(statementNormalized) && line.cursor() < commandHint.length()) {
			candidates.add(createCandidate(commandHint));
		}
	}

	// fallback to Table API hinting
	try {
		executor.completeStatement(context, statement, line.cursor())
			.forEach(hint -> candidates.add(createCandidate(hint)));
	} catch (SqlExecutionException e) {
		LOG.debug("Could not complete statement at " + line.cursor() + ":" + statement, e);
	}
}
 
Example #2
Source File: CliClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private void callCreateView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final String query = cmdCall.operands[1];

	final ViewEntry previousView = context.getViews().get(name);
	if (previousView != null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_ALREADY_EXISTS);
		return;
	}

	try {
		// perform and validate change
		context.addView(ViewEntry.create(name, query));
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_CREATED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.removeView(name);
		printExecutionException(e);
	}
}
 
Example #3
Source File: CliView.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void handleSignal(Signal signal) {
	synchronized (this) {
		switch (signal) {
			case INT:
				close(new SqlExecutionException("Forced interrupt."));
				break;
			case QUIT:
				close(new SqlExecutionException("Forced cancellation."));
				break;
			case WINCH:
				updateSize();
				if (isRunning) {
					display();
				}
				break;
		}
	}
}
 
Example #4
Source File: LocalExecutor.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void useCatalog(SessionContext session, String catalogName) throws SqlExecutionException {
	final ExecutionContext<?> context = getOrCreateExecutionContext(session);
	final TableEnvironment tableEnv = context
		.createEnvironmentInstance()
		.getTableEnvironment();

	context.wrapClassLoader(() -> {
		// Rely on TableEnvironment/CatalogManager to validate input
		try {
			tableEnv.useCatalog(catalogName);
		} catch (CatalogException e) {
			throw new SqlExecutionException("Failed to switch to catalog " + catalogName, e);
		}
		session.setCurrentCatalog(catalogName);
		session.setCurrentDatabase(tableEnv.getCurrentDatabase());
		return null;
	});
}
 
Example #5
Source File: CliClient.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void callCreateView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final String query = cmdCall.operands[1];

	final ViewEntry previousView = context.getViews().get(name);
	if (previousView != null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_ALREADY_EXISTS);
		return;
	}

	try {
		// perform and validate change
		context.addView(ViewEntry.create(name, query));
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_CREATED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.removeView(name);
		printExecutionException(e);
	}
}
 
Example #6
Source File: CliClientTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseNonExistingDB() throws Exception {
	Executor executor = mock(Executor.class);
	doThrow(new SqlExecutionException("mocked exception")).when(executor).useDatabase(any(), any());
	InputStream inputStream = new ByteArrayInputStream("use db;\n".getBytes());
	// don't care about the output
	OutputStream outputStream = new OutputStream() {
		@Override
		public void write(int b) throws IOException {
		}
	};
	CliClient cliClient = null;
	try (Terminal terminal = new DumbTerminal(inputStream, outputStream)) {
		cliClient = new CliClient(terminal, new SessionContext("test-session", new Environment()), executor);
		cliClient.open();
		verify(executor).useDatabase(any(), any());
	} finally {
		if (cliClient != null) {
			cliClient.close();
		}
	}
}
 
Example #7
Source File: LocalExecutor.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Applies the given update statement to the given table environment with query configuration.
 */
private <C> void applyUpdate(ExecutionContext<C> context, TableEnvironment tableEnv, QueryConfig queryConfig, String updateStatement) {
	// parse and validate statement
	try {
		context.wrapClassLoader(() -> {
			if (tableEnv instanceof StreamTableEnvironment) {
				((StreamTableEnvironment) tableEnv).sqlUpdate(updateStatement, (StreamQueryConfig) queryConfig);
			} else {
				tableEnv.sqlUpdate(updateStatement);
			}
			return null;
		});
	} catch (Throwable t) {
		// catch everything such that the statement does not crash the executor
		throw new SqlExecutionException("Invalid SQL update statement.", t);
	}
}
 
Example #8
Source File: SqlCompleter.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
	String statement = line.line();

	// remove ';' at the end
	if (statement.endsWith(";")) {
		statement = statement.substring(0, statement.length() - 1);
	}

	// handle SQL client specific commands
	final String statementNormalized = statement.toUpperCase().trim();
	for (String commandHint : COMMAND_HINTS) {
		if (commandHint.startsWith(statementNormalized) && line.cursor() < commandHint.length()) {
			candidates.add(createCandidate(commandHint));
		}
	}

	// fallback to Table API hinting
	try {
		executor.completeStatement(context, statement, line.cursor())
			.forEach(hint -> candidates.add(createCandidate(hint)));
	} catch (SqlExecutionException e) {
		LOG.debug("Could not complete statement at " + line.cursor() + ":" + statement, e);
	}
}
 
Example #9
Source File: ExecutionContext.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private EnvironmentInstance() {
	// create environments
	if (mergedEnv.getExecution().isStreamingExecution()) {
		streamExecEnv = createStreamExecutionEnvironment();
		execEnv = null;
		tableEnv = StreamTableEnvironment.create(streamExecEnv);
	} else if (mergedEnv.getExecution().isBatchExecution()) {
		streamExecEnv = null;
		execEnv = createExecutionEnvironment();
		tableEnv = BatchTableEnvironment.create(execEnv);
	} else {
		throw new SqlExecutionException("Unsupported execution type specified.");
	}

	// create query config
	queryConfig = createQueryConfig();

	// register table sources
	tableSources.forEach(tableEnv::registerTableSource);

	// register table sinks
	tableSinks.forEach(tableEnv::registerTableSink);

	// register user-defined functions
	registerFunctions();

	// register views and temporal tables in specified order
	mergedEnv.getTables().forEach((name, entry) -> {
		// if registering a view fails at this point,
		// it means that it accesses tables that are not available anymore
		if (entry instanceof ViewEntry) {
			final ViewEntry viewEntry = (ViewEntry) entry;
			registerView(viewEntry);
		} else if (entry instanceof TemporalTableEntry) {
			final TemporalTableEntry temporalTableEntry = (TemporalTableEntry) entry;
			registerTemporalTable(temporalTableEntry);
		}
	});
}
 
Example #10
Source File: ExecutionContext.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public EnvironmentInstance createEnvironmentInstance() {
	try {
		return new EnvironmentInstance();
	} catch (Throwable t) {
		// catch everything such that a wrong environment does not affect invocations
		throw new SqlExecutionException("Could not create environment instance.", t);
	}
}
 
Example #11
Source File: MaterializedCollectBatchResult.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public List<Row> retrievePage(int page) {
	synchronized (resultLock) {
		if (page <= 0 || page > pageCount) {
			throw new SqlExecutionException("Invalid page '" + page + "'.");
		}
		return resultTable.subList(pageSize * (page - 1), Math.min(resultTable.size(), page * pageSize));
	}
}
 
Example #12
Source File: CollectStreamResult.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
	try {
		deployer.run();
	} catch (SqlExecutionException e) {
		executionException = e;
	}
}
 
Example #13
Source File: CliClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void callSet(SqlCommandCall cmdCall) {
	// show all properties
	if (cmdCall.operands.length == 0) {
		final Map<String, String> properties;
		try {
			properties = executor.getSessionProperties(context);
		} catch (SqlExecutionException e) {
			printExecutionException(e);
			return;
		}
		if (properties.isEmpty()) {
			terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
		} else {
			properties
				.entrySet()
				.stream()
				.map((e) -> e.getKey() + "=" + e.getValue())
				.sorted()
				.forEach((p) -> terminal.writer().println(p));
		}
	}
	// set a property
	else {
		context.setSessionProperty(cmdCall.operands[0], cmdCall.operands[1]);
		terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_SET).toAnsi());
	}
	terminal.flush();
}
 
Example #14
Source File: CliClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void callShowTables() {
	final List<String> tables;
	try {
		tables = executor.listTables(context);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	if (tables.isEmpty()) {
		terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_EMPTY).toAnsi());
	} else {
		tables.forEach((v) -> terminal.writer().println(v));
	}
	terminal.flush();
}
 
Example #15
Source File: CliClient.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void callExplain(SqlCommandCall cmdCall) {
	final String explanation;
	try {
		explanation = executor.explainStatement(context, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.writer().println(explanation);
	terminal.flush();
}
 
Example #16
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> getSessionProperties(SessionContext session) throws SqlExecutionException {
	final Environment env = getOrCreateExecutionContext(session)
		.getMergedEnvironment();
	final Map<String, String> properties = new HashMap<>();
	properties.putAll(env.getExecution().asTopLevelMap());
	properties.putAll(env.getDeployment().asTopLevelMap());
	properties.putAll(env.getConfiguration().asMap());
	return properties;
}
 
Example #17
Source File: CliTableResultView.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
protected void refresh() {
	// take snapshot
	TypedResult<Integer> result;
	try {
		result = client.getExecutor().snapshotResult(client.getContext(), resultDescriptor.getResultId(), getVisibleMainHeight());
	} catch (SqlExecutionException e) {
		close(e);
		return;
	}

	// stop retrieval if job is done
	if (result.getType() == TypedResult.ResultType.EOS) {
		stopRetrieval(false);
	}
	// update page
	else if (result.getType() == TypedResult.ResultType.PAYLOAD) {
		int newPageCount = result.getPayload();
		pageCount = newPageCount;
		if (page > newPageCount) {
			page = LAST_PAGE;
		}
		updatePage();
	}

	lastRetrieval = LocalTime.now();

	// reset view
	resetAllParts();
}
 
Example #18
Source File: CliTableResultView.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void updatePage() {
	// retrieve page
	final int retrievalPage = page == LAST_PAGE ? pageCount : page;
	final List<Row> rows;
	try {
		rows = client.getExecutor().retrieveResultPage(resultDescriptor.getResultId(), retrievalPage);
	} catch (SqlExecutionException e) {
		close(e);
		return;
	}

	// convert page
	final List<String[]> stringRows = rows
			.stream()
			.map(CliUtils::rowToString)
			.collect(Collectors.toList());

	// update results
	if (previousResultsPage == retrievalPage) {
		// only use the previous results if the current page number has not changed
		// this allows for updated results when the key space remains constant
		previousResults = results;
	} else {
		previousResults = null;
		previousResultsPage = retrievalPage;
	}

	results = stringRows;

	// check if selected row is still valid
	if (selectedRow != NO_ROW_SELECTED) {
		if (selectedRow >= results.size()) {
			selectedRow = NO_ROW_SELECTED;
		}
	}

	// reset view
	resetAllParts();
}
 
Example #19
Source File: CliClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean callInsertInto(SqlCommandCall cmdCall) {
	printInfo(CliStrings.MESSAGE_SUBMITTING_STATEMENT);

	try {
		final ProgramTargetDescriptor programTarget = executor.executeUpdate(context, cmdCall.operands[0]);
		terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_STATEMENT_SUBMITTED).toAnsi());
		terminal.writer().println(programTarget.toString());
		terminal.flush();
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return false;
	}
	return true;
}
 
Example #20
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private void registerView(ViewEntry viewEntry) {
	try {
		tableEnv.registerTable(viewEntry.getName(), tableEnv.sqlQuery(viewEntry.getQuery()));
	} catch (Exception e) {
		throw new SqlExecutionException(
			"Invalid view '" + viewEntry.getName() + "' with query:\n" + viewEntry.getQuery()
				+ "\nCause: " + e.getMessage());
	}
}
 
Example #21
Source File: MaterializedCollectBatchResult.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<Row> retrievePage(int page) {
	synchronized (resultLock) {
		if (page <= 0 || page > pageCount) {
			throw new SqlExecutionException("Invalid page '" + page + "'.");
		}
		return resultTable.subList(pageSize * (page - 1), Math.min(resultTable.size(), page * pageSize));
	}
}
 
Example #22
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listDatabases(SessionContext session) throws SqlExecutionException {
	final ExecutionContext<?> context = getOrCreateExecutionContext(session);

	final TableEnvironment tableEnv = context
		.createEnvironmentInstance()
		.getTableEnvironment();

	return context.wrapClassLoader(() -> Arrays.asList(tableEnv.listDatabases()));
}
 
Example #23
Source File: CliClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void callUseCatalog(SqlCommandCall cmdCall) {
	try {
		executor.useCatalog(context, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.flush();
}
 
Example #24
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listUserDefinedFunctions(SessionContext session) throws SqlExecutionException {
	final ExecutionContext<?> context = getOrCreateExecutionContext(session);
	final TableEnvironment tableEnv = context
		.createEnvironmentInstance()
		.getTableEnvironment();
	return context.wrapClassLoader(() -> Arrays.asList(tableEnv.listUserDefinedFunctions()));
}
 
Example #25
Source File: LocalExecutor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public TypedResult<Integer> snapshotResult(SessionContext session, String resultId, int pageSize) throws SqlExecutionException {
	final DynamicResult<?> result = resultStore.getResult(resultId);
	if (result == null) {
		throw new SqlExecutionException("Could not find a result with result identifier '" + resultId + "'.");
	}
	if (!result.isMaterialized()) {
		throw new SqlExecutionException("Invalid result retrieval mode.");
	}
	return ((MaterializedResult<?>) result).snapshot(pageSize);
}
 
Example #26
Source File: CliClientTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public ProgramTargetDescriptor executeUpdate(SessionContext session, String statement) throws SqlExecutionException {
	receivedContext = session;
	receivedStatement = statement;
	if (failExecution) {
		throw new SqlExecutionException("Fail execution.");
	}
	return new ProgramTargetDescriptor("testClusterId", "testJobId", "http://testcluster:1234");
}
 
Example #27
Source File: SqlClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private static void validateEnvironment(SessionContext context, Executor executor) {
	System.out.print("Validating current environment...");
	try {
		executor.validateSession(context);
		System.out.println("done.");
	} catch (SqlExecutionException e) {
		throw new SqlClientException(
			"The configured environment is invalid. Please check your environment files again.", e);
	}
}
 
Example #28
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
public EnvironmentInstance createEnvironmentInstance() {
	try {
		return wrapClassLoader(EnvironmentInstance::new);
	} catch (Throwable t) {
		// catch everything such that a wrong environment does not affect invocations
		throw new SqlExecutionException("Could not create environment instance.", t);
	}
}
 
Example #29
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private static CommandLine createCommandLine(DeploymentEntry deployment, Options commandLineOptions) {
	try {
		return deployment.getCommandLine(commandLineOptions);
	} catch (Exception e) {
		throw new SqlExecutionException("Invalid deployment options.", e);
	}
}
 
Example #30
Source File: CliClient.java    From flink with Apache License 2.0 5 votes vote down vote up
private void callUseDatabase(SqlCommandCall cmdCall) {
	try {
		executor.useDatabase(context, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.flush();
}