MaterializeInc / materialize

The data warehouse for operational workloads.
https://materialize.com
Other
5.66k stars 458 forks source link

Reduce noise in `SHOW CREATE VIEW` #2159

Open umanwizard opened 4 years ago

umanwizard commented 4 years ago

Currently, SHOW CREATE VIEW spits out the canonicalized SQL, something like:

CREATE VIEW "materialize"."public"."q02" AS SELECT "su_suppkey", "su_name", "n_name", "i_id", "i_name", "su_address", "su_phone", "su_comment" FROM "materialize"."public"."item", "materialize"."public"."supplier", "materialize"."public"."stock", "materialize"."public"."nation", "materialize"."public"."region", (SELECT "s_i_id" AS "m_i_id", min("s_quantity") AS "m_s_quantity" FROM "materialize"."public"."stock", "materialize"."public"."supplier", "materialize"."public"."nation", "materialize"."public"."region" WHERE "s_su_suppkey" = "su_suppkey" AND "su_nationkey" = "n_nationkey" AND "n_regionkey" = "r_regionkey" AND "r_name" LIKE 'EUROP%' GROUP BY "s_i_id") AS "m" WHERE "i_id" = "s_i_id" AND "s_su_suppkey" = "su_suppkey" AND "su_nationkey" = "n_nationkey" AND "n_regionkey" = "r_regionkey" AND "i_data" LIKE '%b' AND "r_name" LIKE 'EUROP%' AND "i_id" = "m_i_id" AND "s_quantity" = "m_s_quantity" ORDER BY "n_name", "su_name", "i_id"

which is not very useful and can probably be made more readable. At the very least, we could strip out most of the quotes, and the database.schema qualifications.

benesch commented 4 years ago

Dis-nominating as a good second issue. There's a conversation to be had first about what the goal of SHOW CREATE VIEW is. I agree it's not exactly easy for humans to consume at the moment, but take a look at what MySQL does:

mysql> create view v1 as select 1;
Query OK, 0 rows affected (0.00 sec)

mysql> create view v2 as select 2;
Query OK, 0 rows affected (0.00 sec)

mysql> create view v as select * from v1 union all select * from v2;
ERROR 1146 (42S02): Table 'd.v1' doesn't exist

mysql> create view v as select * from v1 union all select * from v2;
Query OK, 0 rows affected (0.00 sec)

mysql> show create view v;
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View | Create View                                                                                                                                                        | character_set_client | collation_connection |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| v    | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `v1`.`1` AS `1` from `v1` union all select `v2`.`2` AS `2` from `v2` | utf8mb4              | utf8mb4_general_ci   |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

MySQL doesn't support schemas, and uses backticks instead of double quotes to quote identifiers, but it is fundamentally doing the same thing we are: canonicalizing the SQL statement. As it stands, the canonicalization makes the statement resilient to new keywords being added to the SQL parser and independent of the setting of the database and search_path session variables. It is going to be very difficult to preserve those properties and be concise.