Skip to content

Add query-rewrite functions - #48757

Closed
Dimakl wants to merge 3 commits into
ClickHouse:masterfrom
Dimakl:new-ticket-21494
Closed

Dimakl wants to merge 3 commits into
ClickHouse:masterfrom
Dimakl:new-ticket-21494

Conversation

@Dimakl

@Dimakl Dimakl commented Apr 13, 2023

Copy link
Copy Markdown

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Added 4 new query-rewrite functions to SQL dialect (obfuscateQuery, tokenizeQuery[OneLine], formatQuery)

Documentation entry for user-facing changes

Feature was being developed based on #29922 ticket.

This PR adds 4 functions to obfuscate/tokenize/format query within user query.
Functions were added directly to SQL dialect - IMO it's the most straightforward and obvious way for user to use them in their queries, moreover this is consistent with query normalization functions. Implementing these functions in imitation to EXPLAIN would be too broad for such functions which mostly would be used as meta-tools for requests.

Functionality is based on already (obviously) present query tokenization used in Clickhouse and on clickhouse-format utility to be more consistent within Clickhouse already present utilities.

@CLAassistant

CLAassistant commented Apr 13, 2023 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 13, 2023
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets)
{
std::string_view query(reinterpret_cast<const char *>(data.data() + offsets[- 1]), offsets[0] - offsets[- 1] - 1);

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 functions doesn't handle columns correctly. It only works on the first string.
To get an idea of what this code should be like you can take a look at

static void vector(
const ColumnString::Chars & data,
const ColumnString::Offsets & offsets,
ColumnString::Chars & res_data,
ColumnString::Offsets & res_offsets)
{
const size_t size = offsets.size();
res_data.resize(size * (length + 1));
res_offsets.resize(size);
size_t prev_offset = 0;
for (size_t i = 0; i < size; ++i)
{
const char * value = reinterpret_cast<const char *>(&data[prev_offset]);
const size_t value_length = offsets[i] - prev_offset - 1;
const size_t out_index = i * (length + 1);
calculate(value, value_length, reinterpret_cast<char *>(&res_data[out_index]));
res_data[out_index + length] = '\0';
res_offsets[i] = (out_index + length + 1);
prev_offset = offsets[i];
}
}

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 applies also to other functions.

// Obfuscated queries are usually don't take more than x2 characters.
res_data.reserve(res.size() * 2);

memcpySmallAllowReadWriteOverflow15(res_data.data(), res.c_str(), res.size());

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.

I don't think it's safe to use this function in this specific case.
You don't know if those buffers have up to res.size()+15 memory allocated.
This comment probably applies to other uses of this function.

return res_col;
}
private:
String getStringColumnValue(const ColumnPtr & column) const

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 function makes unnecessary copies. It also doesn't handle the whole column, but just the first value.


std::string res = buffer.str();
// Obfuscated queries are usually don't take more than x2 characters.
res_data.reserve(res.size() * 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.

Isn't res.size() already the correct size?

Also you will need to resize res_data at some point, just reserve is not enough.

res_offsets.resize(1);
obfuscateQueries(query, buffer, obfuscated_words_map, used_nouns, hash_func, is_known_identifier);

std::string res = buffer.str();

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.

Unnecessary copy.

@alexey-milovidov

Copy link
Copy Markdown
Member

Implemented in other PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants