Merge pull request #57855 from kitaisreal/interpreter-create-query-sample-block-fix

InterpreterCreateQuery sample block fix
This commit is contained in:
Nikolai Kochetov 2023-12-18 17:22:24 +01:00 committed by GitHub
commit 5290b3c9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 8 deletions

View File

@ -786,10 +786,28 @@ InterpreterCreateQuery::TableProperties InterpreterCreateQuery::getTableProperti
}
else
{
as_select_sample = InterpreterSelectWithUnionQuery::getSampleBlock(create.select->clone(),
getContext(),
false /* is_subquery */,
create.isParameterizedView());
/** To get valid sample block we need to prepare query without only_analyze, because we need to execute scalar
* subqueries. Otherwise functions that expect only constant arguments will throw error during query analysis,
* because the result of scalar subquery is not a constant.
*
* Example:
* CREATE MATERIALIZED VIEW test_mv ENGINE=MergeTree ORDER BY arr
* AS
* WITH (SELECT '\d[a-z]') AS constant_value
* SELECT extractAll(concat(toString(number), 'a'), assumeNotNull(constant_value)) AS arr
* FROM test_table;
*
* For new analyzer this issue does not exists because we always execute scalar subqueries.
* We can improve this in new analyzer, and execute scalar subqueries only in contexts when we expect constant
* for example: LIMIT, OFFSET, functions parameters, functions constant only arguments.
*/
SelectQueryOptions options;
if (create.isParameterizedView())
options = options.createParameterizedView();
InterpreterSelectWithUnionQuery interpreter(create.select->clone(), getContext(), options);
as_select_sample = interpreter.getSampleBlock();
}
properties.columns = ColumnsDescription(as_select_sample.getNamesAndTypesList());
@ -1223,7 +1241,7 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create)
{
input_block = InterpreterSelectWithUnionQuery(create.select->clone(),
getContext(),
SelectQueryOptions().analyze()).getSampleBlock();
{}).getSampleBlock();
}
Block output_block = to_table->getInMemoryMetadataPtr()->getSampleBlock();

View File

@ -341,7 +341,7 @@ Chain buildPushingToViewsChain(
if (select_context->getSettingsRef().allow_experimental_analyzer)
header = InterpreterSelectQueryAnalyzer::getSampleBlock(query, select_context);
else
header = InterpreterSelectQuery(query, select_context, SelectQueryOptions().analyze()).getSampleBlock();
header = InterpreterSelectQuery(query, select_context, SelectQueryOptions()).getSampleBlock();
/// Insert only columns returned by select.
Names insert_columns;

View File

@ -18,7 +18,7 @@
89 89 89 89 5
94 94 94 94 5
99 99 99 99 5
02177_MV 3 80 26
02177_MV 7 80 22
10
40
70
@ -60,4 +60,4 @@
178
188
198
02177_MV_3 19 0 2
02177_MV_3 20 0 1

View File

@ -0,0 +1,9 @@
['0a']
--
['0a']
['1a']
--
['0a']
--
['0a']
['1a']

View File

@ -0,0 +1,52 @@
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table
(
number UInt64
)
ENGINE=MergeTree ORDER BY number;
DROP VIEW IF EXISTS test_mv;
CREATE MATERIALIZED VIEW test_mv ENGINE=MergeTree ORDER BY arr
AS
WITH (SELECT '\d[a-z]') AS constant_value
SELECT extractAll(concat(toString(number), 'a'), assumeNotNull(constant_value)) AS arr
FROM test_table;
INSERT INTO test_table VALUES (0);
SELECT * FROM test_mv ORDER BY arr;
SELECT '--';
INSERT INTO test_table VALUES (1);
SELECT * FROM test_mv ORDER BY arr;
SELECT '--';
TRUNCATE test_table;
DROP TABLE IF EXISTS regex_test_table;
CREATE TABLE regex_test_table
(
regex String
)
ENGINE = MergeTree ORDER BY regex;
INSERT INTO regex_test_table VALUES ('\d[a-z]');
DROP VIEW test_mv;
CREATE MATERIALIZED VIEW test_mv ENGINE=MergeTree ORDER BY arr
AS
WITH (SELECT regex FROM regex_test_table) AS constant_value
SELECT extractAll(concat(toString(number), 'a'), assumeNotNull(constant_value)) AS arr
FROM test_table;
INSERT INTO test_table VALUES (0);
SELECT * FROM test_mv ORDER BY arr;
SELECT '--';
INSERT INTO test_table VALUES (1);
SELECT * FROM test_mv ORDER BY arr;
DROP VIEW test_mv;
DROP TABLE test_table;