mihaibudiu commented on code in PR #4897:
URL: https://github.com/apache/calcite/pull/4897#discussion_r3358905160
##########
core/src/main/java/org/apache/calcite/rel/core/Intersect.java:
##########
@@ -79,4 +82,12 @@ protected Intersect(RelInput input) {
dRows *= 0.25;
return dRows;
}
+
+ @Override protected RelDataType deriveRowType() {
Review Comment:
why "refine" not simply "derive"?
##########
core/src/main/java/org/apache/calcite/sql/type/ReturnTypes.java:
##########
@@ -608,6 +608,81 @@ public static SqlCall stripSeparator(SqlCall call) {
.leastRestrictive(opBinding.collectOperandTypes());
}
+ /**
+ * Refines the nullability of {@code base} using INTERSECT semantics: a
+ * column is NOT NULL if it is NOT NULL in at least one of the
+ * {@code inputTypes} (AND-semantics across inputs).
+ */
+ public static RelDataType refineNullabilityForIntersect(
+ RelDataTypeFactory typeFactory,
+ RelDataType base,
+ List<RelDataType> inputTypes) {
+ final RelDataTypeFactory.Builder builder =
+ new RelDataTypeFactory.Builder(typeFactory);
+ final List<RelDataTypeField> outputFields = base.getFieldList();
+ for (int i = 0; i < outputFields.size(); i++) {
+ boolean nullable = true;
+ for (RelDataType inputType : inputTypes) {
+ nullable &= inputType.getFieldList().get(i).getType().isNullable();
+ }
+ builder.add(outputFields.get(i)).nullable(nullable);
+ }
+ return builder.build();
+ }
+
+ /**
+ * Type-inference strategy for INTERSECT. Computes the least restrictive row
+ * type across all inputs, then refines nullability: a column is NOT NULL if
+ * it is NOT NULL in at least one input (AND semantics across inputs).
Review Comment:
AND should be quoted `"AND"` here and in other comments
##########
core/src/test/java/org/apache/calcite/test/RelBuilderTest.java:
##########
@@ -2577,6 +2707,55 @@ private static RelNode groupIdRel(RelBuilder builder,
boolean extra) {
assertThat(root, hasTree(expected));
}
+ @ParameterizedTest
Review Comment:
how about a test with a ROW field type?
the behavior of nulls with ROW types in Calcite is very strange.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]