2020-02-28 15:23:32 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Core/NamesAndTypes.h>
|
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2020-03-08 11:07:05 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
|
2020-05-20 20:16:32 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/StorageID.h>
|
2020-02-28 15:23:32 +00:00
|
|
|
#include <Storages/IStorage_fwd.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class ASTSelectQuery;
|
2020-04-08 18:59:52 +00:00
|
|
|
class TableJoin;
|
2020-03-08 11:07:05 +00:00
|
|
|
struct SelectQueryOptions;
|
2020-06-17 16:39:58 +00:00
|
|
|
struct StorageInMemoryMetadata;
|
|
|
|
using StorageMetadataPtr = std::shared_ptr<StorageInMemoryMetadata>;
|
2020-02-28 15:23:32 +00:00
|
|
|
|
|
|
|
/// Joined tables' columns resolver.
|
|
|
|
/// We want to get each table structure at most once per table occurance. Or even better once per table.
|
|
|
|
/// TODO: joins tree with costs to change joins order by CBO.
|
|
|
|
class JoinedTables
|
|
|
|
{
|
|
|
|
public:
|
2020-03-08 11:07:05 +00:00
|
|
|
JoinedTables(Context && contex, const ASTSelectQuery & select_query);
|
2020-02-28 15:23:32 +00:00
|
|
|
|
2020-03-02 19:39:39 +00:00
|
|
|
void reset(const ASTSelectQuery & select_query)
|
|
|
|
{
|
2020-03-08 11:07:05 +00:00
|
|
|
*this = JoinedTables(std::move(context), select_query);
|
2020-03-02 19:39:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 11:07:05 +00:00
|
|
|
StoragePtr getLeftTableStorage();
|
|
|
|
bool resolveTables();
|
2020-04-14 14:43:09 +00:00
|
|
|
|
|
|
|
/// Make fake tables_with_columns[0] in case we have predefined input in InterpreterSelectQuery
|
2020-06-17 16:39:58 +00:00
|
|
|
void makeFakeTable(StoragePtr storage, const StorageMetadataPtr & metadata_snapshot, const Block & source_header);
|
2020-04-08 18:59:52 +00:00
|
|
|
std::shared_ptr<TableJoin> makeTableJoin(const ASTSelectQuery & select_query);
|
2020-03-02 19:39:39 +00:00
|
|
|
|
2020-06-15 12:36:10 +00:00
|
|
|
const TablesWithColumns & tablesWithColumns() const { return tables_with_columns; }
|
|
|
|
TablesWithColumns moveTablesWithColumns() { return std::move(tables_with_columns); }
|
2020-03-02 19:39:39 +00:00
|
|
|
|
|
|
|
bool isLeftTableSubquery() const;
|
|
|
|
bool isLeftTableFunction() const;
|
2020-03-08 11:07:05 +00:00
|
|
|
size_t tablesCount() const { return table_expressions.size(); }
|
2020-03-02 19:39:39 +00:00
|
|
|
|
2020-03-12 12:16:16 +00:00
|
|
|
const StorageID & leftTableID() const { return table_id; }
|
2020-02-28 15:23:32 +00:00
|
|
|
|
2020-03-27 20:12:14 +00:00
|
|
|
void rewriteDistributedInAndJoins(ASTPtr & query);
|
|
|
|
|
2020-03-08 11:07:05 +00:00
|
|
|
std::unique_ptr<InterpreterSelectWithUnionQuery> makeLeftTableSubquery(const SelectQueryOptions & select_options);
|
|
|
|
|
2020-02-28 15:23:32 +00:00
|
|
|
private:
|
2020-03-08 11:07:05 +00:00
|
|
|
Context context;
|
2020-03-02 19:39:39 +00:00
|
|
|
std::vector<const ASTTableExpression *> table_expressions;
|
2020-06-15 12:36:10 +00:00
|
|
|
TablesWithColumns tables_with_columns;
|
2020-03-02 19:39:39 +00:00
|
|
|
|
|
|
|
/// Legacy (duplicated left table values)
|
|
|
|
ASTPtr left_table_expression;
|
|
|
|
std::optional<DatabaseAndTableWithAlias> left_db_and_table;
|
|
|
|
|
|
|
|
/// left_db_and_table or 'system.one'
|
2020-03-12 12:16:16 +00:00
|
|
|
StorageID table_id = StorageID::createEmpty();
|
2020-02-28 15:23:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|