Skip to content

New error codes for planning, plan complexity, and storage failures - #4792

Merged
sergei-pustovykh merged 31 commits into
mainfrom
apple/sergei-pustovykh/error-codes
Oct 29, 2026
Merged

sergei-pustovykh merged 31 commits into
mainfrom
apple/sergei-pustovykh/error-codes

Conversation

@sergei-pustovykh

@sergei-pustovykh sergei-pustovykh commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Today several different failure kinds share one ErrorCode, so clients cannot tell them apart:

  • UNSUPPORTED_QUERY covers both "the planner found no plan" and unsupported syntax.
  • The old planner throws a plain RecordCoreException when it cannot plan.
  • Plan complexity failures and most storage failures map to UNKNOWN.
  • Semantic errors on the main SQL planning path map to INTERNAL_ERROR.
  • A metadata build failure maps to SERIALIZATION_FAILURE, a transaction rollback code.

This PR gives each of these failures its own code.

New error codes

Code SQLSTATE Used for
TRANSACTION_RESOLUTION_UNKNOWN 08007 FDBStoreMaybeCommittedException
UNABLE_TO_PLAN 0AF02 UnableToPlanException
TRANSACTION_TOO_OLD 40F01 FDBStoreTransactionIsTooOldException
TRANSIENT_TRANSACTION_FAILURE 40F02 other RecordCoreRetriableTransactionException
STATEMENT_TOO_COMPLEX 54001 RecordQueryPlanComplexityException
TRANSACTION_TOO_LARGE 54F02 FDBStoreTransactionSizeException
KEY_TOO_LARGE 54F03 FDBStoreKeySizeException
VALUE_TOO_LARGE 54F04 FDBStoreValueSizeException
LOCK_NOT_AVAILABLE 55F03 FDBStoreLockTakenException
SCHEMA_TEMPLATE_SERIALIZATION_FAILURE XXF02 DescriptorValidationException when building metadata from a schema template
STORAGE_ERROR XXF03 other FDBStoreException

Changes

  • ExceptionUtil maps the exceptions above to the new codes. Among the retriable transaction errors
    (RecordCoreRetriableTransactionException), only a transaction conflict maps to SERIALIZATION_FAILURE.
    Errors where the transaction may have committed get their own code, because a retry is not safe for
    them. FDBStoreLockTakenException maps to LOCK_NOT_AVAILABLE, so clients can wait before they retry.
  • FDBExceptions.wrapException wraps an FDB error with isMaybeCommitted() (for example
    commit_unknown_result or cluster_version_changed) in the new FDBStoreMaybeCommittedException.
    It extends FDBStoreRetriableException, so existing retry handling does not change.
  • RecordQueryPlanner throws UnableToPlanException when it finds no plan. It throws the new
    UnsupportedQueryException for a query that mixes regular and synthetic record types.
    LucenePlanner throws UnsupportedQueryException for an unsupported sort field type.
    Both exceptions are still RecordCoreExceptions, and the messages do not change.
  • PlanGenerator translates a SemanticException through ExceptionUtil, so it gets a specific
    code instead of INTERNAL_ERROR. Its message now starts with the exception class name, as for the
    other Record Layer exceptions that ExceptionUtil translates.
  • RecordLayerSchemaTemplate reports a protobuf descriptor validation failure as
    SCHEMA_TEMPLATE_SERIALIZATION_FAILURE instead of SERIALIZATION_FAILURE, which is a transaction
    rollback code. The descriptor is generated from the schema template, so this is an internal failure.
  • SplitHelper throws FDBStoreValueSizeException for a record that is too long and cannot be split,
    so it maps to VALUE_TOO_LARGE.
  • The ORDER BY and GROUP BY docs list 0AF02 for a query without a suitable index.

@sergei-pustovykh sergei-pustovykh added the enhancement New feature or request label Oct 26, 2026

/**
* Error being thrown when Cascades cannot plan a query.
* Error being thrown when the planner cannot plan a query.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be objectionable that the old planner now uses an exception class from the query/plan/cascades package of the Cascades planner. I’m not familiar with our policy regarding this. Maybe we rather want to leave the old planner untouched, or give it its own UnableToPlanException.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I see that RecordQueryPlanner already has several imports from query.plan.cascades today.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it’s shared, then arguably the class should also be moved up to query.plan, where UnsupportedQueryException already sits. But that, of course, will break external imports…

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now think it’s probably fine as it is now, but a second opinion would be good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some concerns initially but spotted a lot of common files and decided to keep the exception common. do not see any possible issues + such approach minimises the code

@robert-brunel

Copy link
Copy Markdown
Contributor

Consider adding the Run mixed mode and breaking change labels.


// Class 58 - System Error
UNDEFINED_FILE("58F01"),
STORAGE_ERROR("58F02"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STORAGE_ERROR("58F02") is probably fine, but taking inspiration from PostgreSQL, some closely related non-P codes we could alternatively adopt are 58030 io_error (narrower) or just 58000 system_error (i.e., the generic 58 class).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most FDB errors are not I/O errors, I would keep STORAGE_ERROR here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how we came to that conclusion, error category 58 corresponds to errors external to the database engine AFAIK, but FDB errors are internal to FRL, should we use a different category instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered FDB as external part for FRL. If it is wrong, moving to XX - internal errors:
STORAGE_ERROR("XXF03")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debatable what the “system” is. I would have said FDB is external as well, since FRL itself just opens a client connection to FDB. Also, a different category would be useful only if our FRL clients would want to programmatically react differently to those. But to them it’s all just “the system” and beyond their control.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also consider what we say in the comments:
* 58 | System Error (errors external to Relational)
versus

    /**
     * Used for the exceptions due to internal issue of Relational, which is caused by neither clients nor upstream
     * system.
     */
    INTERNAL_ERROR("XX000"),

So XXF03 wouldn’t be good. I’d vote to keep 58F02.

PostgreSQL, for example, uses XX codes only for assertion-failure-type errors that “cannot happen”.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To us, yes sure, FDB is external, but FRL is not presented to the clients as something separable from FDB, especially in a future where FRL is deployed as a service.

@sergei-pustovykh sergei-pustovykh added Run mixed-mode Label to add to Pull Requests to have it run mixed mode tests breaking change Changes that are not backwards compatible labels Oct 28, 2026
@sergei-pustovykh sergei-pustovykh removed the Run mixed-mode Label to add to Pull Requests to have it run mixed mode tests label Oct 28, 2026

@hatyo hatyo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is fixing #3583, we're still getting different errors when running the query with a cold cache, vs. warm cache, unless I missed something.

}
default:
throw new RecordCoreException("unknown type of lucene query component");
throw new VerifyException("unknown type of lucene query component");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like an API contract change, the PR should probably be marked as breaking change, maybe @ohadzeliger could verify whether this change is ok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The switch here is exhaustive for getType(), so I was of the opinion that if this happens, it’s an internal/logic error. Hence the assertion/verify error instead of an application exception.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I’m not familiar how the LucenePlanner is actually used. Are these enum values coming straight from the user and untrustworthy?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also not familiar with this code, the change is probably fine, that's why I would rather have @ohadzeliger or @jjezra verify it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can I just revert this change to make the PR less vague?

}
default:
throw new RecordCoreException("Unsupported boolean query occur: " + booleanQuery);
throw new VerifyException("Unsupported boolean query occur: " + booleanQuery);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switch is exhaustive too, just MUST versus SHOULD.

fileDescriptorProtoSerializer.getFileBuilder().build(), dependencies.toArray(new Descriptors.FileDescriptor[0]));
} catch (Descriptors.DescriptorValidationException e) {
throw new RelationalException(ErrorCode.SERIALIZATION_FAILURE, e).toUncheckedWrappedException();
throw new RelationalException(ErrorCode.INVALID_TABLE_DEFINITION, e).toUncheckedWrappedException();

@hatyo hatyo Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear to me why we think this error code is more "descriptive" than SERIALIZATION_FAILURE. The caught exception e corresponds to failure to serializer the file descriptor of the schema template (and its dependencies) not a logical incorrect definition of a specific table. So a serialization failure from my perspective is more accurate.

Addendum: considering that SERIALIZATION_FAILURE falls in the category of transaction rollback handling, we could (should) probably use a different error code specific to the nature of the serialization failure (serialization keyword is a little bit overloaded here).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introduced new SCHEMA_TEMPLATE_SERIALIZATION_FAILURE("XXF02")

@robert-brunel robert-brunel Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question about XX versus the earlier class-42 INVALID_TABLE_DEFINITION is whether the user can trigger this case or not? If not, then XXF02 for an internal error would be fine. But then the question is whether it’s really worth it and useful having that code, as opposed to just XX000-with-message.

Arguments.of(wrapFdbError(FDBError.COMMIT_UNKNOWN_RESULT), ErrorCode.TRANSACTION_RESOLUTION_UNKNOWN),
Arguments.of(wrapFdbError(FDBError.CLUSTER_VERSION_CHANGED), ErrorCode.TRANSACTION_RESOLUTION_UNKNOWN),
Arguments.of(wrapFdbError(FDBError.NOT_COMMITTED), ErrorCode.SERIALIZATION_FAILURE),
Arguments.of(wrapFdbError(FDBError.TRANSACTION_TOO_OLD), ErrorCode.SERIALIZATION_FAILURE),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transaction too old error could be very useful when debugging, it would be beneficial to have an error code corresponding to it under class 40, something like 40F01. Same for the other errors below. SQL standard permits defining such specific error codes, that could convoy vendor-specific semantics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added TRANSACTION_TOO_OLD("40F01") and TRANSIENT_TRANSACTION_FAILURE("40F02")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note though that clients will often want to programmatically retry in all these conditions. So now, with three codes, they need to check for three codes in their code, instead of just SERIALIZATION_FAILURE.

The best way for them is then probably to just pattern-match the SQLSTATE for 40... in their retry logic. So the extra codes are probably fine to add, it’s just that it’s less obvious for clients how to write their code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is expected. Clients usually define their business logic based upon the class of the SQL error, the LSB of the SQLSTATE are usually vendor-specific and clients are not advised to dispatch on them if they want their logic to work across different vendors.

That is why we're extra careful about the error class (and implicitly so, the category), and we're free to define the subclass as we want.


// Class 58 - System Error
UNDEFINED_FILE("58F01"),
STORAGE_ERROR("58F02"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how we came to that conclusion, error category 58 corresponds to errors external to the database engine AFAIK, but FDB errors are internal to FRL, should we use a different category instead?

@sergei-pustovykh

Copy link
Copy Markdown
Contributor Author

I am not sure this is fixing #3583, we're still getting different errors when running the query with a cold cache, vs. warm cache, unless I missed something.

my fault, constructed wrong test for #3583. you are right, it does not fix the issue, just introduces new codes.
reverting deleted comment, adding comment to the issue.

@hatyo hatyo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@sergei-pustovykh
sergei-pustovykh merged commit bb3de21 into main Oct 29, 2026
14 checks passed
@sergei-pustovykh
sergei-pustovykh deleted the apple/sergei-pustovykh/error-codes branch October 29, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Changes that are not backwards compatible enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants