diff --git a/dbms/src/Functions/FunctionsConditional.h b/dbms/src/Functions/FunctionsConditional.h index 9ca0b0deef5..212582af713 100644 --- a/dbms/src/Functions/FunctionsConditional.h +++ b/dbms/src/Functions/FunctionsConditional.h @@ -830,12 +830,15 @@ public: /// Get result types by argument types. If the function does not apply to these arguments, throw an exception. DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override { + if (arguments[0]->isNull()) + return arguments[0]; + if (arguments[0]->isNullable()) return makeNullableDataTypeIfNot(getReturnTypeImpl({ getNestedDataType(arguments[0]), arguments[1], arguments[2]})); if (!checkDataType(arguments[0].get())) - throw Exception("Illegal type of first argument (condition) of function if. Must be UInt8.", + throw Exception("Illegal type " + arguments[0]->getName() + " of first argument (condition) of function if. Must be UInt8.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); return getLeastCommonType({arguments[1], arguments[2]}); diff --git a/dbms/src/Functions/FunctionsNull.cpp b/dbms/src/Functions/FunctionsNull.cpp index 9dcc72fb66f..c8683331453 100644 --- a/dbms/src/Functions/FunctionsNull.cpp +++ b/dbms/src/Functions/FunctionsNull.cpp @@ -89,7 +89,7 @@ void FunctionIsNotNull::executeImpl(Block & block, const ColumnNumbers & argumen } }; - FunctionIsNull{}.executeImpl(temp_block, {0}, 1); + FunctionIsNull{}.execute(temp_block, {0}, 1); FunctionNot{}.execute(temp_block, {1}, 2); block.getByPosition(result).column = std::move(temp_block.getByPosition(2).column); @@ -168,14 +168,14 @@ void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & argument filtered_args.reserve(arguments.size()); for (const auto & arg : arguments) { - const auto & column = block.getByPosition(arg).column; + const auto & type = block.getByPosition(arg).type; - if (column->isNull()) + if (type->isNull()) continue; filtered_args.push_back(arg); - if (!column->isNullable()) + if (!type->isNullable()) break; } @@ -197,9 +197,9 @@ void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & argument else { temp_block.insert({nullptr, std::make_shared(), ""}); - is_not_null.executeImpl(temp_block, {filtered_args[i]}, res_pos); + is_not_null.execute(temp_block, {filtered_args[i]}, res_pos); temp_block.insert({nullptr, getNestedDataType(block.getByPosition(filtered_args[i]).type), ""}); - assume_not_null.executeImpl(temp_block, {filtered_args[i]}, res_pos + 1); + assume_not_null.execute(temp_block, {filtered_args[i]}, res_pos + 1); multi_if_args.push_back(res_pos); multi_if_args.push_back(res_pos + 1); @@ -219,7 +219,7 @@ void FunctionCoalesce::executeImpl(Block & block, const ColumnNumbers & argument return; } - FunctionMultiIf{context}.executeImpl(temp_block, multi_if_args, result); + FunctionMultiIf{context}.execute(temp_block, multi_if_args, result); auto res = std::move(temp_block.getByPosition(result).column); @@ -256,14 +256,14 @@ DataTypePtr FunctionIfNull::getReturnTypeImpl(const DataTypes & arguments) const void FunctionIfNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) { /// Always null. - if (block.getByPosition(arguments[0]).column->isNull()) + if (block.getByPosition(arguments[0]).type->isNull()) { block.getByPosition(result).column = block.getByPosition(arguments[1]).column; return; } /// Could not contain nulls, so nullIf makes no sense. - if (!block.getByPosition(arguments[0]).column->isNullable()) + if (!block.getByPosition(arguments[0]).type->isNullable()) { block.getByPosition(result).column = block.getByPosition(arguments[0]).column; return; @@ -278,9 +278,14 @@ void FunctionIfNull::executeImpl(Block & block, const ColumnNumbers & arguments, size_t assume_not_null_pos = temp_block.columns(); temp_block.insert({nullptr, getNestedDataType(block.getByPosition(arguments[0]).type), ""}); - FunctionIsNotNull{}.executeImpl(temp_block, {arguments[0]}, is_not_null_pos); - FunctionAssumeNotNull{}.executeImpl(temp_block, {arguments[0]}, assume_not_null_pos); - FunctionIf{}.executeImpl(temp_block, {is_not_null_pos, assume_not_null_pos, arguments[1]}, result); + FunctionIsNotNull{}.execute(temp_block, {arguments[0]}, is_not_null_pos); + FunctionAssumeNotNull{}.execute(temp_block, {arguments[0]}, assume_not_null_pos); + + std::cerr << temp_block.dumpStructure() << "\n"; + + FunctionIf{}.execute(temp_block, {is_not_null_pos, assume_not_null_pos, arguments[1]}, result); + + std::cerr << temp_block.dumpStructure() << "\n"; block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column); } @@ -324,7 +329,7 @@ void FunctionNullIf::executeImpl(Block & block, const ColumnNumbers & arguments, temp_block.insert(null_elem); - FunctionIf{}.executeImpl(temp_block, {res_pos, null_pos, arguments[0]}, result); + FunctionIf{}.execute(temp_block, {res_pos, null_pos, arguments[0]}, result); block.getByPosition(result).column = std::move(temp_block.getByPosition(result).column); } @@ -374,7 +379,7 @@ std::string FunctionToNullable::getName() const DataTypePtr FunctionToNullable::getReturnTypeImpl(const DataTypes & arguments) const { - if (arguments[0]->isNull() || arguments[0]->isNullable()) + if (arguments[0]->isNullable()) return arguments[0]; return std::make_shared(arguments[0]); } @@ -383,7 +388,7 @@ void FunctionToNullable::executeImpl(Block & block, const ColumnNumbers & argume { const ColumnPtr & col = block.getByPosition(arguments[0]).column; - if (col->isNull() || col->isNullable()) + if (col->isNullable()) block.getByPosition(result).column = col; else block.getByPosition(result).column = std::make_shared(col, diff --git a/dbms/src/Functions/FunctionsNull.h b/dbms/src/Functions/FunctionsNull.h index 2b626d45f5a..d21a26949c2 100644 --- a/dbms/src/Functions/FunctionsNull.h +++ b/dbms/src/Functions/FunctionsNull.h @@ -22,6 +22,7 @@ public: std::string getName() const override; size_t getNumberOfArguments() const override { return 1; } bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; }; @@ -37,6 +38,7 @@ public: std::string getName() const override; size_t getNumberOfArguments() const override { return 1; } bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; }; @@ -74,6 +76,7 @@ public: std::string getName() const override; size_t getNumberOfArguments() const override { return 2; } bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; }; @@ -90,6 +93,7 @@ public: std::string getName() const override; size_t getNumberOfArguments() const override { return 2; } bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; }; @@ -107,6 +111,7 @@ public: std::string getName() const override; size_t getNumberOfArguments() const override { return 1; } bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; }; @@ -121,6 +126,7 @@ public: std::string getName() const override; size_t getNumberOfArguments() const override { return 1; } bool useDefaultImplementationForNulls() const override { return false; } + bool useDefaultImplementationForConstants() const override { return true; } DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; }; diff --git a/dbms/tests/queries/0_stateless/00420_null_in_scalar_subqueries.sql b/dbms/tests/queries/0_stateless/00420_null_in_scalar_subqueries.sql index 4bfa8dc4ecb..1a75444921c 100644 --- a/dbms/tests/queries/0_stateless/00420_null_in_scalar_subqueries.sql +++ b/dbms/tests/queries/0_stateless/00420_null_in_scalar_subqueries.sql @@ -3,6 +3,6 @@ SELECT (SELECT * FROM (SELECT * FROM system.numbers LIMIT 2) WHERE number = numb SELECT (SELECT NULL WHERE 0); SELECT (SELECT Null WHERE nuLL IS NOT NULL); SELECT (SELECT Null WHERE 1); -SELECT CAST(NULL as Null); -SELECT (SELECT CAST(NULL as Null) WHERE 0); +SELECT CAST(NULL as Nullable(Nothing)); +SELECT (SELECT CAST(NULL as Nullable(Nothing)) WHERE 0); SELECT (SELECT 1 WHERE 0) AS a, (SELECT 1 WHERE 1) AS b FORMAT TSVWithNames; diff --git a/dbms/tests/queries/0_stateless/00434_tonullable.reference b/dbms/tests/queries/0_stateless/00434_tonullable.reference index 292b9490d95..d633404b936 100644 --- a/dbms/tests/queries/0_stateless/00434_tonullable.reference +++ b/dbms/tests/queries/0_stateless/00434_tonullable.reference @@ -1 +1 @@ -\N Hello 1 \N Hello 1 Null Nullable(String) Nullable(UInt8) Null Nullable(String) Nullable(UInt8) +\N Hello 1 \N Hello 1 Nullable(Nothing) Nullable(String) Nullable(UInt8) Nullable(Nothing) Nullable(String) Nullable(UInt8)