haridsv commented on code in PR #2298:
URL: https://github.com/apache/phoenix/pull/2298#discussion_r2458977305
##########
phoenix-core-client/src/main/java/org/apache/phoenix/compile/RowProjector.java:
##########
@@ -206,4 +235,55 @@ 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)) {
+ additionalMappings.put(sourceLabel, i);
+ }
+
+ // Also add qualified name from source if different
+ if (!sourceCol.getTableName().isEmpty()) {
+ String sourceQualifiedName =
+ SchemaUtil.getColumnName(sourceCol.getTableName(),
sourceCol.getLabel());
+ String currentQualifiedName = currentCol.getTableName().isEmpty()
+ ? ""
+ : SchemaUtil.getColumnName(currentCol.getTableName(),
currentCol.getLabel());
+
+ if (!sourceQualifiedName.equals(currentQualifiedName)) {
+ additionalMappings.put(sourceQualifiedName, i);
+ }
+ }
+ }
Review Comment:
It appears that this can be simplified a bit if you consider the following 2
facts:
- Since qualified name includes label, it is guaranteed to be difference
when label is found to be different at line 266, so there is no need to check.
- If label is same, then the only qualified name can be different is when
the table name is different, so you can skip the construction of
`currentQualifiedName` as it is only needed for the comparison.
--
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]