Skip to content

Commit

Permalink
[Enhancement] (nereids)implement showDataTypesCommand in nereids (#44299
Browse files Browse the repository at this point in the history
)

Issue Number: close #42743
  • Loading branch information
msridhar78 authored Dec 19, 2024
1 parent 0e2de34 commit a9de07b
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ supportedShowStatement
| SHOW COLLATION wildWhere? #showCollation
| SHOW SQL_BLOCK_RULE (FOR ruleName=identifier)? #showSqlBlockRule
| SHOW CREATE VIEW name=multipartIdentifier #showCreateView
| SHOW DATA TYPES #showDataTypes
| SHOW CREATE MATERIALIZED VIEW mvName=identifier
ON tableName=multipartIdentifier #showCreateMaterializedView
| SHOW (WARNINGS | ERRORS) limitClause? #showWarningErrors
Expand Down Expand Up @@ -330,7 +331,6 @@ unsupportedShowStatement
LEFT_PAREN functionArguments? RIGHT_PAREN
((FROM | IN) database=multipartIdentifier)? #showCreateFunction
| SHOW (DATABASES | SCHEMAS) (FROM catalog=identifier)? wildWhere? #showDatabases
| SHOW DATA TYPES #showDataTypes
| SHOW CATALOGS wildWhere? #showCatalogs
| SHOW CATALOG name=identifier #showCatalog
| SHOW FULL? (COLUMNS | FIELDS) (FROM | IN) tableName=multipartIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
import org.apache.doris.nereids.DorisParser.ShowCreateTableContext;
import org.apache.doris.nereids.DorisParser.ShowCreateViewContext;
import org.apache.doris.nereids.DorisParser.ShowDataSkewContext;
import org.apache.doris.nereids.DorisParser.ShowDataTypesContext;
import org.apache.doris.nereids.DorisParser.ShowDatabaseIdContext;
import org.apache.doris.nereids.DorisParser.ShowDeleteContext;
import org.apache.doris.nereids.DorisParser.ShowDiagnoseTabletContext;
Expand Down Expand Up @@ -575,6 +576,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowCreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateViewCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDataSkewCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDataTypesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDatabaseIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDeleteCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDiagnoseTabletCommand;
Expand Down Expand Up @@ -4498,6 +4500,11 @@ public LogicalPlan visitShowLoadProfile(ShowLoadProfileContext ctx) {
return new ShowLoadProfileCommand(ctx.loadIdPath.getText());
}

@Override
public LogicalPlan visitShowDataTypes(ShowDataTypesContext ctx) {
return new ShowDataTypesCommand();
}

@Override
public LogicalPlan visitShowGrants(ShowGrantsContext ctx) {
boolean all = (ctx.ALL() != null) ? true : false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public enum PlanType {
SHOW_DYNAMIC_PARTITION_COMMAND,
SHOW_ENCRYPT_KEYS_COMMAND,
SHOW_EVENTS_COMMAND,
SHOW_DATA_TYPES_COMMAND,
SHOW_FRONTENDS_COMMAND,
SHOW_GRANTS_COMMAND,
SHOW_LAST_INSERT_COMMAND,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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.PrimitiveType;
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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* Represents the command for SHOW DATA TYPES.
*/
public class ShowDataTypesCommand extends ShowCommand {
private static final ShowResultSetMetaData META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("TypeName", ScalarType.createVarchar(20)))
.addColumn(new Column("Size", ScalarType.createVarchar(100)))
.build();

public ShowDataTypesCommand() {
super(PlanType.SHOW_DATA_TYPES_COMMAND);
}

/**
* getTypes().
*/
public static ArrayList<PrimitiveType> getTypes() {
return PrimitiveType.getSupportedTypes();
}

/**
* getTypesAvailableInDdl().
*/
public static List<List<String>> getTypesAvailableInDdl() {
ArrayList<PrimitiveType> supportedTypes = getTypes();
List<List<String>> rows = Lists.newArrayList();
for (PrimitiveType type : supportedTypes) {
List<String> row = new ArrayList<>();
if (type.isAvailableInDdl()) {
row.add(type.toString());
row.add(Integer.toString(type.getSlotSize()));
rows.add(row);
}
}
return rows;
}

/**
* sortMetaData().
*/
public void sortMetaData(List<List<String>> rows) {
Collections.sort(rows, new Comparator<List<String>>() {
@Override
public int compare(List<String> row1, List<String> row2) {
return row1.get(0).compareTo(row2.get(0));
}
});
}

@Override
public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exception {
List<List<String>> rows = getTypesAvailableInDdl();
sortMetaData(rows);
return new ShowResultSet(getMetaData(), rows);
}

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

public ShowResultSetMetaData getMetaData() {
return META_DATA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import org.apache.doris.nereids.trees.plans.commands.ShowCreateTableCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowCreateViewCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDataSkewCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDataTypesCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDatabaseIdCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDeleteCommand;
import org.apache.doris.nereids.trees.plans.commands.ShowDiagnoseTabletCommand;
Expand Down Expand Up @@ -521,6 +522,10 @@ default R visitCleanAllProfileCommand(CleanAllProfileCommand cleanAllProfileComm
return visitCommand(cleanAllProfileCommand, context);
}

default R visitShowDataTypesCommand(ShowDataTypesCommand showDataTypesCommand, C context) {
return visitCommand(showDataTypesCommand, context);
}

default R visitShowFrontendsCommand(ShowFrontendsCommand showFrontendsCommand, C context) {
return visitCommand(showFrontendsCommand, context);
}
Expand Down
31 changes: 31 additions & 0 deletions regression-test/data/nereids_p0/show/test_show_data_types.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !cmd --
AGG_STATE 16
ARRAY 32
BIGINT 8
BITMAP 16
BOOLEAN 1
CHAR 16
DATE 16
DATETIME 16
DATETIMEV2 8
DATEV2 4
DECIMAL128 16
DECIMAL32 4
DECIMAL64 8
DECIMALV2 16
DOUBLE 8
FLOAT 4
HLL 16
INT 4
IPV4 4
IPV6 16
JSON 16
LARGEINT 16
MAP 24
QUANTILE_STATE 16
SMALLINT 2
STRING 16
TINYINT 1
VARCHAR 16

29 changes: 29 additions & 0 deletions regression-test/suites/nereids_p0/show/test_show_data_types.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_data_types_nereids", "query,datatype") {
try {
// Execute the SHOW DATA TYPES command and verify the output
checkNereidsExecute("SHOW DATA TYPES")
qt_cmd("SHOW DATA TYPES")
} catch (Exception e) {
// Log any exceptions that occur during testing
log.error("Failed to execute SHOW DATA TYPES command", e)
throw e
}
}

0 comments on commit a9de07b

Please sign in to comment.