Skip to content

Commit 186d950

Browse files
satyakommula96stoty
andcommitted
PHOENIX-6917 Column alias not working properly in Python
Co-authored-by: Istvan Toth <[email protected]>
1 parent f77151d commit 186d950

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

phoenix-queryserver/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<profile>
252252
<!-- This is required for operation with Phoenix 5.1.0 - 5.1.3 and 4.16.x -->
253253
<!-- See PHOENIX-6861 -->
254+
<!-- This should have been called relocate-java-servlet -->
254255
<id>shade-javax-servlet</id>
255256
<activation>
256257
<property>

python-phoenixdb/NEWS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Unreleased
1212
- Run tests with all supported Python + SqlAlchemy versions (1.3, 1.4, 2.0) (PHOENIX-6892)
1313
- Replace deprecated failUnless methods in tests (PHOENIX-6892)
1414
- Add support for specifying custom HTTP headers (PHOENIX-6921)
15+
- Use JDBC/Avatica column label as column name when set (PHOENIX-6917)
1516

1617
Version 1.2.1
1718
-------------

python-phoenixdb/phoenixdb/cursor.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def description(self):
115115
description = []
116116
for column in self._signature.columns:
117117
description.append(ColumnDescription(
118-
column.column_name,
118+
self._get_column_name(column),
119119
column.type.name,
120120
column.display_size,
121121
None,
@@ -125,6 +125,13 @@ def description(self):
125125
))
126126
return description
127127

128+
def _get_column_name(self, column):
129+
if column.label:
130+
# Not empty
131+
return column.label
132+
else:
133+
return column.column_name
134+
128135
def _set_id(self, id):
129136
if self._id is not None and self._id != id:
130137
self._connection._client.close_statement(self._connection._id, self._id)
@@ -386,5 +393,5 @@ def _transform_row(self, row):
386393
row = super(DictCursor, self)._transform_row(row)
387394
d = {}
388395
for ind, val in enumerate(row):
389-
d[self._signature.columns[ind].column_name] = val
396+
d[self._get_column_name(self._signature.columns[ind])] = val
390397
return d

python-phoenixdb/phoenixdb/tests/dbapi20.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import unittest
3333
import time
3434
import sys
35+
import phoenixdb
3536

3637
def str2bytes(sval):
3738
if sys.version_info < (3,0) and isinstance(sval, str):
@@ -74,7 +75,7 @@ class mytest(dbapi20.DatabaseAPI20Test):
7475
insert = 'insert'
7576

7677
lowerfunc = 'lower' # Name of stored procedure to convert string->lowercase
77-
78+
7879
# Some drivers may need to override these helpers, for example adding
7980
# a 'commit' after the execute.
8081
def executeDDL1(self,cursor):
@@ -208,7 +209,7 @@ def test_rollback(self):
208209
con.rollback()
209210
except self.driver.NotSupportedError:
210211
pass
211-
212+
212213
def test_cursor(self):
213214
con = self._connect()
214215
try:
@@ -399,12 +400,12 @@ def _paraminsert(self,cur):
399400
trouble = "thi%s :may ca%(u)se? troub:1e"
400401
self.assertEqual(res[0][1], trouble,
401402
'cursor.fetchall retrieved incorrect data, or data inserted '
402-
'incorrectly. Got=%s, Expected=%s' % (repr(res[0][1]), repr(trouble)))
403+
'incorrectly. Got=%s, Expected=%s' % (repr(res[0][1]), repr(trouble)))
403404
self.assertEqual(res[1][1], trouble,
404405
'cursor.fetchall retrieved incorrect data, or data inserted '
405406
'incorrectly. Got=%s, Expected=%s' % (repr(res[1][1]), repr(trouble)
406407
))
407-
408+
408409
def test_executemany(self):
409410
con = self._connect()
410411
try:
@@ -575,7 +576,7 @@ def test_fetchmany(self):
575576
self.assertEqual(len(rows),6)
576577
rows = [r[0] for r in rows]
577578
rows.sort()
578-
579+
579580
# Make sure we get the right data back out
580581
for i in range(0,6):
581582
self.assertEqual(rows[i],self.samples[i],
@@ -646,10 +647,10 @@ def test_fetchall(self):
646647
'cursor.fetchall should return an empty list if '
647648
'a select query returns no rows'
648649
)
649-
650+
650651
finally:
651652
con.close()
652-
653+
653654
def test_mixedfetch(self):
654655
con = self._connect()
655656
try:
@@ -832,3 +833,17 @@ def test_ROWID(self):
832833
self.assertTrue(hasattr(self.driver,'ROWID'),
833834
'module.ROWID must be defined.'
834835
)
836+
837+
# https://issues.apache.org/jira/browse/PHOENIX-6917
838+
def test_alias(self):
839+
con = self._connect()
840+
try:
841+
cur = con.cursor(cursor_factory=phoenixdb.cursor.DictCursor)
842+
self.executeDDL1(cur)
843+
for sql in self._populate():
844+
cur.execute(sql)
845+
cur.execute('select name as beverages from %sbooze' % self.table_prefix)
846+
self.assertEqual(cur.description[0][0], 'BEVERAGES')
847+
self.assertEqual(cur.fetchone()['BEVERAGES'], 'Carlton Cold')
848+
finally:
849+
con.close()

0 commit comments

Comments
 (0)