Skip to content

Commit

Permalink
Support modify view comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jibing-Li committed Jan 14, 2025
1 parent 06922c8 commit 88b1d1a
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ supportedCreateStatement
supportedAlterStatement
: ALTER VIEW name=multipartIdentifier (LEFT_PAREN cols=simpleColumnDefs RIGHT_PAREN)?
AS query #alterView
| ALTER VIEW name=multipartIdentifier MODIFY COMMENT comment=STRING_LITERAL #alterViewComment
| ALTER CATALOG name=identifier RENAME newName=identifier #alterCatalogRename
| ALTER ROLE role=identifier commentSpec #alterRole
| ALTER STORAGE VAULT name=multipartIdentifier properties=propertyClause #alterStorageVault
Expand Down
24 changes: 17 additions & 7 deletions fe/fe-core/src/main/java/org/apache/doris/alter/Alter.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,23 +813,28 @@ public void processAlterView(AlterViewStmt stmt, ConnectContext ctx) throws User

String tableName = dbTableName.getTbl();
View view = (View) db.getTableOrMetaException(tableName, TableType.VIEW);
modifyViewDef(db, view, stmt.getInlineViewDef(), ctx.getSessionVariable().getSqlMode(), stmt.getColumns());
modifyViewDef(db, view, stmt.getInlineViewDef(), ctx.getSessionVariable().getSqlMode(), stmt.getColumns(),
stmt.getComment());
}

private void modifyViewDef(Database db, View view, String inlineViewDef, long sqlMode,
List<Column> newFullSchema) throws DdlException {
List<Column> newFullSchema, String comment) throws DdlException {
db.writeLockOrDdlException();
try {
view.writeLockOrDdlException();
try {
view.setInlineViewDefWithSqlMode(inlineViewDef, sqlMode);
view.setNewFullSchema(newFullSchema);
if (comment != null) {
view.setComment(comment);
} else {
view.setInlineViewDefWithSqlMode(inlineViewDef, sqlMode);
view.setNewFullSchema(newFullSchema);
}
String viewName = view.getName();
db.unregisterTable(viewName);
db.registerTable(view);

AlterViewInfo alterViewInfo = new AlterViewInfo(db.getId(), view.getId(),
inlineViewDef, newFullSchema, sqlMode);
inlineViewDef, newFullSchema, sqlMode, comment);
Env.getCurrentEnv().getEditLog().logModifyViewDef(alterViewInfo);
LOG.info("modify view[{}] definition to {}", viewName, inlineViewDef);
} finally {
Expand All @@ -845,6 +850,7 @@ public void replayModifyViewDef(AlterViewInfo alterViewInfo) throws MetaNotFound
long tableId = alterViewInfo.getTableId();
String inlineViewDef = alterViewInfo.getInlineViewDef();
List<Column> newFullSchema = alterViewInfo.getNewFullSchema();
String comment = alterViewInfo.getComment();

Database db = Env.getCurrentInternalCatalog().getDbOrMetaException(dbId);
View view = (View) db.getTableOrMetaException(tableId, TableType.VIEW);
Expand All @@ -853,8 +859,12 @@ public void replayModifyViewDef(AlterViewInfo alterViewInfo) throws MetaNotFound
view.writeLock();
try {
String viewName = view.getName();
view.setInlineViewDefWithSqlMode(inlineViewDef, alterViewInfo.getSqlMode());
view.setNewFullSchema(newFullSchema);
if (comment != null) {
view.setComment(comment);
} else {
view.setInlineViewDefWithSqlMode(inlineViewDef, alterViewInfo.getSqlMode());
view.setNewFullSchema(newFullSchema);
}

// We do not need to init view here.
// During the `init` phase, some `Alter-View` statements will access the remote file system,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,29 @@
@Deprecated
public class AlterViewStmt extends BaseViewStmt implements NotFallbackInParser {

private final String comment;

public AlterViewStmt(TableName tbl, String comment) {
this(tbl, null, null, comment);
}

public AlterViewStmt(TableName tbl, List<ColWithComment> cols, QueryStmt queryStmt) {
this(tbl, cols, queryStmt, null);
}

public AlterViewStmt(TableName tbl, List<ColWithComment> cols, QueryStmt queryStmt, String comment) {
super(tbl, cols, queryStmt);
this.comment = comment;
}

public TableName getTbl() {
return tableName;
}

public String getComment() {
return comment;
}

@Override
public void analyze(Analyzer analyzer) throws UserException {
super.analyze(analyzer);
Expand Down
4 changes: 3 additions & 1 deletion fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -5681,8 +5681,10 @@ public void createView(CreateViewStmt stmt) throws DdlException {
}

if (replace) {
String comment = stmt.getComment();
comment = comment == null || comment.isEmpty() ? null : comment;
AlterViewStmt alterViewStmt = new AlterViewStmt(stmt.getTableName(), stmt.getColWithComments(),
stmt.getViewDefStmt());
stmt.getViewDefStmt(), comment);
alterViewStmt.setInlineViewDef(stmt.getInlineViewDef());
try {
alterView(alterViewStmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.apache.doris.nereids.DorisParser.AlterTableClauseContext;
import org.apache.doris.nereids.DorisParser.AlterTableContext;
import org.apache.doris.nereids.DorisParser.AlterTableDropRollupContext;
import org.apache.doris.nereids.DorisParser.AlterViewCommentContext;
import org.apache.doris.nereids.DorisParser.AlterViewContext;
import org.apache.doris.nereids.DorisParser.AlterWorkloadGroupContext;
import org.apache.doris.nereids.DorisParser.AlterWorkloadPolicyContext;
Expand Down Expand Up @@ -498,6 +499,7 @@
import org.apache.doris.nereids.trees.plans.commands.AlterStorageVaultCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterTableCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommentCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterWorkloadGroupCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterWorkloadPolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CallCommand;
Expand Down Expand Up @@ -1293,6 +1295,13 @@ public LogicalPlan visitAlterView(AlterViewContext ctx) {
return new AlterViewCommand(info);
}

@Override
public LogicalPlan visitAlterViewComment(AlterViewCommentContext ctx) {
List<String> nameParts = visitMultipartIdentifier(ctx.name);
AlterViewInfo info = new AlterViewInfo(new TableNameInfo(nameParts), ctx.comment.getText());
return new AlterViewCommentCommand(info);
}

@Override
public LogicalPlan visitAlterStorageVault(AlterStorageVaultContext ctx) {
List<String> nameParts = this.visitMultipartIdentifier(ctx.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public enum PlanType {
ALTER_CATALOG_RENAME_COMMAND,
ALTER_ROLE_COMMAND,
ALTER_VIEW_COMMAND,
ALTER_VIEW_COMMENT_COMMAND,
ALTER_STORAGE_VAULT,
ALTER_WORKLOAD_GROUP_COMMAND,
ALTER_WORKLOAD_POLICY_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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.doris.nereids.trees.plans.commands;

import org.apache.doris.analysis.AlterViewStmt;
import org.apache.doris.analysis.StmtType;
import org.apache.doris.catalog.Env;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.commands.info.AlterViewInfo;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.StmtExecutor;

/**
* AlterViewCommentCommand
*/
public class AlterViewCommentCommand extends Command implements ForwardWithSync {
private final AlterViewInfo alterViewInfo;

public AlterViewCommentCommand(AlterViewInfo alterViewInfo) {
super(PlanType.ALTER_VIEW_COMMENT_COMMAND);
this.alterViewInfo = alterViewInfo;
}

@Override
public void run(ConnectContext ctx, StmtExecutor executor) throws Exception {
executor.checkBlockRules();
alterViewInfo.init(ctx);
AlterViewStmt alterViewStmt = alterViewInfo.translateToLegacyStmt(ctx);
Env.getCurrentEnv().alterView(alterViewStmt);
}

@Override
public <R, C> R accept(PlanVisitor<R, C> visitor, C context) {
return visitor.visitAlterViewCommentCommand(this, context);
}

@Override
public StmtType stmtType() {
return StmtType.ALTER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,19 @@

/** AlterViewInfo */
public class AlterViewInfo extends BaseViewInfo {

private final String comment;

/** constructor*/
public AlterViewInfo(TableNameInfo viewName,
String querySql, List<SimpleColumnDefinition> simpleColumnDefinitions) {
super(viewName, querySql, simpleColumnDefinitions);
this.comment = null;
}

public AlterViewInfo(TableNameInfo viewName, String comment) {
super(viewName, null, null);
this.comment = comment;
}

/** init */
Expand All @@ -69,6 +78,10 @@ public void init(ConnectContext ctx) throws UserException {
ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLE_ACCESS_DENIED_ERROR,
PrivPredicate.ALTER.getPrivs().toString(), viewName.getTbl());
}
if (comment != null) {
// Modify comment only.
return;
}
analyzeAndFillRewriteSqlMap(querySql, ctx);
PlanUtils.OutermostPlanFinderContext outermostPlanFinderContext = new PlanUtils.OutermostPlanFinderContext();
analyzedPlan.accept(PlanUtils.OutermostPlanFinder.INSTANCE, outermostPlanFinderContext);
Expand All @@ -78,6 +91,9 @@ public void init(ConnectContext ctx) throws UserException {

/**translateToLegacyStmt*/
public AlterViewStmt translateToLegacyStmt(ConnectContext ctx) {
if (comment != null) {
return new AlterViewStmt(viewName.transferToTableName(), comment);
}
// expand star(*) in project list and replace table name with qualifier
String rewrittenSql = rewriteSql(ctx.getStatementContext().getIndexInSqlToString(), querySql);
// rewrite project alias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.doris.nereids.trees.plans.commands.AlterSqlBlockRuleCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterTableCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterViewCommentCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterWorkloadGroupCommand;
import org.apache.doris.nereids.trees.plans.commands.AlterWorkloadPolicyCommand;
import org.apache.doris.nereids.trees.plans.commands.CallCommand;
Expand Down Expand Up @@ -369,6 +370,10 @@ default R visitAlterViewCommand(AlterViewCommand alterViewCommand, C context) {
return visitCommand(alterViewCommand, context);
}

default R visitAlterViewCommentCommand(AlterViewCommentCommand alterViewCommentCommand, C context) {
return visitCommand(alterViewCommentCommand, context);
}

default R visitDropCatalogCommand(DropCatalogCommand dropCatalogCommand, C context) {
return visitCommand(dropCatalogCommand, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ public class AlterViewInfo implements Writable {
private long sqlMode;
@SerializedName(value = "newFullSchema")
private List<Column> newFullSchema;
@SerializedName(value = "comment")
private String comment;

public AlterViewInfo() {
// for persist
newFullSchema = Lists.newArrayList();
}

public AlterViewInfo(long dbId, long tableId, String inlineViewDef, List<Column> newFullSchema, long sqlMode) {
public AlterViewInfo(long dbId, long tableId, String inlineViewDef, List<Column> newFullSchema, long sqlMode,
String comment) {
this.dbId = dbId;
this.tableId = tableId;
this.inlineViewDef = inlineViewDef;
this.newFullSchema = newFullSchema;
this.sqlMode = sqlMode;
this.comment = comment;
}

public long getDbId() {
Expand All @@ -76,6 +80,10 @@ public long getSqlMode() {
return sqlMode;
}

public String getComment() {
return comment;
}

@Override
public int hashCode() {
return Objects.hash(dbId, tableId, inlineViewDef, sqlMode, newFullSchema);
Expand All @@ -92,7 +100,7 @@ public boolean equals(Object other) {
AlterViewInfo otherInfo = (AlterViewInfo) other;
return dbId == otherInfo.getDbId() && tableId == otherInfo.getTableId()
&& inlineViewDef.equalsIgnoreCase(otherInfo.getInlineViewDef()) && sqlMode == otherInfo.getSqlMode()
&& newFullSchema.equals(otherInfo.getNewFullSchema());
&& newFullSchema.equals(otherInfo.getNewFullSchema()) && Objects.equals(comment, otherInfo.comment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void testSerializeAlterViewInfo() throws IOException, AnalysisException {
Column column1 = new Column("col1", PrimitiveType.BIGINT);
Column column2 = new Column("col2", PrimitiveType.DOUBLE);

AlterViewInfo alterViewInfo = new AlterViewInfo(dbId, tableId, inlineViewDef, Lists.newArrayList(column1, column2), sqlMode);
AlterViewInfo alterViewInfo = new AlterViewInfo(dbId, tableId, inlineViewDef, Lists.newArrayList(column1, column2), sqlMode, null);
alterViewInfo.write(out);
out.flush();
out.close();
Expand Down
12 changes: 12 additions & 0 deletions regression-test/data/view_p0/view_p0.out
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ internal regression_test_view_p0 test_view select 1,to_base64(AES_ENCRYPT('doris
-- !show_aes --
test_view_aes CREATE VIEW `test_view_aes` AS SELECT aes_decrypt(from_base64("EXp7k7M9Zv1mIwPpno28Hg=="), '17IMZrGdwWf2Piy8', 'II2HLtihr5TQpQgR', 'AES_128_CBC'); utf8mb4 utf8mb4_0900_bin

-- !comment1 --
test_view_table2_view CREATE VIEW `test_view_table2_view` AS SELECT \n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`,'%Y-%m-%d') AS `CREATE_DATE`\n FROM \n `internal`.`regression_test_view_p0`.`test_view_table2`\n GROUP BY \n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`, '%Y-%m-%d'); utf8mb4 utf8mb4_0900_bin

-- !comment2 --
test_view_table2_view CREATE VIEW `test_view_table2_view` COMMENT '"comment1"' AS SELECT \n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`,'%Y-%m-%d') AS `CREATE_DATE`\n FROM \n `internal`.`regression_test_view_p0`.`test_view_table2`\n GROUP BY \n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`, '%Y-%m-%d'); utf8mb4 utf8mb4_0900_bin

-- !comment3 --
test_view_table2_view CREATE VIEW `test_view_table2_view` COMMENT '"comment1"' AS SELECT\n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`,'%Y-%m-%d') AS `CREATE_DATE`\n FROM\n `internal`.`regression_test_view_p0`.`test_view_table2`\n GROUP BY\n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`, '%Y-%m-%d'); utf8mb4 utf8mb4_0900_bin

-- !comment4 --
test_view_table2_view CREATE VIEW `test_view_table2_view` COMMENT '"comment2"' AS SELECT\n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`,'%Y-%m-%d') AS `CREATE_DATE`\n FROM\n `internal`.`regression_test_view_p0`.`test_view_table2`\n GROUP BY\n date_format(`internal`.`regression_test_view_p0`.`test_view_table2`.`c_date`, '%Y-%m-%d'); utf8mb4 utf8mb4_0900_bin

20 changes: 19 additions & 1 deletion regression-test/suites/view_p0/view_p0.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,25 @@ suite("view_p0") {
"""

sql """select * from test_view_table2_view;"""

qt_comment1 """show create table test_view_table2_view"""
sql """ALTER VIEW `test_view_table2_view` MODIFY COMMENT "comment1";"""
qt_comment2 """show create table test_view_table2_view"""

sql """Alter VIEW `test_view_table2_view`
AS
SELECT
date_format(c_date,'%Y-%m-%d') AS `CREATE_DATE`
FROM
test_view_table2
GROUP BY
date_format(c_date, '%Y-%m-%d');
"""
qt_comment3 """show create table test_view_table2_view"""

sql """Alter VIEW `test_view_table2_view` MODIFY COMMENT "comment2";"""
qt_comment4 """show create table test_view_table2_view"""

sql """ drop view if exists test_view_table2_view;"""
sql """DROP TABLE IF EXISTS test_view_table2"""
}

0 comments on commit 88b1d1a

Please sign in to comment.