Skip to content

Commit

Permalink
[branch-2.1](function) fix random_bytes return same data for multi ro…
Browse files Browse the repository at this point in the history
…ws (#39891) (#40137)

pick #39891

Issue Number: close #xxx

before:
```sql
mysql [optest]>SELECT random_bytes(10) a FROM numbers("number" = "10");
+------------------------+
| a                      |
+------------------------+
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
| 0x7b4e5727024bc5b59e2c |
+------------------------+
```

now:
```sql
mysql [optest]>SELECT random_bytes(10) a FROM numbers("number" = "10");
+------------------------+
| a                      |
+------------------------+
| 0xd82cf60825b29ef2a0fd |
| 0x6f8c808415bdbaa6d257 |
| 0x7c26b5214297a151c25c |
| 0x43f02c77293063900437 |
| 0x5e5727569dec5e24f96b |
| 0x434f20bf74d7759640b7 |
| 0x087ed96b739750c733a6 |
| 0xdf05f6d7ede4972eb846 |
| 0xcefab471912264b5c54f |
| 0x1bddc019409d1926aa10 |
+------------------------+
```

## Proposed changes

Issue Number: close #xxx

<!--Describe your changes.-->
  • Loading branch information
zclllyybb authored Aug 30, 2024
1 parent e4d543f commit 163193b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 628 deletions.
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 @@ -89,11 +89,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

0 comments on commit 163193b

Please sign in to comment.