[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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


##
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:
   Sorry, the above bench test `encode` twice, need re-run, please ignore it



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



[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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)
   

[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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 encode", valuesPerIteration, output 
= output)
   

[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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)
   

[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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


##
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:
   agree with @cloud-fan 



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



[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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
   

[GitHub] [spark] LuciferYang commented on a diff in pull request #37113: [SPARK-39741][SQL] Support url encode/decode as built-in function and tidy up url-related functions

2022-07-12 Thread GitBox


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


##
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:
   We use `java.net.URLEncoder` now. 
   I wonder if `org.apache.commons.codec.net.URLCodec` and 
`java.net.URLEncoder` have the same semantics? 
   Will `org.apache.commons.codec.net.URLCodec` have better performance?
   
   



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