snowflake.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. from datetime import datetime
  18. from typing import Optional
  19. from urllib import parse
  20. from sqlalchemy.engine.url import URL
  21. from superset.db_engine_specs.postgres import PostgresBaseEngineSpec
  22. class SnowflakeEngineSpec(PostgresBaseEngineSpec):
  23. engine = "snowflake"
  24. force_column_alias_quotes = True
  25. max_column_name_length = 256
  26. _time_grain_functions = {
  27. None: "{col}",
  28. "PT1S": "DATE_TRUNC('SECOND', {col})",
  29. "PT1M": "DATE_TRUNC('MINUTE', {col})",
  30. "PT5M": "DATEADD(MINUTE, FLOOR(DATE_PART(MINUTE, {col}) / 5) * 5, \
  31. DATE_TRUNC('HOUR', {col}))",
  32. "PT10M": "DATEADD(MINUTE, FLOOR(DATE_PART(MINUTE, {col}) / 10) * 10, \
  33. DATE_TRUNC('HOUR', {col}))",
  34. "PT15M": "DATEADD(MINUTE, FLOOR(DATE_PART(MINUTE, {col}) / 15) * 15, \
  35. DATE_TRUNC('HOUR', {col}))",
  36. "PT0.5H": "DATEADD(MINUTE, FLOOR(DATE_PART(MINUTE, {col}) / 30) * 30, \
  37. DATE_TRUNC('HOUR', {col}))",
  38. "PT1H": "DATE_TRUNC('HOUR', {col})",
  39. "P1D": "DATE_TRUNC('DAY', {col})",
  40. "P1W": "DATE_TRUNC('WEEK', {col})",
  41. "P1M": "DATE_TRUNC('MONTH', {col})",
  42. "P0.25Y": "DATE_TRUNC('QUARTER', {col})",
  43. "P1Y": "DATE_TRUNC('YEAR', {col})",
  44. }
  45. @classmethod
  46. def adjust_database_uri(
  47. cls, uri: URL, selected_schema: Optional[str] = None
  48. ) -> None:
  49. database = uri.database
  50. if "/" in uri.database:
  51. database = uri.database.split("/")[0]
  52. if selected_schema:
  53. selected_schema = parse.quote(selected_schema, safe="")
  54. uri.database = database + "/" + selected_schema
  55. @classmethod
  56. def epoch_to_dttm(cls) -> str:
  57. return "DATEADD(S, {col}, '1970-01-01')"
  58. @classmethod
  59. def epoch_ms_to_dttm(cls) -> str:
  60. return "DATEADD(MS, {col}, '1970-01-01')"
  61. @classmethod
  62. def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
  63. tt = target_type.upper()
  64. if tt == "DATE":
  65. return f"TO_DATE('{dttm.date().isoformat()}')"
  66. if tt == "DATETIME":
  67. return f"""CAST('{dttm.isoformat(timespec="microseconds")}' AS DATETIME)"""
  68. if tt == "TIMESTAMP":
  69. return f"""TO_TIMESTAMP('{dttm.isoformat(timespec="microseconds")}')"""
  70. return None