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

[branch-2.1](function) fix random_bytes return same data for multi rows (#39891) #40137

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions be/src/vec/functions/function_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -3107,31 +3107,37 @@ class FunctionRandomBytes : public IFunction {
return std::make_shared<DataTypeString>();
}

bool use_default_implementation_for_constants() const final { return false; }

Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
size_t result, size_t input_rows_count) const override {
auto res = ColumnString::create();
auto& res_offsets = res->get_offsets();
auto& res_chars = res->get_chars();
res_offsets.resize(input_rows_count);

ColumnPtr argument_column =
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
const auto* length_col = assert_cast<const ColumnInt32*>(argument_column.get());
auto [arg_col, arg_const] = unpack_if_const(block.get_by_position(arguments[0]).column);
const auto* length_col = assert_cast<const ColumnInt32*>(arg_col.get());

if (arg_const) {
res_chars.reserve(input_rows_count * (length_col->get_element(0) + 2));
}

std::vector<uint8_t> random_bytes;
std::random_device rd;
std::mt19937 gen(rd());

std::uniform_int_distribution<unsigned short> distribution(0, 255);
for (size_t i = 0; i < input_rows_count; ++i) {
if (length_col->get_element(i) < 0) [[unlikely]] {
size_t index = index_check_const(i, arg_const);
if (length_col->get_element(index) < 0) [[unlikely]] {
return Status::InvalidArgument("argument {} of function {} at row {} was invalid.",
length_col->get_element(i), name, i);
length_col->get_element(index), name, index);
}
random_bytes.resize(length_col->get_element(i));
random_bytes.resize(length_col->get_element(index));

std::uniform_int_distribution<uint8_t> distribution(0, 255);
for (auto& byte : random_bytes) {
byte = distribution(gen);
byte = distribution(gen) & 0xFF;
}

std::ostringstream oss;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ public boolean nullable() {
}
}

@Override
public boolean foldable() {
return false;
}

/**
* withChildren.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.doris.catalog.FunctionSignature;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
import org.apache.doris.nereids.trees.expressions.functions.Nondeterministic;
import org.apache.doris.nereids.trees.expressions.functions.PropagateNullable;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;
Expand All @@ -35,7 +36,7 @@
* ScalarFunction 'random_bytes'. This class is generated by GenerateFunction.
*/
public class RandomBytes extends ScalarFunction
implements ExplicitlyCastableSignature, PropagateNullable {
implements ExplicitlyCastableSignature, PropagateNullable, Nondeterministic {

public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
FunctionSignature.ret(StringType.INSTANCE).args(IntegerType.INSTANCE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,3 @@ string3
string3
string3

-- !sql_random_bytes --
\N

Loading
Loading