Type parser: malformed names give a column of another type ("Array Int32" gives Int32, "Array(Int32, String)" gives Array(Int32))

未关闭
#646 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
54/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp
领域
backend, databases

调研方向

Start with clickhouse/types/type_parser.cpp at the TypeParser::Parse branches for Token::Assign, Token::Comma, and Token::Name, then inspect the argument checks in clickhouse/columns/factory.cpp for Array, Decimal, and SimpleAggregateFunction. Compare the parser and factory behavior with the malformed names in the reproduction program; done means each is rejected with nullptr or ValidationError, as the issue describes.

由索引模型根据 Issue 内容生成。

描述

bug data-types native-format

Describe the bug

CreateColumnByType() accepts three kinds of malformed type names that the server rejects, and returns a column of another type with no error:

  • = in place of ,: Map(String = Int32) gives Map(String, Int32), and Decimal(9 = 2) gives Decimal(9,2). The same occurs in Tuple and DateTime64.
  • Two type names in one place: the second name replaces the first. Array Int32 gives Int32, and Nullable(Int32 String) gives Nullable(String). The same occurs at the top level (Int32 Int64 gives Int64) and in Map and LowCardinality arguments.
  • Too many arguments: the client ignores the extra arguments, or uses the last one. Array(Int32, String) gives Array(Int32), Decimal(9, 2, 3) gives Decimal(9,3), and SimpleAggregateFunction(sum, UInt64, String) gives String. The same occurs in Nullable, LowCardinality, FixedString, DateTime64, and Decimal32.

Thus a typo in a type name gives a column of another type, while other names that do not parse give nullptr (Array(Int32) or a ValidationError (too few arguments: Map(String), Array()). The read path of Select() (clickhouse/client.cpp:862) uses the same parser, but a correct server sends only valid type names.

Steps to reproduce

Reproduction program (C++, 15 lines)
// Prints the column type that CreateColumnByType returns for each argument.
#include <clickhouse/columns/factory.h>
#include <cstdio>
#include <exception>

int main(int argc, char** argv) {
    for (int i = 1; i < argc; ++i) {
        try {
            auto col = clickhouse::CreateColumnByType(argv[i]);
            std::printf("%-46s -> %s\n", argv[i], col ? col->Type()->GetName().c_str() : "nullptr");
        } catch (const std::exception& e) {
            std::printf("%-46s -> exception: %s\n", argv[i], e.what());
        }
    }
}

Linked with libclickhouse-cpp-lib.a of a Debug build with -fsanitize=address,undefined (no sanitizer report):

$ ./type_names 'Map(String = Int32)' 'Decimal(9 = 2)' 'Array Int32' 'Nullable(Int32 String)' \
    'Array(Int32, String)' 'Decimal(9, 2, 3)' 'SimpleAggregateFunction(sum, UInt64, String)' 'Array(Int32'
Map(String = Int32)                            -> Map(String, Int32)
Decimal(9 = 2)                                 -> Decimal(9,2)
Array Int32                                    -> Int32
Nullable(Int32 String)                         -> Nullable(String)
Array(Int32, String)                           -> Array(Int32)
Decimal(9, 2, 3)                               -> Decimal(9,3)
SimpleAggregateFunction(sum, UInt64, String)   -> String
Array(Int32                                    -> nullptr

Expected behavior

CreateColumnByType() returns nullptr or throws ValidationError for each of these names, as it does for Array(Int32 and Map(String). The server rejects all of them (SELECT defaultValueOfTypeName('<name>'), messages cut):

Map(String = Int32)                           Code: 62. Syntax error (data type): failed at position 12 (=)
Decimal(9 = 2)                                Code: 43. Decimal argument precision is invalid
Array Int32                                   Code: 62. Syntax error (data type): failed at position 7 (Int32): Int32. Expected end of query
Nullable(Int32 String)                        Code: 62. Syntax error (data type): failed at position 16 (String)
Array(Int32, String)                          Code: 42. Array data type family must have exactly one argument - type of elements
Decimal(9, 2, 3)                              Code: 42. Decimal data type family must have precision and optional scale arguments
SimpleAggregateFunction(sum, UInt64, String)  Code: 42. Aggregate function sum requires single argument

Root cause

  • TypeParser::Parse handles Token::Assign and Token::Comma in one branch (clickhouse/types/type_parser.cpp:232-233), so = separates arguments in every type, not only in Enum items.
  • The Token::Name branch (type_parser.cpp:199-204) moves the first name into element_name when a second name follows, on every node, not only on a Tuple element. Outside Tuple, the factory ignores element_name.
  • CreateColumnFromAst and CreateTerminalColumn (clickhouse/columns/factory.cpp) read the arguments by position with GetASTChildElement (for example Array at :163, Decimal the first and the last argument at :94, SimpleAggregateFunction the last at :285) and do not check the argument count, as the Map case does.

The fix is in TypeParser::Parse and in the argument checks of the factory. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related

  • #616 (open) changes the Enum parsing, which uses the same =/, branch.
  • #643 (open): another malformed name, (Int32), with another cause (uninitialized root node).

Environment

  • clickhouse-cpp: master 6049285 (v2.6.2 has the same parser code)
  • ClickHouse server: 26.11.7.10
  • clang 18.1.3, Linux x86_64 (Ubuntu 24.04)

Classification: impact: none · area: Data types & formats (type names) · native: format


Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-rs#578. Verified against a live server.

主要语言
C
星标
388
派生
220
平均合并
1 天 25 分钟
30 天内合并 PR
16

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/clickhouse-cpp 的其他 Issue

查看 ClickHouse/clickhouse-cpp 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。