Copilot commented on code in PR #12967: URL: https://github.com/apache/gluten/pull/12967#discussion_r3936776217
########## 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: Same issue as the Delta 4.0 variant: substring checks can yield false positives and don’t verify the correct JSON paths/columns. Prefer parsing `stats` JSON and asserting exact `minValues`/`maxValues` values for `ts` and `nested.ts`. ########## 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: `registerAggregateFunctions` typically mutates a global function registry. Calling it inside a single test can introduce order-dependence or duplicate-registration issues if other tests also register the same prefix (now or in the future). Consider registering once for the whole test suite (e.g., in the fixture’s `SetUpTestSuite()` guarded by `std::once_flag`) to make this deterministic. ########## 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: Changing the `TIMESTAMP_NTZ` signature from `ts_ntz` to `tsntz` can be a compatibility break for any persisted/cached Substrait plans or cross-version components that still emit/expect `ts_ntz`. Consider adding backward-compatible parsing/aliasing (accept both `ts_ntz` and `tsntz` on the ‘from signature’ path and/or in any signature normalization), and document the deprecation if supported. ########## 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: `substraitTimestampNtz` is misleading here because the emitted Substrait type is `PrecisionTimestamp` for `TIMESTAMP_UTC`. Renaming this variable to something like `substraitPrecisionTimestamp`/`substraitTimestampUtc` would make the mapping clearer and reduce confusion when debugging type conversions. ########## 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: These assertions are substring-based and can pass even if the stats structure is malformed or if min/max values are present but associated with the wrong column (e.g., `nested.ts` vs `ts`). To make the regression more robust, parse `stats` as JSON and assert the specific `minValues`/`maxValues` entries for both `ts` and `nested.ts` match the expected serialized values. ########## 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: The expected JSON timestamp string format here is hard-coded and may vary across Spark versions/config (e.g., millisecond vs microsecond rendering). To reduce brittleness, consider deriving the expected JSON using Spark’s own expression evaluation (e.g., build an expected DataFrame using the same `to_json(named_struct(...))` over a literal `timestamp_ntz`) and compare Rows, rather than pinning an exact string representation. -- 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]
