jcamachor commented on a change in pull request #663: HIVE-21818
URL: https://github.com/apache/hive/pull/663#discussion_r291370004
##########
File path:
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/RelOptHiveTable.java
##########
@@ -275,103 +279,107 @@ public boolean isKey(ImmutableBitSet columns) {
nonNullbuilder.add(key);
}
// Then UKs
- final UniqueConstraint uki;
- try {
- uki = Hive.get().getReliableUniqueConstraints(
- hiveTblMetadata.getDbName(), hiveTblMetadata.getTableName());
- } catch (HiveException e) {
- throw new RuntimeException(e);
- }
- for (List<UniqueConstraintCol> ukCols :
uki.getUniqueConstraints().values()) {
- ImmutableBitSet.Builder keys = ImmutableBitSet.builder();
- boolean isNonNullable = true;
- for (UniqueConstraintCol ukCol : ukCols) {
- int ukPos;
- for (ukPos = 0; ukPos < rowType.getFieldNames().size(); ukPos++) {
- String colName = rowType.getFieldNames().get(ukPos);
- if (ukCol.colName.equals(colName)) {
- if(rowType.getFieldList().get(ukPos).getType().isNullable()) {
- // they should all be nullable
- isNonNullable = false;
+ if (uniqueKeyInfo != null &&
!uniqueKeyInfo.getUniqueConstraints().isEmpty()) {
+ for (List<UniqueConstraintCol> ukCols :
uniqueKeyInfo.getUniqueConstraints().values()) {
+ ImmutableBitSet.Builder keys = ImmutableBitSet.builder();
+ boolean isNonNullable = true;
+ for (UniqueConstraintCol ukCol : ukCols) {
+ int ukPos;
+ for (ukPos = 0; ukPos < rowType.getFieldNames().size(); ukPos++) {
+ String colName = rowType.getFieldNames().get(ukPos);
+ if (ukCol.colName.equals(colName)) {
+ if (rowType.getFieldList().get(ukPos).getType().isNullable()) {
+ // they should all be nullable
+ isNonNullable = false;
+ }
+ break;
}
- break;
}
+ if (ukPos == rowType.getFieldNames().size()) {
+ LOG.error("Column for unique constraint definition " +
ukCol.colName + " not found");
+ }
+ keys.set(ukPos);
}
- if (ukPos == rowType.getFieldNames().size()) {
- LOG.error("Column for unique constraint definition " + ukCol.colName
+ " not found");
+ ImmutableBitSet key = keys.build();
+ builder.add(key);
+ if (isNonNullable) {
+ nonNullbuilder.add(key);
}
- keys.set(ukPos);
- }
- ImmutableBitSet key = keys.build();
- builder.add(key);
- if(isNonNullable) {
- nonNullbuilder.add(key);
}
}
return new Pair<>(builder.build(), nonNullbuilder.build());
}
private List<RelReferentialConstraint> generateReferentialConstraints() {
- final ForeignKeyInfo fki;
- try {
- fki = Hive.get().getReliableForeignKeys(
- hiveTblMetadata.getDbName(), hiveTblMetadata.getTableName());
- } catch (HiveException e) {
- throw new RuntimeException(e);
- }
+ final ForeignKeyInfo foreignKeyInfo = hiveTblMetadata.getForeignKeyInfo();
ImmutableList.Builder<RelReferentialConstraint> builder =
ImmutableList.builder();
- for (List<ForeignKeyCol> fkCols : fki.getForeignKeys().values()) {
- List<String> foreignKeyTableQualifiedName = qualifiedTblName;
- String parentDatabaseName = fkCols.get(0).parentDatabaseName;
- String parentTableName = fkCols.get(0).parentTableName;
- List<String> parentTableQualifiedName = new ArrayList<>();
- if (parentDatabaseName != null && !parentDatabaseName.isEmpty()) {
- parentTableQualifiedName.add(parentDatabaseName);
- }
- parentTableQualifiedName.add(parentTableName);
- Table parentTab = null;
- try {
- // TODO: We have a cache for Table objects in
SemanticAnalyzer::getTableObjectByName()
- // We need to move that cache elsewhere and use it from places like
this.
- parentTab = Hive.get().getTable(parentDatabaseName, parentTableName);
- } catch (HiveException e) {
- throw new RuntimeException(e);
- }
- if (parentTab == null) {
- LOG.error("Table for primary key not found: "
- + "databaseName: " + parentDatabaseName+ ", "
+ if (foreignKeyInfo != null && !foreignKeyInfo.getForeignKeys().isEmpty()) {
+ for (List<ForeignKeyCol> fkCols :
foreignKeyInfo.getForeignKeys().values()) {
+ String parentDatabaseName = fkCols.get(0).parentDatabaseName;
+ String parentTableName = fkCols.get(0).parentTableName;
+ String qualifiedName;
+ List<String> parentTableQualifiedName = new ArrayList<>();
+ if (parentDatabaseName != null && !parentDatabaseName.isEmpty()) {
+ parentTableQualifiedName.add(parentDatabaseName);
+ parentTableQualifiedName.add(parentTableName);
+ qualifiedName = TableName.getDbTable(
+ parentDatabaseName, parentTableName);
+ } else {
+ parentTableQualifiedName.add(parentTableName);
+ qualifiedName = parentTableName;
+ }
+ Table parentTab = getTable(qualifiedName);
+ if (parentTab == null) {
+ LOG.error("Table for primary key not found: "
+ + "databaseName: " + parentDatabaseName + ", "
+ "tableName: " + parentTableName);
- return ImmutableList.of();
- }
- ImmutableList.Builder<IntPair> keys = ImmutableList.builder();
- for (ForeignKeyCol fkCol : fkCols) {
- int fkPos;
- for (fkPos = 0; fkPos < rowType.getFieldNames().size(); fkPos++) {
- String fkColName = rowType.getFieldNames().get(fkPos);
- if (fkColName.equals(fkCol.childColName)) {
- break;
- }
+ return ImmutableList.of();
}
- int pkPos;
- for (pkPos = 0; pkPos < parentTab.getAllCols().size(); pkPos++) {
- String pkColName = parentTab.getAllCols().get(pkPos).getName();
- if (pkColName.equals(fkCol.parentColName)) {
- break;
+ ImmutableList.Builder<IntPair> keys = ImmutableList.builder();
+ for (ForeignKeyCol fkCol : fkCols) {
+ int fkPos;
+ for (fkPos = 0; fkPos < rowType.getFieldNames().size(); fkPos++) {
+ String fkColName = rowType.getFieldNames().get(fkPos);
+ if (fkColName.equals(fkCol.childColName)) {
+ break;
+ }
}
+ int pkPos;
+ for (pkPos = 0; pkPos < parentTab.getAllCols().size(); pkPos++) {
+ String pkColName = parentTab.getAllCols().get(pkPos).getName();
+ if (pkColName.equals(fkCol.parentColName)) {
+ break;
+ }
+ }
+ if (fkPos == rowType.getFieldNames().size()
+ || pkPos == parentTab.getAllCols().size()) {
+ LOG.error("Column for foreign key definition " + fkCol + " not
found");
+ return ImmutableList.of();
+ }
+ keys.add(IntPair.of(fkPos, pkPos));
}
- if (fkPos == rowType.getFieldNames().size()
- || pkPos == parentTab.getAllCols().size()) {
- LOG.error("Column for foreign key definition " + fkCol + " not
found");
- return ImmutableList.of();
- }
- keys.add(IntPair.of(fkPos, pkPos));
+ builder.add(RelReferentialConstraintImpl.of(qualifiedTblName,
+ parentTableQualifiedName, keys.build()));
}
- builder.add(RelReferentialConstraintImpl.of(foreignKeyTableQualifiedName,
- parentTableQualifiedName, keys.build()));
}
return builder.build();
}
+ private Table getTable(String tableName) {
+ if (!tablesCache.containsKey(tableName)) {
+ try {
+ Table table = db.getTable(tableName);
Review comment:
You do not need the null check; this will never be called for materialized
views, since currently you cannot define constraints on them. Not sure if you
mean that?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]