-
Notifications
You must be signed in to change notification settings - Fork 321
feat: Support Spark Expression Encode #4315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fdc962a
91f1b57
d46c128
8ff9922
3c08105
ef35199
16d4f89
bc1bbcf
f507beb
3e55030
0b54aef
dd2f63d
4448c18
7f0866d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,14 +30,14 @@ import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, DataTypes | |
| import org.apache.comet.CometConf | ||
| import org.apache.comet.CometSparkSessionExtensions.withInfo | ||
| import org.apache.comet.expressions.{CometCast, CometEvalMode} | ||
| import org.apache.comet.serde.{CommonStringExprs, Compatible, ExprOuterClass, Incompatible, SupportLevel} | ||
| import org.apache.comet.serde.{Compatible, ExprOuterClass, Incompatible, SupportLevel} | ||
| import org.apache.comet.serde.ExprOuterClass.{BinaryOutputStyle, Expr} | ||
| import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, optExprWithInfo, scalarFunctionExprToProto, scalarFunctionExprToProtoWithReturnType, supportedScalarSortElementType} | ||
|
|
||
| /** | ||
| * `CometExprShim` acts as a shim for parsing expressions from different Spark versions. | ||
| */ | ||
| trait CometExprShim extends CommonStringExprs { | ||
| trait CometExprShim extends ShimCometExprs { | ||
| protected def evalMode(c: Cast): CometEvalMode.Value = | ||
| CometEvalModeUtil.fromSparkEvalMode(c.evalMode) | ||
|
|
||
|
|
@@ -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) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like we missed removing this piece ? |
||
| case expr @ ToPrettyString(child, timeZoneId) => | ||
| val castSupported = CometCast.isSupported( | ||
| child.dataType, | ||
|
|
@@ -168,7 +181,7 @@ trait CometExprShim extends CommonStringExprs { | |
| optExprWithInfo(mapSortExpr, ms, ms.child) | ||
| } | ||
|
|
||
| case _ => None | ||
| case _ => sparkExprToProto(expr, inputs, binding) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like the trait name seems to be different from |
||
|
|
||
| protected def sparkExprToProto( | ||
| expr: Expression, | ||
| inputs: Seq[Attribute], | ||
| binding: Boolean): Option[Expr] = { | ||
| expr match { | ||
| // encode(str, 'utf-8') -> cast(string AS binary) — Arrow's Utf8->Binary | ||
| // is a zero-copy reinterpret, matching Spark's UTF8String.getBytes() exactly. | ||
| 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) | ||
|
|
||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe spark accepts any variant of |
||
|
|
||
| query | ||
| SELECT encode(s, 'UTF-8') FROM test_encode_utf8 | ||
|
|
||
| -- Mixed-case charset literal exercises toLowerCase normalization | ||
| query | ||
| SELECT encode(s, 'Utf-8') FROM test_encode_utf8 | ||
|
|
||
| query | ||
| SELECT encode('hello', 'utf-8'), encode('', 'utf-8'), encode(CAST(NULL AS STRING), 'utf-8') | ||
|
|
||
| -- Different language(French, Japanese) | ||
| query | ||
| SELECT encode('café', 'utf-8'), encode('日本語', 'utf-8') | ||
|
|
||
| -- non-UTF-8 falls back to Spark JVM | ||
| statement | ||
| CREATE TABLE test_encode_charset_safe(s string) USING parquet | ||
|
|
||
| statement | ||
| INSERT INTO test_encode_charset_safe VALUES ('hello'), ('world'), (''), (NULL) | ||
|
|
||
| query expect_fallback(Comet only supports encoding with 'utf-8'.) | ||
| SELECT encode(s, 'UTF-16BE') FROM test_encode_charset_safe | ||
|
|
||
| query expect_fallback(Comet only supports encoding with 'utf-8'.) | ||
| SELECT encode(s, 'US-ASCII') FROM test_encode_charset_safe | ||
|
|
||
| query expect_fallback(Comet only supports encoding with 'utf-8'.) | ||
| SELECT encode(s, 'ISO-8859-1') FROM test_encode_charset_safe | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a fragile check . Perhaps a better check would be to use
StandardCharsets?