#include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int SIZES_OF_ARRAYS_DONT_MATCH; extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; extern const int ILLEGAL_COLUMN; extern const int ILLEGAL_TYPE_OF_ARGUMENT; } /// Counts the number of different elements in the array, or the number of different tuples from the elements at the corresponding positions in several arrays. /// NOTE The implementation partially matches arrayEnumerateUniq. class FunctionArrayUniq : public IFunction { public: static constexpr auto name = "arrayUniq"; static FunctionPtr create(ContextPtr) { return std::make_shared(); } String getName() const override { return name; } bool isVariadic() const override { return true; } size_t getNumberOfArguments() const override { return 0; } bool useDefaultImplementationForConstants() const override { return true; } bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override { if (arguments.empty()) throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Number of arguments for function {} doesn't match: passed {}, should be at least 1.", getName(), arguments.size()); for (size_t i = 0; i < arguments.size(); ++i) { const DataTypeArray * array_type = checkAndGetDataType(arguments[i].get()); if (!array_type) throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "All arguments for function {} must be arrays but argument {} has type {}.", getName(), i + 1, arguments[i]->getName()); } return std::make_shared(); } ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override; private: /// Initially allocate a piece of memory for 512 elements. NOTE: This is just a guess. static constexpr size_t INITIAL_SIZE_DEGREE = 9; template struct MethodOneNumber { using Set = ClearableHashSetWithStackMemory, INITIAL_SIZE_DEGREE>; using Method = ColumnsHashing::HashMethodOneNumber; }; struct MethodString { using Set = ClearableHashSetWithStackMemory; using Method = ColumnsHashing::HashMethodString; }; struct MethodFixedString { using Set = ClearableHashSetWithStackMemory; using Method = ColumnsHashing::HashMethodFixedString; }; struct MethodFixed { using Set = ClearableHashSetWithStackMemory; using Method = ColumnsHashing::HashMethodKeysFixed; }; struct MethodHashed { using Set = ClearableHashSetWithStackMemory; using Method = ColumnsHashing::HashMethodHashed; }; template void executeMethod(const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, const Sizes & key_sizes, const NullMap * null_map, ColumnUInt32::Container & res_values) const; template void executeMethodImpl(const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, const Sizes & key_sizes, const NullMap * null_map, ColumnUInt32::Container & res_values) const; template bool executeNumber(const ColumnArray::Offsets & offsets, const IColumn & data, const NullMap * null_map, ColumnUInt32::Container & res_values) const; bool executeString(const ColumnArray::Offsets & offsets, const IColumn & data, const NullMap * null_map, ColumnUInt32::Container & res_values) const; bool executeFixedString(const ColumnArray::Offsets & offsets, const IColumn & data, const NullMap * null_map, ColumnUInt32::Container & res_values) const; bool execute128bit(const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, ColumnUInt32::Container & res_values) const; void executeHashed(const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, ColumnUInt32::Container & res_values) const; }; ColumnPtr FunctionArrayUniq::executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const { const ColumnArray::Offsets * offsets = nullptr; const size_t num_arguments = arguments.size(); assert(num_arguments > 0); ColumnRawPtrs data_columns(num_arguments); Columns array_holders; for (size_t i = 0; i < num_arguments; ++i) { const ColumnPtr & array_ptr = arguments[i].column; const ColumnArray * array = checkAndGetColumn(array_ptr.get()); if (!array) { const ColumnConst * const_array = checkAndGetColumnConst( arguments[i].column.get()); if (!const_array) throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of {}-th argument of function {}", arguments[i].column->getName(), i + 1, getName()); array_holders.emplace_back(const_array->convertToFullColumn()); array = checkAndGetColumn(array_holders.back().get()); } const ColumnArray::Offsets & offsets_i = array->getOffsets(); if (i == 0) offsets = &offsets_i; else if (offsets_i != *offsets) throw Exception(ErrorCodes::SIZES_OF_ARRAYS_DONT_MATCH, "Lengths of all arrays passed to {} must be equal.", getName()); const auto * array_data = &array->getData(); data_columns[i] = array_data; } const NullMap * null_map = nullptr; for (size_t i = 0; i < num_arguments; ++i) { if (const auto * nullable_col = checkAndGetColumn(*data_columns[i])) { if (num_arguments == 1) data_columns[i] = &nullable_col->getNestedColumn(); null_map = &nullable_col->getNullMapData(); break; } } auto res = ColumnUInt32::create(); ColumnUInt32::Container & res_values = res->getData(); res_values.resize(offsets->size()); if (num_arguments == 1) { if (!(executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeNumber(*offsets, *data_columns[0], null_map, res_values) || executeFixedString(*offsets, *data_columns[0], null_map, res_values) || executeString(*offsets, *data_columns[0], null_map, res_values))) executeHashed(*offsets, data_columns, res_values); } else { if (!execute128bit(*offsets, data_columns, res_values)) executeHashed(*offsets, data_columns, res_values); } return res; } template void FunctionArrayUniq::executeMethodImpl( const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, const Sizes & key_sizes, [[maybe_unused]] const NullMap * null_map, ColumnUInt32::Container & res_values) const { typename Method::Set set; typename Method::Method method(columns, key_sizes, nullptr); Arena pool; /// Won't use it; ColumnArray::Offset prev_off = 0; for (size_t i = 0; i < offsets.size(); ++i) { set.clear(); bool found_null = false; ColumnArray::Offset off = offsets[i]; for (ColumnArray::Offset j = prev_off; j < off; ++j) { if constexpr (has_null_map) { // NOLINT if ((*null_map)[j]) { found_null = true; continue; } } method.emplaceKey(set, j, pool); } res_values[i] = static_cast(set.size() + found_null); prev_off = off; } } template void FunctionArrayUniq::executeMethod( const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, const Sizes & key_sizes, const NullMap * null_map, ColumnUInt32::Container & res_values) const { if (null_map) executeMethodImpl(offsets, columns, key_sizes, null_map, res_values); else executeMethodImpl(offsets, columns, key_sizes, null_map, res_values); } template bool FunctionArrayUniq::executeNumber(const ColumnArray::Offsets & offsets, const IColumn & data, const NullMap * null_map, ColumnUInt32::Container & res_values) const { const auto * nested = checkAndGetColumn>(&data); if (!nested) return false; executeMethod>(offsets, {nested}, {}, null_map, res_values); return true; } bool FunctionArrayUniq::executeString(const ColumnArray::Offsets & offsets, const IColumn & data, const NullMap * null_map, ColumnUInt32::Container & res_values) const { const auto * nested = checkAndGetColumn(&data); if (nested) executeMethod(offsets, {nested}, {}, null_map, res_values); return nested; } bool FunctionArrayUniq::executeFixedString(const ColumnArray::Offsets & offsets, const IColumn & data, const NullMap * null_map, ColumnUInt32::Container & res_values) const { const auto * nested = checkAndGetColumn(&data); if (nested) executeMethod(offsets, {nested}, {}, null_map, res_values); return nested; } bool FunctionArrayUniq::execute128bit( const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, ColumnUInt32::Container & res_values) const { size_t count = columns.size(); size_t keys_bytes = 0; Sizes key_sizes(count); for (size_t j = 0; j < count; ++j) { if (!columns[j]->isFixedAndContiguous()) return false; key_sizes[j] = columns[j]->sizeOfValueIfFixed(); keys_bytes += key_sizes[j]; } if (keys_bytes > 16) return false; executeMethod(offsets, columns, key_sizes, nullptr, res_values); return true; } void FunctionArrayUniq::executeHashed( const ColumnArray::Offsets & offsets, const ColumnRawPtrs & columns, ColumnUInt32::Container & res_values) const { executeMethod(offsets, columns, {}, nullptr, res_values); } REGISTER_FUNCTION(ArrayUniq) { factory.registerFunction(); } }