This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit 79b97b628ac43e56873029b72a18e7bec0d1e7db
Author: Ryan Fu <[email protected]>
AuthorDate: Tue Aug 13 15:30:00 2019 -0700

    [CALCITE-3235] Add CONCAT function for Redshift (Ryan Fu)
    
    Add '||' to operator precedence list in doc.
    
    Close apache/calcite#1376
---
 .../sql/fun/SqlLibraryOperatorTableFactory.java    |  2 +-
 .../calcite/sql/fun/SqlLibraryOperators.java       | 20 ++++++++++++++
 .../calcite/materialize/LatticeSuggesterTest.java  | 31 ++++++++++++++++++++++
 .../org/apache/calcite/test/SqlValidatorTest.java  | 22 +++++++++++++++
 site/_docs/reference.md                            |  3 ++-
 5 files changed, 76 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
index 4a65685..56c3ba6 100644
--- 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
+++ 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
@@ -41,7 +41,7 @@ import java.util.concurrent.ExecutionException;
  *
  * <blockquote>
  *   <pre>SqlOperatorTable opTab =
- *     SqlDialectOperatorTableFactory.INSTANCE.getOperatorTable(
+ *     SqlLibraryOperatorTableFactory.INSTANCE.getOperatorTable(
  *         EnumSet.of(SqlLibrary.ORACLE, SqlLibrary.MYSQL))</pre>
  * </blockquote>
  *
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index f63bc51..6acadec 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -25,6 +25,7 @@ import org.apache.calcite.sql.SqlOperator;
 import org.apache.calcite.sql.SqlOperatorTable;
 import org.apache.calcite.sql.type.OperandTypes;
 import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
 import org.apache.calcite.sql.type.SqlReturnTypeInference;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.sql.type.SqlTypeTransforms;
@@ -220,6 +221,25 @@ public abstract class SqlLibraryOperators {
           OperandTypes.STRING_STRING,
           SqlFunctionCategory.STRING);
 
+  /** The "CONCAT(arg, ...)" function that concatenates strings.
+   * For example, "CONCACT('a', 'bc', 'd')" returns "abcd". */
+  @LibraryOperator(libraries = {MYSQL, POSTGRESQL, ORACLE})
+  public static final SqlFunction CONCAT_FUNCTION =
+      new SqlFunction("CONCAT",
+          SqlKind.OTHER_FUNCTION,
+          ReturnTypes.cascade(
+              opBinding -> {
+                int precision = opBinding.collectOperandTypes().stream()
+                    .mapToInt(RelDataType::getPrecision).sum();
+                return opBinding.getTypeFactory()
+                    .createSqlType(SqlTypeName.VARCHAR, precision);
+              },
+              SqlTypeTransforms.TO_NULLABLE),
+          null,
+          OperandTypes.repeat(SqlOperandCountRanges.from(2),
+              OperandTypes.STRING),
+          SqlFunctionCategory.STRING);
+
   @LibraryOperator(libraries = {MYSQL})
   public static final SqlFunction REVERSE =
       new SqlFunction("REVERSE",
diff --git 
a/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java 
b/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java
index fb59c1c..d996965 100644
--- 
a/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java
@@ -19,7 +19,11 @@ package org.apache.calcite.materialize;
 import org.apache.calcite.prepare.PlannerImpl;
 import org.apache.calcite.rel.RelRoot;
 import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.sql.SqlDialect;
 import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlOperatorTable;
+import org.apache.calcite.sql.fun.SqlLibrary;
+import org.apache.calcite.sql.fun.SqlLibraryOperatorTableFactory;
 import org.apache.calcite.sql.parser.SqlParseException;
 import org.apache.calcite.sql.parser.SqlParser;
 import org.apache.calcite.statistic.MapSqlStatisticProvider;
@@ -45,6 +49,7 @@ import org.junit.experimental.categories.Category;
 
 import java.util.Arrays;
 import java.util.Comparator;
+import java.util.EnumSet;
 import java.util.List;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -589,6 +594,22 @@ public class LatticeSuggesterTest {
     assertThat(derivedColumns.get(1).tables, is(tables));
   }
 
+  @Test public void testDialect() throws Exception {
+    final Tester t = new Tester().foodmart().withEvolve(true)
+        .withDialect(SqlDialect.DatabaseProduct.REDSHIFT.getDialect())
+        .withLibrary(SqlLibrary.POSTGRESQL);
+
+    final String q0 = "select\n"
+        + "  left(\"fname\", 1) as \"initial\",\n"
+        + "  CONCAT(\"fname\", ' ', \"lname\") as \"full_name\",\n"
+        + "  count(*) as c,\n"
+        + "  avg(\"total_children\" - \"num_children_at_home\")\n"
+        + "from \"customer\" join \"sales_fact_1997\" using 
(\"customer_id\")\n"
+        + "group by \"fname\", \"lname\"";
+    t.addQuery(q0);
+    assertThat(t.s.latticeMap.size(), is(1));
+  }
+
   /** Creates a matcher that matches query graphs to strings. */
   private BaseMatcher<List<Lattice>> isGraphs(
       String... strings) {
@@ -693,6 +714,16 @@ public class LatticeSuggesterTest {
                   .build())
           .build());
     }
+
+    Tester withDialect(SqlDialect dialect) {
+      return withParser(dialect::configureParser);
+    }
+
+    Tester withLibrary(SqlLibrary library) {
+      SqlOperatorTable opTab = SqlLibraryOperatorTableFactory.INSTANCE
+          .getOperatorTable(EnumSet.of(SqlLibrary.STANDARD, library));
+      return withConfig(builder().operatorTable(opTab).build());
+    }
   }
 }
 
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 3dd59d6..54e4589 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -655,6 +655,28 @@ public class SqlValidatorTest extends SqlValidatorTestCase 
{
             + ".*Supported form.s.: '<STRING> \\|\\| <STRING>.*'");
   }
 
+  /** Tests the CONCAT function, which unlike the concat operator ('||') is not
+   * standard but only in the ORACLE and POSTGRESQL libraries. */
+  @Test public void testConcatFunction() {
+    // CONCAT is not in the library operator table
+    tester = tester.withOperatorTable(
+        SqlLibraryOperatorTableFactory.INSTANCE
+            .getOperatorTable(SqlLibrary.STANDARD, SqlLibrary.POSTGRESQL));
+    checkExp("concat('a', 'b')");
+    checkExp("concat(x'12', x'34')");
+    checkExp("concat(_UTF16'a', _UTF16'b', _UTF16'c')");
+    checkExpType("concat('aabbcc', 'ab', '+-')",
+        "VARCHAR(10) NOT NULL");
+    checkExpType("concat('aabbcc', CAST(NULL AS VARCHAR(20)), '+-')",
+        "VARCHAR(28)");
+    checkWholeExpFails("concat('aabbcc', 2)",
+        "(?s)Cannot apply 'CONCAT' to arguments of type 
'CONCAT\\(<CHAR\\(6\\)>, <INTEGER>\\)'\\. .*");
+    checkWholeExpFails("concat('abc', 'ab', 123)",
+        "(?s)Cannot apply 'CONCAT' to arguments of type 
'CONCAT\\(<CHAR\\(3\\)>, <CHAR\\(2\\)>, <INTEGER>\\)'\\. .*");
+    checkWholeExpFails("concat(true, false)",
+        "(?s)Cannot apply 'CONCAT' to arguments of type 'CONCAT\\(<BOOLEAN>, 
<BOOLEAN>\\)'\\. .*");
+  }
+
   @Test public void testBetween() {
     checkExp("1 between 2 and 3");
     checkExp("'a' between 'b' and 'c'");
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index 6cf0c2b..327dac4 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -1116,7 +1116,7 @@ The operator precedence and associativity, highest to 
lowest.
 | ::                                                | left
 | [ ] (array element)                               | left
 | + - (unary plus, minus)                           | right
-| * / %                                             | left
+| * / % &#124;&#124;                                | left
 | + -                                               | left
 | BETWEEN, IN, LIKE, SIMILAR, OVERLAPS, CONTAINS etc. | -
 | < > = <= >= <> !=                                 | left
@@ -2172,6 +2172,7 @@ semantics.
 |:- |:-----------------------------------------------|:-----------
 | p | expr :: type                                   | Casts *expr* to *type*
 | o | CHR(integer) | Returns the character having the binary equivalent to 
*integer* as a CHAR value
+| m o p | CONCAT(string [, string ]*)                | Concatenates two or 
more strings
 | o | DECODE(value, value1, result1 [, valueN, resultN ]* [, default ]) | 
Compares *value* to each *valueN* value one by one; if *value* is equal to a 
*valueN*, returns the corresponding *resultN*, else returns *default*, or NULL 
if *default* is not specified
 | p | DIFFERENCE(string, string)                     | Returns a measure of 
the similarity of two strings, namely the number of character positions that 
their `SOUNDEX` values have in common: 4 if the `SOUNDEX` values are same and 0 
if the `SOUNDEX` values are totally different
 | o | GREATEST(expr [, expr ]*)                      | Returns the greatest of 
the expressions

Reply via email to