Support use alias column in indices

This commit is contained in:
flynn 2023-11-25 15:17:48 +00:00
parent 3b775dee53
commit 9f174b1c5e
5 changed files with 119 additions and 0 deletions

View File

@ -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

View 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();
}
}
}
}

View 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>;
}

View File

@ -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

View File

@ -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;