Skip to content

Commit

Permalink
[CALCITE-4392] The operation of checking types equal ignoring null ca…
Browse files Browse the repository at this point in the history
…n be more efficient
  • Loading branch information
liyafan82 authored and chunweilei committed Nov 18, 2020
1 parent b973aa4 commit 2ddc836
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
20 changes: 7 additions & 13 deletions core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1155,14 +1155,8 @@ public static boolean equalSansNullability(
RelDataTypeFactory factory,
RelDataType type1,
RelDataType type2) {
if (type1.equals(type2)) {
return true;
}

if (type1.isNullable() == type2.isNullable()) {
// If types have the same nullability and they weren't equal above,
// they must be different.
return false;
return type1.equals(type2);
}
return type1.equals(
factory.createTypeWithNullability(type2, type1.isNullable()));
Expand Down Expand Up @@ -1214,9 +1208,9 @@ public static boolean equalAsCollectionSansNullability(
RelDataType type1,
RelDataType type2) {
Preconditions.checkArgument(isCollection(type1),
"Input type must be collection type");
"Input type1 must be collection type");
Preconditions.checkArgument(isCollection(type2),
"Input type must be collection type");
"Input type2 must be collection type");

return (type1 == type2)
|| (type1.getSqlTypeName() == type2.getSqlTypeName()
Expand All @@ -1237,8 +1231,8 @@ public static boolean equalAsMapSansNullability(
RelDataTypeFactory factory,
RelDataType type1,
RelDataType type2) {
Preconditions.checkArgument(isMap(type1), "Input type must be map type");
Preconditions.checkArgument(isMap(type2), "Input type must be map type");
Preconditions.checkArgument(isMap(type1), "Input type1 must be map type");
Preconditions.checkArgument(isMap(type2), "Input type2 must be map type");

MapSqlType mType1 = (MapSqlType) type1;
MapSqlType mType2 = (MapSqlType) type2;
Expand All @@ -1265,8 +1259,8 @@ public static boolean equalAsStructSansNullability(
RelDataType type1,
RelDataType type2,
SqlNameMatcher nameMatcher) {
Preconditions.checkArgument(type1.isStruct(), "Input type must be struct type");
Preconditions.checkArgument(type2.isStruct(), "Input type must be struct type");
Preconditions.checkArgument(type1.isStruct(), "Input type1 must be struct type");
Preconditions.checkArgument(type2.isStruct(), "Input type2 must be struct type");

if (type1 == type2) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,35 @@ private RelDataType struct(RelDataType...relDataTypes) {
}
return builder.build();
}

private void compareTypesIgnoringNullability(
String comment, RelDataType type1, RelDataType type2, boolean expectedResult) {
String typeString1 = type1.getFullTypeString();
String typeString2 = type2.getFullTypeString();

assertThat(
"The result of SqlTypeUtil.equalSansNullability"
+ "(typeFactory, " + typeString1 + ", " + typeString2 + ") is incorrect: " + comment,
SqlTypeUtil.equalSansNullability(f.typeFactory, type1, type2), is(expectedResult));
assertThat("The result of SqlTypeUtil.equalSansNullability"
+ "(" + typeString1 + ", " + typeString2 + ") is incorrect: " + comment,
SqlTypeUtil.equalSansNullability(type1, type2), is(expectedResult));
}

@Test public void testEqualSansNullability() {
RelDataType bigIntType = f.sqlBigInt;
RelDataType nullableBigIntType = f.sqlBigIntNullable;
RelDataType varCharType = f.sqlVarchar;
RelDataType bigIntType1 =
f.typeFactory.createTypeWithNullability(nullableBigIntType, false);

compareTypesIgnoringNullability("different types should return false. ",
bigIntType, varCharType, false);

compareTypesIgnoringNullability("types differing only in nullability should return true.",
bigIntType, nullableBigIntType, true);

compareTypesIgnoringNullability("identical types should return true.",
bigIntType, bigIntType1, true);
}
}

0 comments on commit 2ddc836

Please sign in to comment.