You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ja...@apache.org on 2020/06/04 09:42:33 UTC

[flink] branch release-1.11 updated: [FLINK-18055][sql-client] Fix catalog/database does not exist in sql client

This is an automated email from the ASF dual-hosted git repository.

jark pushed a commit to branch release-1.11
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.11 by this push:
     new f391b0d  [FLINK-18055][sql-client] Fix catalog/database does not exist in sql client
f391b0d is described below

commit f391b0d433ed16f33018837f42c383992953a7bc
Author: godfrey he <go...@163.com>
AuthorDate: Thu Jun 4 17:25:36 2020 +0800

    [FLINK-18055][sql-client] Fix catalog/database does not exist in sql client
    
    This closes #12431
---
 .../flink/table/client/cli/SqlCommandParser.java   |  5 +--
 .../flink/table/client/cli/CliClientTest.java      | 51 ++++++++++++++++++++++
 .../table/client/cli/SqlCommandParserTest.java     |  4 +-
 3 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/SqlCommandParser.java b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/SqlCommandParser.java
index 5b0a88c..715f021 100644
--- a/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/SqlCommandParser.java
+++ b/flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/SqlCommandParser.java
@@ -132,11 +132,10 @@ public final class SqlCommandParser {
 			cmd = SqlCommand.DROP_CATALOG;
 		} else if (operation instanceof UseCatalogOperation) {
 			cmd = SqlCommand.USE_CATALOG;
-			operands = new String[] { String.format("`%s`", ((UseCatalogOperation) operation).getCatalogName()) };
+			operands = new String[] { ((UseCatalogOperation) operation).getCatalogName() };
 		} else if (operation instanceof UseDatabaseOperation) {
 			cmd = SqlCommand.USE;
-			UseDatabaseOperation op = ((UseDatabaseOperation) operation);
-			operands = new String[] { String.format("`%s`.`%s`", op.getCatalogName(), op.getDatabaseName()) };
+			operands = new String[] { ((UseDatabaseOperation) operation).getDatabaseName() };
 		} else if (operation instanceof ShowCatalogsOperation) {
 			cmd = SqlCommand.SHOW_CATALOGS;
 			operands = new String[0];
diff --git a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientTest.java b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientTest.java
index 8a7ca75..19d91ab 100644
--- a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientTest.java
+++ b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/CliClientTest.java
@@ -45,6 +45,7 @@ import org.jline.terminal.impl.DumbTerminal;
 import org.junit.Test;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -167,6 +168,35 @@ public class CliClientTest extends TestLogger {
 	}
 
 	@Test
+	public void testUseCatalog() throws Exception {
+		TestingExecutor executor = new TestingExecutorBuilder()
+				.setUseCatalogConsumer((ignored1, catalogName) -> {
+					if (!catalogName.equals("cat")) {
+						throw new SqlExecutionException("unexpected catalog name: " + catalogName);
+					}
+				})
+				.build();
+
+		String output = testExecuteSql(executor, "use catalog cat;");
+		assertThat(executor.getNumUseCatalogCalls(), is(1));
+		assertFalse(output.contains("unexpected catalog name"));
+	}
+
+	@Test
+	public void testUseDatabase() throws Exception {
+		TestingExecutor executor = new TestingExecutorBuilder()
+				.setUseDatabaseConsumer((ignored1, databaseName) -> {
+					if (!databaseName.equals("db")) {
+						throw new SqlExecutionException("unexpected database name: " + databaseName);
+					}
+				})
+				.build();
+		String output = testExecuteSql(executor, "use db;");
+		assertThat(executor.getNumUseDatabaseCalls(), is(1));
+		assertFalse(output.contains("unexpected database name"));
+	}
+
+	@Test
 	public void testHistoryFile() throws Exception {
 		final SessionContext context = new SessionContext("test-session", new Environment());
 		final MockExecutor mockExecutor = new MockExecutor();
@@ -198,6 +228,27 @@ public class CliClientTest extends TestLogger {
 
 	// --------------------------------------------------------------------------------------------
 
+	/**
+	 * execute a sql statement and return the terminal output as string.
+	 */
+	private String testExecuteSql(TestingExecutor executor, String sql) throws IOException {
+		InputStream inputStream = new ByteArrayInputStream((sql + "\n").getBytes());
+		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
+		CliClient cliClient = null;
+		SessionContext sessionContext = new SessionContext("test-session", new Environment());
+		String sessionId = executor.openSession(sessionContext);
+
+		try (Terminal terminal = new DumbTerminal(inputStream, outputStream)) {
+			cliClient = new CliClient(terminal, sessionId, executor, File.createTempFile("history", "tmp").toPath());
+			cliClient.open();
+			return new String(outputStream.toByteArray());
+		} finally {
+			if (cliClient != null) {
+				cliClient.close();
+			}
+		}
+	}
+
 	private void verifyUpdateSubmission(String statement, boolean failExecution, boolean testFailure) throws Exception {
 		final SessionContext context = new SessionContext("test-session", new Environment());
 
diff --git a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/SqlCommandParserTest.java b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/SqlCommandParserTest.java
index c3e9250..9b7c2d0 100644
--- a/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/SqlCommandParserTest.java
+++ b/flink-table/flink-sql-client/src/test/java/org/apache/flink/table/client/cli/SqlCommandParserTest.java
@@ -134,8 +134,8 @@ public class SqlCommandParserTest {
 				// drop catalog xx
 				TestItem.validSql("drop CATALOG c1", SqlCommand.DROP_CATALOG, "drop CATALOG c1"),
 				// use xx
-				TestItem.validSql("USE CATALOG catalog1;", SqlCommand.USE_CATALOG, "`catalog1`"),
-				TestItem.validSql("use `default`;", SqlCommand.USE, "`default_catalog`.`default`"),
+				TestItem.validSql("USE CATALOG catalog1;", SqlCommand.USE_CATALOG, "catalog1"),
+				TestItem.validSql("use `default`;", SqlCommand.USE, "default"),
 				TestItem.invalidSql("use catalog "), // no catalog name
 				// create database xx
 				TestItem.validSql("create database db1;", SqlCommand.CREATE_DATABASE, "create database db1"),