Skip to content
Snippets Groups Projects
Commit dc358cb0 authored by Nikita Pettik's avatar Nikita Pettik
Browse files

sql: rework VIEW internals

This patch significantly reworks VIEW mechanisms.
Firstly, names resolution for VIEW now occurs during its creation.
In other words, we can't run following code, since name T1 doesn't exist
at the moment of V1 creation:

CREATE VIEW v1 AS SELECT * FROM t1;
CREATE TABLE t1(id PRIMARY KEY);

Now, table t1 must be created before view. As a result, no circularly
defined views are allowed. Moreover, it means that space representing
view is created with appropriate field names, types, collations etc.

Also, introduced view reference counter for each space. It is
incremented for each space participating in select statement.
For instace:

CREATE VIEW v1 AS SELECT * FROM (SELECT * FROM t1, t2);

In this case, view reference counter is increased for spaces T1 and T2.
Similarly, such counter is decremented for all dependent spaces when
VIEW is dropped.  To support such behavior, auxiliary
on_commit triggers are added. However, it is still not enough, since
before dropping space itself, we must drop secondary indexes, clear
_sequence space etc. Such changes can't be rollbacked (due to the lack
of transactional DDL), so before executing DROP routine, special opcode
OP_CheckViewReferences checks view reference counter for space to be
dropped.

Finally, we don't hold struct Select in struct Table or anywhere else
anymore.  Instead, 'CREATE VIEW AS SELECT ...' string is stored in
def->opts.sql.  At compilation time of query on view
(such as 'SELECT * FROM v1;') this string is parsed once and loaded
into struct Select, which in turn is used as before.

Fixed tests where view was created prior to referenced table.

Closes #3429, #3368, #3300
parent df245de9
No related branches found
No related tags found
No related merge requests found
Showing
with 498 additions and 273 deletions
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment