mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-27 10:02:01 +00:00
Support use alias column in indices
This commit is contained in:
parent
3b775dee53
commit
9f174b1c5e
@ -10,6 +10,8 @@
|
||||
#include <Parsers/parseQuery.h>
|
||||
#include <Storages/extractKeyExpressionList.h>
|
||||
|
||||
#include <Storages/ReplaceAliasToExprVisitor.h>
|
||||
|
||||
#include <Core/Defines.h>
|
||||
#include "Common/Exception.h"
|
||||
|
||||
@ -94,6 +96,10 @@ IndexDescription IndexDescription::getIndexFromAST(const ASTPtr & definition_ast
|
||||
if (index_definition->expr)
|
||||
{
|
||||
expr_list = extractKeyExpressionList(index_definition->expr->clone());
|
||||
|
||||
ReplaceAliasToExprVisitor::Data data{columns};
|
||||
ReplaceAliasToExprVisitor{data}.visit(expr_list);
|
||||
|
||||
result.expression_list_ast = expr_list->clone();
|
||||
}
|
||||
else
|
||||
|
31
src/Storages/ReplaceAliasToExprVisitor.cpp
Normal file
31
src/Storages/ReplaceAliasToExprVisitor.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include <Parsers/ASTIdentifier.h>
|
||||
#include <Parsers/ASTLiteral.h>
|
||||
#include <Storages/ColumnsDescription.h>
|
||||
#include <Storages/ReplaceAliasToExprVisitor.h>
|
||||
#include <Common/typeid_cast.h>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
void ReplaceAliasToExprMatcher::visit(ASTPtr & ast, Data & data)
|
||||
{
|
||||
if (auto * identifier = ast->as<ASTIdentifier>())
|
||||
{
|
||||
visit(*identifier, ast, data);
|
||||
}
|
||||
}
|
||||
|
||||
void ReplaceAliasToExprMatcher::visit(const ASTIdentifier & column, ASTPtr & ast, Data & data)
|
||||
{
|
||||
const auto & column_name = column.name();
|
||||
if (data.columns.hasAlias(column_name))
|
||||
{
|
||||
/// Alias expr is saved in default expr.
|
||||
if (auto col_default = data.columns.getDefault(column_name))
|
||||
{
|
||||
ast = col_default->expression->clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
27
src/Storages/ReplaceAliasToExprVisitor.h
Normal file
27
src/Storages/ReplaceAliasToExprVisitor.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <Parsers/IAST.h>
|
||||
#include <Interpreters/InDepthNodeVisitor.h>
|
||||
|
||||
namespace DB
|
||||
{
|
||||
|
||||
class ASTFunction;
|
||||
class ColumnsDescription;
|
||||
class ASTIdentifier;
|
||||
|
||||
class ReplaceAliasToExprMatcher
|
||||
{
|
||||
public:
|
||||
struct Data
|
||||
{
|
||||
const ColumnsDescription & columns;
|
||||
};
|
||||
|
||||
static void visit(ASTPtr & ast, Data &);
|
||||
static void visit(const ASTIdentifier &, ASTPtr & ast, Data &);
|
||||
static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return true; }
|
||||
};
|
||||
|
||||
using ReplaceAliasToExprVisitor = InDepthNodeVisitor<ReplaceAliasToExprMatcher, true>;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
Expression ((Projection + Before ORDER BY))
|
||||
Filter (WHERE)
|
||||
ReadFromMergeTree (02911_support_alias_column_in_indices.test)
|
||||
Indexes:
|
||||
PrimaryKey
|
||||
Keys:
|
||||
x
|
||||
Condition: (plus(x, 1) in [11, +Inf))
|
||||
Parts: 1/2
|
||||
Granules: 1/2
|
||||
Skip
|
||||
Name: i_y
|
||||
Description: minmax GRANULARITY 1
|
||||
Parts: 1/1
|
||||
Granules: 1/1
|
||||
Expression ((Projection + Before ORDER BY))
|
||||
Filter (WHERE)
|
||||
ReadFromMergeTree (02911_support_alias_column_in_indices.test1)
|
||||
Indexes:
|
||||
Skip
|
||||
Name: i_y
|
||||
Description: minmax GRANULARITY 1
|
||||
Parts: 2/2
|
||||
Granules: 2/2
|
@ -0,0 +1,31 @@
|
||||
-- Tags: no-parallel
|
||||
|
||||
drop database if exists 02911_support_alias_column_in_indices;
|
||||
create database 02911_support_alias_column_in_indices;
|
||||
use 02911_support_alias_column_in_indices;
|
||||
|
||||
CREATE TABLE test
|
||||
(
|
||||
x UInt32,
|
||||
y ALIAS x + 1,
|
||||
INDEX i_y (y) TYPE minmax
|
||||
) ENGINE = MergeTree ORDER BY x;
|
||||
|
||||
insert into test select * from numbers(10);
|
||||
insert into test select * from numbers(11, 20);
|
||||
|
||||
CREATE TABLE test1
|
||||
(
|
||||
x UInt32,
|
||||
y1 ALIAS x + 1,
|
||||
y2 ALIAS y1 + 1,
|
||||
INDEX i_y (y2) TYPE minmax
|
||||
) ENGINE = MergeTree ORDER BY tuple();
|
||||
|
||||
insert into test1 select * from numbers(10);
|
||||
insert into test1 select * from numbers(11, 20);
|
||||
|
||||
explain indexes = 1 select * from test where y > 10;
|
||||
explain indexes = 1 select * from test1 where y2 > 10;
|
||||
|
||||
drop database 02911_support_alias_column_in_indices;
|
Loading…
Reference in New Issue
Block a user