Java Code Examples for org.apache.flink.table.client.gateway.Executor#closeSession()

The following examples show how to use org.apache.flink.table.client.gateway.Executor#closeSession() . 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: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testListCatalogs() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> actualCatalogs = executor.listCatalogs(sessionId);

	final List<String> expectedCatalogs = Arrays.asList(
		"catalog1",
		"default_catalog",
		"simple-catalog");
	assertEquals(expectedCatalogs, actualCatalogs);

	executor.closeSession(sessionId);
}
 
Example 2
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
private void executeStreamQueryTable(
		Map<String, String> replaceVars,
		String query,
		List<String> expectedResults) throws Exception {

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	try {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(sessionId, query);

		assertTrue(desc.isMaterialized());

		final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId());

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 3
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testListTables() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> actualTables = executor.listTables(sessionId);

	final List<String> expectedTables = Arrays.asList(
		"TableNumber1",
		"TableNumber2",
		"TableSourceSink",
		"TestView1",
		"TestView2");
	assertEquals(expectedTables, actualTables);
	executor.closeSession(sessionId);
}
 
Example 4
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableSchema() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final TableSchema actualTableSchema = executor.getTableSchema(sessionId, "TableNumber2");

	final TableSchema expectedTableSchema = new TableSchema(
		new String[]{"IntegerField2", "StringField2", "TimestampField2"},
		new TypeInformation[]{Types.INT, Types.STRING, Types.SQL_TIMESTAMP});

	assertEquals(expectedTableSchema, actualTableSchema);
	executor.closeSession(sessionId);
}
 
Example 5
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompleteStatement() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> expectedTableHints = Arrays.asList(
		"default_catalog.default_database.TableNumber1",
		"default_catalog.default_database.TableNumber2",
		"default_catalog.default_database.TableSourceSink");
	assertEquals(expectedTableHints, executor.completeStatement(sessionId, "SELECT * FROM Ta", 16));

	final List<String> expectedClause = Collections.singletonList("WHERE");
	assertEquals(expectedClause, executor.completeStatement(sessionId, "SELECT * FROM TableNumber2 WH", 29));

	final List<String> expectedField = Arrays.asList("IntegerField1");
	assertEquals(expectedField, executor.completeStatement(sessionId, "SELECT * FROM TableNumber1 WHERE Inte", 37));
	executor.closeSession(sessionId);
}
 
Example 6
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testUseNonExistingCatalog() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	exception.expect(SqlExecutionException.class);
	executor.useCatalog(sessionId, "nonexistingcatalog");
	executor.closeSession(sessionId);
}
 
Example 7
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void ensureExceptionOnFaultySourceInBatch() throws Exception {
	final String missingFileName = "missing-source";

	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", missingFileName);
	replaceVars.put("$VAR_EXECUTION_TYPE", "batch");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "");
	replaceVars.put("$VAR_MAX_ROWS", "100");
	replaceVars.put("$VAR_RESTART_STRATEGY_TYPE", "none");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	Optional<Throwable> throwableWithMessage = Optional.empty();
	try {
		final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT * FROM TestView1");
		retrieveTableResult(executor, sessionId, desc.getResultId());
	} catch (SqlExecutionException e) {
		throwableWithMessage = findMissingFileException(e, missingFileName);
	} finally {
		executor.closeSession(sessionId);
	}
	assertTrue(throwableWithMessage.isPresent());
}
 
Example 8
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void ensureExceptionOnFaultySourceInStreamingTableMode() throws Exception {
	final String missingFileName = "missing-source";

	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", missingFileName);
	replaceVars.put("$VAR_EXECUTION_TYPE", "streaming");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "1");
	replaceVars.put("$VAR_RESTART_STRATEGY_TYPE", "none");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	Optional<Throwable> throwableWithMessage = Optional.empty();
	try {
		final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT * FROM TestView1");
		retrieveTableResult(executor, sessionId, desc.getResultId());
	} catch (SqlExecutionException e) {
		throwableWithMessage = findMissingFileException(e, missingFileName);
	} finally {
		executor.closeSession(sessionId);
	}
	assertTrue(throwableWithMessage.isPresent());
}
 
Example 9
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void testBatchQueryExecutionMultipleTimes() throws Exception {
	final URL url = getClass().getClassLoader().getResource("test-data.csv");
	Objects.requireNonNull(url);
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
	replaceVars.put("$VAR_EXECUTION_TYPE", "batch");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "");
	replaceVars.put("$VAR_MAX_ROWS", "100");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("47");
	expectedResults.add("27");
	expectedResults.add("37");
	expectedResults.add("37");
	expectedResults.add("47");
	expectedResults.add("57");

	try {
		for (int i = 0; i < 3; i++) {
			final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT * FROM TestView1");

			assertTrue(desc.isMaterialized());

			final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId());

			TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 10
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 90_000L)
public void testBatchQueryExecution() throws Exception {
	final URL url = getClass().getClassLoader().getResource("test-data.csv");
	Objects.requireNonNull(url);
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
	replaceVars.put("$VAR_EXECUTION_TYPE", "batch");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	replaceVars.put("$VAR_UPDATE_MODE", "");
	replaceVars.put("$VAR_MAX_ROWS", "100");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	try {
		final ResultDescriptor desc = executor.executeQuery(sessionId, "SELECT *, 'ABC' FROM TestView1");

		assertTrue(desc.isMaterialized());

		final List<String> actualResults = retrieveTableResult(executor, sessionId, desc.getResultId());

		final List<String> expectedResults = new ArrayList<>();
		expectedResults.add("47,ABC");
		expectedResults.add("27,ABC");
		expectedResults.add("37,ABC");
		expectedResults.add("37,ABC");
		expectedResults.add("47,ABC");
		expectedResults.add("57,ABC");

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 11
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlterFunction() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.setSessionProperty(sessionId, "execution.type", "batch");
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF NOT EXISTS], func_name
		final String createTemplate = "create %s function %s %s \n"
				+ "as 'org.apache.flink.table.client.gateway.local.LocalExecutorITCase$TestScalaFunction' LANGUAGE JAVA";
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF EXISTS], func_name, func_class
		final String alterTemplate = "alter %s function %s %s AS %s";
		// Test alter function.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(alterTemplate, "", "IF EXISTS", "`default`.func1", "'newClass'"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));

		// Test alter non temporary function with TEMPORARY keyword.
		try {
			executor.executeSql(sessionId, String.format(alterTemplate, "TEMPORARY", "IF EXISTS", "`default`.func2", "'func3'"));
			fail("unexpected exception");
		} catch (Exception var1) {
			assertThat(var1.getCause().getMessage(), is("Alter temporary catalog function is not supported"));
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 12
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testListUserDefinedFunctions() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> actualTables = executor.listUserDefinedFunctions(sessionId);

	final List<String> expectedTables = Arrays.asList("aggregateudf", "tableudf", "scalarudf");
	assertEquals(expectedTables, actualTables);

	executor.closeSession(sessionId);
}
 
Example 13
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test @Ignore // TODO: reopen when FLINK-15075 was fixed.
public void testCreateTableWithComputedColumn() throws Exception {
	Assume.assumeTrue(planner.equals("blink"));
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", "file:///fakePath1");
	replaceVars.put("$VAR_SOURCE_PATH2", "file:///fakePath2");
	replaceVars.put("$VAR_EXECUTION_TYPE", "batch");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "100");
	replaceVars.put("$VAR_RESULT_MODE", "table");
	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final String ddlTemplate = "create table %s(\n" +
			"  a int,\n" +
			"  b bigint,\n" +
			"  c as a + 1\n" +
			") with (\n" +
			"  'connector.type'='filesystem',\n" +
			"  'format.type'='csv',\n" +
			"  'connector.path'='xxx'\n" +
			")\n";
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 14
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateFunction() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF NOT EXISTS], func_name
	final String ddlTemplate = "create %s function %s %s \n"
			+ "as 'org.apache.flink.table.client.gateway.local.LocalExecutorITCase$TestScalaFunction' LANGUAGE JAVA";
	try {
		// Test create table with simple name.
		executor.useCatalog(sessionId, "catalog1");
		executor.executeSql(sessionId, String.format(ddlTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(ddlTemplate, "TEMPORARY", "IF NOT EXISTS", "func2"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2"));

		// Test create function with full qualified name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "", "", "`simple-catalog`.`default_database`.func3"));
		executor.createTable(sessionId, String.format(ddlTemplate, "TEMPORARY", "IF NOT EXISTS", "`simple-catalog`.`default_database`.func4"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2"));
		executor.useCatalog(sessionId, "simple-catalog");
		assertThat(executor.listFunctions(sessionId), hasItems("func3", "func4"));

		// Test create function with db and table name.
		executor.useCatalog(sessionId, "catalog1");
		executor.createTable(sessionId, String.format(ddlTemplate, "TEMPORARY", "", "`default`.func5"));
		executor.createTable(sessionId, String.format(ddlTemplate, "TEMPORARY", "", "`default`.func6"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2", "func5", "func6"));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 15
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateDatabase() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	executor.executeUpdate(sessionId, "create database db1");

	final List<String> actualDatabases = executor.listDatabases(sessionId);
	final List<String> expectedDatabases = Arrays.asList("default_database", "db1");
	assertEquals(expectedDatabases, actualDatabases);

	executor.closeSession(sessionId);
}
 
Example 16
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTableWithPropertiesChanged() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.setSessionProperty(sessionId, "execution.type", "batch");
		final String ddlTemplate = "create table %s(\n" +
				"  a int,\n" +
				"  b bigint,\n" +
				"  c varchar\n" +
				") with (\n" +
				"  'connector.type'='filesystem',\n" +
				"  'format.type'='csv',\n" +
				"  'connector.path'='xxx',\n" +
				"  'update-mode'='append'\n" +
				")\n";
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		// Change the session property to trigger `new ExecutionContext`.
		executor.setSessionProperty(sessionId, "execution.restart-strategy.failure-rate-interval", "12345");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));

		// Reset the session properties.
		executor.resetSessionProperties(sessionId);
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable3"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2", "MyTable3"), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 17
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 90_000L)
public void testStreamQueryExecutionChangelog() throws Exception {
	final URL url = getClass().getClassLoader().getResource("test-data.csv");
	Objects.requireNonNull(url);
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
	replaceVars.put("$VAR_EXECUTION_TYPE", "streaming");
	replaceVars.put("$VAR_RESULT_MODE", "changelog");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "100");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	try {
		// start job and retrieval
		final ResultDescriptor desc = executor.executeQuery(
			sessionId,
			"SELECT scalarUDF(IntegerField1), StringField1, 'ABC' FROM TableNumber1");

		assertFalse(desc.isMaterialized());

		final List<String> actualResults =
			retrieveChangelogResult(executor, sessionId, desc.getResultId());

		final List<String> expectedResults = new ArrayList<>();
		expectedResults.add("(true,47,Hello World,ABC)");
		expectedResults.add("(true,27,Hello World,ABC)");
		expectedResults.add("(true,37,Hello World,ABC)");
		expectedResults.add("(true,37,Hello World,ABC)");
		expectedResults.add("(true,47,Hello World,ABC)");
		expectedResults.add("(true,57,Hello World!!!!,ABC)");

		TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 18
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 90_000L)
public void testStreamQueryExecutionChangelogMultipleTimes() throws Exception {
	final URL url = getClass().getClassLoader().getResource("test-data.csv");
	Objects.requireNonNull(url);
	final Map<String, String> replaceVars = new HashMap<>();
	replaceVars.put("$VAR_PLANNER", planner);
	replaceVars.put("$VAR_SOURCE_PATH1", url.getPath());
	replaceVars.put("$VAR_EXECUTION_TYPE", "streaming");
	replaceVars.put("$VAR_RESULT_MODE", "changelog");
	replaceVars.put("$VAR_UPDATE_MODE", "update-mode: append");
	replaceVars.put("$VAR_MAX_ROWS", "100");

	final Executor executor = createModifiedExecutor(clusterClient, replaceVars);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	assertEquals("test-session", sessionId);

	final List<String> expectedResults = new ArrayList<>();
	expectedResults.add("(true,47,Hello World)");
	expectedResults.add("(true,27,Hello World)");
	expectedResults.add("(true,37,Hello World)");
	expectedResults.add("(true,37,Hello World)");
	expectedResults.add("(true,47,Hello World)");
	expectedResults.add("(true,57,Hello World!!!!)");

	try {
		for (int i = 0; i < 3; i++) {
			// start job and retrieval
			final ResultDescriptor desc = executor.executeQuery(
					sessionId,
					"SELECT scalarUDF(IntegerField1), StringField1 FROM TableNumber1");

			assertFalse(desc.isMaterialized());

			final List<String> actualResults =
					retrieveChangelogResult(executor, sessionId, desc.getResultId());

			TestBaseUtils.compareResultCollections(expectedResults, actualResults, Comparator.naturalOrder());
		}
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 19
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testDropTable() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.setSessionProperty(sessionId, "execution.type", "batch");
		final String ddlTemplate = "create table %s(\n" +
				"  a int,\n" +
				"  b bigint,\n" +
				"  c varchar\n" +
				") with (\n" +
				"  'connector.type'='filesystem',\n" +
				"  'format.type'='csv',\n" +
				"  'connector.path'='xxx',\n" +
				"  'update-mode'='append'\n" +
				")\n";
		// Test drop table.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table if exists.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE IF EXISTS MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table with full qualified name.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE catalog1.`default`.MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table with db and table name.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE `default`.MyTable1");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table that does not exist.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE IF EXISTS catalog2.`default`.MyTable1");
		assertEquals(Collections.singletonList("MyTable1"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE `default`.MyTable1");

		// Test drop table with properties changed.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		// Change the session property to trigger `new ExecutionContext`.
		executor.setSessionProperty(sessionId, "execution.restart-strategy.failure-rate-interval", "12345");
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE MyTable1");
		executor.dropTable(sessionId, "DROP TABLE MyTable2");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));

		// Test drop table with properties reset.
		// Reset the session properties.
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable1"));
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable2"));
		executor.resetSessionProperties(sessionId);
		executor.createTable(sessionId, String.format(ddlTemplate, "MyTable3"));
		assertEquals(Arrays.asList("MyTable1", "MyTable2", "MyTable3"), executor.listTables(sessionId));
		executor.dropTable(sessionId, "DROP TABLE MyTable1");
		executor.dropTable(sessionId, "DROP TABLE MyTable2");
		executor.dropTable(sessionId, "DROP TABLE MyTable3");
		assertEquals(Collections.emptyList(), executor.listTables(sessionId));
	} finally {
		executor.closeSession(sessionId);
	}
}
 
Example 20
Source File: LocalExecutorITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testDropFunction() throws Exception {
	final Executor executor = createDefaultExecutor(clusterClient);
	final SessionContext session = new SessionContext("test-session", new Environment());
	String sessionId = executor.openSession(session);
	try {
		executor.useCatalog(sessionId, "catalog1");
		executor.setSessionProperty(sessionId, "execution.type", "batch");
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF NOT EXISTS], func_name
		final String createTemplate = "create %s function %s %s \n"
				+ "as 'org.apache.flink.table.client.gateway.local.LocalExecutorITCase$TestScalaFunction' LANGUAGE JAVA";
		// arguments: [TEMPORARY|TEMPORARY SYSTEM], [IF EXISTS], func_name
		final String dropTemplate = "drop %s function %s %s";
		// Test drop function.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function if exists.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function with full qualified name.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "catalog1.`default`.func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function with db and function name.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "`default`.func1"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1")));

		// Test drop function that does not exist.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		try {
			executor.executeSql(sessionId, String.format(dropTemplate, "", "IF EXISTS", "catalog2.`default`.func1"));
			fail("unexpected");
		} catch (Exception e) {
			assertThat(e.getCause().getMessage(), is("Catalog catalog2 does not exist"));
		}
		assertThat(executor.listFunctions(sessionId), hasItems("func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "`default`.func1"));

		// Test drop function with properties changed.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		// Change the session property to trigger `new ExecutionContext`.
		executor.setSessionProperty(sessionId, "execution.restart-strategy.failure-rate-interval", "12345");
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func2"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func2"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1", "func2")));

		// Test drop function with properties reset.
		// Reset the session properties.
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func1"));
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func2"));
		executor.resetSessionProperties(sessionId);
		executor.executeSql(sessionId, String.format(createTemplate, "", "", "func3"));
		assertThat(executor.listFunctions(sessionId), hasItems("func1", "func2", "func3"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func1"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func2"));
		executor.executeSql(sessionId, String.format(dropTemplate, "", "", "func3"));
		assertThat(executor.listFunctions(sessionId), not(hasItems("func1", "func2", "func3")));
	} finally {
		executor.closeSession(sessionId);
	}
}