Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] (nereids)implement showCollationCommand in nereids #43157

Merged
merged 5 commits into from
Dec 2, 2024
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 @@ -231,6 +231,7 @@ supportedShowStatement
| SHOW FILE ((FROM | IN) database=multipartIdentifier)? #showSmallFiles
| SHOW STORAGE? ENGINES #showStorageEngines
| SHOW CREATE CATALOG name=identifier #showCreateCatalog
| SHOW COLLATION wildWhere? #showCollation
| SHOW SQL_BLOCK_RULE (FOR ruleName=identifier)? #showSqlBlockRule
| SHOW CREATE MATERIALIZED VIEW mvName=identifier
ON tableName=multipartIdentifier #showCreateMaterializedView
Expand Down Expand Up @@ -300,7 +301,6 @@ unsupportedShowStatement
| SHOW CATALOG name=identifier #showCatalog
| SHOW FULL? (COLUMNS | FIELDS) (FROM | IN) tableName=multipartIdentifier
((FROM | IN) database=multipartIdentifier)? wildWhere? #showColumns
| SHOW COLLATION wildWhere? #showCollation
| SHOW ((CHAR SET) | CHARSET) wildWhere? #showCharset
| SHOW COUNT LEFT_PAREN ASTERISK RIGHT_PAREN (WARNINGS | ERRORS) #showWaringErrorCount
| SHOW (WARNINGS | ERRORS) limitClause? #showWaringErrors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
import org.apache.doris.nereids.DorisParser.ShowAuthorsContext;
import org.apache.doris.nereids.DorisParser.ShowBackendsContext;
import org.apache.doris.nereids.DorisParser.ShowBrokerContext;
import org.apache.doris.nereids.DorisParser.ShowCollationContext;
import org.apache.doris.nereids.DorisParser.ShowConfigContext;
import org.apache.doris.nereids.DorisParser.ShowConstraintContext;
import org.apache.doris.nereids.DorisParser.ShowCreateCatalogContext;
Expand Down Expand Up @@ -494,6 +495,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowAuthorsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBackendsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBrokerCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCollationCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateCatalogCommand;
Expand Down Expand Up @@ -4582,4 +4584,17 @@ public LogicalPlan visitShowTabletsBelong(ShowTabletsBelongContext ctx) {
});
return new ShowTabletsBelongCommand(tabletIdLists);
}

@Override
public LogicalPlan visitShowCollation(ShowCollationContext ctx) {
String wild = null;
if (ctx.wildWhere() != null) {
if (ctx.wildWhere().LIKE() != null) {
wild = stripQuotes(ctx.wildWhere().STRING_LITERAL().getText());
} else if (ctx.wildWhere().WHERE() != null) {
wild = ctx.wildWhere().expression().getText();
}
}
return new ShowCollationCommand(wild);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public enum PlanType {
SHOW_BACKENDS_COMMAND,
SHOW_BLOCK_RULE_COMMAND,
SHOW_BROKER_COMMAND,
SHOW_COLLATION_COMMAND,
SHOW_CONFIG_COMMAND,
SHOW_CREATE_CATALOG_COMMAND,
SHOW_CREATE_MATERIALIZED_VIEW_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.catalog.Column;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.nereids.trees.plans.PlanType;
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.qe.ShowResultSet;
import org.apache.doris.qe.ShowResultSetMetaData;
import org.apache.doris.qe.StmtExecutor;

import com.google.common.collect.Lists;

import java.util.List;

/**
* Represents the command for SHOW COLLATION
*/
public class ShowCollationCommand extends ShowCommand {
private static final ShowResultSetMetaData COLLATION_META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("Collation", ScalarType.createVarchar(20)))
.addColumn(new Column("Charset", ScalarType.createVarchar(20)))
.addColumn(new Column("Id", ScalarType.createVarchar(10)))
.addColumn(new Column("Default", ScalarType.createVarchar(10)))
.addColumn(new Column("Compiled", ScalarType.createVarchar(10)))
.addColumn(new Column("Sortlen", ScalarType.createVarchar(10)))
.build();

private final String wild;

public ShowCollationCommand(String wild) {
super(PlanType.SHOW_COLLATION_COMMAND);
this.wild = wild;
}

@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
List<List<String>> rows = Lists.newArrayList();
List<String> utf8mb40900Bin = Lists.newArrayList();
// | utf8mb4_0900_bin | utf8mb4 | 309 | Yes | Yes | 1 |
utf8mb40900Bin.add(ctx.getSessionVariable().getCollationConnection());
utf8mb40900Bin.add(ctx.getSessionVariable().getCharsetServer());
utf8mb40900Bin.add("309");
utf8mb40900Bin.add("Yes");
utf8mb40900Bin.add("Yes");
utf8mb40900Bin.add("1");
rows.add(utf8mb40900Bin);
// ATTN: we must have this collation for compatible with some bi tools
List<String> utf8mb3GeneralCi = Lists.newArrayList();
// | utf8mb3_general_ci | utf8mb3 | 33 | Yes | Yes | 1 |
utf8mb3GeneralCi.add("utf8mb3_general_ci");
utf8mb3GeneralCi.add("utf8mb3");
utf8mb3GeneralCi.add("33");
utf8mb3GeneralCi.add("Yes");
utf8mb3GeneralCi.add("Yes");
utf8mb3GeneralCi.add("1");
rows.add(utf8mb3GeneralCi);
// Set the result set and send it using the executor
return new ShowResultSet(COLLATION_META_DATA, rows);
}

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

@Override
public String toString() {
return "SHOW COLLATION" + (wild != null ? " LIKE '" + wild + "'" : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowAuthorsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBackendsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowBrokerCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCollationCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConfigCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowConstraintsCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateCatalogCommand;
Expand Down Expand Up @@ -475,6 +476,10 @@ default R visitShowTabletsBelongCommand(ShowTabletsBelongCommand showTabletBelon
return visitCommand(showTabletBelongCommand, context);
}

default R visitShowCollationCommand(ShowCollationCommand showCollationCommand, C context) {
return visitCommand(showCollationCommand, context);
}

default R visitCreateRoutineLoadCommand(CreateRoutineLoadCommand createRoutineLoadCommand, C context) {
return visitCommand(createRoutineLoadCommand, context);
}
Expand Down
5 changes: 5 additions & 0 deletions regression-test/data/nereids_p0/show/test_show_collation.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !cmd --
utf8mb4_0900_bin utf8mb4 309 Yes Yes 1
utf8mb3_general_ci utf8mb3 33 Yes Yes 1

29 changes: 29 additions & 0 deletions regression-test/suites/nereids_p0/show/test_show_collation.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.

suite("test_show_collation", "query,collation") {
try {
// Execute the SHOW COLLATION command and verify the output
checkNereidsExecute("SHOW COLLATION")
qt_cmd("SHOW COLLATION")
} catch (Exception e) {
// Log any exceptions that occur during testing
log.error("Failed to execute SHOW COLLATION command", e)
throw e
}
}