github-actions[bot] commented on code in PR #63192:
URL: https://github.com/apache/doris/pull/63192#discussion_r3231622050


##########
be/src/format/parquet/vparquet_column_reader.cpp:
##########
@@ -103,6 +114,410 @@ static void fill_array_offset(FieldSchema* field, 
ColumnArray::Offsets64& offset
     }
 }
 
+static constexpr int64_t UNIX_EPOCH_DAYNR = 719528;
+static constexpr int64_t MICROS_PER_SECOND = 1000000;
+
+static int64_t variant_date_value(const VecDateTimeValue& value) {
+    return value.daynr() - UNIX_EPOCH_DAYNR;
+}
+
+static int64_t variant_date_value(const DateV2Value<DateV2ValueType>& value) {
+    return value.daynr() - UNIX_EPOCH_DAYNR;
+}
+
+static int64_t variant_datetime_value(const VecDateTimeValue& value) {
+    int64_t timestamp = 0;
+    value.unix_timestamp(&timestamp, cctz::utc_time_zone());
+    return timestamp * MICROS_PER_SECOND;
+}
+
+static int64_t variant_datetime_value(const DateV2Value<DateTimeV2ValueType>& 
value) {
+    int64_t timestamp = 0;
+    value.unix_timestamp(&timestamp, cctz::utc_time_zone());
+    return timestamp * MICROS_PER_SECOND + value.microsecond();
+}
+
+static int64_t variant_datetime_value(const TimestampTzValue& value) {
+    int64_t timestamp = 0;
+    value.unix_timestamp(&timestamp, cctz::utc_time_zone());
+    return timestamp * MICROS_PER_SECOND + value.microsecond();
+}
+
+static int find_child_idx(const FieldSchema& field, std::string_view name) {
+    for (int i = 0; i < field.children.size(); ++i) {
+        if (field.children[i].lower_case_name == name) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+static bool is_variant_wrapper_field(const FieldSchema& field) {
+    auto type = remove_nullable(field.data_type);
+    if (type->get_primitive_type() != TYPE_STRUCT && 
type->get_primitive_type() != TYPE_VARIANT) {
+        return false;
+    }
+
+    bool has_metadata = false;
+    bool has_value = false;
+    bool has_typed_value = false;
+    for (const auto& child : field.children) {
+        if (child.lower_case_name == "metadata") {
+            if (child.physical_type != tparquet::Type::BYTE_ARRAY) {
+                return false;
+            }
+            has_metadata = true;
+            continue;
+        }
+        if (child.lower_case_name == "value") {
+            if (child.physical_type != tparquet::Type::BYTE_ARRAY) {
+                return false;
+            }
+            has_value = true;
+            continue;
+        }
+        if (child.lower_case_name == "typed_value") {
+            has_typed_value = true;
+            continue;
+        }
+        return false;
+    }
+    return has_typed_value || (has_metadata && has_value);
+}
+
+static const IColumn& remove_nullable_column(const IColumn& column) {
+    if (const auto* nullable = check_and_get_column<ColumnNullable>(&column)) {
+        return nullable->get_nested_column();
+    }
+    return column;
+}
+
+static Status get_binary_field(const Field& field, std::string* value, bool* 
present) {
+    if (field.is_null()) {
+        *present = false;
+        return Status::OK();
+    }
+    *present = true;
+    switch (field.get_type()) {
+    case TYPE_STRING:
+        *value = field.get<TYPE_STRING>();
+        return Status::OK();
+    case TYPE_CHAR:
+        *value = field.get<TYPE_CHAR>();
+        return Status::OK();
+    case TYPE_VARCHAR:
+        *value = field.get<TYPE_VARCHAR>();
+        return Status::OK();
+    case TYPE_VARBINARY: {
+        auto ref = field.get<TYPE_VARBINARY>().to_string_ref();
+        value->assign(ref.data, ref.size);
+        return Status::OK();
+    }
+    default:
+        return Status::Corruption("Parquet VARIANT binary field has unexpected 
Doris type {}",
+                                  field.get_type_name());
+    }
+}
+
+static PathInData append_path(const PathInData& prefix, const PathInData& 
suffix) {
+    if (prefix.empty()) {
+        return suffix;
+    }
+    if (suffix.empty()) {
+        return prefix;
+    }
+    PathInDataBuilder builder;
+    builder.append(prefix.get_parts(), false);
+    builder.append(suffix.get_parts(), false);
+    return builder.build();
+}
+
+static Status parse_json_to_variant_map(const std::string& json, const 
PathInData& prefix,
+                                        VariantMap* values) {
+    auto parsed_column = ColumnVariant::create(0, false);
+    ParseConfig parse_config;
+    StringRef json_ref(json.data(), json.size());
+    RETURN_IF_CATCH_EXCEPTION(
+            variant_util::parse_json_to_variant(*parsed_column, json_ref, 
nullptr, parse_config));
+    Field parsed = (*parsed_column)[0];
+    auto& parsed_values = parsed.get<TYPE_VARIANT>();
+    for (auto& [path, value] : parsed_values) {
+        (*values)[append_path(prefix, path)] = std::move(value);
+    }
+    return Status::OK();
+}
+
+static bool is_direct_variant_leaf_type(const DataTypePtr& data_type) {
+    const auto& type = remove_nullable(data_type);
+    switch (type->get_primitive_type()) {
+    case TYPE_BOOLEAN:
+    case TYPE_TINYINT:
+    case TYPE_SMALLINT:
+    case TYPE_INT:
+    case TYPE_BIGINT:
+    case TYPE_LARGEINT:
+    case TYPE_DECIMALV2:
+    case TYPE_DECIMAL32:
+    case TYPE_DECIMAL64:
+    case TYPE_DECIMAL128I:
+    case TYPE_DECIMAL256:
+    case TYPE_STRING:
+    case TYPE_CHAR:
+    case TYPE_VARCHAR:
+        return true;
+    case TYPE_ARRAY: {
+        const auto* array_type = assert_cast<const DataTypeArray*>(type.get());
+        return is_direct_variant_leaf_type(array_type->get_nested_type());
+    }
+    default:
+        return false;
+    }
+}
+
+static bool can_direct_read_typed_value(const FieldSchema& field_schema) {
+    if (is_variant_wrapper_field(field_schema)) {
+        const int value_idx = find_child_idx(field_schema, "value");

Review Comment:
   This wrapper detection is used by the typed-only direct-read fast path 
without knowing whether `field_schema` is the top-level `typed_value` 
object-field container or an actual shredded-field wrapper. A valid VARIANT 
object can have a key named `typed_value`; for a typed-only schema like 
`v.typed_value.typed_value` where that key is an `INT`, 
`can_direct_read_typed_value(_field_schema->children[typed_value_idx])` treats 
the top-level `typed_value` struct itself as a wrapper, unwraps the user field, 
and `append_direct_typed_column_to_batch()` writes the primitive at the root 
path instead of path `typed_value`. That changes `v` from an object 
(`{"typed_value": 1}`) into a scalar and makes `v['typed_value']` wrong. Please 
make wrapper detection context-aware (do not unwrap the top-level typed-value 
object container just because a user key collides with the structural name) and 
add coverage for a VARIANT key named `typed_value` (and ideally `value`) in 
typed-only shredding.



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