hana.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 superset.db_engine_specs.base import LimitMethod
  20. from superset.db_engine_specs.postgres import PostgresBaseEngineSpec
  21. class HanaEngineSpec(PostgresBaseEngineSpec):
  22. engine = "hana"
  23. limit_method = LimitMethod.WRAP_SQL
  24. force_column_alias_quotes = True
  25. max_column_name_length = 30
  26. _time_grain_functions = {
  27. None: "{col}",
  28. "PT1S": "TO_TIMESTAMP(SUBSTRING(TO_TIMESTAMP({col}),0,20))",
  29. "PT1M": "TO_TIMESTAMP(SUBSTRING(TO_TIMESTAMP({col}),0,17) || '00')",
  30. "PT1H": "TO_TIMESTAMP(SUBSTRING(TO_TIMESTAMP({col}),0,14) || '00:00')",
  31. "P1D": "TO_DATE({col})",
  32. "P1M": "TO_DATE(SUBSTRING(TO_DATE({col}),0,7)||'-01')",
  33. "P0.25Y": "TO_DATE(SUBSTRING( \
  34. TO_DATE({col}), 0, 5)|| LPAD(CAST((CAST(SUBSTRING(QUARTER( \
  35. TO_DATE({col}), 1), 7, 1) as int)-1)*3 +1 as text),2,'0') ||'-01')",
  36. "P1Y": "TO_DATE(YEAR({col})||'-01-01')",
  37. }
  38. @classmethod
  39. def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
  40. tt = target_type.upper()
  41. if tt == "DATE":
  42. return f"TO_DATE('{dttm.date().isoformat()}', 'YYYY-MM-DD')"
  43. if tt == "TIMESTAMP":
  44. return f"""TO_TIMESTAMP('{dttm.isoformat(timespec="microseconds")}', 'YYYY-MM-DD"T"HH24:MI:SS.ff6')""" # pylint: disable=line-too-long
  45. return None