Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ singleStatement
;

statement
: CREATE (BRANCH|TAG) (IF NOT EXISTS)? identifier (IN catalog=identifier)? (AS reference=identifier)? #nessieCreateRef
: CREATE (BRANCH|TAG) (IF NOT EXISTS)? identifier (IN catalog=identifier)? (FROM reference=identifier)? #nessieCreateRef
| DROP (BRANCH|TAG) identifier (IN catalog=identifier)? #nessieDropRef
| USE REFERENCE identifier (AT ts=identifier)? (IN catalog=identifier)? #nessieUseRef
| LIST REFERENCES (IN catalog=identifier)? #nessieListRef
| SHOW REFERENCE (IN catalog=identifier)? #nessieShowRef
| MERGE BRANCH (identifier)? (INTO toRef=identifier)? (IN catalog=identifier)? #nessieMergeRef
| SHOW LOG (identifier)? (IN catalog=identifier)? #nessieShowLog
| ASSIGN (BRANCH|TAG) (identifier)? (AS toRef=identifier)? (IN catalog=identifier)? #nessieAssignRef
| ASSIGN (BRANCH|TAG) (identifier)? (TO toRef=identifier)? (IN catalog=identifier)? #nessieAssignRef
// add collect gc action
// add purge gc action
;
Expand All @@ -83,7 +83,7 @@ quotedIdentifier
;

nonReserved
: AS | ASSIGN | AT | BRANCH | CREATE| DROP | IN | TAG | LOG | USE | REFERENCE | REFERENCES
: ASSIGN | AT | BRANCH | CREATE| DROP | FROM | IN | TAG | TO | LOG | USE | REFERENCE | REFERENCES
| SHOW | LIST | MERGE | INTO | IF | NOT | EXISTS
;

Expand All @@ -97,7 +97,8 @@ REFERENCES: 'REFERENCES';
SHOW: 'SHOW';
LIST: 'LIST';
MERGE: 'MERGE';
AS: 'AS';
FROM: 'FROM';
TO: 'TO';
AT: 'AT';
BRANCH: 'BRANCH';
CREATE: 'CREATE';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.connector.catalog.CatalogPlugin
import org.apache.spark.unsafe.types.UTF8String
import org.projectnessie.client.NessieClient
import org.projectnessie.model.{
Branch,
ImmutableBranch,
ImmutableHash,
ImmutableMerge,
ImmutableTag,
Tag
}
import org.projectnessie.model.{Branch, Tag}

case class AssignReferenceExec(
output: Seq[Attribute],
Expand All @@ -41,18 +34,22 @@ case class AssignReferenceExec(
override protected def runInternal(
nessieClient: NessieClient
): Seq[InternalRow] = {
val toHash = toRefName
.map(r => nessieClient.getTreeApi.getReferenceByName(r).getHash)
.getOrElse(nessieClient.getTreeApi.getDefaultBranch.getHash)
val toRef = toRefName
.map(r => nessieClient.getTreeApi.getReferenceByName(r))
.getOrElse(nessieClient.getTreeApi.getDefaultBranch)
val hash = nessieClient.getTreeApi.getReferenceByName(branch).getHash
if (isBranch) {
nessieClient.getTreeApi.assignBranch(
branch,
hash,
Branch.of(branch, toHash)
Branch.of(toRef.getName, toRef.getHash)
)
} else {
nessieClient.getTreeApi.assignTag(branch, hash, Tag.of(branch, toHash))
nessieClient.getTreeApi.assignTag(
branch,
hash,
Tag.of(toRef.getName, toRef.getHash)
)
}

val ref = nessieClient.getTreeApi.getReferenceByName(branch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ void testCreateTagIn() throws NessieNotFoundException {
}

@Test
void testCreateBranchInAs() throws NessieNotFoundException {
assertThat(sql("CREATE BRANCH %s IN nessie", refName))
void testCreateBranchInFrom() throws NessieNotFoundException {
assertThat(sql("CREATE BRANCH %s IN nessie FROM main", refName))
.containsExactly(row("Branch", refName, hash));
assertThat(nessieClient.getTreeApi().getReferenceByName(refName))
.isEqualTo(Branch.of(refName, hash));
Expand All @@ -178,8 +178,8 @@ void testCreateBranchInAs() throws NessieNotFoundException {
}

@Test
void testCreateTagInAs() throws NessieNotFoundException {
assertThat(sql("CREATE TAG %s IN nessie AS main", refName))
void testCreateTagInFrom() throws NessieNotFoundException {
assertThat(sql("CREATE TAG %s IN nessie FROM main", refName))
.containsExactly(row("Tag", refName, hash));
assertThat(nessieClient.getTreeApi().getReferenceByName(refName))
.isEqualTo(Tag.of(refName, hash));
Expand All @@ -192,6 +192,64 @@ void testCreateTagInAs() throws NessieNotFoundException {
.hasMessage("Unable to find reference [testBranch].");
}

@Test
void testAssignBranch() throws NessieConflictException, NessieNotFoundException {
String random = "randomBranch";
assertThat(sql("CREATE BRANCH %s IN nessie", random))
.containsExactly(row("Branch", random, hash));

commitAndReturnLog(refName);
sql("USE REFERENCE %s", refName);
sql("MERGE BRANCH %s INTO main IN nessie", refName);
Reference main = nessieClient.getTreeApi().getReferenceByName("main");

assertThat(sql("ASSIGN BRANCH %s IN nessie", random))
.containsExactly(row("Branch", random, main.getHash()));
}

@Test
void testAssignTag() throws NessieConflictException, NessieNotFoundException {
String random = "randomTag";
assertThat(sql("CREATE TAG %s IN nessie", random)).containsExactly(row("Tag", random, hash));

commitAndReturnLog(refName);
sql("USE REFERENCE %s", refName);
sql("MERGE BRANCH %s INTO main IN nessie", refName);
Reference main = nessieClient.getTreeApi().getReferenceByName("main");

assertThat(sql("ASSIGN TAG %s IN nessie", random))
.containsExactly(row("Tag", random, main.getHash()));
}

@Test
void testAssignBranchTo() throws NessieConflictException, NessieNotFoundException {
String random = "randomBranch";
assertThat(sql("CREATE BRANCH %s IN nessie", random))
.containsExactly(row("Branch", random, hash));

commitAndReturnLog(refName);
sql("USE REFERENCE %s", refName);
sql("MERGE BRANCH %s INTO main IN nessie", refName);
Reference main = nessieClient.getTreeApi().getReferenceByName("main");

assertThat(sql("ASSIGN BRANCH %s TO main IN nessie", random))
.containsExactly(row("Branch", random, main.getHash()));
}

@Test
void testAssignTagTo() throws NessieConflictException, NessieNotFoundException {
String random = "randomTag";
assertThat(sql("CREATE TAG %s IN nessie", random)).containsExactly(row("Tag", random, hash));

commitAndReturnLog(refName);
sql("USE REFERENCE %s", refName);
sql("MERGE BRANCH %s INTO main IN nessie", refName);
Reference main = nessieClient.getTreeApi().getReferenceByName("main");

assertThat(sql("ASSIGN TAG %s TO main IN nessie", random))
.containsExactly(row("Tag", random, main.getHash()));
}

@Test
void testCreateBranch() throws NessieNotFoundException {
String catalog = spark.sessionState().catalogManager().currentCatalog().name();
Expand Down Expand Up @@ -365,7 +423,8 @@ void showLogIn() throws NessieConflictException, NessieNotFoundException, Analys

private List<Object[]> commitAndReturnLog(String branch)
throws NessieConflictException, NessieNotFoundException {
sql("CREATE BRANCH %s IN nessie", branch);
assertThat(sql("CREATE BRANCH %s IN nessie", branch))
.containsExactly(row("Branch", branch, hash));
ContentsKey key = ContentsKey.of("table", "name");
CommitMeta cm1 =
ImmutableCommitMeta.builder()
Expand Down
10 changes: 5 additions & 5 deletions site/docs/tools/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ Additional configuration details can be found in the [Spark via Iceberg](iceberg
## Grammar
The current grammar is shown below:
```
: CREATE (BRANCH|TAG) (IF NOT EXISTS)? identifier (IN catalog=identifier)? (AS reference=identifier)?
: CREATE (BRANCH|TAG) (IF NOT EXISTS)? identifier (IN catalog=identifier)? (FROM reference=identifier)?
| DROP (BRANCH|TAG) identifier (IN catalog=identifier)?
| USE REFERENCE identifier (AT ts=identifier)? (IN catalog=identifier)?
| LIST REFERENCES (IN catalog=identifier)?
| SHOW REFERENCE (IN catalog=identifier)?
| MERGE BRANCH (identifier)? (INTO toRef=identifier)? (IN catalog=identifier)?
| SHOW LOG (identifier)? (IN catalog=identifier)?
| ASSIGN (BRANCH|TAG) (identifier)? (AS toRef=identifier)? (IN catalog=identifier)?
| ASSIGN (BRANCH|TAG) (identifier)? (TO toRef=identifier)? (IN catalog=identifier)?
```

## Creating Branches/Tags
Expand All @@ -41,7 +41,7 @@ Creating a tag `devTag` in the `nessie` catalog (in case it doesn't already exis

Creating a branch `dev` in the `nessie` catalog off of an existing branch/tag `base`:

* `CREATE BRANCH IF NOT EXISTS dev IN nessie AS base`
* `CREATE BRANCH IF NOT EXISTS dev IN nessie FROM base`

Note that in case `base` doesn't exist, Nessie will fallback to the default branch (`main`).

Expand Down Expand Up @@ -87,11 +87,11 @@ It is possible to look at the commit log of a particular branch/tag in the `ness

Assigning a branch `dev` to `base` in catalog `nessie` can be done via:

* `ASSIGN BRANCH dev AS base IN nessie`
* `ASSIGN BRANCH dev TO base IN nessie`

Assigning a tag `devTag` to `base` in catalog `nessie` can be done via:

* `ASSIGN TAG devTag AS base IN nessie`
* `ASSIGN TAG devTag TO base IN nessie`

Note that in case `base` doesn't exist, Nessie will fallback to the default branch (`main`).

Expand Down