-
Notifications
You must be signed in to change notification settings - Fork 29
ClickHouse mirror #823
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
base: main
Are you sure you want to change the base?
ClickHouse mirror #823
Changes from 14 commits
e9477fd
d37bdde
22b659e
189424f
107f7c4
3c3b8df
d673b9d
4f80a00
e25caff
6e93e4b
92c3a53
35c16f7
b09515d
196b19f
46cf29a
91255b7
cf245a7
a95e89c
a6d4680
e6c6866
4b9fbb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| type t = { | ||
| initialize: ( | ||
| ~chainConfigs: array<Config.chain>=?, | ||
| ~entities: array<Internal.entityConfig>=?, | ||
| ~enums: array<Internal.enumConfig<Internal.enum>>=?, | ||
| ) => promise<unit>, | ||
| setOrThrow: 'item. ( | ||
| ~items: array<'item>, | ||
| ~table: Table.table, | ||
| ~itemSchema: S.t<'item>, | ||
| ) => promise<unit>, | ||
| } | ||
|
|
||
| let makeClickHouse = (~host, ~database, ~username, ~password): t => { | ||
| let client = ClickHouse.createClient({ | ||
| url: host, | ||
| username, | ||
| password, | ||
| }) | ||
|
|
||
| { | ||
| initialize: (~chainConfigs as _=[], ~entities=[], ~enums=[]) => { | ||
| ClickHouse.initialize(client, ~database, ~entities, ~enums) | ||
| }, | ||
| setOrThrow: (~items, ~table, ~itemSchema) => { | ||
| ClickHouse.setOrThrow(client, ~items, ~table, ~itemSchema, ~database) | ||
| }, | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,169 @@ | ||||||||||||||||||||||||||||||||||||||||||
| // ClickHouse client bindings for @clickhouse/client | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type client | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type clientConfig = { | ||||||||||||||||||||||||||||||||||||||||||
| url?: string, | ||||||||||||||||||||||||||||||||||||||||||
| database?: string, | ||||||||||||||||||||||||||||||||||||||||||
| username?: string, | ||||||||||||||||||||||||||||||||||||||||||
| password?: string, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type execParams = {query: string} | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @module("@clickhouse/client") | ||||||||||||||||||||||||||||||||||||||||||
| external createClient: clientConfig => client = "createClient" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @send | ||||||||||||||||||||||||||||||||||||||||||
| external exec: (client, execParams) => promise<unit> = "exec" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @send | ||||||||||||||||||||||||||||||||||||||||||
| external close: client => promise<unit> = "close" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| type insertParams<'a> = { | ||||||||||||||||||||||||||||||||||||||||||
| table: string, | ||||||||||||||||||||||||||||||||||||||||||
| values: array<'a>, | ||||||||||||||||||||||||||||||||||||||||||
| format: string, | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @send | ||||||||||||||||||||||||||||||||||||||||||
| external insert: (client, insertParams<'a>) => promise<unit> = "insert" | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let getClickHouseFieldType = ( | ||||||||||||||||||||||||||||||||||||||||||
| ~fieldType: Table.fieldType, | ||||||||||||||||||||||||||||||||||||||||||
| ~isNullable: bool, | ||||||||||||||||||||||||||||||||||||||||||
| ~isArray: bool, | ||||||||||||||||||||||||||||||||||||||||||
| ): string => { | ||||||||||||||||||||||||||||||||||||||||||
| let baseType = switch fieldType { | ||||||||||||||||||||||||||||||||||||||||||
| | Int32 => "Int32" | ||||||||||||||||||||||||||||||||||||||||||
| | Uint32 => "UInt32" | ||||||||||||||||||||||||||||||||||||||||||
| | Serial => "Int32" | ||||||||||||||||||||||||||||||||||||||||||
| | BigInt({?precision}) => | ||||||||||||||||||||||||||||||||||||||||||
| switch precision { | ||||||||||||||||||||||||||||||||||||||||||
| | None => "String" // Fallback for unbounded BigInt | ||||||||||||||||||||||||||||||||||||||||||
| | Some(precision) => | ||||||||||||||||||||||||||||||||||||||||||
| if precision > 38 { | ||||||||||||||||||||||||||||||||||||||||||
| "String" | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| `Decimal(${precision->Js.Int.toString},0)` | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| | BigDecimal({?config}) => | ||||||||||||||||||||||||||||||||||||||||||
| switch config { | ||||||||||||||||||||||||||||||||||||||||||
| | None => "String" // Fallback for unbounded BigInt | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incorrect comment. The comment says "Fallback for unbounded BigInt" but this is the Apply this diff: - | None => "String" // Fallback for unbounded BigInt
+ | None => "String" // Fallback for BigDecimal without config🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| | Some((precision, scale)) => | ||||||||||||||||||||||||||||||||||||||||||
| if precision > 38 || scale > precision { | ||||||||||||||||||||||||||||||||||||||||||
| "String" | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| `Decimal(${precision->Js.Int.toString},${scale->Js.Int.toString})` | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+60
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate that precision and scale are non-negative. The BigDecimal branch correctly checks ClickHouse's upper limits (precision ≤ 38, scale ≤ precision), but doesn't verify that both values are non-negative. Negative values would bypass validation and produce invalid Decimal types. Apply this diff to add non-negative validation: | BigDecimal({?config}) =>
switch config {
| None => "String" // Fallback for unbounded BigInt
| Some((precision, scale)) =>
- if precision > 38 || scale > precision {
+ if precision < 0 || scale < 0 || precision > 38 || scale > precision {
"String"
} else {
`Decimal(${precision->Js.Int.toString},${scale->Js.Int.toString})`
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| | Boolean => "Bool" | ||||||||||||||||||||||||||||||||||||||||||
| | Number => "Float64" | ||||||||||||||||||||||||||||||||||||||||||
| | String => "String" | ||||||||||||||||||||||||||||||||||||||||||
| | Json => "String" | ||||||||||||||||||||||||||||||||||||||||||
| | Date => "DateTime64(3, 'UTC')" | ||||||||||||||||||||||||||||||||||||||||||
| | Enum(_) => "String" | ||||||||||||||||||||||||||||||||||||||||||
| | Entity(_) => "String" | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let baseType = if isArray { | ||||||||||||||||||||||||||||||||||||||||||
| `Array(${baseType})` | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| baseType | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| isNullable ? `Nullable(${baseType})` : baseType | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+92
to
+98
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't emit ClickHouse forbids wrapping composite types in - let baseType = if isArray {
- `Array(${baseType})`
- } else {
- baseType
- }
-
- isNullable ? `Nullable(${baseType})` : baseType
+ if isArray {
+ let elementType = if isNullable { `Nullable(${baseType})` } else { baseType }
+ `Array(${elementType})`
+ } else {
+ isNullable ? `Nullable(${baseType})` : baseType
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let setOrThrow = async ( | ||||||||||||||||||||||||||||||||||||||||||
| client, | ||||||||||||||||||||||||||||||||||||||||||
| ~items: array<'item>, | ||||||||||||||||||||||||||||||||||||||||||
| ~table: Table.table, | ||||||||||||||||||||||||||||||||||||||||||
| ~itemSchema: S.t<'item>, | ||||||||||||||||||||||||||||||||||||||||||
| ~database: string, | ||||||||||||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||||||||||||
| if items->Array.length === 0 { | ||||||||||||||||||||||||||||||||||||||||||
| () | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| // Convert entity updates to ClickHouse row format | ||||||||||||||||||||||||||||||||||||||||||
| let values = items->Js.Array2.map(item => { | ||||||||||||||||||||||||||||||||||||||||||
| item->S.reverseConvertOrThrow(itemSchema) | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| await client->insert({ | ||||||||||||||||||||||||||||||||||||||||||
| table: `${database}.\`${table.tableName}\``, | ||||||||||||||||||||||||||||||||||||||||||
| values, | ||||||||||||||||||||||||||||||||||||||||||
| format: "JSONEachRow", | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||
| | exn => | ||||||||||||||||||||||||||||||||||||||||||
| raise( | ||||||||||||||||||||||||||||||||||||||||||
| Persistence.StorageError({ | ||||||||||||||||||||||||||||||||||||||||||
| message: `Failed to insert items into ClickHouse table "${table.tableName}"`, | ||||||||||||||||||||||||||||||||||||||||||
| reason: exn->Utils.prettifyExn, | ||||||||||||||||||||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Generate CREATE TABLE query for entity history table | ||||||||||||||||||||||||||||||||||||||||||
| let makeCreateHistoryTableQuery = (entity: Internal.entityConfig, ~database: string) => { | ||||||||||||||||||||||||||||||||||||||||||
| let historyTable = entity.entityHistory.table | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let fieldDefinitions = | ||||||||||||||||||||||||||||||||||||||||||
| historyTable.fields | ||||||||||||||||||||||||||||||||||||||||||
| ->Belt.Array.keepMap(field => { | ||||||||||||||||||||||||||||||||||||||||||
| switch field { | ||||||||||||||||||||||||||||||||||||||||||
| | Field(field) => | ||||||||||||||||||||||||||||||||||||||||||
| Some({ | ||||||||||||||||||||||||||||||||||||||||||
| let fieldName = field->Table.getDbFieldName | ||||||||||||||||||||||||||||||||||||||||||
| let clickHouseType = getClickHouseFieldType( | ||||||||||||||||||||||||||||||||||||||||||
| ~fieldType=field.fieldType, | ||||||||||||||||||||||||||||||||||||||||||
| ~isNullable=field.isNullable, | ||||||||||||||||||||||||||||||||||||||||||
| ~isArray=field.isArray, | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| `\`${fieldName}\` ${clickHouseType}` | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| | DerivedFrom(_) => None | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| ->Js.Array2.joinWith(",\n ") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let tableName = historyTable.tableName | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| `CREATE TABLE IF NOT EXISTS ${database}.\`${tableName}\` ( | ||||||||||||||||||||||||||||||||||||||||||
| ${fieldDefinitions} | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| ENGINE = MergeTree() | ||||||||||||||||||||||||||||||||||||||||||
| ORDER BY (id, ${EntityHistory.checkpointIdFieldName})` | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Initialize ClickHouse tables for entities | ||||||||||||||||||||||||||||||||||||||||||
| let initialize = async ( | ||||||||||||||||||||||||||||||||||||||||||
| client, | ||||||||||||||||||||||||||||||||||||||||||
| ~database: string, | ||||||||||||||||||||||||||||||||||||||||||
| ~entities: array<Internal.entityConfig>, | ||||||||||||||||||||||||||||||||||||||||||
| ~enums as _: array<Internal.enumConfig<Internal.enum>>, | ||||||||||||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| await client->exec({query: `DROP DATABASE IF EXISTS ${database}`}) | ||||||||||||||||||||||||||||||||||||||||||
| await client->exec({query: `CREATE DATABASE ${database}`}) | ||||||||||||||||||||||||||||||||||||||||||
| await client->exec({query: `USE ${database}`}) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+228
to
+232
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stop dropping the ClickHouse database on init
- await client->exec({query: `DROP DATABASE IF EXISTS ${database}`})
- await client->exec({query: `CREATE DATABASE ${database}`})
- await client->exec({query: `USE ${database}`})
+ await client->exec({query: `CREATE DATABASE IF NOT EXISTS ${database}`})
+ await client->exec({query: `USE ${database}`})📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| await Promise.all( | ||||||||||||||||||||||||||||||||||||||||||
| entities->Belt.Array.map(entity => | ||||||||||||||||||||||||||||||||||||||||||
| client->exec({query: makeCreateHistoryTableQuery(entity, ~database)}) | ||||||||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||||||||
| )->Promise.ignoreValue | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Logging.trace("ClickHouse mirror initialization completed successfully") | ||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||
| | exn => { | ||||||||||||||||||||||||||||||||||||||||||
| Logging.errorWithExn(exn, "Failed to initialize ClickHouse mirror") | ||||||||||||||||||||||||||||||||||||||||||
| Js.Exn.raiseError("ClickHouse initialization failed") | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add non-negative validation for BigInt precision.
The precision validation checks the upper bound (38) but doesn't verify that precision is non-negative. A negative precision would bypass validation and produce an invalid Decimal type.
Apply this diff to add validation:
| BigInt({?precision}) => switch precision { | None => "String" // Fallback for unbounded BigInt | Some(precision) => - if precision > 38 { + if precision < 0 || precision > 38 { "String" } else { `Decimal(${precision->Js.Int.toString},0)` } }📝 Committable suggestion
🤖 Prompt for AI Agents