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

The following examples show how to use org.apache.flink.table.client.cli.SqlCommandParser.SqlCommandCall. 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: CliClient.java    From Flink-CEPplus with 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 File: SqlCommandParserTest.java    From flink with 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 #3
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 #4
Source File: CliClient.java    From flink with 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 #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: CliClient.java    From Flink-CEPplus with 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 File: CliClient.java    From flink with 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 #8
Source File: CliClient.java    From flink with 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 #9
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(sessionId, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.flush();
}
 
Example #10
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 #11
Source File: CliClient.java    From flink with 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 #12
Source File: SqlCommandParserTest.java    From flink with 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 #13
Source File: CliClient.java    From flink with 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 #14
Source File: CliClient.java    From flink with 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 #15
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(sessionId, cmdCall.operands[0]);
	} catch (SqlExecutionException e) {
		printExecutionException(e);
		return;
	}
	terminal.flush();
}
 
Example #16
Source File: CliClient.java    From flink with 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 #17
Source File: CliClient.java    From flink with 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 #18
Source File: CliClient.java    From flink with 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;
}
 
Example #19
Source File: CliClient.java    From flink with 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 #20
Source File: SqlCommandParserTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private Tuple2<Boolean, SqlCommandCall> parseSqlAndCheckException(TestItem item) {
	SqlCommandCall call = null;
	Throwable actualException = null;
	try {
		call = SqlCommandParser.parse(parser, item.sql);
	} catch (Throwable e) {
		actualException = e;
	}

	if (item.expectedException == null && actualException == null) {
		return Tuple2.of(true, call);
	} else if (item.expectedException == null) {
		actualException.printStackTrace();
		fail("Failed to run sql: " + item.sql);
	} else if (actualException == null) {
		fail("the excepted exception: '" + item.expectedException + "' does not occur.\n" +
				"test statement: " + item.sql);
	} else {
		assertTrue(actualException.getClass().isAssignableFrom(item.expectedException));
		boolean hasExpectedExceptionMsg = false;
		while (actualException != null) {
			if (actualException.getMessage().contains(item.expectedExceptionMsg)) {
				hasExpectedExceptionMsg = true;
				break;
			}
			actualException = actualException.getCause();
		}
		if (!hasExpectedExceptionMsg) {
			fail("the excepted exception message: '" + item.expectedExceptionMsg + "' does not occur.\n" +
					"test statement: " + item.sql);
		}
	}
	return Tuple2.of(false, null);
}
 
Example #21
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();
}
 
Example #22
Source File: CliClient.java    From Flink-CEPplus with 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 #23
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 #24
Source File: CliClient.java    From Flink-CEPplus with 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 #25
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 #26
Source File: CliClient.java    From Flink-CEPplus 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 #27
Source File: CliClient.java    From Flink-CEPplus with 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 #28
Source File: SqlCommandParserTest.java    From Flink-CEPplus with 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 #29
Source File: CliClient.java    From flink with 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 #30
Source File: CliClient.java    From flink 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();
}