From aa0c94f20c5c9acd08d3facb0781a7b7de0b9470 Mon Sep 17 00:00:00 2001 From: flynn Date: Tue, 5 Sep 2023 13:53:15 +0000 Subject: [PATCH 1/3] Handle Null in function isZeroOrNull --- src/Functions/isZeroOrNull.cpp | 18 ++++++++++++++---- .../02872_is_zero_or_null.reference | 1 + .../0_stateless/02872_is_zero_or_null.sql | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 tests/queries/0_stateless/02872_is_zero_or_null.reference create mode 100644 tests/queries/0_stateless/02872_is_zero_or_null.sql diff --git a/src/Functions/isZeroOrNull.cpp b/src/Functions/isZeroOrNull.cpp index bc0ac299a23..119fb2f67fd 100644 --- a/src/Functions/isZeroOrNull.cpp +++ b/src/Functions/isZeroOrNull.cpp @@ -44,14 +44,18 @@ public: DataTypePtr getReturnTypeImpl(const DataTypes & types) const override { - if (!isNumber(removeNullable(types.at(0)))) - throw Exception(ErrorCodes::BAD_ARGUMENTS, "The argument of function {} must have simple numeric type, possibly Nullable", name); + if (!isNumber(removeNullable(types.at(0))) && !isNothing(removeNullable(types.at(0)))) + throw Exception( + ErrorCodes::BAD_ARGUMENTS, "The argument of function {} must have simple numeric type, possibly Nullable or Null", name); return std::make_shared(); } ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override { + if (isNothing(removeNullable(arguments[0].type))) + return DataTypeUInt8{}.createColumnConst(input_rows_count, 1); + const ColumnPtr & input_column = arguments[0].column; ColumnPtr res; @@ -72,7 +76,10 @@ public: return true; })) { - throw Exception(ErrorCodes::ILLEGAL_COLUMN, "The argument of function {} must have simple numeric type, possibly Nullable", name); + throw Exception( + ErrorCodes::ILLEGAL_COLUMN, + "The argument of function {} must have simple numeric type, possibly Nullable or Null", + name); } } else @@ -89,7 +96,10 @@ public: return true; })) { - throw Exception(ErrorCodes::ILLEGAL_COLUMN, "The argument of function {} must have simple numeric type, possibly Nullable", name); + throw Exception( + ErrorCodes::ILLEGAL_COLUMN, + "The argument of function {} must have simple numeric type, possibly Nullable or Null", + name); } } diff --git a/tests/queries/0_stateless/02872_is_zero_or_null.reference b/tests/queries/0_stateless/02872_is_zero_or_null.reference new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/tests/queries/0_stateless/02872_is_zero_or_null.reference @@ -0,0 +1 @@ +1 diff --git a/tests/queries/0_stateless/02872_is_zero_or_null.sql b/tests/queries/0_stateless/02872_is_zero_or_null.sql new file mode 100644 index 00000000000..98830027603 --- /dev/null +++ b/tests/queries/0_stateless/02872_is_zero_or_null.sql @@ -0,0 +1 @@ +select isZeroOrNull(Null); From 37cabaf993718b392ab034c138a120c4fbfda0fd Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Tue, 5 Sep 2023 17:31:37 +0000 Subject: [PATCH 2/3] Add docs, consolidate tests --- .../functions/functions-for-nulls.md | 44 +++++++++++++++++++ .../01373_is_zero_or_null.reference | 2 + .../0_stateless/01373_is_zero_or_null.sql | 4 ++ .../02872_is_zero_or_null.reference | 1 - .../0_stateless/02872_is_zero_or_null.sql | 1 - 5 files changed, 50 insertions(+), 2 deletions(-) delete mode 100644 tests/queries/0_stateless/02872_is_zero_or_null.reference delete mode 100644 tests/queries/0_stateless/02872_is_zero_or_null.sql diff --git a/docs/en/sql-reference/functions/functions-for-nulls.md b/docs/en/sql-reference/functions/functions-for-nulls.md index d57b799e94c..bde2a8a9505 100644 --- a/docs/en/sql-reference/functions/functions-for-nulls.md +++ b/docs/en/sql-reference/functions/functions-for-nulls.md @@ -92,6 +92,50 @@ Result: └───┘ ``` +## isZeroOrNull + +Returns whether the argument is 0 (zero) or [NULL](../../sql-reference/syntax.md#null-literal). + +``` sql +isZeroOrNull(x) +``` + +**Arguments:** + +- `x` — A value of non-compound data type. + +**Returned value** + +- `1` if `x` is 0 (zero) or `NULL`. +- `0` else. + +**Example** + +Table: + +``` text +┌─x─┬────y─┐ +│ 1 │ ᴺᵁᴸᴸ │ +│ 2 │ 0 │ +│ 3 │ 3 │ +└───┴──────┘ +``` + +Query: + +``` sql +SELECT x FROM t_null WHERE isZeroOrNull(y); +``` + +Result: + +``` text +┌─x─┐ +│ 1 │ +│ 2 │ +└───┘ +``` + ## coalesce Returns the leftmost non-`NULL` argument. diff --git a/tests/queries/0_stateless/01373_is_zero_or_null.reference b/tests/queries/0_stateless/01373_is_zero_or_null.reference index d9caaa2089a..baf51073fd4 100644 --- a/tests/queries/0_stateless/01373_is_zero_or_null.reference +++ b/tests/queries/0_stateless/01373_is_zero_or_null.reference @@ -20,3 +20,5 @@ world 3 --- 4 +--- +1 diff --git a/tests/queries/0_stateless/01373_is_zero_or_null.sql b/tests/queries/0_stateless/01373_is_zero_or_null.sql index 32458dc9f62..dcb4f9649f9 100644 --- a/tests/queries/0_stateless/01373_is_zero_or_null.sql +++ b/tests/queries/0_stateless/01373_is_zero_or_null.sql @@ -27,3 +27,7 @@ SELECT count() FROM UNION ALL SELECT * FROM test WHERE isZeroOrNull(x != 'xyz') ); + +SELECT '---'; + +select isZeroOrNull(Null); diff --git a/tests/queries/0_stateless/02872_is_zero_or_null.reference b/tests/queries/0_stateless/02872_is_zero_or_null.reference deleted file mode 100644 index d00491fd7e5..00000000000 --- a/tests/queries/0_stateless/02872_is_zero_or_null.reference +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/tests/queries/0_stateless/02872_is_zero_or_null.sql b/tests/queries/0_stateless/02872_is_zero_or_null.sql deleted file mode 100644 index 98830027603..00000000000 --- a/tests/queries/0_stateless/02872_is_zero_or_null.sql +++ /dev/null @@ -1 +0,0 @@ -select isZeroOrNull(Null); From b004aa80762dec9a7b7077487226dd1d32401833 Mon Sep 17 00:00:00 2001 From: Robert Schulze Date: Tue, 5 Sep 2023 19:55:11 +0000 Subject: [PATCH 3/3] Fix style --- utils/check-style/aspell-ignore/en/aspell-dict.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/check-style/aspell-ignore/en/aspell-dict.txt b/utils/check-style/aspell-ignore/en/aspell-dict.txt index 2e45b885afd..608365d087c 100644 --- a/utils/check-style/aspell-ignore/en/aspell-dict.txt +++ b/utils/check-style/aspell-ignore/en/aspell-dict.txt @@ -1329,10 +1329,10 @@ ddl deallocation deallocations debian +decodeHTMLComponent decodeURLComponent decodeURLFormComponent decodeXMLComponent -decodeHTMLComponent decompressor decrypt decrypted @@ -1558,6 +1558,7 @@ gzipped hadoop halfMD halfday +hardlink hardlinks hasAll hasAny @@ -1637,6 +1638,7 @@ isNotNull isNull isValidJSON isValidUTF +isZeroOrNull iteratively jaccard javaHash @@ -2582,4 +2584,3 @@ znode znodes zookeeperSessionUptime zstd -hardlink