Author: hashutosh
Date: Mon Mar 24 05:28:25 2014
New Revision: 1580751

URL: http://svn.apache.org/r1580751
Log:
HIVE-6687 : JDBC ResultSet fails to get value by qualified projection name 
(Laljo John Pullokkaran via Ashutosh Chauhan)

Modified:
    
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
    hive/branches/branch-0.13/conf/hive-default.xml.template
    
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
    
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
    
hive/branches/branch-0.13/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
    
hive/branches/branch-0.13/ql/src/test/results/clientpositive/print_header.q.out

Modified: 
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java?rev=1580751&r1=1580750&r2=1580751&view=diff
==============================================================================
--- 
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
 (original)
+++ 
hive/branches/branch-0.13/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
 Mon Mar 24 05:28:25 2014
@@ -221,6 +221,9 @@ public class HiveConf extends Configurat
     // Max number of lines of footer user can set for a table file.
     HIVE_FILE_MAX_FOOTER("hive.file.max.footer", 100),
 
+    // Make column names unique in the result set by using table alias if 
needed
+    
HIVE_RESULTSET_USE_UNIQUE_COLUMN_NAMES("hive.resultset.use.unique.column.names",
 true),
+
     // Hadoop Configuration Properties
     // Properties with null values are ignored and exist only for the purpose 
of giving us
     // a symbolic name to reference in the Hive source code. Properties with 
non-null

Modified: hive/branches/branch-0.13/conf/hive-default.xml.template
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/conf/hive-default.xml.template?rev=1580751&r1=1580750&r2=1580751&view=diff
==============================================================================
--- hive/branches/branch-0.13/conf/hive-default.xml.template (original)
+++ hive/branches/branch-0.13/conf/hive-default.xml.template Mon Mar 24 
05:28:25 2014
@@ -2466,6 +2466,16 @@
 </property>
 
 <property>
+  <name>hive.resultset.use.unique.column.names</name>
+  <value>true</value>
+  <description>
+    Make column names unique in the result set by qualifying column names with 
table alias if needed.
+    Table alias will be added to column names for queries of type "select *" or
+    if query explicitly uses table alias "select r1.x..".
+  </description>
+</property>
+
+<property>
   <name>hive.compat</name>
   <value>0.12</value>
   <description>

Modified: 
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java?rev=1580751&r1=1580750&r2=1580751&view=diff
==============================================================================
--- 
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
 (original)
+++ 
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hadoop/hive/jdbc/TestJdbcDriver.java
 Mon Mar 24 05:28:25 2014
@@ -513,16 +513,17 @@ public class TestJdbcDriver extends Test
     assertEquals(
       "Unexpected column count", expectedColCount, meta.getColumnCount());
 
+    String colQualifier = ((tableName != null) && !tableName.isEmpty()) ? 
tableName.toLowerCase() + "."  : "";
     boolean moreRow = res.next();
     while (moreRow) {
       try {
         i++;
-        assertEquals(res.getInt(1), res.getInt("under_col"));
-        assertEquals(res.getString(1), res.getString("under_col"));
-        assertEquals(res.getString(2), res.getString("value"));
+        assertEquals(res.getInt(1), res.getInt(colQualifier + "under_col"));
+        assertEquals(res.getString(1), res.getString(colQualifier + 
"under_col"));
+        assertEquals(res.getString(2), res.getString(colQualifier + "value"));
         if (isPartitionTable) {
           assertEquals(res.getString(3), partitionedColumnValue);
-          assertEquals(res.getString(3), res.getString(partitionedColumnName));
+          assertEquals(res.getString(3), res.getString(colQualifier + 
partitionedColumnName));
         }
         assertFalse("Last result value was not null", res.wasNull());
         assertNull("No warnings should be found on ResultSet", res

Modified: 
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java?rev=1580751&r1=1580750&r2=1580751&view=diff
==============================================================================
--- 
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
 (original)
+++ 
hive/branches/branch-0.13/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestJdbcDriver2.java
 Mon Mar 24 05:28:25 2014
@@ -911,16 +911,17 @@ public class TestJdbcDriver2 {
     assertEquals(
         "Unexpected column count", expectedColCount, meta.getColumnCount());
 
+    String colQualifier = ((tableName != null) && !tableName.isEmpty()) ? 
tableName.toLowerCase() + "."  : "";
     boolean moreRow = res.next();
     while (moreRow) {
       try {
         i++;
-        assertEquals(res.getInt(1), res.getInt("under_col"));
-        assertEquals(res.getString(1), res.getString("under_col"));
-        assertEquals(res.getString(2), res.getString("value"));
+        assertEquals(res.getInt(1), res.getInt(colQualifier + "under_col"));
+        assertEquals(res.getString(1), res.getString(colQualifier + 
"under_col"));
+        assertEquals(res.getString(2), res.getString(colQualifier + "value"));
         if (isPartitionTable) {
           assertEquals(res.getString(3), partitionedColumnValue);
-          assertEquals(res.getString(3), res.getString(partitionedColumnName));
+          assertEquals(res.getString(3), res.getString(colQualifier + 
partitionedColumnName));
         }
         assertFalse("Last result value was not null", res.wasNull());
         assertNull("No warnings should be found on ResultSet", res
@@ -1866,7 +1867,7 @@ public class TestJdbcDriver2 {
    */
   @Test
   public void testFetchFirstNonMR() throws Exception {
-    execFetchFirst("select * from " + dataTypeTableName, "c4", false);
+    execFetchFirst("select * from " + dataTypeTableName, 
dataTypeTableName.toLowerCase() + "." + "c4", false);
   }
 
   /**

Modified: 
hive/branches/branch-0.13/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java?rev=1580751&r1=1580750&r2=1580751&view=diff
==============================================================================
--- 
hive/branches/branch-0.13/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
 (original)
+++ 
hive/branches/branch-0.13/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
 Mon Mar 24 05:28:25 2014
@@ -9217,8 +9217,11 @@ public class SemanticAnalyzer extends Ba
     // up with later.
     Operator sinkOp = genPlan(qb);
 
-    resultSchema =
-        convertRowSchemaToViewSchema(opParseCtx.get(sinkOp).getRowResolver());
+    if (createVwDesc != null)
+      resultSchema = 
convertRowSchemaToViewSchema(opParseCtx.get(sinkOp).getRowResolver());
+    else
+      resultSchema = 
convertRowSchemaToResultSetSchema(opParseCtx.get(sinkOp).getRowResolver(),
+          HiveConf.getBoolVar(conf, 
HiveConf.ConfVars.HIVE_RESULTSET_USE_UNIQUE_COLUMN_NAMES));
 
     ParseContext pCtx = new ParseContext(conf, qb, child, opToPartPruner,
         opToPartList, topOps, topSelOps, opParseCtx, joinContext, 
smbMapJoinContext,
@@ -9406,15 +9409,30 @@ public class SemanticAnalyzer extends Ba
     createVwDesc.setViewExpandedText(expandedText);
   }
 
-  private List<FieldSchema> convertRowSchemaToViewSchema(RowResolver rr) {
+  private List<FieldSchema> convertRowSchemaToViewSchema(RowResolver rr) 
throws SemanticException {
+    List<FieldSchema> fieldSchema = convertRowSchemaToResultSetSchema(rr, 
false);
+    ParseUtils.validateColumnNameUniqueness(fieldSchema);
+    return fieldSchema;
+  }
+
+  private List<FieldSchema> convertRowSchemaToResultSetSchema(RowResolver rr,
+      boolean useTabAliasIfAvailable) {
     List<FieldSchema> fieldSchemas = new ArrayList<FieldSchema>();
+    String[] qualifiedColName;
+    String colName;
+
     for (ColumnInfo colInfo : rr.getColumnInfos()) {
       if (colInfo.isHiddenVirtualCol()) {
         continue;
       }
-      String colName = rr.reverseLookup(colInfo.getInternalName())[1];
-      fieldSchemas.add(new FieldSchema(colName,
-          colInfo.getType().getTypeName(), null));
+
+      qualifiedColName = rr.reverseLookup(colInfo.getInternalName());
+      if (useTabAliasIfAvailable && qualifiedColName[0] != null && 
!qualifiedColName[0].isEmpty()) {
+        colName = qualifiedColName[0] + "." + qualifiedColName[1];
+      } else {
+        colName = qualifiedColName[1];
+      }
+      fieldSchemas.add(new FieldSchema(colName, 
colInfo.getType().getTypeName(), null));
     }
     return fieldSchemas;
   }

Modified: 
hive/branches/branch-0.13/ql/src/test/results/clientpositive/print_header.q.out
URL: 
http://svn.apache.org/viewvc/hive/branches/branch-0.13/ql/src/test/results/clientpositive/print_header.q.out?rev=1580751&r1=1580750&r2=1580751&view=diff
==============================================================================
--- 
hive/branches/branch-0.13/ql/src/test/results/clientpositive/print_header.q.out 
(original)
+++ 
hive/branches/branch-0.13/ql/src/test/results/clientpositive/print_header.q.out 
Mon Mar 24 05:28:25 2014
@@ -37,7 +37,7 @@ POSTHOOK: query: SELECT src.key, sum(sub
 POSTHOOK: type: QUERY
 POSTHOOK: Input: default@src
 #### A masked pattern was here ####
-key    _c1
+src.key        _c1
 0      0.0
 10     10.0
 100    200.0


Reply via email to