Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5350,9 +5350,28 @@ public void testCreateMaterializedView()
@Test
public void testShowCreateOnMaterializedView()
{
String createMaterializedViewSql = formatSqlText(format("CREATE MATERIALIZED VIEW %s.%s.test_customer_mv_1\n" +
"WITH (\n" +
" format = 'ORC'," +
" partitioned_by = ARRAY['nationkey']\n" +
retentionDays(15) +
") AS SELECT\n" +
" name\n" +
", nationkey\n" +
"FROM\n" +
" test_customer_base_1",
getSession().getCatalog().get(),
getSession().getSchema().get()));

computeActual("CREATE TABLE test_customer_base_1 WITH (partitioned_by = ARRAY['nationkey']) AS SELECT custkey, name, address, nationkey FROM customer LIMIT 10");
computeActual("CREATE MATERIALIZED VIEW test_customer_mv_1 WITH (partitioned_by = ARRAY['nationkey']" + retentionDays(30) + ") AS SELECT name, nationkey FROM test_customer_base_1");
computeActual(createMaterializedViewSql);

MaterializedResult actualResult = computeActual("SHOW CREATE MATERIALIZED VIEW test_customer_mv_1");
assertEquals(getOnlyElement(actualResult.getOnlyColumnAsSet()), createMaterializedViewSql.trim());

assertQueryFails(
"SHOW CREATE MATERIALIZED VIEW test_customer_base_1",
format(".*Relation '%s.%s.test_customer_base_1' is a table, not a materialized view", getSession().getCatalog().get(), getSession().getSchema().get()));
assertQueryFails(
"SHOW CREATE VIEW test_customer_mv_1",
format(".*Relation '%s.%s.test_customer_mv_1' is a materialized view, not a view", getSession().getCatalog().get(), getSession().getSchema().get()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.facebook.presto.sql.tree.BooleanLiteral;
import com.facebook.presto.sql.tree.ColumnDefinition;
import com.facebook.presto.sql.tree.CreateFunction;
import com.facebook.presto.sql.tree.CreateMaterializedView;
import com.facebook.presto.sql.tree.CreateTable;
import com.facebook.presto.sql.tree.CreateView;
import com.facebook.presto.sql.tree.DoubleLiteral;
Expand Down Expand Up @@ -140,6 +141,7 @@
import static com.facebook.presto.sql.tree.RoutineCharacteristics.Determinism;
import static com.facebook.presto.sql.tree.RoutineCharacteristics.Language;
import static com.facebook.presto.sql.tree.RoutineCharacteristics.NullCallClause;
import static com.facebook.presto.sql.tree.ShowCreate.Type.MATERIALIZED_VIEW;
import static com.facebook.presto.sql.tree.ShowCreate.Type.TABLE;
import static com.facebook.presto.sql.tree.ShowCreate.Type.VIEW;
import static com.google.common.base.Strings.nullToEmpty;
Expand Down Expand Up @@ -460,6 +462,36 @@ protected Node visitShowCreate(ShowCreate node, Void context)
return singleValueQuery("Create View", sql);
}

if (node.getType() == MATERIALIZED_VIEW) {
Optional<TableHandle> tableHandle = metadata.getTableHandle(session, objectName);

if (!materializedViewDefinition.isPresent()) {
if (viewDefinition.isPresent()) {
throw new SemanticException(NOT_SUPPORTED, node, "Relation '%s' is a view, not a materialized view", objectName);
}
if (tableHandle.isPresent()) {
throw new SemanticException(NOT_SUPPORTED, node, "Relation '%s' is a table, not a materialized view", objectName);
}
throw new SemanticException(MISSING_TABLE, node, "Materialized view '%s' does not exist", objectName);
}

Query query = parseView(materializedViewDefinition.get().getOriginalSql(), objectName, node);

ConnectorTableMetadata connectorTableMetadata = metadata.getTableMetadata(session, tableHandle.get()).getMetadata();
Map<String, Object> properties = connectorTableMetadata.getProperties();
Map<String, PropertyMetadata<?>> allTableProperties = metadata.getTablePropertyManager().getAllProperties().get(tableHandle.get().getConnectorId());
List<Property> propertyNodes = buildProperties(objectName, Optional.empty(), INVALID_TABLE_PROPERTY, properties, allTableProperties);

CreateMaterializedView createMaterializedView = new CreateMaterializedView(
Optional.empty(),
createQualifiedName(objectName),
query,
false,
propertyNodes,
connectorTableMetadata.getComment());
return singleValueQuery("Create Materialized View", formatSql(createMaterializedView, Optional.of(parameters)).trim());
}

if (node.getType() == TABLE) {
if (viewDefinition.isPresent()) {
throw new SemanticException(NOT_SUPPORTED, node, "Relation '%s' is a view, not a table", objectName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ statement
('(' explainOption (',' explainOption)* ')')? statement #explain
| SHOW CREATE TABLE qualifiedName #showCreateTable
| SHOW CREATE VIEW qualifiedName #showCreateView
| SHOW CREATE MATERIALIZED VIEW qualifiedName #showCreateMaterializedView
| SHOW CREATE FUNCTION qualifiedName types? #showCreateFunction
| SHOW TABLES ((FROM | IN) qualifiedName)?
(LIKE pattern=string (ESCAPE escape=string)?)? #showTables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,10 @@ else if (node.getType() == ShowCreate.Type.VIEW) {
builder.append("SHOW CREATE VIEW ")
.append(formatName(node.getName()));
}
else if (node.getType() == ShowCreate.Type.MATERIALIZED_VIEW) {
builder.append("SHOW CREATE MATERIALIZED VIEW ")
.append(formatName(node.getName()));
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@ public Node visitShowCreateView(SqlBaseParser.ShowCreateViewContext context)
return new ShowCreate(getLocation(context), ShowCreate.Type.VIEW, getQualifiedName(context.qualifiedName()));
}

@Override
public Node visitShowCreateMaterializedView(SqlBaseParser.ShowCreateMaterializedViewContext context)
{
return new ShowCreate(getLocation(context), ShowCreate.Type.MATERIALIZED_VIEW, getQualifiedName(context.qualifiedName()));
}

@Override
public Node visitShowFunctions(SqlBaseParser.ShowFunctionsContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public class ShowCreate
public enum Type
{
TABLE,
VIEW
VIEW,
MATERIALIZED_VIEW
}

private final Type type;
Expand Down