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


##########
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:
   Update result:
   
   **Java 8**
   ```
   OpenJDK 64-Bit Server VM 1.8.0_332-b09 on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) Platinum 8171M CPU @ 2.60GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               40             41      
     1          2.5         404.9       1.0X
   Use org.apache.commons.codec.net.URLCodec             36             38      
     1          2.8         358.5       1.1X
   
   OpenJDK 64-Bit Server VM 1.8.0_332-b09 on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) Platinum 8171M CPU @ 2.60GHz
   Test decode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               38             39      
     1          2.6         379.0       1.0X
   Use org.apache.commons.codec.net.URLCodec             31             32      
     2          3.2         312.8       1.2X
   ```
   
   **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                               26             26      
     0          3.9         256.5       1.0X
   Use org.apache.commons.codec.net.URLCodec             24             24      
     1          4.2         240.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 decode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               22             22      
     0          4.6         217.5       1.0X
   Use org.apache.commons.codec.net.URLCodec             18             18      
     0          5.6         179.0       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 v4 @ 2.30GHz
   Test encode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               29             33      
     2          3.4         290.1       1.0X
   Use org.apache.commons.codec.net.URLCodec             31             35      
     2          3.3         305.3       1.0X
   
   OpenJDK 64-Bit Server VM 17.0.3+7-LTS on Linux 5.13.0-1031-azure
   Intel(R) Xeon(R) CPU E5-2673 v4 @ 2.30GHz
   Test decode:                               Best Time(ms)   Avg Time(ms)   
Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
   
-------------------------------------------------------------------------------------------------------------------------
   Use java.net.URLEncoder                               22             26      
     2          4.5         223.1       1.0X
   Use org.apache.commons.codec.net.URLCodec             25             29      
     2          4.0         252.3       0.9X
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to