Repository navigation
Description
That's a really strange one!
When you create a view (let's call it test_stats) that has a subquery with multiple tables (test1 and test2) combined using UNION ALL clause, and the first table in that clause has some custom-named columns (using AS); then if you try to insert some data to the first table (test1) the server will raise an exception that the renamed column wasn't found.
Should the VIEW/UNION ALL query affect the original tables? It looks really weird to me.
Here's an example:
>> CREATE TABLE test1 (id UInt64, date Date) ENGINE = MergeTree(date, (id, date), 8192);
>> CREATE TABLE test2 (id UInt64, date Date) ENGINE = MergeTree(date, (id, date), 8192);
>> CREATE VIEW test_stats AS SELECT _name, rows_total, rows_inserted, minus(rows_total, rows_inserted) AS _delta FROM
(SELECT * FROM (
SELECT 'test1' AS _name, 1234 AS rows_total, count() AS rows_inserted FROM default.test1
UNION ALL
SELECT 'test2' AS _name, 4568 AS rows_total, count() AS rows_inserted FROM default.test2
)
);
>> SELECT * FROM test_stats;
Ok. 0 rows in set.
>> INSERT INTO test1 VALUES (1, '2016-09-10');
Received exception from server:
Code: 10. DB::Exception: Received from localhost:9000, ::1. DB::Exception: Not found column: 'rows_total'.
1 rows in set.
Even if I write explicitly that I want to insert just the id and date, it still raises the same exception:
>> INSERT INTO test1 (id, date) VALUES (1, '2016-09-10');
Received exception from server:
Code: 10. DB::Exception: Received from localhost:9000, ::1. DB::Exception: Not found column: 'rows_total'.
1 rows in set.
Inserting into other tables works OK with either syntax:
>> INSERT INTO test2 (id, date) VALUES (1, '2016-09-10');
Ok. 1 rows in set.
>> INSERT INTO test2 VALUES (2, '2016-10-10');
Ok. 1 rows in set.
>> SELECT * FROM test_stats;
┌─_name─┬─rows_total─┬─rows_inserted─┬─_delta─┐
│ test2 │ 4568 │ 2 │ 4566 │
└───────┴────────────┴───────────────┴────────┘`
>> DROP TABLE test_stats;
Ok. 0 rows in set.
Let's make sure it was the view/subquery/union's fault:
>> INSERT INTO test1 (id, date) VALUES (1, '2016-09-10');
Ok. 1 rows in set.
>> CREATE VIEW test_stats AS SELECT cnt FROM
(SELECT cnt FROM (
SELECT count() AS cnt FROM default.test1
UNION ALL
SELECT count() AS cnt FROM default.test2
)
);
>> INSERT INTO test1 (id, date) VALUES (2, '2016-09-10');
Received exception from server:
Code: 10. DB::Exception: Received from localhost:9000, ::1. DB::Exception: Not found column cnt in block. There are only columns: id, date.
1 rows in set.
I modified the server's output to make it less bloated; this bug appears on the v1.1.53981 (sorry, can't test it with the newer one right now).
Activity
alexey-milovidov commentedon Oct 9, 2016
Confirmed. We have an issue with VIEWS and UNION ALL.
PS. Sorry for the delay, I was on conference trip this week.
hatarist commentedon Nov 11, 2016
I just noticed that the tables are affected even if there are views without UNION ALL.
If there's a column rename in the subquery, it messes with the latter INSERT queries to the table.
Also, ClickHouse seems to lose track of the current database.
Proof of concept:
[-]Problem with INSERT while having a VIEW with UNION ALL[/-][+]Problem with INSERT if having a VIEW with subqueries & AS keyword[/+]sirkon commentedon May 5, 2017
Any progress on this?
golangg commentedon Jun 23, 2017
+1
erebus1 commentedon Aug 3, 2017
same problem
VKoptev commentedon Sep 28, 2017
+1
mbobakov commentedon Sep 28, 2017
Any estimates ?
dyomina-nn commentedon Dec 7, 2017
Is any updates or workarounds?
filimonov commentedon Dec 7, 2017
Generally VIEWS do work nice. My workaround is to avoid views, and generate long and ugly selects.
dyomina-nn commentedon Dec 7, 2017
unfortunately my MATERIALIZED VIEWs dont work if they have subqueries or JOIN, but with AS keyword they work normal. And for me using long and ugly selects isnt very good, there are too many big and long selects....
At that VIEW works fine, analogic MATERIALIZED VIEW doesnt work...
sirkon commentedon Dec 10, 2017
I decided to hunt down this issue, reproduced steps and oops

This is old clickhouse version, it works and I did nothing. I am running Ubuntu 17.10. I remember though I installed libpoco yesterday, together with
libpocoand related stuff, includinglibmysqlclient-devanddefault-libmysqlclient-dev. Could it be related somehow?zhang2014 commentedon Feb 9, 2018
I tried to reappear with the above statement, But I can't reproduce this problem. Maybe this problem has been fixed? Can we close this Issue? @alexey-milovidov
DOCS-664: system.quotas (#12472)
fixed : cover more jni interfaces for catching c++ exceptions (ClickH…