Skip to content

Commit

Permalink
Add support for ALTER VIEW [ IF EXISTS ] RENAME TO statement in Memor…
Browse files Browse the repository at this point in the history
…y connector
  • Loading branch information
dmariamgeorge committed Nov 18, 2024
1 parent 1fe8b94 commit c0e1953
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
9 changes: 9 additions & 0 deletions presto-docs/src/main/sphinx/connector/memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ To delete an existing table:
.. note:: After using ``DROP TABLE``, memory is not released immediately. It is released after the next write access to the memory connector.

ALTER VIEW
^^^^^^^^^^

Alter view operations to alter the name of an existing view to a new name is supported in the Memory connector.

.. code-block:: sql
ALTER VIEW memory.default.nation RENAME TO memory.default.new_nation;
Memory Connector Limitations
----------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ else if (views.putIfAbsent(viewName, viewData) != null) {
}
}

@Override
public synchronized void renameView(ConnectorSession session, SchemaTableName viewName, SchemaTableName newViewName)
{
checkSchemaExists(newViewName.getSchemaName());
if (tableIds.containsKey(newViewName)) {
throw new PrestoException(ALREADY_EXISTS, "Table already exists: " + newViewName);
}

if (views.containsKey(newViewName)) {
throw new PrestoException(ALREADY_EXISTS, "View already exists: " + newViewName);
}

views.put(newViewName, views.remove(viewName));
}

@Override
public synchronized void dropView(ConnectorSession session, SchemaTableName viewName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ public void testViews()
test2,
ImmutableList.of(new ColumnMetadata("a", BIGINT)));

SchemaTableName test3 = new SchemaTableName("test", "test_view3");

// create schema
metadata.createSchema(SESSION, "test", ImmutableMap.of());

Expand Down Expand Up @@ -265,8 +267,14 @@ public void testViews()
views = metadata.getViews(SESSION, new SchemaTablePrefix("test"));
assertEquals(views.keySet(), ImmutableSet.of(test2));

// rename second view
metadata.renameView(SESSION, test2, test3);

Map<?, ?> result = metadata.getViews(SESSION, new SchemaTablePrefix("test"));
assertTrue(result.containsKey(test3));

// drop second view
metadata.dropView(SESSION, test2);
metadata.dropView(SESSION, test3);

views = metadata.getViews(SESSION, new SchemaTablePrefix("test"));
assertTrue(views.isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,31 @@ public void testViews()
assertQueryFails("DROP VIEW test_view", "line 1:1: View 'memory.default.test_view' does not exist");
}

@Test
public void testRenameView()
{
@Language("SQL") String query = "SELECT orderkey, orderstatus, totalprice / 2 half FROM orders";

assertUpdate("CREATE VIEW test_view_to_be_renamed AS " + query);
assertQueryFails("ALTER VIEW test_view_to_be_renamed RENAME TO memory.test_schema_not_exist.test_view_renamed", "Schema test_schema_not_exist not found");
assertUpdate("ALTER VIEW test_view_to_be_renamed RENAME TO test_view_renamed");
assertQuery("SELECT * FROM test_view_renamed", query);

assertUpdate("CREATE SCHEMA test_different_schema");
assertUpdate("ALTER VIEW test_view_renamed RENAME TO test_different_schema.test_view_renamed");
assertQuery("SELECT * FROM test_different_schema.test_view_renamed", query);

assertUpdate("DROP VIEW test_different_schema.test_view_renamed");
assertUpdate("DROP SCHEMA test_different_schema");
}

@Test
public void testRenameViewIfNotExists()
{
assertQueryFails("ALTER VIEW test_rename_view_not_exist RENAME TO test_renamed_view_not_exist", "line 1:1: View 'memory.default.test_rename_view_not_exist' does not exist");
assertQuerySucceeds("ALTER VIEW IF EXISTS test_rename_view_not_exist RENAME TO test_renamed_view_not_exist");
}

private List<QualifiedObjectName> listMemoryTables()
{
return getQueryRunner().listTables(getSession(), "memory", "default");
Expand Down

0 comments on commit c0e1953

Please sign in to comment.