Implementation:Haifengl Smile StructType
| Knowledge Sources | |
|---|---|
| Domains | Type System, Schema, Data Structures |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
StructType is a record class that represents the schema of a data frame as an ordered collection of named, typed fields.
Description
StructType is a Java record implementing the DataType interface. It defines a struct data type determined by a fixed order of fields, where each field has a name, data type, and optional measurement level. The record maintains a list of StructField objects and a map from field names to ordinal indices for fast lookup. It supports field access by index or name, field mutation (set, rename, add), and provides factory methods to construct schemas from Java record/bean classes, ValueVector columns, Property arrays, and JDBC ResultSet metadata.
Usage
Use StructType when defining or inspecting the schema of a DataFrame, when reading data from databases, or when creating typed data pipelines. It is the primary schema representation in the Smile data framework.
Code Reference
Source Location
- Repository: Haifengl_Smile
- File: base/src/main/java/smile/data/type/StructType.java
- Lines: 1-371
Signature
public record StructType(List<StructField> fields, Map<String, Integer> index) implements DataType {
// Constructors
public StructType(List<StructField> fields);
public StructType(StructField... fields);
// Field access
public int length();
public StructField field(int i);
public StructField field(String name);
public StructField apply(int i);
public StructField apply(String name);
public int indexOf(String field);
public String[] names();
public DataType[] dtypes();
public Measure[] measures();
// Field mutation
public void set(int i, StructField field);
public void update(int i, StructField field);
public void rename(String name, String newName);
public void add(StructField field);
// Parsing
public List<Function<String, Object>> parser();
public Tuple valueOf(String s);
// DataType interface
public String name();
public ID id();
// Static factory methods
public static StructType of(ValueVector... columns);
public static StructType of(Class<?> clazz);
public static StructType of(Property[] props);
public static StructType of(ResultSet rs) throws SQLException;
public static StructType of(ResultSetMetaData meta, String dbms) throws SQLException;
}
Import
import smile.data.type.StructType;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| fields | StructField[] or List<StructField> | Yes | The ordered list of struct fields defining the schema. |
| clazz | Class<?> | Yes (for of(Class)) | A Java record or bean class to introspect. |
| rs | ResultSet | Yes (for of(ResultSet)) | A JDBC result set to extract schema metadata from. |
| meta | ResultSetMetaData | Yes (for of(meta, dbms)) | JDBC result set metadata. |
| dbms | String | Yes (for of(meta, dbms)) | Database product name for type mapping. |
Outputs
| Name | Type | Description |
|---|---|---|
| StructType | StructType | The struct type representing the schema. |
| length() | int | The number of fields in the schema. |
| names() | String[] | Array of all field names. |
| dtypes() | DataType[] | Array of all field data types. |
| measures() | Measure[] | Array of all field measurement levels. |
| parser() | List<Function<String, Object>> | List of parsing functions for each field. |
Usage Examples
Basic Usage
import smile.data.type.StructType;
import smile.data.type.StructField;
import smile.data.type.DataTypes;
// Create a schema manually
StructType schema = new StructType(
new StructField("name", DataTypes.StringType),
new StructField("age", DataTypes.IntType),
new StructField("salary", DataTypes.DoubleType)
);
// Access fields
int numFields = schema.length(); // 3
StructField nameField = schema.field(0); // name: String
int ageIndex = schema.indexOf("age"); // 1
String[] names = schema.names(); // ["name", "age", "salary"]
// Modify schema
schema.rename("salary", "income");
schema.add(new StructField("department", DataTypes.StringType));
From Java Record
import smile.data.type.StructType;
// Derive schema from a Java record
record Person(String name, int age, double height) {}
StructType schema = StructType.of(Person.class);
// Fields: name: String, age: int, height: double