Skip to content

CreateView Logical Operator

CreateView is a BinaryCommand logical operator that represents CREATE VIEW SQL statement to create a persisted (non-temporary) view.

CreateViewCommand

CreateViewCommand logical operator is used to represent CREATE TEMPORARY VIEW SQL statements.

CreateView is resolved into CreateViewCommand logical operator by ResolveSessionCatalog logical analysis rule.

Demo

sql("""
CREATE TEMPORARY VIEW demo_view
    COMMENT 'demo view'
    AS
        SELECT *
        FROM values (1), (2)
""")
scala> sql("SHOW VIEWS").show(truncate=false)
+---------+---------+-----------+
|namespace|viewName |isTemporary|
+---------+---------+-----------+
|default  |demo_view|false      |
|         |demo_view|true       |
+---------+---------+-----------+
sql("CREATE OR REPLACE TABLE nums USING delta AS SELECT * FROM values (1), (2) t(n)")
sql("""
CREATE OR REPLACE TEMPORARY VIEW nums_view
    COMMENT 'A view over nums table'
    AS
        SELECT *
        FROM nums
""")
scala> sql("DESC EXTENDED nums_view").show(truncate=false)
+--------+---------+-------+
|col_name|data_type|comment|
+--------+---------+-------+
|n       |int      |NULL   |
+--------+---------+-------+
scala> sql("SELECT * FROM nums_view").show()
+---+
|  n|
+---+
|  1|
|  2|
+---+

Let's cache the view. It does not seem to change anything, though.

"Why?!", you ask? This is likely because of Delta Lake sitting under the covers.

sql("CACHE TABLE nums_view")
assert(spark.catalog.isCached("nums_view"))
spark.range(3, 5).write.insertInto("nums")
scala> sql("SELECT * FROM nums_view ORDER BY n").show()
+---+
|  n|
+---+
|  1|
|  2|
|  3|
|  4|
+---+

Creating Instance

CreateView takes the following to be created:

  • Child LogicalPlan
  • User-specified columns (Seq[(String, Option[String])])
  • Optional Comment
  • Properties (Map[String, String])
  • Optional "Original Text"
  • Logical Query Plan
  • allowExisting flag
  • replace flag

CreateView is created when: