felipepessoto commented on code in PR #12967: URL: https://github.com/apache/gluten/pull/12967#discussion_r3937146783
########## backends-velox/src-delta33/test/scala/org/apache/spark/sql/delta/GlutenDeltaStatsSuite.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.spark.sql.delta + +import org.apache.spark.sql.delta.sources.DeltaSQLConf +import org.apache.spark.sql.delta.test.DeltaSQLCommandTest + +class GlutenDeltaStatsSuite extends DeltaSQLCommandTest { + + import testImplicits._ + + test("collect TIMESTAMP_NTZ statistics natively") { + withSQLConf(DeltaSQLConf.DELTA_COLLECT_STATS.key -> "true") { + withTempDir { + dir => + val path = dir.getCanonicalPath + val data = Seq( + "1969-12-31 23:59:59.999999", + "2024-01-01 00:00:00.123456" + ).toDF("input") + .selectExpr( + "cast(input as timestamp_ntz) as ts", + "struct(cast(input as timestamp_ntz) as ts) as nested") + + data.coalesce(1).write.format("delta").save(path) + + val actual = spark.read.format("delta").load(path) + assert(actual.collect().toSet == data.collect().toSet) + + val addFiles = DeltaLog.forTable(spark, path).update().allFiles.collect() + assert(addFiles.length == 1) + val stats = addFiles.head.stats + assert(stats != null) + assert(stats.contains("\"minValues\""), stats) + assert(stats.contains("\"maxValues\""), stats) + assert(stats.contains("1969-12-31") && stats.contains("23:59:59.999"), stats) + assert(stats.contains("2024-01-01") && stats.contains("00:00:00.123"), stats) Review Comment: Valid. Replaced the substring checks with exact JSON-path assertions for minValues.ts, minValues.nested.ts, maxValues.ts, and maxValues.nested.ts in bcfffcd5cd. The Delta 3.3 test passes. ########## cpp/velox/tests/VeloxSubstraitRoundTripTest.cc: ########## @@ -205,6 +206,38 @@ TEST_F(VeloxSubstraitRoundTripTest, countAll) { assertPlanConversion(plan, "SELECT count(*) as num_price FROM tmp WHERE c6 < 24 GROUP BY c0, c1"); } +TEST_F(VeloxSubstraitRoundTripTest, minMaxTimestampUtc) { + functions::aggregate::sparksql::registerAggregateFunctions("spark_"); Review Comment: Valid. Moved the Spark-prefixed aggregate registration to the test binary main(), so it runs once before the suite instead of mutating the global registry from an individual test. Updated in bcfffcd5cd. ########## gluten-ut/spark41/src/test/scala/org/apache/spark/sql/GlutenTimestampNtzAggregateSuite.scala: ########## @@ -0,0 +1,81 @@ +/* + * 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 + +import org.apache.gluten.config.GlutenConfig +import org.apache.gluten.execution.{HashAggregateExecBaseTransformer, ProjectExecTransformer} + +import org.apache.spark.sql.functions.{max, min} +import org.apache.spark.sql.internal.SQLConf + +import java.time.LocalDateTime + +class GlutenTimestampNtzAggregateSuite extends GlutenSQLTestsTrait { + + import testImplicits._ + + testGluten("min and max") { + withSQLConf( + SQLConf.ANSI_ENABLED.key -> "false", + GlutenConfig.GLUTEN_ANSI_FALLBACK_ENABLED.key -> "false") { + withTempPath { + path => + Seq( + "1969-12-31 23:59:59.999999", + "2024-01-01 00:00:00.123456" + ).toDF("input") + .selectExpr("cast(input as timestamp_ntz) as ts") + .write + .parquet(path.getCanonicalPath) + + val result = spark.read.parquet(path.getCanonicalPath).agg(min($"ts"), max($"ts")) + checkAnswer( + result, + Row( + LocalDateTime.parse("1969-12-31T23:59:59.999999"), + LocalDateTime.parse("2024-01-01T00:00:00.123456"))) + assert( + getExecutedPlan(result).exists(_.isInstanceOf[HashAggregateExecBaseTransformer]), + result.queryExecution.executedPlan.treeString) + } + } + } + + testGluten("unsupported project falls back") { + withSQLConf( + SQLConf.ANSI_ENABLED.key -> "false", + SQLConf.SESSION_LOCAL_TIMEZONE.key -> "America/Los_Angeles", + GlutenConfig.GLUTEN_ANSI_FALLBACK_ENABLED.key -> "false") { + withTempPath { + path => + Seq("2024-01-01 00:00:00.123456") + .toDF("input") + .selectExpr("cast(input as timestamp_ntz) as ts") + .write + .parquet(path.getCanonicalPath) + + val result = spark.read + .parquet(path.getCanonicalPath) + .selectExpr("to_json(named_struct('ts', ts))") + checkAnswer(result, Row("""{"ts":"2024-01-01T00:00:00.123"}""")) Review Comment: Keeping the explicit expected string intentionally. This suite is Spark 4.1-specific and pins the session timezone, and the exact value is the correctness oracle for the bug: native execution previously shifted the NTZ value by the session offset. Deriving the expectation through the same expression path would weaken that regression check. ########## backends-velox/src-delta40/test/scala/org/apache/spark/sql/delta/GlutenDeltaStatsSuite.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.spark.sql.delta + +import org.apache.spark.sql.delta.sources.DeltaSQLConf +import org.apache.spark.sql.delta.test.DeltaSQLCommandTest + +class GlutenDeltaStatsSuite extends DeltaSQLCommandTest { + + import testImplicits._ + + test("collect TIMESTAMP_NTZ statistics natively") { + withSQLConf(DeltaSQLConf.DELTA_COLLECT_STATS.key -> "true") { + withTempDir { + dir => + val path = dir.getCanonicalPath + val data = Seq( + "1969-12-31 23:59:59.999999", + "2024-01-01 00:00:00.123456" + ).toDF("input") + .selectExpr( + "cast(input as timestamp_ntz) as ts", + "struct(cast(input as timestamp_ntz) as ts) as nested") + + data.coalesce(1).write.format("delta").save(path) + + val actual = spark.read.format("delta").load(path) + assert(actual.collect().toSet == data.collect().toSet) + + val addFiles = DeltaLog.forTable(spark, path).update().allFiles.collect() + assert(addFiles.length == 1) + val stats = addFiles.head.stats + assert(stats != null) + assert(stats.contains("\"minValues\""), stats) + assert(stats.contains("\"maxValues\""), stats) + assert(stats.contains("1969-12-31") && stats.contains("23:59:59.999"), stats) + assert(stats.contains("2024-01-01") && stats.contains("00:00:00.123"), stats) Review Comment: Valid. Replaced the substring checks with exact JSON-path assertions for minValues.ts, minValues.nested.ts, maxValues.ts, and maxValues.nested.ts in bcfffcd5cd. The Delta 4.x test passes. ########## cpp/velox/substrait/VeloxToSubstraitType.cc: ########## @@ -31,6 +31,13 @@ const ::substrait::Type& VeloxToSubstraitTypeConvertor::toSubstraitType( substraitType->set_allocated_date(substraitDate); return *substraitType; } + if (type->equivalent(*velox::TIMESTAMP_UTC())) { + auto substraitTimestampNtz = google::protobuf::Arena::CreateMessage<::substrait::Type_PrecisionTimestamp>(&arena); + substraitTimestampNtz->set_precision(6); + substraitTimestampNtz->set_nullability(::substrait::Type_Nullability_NULLABILITY_NULLABLE); + substraitType->set_allocated_precision_timestamp(substraitTimestampNtz); Review Comment: Agreed. Renamed the variable to substraitPrecisionTimestamp in bcfffcd5cd to match the emitted PrecisionTimestamp type. ########## gluten-substrait/src/main/scala/org/apache/gluten/expression/ConverterUtils.scala: ########## @@ -414,7 +414,8 @@ object ConverterUtils extends Logging { case DoubleType => "fp64" case DateType => "date" case TimestampType => "ts" - case other if other.typeName == "timestamp_ntz" => "ts_ntz" + // Underscores delimit arguments in native function signatures. + case TimestampNTZType => "tsntz" Review Comment: No compatibility alias added. Gluten generates these Substrait plans per query and requires matching JVM/native components; they are not a persisted cross-version format. More importantly, ts_ntz was never parseable for aggregates because the signature parser splits on underscores, so there is no previously valid aggregate encoding to preserve. Accepting it only in fromSubstraitSignature would not fix sigToTypes and would give a false compatibility guarantee. -- 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]
