924060929 commented on code in PR #70808:
URL: https://github.com/apache/doris/pull/70808#discussion_r4679204432


##########
fe/fe-connector/fe-connector-hive/src/main/java/org/apache/doris/connector/hive/HiveScanPlanProvider.java:
##########
@@ -818,9 +847,11 @@ void recordPruningProfile(HiveTableHandle handle) {
     }
 
     private List<PartitionScanInfo> convertPartitions(
-            List<HmsPartitionInfo> hmsPartitions, List<String> partKeyNames) {
+            List<HmsPartitionInfo> hmsPartitions, HiveTableHandle handle, 
HiveFileFormat format) {
+        List<String> partKeyNames = handle.getPartitionKeyNames();
         List<PartitionScanInfo> result = new ArrayList<>(hmsPartitions.size());
         for (HmsPartitionInfo part : hmsPartitions) {
+            HiveAvroSchemaPolicy.validatePartition(handle, part, format);

Review Comment:
   [P2] Check the partition’s storage format before planning an Avro scan
   
   A Hive table can use Avro at the table level and Parquet for an individual 
partition. This call receives the table format. With a Parquet partition in 
HMS, both the synchronous and batch planners emitted `avro` for that partition 
in my test. The BE Avro reader then checks for the Avro container header, so a 
query that includes this partition fails on a valid Parquet file. Please use 
the partition’s storage descriptor to select its reader, or reject the format 
mismatch during planning if the scan can only carry one format.



##########
fe/fe-connector/fe-connector-hive/src/main/java/org/apache/doris/connector/hive/HiveAvroSchemaPolicy.java:
##########
@@ -0,0 +1,252 @@
+// 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.doris.connector.hive;
+
+import org.apache.doris.connector.hms.HmsPartitionInfo;
+import org.apache.doris.connector.spi.ConnectorColumn;
+import org.apache.doris.connector.spi.ConnectorType;
+import org.apache.doris.connector.spi.DorisConnectorException;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonParser;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+final class HiveAvroSchemaPolicy {
+    private static final String LITERAL = "avro.schema.literal";
+    private static final String URL = "avro.schema.url";
+    private static final Set<String> PRIMITIVES =
+            Set.of("null", "boolean", "int", "long", "float", "double", 
"bytes", "string");
+
+    private HiveAvroSchemaPolicy() {
+    }
+
+    static void validate(HiveTableHandle handle, HiveFileFormat format) {
+        if (format != HiveFileFormat.AVRO) {
+            return;
+        }
+        if (handle.requiresAvroStringCoercion()) {
+            throw new DorisConnectorException("Native Hive Avro CHAR/VARCHAR 
coercion is unsupported");
+        }
+        String literal = property(handle, LITERAL);
+        if (literal == null || "none".equals(literal)) {
+            String url = property(handle, URL);
+            if (url != null && !"none".equals(url)) {
+                throw unsupported();
+            }
+            return;
+        }
+        // Hive synthesizes nullable fields with explicit null defaults for 
ordinary column DDL.
+        // Custom reader defaults/aliases require Avro schema resolution; 
returning NULL instead
+        // of a declared default (for example 7) would silently change the 
table's result.
+        if (literal.length() > 1024 * 1024 || !boundedJson(literal)) {
+            throw unsupported();
+        }
+        try {
+            JsonElement schema = JsonParser.parseString(literal);
+            if (!schema.isJsonObject() || !"record".equals(typeName(schema))
+                    || !supported(schema, 0)) {
+                throw unsupported();
+            }
+        } catch (JsonParseException | IllegalStateException e) {
+            throw unsupported();
+        }
+    }
+
+    static void validatePartition(HiveTableHandle handle, HmsPartitionInfo 
partition, HiveFileFormat format) {
+        if (format != HiveFileFormat.AVRO) {
+            return;
+        }
+        String literal = partitionProperty(partition, LITERAL);
+        String url = partitionProperty(partition, URL);
+        // Hive opens partition files using partition-local reader properties 
before adapting
+        // records to the table schema. A table literal cannot hide a 
partition URL. Even URL
+        // "none" is a non-null URL setting in this path, rather than the 
literal's sentinel.
+        if ((literal == null || "none".equals(literal)) && url != null) {
+            throw unsupportedPartition();
+        }
+        literal = literal != null ? literal : property(handle, LITERAL);
+        String tableLiteral = readerSchema(handle);
+        literal = "none".equals(literal) ? null : literal;
+        // BE receives one reader schema for the whole scan. A different 
partition schema could
+        // supply a default (e.g. 7) where the table schema would silently 
materialize NULL.
+        if (!Objects.equals(literal, tableLiteral)) {
+            throw unsupportedPartition();
+        }
+        url = url != null ? url : property(handle, URL);
+        // Hive ignores schema URLs when an effective literal is present.
+        if (literal == null && url != null && !"none".equals(url)) {
+            throw unsupportedPartition();
+        }
+    }
+
+    private static String partitionProperty(HmsPartitionInfo partition, String 
key) {
+        // Hive overlays partition SerDe properties and then HMS partition 
parameters. A literal
+        // stored in the latter can replace a SerDe schema with a default of 7.
+        String value = partition.getParameters().get(key);
+        if (value == null) {
+            value = partition.getSdParameters().get(key);
+        }
+        return value;
+    }
+
+    private static DorisConnectorException unsupportedPartition() {
+        return new DorisConnectorException("Native Hive Avro partition reader 
schema overrides are unsupported");
+    }
+
+    static boolean requiresStringCoercion(List<ConnectorColumn> columns) {
+        return columns != null && columns.stream().anyMatch(column -> 
requiresStringCoercion(column.getType()));
+    }
+
+    private static boolean requiresStringCoercion(ConnectorType type) {
+        // DataTypeString later erases length and Hive's Unicode 
truncation/padding behavior.
+        return "CHAR".equalsIgnoreCase(type.getTypeName()) || 
"VARCHAR".equalsIgnoreCase(type.getTypeName())
+                || 
type.getChildren().stream().anyMatch(HiveAvroSchemaPolicy::requiresStringCoercion);
+    }
+
+    static String readerSchema(HiveTableHandle handle) {
+        String literal = property(handle, LITERAL);
+        return literal == null || "none".equals(literal) ? null : literal;
+    }
+
+    private static String property(HiveTableHandle handle, String key) {
+        String value = handle.getTableParameters().get(key);
+        return value != null ? value : handle.getSdParameters().get(key);
+    }
+
+    private static DorisConnectorException unsupported() {
+        return new DorisConnectorException("Native Hive Avro reads support 
only generated-style nullable reader "
+                + "schemas with null defaults; custom Avro reader defaults, 
aliases, named references and "
+                + "schema URLs and Hive CHAR/VARCHAR logical types are 
unsupported");
+    }
+
+    private static String typeName(JsonElement schema) {
+        if (schema == null) {
+            return "";
+        }
+        JsonElement type = schema.isJsonObject() ? 
schema.getAsJsonObject().get("type") : schema;
+        return type != null && type.isJsonPrimitive() && 
type.getAsJsonPrimitive().isString()
+                ? type.getAsString() : "";
+    }
+
+    private static boolean nullable(JsonElement schema) {
+        if ("null".equals(typeName(schema))) {
+            return true;
+        }
+        if (schema != null && schema.isJsonArray()) {
+            for (JsonElement branch : schema.getAsJsonArray()) {
+                if ("null".equals(typeName(branch))) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    private static boolean aliases(JsonObject object) {
+        JsonElement value = object.get("aliases");
+        return value != null && (!value.isJsonArray() || 
value.getAsJsonArray().size() != 0);
+    }
+
+    private static boolean supported(JsonElement schema, int depth) {
+        if (schema == null || depth > 64) {
+            return false;
+        }
+        if (schema.isJsonArray()) {
+            int nulls = 0;
+            for (JsonElement branch : schema.getAsJsonArray()) {
+                if (!supported(branch, depth + 1)) {
+                    return false;
+                }
+                nulls += "null".equals(typeName(branch)) ? 1 : 0;
+            }
+            return nulls == 1 && schema.getAsJsonArray().size() <= 2;
+        }
+        String type = typeName(schema);
+        if (!schema.isJsonObject()) {
+            return PRIMITIVES.contains(type);
+        }
+        JsonObject object = schema.getAsJsonObject();
+        JsonElement logical = object.get("logicalType");
+        if (logical != null && logical.isJsonPrimitive()) {
+            String name = logical.getAsString();
+            // Hive recognizes these annotations without case, while the 
native SDK does not.
+            if (name.chars().anyMatch(value -> value >= 128)
+                    || "char".equalsIgnoreCase(name) || 
"varchar".equalsIgnoreCase(name)
+                    || ("decimal".equalsIgnoreCase(name) && 
!"decimal".equals(name))) {
+                return false;
+            }
+        }
+        if (aliases(object)) {
+            return false;
+        }
+        if ("record".equals(type)) {
+            JsonElement fields = object.get("fields");
+            if (fields == null || !fields.isJsonArray()) {
+                return false;
+            }
+            for (JsonElement entry : fields.getAsJsonArray()) {
+                if (!entry.isJsonObject()) {
+                    return false;
+                }
+                JsonObject field = entry.getAsJsonObject();
+                if (aliases(field) || !field.has("default") || 
!field.get("default").isJsonNull()
+                        || !nullable(field.get("type")) || 
!supported(field.get("type"), depth + 1)) {

Review Comment:
   [P2] Validate a null reader default against the first union branch
   
   For a reader field declared as 
`{"name":"extra","type":["int","null"],"default":null}`, `nullable()` succeeds 
because the union contains `null`. The difference matters when an older file 
lacks `extra`. On that record, Hive 3.1.3’s bundled Avro reader threw 
`Non-numeric default value for int: null`. Reversing the branches to 
`["null","int"]` produced `extra = null`. FE accepted both literals, and the 
native reader treats a missing nullable field as NULL, so Doris can return a 
row that Hive rejects. Please require `null` as the first branch when accepting 
this reader default, and cover both orders with a missing writer field.



##########
fe/fe-connector/fe-connector-iceberg/src/main/java/org/apache/doris/connector/iceberg/IcebergScanPlanProvider.java:
##########
@@ -1352,12 +1353,13 @@ private IcebergScanRange 
buildPositionDeleteRange(PositionDeletesScanTask task,
     }
 
     /**
-     * PARQUET and PUFFIN both read through BE's parquet path; ORC through the 
orc one. AVRO position-delete
-     * files have no native reader — fail loud rather than mis-route. Message 
text mirrors legacy exactly.
+     * PUFFIN deletion vectors travel through the Parquet path; ordinary 
delete files retain their format.
      */
     private static TFileFormatType 
getNativePositionDeleteFileFormat(FileFormat fileFormat) {
         if (fileFormat == FileFormat.PARQUET || fileFormat == 
FileFormat.PUFFIN) {
             return TFileFormatType.FORMAT_PARQUET;
+        } else if (fileFormat == FileFormat.AVRO) {
+            return TFileFormatType.FORMAT_AVRO;

Review Comment:
   [P2] Apply the Avro split limit to `$position_deletes` scans
   
   This enables Avro for the metadata table, but 
`splitPositionDeleteScanTask()` calls the Iceberg SDK directly and bypasses the 
two-range limit in `splitFileTasks()`. In a planner test, one 1 GiB Avro delete 
file produced 16 ranges with a 64 MiB target. With 16 stored offsets, it still 
produced 16 ranges with a 2 GiB target because the SDK uses the offsets. The 
native reader walks the container prefix to locate each range, so these split 
starts add up to 7.5 GiB of repeated prefix traversal before scanning the 
assigned data. Actual remote I/O depends on caching. Please apply the same 
limit here, including the stored-offset case.



##########
fe/fe-connector/fe-connector-hive/src/main/java/org/apache/doris/connector/hive/HiveAvroSchemaPolicy.java:
##########
@@ -0,0 +1,252 @@
+// 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.doris.connector.hive;
+
+import org.apache.doris.connector.hms.HmsPartitionInfo;
+import org.apache.doris.connector.spi.ConnectorColumn;
+import org.apache.doris.connector.spi.ConnectorType;
+import org.apache.doris.connector.spi.DorisConnectorException;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonParser;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+final class HiveAvroSchemaPolicy {
+    private static final String LITERAL = "avro.schema.literal";
+    private static final String URL = "avro.schema.url";
+    private static final Set<String> PRIMITIVES =
+            Set.of("null", "boolean", "int", "long", "float", "double", 
"bytes", "string");
+
+    private HiveAvroSchemaPolicy() {
+    }
+
+    static void validate(HiveTableHandle handle, HiveFileFormat format) {
+        if (format != HiveFileFormat.AVRO) {
+            return;
+        }
+        if (handle.requiresAvroStringCoercion()) {
+            throw new DorisConnectorException("Native Hive Avro CHAR/VARCHAR 
coercion is unsupported");
+        }
+        String literal = property(handle, LITERAL);
+        if (literal == null || "none".equals(literal)) {
+            String url = property(handle, URL);
+            if (url != null && !"none".equals(url)) {
+                throw unsupported();
+            }
+            return;
+        }
+        // Hive synthesizes nullable fields with explicit null defaults for 
ordinary column DDL.
+        // Custom reader defaults/aliases require Avro schema resolution; 
returning NULL instead
+        // of a declared default (for example 7) would silently change the 
table's result.
+        if (literal.length() > 1024 * 1024 || !boundedJson(literal)) {
+            throw unsupported();
+        }
+        try {
+            JsonElement schema = JsonParser.parseString(literal);
+            if (!schema.isJsonObject() || !"record".equals(typeName(schema))
+                    || !supported(schema, 0)) {
+                throw unsupported();
+            }
+        } catch (JsonParseException | IllegalStateException e) {
+            throw unsupported();
+        }
+    }
+
+    static void validatePartition(HiveTableHandle handle, HmsPartitionInfo 
partition, HiveFileFormat format) {
+        if (format != HiveFileFormat.AVRO) {
+            return;
+        }
+        String literal = partitionProperty(partition, LITERAL);
+        String url = partitionProperty(partition, URL);
+        // Hive opens partition files using partition-local reader properties 
before adapting
+        // records to the table schema. A table literal cannot hide a 
partition URL. Even URL
+        // "none" is a non-null URL setting in this path, rather than the 
literal's sentinel.
+        if ((literal == null || "none".equals(literal)) && url != null) {
+            throw unsupportedPartition();
+        }
+        literal = literal != null ? literal : property(handle, LITERAL);
+        String tableLiteral = readerSchema(handle);
+        literal = "none".equals(literal) ? null : literal;
+        // BE receives one reader schema for the whole scan. A different 
partition schema could
+        // supply a default (e.g. 7) where the table schema would silently 
materialize NULL.
+        if (!Objects.equals(literal, tableLiteral)) {

Review Comment:
   [P2] Accept partition reader schemas that differ only in JSON formatting
   
   If the table stores a compact `avro.schema.literal` and a partition stores 
the same record pretty-printed, `Objects.equals()` compares the two strings and 
rejects the partition. I verified that Avro parsed the literals as equal while 
FE raised `Native Hive Avro partition reader schema overrides are unsupported`. 
A query that includes this partition therefore fails during planning even 
though the table reader can read it. Please compare the parsed schema 
structures, preserving field and union order, defaults, and logical types. A 
whitespace-only test would cover this case.



##########
fe/fe-connector/fe-connector-hive/src/main/java/org/apache/doris/connector/hive/HiveAvroSchemaPolicy.java:
##########
@@ -0,0 +1,252 @@
+// 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.doris.connector.hive;
+
+import org.apache.doris.connector.hms.HmsPartitionInfo;
+import org.apache.doris.connector.spi.ConnectorColumn;
+import org.apache.doris.connector.spi.ConnectorType;
+import org.apache.doris.connector.spi.DorisConnectorException;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonParser;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+final class HiveAvroSchemaPolicy {
+    private static final String LITERAL = "avro.schema.literal";
+    private static final String URL = "avro.schema.url";
+    private static final Set<String> PRIMITIVES =
+            Set.of("null", "boolean", "int", "long", "float", "double", 
"bytes", "string");
+
+    private HiveAvroSchemaPolicy() {
+    }
+
+    static void validate(HiveTableHandle handle, HiveFileFormat format) {
+        if (format != HiveFileFormat.AVRO) {
+            return;
+        }
+        if (handle.requiresAvroStringCoercion()) {
+            throw new DorisConnectorException("Native Hive Avro CHAR/VARCHAR 
coercion is unsupported");
+        }
+        String literal = property(handle, LITERAL);
+        if (literal == null || "none".equals(literal)) {
+            String url = property(handle, URL);
+            if (url != null && !"none".equals(url)) {

Review Comment:
   [P2] Reject a table-level `avro.schema.url='none'` when no literal is 
available
   
   With no `avro.schema.literal`, an explicit URL value of `none`, and ordinary 
columns such as `id INT`, Hive 3.1.3’s `determineSchemaOrThrowException()` 
throws `AvroSerdeException`. I checked the same properties in FE: validation 
succeeded and the scan carried no reader literal. Doris can therefore proceed 
using schema fallback for a table Hive cannot read. The partition check already 
rejects this case. Please distinguish an absent URL from the explicit string 
`none` in table validation as well, while preserving literal precedence when a 
usable literal is present.



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