ClickHouse/src/Storages/VirtualColumnUtils.h

52 lines
1.6 KiB
C++
Raw Normal View History

2014-01-17 15:19:20 +00:00
#pragma once
#include <unordered_set>
#include <Core/Block.h>
2019-05-20 12:16:51 +00:00
#include <Parsers/IAST_fwd.h>
2021-03-04 05:59:57 +00:00
#include <Storages/SelectQueryInfo.h>
2014-01-17 15:19:20 +00:00
namespace DB
{
class Context;
class NamesAndTypesList;
namespace VirtualColumnUtils
2014-01-17 15:19:20 +00:00
{
/// Adds to the select query section `WITH value AS column_name`, and uses func
/// to wrap the value (if any)
///
/// For example:
/// - `WITH 9000 as _port`.
/// - `WITH toUInt16(9000) as _port`.
void rewriteEntityInAst(ASTPtr ast, const String & column_name, const Field & value, const String & func = "");
2021-03-03 08:36:20 +00:00
/// Prepare `expression_ast` to filter block. Returns true if `expression_ast` is not trimmed, that is,
2021-03-04 05:59:57 +00:00
/// `block` provides all needed columns for `expression_ast`, else return false.
2021-03-04 14:27:07 +00:00
bool prepareFilterBlockWithQuery(const ASTPtr & query, const Context & context, Block block, ASTPtr & expression_ast);
2021-03-03 08:36:20 +00:00
2017-05-07 20:25:26 +00:00
/// Leave in the block only the rows that fit under the WHERE clause and the PREWHERE clause of the query.
/// Only elements of the outer conjunction are considered, depending only on the columns present in the block.
2021-03-03 08:36:20 +00:00
/// If `expression_ast` is passed, use it to filter block.
2021-03-04 14:27:07 +00:00
void filterBlockWithQuery(const ASTPtr & query, Block & block, const Context & context, ASTPtr expression_ast = {});
2014-01-17 15:19:20 +00:00
2017-05-07 20:25:26 +00:00
/// Extract from the input stream a set of `name` column values
2019-05-20 12:16:51 +00:00
template <typename T>
auto extractSingleValueFromBlock(const Block & block, const String & name)
{
std::unordered_set<T> res;
const ColumnWithTypeAndName & data = block.getByName(name);
size_t rows = block.rows();
for (size_t i = 0; i < rows; ++i)
2019-05-20 12:16:51 +00:00
res.insert((*data.column)[i].get<T>());
return res;
}
}
}