Search in sources :

Example 11 with Reference

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

the class AbstractRestDiff method testDiff.

@ParameterizedTest
@MethodSource("diffRefModes")
public void testDiff(ReferenceMode refModeFrom, ReferenceMode refModeTo) throws BaseNessieClientServerException {
    int commitsPerBranch = 10;
    Reference fromRef = getApi().createReference().reference(Branch.of("testDiffFromRef", null)).create();
    Reference toRef = getApi().createReference().reference(Branch.of("testDiffToRef", null)).create();
    String toRefHash = createCommits(toRef, 1, commitsPerBranch, toRef.getHash());
    toRef = Branch.of(toRef.getName(), toRefHash);
    List<DiffEntry> diffOnRefHeadResponse = getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRef)).get().getDiffs();
    // we only committed to toRef, the "from" diff should be null
    assertThat(diffOnRefHeadResponse).hasSize(commitsPerBranch).allSatisfy(diff -> {
        assertThat(diff.getKey()).isNotNull();
        assertThat(diff.getFrom()).isNull();
        assertThat(diff.getTo()).isNotNull();
    });
    // Some combinations with explicit fromHashOnRef/toHashOnRef
    assertThat(getApi().getDiff().fromRefName(fromRef.getName()).fromHashOnRef(fromRef.getHash()).toRefName(toRef.getName()).toHashOnRef(toRef.getHash()).get().getDiffs()).isEqualTo(diffOnRefHeadResponse);
    // result
    if (refModeTo != ReferenceMode.NAME_ONLY) {
        Branch toRefAtFrom = Branch.of(toRef.getName(), fromRef.getHash());
        assertThat(getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRefAtFrom)).get().getDiffs()).isEmpty();
    }
    // after committing to fromRef, "from/to" diffs should both have data
    fromRef = Branch.of(fromRef.getName(), createCommits(fromRef, 1, commitsPerBranch, fromRef.getHash()));
    assertThat(getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRef)).get().getDiffs()).hasSize(commitsPerBranch).allSatisfy(diff -> {
        assertThat(diff.getKey()).isNotNull();
        assertThat(diff.getFrom()).isNotNull();
        assertThat(diff.getTo()).isNotNull();
        // we only have a diff on the ID
        assertThat(diff.getFrom().getId()).isNotEqualTo(diff.getTo().getId());
        Optional<IcebergTable> fromTable = diff.getFrom().unwrap(IcebergTable.class);
        assertThat(fromTable).isPresent();
        Optional<IcebergTable> toTable = diff.getTo().unwrap(IcebergTable.class);
        assertThat(toTable).isPresent();
        assertThat(fromTable.get().getMetadataLocation()).isEqualTo(toTable.get().getMetadataLocation());
        assertThat(fromTable.get().getSchemaId()).isEqualTo(toTable.get().getSchemaId());
        assertThat(fromTable.get().getSnapshotId()).isEqualTo(toTable.get().getSnapshotId());
        assertThat(fromTable.get().getSortOrderId()).isEqualTo(toTable.get().getSortOrderId());
        assertThat(fromTable.get().getSpecId()).isEqualTo(toTable.get().getSpecId());
    });
    List<ContentKey> keys = IntStream.rangeClosed(0, commitsPerBranch).mapToObj(i -> ContentKey.of("table" + i)).collect(Collectors.toList());
    // request all keys and delete the tables for them on toRef
    Map<ContentKey, Content> map = getApi().getContent().refName(toRef.getName()).keys(keys).get();
    for (Map.Entry<ContentKey, Content> entry : map.entrySet()) {
        toRef = getApi().commitMultipleOperations().branchName(toRef.getName()).hash(toRefHash).commitMeta(CommitMeta.fromMessage("delete")).operation(Delete.of(entry.getKey())).commit();
    }
    // now that we deleted all tables on toRef, the diff for "to" should be null
    assertThat(getApi().getDiff().fromRef(refModeFrom.transform(fromRef)).toRef(refModeTo.transform(toRef)).get().getDiffs()).hasSize(commitsPerBranch).allSatisfy(diff -> {
        assertThat(diff.getKey()).isNotNull();
        assertThat(diff.getFrom()).isNotNull();
        assertThat(diff.getTo()).isNull();
    });
}
Also used : IntStream(java.util.stream.IntStream) DiffEntry(org.projectnessie.model.DiffResponse.DiffEntry) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Branch(org.projectnessie.model.Branch) Collectors(java.util.stream.Collectors) Reference(org.projectnessie.model.Reference) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Delete(org.projectnessie.model.Operation.Delete) IcebergTable(org.projectnessie.model.IcebergTable) Map(java.util.Map) Optional(java.util.Optional) BaseNessieClientServerException(org.projectnessie.error.BaseNessieClientServerException) Content(org.projectnessie.model.Content) ContentKey(org.projectnessie.model.ContentKey) CommitMeta(org.projectnessie.model.CommitMeta) MethodSource(org.junit.jupiter.params.provider.MethodSource) Reference(org.projectnessie.model.Reference) ContentKey(org.projectnessie.model.ContentKey) Branch(org.projectnessie.model.Branch) Content(org.projectnessie.model.Content) IcebergTable(org.projectnessie.model.IcebergTable) Map(java.util.Map) DiffEntry(org.projectnessie.model.DiffResponse.DiffEntry) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with Reference

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

the class AbstractRestAssign method testAssignRefToFreshMain.

/**
 * Assigning a branch/tag to a fresh main without any commits didn't work in 0.9.2
 */
@ParameterizedTest
@EnumSource(ReferenceMode.class)
public void testAssignRefToFreshMain(ReferenceMode refMode) throws BaseNessieClientServerException {
    Reference main = getApi().getReference().refName("main").get();
    // make sure main doesn't have any commits
    LogResponse log = getApi().getCommitLog().refName(main.getName()).get();
    assertThat(log.getLogEntries()).isEmpty();
    Branch testBranch = createBranch("testBranch");
    getApi().assignBranch().branch(testBranch).assignTo(main).assign();
    Reference testBranchRef = getApi().getReference().refName(testBranch.getName()).get();
    assertThat(testBranchRef.getHash()).isEqualTo(main.getHash());
    String testTag = "testTag";
    Reference testTagRef = getApi().createReference().sourceRefName(main.getName()).reference(Tag.of(testTag, main.getHash())).create();
    assertThat(testTagRef.getHash()).isNotNull();
    getApi().assignTag().hash(testTagRef.getHash()).tagName(testTag).assignTo(refMode.transform(main)).assign();
    testTagRef = getApi().getReference().refName(testTag).get();
    assertThat(testTagRef.getHash()).isEqualTo(main.getHash());
}
Also used : LogResponse(org.projectnessie.model.LogResponse) Reference(org.projectnessie.model.Reference) Branch(org.projectnessie.model.Branch) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with Reference

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

the class AbstractDeltaTest method removeBranches.

@AfterEach
void removeBranches() throws NessieConflictException, NessieNotFoundException {
    for (Reference ref : api.getAllReferences().get().getReferences()) {
        if (ref instanceof Branch) {
            api.deleteBranch().branchName(ref.getName()).hash(ref.getHash()).delete();
        }
        if (ref instanceof Tag) {
            api.deleteTag().tagName(ref.getName()).hash(ref.getHash()).delete();
        }
    }
    api.createReference().reference(Branch.of("main", null)).create();
    api.close();
    api = null;
}
Also used : Reference(org.projectnessie.model.Reference) Branch(org.projectnessie.model.Branch) Tag(org.projectnessie.model.Tag) AfterEach(org.junit.jupiter.api.AfterEach)

Example 14 with Reference

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

the class ITDeltaLog method testMultipleBranches.

@Test
void testMultipleBranches() throws Exception {
    String csvSalaries1 = ITDeltaLog.class.getResource("/salaries1.csv").getPath();
    String csvSalaries2 = ITDeltaLog.class.getResource("/salaries2.csv").getPath();
    String csvSalaries3 = ITDeltaLog.class.getResource("/salaries3.csv").getPath();
    String pathSalaries = new File(tempPath, "salaries").getAbsolutePath();
    spark.sql(String.format("CREATE TABLE IF NOT EXISTS test_multiple_branches (Season STRING, Team STRING, Salary STRING, " + "Player STRING) USING delta LOCATION '%s'", pathSalaries));
    Dataset<Row> salariesDf1 = spark.read().option("header", true).csv(csvSalaries1);
    salariesDf1.write().format("delta").mode("overwrite").save(pathSalaries);
    Dataset<Row> count1 = spark.sql("SELECT COUNT(*) FROM test_multiple_branches");
    Assertions.assertEquals(15L, count1.collectAsList().get(0).getLong(0));
    Reference mainBranch = api.getReference().refName("main").get();
    Reference devBranch = api.createReference().sourceRefName(mainBranch.getName()).reference(Branch.of("testMultipleBranches", mainBranch.getHash())).create();
    spark.sparkContext().conf().set("spark.sql.catalog.spark_catalog.ref", devBranch.getName());
    Dataset<Row> salariesDf2 = spark.read().option("header", true).csv(csvSalaries2);
    salariesDf2.write().format("delta").mode("append").save(pathSalaries);
    Dataset<Row> count2 = spark.sql("SELECT COUNT(*) FROM test_multiple_branches");
    Assertions.assertEquals(30L, count2.collectAsList().get(0).getLong(0));
    spark.sparkContext().conf().set("spark.sql.catalog.spark_catalog.ref", "main");
    Dataset<Row> salariesDf3 = spark.read().option("header", true).csv(csvSalaries3);
    salariesDf3.write().format("delta").mode("append").save(pathSalaries);
    Dataset<Row> count3 = spark.sql("SELECT COUNT(*) FROM test_multiple_branches");
    Assertions.assertEquals(35L, count3.collectAsList().get(0).getLong(0));
}
Also used : Reference(org.projectnessie.model.Reference) Row(org.apache.spark.sql.Row) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 15 with Reference

use of org.projectnessie.model.Reference in project presto by prestodb.

the class TestNessieMultiBranching method testTableDataVisibility.

@Test
public void testTableDataVisibility() throws NessieConflictException, NessieNotFoundException {
    assertQuerySucceeds("CREATE SCHEMA namespace_one");
    assertQuerySucceeds("CREATE TABLE namespace_one.tbl (a int)");
    assertQuerySucceeds("INSERT INTO namespace_one.tbl (a) VALUES (1)");
    assertQuerySucceeds("INSERT INTO namespace_one.tbl (a) VALUES (2)");
    Reference one = createBranch("branchOneWithTable");
    Reference two = createBranch("branchTwoWithTable");
    Session sessionOne = sessionOnRef(one.getName());
    Session sessionTwo = sessionOnRef(two.getName());
    assertQuerySucceeds(sessionOne, "INSERT INTO namespace_one.tbl (a) VALUES (3)");
    assertQuerySucceeds(sessionTwo, "INSERT INTO namespace_one.tbl (a) VALUES (5)");
    assertQuerySucceeds(sessionTwo, "INSERT INTO namespace_one.tbl (a) VALUES (6)");
    // main branch should still have 2 entries
    assertThat(computeScalar("SELECT count(*) FROM namespace_one.tbl")).isEqualTo(2L);
    MaterializedResult rows = computeActual("SELECT * FROM namespace_one.tbl");
    assertThat(rows.getMaterializedRows()).hasSize(2);
    assertEqualsIgnoreOrder(rows.getMaterializedRows(), resultBuilder(getSession(), rows.getTypes()).row(1).row(2).build().getMaterializedRows());
    // there should be 3 entries on this branch
    assertThat(computeScalar(sessionOne, "SELECT count(*) FROM namespace_one.tbl")).isEqualTo(3L);
    rows = computeActual(sessionOne, "SELECT * FROM namespace_one.tbl");
    assertThat(rows.getMaterializedRows()).hasSize(3);
    assertEqualsIgnoreOrder(rows.getMaterializedRows(), resultBuilder(sessionOne, rows.getTypes()).row(1).row(2).row(3).build().getMaterializedRows());
    // and 4 entries on this branch
    assertThat(computeScalar(sessionTwo, "SELECT count(*) FROM namespace_one.tbl")).isEqualTo(4L);
    rows = computeActual(sessionTwo, "SELECT * FROM namespace_one.tbl");
    assertThat(rows.getMaterializedRows()).hasSize(4);
    assertEqualsIgnoreOrder(rows.getMaterializedRows(), resultBuilder(sessionTwo, rows.getTypes()).row(1).row(2).row(5).row(6).build().getMaterializedRows());
    // retrieve the second to the last commit hash and query the table with that hash
    List<LogResponse.LogEntry> logEntries = nessieApiV1.getCommitLog().refName(two.getName()).get().getLogEntries();
    assertThat(logEntries).isNotEmpty();
    String hash = logEntries.get(1).getCommitMeta().getHash();
    Session sessionTwoAtHash = sessionOnRef(two.getName(), hash);
// TODO: enable this after bump to Iceberg 0.14.0
// at this hash there were only 3 rows
// assertThat(computeScalar(sessionTwoAtHash, "SELECT count(*) FROM namespace_one.tbl")).isEqualTo(3L);
// rows = computeActual(sessionTwoAtHash, "SELECT * FROM namespace_one.tbl");
// assertThat(rows.getMaterializedRows()).hasSize(3);
// assertEqualsIgnoreOrder(rows.getMaterializedRows(), resultBuilder(sessionTwoAtHash, rows.getTypes()).row(1).row(2).row(5).build().getMaterializedRows());
}
Also used : Reference(org.projectnessie.model.Reference) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Aggregations

Reference (org.projectnessie.model.Reference)38 Branch (org.projectnessie.model.Branch)19 Test (org.junit.jupiter.api.Test)17 ContentKey (org.projectnessie.model.ContentKey)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 NessieNotFoundException (org.projectnessie.error.NessieNotFoundException)10 CommitMeta (org.projectnessie.model.CommitMeta)10 IcebergTable (org.projectnessie.model.IcebergTable)10 List (java.util.List)9 Collectors (java.util.stream.Collectors)9 LogResponse (org.projectnessie.model.LogResponse)9 Tag (org.projectnessie.model.Tag)9 Put (org.projectnessie.model.Operation.Put)8 Map (java.util.Map)7 Stream (java.util.stream.Stream)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 BaseNessieClientServerException (org.projectnessie.error.BaseNessieClientServerException)7 Content (org.projectnessie.model.Content)7 NessieApiV1 (org.projectnessie.client.api.NessieApiV1)6 Operation (org.projectnessie.model.Operation)6