ClickHouse/src/Analyzer/LambdaNode.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.0 KiB
C++
Raw Normal View History

2022-07-14 11:20:16 +00:00
#pragma once
#include <Analyzer/IQueryTreeNode.h>
#include <Analyzer/ListNode.h>
#include <Analyzer/IdentifierNode.h>
#include <Parsers/ASTFunction.h>
namespace DB
{
/** Lambda node represents lambda expression in query tree.
*
* Lambda consist of argument names and lambda expression body.
2022-10-19 10:25:27 +00:00
* Lambda expression body does not necessary use lambda arguments. Example: SELECT arrayMap(x -> 1, [1, 2, 3])
2022-07-14 11:20:16 +00:00
*
2022-10-19 10:25:27 +00:00
* Initially lambda is initialized with argument names and lambda body expression.
2022-07-14 11:20:16 +00:00
*
2022-10-19 10:25:27 +00:00
* Lambda expression result type can depend on arguments types.
2022-07-14 11:20:16 +00:00
* Example: WITH (x -> x) as lambda SELECT lambda(1), lambda('string_value').
*
* During query analysis pass lambdas must be resolved.
* Lambda resolve must set concrete lambda arguments and resolve lambda expression body.
* In query tree lambda arguments are represented by ListNode.
* If client modified lambda arguments array its size must be equal to initial lambda argument names array.
*
* Examples:
2022-10-19 10:25:27 +00:00
* WITH (x -> x + 1) as lambda SELECT lambda(1);
* SELECT arrayMap(x -> x + 1, [1,2,3]);
2022-07-14 11:20:16 +00:00
*/
class LambdaNode;
using LambdaNodePtr = std::shared_ptr<LambdaNode>;
class LambdaNode final : public IQueryTreeNode
{
public:
2022-10-19 10:25:27 +00:00
/// Initialize lambda with argument names and lambda body expression
2022-07-14 11:20:16 +00:00
explicit LambdaNode(Names argument_names_, QueryTreeNodePtr expression_);
/// Get argument names
const Names & getArgumentNames() const
{
return argument_names;
}
/// 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];
}
2022-10-19 10:25:27 +00:00
/// Get arguments node
2022-07-14 11:20:16 +00:00
QueryTreeNodePtr & getArgumentsNode()
{
return children[arguments_child_index];
}
/// Get expression
const QueryTreeNodePtr & getExpression() const
{
return children[expression_child_index];
}
/// Get expression
QueryTreeNodePtr & getExpression()
{
return children[expression_child_index];
}
QueryTreeNodeType getNodeType() const override
{
return QueryTreeNodeType::LAMBDA;
}
DataTypePtr getResultType() const override
{
return getExpression()->getResultType();
}
void dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const override;
protected:
2022-07-15 13:32:53 +00:00
bool isEqualImpl(const IQueryTreeNode & rhs) const override;
2022-07-14 11:20:16 +00:00
void updateTreeHashImpl(HashState & state) const override;
QueryTreeNodePtr cloneImpl() const override;
ASTPtr toASTImpl() const override;
2022-07-14 11:20:16 +00:00
private:
2022-10-07 10:44:28 +00:00
Names argument_names;
2022-07-14 11:20:16 +00:00
static constexpr size_t arguments_child_index = 0;
static constexpr size_t expression_child_index = 1;
2022-10-07 10:44:28 +00:00
static constexpr size_t children_size = expression_child_index + 1;
2022-07-14 11:20:16 +00:00
};
}