Skip to content

Commit

Permalink
[FLINK-36050][table] Enhance SHOW VIEW syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Aug 19, 2024
1 parent e4abd06 commit 97f1b30
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 39 deletions.
11 changes: 9 additions & 2 deletions docs/content.zh/docs/dev/table/sql/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -913,10 +913,17 @@ SHOW PROCEDURES [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] (LIKE | I
## SHOW VIEWS

```sql
SHOW VIEWS
SHOW VIEWS [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] LIKE <sql_like_pattern> ]
```

展示当前 catalog 和当前 database 中所有的视图。
Show all views for an optionally specified database. If no database is specified then the views are returned from the current database. Additionally, the output of this statement may be filtered by an optional matching pattern.

**LIKE**
Show all views with given view name and optional `LIKE` clause, whose name is whether similar to the `<sql_like_pattern>`.

The syntax of sql pattern in `LIKE` clause is the same as that of `MySQL` dialect.
* `%` matches any number of characters, even zero characters, `\%` matches one `%` character.
* `_` matches exactly one character, `\_` matches one `_` character.

## SHOW CREATE VIEW

Expand Down
11 changes: 9 additions & 2 deletions docs/content/docs/dev/table/sql/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,10 +923,17 @@ The same behavior as `LIKE` but the SQL pattern is case-insensitive.
## SHOW VIEWS

```sql
SHOW VIEWS
SHOW VIEWS [ ( FROM | IN ) [catalog_name.]database_name ] [ [NOT] LIKE <sql_like_pattern> ]
```

Show all views in the current catalog and the current database.
Show all views for an optionally specified database. If no database is specified then the views are returned from the current database. Additionally, the output of this statement may be filtered by an optional matching pattern.

**LIKE**
Show all views with given view name and optional `LIKE` clause, whose name is whether similar to the `<sql_like_pattern>`.

The syntax of sql pattern in `LIKE` clause is the same as that of `MySQL` dialect.
* `%` matches any number of characters, even zero characters, `\%` matches one `%` character.
* `_` matches exactly one character, `\_` matches one `_` character.

## SHOW CREATE VIEW

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ show tables;
!ok

# ==========================================================================
# test enhanced show tables
# test enhanced show tables and views
# ==========================================================================

create catalog catalog1 with ('type'='generic_in_memory');
Expand All @@ -829,16 +829,31 @@ create view catalog1.db1.v_person as select * from catalog1.db1.person;
[INFO] Execute statement succeeded.
!info

create view catalog1.db1.v_address comment 'view comment' as select * from catalog1.db1.address;
[INFO] Execute statement succeeded.
!info

show tables from catalog1.db1;
+------------+
| table name |
+------------+
| address |
| dim |
| person |
| v_address |
| v_person |
+------------+
4 rows in set
5 rows in set
!ok

show views from catalog1.db1;
+-----------+
| view name |
+-----------+
| v_address |
| v_person |
+-----------+
2 rows in set
!ok

show tables from catalog1.db1 like '%person%';
Expand All @@ -851,14 +866,33 @@ show tables from catalog1.db1 like '%person%';
2 rows in set
!ok

show views from catalog1.db1 like '%person%';
+-----------+
| view name |
+-----------+
| v_person |
+-----------+
1 row in set
!ok

show tables in catalog1.db1 not like '%person%';
+------------+
| table name |
+------------+
| address |
| dim |
| v_address |
+------------+
2 rows in set
3 rows in set
!ok

show views in catalog1.db1 not like '%person%';
+-----------+
| view name |
+-----------+
| v_address |
+-----------+
1 row in set
!ok

use catalog catalog1;
Expand All @@ -873,3 +907,12 @@ show tables from db1 like 'p_r%';
+------------+
1 row in set
!ok

show views from db1 like '%p_r%';
+-----------+
| view name |
+-----------+
| v_person |
+-----------+
1 row in set
!ok
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ show tables;
!ok

# ==========================================================================
# test enhanced show tables
# test enhanced show tables and views
# ==========================================================================

create catalog catalog1 with ('type'='generic_in_memory');
Expand Down Expand Up @@ -991,6 +991,16 @@ create view catalog1.db1.v_person as select * from catalog1.db1.person;
1 row in set
!ok

create view catalog1.db1.v_address comment 'view comment' as select * from catalog1.db1.address;
!output
+--------+
| result |
+--------+
| OK |
+--------+
1 row in set
!ok

show tables from catalog1.db1;
!output
+------------+
Expand All @@ -999,9 +1009,21 @@ show tables from catalog1.db1;
| address |
| dim |
| person |
| v_address |
| v_person |
+------------+
4 rows in set
5 rows in set
!ok

show views from catalog1.db1;
!output
+-----------+
| view name |
+-----------+
| v_address |
| v_person |
+-----------+
2 rows in set
!ok

show tables from catalog1.db1 like '%person%';
Expand All @@ -1015,15 +1037,36 @@ show tables from catalog1.db1 like '%person%';
2 rows in set
!ok

show views from catalog1.db1 like '%person%';
!output
+-----------+
| view name |
+-----------+
| v_person |
+-----------+
1 row in set
!ok

show tables in catalog1.db1 not like '%person%';
!output
+------------+
| table name |
+------------+
| address |
| dim |
| v_address |
+------------+
2 rows in set
3 rows in set
!ok

show views in catalog1.db1 not like '%person%';
!output
+-----------+
| view name |
+-----------+
| v_address |
+-----------+
1 row in set
!ok

use catalog catalog1;
Expand All @@ -1045,3 +1088,13 @@ show tables from db1 like 'p_r%';
+------------+
1 row in set
!ok

show views from db1 like '%p_r%';
!output
+-----------+
| view name |
+-----------+
| v_person |
+-----------+
1 row in set
!ok
Original file line number Diff line number Diff line change
Expand Up @@ -626,16 +626,39 @@ SqlShowProcedures SqlShowProcedures() :
}

/**
* Parse a "Show Views" metadata query command.
* SHOW VIEWS FROM [catalog.] database sql call.
*/
SqlShowViews SqlShowViews() :
{
SqlIdentifier databaseName = null;
SqlCharStringLiteral likeLiteral = null;
String prep = null;
boolean notLike = false;
SqlParserPos pos;
}
{
<SHOW> <VIEWS> { pos = getPos(); }
<SHOW> <VIEWS>
{ pos = getPos(); }
[
( <FROM> { prep = "FROM"; } | <IN> { prep = "IN"; } )
{ pos = getPos(); }
databaseName = CompoundIdentifier()
]
[
[
<NOT>
{
notLike = true;
}
]
<LIKE> <QUOTED_STRING>
{
String likeCondition = SqlParserUtil.parseString(token.image);
likeLiteral = SqlLiteral.createCharString(likeCondition, getPos());
}
]
{
return new SqlShowViews(pos);
return new SqlShowViews(pos, prep, databaseName, notLike, likeLiteral);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ public List<SqlNode> getOperandList() {
: Collections.singletonList(databaseName);
}

public String[] fullDatabaseName() {
return Objects.isNull(this.databaseName)
? new String[] {}
: databaseName.names.toArray(new String[0]);
public List<String> fullDatabaseName() {
return Objects.isNull(this.databaseName) ? Collections.emptyList() : databaseName.names;
}

@Override
Expand All @@ -116,11 +114,8 @@ public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
databaseName.unparse(writer, leftPrec, rightPrec);
}
if (isWithLike()) {
if (isNotLike()) {
writer.keyword(String.format("NOT LIKE '%s'", getLikeSqlPattern()));
} else {
writer.keyword(String.format("LIKE '%s'", getLikeSqlPattern()));
}
final String prefix = isNotLike() ? "NOT " : "";
writer.keyword(String.format("%sLIKE '%s'", prefix, getLikeSqlPattern()));
}
}
}
Loading

0 comments on commit 97f1b30

Please sign in to comment.