Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Dec 11, 2024
1 parent 0423c37 commit 7e94baa
Showing 1 changed file with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,43 +306,59 @@ public static Pair<List<List<NamedExpression>>, List<Boolean>> castCommonDataTyp

private static Pair<List<DataType>, List<Boolean>> computeCommonDataTypeAndNullable(
List<List<NamedExpression>> constantExprsList, int columnCount) {
List<Boolean> nullables = Lists.newArrayList();
List<Boolean> nullables = Lists.newArrayListWithCapacity(columnCount);
List<DataType> commonDataTypes = Lists.newArrayListWithCapacity(columnCount);
List<NamedExpression> firstRow = constantExprsList.get(0);
for (int columnId = 0; columnId < columnCount; columnId++) {
DataType commonDataType = constantExprsList.get(0).get(columnId).getDataType();
boolean nullable = false;
for (int rowId = 1; rowId < constantExprsList.size(); rowId++) {
NamedExpression namedExpression = constantExprsList.get(rowId).get(columnId);
nullable |= namedExpression.nullable();
DataType otherDataType = namedExpression.getDataType();
commonDataType = getAssignmentCompatibleType(commonDataType, otherDataType);
}
nullables.add(nullable);
commonDataTypes.add(commonDataType);
Expression constant = firstRow.get(columnId).child(0);
Pair<DataType, Boolean> commonDataTypeAndNullable
= computeCommonDataTypeAndNullable(constant, columnId, constantExprsList);
commonDataTypes.add(commonDataTypeAndNullable.first);
nullables.add(commonDataTypeAndNullable.second);
}
return Pair.of(commonDataTypes, nullables);
}

private static Pair<DataType, Boolean> computeCommonDataTypeAndNullable(
Expression firstRowExpr, int columnId, List<List<NamedExpression>> constantExprsList) {
DataType commonDataType = firstRowExpr.getDataType();
boolean nullable = firstRowExpr.nullable();
for (int rowId = 1; rowId < constantExprsList.size(); rowId++) {
NamedExpression namedExpression = constantExprsList.get(rowId).get(columnId);
Expression otherConstant = namedExpression.child(0);
nullable |= otherConstant.nullable();
DataType otherDataType = otherConstant.getDataType();
commonDataType = getAssignmentCompatibleType(commonDataType, otherDataType);
}
return Pair.of(commonDataType, nullable);
}

private static List<List<NamedExpression>> castToCommonType(
List<List<NamedExpression>> rows, List<DataType> commonDataTypes, int columnCount) {
ImmutableList.Builder<List<NamedExpression>> castedConstants
= ImmutableList.builderWithExpectedSize(rows.size());
for (List<NamedExpression> row : rows) {
ImmutableList.Builder<NamedExpression> castedRow = ImmutableList.builderWithExpectedSize(columnCount);
for (int i = 0; i < row.size(); i++) {
NamedExpression constantAlias = row.get(i);
DataType commonType = commonDataTypes.get(i);
Expression constant = constantAlias.child(0);
if (commonType.equals(constant.getDataType())) {
castedRow.add(constantAlias);
} else {
Expression expression
= TypeCoercionUtils.castIfNotSameTypeStrict(constant, commonType);
castedRow.add((NamedExpression) constant.withChildren(expression));
}
}
castedConstants.add(castedRow.build());
castedConstants.add(castToCommonType(row, commonDataTypes));
}
return castedConstants.build();
}

private static List<NamedExpression> castToCommonType(List<NamedExpression> row, List<DataType> commonTypes) {
ImmutableList.Builder<NamedExpression> castedRow = ImmutableList.builderWithExpectedSize(row.size());
boolean changed = false;
for (int columnId = 0; columnId < row.size(); columnId++) {
NamedExpression constantAlias = row.get(columnId);
Expression constant = constantAlias.child(0);
DataType commonType = commonTypes.get(columnId);
if (commonType.equals(constant.getDataType())) {
castedRow.add(constantAlias);
} else {
changed = true;
Expression expression
= TypeCoercionUtils.castIfNotSameTypeStrict(constant, commonType);
castedRow.add((NamedExpression) constant.withChildren(expression));
}
}
return changed ? castedRow.build() : row;
}
}

0 comments on commit 7e94baa

Please sign in to comment.