elasticsearch.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 Dict, Optional
  19. from superset.db_engine_specs.base import BaseEngineSpec
  20. class ElasticSearchEngineSpec(BaseEngineSpec): # pylint: disable=abstract-method
  21. engine = "elasticsearch"
  22. time_groupby_inline = True
  23. time_secondary_columns = True
  24. allows_joins = False
  25. allows_subqueries = True
  26. _time_grain_functions = {
  27. None: "{col}",
  28. "PT1S": "HISTOGRAM({col}, INTERVAL 1 SECOND)",
  29. "PT1M": "HISTOGRAM({col}, INTERVAL 1 MINUTE)",
  30. "PT1H": "HISTOGRAM({col}, INTERVAL 1 HOUR)",
  31. "P1D": "HISTOGRAM({col}, INTERVAL 1 DAY)",
  32. "P1M": "HISTOGRAM({col}, INTERVAL 1 MONTH)",
  33. "P1Y": "HISTOGRAM({col}, INTERVAL 1 YEAR)",
  34. }
  35. type_code_map: Dict[int, str] = {} # loaded from get_datatype only if needed
  36. @classmethod
  37. def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
  38. if target_type.upper() == "DATETIME":
  39. return f"""CAST('{dttm.isoformat(timespec="seconds")}' AS DATETIME)"""
  40. return None