Skip to content
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

[spark] Adjust ExpireSnapshotsProcedure param type #4059

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -20,6 +20,8 @@

import org.apache.paimon.options.ExpireConfig;
import org.apache.paimon.table.ExpireSnapshots;
import org.apache.paimon.utils.DateTimeUtils;
import org.apache.paimon.utils.StringUtils;

import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.connector.catalog.Identifier;
Expand All @@ -29,10 +31,10 @@
import org.apache.spark.sql.types.StructType;

import java.time.Duration;
import java.util.TimeZone;

import static org.apache.spark.sql.types.DataTypes.IntegerType;
import static org.apache.spark.sql.types.DataTypes.StringType;
import static org.apache.spark.sql.types.DataTypes.TimestampType;

/** A procedure to expire snapshots. */
public class ExpireSnapshotsProcedure extends BaseProcedure {
Expand All @@ -42,7 +44,7 @@ public class ExpireSnapshotsProcedure extends BaseProcedure {
ProcedureParameter.required("table", StringType),
ProcedureParameter.optional("retain_max", IntegerType),
ProcedureParameter.optional("retain_min", IntegerType),
ProcedureParameter.optional("older_than", TimestampType),
ProcedureParameter.optional("older_than", StringType),
ProcedureParameter.optional("max_deletes", IntegerType)
};

Expand Down Expand Up @@ -72,8 +74,9 @@ public InternalRow[] call(InternalRow args) {
Identifier tableIdent = toIdentifier(args.getString(0), PARAMETERS[0].name());
Integer retainMax = args.isNullAt(1) ? null : args.getInt(1);
Integer retainMin = args.isNullAt(2) ? null : args.getInt(2);
Long olderThanMills = args.isNullAt(3) ? null : args.getLong(3) / 1000;
String olderThanStr = args.isNullAt(3) ? null : args.getString(3);
Integer maxDeletes = args.isNullAt(4) ? null : args.getInt(4);

return modifyPaimonTable(
tableIdent,
table -> {
Expand All @@ -85,9 +88,21 @@ public InternalRow[] call(InternalRow args) {
if (retainMin != null) {
builder.snapshotRetainMin(retainMin);
}
if (olderThanMills != null) {
builder.snapshotTimeRetain(
Duration.ofMillis(System.currentTimeMillis() - olderThanMills));
if (!StringUtils.isNullOrWhitespaceOnly(olderThanStr)) {
long olderThanMills;
// forward compatibility for timestamp type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How to forward compatibility for timestamp type? It is a string instead of a timestamp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In default, the value of timestamp type is like "1724664000000", it's actually a string that contains numerics. And get it by
Long olderThanMills = args.isNullAt(3) ? null : args.getLong(3) / 1000;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change param older_than type from TimeStampType to StringType, TimeStampType is not easy to understand and use.

Is this related? Can you show the use cases? How to improve the usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got, has only retain StringType

if (StringUtils.isNumeric(olderThanStr)) {
olderThanMills = Long.parseLong(olderThanStr) / 1000;
builder.snapshotTimeRetain(
Duration.ofMillis(System.currentTimeMillis() - olderThanMills));
} else {
olderThanMills =
DateTimeUtils.parseTimestampData(
olderThanStr, 3, TimeZone.getDefault())
.getMillisecond();
builder.snapshotTimeRetain(
Duration.ofMillis(System.currentTimeMillis() - olderThanMills));
}
}
if (maxDeletes != null) {
builder.snapshotMaxDeletes(maxDeletes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
package org.apache.paimon.spark.procedure

import org.apache.paimon.spark.PaimonSparkTestBase
import org.apache.paimon.utils.SnapshotManager

import org.apache.spark.sql.{Dataset, Row}
import org.apache.spark.sql.execution.streaming.MemoryStream
import org.apache.spark.sql.streaming.StreamTest
import org.assertj.core.api.Assertions.{assertThat, assertThatIllegalArgumentException}

import java.sql.Timestamp

class ExpireSnapshotsProcedureTest extends PaimonSparkTestBase with StreamTest {

Expand Down Expand Up @@ -136,4 +140,36 @@ class ExpireSnapshotsProcedureTest extends PaimonSparkTestBase with StreamTest {
}
}
}

test("test parameter order_than with old timestamp type and new string type") {
sql(
"CREATE TABLE T (a INT, b STRING) " +
"TBLPROPERTIES ( 'num-sorted-run.compaction-trigger' = '999' )")
val table = loadTable("T")
val snapshotManager = table.snapshotManager

// generate 5 snapshot
for (i <- 1 to 5) {
sql(s"INSERT INTO T VALUES ($i, '$i')")
}
checkSnapshots(snapshotManager, 1, 5)

// older_than with old timestamp type, like "1724664000000"
val nanosecond: Long = snapshotManager.latestSnapshot().timeMillis * 1000
spark.sql(
s"CALL paimon.sys.expire_snapshots(table => 'test.T', older_than => $nanosecond, max_deletes => 2)")
checkSnapshots(snapshotManager, 3, 5)

// older_than with new string type, like "2024-08-26 17:20:00"
val timestamp = new Timestamp(snapshotManager.latestSnapshot().timeMillis)
spark.sql(
s"CALL paimon.sys.expire_snapshots(table => 'test.T', older_than => '${timestamp.toString}', max_deletes => 2)")
checkSnapshots(snapshotManager, 5, 5)
}

def checkSnapshots(sm: SnapshotManager, earliest: Int, latest: Int): Unit = {
assertThat(sm.snapshotCount).isEqualTo(latest - earliest + 1)
assertThat(sm.earliestSnapshotId).isEqualTo(earliest)
assertThat(sm.latestSnapshotId).isEqualTo(latest)
}
}
Loading