flink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From twal...@apache.org
Subject flink git commit: [FLINK-4260] [table] Allow SQL's LIKE ESCAPE
Date Tue, 29 Nov 2016 15:01:51 GMT
Repository: flink
Updated Branches:
  refs/heads/master ecfb5b5f6 -> bcef8a3f8


[FLINK-4260] [table] Allow SQL's LIKE ESCAPE

This closes #2758.


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/bcef8a3f
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/bcef8a3f
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/bcef8a3f

Branch: refs/heads/master
Commit: bcef8a3f8db5b07e1c4f1f62731332cc1ea3aaa0
Parents: ecfb5b5
Author: miaoever <leo.miao.ever@gmail.com>
Authored: Fri Nov 4 23:31:00 2016 +0800
Committer: twalthr <twalthr@apache.org>
Committed: Tue Nov 29 15:57:31 2016 +0100

----------------------------------------------------------------------
 docs/dev/table_api.md                           | 16 ++--
 .../table/codegen/calls/BuiltInMethods.scala    |  4 +
 .../table/codegen/calls/ScalarFunctions.scala   | 12 +++
 .../table/expressions/ScalarFunctionsTest.scala | 85 +++++++++++++++++++-
 4 files changed, 107 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/bcef8a3f/docs/dev/table_api.md
----------------------------------------------------------------------
diff --git a/docs/dev/table_api.md b/docs/dev/table_api.md
index 4d8c953..848f9e4 100644
--- a/docs/dev/table_api.md
+++ b/docs/dev/table_api.md
@@ -2736,33 +2736,33 @@ value1 NOT BETWEEN value2 AND value3
     <tr>
       <td>
         {% highlight text %}
-string1 LIKE string2
+string1 LIKE string2 [ ESCAPE string3 ]
 {% endhighlight %}
       </td>
       <td>
-        <p>Whether <i>string1</i> matches pattern <i>string2</i>.</p>
+        <p>Whether <i>string1</i> matches pattern <i>string2</i>.
An escape character can be defined if necessary.</p>
       </td>
     </tr>
 
     <tr>
       <td>
         {% highlight text %}
-string1 NOT LIKE string2
+string1 NOT LIKE string2 [ ESCAPE string3 ]
 {% endhighlight %}
       </td>
       <td>
-        <p>Whether <i>string1</i> does not match pattern <i>string2</i>.</p>
+        <p>Whether <i>string1</i> does not match pattern <i>string2</i>.
An escape character can be defined if necessary.</p>
       </td>
     </tr>
 
     <tr>
       <td>
         {% highlight text %}
-string1 SIMILAR TO string2
+string1 SIMILAR TO string2 [ ESCAPE string3 ]
 {% endhighlight %}
       </td>
       <td>
-        <p>Whether <i>string1</i> matches regular expression <i>string2</i>.</p>
+        <p>Whether <i>string1</i> matches regular expression <i>string2</i>.
An escape character can be defined if necessary.</p>
       </td>
     </tr>
 
@@ -2770,11 +2770,11 @@ string1 SIMILAR TO string2
     <tr>
       <td>
         {% highlight text %}
-string1 NOT SIMILAR TO string2
+string1 NOT SIMILAR TO string2 [ ESCAPE string3 ]
 {% endhighlight %}
       </td>
       <td>
-        <p>Whether <i>string1</i> does not match regular expression <i>string2</i>.</p>
+        <p>Whether <i>string1</i> does not match regular expression <i>string2</i>.
An escape character can be defined if necessary.</p>
       </td>
     </tr>
 

http://git-wip-us.apache.org/repos/asf/flink/blob/bcef8a3f/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
index 6a6898d..c7c7477 100644
--- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
@@ -32,4 +32,8 @@ object BuiltInMethods {
   val LN = Types.lookupMethod(classOf[Math], "log", classOf[Double])
   val ABS = Types.lookupMethod(classOf[SqlFunctions], "abs", classOf[Double])
   val ABS_DEC = Types.lookupMethod(classOf[SqlFunctions], "abs", classOf[JBigDecimal])
+  val LIKE_WITH_ESCAPE = Types.lookupMethod(classOf[SqlFunctions], "like",
+    classOf[String], classOf[String], classOf[String])
+  val SIMILAR_WITH_ESCAPE = Types.lookupMethod(classOf[SqlFunctions], "similar",
+    classOf[String], classOf[String], classOf[String])
 }

http://git-wip-us.apache.org/repos/asf/flink/blob/bcef8a3f/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
index a39829c..e7c436a 100644
--- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
+++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
@@ -97,6 +97,12 @@ object ScalarFunctions {
     BOOLEAN_TYPE_INFO,
     BuiltInMethod.LIKE.method)
 
+  addSqlFunctionMethod(
+    LIKE,
+    Seq(STRING_TYPE_INFO, STRING_TYPE_INFO, STRING_TYPE_INFO),
+    BOOLEAN_TYPE_INFO,
+    BuiltInMethods.LIKE_WITH_ESCAPE)
+
   addSqlFunctionNotMethod(
     NOT_LIKE,
     Seq(STRING_TYPE_INFO, STRING_TYPE_INFO),
@@ -108,6 +114,12 @@ object ScalarFunctions {
     BOOLEAN_TYPE_INFO,
     BuiltInMethod.SIMILAR.method)
 
+  addSqlFunctionMethod(
+    SIMILAR_TO,
+    Seq(STRING_TYPE_INFO, STRING_TYPE_INFO, STRING_TYPE_INFO),
+    BOOLEAN_TYPE_INFO,
+    BuiltInMethods.SIMILAR_WITH_ESCAPE)
+
   addSqlFunctionNotMethod(
     NOT_SIMILAR_TO,
     Seq(STRING_TYPE_INFO, STRING_TYPE_INFO),

http://git-wip-us.apache.org/repos/asf/flink/blob/bcef8a3f/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
----------------------------------------------------------------------
diff --git a/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
b/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
index a6975b9..1d2a1b7 100644
--- a/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
+++ b/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
@@ -219,6 +219,44 @@ class ScalarFunctionsTest extends ExpressionTestBase {
   }
 
   @Test
+  def testLikeWithEscape(): Unit = {
+    testSqlApi(
+      "f23 LIKE '&%Th_s%' ESCAPE '&'",
+      "true")
+
+    testSqlApi(
+      "f23 LIKE '&%%is a%' ESCAPE '&'",
+      "true")
+
+    testSqlApi(
+      "f0 LIKE 'Th_s%' ESCAPE '&'",
+      "true")
+
+    testSqlApi(
+      "f0 LIKE '%is a%' ESCAPE '&'",
+      "true")
+  }
+
+  @Test
+  def testNotLikeWithEscape(): Unit = {
+    testSqlApi(
+      "f23 NOT LIKE '&%Th_s%' ESCAPE '&'",
+      "false")
+
+    testSqlApi(
+      "f23 NOT LIKE '&%%is a%' ESCAPE '&'",
+      "false")
+
+    testSqlApi(
+      "f0 NOT LIKE 'Th_s%' ESCAPE '&'",
+      "false")
+
+    testSqlApi(
+      "f0 NOT LIKE '%is a%' ESCAPE '&'",
+      "false")
+  }
+
+  @Test
   def testSimilar(): Unit = {
     testAllApis(
       'f0.similar("_*"),
@@ -248,6 +286,44 @@ class ScalarFunctionsTest extends ExpressionTestBase {
       "false")
   }
 
+  @Test
+  def testSimilarWithEscape(): Unit = {
+    testSqlApi(
+      "f24 SIMILAR TO '&*&__*' ESCAPE '&'",
+      "true")
+
+    testSqlApi(
+      "f0 SIMILAR TO '_*' ESCAPE '&'",
+      "true")
+
+    testSqlApi(
+      "f24 SIMILAR TO '&*&_This (is)? a (test)+ Strin_*' ESCAPE '&'",
+      "true")
+
+    testSqlApi(
+      "f0 SIMILAR TO 'This (is)? a (test)+ Strin_*' ESCAPE '&'",
+      "true")
+  }
+
+  @Test
+  def testNotSimilarWithEscape(): Unit = {
+    testSqlApi(
+      "f24 NOT SIMILAR TO '&*&__*' ESCAPE '&'",
+      "false")
+
+    testSqlApi(
+      "f0 NOT SIMILAR TO '_*' ESCAPE '&'",
+      "false")
+
+    testSqlApi(
+      "f24 NOT SIMILAR TO '&*&_This (is)? a (test)+ Strin_*' ESCAPE '&'",
+      "false")
+
+    testSqlApi(
+      "f0 NOT SIMILAR TO 'This (is)? a (test)+ Strin_*' ESCAPE '&'",
+      "false")
+  }
+
   // ----------------------------------------------------------------------------------------------
   // Math functions
   // ----------------------------------------------------------------------------------------------
@@ -1028,7 +1104,7 @@ class ScalarFunctionsTest extends ExpressionTestBase {
   // ----------------------------------------------------------------------------------------------
 
   def testData = {
-    val testData = new Row(23)
+    val testData = new Row(25)
     testData.setField(0, "This is a test String.")
     testData.setField(1, true)
     testData.setField(2, 42.toByte)
@@ -1052,6 +1128,8 @@ class ScalarFunctionsTest extends ExpressionTestBase {
     testData.setField(20, 25) // +2-01
     testData.setField(21, null)
     testData.setField(22, BigDecimal("2").bigDecimal)
+    testData.setField(23, "%This is a test String.")
+    testData.setField(24, "*_This is a test String.")
     testData
   }
 
@@ -1079,6 +1157,9 @@ class ScalarFunctionsTest extends ExpressionTestBase {
       Types.INTERVAL_MILLIS,
       Types.INTERVAL_MONTHS,
       Types.BOOLEAN,
-      Types.DECIMAL)).asInstanceOf[TypeInformation[Any]]
+      Types.DECIMAL,
+      Types.STRING,
+      Types.STRING)).asInstanceOf[TypeInformation[Any]]
+
   }
 }


Mime
View raw message