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

[Presto] Support time travel scan for paimon-presto connector #48

Merged
merged 1 commit into from
Jan 9, 2025
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 @@ -37,6 +37,7 @@
import org.apache.paimon.types.RowType;
import org.apache.paimon.types.VarCharType;

import com.facebook.presto.Session;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.DistributedQueryRunner;
Expand All @@ -49,6 +50,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.facebook.airlift.testing.Closeables.closeAllSuppress;
import static com.facebook.presto.testing.TestingSession.testSessionBuilder;
Expand Down Expand Up @@ -212,7 +214,18 @@ public void testProjection() throws Exception {

@Test
public void testFilter() throws Exception {
assertThat(sql("SELECT a, aCa FROM paimon.default.t2 WHERE a < 4"))
assertThat(sql("SELECT a, aCa FROM paimon.default.t2 WHERE a < 7"))
.isEqualTo("[[1, 1], [3, 2], [5, 3]]");
}

@Test
public void testFilterWithTimeTravel() throws Exception {
// Time travel table t2 to first commit.
assertThat(
sql(
"SELECT a, aCa FROM paimon.default.t2 WHERE a < 7",
PrestoSessionProperties.SCAN_VERSION,
"1"))
.isEqualTo("[[1, 1], [3, 2]]");
}

Expand All @@ -224,6 +237,17 @@ public void testGroupByWithCast() throws Exception {
.isEqualTo("[[1, 1, 3, 3], [2, 3, 3, 3]]");
}

private String sql(String sql, String key, String value) throws Exception {
Session session =
testSessionBuilder().setCatalogSessionProperty("paimon", key, value).build();
MaterializedResult result = queryRunner.execute(session, sql);
return result.getMaterializedRows().stream()
.map(Object::toString)
.sorted()
.collect(Collectors.toList())
.toString();
}

private String sql(String sql) throws Exception {
MaterializedResult result = queryRunner.execute(sql);
return result.getMaterializedRows().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.paimon.schema.Schema;
import org.apache.paimon.security.SecurityContext;
import org.apache.paimon.shade.guava30.com.google.common.collect.ImmutableList;
import org.apache.paimon.table.Table;
import org.apache.paimon.utils.InstantiationUtil;
import org.apache.paimon.utils.StringUtils;

Expand Down Expand Up @@ -130,14 +131,24 @@ public void dropSchema(ConnectorSession session, String schemaName) {

@Override
public PrestoTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName) {
return getTableHandle(tableName);
return getTableHandle(tableName, PrestoSessionProperties.getScanVersion(session));
}

public PrestoTableHandle getTableHandle(SchemaTableName tableName) {
public PrestoTableHandle getTableHandle(SchemaTableName tableName, String scanVersion) {
Identifier tablePath = new Identifier(tableName.getSchemaName(), tableName.getTableName());
byte[] serializedTable;
try {
serializedTable = InstantiationUtil.serializeObject(catalog.getTable(tablePath));
Table table = catalog.getTable(tablePath);
if (!StringUtils.isBlank(scanVersion)) {
table =
table.copy(
new HashMap<String, String>() {
{
put(CoreOptions.SCAN_VERSION.key(), scanVersion);
}
});
}
serializedTable = InstantiationUtil.serializeObject(table);
} catch (Catalog.TableNotExistException e) {
return null;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
import java.util.List;

import static com.facebook.presto.spi.session.PropertyMetadata.booleanProperty;
import static com.facebook.presto.spi.session.PropertyMetadata.stringProperty;

/** Presto {@link PrestoSessionProperties}. */
public class PrestoSessionProperties {

public static final String QUERY_PUSHDOWN_ENABLED = "query_pushdown_enabled";
public static final String PARTITION_PRUNE_ENABLED = "partition_prune_enabled";
public static final String RANGE_FILTERS_ON_SUBSCRIPTS_ENABLED =
"range_filters_on_subscripts_enabled";
public static final String SCAN_VERSION = "scan_version";

private final List<PropertyMetadata<?>> sessionProperties;

Expand All @@ -49,7 +53,13 @@ public PrestoSessionProperties(PaimonConfig config) {
PARTITION_PRUNE_ENABLED,
"Enable paimon query partition prune",
config.isPaimonPartitionPruningEnabled(),
false));
false),
booleanProperty(
RANGE_FILTERS_ON_SUBSCRIPTS_ENABLED,
"Whether to enable pushdown of range filters on subscripts like (a[2] = 5)",
false,
false),
stringProperty(SCAN_VERSION, "Paimon table scan version", null, false));
}

public List<PropertyMetadata<?>> getSessionProperties() {
Expand All @@ -63,4 +73,8 @@ public static boolean isPaimonPushdownEnabled(ConnectorSession session) {
public static boolean isPartitionPruneEnabled(ConnectorSession session) {
return session.getProperty(PARTITION_PRUNE_ENABLED, Boolean.class);
}

public static String getScanVersion(ConnectorSession session) {
return session.getProperty(SCAN_VERSION, String.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,18 @@ public void testProjection() throws Exception {

@Test
public void testFilter() throws Exception {
assertThat(sql("SELECT a, aCa FROM paimon.default.t2 WHERE a < 4"))
assertThat(sql("SELECT a, aCa FROM paimon.default.t2 WHERE a < 7"))
.isEqualTo("[[1, 1], [3, 2], [5, 3]]");
}

@Test
public void testFilterWithTimeTravel() throws Exception {
// Time travel table t2 to first commit.
assertThat(
sql(
"SELECT a, aCa FROM paimon.default.t2 WHERE a < 7",
PrestoSessionProperties.SCAN_VERSION,
"1"))
.isEqualTo("[[1, 1], [3, 2]]");
}

Expand Down
Loading