
目录
2.4 Serialize() 和 Deserialize()
在 DuckDB 的架构中,TableRef 是一个核心组件,用于表示查询中的表引用。它不仅是一个抽象基类,还通过多种子类实现支持了复杂多样的表引用场景。本文将从源码层面深入分析 TableRef 的设计思想及其子类的实现细节。
TableRef 的设计及实现
设计思想
TableRef 是一个抽象基类,用于表示查询中的表引用。它在解析 SQL 查询时被定义,并在后续的绑定和优化阶段中被进一步处理。TableRef 的设计思想主要体现在以下几个方面:
1)抽象性与扩展性
TableRef 是一个抽象类,定义了表引用的基本接口和行为。通过继承 TableRef,DuckDB 实现了多种具体的表引用类型,例如:
BaseTableRef:表示普通表引用。
SubqueryRef:表示子查询引用。
JoinRef:表示连接操作的表引用。
TableFunctionRef:表示通过表函数生成的表引用。EmptyTableRef:空表引用
这种设计使得 DuckDB 能够灵活地处理各种复杂的查询场景。
2)模块化与解耦
TableRef 的设计将表引用逻辑与查询解析、执行计划生成等模块分离。这种解耦使得 DuckDB 的代码更加模块化,便于维护和扩展。
3)支持多种数据源
通过 TableRef 的子类实现,DuckDB 能够支持多种数据源,包括本地表、内存表、外部表以及通过表函数生成的虚拟表。
实现剖析
TableRef是个多态类,通过继承和多态机制支持多种表引用类型(如普通表、子查询、表函数等)。以下是对 TableRef 类及其设计思想的详细分析:
1)类的定义与结构
namespace duckdb {
//! Represents a generic expression that returns a table.
class TableRef {
public:
static constexpr const TableReferenceType TYPE = TableReferenceType::INVALID;
public:
explicit TableRef(TableReferenceType type) : type(type) {
}
virtual ~TableRef() {
}
TableReferenceType type;
string alias;
//! Sample options (if any)
unique_ptr<SampleOptions> sample;
//! The location in the query (if any)
optional_idx query_location;
//! External dependencies of this table function
shared_ptr<ExternalDependency> external_dependency;
//! Aliases for the column names
vector<string> column_name_alias;
public:
//! Convert the object to a string
virtual string ToString() const = 0;
string BaseToString(string result) const;
string BaseToString(string result, const vector<string> &column_name_alias) const;
void Print();
virtual bool Equals(const TableRef &other) const;
static bool Equals(const unique_ptr<TableRef> &left, const unique_ptr<TableRef> &right);
virtual unique_ptr<TableRef> Copy() = 0;
//! Copy the properties of this table ref to the target
void CopyProperties(TableRef &target) const;
virtual void Serialize(Serializer &serializer) const;
static unique_ptr<TableRef> Deserialize(Deserializer &deserializer);
public:
template <class TARGET>
TARGET &Cast() {
if (type != TARGET::TYPE && TARGET::TYPE != TableReferenceType::INVALID) {
throw InternalException("Failed to cast constraint to type - constraint type mismatch");
}
return reinterpret_cast<TARGET &>(*this);
}
template <class TARGET>
const TARGET &Cast() const {
if (type != TARGET::TYPE && TARGET::TYPE != TableReferenceType::INVALID) {
throw InternalException("Failed to cast constraint to type - constraint type mismatch");
}
return reinterpret_cast<const TARGET &>(*this);
}
};
} // namespace duckdb
1.1 构造函数与析构函数
-
TableRef的构造函数接受一个TableReferenceType参数,用于初始化type属性。TableReferenceType是一个枚举类型,用于区分不同的表引用类型(如普通表、子查询、表函数等)。 -
析构函数是虚函数,这表明
TableRef是一个多态类,允许派生类覆盖析构函数。
1.2 成员变量
-
代码中实际支持的表类型很多:TableReferenceType type:表示表引用的类型enum class TableReferenceType : uint8_t { INVALID = 0, // invalid table reference type BASE_TABLE = 1, // base table reference SUBQUERY = 2, // output of a subquery JOIN = 3, // output of join TABLE_FUNCTION = 5, // table producing function EXPRESSION_LIST = 6, // expression list CTE = 7, // Recursive CTE EMPTY_FROM = 8, // placeholder for empty FROM PIVOT = 9, // pivot statement SHOW_REF = 10, // SHOW statement COLUMN_DATA = 11, // column

最低0.47元/天 解锁文章
1897

被折叠的 条评论
为什么被折叠?



