Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crosscluster/logical: handle user-defined types in SQL mode #133280

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Oct 23, 2024

This PR also includes a cleanup refactor:

importer,crosscluster: move type resolver to a different package

Since we're using this type resolver for more than just IMPORT, it makes
more sense to put it in a more fitting package.


crosscluster/logical: handle user-defined types in SQL mode

Previously, using a user-defined type in SQL mode would fail because of
a low level check in the execution engine that verifies that the input
datum has the same type OID as the destination column.

This does not work for LDR, since the input datums come directly from a
source table in a different cluster, so user-defined types have
different OIDs.

This fixes it by setting the datum type to "unknown" before executing
the insert query. The "unknown" type is what is normally used when any
SQL statement is sent to CRDB without explicit type annotations/hints.
When the execution engine sees this type, it will perform an automatic
(and cheap) immutable assignment cast to change the datum to the
appropriate type.

fixes #132164
Release note (ops change): Logical replication streams that reference
tables with user-defined types can now be created with the mode = immediate option.

@rafiss rafiss added the backport-24.3.x Flags PRs that need to be backported to 24.3 label Oct 23, 2024
@rafiss rafiss requested a review from msbutler October 23, 2024 18:31
@rafiss rafiss requested review from a team as code owners October 23, 2024 18:32
@rafiss rafiss requested review from mgartner and removed request for a team October 23, 2024 18:32
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss force-pushed the ldr-udt-hydration branch from 84b7479 to 9b8ac45 Compare October 23, 2024 18:38
Copy link
Collaborator

@msbutler msbutler left a comment

Choose a reason for hiding this comment

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

Amazing! this fix is much lower impact than what we originally proposed.

@@ -211,16 +210,9 @@ func createLogicalReplicationStreamPlanHook(

sourceTypes := make([]*descpb.TypeDescriptor, len(spec.TypeDescriptors))
for i, desc := range spec.TypeDescriptors {
// Until https://github.com/cockroachdb/cockroach/issues/132164 is resolved,
// we cannot allow user-defined types on the SQL ingestion path.
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think this little block of code should be in the previous commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@@ -92,6 +92,9 @@ func (q *queryBuilder) AddRow(row cdcevent.Row) error {
}
if err := it.Datum(func(d tree.Datum, col cdcevent.ResultColumn) error {
if dEnum, ok := d.(*tree.DEnum); ok {
// Override the type to Unknown to avoid a mismatched type OID error
// during execution. Note that Unknown is the type used by default
Copy link
Collaborator

Choose a reason for hiding this comment

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

the diff in this file should be in the previous commit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Previously, using a user-defined type in SQL mode would fail because of
a low level check in the execution engine that verifies that the input
datum has the same type OID as the destination column.

This does not work for LDR, since the input datums come directly from a
source table in a different cluster, so user-defined types have
different OIDs.

This fixes it by setting the datum type to "unknown" before executing
the insert query. The "unknown" type is what is normally used when any
SQL statement is sent to CRDB without explicit type annotations/hints.
When the execution engine sees this type, it will perform an automatic
(and cheap) immutable assignment cast to change the datum to the
appropriate type.

Release note (ops change): Logical replication streams that reference
tables with user-defined types can now be created with the `mode =
immediate` option.
Since we're using this type resolver for more than just IMPORT, it makes
more sense to put it in a more fitting package.

Release note: None
@rafiss rafiss force-pushed the ldr-udt-hydration branch from 9b8ac45 to 579ecef Compare October 23, 2024 19:21
Copy link
Collaborator Author

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

tftr!

bors r+

@craig craig bot merged commit 23adad6 into cockroachdb:master Oct 23, 2024
22 of 23 checks passed
@rafiss rafiss deleted the ldr-udt-hydration branch October 24, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-24.3.x Flags PRs that need to be backported to 24.3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

eval: create TypeResolver that can hydrate a Table with foreign type descriptors
3 participants