sqllab.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. .. http://www.apache.org/licenses/LICENSE-2.0
  9. .. Unless required by applicable law or agreed to in writing,
  10. software distributed under the License is distributed on an
  11. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND, either express or implied. See the License for the
  13. specific language governing permissions and limitations
  14. under the License.
  15. SQL Lab
  16. =======
  17. SQL Lab is a modern, feature-rich SQL IDE written in
  18. `React <https://facebook.github.io/react/>`_.
  19. ------
  20. .. image:: _static/images/screenshots/sqllab.png
  21. ------
  22. Feature Overview
  23. ----------------
  24. - Connects to just about any database backend
  25. - A multi-tab environment to work on multiple queries at a time
  26. - A smooth flow to visualize your query results using Superset's rich
  27. visualization capabilities
  28. - Browse database metadata: tables, columns, indexes, partitions
  29. - Support for long-running queries
  30. - uses the `Celery distributed queue <http://www.celeryproject.org/>`_
  31. to dispatch query handling to workers
  32. - supports defining a "results backend" to persist query results
  33. - A search engine to find queries executed in the past
  34. - Supports templating using the
  35. `Jinja templating language <http://jinja.pocoo.org/docs/dev/>`_
  36. which allows for using macros in your SQL code
  37. Extra features
  38. --------------
  39. - Hit ``alt + enter`` as a keyboard shortcut to run your query
  40. Templating with Jinja
  41. ---------------------
  42. .. code-block:: sql
  43. SELECT *
  44. FROM some_table
  45. WHERE partition_key = '{{ presto.first_latest_partition('some_table') }}'
  46. Templating unleashes the power and capabilities of a
  47. programming language within your SQL code.
  48. Templates can also be used to write generic queries that are
  49. parameterized so they can be re-used easily.
  50. Available macros
  51. ''''''''''''''''
  52. We expose certain modules from Python's standard library in
  53. Superset's Jinja context:
  54. - ``time``: ``time``
  55. - ``datetime``: ``datetime.datetime``
  56. - ``uuid``: ``uuid``
  57. - ``random``: ``random``
  58. - ``relativedelta``: ``dateutil.relativedelta.relativedelta``
  59. `Jinja's builtin filters <http://jinja.pocoo.org/docs/dev/templates/>`_ can be also be applied where needed.
  60. .. autofunction:: superset.jinja_context.current_user_id
  61. .. autofunction:: superset.jinja_context.current_username
  62. .. autofunction:: superset.jinja_context.url_param
  63. .. autofunction:: superset.jinja_context.filter_values
  64. .. autofunction:: superset.jinja_context.CacheKeyWrapper.cache_key_wrapper
  65. .. autoclass:: superset.jinja_context.PrestoTemplateProcessor
  66. :members:
  67. .. autoclass:: superset.jinja_context.HiveTemplateProcessor
  68. :members:
  69. Extending macros
  70. ''''''''''''''''
  71. As mentioned in the `Installation & Configuration <https://superset.incubator.apache.org/installation.html#installation-configuration>`_ documentation,
  72. it's possible for administrators to expose more more macros in their
  73. environment using the configuration variable ``JINJA_CONTEXT_ADDONS``.
  74. All objects referenced in this dictionary will become available for users
  75. to integrate in their queries in **SQL Lab**.
  76. Query cost estimation
  77. '''''''''''''''''''''
  78. Some databases support ``EXPLAIN`` queries that allow users to estimate the cost
  79. of queries before executing this. Currently, Presto is supported in SQL Lab. To
  80. enable query cost estimation, add the following keys to the "Extra" field in the
  81. database configuration:
  82. .. code-block:: text
  83. {
  84. "version": "0.319",
  85. "cost_estimate_enabled": true
  86. ...
  87. }
  88. Here, "version" should be the version of your Presto cluster. Support for this
  89. functionality was introduced in Presto 0.319.
  90. You also need to enable the feature flag in your `superset_config.py`, and you
  91. can optionally specify a custom formatter. Eg:
  92. .. code-block:: python
  93. def presto_query_cost_formatter(cost_estimate: List[Dict[str, float]]) -> List[Dict[str, str]]:
  94. """
  95. Format cost estimate returned by Presto.
  96. :param cost_estimate: JSON estimate from Presto
  97. :return: Human readable cost estimate
  98. """
  99. # Convert cost to dollars based on CPU and network cost. These coefficients are just
  100. # examples, they need to be estimated based on your infrastructure.
  101. cpu_coefficient = 2e-12
  102. network_coefficient = 1e-12
  103. cost = 0
  104. for row in cost_estimate:
  105. cost += row.get("cpuCost", 0) * cpu_coefficient
  106. cost += row.get("networkCost", 0) * network_coefficient
  107. return [{"Cost": f"US$ {cost:.2f}"}]
  108. DEFAULT_FEATURE_FLAGS = {
  109. "ESTIMATE_QUERY_COST": True,
  110. "QUERY_COST_FORMATTERS_BY_ENGINE": {"presto": presto_query_cost_formatter},
  111. }
  112. .. _ref_ctas_engine_config:
  113. Create Table As (CTAS)
  114. ''''''''''''''''''''''
  115. You can use ``CREATE TABLE AS SELECT ...`` statements on SQLLab. This feature can be toggled on
  116. and off at the database configuration level.
  117. Note that since ``CREATE TABLE..`` belongs to a SQL DDL category. Specifically on PostgreSQL, DDL is transactional,
  118. this means that to properly use this feature you have to set ``autocommit`` to true on your engine parameters:
  119. .. code-block:: text
  120. {
  121. ...
  122. "engine_params": {"isolation_level":"AUTOCOMMIT"},
  123. ...
  124. }