sql_lab.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """A collection of ORM sqlalchemy models for SQL Lab"""
  18. import re
  19. from datetime import datetime
  20. # pylint: disable=ungrouped-imports
  21. import simplejson as json
  22. import sqlalchemy as sqla
  23. from flask import Markup
  24. from flask_appbuilder import Model
  25. from sqlalchemy import (
  26. Boolean,
  27. Column,
  28. DateTime,
  29. ForeignKey,
  30. Integer,
  31. Numeric,
  32. String,
  33. Text,
  34. )
  35. from sqlalchemy.orm import backref, relationship
  36. from superset import security_manager
  37. from superset.models.helpers import AuditMixinNullable, ExtraJSONMixin
  38. from superset.models.tags import QueryUpdater
  39. from superset.utils.core import QueryStatus, user_label
  40. class Query(Model, ExtraJSONMixin):
  41. """ORM model for SQL query
  42. Now that SQL Lab support multi-statement execution, an entry in this
  43. table may represent multiple SQL statements executed sequentially"""
  44. __tablename__ = "query"
  45. id = Column(Integer, primary_key=True) # pylint: disable=invalid-name
  46. client_id = Column(String(11), unique=True, nullable=False)
  47. database_id = Column(Integer, ForeignKey("dbs.id"), nullable=False)
  48. # Store the tmp table into the DB only if the user asks for it.
  49. tmp_table_name = Column(String(256))
  50. user_id = Column(Integer, ForeignKey("ab_user.id"), nullable=True)
  51. status = Column(String(16), default=QueryStatus.PENDING)
  52. tab_name = Column(String(256))
  53. sql_editor_id = Column(String(256))
  54. schema = Column(String(256))
  55. sql = Column(Text)
  56. # Query to retrieve the results,
  57. # used only in case of select_as_cta_used is true.
  58. select_sql = Column(Text)
  59. executed_sql = Column(Text)
  60. # Could be configured in the superset config.
  61. limit = Column(Integer)
  62. select_as_cta = Column(Boolean)
  63. select_as_cta_used = Column(Boolean, default=False)
  64. progress = Column(Integer, default=0) # 1..100
  65. # # of rows in the result set or rows modified.
  66. rows = Column(Integer)
  67. error_message = Column(Text)
  68. # key used to store the results in the results backend
  69. results_key = Column(String(64), index=True)
  70. # Using Numeric in place of DateTime for sub-second precision
  71. # stored as seconds since epoch, allowing for milliseconds
  72. start_time = Column(Numeric(precision=20, scale=6))
  73. start_running_time = Column(Numeric(precision=20, scale=6))
  74. end_time = Column(Numeric(precision=20, scale=6))
  75. end_result_backend_time = Column(Numeric(precision=20, scale=6))
  76. tracking_url = Column(Text)
  77. changed_on = Column(
  78. DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=True
  79. )
  80. database = relationship(
  81. "Database",
  82. foreign_keys=[database_id],
  83. backref=backref("queries", cascade="all, delete-orphan"),
  84. )
  85. user = relationship(security_manager.user_model, foreign_keys=[user_id])
  86. __table_args__ = (sqla.Index("ti_user_id_changed_on", user_id, changed_on),)
  87. def to_dict(self):
  88. return {
  89. "changedOn": self.changed_on,
  90. "changed_on": self.changed_on.isoformat(),
  91. "dbId": self.database_id,
  92. "db": self.database.database_name,
  93. "endDttm": self.end_time,
  94. "errorMessage": self.error_message,
  95. "executedSql": self.executed_sql,
  96. "id": self.client_id,
  97. "limit": self.limit,
  98. "progress": self.progress,
  99. "rows": self.rows,
  100. "schema": self.schema,
  101. "ctas": self.select_as_cta,
  102. "serverId": self.id,
  103. "sql": self.sql,
  104. "sqlEditorId": self.sql_editor_id,
  105. "startDttm": self.start_time,
  106. "state": self.status.lower(),
  107. "tab": self.tab_name,
  108. "tempTable": self.tmp_table_name,
  109. "userId": self.user_id,
  110. "user": user_label(self.user),
  111. "resultsKey": self.results_key,
  112. "trackingUrl": self.tracking_url,
  113. "extra": self.extra,
  114. }
  115. @property
  116. def name(self):
  117. """Name property"""
  118. ts = datetime.now().isoformat()
  119. ts = ts.replace("-", "").replace(":", "").split(".")[0]
  120. tab = self.tab_name.replace(" ", "_").lower() if self.tab_name else "notab"
  121. tab = re.sub(r"\W+", "", tab)
  122. return f"sqllab_{tab}_{ts}"
  123. @property
  124. def database_name(self):
  125. return self.database.name
  126. @property
  127. def username(self):
  128. return self.user.username
  129. class SavedQuery(Model, AuditMixinNullable, ExtraJSONMixin):
  130. """ORM model for SQL query"""
  131. __tablename__ = "saved_query"
  132. id = Column(Integer, primary_key=True) # pylint: disable=invalid-name
  133. user_id = Column(Integer, ForeignKey("ab_user.id"), nullable=True)
  134. db_id = Column(Integer, ForeignKey("dbs.id"), nullable=True)
  135. schema = Column(String(128))
  136. label = Column(String(256))
  137. description = Column(Text)
  138. sql = Column(Text)
  139. user = relationship(
  140. security_manager.user_model,
  141. backref=backref("saved_queries", cascade="all, delete-orphan"),
  142. foreign_keys=[user_id],
  143. )
  144. database = relationship(
  145. "Database",
  146. foreign_keys=[db_id],
  147. backref=backref("saved_queries", cascade="all, delete-orphan"),
  148. )
  149. @property
  150. def pop_tab_link(self):
  151. return Markup(
  152. f"""
  153. <a href="/superset/sqllab?savedQueryId={self.id}">
  154. <i class="fa fa-link"></i>
  155. </a>
  156. """
  157. )
  158. @property
  159. def user_email(self):
  160. return self.user.email
  161. @property
  162. def sqlalchemy_uri(self):
  163. return self.database.sqlalchemy_uri
  164. def url(self):
  165. return "/superset/sqllab?savedQueryId={0}".format(self.id)
  166. class TabState(Model, AuditMixinNullable, ExtraJSONMixin):
  167. __tablename__ = "tab_state"
  168. # basic info
  169. id = Column( # pylint: disable=invalid-name
  170. Integer, primary_key=True, autoincrement=True
  171. )
  172. user_id = Column(Integer, ForeignKey("ab_user.id"))
  173. label = Column(String(256))
  174. active = Column(Boolean, default=False)
  175. # selected DB and schema
  176. database_id = Column(Integer, ForeignKey("dbs.id"))
  177. database = relationship("Database", foreign_keys=[database_id])
  178. schema = Column(String(256))
  179. # tables that are open in the schema browser and their data previews
  180. table_schemas = relationship(
  181. "TableSchema",
  182. cascade="all, delete-orphan",
  183. backref="tab_state",
  184. passive_deletes=True,
  185. )
  186. # the query in the textarea, and results (if any)
  187. sql = Column(Text)
  188. query_limit = Column(Integer)
  189. # latest query that was run
  190. latest_query_id = Column(Integer, ForeignKey("query.client_id"))
  191. latest_query = relationship("Query")
  192. # other properties
  193. autorun = Column(Boolean, default=False)
  194. template_params = Column(Text)
  195. def to_dict(self):
  196. return {
  197. "id": self.id,
  198. "user_id": self.user_id,
  199. "label": self.label,
  200. "active": self.active,
  201. "database_id": self.database_id,
  202. "schema": self.schema,
  203. "table_schemas": [ts.to_dict() for ts in self.table_schemas],
  204. "sql": self.sql,
  205. "query_limit": self.query_limit,
  206. "latest_query": self.latest_query.to_dict() if self.latest_query else None,
  207. "autorun": self.autorun,
  208. "template_params": self.template_params,
  209. }
  210. class TableSchema(Model, AuditMixinNullable, ExtraJSONMixin):
  211. __tablename__ = "table_schema"
  212. id = Column( # pylint: disable=invalid-name
  213. Integer, primary_key=True, autoincrement=True
  214. )
  215. tab_state_id = Column(Integer, ForeignKey("tab_state.id", ondelete="CASCADE"))
  216. database_id = Column(Integer, ForeignKey("dbs.id"), nullable=False)
  217. database = relationship("Database", foreign_keys=[database_id])
  218. schema = Column(String(256))
  219. table = Column(String(256))
  220. # JSON describing the schema, partitions, latest partition, etc.
  221. description = Column(Text)
  222. expanded = Column(Boolean, default=False)
  223. def to_dict(self):
  224. try:
  225. description = json.loads(self.description)
  226. except json.JSONDecodeError:
  227. description = None
  228. return {
  229. "id": self.id,
  230. "tab_state_id": self.tab_state_id,
  231. "database_id": self.database_id,
  232. "schema": self.schema,
  233. "table": self.table,
  234. "description": description,
  235. "expanded": self.expanded,
  236. }
  237. # events for updating tags
  238. sqla.event.listen(SavedQuery, "after_insert", QueryUpdater.after_insert)
  239. sqla.event.listen(SavedQuery, "after_update", QueryUpdater.after_update)
  240. sqla.event.listen(SavedQuery, "after_delete", QueryUpdater.after_delete)