Skip to content

Commit

Permalink
Core: Support unpartitioned specs in SortOrderUtil (apache#2239)
Browse files Browse the repository at this point in the history
  • Loading branch information
aokolnychyi authored Feb 25, 2021
1 parent f356be7 commit e237a69
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
6 changes: 2 additions & 4 deletions core/src/main/java/org/apache/iceberg/util/SortOrderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ private SortOrderUtil() {
}

public static SortOrder buildSortOrder(Table table) {
return buildSortOrder(table.spec(), table.sortOrder());
return buildSortOrder(table.schema(), table.spec(), table.sortOrder());
}

public static SortOrder buildSortOrder(PartitionSpec spec, SortOrder sortOrder) {
static SortOrder buildSortOrder(Schema schema, PartitionSpec spec, SortOrder sortOrder) {
if (sortOrder.isUnsorted() && spec.isUnpartitioned()) {
return SortOrder.unsorted();
}

Schema schema = spec.schema();

Multimap<Integer, SortField> sortFieldIndex = Multimaps.index(sortOrder.fields(), SortField::sourceId);

// build a sort prefix of partition fields that are not already in the sort order
Expand Down
94 changes: 94 additions & 0 deletions core/src/test/java/org/apache/iceberg/util/TestSortOrderUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 org.apache.iceberg.util;

import java.io.File;
import java.io.IOException;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.SortOrder;
import org.apache.iceberg.TestTables;
import org.apache.iceberg.types.Types;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.apache.iceberg.NullOrder.NULLS_LAST;
import static org.apache.iceberg.SortDirection.ASC;
import static org.apache.iceberg.types.Types.NestedField.required;

@RunWith(Parameterized.class)
public class TestSortOrderUtil {

// column ids will be reassigned during table creation
private static final Schema SCHEMA = new Schema(
required(10, "id", Types.IntegerType.get()),
required(11, "data", Types.StringType.get())
);

@Rule
public TemporaryFolder temp = new TemporaryFolder();
private File tableDir = null;

@Parameterized.Parameters(name = "formatVersion = {0}")
public static Object[] parameters() {
return new Object[] { 1, 2 };
}

private final int formatVersion;

public TestSortOrderUtil(int formatVersion) {
this.formatVersion = formatVersion;
}

@Before
public void setupTableDir() throws IOException {
this.tableDir = temp.newFolder();
}

@After
public void cleanupTables() {
TestTables.clearTables();
}

@Test
public void testEmptySpecs() {
PartitionSpec spec = PartitionSpec.unpartitioned();
SortOrder order = SortOrder.builderFor(SCHEMA)
.withOrderId(1)
.asc("id", NULLS_LAST)
.build();
TestTables.TestTable table = TestTables.create(tableDir, "test", SCHEMA, spec, order, formatVersion);

// pass PartitionSpec.unpartitioned() on purpose as it has an empty schema
SortOrder actualOrder = SortOrderUtil.buildSortOrder(table.schema(), spec, table.sortOrder());

Assert.assertEquals("Order ID must be fresh", 1, actualOrder.orderId());
Assert.assertEquals("Order must have 1 field", 1, actualOrder.fields().size());
Assert.assertEquals("Field id must be fresh", 1, actualOrder.fields().get(0).sourceId());
Assert.assertEquals("Direction must match", ASC, actualOrder.fields().get(0).direction());
Assert.assertEquals("Null order must match", NULLS_LAST, actualOrder.fields().get(0).nullOrder());
}
}

0 comments on commit e237a69

Please sign in to comment.