From b524a991363c707bf5bf6705c8e661fc4166e2d2 Mon Sep 17 00:00:00 2001 From: Deepa-George Date: Wed, 13 Nov 2024 14:59:56 +0530 Subject: [PATCH] Add support for ALTER VIEW [ IF EXISTS ] RENAME TO statement in Memory connector --- .../src/main/sphinx/connector/memory.rst | 9 ++++++++ .../presto/plugin/memory/MemoryMetadata.java | 15 +++++++++++++ .../plugin/memory/TestMemoryMetadata.java | 10 ++++++++- .../presto/plugin/memory/TestMemorySmoke.java | 22 +++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/presto-docs/src/main/sphinx/connector/memory.rst b/presto-docs/src/main/sphinx/connector/memory.rst index 0a49cc4a6290..041282c60d84 100644 --- a/presto-docs/src/main/sphinx/connector/memory.rst +++ b/presto-docs/src/main/sphinx/connector/memory.rst @@ -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 ---------------------------- diff --git a/presto-memory/src/main/java/com/facebook/presto/plugin/memory/MemoryMetadata.java b/presto-memory/src/main/java/com/facebook/presto/plugin/memory/MemoryMetadata.java index 0c8c9e8b62f9..b600eabc8ea1 100644 --- a/presto-memory/src/main/java/com/facebook/presto/plugin/memory/MemoryMetadata.java +++ b/presto-memory/src/main/java/com/facebook/presto/plugin/memory/MemoryMetadata.java @@ -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) { diff --git a/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemoryMetadata.java b/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemoryMetadata.java index 1a1c200935f2..acc108c57a76 100644 --- a/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemoryMetadata.java +++ b/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemoryMetadata.java @@ -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()); @@ -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()); diff --git a/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemorySmoke.java b/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemorySmoke.java index 35c31c556540..4e0a43685291 100644 --- a/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemorySmoke.java +++ b/presto-memory/src/test/java/com/facebook/presto/plugin/memory/TestMemorySmoke.java @@ -185,6 +185,28 @@ 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); + 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"); + assertQueryFails("ALTER VIEW test_view_renamed RENAME TO test_different_schema.test_view_renamed", "line 1:1: View rename across schemas is not supported"); + assertUpdate("DROP VIEW 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 listMemoryTables() { return getQueryRunner().listTables(getSession(), "memory", "default");