-
Notifications
You must be signed in to change notification settings - Fork 383
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
[#4062] feat(iceberg-rest-server): support RegisterTable for Iceberg REST server #4088
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import org.apache.gravitino.iceberg.common.IcebergCatalogBackend; | ||
import org.apache.iceberg.exceptions.BadRequestException; | ||
import org.apache.iceberg.exceptions.ServiceFailureException; | ||
import org.apache.iceberg.exceptions.ValidationException; | ||
import org.apache.spark.sql.AnalysisException; | ||
import org.apache.spark.sql.catalyst.analysis.NamespaceAlreadyExistsException; | ||
|
@@ -262,13 +260,8 @@ void testRenameTable() { | |
"CREATE TABLE iceberg_rest_table_test.rename_foo1" | ||
+ "(id bigint COMMENT 'unique id',data string) using iceberg"); | ||
|
||
Class exception = | ||
catalogType == IcebergCatalogBackend.HIVE | ||
? ServiceFailureException.class | ||
: TableAlreadyExistsException.class; | ||
|
||
Assertions.assertThrowsExactly( | ||
exception, | ||
TableAlreadyExistsException.class, | ||
() -> | ||
sql( | ||
"ALTER TABLE iceberg_rest_table_test.rename_foo2 " | ||
|
@@ -526,4 +519,42 @@ void testSnapshot() { | |
convertToStringMap(sql("select * from iceberg_rest_table_test.snapshot_foo1")); | ||
Assertions.assertEquals(ImmutableMap.of("1", "a", "2", "b"), result); | ||
} | ||
|
||
@Test | ||
@EnabledIf("catalogTypeNotMemory") | ||
void testRegisterTable() { | ||
String registerDB = "iceberg_register_db"; | ||
String registerTableName = "register_foo1"; | ||
sql("CREATE DATABASE " + registerDB); | ||
sql( | ||
String.format( | ||
"CREATE TABLE %s.%s (id bigint COMMENT 'unique id',data string) using iceberg", | ||
registerDB, registerTableName)); | ||
sql(String.format("INSERT INTO %s.%s VALUES (1, 'a')", registerDB, registerTableName)); | ||
|
||
// get metadata location | ||
List<String> metadataLocations = | ||
convertToStringList( | ||
sql( | ||
String.format( | ||
"select file from %s.%s.metadata_log_entries", registerDB, registerTableName)), | ||
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. Can you please make all the SQL keywords uppercase. 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. done |
||
0); | ||
String metadataLocation = metadataLocations.get(metadataLocations.size() - 1); | ||
|
||
// register table | ||
String register = | ||
String.format( | ||
"CALL rest.system.register_table(table => 'iceberg_rest_table_test.register_foo2', metadata_file=> '%s')", | ||
metadataLocation); | ||
sql(register); | ||
|
||
Map<String, String> result = | ||
convertToStringMap(sql("select * from iceberg_rest_table_test.register_foo2")); | ||
Assertions.assertEquals(ImmutableMap.of("1", "a"), result); | ||
|
||
// insert other data | ||
sql(" INSERT INTO iceberg_rest_table_test.register_foo2 VALUES (2, 'b')"); | ||
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. Remove the whitespace before "INSERT". 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. done |
||
result = convertToStringMap(sql("select * from iceberg_rest_table_test.register_foo2")); | ||
Assertions.assertEquals(ImmutableMap.of("1", "a", "2", "b"), result); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.gravitino.iceberg.service.rest; | ||
|
||
import org.apache.gravitino.iceberg.common.ops.IcebergTableOps; | ||
import org.apache.iceberg.PartitionSpec; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.TableMetadata; | ||
import org.apache.iceberg.catalog.Namespace; | ||
import org.apache.iceberg.exceptions.AlreadyExistsException; | ||
import org.apache.iceberg.rest.requests.RegisterTableRequest; | ||
import org.apache.iceberg.rest.responses.LoadTableResponse; | ||
import org.apache.iceberg.types.Types.NestedField; | ||
import org.apache.iceberg.types.Types.StringType; | ||
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap; | ||
|
||
public class IcebergTableOpsForTest extends IcebergTableOps { | ||
@Override | ||
public LoadTableResponse registerTable(Namespace namespace, RegisterTableRequest request) { | ||
if (request.name().contains("fail")) { | ||
throw new AlreadyExistsException("Already exits exception for test"); | ||
} | ||
|
||
Schema mockSchema = new Schema(NestedField.of(1, false, "foo_string", StringType.get())); | ||
TableMetadata tableMetadata = | ||
TableMetadata.newTableMetadata( | ||
mockSchema, PartitionSpec.unpartitioned(), "/mock", ImmutableMap.of()); | ||
LoadTableResponse loadTableResponse = | ||
LoadTableResponse.builder() | ||
.withTableMetadata(tableMetadata) | ||
.addAllConfig(ImmutableMap.of()) | ||
.build(); | ||
return loadTableResponse; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
package org.apache.gravitino.iceberg.service.rest; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
@@ -29,10 +30,10 @@ | |
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
import org.apache.iceberg.MetadataUpdate; | ||
import org.apache.iceberg.MetadataUpdate.AddSchema; | ||
import org.apache.iceberg.MetadataUpdate.SetCurrentSchema; | ||
import org.apache.iceberg.Schema; | ||
import org.apache.iceberg.TableMetadata; | ||
import org.apache.iceberg.UpdateRequirement; | ||
import org.apache.iceberg.UpdateRequirements; | ||
import org.apache.iceberg.catalog.Namespace; | ||
import org.apache.iceberg.catalog.TableIdentifier; | ||
import org.apache.iceberg.metrics.CommitReport; | ||
|
@@ -107,10 +108,10 @@ private Response doLoadTable(String name) { | |
} | ||
|
||
private Response doUpdateTable(String name, TableMetadata base) { | ||
MetadataUpdate addSchema = new AddSchema(newTableSchema, base.lastColumnId()); | ||
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. API changes for new Iceberg Version |
||
MetadataUpdate setCurrentSchema = new SetCurrentSchema(1); | ||
UpdateTableRequest updateTableRequest = | ||
UpdateTableRequest.builderFor(base).update(addSchema).update(setCurrentSchema).build(); | ||
TableMetadata newMetadata = base.updateSchema(newTableSchema, base.lastColumnId()); | ||
List<MetadataUpdate> metadataUpdates = newMetadata.changes(); | ||
List<UpdateRequirement> requirements = UpdateRequirements.forUpdateTable(base, metadataUpdates); | ||
UpdateTableRequest updateTableRequest = new UpdateTableRequest(requirements, metadataUpdates); | ||
return getTableClientBuilder(Optional.of(name)) | ||
.post(Entity.entity(updateTableRequest, MediaType.APPLICATION_JSON_TYPE)); | ||
} | ||
|
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 new Iceberg version will check the correctness of jdbc uri