1. Syntax and Repository Standards
- Write code in the language and style used by the target database repository (C for SQLite/PostgreSQL, C++ for DuckDB/ClickHouse).
- Follow official conventions for naming, indentation, data types, return values, and integration with the repository.
- Reuse existing modules (macros, namespaces, utility functions) rather than reimplementing them.
2. Functionality Accuracy
- Implement only the described functionality without adding extra features.
- Avoid undefined behavior, memory leaks, or dangling pointers.
- Respect all explicit and implicit dependencies (catalog definitions, type system rules) to prevent conflicts.
3. Robustness
- Cover edge cases: NULL values, input ranges, type conversions, invalid inputs, overflow/underflow.
- Apply repository-approved error handling to safely process invalid inputs.
4. Performance and Maintainability
- Implement with time and space efficiency; avoid redundant allocations or computations.
- Write readable, modular code consistent with repository practices.
Before writing code, verify whether the function already exists in the target source files to avoid duplicate symbol errors. If the existing implementation already satisfies the specification, do not rewrite it — run minimal verification and call task_done.
- Language: C
- Scalar function registration:
FuncDef aBuiltinFunc[]infunc.c, using theFUNCTIONmacro. Example:FUNCTION(sign, 1, 0, 0, signFunc) - Aggregate functions: use the
AGGREGATEmacro. - Memory:
sqlite3_malloc/sqlite3_free; always check for NULL after allocation. - NULL handling: explicitly check with
sqlite3_value_type(argv[i]) == SQLITE_NULL. - Errors:
sqlite3_result_error()orsqlite3_result_error_nomem(). - Common pitfalls: missing registration → "no such function"; missing NULL check → segfault.
- Language: C
- Memory:
palloc/pfreewithin the current memory context; no need to free explicitly. - Errors: prefer
ereport(ERROR, errcode(ERRCODE_*), errmsg(...))for new code;elogmay appear in older paths but avoid introducing it in new functions. - NULL handling: check arguments with
PG_ARGISNULL(n); return NULL withPG_RETURN_NULL(). - Return type:
Datum; use the correct type-cast macros (e.g.,Int32GetDatum). - Registration: declare in
builtins.hand implement in a.cfile, or viaCREATE FUNCTIONSQL.
- Language: C++
- Registration:
FunctionFactory::RegisterFunctionorScalarFunctionSet. - Errors:
throw InvalidInputException(...)orthrow NotImplementedException(...). - NULL: check with
Value::IsNull(); return an emptyValue(). - Common pitfalls: incorrect template instantiation order; overload resolution ambiguity.
- Language: C++
- Registration:
FunctionFactory::instance().registerFunction<FunctionName>()at the end of the.cppfile. - Logging: prefer
LOG_ERROR/LOG_WARNING/LOG_INFOmacros; avoidstd::coutin production code unless the surrounding file already uses it. - NULL: Nullable columns require explicit handling; function signatures should support the
is_nullableparameter.