From ab829ee041187957c037f93b1f2fb7ae2d13a409 Mon Sep 17 00:00:00 2001 From: Pat Buxton Date: Thu, 4 Sep 2025 10:05:46 +0100 Subject: [PATCH 1/7] Fix - Presto SQLAlchemy dialect did not implement get_view_names --- python/pyhive/sqlalchemy_presto.py | 34 ++++++++++++++++++- python/pyhive/tests/test_sqlalchemy_presto.py | 29 +++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/python/pyhive/sqlalchemy_presto.py b/python/pyhive/sqlalchemy_presto.py index 33a41bae3e7..47e9279c2b1 100644 --- a/python/pyhive/sqlalchemy_presto.py +++ b/python/pyhive/sqlalchemy_presto.py @@ -23,7 +23,7 @@ from sqlalchemy.dialects import mysql mysql_tinyinteger = mysql.base.MSTinyInteger from sqlalchemy.engine import default -from sqlalchemy.sql import compiler +from sqlalchemy.sql import compiler, bindparam from sqlalchemy.sql.compiler import SQLCompiler from pyhive import presto @@ -204,12 +204,44 @@ def get_indexes(self, connection, table_name, schema=None, **kw): else: return [] + def _get_default_schema_name(self, connection): + #'SELECT CURRENT_SCHEMA()' + return super()._get_default_schema_name(connection) + def get_table_names(self, connection, schema=None, **kw): query = 'SHOW TABLES' + # N.B. This is incorrect, if no schema is provided, the current/default schema should be used + # with a call to an overridden self._get_default_schema_name(connection), but I could not see how to implement + # that as there is no CURRENT_SCHEMA function + # default_schema = self._get_default_schema_name(connection) + if schema: query += ' FROM ' + self.identifier_preparer.quote_identifier(schema) return [row.Table for row in connection.execute(text(query))] + def get_view_names(self, connection, schema=None, **kw): + if schema: + view_name_query = """ + SELECT table_name + FROM information_schema.views + WHERE table_schema = :schema + """ + query = text(view_name_query).bindparams( + bindparam("schema", type_=types.Unicode) + ) + else: + # N.B. This is incorrect, if no schema is provided, the current/default schema should be used + # with a call to self._get_default_schema_name(connection), but I could not see how to implement that + # default_schema = self._get_default_schema_name(connection) + view_name_query = """ + SELECT table_name + FROM information_schema.views + """ + query = text(view_name_query) + + result = connection.execute(query, dict(schema=schema)) + return [row[0] for row in result] + def do_rollback(self, dbapi_connection): # No transactions for Presto pass diff --git a/python/pyhive/tests/test_sqlalchemy_presto.py b/python/pyhive/tests/test_sqlalchemy_presto.py index 336dd12e243..e8b04ea249c 100644 --- a/python/pyhive/tests/test_sqlalchemy_presto.py +++ b/python/pyhive/tests/test_sqlalchemy_presto.py @@ -102,4 +102,31 @@ def test_hash_table(self, engine, connection): self.assertFalse(insp.has_table("THIS_TABLE_DOSE_not_exist")) else: self.assertFalse(Table('THIS_TABLE_DOSE_NOT_EXIST', MetaData(bind=engine)).exists()) - self.assertFalse(Table('THIS_TABLE_DOSE_not_exits', MetaData(bind=engine)).exists()) \ No newline at end of file + self.assertFalse(Table('THIS_TABLE_DOSE_not_exits', MetaData(bind=engine)).exists()) + + @with_engine_connection + def test_reflect_table_names(self, engine, connection): + sqlalchemy_version = float(re.search(r"^([\d]+\.[\d]+)\..+", sqlalchemy.__version__).group(1)) + if sqlalchemy_version >= 1.4: + insp = sqlalchemy.inspect(engine) + table_names = insp.get_table_names() + self.assertIn("one_row", table_names) + self.assertIn("one_row_complex", table_names) + self.assertIn("many_rows", table_names) + self.assertNotIn("THIS_TABLE_DOES_not_exist", table_names) + else: + self.assertTrue(Table('one_row', MetaData(bind=engine)).exists()) + self.assertTrue(Table('one_row_complex', MetaData(bind=engine)).exists()) + self.assertTrue(Table('many_rows', MetaData(bind=engine)).exists()) + self.assertFalse(Table('THIS_TABLE_DOES_not_exist', MetaData(bind=engine)).exists()) + + @with_engine_connection + def test_reflect_view_names(self, engine, connection): + sqlalchemy_version = float(re.search(r"^([\d]+\.[\d]+)\..+", sqlalchemy.__version__).group(1)) + if sqlalchemy_version >= 1.4: + insp = sqlalchemy.inspect(engine) + view_names = insp.get_view_names() + self.assertNotIn("one_row", view_names) + self.assertNotIn("one_row_complex", view_names) + self.assertNotIn("many_rows", view_names) + self.assertNotIn("THIS_TABLE_DOES_not_exist", view_names) From b8deadcdb505115c4790c56f7ea0e4ca1decc50e Mon Sep 17 00:00:00 2001 From: Pat Buxton Date: Fri, 5 Sep 2025 17:08:29 +0100 Subject: [PATCH 2/7] Bump python version to 0.7.1 --- python/pyhive/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyhive/__init__.py b/python/pyhive/__init__.py index 0a6bb1f635b..aa1b9a92a78 100644 --- a/python/pyhive/__init__.py +++ b/python/pyhive/__init__.py @@ -1,3 +1,3 @@ from __future__ import absolute_import from __future__ import unicode_literals -__version__ = '0.7.0' +__version__ = '0.7.1' From 89d3f55fecb2797e79f51060f3279b8cabad6592 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 22 Sep 2025 15:35:58 +0800 Subject: [PATCH 3/7] Update python/pyhive/__init__.py --- python/pyhive/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyhive/__init__.py b/python/pyhive/__init__.py index aa1b9a92a78..0a6bb1f635b 100644 --- a/python/pyhive/__init__.py +++ b/python/pyhive/__init__.py @@ -1,3 +1,3 @@ from __future__ import absolute_import from __future__ import unicode_literals -__version__ = '0.7.1' +__version__ = '0.7.0' From 2e7040a141511f803c18e18d0a9566c24e2a55c5 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 22 Sep 2025 15:36:52 +0800 Subject: [PATCH 4/7] Update python/pyhive/sqlalchemy_presto.py --- python/pyhive/sqlalchemy_presto.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/pyhive/sqlalchemy_presto.py b/python/pyhive/sqlalchemy_presto.py index 47e9279c2b1..060cacbe361 100644 --- a/python/pyhive/sqlalchemy_presto.py +++ b/python/pyhive/sqlalchemy_presto.py @@ -211,8 +211,8 @@ def _get_default_schema_name(self, connection): def get_table_names(self, connection, schema=None, **kw): query = 'SHOW TABLES' # N.B. This is incorrect, if no schema is provided, the current/default schema should be used - # with a call to an overridden self._get_default_schema_name(connection), but I could not see how to implement - # that as there is no CURRENT_SCHEMA function + # with a call to an overridden self._get_default_schema_name(connection), but I could not + # see how to implement that as there is no CURRENT_SCHEMA function # default_schema = self._get_default_schema_name(connection) if schema: From 1c7b628501207a68b0173a00015022dc307d8674 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 22 Sep 2025 15:37:10 +0800 Subject: [PATCH 5/7] Update python/pyhive/sqlalchemy_presto.py --- python/pyhive/sqlalchemy_presto.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyhive/sqlalchemy_presto.py b/python/pyhive/sqlalchemy_presto.py index 060cacbe361..f2d5dd69ea9 100644 --- a/python/pyhive/sqlalchemy_presto.py +++ b/python/pyhive/sqlalchemy_presto.py @@ -212,7 +212,7 @@ def get_table_names(self, connection, schema=None, **kw): query = 'SHOW TABLES' # N.B. This is incorrect, if no schema is provided, the current/default schema should be used # with a call to an overridden self._get_default_schema_name(connection), but I could not - # see how to implement that as there is no CURRENT_SCHEMA function + # see how to implement that as there is no CURRENT_SCHEMA function # default_schema = self._get_default_schema_name(connection) if schema: From 27396977e8301dea8603cea6e1514a2d541eedac Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 22 Sep 2025 15:37:55 +0800 Subject: [PATCH 6/7] Update python/pyhive/sqlalchemy_presto.py --- python/pyhive/sqlalchemy_presto.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/python/pyhive/sqlalchemy_presto.py b/python/pyhive/sqlalchemy_presto.py index f2d5dd69ea9..8b755a3e037 100644 --- a/python/pyhive/sqlalchemy_presto.py +++ b/python/pyhive/sqlalchemy_presto.py @@ -230,9 +230,10 @@ def get_view_names(self, connection, schema=None, **kw): bindparam("schema", type_=types.Unicode) ) else: - # N.B. This is incorrect, if no schema is provided, the current/default schema should be used - # with a call to self._get_default_schema_name(connection), but I could not see how to implement that - # default_schema = self._get_default_schema_name(connection) + # N.B. This is incorrect, if no schema is provided, the current/default schema should + # be used with a call to self._get_default_schema_name(connection), but I could not + # see how to implement that + # default_schema = self._get_default_schema_name(connection) view_name_query = """ SELECT table_name FROM information_schema.views From c2d06f7e0946a6bebc9e4e4d076ea6db09654a09 Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 22 Sep 2025 15:38:31 +0800 Subject: [PATCH 7/7] Update python/pyhive/sqlalchemy_presto.py --- python/pyhive/sqlalchemy_presto.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/pyhive/sqlalchemy_presto.py b/python/pyhive/sqlalchemy_presto.py index 8b755a3e037..f5a256fb8d7 100644 --- a/python/pyhive/sqlalchemy_presto.py +++ b/python/pyhive/sqlalchemy_presto.py @@ -231,9 +231,9 @@ def get_view_names(self, connection, schema=None, **kw): ) else: # N.B. This is incorrect, if no schema is provided, the current/default schema should - # be used with a call to self._get_default_schema_name(connection), but I could not - # see how to implement that - # default_schema = self._get_default_schema_name(connection) + # be used with a call to self._get_default_schema_name(connection), but I could not + # see how to implement that + # default_schema = self._get_default_schema_name(connection) view_name_query = """ SELECT table_name FROM information_schema.views