[sql] Fixing datetime SQL literal (#8464)

This commit is contained in:
John Bodley
2019-10-29 23:24:48 -07:00
committed by GitHub
parent 7afda6e4f5
commit 0a3b121244
33 changed files with 436 additions and 82 deletions

View File

@@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.
from datetime import datetime
from typing import Optional
from urllib import parse
from superset.db_engine_specs.base import BaseEngineSpec
@@ -49,13 +50,13 @@ class DrillEngineSpec(BaseEngineSpec):
return "TO_DATE({col})"
@classmethod
def convert_dttm(cls, target_type: str, dttm: datetime) -> str:
def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]:
tt = target_type.upper()
if tt == "DATE":
return "CAST('{}' AS DATE)".format(dttm.isoformat()[:10])
return f"TO_DATE('{dttm.date().isoformat()}', 'yyyy-MM-dd')"
elif tt == "TIMESTAMP":
return "CAST('{}' AS TIMESTAMP)".format(dttm.strftime("%Y-%m-%d %H:%M:%S"))
return "'{}'".format(dttm.strftime("%Y-%m-%d %H:%M:%S"))
return f"""TO_TIMESTAMP('{dttm.isoformat(sep=" ", timespec="seconds")}', 'yyyy-MM-dd HH:mm:ss')""" # pylint: disable=line-too-long
return None
@classmethod
def adjust_database_uri(cls, uri, selected_schema):