harshmotw-db commented on code in PR #60634:
URL: https://github.com/apache/spark/pull/60634#discussion_r4648614424


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertVariantCanonicalization.scala:
##########
@@ -0,0 +1,230 @@
+/*
+ * 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.optimizer
+
+import scala.collection.immutable.VectorMap
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions.{Alias, ArrayTransform, 
Attribute, CreateNamedStruct, Expression, GetStructField, If, IsNull, 
LambdaFunction, Literal, NamedExpression, NamedLambdaVariable, SortOrder, 
TransformValues}
+import 
org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, 
AggregateFunction}
+import org.apache.spark.sql.catalyst.expressions.variant.CanonicalizeVariant
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan, 
Project, RepartitionByExpression}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{AGGREGATE, 
REPARTITION_OPERATION}
+import org.apache.spark.sql.types.{ArrayType, MapType, StructType, VariantType}
+import org.apache.spark.util.ArrayImplicits.SparkArrayOps
+
+/**
+ * Adds [[CanonicalizeVariant]] to grouping expressions and distinct aggregate 
arguments that
+ * contain Variant values, ensuring logically equal Variants have identical 
bytes before
+ * aggregation:
+ *
+ * SELECT variant_column, COUNT(*) FROM TABLE GROUP BY variant_column =>
+ * SELECT _groupingvariantcanonicalization AS variant_column, COUNT(*) FROM (
+ *   SELECT canonicalize_variant(variant_column) AS 
_groupingvariantcanonicalization FROM TABLE
+ * ) GROUP BY _groupingvariantcanonicalization
+ *
+ * SELECT COUNT(DISTINCT variant_column) FROM TABLE =>
+ * SELECT COUNT(DISTINCT _distinctvariantcanonicalization) FROM (
+ *   SELECT canonicalize_variant(variant_column) AS 
_distinctvariantcanonicalization FROM TABLE
+ * )
+ *
+ * This rule runs after [[PullOutGroupingExpressions]], so grouping keys are 
attributes or
+ * literals. Distinct arguments are normalized here instead of in 
[[RewriteDistinctAggregates]]
+ * because a single distinct group without a filter bypasses that rule and is 
handled by the
+ * physical planner.
+ */
+object InsertVariantCanonicalizationInAggregate extends Rule[LogicalPlan] {

Review Comment:
   Currently, the query would fail because we don't allow `group by v` :)
   
   But yes, canonicalization can change the `schema_of_variant` output. 
However, I think that is fine. According to the [variant 
spec](https://parquet.apache.org/docs/file-format/types/variantencoding/#encoding-types),
 decimal and integer types both fall into the exact numeric equivalence class 
and so I think they may be transformed into each other.
   
   @gene-db What do you think?



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