tanclary commented on code in PR #3258:
URL: https://github.com/apache/calcite/pull/3258#discussion_r1231279122


##########
core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java:
##########
@@ -792,6 +792,16 @@ public static String concatMulti(String... args) {
     return String.join("", args);
   }
 
+  /** SQL {@code CONCAT(arg0, ...)} function which can accept null

Review Comment:
   sorry what is emprt?



##########
core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java:
##########
@@ -149,6 +151,24 @@ static <E> List<E> list() {
     assertThat(concatWithNull(null, "b"), is("b"));
   }
 
+  @Test void testConcatMulti() {
+    assertThat(concatMulti("a b", "cd", "e"), is("a bcde"));
+    // The code generator will ensure that nulls are never passed in. If we
+    // pass in null, it is treated like the string "null", as the following
+    // tests show. Not the desired behavior for SQL.
+    assertThat(concatMulti((String) null), is("null"));
+    assertThat(concatMulti((String) null, null), is("nullnull"));
+    assertThat(concatMulti("a", null, "b"), is("anullb"));
+  }
+
+  @Test void testConcatMultiWithNull() {
+    assertThat(concatMultiWithNull("a b", "cd", "e"), is("a bcde"));
+    // Null value could be passed in which is treated as empty string.
+    assertThat(concatMultiWithNull((String) null), is(""));

Review Comment:
   Just to clarify, which of these tests mimics the behavior that BigQuery 
follows? My understanding is that BigQuery does the following:
   
   `CONCAT(null, null)` returns `NULL` while
   `CONCAT("", "")` returns `""`



##########
core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java:
##########
@@ -831,16 +831,43 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding 
operatorBinding,
       new SqlLikeOperator("NOT RLIKE", SqlKind.RLIKE, true, true);
 
   /** The "CONCAT(arg, ...)" function that concatenates strings.
-   * For example, "CONCAT('a', 'bc', 'd')" returns "abcd". */
-  @LibraryOperator(libraries = {MYSQL, POSTGRESQL, BIG_QUERY})
+   * For example, "CONCAT('a', 'bc', 'd')" returns "abcd".
+   *
+   * <p>It accepts at least 1 argument and returns null if any of
+   * the arguments is null. */
+  @LibraryOperator(libraries = {MYSQL, BIG_QUERY})
   public static final SqlFunction CONCAT_FUNCTION =
       SqlBasicFunction.create("CONCAT",
           ReturnTypes.MULTIVALENT_STRING_SUM_PRECISION_NULLABLE,
-          OperandTypes.repeat(SqlOperandCountRanges.from(2),
+          OperandTypes.repeat(SqlOperandCountRanges.from(1),
               OperandTypes.STRING),
           SqlFunctionCategory.STRING)
           .withOperandTypeInference(InferTypes.RETURN_TYPE);
 
+  /** The "CONCAT(arg, ...)" function that concatenates strings,
+   * which never returns null.
+   * For example, "CONCAT('a', 'bc', 'd')" returns "abcd".
+   *
+   * <p>If one of the arguments is null, it will be treated as empty string.
+   * "CONCAT('a', null)" returns "a".
+   * "CONCAT('a', null, 'b')" returns "ab".
+   *
+   * <p>Returns empty string only when all arguments are null.
+   * "CONCAT(null)" returns "".
+   * "CONCAT(null, null, null)" returns "".
+   *
+   * <p>It differs from {@link #CONCAT_FUNCTION} when processing
+   * null values. */
+  @LibraryOperator(libraries = {MSSQL, POSTGRESQL})
+  public static final SqlFunction CONCAT_FUNCTION_WITH_NULL =
+      SqlBasicFunction.create("CONCAT",
+          ReturnTypes.MULTIVALENT_STRING_SUM_PRECISION_NOT_NULLABLE,
+          OperandTypes.repeat(SqlOperandCountRanges.from(1),
+              OperandTypes.STRING),
+          SqlFunctionCategory.STRING)
+          .withOperandTypeInference(InferTypes.RETURN_TYPE)
+          .withKind(SqlKind.CONCAT_WITH_NULL);

Review Comment:
   is there a SqlKind.CONCAT? What is the advantage of having multiply SqlKind 
entries?



-- 
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]

Reply via email to