2018-10-11 19:28:59 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
#include <Parsers/ASTLiteral.h>
|
|
|
|
#include <Parsers/ASTSubquery.h>
|
2019-01-16 17:26:14 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2018-10-11 19:28:59 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
|
|
|
#include <Parsers/ASTExpressionList.h>
|
|
|
|
|
|
|
|
#include <Interpreters/Context.h>
|
2019-10-23 13:59:03 +00:00
|
|
|
#include <Interpreters/misc.h>
|
2018-10-11 19:28:59 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
|
|
|
|
#include <Interpreters/ExecuteScalarSubqueriesVisitor.h>
|
2019-06-14 17:15:30 +00:00
|
|
|
#include <Interpreters/addTypeConversionToAST.h>
|
2018-10-11 19:28:59 +00:00
|
|
|
|
2019-05-17 14:34:25 +00:00
|
|
|
#include <DataStreams/IBlockInputStream.h>
|
2019-10-19 20:36:35 +00:00
|
|
|
#include <DataStreams/materializeBlock.h>
|
2018-12-18 18:43:06 +00:00
|
|
|
#include <DataTypes/DataTypeAggregateFunction.h>
|
2019-10-19 20:36:35 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2018-12-18 18:43:06 +00:00
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
#include <Columns/ColumnTuple.h>
|
2019-06-14 17:15:30 +00:00
|
|
|
|
2019-12-12 08:57:25 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2018-10-11 19:28:59 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int INCORRECT_RESULT_OF_SCALAR_SUBQUERY;
|
|
|
|
extern const int TOO_MANY_ROWS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-09 16:16:59 +00:00
|
|
|
bool ExecuteScalarSubqueriesMatcher::needChildVisit(ASTPtr & node, const ASTPtr & child)
|
2018-12-10 13:19:09 +00:00
|
|
|
{
|
|
|
|
/// Processed
|
2019-03-11 13:22:51 +00:00
|
|
|
if (node->as<ASTSubquery>() || node->as<ASTFunction>())
|
2018-12-10 13:19:09 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/// Don't descend into subqueries in FROM section
|
2019-03-11 13:22:51 +00:00
|
|
|
if (node->as<ASTTableExpression>())
|
2018-12-10 13:19:09 +00:00
|
|
|
return false;
|
|
|
|
|
2019-03-11 13:22:51 +00:00
|
|
|
if (node->as<ASTSelectQuery>())
|
2019-01-09 16:16:59 +00:00
|
|
|
{
|
|
|
|
/// Do not go to FROM, JOIN, UNION.
|
2019-03-11 13:22:51 +00:00
|
|
|
if (child->as<ASTTableExpression>() || child->as<ASTSelectQuery>())
|
2019-01-09 16:16:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:19:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
void ExecuteScalarSubqueriesMatcher::visit(ASTPtr & ast, Data & data)
|
2018-12-10 13:19:09 +00:00
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * t = ast->as<ASTSubquery>())
|
2018-12-10 13:19:09 +00:00
|
|
|
visit(*t, ast, data);
|
2019-03-11 13:22:51 +00:00
|
|
|
if (const auto * t = ast->as<ASTFunction>())
|
2019-02-22 13:33:56 +00:00
|
|
|
visit(*t, ast, data);
|
2018-12-10 13:19:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
/// Converting to literal values might take a fair amount of overhead when the value is large, (e.g.
|
|
|
|
/// Array, BitMap, etc.), This conversion is required for constant folding, index lookup, branch
|
|
|
|
/// elimination. However, these optimizations should never be related to large values, thus we
|
|
|
|
/// blacklist them here.
|
|
|
|
static bool worthConvertingToLiteral(const Block & scalar)
|
|
|
|
{
|
2020-04-22 06:01:33 +00:00
|
|
|
const auto * scalar_type_name = scalar.safeGetByPosition(0).type->getFamilyName();
|
2019-10-19 20:36:35 +00:00
|
|
|
std::set<String> useless_literal_types = {"Array", "Tuple", "AggregateFunction", "Function", "Set", "LowCardinality"};
|
|
|
|
return !useless_literal_types.count(scalar_type_name);
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:02:45 +00:00
|
|
|
void ExecuteScalarSubqueriesMatcher::visit(const ASTSubquery & subquery, ASTPtr & ast, Data & data)
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2019-10-19 20:36:35 +00:00
|
|
|
auto hash = subquery.getTreeHash();
|
|
|
|
auto scalar_query_hash_str = toString(hash.first) + "_" + toString(hash.second);
|
|
|
|
|
|
|
|
Block scalar;
|
|
|
|
if (data.context.hasQueryContext() && data.context.getQueryContext().hasScalar(scalar_query_hash_str))
|
|
|
|
scalar = data.context.getQueryContext().getScalar(scalar_query_hash_str);
|
|
|
|
else if (data.scalars.count(scalar_query_hash_str))
|
|
|
|
scalar = data.scalars[scalar_query_hash_str];
|
|
|
|
else
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2019-10-19 20:36:35 +00:00
|
|
|
Context subquery_context = data.context;
|
|
|
|
Settings subquery_settings = data.context.getSettings();
|
|
|
|
subquery_settings.max_result_rows = 1;
|
2020-03-08 23:48:08 +00:00
|
|
|
subquery_settings.extremes = false;
|
2019-10-19 20:36:35 +00:00
|
|
|
subquery_context.setSettings(subquery_settings);
|
|
|
|
|
|
|
|
ASTPtr subquery_select = subquery.children.at(0);
|
|
|
|
|
2020-04-30 17:07:34 +00:00
|
|
|
auto options = SelectQueryOptions(QueryProcessingStage::Complete, data.subquery_depth + 1);
|
|
|
|
options.analyze(data.only_analyze);
|
|
|
|
|
|
|
|
auto interpreter = InterpreterSelectWithUnionQuery(subquery_select, subquery_context, options);
|
2019-10-19 20:36:35 +00:00
|
|
|
Block block;
|
2020-04-30 17:07:34 +00:00
|
|
|
|
|
|
|
if (data.only_analyze)
|
|
|
|
{
|
|
|
|
/// If query is only analyzed, then constants are not correct.
|
|
|
|
block = interpreter.getSampleBlock();
|
|
|
|
for (auto & column : block)
|
|
|
|
if (column.column->empty())
|
|
|
|
column.column->cloneResized(1);
|
|
|
|
}
|
|
|
|
else
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2020-04-30 17:07:34 +00:00
|
|
|
BlockIO res = interpreter.execute();
|
2019-10-19 20:36:35 +00:00
|
|
|
|
2020-04-30 17:07:34 +00:00
|
|
|
try
|
2019-10-19 20:36:35 +00:00
|
|
|
{
|
2020-04-30 17:07:34 +00:00
|
|
|
block = res.in->read();
|
|
|
|
|
|
|
|
if (!block)
|
|
|
|
{
|
|
|
|
/// Interpret subquery with empty result as Null literal
|
|
|
|
auto ast_new = std::make_unique<ASTLiteral>(Null());
|
|
|
|
ast_new->setAlias(ast->tryGetAlias());
|
|
|
|
ast = std::move(ast_new);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block.rows() != 1 || res.in->read())
|
|
|
|
throw Exception("Scalar subquery returned more than one row", ErrorCodes::INCORRECT_RESULT_OF_SCALAR_SUBQUERY);
|
|
|
|
}
|
|
|
|
catch (const Exception & e)
|
|
|
|
{
|
|
|
|
if (e.code() == ErrorCodes::TOO_MANY_ROWS)
|
|
|
|
throw Exception("Scalar subquery returned more than one row", ErrorCodes::INCORRECT_RESULT_OF_SCALAR_SUBQUERY);
|
|
|
|
else
|
|
|
|
throw;
|
2019-10-19 20:36:35 +00:00
|
|
|
}
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
block = materializeBlock(block);
|
|
|
|
size_t columns = block.columns();
|
|
|
|
|
|
|
|
if (columns == 1)
|
|
|
|
scalar = block;
|
2018-10-11 19:28:59 +00:00
|
|
|
else
|
2019-10-19 20:36:35 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
ColumnWithTypeAndName ctn;
|
|
|
|
ctn.type = std::make_shared<DataTypeTuple>(block.getDataTypes());
|
|
|
|
ctn.column = ColumnTuple::create(block.getColumns());
|
|
|
|
scalar.insert(ctn);
|
|
|
|
}
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-19 20:36:35 +00:00
|
|
|
const Settings & settings = data.context.getSettingsRef();
|
|
|
|
|
|
|
|
// Always convert to literals when there is no query context.
|
2020-05-08 11:17:23 +00:00
|
|
|
if (data.only_analyze || !settings.enable_scalar_subquery_optimization || worthConvertingToLiteral(scalar) || !data.context.hasQueryContext())
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
2019-10-19 20:36:35 +00:00
|
|
|
auto lit = std::make_unique<ASTLiteral>((*scalar.safeGetByPosition(0).column)[0]);
|
2018-11-01 17:07:20 +00:00
|
|
|
lit->alias = subquery.alias;
|
|
|
|
lit->prefer_alias_to_column_name = subquery.prefer_alias_to_column_name;
|
2019-10-19 20:36:35 +00:00
|
|
|
ast = addTypeConversionToAST(std::move(lit), scalar.safeGetByPosition(0).type->getName());
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-19 20:36:35 +00:00
|
|
|
auto func = makeASTFunction("__getScalar", std::make_shared<ASTLiteral>(scalar_query_hash_str));
|
|
|
|
func->alias = subquery.alias;
|
|
|
|
func->prefer_alias_to_column_name = subquery.prefer_alias_to_column_name;
|
|
|
|
ast = std::move(func);
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
2019-10-19 20:36:35 +00:00
|
|
|
|
|
|
|
data.scalars[scalar_query_hash_str] = std::move(scalar);
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
void ExecuteScalarSubqueriesMatcher::visit(const ASTFunction & func, ASTPtr & ast, Data & data)
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
|
|
|
/// Don't descend into subqueries in arguments of IN operator.
|
|
|
|
/// But if an argument is not subquery, than deeper may be scalar subqueries and we need to descend in them.
|
|
|
|
|
2018-12-10 13:02:45 +00:00
|
|
|
std::vector<ASTPtr *> out;
|
2018-11-01 17:07:20 +00:00
|
|
|
if (functionIsInOrGlobalInOperator(func.name))
|
2018-10-11 19:28:59 +00:00
|
|
|
{
|
|
|
|
for (auto & child : ast->children)
|
|
|
|
{
|
2018-11-01 17:07:20 +00:00
|
|
|
if (child != func.arguments)
|
2018-12-10 13:02:45 +00:00
|
|
|
out.push_back(&child);
|
2018-10-11 19:28:59 +00:00
|
|
|
else
|
2018-11-01 17:07:20 +00:00
|
|
|
for (size_t i = 0, size = func.arguments->children.size(); i < size; ++i)
|
2019-03-11 13:22:51 +00:00
|
|
|
if (i != 1 || !func.arguments->children[i]->as<ASTSubquery>())
|
2018-12-10 13:02:45 +00:00
|
|
|
out.push_back(&func.arguments->children[i]);
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-12-10 13:02:45 +00:00
|
|
|
for (auto & child : ast->children)
|
|
|
|
out.push_back(&child);
|
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
for (ASTPtr * add_node : out)
|
2019-02-22 15:45:47 +00:00
|
|
|
Visitor(data).visit(*add_node);
|
2018-10-11 19:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|