Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix throw exception when compare with null #14074

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,28 @@ public void testFilterWithUDTF() {
fail(throwable.getMessage());
}
}

@Test
public void testCompareWithNull() {
tableResultSetEqualTest(
"select s1 from sg1 where s1 != null", new String[] {"s1"}, new String[] {}, DATABASE_NAME);
tableResultSetEqualTest(
"select s1 from sg1 where s1 <> null", new String[] {"s1"}, new String[] {}, DATABASE_NAME);
tableResultSetEqualTest(
"select s1 from sg1 where s1 = null", new String[] {"s1"}, new String[] {}, DATABASE_NAME);
}

@Test
public void testCalculateWithNull() {
tableResultSetEqualTest(
"select s1 + null from sg1",
new String[] {"_col0"},
new String[] {"null,", "null,"},
DATABASE_NAME);
tableResultSetEqualTest(
"select s2 - null from sg1",
new String[] {"_col0"},
new String[] {"null,", "null,"},
DATABASE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@

import static org.apache.iotdb.commons.conf.IoTDBConstant.PATH_ROOT;
import static org.apache.iotdb.commons.conf.IoTDBConstant.PATH_SEPARATOR;
import static org.apache.iotdb.db.queryengine.plan.relational.function.OperatorType.EQUAL;
import static org.apache.iotdb.db.queryengine.plan.relational.function.OperatorType.LESS_THAN;
import static org.apache.iotdb.db.queryengine.plan.relational.function.OperatorType.LESS_THAN_OR_EQUAL;
import static org.apache.tsfile.read.common.type.BinaryType.TEXT;
import static org.apache.tsfile.read.common.type.BooleanType.BOOLEAN;
import static org.apache.tsfile.read.common.type.DateType.DATE;
Expand All @@ -70,6 +73,7 @@
import static org.apache.tsfile.read.common.type.LongType.INT64;
import static org.apache.tsfile.read.common.type.StringType.STRING;
import static org.apache.tsfile.read.common.type.TimestampType.TIMESTAMP;
import static org.apache.tsfile.read.common.type.UnknownType.UNKNOWN;

public class TableMetadataImpl implements Metadata {

Expand Down Expand Up @@ -107,6 +111,12 @@ public Optional<TableSchema> getTableSchema(SessionInfo session, QualifiedObject
public Type getOperatorReturnType(OperatorType operatorType, List<? extends Type> argumentTypes)
throws OperatorNotFoundException {

if (isCompareWithNull(argumentTypes)) {
return BOOLEAN;
} else if (isCalculateWithNull(argumentTypes)) {
return argumentTypes.get(0);
}

switch (operatorType) {
case ADD:
if (!isTwoTypeCalculable(argumentTypes)
Expand Down Expand Up @@ -780,6 +790,10 @@ public static boolean isTimestampType(Type type) {
return TIMESTAMP.equals(type);
}

public static boolean isUnknownType(Type type) {
return UNKNOWN.equals(type);
}

public static boolean isIntegerNumber(Type type) {
return INT32.equals(type) || INT64.equals(type);
}
Expand All @@ -798,6 +812,15 @@ public static boolean isTwoTypeComparable(List<? extends Type> argumentTypes) {
return (isNumericType(left) && isNumericType(right)) || (isCharType(left) && isCharType(right));
}

public static boolean isCompareWithNull(List<? extends Type> argumentTypes) {
if (argumentTypes.size() != 2) {
return false;
}
Type left = argumentTypes.get(0);
Type right = argumentTypes.get(1);
return isNumericType(left) || isCharType(left) && isUnknownType(right);
}

public static boolean isArithmeticType(Type type) {
return INT32.equals(type)
|| INT64.equals(type)
Expand All @@ -815,4 +838,13 @@ public static boolean isTwoTypeCalculable(List<? extends Type> argumentTypes) {
Type right = argumentTypes.get(1);
return isArithmeticType(left) && isArithmeticType(right);
}

public static boolean isCalculateWithNull(List<? extends Type> argumentTypes) {
if (argumentTypes.size() != 2) {
return false;
}
Type left = argumentTypes.get(0);
Type right = argumentTypes.get(1);
return isArithmeticType(left) && isUnknownType(right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,39 @@ protected AbstractCastFunctionColumnTransformer(

@Override
protected void doTransform(Column column, ColumnBuilder columnBuilder) {
TypeEnum sourceType = childColumnTransformer.getType().getTypeEnum();
Type childType = childColumnTransformer.getType();
for (int i = 0, n = column.getPositionCount(); i < n; i++) {
if (!column.isNull(i)) {
transform(column, columnBuilder, sourceType, childType, i);
} else {
if (childType == null) {
for (int i = 0; i < column.getPositionCount(); i++) {
columnBuilder.appendNull();
}
} else {
TypeEnum sourceType = childType.getTypeEnum();
for (int i = 0, n = column.getPositionCount(); i < n; i++) {
if (!column.isNull(i)) {
transform(column, columnBuilder, sourceType, childType, i);
} else {
columnBuilder.appendNull();
}
}
}
}

@Override
protected void doTransform(Column column, ColumnBuilder columnBuilder, boolean[] selection) {
TypeEnum sourceType = childColumnTransformer.getType().getTypeEnum();
Type childType = childColumnTransformer.getType();
for (int i = 0, n = column.getPositionCount(); i < n; i++) {
if (selection[i] && !column.isNull(i)) {
transform(column, columnBuilder, sourceType, childType, i);
} else {
if (childType == null) {
for (int i = 0; i < column.getPositionCount(); i++) {
columnBuilder.appendNull();
}
} else {
TypeEnum sourceType = childType.getTypeEnum();
for (int i = 0, n = column.getPositionCount(); i < n; i++) {
if (selection[i] && !column.isNull(i)) {
transform(column, columnBuilder, sourceType, childType, i);
} else {
columnBuilder.appendNull();
}
}
}
}

Expand Down