Skip to content

Invalid use of collate in window functions #44039

Closed

Description

@rgzntrade

Question

  • Invalid use of collate in window functions

Version

  • 22.8.5.1

Recurrence

create table test_window_collate(c1 String, c2 String) engine=MergeTree order by c1;
insert into test_window_collate values('1', '上海');
insert into test_window_collate values('1', '北京');
insert into test_window_collate values('1', '西安');
select c2, groupArray(c2) over (partition by c1 order by c2  asc collate 'zh_Hans_CN') as res from test_window_collate order by c2  asc collate 'zh_Hans_CN';

Actual result

c2 res
北京 ['上海‘, '北京']
上海 ['上海‘]
西安 ['上海‘, '北京', '西安']

Expected result

c2 res
北京 ['北京']
上海 ['北京', '上海‘]
西安 ['北京', '上海‘, '西安']

Guess the cause

It's not the expected result because invalid use of collate in window functions.

Code analysis

  • File ParserSelectQuery.cpp

  • Line 164

  • Code if (!exp_list_for_select_clause.parse(pos, select_expression_list, expected))
    The code here does not give s_order_by(Line 262) a chance to get the collate

  • File ExpressionAnalyzer.cpp

  • Line 1951

  • Code && !query_analyzer.hasWindow()
    This code causes collator not to be created

How to make window function support collate?

Activity

  1. rgzntrade commented on Dec 9, 2022

    @rgzntrade
    ContributorAuthor

    I found a way on my own and the results were as expected. I don't know if there are any potential risks.

    • File:ExpressionAnalyzer.cpp
    • Line:744
    • Code:
    if (order_by_element.collation)
                {
                    desc.order_by.push_back(
                        SortColumnDescription(
                            order_by_element.children.front()->getColumnName(),
                            order_by_element.direction,
                            order_by_element.nulls_direction,
                            std::make_shared<Collator>(order_by_element.collation->as<ASTLiteral &>().value.get<String>())));
                }
                else
                {
                    desc.order_by.push_back(
                        SortColumnDescription(
                            order_by_element.children.front()->getColumnName(),
                            order_by_element.direction,
                            order_by_element.nulls_direction));
                }

    Is there any risk in me doing this?

  2. added a commit that references this issue on Apr 11, 2024
  3. fm4v commented on Apr 11, 2024

    @fm4v
    Member

    Fixed with the new analyzer which is enabled by default.

    SET allow_experimental_analyzer=1;
    SELECT
        c2,
        groupArray(c2) OVER (PARTITION BY c1 ORDER BY c2 ASC COLLATE 'zh_Hans_CN') AS res
    FROM test_window_collate
    ORDER BY c2 ASC COLLATE 'zh_Hans_CN'
    
       ┌─c2───┬─res────────────────────┐
    1. │ 北京 │ ['北京']               │
    2. │ 上海 │ ['北京','上海']        │
    3. │ 西安 │ ['北京','上海','西安'] │
       └──────┴────────────────────────┘
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions