Java Code Examples for org.apache.flink.table.client.cli.SqlCommandParser.SqlCommandCall

The following examples show how to use org.apache.flink.table.client.cli.SqlCommandParser.SqlCommandCall. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Source File: CliClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Submits a SQL update statement and prints status information and/or errors on the terminal.
 *
 * @param statement SQL update statement
 * @return flag to indicate if the submission was successful or not
 */
public boolean submitUpdate(String statement) {
	terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_WILL_EXECUTE).toAnsi());
	terminal.writer().println(new AttributedString(statement).toString());
	terminal.flush();

	final Optional<SqlCommandCall> parsedStatement = parseCommand(statement);
	// only support INSERT INTO
	return parsedStatement.map(cmdCall -> {
		switch (cmdCall.command) {
			case INSERT_INTO:
				return callInsertInto(cmdCall);
			default:
				printError(CliStrings.MESSAGE_UNSUPPORTED_SQL);
				return false;
		}
	}).orElse(false);
}
 
Example 2
Source Project: Flink-CEPplus   Source File: CliClient.java    License: 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 Project: Flink-CEPplus   Source File: CliClient.java    License: Apache License 2.0 6 votes vote down vote up
private void callDropView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final ViewEntry view = context.getViews().get(name);

	if (view == null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_NOT_FOUND);
		return;
	}

	try {
		// perform and validate change
		context.removeView(name);
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_REMOVED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.addView(view);
		printExecutionException(CliStrings.MESSAGE_VIEW_NOT_REMOVED, e);
	}
}
 
Example 4
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Submits a SQL update statement and prints status information and/or errors on the terminal.
 *
 * @param statement SQL update statement
 * @return flag to indicate if the submission was successful or not
 */
public boolean submitUpdate(String statement) {
	terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_WILL_EXECUTE).toAnsi());
	terminal.writer().println(new AttributedString(statement).toString());
	terminal.flush();

	final Optional<SqlCommandCall> parsedStatement = parseCommand(statement);
	// only support INSERT INTO
	return parsedStatement.map(cmdCall -> {
		switch (cmdCall.command) {
			case INSERT_INTO:
				return callInsertInto(cmdCall);
			default:
				printError(CliStrings.MESSAGE_UNSUPPORTED_SQL);
				return false;
		}
	}).orElse(false);
}
 
Example 5
Source Project: flink   Source File: CliClient.java    License: 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 Project: flink   Source File: CliClient.java    License: Apache License 2.0 6 votes vote down vote up
private void callDropView(SqlCommandCall cmdCall) {
	final String name = cmdCall.operands[0];
	final ViewEntry view = context.getViews().get(name);

	if (view == null) {
		printExecutionError(CliStrings.MESSAGE_VIEW_NOT_FOUND);
		return;
	}

	try {
		// perform and validate change
		context.removeView(name);
		executor.validateSession(context);
		printInfo(CliStrings.MESSAGE_VIEW_REMOVED);
	} catch (SqlExecutionException e) {
		// rollback change
		context.addView(view);
		printExecutionException(CliStrings.MESSAGE_VIEW_NOT_REMOVED, e);
	}
}
 
Example 7
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Submits a SQL update statement and prints status information and/or errors on the terminal.
 *
 * @param statement SQL update statement
 * @return flag to indicate if the submission was successful or not
 */
public boolean submitUpdate(String statement) {
	terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_WILL_EXECUTE).toAnsi());
	terminal.writer().println(new AttributedString(statement).toString());
	terminal.flush();

	final Optional<SqlCommandCall> parsedStatement = parseCommand(statement);
	// only support INSERT INTO/OVERWRITE
	return parsedStatement.map(cmdCall -> {
		switch (cmdCall.command) {
			case INSERT_INTO:
			case INSERT_OVERWRITE:
				return callInsert(cmdCall);
			default:
				printError(CliStrings.MESSAGE_UNSUPPORTED_SQL);
				return false;
		}
	}).orElse(false);
}
 
Example 8
Source Project: flink   Source File: SqlCommandParserTest.java    License: Apache License 2.0 6 votes vote down vote up
private void runTestItem(TestItem item) {
	Tuple2<Boolean, SqlCommandCall> checkFlagAndActualCall = parseSqlAndCheckException(item);
	if (!checkFlagAndActualCall.f0) {
		return;
	}
	SqlCommandCall actualCall = checkFlagAndActualCall.f1;
	assertNotNull(item.expectedCmd);
	assertEquals("test statement: " + item.sql,
			new SqlCommandCall(item.expectedCmd, item.expectedOperands), actualCall);

	String stmtWithComment = "-- comments \n " + item.sql;
	try {
		actualCall = SqlCommandParser.parse(parser, stmtWithComment);
	} catch (SqlExecutionException e) {
		if (!item.cannotParseComment) {
			fail("test statement: " + item.sql);
		}
		return;
	}
	assertEquals(item.expectedCmd, actualCall.command);
}
 
Example 9
Source Project: Flink-CEPplus   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private Optional<SqlCommandCall> parseCommand(String line) {
	final Optional<SqlCommandCall> parsedLine = SqlCommandParser.parse(line);
	if (!parsedLine.isPresent()) {
		printError(CliStrings.MESSAGE_UNKNOWN_SQL);
	}
	return parsedLine;
}
 
Example 10
Source Project: Flink-CEPplus   Source File: CliClient.java    License: 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 11
Source Project: Flink-CEPplus   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callDescribe(SqlCommandCall cmdCall) {
	final TableSchema schema;
	try {
		schema = executor.getTableSchema(context, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.writer().println(schema.toString());
	terminal.flush();
}
 
Example 12
Source Project: Flink-CEPplus   Source File: CliClient.java    License: 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 13
Source Project: Flink-CEPplus   Source File: CliClient.java    License: 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 14
Source Project: Flink-CEPplus   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callSource(SqlCommandCall cmdCall) {
	final String pathString = cmdCall.operands[0];

	// load file
	final String stmt;
	try {
		final Path path = Paths.get(pathString);
		byte[] encoded = Files.readAllBytes(path);
		stmt = new String(encoded, Charset.defaultCharset());
	} catch (IOException e) {
		printExecutionException(e);
		return;
	}

	// limit the output a bit
	if (stmt.length() > SOURCE_MAX_SIZE) {
		printExecutionError(CliStrings.MESSAGE_MAX_SIZE_EXCEEDED);
		return;
	}

	terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_WILL_EXECUTE).toAnsi());
	terminal.writer().println(new AttributedString(stmt).toString());
	terminal.flush();

	// try to run it
	final Optional<SqlCommandCall> call = parseCommand(stmt);
	call.ifPresent(this::callCommand);
}
 
Example 15
Source Project: Flink-CEPplus   Source File: SqlCommandParserTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testValidSqlCommand(String stmt, SqlCommandCall expectedCall) {
	final Optional<SqlCommandCall> actualCall = SqlCommandParser.parse(stmt);
	if (!actualCall.isPresent()) {
		fail();
	}
	assertEquals(expectedCall, actualCall.get());
}
 
Example 16
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private Optional<SqlCommandCall> parseCommand(String line) {
	final Optional<SqlCommandCall> parsedLine = SqlCommandParser.parse(line);
	if (!parsedLine.isPresent()) {
		printError(CliStrings.MESSAGE_UNKNOWN_SQL);
	}
	return parsedLine;
}
 
Example 17
Source Project: flink   Source File: CliClient.java    License: 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 18
Source Project: flink   Source File: CliClient.java    License: 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 19
Source Project: flink   Source File: CliClient.java    License: 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();
}
 
Example 20
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callDescribe(SqlCommandCall cmdCall) {
	final TableSchema schema;
	try {
		schema = executor.getTableSchema(context, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.writer().println(schema.toString());
	terminal.flush();
}
 
Example 21
Source Project: flink   Source File: CliClient.java    License: 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 22
Source Project: flink   Source File: CliClient.java    License: 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 23
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callSource(SqlCommandCall cmdCall) {
	final String pathString = cmdCall.operands[0];

	// load file
	final String stmt;
	try {
		final Path path = Paths.get(pathString);
		byte[] encoded = Files.readAllBytes(path);
		stmt = new String(encoded, Charset.defaultCharset());
	} catch (IOException e) {
		printExecutionException(e);
		return;
	}

	// limit the output a bit
	if (stmt.length() > SOURCE_MAX_SIZE) {
		printExecutionError(CliStrings.MESSAGE_MAX_SIZE_EXCEEDED);
		return;
	}

	terminal.writer().println(CliStrings.messageInfo(CliStrings.MESSAGE_WILL_EXECUTE).toAnsi());
	terminal.writer().println(new AttributedString(stmt).toString());
	terminal.flush();

	// try to run it
	final Optional<SqlCommandCall> call = parseCommand(stmt);
	call.ifPresent(this::callCommand);
}
 
Example 24
Source Project: flink   Source File: SqlCommandParserTest.java    License: Apache License 2.0 5 votes vote down vote up
private void testValidSqlCommand(String stmt, SqlCommandCall expectedCall) {
	final Optional<SqlCommandCall> actualCall = SqlCommandParser.parse(stmt);
	if (!actualCall.isPresent()) {
		fail();
	}
	assertEquals(expectedCall, actualCall.get());
}
 
Example 25
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private Optional<SqlCommandCall> parseCommand(String line) {
	final SqlCommandCall parsedLine;
	try {
		parsedLine = SqlCommandParser.parse(executor.getSqlParser(sessionId), line);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return Optional.empty();
	}
	return Optional.of(parsedLine);
}
 
Example 26
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callUseCatalog(SqlCommandCall cmdCall) {
	try {
		executor.useCatalog(sessionId, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.flush();
}
 
Example 27
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callUseDatabase(SqlCommandCall cmdCall) {
	try {
		executor.useDatabase(sessionId, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.flush();
}
 
Example 28
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callDescribe(SqlCommandCall cmdCall) {
	final TableSchema schema;
	try {
		schema = executor.getTableSchema(sessionId, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.writer().println(schema.toString());
	terminal.flush();
}
 
Example 29
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private void callExplain(SqlCommandCall cmdCall) {
	final String explanation;
	try {
		TableResult tableResult = executor.executeSql(sessionId, cmdCall.operands[0]);
		explanation = tableResult.collect().next().getField(0).toString();
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.writer().println(explanation);
	terminal.flush();
}
 
Example 30
Source Project: flink   Source File: CliClient.java    License: Apache License 2.0 5 votes vote down vote up
private boolean callInsert(SqlCommandCall cmdCall) {
	printInfo(CliStrings.MESSAGE_SUBMITTING_STATEMENT);

	try {
		final ProgramTargetDescriptor programTarget = executor.executeUpdate(sessionId, 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;
}