Browse Source

HUE-9367 [phoenix] Include PHOENIX-5936 sqlAlchemy get_columns KeyError

Romain 5 năm trước cách đây
mục cha
commit
ed3c2fe249

+ 11 - 11
desktop/core/ext-py/phoenixdb/phoenixdb/sqlalchemy_phoenix.py

@@ -137,24 +137,24 @@ class PhoenixDialect(DefaultDialect):
 
     def get_columns(self, connection, table_name, schema=None, **kw):
         if schema is None:
-            query = "SELECT COLUMN_NAME,  DATA_TYPE, NULLABLE " \
-                    "FROM system.catalog " \
-                    "WHERE table_name = ? " \
-                    "ORDER BY ORDINAL_POSITION"
+            query = """SELECT COLUMN_NAME,  DATA_TYPE, NULLABLE
+                    FROM system.catalog
+                    WHERE table_name = ?
+                    AND ORDINAL_POSITION is not null
+                    ORDER BY ORDINAL_POSITION"""
             params = [table_name.upper()]
         else:
-            query = "SELECT COLUMN_NAME, DATA_TYPE, NULLABLE " \
-                    "FROM system.catalog " \
-                    "WHERE TABLE_SCHEM = ? " \
-                    "AND table_name = ? " \
-                    "ORDER BY ORDINAL_POSITION"
+            query = """SELECT COLUMN_NAME, DATA_TYPE, NULLABLE
+                    FROM system.catalog
+                    WHERE TABLE_SCHEM = ?
+                    AND table_name = ?
+                    AND ORDINAL_POSITION is not null
+                    ORDER BY ORDINAL_POSITION"""
             params = [schema.upper(), table_name.upper()]
 
         # get all of the fields for this table
         c = connection.execute(query, params)
         cols = []
-        # first always none
-        c.fetchone()
         while True:
             row = c.fetchone()
             if row is None:

+ 19 - 0
desktop/core/ext-py/phoenixdb/phoenixdb/tests/test_sqlalchemy.py

@@ -52,6 +52,25 @@ class SQLAlchemyTest(unittest.TestCase):
             finally:
                 connection.execute('drop table if exists ALCHEMY_TEST')
 
+    def test_reflection(self):
+        engine = self._create_engine()
+        with engine.connect() as connection:
+            try:
+                inspector = db.inspect(engine)
+                columns_result = inspector.get_columns('DOES_NOT_EXIST')
+                self.assertEqual([], columns_result)
+                connection.execute('drop table if exists us_population')
+                connection.execute(text('''create table if not exists US_POPULATION (
+                state CHAR(2) NOT NULL,
+                city VARCHAR NOT NULL,
+                population BIGINT
+                CONSTRAINT my_pk PRIMARY KEY (state, city))'''))
+                columns_result = inspector.get_columns('us_population')
+                self.assertEqual(len(columns_result), 3)
+                print(columns_result)
+            finally:
+                connection.execute('drop table if exists us_population')
+
     @unittest.skip("ORM feature not implemented")
     def test_orm(self):
         pass