Skip to content

query-api(clickhouse): use type constraints for strongly typed queries

Joe Shaw requested to merge joe/generic_clickhouse_query_api into main

Some fun with generics to provide a strongly typed query API for ClickHouse that serializes types correctly and in a tested way. Related to Tech debt: migrate ClickHouse query builder to ... (#2720)

The aim is to provide a cleaner API for both ? placeholder syntax and named native parameters with some compile time safety. This also supports cleaning up of TypeValPair structs for things like arrays, where the types are strings rather than inferred from the compiled types in use. Though things can still be overridden in this way.

Extensible Literal type allows external type definitions, but mainly simple additions of new custom types in the package are used.

Go builtins (e.g. int64) still require some runtime reflection, as we can't add interface methods to these and we don't have overloading. Likewise, you cannot use type definitions like type Builtin[T GoTypes] T to make things cleaner (this doesn't compile).

Testing:

  • Updated unit tests.
  • Added ClickHouse server type tests to verify serialization and type checking.
  • e2e tests.
  • Manual testing with local otel-demo.

Further work

Having the type data exposed allows us to have native query parameters that can be referenced with "variables".

For example, query parameters for other dbms support syntax like @name or $name that are substituted. This has all the benefits of proper variable naming and reuse.

Edited by Joe Shaw

Merge request reports