Skip to content

Commit f07a719

Browse files
authored
feat/add support parition type by option (#31)
* add partition by option * version bump, readme update * revert options to private * readme update * deps update
1 parent 539f978 commit f07a719

File tree

7 files changed

+856
-1464
lines changed

7 files changed

+856
-1464
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Minor Changes
66

77
- support for point type, tuple type and bug fixes
8+
- Add support for partition_by option
89

910
## 3.0.0
1011

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ To start using ClickHouse-Schema in your projects, follow these steps:
9797
- Use `<your_schema>.GetOptions()` to access the options passed when creating the table schema.
9898
- Use `<your_schema>.GetCreateTableQueryAsList()` to get the `CREATE TABLE` query as a list of strings, which can be helpful for debugging or logging.
9999
100+
## Schema Options
101+
102+
When creating a schema, you can provide the following options:
103+
104+
- **`table_name`** (required): The name of the table in ClickHouse
105+
- **`primary_key`** (optional): The primary key for the table. If not specified, `order_by` must be specified
106+
- **`order_by`** (optional): The ORDER BY clause for the table. If not specified, `primary_key` must be specified
107+
- **`database`** (optional): The database to use for the table
108+
- **`on_cluster`** (optional): The name of the cluster to use for the table
109+
- **`engine`** (optional): The engine to use for the table, default is `MergeTree()`
110+
- **`partition_by`** (optional): The partition expression for the table. Can be any valid ClickHouse expression:
111+
- Single expression: `"toYYYYMM(visitDate)"` or `"visitDate"`
112+
- Tuple of expressions: `"(toMonday(startDate), eventType)"`
113+
- Complex expressions: `"sipHash64(userId) % 16"`
114+
- **`additional_options`** (optional): An array of strings that are appended to the end of the CREATE TABLE query (e.g., `['COMMENT \'Table comment\'']`)
115+
100116
## Supported Types
101117
102118
- Integer (signed and unsigned integers): `UInt8, UInt16, UInt32, UInt64, UInt128, UInt256, Int8, Int16, Int32, Int64, Int128, Int256` types

core/clickhouse_schema.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import { type ChDataType } from '@clickhouse-schema-data-types/index'
22

3-
type IsType<T, U> = T extends U ? true : false
4-
5-
// Distributive conditional type that checks if T is of any type in union U
6-
type OmitKeysOfTypes<T extends ChSchemaDefinition, U> = {
7-
[K in keyof T as IsType< T[K]['type']['typeStr'], U> extends true ? never : K]: T[K]
8-
}
9-
103
export interface SchemaValue {
11-
type: ChDataType
4+
type: ChDataType
125
}
13-
export type ChSchemaDefinition = Record<string, SchemaValue>
14-
6+
export type ChSchemaDefinition = {[key: string]: SchemaValue}
157
/**
168
* ChSchemaOptions is used to define the options for a clickhouse table schema.
179
*
@@ -21,15 +13,17 @@ export type ChSchemaDefinition = Record<string, SchemaValue>
2113
* @param primary_key is the primary key for the table. if not specified, order_by must be specified
2214
* @param order_by is the order by clause for the table. if not specified, primary_key must be specified
2315
* @param engine is the engine to use for the table, default is MergeTree()
16+
* @param partition_by is the partition expression for the table. Can be any valid ClickHouse expression (e.g., "toYYYYMM(date_column)", "(toMonday(StartDate), EventType)", "sipHash64(userId) % 16")
2417
* @param additional_options is an string array of options that are appended to the end of the create table query
2518
*/
2619
export interface ChSchemaOptions<T extends ChSchemaDefinition> {
2720
database?: string
2821
table_name: string
2922
on_cluster?: string
30-
primary_key?: keyof OmitKeysOfTypes<T, 'Object(\'JSON\')' >
31-
order_by?: keyof OmitKeysOfTypes<T, 'Object(\'JSON\')' >
23+
primary_key?: keyof T
24+
order_by?: keyof T
3225
engine?: string
26+
partition_by?: string
3327
additional_options?: string[]
3428
}
3529

@@ -98,11 +92,11 @@ export class ClickhouseSchema<SchemaDefinition extends ChSchemaDefinition> imple
9892
if (this.options.additional_options !== undefined) {
9993
additionalOptions = `${this.options.additional_options.join('\n')}`
10094
}
101-
10295
const createTableQuery = [
10396
`CREATE TABLE IF NOT EXISTS ${this.options.database !== undefined ? `${this.options.database}.` : ''}${this.options.table_name}${this.options.on_cluster !== undefined ? ` ON CLUSTER ${this.options.on_cluster}` : ''}`,
10497
`(\n${columns}\n)`,
10598
`ENGINE = ${engine}`,
99+
this.options.partition_by !== undefined ? `PARTITION BY ${this.options.partition_by}` : '',
106100
this.options.order_by !== undefined ? `ORDER BY ${this.options.order_by.toString()}` : '',
107101
this.options.primary_key !== undefined ? `PRIMARY KEY ${this.options.primary_key.toString()}` : '',
108102
additionalOptions

core/infer_schema_type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
55
type Infer<T extends ChDataType> = T['typeScriptType']
66

77
/** InferSchemaClickhouseSchemaType is a type that takes a ClickhouseSchema and returns the typescript that it represents */
8-
export type InferClickhouseSchemaType<T extends ClickhouseSchema<ChSchemaDefinition>> = { [K in keyof T['schema']]: Infer<T['schema'][K]['type']> }
8+
export type InferClickhouseSchemaType<T extends ClickhouseSchema<any>> = { [K in keyof T['schema']]: Infer<T['schema'][K]['type']> }

0 commit comments

Comments
 (0)