-
Notifications
You must be signed in to change notification settings - Fork 3k
API, Flink: Correct Handling of Table Names Containing Dots #14932
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
Open
peach12345
wants to merge
13
commits into
apache:main
Choose a base branch
from
peach12345:tableLoader-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−7
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1e953c0
fix: use TableIdentifier instead of saving identifier as a string
peach12345 c897e25
feat: add serialVersionUUID to TableIdentifier.java
peach12345 3e9e8a6
fix: fix code format
peach12345 901cd04
fix: fix code format
peach12345 74aab7c
test: add test for CatalogTableLoader class
peach12345 2921765
style: format code
peach12345 c15a52f
docs: add license header
peach12345 20bafe9
style: fix format
peach12345 82381c4
style: fix format
peach12345 7b50647
style: fix format
peach12345 b9c0cc2
style: please linter
peach12345 b5af1d4
style: please linter
peach12345 c94a70c
style: please linter
peach12345 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
flink/v2.1/flink/src/test/java/org/apache/iceberg/flink/CatalogTableLoaderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.iceberg.flink; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be related directly to Flink, but rather directly to Iceberg core.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ignore. Will rewrite those tests. |
||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.Closeable; | ||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.io.Serializable; | ||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Proxy; | ||
| import org.apache.iceberg.Table; | ||
| import org.apache.iceberg.catalog.Catalog; | ||
| import org.apache.iceberg.catalog.TableIdentifier; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class CatalogTableLoaderTest { | ||
|
|
||
| private static final TableIdentifier IDENTIFIER = TableIdentifier.of("db", "tbl"); | ||
|
|
||
| // Simple serializable CatalogLoader that creates a proxy Catalog on each loadCatalog() call. | ||
| static class SerializableCatalogLoader | ||
| implements org.apache.iceberg.flink.CatalogLoader, Serializable { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| @Override | ||
| public Catalog loadCatalog() { | ||
| // Create a simple Table proxy object | ||
| Table tableProxy = | ||
| (Table) | ||
| Proxy.newProxyInstance( | ||
| Table.class.getClassLoader(), | ||
| new Class[] {Table.class}, | ||
| new InvocationHandler() { | ||
| @Override | ||
| public Object invoke(Object proxy, Method method, Object[] args) { | ||
| // For tests the Table object does not need to implement methods | ||
| if (method.getName().equals("toString")) { | ||
| return "fake-table"; | ||
| } | ||
| return null; | ||
| } | ||
| }); | ||
|
|
||
| // Create a Catalog proxy that returns loadTable(identifier) and is Closeable | ||
| InvocationHandler handler = | ||
| new InvocationHandler() { | ||
| private boolean closed = false; | ||
|
|
||
| @Override | ||
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
| String name = method.getName(); | ||
| if ("loadTable".equals(name)) { | ||
| return tableProxy; | ||
| } else if ("close".equals(name)) { | ||
| closed = true; | ||
| return null; | ||
| } else if ("toString".equals(name)) { | ||
| return "fake-catalog"; | ||
| } | ||
| throw new UnsupportedOperationException("Not implemented in test proxy: " + name); | ||
| } | ||
| }; | ||
|
|
||
| return (Catalog) | ||
| Proxy.newProxyInstance( | ||
| Catalog.class.getClassLoader(), | ||
| new Class[] {Catalog.class, Closeable.class}, | ||
| handler); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings({"checkstyle:NoClone", "checkstyle:SuperClone"}) | ||
| public org.apache.iceberg.flink.CatalogLoader clone() { | ||
| // A new instance is sufficient for tests | ||
| return new SerializableCatalogLoader(); | ||
| } | ||
| } | ||
|
|
||
| private static <T extends Serializable> T roundTripSerialize(T obj) throws Exception { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { | ||
| oos.writeObject(obj); | ||
| } | ||
| byte[] bytes = baos.toByteArray(); | ||
| try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) { | ||
| @SuppressWarnings("unchecked") | ||
| T deserialized = (T) ois.readObject(); | ||
| return deserialized; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testOpenLoadClose() throws Exception { | ||
| CatalogLoader catalogLoader = new SerializableCatalogLoader(); | ||
| TableLoader loader = TableLoader.fromCatalog(catalogLoader, IDENTIFIER); | ||
|
|
||
| // initially closed | ||
| assertThat(loader.isOpen()).isFalse(); | ||
|
|
||
| // open and load | ||
| loader.open(); | ||
| assertThat(loader.isOpen()).isTrue(); | ||
| Table table = loader.loadTable(); | ||
| assertThat(table).isNotNull(); | ||
| assertThat(table).hasToString("fake-table"); | ||
|
|
||
| // close | ||
| loader.close(); | ||
| assertThat(loader.isOpen()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSerializationKeepsLoaderFunctional() throws Exception { | ||
| org.apache.iceberg.flink.CatalogLoader catalogLoader = new SerializableCatalogLoader(); | ||
| TableLoader original = TableLoader.fromCatalog(catalogLoader, IDENTIFIER); | ||
|
|
||
| // serialize / deserialize the TableLoader | ||
| TableLoader deserialized = roundTripSerialize(original); | ||
|
|
||
| // should still work after deserialization | ||
| assertThat(deserialized.isOpen()).isFalse(); | ||
| deserialized.open(); | ||
| assertThat(deserialized.isOpen()).isTrue(); | ||
| Table table = deserialized.loadTable(); | ||
| assertThat(table).isNotNull(); | ||
| assertThat(table).hasToString("fake-table"); | ||
| deserialized.close(); | ||
| assertThat(deserialized.isOpen()).isFalse(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does making this class Serializable really fix the root cause? It looks like the
parse(String)method would have to have a different parsing logic to respect escaped dots.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The root cause was the following line:
this.identifier = tableIdentifier.toString();
Instead of storing the string value, I stored the TableIdentifier object itself. As a result, the TableIdentifier class needs to implement Serializable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, but wouldn't
TableIdentifier.parse(tableName)still suffer from the same issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you’re right. When using the parse function, dots are treated as separators.
Maybe parsing isn’t the best approach in this case. Would it make more sense to work with the complete object instead? Would of course be a breaking change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the information about which part of the string represents the namespace is lost.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parsing function is, of course, working as intended.
However, in the CatalogTableLoader class, when you pass in a TableIdentifier, the identifier is stored as a string:
Later, when calling the loadTable function, that string is parsed again. This fails when the table name contains dots:
That’s why storing the full TableIdentifier object instead of its string representation makes more sense.
Fixing the parsing function to properly handle dots would also be a valid approach, but in my experience, relying on parsing often leads to subtle errors or bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mxm what do you think? or should be change the parsing to logic to make it more robust and support dots in table names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think fixing the parsing logic would be preferable for me. There are many cases where we dynamically need to parse a table name and we don't want to rely on Java serialization in the process.
Making Namespace / TableIdentifier Serializable could be an addition. What do you think about the Serializable change @pvary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mxm for the answer.
If we only change the parsing logic, I don’t think we need the additional Serializable parts. I’ll give it a try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The iceberg-go library uses a special ASCII separator charactor for this. Maybe we could do something similar?
https://github.com/apache/iceberg-go/blob/7cfbf238ee7f6159af86a30f90e78097f2caae8b/catalog/rest/rest.go#L64