From 2a0890d803bde7ed589b1aa4aecf771ebacfabcd Mon Sep 17 00:00:00 2001 From: ZashJie <77664070+ZashJie@users.noreply.github.com> Date: Sun, 26 Mar 2023 12:37:06 +0800 Subject: [PATCH] [feature](datatype) add show data types stmt (#18111) --- .../Show-Statements/SHOW-DATA-TYPES.md | 52 +++++++++++++ .../Show-Statements/SHOW-DATA-TYPES.md | 52 +++++++++++++ fe/fe-core/src/main/cup/sql_parser.cup | 5 ++ .../doris/analysis/ShowDataTypesStmt.java | 75 +++++++++++++++++++ .../org/apache/doris/qe/ShowExecutor.java | 17 +++++ .../doris/analysis/ShowDataTypesStmtTest.java | 48 ++++++++++++ .../data/types/test_show_data_types.out | 32 ++++++++ .../suites/types/test_show_data_types.groovy | 22 ++++++ 8 files changed, 303 insertions(+) create mode 100644 docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md create mode 100644 docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md create mode 100644 fe/fe-core/src/main/java/org/apache/doris/analysis/ShowDataTypesStmt.java create mode 100644 fe/fe-core/src/test/java/org/apache/doris/analysis/ShowDataTypesStmtTest.java create mode 100644 regression-test/data/types/test_show_data_types.out create mode 100644 regression-test/suites/types/test_show_data_types.groovy diff --git a/docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md b/docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md new file mode 100644 index 0000000000..39631f483b --- /dev/null +++ b/docs/en/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md @@ -0,0 +1,52 @@ +--- +{ + "title": "SHOW-DATA-TYPES", + "language": "en" +} +--- + + + +## SHOW-DATA-TYPES + +### Name + +SHOW DATA TYPES + +### Description + + This statement is used to view all supported data types. + + grammar: + ```sql + SHOW DATA TYPES; + ``` + +### Example + + 1. Display all supported data types + + SHOW DATA TYPES; + +### Keywords + + SHOW,DATA,TYPES + +### Best Practice diff --git a/docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md b/docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md new file mode 100644 index 0000000000..3e18ed6e95 --- /dev/null +++ b/docs/zh-CN/docs/sql-manual/sql-reference/Show-Statements/SHOW-DATA-TYPES.md @@ -0,0 +1,52 @@ +--- +{ + "title": "SHOW-DATA-TYPES", + "language": "zh-CN" +} +--- + + + +## SHOW-DATA-TYPES + +### Name + +SHOW DATA TYPES + +### Description + + 该语句用于查看DORIS支持的所有数据类型。 + + 语法: + ```sql + SHOW DATA TYPES; + ``` + +### Example + + 1. 查看Doris支持的所有数据类型 + + SHOW DATA TYPES; + +### Keywords + + SHOW,DATA,TYPES + +### Best Practice diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index 3df587db3d..674530fb37 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -3672,6 +3672,11 @@ show_param ::= {: RESULT = new ShowDbIdStmt(dbId); :} + /* show all data types */ + | KW_DATA KW_TYPES + {: + RESULT = new ShowDataTypesStmt(); + :} | KW_SCHEMAS opt_wild_where {: RESULT = new ShowDbStmt(parser.wild, parser.where); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowDataTypesStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowDataTypesStmt.java new file mode 100644 index 0000000000..ecdc47a5cf --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowDataTypesStmt.java @@ -0,0 +1,75 @@ +// 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.analysis; + +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.PrimitiveType; +import org.apache.doris.catalog.ScalarType; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.UserException; +import org.apache.doris.qe.ShowResultSetMetaData; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class ShowDataTypesStmt extends ShowStmt { + + private static final ShowResultSetMetaData META_DATA = + ShowResultSetMetaData.builder() + .addColumn(new Column("TypeName", ScalarType.createVarchar(20))) + .addColumn(new Column("Size", ScalarType.createVarchar(20))) + .build(); + + public static ArrayList getTypes() { + return PrimitiveType.getSupportedTypes(); + } + + @Override + public void analyze(Analyzer analyzer) throws AnalysisException, UserException { + super.analyze(analyzer); + } + + @Override + public String toSql() { + StringBuilder sb = new StringBuilder(); + sb.append("SHOW "); + sb.append("DATA TYPES"); + return sb.toString(); + } + + @Override + public String toString() { + return toSql(); + } + + public void sortMetaData(List> rows) { + Collections.sort(rows, new Comparator>() { + @Override + public int compare(List row1, List row2) { + return row1.get(0).compareTo(row2.get(0)); + } + }); + } + + @Override + public ShowResultSetMetaData getMetaData() { + return META_DATA; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java index af2c235466..b8a7d28bb6 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java @@ -47,6 +47,7 @@ import org.apache.doris.analysis.ShowCreateRoutineLoadStmt; import org.apache.doris.analysis.ShowCreateTableStmt; import org.apache.doris.analysis.ShowDataSkewStmt; import org.apache.doris.analysis.ShowDataStmt; +import org.apache.doris.analysis.ShowDataTypesStmt; import org.apache.doris.analysis.ShowDbIdStmt; import org.apache.doris.analysis.ShowDbStmt; import org.apache.doris.analysis.ShowDeleteStmt; @@ -270,6 +271,8 @@ public class ShowExecutor { handleShowEngines(); } else if (stmt instanceof ShowFunctionsStmt) { handleShowFunctions(); + } else if (stmt instanceof ShowDataTypesStmt) { + handleShowDataTypes(); } else if (stmt instanceof ShowCreateFunctionStmt) { handleShowCreateFunction(); } else if (stmt instanceof ShowEncryptKeysStmt) { @@ -469,6 +472,20 @@ public class ShowExecutor { resultSet = new ShowResultSet(showMetaData, resultRowSet); } + private void handleShowDataTypes() throws AnalysisException { + ShowDataTypesStmt showStmt = (ShowDataTypesStmt) stmt; + ArrayList supportedTypes = showStmt.getTypes(); + List> rows = Lists.newArrayList(); + for (PrimitiveType type : supportedTypes) { + List row = new ArrayList<>(); + row.add(type.toString()); + row.add(Integer.toString(type.getSlotSize())); + rows.add(row); + } + showStmt.sortMetaData(rows); + resultSet = new ShowResultSet(showStmt.getMetaData(), rows); + } + /*** * get resultRowSet by showFunctionsStmt * @param showStmt diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowDataTypesStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowDataTypesStmtTest.java new file mode 100644 index 0000000000..197f5cc723 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowDataTypesStmtTest.java @@ -0,0 +1,48 @@ +// 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.analysis; + +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.UserException; +import org.apache.doris.mysql.privilege.MockedAuth; +import org.apache.doris.qe.ConnectContext; + +import mockit.Mocked; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class ShowDataTypesStmtTest { + private Analyzer analyzer; + + @Mocked + private ConnectContext ctx; + + @Before + public void setUp() { + analyzer = AccessTestUtil.fetchAdminAnalyzer(true); + MockedAuth.mockedConnectContext(ctx, "root", "192.168.1.1"); + } + + @Test + public void testNormal() throws UserException, AnalysisException { + ShowDataTypesStmt stmt = new ShowDataTypesStmt(); + stmt.analyze(analyzer); + Assert.assertEquals("SHOW DATA TYPES", stmt.toString()); + } +} diff --git a/regression-test/data/types/test_show_data_types.out b/regression-test/data/types/test_show_data_types.out new file mode 100644 index 0000000000..136a99ceaf --- /dev/null +++ b/regression-test/data/types/test_show_data_types.out @@ -0,0 +1,32 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !sql -- +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 +JSONB 16 +LARGEINT 16 +MAP 24 +NULL_TYPE 1 +QUANTILE_STATE 16 +SMALLINT 2 +STRING 16 +TIME 8 +TIMEV2 8 +TINYINT 1 +VARCHAR 16 +VARIANT 24 + diff --git a/regression-test/suites/types/test_show_data_types.groovy b/regression-test/suites/types/test_show_data_types.groovy new file mode 100644 index 0000000000..41312d2a74 --- /dev/null +++ b/regression-test/suites/types/test_show_data_types.groovy @@ -0,0 +1,22 @@ +// 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", "types") { + + qt_sql """show data types""" + +}