Source:
<code>public ConnectorTableLayoutHandle getAlternativeLayoutHandle(ConnectorSession session, ConnectorTableLayoutHandle tableLayoutHandle, ConnectorPartitioningHandle partitioningHandle) { HiveTableLayoutHandle hiveLayoutHandle = (HiveTableLayoutHandle) tableLayoutHandle; HivePartitioningHandle hivePartitioningHandle = (HivePartitioningHandle) partitioningHandle; checkArgument(hiveLayoutHandle.getBucketHandle().isPresent(), "Hive connector only provides alternative layout for bucketed table"); HiveBucketHandle bucketHandle = hiveLayoutHandle.getBucketHandle().get(); ImmutableList<HiveType> bucketTypes = bucketHandle.getColumns().stream().map(HiveColumnHandle::getHiveType).collect(toImmutableList()); Optional<List<HiveType>> hiveTypes = hivePartitioningHandle.getHiveTypes(); checkArgument( <START> hivePartitioningHandle.getBucketFunctionType().equals(HIVE_COMPATIBLE) && <END> hiveTypes.isPresent(), "partitionColumns in HiveTableLayoutHandle must use a hive compatible bucket function"); checkArgument( hiveTypes.get().equals(bucketTypes), "Types from the new PartitioningHandle (%s) does not match the TableLayoutHandle (%s)", hiveTypes.get(), bucketTypes); int largerBucketCount = Math.max(bucketHandle.getTableBucketCount(), hivePartitioningHandle.getBucketCount()); int smallerBucketCount = Math.min(bucketHandle.getTableBucketCount(), hivePartitioningHandle.getBucketCount()); checkArgument( largerBucketCount % smallerBucketCount == 0 && Integer.bitCount(largerBucketCount / smallerBucketCount) == 1, "The requested partitioning is not a valid alternative for the table layout"); return new HiveTableLayoutHandle( hiveLayoutHandle.getSchemaTableName(), hiveLayoutHandle.getPartitionColumns(), hiveLayoutHandle.getDataColumns(), hiveLayoutHandle.getTableParameters(), hiveLayoutHandle.getPartitions().get(), hiveLayoutHandle.getDomainPredicate(), hiveLayoutHandle.getRemainingPredicate(), hiveLayoutHandle.getPredicateColumns(), hiveLayoutHandle.getPartitionColumnPredicate(), Optional.of(new HiveBucketHandle(bucketHandle.getColumns(), bucketHandle.getTableBucketCount(), hivePartitioningHandle.getBucketCount())), hiveLayoutHandle.getBucketFilter(), hiveLayoutHandle.isPushdownFilterEnabled(), hiveLayoutHandle.getLayoutString()); }</code><technical_language>hiveTypes.isPresent() is extra. is checked in HivePartitioningHandle constructor</technical_language>

Target:
public ConnectorTableLayoutHandle getAlternativeLayoutHandle(ConnectorSession session, ConnectorTableLayoutHandle tableLayoutHandle, ConnectorPartitioningHandle partitioningHandle) { HiveTableLayoutHandle hiveLayoutHandle = (HiveTableLayoutHandle) tableLayoutHandle; HivePartitioningHandle hivePartitioningHandle = (HivePartitioningHandle) partitioningHandle; checkArgument(hiveLayoutHandle.getBucketHandle().isPresent(), "Hive connector only provides alternative layout for bucketed table"); HiveBucketHandle bucketHandle = hiveLayoutHandle.getBucketHandle().get(); ImmutableList<HiveType> bucketTypes = bucketHandle.getColumns().stream().map(HiveColumnHandle::getHiveType).collect(toImmutableList()); Optional<List<HiveType>> hiveTypes = hivePartitioningHandle.getHiveTypes(); checkArgument( hivePartitioningHandle.getBucketFunctionType().equals(HIVE_COMPATIBLE), "bucketFunctionType is expected to be HIVE_COMPATIBLE, got: %s", hivePartitioningHandle.getBucketFunctionType()); checkArgument( hiveTypes.get().equals(bucketTypes), "Types from the new PartitioningHandle (%s) does not match the TableLayoutHandle (%s)", hiveTypes.get(), bucketTypes); int largerBucketCount = Math.max(bucketHandle.getTableBucketCount(), hivePartitioningHandle.getBucketCount()); int smallerBucketCount = Math.min(bucketHandle.getTableBucketCount(), hivePartitioningHandle.getBucketCount()); checkArgument( largerBucketCount % smallerBucketCount == 0 && Integer.bitCount(largerBucketCount / smallerBucketCount) == 1, "The requested partitioning is not a valid alternative for the table layout"); return new HiveTableLayoutHandle( hiveLayoutHandle.getSchemaTableName(), hiveLayoutHandle.getPartitionColumns(), hiveLayoutHandle.getDataColumns(), hiveLayoutHandle.getTableParameters(), hiveLayoutHandle.getPartitions().get(), hiveLayoutHandle.getDomainPredicate(), hiveLayoutHandle.getRemainingPredicate(), hiveLayoutHandle.getPredicateColumns(), hiveLayoutHandle.getPartitionColumnPredicate(), Optional.of(new HiveBucketHandle(bucketHandle.getColumns(), bucketHandle.getTableBucketCount(), hivePartitioningHandle.getBucketCount())), hiveLayoutHandle.getBucketFilter(), hiveLayoutHandle.isPushdownFilterEnabled(), hiveLayoutHandle.getLayoutString()); }