-
Notifications
You must be signed in to change notification settings - Fork 53
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
Extend OH SQL for snapshots retention #223
Draft
teamurko
wants to merge
1
commit into
linkedin:main
Choose a base branch
from
teamurko:snapshots_policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...est/java/com/linkedin/openhouse/spark/e2e/extensions/SetSnapshotsRetentionPolicyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.linkedin.openhouse.spark.e2e.extensions; | ||
|
||
import static com.linkedin.openhouse.spark.MockHelpers.*; | ||
import static com.linkedin.openhouse.spark.SparkTestBase.*; | ||
|
||
import com.linkedin.openhouse.spark.SparkTestBase; | ||
import org.apache.iceberg.catalog.TableIdentifier; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith(SparkTestBase.class) | ||
public class SetSnapshotsRetentionPolicyTest { | ||
@Test | ||
public void testSetSnapshotsRetentionPolicy() { | ||
Object existingTable = | ||
mockGetTableResponseBody( | ||
"dbSetSnapshotsRetention", | ||
"t1", | ||
"c1", | ||
"dbSetSnapshotsRetention.t1", | ||
"u1", | ||
mockTableLocation( | ||
TableIdentifier.of("dbSetSnapshotsRetention", "t1"), | ||
convertSchemaToDDLComponent(baseSchema), | ||
""), | ||
"V1", | ||
baseSchema, | ||
null, | ||
null); | ||
Object existingOpenhouseTable = | ||
mockGetTableResponseBody( | ||
"dbSetSnapshotsRetention", | ||
"t1", | ||
"c1", | ||
"dbSetSnapshotsRetention.t1", | ||
"u1", | ||
mockTableLocationAfterOperation( | ||
TableIdentifier.of("dbSetSnapshotsRetention", "t1"), | ||
"ALTER TABLE %t SET TBLPROPERTIES ('openhouse.tableId'='t1')"), | ||
"V1", | ||
baseSchema, | ||
null, | ||
null); | ||
Object tableAfterSetSnapshotsRetention = | ||
mockGetTableResponseBody( | ||
"dbSetSnapshotsRetention", | ||
"t1", | ||
"c1", | ||
"dbSetSnapshotsRetention.t1", | ||
"u1", | ||
mockTableLocationAfterOperation( | ||
TableIdentifier.of("dbSetSnapshotsRetention", "t1"), | ||
"ALTER TABLE %t SET TBLPROPERTIES('policies'='{\"snapshotsRetention\":{\"timeCount\":24,\"granularity\":\"HOUR\",\"count\":10}}')"), | ||
"V2", | ||
baseSchema, | ||
null, | ||
null); | ||
mockTableService.enqueue(mockResponse(200, existingOpenhouseTable)); // doRefresh() | ||
mockTableService.enqueue(mockResponse(200, existingOpenhouseTable)); // doRefresh() | ||
mockTableService.enqueue(mockResponse(201, tableAfterSetSnapshotsRetention)); // doCommit() | ||
String ddlWithSchema = | ||
"ALTER TABLE openhouse.dbSetSnapshotsRetention.t1 SET POLICY (SNAPSHOTS_RETENTION TTL=24H COUNT=10)"; | ||
Assertions.assertDoesNotThrow(() -> spark.sql(ddlWithSchema)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...com/linkedin/openhouse/spark/sql/catalyst/plans/logical/SetSnapshotsRetentionPolicy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.linkedin.openhouse.spark.sql.catalyst.plans.logical | ||
|
||
import org.apache.spark.sql.catalyst.plans.logical.Command | ||
|
||
case class SetSnapshotsRetentionPolicy (tableName: Seq[String], granularity: String, timeCount: Int, count: Int) extends Command { | ||
override def simpleString(maxFields: Int): String = { | ||
s"SetSnapshotsRetentionPolicy: ${tableName} ${timeCount} ${granularity} ${count}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...nkedin/openhouse/spark/sql/execution/datasources/v2/SetSnapshotsRetentionPolicyExec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.linkedin.openhouse.spark.sql.execution.datasources.v2 | ||
|
||
import org.apache.iceberg.spark.source.SparkTable | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.Attribute | ||
import org.apache.spark.sql.connector.catalog.{Identifier, TableCatalog} | ||
import org.apache.spark.sql.execution.datasources.v2.V2CommandExec | ||
|
||
case class SetSnapshotsRetentionPolicyExec( | ||
catalog: TableCatalog, | ||
ident: Identifier, | ||
granularity: String, | ||
timeCount: Int, | ||
count: Int | ||
) extends V2CommandExec { | ||
|
||
override lazy val output: Seq[Attribute] = Nil | ||
|
||
override protected def run(): Seq[InternalRow] = { | ||
catalog.loadTable(ident) match { | ||
case iceberg: SparkTable if iceberg.table().properties().containsKey("openhouse.tableId") => | ||
val key = "updated.openhouse.policy" | ||
val value = { | ||
(count) match { | ||
case (0) => s"""{"snapshotsRetention":{"timeCount":${timeCount},"granularity":"${granularity}"}}""" | ||
case (_) => | ||
s"""{"snapshotsRetention":{"timeCount":${timeCount}, "granularity":"${granularity}", "count":${count}}}""" | ||
} | ||
} | ||
|
||
iceberg.table().updateProperties() | ||
.set(key, value) | ||
.commit() | ||
|
||
case table => | ||
throw new UnsupportedOperationException(s"Cannot set snapshots retention policy for non-Openhouse table: $table") | ||
} | ||
|
||
Nil | ||
} | ||
|
||
override def simpleString(maxFields: Int): String = { | ||
s"SetSnapshotsRetentionPolicyExec: ${catalog} ${ident} ${timeCount} ${granularity} ${count}" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
version
instead ofsnapshots
, the latter was a overloaded term that can also be used to describe a non-partitioned table.SET POLICY RETAIN 100 HOURS
SET POLICY RETAIN 50 VERSIONS