//===----------------------------------------------------------------------===// // DuckDB // // duckdb/main/relation.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/common/common.hpp" #include "duckdb/common/enums/join_type.hpp" #include "duckdb/common/enums/relation_type.hpp" #include "duckdb/common/winapi.hpp" #include "duckdb/main/query_result.hpp" #include "duckdb/parser/column_definition.hpp" #include "duckdb/common/named_parameter_map.hpp" #include "duckdb/main/client_context.hpp" #include "duckdb/main/external_dependencies.hpp" #include "duckdb/parser/statement/explain_statement.hpp" #include namespace duckdb { struct BoundStatement; class ClientContextWrapper; class Binder; class LogicalOperator; class QueryNode; class TableRef; class Relation : public std::enable_shared_from_this { public: Relation(const std::shared_ptr &context, RelationType type) : context(context), type(type) { } Relation(ClientContextWrapper &context, RelationType type) : context(context.GetContext()), type(type) { } virtual ~Relation() { } ClientContextWrapper context; RelationType type; shared_ptr extra_dependencies; public: DUCKDB_API virtual const vector &Columns() = 0; DUCKDB_API virtual unique_ptr GetQueryNode(); DUCKDB_API virtual BoundStatement Bind(Binder &binder); DUCKDB_API virtual string GetAlias(); DUCKDB_API unique_ptr ExecuteOrThrow(); DUCKDB_API unique_ptr Execute(); DUCKDB_API string ToString(); DUCKDB_API virtual string ToString(idx_t depth) = 0; DUCKDB_API void Print(); DUCKDB_API void Head(idx_t limit = 10); DUCKDB_API shared_ptr CreateView(const string &name, bool replace = true, bool temporary = false); DUCKDB_API shared_ptr CreateView(const string &schema_name, const string &name, bool replace = true, bool temporary = false); DUCKDB_API unique_ptr Query(const string &sql); DUCKDB_API unique_ptr Query(const string &name, const string &sql); //! Explain the query plan of this relation DUCKDB_API unique_ptr Explain(ExplainType type = ExplainType::EXPLAIN_STANDARD); DUCKDB_API virtual unique_ptr GetTableRef(); virtual bool IsReadOnly() { return true; } public: // PROJECT DUCKDB_API shared_ptr Project(const string &select_list); DUCKDB_API shared_ptr Project(const string &expression, const string &alias); DUCKDB_API shared_ptr Project(const string &select_list, const vector &aliases); DUCKDB_API shared_ptr Project(const vector &expressions); DUCKDB_API shared_ptr Project(const vector &expressions, const vector &aliases); // FILTER DUCKDB_API shared_ptr Filter(const string &expression); DUCKDB_API shared_ptr Filter(const vector &expressions); // LIMIT DUCKDB_API shared_ptr Limit(int64_t n, int64_t offset = 0); // ORDER DUCKDB_API shared_ptr Order(const string &expression); DUCKDB_API shared_ptr Order(const vector &expressions); // JOIN operation DUCKDB_API shared_ptr Join(const shared_ptr &other, const string &condition, JoinType type = JoinType::INNER); // CROSS PRODUCT operation DUCKDB_API shared_ptr CrossProduct(const shared_ptr &other); // SET operations DUCKDB_API shared_ptr Union(const shared_ptr &other); DUCKDB_API shared_ptr Except(const shared_ptr &other); DUCKDB_API shared_ptr Intersect(const shared_ptr &other); // DISTINCT operation DUCKDB_API shared_ptr Distinct(); // AGGREGATES DUCKDB_API shared_ptr Aggregate(const string &aggregate_list); DUCKDB_API shared_ptr Aggregate(const vector &aggregates); DUCKDB_API shared_ptr Aggregate(const string &aggregate_list, const string &group_list); DUCKDB_API shared_ptr Aggregate(const vector &aggregates, const vector &groups); // ALIAS DUCKDB_API shared_ptr Alias(const string &alias); //! Insert the data from this relation into a table DUCKDB_API shared_ptr InsertRel(const string &schema_name, const string &table_name); DUCKDB_API void Insert(const string &table_name); DUCKDB_API void Insert(const string &schema_name, const string &table_name); //! Insert a row (i.e.,list of values) into a table DUCKDB_API void Insert(const vector> &values); //! Create a table and insert the data from this relation into that table DUCKDB_API shared_ptr CreateRel(const string &schema_name, const string &table_name); DUCKDB_API void Create(const string &table_name); DUCKDB_API void Create(const string &schema_name, const string &table_name); //! Write a relation to a CSV file DUCKDB_API shared_ptr WriteCSVRel(const string &csv_file, case_insensitive_map_t> options = case_insensitive_map_t>()); DUCKDB_API void WriteCSV(const string &csv_file, case_insensitive_map_t> options = case_insensitive_map_t>()); //! Write a relation to a Parquet file DUCKDB_API shared_ptr WriteParquetRel(const string &parquet_file, case_insensitive_map_t> options = case_insensitive_map_t>()); DUCKDB_API void WriteParquet(const string &parquet_file, case_insensitive_map_t> options = case_insensitive_map_t>()); //! Update a table, can only be used on a TableRelation DUCKDB_API virtual void Update(const string &update, const string &condition = string()); //! Delete from a table, can only be used on a TableRelation DUCKDB_API virtual void Delete(const string &condition = string()); //! Create a relation from calling a table in/out function on the input relation //! Create a relation from calling a table in/out function on the input relation DUCKDB_API shared_ptr TableFunction(const std::string &fname, const vector &values); DUCKDB_API shared_ptr TableFunction(const std::string &fname, const vector &values, const named_parameter_map_t &named_parameters); public: //! Whether or not the relation inherits column bindings from its child or not, only relevant for binding virtual bool InheritsColumnBindings() { return false; } virtual Relation *ChildRelation() { return nullptr; } DUCKDB_API vector> GetAllDependencies(); protected: DUCKDB_API string RenderWhitespace(idx_t depth); public: template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); return reinterpret_cast(*this); } }; } // namespace duckdb