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 #27490

Closed
wants to merge 14 commits into from
Closed
34 changes: 34 additions & 0 deletions packages/@aws-cdk/aws-glue-alpha/README.md
Copy link
Contributor

@vinayak-kukreja vinayak-kukreja Dec 6, 2023

Choose a reason for hiding this comment

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

Hey, @guyernest I see the following error,

@aws-cdk/aws-glue-alpha: README.md:293 MD028/no-blanks-blockquote Blank line inside blockquote
@aws-cdk/aws-glue-alpha: Error: ''"'"'/usr/bin/node'"'"'' -- /codebuild/output/src2608913370/src/github.com/aws/aws-cdk/node_modules/markdownlint-cli/markdownlint.js --config /codebuild/output/src2608913370/src/github.com/aws/aws-cdk/tools/@aws-cdk/cdk-build-tools/config/markdownlint.json README.md exited with error code 1
@aws-cdk/aws-glue-alpha: Build failed.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@aws-cdk/aws-glue-alpha: �[2K�[1G@aws-cdk/aws-glue-alpha: �[31merror�[39m Command failed with exit code 1.
@aws-cdk/aws-glue-alpha: �[2K�[1G@aws-cdk/aws-glue-alpha: �[34minfo�[39m Visit �[1mhttps://yarnpkg.com/en/docs/cli/run�[22m for documentation about this command.

I am curious, are you seeing these fail in PR builds or local builds too?

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';
135 changes: 135 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,135 @@
/*
* @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