impala.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 List, Optional
  19. from sqlalchemy.engine.reflection import Inspector
  20. from superset.db_engine_specs.base import BaseEngineSpec
  21. class ImpalaEngineSpec(BaseEngineSpec):
  22. """Engine spec for Cloudera's Impala"""
  23. engine = "impala"
  24. _time_grain_functions = {
  25. None: "{col}",
  26. "PT1M": "TRUNC({col}, 'MI')",
  27. "PT1H": "TRUNC({col}, 'HH')",
  28. "P1D": "TRUNC({col}, 'DD')",
  29. "P1W": "TRUNC({col}, 'WW')",
  30. "P1M": "TRUNC({col}, 'MONTH')",
  31. "P0.25Y": "TRUNC({col}, 'Q')",
  32. "P1Y": "TRUNC({col}, 'YYYY')",
  33. }
  34. @classmethod
  35. def epoch_to_dttm(cls) -> str:
  36. return "from_unixtime({col})"
  37. @classmethod
  38. def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
  39. tt = target_type.upper()
  40. if tt == "DATE":
  41. return f"CAST('{dttm.date().isoformat()}' AS DATE)"
  42. elif tt == "TIMESTAMP":
  43. return f"""CAST('{dttm.isoformat(timespec="microseconds")}' AS TIMESTAMP)"""
  44. return None
  45. @classmethod
  46. def get_schema_names(cls, inspector: Inspector) -> List[str]:
  47. schemas = [
  48. row[0]
  49. for row in inspector.engine.execute("SHOW SCHEMAS")
  50. if not row[0].startswith("_")
  51. ]
  52. return schemas