coderfender commented on code in PR #4315:
URL: https://github.com/apache/datafusion-comet/pull/4315#discussion_r3253731787
##########
spark/src/main/scala/org/apache/comet/serde/strings.scala:
##########
@@ -495,4 +495,33 @@ trait CommonStringExprs {
None
}
}
+
+ def stringEncode(
+ expr: Expression,
+ charset: Expression,
+ value: Expression,
+ inputs: Seq[Attribute],
+ binding: Boolean): Option[Expr] = {
+ charset match {
+ case Literal(str, DataTypes.StringType)
+ if str != null && str.toString.toLowerCase(Locale.ROOT) == "utf-8" =>
Review Comment:
This seems like a fragile check . Perhaps a better check would be to use
`StandardCharsets` ?
##########
spark/src/test/resources/sql-tests/expressions/string/encode.sql:
##########
@@ -0,0 +1,61 @@
+-- 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.
+
+-- Tests for the SQL `encode(str, charset)` function.
+--
+-- Spark 3.x: Encode is a BinaryExpression(value, charset).
+-- Spark 4.x+: Encode is RuntimeReplaceable; the analyzer rewrites it to
+-- StaticInvoke(classOf[Encode], BinaryType, "encode", ...)
+
+statement
+CREATE TABLE test_encode_utf8(s string) USING parquet
+
+statement
+INSERT INTO test_encode_utf8 VALUES ('hello'), ('world'), (''), ('café'),
(NULL)
+
+query
+SELECT encode(s, 'utf-8') FROM test_encode_utf8
Review Comment:
I believe spark accepts any variant of `utf-8` (`utf8 /UTF8` etc) . May be a
good idea to add those tests ?
##########
spark/src/main/spark-4.1/org/apache/comet/shims/CometExprShim.scala:
##########
@@ -92,6 +92,19 @@ trait CometExprShim extends CommonStringExprs {
val Seq(bin, charset, _, _) = s.arguments
stringDecode(expr, charset, bin, inputs, binding)
+ case s: StaticInvoke
+ if s.staticObject == classOf[Encode] &&
+ s.dataType.isInstanceOf[BinaryType] &&
+ s.functionName == "encode" &&
+ s.arguments.size == 4 &&
+ s.inputTypes == Seq(
+ StringTypeWithCollation(supportsTrimCollation = true),
+ StringTypeWithCollation(supportsTrimCollation = true),
+ BooleanType,
+ BooleanType) =>
+ val Seq(value, charset, _, _) = s.arguments
+ stringEncode(expr, charset, value, inputs, binding)
+
Review Comment:
Seems like we missed removing this piece ?
##########
spark/src/main/spark-4.x/org/apache/comet/shims/ShimCometExprs.scala:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.comet.shims
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke
+import org.apache.spark.sql.internal.types.StringTypeWithCollation
+import org.apache.spark.sql.types.{BinaryType, BooleanType}
+
+import org.apache.comet.serde.CommonStringExprs
+import org.apache.comet.serde.ExprOuterClass.Expr
+
+trait ShimCometExprs extends CommonStringExprs {
Review Comment:
seems like the trait name seems to be different from `Comet<>` ?
--
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]