add boundary order check

This commit is contained in:
Alexander Kuzmenkov 2021-02-09 18:36:08 +03:00
parent c18749a704
commit 8212976dc0
3 changed files with 34 additions and 15 deletions

View File

@ -121,11 +121,21 @@ void WindowFrame::checkValid() const
if (end_type == BoundaryType::Offset
&& begin_type == BoundaryType::Offset)
{
// Frame starting with following rows can't have preceding rows.
if (!(end_preceding && !begin_preceding))
{
// Should probably check here that starting offset is less than
// ending offset, but with regard to ORDER BY direction for RANGE
// frames.
// Frame start offset must be less or equal that the frame end offset.
const bool begin_before_end
= begin_offset * (begin_preceding ? -1 : 1)
<= end_offset * (end_preceding ? -1 : 1);
if (!begin_before_end)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Frame start offset {} {} does not precede the frame end offset {} {}",
begin_offset, begin_preceding ? "PRECEDING" : "FOLLOWING",
end_offset, end_preceding ? "PRECEDING" : "FOLLOWING");
}
return;
}
}
@ -135,4 +145,20 @@ void WindowFrame::checkValid() const
toString());
}
void WindowDescription::checkValid() const
{
frame.checkValid();
// RANGE OFFSET requires exactly one ORDER BY column.
if (frame.type == WindowFrame::FrameType::Range
&& (frame.begin_type == WindowFrame::BoundaryType::Offset
|| frame.end_type == WindowFrame::BoundaryType::Offset)
&& order_by.size() != 1)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"The RANGE OFFSET window frame requires exactly one ORDER BY column, {} given",
order_by.size());
}
}
}

View File

@ -131,7 +131,10 @@ struct WindowDescription
// The window functions that are calculated for this window.
std::vector<WindowFunctionDescription> window_functions;
std::string dump() const;
void checkValid() const;
};
using WindowFunctionDescriptions = std::vector<WindowFunctionDescription>;

View File

@ -60,21 +60,11 @@ WindowStep::WindowStep(const DataStream & input_stream_,
, window_functions(window_functions_)
, input_header(input_stream_.header)
{
const auto & frame = window_description.frame;
// We don't remove any columns, only add, so probably we don't have to update
// the output DataStream::distinct_columns.
frame.checkValid();
// RANGE OFFSET requires exactly one ORDER BY column.
if (frame.type == WindowFrame::FrameType::Range
&& (frame.begin_type == WindowFrame::BoundaryType::Offset
|| frame.end_type == WindowFrame::BoundaryType::Offset)
&& window_description.order_by.size() != 1)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"The RANGE OFFSET window frame requires exactly one ORDER BY column, {} given",
window_description.order_by.size());
}
window_description.checkValid();
}
void WindowStep::transformPipeline(QueryPipeline & pipeline)