2019-01-15 18:53:27 -05:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
# distributed with this work for additional information
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2019-06-24 01:37:41 -04:00
|
|
|
import unittest
|
2019-03-27 17:08:36 -04:00
|
|
|
from unittest import mock
|
2018-07-31 02:44:30 -04:00
|
|
|
|
2019-06-27 14:19:55 -04:00
|
|
|
import pandas as pd
|
|
|
|
from sqlalchemy import column, literal_column, table
|
2019-05-30 01:28:37 -04:00
|
|
|
from sqlalchemy.dialects import mssql, oracle, postgresql
|
2019-05-01 12:17:34 -04:00
|
|
|
from sqlalchemy.engine.result import RowProxy
|
2019-06-27 14:19:55 -04:00
|
|
|
from sqlalchemy.sql import select
|
2019-03-24 23:44:05 -04:00
|
|
|
from sqlalchemy.types import String, UnicodeText
|
2018-11-28 18:13:38 -05:00
|
|
|
|
2019-06-08 14:27:13 -04:00
|
|
|
from superset.db_engine_specs import engines
|
|
|
|
from superset.db_engine_specs.base import (
|
|
|
|
BaseEngineSpec,
|
|
|
|
builtin_time_grains,
|
|
|
|
create_time_grains_tuple,
|
2018-06-28 00:35:12 -04:00
|
|
|
)
|
2019-06-08 14:27:13 -04:00
|
|
|
from superset.db_engine_specs.bigquery import BigQueryEngineSpec
|
|
|
|
from superset.db_engine_specs.hive import HiveEngineSpec
|
|
|
|
from superset.db_engine_specs.mssql import MssqlEngineSpec
|
|
|
|
from superset.db_engine_specs.mysql import MySQLEngineSpec
|
|
|
|
from superset.db_engine_specs.oracle import OracleEngineSpec
|
|
|
|
from superset.db_engine_specs.pinot import PinotEngineSpec
|
|
|
|
from superset.db_engine_specs.postgres import PostgresEngineSpec
|
|
|
|
from superset.db_engine_specs.presto import PrestoEngineSpec
|
2018-05-14 15:44:05 -04:00
|
|
|
from superset.models.core import Database
|
|
|
|
from .base_tests import SupersetTestCase
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
|
2018-05-14 15:44:05 -04:00
|
|
|
class DbEngineSpecsTestCase(SupersetTestCase):
|
2017-03-06 19:20:55 -05:00
|
|
|
def test_0_progress(self):
|
2017-11-08 23:34:23 -05:00
|
|
|
log = """
|
2017-03-06 19:20:55 -05:00
|
|
|
17/02/07 18:26:27 INFO log.PerfLogger: <PERFLOG method=compile from=org.apache.hadoop.hive.ql.Driver>
|
|
|
|
17/02/07 18:26:27 INFO log.PerfLogger: <PERFLOG method=parse from=org.apache.hadoop.hive.ql.Driver>
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
|
|
|
self.assertEquals(0, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_number_of_jobs_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(0, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_job_1_launched_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 1 out of 2
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(0, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_job_1_launched_stage_1_0_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 1 out of 2
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 0%, reduce = 0%
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(0, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_job_1_launched_stage_1_map_40_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 1 out of 2
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 0%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 40%, reduce = 0%
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(10, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_job_1_launched_stage_1_map_80_reduce_40_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 1 out of 2
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 0%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 40%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 80%, reduce = 40%
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(30, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_job_1_launched_stage_2_stages_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 1 out of 2
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 0%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 40%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 80%, reduce = 40%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-2 map = 0%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 100%, reduce = 0%
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(12, HiveEngineSpec.progress(log))
|
2017-03-06 19:20:55 -05:00
|
|
|
|
|
|
|
def test_job_2_launched_stage_2_stages_progress(self):
|
|
|
|
log = """
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Total jobs = 2
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 1 out of 2
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 100%, reduce = 0%
|
|
|
|
17/02/07 19:15:55 INFO ql.Driver: Launching Job 2 out of 2
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 0%, reduce = 0%
|
|
|
|
17/02/07 19:16:09 INFO exec.Task: 2017-02-07 19:16:09,173 Stage-1 map = 40%, reduce = 0%
|
2019-06-25 16:34:48 -04:00
|
|
|
""".split(
|
|
|
|
"\n"
|
|
|
|
)
|
2017-07-27 17:00:19 -04:00
|
|
|
self.assertEquals(60, HiveEngineSpec.progress(log))
|
2018-05-14 15:44:05 -04:00
|
|
|
|
2018-07-26 18:17:55 -04:00
|
|
|
def test_hive_error_msg(self):
|
|
|
|
msg = (
|
|
|
|
'{...} errorMessage="Error while compiling statement: FAILED: '
|
2019-06-25 16:34:48 -04:00
|
|
|
"SemanticException [Error 10001]: Line 4"
|
|
|
|
":5 Table not found 'fact_ridesfdslakj'\", statusCode=3, "
|
|
|
|
"sqlState='42S02', errorCode=10001)){...}"
|
|
|
|
)
|
|
|
|
self.assertEquals(
|
|
|
|
(
|
|
|
|
"Error while compiling statement: FAILED: "
|
|
|
|
"SemanticException [Error 10001]: Line 4:5 "
|
|
|
|
"Table not found 'fact_ridesfdslakj'"
|
|
|
|
),
|
|
|
|
HiveEngineSpec.extract_error_message(Exception(msg)),
|
|
|
|
)
|
2018-07-26 18:17:55 -04:00
|
|
|
|
|
|
|
e = Exception("Some string that doesn't match the regex")
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEquals(str(e), HiveEngineSpec.extract_error_message(e))
|
2018-07-26 18:17:55 -04:00
|
|
|
|
2018-07-27 11:31:37 -04:00
|
|
|
msg = (
|
2019-06-25 16:34:48 -04:00
|
|
|
"errorCode=10001, "
|
2018-07-27 11:31:37 -04:00
|
|
|
'errorMessage="Error while compiling statement"), operationHandle'
|
|
|
|
'=None)"'
|
|
|
|
)
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEquals(
|
|
|
|
("Error while compiling statement"),
|
|
|
|
HiveEngineSpec.extract_error_message(Exception(msg)),
|
|
|
|
)
|
2018-07-27 11:31:37 -04:00
|
|
|
|
2018-05-14 15:44:05 -04:00
|
|
|
def get_generic_database(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
return Database(sqlalchemy_uri="sqlite://")
|
2018-05-14 15:44:05 -04:00
|
|
|
|
|
|
|
def sql_limit_regex(
|
2019-06-25 16:34:48 -04:00
|
|
|
self, sql, expected_sql, engine_spec_class=MySQLEngineSpec, limit=1000
|
|
|
|
):
|
2018-05-14 15:44:05 -04:00
|
|
|
main = self.get_generic_database()
|
|
|
|
limited = engine_spec_class.apply_limit_to_sql(sql, limit, main)
|
|
|
|
self.assertEquals(expected_sql, limited)
|
|
|
|
|
2018-05-25 18:45:11 -04:00
|
|
|
def test_extract_limit_from_query(self, engine_spec_class=MySQLEngineSpec):
|
2019-06-25 16:34:48 -04:00
|
|
|
q0 = "select * from table"
|
|
|
|
q1 = "select * from mytable limit 10"
|
|
|
|
q2 = "select * from (select * from my_subquery limit 10) where col=1 limit 20"
|
|
|
|
q3 = "select * from (select * from my_subquery limit 10);"
|
|
|
|
q4 = "select * from (select * from my_subquery limit 10) where col=1 limit 20;"
|
|
|
|
q5 = "select * from mytable limit 10, 20"
|
|
|
|
q6 = "select * from mytable limit 10 offset 20"
|
|
|
|
q7 = "select * from mytable limit"
|
|
|
|
q8 = "select * from mytable limit 10.0"
|
|
|
|
q9 = "select * from mytable limit x"
|
|
|
|
q10 = "select * from mytable limit x, 20"
|
|
|
|
q11 = "select * from mytable limit x offset 20"
|
2018-05-25 18:45:11 -04:00
|
|
|
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q0), None)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q1), 10)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q2), 20)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q3), None)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q4), 20)
|
2019-03-05 12:36:08 -05:00
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q5), 10)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q6), 10)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q7), None)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q8), None)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q9), None)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q10), None)
|
|
|
|
self.assertEqual(engine_spec_class.get_limit_from_sql(q11), None)
|
2018-05-25 18:45:11 -04:00
|
|
|
|
2018-05-14 15:44:05 -04:00
|
|
|
def test_wrapped_query(self):
|
|
|
|
self.sql_limit_regex(
|
2019-06-25 16:34:48 -04:00
|
|
|
"SELECT * FROM a",
|
|
|
|
"SELECT * \nFROM (SELECT * FROM a) AS inner_qry\n LIMIT 1000 OFFSET 0",
|
2018-05-14 15:44:05 -04:00
|
|
|
MssqlEngineSpec,
|
|
|
|
)
|
|
|
|
|
2019-06-24 01:37:41 -04:00
|
|
|
@unittest.skipUnless(
|
2019-06-25 16:34:48 -04:00
|
|
|
SupersetTestCase.is_module_installed("MySQLdb"), "mysqlclient not installed"
|
|
|
|
)
|
2018-05-14 15:44:05 -04:00
|
|
|
def test_wrapped_semi_tabs(self):
|
|
|
|
self.sql_limit_regex(
|
2019-06-25 16:34:48 -04:00
|
|
|
"SELECT * FROM a \t \n ; \t \n ", "SELECT * FROM a\nLIMIT 1000"
|
2018-05-14 15:44:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_simple_limit_query(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.sql_limit_regex("SELECT * FROM a", "SELECT * FROM a\nLIMIT 1000")
|
2018-05-14 15:44:05 -04:00
|
|
|
|
|
|
|
def test_modify_limit_query(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.sql_limit_regex("SELECT * FROM a LIMIT 9999", "SELECT * FROM a LIMIT 1000")
|
2018-05-14 15:44:05 -04:00
|
|
|
|
|
|
|
def test_limit_query_with_limit_subquery(self):
|
|
|
|
self.sql_limit_regex(
|
2019-06-25 16:34:48 -04:00
|
|
|
"SELECT * FROM (SELECT * FROM a LIMIT 10) LIMIT 9999",
|
|
|
|
"SELECT * FROM (SELECT * FROM a LIMIT 10) LIMIT 1000",
|
2018-05-14 15:44:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_limit_with_expr(self):
|
|
|
|
self.sql_limit_regex(
|
2018-07-16 18:27:30 -04:00
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
|
|
|
table
|
|
|
|
LIMIT 99990""",
|
2019-01-17 12:16:35 -05:00
|
|
|
"""SELECT
|
2018-05-14 15:44:05 -04:00
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
2018-07-16 18:27:30 -04:00
|
|
|
table
|
|
|
|
LIMIT 1000""",
|
2018-05-14 15:44:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_limit_expr_and_semicolon(self):
|
|
|
|
self.sql_limit_regex(
|
2018-07-16 18:27:30 -04:00
|
|
|
"""
|
2018-05-14 15:44:05 -04:00
|
|
|
SELECT
|
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
|
|
|
table
|
2018-07-16 18:27:30 -04:00
|
|
|
LIMIT 99990 ;""",
|
2019-01-17 12:16:35 -05:00
|
|
|
"""SELECT
|
2018-05-14 15:44:05 -04:00
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
2018-07-16 18:27:30 -04:00
|
|
|
table
|
2019-01-17 12:16:35 -05:00
|
|
|
LIMIT 1000""",
|
2018-05-14 15:44:05 -04:00
|
|
|
)
|
2018-06-28 00:35:12 -04:00
|
|
|
|
2019-06-24 01:37:41 -04:00
|
|
|
@unittest.skipUnless(
|
2019-06-25 16:34:48 -04:00
|
|
|
SupersetTestCase.is_module_installed("MySQLdb"), "mysqlclient not installed"
|
|
|
|
)
|
2019-06-24 01:37:41 -04:00
|
|
|
def test_get_datatype_mysql(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEquals("TINY", MySQLEngineSpec.get_datatype(1))
|
|
|
|
self.assertEquals("VARCHAR", MySQLEngineSpec.get_datatype(15))
|
2019-06-24 01:37:41 -04:00
|
|
|
|
|
|
|
@unittest.skipUnless(
|
2019-06-25 16:34:48 -04:00
|
|
|
SupersetTestCase.is_module_installed("pyhive"), "pyhive not installed"
|
|
|
|
)
|
2019-06-24 01:37:41 -04:00
|
|
|
def test_get_datatype_presto(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEquals("STRING", PrestoEngineSpec.get_datatype("string"))
|
2019-06-24 01:37:41 -04:00
|
|
|
|
|
|
|
def test_get_datatype(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEquals("VARCHAR", BaseEngineSpec.get_datatype("VARCHAR"))
|
2018-07-16 18:27:30 -04:00
|
|
|
|
|
|
|
def test_limit_with_implicit_offset(self):
|
|
|
|
self.sql_limit_regex(
|
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
|
|
|
table
|
|
|
|
LIMIT 99990, 999999""",
|
2019-01-17 12:16:35 -05:00
|
|
|
"""SELECT
|
2018-07-16 18:27:30 -04:00
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
|
|
|
table
|
|
|
|
LIMIT 99990, 1000""",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_limit_with_explicit_offset(self):
|
|
|
|
self.sql_limit_regex(
|
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
|
|
|
table
|
|
|
|
LIMIT 99990
|
|
|
|
OFFSET 999999""",
|
2019-01-17 12:16:35 -05:00
|
|
|
"""SELECT
|
2018-07-16 18:27:30 -04:00
|
|
|
'LIMIT 777' AS a
|
|
|
|
, b
|
|
|
|
FROM
|
|
|
|
table
|
|
|
|
LIMIT 1000
|
|
|
|
OFFSET 999999""",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_limit_with_non_token_limit(self):
|
|
|
|
self.sql_limit_regex(
|
2019-06-25 16:34:48 -04:00
|
|
|
"""SELECT 'LIMIT 777'""", """SELECT 'LIMIT 777'\nLIMIT 1000"""
|
2018-07-16 18:27:30 -04:00
|
|
|
)
|
2018-07-31 02:44:30 -04:00
|
|
|
|
|
|
|
def test_time_grain_blacklist(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
blacklist = ["PT1M"]
|
|
|
|
time_grains = {"PT1S": "second", "PT1M": "minute"}
|
|
|
|
time_grain_functions = {"PT1S": "{col}", "PT1M": "{col}"}
|
|
|
|
time_grains = create_time_grains_tuple(
|
|
|
|
time_grains, time_grain_functions, blacklist
|
|
|
|
)
|
2018-07-31 02:44:30 -04:00
|
|
|
self.assertEqual(1, len(time_grains))
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual("PT1S", time_grains[0].duration)
|
2018-07-31 02:44:30 -04:00
|
|
|
|
|
|
|
def test_engine_time_grain_validity(self):
|
2019-06-08 14:27:13 -04:00
|
|
|
time_grains = set(builtin_time_grains.keys())
|
2018-07-31 02:44:30 -04:00
|
|
|
# loop over all subclasses of BaseEngineSpec
|
2019-06-08 14:27:13 -04:00
|
|
|
for engine in engines.values():
|
|
|
|
if engine is not BaseEngineSpec:
|
2018-12-16 11:53:29 -05:00
|
|
|
# make sure time grain functions have been defined
|
2019-06-08 14:27:13 -04:00
|
|
|
self.assertGreater(len(engine.time_grain_functions), 0)
|
2018-07-31 02:44:30 -04:00
|
|
|
# make sure that all defined time grains are supported
|
2019-06-08 14:27:13 -04:00
|
|
|
defined_grains = {grain.duration for grain in engine.get_time_grains()}
|
|
|
|
intersection = time_grains.intersection(defined_grains)
|
|
|
|
self.assertSetEqual(defined_grains, intersection, engine)
|
2018-11-28 18:13:38 -05:00
|
|
|
|
|
|
|
def test_presto_get_view_names_return_empty_list(self):
|
|
|
|
self.assertEquals([], PrestoEngineSpec.get_view_names(mock.ANY, mock.ANY))
|
|
|
|
|
2019-05-01 12:17:34 -04:00
|
|
|
def verify_presto_column(self, column, expected_results):
|
|
|
|
inspector = mock.Mock()
|
|
|
|
inspector.engine.dialect.identifier_preparer.quote_identifier = mock.Mock()
|
2019-06-25 16:34:48 -04:00
|
|
|
keymap = {
|
|
|
|
"Column": (None, None, 0),
|
|
|
|
"Type": (None, None, 1),
|
|
|
|
"Null": (None, None, 2),
|
|
|
|
}
|
2019-05-01 12:17:34 -04:00
|
|
|
row = RowProxy(mock.Mock(), column, [None, None, None, None], keymap)
|
|
|
|
inspector.bind.execute = mock.Mock(return_value=[row])
|
2019-06-25 16:34:48 -04:00
|
|
|
results = PrestoEngineSpec.get_columns(inspector, "", "")
|
2019-05-01 12:17:34 -04:00
|
|
|
self.assertEqual(len(expected_results), len(results))
|
|
|
|
for expected_result, result in zip(expected_results, results):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(expected_result[0], result["name"])
|
|
|
|
self.assertEqual(expected_result[1], str(result["type"]))
|
2019-05-01 12:17:34 -04:00
|
|
|
|
|
|
|
def test_presto_get_column(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
presto_column = ("column_name", "boolean", "")
|
|
|
|
expected_results = [("column_name", "BOOLEAN")]
|
2019-05-01 12:17:34 -04:00
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
|
|
|
def test_presto_get_simple_row_column(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
presto_column = ("column_name", "row(nested_obj double)", "")
|
|
|
|
expected_results = [("column_name", "ROW"), ("column_name.nested_obj", "FLOAT")]
|
2019-05-01 12:17:34 -04:00
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
2019-05-31 16:38:05 -04:00
|
|
|
def test_presto_get_simple_row_column_with_name_containing_whitespace(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
presto_column = ("column name", "row(nested_obj double)", "")
|
|
|
|
expected_results = [("column name", "ROW"), ("column name.nested_obj", "FLOAT")]
|
2019-05-31 16:38:05 -04:00
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
|
|
|
def test_presto_get_simple_row_column_with_tricky_nested_field_name(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
presto_column = ("column_name", 'row("Field Name(Tricky, Name)" double)', "")
|
2019-05-01 12:17:34 -04:00
|
|
|
expected_results = [
|
2019-06-25 16:34:48 -04:00
|
|
|
("column_name", "ROW"),
|
|
|
|
('column_name."Field Name(Tricky, Name)"', "FLOAT"),
|
|
|
|
]
|
2019-05-01 12:17:34 -04:00
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
|
|
|
def test_presto_get_simple_array_column(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
presto_column = ("column_name", "array(double)", "")
|
|
|
|
expected_results = [("column_name", "ARRAY")]
|
2019-05-01 12:17:34 -04:00
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
|
|
|
def test_presto_get_row_within_array_within_row_column(self):
|
|
|
|
presto_column = (
|
2019-06-25 16:34:48 -04:00
|
|
|
"column_name",
|
|
|
|
"row(nested_array array(row(nested_row double)), nested_obj double)",
|
|
|
|
"",
|
|
|
|
)
|
2019-05-01 12:17:34 -04:00
|
|
|
expected_results = [
|
2019-06-25 16:34:48 -04:00
|
|
|
("column_name", "ROW"),
|
|
|
|
("column_name.nested_array", "ARRAY"),
|
|
|
|
("column_name.nested_array.nested_row", "FLOAT"),
|
|
|
|
("column_name.nested_obj", "FLOAT"),
|
2019-05-01 12:17:34 -04:00
|
|
|
]
|
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
|
|
|
def test_presto_get_array_within_row_within_array_column(self):
|
|
|
|
presto_column = (
|
2019-06-25 16:34:48 -04:00
|
|
|
"column_name",
|
|
|
|
"array(row(nested_array array(double), nested_obj double))",
|
|
|
|
"",
|
|
|
|
)
|
2019-05-01 12:17:34 -04:00
|
|
|
expected_results = [
|
2019-06-25 16:34:48 -04:00
|
|
|
("column_name", "ARRAY"),
|
|
|
|
("column_name.nested_array", "ARRAY"),
|
|
|
|
("column_name.nested_obj", "FLOAT"),
|
|
|
|
]
|
2019-05-01 12:17:34 -04:00
|
|
|
self.verify_presto_column(presto_column, expected_results)
|
|
|
|
|
2019-05-03 19:29:57 -04:00
|
|
|
def test_presto_get_fields(self):
|
|
|
|
cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "column"},
|
|
|
|
{"name": "column.nested_obj"},
|
|
|
|
{"name": 'column."quoted.nested obj"'},
|
|
|
|
]
|
2019-05-03 19:29:57 -04:00
|
|
|
actual_results = PrestoEngineSpec._get_fields(cols)
|
|
|
|
expected_results = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": '"column"', "label": "column"},
|
|
|
|
{"name": '"column"."nested_obj"', "label": "column.nested_obj"},
|
|
|
|
{
|
|
|
|
"name": '"column"."quoted.nested obj"',
|
|
|
|
"label": 'column."quoted.nested obj"',
|
|
|
|
},
|
|
|
|
]
|
2019-05-03 19:29:57 -04:00
|
|
|
for actual_result, expected_result in zip(actual_results, expected_results):
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(actual_result.element.name, expected_result["name"])
|
|
|
|
self.assertEqual(actual_result.name, expected_result["label"])
|
2019-05-03 19:29:57 -04:00
|
|
|
|
2019-05-31 14:25:07 -04:00
|
|
|
def test_presto_filter_out_array_nested_cols(self):
|
2019-05-03 19:29:57 -04:00
|
|
|
cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "column", "type": "ARRAY"},
|
|
|
|
{"name": "column.nested_obj", "type": "FLOAT"},
|
|
|
|
]
|
|
|
|
actual_filtered_cols, actual_array_cols = PrestoEngineSpec._filter_out_array_nested_cols( # noqa ignore: E50
|
|
|
|
cols
|
|
|
|
)
|
|
|
|
expected_filtered_cols = [{"name": "column", "type": "ARRAY"}]
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_filtered_cols, expected_filtered_cols)
|
|
|
|
self.assertEqual(actual_array_cols, cols)
|
|
|
|
|
|
|
|
def test_presto_create_row_and_array_hierarchy(self):
|
|
|
|
cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{
|
|
|
|
"name": "row_column",
|
|
|
|
"type": "ROW(NESTED_OBJ1 VARCHAR, NESTED_ROW ROW(NESTED_OBJ2 VARCHAR)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "array_column",
|
|
|
|
"type": "ARRAY(ROW(NESTED_ARRAY ARRAY(ROW(NESTED_OBJ VARCHAR))))",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
actual_row_col_hierarchy, actual_array_col_hierarchy, actual_expanded_cols = PrestoEngineSpec._create_row_and_array_hierarchy( # noqa ignore: E50
|
|
|
|
cols
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_row_col_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"row_column": {
|
|
|
|
"type": "ROW",
|
|
|
|
"children": ["row_column.nested_obj1", "row_column.nested_row"],
|
|
|
|
},
|
|
|
|
"row_column.nested_row": {
|
|
|
|
"type": "ROW",
|
|
|
|
"children": ["row_column.nested_row.nested_obj2"],
|
2019-05-31 14:25:07 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
expected_array_col_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"array_column": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array"],
|
|
|
|
},
|
|
|
|
"array_column.nested_array": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array.nested_obj"],
|
2019-05-31 14:25:07 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
expected_expanded_cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "row_column.nested_obj1", "type": "VARCHAR"},
|
|
|
|
{"name": "row_column.nested_row", "type": "ROW"},
|
|
|
|
{"name": "row_column.nested_row.nested_obj2", "type": "VARCHAR"},
|
|
|
|
{"name": "array_column.nested_array", "type": "ARRAY"},
|
|
|
|
{"name": "array_column.nested_array.nested_obj", "type": "VARCHAR"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_row_col_hierarchy, expected_row_col_hierarchy)
|
|
|
|
self.assertEqual(actual_array_col_hierarchy, expected_array_col_hierarchy)
|
|
|
|
self.assertEqual(actual_expanded_cols, expected_expanded_cols)
|
|
|
|
|
|
|
|
def test_presto_expand_row_data(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
datum = {"row_col": [1, "a"]}
|
|
|
|
row_column = "row_col"
|
2019-05-31 14:25:07 -04:00
|
|
|
row_col_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"row_col": {
|
|
|
|
"type": "ROW",
|
|
|
|
"children": ["row_col.nested_int", "row_col.nested_str"],
|
|
|
|
}
|
2019-05-31 14:25:07 -04:00
|
|
|
}
|
|
|
|
PrestoEngineSpec._expand_row_data(datum, row_column, row_col_hierarchy)
|
|
|
|
expected_datum = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"row_col": [1, "a"],
|
|
|
|
"row_col.nested_int": 1,
|
|
|
|
"row_col.nested_str": "a",
|
2019-05-31 14:25:07 -04:00
|
|
|
}
|
|
|
|
self.assertEqual(datum, expected_datum)
|
|
|
|
|
|
|
|
def test_split_array_columns_by_process_state(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
array_cols = ["array_column", "array_column.nested_array"]
|
2019-05-31 14:25:07 -04:00
|
|
|
array_col_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"array_column": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array"],
|
|
|
|
},
|
|
|
|
"array_column.nested_array": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array.nested_obj"],
|
2019-05-31 14:25:07 -04:00
|
|
|
},
|
|
|
|
}
|
2019-06-25 16:34:48 -04:00
|
|
|
datum = {"array_column": [[[1], [2]]]}
|
|
|
|
actual_array_cols_to_process, actual_unprocessed_array_cols = PrestoEngineSpec._split_array_columns_by_process_state( # noqa ignore: E50
|
|
|
|
array_cols, array_col_hierarchy, datum
|
|
|
|
)
|
|
|
|
expected_array_cols_to_process = ["array_column"]
|
|
|
|
expected_unprocessed_array_cols = {"array_column.nested_array"}
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_array_cols_to_process, expected_array_cols_to_process)
|
|
|
|
self.assertEqual(actual_unprocessed_array_cols, expected_unprocessed_array_cols)
|
|
|
|
|
|
|
|
def test_presto_convert_data_list_to_array_data_dict(self):
|
|
|
|
data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"array_column": [1, 2], "int_column": 3},
|
|
|
|
{"array_column": [11, 22], "int_column": 33},
|
2019-05-31 14:25:07 -04:00
|
|
|
]
|
2019-06-25 16:34:48 -04:00
|
|
|
array_columns_to_process = ["array_column"]
|
2019-05-31 14:25:07 -04:00
|
|
|
actual_array_data_dict = PrestoEngineSpec._convert_data_list_to_array_data_dict(
|
2019-06-25 16:34:48 -04:00
|
|
|
data, array_columns_to_process
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_array_data_dict = {
|
2019-06-25 16:34:48 -04:00
|
|
|
0: [{"array_column": [1, 2]}],
|
|
|
|
1: [{"array_column": [11, 22]}],
|
|
|
|
}
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_array_data_dict, expected_array_data_dict)
|
|
|
|
|
|
|
|
def test_presto_process_array_data(self):
|
|
|
|
data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"array_column": [[1], [2]], "int_column": 3},
|
|
|
|
{"array_column": [[11], [22]], "int_column": 33},
|
2019-05-31 14:25:07 -04:00
|
|
|
]
|
|
|
|
all_columns = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "array_column", "type": "ARRAY"},
|
|
|
|
{"name": "array_column.nested_row", "type": "BIGINT"},
|
|
|
|
{"name": "int_column", "type": "BIGINT"},
|
2019-05-31 14:25:07 -04:00
|
|
|
]
|
|
|
|
array_column_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"array_column": {"type": "ARRAY", "children": ["array_column.nested_row"]}
|
2019-05-31 14:25:07 -04:00
|
|
|
}
|
|
|
|
actual_array_data = PrestoEngineSpec._process_array_data(
|
2019-06-25 16:34:48 -04:00
|
|
|
data, all_columns, array_column_hierarchy
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_array_data = {
|
|
|
|
0: [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"array_column": [[1], [2]], "array_column.nested_row": 1},
|
|
|
|
{"array_column": "", "array_column.nested_row": 2, "int_column": ""},
|
2019-05-31 14:25:07 -04:00
|
|
|
],
|
|
|
|
1: [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"array_column": [[11], [22]], "array_column.nested_row": 11},
|
|
|
|
{"array_column": "", "array_column.nested_row": 22, "int_column": ""},
|
2019-05-31 14:25:07 -04:00
|
|
|
],
|
|
|
|
}
|
|
|
|
self.assertEqual(actual_array_data, expected_array_data)
|
|
|
|
|
|
|
|
def test_presto_consolidate_array_data_into_data(self):
|
|
|
|
data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"arr_col": [[1], [2]], "int_col": 3},
|
|
|
|
{"arr_col": [[11], [22]], "int_col": 33},
|
2019-05-31 14:25:07 -04:00
|
|
|
]
|
|
|
|
array_data = {
|
|
|
|
0: [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"arr_col": [[1], [2]], "arr_col.nested_row": 1},
|
|
|
|
{"arr_col": "", "arr_col.nested_row": 2, "int_col": ""},
|
2019-05-31 14:25:07 -04:00
|
|
|
],
|
|
|
|
1: [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"arr_col": [[11], [22]], "arr_col.nested_row": 11},
|
|
|
|
{"arr_col": "", "arr_col.nested_row": 22, "int_col": ""},
|
2019-05-31 14:25:07 -04:00
|
|
|
],
|
|
|
|
}
|
|
|
|
PrestoEngineSpec._consolidate_array_data_into_data(data, array_data)
|
|
|
|
expected_data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"arr_col": [[1], [2]], "arr_col.nested_row": 1, "int_col": 3},
|
|
|
|
{"arr_col": "", "arr_col.nested_row": 2, "int_col": ""},
|
|
|
|
{"arr_col": [[11], [22]], "arr_col.nested_row": 11, "int_col": 33},
|
|
|
|
{"arr_col": "", "arr_col.nested_row": 22, "int_col": ""},
|
2019-05-31 14:25:07 -04:00
|
|
|
]
|
|
|
|
self.assertEqual(data, expected_data)
|
|
|
|
|
|
|
|
def test_presto_remove_processed_array_columns(self):
|
|
|
|
array_col_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"array_column": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array"],
|
|
|
|
},
|
|
|
|
"array_column.nested_array": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array.nested_obj"],
|
2019-05-31 14:25:07 -04:00
|
|
|
},
|
|
|
|
}
|
2019-06-25 16:34:48 -04:00
|
|
|
unprocessed_array_cols = {"array_column.nested_array"}
|
2019-05-31 14:25:07 -04:00
|
|
|
PrestoEngineSpec._remove_processed_array_columns(
|
2019-06-25 16:34:48 -04:00
|
|
|
unprocessed_array_cols, array_col_hierarchy
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_array_col_hierarchy = {
|
2019-06-25 16:34:48 -04:00
|
|
|
"array_column.nested_array": {
|
|
|
|
"type": "ARRAY",
|
|
|
|
"children": ["array_column.nested_array.nested_obj"],
|
|
|
|
}
|
2019-05-31 14:25:07 -04:00
|
|
|
}
|
|
|
|
self.assertEqual(array_col_hierarchy, expected_array_col_hierarchy)
|
|
|
|
|
|
|
|
def test_presto_expand_data_with_simple_structural_columns(self):
|
|
|
|
cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "row_column", "type": "ROW(NESTED_OBJ VARCHAR)"},
|
|
|
|
{"name": "array_column", "type": "ARRAY(BIGINT)"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"row_column": ["a"], "array_column": [1, 2, 3]},
|
|
|
|
{"row_column": ["b"], "array_column": [4, 5, 6]},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
actual_cols, actual_data, actual_expanded_cols = PrestoEngineSpec.expand_data(
|
2019-06-25 16:34:48 -04:00
|
|
|
cols, data
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "row_column", "type": "ROW"},
|
|
|
|
{"name": "row_column.nested_obj", "type": "VARCHAR"},
|
|
|
|
{"name": "array_column", "type": "ARRAY"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"row_column": ["a"], "row_column.nested_obj": "a", "array_column": 1},
|
|
|
|
{"row_column": "", "row_column.nested_obj": "", "array_column": 2},
|
|
|
|
{"row_column": "", "row_column.nested_obj": "", "array_column": 3},
|
|
|
|
{"row_column": ["b"], "row_column.nested_obj": "b", "array_column": 4},
|
|
|
|
{"row_column": "", "row_column.nested_obj": "", "array_column": 5},
|
|
|
|
{"row_column": "", "row_column.nested_obj": "", "array_column": 6},
|
|
|
|
]
|
|
|
|
expected_expanded_cols = [{"name": "row_column.nested_obj", "type": "VARCHAR"}]
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_cols, expected_cols)
|
|
|
|
self.assertEqual(actual_data, expected_data)
|
|
|
|
self.assertEqual(actual_expanded_cols, expected_expanded_cols)
|
|
|
|
|
|
|
|
def test_presto_expand_data_with_complex_row_columns(self):
|
|
|
|
cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{
|
|
|
|
"name": "row_column",
|
|
|
|
"type": "ROW(NESTED_OBJ1 VARCHAR, NESTED_ROW ROW(NESTED_OBJ2 VARCHAR)",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
data = [{"row_column": ["a1", ["a2"]]}, {"row_column": ["b1", ["b2"]]}]
|
2019-05-31 14:25:07 -04:00
|
|
|
actual_cols, actual_data, actual_expanded_cols = PrestoEngineSpec.expand_data(
|
2019-06-25 16:34:48 -04:00
|
|
|
cols, data
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "row_column", "type": "ROW"},
|
|
|
|
{"name": "row_column.nested_obj1", "type": "VARCHAR"},
|
|
|
|
{"name": "row_column.nested_row", "type": "ROW"},
|
|
|
|
{"name": "row_column.nested_row.nested_obj2", "type": "VARCHAR"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{
|
|
|
|
"row_column": ["a1", ["a2"]],
|
|
|
|
"row_column.nested_obj1": "a1",
|
|
|
|
"row_column.nested_row": ["a2"],
|
|
|
|
"row_column.nested_row.nested_obj2": "a2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"row_column": ["b1", ["b2"]],
|
|
|
|
"row_column.nested_obj1": "b1",
|
|
|
|
"row_column.nested_row": ["b2"],
|
|
|
|
"row_column.nested_row.nested_obj2": "b2",
|
|
|
|
},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_expanded_cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "row_column.nested_obj1", "type": "VARCHAR"},
|
|
|
|
{"name": "row_column.nested_row", "type": "ROW"},
|
|
|
|
{"name": "row_column.nested_row.nested_obj2", "type": "VARCHAR"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_cols, expected_cols)
|
|
|
|
self.assertEqual(actual_data, expected_data)
|
|
|
|
self.assertEqual(actual_expanded_cols, expected_expanded_cols)
|
|
|
|
|
|
|
|
def test_presto_expand_data_with_complex_array_columns(self):
|
|
|
|
cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "int_column", "type": "BIGINT"},
|
|
|
|
{
|
|
|
|
"name": "array_column",
|
|
|
|
"type": "ARRAY(ROW(NESTED_ARRAY ARRAY(ROW(NESTED_OBJ VARCHAR))))",
|
|
|
|
},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"int_column": 1, "array_column": [[[["a"], ["b"]]], [[["c"], ["d"]]]]},
|
|
|
|
{"int_column": 2, "array_column": [[[["e"], ["f"]]], [[["g"], ["h"]]]]},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
actual_cols, actual_data, actual_expanded_cols = PrestoEngineSpec.expand_data(
|
2019-06-25 16:34:48 -04:00
|
|
|
cols, data
|
|
|
|
)
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "int_column", "type": "BIGINT"},
|
|
|
|
{"name": "array_column", "type": "ARRAY"},
|
|
|
|
{"name": "array_column.nested_array", "type": "ARRAY"},
|
|
|
|
{"name": "array_column.nested_array.nested_obj", "type": "VARCHAR"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_data = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{
|
|
|
|
"int_column": 1,
|
|
|
|
"array_column": [[[["a"], ["b"]]], [[["c"], ["d"]]]],
|
|
|
|
"array_column.nested_array": [["a"], ["b"]],
|
|
|
|
"array_column.nested_array.nested_obj": "a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": "",
|
|
|
|
"array_column": "",
|
|
|
|
"array_column.nested_array": "",
|
|
|
|
"array_column.nested_array.nested_obj": "b",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": "",
|
|
|
|
"array_column": "",
|
|
|
|
"array_column.nested_array": [["c"], ["d"]],
|
|
|
|
"array_column.nested_array.nested_obj": "c",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": "",
|
|
|
|
"array_column": "",
|
|
|
|
"array_column.nested_array": "",
|
|
|
|
"array_column.nested_array.nested_obj": "d",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": 2,
|
|
|
|
"array_column": [[[["e"], ["f"]]], [[["g"], ["h"]]]],
|
|
|
|
"array_column.nested_array": [["e"], ["f"]],
|
|
|
|
"array_column.nested_array.nested_obj": "e",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": "",
|
|
|
|
"array_column": "",
|
|
|
|
"array_column.nested_array": "",
|
|
|
|
"array_column.nested_array.nested_obj": "f",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": "",
|
|
|
|
"array_column": "",
|
|
|
|
"array_column.nested_array": [["g"], ["h"]],
|
|
|
|
"array_column.nested_array.nested_obj": "g",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int_column": "",
|
|
|
|
"array_column": "",
|
|
|
|
"array_column.nested_array": "",
|
|
|
|
"array_column.nested_array.nested_obj": "h",
|
|
|
|
},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
expected_expanded_cols = [
|
2019-06-25 16:34:48 -04:00
|
|
|
{"name": "array_column.nested_array", "type": "ARRAY"},
|
|
|
|
{"name": "array_column.nested_array.nested_obj", "type": "VARCHAR"},
|
|
|
|
]
|
2019-05-31 14:25:07 -04:00
|
|
|
self.assertEqual(actual_cols, expected_cols)
|
|
|
|
self.assertEqual(actual_data, expected_data)
|
|
|
|
self.assertEqual(actual_expanded_cols, expected_expanded_cols)
|
2019-05-03 19:29:57 -04:00
|
|
|
|
2019-06-27 14:19:55 -04:00
|
|
|
def test_presto_extra_table_metadata(self):
|
|
|
|
db = mock.Mock()
|
|
|
|
db.get_indexes = mock.Mock(return_value=[{'column_names': ['ds', 'hour']}])
|
|
|
|
df = pd.DataFrame({'ds': ['01-01-19'], 'hour': [1]})
|
|
|
|
db.get_df = mock.Mock(return_value=df)
|
|
|
|
result = PrestoEngineSpec.extra_table_metadata(db, 'test_table', 'test_schema')
|
|
|
|
self.assertEqual({'ds': '01-01-19', 'hour': 1}, result['partitions']['latest'])
|
|
|
|
|
|
|
|
def test_presto_where_latest_partition(self):
|
|
|
|
db = mock.Mock()
|
|
|
|
db.get_indexes = mock.Mock(return_value=[{'column_names': ['ds', 'hour']}])
|
|
|
|
df = pd.DataFrame({'ds': ['01-01-19'], 'hour': [1]})
|
|
|
|
db.get_df = mock.Mock(return_value=df)
|
|
|
|
columns = [{'name': 'ds'}, {'name': 'hour'}]
|
|
|
|
result = PrestoEngineSpec.where_latest_partition('test_table', 'test_schema', db, select(), columns)
|
|
|
|
query_result = str(result.compile(compile_kwargs={'literal_binds': True}))
|
|
|
|
self.assertEqual('SELECT \nWHERE ds = \'01-01-19\' AND hour = 1', query_result)
|
|
|
|
|
2018-11-28 18:13:38 -05:00
|
|
|
def test_hive_get_view_names_return_empty_list(self):
|
|
|
|
self.assertEquals([], HiveEngineSpec.get_view_names(mock.ANY, mock.ANY))
|
2019-02-21 02:05:35 -05:00
|
|
|
|
|
|
|
def test_bigquery_sqla_column_label(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
label = BigQueryEngineSpec.make_label_compatible(column("Col").name)
|
|
|
|
label_expected = "Col"
|
2019-02-21 02:05:35 -05:00
|
|
|
self.assertEqual(label, label_expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
label = BigQueryEngineSpec.make_label_compatible(column("SUM(x)").name)
|
|
|
|
label_expected = "SUM_x__5f110"
|
2019-02-21 02:05:35 -05:00
|
|
|
self.assertEqual(label, label_expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
label = BigQueryEngineSpec.make_label_compatible(column("SUM[x]").name)
|
|
|
|
label_expected = "SUM_x__7ebe1"
|
2019-02-21 02:05:35 -05:00
|
|
|
self.assertEqual(label, label_expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
label = BigQueryEngineSpec.make_label_compatible(column("12345_col").name)
|
|
|
|
label_expected = "_12345_col_8d390"
|
2019-02-21 02:05:35 -05:00
|
|
|
self.assertEqual(label, label_expected)
|
|
|
|
|
|
|
|
def test_oracle_sqla_column_name_length_exceeded(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("This_Is_32_Character_Column_Name")
|
2019-02-21 02:05:35 -05:00
|
|
|
label = OracleEngineSpec.make_label_compatible(col.name)
|
|
|
|
self.assertEqual(label.quote, True)
|
2019-06-25 16:34:48 -04:00
|
|
|
label_expected = "3b26974078683be078219674eeb8f5"
|
2019-02-21 02:05:35 -05:00
|
|
|
self.assertEqual(label, label_expected)
|
2019-03-24 23:44:05 -04:00
|
|
|
|
|
|
|
def test_mssql_column_types(self):
|
|
|
|
def assert_type(type_string, type_expected):
|
|
|
|
type_assigned = MssqlEngineSpec.get_sqla_column_type(type_string)
|
|
|
|
if type_expected is None:
|
|
|
|
self.assertIsNone(type_assigned)
|
|
|
|
else:
|
|
|
|
self.assertIsInstance(type_assigned, type_expected)
|
|
|
|
|
2019-06-25 16:34:48 -04:00
|
|
|
assert_type("INT", None)
|
|
|
|
assert_type("STRING", String)
|
|
|
|
assert_type("CHAR(10)", String)
|
|
|
|
assert_type("VARCHAR(10)", String)
|
|
|
|
assert_type("TEXT", String)
|
|
|
|
assert_type("NCHAR(10)", UnicodeText)
|
|
|
|
assert_type("NVARCHAR(10)", UnicodeText)
|
|
|
|
assert_type("NTEXT", UnicodeText)
|
2019-03-24 23:44:05 -04:00
|
|
|
|
|
|
|
def test_mssql_where_clause_n_prefix(self):
|
2019-05-30 01:28:37 -04:00
|
|
|
dialect = mssql.dialect()
|
2019-03-24 23:44:05 -04:00
|
|
|
spec = MssqlEngineSpec
|
2019-06-25 16:34:48 -04:00
|
|
|
str_col = column("col", type_=spec.get_sqla_column_type("VARCHAR(10)"))
|
|
|
|
unicode_col = column("unicode_col", type_=spec.get_sqla_column_type("NTEXT"))
|
|
|
|
tbl = table("tbl")
|
|
|
|
sel = (
|
|
|
|
select([str_col, unicode_col])
|
|
|
|
.select_from(tbl)
|
|
|
|
.where(str_col == "abc")
|
|
|
|
.where(unicode_col == "abc")
|
|
|
|
)
|
|
|
|
|
|
|
|
query = str(
|
|
|
|
sel.compile(dialect=dialect, compile_kwargs={"literal_binds": True})
|
|
|
|
)
|
|
|
|
query_expected = (
|
|
|
|
"SELECT col, unicode_col \n"
|
|
|
|
"FROM tbl \n"
|
|
|
|
"WHERE col = 'abc' AND unicode_col = N'abc'"
|
|
|
|
)
|
2019-03-24 23:44:05 -04:00
|
|
|
self.assertEqual(query, query_expected)
|
2019-05-25 23:13:16 -04:00
|
|
|
|
|
|
|
def test_get_table_names(self):
|
|
|
|
inspector = mock.Mock()
|
2019-06-25 16:34:48 -04:00
|
|
|
inspector.get_table_names = mock.Mock(return_value=["schema.table", "table_2"])
|
|
|
|
inspector.get_foreign_table_names = mock.Mock(return_value=["table_3"])
|
2019-05-25 23:13:16 -04:00
|
|
|
|
|
|
|
""" Make sure base engine spec removes schema name from table name
|
|
|
|
ie. when try_remove_schema_from_table_name == True. """
|
2019-06-25 16:34:48 -04:00
|
|
|
base_result_expected = ["table", "table_2"]
|
2019-06-08 14:27:13 -04:00
|
|
|
base_result = BaseEngineSpec.get_table_names(
|
2019-06-25 16:34:48 -04:00
|
|
|
schema="schema", inspector=inspector
|
|
|
|
)
|
2019-05-25 23:13:16 -04:00
|
|
|
self.assertListEqual(base_result_expected, base_result)
|
|
|
|
|
|
|
|
""" Make sure postgres doesn't try to remove schema name from table name
|
|
|
|
ie. when try_remove_schema_from_table_name == False. """
|
2019-06-25 16:34:48 -04:00
|
|
|
pg_result_expected = ["schema.table", "table_2", "table_3"]
|
2019-06-08 14:27:13 -04:00
|
|
|
pg_result = PostgresEngineSpec.get_table_names(
|
2019-06-25 16:34:48 -04:00
|
|
|
schema="schema", inspector=inspector
|
|
|
|
)
|
2019-05-25 23:13:16 -04:00
|
|
|
self.assertListEqual(pg_result_expected, pg_result)
|
2019-05-30 01:28:37 -04:00
|
|
|
|
|
|
|
def test_pg_time_expression_literal_no_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = literal_column("COALESCE(a, b)")
|
2019-05-30 01:28:37 -04:00
|
|
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
|
|
|
|
result = str(expr.compile(dialect=postgresql.dialect()))
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(result, "COALESCE(a, b)")
|
2019-05-30 01:28:37 -04:00
|
|
|
|
|
|
|
def test_pg_time_expression_literal_1y_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = literal_column("COALESCE(a, b)")
|
|
|
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
2019-05-30 01:28:37 -04:00
|
|
|
result = str(expr.compile(dialect=postgresql.dialect()))
|
|
|
|
self.assertEqual(result, "DATE_TRUNC('year', COALESCE(a, b))")
|
|
|
|
|
|
|
|
def test_pg_time_expression_lower_column_no_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("lower_case")
|
2019-05-30 01:28:37 -04:00
|
|
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
|
|
|
|
result = str(expr.compile(dialect=postgresql.dialect()))
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(result, "lower_case")
|
2019-05-30 01:28:37 -04:00
|
|
|
|
|
|
|
def test_pg_time_expression_lower_case_column_sec_1y_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("lower_case")
|
|
|
|
expr = PostgresEngineSpec.get_timestamp_expr(col, "epoch_s", "P1Y")
|
2019-05-30 01:28:37 -04:00
|
|
|
result = str(expr.compile(dialect=postgresql.dialect()))
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
"DATE_TRUNC('year', (timestamp 'epoch' + lower_case * interval '1 second'))", # noqa ignore: E50
|
|
|
|
)
|
2019-05-30 01:28:37 -04:00
|
|
|
|
|
|
|
def test_pg_time_expression_mixed_case_column_1y_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("MixedCase")
|
|
|
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
2019-05-30 01:28:37 -04:00
|
|
|
result = str(expr.compile(dialect=postgresql.dialect()))
|
|
|
|
self.assertEqual(result, "DATE_TRUNC('year', \"MixedCase\")")
|
|
|
|
|
|
|
|
def test_mssql_time_expression_mixed_case_column_1y_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("MixedCase")
|
|
|
|
expr = MssqlEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
2019-05-30 01:28:37 -04:00
|
|
|
result = str(expr.compile(dialect=mssql.dialect()))
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(result, "DATEADD(year, DATEDIFF(year, 0, [MixedCase]), 0)")
|
2019-05-30 01:28:37 -04:00
|
|
|
|
|
|
|
def test_oracle_time_expression_reserved_keyword_1m_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("decimal")
|
|
|
|
expr = OracleEngineSpec.get_timestamp_expr(col, None, "P1M")
|
2019-05-30 01:28:37 -04:00
|
|
|
result = str(expr.compile(dialect=oracle.dialect()))
|
|
|
|
self.assertEqual(result, "TRUNC(CAST(\"decimal\" as DATE), 'MONTH')")
|
|
|
|
|
|
|
|
def test_pinot_time_expression_sec_1m_grain(self):
|
2019-06-25 16:34:48 -04:00
|
|
|
col = column("tstamp")
|
|
|
|
expr = PinotEngineSpec.get_timestamp_expr(col, "epoch_s", "P1M")
|
2019-05-30 01:28:37 -04:00
|
|
|
result = str(expr.compile())
|
2019-06-25 16:34:48 -04:00
|
|
|
self.assertEqual(
|
|
|
|
result,
|
|
|
|
'DATETIMECONVERT(tstamp, "1:SECONDS:EPOCH", "1:SECONDS:EPOCH", "1:MONTHS")',
|
|
|
|
) # noqa
|
2019-06-03 12:52:25 -04:00
|
|
|
|
|
|
|
def test_column_datatype_to_string(self):
|
|
|
|
main_db = self.get_main_database()
|
2019-06-25 16:34:48 -04:00
|
|
|
sqla_table = main_db.get_table("energy_usage")
|
2019-06-03 12:52:25 -04:00
|
|
|
dialect = main_db.get_dialect()
|
|
|
|
col_names = [
|
|
|
|
main_db.db_engine_spec.column_datatype_to_string(c.type, dialect)
|
|
|
|
for c in sqla_table.columns
|
|
|
|
]
|
2019-06-25 16:34:48 -04:00
|
|
|
if main_db.backend == "postgresql":
|
|
|
|
expected = ["VARCHAR(255)", "VARCHAR(255)", "DOUBLE PRECISION"]
|
2019-06-03 12:52:25 -04:00
|
|
|
else:
|
2019-06-25 16:34:48 -04:00
|
|
|
expected = ["VARCHAR(255)", "VARCHAR(255)", "FLOAT"]
|
2019-06-03 12:52:25 -04:00
|
|
|
self.assertEquals(col_names, expected)
|