Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sql failed when using replace function #9524

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
280 changes: 58 additions & 222 deletions dbms/src/Functions/FunctionsStringReplace.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,58 +122,10 @@ class FunctionStringReplace : public IFunction

ColumnWithTypeAndName & column_result = block.getByPosition(result);

bool src_const = column_src->isColumnConst();
bool needle_const = column_needle->isColumnConst();
bool replacement_const = column_replacement->isColumnConst();

if (src_const)
{
if (needle_const && !replacement_const)
{
executeImplConstFirstThireParaReplacement(
column_src,
column_needle,
column_replacement,
pos,
occ,
match_type,
column_result);
}
else if (!needle_const && replacement_const)
{
executeImplConstReplacement(
column_src,
column_needle,
column_replacement,
pos,
occ,
match_type,
column_result);
}
else if (!needle_const && !replacement_const)
{
executeImplConstFirstParaReplacement(
column_src,
column_needle,
column_replacement,
pos,
occ,
match_type,
column_result);
}
else
{
executeImplConstAllParaReplacement(
column_src,
column_needle,
column_replacement,
pos,
occ,
match_type,
column_result);
}
}
else if (needle_const && replacement_const)
if (needle_const && replacement_const)
{
executeImpl(column_src, column_needle, column_replacement, pos, occ, match_type, column_result);
}
Expand Down Expand Up @@ -280,7 +232,28 @@ class FunctionStringReplace : public IFunction
const auto * col_replacement_const = typeid_cast<const ColumnConst *>(column_replacement.get());
auto replacement = col_replacement_const->getValue<String>();

if (const auto * col = checkAndGetColumn<ColumnString>(column_src.get()))
bool col_const = column_src->isColumnConst();
EricZequan marked this conversation as resolved.
Show resolved Hide resolved

if (col_const)
{
auto new_src = column_src->convertToFullColumnIfConst();
EricZequan marked this conversation as resolved.
Show resolved Hide resolved
const auto * col = typeid_cast<const ColumnString *>(new_src.get());
auto col_res = ColumnString::create();
Impl::vectorNonConstNeedle(
col->getChars(),
col->getOffsets(),
col_needle->getChars(),
col_needle->getOffsets(),
replacement,
pos,
occ,
match_type,
collator,
col_res->getChars(),
col_res->getOffsets());
column_result.column = std::move(col_res);
}
else if (const auto * col = checkAndGetColumn<ColumnString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vectorNonConstNeedle(
Expand Down Expand Up @@ -340,8 +313,12 @@ class FunctionStringReplace : public IFunction
auto needle = col_needle_const->getValue<String>();
const auto * col_replacement = typeid_cast<const ColumnString *>(column_replacement.get());

if (const auto * col = checkAndGetColumn<ColumnString>(column_src.get()))
bool col_const = column_src->isColumnConst();

if (col_const)
{
auto new_src = column_src->convertToFullColumnIfConst();
const auto * col = typeid_cast<const ColumnString *>(new_src.get());
auto col_res = ColumnString::create();
Impl::vectorNonConstReplacement(
col->getChars(),
Expand All @@ -357,56 +334,13 @@ class FunctionStringReplace : public IFunction
col_res->getOffsets());
column_result.column = std::move(col_res);
}
else if (const auto * col = checkAndGetColumn<ColumnFixedString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vectorFixedNonConstReplacement(
col->getChars(),
col->getN(),
needle,
col_replacement->getChars(),
col_replacement->getOffsets(),
pos,
occ,
match_type,
collator,
col_res->getChars(),
col_res->getOffsets());
column_result.column = std::move(col_res);
}
else
throw Exception(
"Illegal column " + column_src->getName() + " of first argument of function " + getName(),
ErrorCodes::ILLEGAL_COLUMN);
}
else
{
throw Exception("Argument at index 3 for function replace must be constant", ErrorCodes::ILLEGAL_COLUMN);
}
}

void executeImplNonConstNeedleReplacement(
const ColumnPtr & column_src,
const ColumnPtr & column_needle,
const ColumnPtr & column_replacement,
Int64 pos [[maybe_unused]],
Int64 occ [[maybe_unused]],
const String & match_type,
ColumnWithTypeAndName & column_result) const
{
if constexpr (Impl::support_non_const_needle && Impl::support_non_const_replacement)
{
const auto * col_needle = typeid_cast<const ColumnString *>(column_needle.get());
const auto * col_replacement = typeid_cast<const ColumnString *>(column_replacement.get());

if (const auto * col = checkAndGetColumn<ColumnString>(column_src.get()))
else if (const auto * col = checkAndGetColumn<ColumnString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vectorNonConstNeedleReplacement(
Impl::vectorNonConstReplacement(
col->getChars(),
col->getOffsets(),
col_needle->getChars(),
col_needle->getOffsets(),
needle,
col_replacement->getChars(),
col_replacement->getOffsets(),
pos,
Expand All @@ -420,11 +354,10 @@ class FunctionStringReplace : public IFunction
else if (const auto * col = checkAndGetColumn<ColumnFixedString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vectorFixedNonConstNeedleReplacement(
Impl::vectorFixedNonConstReplacement(
col->getChars(),
col->getN(),
col_needle->getChars(),
col_needle->getOffsets(),
needle,
col_replacement->getChars(),
col_replacement->getOffsets(),
pos,
Expand All @@ -442,13 +375,11 @@ class FunctionStringReplace : public IFunction
}
else
{
throw Exception(
"Argument at index 2 and 3 for function replace must be constant",
ErrorCodes::ILLEGAL_COLUMN);
throw Exception("Argument at index 3 for function replace must be constant", ErrorCodes::ILLEGAL_COLUMN);
}
}

void executeImplConstFirstParaReplacement(
void executeImplNonConstNeedleReplacement(
const ColumnPtr & column_src,
const ColumnPtr & column_needle,
const ColumnPtr & column_replacement,
Expand All @@ -461,11 +392,17 @@ class FunctionStringReplace : public IFunction
{
const auto * col_needle = typeid_cast<const ColumnString *>(column_needle.get());
const auto * col_replacement = typeid_cast<const ColumnString *>(column_replacement.get());
if (const auto * col = checkAndGetColumn<ColumnConst>(column_src.get()))

bool col_const = column_src->isColumnConst();

if (col_const)
{
auto new_src = column_src->convertToFullColumnIfConst();
const auto * col = typeid_cast<const ColumnString *>(new_src.get());
auto col_res = ColumnString::create();
Impl::vectorConstFirstParaReplacement(
col->getValue<String>(),
Impl::vectorNonConstNeedleReplacement(
col->getChars(),
col->getOffsets(),
col_needle->getChars(),
col_needle->getOffsets(),
col_replacement->getChars(),
Expand All @@ -478,39 +415,14 @@ class FunctionStringReplace : public IFunction
col_res->getOffsets());
column_result.column = std::move(col_res);
}
else
throw Exception(
"Illegal column " + column_src->getName() + " of first argument of function " + getName()
+ "Illegal column " + column_needle->getName() + " of second argument of function " + getName()
+ "Illegal column " + column_replacement->getName() + " of third argument of function "
+ getName(),
ErrorCodes::ILLEGAL_COLUMN);
}
else
{
throw Exception("Argument at index 1 for function replace must be constant", ErrorCodes::ILLEGAL_COLUMN);
}
}

void executeImplConstFirstThireParaReplacement(
const ColumnPtr & column_src,
const ColumnPtr & column_needle,
const ColumnPtr & column_replacement,
Int64 pos [[maybe_unused]],
Int64 occ [[maybe_unused]],
const String & match_type,
ColumnWithTypeAndName & column_result) const
{
if constexpr (Impl::support_non_const_needle && Impl::support_non_const_replacement)
{
const auto * col_needle = typeid_cast<const ColumnConst *>(column_needle.get());
const auto * col_replacement = typeid_cast<const ColumnString *>(column_replacement.get());
if (const auto * col = checkAndGetColumn<ColumnConst>(column_src.get()))
else if (const auto * col = checkAndGetColumn<ColumnString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vectorConstFirstThireParaReplacement(
col->getValue<String>(),
col_needle->getValue<String>(),
Impl::vectorNonConstNeedleReplacement(
col->getChars(),
col->getOffsets(),
col_needle->getChars(),
col_needle->getOffsets(),
col_replacement->getChars(),
col_replacement->getOffsets(),
pos,
Expand All @@ -521,43 +433,16 @@ class FunctionStringReplace : public IFunction
col_res->getOffsets());
column_result.column = std::move(col_res);
}
else
throw Exception(
"Illegal column " + column_src->getName() + " of first argument of function " + getName()
+ "Illegal column " + column_needle->getName() + " of second argument of function " + getName()
+ "Illegal column " + column_replacement->getName() + " of third argument of function "
+ getName(),
ErrorCodes::ILLEGAL_COLUMN);
}
else
{
throw Exception(
"Argument at index 1 and 2 for function replace must be constant",
ErrorCodes::ILLEGAL_COLUMN);
}
}

void executeImplConstReplacement(
const ColumnPtr & column_src,
const ColumnPtr & column_needle,
const ColumnPtr & column_replacement,
Int64 pos [[maybe_unused]],
Int64 occ [[maybe_unused]],
const String & match_type,
ColumnWithTypeAndName & column_result) const
{
if constexpr (Impl::support_non_const_needle && Impl::support_non_const_replacement)
{
const auto * col_needle = typeid_cast<const ColumnString *>(column_needle.get());
const auto * col_replacement = typeid_cast<const ColumnConst *>(column_replacement.get());
if (const auto * col = checkAndGetColumn<ColumnConst>(column_src.get()))
else if (const auto * col = checkAndGetColumn<ColumnFixedString>(column_src.get()))
{
auto col_res = ColumnString::create();
Impl::vectorConstReplacement(
col->getValue<String>(),
Impl::vectorFixedNonConstNeedleReplacement(
col->getChars(),
col->getN(),
col_needle->getChars(),
col_needle->getOffsets(),
col_replacement->getValue<String>(),
col_replacement->getChars(),
col_replacement->getOffsets(),
pos,
occ,
match_type,
Expand All @@ -568,62 +453,13 @@ class FunctionStringReplace : public IFunction
}
else
throw Exception(
"Illegal column " + column_src->getName() + " of first argument of function " + getName()
+ "Illegal column " + column_needle->getName() + " of second argument of function " + getName()
+ "Illegal column " + column_replacement->getName() + " of third argument of function "
+ getName(),
ErrorCodes::ILLEGAL_COLUMN);
}
else
{
throw Exception(
"Argument at index 1 and 3 for function replace must be constant",
ErrorCodes::ILLEGAL_COLUMN);
}
}

void executeImplConstAllParaReplacement(
const ColumnPtr & column_src,
const ColumnPtr & column_needle,
const ColumnPtr & column_replacement,
Int64 pos [[maybe_unused]],
Int64 occ [[maybe_unused]],
const String & match_type,
ColumnWithTypeAndName & column_result) const
{
if constexpr (Impl::support_non_const_needle && Impl::support_non_const_replacement)
{
const auto * col_needle = typeid_cast<const ColumnConst *>(column_needle.get());
const auto * col_replacement = typeid_cast<const ColumnConst *>(column_replacement.get());
auto col_resss = ColumnString::create();
if (const auto * col = checkAndGetColumn<ColumnConst>(column_src.get()))
{
std::string result_value;
Impl::constant(
col->getValue<String>(),
col_needle->getValue<String>(),
col_replacement->getValue<String>(),
pos,
occ,
match_type,
collator,
result_value);
auto col_res = ColumnString::create();
col_res->insert(result_value);
column_result.column = std::move(col_res);
}
else
throw Exception(
"Illegal column " + column_src->getName() + " of first argument of function " + getName()
+ "Illegal column " + column_needle->getName() + " of second argument of function " + getName()
+ "Illegal column " + column_replacement->getName() + " of third argument of function "
+ getName(),
"Illegal column " + column_src->getName() + " of first argument of function " + getName(),
ErrorCodes::ILLEGAL_COLUMN);
}
else
{
throw Exception(
"Argument at index 1 and 2 and 3 for function replace must be constant",
"Argument at index 2 and 3 for function replace must be constant",
ErrorCodes::ILLEGAL_COLUMN);
}
}
Expand Down
Loading