Skip to content

API: Introduce a builder in Catalog - #1409

Merged
aokolnychyi merged 2 commits into
apache:masterfrom
aokolnychyi:catalog-builder
Sep 3, 2020
Merged

aokolnychyi merged 2 commits into
apache:masterfrom
aokolnychyi:catalog-builder

Conversation

@aokolnychyi

Copy link
Copy Markdown
Contributor

This PR introduces a builder in Catalog to avoid duplicating the number of methods each time we introduce a new param.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

@rdblue @RussellSpitzer @rdsr this is the builder we discussed here.

@rdblue

rdblue commented Sep 1, 2020

Copy link
Copy Markdown
Contributor

I think there's a slight problem with the approach that this uses. This creates a builder that interacts with the public Catalog API. That's great for users of the API because it is easier to create tables using the builder. But it doesn't help implementers of the API or handle the problem that we keep adding new variations of createTable when we add new table metadata.

Since the builder ends up calling createTable with all of the metadata options it has accumulated, when we need to add a new one, like distribute by for example, we will still need to create another variant of createTable and update the builder to call it. So we will still accumulate more methods in the public API.

Ideally, the builder would create TableMetadata and pass it into the implementation. The problem is that TableMetadata is not an API class. So if we wanted to do that, we wouldn't be able to provide the builder implementation in the API. We would have to add an interface for the builder that is implemented in some BaseCatalog in iceberg-core. That's a bit more work, but I think it would help in the long term. What do you think @aokolnychyi?

@jacques-n

Copy link
Copy Markdown

Random aside, have you guys considered using Immutables?

We've started to use it extensively other places as it does a nice job of several things around pojo and builders. It's also nice as it is an annotation processor and creates no runtime dependency (but still produces inspectable code, as opposed to something like lombok, which is pretty much black box)

* <p>
* Call {@link #buildTable(TableIdentifier, Schema)} to create a new builder.
*/
interface TableBuilder {

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'll need to fix CachingCatalog.

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.

Also, we will need to add extra validation to the builder in HadoopCatalog.

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.

Okay, I added validation for HadoopCatalog but we can add support for buildTable in CachingCatalog later. Looks like we will need a separate builder implementation that would delegate to another builder but add caching callbacks.

* @return the builder to create a table or start a create/replace transaction
*/
default TableBuilder buildTable(TableIdentifier identifier, Schema schema) {
throw new UnsupportedOperationException(this.getClass().getName() + " does not implement buildTable");

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 am throwing UnsupportedOperationException to avoid breaking custom catalogs.

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.

Sounds good.

PartitionSpec spec,
String location,
Map<String, String> properties) {
Preconditions.checkArgument(isValidIdentifier(identifier), "Invalid table identifier: %s", identifier);

@aokolnychyi aokolnychyi Sep 3, 2020 •

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 moved this logic to the builder and use the builder here.

private String location = null;

protected BaseMetastoreCatalogTableBuilder(TableIdentifier identifier, Schema schema) {
Preconditions.checkArgument(isValidIdentifier(identifier), "Invalid table identifier: %s", identifier);

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.

We did not validate this for replace transactions before but there should be no harm.

}

@Override
public Table createTable(

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.

This is now validated in the builder.

@rdblue

rdblue commented Sep 3, 2020

Copy link
Copy Markdown
Contributor

+1 when conflicts are addressed and tests are passing.

@RussellSpitzer RussellSpitzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor Question on null handling, but this looks much cleaner to me than before!


@Override
public TableBuilder withPartitionSpec(PartitionSpec newSpec) {
if (spec != null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why allow null here? We used to allow null for the parameter because you had to pass it, but now you don't have to pass it so maybe we should reject null?


@Override
public TableBuilder withProperties(Map<String, String> properties) {
if (properties != null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same null question here

@aokolnychyi aokolnychyi Sep 3, 2020 •

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.

We used to have this because overloaded createTable methods pass null for props:

Map<String, String> tableProperties = properties != null ? properties : Maps.newHashMap();

Since we are using the builder for the root createTable now, I added handling here too. For partition spec, it is optional but probably makes sense for consistency.

@aokolnychyi

Copy link
Copy Markdown
Contributor Author

@jacques-n, I haven't seen immutables before. How is your impression? Is the naming of generated methods fixed?

@jacques-n

Copy link
Copy Markdown

@jacques-n, I haven't seen immutables before. How is your impression? Is the naming of generated methods fixed?

I'm not sure whether you mean "fixed" as in broken or "fixed" as in not-customizable. If the former, I'm not sure what you're referring to. If the latter, yes there is some control to customize the method names:
https://immutables.github.io/style.html

We find that it is relatively painless to use, has good IDE integration and removes a lot of boilerplate code. Allows us to do a better job of creating domain specific model classes in different layers rather than over-sharing them.

@aokolnychyi
aokolnychyi merged commit 594f461 into apache:master Sep 3, 2020
@rdblue rdblue added this to the Java 0.10.0 Release milestone Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants