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 @@ -133,6 +133,7 @@
"org.apache.flink.sql.parser.dql.SqlShowTables"
"org.apache.flink.sql.parser.dql.SqlShowColumns"
"org.apache.flink.sql.parser.dql.SqlShowCreate"
"org.apache.flink.sql.parser.dql.SqlShowCreateMaterializedTable"
"org.apache.flink.sql.parser.dql.SqlShowCreateModel"
"org.apache.flink.sql.parser.dql.SqlShowCreateTable"
"org.apache.flink.sql.parser.dql.SqlShowCreateView"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,13 @@ SqlShowCreate SqlShowCreate() :
{
return new SqlShowCreateModel(pos, sqlIdentifier);
}
|
<MATERIALIZED> <TABLE>
{ pos = getPos(); }
sqlIdentifier = CompoundIdentifier()
{
return new SqlShowCreateMaterializedTable(pos, sqlIdentifier);
}
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.sql.parser.dql;

import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;

import java.util.Collections;
import java.util.List;

/** SHOW CREATE MATERIALIZED TABLE sql call. */
public class SqlShowCreateMaterializedTable extends SqlShowCreate {
public static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("SHOW CREATE MATERIALIZED TABLE", SqlKind.OTHER_DDL);

public SqlShowCreateMaterializedTable(SqlParserPos pos, SqlIdentifier sqlIdentifier) {
super(pos, sqlIdentifier);
}

public SqlIdentifier getMaterializedTableName() {
return sqlIdentifier;
}

public String[] getFullMaterializedTableName() {
return sqlIdentifier.names.toArray(new String[0]);
}

@Override
public SqlOperator getOperator() {
return OPERATOR;
}

@Override
public List<SqlNode> getOperandList() {
return Collections.singletonList(sqlIdentifier);
}

@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword(OPERATOR.getName());
sqlIdentifier.unparse(writer, leftPrec, rightPrec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import org.apache.flink.annotation.Internal;
import org.apache.flink.table.api.TableException;
import org.apache.flink.table.catalog.CatalogBaseTable;
import org.apache.flink.table.catalog.CatalogBaseTable.TableKind;
import org.apache.flink.table.catalog.CatalogDescriptor;
import org.apache.flink.table.catalog.CatalogView;
import org.apache.flink.table.catalog.Column;
import org.apache.flink.table.catalog.IntervalFreshness;
import org.apache.flink.table.catalog.ObjectIdentifier;
import org.apache.flink.table.catalog.QueryOperationCatalogView;
import org.apache.flink.table.catalog.ResolvedCatalogBaseTable;
import org.apache.flink.table.catalog.ResolvedCatalogMaterializedTable;
import org.apache.flink.table.catalog.ResolvedCatalogModel;
import org.apache.flink.table.catalog.ResolvedCatalogTable;
import org.apache.flink.table.catalog.ResolvedSchema;
Expand All @@ -38,6 +41,7 @@
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -82,12 +86,7 @@ public static String buildShowCreateTableRow(
ObjectIdentifier tableIdentifier,
boolean isTemporary,
SqlFactory sqlFactory) {
if (table.getTableKind() == CatalogBaseTable.TableKind.VIEW) {
throw new TableException(
String.format(
"SHOW CREATE TABLE is only supported for tables, but %s is a view. Please use SHOW CREATE VIEW instead.",
tableIdentifier.asSerializableString()));
}
validateTableKind(table, tableIdentifier, TableKind.TABLE);
StringBuilder sb =
new StringBuilder()
.append(buildCreateFormattedPrefix("TABLE", isTemporary, tableIdentifier));
Expand All @@ -110,17 +109,40 @@ public static String buildShowCreateTableRow(
return sb.toString();
}

/** Show create materialized table statement only for materialized tables. */
public static String buildShowCreateMaterializedTableRow(
ResolvedCatalogMaterializedTable table,
ObjectIdentifier tableIdentifier,
boolean isTemporary) {
validateTableKind(table, tableIdentifier, TableKind.MATERIALIZED_TABLE);
StringBuilder sb =
new StringBuilder()
.append(
buildCreateFormattedPrefix(
"MATERIALIZED TABLE", isTemporary, tableIdentifier));
sb.append(extractFormattedColumns(table, PRINT_INDENT));
extractFormattedPrimaryKey(table, PRINT_INDENT)
.ifPresent(pk -> sb.append(",\n").append(pk));
sb.append("\n)\n");
extractComment(table).ifPresent(c -> sb.append(formatComment(c)).append("\n"));
extractFormattedPartitionedInfo(table)
.ifPresent(partitionedBy -> sb.append(formatPartitionedBy(partitionedBy)));
extractFormattedOptions(table.getOptions(), PRINT_INDENT)
.ifPresent(v -> sb.append("WITH (\n").append(v).append("\n)\n"));
sb.append(extractFreshness(table))
.append("\n")
.append(extractRefreshMode(table))
.append("\n");
sb.append("AS ").append(table.getDefinitionQuery()).append('\n');
return sb.toString();
}

/** Show create view statement only for views. */
public static String buildShowCreateViewRow(
ResolvedCatalogBaseTable<?> view,
ObjectIdentifier viewIdentifier,
boolean isTemporary) {
if (view.getTableKind() != CatalogBaseTable.TableKind.VIEW) {
throw new TableException(
String.format(
"SHOW CREATE VIEW is only supported for views, but %s is a table. Please use SHOW CREATE TABLE instead.",
viewIdentifier.asSerializableString()));
}
validateTableKind(view, viewIdentifier, TableKind.VIEW);
final CatalogBaseTable origin = view.getOrigin();
if (origin instanceof QueryOperationCatalogView
&& !((QueryOperationCatalogView) origin).supportsShowCreateView()) {
Expand Down Expand Up @@ -243,6 +265,10 @@ private static String formatComment(String comment) {
return String.format("COMMENT '%s'", EncodingUtils.escapeSingleQuotes(comment));
}

private static String formatPartitionedBy(String partitionedByColumns) {
return String.format("PARTITION BY (%s)\n", partitionedByColumns);
}

static Optional<String> extractComment(ResolvedCatalogBaseTable<?> table) {
return StringUtils.isEmpty(table.getComment())
? Optional.empty()
Expand All @@ -263,10 +289,32 @@ static Optional<String> extractFormattedPartitionedInfo(ResolvedCatalogTable cat
if (!catalogTable.isPartitioned()) {
return Optional.empty();
}
return Optional.of(
catalogTable.getPartitionKeys().stream()
.map(EncodingUtils::escapeIdentifier)
.collect(Collectors.joining(", ")));
return Optional.of(extractPartitionKeys(catalogTable.getPartitionKeys()));
}

static Optional<String> extractFormattedPartitionedInfo(
ResolvedCatalogMaterializedTable catalogMaterializedTable) {
if (!catalogMaterializedTable.isPartitioned()) {
return Optional.empty();
}
return Optional.of(extractPartitionKeys(catalogMaterializedTable.getPartitionKeys()));
}

private static String extractPartitionKeys(List<String> partitionKeys) {
return partitionKeys.stream()
.map(EncodingUtils::escapeIdentifier)
.collect(Collectors.joining(", "));
}

static String extractFreshness(ResolvedCatalogMaterializedTable materializedTable) {
final IntervalFreshness definitionFreshness = materializedTable.getDefinitionFreshness();
return String.format(
"FRESHNESS = INTERVAL '%s' %s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the SQL parser allow parsing INTERVAL '5' DAY. Doesn't it need to be plural. Also why is the interval a string, this looks like a bug to me. Maybe we should change it before it is too late.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok it seems the SQL parser allows parsing INTERVAL '5' DAY

definitionFreshness.getInterval(), definitionFreshness.getTimeUnit());
}

static String extractRefreshMode(ResolvedCatalogMaterializedTable materializedTable) {
return String.format("REFRESH_MODE = %s", materializedTable.getRefreshMode());
}

static Optional<String> extractFormattedOptions(Map<String, String> conf, String printIndent) {
Expand Down Expand Up @@ -297,4 +345,42 @@ static String extractFormattedColumnNames(
EncodingUtils.escapeIdentifier(column.getName())))
.collect(Collectors.joining(",\n"));
}

private static void validateTableKind(
ResolvedCatalogBaseTable<?> table,
ObjectIdentifier tableIdentifier,
TableKind expectedTableKind) {
if (table.getTableKind() == expectedTableKind) {
return;
}

final String tableKindName = table.getTableKind().name().replace('_', ' ');
final String commandToUse = showCreateCommandToUse(table.getTableKind());
final String expectedTableKindName = expectedTableKind.name().replace('_', ' ');
final String currentCommand = "SHOW CREATE " + expectedTableKindName;

throw new TableException(
String.format(
"%s is only supported for %ss, but %s is a %s. Please use %s instead.",
currentCommand,
expectedTableKindName.toLowerCase(Locale.ROOT),
tableIdentifier.asSerializableString(),
tableKindName.toLowerCase(Locale.ROOT),
commandToUse));
}

private static String showCreateCommandToUse(TableKind tableKind) {
switch (tableKind) {
case MATERIALIZED_TABLE:
return "SHOW CREATE MATERIALIZED TABLE";
case TABLE:
return "SHOW CREATE TABLE";
case VIEW:
return "SHOW CREATE VIEW";
default:
throw new TableException(
String.format(
"SHOW CREATE is not implemented for %s yet.", tableKind.name()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.table.operations;

import org.apache.flink.annotation.Internal;
import org.apache.flink.table.api.ValidationException;
import org.apache.flink.table.api.internal.ShowCreateUtil;
import org.apache.flink.table.api.internal.TableResultInternal;
import org.apache.flink.table.catalog.ContextResolvedTable;
import org.apache.flink.table.catalog.ObjectIdentifier;

import static org.apache.flink.table.api.internal.TableResultUtils.buildStringArrayResult;

/** Operation to describe a SHOW CREATE MATERIALIZED TABLE statement. */
@Internal
public class ShowCreateMaterializedTableOperation implements ShowOperation {
private final ObjectIdentifier tableIdentifier;

public ShowCreateMaterializedTableOperation(ObjectIdentifier sqlIdentifier) {
this.tableIdentifier = sqlIdentifier;
}

@Override
public String asSummaryString() {
return String.format(
"SHOW CREATE MATERIALIZED TABLE %s", tableIdentifier.asSummaryString());
}

@Override
public TableResultInternal execute(Context ctx) {
ContextResolvedTable table =
ctx.getCatalogManager()
.getTable(tableIdentifier)
.orElseThrow(
() ->
new ValidationException(
String.format(
"Could not execute SHOW CREATE MATERIALIZED TABLE. Materialized table with identifier %s does not exist.",
tableIdentifier.asSerializableString())));
String resultRow =
ShowCreateUtil.buildShowCreateMaterializedTableRow(
table.getResolvedTable(), tableIdentifier, table.isTemporary());

return buildStringArrayResult("result", new String[] {resultRow});
}
}
Loading