Skip to content

Conversation

@shangxinli
Copy link
Contributor

@shangxinli shangxinli commented Dec 30, 2025

The previous code had a logic error when checking the result of FindFieldByName(). It used:
if (!field.has_value() || !field.value().has_value())

This was incorrect because:

  1. If FindFieldByName() returns an error (Result contains an error), field.has_value() would be false, but the error was not propagated
  2. field.value() returns std::optional<std::reference_wrapper<...>>, so the second check was redundant and confusing

Fixed by using ICEBERG_ASSIGN_OR_RAISE to properly propagate any errors from FindFieldByName(), then checking if the returned optional contains a value. This matches the pattern used throughout the rest of the codebase and ensures errors are properly handled.

The previous error handling in VerifyReferencedColumns treated all
failures the same way. When FindFieldByName() failed, both actual
errors (e.g., internal failures during name resolution) and simple
"field not found" cases would return the same generic validation
error message, losing important error context.

FindFieldByName() returns Result<optional<reference_wrapper>>, which
can represent three states:
1. Error during lookup (Result contains error)
2. Lookup succeeded but field not found (Result contains empty optional)
3. Lookup succeeded and field found (Result contains optional with value)

The original code combined states 1 and 2:
  if (!field.has_value() || !field.value().has_value())

Changed to use ICEBERG_ASSIGN_OR_RAISE to separate error propagation
from the "not found" check:
  ICEBERG_ASSIGN_OR_RAISE(auto field, schema.FindFieldByName(...));
  if (!field.has_value())

The macro unwraps the Result, propagating any errors immediately,
then assigns the optional to field. The subsequent has_value() check
verifies if the optional contains a value, maintaining the same
functionality while properly handling errors. This pattern is already
used elsewhere in the codebase (e.g., table_metadata.cc:80-85).
@WZhuo
Copy link
Contributor

WZhuo commented Dec 31, 2025

if (!field.has_value() || !field.value().has_value()), the first is for Result, the second is for optional. I agree it should propagate any errors from FindFieldByName().

@wgtmac
Copy link
Member

wgtmac commented Dec 31, 2025

if (!field.has_value() || !field.value().has_value()), the first is for Result, the second is for optional. I agree it should propagate any errors from FindFieldByName().

Agreed. I think they are different errors and should be handled differently.

@wgtmac wgtmac merged commit e2cf82f into apache:main Dec 31, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants