Implementation:Haifengl Smile DataType System
| Knowledge Sources | |
|---|---|
| Domains | Type System, Data Structures, Schema |
| Last Updated | 2026-02-08 22:00 GMT |
Overview
DataType is the core interface for Smile's type system, defining all supported data types and providing methods for type checking, conversion, inference, and promotion.
Description
DataType is a Serializable interface that represents the data types used throughout the Smile data framework. It defines an inner enum ID with 16 type identifiers (Boolean, Byte, Char, Short, Int, Long, Float, Double, Decimal, String, Date, Time, DateTime, Object, Array, Struct). The interface provides default methods for type classification (isPrimitive, isNumeric, isFloating, isIntegral, etc.), type promotion rules for arithmetic expressions, type coercion for schema inference, and static factory methods to construct DataType instances from strings, Java classes, or JDBC types.
Usage
Use DataType when defining schemas for DataFrames, when parsing data from files or databases, or when performing type inference on string data. It is the foundation of Smile's type system and is used by StructField, StructType, and DataTypes throughout the framework.
Code Reference
Source Location
- Repository: Haifengl_Smile
- File: base/src/main/java/smile/data/type/DataType.java
- Lines: 1-430
Signature
public interface DataType extends Serializable {
// Inner enum
enum ID { Boolean, Byte, Char, Short, Int, Long, Float, Double,
Decimal, String, Date, Time, DateTime, Object, Array, Struct }
// Abstract methods
ID id();
String name();
Object valueOf(String s);
// Default type-check methods
default String toString(Object o);
default boolean isNullable();
default boolean isPrimitive();
default boolean isFloating();
default boolean isIntegral();
default boolean isNumeric();
default boolean isBoolean();
default boolean isChar();
default boolean isByte();
default boolean isShort();
default boolean isInt();
default boolean isLong();
default boolean isDecimal();
default boolean isFloat();
default boolean isDouble();
default boolean isString();
default boolean isObject();
// Static utility methods
static DataType prompt(DataType a, DataType b);
static DataType coerce(DataType a, DataType b);
static DataType infer(String s);
static DataType of(String s) throws ClassNotFoundException;
static DataType of(Class<?> clazz);
static DataType of(java.sql.JDBCType type, boolean nullable, String dbms);
}
Import
import smile.data.type.DataType;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| s | String | Yes (for valueOf/infer/of) | String representation of a value or type name. |
| clazz | Class<?> | Yes (for of(Class)) | A Java class to map to a Smile DataType. |
| type | JDBCType | Yes (for of(JDBCType)) | A JDBC type to convert. |
| nullable | boolean | Yes (for of(JDBCType)) | Whether the column allows null values. |
| dbms | String | Yes (for of(JDBCType)) | The database product name (e.g., "SQLite"). |
Outputs
| Name | Type | Description |
|---|---|---|
| id() | DataType.ID | The type identifier enum value. |
| name() | String | The type name for external catalogs. |
| valueOf(s) | Object | The parsed value from a string representation. |
| of(s) | DataType | A DataType instance from its string representation. |
| infer(s) | DataType | The inferred DataType from a string value (e.g., "123" yields IntType). |
| prompt(a, b) | DataType | The promoted type for arithmetic between types a and b. |
| coerce(a, b) | DataType | The common type for schema inference between types a and b. |
Usage Examples
Basic Usage
import smile.data.type.DataType;
import smile.data.type.DataTypes;
// Get a DataType from a string name
DataType intType = DataType.of("int");
DataType doubleType = DataType.of("double");
// Check type properties
boolean isNum = intType.isNumeric(); // true
boolean isPrim = intType.isPrimitive(); // true
boolean isNull = intType.isNullable(); // false
// Type promotion for expressions
DataType promoted = DataType.prompt(intType, doubleType); // DoubleType
// Infer type from string values
DataType inferred1 = DataType.infer("42"); // IntType
DataType inferred2 = DataType.infer("3.14"); // DoubleType
DataType inferred3 = DataType.infer("2024-01-15"); // DateType
DataType inferred4 = DataType.infer("hello"); // StringType
From Java Class
import smile.data.type.DataType;
// Map Java classes to Smile DataTypes
DataType dt1 = DataType.of(int.class); // IntType
DataType dt2 = DataType.of(Double.class); // NullableDoubleType
DataType dt3 = DataType.of(String.class); // StringType
// Parse a value from string
Object val = DataTypes.IntType.valueOf("42"); // returns Integer 42