-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2962] feat(catalog-lakehouse-iceberg): Add reserved properties to I…
…ceberg Table Properties in Gravitino (#3511) ### What changes were proposed in this pull request? Add reserved properties to Table properties when load an Iceberg table, such as: ``` provider, format, current-snapshot-id, location, format-version, sort-order, identifier-fields ``` ### Why are the changes needed? for example, when execute `desc extended IcebergTableName`,it will get some information from the result properties of this Iceberg table, so it should contain the above properties. Fix: #2962 ### Does this PR introduce any user-facing change? No. ### How was this patch tested? New UTs and ITs.
- Loading branch information
Showing
7 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...strato/gravitino/catalog/lakehouse/iceberg/converter/DescribeIcebergSortOrderVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.datastrato.gravitino.catalog.lakehouse.iceberg.converter; | ||
|
||
import org.apache.iceberg.NullOrder; | ||
import org.apache.iceberg.transforms.SortOrderVisitor; | ||
|
||
/** | ||
* Convert expressions of Iceberg SortOrders to function string. | ||
* | ||
* <p>Referred from org/apache/iceberg/spark/Spark3Util/DescribeSortOrderVisitor.java | ||
*/ | ||
public class DescribeIcebergSortOrderVisitor implements SortOrderVisitor<String> { | ||
public static final DescribeIcebergSortOrderVisitor INSTANCE = | ||
new DescribeIcebergSortOrderVisitor(); | ||
|
||
private DescribeIcebergSortOrderVisitor() {} | ||
|
||
@Override | ||
public String field( | ||
String sourceName, | ||
int sourceId, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("%s %s %s", sourceName, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String bucket( | ||
String sourceName, | ||
int sourceId, | ||
int numBuckets, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("bucket(%s, %s) %s %s", numBuckets, sourceName, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String truncate( | ||
String sourceName, | ||
int sourceId, | ||
int width, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("truncate(%s, %s) %s %s", sourceName, width, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String year( | ||
String sourceName, | ||
int sourceId, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("years(%s) %s %s", sourceName, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String month( | ||
String sourceName, | ||
int sourceId, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("months(%s) %s %s", sourceName, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String day( | ||
String sourceName, | ||
int sourceId, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("days(%s) %s %s", sourceName, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String hour( | ||
String sourceName, | ||
int sourceId, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("hours(%s) %s %s", sourceName, direction, nullOrder); | ||
} | ||
|
||
@Override | ||
public String unknown( | ||
String sourceName, | ||
int sourceId, | ||
String transform, | ||
org.apache.iceberg.SortDirection direction, | ||
NullOrder nullOrder) { | ||
return String.format("%s(%s) %s %s", transform, sourceName, direction, nullOrder); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
.../com/datastrato/gravitino/catalog/lakehouse/iceberg/utils/IcebergTablePropertiesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.datastrato.gravitino.catalog.lakehouse.iceberg.utils; | ||
|
||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTable.DEFAULT_ICEBERG_PROVIDER; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.CURRENT_SNAPSHOT_ID; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.FORMAT; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.FORMAT_VERSION; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.IDENTIFIER_FIELDS; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.LOCATION; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.PROVIDER; | ||
import static com.datastrato.gravitino.catalog.lakehouse.iceberg.IcebergTablePropertiesMetadata.SORT_ORDER; | ||
|
||
import com.datastrato.gravitino.catalog.lakehouse.iceberg.converter.DescribeIcebergSortOrderVisitor; | ||
import com.google.common.base.Joiner; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.apache.iceberg.TableMetadata; | ||
import org.apache.iceberg.TableProperties; | ||
import org.apache.iceberg.transforms.SortOrderVisitor; | ||
|
||
/** Referred from org/apache/iceberg/spark/source/SparkTable.java#properties() */ | ||
public class IcebergTablePropertiesUtil { | ||
|
||
public static Map<String, String> buildReservedProperties(TableMetadata table) { | ||
Map<String, String> properties = new HashMap<>(); | ||
String fileFormat = | ||
table | ||
.properties() | ||
.getOrDefault( | ||
TableProperties.DEFAULT_FILE_FORMAT, TableProperties.DEFAULT_FILE_FORMAT_DEFAULT); | ||
properties.put(FORMAT, String.join("/", DEFAULT_ICEBERG_PROVIDER, fileFormat)); | ||
properties.put(PROVIDER, DEFAULT_ICEBERG_PROVIDER); | ||
String currentSnapshotId = | ||
table.currentSnapshot() != null | ||
? String.valueOf(table.currentSnapshot().snapshotId()) | ||
: "none"; | ||
properties.put(CURRENT_SNAPSHOT_ID, currentSnapshotId); | ||
properties.put(LOCATION, table.location()); | ||
|
||
properties.put(FORMAT_VERSION, String.valueOf(table.formatVersion())); | ||
|
||
if (!table.sortOrder().isUnsorted()) { | ||
properties.put(SORT_ORDER, describeIcebergSortOrder(table.sortOrder())); | ||
} | ||
|
||
Set<String> identifierFields = table.schema().identifierFieldNames(); | ||
if (!identifierFields.isEmpty()) { | ||
properties.put(IDENTIFIER_FIELDS, "[" + String.join(",", identifierFields) + "]"); | ||
} | ||
|
||
return properties; | ||
} | ||
|
||
private static String describeIcebergSortOrder(org.apache.iceberg.SortOrder sortOrder) { | ||
return Joiner.on(", ") | ||
.join(SortOrderVisitor.visit(sortOrder, DescribeIcebergSortOrderVisitor.INSTANCE)); | ||
} | ||
|
||
private IcebergTablePropertiesUtil() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters