| 233 | } |
| 234 | |
| 235 | void filterBlockWithQuery(const ASTPtr & query, Block & block, ContextPtr context, ASTPtr expression_ast, ASTPtr partition_filter) |
| 236 | { |
| 237 | if (block.rows() == 0) |
| 238 | return; |
| 239 | |
| 240 | if (!expression_ast) |
| 241 | prepareFilterBlockWithQuery(query, context, block, expression_ast, partition_filter); |
| 242 | |
| 243 | if (!expression_ast) |
| 244 | return; |
| 245 | |
| 246 | /// Let's analyze and calculate the prepared expression. |
| 247 | auto syntax_result = TreeRewriter(context).analyze(expression_ast, block.getNamesAndTypesList()); |
| 248 | ExpressionAnalyzer analyzer(expression_ast, syntax_result, context); |
| 249 | buildSets(expression_ast, analyzer); |
| 250 | ExpressionActionsPtr actions = analyzer.getActions(false /* add alises */, true /* project result */, CompileExpressions::yes); |
| 251 | |
| 252 | Block block_with_filter = block; |
| 253 | actions->execute(block_with_filter); |
| 254 | |
| 255 | /// Filter the block. |
| 256 | String filter_column_name = expression_ast->getColumnName(); |
| 257 | ColumnPtr filter_column = block_with_filter.getByName(filter_column_name).column->convertToFullColumnIfConst(); |
| 258 | |
| 259 | ConstantFilterDescription constant_filter(*filter_column); |
| 260 | |
| 261 | if (constant_filter.always_true) |
| 262 | { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | if (constant_filter.always_false) |
| 267 | { |
| 268 | block = block.cloneEmpty(); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | FilterDescription filter(*filter_column); |
| 273 | |
| 274 | for (size_t i = 0; i < block.columns(); ++i) |
| 275 | { |
| 276 | ColumnPtr & column = block.safeGetByPosition(i).column; |
| 277 | column = column->filter(*filter.data, -1); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | void cascadingDrop(const StorageID & table_id, const ASTPtr & partition_or_predicate, |
| 282 | bool drop_where, bool detach, bool cascading, ContextPtr context) |
no test coverage detected