-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4239 from danlark1/master
produce hints for typo functions and types
- Loading branch information
Showing
8 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#pragma once | ||
|
||
#include <Core/Types.h> | ||
|
||
#include <algorithm> | ||
#include <cctype> | ||
#include <queue> | ||
#include <utility> | ||
|
||
namespace DB | ||
{ | ||
template <size_t MistakeFactor, size_t MaxNumHints> | ||
class NamePrompter | ||
{ | ||
public: | ||
using DistanceIndex = std::pair<size_t, size_t>; | ||
using DistanceIndexQueue = std::priority_queue<DistanceIndex>; | ||
|
||
static std::vector<String> getHints(const String & name, const std::vector<String> & prompting_strings) | ||
{ | ||
DistanceIndexQueue queue; | ||
for (size_t i = 0; i < prompting_strings.size(); ++i) | ||
appendToQueue(i, name, queue, prompting_strings); | ||
return release(queue, prompting_strings); | ||
} | ||
|
||
private: | ||
static size_t levenshteinDistance(const String & lhs, const String & rhs) | ||
{ | ||
size_t n = lhs.size(); | ||
size_t m = rhs.size(); | ||
std::vector<std::vector<size_t>> dp(n + 1, std::vector<size_t>(m + 1)); | ||
|
||
for (size_t i = 1; i <= n; ++i) | ||
dp[i][0] = i; | ||
|
||
for (size_t i = 1; i <= m; ++i) | ||
dp[0][i] = i; | ||
|
||
for (size_t j = 1; j <= m; ++j) | ||
{ | ||
for (size_t i = 1; i <= n; ++i) | ||
{ | ||
if (std::tolower(lhs[i - 1]) == std::tolower(rhs[j - 1])) | ||
dp[i][j] = dp[i - 1][j - 1]; | ||
else | ||
dp[i][j] = std::min(dp[i - 1][j] + 1, std::min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1)); | ||
} | ||
} | ||
|
||
return dp[n][m]; | ||
} | ||
|
||
static void appendToQueue(size_t ind, const String & name, DistanceIndexQueue & queue, const std::vector<String> & prompting_strings) | ||
{ | ||
if (prompting_strings[ind].size() <= name.size() + MistakeFactor && prompting_strings[ind].size() + MistakeFactor >= name.size()) | ||
{ | ||
size_t distance = levenshteinDistance(prompting_strings[ind], name); | ||
if (distance <= MistakeFactor) | ||
{ | ||
queue.emplace(distance, ind); | ||
if (queue.size() > MaxNumHints) | ||
queue.pop(); | ||
} | ||
} | ||
} | ||
|
||
static std::vector<String> release(DistanceIndexQueue & queue, const std::vector<String> & prompting_strings) | ||
{ | ||
std::vector<String> ans; | ||
ans.reserve(queue.size()); | ||
while (!queue.empty()) | ||
{ | ||
auto top = queue.top(); | ||
queue.pop(); | ||
ans.push_back(prompting_strings[top.second]); | ||
} | ||
std::reverse(ans.begin(), ans.end()); | ||
return ans; | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
14 changes: 14 additions & 0 deletions
14
dbms/tests/queries/0_stateless/00834_hints_for_type_function_typos.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) | ||
. $CURDIR/../shell_config.sh | ||
|
||
$CLICKHOUSE_CLIENT -q "select c23ount(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['count'" &>/dev/null; | ||
$CLICKHOUSE_CLIENT -q "select cunt(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['count'" &>/dev/null; | ||
$CLICKHOUSE_CLIENT -q "select positin(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['position'" &>/dev/null; | ||
$CLICKHOUSE_CLIENT -q "select POSITIO(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['position'" &>/dev/null; | ||
$CLICKHOUSE_CLIENT -q "select fount(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['count'" | grep "Maybe you meant: \['round'" | grep "Or unknown aggregate function" &>/dev/null; | ||
$CLICKHOUSE_CLIENT -q "select positin(*) from system.functions;" 2>&1 | grep -v "Or unknown aggregate function" &>/dev/null; | ||
$CLICKHOUSE_CLIENT -q "select pov(*) from system.functions;" 2>&1 | grep "Maybe you meant: \['pow','cos'\]" &>/dev/null; |