Skip to content
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
108 changes: 108 additions & 0 deletions docs/en/docs/admin-manual/http-actions/fe/query-schema-action.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
{
"title": "Query Schema Action",
"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.
-->

# Query Schema Action


## Request

```
POST /api/query_schema/<ns_name>/<db_name>
```

## Description

The Query Schema Action can return the table creation statement for the given SQL-related table. Can be used to test some query scenarios locally.

The API was released in version 1.2.

## Path parameters

* `<db_name>`

Specify the database name. This database will be considered as the default database for the current session, and will be used if the table name in SQL does not qualify the database name.

## Query parameters

None

## Request body

```
text/plain

sql
```

* "sql" field is the SQL string.

## Response

* Return value

```
CREATE TABLE `tbl1` (
`k1` int(11) NULL,
`k2` int(11) NULL
) ENGINE=OLAP
DUPLICATE KEY(`k1`, `k2`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`k1`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2",
"disable_auto_compaction" = "false"
);

CREATE TABLE `tbl2` (
`k1` int(11) NULL,
`k2` int(11) NULL
) ENGINE=OLAP
DUPLICATE KEY(`k1`, `k2`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`k1`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2",
"disable_auto_compaction" = "false"
);
```

## Example

1. Write the SQL in local file 1.sql

```
select tbl1.k2 from tbl1 join tbl2 on tbl1.k1 = tbl2.k1;
```

2. Use curl to get the table creation statement.

```
curl -X POST -H 'Content-Type: text/plain' -uroot: http://127.0.0.1:8030/api/query_schema/internal/db1 -d@1.sql
```
3 changes: 2 additions & 1 deletion docs/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,8 @@
"admin-manual/http-actions/fe/show-proc-action",
"admin-manual/http-actions/fe/check-decommission-action",
"admin-manual/http-actions/fe/health-action",
"admin-manual/http-actions/fe/check-storage-type-action"
"admin-manual/http-actions/fe/check-storage-type-action",
"admin-manual/http-actions/fe/query-schema-action"
]
},
"admin-manual/http-actions/restore-tablet",
Expand Down
107 changes: 107 additions & 0 deletions docs/zh-CN/docs/admin-manual/http-actions/fe/query-schema-action.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
{
"title": "Query Schema Action",
"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.
-->

# Query Schema Action


## Request

```
POST /api/query_schema/<ns_name>/<db_name>
```

## Description

Query Schema Action 可以返回给定的 SQL 有关的表的建表语句。可以用于本地测试一些查询场景。
该 API 在 1.2 版本中发布。

## Path parameters

* `<db_name>`

指定数据库名称。该数据库会被视为当前session的默认数据库,如果在 SQL 中的表名没有限定数据库名称的话,则使用该数据库。

## Query parameters


## Request body

```
text/plain

sql
```

* sql 字段为具体的 SQL

## Response

* 返回结果集

```
CREATE TABLE `tbl1` (
`k1` int(11) NULL,
`k2` int(11) NULL
) ENGINE=OLAP
DUPLICATE KEY(`k1`, `k2`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`k1`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2",
"disable_auto_compaction" = "false"
);

CREATE TABLE `tbl2` (
`k1` int(11) NULL,
`k2` int(11) NULL
) ENGINE=OLAP
DUPLICATE KEY(`k1`, `k2`)
COMMENT 'OLAP'
DISTRIBUTED BY HASH(`k1`) BUCKETS 3
PROPERTIES (
"replication_allocation" = "tag.location.default: 1",
"in_memory" = "false",
"storage_format" = "V2",
"disable_auto_compaction" = "false"
);
```

## Example

1. 在本地文件 1.sql 中写入 SQL

```
select tbl1.k2 from tbl1 join tbl2 on tbl1.k1 = tbl2.k1;
```

2. 使用 curl 命令获取建表语句

```
curl -X POST -H 'Content-Type: text/plain' -uroot: http://127.0.0.1:8030/api/query_schema/internal/db1 -d@1.sql
```
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public String getTbl() {
public void getTables(Analyzer analyzer, Map<Long, TableIf> tableMap, Set<String> parentViewNameSet)
throws AnalysisException {
// get dbs of statement
queryStmt.getTables(analyzer, tableMap, parentViewNameSet);
queryStmt.getTables(analyzer, false, tableMap, parentViewNameSet);
tblName.analyze(analyzer);
// disallow external catalog
Util.prohibitExternalCatalog(tblName.getCtl(), this.getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,17 @@ private Expr trySubstituteOrdinal(Expr expr, String errorPrefix,
}
if (pos > resultExprs.size()) {
throw new AnalysisException(
errorPrefix + ": ordinal exceeds number of items in select list: "
+ expr.toSql());
errorPrefix + ": ordinal exceeds number of items in select list: " + expr.toSql());
}

// Create copy to protect against accidentally shared state.
return resultExprs.get((int) pos - 1).clone();
}

public void getWithClauseTables(Analyzer analyzer, Map<Long, TableIf> tableMap, Set<String> parentViewNameSet)
throws AnalysisException {
public void getWithClauseTables(Analyzer analyzer, boolean expandView, Map<Long, TableIf> tableMap,
Set<String> parentViewNameSet) throws AnalysisException {
if (withClause != null) {
withClause.getTables(analyzer, tableMap, parentViewNameSet);
withClause.getTables(analyzer, expandView, tableMap, parentViewNameSet);
}
}

Expand Down Expand Up @@ -542,8 +541,8 @@ public boolean containAlias(Expr expr) {
// tmp in child stmt "(select siteid, citycode from tmp)" do not contain with_Clause
// so need to check is view name by parentViewNameSet.
// issue link: https://github.com/apache/doris/issues/4598
public abstract void getTables(Analyzer analyzer, Map<Long, TableIf> tables, Set<String> parentViewNameSet)
throws AnalysisException;
public abstract void getTables(Analyzer analyzer, boolean expandView, Map<Long, TableIf> tables,
Set<String> parentViewNameSet) throws AnalysisException;

// get TableRefs in this query, including physical TableRefs of this statement and
// nested statements of inline views and with_Clause.
Expand Down
29 changes: 17 additions & 12 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,18 @@ public ExprSubstitutionMap getBaseTblSmap() {
}

@Override
public void getTables(Analyzer analyzer, Map<Long, TableIf> tableMap, Set<String> parentViewNameSet)
throws AnalysisException {
getWithClauseTables(analyzer, tableMap, parentViewNameSet);
public void getTables(Analyzer analyzer, boolean expandView, Map<Long, TableIf> tableMap,
Set<String> parentViewNameSet) throws AnalysisException {
getWithClauseTables(analyzer, expandView, tableMap, parentViewNameSet);
for (TableRef tblRef : fromClause) {
if (tblRef instanceof InlineViewRef) {
// Inline view reference
QueryStmt inlineStmt = ((InlineViewRef) tblRef).getViewStmt();
inlineStmt.getTables(analyzer, tableMap, parentViewNameSet);
inlineStmt.getTables(analyzer, expandView, tableMap, parentViewNameSet);
} else if (tblRef instanceof TableValuedFunctionRef) {
TableValuedFunctionRef tblFuncRef = (TableValuedFunctionRef) tblRef;
tableMap.put(tblFuncRef.getTableFunction().getTable().getId(),
tblFuncRef.getTableFunction().getTable());
tblFuncRef.getTableFunction().getTable());
} else {
String dbName = tblRef.getName().getDb();
String tableName = tblRef.getName().getTbl();
Expand All @@ -320,14 +320,19 @@ public void getTables(Analyzer analyzer, Map<Long, TableIf> tableMap, Set<String
.getCatalogOrAnalysisException(tblRef.getName().getCtl()).getDbOrAnalysisException(dbName);
TableIf table = db.getTableOrAnalysisException(tableName);

// check auth
if (!Env.getCurrentEnv().getAuth()
.checkTblPriv(ConnectContext.get(), tblRef.getName(), PrivPredicate.SELECT)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "SELECT",
ConnectContext.get().getQualifiedUser(), ConnectContext.get().getRemoteIP(),
dbName + ": " + tableName);
if (expandView && (table instanceof View)) {
View view = (View) table;
view.getQueryStmt().getTables(analyzer, expandView, tableMap, parentViewNameSet);
} else {
// check auth
if (!Env.getCurrentEnv().getAuth()
.checkTblPriv(ConnectContext.get(), tblRef.getName(), PrivPredicate.SELECT)) {
ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "SELECT",
ConnectContext.get().getQualifiedUser(), ConnectContext.get().getRemoteIP(),
dbName + ": " + tableName);
}
tableMap.put(table.getId(), table);
}
tableMap.put(table.getId(), table);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ public List<Expr> getSetOpsResultExprs() {
}

@Override
public void getTables(Analyzer analyzer, Map<Long, TableIf> tableMap, Set<String> parentViewNameSet)
throws AnalysisException {
getWithClauseTables(analyzer, tableMap, parentViewNameSet);
public void getTables(Analyzer analyzer, boolean expandView, Map<Long, TableIf> tableMap,
Set<String> parentViewNameSet) throws AnalysisException {
getWithClauseTables(analyzer, expandView, tableMap, parentViewNameSet);
for (SetOperand op : operands) {
op.getQueryStmt().getTables(analyzer, tableMap, parentViewNameSet);
op.getQueryStmt().getTables(analyzer, expandView, tableMap, parentViewNameSet);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ private WithClause(WithClause other) {
Preconditions.checkNotNull(other);
views = Lists.newArrayList();
for (View view : other.views) {
views.add(new View(view.getName(), view.getQueryStmt().clone(),
view.getOriginalColLabels()));
views.add(new View(view.getName(), view.getQueryStmt().clone(), view.getOriginalColLabels()));
}
}

Expand All @@ -113,12 +112,12 @@ public void reset() {
}
}

public void getTables(Analyzer analyzer, Map<Long, TableIf> tableMap, Set<String> parentViewNameSet)
throws AnalysisException {
public void getTables(Analyzer analyzer, boolean expandView, Map<Long, TableIf> tableMap,
Set<String> parentViewNameSet) throws AnalysisException {
for (View view : views) {
QueryStmt stmt = view.getQueryStmt();
parentViewNameSet.add(view.getName());
stmt.getTables(analyzer, tableMap, parentViewNameSet);
stmt.getTables(analyzer, expandView, tableMap, parentViewNameSet);
}
}

Expand Down
Loading