Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
caican00 committed May 22, 2024
1 parent dca7017 commit ace6cae
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.iceberg.Schema;
import org.apache.iceberg.SortField;
import org.apache.iceberg.types.Types;

Expand Down Expand Up @@ -133,22 +134,25 @@ protected static Types.NestedField[] createNestedField(String... colNames) {
return results.toArray(new Types.NestedField[0]);
}

protected static String convertIcebergTransform(SortField sortField) {
protected static String getIcebergTransfromString(SortField sortField, Schema schema) {
String transform = sortField.transform().toString();
Map<Integer, String> idToName = schema.idToName();
if (transform.startsWith("year")
|| transform.startsWith("month")
|| transform.startsWith("day")
|| transform.startsWith("hour")
|| transform.startsWith("identity")) {
return transform + "(";
return String.format("%s(%s)", transform, idToName.get(sortField.sourceId()));
} else if (transform.startsWith("truncate") || transform.startsWith("bucket")) {
return transform.replace("[", "(").replace("]", ", ");
return String.format(
"%s, %s)",
transform.replace("[", "(").replace("]", ""), idToName.get(sortField.sourceId()));
} else {
throw new RuntimeException("Unsupported Iceberg transform type");
}
}

protected static String convertGravitinoSortOrderExpression(Expression sortOrderExpression) {
protected static String getGravitinoSortOrderExpressionString(Expression sortOrderExpression) {
if (sortOrderExpression instanceof NamedReference.FieldReference) {
NamedReference.FieldReference fieldReference =
(NamedReference.FieldReference) sortOrderExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public void testFromSortOrder() {
? NullOrdering.NULLS_FIRST
: NullOrdering.NULLS_LAST,
sortOrder.nullOrdering());
String icebergSortOrderString =
convertIcebergTransform(sortField) + idToName.get(sortField.sourceId()) + ")";
String gravitinoSortOrderString = convertGravitinoSortOrderExpression(sortOrder.expression());
String icebergSortOrderString = getIcebergTransfromString(sortField, schema);
String gravitinoSortOrderString =
getGravitinoSortOrderExpressionString(sortOrder.expression());
Assertions.assertEquals(icebergSortOrderString, gravitinoSortOrderString);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public void testToSortOrder() {
? NullOrder.NULLS_FIRST
: NullOrder.NULLS_LAST,
sortField.nullOrder());
String icebergSortOrderString =
convertIcebergTransform(sortField) + idToName.get(sortField.sourceId()) + ")";
String gravitinoSortOrderString = convertGravitinoSortOrderExpression(sortOrder.expression());
String icebergSortOrderString = getIcebergTransfromString(sortField, schema);
String gravitinoSortOrderString =
getGravitinoSortOrderExpressionString(sortOrder.expression());
Assertions.assertEquals(icebergSortOrderString, gravitinoSortOrderString);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,18 +1211,6 @@ public void testTableSortOrder() {
partitioning,
loadTable);

SortOrder[] loadedSortOrders = loadTable.sortOrder();
Assertions.assertEquals(sortOrders.length, loadedSortOrders.length);
for (int i = 0; i < sortOrders.length; i++) {
// `sortOrders` are converted to Iceberg sortOrders when invoking tableCatalog.createTable
// method, and then the Iceberg SortOrders are converted to Gravitino SortOrders, which is
// `loadedSortOrders` when invoking tableCatalog.loadTable method.
// Therefore, here we only need to compare whether sortOrders and loadedSortOrders are equal
// to verify whether the transformation between Gravitino sortOrders and Iceberg sortOrders
// meets the expectation.
Assertions.assertEquals(sortOrders[i].toString(), loadedSortOrders[i].toString());
}

Assertions.assertDoesNotThrow(() -> tableCatalog.dropTable(tableIdentifier));
}

Expand Down

0 comments on commit ace6cae

Please sign in to comment.