julianhyde commented on code in PR #3565:
URL: https://github.com/apache/calcite/pull/3565#discussion_r1416677533


##########
core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java:
##########
@@ -6557,6 +6557,34 @@ private void checkLiteral2(String expression, String 
expected) {
     sql(sql).withMysql().ok(expectedMysql);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6156";>[CALCITE-6156]
+   * Add ENDSWITH, STARTSWITH functions (enabled in Postgres, Snowflake 
libraries)</a>. */
+  @Test void testSnowflakeStartsWith() {
+    final String bigQuerySql =         "select starts_with(\"brand_name\", 
'a')\n"
+        + "from \"product\"";
+    final String snowflakeSql = "select startswith(\"brand_name\", 'a')\n"
+        + "from \"product\"";
+    final String expected = "SELECT STARTSWITH(\"brand_name\" 'a')\n"
+        + "FROM \"foodmart\".\"product\"";
+    
sql(bigQuerySql).withLibrary(SqlLibrary.BIG_QUERY).withSnowflake().ok(expected);
+    
sql(snowflakeSql).withLibrary(SqlLibrary.SNOWFLAKE).withSnowflake().ok(expected);

Review Comment:
   It's not very important to have SQL in more than one source dialect. But 
it's worth testing what SQL is generated in Postgres, BigQuery and Snowflake 
dialects.



##########
site/_docs/reference.md:
##########
@@ -2824,7 +2826,8 @@ BigQuery's type system uses confusingly different names 
for types and functions:
 | s | SOUNDEX(string)                                | Returns the phonetic 
representation of *string*; return original *string* if *string* is encoded 
with multi-byte encoding such as UTF-8
 | m | SPACE(integer)                                 | Returns a string of 
*integer* spaces; returns an empty string if *integer* is less than 1
 | b | SPLIT(string [, delimiter ])                   | Returns the string 
array of *string* split at *delimiter* (if omitted, default is comma).  If the 
*string* is empty it returns an empty array, otherwise, if the *delimiter* is 
empty, it returns an array containing the original *string*.
-| b | STARTS_WITH(string1, string2)                  | Returns whether 
*string2* is a prefix of *string1*
+| f | STARTSWITH(string1, string2)                   | Returns whether 
*string2* is a prefix of*string1*
+| b p | STARTS_WITH(string1, string2)                | Equivalent to 
`STARTSWITH(string1, string2)`

Review Comment:
   missing space after 'of'



##########
core/src/main/java/org/apache/calcite/sql/dialect/SnowflakeSqlDialect.java:
##########
@@ -36,6 +39,30 @@ public SnowflakeSqlDialect(Context context) {
     super(context);
   }
 
+  @Override public void unparseCall(final SqlWriter writer, final SqlCall 
call, final int leftPrec,
+      final int rightPrec) {
+    switch(call.getKind()) {
+    case ENDS_WITH:
+    case STARTS_WITH:
+      unparseEndsStartsWith(writer, call);
+      break;
+    default:
+      super.unparseCall(writer, call, leftPrec, rightPrec);
+    }
+  }
+
+       /** 
+        * Remove underscore for {@code STARTS_WITH} and {@code ENDS_WITH} 
operators to 
+        * comply with Snowflake syntax.
+        */
+  private static void unparseEndsStartsWith(final SqlWriter writer, final 
SqlCall call) {
+    final String name = call.getKind() == SqlKind.ENDS_WITH ? "ENDSWITH" : 
"STARTSWITH";

Review Comment:
   This works, but it's a bit janky.
   
   I would be better to map functions. If there is a call to `ENDS_WITH`, map 
it to a call to `ENDSWITH`. Then let the operator unparse itself.
   
   A principled approach will allow us to scale the many functions x many 
dialects.



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