LuciferYang commented on code in PR #37113:
URL: https://github.com/apache/spark/pull/37113#discussion_r918907788


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/urlExpressions.scala:
##########
@@ -0,0 +1,290 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.expressions
+
+import java.net.{URI, URISyntaxException, URLDecoder, URLEncoder}
+import java.util.regex.Pattern
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
+import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
+import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke
+import org.apache.spark.sql.catalyst.trees.UnaryLike
+import org.apache.spark.sql.errors.QueryExecutionErrors
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{AbstractDataType, DataType, StringType}
+import org.apache.spark.unsafe.types.UTF8String
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = """
+    _FUNC_(str) - Translates a string into 'application/x-www-form-urlencoded' 
format using a specific encoding scheme.
+  """,
+  arguments = """
+    Arguments:
+      str - a string expression to be translated
+  """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_('https://spark.apache.org');
+       https%3A%2F%2Fspark.apache.org
+  """,
+  since = "3.4.0",
+  group = "url_funcs")
+// scalastyle:on line.size.limit
+case class UrlEncode(child: Expression)
+  extends RuntimeReplaceable with UnaryLike[Expression] with 
ImplicitCastInputTypes {
+
+  override def replacement: Expression =
+    StaticInvoke(
+      UrlCodec.getClass,
+      StringType,
+      "encode",
+      Seq(child, Literal("UTF-8")),
+      Seq(StringType))
+
+  override protected def withNewChildInternal(newChild: Expression): 
Expression = {
+    copy(child = newChild)
+  }
+
+  override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
+
+  override def prettyName: String = "url_encode"
+}
+
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = """
+    _FUNC_(str) - Decodes a `str` in 'application/x-www-form-urlencoded' 
format using a specific encoding scheme.
+  """,
+  arguments = """
+    Arguments:
+      * str - a string expression to decode
+  """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_('https%3A%2F%2Fspark.apache.org');
+       https://spark.apache.org
+  """,
+  since = "3.4.0",
+  group = "url_funcs")
+// scalastyle:on line.size.limit
+case class UrlDecode(child: Expression)
+  extends RuntimeReplaceable with UnaryLike[Expression] with 
ImplicitCastInputTypes {
+
+  override def replacement: Expression =
+    StaticInvoke(
+      UrlCodec.getClass,
+      StringType,
+      "decode",
+      Seq(child, Literal("UTF-8")),
+      Seq(StringType))
+
+  override protected def withNewChildInternal(newChild: Expression): 
Expression = {
+    copy(child = newChild)
+  }
+
+  override def inputTypes: Seq[AbstractDataType] = Seq(StringType)
+
+  override def prettyName: String = "url_decode"
+}
+
+object UrlCodec {
+  def encode(src: UTF8String, enc: UTF8String): UTF8String = {
+    UTF8String.fromString(URLEncoder.encode(src.toString, enc.toString))

Review Comment:
   I write a micro bench as follows:
   
   ```scala
   def testEncode(url: String, valuesPerIteration: Int): Unit = {
       import org.apache.commons.codec.net.URLCodec
   
       val benchmark = new Benchmark("Test encode", valuesPerIteration, output 
= output)
   
       benchmark.addCase("Use java.net.URLEncoder") { _: Int =>
         for (_ <- 0L until valuesPerIteration) {
           java.net.URLEncoder.encode(url, "UTF-8")
         }
       }
   
       val codec = new URLCodec()
       benchmark.addCase("Use org.apache.commons.codec.net.URLCodec") { _: Int 
=>
         for (_ <- 0L until valuesPerIteration) {
           codec.encode(url)
         }
       }
       benchmark.run()
     }
   
     def testDecode(value: String, valuesPerIteration: Int): Unit = {
       import org.apache.commons.codec.net.URLCodec
   
       val benchmark = new Benchmark("Test decode", valuesPerIteration, output 
= output)
   
       benchmark.addCase("Use java.net.URLEncoder") { _: Int =>
         for (_ <- 0L until valuesPerIteration) {
           java.net.URLDecoder.decode(value, "UTF-8")
         }
       }
   
       val codec = new URLCodec()
       benchmark.addCase("Use org.apache.commons.codec.net.URLCodec") { _: Int 
=>
         for (_ <- 0L until valuesPerIteration) {
           codec.decode(value)
         }
       }
       benchmark.run()
     }
   
     override def runBenchmarkSuite(mainArgs: Array[String]): Unit = {
   
       val valuesPerIteration = 100000
   
       // Test Contains
       testEncode("https://spark.apache.org";, valuesPerIteration)
       testDecode("https%3A%2F%2Fspark.apache.org", valuesPerIteration)
     }
   ```
   
   and run it use GA, the result as follows:
   
   **Java 8**
   ```
   OpenJDK 64-Bit Server VM 1.8.0_332-b09 on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               32             33      
     1          3.1         324.3       1.0X
   Use org.apache.commons.codec.net.URLCodec             29             29      
     0          3.4         291.4       1.1X
   
   OpenJDK 64-Bit Server VM 1.8.0_332-b09 on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               53             54      
     1          1.9         530.0       1.0X
   Use org.apache.commons.codec.net.URLCodec             37             37      
     0          2.7         365.5       1.5X
   ```
   
   **Java 11**
   
   ```
   OpenJDK 64-Bit Server VM 11.0.15+10-LTS on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               25             28      
     2          4.0         251.6       1.0X
   Use org.apache.commons.codec.net.URLCodec             22             25      
     1          4.6         219.5       1.1X
   
   OpenJDK 64-Bit Server VM 11.0.15+10-LTS on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               31             31      
     1          3.3         306.2       1.0X
   Use org.apache.commons.codec.net.URLCodec             26             29      
     2          3.8         260.4       1.2X
   ```
   
   **Java 17**
   
   ```
   OpenJDK 64-Bit Server VM 17.0.3+7-LTS on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               31             32      
     1          3.2         308.3       1.0X
   Use org.apache.commons.codec.net.URLCodec             26             29      
     2          3.8         262.6       1.2X
   
   OpenJDK 64-Bit Server VM 17.0.3+7-LTS on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               40             44      
     3          2.5         402.0       1.0X
   Use org.apache.commons.codec.net.URLCodec             31             33      
     1          3.2         311.0       1.3X
   ```
   
   `org.apache.commons.codec.net.URLCodec` seems a little quick than 
`java.net.URLEncoder` from benchmark result, so should we change to use 
`org.apache.commons.codec.net.URLCodec` in this pr? @cloud-fan @Yikf 
   
   



-- 
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: reviews-unsubscr...@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to