Implementation:Apache Paimon DataTypeDefaultVisitor
| Knowledge Sources | |
|---|---|
| Domains | Type System, Design Patterns |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
DataTypeDefaultVisitor is an abstract implementation of DataTypeVisitor that redirects all visit calls to a single default method, simplifying visitor implementations.
Description
DataTypeDefaultVisitor is an abstract generic class that implements the DataTypeVisitor interface, providing a convenient base for creating type visitors that handle most types uniformly with a default behavior. Rather than requiring implementations to override all 18+ visit methods for each data type (CharType, VarCharType, BooleanType, etc.), this class redirects all of them to a single abstract defaultMethod(DataType), significantly reducing boilerplate code.
This design pattern is particularly useful when you need to process most types in the same way but only specialize handling for a few specific types. Subclasses can override specific visit methods for types requiring special treatment while letting the rest fall through to the default behavior. For example, a visitor extracting numeric properties might only override visit methods for DecimalType, TimeType, and TimestampType, while all other types return a default value.
The class has been available since Paimon version 0.4.0 and is marked with the @Public annotation, indicating its stability as part of the public API. It is parameterized with type R, which represents the return type of the visitor operations, allowing for flexible result types such as integers for size calculations, strings for formatting, or custom objects for complex transformations.
Usage
Use DataTypeDefaultVisitor when implementing custom type processing logic where most types share common behavior and only a subset requires specialized handling. It is ideal for implementing property extractors, type converters, or validation logic where exceptions to the rule are minimal.
Code Reference
Source Location
- Repository: Apache_Paimon
- File: paimon-api/src/main/java/org/apache/paimon/types/DataTypeDefaultVisitor.java
Signature
@Public
public abstract class DataTypeDefaultVisitor<R> implements DataTypeVisitor<R> {
@Override
public R visit(CharType charType)
@Override
public R visit(VarCharType varCharType)
@Override
public R visit(BooleanType booleanType)
@Override
public R visit(BinaryType binaryType)
@Override
public R visit(VarBinaryType varBinaryType)
@Override
public R visit(DecimalType decimalType)
@Override
public R visit(TinyIntType tinyIntType)
@Override
public R visit(SmallIntType smallIntType)
@Override
public R visit(IntType intType)
@Override
public R visit(BigIntType bigIntType)
@Override
public R visit(FloatType floatType)
@Override
public R visit(DoubleType doubleType)
@Override
public R visit(DateType dateType)
@Override
public R visit(TimeType timeType)
@Override
public R visit(TimestampType timestampType)
@Override
public R visit(LocalZonedTimestampType localZonedTimestampType)
@Override
public R visit(VariantType variantType)
@Override
public R visit(BlobType blobType)
@Override
public R visit(ArrayType arrayType)
@Override
public R visit(VectorType vectorType)
@Override
public R visit(MultisetType multisetType)
@Override
public R visit(MapType mapType)
@Override
public R visit(RowType rowType)
protected abstract R defaultMethod(DataType dataType)
}
Import
import org.apache.paimon.types.DataTypeDefaultVisitor;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| dataType | DataType | Yes | Any data type to be visited (passed to defaultMethod) |
Outputs
| Name | Type | Description |
|---|---|---|
| Result | R (generic) | The result of processing the data type, type determined by implementation |
Usage Examples
// Example 1: Return default size for all types except arrays
class SizeCalculator extends DataTypeDefaultVisitor<Integer> {
@Override
public Integer visit(ArrayType arrayType) {
// Special handling for arrays
return arrayType.getElementType().accept(this) * 10;
}
@Override
protected Integer defaultMethod(DataType dataType) {
return dataType.defaultSize();
}
}
// Example 2: Check if type is a temporal type
class TemporalChecker extends DataTypeDefaultVisitor<Boolean> {
@Override
public Boolean visit(DateType dateType) {
return true;
}
@Override
public Boolean visit(TimeType timeType) {
return true;
}
@Override
public Boolean visit(TimestampType timestampType) {
return true;
}
@Override
protected Boolean defaultMethod(DataType dataType) {
return false;
}
}
// Example 3: Extract precision, return empty if not applicable
class PrecisionExtractor extends DataTypeDefaultVisitor<OptionalInt> {
@Override
public OptionalInt visit(DecimalType decimalType) {
return OptionalInt.of(decimalType.getPrecision());
}
@Override
public OptionalInt visit(TimestampType timestampType) {
return OptionalInt.of(timestampType.getPrecision());
}
@Override
protected OptionalInt defaultMethod(DataType dataType) {
return OptionalInt.empty();
}
}
// Using the visitors
DataType intType = DataTypes.INT();
DataType arrayType = DataTypes.ARRAY(DataTypes.STRING());
DataType timestampType = DataTypes.TIMESTAMP(6);
SizeCalculator sizeCalc = new SizeCalculator();
int intSize = intType.accept(sizeCalc); // Returns 4
int arraySize = arrayType.accept(sizeCalc); // Returns special array handling
TemporalChecker tempChecker = new TemporalChecker();
boolean isIntTemporal = intType.accept(tempChecker); // false
boolean isTimestampTemporal = timestampType.accept(tempChecker); // true
PrecisionExtractor precExtractor = new PrecisionExtractor();
OptionalInt precision = timestampType.accept(precExtractor); // OptionalInt[6]