2022-07-20 15:25:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Storages/IStorage_fwd.h>
|
|
|
|
#include <Storages/TableLockHolder.h>
|
|
|
|
#include <Storages/StorageSnapshot.h>
|
|
|
|
|
|
|
|
#include <Interpreters/Context_fwd.h>
|
|
|
|
#include <Interpreters/StorageID.h>
|
|
|
|
|
|
|
|
#include <Analyzer/IQueryTreeNode.h>
|
|
|
|
#include <Analyzer/ListNode.h>
|
2022-09-04 15:20:59 +00:00
|
|
|
#include <Analyzer/TableExpressionModifiers.h>
|
2022-07-20 15:25:24 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-09-06 15:25:52 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-07-20 15:25:24 +00:00
|
|
|
class ITableFunction;
|
|
|
|
using TableFunctionPtr = std::shared_ptr<ITableFunction>;
|
|
|
|
|
|
|
|
/** Table function node represents table function in query tree.
|
|
|
|
* Example: SELECT a FROM table_function(arguments...).
|
|
|
|
*
|
|
|
|
* In query tree table function arguments are represented by ListNode.
|
|
|
|
*
|
|
|
|
* Table function resolution must be done during query analysis pass.
|
|
|
|
*/
|
|
|
|
class TableFunctionNode;
|
|
|
|
using TableFunctionNodePtr = std::shared_ptr<TableFunctionNode>;
|
|
|
|
|
|
|
|
class TableFunctionNode : public IQueryTreeNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Construct table function node with table function name
|
|
|
|
explicit TableFunctionNode(String table_function_name);
|
|
|
|
|
|
|
|
/// Get table function name
|
|
|
|
const String & getTableFunctionName() const
|
|
|
|
{
|
|
|
|
return table_function_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get arguments
|
|
|
|
const ListNode & getArguments() const
|
|
|
|
{
|
|
|
|
return children[arguments_child_index]->as<const ListNode &>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get arguments
|
|
|
|
ListNode & getArguments()
|
|
|
|
{
|
|
|
|
return children[arguments_child_index]->as<ListNode &>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get arguments node
|
|
|
|
const QueryTreeNodePtr & getArgumentsNode() const
|
|
|
|
{
|
|
|
|
return children[arguments_child_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get arguments node
|
|
|
|
QueryTreeNodePtr & getArgumentsNode()
|
|
|
|
{
|
|
|
|
return children[arguments_child_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true, if table function is resolved, false otherwise
|
|
|
|
bool isResolved() const
|
|
|
|
{
|
|
|
|
return storage != nullptr && table_function != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get table function, returns nullptr if table function node is not resolved
|
|
|
|
const TableFunctionPtr & getTableFunction() const
|
|
|
|
{
|
|
|
|
return table_function;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get storage, returns nullptr if table function node is not resolved
|
|
|
|
const StoragePtr & getStorage() const
|
|
|
|
{
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
2022-08-25 14:19:35 +00:00
|
|
|
const StoragePtr & getStorageOrThrow() const
|
|
|
|
{
|
|
|
|
if (!storage)
|
2022-09-06 15:25:52 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function node is not resolved");
|
2022-08-25 14:19:35 +00:00
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
2022-07-20 15:25:24 +00:00
|
|
|
/// Resolve table function with table_function, storage and context
|
|
|
|
void resolve(TableFunctionPtr table_function_value, StoragePtr storage_value, ContextPtr context);
|
|
|
|
|
|
|
|
/// Get storage id, throws exception if function node is not resolved
|
|
|
|
const StorageID & getStorageID() const;
|
|
|
|
|
|
|
|
/// Get storage snapshot, throws exception if function node is not resolved
|
|
|
|
const StorageSnapshotPtr & getStorageSnapshot() const;
|
|
|
|
|
2022-09-04 15:20:59 +00:00
|
|
|
/// Return true if table function node has table expression modifiers, false otherwise
|
|
|
|
bool hasTableExpressionModifiers() const
|
|
|
|
{
|
|
|
|
return table_expression_modifiers.has_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get table expression modifiers
|
|
|
|
std::optional<TableExpressionModifiers> getTableExpressionModifiers() const
|
|
|
|
{
|
|
|
|
return table_expression_modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set table expression modifiers
|
|
|
|
void setTableExpressionModifiers(TableExpressionModifiers table_expression_modifiers_value)
|
|
|
|
{
|
|
|
|
table_expression_modifiers = std::move(table_expression_modifiers_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-07-20 15:25:24 +00:00
|
|
|
QueryTreeNodeType getNodeType() const override
|
|
|
|
{
|
|
|
|
return QueryTreeNodeType::TABLE_FUNCTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override;
|
|
|
|
|
|
|
|
void dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const override;
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
protected:
|
2022-07-20 15:25:24 +00:00
|
|
|
bool isEqualImpl(const IQueryTreeNode & rhs) const override;
|
|
|
|
|
|
|
|
void updateTreeHashImpl(HashState & state) const override;
|
|
|
|
|
|
|
|
QueryTreeNodePtr cloneImpl() const override;
|
|
|
|
|
2022-10-10 10:25:58 +00:00
|
|
|
ASTPtr toASTImpl() const override;
|
|
|
|
|
2022-07-20 15:25:24 +00:00
|
|
|
private:
|
|
|
|
String table_function_name;
|
|
|
|
TableFunctionPtr table_function;
|
|
|
|
StoragePtr storage;
|
|
|
|
StorageID storage_id;
|
|
|
|
StorageSnapshotPtr storage_snapshot;
|
2022-09-04 15:20:59 +00:00
|
|
|
std::optional<TableExpressionModifiers> table_expression_modifiers;
|
2022-10-07 10:44:28 +00:00
|
|
|
|
|
|
|
static constexpr size_t arguments_child_index = 0;
|
|
|
|
static constexpr size_t children_size = arguments_child_index + 1;
|
2022-07-20 15:25:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|