-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support (EXPLAIN SELECT ...) as a subquery (ported from clickhouse #4…
…0630) (#5857) (#826) * Support (EXPLAIN SELECT ...) as a subquery (ported from clickhouse) * minor adjustments and changes * change stateless test file to pass the tests * adjust format and add extra info * small adjustments of format
- Loading branch information
1 parent
eca386e
commit 75a150a
Showing
9 changed files
with
258 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include <Interpreters/InterpreterSelectWithUnionQuery.h> | ||
#include <Parsers/ASTFunction.h> | ||
#include <Parsers/ASTSelectWithUnionQuery.h> | ||
#include <Parsers/queryToString.h> | ||
#include <Storages/StorageValues.h> | ||
#include <TableFunctions/ITableFunction.h> | ||
#include <TableFunctions/TableFunctionFactory.h> | ||
#include <TableFunctions/TableFunctionExplain.h> | ||
#include <TableFunctions/registerTableFunctions.h> | ||
#include <Processors/Executors/PullingPipelineExecutor.h> | ||
|
||
namespace DB | ||
{ | ||
namespace ErrorCodes | ||
{ | ||
extern const int LOGICAL_ERROR; | ||
extern const int BAD_ARGUMENTS; | ||
} | ||
|
||
void TableFunctionExplain::parseArguments(const ASTPtr & ast_function, ContextPtr /*context*/) | ||
{ | ||
const auto * function = ast_function->as<ASTFunction>(); | ||
if (function && function->arguments && function->arguments->children.size() == 1) | ||
{ | ||
const auto & query_arg = function->arguments->children[0]; | ||
|
||
if (!query_arg->as<ASTExplainQuery>()) | ||
throw Exception(ErrorCodes::BAD_ARGUMENTS, | ||
"Table function '{}' requires a explain query argument, got '{}'", | ||
getName(), queryToString(query_arg)); | ||
|
||
query = query_arg; | ||
} | ||
else | ||
{ | ||
throw Exception(ErrorCodes::BAD_ARGUMENTS, | ||
"Table function '{}' cannot be called directly, use `SELECT * FROM (EXPLAIN ...)` syntax", getName()); | ||
} | ||
} | ||
|
||
ColumnsDescription TableFunctionExplain::getActualTableStructure(ContextPtr context) const | ||
{ | ||
Block sample_block = getInterpreter(context).getSampleBlock(query->as<ASTExplainQuery>()->getKind()); | ||
ColumnsDescription columns_description; | ||
for (const auto & column : sample_block.getColumnsWithTypeAndName()) | ||
columns_description.add(ColumnDescription(column.name, column.type)); | ||
return columns_description; | ||
} | ||
|
||
static Block executeMonoBlock(QueryPipeline & pipeline) | ||
{ | ||
if (!pipeline.pulling()) | ||
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected pulling pipeline"); | ||
|
||
PullingPipelineExecutor pulling_executor(pipeline); | ||
std::vector<Block> blocks; | ||
while (true) | ||
{ | ||
Block block; | ||
if (pulling_executor.pull(block)) | ||
blocks.push_back(std::move(block)); | ||
else | ||
break; | ||
} | ||
|
||
if (blocks.size() == 1) | ||
return blocks[0]; | ||
|
||
return concatenateBlocks(blocks); | ||
} | ||
|
||
StoragePtr TableFunctionExplain::executeImpl( | ||
const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/) const | ||
{ | ||
BlockIO blockio = getInterpreter(context).execute(); | ||
Block block = executeMonoBlock(blockio.pipeline); | ||
|
||
StorageID storage_id(getDatabaseName(), table_name); | ||
/// proton: start cannot directly make shared DataTypePtr and StorageValues::create is the only way to do it | ||
auto storage = StorageValues::create(storage_id,getActualTableStructure(context),std::move(block)); | ||
/// proton: end | ||
storage->startup(); | ||
return storage; | ||
} | ||
|
||
InterpreterExplainQuery TableFunctionExplain::getInterpreter(ContextPtr context) const | ||
{ | ||
if (!query) | ||
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function '{}' requires a explain query argument", getName()); | ||
|
||
return InterpreterExplainQuery(query, context); | ||
} | ||
|
||
void registerTableFunctionExplain(TableFunctionFactory & factory) | ||
{ | ||
factory.registerFunction<TableFunctionExplain>({R"( | ||
Returns result of EXPLAIN query. | ||
The function should not be called directly but can be invoked via `SELECT * FROM (EXPLAIN <query>)`. | ||
You can use this query to process the result of EXPLAIN further using SQL (e.g., in tests). | ||
Example: | ||
[example:1] | ||
)", | ||
{{"1", "SELECT explain FROM (EXPLAIN AST SELECT * FROM system.numbers) WHERE explain LIKE '%Asterisk%'"}} | ||
}); | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <TableFunctions/ITableFunction.h> | ||
#include <Parsers/ASTExplainQuery.h> | ||
#include <Interpreters/InterpreterExplainQuery.h> | ||
#include <base/types.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
|
||
class TableFunctionExplain : public ITableFunction | ||
{ | ||
public: | ||
static constexpr auto name = "viewExplain"; | ||
std::string getName() const override { return name; } | ||
|
||
private: | ||
StoragePtr executeImpl(const ASTPtr & ast_function, ContextPtr context, const String & table_name, ColumnsDescription cached_columns) const override; | ||
const char * getStorageTypeName() const override { return "Explain"; } | ||
|
||
void parseArguments(const ASTPtr & ast_function, ContextPtr context) override; | ||
ColumnsDescription getActualTableStructure(ContextPtr context) const override; | ||
|
||
InterpreterExplainQuery getInterpreter(ContextPtr context) const; | ||
|
||
ASTPtr query = nullptr; | ||
}; | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/queries_ported/0_stateless/02421_explain_subquery.reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true | ||
true |
24 changes: 24 additions & 0 deletions
24
tests/queries_ported/0_stateless/02421_explain_subquery.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
SELECT count() > 3 FROM (EXPLAIN PIPELINE header = 1 SELECT * FROM system.numbers ORDER BY number DESC) WHERE explain LIKE '%Header: number uint64%'; | ||
SELECT count() > 0 FROM (EXPLAIN PLAN SELECT * FROM system.numbers ORDER BY number DESC) WHERE explain ILIKE '%Sort%'; | ||
SELECT count() > 0 FROM (EXPLAIN SELECT * FROM system.numbers ORDER BY number DESC) WHERE explain ILIKE '%Sort%'; | ||
SELECT count() > 0 FROM (EXPLAIN CURRENT TRANSACTION); | ||
SELECT count() == 1 FROM (EXPLAIN SYNTAX SELECT number FROM system.numbers ORDER BY number DESC) WHERE explain ILIKE 'SELECT%'; | ||
SELECT trim(explain) == 'Asterisk' FROM (EXPLAIN AST SELECT * FROM system.numbers LIMIT 10) WHERE explain LIKE '%Asterisk%'; | ||
|
||
SELECT * FROM ( | ||
EXPLAIN AST SELECT * FROM ( | ||
EXPLAIN PLAN SELECT * FROM ( | ||
EXPLAIN SYNTAX SELECT trim(explain) == 'Asterisk' FROM ( | ||
EXPLAIN AST SELECT * FROM system.numbers LIMIT 10 | ||
) WHERE explain LIKE '%Asterisk%' | ||
) | ||
) | ||
) FORMAT Null; | ||
|
||
CREATE STREAM t1 ( a uint64 ) AS SELECT number AS a,now64() as _tp_time FROM system.numbers LIMIT 10000; | ||
SELECT * FROM (SELECT sleep(3) AS n) AS a JOIN (SELECT 1 + sleep(3) AS f) AS b ON a.n == b.f; --- sleep to make sure that the stream t1 finishes being created and inserted into | ||
SELECT rows > 1000 FROM (EXPLAIN ESTIMATE SELECT sum(a) FROM table(t1)); | ||
SELECT count() == 1 FROM (EXPLAIN ESTIMATE SELECT sum(a) FROM table(t1)); | ||
|
||
DROP STREAM IF EXISTS t1; | ||
|