mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(athena): run Hive DDL against the Glue catalog (#8411)
Athena runs Hive DDL against the Glue Data Catalog, but Floci sent every statement other than CREATE DATABASE to DuckDB: CREATE EXTERNAL TABLE, ALTER TABLE ADD PARTITION, MSCK REPAIR TABLE and SHOW PARTITIONS failed to parse, and DROP TABLE succeeded without touching Glue. AthenaHiveDdl parses CREATE EXTERNAL TABLE (columns, PARTITIONED BY, CLUSTERED BY, ROW FORMAT DELIMITED or SERDE, STORED AS, LOCATION, TBLPROPERTIES, with the SerDe parameters and input/output formats Hive stores), DROP TABLE, ALTER TABLE ADD/DROP PARTITION, MSCK REPAIR TABLE and SHOW PARTITIONS; AthenaHiveDdlExecutor applies them to Glue as Athena does: DROP TABLE keeps the data, MSCK only adds Hive-style partitions, IF [NOT] EXISTS turns duplicates and misses into no-ops, and SHOW PARTITIONS writes its rows as the query's result. A CREATE TABLE without EXTERNAL fails unless it is Iceberg or CREATE TABLE AS, which still go to the query engine. Mock mode keeps DDL result rows.
This commit is contained in:
1 parent
7a682dcd00
commit
cc9b5bfee1
7 files changed
+1429
-2
No files matched your search
@@ -0,0 +1,51 @@
|
||||
package io.floci.aws.services.athena;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.glue.GlueService;
|
||||
import io.floci.aws.services.glue.model.Column;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import io.floci.aws.services.s3.S3Service;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class AthenaHiveDdlExecutorTest {
|
||||
|
||||
@Test
|
||||
void dropTableIfExistsStaysANoOpWhenAnotherDropWinsTheRace() {
|
||||
GlueService glue = Mockito.mock(GlueService.class);
|
||||
Table table = new Table();
|
||||
table.setName("events");
|
||||
when(glue.getTable("lake", "events")).thenReturn(table);
|
||||
doThrow(new AwsException("EntityNotFoundException", "Table not found", 400)).when(glue).deleteTable("lake", "events");
|
||||
AthenaHiveDdlExecutor executor = new AthenaHiveDdlExecutor(glue, Mockito.mock(S3Service.class));
|
||||
|
||||
assertDoesNotThrow(() -> executor.execute(AthenaHiveDdl.parse("DROP TABLE IF EXISTS events"), "lake"));
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> executor.execute(AthenaHiveDdl.parse("DROP TABLE events"), "lake"));
|
||||
assertEquals("EntityNotFoundException", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFolderWithAnInvalidEscapeIsNotAPartitionFolder() {
|
||||
Column region = new Column();
|
||||
region.setName("region");
|
||||
|
||||
assertNull(AthenaHiveDdlExecutor.hivePartitionValues("region=bad%ZZ/part-0", List.of(region)));
|
||||
assertEquals(List.of("sa/west"), AthenaHiveDdlExecutor.hivePartitionValues("region=sa%2fwest/part-0", List.of(region)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hiveEscapesTheCharactersItReservesInFolderNames() {
|
||||
assertEquals("sa%2Fwest%3Dx%25", AthenaHiveDdlExecutor.escapePathName("sa/west=x%"));
|
||||
assertEquals("2026-11-14 10.00", AthenaHiveDdlExecutor.escapePathName("2026-11-14 10.00"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package io.floci.aws.services.athena;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.athena.AthenaClient;
|
||||
import software.amazon.awssdk.services.athena.model.QueryExecution;
|
||||
import software.amazon.awssdk.services.athena.model.QueryExecutionState;
|
||||
import software.amazon.awssdk.services.athena.model.Row;
|
||||
import software.amazon.awssdk.services.glue.GlueClient;
|
||||
import software.amazon.awssdk.services.glue.model.EntityNotFoundException;
|
||||
import software.amazon.awssdk.services.glue.model.Partition;
|
||||
import software.amazon.awssdk.services.glue.model.Table;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Hive DDL submitted through Athena changes the Glue Data Catalog the way Athena does, checked
|
||||
* through the Glue and Athena APIs with the AWS SDK.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class AthenaHiveDdlIntegrationTest {
|
||||
|
||||
private static final AtomicLong ACCOUNTS = new AtomicLong(741680700000L);
|
||||
|
||||
private AthenaClient athena;
|
||||
private GlueClient glue;
|
||||
private S3Client s3;
|
||||
private String bucket;
|
||||
private String results;
|
||||
|
||||
@BeforeEach
|
||||
void openClients() {
|
||||
String account = Long.toString(ACCOUNTS.incrementAndGet());
|
||||
athena = AwsTestClients.create(AthenaClient.builder(), account, Region.US_EAST_1);
|
||||
glue = AwsTestClients.create(GlueClient.builder(), account, Region.US_EAST_1);
|
||||
s3 = AwsTestClients.create(S3Client.builder().forcePathStyle(true), account, Region.US_EAST_1);
|
||||
bucket = "hive-ddl-" + account;
|
||||
results = "hive-ddl-results-" + account;
|
||||
s3.createBucket(r -> r.bucket(bucket));
|
||||
assertEquals(QueryExecutionState.SUCCEEDED, run("CREATE DATABASE lake").status().state());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void closeClients() {
|
||||
athena.close();
|
||||
glue.close();
|
||||
s3.close();
|
||||
}
|
||||
|
||||
private QueryExecution run(String sql) {
|
||||
String id = athena.startQueryExecution(r -> r.queryString(sql)
|
||||
.queryExecutionContext(c -> c.database("lake"))
|
||||
.resultConfiguration(c -> c.outputLocation("s3://" + results + "/"))).queryExecutionId();
|
||||
return athena.getQueryExecution(r -> r.queryExecutionId(id)).queryExecution();
|
||||
}
|
||||
|
||||
private void succeeds(String sql) {
|
||||
QueryExecution execution = run(sql);
|
||||
assertEquals(QueryExecutionState.SUCCEEDED, execution.status().state(), execution.status().stateChangeReason());
|
||||
assertEquals("DDL", execution.statementTypeAsString());
|
||||
}
|
||||
|
||||
private String fails(String sql) {
|
||||
QueryExecution execution = run(sql);
|
||||
assertEquals(QueryExecutionState.FAILED, execution.status().state(), sql);
|
||||
return execution.status().stateChangeReason();
|
||||
}
|
||||
|
||||
private void createEventsTable() {
|
||||
succeeds("""
|
||||
CREATE EXTERNAL TABLE events (id bigint, payload struct<a:int,b:array<string>>)
|
||||
PARTITIONED BY (dt string, region string)
|
||||
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\t'
|
||||
STORED AS TEXTFILE
|
||||
LOCATION 's3://%s/events/'
|
||||
TBLPROPERTIES ('classification'='csv')
|
||||
""".formatted(bucket));
|
||||
}
|
||||
|
||||
private Set<String> partitionLocations() {
|
||||
return glue.getPartitions(r -> r.databaseName("lake").tableName("events")).partitions().stream()
|
||||
.map(p -> String.join("/", p.values()) + "@" + p.storageDescriptor().location())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createExternalTableRegistersTheGlueTable() {
|
||||
createEventsTable();
|
||||
|
||||
Table table = glue.getTable(r -> r.databaseName("lake").name("events")).table();
|
||||
assertEquals("EXTERNAL_TABLE", table.tableType());
|
||||
assertEquals(Map.of("classification", "csv", "EXTERNAL", "TRUE"), table.parameters());
|
||||
assertEquals(List.of("bigint", "struct<a:int,b:array<string>>"),
|
||||
table.storageDescriptor().columns().stream().map(c -> c.type()).toList());
|
||||
assertEquals(List.of("dt", "region"), table.partitionKeys().stream().map(c -> c.name()).toList());
|
||||
assertEquals("s3://" + bucket + "/events/", table.storageDescriptor().location());
|
||||
assertEquals("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe",
|
||||
table.storageDescriptor().serdeInfo().serializationLibrary());
|
||||
assertEquals("\t", table.storageDescriptor().serdeInfo().parameters().get("field.delim"));
|
||||
|
||||
assertTrue(fails(String.format("CREATE EXTERNAL TABLE events (id bigint) LOCATION 's3://%s/x/'", bucket))
|
||||
.contains("exists"));
|
||||
succeeds(String.format("CREATE EXTERNAL TABLE IF NOT EXISTS events (id bigint) LOCATION 's3://%s/x/'", bucket));
|
||||
assertTrue(fails("CREATE TABLE plain (a int) STORED AS PARQUET LOCATION 's3://b/plain/'").contains("EXTERNAL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addPartitionDefaultsToTheHiveLayoutAndHonoursIfNotExists() {
|
||||
createEventsTable();
|
||||
|
||||
succeeds("""
|
||||
ALTER TABLE events ADD
|
||||
PARTITION (dt = '2026-11-14', region = 'eu')
|
||||
PARTITION (region = 'us', dt = '2026-11-15') LOCATION 's3://%s/custom/'
|
||||
""".formatted(bucket));
|
||||
|
||||
assertEquals(Set.of("2026-11-14/eu@s3://" + bucket + "/events/dt=2026-11-14/region=eu/",
|
||||
"2026-11-15/us@s3://" + bucket + "/custom/"), partitionLocations());
|
||||
fails("ALTER TABLE events ADD PARTITION (dt = '2026-11-14', region = 'eu')");
|
||||
succeeds("ALTER TABLE events ADD IF NOT EXISTS PARTITION (dt = '2026-11-14', region = 'eu')");
|
||||
assertTrue(fails("ALTER TABLE events ADD PARTITION (dt = '2026-11-16')").contains("partition keys"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void msckRepairAddsTheHiveFoldersItFindsAndShowPartitionsListsThem() {
|
||||
createEventsTable();
|
||||
succeeds("ALTER TABLE events ADD PARTITION (dt = '2026-11-14', region = 'eu')");
|
||||
for (String key : List.of("events/dt=2026-11-14/region=eu/part-0.tsv", "events/dt=2026-11-16/region=ap/part-0.tsv",
|
||||
"events/dt=2026-11-17/region=sa%2Fwest/part-0.tsv", "events/dt=2026-11-18/part-0.tsv",
|
||||
"events/other/part-0.tsv")) {
|
||||
s3.putObject(r -> r.bucket(bucket).key(key), RequestBody.fromString("1\t{}\n"));
|
||||
}
|
||||
|
||||
succeeds("MSCK REPAIR TABLE events");
|
||||
|
||||
assertEquals(Set.of("2026-11-14/eu", "2026-11-16/ap", "2026-11-17/sa/west"),
|
||||
glue.getPartitions(r -> r.databaseName("lake").tableName("events")).partitions().stream()
|
||||
.map(p -> String.join("/", p.values())).collect(Collectors.toSet()));
|
||||
QueryExecution show = run("SHOW PARTITIONS events");
|
||||
assertEquals(QueryExecutionState.SUCCEEDED, show.status().state(), show.status().stateChangeReason());
|
||||
List<Row> rows = athena.getQueryResults(r -> r.queryExecutionId(show.queryExecutionId())).resultSet().rows();
|
||||
assertEquals("partition", rows.getFirst().data().getFirst().varCharValue());
|
||||
assertEquals(Set.of("dt=2026-11-14/region=eu", "dt=2026-11-16/region=ap", "dt=2026-11-17/region=sa%2Fwest"),
|
||||
rows.stream().skip(1).map(row -> row.data().getFirst().varCharValue()).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void msckKeepsEachFolderAsSpelledAndSkipsOnesHiveCouldNotHaveWritten() {
|
||||
createEventsTable();
|
||||
for (String key : List.of("events/dt=2026-11-14/region=sa%2fwest/part-0.tsv",
|
||||
"events/dt=2026-11-14/region=bad%ZZ/part-0.tsv")) {
|
||||
s3.putObject(r -> r.bucket(bucket).key(key), RequestBody.fromString("1\t{}\n"));
|
||||
}
|
||||
|
||||
succeeds("MSCK REPAIR TABLE events");
|
||||
|
||||
assertEquals(Set.of("2026-11-14/sa/west@s3://" + bucket + "/events/dt=2026-11-14/region=sa%2fwest/"),
|
||||
partitionLocations());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAddedPartitionInheritsTheTablesSortColumns() {
|
||||
succeeds("""
|
||||
CREATE EXTERNAL TABLE sorted_events (id bigint) PARTITIONED BY (dt string)
|
||||
CLUSTERED BY (id) SORTED BY (id DESC) INTO 2 BUCKETS STORED AS PARQUET LOCATION 's3://%s/sorted/'
|
||||
""".formatted(bucket));
|
||||
|
||||
succeeds("ALTER TABLE sorted_events ADD PARTITION (dt = '2026-11-14')");
|
||||
|
||||
Partition partition = glue.getPartitions(r -> r.databaseName("lake").tableName("sorted_events")).partitions().getFirst();
|
||||
assertEquals("id", partition.storageDescriptor().sortColumns().getFirst().column());
|
||||
assertEquals(0, partition.storageDescriptor().sortColumns().getFirst().sortOrder());
|
||||
}
|
||||
|
||||
@Test
|
||||
void droppingAnIcebergTableDeletesItsDataToo() {
|
||||
glue.createTable(r -> r.databaseName("lake").tableInput(t -> t.name("ice")
|
||||
.tableType("EXTERNAL_TABLE")
|
||||
.parameters(Map.of("table_type", "ICEBERG", "metadata_location", "s3://" + bucket + "/ice/metadata/v1.json"))
|
||||
.storageDescriptor(sd -> sd.location("s3://" + bucket + "/ice"))));
|
||||
for (String key : List.of("ice/metadata/v1.json", "ice/data/part-0.parquet", "iceberg-other/keep.txt")) {
|
||||
s3.putObject(r -> r.bucket(bucket).key(key), RequestBody.fromString("x"));
|
||||
}
|
||||
|
||||
succeeds("DROP TABLE ice");
|
||||
|
||||
assertEquals(List.of("iceberg-other/keep.txt"), s3.listObjectsV2(r -> r.bucket(bucket)).contents().stream()
|
||||
.map(o -> o.key()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropPartitionAndDropTableRemoveMetadataOnly() {
|
||||
createEventsTable();
|
||||
succeeds("ALTER TABLE events ADD PARTITION (dt = '2026-11-14', region = 'eu') PARTITION (dt = '2026-11-15', region = 'us')");
|
||||
s3.putObject(r -> r.bucket(bucket).key("events/dt=2026-11-14/region=eu/part-0.tsv"), RequestBody.fromString("1\n"));
|
||||
|
||||
succeeds("ALTER TABLE events DROP PARTITION (dt = '2026-11-15', region = 'us')");
|
||||
assertEquals(List.of("2026-11-14"), glue.getPartitions(r -> r.databaseName("lake").tableName("events"))
|
||||
.partitions().stream().map(Partition::values).map(List::getFirst).toList());
|
||||
fails("ALTER TABLE events DROP PARTITION (dt = '2026-11-15', region = 'us')");
|
||||
succeeds("ALTER TABLE events DROP IF EXISTS PARTITION (dt = '2026-11-15', region = 'us')");
|
||||
|
||||
succeeds("DROP TABLE lake.events");
|
||||
assertThrows(EntityNotFoundException.class, () -> glue.getTable(r -> r.databaseName("lake").name("events")));
|
||||
assertEquals(1, s3.listObjectsV2(r -> r.bucket(bucket).prefix("events/")).keyCount(),
|
||||
"the table's data stays where it was");
|
||||
assertTrue(fails("DROP TABLE events").contains("not found"));
|
||||
succeeds("DROP TABLE IF EXISTS events");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package io.floci.aws.services.athena;
|
||||
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.glue.model.Column;
|
||||
import io.floci.aws.services.glue.model.StorageDescriptor;
|
||||
import io.floci.aws.services.glue.model.Table;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AthenaHiveDdlTest {
|
||||
|
||||
private static AthenaHiveDdl.CreateTable create(String sql) {
|
||||
return assertInstanceOf(AthenaHiveDdl.CreateTable.class, AthenaHiveDdl.parse(sql));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createExternalTableWithDelimitedTextBecomesTheGlueTableHiveStores() {
|
||||
AthenaHiveDdl.CreateTable create = create("""
|
||||
-- a headerless TSV table
|
||||
CREATE EXTERNAL TABLE IF NOT EXISTS Lake.`Events` (
|
||||
`Id` BIGINT COMMENT 'the id',
|
||||
payload STRUCT<a:INT, b:ARRAY<STRING>>,
|
||||
amount DECIMAL(10, 2)
|
||||
)
|
||||
COMMENT 'raw events'
|
||||
PARTITIONED BY (dt STRING, region STRING)
|
||||
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\\t' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'
|
||||
NULL DEFINED AS ''
|
||||
STORED AS TEXTFILE
|
||||
LOCATION 's3://lake/events/'
|
||||
TBLPROPERTIES ('classification'='csv', 'skip.header.line.count'='0');
|
||||
""");
|
||||
|
||||
assertEquals(new AthenaHiveDdl.TableRef("lake", "events"), create.ref());
|
||||
assertTrue(create.ifNotExists());
|
||||
Table table = create.table();
|
||||
assertEquals("events", table.getName());
|
||||
assertEquals("EXTERNAL_TABLE", table.getTableType());
|
||||
assertEquals("raw events", table.getDescription());
|
||||
assertEquals(Map.of("classification", "csv", "skip.header.line.count", "0", "EXTERNAL", "TRUE"),
|
||||
table.getParameters());
|
||||
StorageDescriptor descriptor = table.getStorageDescriptor();
|
||||
assertEquals(List.of("id:bigint:the id", "payload:struct<a:int,b:array<string>>:null", "amount:decimal(10,2):null"),
|
||||
descriptor.getColumns().stream().map(c -> c.getName() + ":" + c.getType() + ":" + c.getComment()).toList());
|
||||
assertEquals(List.of("dt", "region"), table.getPartitionKeys().stream().map(Column::getName).toList());
|
||||
assertEquals("s3://lake/events/", descriptor.getLocation());
|
||||
assertEquals("org.apache.hadoop.mapred.TextInputFormat", descriptor.getInputFormat());
|
||||
assertEquals("org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat", descriptor.getOutputFormat());
|
||||
assertEquals("org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe",
|
||||
descriptor.getSerdeInfo().getSerializationLibrary());
|
||||
assertEquals(Map.of("field.delim", "\t", "serialization.format", "\t", "escape.delim", "\\",
|
||||
"line.delim", "\n", "serialization.null.format", ""), descriptor.getSerdeInfo().getParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusteredAndSortedByKeepTheirColumnsAndDirections() {
|
||||
StorageDescriptor descriptor = create("""
|
||||
CREATE EXTERNAL TABLE t (a int, b string) CLUSTERED BY (a) SORTED BY (a DESC, b) INTO 4 BUCKETS
|
||||
STORED AS PARQUET LOCATION 's3://b/t/'
|
||||
""").table().getStorageDescriptor();
|
||||
|
||||
assertEquals(List.of("a"), descriptor.getBucketColumns());
|
||||
assertEquals(4, descriptor.getNumberOfBuckets());
|
||||
assertEquals(List.of("a:0", "b:1"),
|
||||
descriptor.getSortColumns().stream().map(o -> o.getColumn() + ":" + o.getSortOrder()).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void storedAsPicksTheFormatsInputOutputAndSerde() {
|
||||
StorageDescriptor parquet = create("CREATE EXTERNAL TABLE t (a int) STORED AS PARQUET LOCATION 's3://b/t/'")
|
||||
.table().getStorageDescriptor();
|
||||
|
||||
assertEquals("org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat", parquet.getInputFormat());
|
||||
assertEquals("org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe",
|
||||
parquet.getSerdeInfo().getSerializationLibrary());
|
||||
assertEquals(Map.of(), parquet.getSerdeInfo().getParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitSerdeAndInputFormatAreKeptAsWritten() {
|
||||
StorageDescriptor json = create("""
|
||||
CREATE EXTERNAL TABLE t (a int)
|
||||
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe' WITH SERDEPROPERTIES ("ignore.malformed.json"="true")
|
||||
STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
|
||||
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat'
|
||||
LOCATION 's3://b/t/'
|
||||
""").table().getStorageDescriptor();
|
||||
|
||||
assertEquals("org.openx.data.jsonserde.JsonSerDe", json.getSerdeInfo().getSerializationLibrary());
|
||||
assertEquals(Map.of("ignore.malformed.json", "true"), json.getSerdeInfo().getParameters());
|
||||
assertEquals("org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat", json.getOutputFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutRowFormatOrStoredAsATableIsTextWithTheDefaultSerde() {
|
||||
StorageDescriptor text = create("CREATE EXTERNAL TABLE t (a int) LOCATION 's3://b/t/'").table().getStorageDescriptor();
|
||||
|
||||
assertEquals("org.apache.hadoop.mapred.TextInputFormat", text.getInputFormat());
|
||||
assertEquals(Map.of("serialization.format", "1"), text.getSerdeInfo().getParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createTableWithoutExternalIsLeftToIcebergAndCtasOrRefused() {
|
||||
assertNull(AthenaHiveDdl.parse("CREATE TABLE t (a int) LOCATION 's3://b/t/' TBLPROPERTIES ('table_type'='ICEBERG')"));
|
||||
assertNull(AthenaHiveDdl.parse("CREATE TABLE t WITH (format = 'PARQUET') AS SELECT 1 AS a"));
|
||||
assertNull(AthenaHiveDdl.parse("CREATE TABLE t AS SELECT 1 AS a"));
|
||||
AwsException hive = assertThrows(AwsException.class,
|
||||
() -> AthenaHiveDdl.parse("CREATE TABLE t (a int) STORED AS PARQUET LOCATION 's3://b/t/'"));
|
||||
assertTrue(hive.getMessage().contains("EXTERNAL"), hive.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropRepairAndShowNameTheirTable() {
|
||||
AthenaHiveDdl.DropTable drop = assertInstanceOf(AthenaHiveDdl.DropTable.class,
|
||||
AthenaHiveDdl.parse("DROP TABLE IF EXISTS lake.events;"));
|
||||
assertTrue(drop.ifExists());
|
||||
assertEquals(new AthenaHiveDdl.TableRef("lake", "events"), drop.ref());
|
||||
assertFalse(assertInstanceOf(AthenaHiveDdl.DropTable.class, AthenaHiveDdl.parse("drop table t")).ifExists());
|
||||
assertEquals(new AthenaHiveDdl.TableRef(null, "events"),
|
||||
AthenaHiveDdl.parse("MSCK REPAIR TABLE `Events`").ref());
|
||||
assertInstanceOf(AthenaHiveDdl.ShowPartitions.class, AthenaHiveDdl.parse("SHOW PARTITIONS lake.events"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void alterTableAddAndDropPartitionsKeepEachSpecAndLocation() {
|
||||
AthenaHiveDdl.AddPartitions add = assertInstanceOf(AthenaHiveDdl.AddPartitions.class, AthenaHiveDdl.parse("""
|
||||
ALTER TABLE events ADD IF NOT EXISTS
|
||||
PARTITION (dt = '2026-11-14', region = 'eu')
|
||||
PARTITION (dt = '2026-11-15', region = 'us') LOCATION 's3://lake/custom/'
|
||||
"""));
|
||||
assertTrue(add.ifNotExists());
|
||||
assertEquals(List.of(new AthenaHiveDdl.PartitionSpec(Map.of("dt", "2026-11-14", "region", "eu"), null),
|
||||
new AthenaHiveDdl.PartitionSpec(Map.of("dt", "2026-11-15", "region", "us"), "s3://lake/custom/")),
|
||||
add.partitions());
|
||||
|
||||
AthenaHiveDdl.DropPartitions drop = assertInstanceOf(AthenaHiveDdl.DropPartitions.class,
|
||||
AthenaHiveDdl.parse("ALTER TABLE events DROP PARTITION (year = 2026), PARTITION (year = 2025)"));
|
||||
assertFalse(drop.ifExists());
|
||||
assertEquals(List.of(Map.of("year", "2026"), Map.of("year", "2025")),
|
||||
drop.partitions().stream().map(AthenaHiveDdl.PartitionSpec::values).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherAlterTableStatementsAreRefusedClearly() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> AthenaHiveDdl.parse("ALTER TABLE events ADD COLUMNS (b string)"));
|
||||
|
||||
assertTrue(e.getMessage().contains("ADD PARTITION"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queriesAndOtherStatementsAreNotHiveDdl() {
|
||||
assertNull(AthenaHiveDdl.parse("SELECT a || b, 'x' FROM t WHERE c ~ 'y'"));
|
||||
assertNull(AthenaHiveDdl.parse("SHOW TABLES IN lake"));
|
||||
assertNull(AthenaHiveDdl.parse("CREATE DATABASE lake"));
|
||||
assertNull(AthenaHiveDdl.parse("DROP DATABASE lake"));
|
||||
assertNull(AthenaHiveDdl.parse("INSERT INTO t VALUES (1)"));
|
||||
assertNull(AthenaHiveDdl.parse(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMalformedHiveStatementSaysWhatIsWrong() {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> AthenaHiveDdl.parse("CREATE EXTERNAL TABLE t (a int LOCATION 's3://b/'"));
|
||||
|
||||
assertEquals("InvalidRequestException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user