You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "mbutrovich (via GitHub)" <gi...@apache.org> on 2025/04/30 14:54:30 UTC

[PR] Regexp replace [datafusion-comet]

mbutrovich opened a new pull request, #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   Preliminary support for Spark's `regexp_replace`. Spark optionally allows for an extra arg that indicates a starting offset which the native scalar function does not support. For now, we'll just only support `regexp_replace` without an offset.
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   Convert `RegexpReplace` Spark expression to native scalar function if the offset is 1.
   
   ## How are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   Added a fuzz test. Spark SQL tests.
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2070460938


##########
spark/src/test/scala/org/apache/comet/CometFuzzTestSuite.scala:
##########
@@ -188,6 +188,22 @@ class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("regexp_replace") {
+    withSQLConf(CometConf.COMET_REGEXP_ALLOW_INCOMPATIBLE.key -> "true") {
+      val df = spark.read.parquet(filename)
+      df.createOrReplaceTempView("t1")
+      // We want to make sure that the schema generator wasn't modified to accidentally omit
+      // StringType, since then this test would not run any queries and silently pass.
+      var testedString = false
+      for (field <- df.schema.fields if field.dataType == StringType) {
+        testedString = true
+        val sql = s"SELECT regexp_replace(${field.name}, 'a', 'b') FROM t1"

Review Comment:
   Makes sense. I find myself expanding the generator for #1697 to create dictionaries, so let me sync up with you for good solutions to expand the schemas and values in specific types.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2070420243


##########
spark/src/test/scala/org/apache/comet/CometFuzzTestSuite.scala:
##########
@@ -188,6 +188,22 @@ class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("regexp_replace") {
+    withSQLConf(CometConf.COMET_REGEXP_ALLOW_INCOMPATIBLE.key -> "true") {
+      val df = spark.read.parquet(filename)
+      df.createOrReplaceTempView("t1")
+      // We want to make sure that the schema generator wasn't modified to accidentally omit
+      // StringType, since then this test would not run any queries and silently pass.
+      var testedString = false
+      for (field <- df.schema.fields if field.dataType == StringType) {
+        testedString = true
+        val sql = s"SELECT regexp_replace(${field.name}, 'a', 'b') FROM t1"

Review Comment:
   Perhaps we should improve the logic in `ParquetGenerator` to also generate ASCII strings:
   
   ```scala
         case DataTypes.StringType =>
           Range(0, numRows).map(_ => {
             r.nextInt(10) match {
               case 0 if options.allowNull => null
               case 1 => r.nextInt().toByte.toString
               case 2 => r.nextLong().toString
               case 3 => r.nextDouble().toString
               case _ => r.nextString(8)
             }
           })
   ```
   
   The call to ` r.nextString(8)` generates strings containing characters in the range 0 through 0xD800, so while it could theoretically generate `a`, it will be rare.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2068995618


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -1430,6 +1430,27 @@ object QueryPlanSerde extends Logging with CometExprShim {
         val optExpr = scalarFunctionExprToProto("ascii", childExpr)
         optExprWithInfo(optExpr, expr, castExpr)
 
+      case RegExpReplace(subject, pattern, replacement, startPosition) =>

Review Comment:
   Could you also check that `COMET_REGEXP_ALLOW_INCOMPATIBLE` is enabled? There are differences in behavior between Java's and Rust's regex implementation.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#issuecomment-2842651369

   I should probably add some better tests, since now that we're checking the flag that means we won't get coverage in the Spark SQL tests -- we'll just fall back.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2071746681


##########
spark/src/test/scala/org/apache/comet/CometFuzzTestSuite.scala:
##########
@@ -188,6 +188,22 @@ class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("regexp_replace") {
+    withSQLConf(CometConf.COMET_REGEXP_ALLOW_INCOMPATIBLE.key -> "true") {
+      val df = spark.read.parquet(filename)
+      df.createOrReplaceTempView("t1")
+      // We want to make sure that the schema generator wasn't modified to accidentally omit
+      // StringType, since then this test would not run any queries and silently pass.
+      var testedString = false
+      for (field <- df.schema.fields if field.dataType == StringType) {
+        testedString = true
+        val sql = s"SELECT regexp_replace(${field.name}, 'a', 'b') FROM t1"

Review Comment:
   Pushed a commit to generate ASCII strings, ran your `COUNT(*)` query and confirmed I saw at least a dozen matching rows.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2070408978


##########
spark/src/test/scala/org/apache/comet/CometFuzzTestSuite.scala:
##########
@@ -188,6 +188,22 @@ class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("regexp_replace") {
+    withSQLConf(CometConf.COMET_REGEXP_ALLOW_INCOMPATIBLE.key -> "true") {
+      val df = spark.read.parquet(filename)
+      df.createOrReplaceTempView("t1")
+      // We want to make sure that the schema generator wasn't modified to accidentally omit
+      // StringType, since then this test would not run any queries and silently pass.
+      var testedString = false
+      for (field <- df.schema.fields if field.dataType == StringType) {
+        testedString = true
+        val sql = s"SELECT regexp_replace(${field.name}, 'a', 'b') FROM t1"

Review Comment:
   I'm not sure how likely it is that the input strings will contain the character `a`, so it is possible that this test isn't really testing anything. I modified the test locally to add:
   
   ```
           val sql = s"SELECT count(*) FROM t1 WHERE ${field.name} LIKE '%a%'"
           spark.sql(sql).show()
   ```
   
   this produced
   
   ```
   +--------+
   |count(1)|
   +--------+
   |       0|
   +--------+
   ```
   
   This is a general problem with just comparing results between Spark and Comet and it would be nice to have a way to also ensure that we see expected results.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2068997865


##########
spark/src/test/scala/org/apache/comet/CometFuzzTestSuite.scala:
##########
@@ -188,6 +188,20 @@ class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper {
     }
   }
 
+  test("regexp_replace") {
+    val df = spark.read.parquet(filename)
+    df.createOrReplaceTempView("t1")
+    // We want to make sure that the schema generator wasn't modified to accidentally omit
+    // StringType, since then this test would not run any queries and silently pass.
+    var tested_string = false

Review Comment:
   nit: could you use camel case here



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove merged PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on code in PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#discussion_r2068997181


##########
spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala:
##########
@@ -1430,6 +1430,27 @@ object QueryPlanSerde extends Logging with CometExprShim {
         val optExpr = scalarFunctionExprToProto("ascii", childExpr)
         optExprWithInfo(optExpr, expr, castExpr)
 
+      case RegExpReplace(subject, pattern, replacement, startPosition) =>

Review Comment:
   `RLike` has this behavior:
   
   ```scala
         case RLike(left, right) =>
           // we currently only support scalar regex patterns
           right match {
             case Literal(pattern, DataTypes.StringType) =>
               if (!RegExp.isSupportedPattern(pattern.toString) &&
                 !CometConf.COMET_REGEXP_ALLOW_INCOMPATIBLE.get()) {
                 withInfo(
                   expr,
                   s"Regexp pattern $pattern is not compatible with Spark. " +
                     s"Set ${CometConf.COMET_REGEXP_ALLOW_INCOMPATIBLE.key}=true " +
                     "to allow it anyway.")
                 return None
               }
   ```



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat: regexp_replace() expression with no starting offset [datafusion-comet]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #1700:
URL: https://github.com/apache/datafusion-comet/pull/1700#issuecomment-2842878963

   ## [Codecov](https://app.codecov.io/gh/apache/datafusion-comet/pull/1700?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   Attention: Patch coverage is `56.52174%` with `10 lines` in your changes missing coverage. Please review.
   > Project coverage is 57.33%. Comparing base [(`f09f8af`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/f09f8af64c6599255e116a376f4f008f2fd63b43?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) to head [(`12f5089`)](https://app.codecov.io/gh/apache/datafusion-comet/commit/12f5089b92c56c197bfc26149a8f0156a1195da5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   > Report is 174 commits behind head on main.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/datafusion-comet/pull/1700?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [.../scala/org/apache/comet/serde/QueryPlanSerde.scala](https://app.codecov.io/gh/apache/datafusion-comet/pull/1700?src=pr&el=tree&filepath=spark%2Fsrc%2Fmain%2Fscala%2Forg%2Fapache%2Fcomet%2Fserde%2FQueryPlanSerde.scala&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3Bhcmsvc3JjL21haW4vc2NhbGEvb3JnL2FwYWNoZS9jb21ldC9zZXJkZS9RdWVyeVBsYW5TZXJkZS5zY2FsYQ==) | 56.52% | [7 Missing and 3 partials :warning: ](https://app.codecov.io/gh/apache/datafusion-comet/pull/1700?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   ```diff
   @@             Coverage Diff              @@
   ##               main    #1700      +/-   ##
   ============================================
   + Coverage     56.12%   57.33%   +1.20%     
   - Complexity      976     1072      +96     
   ============================================
     Files           119      129      +10     
     Lines         11743    12506     +763     
     Branches       2251     2344      +93     
   ============================================
   + Hits           6591     7170     +579     
   - Misses         4012     4153     +141     
   - Partials       1140     1183      +43     
   ```
   
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/datafusion-comet/pull/1700?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   </details>


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org