Fix an unexpected behavior in #53152

The problematic case happens when an array column of date/datetime
is simultaneously specified in an Array Join clause without aliases
and in a time converter, such as toYYYYMM.

After applying Array Join without aliases, the column's name refers
to the flattened array items, however, its data type is recognized
as a ColumnArray still, which leads to the unexpected exception
throws when building the preimage for the time converters.

As a quick fix, we more strictly check the data types of the time
converters and quit the preimage optimization pass early.
This commit is contained in:
Zhiguo Zhou 2023-08-15 14:02:36 +08:00
parent aae35f3b1c
commit 3579039733
3 changed files with 16 additions and 2 deletions

View File

@ -160,15 +160,18 @@ void OptimizeDateOrDateTimeConverterWithPreimageMatcher::visit(const ASTFunction
auto data_type_and_name = data.tables[*pos].columns.tryGetByName(column_id->shortName());
if (!data_type_and_name) return;
const auto column_type = data_type_and_name->type;
if (!column_type || (!isDateOrDate32(*column_type) && !isDateTime(*column_type) && !isDateTime64(*column_type))) return;
const auto & converter = FunctionFactory::instance().tryGet(ast_func->name, data.context);
if (!converter) return;
ColumnsWithTypeAndName args;
args.emplace_back(data_type_and_name->type, "tmp");
args.emplace_back(column_type, "tmp");
auto converter_base = converter->build(args);
if (!converter_base || !converter_base->hasInformationAboutPreimage()) return;
auto preimage_range = converter_base->getPreimage(*(data_type_and_name->type), literal->value);
auto preimage_range = converter_base->getPreimage(*column_type, literal->value);
if (!preimage_range) return;
const auto new_ast = generateOptimizedDateFilterAST(comparator, *data_type_and_name, *preimage_range);

View File

@ -0,0 +1,2 @@
202308 1
202308 2

View File

@ -0,0 +1,9 @@
select
toYYYYMM(date) as date_,
n
from (select
[toDate('20230815'), toDate('20230816')] as date,
[1, 2] as n
) as data
array join date, n
where date_ >= 202303;