-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict to use SELECT in columns default expressions. #9481
Changes from all commits
1aac3a5
a9edc5c
00c3898
d0bcbf7
d19e21b
3554db1
7cd584b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
#include <Parsers/ParserCreateQuery.h> | ||
#include <Parsers/parseQuery.h> | ||
#include <Parsers/queryToString.h> | ||
#include <Parsers/ASTSubquery.h> | ||
#include <Parsers/ASTSelectQuery.h> | ||
#include <Parsers/ASTSelectWithUnionQuery.h> | ||
#include <IO/WriteBuffer.h> | ||
#include <IO/WriteHelpers.h> | ||
#include <IO/ReadBuffer.h> | ||
|
@@ -20,7 +23,9 @@ | |
#include <Storages/IStorage.h> | ||
#include <Common/typeid_cast.h> | ||
#include <Compression/CompressionFactory.h> | ||
|
||
#include <Interpreters/ExpressionAnalyzer.h> | ||
#include <Interpreters/SyntaxAnalyzer.h> | ||
#include <Interpreters/ExpressionActions.h> | ||
|
||
namespace DB | ||
{ | ||
|
@@ -30,6 +35,7 @@ namespace ErrorCodes | |
extern const int NO_SUCH_COLUMN_IN_TABLE; | ||
extern const int ILLEGAL_COLUMN; | ||
extern const int CANNOT_PARSE_TEXT; | ||
extern const int THERE_IS_NO_DEFAULT_VALUE; | ||
} | ||
|
||
ColumnDescription::ColumnDescription(String name_, DataTypePtr type_, bool is_virtual_) | ||
|
@@ -421,4 +427,28 @@ ColumnsDescription ColumnsDescription::parse(const String & str) | |
return result; | ||
} | ||
|
||
|
||
Block validateColumnsDefaultsAndGetSampleBlock(ASTPtr default_expr_list, const NamesAndTypesList & all_columns, const Context & context) | ||
{ | ||
for (const auto & child : default_expr_list->children) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @4ertus2 I think this check is enough, but maybe I have missed something. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally ASTSelectQuery appears inside of ASTSelectWithUnionQuery. So we'd better add both or none of them in check. |
||
if (child->as<ASTSelectQuery>() || child->as<ASTSelectWithUnionQuery>() || child->as<ASTSubquery>()) | ||
throw Exception("Select query is not allowed in columns DEFAULT expression", ErrorCodes::THERE_IS_NO_DEFAULT_VALUE); | ||
|
||
try | ||
{ | ||
auto syntax_analyzer_result = SyntaxAnalyzer(context).analyze(default_expr_list, all_columns); | ||
const auto actions = ExpressionAnalyzer(default_expr_list, syntax_analyzer_result, context).getActions(true); | ||
for (auto & action : actions->getActions()) | ||
if (action.type == ExpressionAction::Type::JOIN || action.type == ExpressionAction::Type::ARRAY_JOIN) | ||
throw Exception("Unsupported default value that requires ARRAY JOIN or JOIN action", ErrorCodes::THERE_IS_NO_DEFAULT_VALUE); | ||
|
||
return actions->getSampleBlock(); | ||
} | ||
catch (Exception & ex) | ||
{ | ||
ex.addMessage("default expression and column type are incompatible."); | ||
throw; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
1 1 | ||
1 1 1 | ||
2 2 4 | ||
2 2 2 4 | ||
3 3 9 | ||
3 3 3 9 27 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
DROP TABLE IF EXISTS table_with_defaults_on_aliases; | ||
|
||
CREATE TABLE table_with_defaults_on_aliases (col1 UInt32, col2 ALIAS col1, col3 DEFAULT col2) Engine = MergeTree() ORDER BY tuple(); | ||
|
||
INSERT INTO table_with_defaults_on_aliases (col1) VALUES (1); | ||
|
||
SELECT * FROM table_with_defaults_on_aliases WHERE col1 = 1; | ||
|
||
SELECT col1, col2, col3 FROM table_with_defaults_on_aliases WHERE col1 = 1; | ||
|
||
ALTER TABLE table_with_defaults_on_aliases ADD COLUMN col4 UInt64 DEFAULT col2 * col3; | ||
|
||
INSERT INTO table_with_defaults_on_aliases (col1) VALUES (2); | ||
|
||
SELECT * FROM table_with_defaults_on_aliases WHERE col1 = 2; | ||
|
||
SELECT col1, col2, col3, col4 FROM table_with_defaults_on_aliases WHERE col1 = 2; | ||
|
||
ALTER TABLE table_with_defaults_on_aliases ADD COLUMN col5 UInt64 ALIAS col2 * col4; | ||
|
||
INSERT INTO table_with_defaults_on_aliases (col1) VALUES (3); | ||
|
||
SELECT * FROM table_with_defaults_on_aliases WHERE col1 = 3; | ||
|
||
SELECT col1, col2, col3, col4, col5 FROM table_with_defaults_on_aliases WHERE col1 = 3; | ||
|
||
|
||
ALTER TABLE table_with_defaults_on_aliases ADD COLUMN col6 UInt64 MATERIALIZED col2 * col4; | ||
|
||
DROP TABLE IF EXISTS table_with_defaults_on_aliases; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0 0 test0 | ||
ClickHouse is great ClickHouse is fast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok but it can be terribly slow.