8#include <unordered_map>
36 bool needs_grouping =
false;
40 std::reference_wrapper<Expr>
expr_;
46 result_t(
Expr &expr,
unsigned order) : expr_(expr), order(order) { }
48 : expr_(expr), order(order), alias(
std::move(alias))
52 const Expr &
expr()
const {
return expr_.get(); }
57 std::pair<std::reference_wrapper<Expr>,
unsigned>>;
61 using source_table = std::unordered_map<ThreadSafePooledString, std::pair<source_type, unsigned>>;
65 std::unordered_multimap<ThreadSafePooledString, result_t>
results;
67 std::unordered_multimap<ThreadSafePooledString, std::reference_wrapper<Expr>>
grouping_keys;
79 bool needs_context_ =
false;
84 , needs_context_(sema_.contexts_.empty())
87 sema_.push_context(stmt);
112 std::unique_ptr<DatabaseCommand> analyze(std::unique_ptr<ast::Command> ast);
114 using ASTExprVisitor::operator();
115 using ASTClauseVisitor::operator();
116 using ASTCommandVisitor::operator();
117#define DECLARE(CLASS) void operator()(CLASS&) override;
125 auto &ref = contexts_.emplace_back(
new SemaContext(stmt));
126 ref->
alias = std::move(alias);
130 auto ctx = *contexts_.back();
131 delete contexts_.back();
132 contexts_.pop_back();
137 return *contexts_.back();
141 return *contexts_.back();
145 bool is_nested()
const;
157 std::unique_ptr<Designator> create_designator(
const Expr &name,
const Expr &target,
bool drop_table_name =
false);
165 auto &C = Catalog::Get();
166 Token dot(pos, C.pool(
"."), TK_DOT);
167 Token table(pos, std::move(table_name), TK_IDENTIFIER);
168 Token attr(pos, std::move(attr_name), TK_IDENTIFIER);
169 auto d = std::make_unique<Designator>(std::move(dot), std::move(table), std::move(attr));
177 void replace_by_fresh_designator_to(std::unique_ptr<Expr> &to_replace,
const Expr &target);
189 void compose_of(std::unique_ptr<ast::Expr> &ptr,
const std::vector<std::reference_wrapper<ast::Expr>> components);
193 context_stack_t::reverse_iterator binding_ctx);
#define M_AST_COMMAND_LIST(X)
#define M_AST_CLAUSE_LIST(X)
bool is_composable_of(const ast::Expr &expr, const std::vector< std::reference_wrapper< const ast::Expr > > components)
Computes whether the bound parts of expr are composable of elements in components.
ThreadSafeStringPool::proxy_type ThreadSafePooledString
A data type representing a pooled (or internalized) object.
This class represents types in the SQL type system.
std::variant< std::monostate, const Expr *, const Attribute * > target_type
Helper class to create a context when one is required but does not yet exist.
RequireContext(Sema *sema, Stmt &stmt)
const Expr & expr() const
result_t(Expr &expr, unsigned order)
result_t(Expr &expr, unsigned order, ThreadSafePooledOptionalString alias)
ThreadSafePooledOptionalString alias
alias of the expression; may not have a value
unsigned order
the order of this result column in the result set
std::reference_wrapper< Expr > expr_
Holds context information used by semantic analysis of a single statement.
source_table sources
list of all sources along with their order
std::unordered_multimap< ThreadSafePooledString, std::pair< std::reference_wrapper< Expr >, unsigned > > named_expr_table
list of all computed expressions along with their order
std::unordered_multimap< ThreadSafePooledString, result_t > results
list of all results computed by this statement along with their order
std::variant< std::monostate, std::reference_wrapper< const Table >, named_expr_table > source_type
the type of a source of data: either a database table or a nested query with named results
Stmt & stmt
the statement that is currently being analyzed and for which this SemaContext is used
ThreadSafePooledOptionalString alias
if the statement that is being analyzed is a nested query, this is its alias in the outer statement
std::unordered_map< ThreadSafePooledString, std::pair< source_type, unsigned > > source_table
associative container mapping source name to data source and its order
std::unordered_multimap< ThreadSafePooledString, std::reference_wrapper< Expr > > grouping_keys
list of grouping keys
std::ostringstream oss
used to create textual representation of complex AST objects, e.g. expressions
const SemaContext & get_context() const
context_stack_t contexts_
std::unique_ptr< Designator > create_designator(Position pos, ThreadSafePooledString table_name, ThreadSafePooledString attr_name, typename Designator::target_type target, const Type *type)
Creates an entirely new Designator.
std::vector< SemaContext * > context_stack_t
a stack of sema contexts; one per statement; grows by nesting statements
SemaContext pop_context()
SemaContext & get_context()
std::unique_ptr< DatabaseCommand > command_
the command to execute when semantic analysis completes without errors
M_AST_EXPR_LIST(DECLARE) M_AST_CLAUSE_LIST(DECLARE) M_AST_COMMAND_LIST(DECLARE) private