Skip to content
Merged
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 @@ -186,6 +186,35 @@ public static void validateTableSchema(TableSchema schema) {
}
fileFormat.validateDataFields(new RowType(fieldsInNormalFile));

for (Map.Entry<Integer, String> entry : options.fileFormatPerLevel().entrySet()) {
if (!"avro".equalsIgnoreCase(entry.getValue())) {
continue;
}
for (DataField field : fieldsInNormalFile) {
DataType type = field.type();
int precision = -1;
if (type instanceof TimestampType) {
precision = ((TimestampType) type).getPrecision();
} else if (type instanceof LocalZonedTimestampType) {
precision = ((LocalZonedTimestampType) type).getPrecision();
}
if (precision > 6) {
throw new IllegalArgumentException(
String.format(
"'%s' entry '%d:avro' is incompatible with column '%s' of type %s: "
+ "Avro supports timestamp precision up to 6, got %d. "
+ "Either lower the column precision, drop the per-level mapping for level %d, "
+ "or use a different format (parquet or orc) for that level.",
CoreOptions.FILE_FORMAT_PER_LEVEL.key(),
entry.getKey(),
field.name(),
type,
precision,
entry.getKey()));
}
}
}

// Check column names in schema
schema.fieldNames()
.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,45 @@ private void validateTableSchemaWithMapField(Map<String, String> options) {
validateTableSchema(
new TableSchema(1, fields, 10, emptyList(), singletonList("f1"), options, ""));
}

@Test
public void testFileFormatPerLevelRejectsIncompatibleSchema() {
List<DataField> fields =
Arrays.asList(
new DataField(0, "k", DataTypes.INT()),
new DataField(1, "v", DataTypes.TIMESTAMP(9)));
Map<String, String> options = new HashMap<>();
options.put(BUCKET.key(), String.valueOf(-1));
options.put(CoreOptions.FILE_FORMAT_PER_LEVEL.key(), "0:avro");

assertThatThrownBy(
() ->
validateTableSchema(
new TableSchema(
1,
fields,
10,
emptyList(),
singletonList("k"),
options,
"")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("file.format.per.level")
.hasMessageContaining("0:avro")
.hasMessageContaining("TIMESTAMP");
}

@Test
public void testFileFormatPerLevelAcceptsCompatibleSchema() {
List<DataField> fields =
Arrays.asList(
new DataField(0, "k", DataTypes.INT()),
new DataField(1, "v", DataTypes.TIMESTAMP(9)));
Map<String, String> options = new HashMap<>();
options.put(BUCKET.key(), String.valueOf(-1));
options.put(CoreOptions.FILE_FORMAT_PER_LEVEL.key(), "0:parquet");

validateTableSchema(
new TableSchema(1, fields, 10, emptyList(), singletonList("k"), options, ""));
}
}