clickhouse/clickhouse
Functions
src/Functions/ is the registry of scalar functions. There are hundreds of them; the directory has more .cpp files than any other under src/.
Architecture
Every function is a class that implements IFunction (src/Functions/IFunction.h). It declares its name, argument signature, and a vectorized executeImpl(...) that takes a ColumnsWithTypeAndName and returns a ColumnPtr. Most functions are templated over column types and have specialized integer/float/string paths. The FunctionFactory (src/Functions/FunctionFactory.cpp) maps the name to a constructor.
Each function lives in its own .cpp file. They self-register with an anonymous-namespace void registerFunction<Name>() invoked from src/Functions/registerFunctions.cpp.
Big families
| Family | Examples | File / dir |
|---|---|---|
| Arithmetic | +, -, *, /, intDiv, modulo, abs, negate, least, greatest |
FunctionBinaryArithmetic.h (~160 KB header), plus.cpp, minus.cpp, multiply.cpp, divide.cpp |
| Comparison | equals, less, greater, …, like, match, multiMatchAny |
equals.cpp, less.cpp, … |
| Logical | and, or, not, xor |
FunctionsLogical.cpp |
| String | concat, substring, lower, upper, replaceAll, regex*, lpad, rpad, position, extract*, splitByChar |
concat.cpp, substring.cpp, Functions/StringHelpers.cpp, Functions/extract* |
| Date/Time | toDate, toDateTime, toStartOfMonth, formatDateTime, addDays, subtractMinutes, dateDiff, now, today |
DateTimeTransforms.h (~94 KB), makeDate.cpp, dateDiff.cpp, formatDateTime.cpp |
| Conversion | toUInt32, toString, CAST, parseDateTimeBestEffort, toType* |
CastOverloadResolver.cpp, toString.cpp, parseDateTimeBestEffort.cpp |
| JSON | JSONExtract*, JSONHas, JSONLength, simdjson |
FunctionsJSON.cpp (~90 KB) |
| Hashing | cityHash64, sipHash64, MD5, SHA*, xxHash*, farmHash* |
FunctionsHashing.h (~70 KB), FunctionsHashingMisc.cpp, FunctionsHashingMurmur.cpp, CRC.cpp |
| Encoding | base64Encode, hex, unhex, base58Encode |
FunctionBase64Conversion.h, FunctionBase58Conversion.h, FunctionBaseAI.h, hex.cpp |
| Encryption | encrypt, decrypt, tryDecrypt, aes_* |
encrypt.cpp, decrypt.cpp, tryDecrypt.cpp |
| Random | rand, randNormal, randCanonical, generateUUIDv4 |
FunctionsRandom.cpp |
| Arrays | arrayMap, arrayFilter, arrayJoin, arrayReduce, arrayConcat, arrayResize, arraySort, groupArray* |
a long tail in array*.cpp |
| Higher-order | arrayMap(x -> ...), arrayFilter(x -> ...) |
implemented via IFunction::isSuitableForShortCircuitArgumentsExecution and lambdas |
| Tuple/Map | tupleElement, mapKeys, mapValues, mapContains, mapApply |
tupleElement.cpp, map*.cpp |
| Geo | geohashEncode, geohashDecode, pointInPolygon, geoToS2, s2* |
geohash*.cpp, geo*.cpp, pointInPolygon.cpp |
| Vectors | cosineDistance, L1Norm, L2Norm, dotProduct |
vectorFunctions.cpp (~130 KB) |
| URL/IP | domain, topLevelDomain, IPv4StringToNum, cutQueryString |
URL/, IP/ (subdirs) |
| Bitmap | bitmapAnd, bitmapOr, bitmapHasAll |
bitmap*.cpp |
| External dictionaries | dictGet, dictGetOrDefault, dictHas, dictGetHierarchy |
FunctionsExternalDictionaries.cpp |
| Sketches/HLL | uniqHLL12*, groupBitmap*, quantilesTDigest (most in aggregates) |
shares code with src/AggregateFunctions/ |
| AI | chatbotAnswer, embedText, aiGenerateText |
Functions/AI/, builds on FunctionBaseAI.h and contrib/ai-sdk-cpp |
| Misc | version, hostname, errorCodeToName, getMacro, currentRoles, currentDatabase |
version.cpp, getMacro.cpp, errorCodeToName.cpp, … |
src/Functions/registerFunctions*.cpp carries the giant list of register* calls grouped by category.
Vectorized execution
A function receives ColumnPtrs (typed wrappers around a contiguous buffer) and produces a ColumnPtr. Hot paths are templated over physical types (e.g. ColumnVector<UInt32>) so the compiler unrolls the inner loop. For variable-length data (String, Array), functions operate on the offsets buffer plus the values buffer.
The generic flow:
- Resolve overload via
IFunctionOverloadResolverandcastColumnto align argument types if needed. - Bind a concrete
IExecutableFunction. - The executor calls
executeImpl(arguments, result_type, input_rows_count)once perChunk. IFunction::isSuitableForConstantFolding,useDefaultImplementationForConstants,useDefaultImplementationForNulls, etc. control which fast paths are taken.
Lambdas / higher-order
arrayMap, arrayFilter, arrayCount, etc. accept a lambda. Internally the function compiles the lambda body via ActionsDAG, evaluates it against an offset-flattened view of the array, and assembles results.
JIT compilation
src/Interpreters/ExpressionJIT.cpp (using vendored LLVM under contrib/llvm-project) compiles inner expression loops to native code when compile_expressions=1. This kicks in for filter and projection DAGs that are evaluated many times per query.
CAST
src/Functions/CastOverloadResolver.cpp is a meta-function that picks the right typed conversion. It handles all the awkward cases (Nullable → not, LowCardinality ↔ regular, Decimal rounding, DateTime time-zone reinterpretation, Variant projection, Dynamic dispatch).
Adding a function
- Write a
.cppundersrc/Functions/. - Subclass
IFunction(or one of the helpers likeFunctionFactory::register<NameOf>). - Add a
void registerFunctionFoo(FunctionFactory & factory);and call it fromregisterFunctions.cpp. - Add tests under
tests/queries/0_stateless/.
Per AGENTS.md: refer to the function as f, not f(), in commit messages and prose.
Related pages
- Aggregate functions
- Data types and columns
- Interpreters —
ActionsDAG, JIT, expression analyzer.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.