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();
});
}
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());
}
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;
}
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));
}
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());
}
Aggregations