org.dinky.shaded.paimon.casting.CastExecutors Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dinky.shaded.paimon.casting;
import org.dinky.shaded.paimon.types.DataType;
import org.dinky.shaded.paimon.types.DataTypeFamily;
import org.dinky.shaded.paimon.types.DataTypeRoot;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
/** Cast executors for input type and output type. */
public class CastExecutors {
/* ------- Singleton declaration ------- */
private static final CastExecutors INSTANCE = new CastExecutors();
static {
INSTANCE
// Numeric rules
.addRule(DecimalToDecimalCastRule.INSTANCE)
.addRule(NumericPrimitiveToDecimalCastRule.INSTANCE)
.addRule(DecimalToNumericPrimitiveCastRule.INSTANCE)
.addRule(NumericPrimitiveCastRule.INSTANCE)
// Boolean <-> numeric rules
.addRule(BooleanToNumericCastRule.INSTANCE)
.addRule(NumericToBooleanCastRule.INSTANCE)
// To string rules
.addRule(NumericToStringCastRule.INSTANCE)
.addRule(BooleanToStringCastRule.INSTANCE)
.addRule(TimestampToStringCastRule.INSTANCE)
.addRule(TimeToStringCastRule.INSTANCE)
.addRule(DateToStringCastRule.INSTANCE)
.addRule(StringToStringCastRule.INSTANCE)
// From string rules
.addRule(StringToBooleanCastRule.INSTANCE)
.addRule(StringToDecimalCastRule.INSTANCE)
.addRule(StringToNumericPrimitiveCastRule.INSTANCE)
.addRule(StringToDateCastRule.INSTANCE)
.addRule(StringToTimeCastRule.INSTANCE)
.addRule(StringToTimestampCastRule.INSTANCE)
.addRule(StringToBinaryCastRule.INSTANCE)
// Date/Time/Timestamp rules
.addRule(TimestampToTimestampCastRule.INSTANCE)
.addRule(TimestampToDateCastRule.INSTANCE)
.addRule(TimestampToTimeCastRule.INSTANCE)
.addRule(DateToTimestampCastRule.INSTANCE)
.addRule(TimeToTimestampCastRule.INSTANCE)
// To binary rules
.addRule(BinaryToBinaryCastRule.INSTANCE);
}
/* ------- Entrypoint ------- */
private static final CastExecutor, ?> IDENTITY_CAST_EXECUTOR = value -> value;
/**
* Resolve a {@link CastExecutor} for the provided input type and target type. Returns null if
* no rule can be resolved.
*
* @param inputType the input value type.
* @param outputType the output value type.
* @return the {@link CastExecutor} instance.
*/
public static @Nullable CastExecutor, ?> resolve(DataType inputType, DataType outputType) {
CastRule, ?> rule = INSTANCE.internalResolve(inputType, outputType);
if (rule == null) {
return null;
}
return rule.create(inputType, outputType);
}
public static CastExecutor, ?> identityCastExecutor() {
return IDENTITY_CAST_EXECUTOR;
}
// Map>
private final Map