Skip to content
Open
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 @@ -74,7 +74,9 @@ public DataType struct(Types.StructType struct, List<DataType> fieldResults) {
Types.NestedField field = fields.get(i);
DataType type = fieldResults.get(i);
Metadata metadata = fieldMetadata(field.fieldId());
StructField sparkField = StructField.apply(field.name(), type, field.isOptional(), metadata);
StructField sparkField =
StructField.apply(field.name(), type, field.isOptional(), metadata)
.withId(Integer.toString(field.fieldId()));
if (field.doc() != null) {
sparkField = sparkField.withComment(field.doc());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.apache.spark.sql.catalyst.expressions.MetadataAttribute;
import org.apache.spark.sql.catalyst.types.DataTypeUtils;
import org.apache.spark.sql.catalyst.util.ResolveDefaultColumnsUtils$;
import org.apache.spark.sql.connector.catalog.CatalogV2Util;
import org.apache.spark.sql.connector.catalog.Column;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.GeographyType;
Expand Down Expand Up @@ -101,6 +103,34 @@ public void testSchemaConversionWithMetaDataColumnSchema() {
}
}

@Test
public void testFieldIDsInSparkColumns() {
Schema schema =
new Schema(
optional(
10,
"location",
Types.StructType.of(
optional(11, "latitude", Types.DoubleType.get()),
optional(12, "longitude", Types.DoubleType.get()))));

StructType sparkSchema = SparkSchemaUtil.convert(schema);
StructField location = sparkSchema.apply("location");
StructType locationType = (StructType) location.dataType();

assertThat(location.id().get()).isEqualTo("10");
assertThat(locationType.apply("latitude").id().get()).isEqualTo("11");
assertThat(locationType.apply("longitude").id().get()).isEqualTo("12");

Column[] columns = CatalogV2Util.structTypeToV2Columns(sparkSchema, true);
assertThat(columns).hasSize(1);
assertThat(columns[0].id()).isEqualTo("10");

StructType columnType = (StructType) columns[0].dataType();
assertThat(columnType.apply("latitude").id().get()).isEqualTo("11");
assertThat(columnType.apply("longitude").id().get()).isEqualTo("12");
}

@Test
public void testGeospatialTypeConversion() {
// a default-CRS geometry round-trips through the null <-> OGC:CRS84 normalization
Expand Down
Loading