zabetak commented on code in PR #4403:
URL: https://github.com/apache/calcite/pull/4403#discussion_r2125965297
##########
core/src/main/java/org/apache/calcite/sql/fun/SqlInternalOperators.java:
##########
@@ -224,6 +224,40 @@ private SqlInternalOperators() {
public static final SqlAggFunction LITERAL_AGG =
SqlLiteralAggFunction.INSTANCE;
+ /** CAST NOT NULL operator used for cast expression to make it non-nullable
in SqlNode.
+ *
+ * <p>For example:
+ * <pre>{@code COALESCE(a,b)
+ * }</pre>
+ *
+ * <p>is converted to
+ *
+ * <pre>{@code CASE WHEN a is not null THEN a ELSE b
+ * }</pre>
+ * by {@code SqlCoalesceFunction#rewriteCall}.
+ *
+ * <p>When a is nullable and b is non-nullable, the {@code COALESCE(a,b)}
data type will be
+ * non-nullable and {@code CASE WHEN a is not null THEN a ELSE b} data type
will be nullable.
Review Comment:
If CALCITE-3675 is now redundant shouldn't we remove the respective code?
##########
core/src/main/java/org/apache/calcite/sql/fun/SqlInternalOperators.java:
##########
@@ -224,6 +224,40 @@ private SqlInternalOperators() {
public static final SqlAggFunction LITERAL_AGG =
SqlLiteralAggFunction.INSTANCE;
+ /** CAST NOT NULL operator used for cast expression to make it non-nullable
in SqlNode.
+ *
+ * <p>For example:
+ * <pre>{@code COALESCE(a,b)
+ * }</pre>
+ *
+ * <p>is converted to
+ *
+ * <pre>{@code CASE WHEN a is not null THEN a ELSE b
+ * }</pre>
+ * by {@code SqlCoalesceFunction#rewriteCall}.
+ *
+ * <p>When a is nullable and b is non-nullable, the {@code COALESCE(a,b)}
data type will be
+ * non-nullable and {@code CASE WHEN a is not null THEN a ELSE b} data type
will be nullable.
+ * The validator will throw an exception about failing to preserve the data
type.
+ *
+ * <p>Add CAST NOT NULL operator to {@code CASE WHEN a is not null THEN a
ELSE b},
+ * making it becomes
+ * <pre>{@code CASE WHEN a is not null THEN CAST NOT NULL(a) ELSE b}
+ * </pre>
+ *
+ * <p>Then the validator knows this data type is non-nullable.
+ * So we can keep the types consistent before and after conversion.
+ * */
+ public static final SqlOperator CAST_NOT_NULL =
+ new SqlInternalOperator("CAST NOT NULL", SqlKind.CAST_NOT_NULL, 2, true,
+ ReturnTypes.ARG0.andThen(SqlTypeTransforms.TO_NOT_NULLABLE), null,
+ OperandTypes.ANY) {
+ // This is an internal operator, which should not be unparsed to sql.
+ @Override public void unparse(SqlWriter writer, SqlCall call, int
leftPrec, int rightPrec) {
+ call.operand(0).unparse(writer, leftPrec, rightPrec);
+ }
Review Comment:
If we never expect to see this operator in SQL then I guess we don't need to
override the unparse method.
--
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]