Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core][bug] Fix the incorrect partition key format generated from the DATE type #853

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class PartitionPathUtils {
'\u0009', '\n', '\u000B', '\u000C', '\r', '\u000E', '\u000F', '\u0010',
'\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018',
'\u0019', '\u001A', '\u001B', '\u001C', '\u001D', '\u001E', '\u001F', '"', '#',
'%', '\'', '*', '/', ':', '=', '?', '\\', '\u007F', '{', '}', '[', ']', '^'
'%', '\'', '*', '/', '=', '?', '\\', '\u007F', '{', '}', '[', ']', '^'
};

for (char c : clist) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,28 @@
package org.apache.paimon.utils;

import org.apache.paimon.data.InternalRow;
import org.apache.paimon.types.DataType;
import org.apache.paimon.types.RowType;

import java.time.LocalTime;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

/** PartitionComputer for {@link InternalRow}. */
public class RowDataPartitionComputer {

protected final String defaultPartValue;
protected final String[] partitionColumns;
protected final List<DataType> fieldTypes;
protected final InternalRow.FieldGetter[] partitionFieldGetters;

public RowDataPartitionComputer(
String defaultPartValue, RowType rowType, String[] partitionColumns) {
this.defaultPartValue = defaultPartValue;
this.partitionColumns = partitionColumns;
this.fieldTypes = rowType.getFieldTypes();
List<String> columnList = rowType.getFieldNames();
this.partitionFieldGetters =
Arrays.stream(partitionColumns)
Expand All @@ -52,12 +57,35 @@ public LinkedHashMap<String, String> generatePartValues(InternalRow in) {

for (int i = 0; i < partitionFieldGetters.length; i++) {
Object field = partitionFieldGetters[i].getFieldOrNull(in);
String partitionValue = field != null ? field.toString() : null;
String partitionValue;

if (Objects.nonNull(field)) {
switch (fieldTypes.get(i).getTypeRoot()) {
case DATE:
partitionValue = DateTimeUtils.toLocalDate((int) field).toString();
break;
case TIME_WITHOUT_TIME_ZONE:
partitionValue = formatTimeTypePart((int) field);
break;
default:
partitionValue = field.toString();
}
} else {
partitionValue = null;
}

if (StringUtils.isNullOrWhitespaceOnly(partitionValue)) {
partitionValue = defaultPartValue;
}
partSpec.put(partitionColumns[i], partitionValue);
}
return partSpec;
}

public String formatTimeTypePart(int field) {
// retain only second-level precision
int time = field - (field % 1000);
LocalTime localTime = DateTimeUtils.toLocalTime(time);
return localTime.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.paimon.utils;

import org.apache.paimon.data.BinaryRow;
import org.apache.paimon.data.BinaryRowWriter;
import org.apache.paimon.types.DataType;
import org.apache.paimon.types.DateType;
import org.apache.paimon.types.IntType;
import org.apache.paimon.types.RowType;
import org.apache.paimon.types.TimeType;

import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.LinkedHashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;

/** Tests for {@link RowDataPartitionComputer}. */
public class RowDataPartitionComputerTest {

@Test
public void testGeneratePartitionValues() {
RowDataPartitionComputer partitionComputer =
FileStorePathFactory.getPartitionComputer(
RowType.of(
new DataType[] {new DateType(), new IntType(), new TimeType()},
new String[] {"dt", "hour", "time"}),
FileStorePathFactory.PARTITION_DEFAULT_NAME.defaultValue());

LocalDate localDate = LocalDate.now();
int hour = LocalTime.now().getHour();
int time = LocalTime.now().toSecondOfDay() * 1000;
BinaryRow binaryRow = buildPartitionBinaryRow(localDate, hour, time);
LinkedHashMap<String, String> map = partitionComputer.generatePartValues(binaryRow);

assertEquals(map.get("dt"), localDate.toString());
assertEquals(map.get("hour"), String.valueOf(hour));
assertEquals(map.get("time"), partitionComputer.formatTimeTypePart(time));
}

public BinaryRow buildPartitionBinaryRow(LocalDate dt, Integer hour, Integer time) {
BinaryRow partition = new BinaryRow(3);
BinaryRowWriter writer = new BinaryRowWriter(partition);
if (dt != null) {
writer.writeInt(0, (int) dt.toEpochDay());
} else {
writer.setNullAt(0);
}
if (hour != null) {
writer.writeInt(1, hour);
} else {
writer.setNullAt(1);
}
if (time != null) {
writer.writeInt(2, time);
} else {
writer.setNullAt(2);
}
writer.complete();
return partition;
}
}