Skip to content

Commit

Permalink
fix: Use Optional to make a judgment about possible nulls.
Browse files Browse the repository at this point in the history
  • Loading branch information
FearfulTomcat27 committed Sep 4, 2024
1 parent cfa7179 commit b131eb8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,34 +88,34 @@ public class ArithmeticColumnTransformerApi {
switch (rightTransformer.getType().getTypeEnum()) {
case INT32:
return new ${firstType}${operator.name}IntColumnTransformer(
${operator.name}Resolver.checkConditions(argumentTypes),
${operator.name}Resolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer);
case INT64:
return new ${firstType}${operator.name}LongColumnTransformer(
${operator.name}Resolver.checkConditions(argumentTypes),
${operator.name}Resolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer);
case FLOAT:
return new ${firstType}${operator.name}FloatColumnTransformer(
${operator.name}Resolver.checkConditions(argumentTypes),
${operator.name}Resolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer);
case DOUBLE:
return new ${firstType}${operator.name}DoubleColumnTransformer(
${operator.name}Resolver.checkConditions(argumentTypes),
${operator.name}Resolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer);
<#if operator.name == "Addition" && (firstType == "Int" || firstType == "Long")>
case DATE:
return new ${firstType}${operator.name}DateColumnTransformer(
${operator.name}Resolver.checkConditions(argumentTypes),
${operator.name}Resolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
case TIMESTAMP:
return new ${firstType}${operator.name}TimestampColumnTransformer(
${operator.name}Resolver.checkConditions(argumentTypes),
${operator.name}Resolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
Expand All @@ -135,13 +135,13 @@ public class ArithmeticColumnTransformerApi {
switch (rightTransformer.getType().getTypeEnum()) {
case INT32:
return new DateAdditionIntColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
case INT64:
return new DateAdditionLongColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
Expand All @@ -157,13 +157,13 @@ public class ArithmeticColumnTransformerApi {
switch (rightTransformer.getType().getTypeEnum()) {
case INT32:
return new TimestampAdditionIntColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
case INT64:
return new TimestampAdditionLongColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
Expand All @@ -179,13 +179,13 @@ public class ArithmeticColumnTransformerApi {
switch (rightTransformer.getType().getTypeEnum()) {
case INT32:
return new DateSubtractionIntColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
case INT64:
return new DateSubtractionLongColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
Expand All @@ -201,13 +201,13 @@ public class ArithmeticColumnTransformerApi {
switch (rightTransformer.getType().getTypeEnum()) {
case INT32:
return new TimestampSubtractionIntColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
case INT64:
return new TimestampSubtractionLongColumnTransformer(
AdditionResolver.checkConditions(argumentTypes),
AdditionResolver.checkConditions(argumentTypes).get(),
leftTransformer,
rightTransformer,
zoneId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import org.apache.tsfile.read.common.type.Type;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.tsfile.read.common.type.DateType.DATE;
import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
Expand Down Expand Up @@ -68,8 +70,8 @@ private static void addCondition(Type condition1, Type condition2, Type result)
}

public static Optional<Type> checkConditions(List<? extends Type> argumentTypes) {
return CONDITION_MAP
.getOrDefault(argumentTypes.get(0), Collections.empty_map())
.getOrDefault(argumentTypes.get(1), null);
return Optional.ofNullable(CONDITION_MAP
.getOrDefault(argumentTypes.get(0), Collections.emptyMap())
.getOrDefault(argumentTypes.get(1), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import org.apache.tsfile.read.common.type.Type;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
import static org.apache.tsfile.read.common.type.FloatType.FLOAT;
Expand Down Expand Up @@ -55,9 +57,9 @@ private static void addCondition(Type condition1, Type condition2, Type result)
CONDITION_MAP.computeIfAbsent(condition1, k -> new HashMap<>()).put(condition2, result);
}

public static Type checkConditions(List<? extends Type> argumentTypes) {
return CONDITION_MAP
.getOrDefault(argumentTypes.get(0), new HashMap<>())
.getOrDefault(argumentTypes.get(1), null);
public static Optional<Type> checkConditions(List<? extends Type> argumentTypes) {
return Optional.ofNullable(CONDITION_MAP
.getOrDefault(argumentTypes.get(0), Collections.emptyMap())
.getOrDefault(argumentTypes.get(1), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import org.apache.tsfile.read.common.type.Type;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
import static org.apache.tsfile.read.common.type.FloatType.FLOAT;
Expand Down Expand Up @@ -55,9 +57,10 @@ private static void addCondition(Type condition1, Type condition2, Type result)
CONDITION_MAP.computeIfAbsent(condition1, k -> new HashMap<>()).put(condition2, result);
}

public static Type checkConditions(List<? extends Type> argumentTypes) {
return CONDITION_MAP
.getOrDefault(argumentTypes.get(0), new HashMap<>())
.getOrDefault(argumentTypes.get(1), null);
public static Optional<Type> checkConditions(List<? extends Type> argumentTypes) {
return Optional.ofNullable(
CONDITION_MAP
.getOrDefault(argumentTypes.get(0), Collections.emptyMap())
.getOrDefault(argumentTypes.get(1), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import org.apache.tsfile.read.common.type.Type;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
import static org.apache.tsfile.read.common.type.FloatType.FLOAT;
Expand Down Expand Up @@ -55,9 +57,9 @@ private static void addCondition(Type condition1, Type condition2, Type result)
CONDITION_MAP.computeIfAbsent(condition1, k -> new HashMap<>()).put(condition2, result);
}

public static Type checkConditions(List<? extends Type> argumentTypes) {
return CONDITION_MAP
.getOrDefault(argumentTypes.get(0), new HashMap<>())
.getOrDefault(argumentTypes.get(1), null);
public static Optional<Type> checkConditions(List<? extends Type> argumentTypes) {
return Optional.ofNullable(CONDITION_MAP
.getOrDefault(argumentTypes.get(0), Collections.emptyMap())
.getOrDefault(argumentTypes.get(1), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import org.apache.tsfile.read.common.type.Type;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.apache.tsfile.read.common.type.DateType.DATE;
import static org.apache.tsfile.read.common.type.DoubleType.DOUBLE;
Expand Down Expand Up @@ -63,9 +65,9 @@ private static void addCondition(Type condition1, Type condition2, Type result)
CONDITION_MAP.computeIfAbsent(condition1, k -> new HashMap<>()).put(condition2, result);
}

public static Type checkConditions(List<? extends Type> argumentTypes) {
return CONDITION_MAP
.getOrDefault(argumentTypes.get(0), new HashMap<>())
.getOrDefault(argumentTypes.get(1), null);
public static Optional<Type> checkConditions(List<? extends Type> argumentTypes) {
return Optional.ofNullable(CONDITION_MAP
.getOrDefault(argumentTypes.get(0), Collections.emptyMap())
.getOrDefault(argumentTypes.get(1), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,49 +111,49 @@ public Type getOperatorReturnType(OperatorType operatorType, List<? extends Type
switch (operatorType) {
case ADD:
if (!isTwoTypeCalculable(argumentTypes)
|| !AdditionResolver.checkConditions(argumentTypes).isPresent()) {
|| AdditionResolver.checkConditions(argumentTypes).isPresent()) {
throw new OperatorNotFoundException(
operatorType,
argumentTypes,
new IllegalArgumentException("Should have two numeric operands."));
}
return AdditionResolver.checkConditions(argumentTypes);
return AdditionResolver.checkConditions(argumentTypes).get();
case SUBTRACT:
if (!isTwoTypeCalculable(argumentTypes)
|| SubtractionResolver.checkConditions(argumentTypes) == null) {
|| !SubtractionResolver.checkConditions(argumentTypes).isPresent()) {
throw new OperatorNotFoundException(
operatorType,
argumentTypes,
new IllegalArgumentException("Should have two numeric operands."));
}
return SubtractionResolver.checkConditions(argumentTypes);
return SubtractionResolver.checkConditions(argumentTypes).get();
case MULTIPLY:
if (!isTwoTypeCalculable(argumentTypes)
|| MultiplicationResolver.checkConditions(argumentTypes) == null) {
|| !MultiplicationResolver.checkConditions(argumentTypes).isPresent()) {
throw new OperatorNotFoundException(
operatorType,
argumentTypes,
new IllegalArgumentException("Should have two numeric operands."));
}
return MultiplicationResolver.checkConditions(argumentTypes);
return MultiplicationResolver.checkConditions(argumentTypes).get();
case DIVIDE:
if (!isTwoTypeCalculable(argumentTypes)
|| DivisionResolver.checkConditions(argumentTypes) == null) {
|| !DivisionResolver.checkConditions(argumentTypes).isPresent()) {
throw new OperatorNotFoundException(
operatorType,
argumentTypes,
new IllegalArgumentException("Should have two numeric operands."));
}
return DivisionResolver.checkConditions(argumentTypes);
return DivisionResolver.checkConditions(argumentTypes).get();
case MODULUS:
if (!isTwoTypeCalculable(argumentTypes)
|| ModulusResolver.checkConditions(argumentTypes) == null) {
|| !ModulusResolver.checkConditions(argumentTypes).isPresent()) {
throw new OperatorNotFoundException(
operatorType,
argumentTypes,
new IllegalArgumentException("Should have two numeric operands."));
}
return ModulusResolver.checkConditions(argumentTypes);
return ModulusResolver.checkConditions(argumentTypes).get();
case NEGATION:
if (!isOneNumericType(argumentTypes) && !isTimestampType(argumentTypes.get(0))) {
throw new OperatorNotFoundException(
Expand Down

0 comments on commit b131eb8

Please sign in to comment.