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

[branch-1.2](chore)support statement to show views from database #33510

Merged
merged 4 commits into from
Apr 11, 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
@@ -0,0 +1,78 @@
---
{
"title": "SHOW-VIEWS",
"language": "en"
}
---

<!--
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.
-->

## SHOW-VIEWS

### Name

SHOW VIEWS

### Description

This statement is used to display all logical views under the current db

grammar:

```sql
SHOW [FULL] VIEWS [LIKE]
````

illustrate:

1. LIKE: Fuzzy query can be performed according to the table name

### Example

1. Desplay all views under DB

```sql
MySQL [test]> show views;
+----------------+
| Tables_in_test |
+----------------+
| t1_view |
| t2_view |
+----------------+
2 rows in set (0.00 sec)
```

2. Fuzzy query by view name

```sql
MySQL [test]> show views like '%t1%';
+----------------+
| Tables_in_test |
+----------------+
| t1_view |
+----------------+
1 row in set (0.01 sec)
```

### Keywords

SHOW, VIEWS

### Best Practice
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
{
"title": "SHOW-VIEWS",
"language": "zh-CN"
}
---

<!--
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.
-->

## SHOW-VIEWS

### Name

SHOW VIEWS

### Description

该语句用于展示当前 db 下所有的 logical view

语法:

```sql
SHOW [FULL] VIEWS [LIKE] | [WHERE where_condition]
```

说明:

1. LIKE:可按照表名进行模糊查询

### Example

1. 查看DB下所有逻辑视图

```sql
MySQL [test]> show views;
+----------------+
| Tables_in_test |
+----------------+
| t1_view |
| t2_view |
+----------------+
2 rows in set (0.00 sec)
```

2. 按照VIEW名进行模糊查询

```sql
MySQL [test]> show views like '%t1%';
+----------------+
| Tables_in_test |
+----------------+
| t1_view |
+----------------+
1 row in set (0.01 sec)
```

### Keywords

SHOW, VIEWS

### Best Practice
14 changes: 14 additions & 0 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import org.apache.doris.catalog.ArrayType;
import org.apache.doris.catalog.MapType;
import org.apache.doris.catalog.StructField;
import org.apache.doris.catalog.StructType;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.catalog.View;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.FeConstants;
Expand Down Expand Up @@ -597,6 +598,7 @@ terminal String
KW_VERBOSE,
KW_VERSION,
KW_VIEW,
KW_VIEWS,
KW_WARNINGS,
KW_WEEK,
KW_WHEN,
Expand Down Expand Up @@ -3384,6 +3386,16 @@ show_param ::=
{:
RESULT = new ShowTableStmt(db, ctl, parser.isVerbose, parser.wild, parser.where);
:}
/* show views */
| opt_full KW_VIEWS opt_db:db opt_wild_where
{:
RESULT = new ShowTableStmt(db, null, parser.isVerbose, TableType.VIEW, parser.wild, parser.where);
:}
/* show views */
| opt_full KW_VIEWS from_or_in ident:ctl DOT ident:db opt_wild_where
{:
RESULT = new ShowTableStmt(db, ctl, parser.isVerbose, TableType.VIEW, parser.wild, parser.where);
:}
/* show table id */
| KW_TABLE INTEGER_LITERAL:tableId
{:
Expand Down Expand Up @@ -6852,6 +6864,8 @@ keyword ::=
{: RESULT = id; :}
| KW_VIEW:id
{: RESULT = id; :}
| KW_VIEWS:id
{: RESULT = id; :}
| KW_WARNINGS:id
{: RESULT = id; :}
| KW_WORK:id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.catalog.Column;
import org.apache.doris.catalog.InfoSchemaDb;
import org.apache.doris.catalog.ScalarType;
import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.cluster.ClusterNamespace;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.common.ErrorCode;
Expand All @@ -42,8 +43,9 @@ public class ShowTableStmt extends ShowStmt {
InfoSchemaDb.DATABASE_NAME, "tables");
private String db;
private String catalog;
private boolean isVerbose;
private String pattern;
private final boolean isVerbose;
private TableType type;
private final String pattern;
private Expr where;
private SelectStmt selectStmt;

Expand All @@ -63,6 +65,12 @@ public ShowTableStmt(String db, String catalog, boolean isVerbose, String patter
this.catalog = catalog;
}

public ShowTableStmt(String db, String catalog, boolean isVerbose, TableType type, String pattern,
Expr where) {
this(db, catalog, isVerbose, pattern, where);
this.type = type;
}

public String getDb() {
return db;
}
Expand All @@ -75,6 +83,10 @@ public boolean isVerbose() {
return isVerbose;
}

public TableType getType() {
return type;
}

public String getPattern() {
return pattern;
}
Expand Down Expand Up @@ -122,6 +134,11 @@ public SelectStmt toSelectStmt(Analyzer analyzer) throws AnalysisException {
selectList.addItem(item);
aliasMap.put(new SlotRef(null, TYPE_COL), item.getExpr().clone(null));
}
if (type != null) {
BinaryPredicate viewFilter = new BinaryPredicate(BinaryPredicate.Operator.EQ,
new SlotRef(TABLE_NAME, "ENGINE"), new StringLiteral(type.toEngineName()));
where = CompoundPredicate.createConjunction(viewFilter, where);
}
where = where.substitute(aliasMap);
selectStmt = new SelectStmt(selectList,
new FromClause(Lists.newArrayList(new TableRef(TABLE_NAME, null))),
Expand All @@ -139,7 +156,18 @@ public String toSql() {
if (isVerbose) {
sb.append(" FULL");
}
sb.append(" TABLES");
if (type != null) {
switch (type) {
// todo(only show views from now)
case VIEW:
sb.append(" VIEWS");
break;
default:
sb.append(" TABLES");
}
} else {
sb.append(" TABLES");
}
if (!Strings.isNullOrEmpty(db)) {
if (!Strings.isNullOrEmpty(catalog)) {
sb.append(" FROM ").append(catalog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,9 @@ private void handleShowTable() throws AnalysisException {
CaseSensibility.TABLE.getCaseSensibility());
}
for (TableIf tbl : db.getTables()) {
if (showTableStmt.getType() != null && tbl.getType() != showTableStmt.getType()) {
continue;
}
if (matcher != null && !matcher.match(tbl.getName())) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions fe/fe-core/src/main/jflex/sql_scanner.flex
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ import org.apache.doris.qe.SqlModeHelper;
keywordMap.put("verbose", new Integer(SqlParserSymbols.KW_VERBOSE));
keywordMap.put("version", new Integer(SqlParserSymbols.KW_VERSION));
keywordMap.put("view", new Integer(SqlParserSymbols.KW_VIEW));
keywordMap.put("views", new Integer(SqlParserSymbols.KW_VIEWS));
keywordMap.put("warnings", new Integer(SqlParserSymbols.KW_WARNINGS));
keywordMap.put("week", new Integer(SqlParserSymbols.KW_WEEK));
keywordMap.put("when", new Integer(SqlParserSymbols.KW_WHEN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.analysis;

import org.apache.doris.catalog.TableIf.TableType;
import org.apache.doris.common.AnalysisException;
import org.apache.doris.mysql.privilege.MockedAuth;
import org.apache.doris.mysql.privilege.PaloAuth;
Expand Down Expand Up @@ -68,6 +69,28 @@ public void testNormal() throws AnalysisException {
Assert.assertEquals("Table_type", stmt.getMetaData().getColumn(1).getName());
}

@Test
public void testShowViews() throws AnalysisException {
ShowTableStmt stmt = new ShowTableStmt("", null, false, TableType.VIEW,
null, null);
stmt.analyze(analyzer);
Assert.assertEquals("SHOW VIEWS FROM internal.testDb", stmt.toString());
Assert.assertEquals("testCluster:testDb", stmt.getDb());
Assert.assertEquals(TableType.VIEW, stmt.getType());
Assert.assertFalse(stmt.isVerbose());
Assert.assertEquals(1, stmt.getMetaData().getColumnCount());
Assert.assertEquals("Tables_in_testDb", stmt.getMetaData().getColumn(0).getName());

stmt = new ShowTableStmt("abc", null, true, TableType.VIEW, "bcd", null);
stmt.analyze(analyzer);
Assert.assertEquals("bcd", stmt.getPattern());
Assert.assertEquals("SHOW FULL VIEWS FROM internal.abc LIKE 'bcd'", stmt.toString());
Assert.assertEquals(3, stmt.getMetaData().getColumnCount());
Assert.assertEquals("Tables_in_abc", stmt.getMetaData().getColumn(0).getName());
Assert.assertEquals("Table_type", stmt.getMetaData().getColumn(1).getName());
Assert.assertEquals(TableType.VIEW, stmt.getType());
}

@Test(expected = AnalysisException.class)
public void testNoDb() throws AnalysisException {
ShowTableStmt stmt = new ShowTableStmt("", null, false, null);
Expand Down
10 changes: 10 additions & 0 deletions fe/fe-core/src/test/java/org/apache/doris/qe/ShowExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ public void testShowTable() throws AnalysisException {
Assert.assertFalse(resultSet.next());
}

@Test
public void testShowViews() throws AnalysisException {
ShowTableStmt stmt = new ShowTableStmt("testDb", null, false, TableType.VIEW,
null, null);
ShowExecutor executor = new ShowExecutor(ctx, stmt);
ShowResultSet resultSet = executor.execute();

Assert.assertFalse(resultSet.next());
}

@Test
public void testShowTableFromCatalog() throws AnalysisException {
ShowTableStmt stmt = new ShowTableStmt("testCluster:testDb", "internal", false, null);
Expand Down
Loading