hello-stephen commented on code in PR #71365:
URL: https://github.com/apache/doris/pull/71365#discussion_r4788244952


##########
fe/fe-connector/fe-connector-spi/src/main/java/org/apache/doris/connector/spi/mvcc/ConnectorMvccPartition.java:
##########
@@ -40,6 +40,10 @@
  * upper as {@code lowerKey.successor()} in that case — matching the 
connector's source behavior (e.g. iceberg's
  * {@code nullLowKey.successor()}). A non-NULL RANGE partition always carries 
BOTH bounds non-empty; the model
  * must not call {@code createPartitionKey} on an empty upper tuple.</p>
+ *
+ * <p>For temporal RANGE columns, {@code "MAXVALUE"} denotes an unbounded 
endpoint when the

Review Comment:
   [P2] Version the temporal MAXVALUE bound contract
   
   The new connector emits MAXVALUE for the exclusive bound of a DATE identity 
partition at 9999-12-31. A connector built from this change still advertises 
42.0, so an FE built from the base accepts it through the plugin version gate. 
That FE treats MAXVALUE as an ordinary date string and throws when it builds 
the range. Version the new bound convention or negotiate support before 
emitting it.



##########
fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergPartitionUtils.java:
##########
@@ -1143,9 +1146,11 @@ static RangeBuild buildRange(String name, String value, 
String transform, Type s
         // "yyyy-MM-dd", iceberg TIMESTAMP/TIMESTAMPTZ -> "yyyy-MM-dd 
HH:mm:ss" (HOUR's source is always a
         // timestamp). Equivalent to master's c.getType().isDate()||isDateV2() 
formatter switch.
         DateTimeFormatter formatter = sourceType.typeId() == TypeID.DATE ? 
RANGE_DATE_FORMAT : RANGE_DATETIME_FORMAT;
+        // The last valid calendar interval ends in year 10000, outside 
Doris's literal domain.
+        String upperBound = upper.getYear() > 9999 ? "MAXVALUE" : 
upper.format(formatter) + rangeOffset(sourceType);

Review Comment:
   [P2] Preserve native reads when an evolved range cannot be represented
   
   Start with an empty unpartitioned Iceberg timestamp table, add hour(ts), and 
append a file for 0000-02-28 23:00:00 using Iceberg’s Gregorian hour ordinal. 
This layout now qualifies, but its exclusive endpoint is 0000-02-29 00:00:00, 
which Doris’s DATETIMEV2 parser rejects. With 
enable_iceberg_internal_partition_pruning=true, even SELECT id fails during 
range materialization. Check both endpoints when deriving the range and use 
native planning if the view cannot represent it.



##########
regression-test/suites/mtmv_p0/test_iceberg_mtmv_retained_specs.groovy:
##########
@@ -0,0 +1,136 @@
+// 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.
+
+suite("test_iceberg_mtmv_retained_specs", 
"p0,external,iceberg,external_docker,external_docker_hive") {
+    if 
(!"true".equalsIgnoreCase(context.config.otherConfigs.get("enableIcebergTest")))
 {
+        logger.info("disable iceberg test.")
+        return
+    }
+    String externalEnvIp = context.config.otherConfigs.get("externalEnvIp")
+    String restPort = context.config.otherConfigs.get("iceberg_rest_uri_port")
+    String minioPort = context.config.otherConfigs.get("iceberg_minio_port")
+    String catalog = "iceberg_mtmv_retained_specs"
+    String database = "regression_test_iceberg_mtmv_retained_specs"
+    String sourceDatabase = "mtmv_retained_specs"
+
+    sql "drop catalog if exists ${catalog}"
+    sql """create catalog ${catalog} properties (
+        'type'='iceberg',
+        'iceberg.catalog.type'='rest',
+        'uri'='http://${externalEnvIp}:${restPort}',
+        's3.access_key'='admin',
+        's3.secret_key'='password',
+        's3.endpoint'='http://${externalEnvIp}:${minioPort}',
+        's3.region'='us-east-1'
+    )"""
+    sql "switch internal"
+    sql "drop database if exists ${database} force"
+    sql "create database ${database}"
+    sql "use ${database}"
+    sql "drop database if exists ${catalog}.${sourceDatabase} force"
+    sql "create database ${catalog}.${sourceDatabase}"
+
+    def compareWithSource = { String mv, String source ->
+        // Compare complete ordered rows against the independently executed 
base-table query.
+        def expected = sql "select /*+ 
SET_VAR(enable_materialized_view_rewrite=false) */ id, event_date from 
${catalog}.${sourceDatabase}.${source} order by id"
+        def actual = sql "select id, event_date from ${mv} order by id"
+        assertEquals(expected, actual)
+    }
+
+    def overwriteAll = { String statement ->
+        String previousMode = sql("show variables like 
'iceberg_partition_overwrite_mode'")[0][1].toString()
+        try {
+            sql "set iceberg_partition_overwrite_mode='static'"
+            sql statement
+        } finally {
+            sql "set iceberg_partition_overwrite_mode='${previousMode}'"
+        }
+    }
+
+    sql """create table ${catalog}.${sourceDatabase}.evolved_events (id int, 
event_date date)
+        engine=iceberg properties ('format-version'='2')"""
+    sql """insert into ${catalog}.${sourceDatabase}.evolved_events values
+        (1, '1969-12-31'), (2, null)"""
+    sql "alter table ${catalog}.${sourceDatabase}.evolved_events add partition 
key event_date"
+    sql "alter table ${catalog}.${sourceDatabase}.evolved_events drop 
partition key event_date"
+    sql "alter table ${catalog}.${sourceDatabase}.evolved_events add partition 
key day(event_date)"
+    sql "insert into ${catalog}.${sourceDatabase}.evolved_events values (3, 
'2024-01-02')"
+    sql "refresh table ${catalog}.${sourceDatabase}.evolved_events"
+
+    // The default day spec is insufficient while unpartitioned data is still 
live.
+    test {
+        sql """create materialized view mv_evolved_events build deferred 
refresh auto on manual
+            partition by(event_date) distributed by random buckets 1
+            properties ('replication_num'='1')
+            as select id, event_date from 
${catalog}.${sourceDatabase}.evolved_events"""
+        exception "Unable to find a suitable base table for partitioning"
+    }
+
+    // Rewriting live files keeps the old specs and snapshots but makes the 
current layout representable.
+    overwriteAll """insert overwrite table 
${catalog}.${sourceDatabase}.evolved_events
+        select /*+ SET_VAR(enable_materialized_view_rewrite=false) */ id, 
event_date from ${catalog}.${sourceDatabase}.evolved_events"""
+    sql "refresh table ${catalog}.${sourceDatabase}.evolved_events"
+    sql """create materialized view mv_evolved_events build deferred refresh 
auto on manual
+        partition by(event_date) distributed by random buckets 1
+        properties ('replication_num'='1')
+        as select id, event_date from 
${catalog}.${sourceDatabase}.evolved_events"""
+    sql "refresh materialized view mv_evolved_events complete"
+    waitingMTMVTaskFinishedByMvName("mv_evolved_events", database)
+    compareWithSource("mv_evolved_events", "evolved_events")
+    sql "insert into ${catalog}.${sourceDatabase}.evolved_events values (4, 
'2024-01-03')"
+    sql "refresh materialized view mv_evolved_events auto"
+    waitingMTMVTaskFinishedByMvName("mv_evolved_events", database)
+    compareWithSource("mv_evolved_events", "evolved_events")
+
+    sql """create table ${catalog}.${sourceDatabase}.identity_events (id int, 
event_date date)
+        engine=iceberg partition by list(event_date) () properties 
('format-version'='2')"""
+    sql """insert into ${catalog}.${sourceDatabase}.identity_events values
+        (1, '2024-02-01'), (2, null), (6, '9999-12-31')"""
+    sql "alter table ${catalog}.${sourceDatabase}.identity_events drop 
partition key event_date"
+    sql "alter table ${catalog}.${sourceDatabase}.identity_events add 
partition key day(event_date)"
+    sql """insert into ${catalog}.${sourceDatabase}.identity_events values
+        (3, '2024-02-01'), (4, null), (5, '2024-02-02')"""
+    sql "refresh table ${catalog}.${sourceDatabase}.identity_events"
+    sql """create materialized view mv_identity_events build deferred refresh 
auto on manual
+        partition by(event_date) distributed by random buckets 1
+        properties ('replication_num'='1')
+        as select id, event_date from 
${catalog}.${sourceDatabase}.identity_events"""
+    sql "refresh materialized view mv_identity_events complete"
+    waitingMTMVTaskFinishedByMvName("mv_identity_events", database)
+    compareWithSource("mv_identity_events", "identity_events")
+
+    // NULL must not absorb other timestamp hours in the first representable 
day.
+    sql """create table ${catalog}.${sourceDatabase}.hour_events (id int, 
event_date datetimev2(6))
+        engine=iceberg properties ('format-version'='2')"""
+    sql "alter table ${catalog}.${sourceDatabase}.hour_events add partition 
key hour(event_date)"
+    sql """insert into ${catalog}.${sourceDatabase}.hour_events values
+        (1, null), (2, '0000-01-01 12:00:00'), (3, '0000-01-01 00:00:00')"""

Review Comment:
   [P2] Fix year-zero hour ordinals before enabling this layout
   
   For the noon row in this INSERT, the BE hour transform uses a day offset of 
−719527. The timestamp written to Parquet uses the Gregorian offset −719528. 
The committed manifest therefore describes January 2, and the MV builds a 
January 2 refresh predicate for a January 1 timestamp. The connector unit test 
supplies the correct ordinal directly, so it skips the writer path. Align the 
writer’s hour calculation with Iceberg and assert the expected timestamps in 
this SQL test.



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