Skip to content

Fix header for union stream in distributed - #2226

Merged
alexey-milovidov merged 24 commits into
masterfrom
fix-header-for-union-stream-in-distributed-2
Aug 20, 2018
Merged

alexey-milovidov merged 24 commits into
masterfrom
fix-header-for-union-stream-in-distributed-2

Conversation

@KochetovNicolai

Copy link
Copy Markdown
Member

@KochetovNicolai
KochetovNicolai force-pushed the fix-header-for-union-stream-in-distributed-2 branch from aaccf3a to 081f0fe Compare April 16, 2018 19:49
/// filter_column_ - the number of the column with filter conditions.
FilterBlockInputStream(const BlockInputStreamPtr & input, const ExpressionActionsPtr & expression_, ssize_t filter_column_);
FilterBlockInputStream(const BlockInputStreamPtr & input, const ExpressionActionsPtr & expression_, const String & filter_column_name_);
FilterBlockInputStream(const BlockInputStreamPtr & input, const ExpressionActionsPtr & expression_, ssize_t filter_column_, bool remove_filter = false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment.

: expression(expression_), filter_column(filter_column_)
FilterBlockInputStream::FilterBlockInputStream(const BlockInputStreamPtr & input, const ExpressionActionsPtr & expression_,
ssize_t filter_column_, bool remove_filter)
: remove_filter(remove_filter), expression(expression_), filter_column(filter_column_)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When first ctor is used, header is not set.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this ctor.

alexey-milovidov added a commit that referenced this pull request Apr 18, 2018
namespace DB
{

class PrewhereFilterBlockInputStream : public FilterBlockInputStream

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total absence of comments.

prewhere_info->prewhere_column_name, prewhere_info->remove_prewhere_column)
, prewhere_info(prewhere_info)
{
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about header?

protected:
Block readImpl() override
{
remove_filter = prewhere_info->remove_prewhere_column;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have to be evaluated for each block?

for (int i = static_cast<int>(steps.size()) - 1; i >= 0; --i)
{
Names required_output = steps[i].required_output;
std::unordered_map<String, size_t> required_output_indexes;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indices

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still Ok.

bool appendArrayJoin(ExpressionActionsChain & chain, bool only_types);
bool appendJoin(ExpressionActionsChain & chain, bool only_types);
bool appendWhere(ExpressionActionsChain & chain, bool only_types);
bool appendPrewhere(ExpressionActionsChain & chain, bool only_types, std::shared_ptr<bool> & remove_filter);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot understand without comments.
Why we need std::shared_ptr<bool>. Looks weird.

struct Step
{
ExpressionActionsPtr actions;
NameSet additional_input;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this? Don't understand.

ExpressionActionsPtr actions;
NameSet additional_input;
Names required_output;
std::vector<std::shared_ptr<bool>> can_remove_required_output; /// Has the same size with required_output, is filled in finalize()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the output is required, why it's possible that we "can remove" it? No explanation.
Non obvious.


if (!dry_run && query_analyzer->appendPrewhere(chain, false, remove_prewhere_filter))
{
query_info.prewhere_info = prewhere_info = std::make_shared<PrewhereInfo>(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This style is discouraged.

block.erase(prewhere_info->prewhere_column_name);

if (!block)
block.insert({nullptr, std::make_shared<DataTypeNothing>(), "_nothing"});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it Ok to have a block without column?


Names pre_column_names, column_names = ordered_names;
bool remove_prewhere_column = false;
Names pre_column_names, column_names = required_columns;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad style.


private:
Block header;
mutable Block header;

@alexey-milovidov alexey-milovidov Apr 19, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a red flag.
May lead to race conditions in getHeader method.


Logger * log = &Logger::get("MergeTreeBlockInputStream");

const Names & getOrderedNames();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing comment.

}

void MergeTreeReader::reorderColumns(Block & res, const Names & ordered_names)
void MergeTreeReader::reorderColumns(Block & res, const Names & ordered_names, const String * filter_name)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::optional?

void fillMissingColumns(Block & res, bool & should_reorder, bool & should_evaluate_missing_defaults);
/// Sort columns to ensure consistent order among all blocks.
void reorderColumns(Block & res, const Names & ordered_names);
void reorderColumns(Block & res, const Names & ordered_names, const String * filter_name);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above wasn't updated.

@KochetovNicolai
KochetovNicolai force-pushed the fix-header-for-union-stream-in-distributed-2 branch from 164fb72 to a6375d6 Compare April 20, 2018 14:15
@KochetovNicolai
KochetovNicolai force-pushed the fix-header-for-union-stream-in-distributed-2 branch from ece7662 to 8061046 Compare April 23, 2018 19:39
const SelectQueryInfo & /*query_info*/,
const Context & /*context*/,
QueryProcessingStage::Enum & /*processed_stage*/,
QueryProcessingStage::Enum /*processed_stage*/,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this parameter is unnecessary now?

QueryProcessingStage::Enum processed_stage,
const size_t max_block_size,
const unsigned num_streams)
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we check processed_stage here?
What will happen if new tables with a different stage will be added between calls getQueryProcessingStage() and read()?

/// Example: select A prewhere B > 0. B can be removed at prewhere step.
/// 2. Store side columns which were calculated during prewhere actions execution if they are used.
/// Example: select F(A) prewhere F(A) > 0. F(A) can be saved from prewhere step.
/// 3. Check if we can remove filer column at prewhere step. If we can, action will store single REMOVE_COLUMN.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filer -> filter

@chenxing-xc

Copy link
Copy Markdown
Contributor

is there any progress on this fix? we hit the same problem multiple times and has to disable optimize_move_to_prewhere to workaround the problem.

@KochetovNicolai
KochetovNicolai force-pushed the fix-header-for-union-stream-in-distributed-2 branch from 83b2541 to 60f0ed9 Compare June 25, 2018 12:48
/// True if column from required_output is needed only for current step and not used in next actions
/// (and can be removed from block). Example: filter column for where actions.
/// If not empty, has the same size with required_output; is filled in finalize().
std::vector<bool *> not_need_in_future_steps;

@ztlpn ztlpn Jun 28, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite dangerous and also hurts code readability - when we modify a flag in this vector, where is it in the code? You have to switch to another file to determine.

Alternative - have a collection (can be a vector<bool> or even a NameSet that will tell us which columns are not needed by future steps). Then the calling code can set any needed bool value itself.

if (block && remove_filter)
block.erase(static_cast<size_t>(filter_column));

return block;

@ztlpn ztlpn Jun 29, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to add std::move here otherwise a copy will be performed (the compiler won't do copy elision for a function parameter).


if (i + 1 < static_cast<int>(steps.size()))
{
const NameSet & additional_input = steps[i + 1].additional_input;

@ztlpn ztlpn Jun 29, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok that we are considering only the next step when setting the not_need_in_future_steps variable (the name points to all future steps instead of just the next one)?

Also it's better to stick to a single name (can_remove_required_output or not_need_in_future_steps)

create table test.tab (date Date, time DateTime, data String) ENGINE = MergeTree(date, (time, data), 8192);
insert into test.tab values ('2018-01-21','2018-01-21 15:12:13','test');
select time FROM remote('127.0.0.{1,2}', test.tab) WHERE date = '2018-01-21' limit 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to drop the test table at the end

@ztlpn

ztlpn commented Jun 29, 2018 •

Copy link
Copy Markdown
Contributor

Seems that #2083 is still not fixed in this PR.

@KochetovNicolai
KochetovNicolai force-pushed the fix-header-for-union-stream-in-distributed-2 branch from 92a923a to 6e5e573 Compare June 29, 2018 16:19

AnalysisResult expressions;

if (dry_run)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dry_run condition does not cover all cases. There exists some cases (i.e. case 00229 in /dbms/tests/queries/stateless/) that makes Expression::execute function throw an exception.
In this case, apply_functions in prewhere actions will not be executed if InterpreterSelectQuery::getHeader() is invoked. Then the PROJECT will not find corresponding columns, which leads to an exception.
I think it can be solved by swapping prewhere_expression with where_expression if dry_run is true.

@alexey-milovidov
alexey-milovidov merged commit 28bc6db into master Aug 20, 2018
@Felixoid
Felixoid deleted the fix-header-for-union-stream-in-distributed-2 branch April 10, 2022 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants