Implementation:Apache Paimon DataTypeVisitor
| Knowledge Sources | |
|---|---|
| Domains | Type System, Design Patterns |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
DataTypeVisitor defines the visitor pattern interface for traversing and transforming DataType instances into results of type R.
Description
DataTypeVisitor is a generic interface that establishes the contract for implementing the visitor pattern across Paimon's type system. The interface is parameterized with type R, representing the result type produced by visiting each data type. It declares visit methods for all 18 concrete data types in Paimon, including primitive types (CharType, VarCharType, BooleanType, numeric types, temporal types), specialized types (BlobType, VariantType), and complex types (ArrayType, VectorType, MultisetType, MapType, RowType).
The visitor pattern enables type-specific processing without cluttering the DataType classes themselves with operation-specific logic. Instead of adding methods to DataType for every operation (serialization, validation, conversion, etc.), implementations can create focused visitor classes that handle specific tasks. This approach maintains separation of concerns and makes the type system more extensible - new operations can be added by creating new visitors without modifying existing type classes.
Common uses include extracting type properties (like the PrecisionExtractor and LengthExtractor in DataTypeChecks), converting types between representations, validating type constraints, and generating code or documentation from type definitions. The interface has been stable since version 0.4.0 and is marked @Public, ensuring compatibility for custom visitor implementations.
Usage
Use DataTypeVisitor when implementing operations that need to handle each data type differently, such as type conversions, property extraction, validation rules, or code generation. Extend DataTypeDefaultVisitor for simpler cases where most types share common behavior.
Code Reference
Source Location
- Repository: Apache_Paimon
- File: paimon-api/src/main/java/org/apache/paimon/types/DataTypeVisitor.java
Signature
@Public
public interface DataTypeVisitor<R> {
R visit(CharType charType);
R visit(VarCharType varCharType);
R visit(BooleanType booleanType);
R visit(BinaryType binaryType);
R visit(VarBinaryType varBinaryType);
R visit(DecimalType decimalType);
R visit(TinyIntType tinyIntType);
R visit(SmallIntType smallIntType);
R visit(IntType intType);
R visit(BigIntType bigIntType);
R visit(FloatType floatType);
R visit(DoubleType doubleType);
R visit(DateType dateType);
R visit(TimeType timeType);
R visit(TimestampType timestampType);
R visit(LocalZonedTimestampType localZonedTimestampType);
R visit(VariantType variantType);
R visit(BlobType blobType);
R visit(ArrayType arrayType);
R visit(VectorType vectorType);
R visit(MultisetType multisetType);
R visit(MapType mapType);
R visit(RowType rowType);
}
Import
import org.apache.paimon.types.DataTypeVisitor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| Specific type | *Type (e.g., IntType) | Yes | The concrete data type being visited |
Outputs
| Name | Type | Description |
|---|---|---|
| Result | R (generic) | The result of visiting the type, determined by visitor implementation |
Usage Examples
// Example 1: Simple type name visitor
class TypeNameVisitor implements DataTypeVisitor<String> {
@Override
public String visit(IntType intType) {
return "Integer";
}
@Override
public String visit(VarCharType varCharType) {
return "String";
}
@Override
public String visit(ArrayType arrayType) {
return "Array of " + arrayType.getElementType().accept(this);
}
// ... implement all other visit methods
}
// Example 2: Size calculator
class TypeSizeVisitor implements DataTypeVisitor<Integer> {
@Override
public Integer visit(IntType intType) {
return 4;
}
@Override
public Integer visit(BigIntType bigIntType) {
return 8;
}
@Override
public Integer visit(VarCharType varCharType) {
return varCharType.getLength();
}
@Override
public Integer visit(ArrayType arrayType) {
// Return element size (simplified)
return arrayType.getElementType().accept(this);
}
// ... implement other methods
}
// Example 3: Nullability checker
class NullabilityVisitor implements DataTypeVisitor<Boolean> {
@Override
public Boolean visit(IntType intType) {
return intType.isNullable();
}
// Same implementation for all types
@Override
public Boolean visit(VarCharType varCharType) {
return varCharType.isNullable();
}
// ... etc
}
// Using the visitors
DataType intType = DataTypes.INT();
DataType arrayType = DataTypes.ARRAY(DataTypes.STRING());
TypeNameVisitor nameVisitor = new TypeNameVisitor();
String intName = intType.accept(nameVisitor); // "Integer"
String arrayName = arrayType.accept(nameVisitor); // "Array of String"
TypeSizeVisitor sizeVisitor = new TypeSizeVisitor();
int intSize = intType.accept(sizeVisitor); // 4
int arraySize = arrayType.accept(sizeVisitor);
// Using with DataTypeDefaultVisitor (simpler for uniform behavior)
class SimpleSizeVisitor extends DataTypeDefaultVisitor<Integer> {
@Override
protected Integer defaultMethod(DataType dataType) {
return dataType.defaultSize();
}
}
SimpleSizeVisitor simpleVisitor = new SimpleSizeVisitor();
int size = intType.accept(simpleVisitor);