haridsv commented on code in PR #2298:
URL: https://github.com/apache/phoenix/pull/2298#discussion_r2466305276


##########
phoenix-core-client/src/main/java/org/apache/phoenix/compile/RowProjector.java:
##########
@@ -206,4 +235,54 @@ public void reset() {
       projector.getExpression().reset();
     }
   }
+
+  /**
+   * PHOENIX-6644: Creates a new RowProjector with additional column name 
mappings merged from
+   * another projector. This is useful when an optimized query (e.g., using an 
index) rewrites
+   * column references but we want to preserve the original column names for
+   * ResultSet.getString(columnName) compatibility. For example, when a view 
has "WHERE v1 = 'a'"
+   * and an index is used, the optimizer may rewrite "SELECT v1 FROM view" to 
"SELECT 'a' FROM
+   * index". This method adds the original column name "v1" to the 
reverseIndex so
+   * ResultSet.getString("v1") continues to work.
+   * @param sourceProjector the projector containing original column name 
mappings to preserve
+   * @return a new RowProjector with merged column name mappings
+   */
+  public RowProjector mergeColumnNameMappings(RowProjector sourceProjector) {
+    if (this.columnProjectors.size() != 
sourceProjector.columnProjectors.size()) {
+      return this;
+    }
+
+    ListMultimap<String, Integer> additionalMappings = 
ArrayListMultimap.create();
+
+    for (int i = 0; i < sourceProjector.columnProjectors.size(); i++) {
+      ColumnProjector sourceCol = sourceProjector.columnProjectors.get(i);
+      ColumnProjector currentCol = this.columnProjectors.get(i);
+
+      // Only add source labels if they're different from current labels
+      // This preserves original names like "v1" when optimizer rewrites to 
"'a'"
+      String sourceLabel = sourceCol.getLabel();
+      String currentLabel = currentCol.getLabel();
+
+      if (!sourceLabel.equals(currentLabel)) {
+        // Labels are different, so qualified names are guaranteed to be 
different
+        additionalMappings.put(sourceLabel, i);
+        if (!sourceCol.getTableName().isEmpty()) {
+          additionalMappings.put(
+            SchemaUtil.getColumnName(sourceCol.getTableName(), 
sourceCol.getLabel()), i);
+        }
+      } else {
+        // Labels are the same, so check if table names differ
+        if (!sourceCol.getTableName().isEmpty()
+          && !sourceCol.getTableName().equals(currentCol.getTableName())) {
+          additionalMappings.put(
+            SchemaUtil.getColumnName(sourceCol.getTableName(), 
sourceCol.getLabel()), i);
+        }
+      }

Review Comment:
   I see a duplicate call, how bout combining something like this (untested)?
   
   ```suggestion
         if (!sourceLabel.equals(currentLabel)) {
           // Labels are different, so qualified names are guaranteed to be 
different
           additionalMappings.put(sourceLabel, i);
         }
         if (!sourceCol.getTableName().isEmpty()
           && (!sourceCol.getTableName().equals(currentCol.getTableName())
               || !sourceLabel.equals(currentLabel))) {
           additionalMappings.put(
             SchemaUtil.getColumnName(sourceCol.getTableName(), 
sourceCol.getLabel()), i);
         }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to