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

feat(glue-alpha): adding partition projection #27447

Closed
wants to merge 6 commits into from
Closed
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
34 changes: 34 additions & 0 deletions packages/@aws-cdk/aws-glue-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,40 @@ new glue.S3Table(this, 'MyTable', {
});
```

### Partition Projection

From the [Athena documentation](https://docs.aws.amazon.com/athena/latest/ug/partition-projection.html):
> You can use partition projection in Athena to speed up query processing of highly partitioned tables and automate partition management.

> In partition projection, Athena calculates partition values and locations using the table properties that you configure directly on your table in AWS Glue. The table properties allow Athena to 'project', or determine, the necessary partition information instead of having to do a more time-consuming metadata lookup in the AWS Glue Data Catalog. Because in-memory operations are often faster than remote operations, partition projection can reduce the runtime of queries against highly partitioned tables. Depending on the specific characteristics of the query and underlying data, partition projection can significantly reduce query runtime for queries that are constrained on partition metadata retrieval.

```ts
declare const myDatabase: glue.Database;
const partitionProjection = new DatePartitionProjection(
'datehour',
's3://DOC-EXAMPLE-BUCKET/prefix/${datehour}/',
'2021/01/01,NOW',
'yyyy/MM/dd',
1,
DateIntervalUnit.DAYS,
);

new glue.S3Table(this, 'MyTable', {
database: myDatabase,
columns: [{
name: 'col1',
type: glue.Schema.STRING,
}],
partitionKeys: [{
name: 'datehour',
type: glue.Schema.STRING,
}],
partitionProjection,
dataFormat: glue.DataFormat.JSON,
});
```


### Partition Indexes

Another way to improve query performance is to specify partition indexes. If no partition indexes are
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-glue-alpha/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export * from './s3-table';
export * from './schema';
export * from './security-configuration';
export * from './storage-parameter';
export * from './partition-projection';
export * from './table-base';
export * from './table-deprecated';
141 changes: 141 additions & 0 deletions packages/@aws-cdk/aws-glue-alpha/lib/partition-projection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* @see https://docs.aws.amazon.com/athena/latest/ug/partition-projection-supported-types.html#partition-projection-date-type
*/
export enum PartitionProjectionType {
ENUM_TYPE = 'enum',
INTEGER_TYPE = 'integer',
DATE_TYPE = 'date',
INJECTED_TYPE = 'injected',
}

/*
* Dynamic Partition Projection Class
*
* @see https://docs.aws.amazon.com/athena/latest/ug/partition-projection.html
*
*/
export abstract class PartitionProjection {
constructor(
public readonly type: PartitionProjectionType,
public readonly columnName: string,
public readonly storageLocationTemplate: string) {}

public getParameterKey(paramName: string): string {
return `${this.columnName}.${paramName}`;
}
public toOutputFormat(): any {
throw new Error('Method not implemented.');
}
}

/*
* @see https://docs.aws.amazon.com/athena/latest/ug/partition-projection-supported-types.html#partition-projection-date-type
* A time unit word that represents the serialized form of a ChronoUnit.
* Possible values are YEARS, MONTHS, WEEKS, DAYS, HOURS, MINUTES, SECONDS, or MILLISECONDS. These values are case insensitive.
*/
export enum DateIntervalUnit {
YEARS = 'YEARS',
MONTHS = 'MONTHS',
WEEKS = 'WEEKS',
DAYS = 'DAYS',
HOURS = 'HOURS',
MINUTES = 'MINUTES',
SECONDS = 'SECONDS',
MILLISECONDS = 'MILLISECONDS',
}

export class DatePartitionProjection extends PartitionProjection {
constructor(
public readonly columnName: string,
public readonly storageLocationTemplate: string,
public readonly range: string,
public readonly format: string,
public readonly interval?: number,
public readonly intervalUnit?: DateIntervalUnit,
) {
super(
PartitionProjectionType.DATE_TYPE,
columnName,
storageLocationTemplate);
}

toOutputFormat(): any {
const baseKey = `projection.${this.columnName}`;

return {
['projection.enabled']: true,
['storage.location.template']: this.storageLocationTemplate,
[`${baseKey}.type`]: this.type,
[`${baseKey}.format`]: this.format,
[`${baseKey}.range`]: this.range,
[`${baseKey}.interval`]: this.interval ? String(this.interval) : undefined,
[`${baseKey}.interval.unit`]: this.intervalUnit ? this.intervalUnit : undefined,
};
}
}

export class IntegerPartitionProjection extends PartitionProjection {
constructor(
public readonly columnName: string,
public readonly storageLocationTemplate: string,
public readonly range: string,
public readonly interval?: number,
public readonly digits?: number)
{
super(
PartitionProjectionType.INTEGER_TYPE,
columnName,
storageLocationTemplate);
}

toOutputFormat(): any {
const baseKey = `projection.${this.columnName}`;

return {
['projection.enabled']: true,
['storage.location.template']: this.storageLocationTemplate,
[`${baseKey}.type`]: this.type,
[`${baseKey}.range`]: this.range,
[`${baseKey}.interval`]: this.interval ? String(this.interval) : undefined,
[`${baseKey}.digits`]: this.digits ? String(this.digits) : undefined,
};
}

}


export class EnumPartitionProjection extends PartitionProjection {
constructor(
public readonly columnName: string,
public readonly storageLocationTemplate: string,
public readonly values: string) {
super(
PartitionProjectionType.ENUM_TYPE,
columnName,
storageLocationTemplate
);
}

toOutputFormat(): any {
const baseKey = `projection.${this.columnName}`;

return {
['projection.enabled']: true,
['storage.location.template']: this.storageLocationTemplate,
[`${baseKey}.type`]: this.type,
[`${baseKey}.values`]: this.values,
};
}

}

export class InjectedPartitionProjection extends PartitionProjection {
constructor(
public readonly columnName: string,
public readonly storageLocationTemplate: string,) {
super(
PartitionProjectionType.INJECTED_TYPE,
columnName,
storageLocationTemplate);
}
}
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-glue-alpha/lib/s3-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { Column } from './schema';
import { PartitionIndex, TableBase, TableBaseProps } from './table-base';
import { PartitionProjection } from './partition-projection';

/**
* Encryption options for a Table.
Expand Down Expand Up @@ -54,6 +55,12 @@ export interface S3TableProps extends TableBaseProps {
*/
readonly s3Prefix?: string;

/**
* Optional Partition Projection for this table.
* TODO: Add the option for multiple partition projections.
*/
readonly partitionProjection?: PartitionProjection;

/**
* The kind of encryption to secure the data with.
*
Expand Down Expand Up @@ -137,11 +144,11 @@ export class S3Table extends TableBase {

partitionKeys: renderColumns(props.partitionKeys),

parameters: {
parameters: Object.assign({
'classification': props.dataFormat.classificationString?.value,
'has_encrypted_data': true,
'partition_filtering.enabled': props.enablePartitionFiltering,
},
}, props.partitionProjection ? props.partitionProjection.toOutputFormat() : {}),
storageDescriptor: {
location: `s3://${this.bucket.bucketName}/${this.s3Prefix}`,
compressed: this.compressed,
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-glue-alpha/test/integ.table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ new glue.S3Table(stack, 'MyTableWithStorageDescriptorParameters', {
],
});

new glue.S3Table(stack, 'MyTableWithPartitionProjectionParameters', {
database,
bucket,
tableName: 'table_with_partition_projection_parameters',
columns,
dataFormat: glue.DataFormat.JSON,
partitionProjection: new glue.DatePartitionProjection(
'columnName',
's3://DOC-EXAMPLE-BUCKET/prefix/${columnName}/',
'NOW-3YEARS,NOW',
'dd-MM-yyyy',
1,
glue.DateIntervalUnit.DAYS,
)
});

new glue.Table(stack, 'MyDeprecatedTable', {
database,
bucket,
Expand Down
Loading
Loading