Skip to content

Commit

Permalink
IMPALA-13035
Browse files Browse the repository at this point in the history
  • Loading branch information
Asmoday committed May 30, 2024
1 parent bafce5c commit ccb5a85
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
9 changes: 6 additions & 3 deletions fe/src/main/java/org/apache/impala/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3619,7 +3619,7 @@ public void addAccessEvent(TAccessEvent event) {
*/
public FeTable getTable(TableName tblName, boolean mustExist)
throws AnalysisException, TableLoadingException {
if (IcebergMetadataTable.isIcebergMetadataTable(tblName.toPath())) {
if (IcebergMetadataTable.isIcebergMetadataTable(tblName.toPath(), this)) {
return getMetadataVirtualTable(tblName.toPath());
}
FeTable table = globalState_.stmtTableCache.tables.get(tblName);
Expand Down Expand Up @@ -3671,7 +3671,8 @@ public void addVirtualTable(VirtualTable virtTable) {
*/
public FeTable getMetadataVirtualTable(List<String> tblRefPath)
throws AnalysisException {
Preconditions.checkArgument(IcebergMetadataTable.isIcebergMetadataTable(tblRefPath));
Preconditions.checkArgument(
IcebergMetadataTable.canBeIcebergMetadataTable(tblRefPath));
try {
TableName catalogTableName = new TableName(tblRefPath.get(0),
tblRefPath.get(1));
Expand All @@ -3683,8 +3684,10 @@ public FeTable getMetadataVirtualTable(List<String> tblRefPath)
if (catalogTable instanceof IcebergMetadataTable || catalogTable == null) {
return catalogTable;
}
Preconditions.checkState(catalogTable instanceof FeIcebergTable);
FeIcebergTable catalogIceTable = (FeIcebergTable) catalogTable;
IcebergMetadataTable virtualTable =
new IcebergMetadataTable(catalogTable, tblRefPath.get(2));
new IcebergMetadataTable(catalogIceTable, tblRefPath.get(2));
getStmtTableCache().tables.put(catalogTableName, catalogTable);
getStmtTableCache().tables.put(virtualTableName, virtualTable);
return virtualTable;
Expand Down
2 changes: 1 addition & 1 deletion fe/src/main/java/org/apache/impala/analysis/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public static List<TableName> getCandidateTables(List<String> path, String sessi
String dbName = (tblNameIdx == 0) ? sessionDb : path.get(0);
String tblName = path.get(tblNameIdx);
String vTblName = null;
if (IcebergMetadataTable.isIcebergMetadataTable(path)) {
if (IcebergMetadataTable.canBeIcebergMetadataTable(path)) {
vTblName = path.get(2);
}
result.add(new TableName(dbName, tblName, vTblName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.iceberg.MetadataTableUtils;
import org.apache.iceberg.Schema;
import org.apache.iceberg.Table;
import org.apache.impala.analysis.Analyzer;
import org.apache.impala.analysis.TableName;
import org.apache.impala.catalog.CatalogObject.ThriftObjectType;
import org.apache.impala.catalog.Column;
Expand Down Expand Up @@ -59,11 +60,10 @@ public class IcebergMetadataTable extends VirtualTable {
// Name of the metadata table.
private String metadataTableName_;

public IcebergMetadataTable(FeTable baseTable, String metadataTableTypeStr)
public IcebergMetadataTable(FeIcebergTable baseTable, String metadataTableTypeStr)
throws ImpalaRuntimeException {
super(null, baseTable.getDb(), baseTable.getName(), baseTable.getOwnerUser());
Preconditions.checkArgument(baseTable instanceof FeIcebergTable);
baseTable_ = (FeIcebergTable) baseTable;
baseTable_ = baseTable;
metadataTableName_ = metadataTableTypeStr.toUpperCase();
MetadataTableType type = MetadataTableType.from(metadataTableTypeStr.toUpperCase());
Preconditions.checkNotNull(type);
Expand Down Expand Up @@ -136,7 +136,31 @@ private List<TColumnDescriptor> getTColumnDescriptors() {
/**
* Returns true if the table ref is referring to a valid metadata table.
*/
public static boolean isIcebergMetadataTable(List<String> tblRefPath) {
public static boolean isIcebergMetadataTable(List<String> tblRefPath,
Analyzer analyzer) {
if (!canBeIcebergMetadataTable(tblRefPath)) return false;

TableName virtualTableName = new TableName(tblRefPath.get(0),
tblRefPath.get(1), tblRefPath.get(2));
// The catalog table (the base of the virtual table) has been loaded and cached
// under the name of the virtual table.
FeTable catalogTable = analyzer.getStmtTableCache().tables.get(virtualTableName);
// If the metadata table has already been analyzed in the query, the table cache will
// return the virtual table, not the base table.
return catalogTable instanceof FeIcebergTable ||
catalogTable instanceof IcebergMetadataTable;
}

/**
* Returns true if the path could refer to an Iceberg metadata table in a syntactically
* correct way (also checking that the name of the metadata table is valid). Does not
* check whether the base table is an Iceberg table, so the path is not guaranteed to
* actually refer to a valid Iceberg metadata table.
*
* This function can be called before analysis is done, when isIcebergMetadataTable()
* cannot be called.
*/
public static boolean canBeIcebergMetadataTable(List<String> tblRefPath) {
if (tblRefPath == null) return false;
if (tblRefPath.size() < 3) return false;
String vTableName = tblRefPath.get(2).toUpperCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,9 @@ public static TDescribeResult buildIcebergDescribeMinimalResult(List<Column> col
* it is simpler to re-create this object than to extract those from a new
* org.apache.iceberg.Table object or to send it over.
*/
public static TDescribeResult buildIcebergMetadataDescribeMinimalResult(FeTable table,
String vTableName) throws ImpalaRuntimeException {
public static TDescribeResult buildIcebergMetadataDescribeMinimalResult(
FeIcebergTable table, String vTableName) throws ImpalaRuntimeException {
IcebergMetadataTable metadataTable = new IcebergMetadataTable(table, vTableName);
return buildIcebergDescribeMinimalResult(metadataTable.getColumns());
}
}
}
4 changes: 2 additions & 2 deletions fe/src/main/java/org/apache/impala/service/Frontend.java
Original file line number Diff line number Diff line change
Expand Up @@ -1797,8 +1797,8 @@ private TDescribeResult doDescribeTable(TTableName tableName,
return DescribeResultFactory.buildIcebergDescribeMinimalResult(filteredColumns);
} else {
Preconditions.checkArgument(vTableName != null);
return DescribeResultFactory.buildIcebergMetadataDescribeMinimalResult(table,
vTableName);
return DescribeResultFactory.buildIcebergMetadataDescribeMinimalResult(
(FeIcebergTable) table, vTableName);
}
} else {
return DescribeResultFactory.buildDescribeMinimalResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ select * from $DATABASE.empty_ice_tbl.entries;
INT,BIGINT,BIGINT,BIGINT,STRING,STRING

####
# Test 2 : Test select list
# Test select list
####
====
---- QUERY
Expand Down Expand Up @@ -401,7 +401,7 @@ AnalysisException: FOR SYSTEM_VERSION AS OF clause is only supported for Iceberg
====

####
# Test 9 : Use-cases
# Use-cases
####
====
---- QUERY
Expand Down Expand Up @@ -430,11 +430,20 @@ row_regex:[1-9]\d*|0,'$NAMENODE/test-warehouse/iceberg_test/hadoop_catalog/ice/i
---- TYPES
INT,STRING,BIGINT

####
# Test querying a metadata table of a non-Iceberg table.
####
====
---- QUERY
select * from functional_parquet.alltypes.`files`;
---- CATCH
AnalysisException: Could not resolve table reference: 'functional_parquet.alltypes.files'
====

####
# Invalid operations
# In most cases the parser catches the table reference.
####
====
---- QUERY
describe formatted functional_parquet.iceberg_query_metadata.snapshots;
---- CATCH
Expand Down

0 comments on commit ccb5a85

Please sign in to comment.