Search in sources :

Example 1 with IcebergView

use of org.projectnessie.model.IcebergView in project nessie by projectnessie.

the class TestNessieIcebergViews method testViewColumnComments.

@Test
public void testViewColumnComments() throws NessieNotFoundException {
    // update comment
    CommentUpdate commentUpdate = new CommentUpdate(catalog.getViewCatalog().newViewOps(VIEW_IDENTIFIER));
    String comment = "The column name is id";
    commentUpdate.updateColumnDoc("id", comment);
    Schema schema = commentUpdate.apply();
    assertThat(schema.findField("id").doc()).isEqualTo(comment);
    comment = comment + " and type is integer";
    commentUpdate.updateColumnDoc("id", comment);
    schema = commentUpdate.apply();
    assertThat(schema.findField("id").doc()).isEqualTo(comment);
    commentUpdate.commit();
    View icebergView = catalog.load(VIEW_IDENTIFIER.toString());
    assertThat(icebergView).isNotNull();
    assertThat(icebergView.currentVersion().versionId()).isEqualTo(2);
    assertThat(icebergView.currentVersion().parentId()).isEqualTo(1);
    assertThat(icebergView.properties()).isEmpty();
    assertThat(Paths.get(metadataLocationViews(VIEW_IDENTIFIER.name()))).exists();
    assertThat(metadataFilesForViews(VIEW_IDENTIFIER.name())).isNotNull().hasSize(2);
    verifyCommitMetadata();
    assertThat(api.getCommitLog().refName(BRANCH).get().getLogEntries()).hasSize(3);
    verifyViewInNessie(VIEW_IDENTIFIER, icebergView);
}
Also used : CommentUpdate(org.apache.iceberg.view.CommentUpdate) Schema(org.apache.iceberg.Schema) View(org.apache.iceberg.view.View) IcebergView(org.projectnessie.model.IcebergView) Test(org.junit.jupiter.api.Test)

Example 2 with IcebergView

use of org.projectnessie.model.IcebergView in project nessie by projectnessie.

the class TestNessieIcebergViews method verifyViewInNessie.

private void verifyViewInNessie(TableIdentifier viewIdentifier, View icebergView) throws NessieNotFoundException {
    ContentKey contentKey = ContentKey.of(viewIdentifier.toString().split("\\."));
    Map<ContentKey, Content> contentMap = api.getContent().key(contentKey).refName(BRANCH).get();
    assertThat(contentMap).hasSize(1).containsKey(contentKey);
    Content content = contentMap.get(contentKey);
    assertThat(content.unwrap(IcebergView.class)).isPresent();
    IcebergView view = content.unwrap(IcebergView.class).get();
    assertThat(metadataFilesForViewsPath(viewIdentifier.name())).contains(view.getMetadataLocation());
    // TODO: currently the schema id is always 0
    assertThat(view.getSchemaId()).isEqualTo(icebergView.currentVersion().viewDefinition().schema().schemaId());
    assertThat(view.getVersionId()).isEqualTo(view.getVersionId());
    assertThat(view.getSqlText()).isEqualTo(icebergView.currentVersion().viewDefinition().sql());
// TODO: currently not implemented in the view definition
// assertThat(view.getDialect()).isEqualTo(viewDefinition.dialect());
}
Also used : ContentKey(org.projectnessie.model.ContentKey) Content(org.projectnessie.model.Content) IcebergView(org.projectnessie.model.IcebergView)

Example 3 with IcebergView

use of org.projectnessie.model.IcebergView in project nessie by projectnessie.

the class TestNessieIcebergViews method testReplaceView.

@Test
public void testReplaceView() throws NessieNotFoundException {
    // add a column
    catalog.loadTable(TABLE_IDENTIFIER).updateSchema().addColumn("mother", Types.LongType.get()).commit();
    // update schema
    Schema schema = catalog.loadTable(TABLE_IDENTIFIER).schema();
    ViewDefinition updatedView = ViewDefinition.of(SQL, schema, CATALOG_NAME, new ArrayList<>());
    catalog.replace(VIEW_IDENTIFIER.toString(), updatedView, Collections.emptyMap());
    View icebergView = catalog.load(VIEW_IDENTIFIER.toString());
    assertThat(icebergView).isNotNull();
    assertThat(icebergView.currentVersion().versionId()).isEqualTo(2);
    assertThat(icebergView.currentVersion().parentId()).isEqualTo(1);
    assertThat(icebergView.currentVersion().viewDefinition()).isEqualTo(updatedView);
    assertThat(icebergView.properties()).isEmpty();
    assertThat(Paths.get(metadataLocationViews(VIEW_IDENTIFIER.name()))).exists();
    assertThat(metadataFilesForViews(VIEW_IDENTIFIER.name())).isNotNull().hasSize(2);
    verifyViewInNessie(VIEW_IDENTIFIER, icebergView);
    // update sql
    ViewDefinition updatedSql = ViewDefinition.of(SQL_ALTERED, schema, CATALOG_NAME, new ArrayList<>());
    catalog.replace(VIEW_IDENTIFIER.toString(), updatedSql, Collections.emptyMap());
    icebergView = catalog.load(VIEW_IDENTIFIER.toString());
    assertThat(icebergView).isNotNull();
    assertThat(icebergView.currentVersion().versionId()).isEqualTo(3);
    assertThat(icebergView.currentVersion().parentId()).isEqualTo(2);
    assertThat(icebergView.currentVersion().viewDefinition()).isEqualTo(updatedSql);
    assertThat(icebergView.properties()).isEmpty();
    assertThat(Paths.get(metadataLocationViews(VIEW_IDENTIFIER.name()))).exists();
    assertThat(metadataFilesForViews(VIEW_IDENTIFIER.name())).isNotNull().hasSize(3);
    verifyViewInNessie(VIEW_IDENTIFIER, icebergView);
    // update properties
    Map<String, String> properties = new HashMap<>();
    properties.put("prop1", "val1");
    properties.put("prop2", "val2");
    catalog.replace(VIEW_IDENTIFIER.toString(), ViewDefinition.of(SQL_ALTERED, schema, CATALOG_NAME, new ArrayList<>()), properties);
    icebergView = catalog.load(VIEW_IDENTIFIER.toString());
    assertThat(icebergView).isNotNull();
    assertThat(icebergView.currentVersion().versionId()).isEqualTo(4);
    assertThat(icebergView.currentVersion().parentId()).isEqualTo(3);
    assertThat(icebergView.currentVersion().viewDefinition()).isEqualTo(updatedSql);
    assertThat(icebergView.properties()).isEqualTo(properties);
    assertThat(Paths.get(metadataLocationViews(VIEW_IDENTIFIER.name()))).exists();
    assertThat(metadataFilesForViews(VIEW_IDENTIFIER.name())).isNotNull().hasSize(4);
    verifyCommitMetadata();
    assertThat(api.getCommitLog().refName(BRANCH).get().getLogEntries()).hasSize(6);
    verifyViewInNessie(VIEW_IDENTIFIER, icebergView);
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.iceberg.Schema) ViewDefinition(org.apache.iceberg.view.ViewDefinition) ArrayList(java.util.ArrayList) View(org.apache.iceberg.view.View) IcebergView(org.projectnessie.model.IcebergView) Test(org.junit.jupiter.api.Test)

Example 4 with IcebergView

use of org.projectnessie.model.IcebergView in project nessie by projectnessie.

the class TableCommitMetaStoreWorker method toStoreOnReferenceState.

@Override
public ByteString toStoreOnReferenceState(Content content) {
    ObjectTypes.Content.Builder builder = ObjectTypes.Content.newBuilder().setId(content.getId());
    if (content instanceof IcebergTable) {
        IcebergTable state = (IcebergTable) content;
        ObjectTypes.IcebergRefState.Builder stateBuilder = ObjectTypes.IcebergRefState.newBuilder().setSnapshotId(state.getSnapshotId()).setSchemaId(state.getSchemaId()).setSpecId(state.getSpecId()).setSortOrderId(state.getSortOrderId());
        builder.setIcebergRefState(stateBuilder);
    } else if (content instanceof IcebergView) {
        IcebergView view = (IcebergView) content;
        builder.setIcebergViewState(ObjectTypes.IcebergViewState.newBuilder().setVersionId(view.getVersionId()).setSchemaId(view.getSchemaId()).setDialect(view.getDialect()).setSqlText(view.getSqlText()));
    } else if (content instanceof DeltaLakeTable) {
        ObjectTypes.DeltaLakeTable.Builder table = ObjectTypes.DeltaLakeTable.newBuilder().addAllMetadataLocationHistory(((DeltaLakeTable) content).getMetadataLocationHistory()).addAllCheckpointLocationHistory(((DeltaLakeTable) content).getCheckpointLocationHistory());
        String lastCheckpoint = ((DeltaLakeTable) content).getLastCheckpoint();
        if (lastCheckpoint != null) {
            table.setLastCheckpoint(lastCheckpoint);
        }
        builder.setDeltaLakeTable(table);
    } else if (content instanceof Namespace) {
        builder.setNamespace(ObjectTypes.Namespace.newBuilder().setName(((Namespace) content).name()));
    } else {
        throw new IllegalArgumentException("Unknown type " + content);
    }
    return builder.build().toByteString();
}
Also used : ImmutableDeltaLakeTable(org.projectnessie.model.ImmutableDeltaLakeTable) DeltaLakeTable(org.projectnessie.model.DeltaLakeTable) Content(org.projectnessie.model.Content) ImmutableIcebergTable(org.projectnessie.model.ImmutableIcebergTable) IcebergTable(org.projectnessie.model.IcebergTable) ImmutableIcebergView(org.projectnessie.model.ImmutableIcebergView) IcebergView(org.projectnessie.model.IcebergView) ByteString(com.google.protobuf.ByteString) ImmutableNamespace(org.projectnessie.model.ImmutableNamespace) Namespace(org.projectnessie.model.Namespace)

Example 5 with IcebergView

use of org.projectnessie.model.IcebergView in project nessie by projectnessie.

the class TestStoreWorker method testSerdeIcebergView.

@Test
void testSerdeIcebergView() {
    String path = "foo/view";
    String dialect = "Dremio";
    String sqlText = "select * from world";
    IcebergView view = IcebergView.of(ID, path, 1, 123, dialect, sqlText);
    ObjectTypes.Content protoTableGlobal = ObjectTypes.Content.newBuilder().setId(ID).setIcebergMetadataPointer(IcebergMetadataPointer.newBuilder().setMetadataLocation(path)).build();
    ObjectTypes.Content protoOnRef = ObjectTypes.Content.newBuilder().setId(ID).setIcebergViewState(IcebergViewState.newBuilder().setVersionId(1).setDialect(dialect).setSchemaId(123).setSqlText(sqlText)).build();
    ByteString tableGlobalBytes = worker.toStoreGlobalState(view);
    ByteString snapshotBytes = worker.toStoreOnReferenceState(view);
    Assertions.assertEquals(protoTableGlobal.toByteString(), tableGlobalBytes);
    Assertions.assertEquals(protoOnRef.toByteString(), snapshotBytes);
    Content deserialized = worker.valueFromStore(snapshotBytes, Optional.of(tableGlobalBytes));
    Assertions.assertEquals(view, deserialized);
}
Also used : ByteString(com.google.protobuf.ByteString) Content(org.projectnessie.model.Content) IcebergView(org.projectnessie.model.IcebergView) ObjectTypes(org.projectnessie.store.ObjectTypes) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

IcebergView (org.projectnessie.model.IcebergView)11 Content (org.projectnessie.model.Content)6 ImmutableIcebergView (org.projectnessie.model.ImmutableIcebergView)5 Test (org.junit.jupiter.api.Test)4 Branch (org.projectnessie.model.Branch)4 ContentKey (org.projectnessie.model.ContentKey)4 IcebergTable (org.projectnessie.model.IcebergTable)4 View (org.apache.iceberg.view.View)3 ImmutableIcebergTable (org.projectnessie.model.ImmutableIcebergTable)3 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 Schema (org.apache.iceberg.Schema)2 TableIdentifier (org.apache.iceberg.catalog.TableIdentifier)2 CommitFailedException (org.apache.iceberg.exceptions.CommitFailedException)2 CommitStateUnknownException (org.apache.iceberg.exceptions.CommitStateUnknownException)2 ViewDefinition (org.apache.iceberg.view.ViewDefinition)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 CommitMultipleOperationsBuilder (org.projectnessie.client.api.CommitMultipleOperationsBuilder)2 NessieApiV1 (org.projectnessie.client.api.NessieApiV1)2 HttpClientException (org.projectnessie.client.http.HttpClientException)2