|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.iceberg.procedure; |
| 15 | + |
| 16 | +import com.facebook.presto.iceberg.IcebergConfig; |
| 17 | +import com.facebook.presto.iceberg.IcebergQueryRunner; |
| 18 | +import com.facebook.presto.testing.QueryRunner; |
| 19 | +import com.facebook.presto.tests.AbstractTestQueryFramework; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import org.apache.hadoop.conf.Configuration; |
| 22 | +import org.apache.iceberg.CatalogUtil; |
| 23 | +import org.apache.iceberg.Table; |
| 24 | +import org.apache.iceberg.catalog.Catalog; |
| 25 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 26 | +import org.apache.iceberg.hadoop.HadoopCatalog; |
| 27 | +import org.testng.annotations.Test; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static com.facebook.presto.iceberg.CatalogType.HADOOP; |
| 34 | +import static com.facebook.presto.iceberg.IcebergQueryRunner.getIcebergDataDirectoryPath; |
| 35 | +import static java.lang.String.format; |
| 36 | +import static java.util.regex.Pattern.quote; |
| 37 | + |
| 38 | +public class TestSetCurrentSnapshotProcedure |
| 39 | + extends AbstractTestQueryFramework |
| 40 | +{ |
| 41 | + public static final String ICEBERG_CATALOG = "iceberg"; |
| 42 | + public static final String TEST_SCHEMA = "tpch"; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected QueryRunner createQueryRunner() |
| 46 | + throws Exception |
| 47 | + { |
| 48 | + return IcebergQueryRunner.createIcebergQueryRunner(ImmutableMap.of(), HADOOP, ImmutableMap.of()); |
| 49 | + } |
| 50 | + |
| 51 | + public void createTable(String tableName) |
| 52 | + { |
| 53 | + assertUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (id integer, value VARCHAR)"); |
| 54 | + } |
| 55 | + |
| 56 | + public void dropTable(String tableName) |
| 57 | + { |
| 58 | + assertQuerySucceeds("DROP TABLE IF EXISTS " + TEST_SCHEMA + "." + tableName); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testSetCurrentSnapshotUsingPositionalArgs() |
| 63 | + { |
| 64 | + String tableName = "test_current_snapshot_table"; |
| 65 | + createTable(tableName); |
| 66 | + try { |
| 67 | + assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'a')", 1); |
| 68 | + Table table = loadTable(tableName); |
| 69 | + |
| 70 | + long snapShotIdv1 = table.currentSnapshot().snapshotId(); |
| 71 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a')"); |
| 72 | + |
| 73 | + assertUpdate("INSERT INTO " + tableName + " VALUES (2, 'b')", 1); |
| 74 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a'), (2, 'b')"); |
| 75 | + |
| 76 | + assertUpdate(format("CALL system.set_current_snapshot('%s', '%s', %d)", TEST_SCHEMA, tableName, snapShotIdv1)); |
| 77 | + // now current table will have only 1 row same as snapShotIdv1 |
| 78 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a')"); |
| 79 | + } |
| 80 | + finally { |
| 81 | + dropTable(tableName); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testSetCurrentSnapshotUsingNamedArgs() |
| 87 | + { |
| 88 | + String tableName = "test_named_arg_table"; |
| 89 | + createTable(tableName); |
| 90 | + try { |
| 91 | + assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'a')", 1); |
| 92 | + Table table = loadTable(tableName); |
| 93 | + |
| 94 | + long snapShotIdv1 = table.currentSnapshot().snapshotId(); |
| 95 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a')"); |
| 96 | + |
| 97 | + assertUpdate("INSERT INTO " + tableName + " VALUES (2, 'b')", 1); |
| 98 | + table.refresh(); |
| 99 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a'), (2, 'b')"); |
| 100 | + |
| 101 | + assertUpdate(format("CALL system.set_current_snapshot(snapshot_id => %d, table_name => '%s', schema => '%s')", |
| 102 | + snapShotIdv1, tableName, TEST_SCHEMA)); |
| 103 | + // now current table will have only 1 row same as snapShotIdv1 |
| 104 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a')"); |
| 105 | + } |
| 106 | + finally { |
| 107 | + dropTable(tableName); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testSetCurrentSnapshotToInvalidSnapshot() |
| 113 | + { |
| 114 | + String tableName = "invalid_test_table"; |
| 115 | + createTable(tableName); |
| 116 | + try { |
| 117 | + assertQueryFails(format("CALL system.set_current_snapshot(snapshot_id => %d, table_name => '%s', schema => '%s')", |
| 118 | + -1L, tableName, TEST_SCHEMA), |
| 119 | + "Cannot roll back to unknown snapshot id: -1"); |
| 120 | + assertQueryFails(format("CALL system.set_current_snapshot('%s', '%s', %d)", TEST_SCHEMA, tableName, -1L), |
| 121 | + "Cannot roll back to unknown snapshot id: -1"); |
| 122 | + } |
| 123 | + finally { |
| 124 | + dropTable(tableName); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testInvalidRollbackToSnapshotCases() |
| 130 | + { |
| 131 | + assertQueryFails(format("CALL system.set_current_snapshot(schema => '%s', table_name => '%s', %d)", |
| 132 | + TEST_SCHEMA, "tableName", 1), |
| 133 | + "line 1:1: Named and positional arguments cannot be mixed"); |
| 134 | + assertQueryFails("CALL custom.set_current_snapshot('test_schema', 'test_table', 1)", |
| 135 | + "Procedure not registered: custom.set_current_snapshot"); |
| 136 | + assertQueryFails("CALL system.set_current_snapshot('', 'test_table', 1)", |
| 137 | + "schemaName is empty"); |
| 138 | + assertQueryFails("CALL system.set_current_snapshot('test_schema', '', 1)", |
| 139 | + "tableName is empty"); |
| 140 | + assertQueryFails("CALL system.set_current_snapshot('test_schema', 'test_table')", |
| 141 | + "Either snapshot_id or reference must be provided, not both"); |
| 142 | + assertQueryFails("CALL system.set_current_snapshot('test_schema', 'test_table', 1, 'branch1')", |
| 143 | + "Either snapshot_id or reference must be provided, not both"); |
| 144 | + assertQueryFails("CALL system.set_current_snapshot('schema_name', 'table_name', 'branch1')", |
| 145 | + quote("line 1:63: Cannot cast type varchar(7) to bigint")); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testSetCurrentSnapshotToRef() |
| 150 | + { |
| 151 | + String tableName = "test_set_snapshot_ref_table"; |
| 152 | + createTable(tableName); |
| 153 | + try { |
| 154 | + assertUpdate("INSERT INTO " + tableName + " VALUES (1, 'a')", 1); |
| 155 | + Table table = loadTable(tableName); |
| 156 | + |
| 157 | + long snapShotIdv1 = table.currentSnapshot().snapshotId(); |
| 158 | + table.manageSnapshots().createBranch("ref1", snapShotIdv1).commit(); |
| 159 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a')"); |
| 160 | + |
| 161 | + assertUpdate("INSERT INTO " + tableName + " VALUES (2, 'b')", 1); |
| 162 | + table.refresh(); |
| 163 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a'), (2, 'b')"); |
| 164 | + |
| 165 | + assertUpdate(format("CALL system.set_current_snapshot(ref => '%s', table_name => '%s', schema => '%s')", |
| 166 | + "ref1", tableName, TEST_SCHEMA)); |
| 167 | + // now current table will have only 1 row same as ref1 |
| 168 | + assertQuery("SELECT * FROM " + tableName + " ORDER BY id", " VALUES (1, 'a')"); |
| 169 | + } |
| 170 | + finally { |
| 171 | + dropTable(tableName); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private Table loadTable(String tableName) |
| 176 | + { |
| 177 | + Catalog catalog = CatalogUtil.loadCatalog(HadoopCatalog.class.getName(), ICEBERG_CATALOG, getProperties(), new Configuration()); |
| 178 | + return catalog.loadTable(TableIdentifier.of(TEST_SCHEMA, tableName)); |
| 179 | + } |
| 180 | + |
| 181 | + private Map<String, String> getProperties() |
| 182 | + { |
| 183 | + File metastoreDir = getCatalogDirectory(); |
| 184 | + return ImmutableMap.of("warehouse", metastoreDir.toString()); |
| 185 | + } |
| 186 | + |
| 187 | + private File getCatalogDirectory() |
| 188 | + { |
| 189 | + Path dataDirectory = getDistributedQueryRunner().getCoordinator().getDataDirectory(); |
| 190 | + Path catalogDirectory = getIcebergDataDirectoryPath(dataDirectory, HADOOP.name(), new IcebergConfig().getFileFormat(), false); |
| 191 | + return catalogDirectory.toFile(); |
| 192 | + } |
| 193 | +} |
0 commit comments