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

binding: typechecks #143

Merged
merged 33 commits into from
Oct 2, 2024
Merged

binding: typechecks #143

merged 33 commits into from
Oct 2, 2024

Conversation

muzarski
Copy link
Collaborator

@muzarski muzarski commented Jul 18, 2024

Fix: #142

Motivation

Right now, cpp-rust-driver does not perform any typechecks during binding. This PR changes that and implements a typecheck logic based on cpp-driver's logic.

Why not just use a builtin rust-driver's typechecks mechanism?

The reason for this is that cpp-driver has a bit different language (cpp/rust) to CQL type mapping. A simple example is an int64_t/i64 value which in cpp-driver can be mapped to 4 different CQL types.

Other reason is that cpp-driver allows users to define untyped tuples/collections. For such objects, the typechecks are skipped.

Cpp-driver performs typechecks during binding - we will follow the same approach. It is much convenient and cleaner to handle untyped objects this way.

Implemented API functions

  • cass_collection_new_from_data_type -> allows user to create a TYPED collection. We also adjusted the cass_collection_new logic - this function is responsible for creating an UNTYPED version of a collection.
  • cass_collection_data_type -> returns a data type representing a collection. Since collections now hold info about the data types, this function was very trivial to implement.

Unit tests

I implemented unit tests for simple and complex (udt, tuples, collections) types. Please make sure that all edge cases are covered.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have implemented Rust unit tests for the features/changes introduced.
  • [ ] I have enabled appropriate tests in .github/workflows/build.yml in gtest_filter.
  • [ ] I have enabled appropriate tests in .github/workflows/cassandra.yml in gtest_filter

@muzarski
Copy link
Collaborator Author

v1.1: Added a link to cpp-driver's UDT typecheck logic in a comment.

@Lorak-mmk Lorak-mmk self-assigned this Jul 30, 2024
@roydahan roydahan assigned Lorak-mmk and unassigned Lorak-mmk Jul 30, 2024
@Lorak-mmk Lorak-mmk removed their assignment Jul 31, 2024
@roydahan roydahan requested review from roydahan and removed request for roydahan July 31, 2024 12:56
scylla-rust-wrapper/src/value.rs Show resolved Hide resolved
scylla-rust-wrapper/src/cass_types.rs Outdated Show resolved Hide resolved
scylla-rust-wrapper/src/cass_types.rs Outdated Show resolved Hide resolved
scylla-rust-wrapper/src/cass_types.rs Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
scylla-rust-wrapper/src/collection.rs Outdated Show resolved Hide resolved
@muzarski
Copy link
Collaborator Author

muzarski commented Aug 1, 2024

v1.2: rebased on master

@muzarski
Copy link
Collaborator Author

muzarski commented Aug 1, 2024

v2: addressed some review comments

What should we do with the custom types? @Lorak-mmk

@muzarski muzarski requested a review from Lorak-mmk August 1, 2024 13:19
@Lorak-mmk
Copy link
Collaborator

I think we should either support them fully (which would require some changes in Rust Driver, probably not big ones) or not support them at all - remove them from enums, return errors as early as possible when encountering them somewhere. WDYT @wprzytula ?

@muzarski
Copy link
Collaborator Author

muzarski commented Aug 5, 2024

Rebased on master (duration)

@muzarski
Copy link
Collaborator Author

muzarski commented Aug 6, 2024

Rebased on master (decimal)

cpp-driver allows to bind bytes to custom values as well,
but we do not plan to support custom values in cpp-rust-driver.
Note: I believe that I previously mentioned that
cpp-driver maps `CassDecimal` to varint as well, but this is not true.
This method will be used by `value::is_type_compatible` for compound types,
i.e. tuples, collections and udts. Corresponding CassCqlValue variants
will hold info about the DataType of value. To perform the typecheck,
we will simply compare two types - type of the value with the type provided
to `is_type_compatible` function.
Added information about data type of a collection.
This will be needed to implement typechecks.

In addition, we can implement two missing API functions for collections:
- cass_collection_new_from_data_type -> create a typed collection
- cass_collection_data_type -> return a data type of a collection
Until now, we would not hold the information about column data types
from PreparedMetadata. We need to hold this information to perform
a typecheck during binding values to statement.

We could construct the data types from column specs each time
we bind a value. However, CassDataType might be a heavy nested
object, and so I decided to cache it in CassPrepared.
@muzarski
Copy link
Collaborator Author

muzarski commented Oct 1, 2024

v4: addressed @wprzytula comments. Thanks for spotting the bug! I introduced a lot of additional test cases which consider more combinations for typechecks

It's not possible for a map type to have value type defined,
when key type is not defined. A representation of map type before this commit, would
technically allow us to create such state.

Introduced a `MapDataType` enum which represents valid states of map data type.
@wprzytula wprzytula merged commit af43106 into scylladb:master Oct 2, 2024
7 checks passed
@muzarski muzarski mentioned this pull request Oct 3, 2024
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.

binding: implement typechecks logic
4 participants