[ https://issues.apache.org/jira/browse/PHOENIX-6365?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17410389#comment-17410389 ]
ASF GitHub Bot commented on PHOENIX-6365: ----------------------------------------- stoty commented on a change in pull request #1133: URL: https://github.com/apache/phoenix/pull/1133#discussion_r702615589 ########## File path: phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java ########## @@ -1121,46 +1124,55 @@ public PSchema resolveSchema(String schemaName) throws SQLException { return null; } } - + private static class ProjectedTableColumnResolver extends MultiTableColumnResolver { private final boolean isLocalIndex; + // We must handle the local index data tables separately + protected final ListMultimap<String, TableRef> localIndexDataTableMap = ArrayListMultimap.<String, TableRef> create(); Review comment: Yes, but we use the same initialization for tableMap, and I wanted to keep as similar as possible. We could change it in both places, but that would add even more noise to the patch. ########## File path: phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java ########## @@ -985,7 +976,17 @@ public TableRef refreshDerivedTableNode(DerivedTableNode derivedTableNode) throw return this.resolveTable(null, tableAlias); } - private static class ColumnFamilyRef { + protected void rememberTable(TableRef tableRef, String alias) { + String name = tableRef.getTable().getName().getString(); Review comment: Done ########## File path: phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java ########## @@ -985,7 +976,17 @@ public TableRef refreshDerivedTableNode(DerivedTableNode derivedTableNode) throw return this.resolveTable(null, tableAlias); } - private static class ColumnFamilyRef { + protected void rememberTable(TableRef tableRef, String alias) { + String name = tableRef.getTable().getName().getString(); + if (alias != null) { + tableMap.put(alias, tableRef); + } else { + tableMap.put(name, tableRef); + } + tables.add(tableRef); + } + + protected static class ColumnFamilyRef { Review comment: No, we use it in child ProjectedTableColumnResolver. ########## File path: phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java ########## @@ -1121,46 +1124,55 @@ public PSchema resolveSchema(String schemaName) throws SQLException { return null; } } - + private static class ProjectedTableColumnResolver extends MultiTableColumnResolver { private final boolean isLocalIndex; + // We must handle the local index data tables separately + protected final ListMultimap<String, TableRef> localIndexDataTableMap = ArrayListMultimap.<String, TableRef> create(); + protected final List<TableRef> localIndexDataTables = new ArrayList<>();; private final List<TableRef> theTableRefs; private final Map<ColumnRef, Integer> columnRefMap; private ProjectedTableColumnResolver(PTable projectedTable, PhoenixConnection conn, Map<String, UDFParseNode> udfParseNodes) throws SQLException { + super(conn, 0, udfParseNodes, null); Preconditions.checkArgument(projectedTable.getType() == PTableType.PROJECTED); this.isLocalIndex = projectedTable.getIndexType() == IndexType.LOCAL; this.columnRefMap = new HashMap<ColumnRef, Integer>(); long ts = Long.MAX_VALUE; for (int i = projectedTable.getBucketNum() == null ? 0 : 1; i < projectedTable.getColumns().size(); i++) { PColumn column = projectedTable.getColumns().get(i); - ColumnRef colRef = ((ProjectedColumn) column).getSourceColumnRef(); - TableRef tableRef = colRef.getTableRef(); - if (!tables.contains(tableRef)) { - String alias = tableRef.getTableAlias(); - if (alias != null) { - this.tableMap.put(alias, tableRef); - } - String name = tableRef.getTable().getName().getString(); - if (alias == null || !alias.equals(name)) { - tableMap.put(name, tableRef); - } - tables.add(tableRef); - if (tableRef.getLowerBoundTimeStamp() < ts) { - ts = tableRef.getLowerBoundTimeStamp(); - } + ColumnRef sourceColRef = ((ProjectedColumn) column).getSourceColumnRef(); + TableRef tableRef = sourceColRef.getTableRef(); + + if (sourceColRef instanceof LocalIndexDataColumnRef && !localIndexDataTables.contains(tableRef)) { + rememberLocalIndexDataTable(tableRef, tableRef.getTableAlias()); + } else + if (!tables.contains(tableRef)) { + rememberTable(tableRef, tableRef.getTableAlias()); + } + if (tableRef.getLowerBoundTimeStamp() < ts) { + ts = tableRef.getLowerBoundTimeStamp(); } - this.columnRefMap.put(new ColumnRef(tableRef, colRef.getColumnPosition()), column.getPosition()); + this.columnRefMap.put(new ColumnRef(tableRef, sourceColRef.getColumnPosition()), column.getPosition()); } this.theTableRefs = ImmutableList.of(new TableRef(ParseNodeFactory.createTempAlias(), projectedTable, ts, false)); - } - + + protected void rememberLocalIndexDataTable(TableRef tableRef, String alias) { + String name = tableRef.getTable().getName().getString(); Review comment: Done. ########## File path: phoenix-core/src/main/java/org/apache/phoenix/compile/FromCompiler.java ########## @@ -1171,14 +1183,69 @@ public ColumnRef resolveColumn(String schemaName, String tableName, String colNa TableRef tableRef = isLocalIndex ? super.getTables().get(0) : super.resolveTable(schemaName, tableName); if (tableRef.getTable().getIndexType() == IndexType.LOCAL) { try { - TableRef parentTableRef = super.resolveTable( - tableRef.getTable().getSchemaName().getString(), - tableRef.getTable().getParentTableName().getString()); + TableRef parentTableRef = null; + if (tableName == null) { + // No table specified + Iterator<TableRef> iterator = localIndexDataTables.iterator(); + while (iterator.hasNext()) { + TableRef searchTableRef = iterator.next(); + try { + searchTableRef.getTable().getColumnForColumnName(colName); + if (parentTableRef != null) { + throw new AmbiguousColumnException(colName); + } + parentTableRef = searchTableRef; + } catch (ColumnNotFoundException e2) { + } + } + if (parentTableRef == null) { + throw new ColumnNotFoundException(schemaName, tableName, null, colName); + } + } else { + // table specified + parentTableRef = resolveLocalIndexDataTable( schemaName, tableName); + } colRef = new ColumnRef(parentTableRef, IndexUtil.getDataColumnFamilyName(colName), IndexUtil.getDataColumnName(colName)); } catch (TableNotFoundException te) { - throw e; + //columfamily format + TableRef theTableRef = null; + PColumnFamily theColumnFamily = null; + PColumn theColumn = null; + if (schemaName != null) { + try { + // Try schemaName as the tableName and use tableName as column family name + theTableRef = resolveLocalIndexDataTable(null, schemaName); + theColumnFamily = theTableRef.getTable().getColumnFamily(tableName); + theColumn = theColumnFamily.getPColumnForColumnName(colName); + } catch (MetaDataEntityNotFoundException e2) { + } + } + if (theColumn == null) { + // Try using the tableName as a columnFamily reference instead + // and resolve column in each column family. + Iterator<TableRef> iterator = localIndexDataTables.iterator(); + while (iterator.hasNext()) { + TableRef searchTableRef = iterator.next(); Review comment: Again, this copies the similar code from the parent class, and I wanted to keep that obvious. We could change it in both places, but that would add even more noise to the patch. -- 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: issues-unsubscr...@phoenix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Bogus AmbiguousTableException in query with aliases on local indexed tables > --------------------------------------------------------------------------- > > Key: PHOENIX-6365 > URL: https://issues.apache.org/jira/browse/PHOENIX-6365 > Project: Phoenix > Issue Type: Task > Components: core > Affects Versions: 5.1.0, 4.16.0 > Reporter: Istvan Toth > Assignee: Istvan Toth > Priority: Major > > Certain queries with aliases on tbales with local indexes throw > AmbiguousTableException -- This message was sent by Atlassian Jira (v8.3.4#803005)