Skip to content

Commit 9bc6b55

Browse files
committed
Sometimes generate a not matching structure
1 parent c043393 commit 9bc6b55

File tree

5 files changed

+105
-84
lines changed

5 files changed

+105
-84
lines changed

src/Client/BuzzHouse/AST/SQLProtoStr.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,9 +1730,7 @@ CONV_FN(FileFunc, ff)
17301730
ret += "'";
17311731
if (ff.has_structure())
17321732
{
1733-
ret += ", '";
1734-
ret += ff.structure();
1735-
ret += "'";
1733+
ExprToString(ret, ff.structure());
17361734
}
17371735
if (ff.has_fcomp())
17381736
{
@@ -1749,9 +1747,7 @@ CONV_FN(FormatFunc, ff)
17491747
ret += InFormat_Name(ff.format()).substr(3);
17501748
if (ff.has_structure())
17511749
{
1752-
ret += "', '";
1753-
ret += ff.structure();
1754-
ret += "'";
1750+
ExprToString(ret, ff.structure());
17551751
}
17561752
ret += ", $$\n";
17571753
ret += ff.data();
@@ -1906,9 +1902,8 @@ CONV_FN(S3Func, sfunc)
19061902
ret += sfunc.password();
19071903
ret += "', '";
19081904
ret += InOutFormat_Name(sfunc.format()).substr(6);
1909-
ret += "', '";
1910-
ret += sfunc.structure();
1911-
ret += "'";
1905+
ret += "', ";
1906+
ExprToString(ret, sfunc.structure());
19121907
if (sfunc.has_fcomp())
19131908
{
19141909
ret += ", '";

src/Client/BuzzHouse/Generator/QueryOracle.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,10 @@ void QueryOracle::dumpTableContent(RandomGenerator & rg, StatementGenerator & ge
226226
void QueryOracle::generateExportQuery(
227227
RandomGenerator & rg, StatementGenerator & gen, const bool test_content, const SQLTable & t, SQLQuery & sq2)
228228
{
229-
String buf;
230-
bool first = true;
231229
std::error_code ec;
232230
Insert * ins = sq2.mutable_explain()->mutable_inner_query()->mutable_insert();
233231
FileFunc * ff = ins->mutable_tof()->mutable_tfunc()->mutable_file();
232+
Expr * expr = ff->mutable_structure();
234233
SelectStatementCore * sel = ins->mutable_select()->mutable_select_core();
235234
const std::filesystem::path & nfile = fc.db_file_path / "table.data";
236235
OutFormat outf = rg.pickRandomly(outIn);
@@ -244,29 +243,40 @@ void QueryOracle::generateExportQuery(
244243
ff->set_path(nfile.generic_string());
245244

246245
gen.flatTableColumnPath(skip_nested_node | flat_nested, t.cols, [](const SQLColumn & c) { return c.canBeInserted(); });
247-
for (const auto & entry : gen.entries)
246+
if (!can_test_query_success && rg.nextSmallNumber() < 3)
247+
{
248+
/// Sometimes generate a not matching structure
249+
gen.addRandomRelation(rg, std::nullopt, gen.entries.size(), expr);
250+
}
251+
else
248252
{
249-
SQLType * tp = entry.getBottomType();
253+
String buf;
254+
bool first = true;
250255

251-
buf += fmt::format(
252-
"{}{} {}{}{}{}",
253-
first ? "" : ", ",
254-
entry.getBottomName(),
255-
entry.path.size() > 1 ? "Array(" : "",
256-
tp->typeName(true),
257-
entry.path.size() > 1 ? ")" : "",
258-
(entry.path.size() == 1 && entry.nullable.has_value()) ? (entry.nullable.value() ? " NULL" : " NOT NULL") : "");
259-
gen.columnPathRef(entry, sel->add_result_columns()->mutable_etc()->mutable_col()->mutable_path());
260-
/// ArrowStream doesn't support UUID
261-
if (outf == OutFormat::OUT_ArrowStream && tp->getTypeClass() == SQLTypeClass::UUID)
256+
for (const auto & entry : gen.entries)
262257
{
263-
outf = OutFormat::OUT_CSV;
258+
SQLType * tp = entry.getBottomType();
259+
260+
buf += fmt::format(
261+
"{}{} {}{}{}{}",
262+
first ? "" : ", ",
263+
entry.getBottomName(),
264+
entry.path.size() > 1 ? "Array(" : "",
265+
tp->typeName(true),
266+
entry.path.size() > 1 ? ")" : "",
267+
(entry.path.size() == 1 && entry.nullable.has_value()) ? (entry.nullable.value() ? " NULL" : " NOT NULL") : "");
268+
gen.columnPathRef(entry, sel->add_result_columns()->mutable_etc()->mutable_col()->mutable_path());
269+
/// ArrowStream doesn't support UUID
270+
if (outf == OutFormat::OUT_ArrowStream && tp->getTypeClass() == SQLTypeClass::UUID)
271+
{
272+
outf = OutFormat::OUT_CSV;
273+
}
274+
first = false;
264275
}
265-
first = false;
276+
expr->mutable_lit_val()->set_string_lit(std::move(buf));
266277
}
267278
gen.entries.clear();
268279
ff->set_outformat(outf);
269-
ff->set_structure(buf);
270280
if (rg.nextSmallNumber() < 4)
271281
{
272282
ff->set_fcomp(static_cast<FileCompression>((rg.nextRandomUInt32() % static_cast<uint32_t>(FileCompression_MAX)) + 1));

src/Client/BuzzHouse/Generator/SQLQuery.cpp

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void StatementGenerator::setTableRemote(
254254
first = false;
255255
}
256256
this->remote_entries.clear();
257-
sfunc->set_structure(buf);
257+
sfunc->mutable_structure()->mutable_lit_val()->set_string_lit(std::move(buf));
258258
if (!t.file_comp.empty())
259259
{
260260
sfunc->set_fcomp(t.file_comp);
@@ -319,6 +319,72 @@ auto StatementGenerator::getQueryTableLambda()
319319
};
320320
}
321321

322+
void StatementGenerator::addRandomRelation(RandomGenerator & rg, const std::optional<String> rel_name, const uint32_t ncols, Expr * expr)
323+
{
324+
if (rg.nextBool())
325+
{
326+
/// Use generateRandomStructure function
327+
SQLFuncCall * sfc = expr->mutable_comp_expr()->mutable_func_call();
328+
sfc->mutable_func()->set_catalog_func(FUNCgenerateRandomStructure);
329+
330+
/// Number of columns parameter
331+
sfc->add_args()->mutable_expr()->mutable_lit_val()->mutable_int_lit()->set_uint_lit(static_cast<uint64_t>(ncols));
332+
/// Seed parameter
333+
sfc->add_args()->mutable_expr()->mutable_lit_val()->mutable_int_lit()->set_uint_lit(rg.nextRandomUInt64());
334+
if (rel_name.has_value())
335+
{
336+
SQLRelation rel(rel_name.value());
337+
338+
for (uint32_t i = 0; i < ncols; i++)
339+
{
340+
rel.cols.emplace_back(SQLRelationCol(rel_name.value(), {"c" + std::to_string(i + 1)}));
341+
}
342+
this->levels[this->current_level].rels.emplace_back(rel);
343+
}
344+
}
345+
else
346+
{
347+
/// Use BuzzHouse approach
348+
String buf;
349+
bool first = true;
350+
uint32_t col_counter = 0;
351+
const uint32_t type_mask_backup = this->next_type_mask;
352+
std::unordered_map<uint32_t, std::unique_ptr<SQLType>> centries;
353+
354+
this->next_type_mask = fc.type_mask;
355+
for (uint32_t i = 0; i < ncols; i++)
356+
{
357+
const uint32_t ncame = col_counter++;
358+
auto tp = std::unique_ptr<SQLType>(randomNextType(rg, this->next_type_mask, col_counter, nullptr));
359+
360+
buf += fmt::format("{}c{} {}", first ? "" : ", ", ncame, tp->typeName(false));
361+
first = false;
362+
centries[ncame] = std::move(tp);
363+
}
364+
this->next_type_mask = type_mask_backup;
365+
expr->mutable_lit_val()->set_string_lit(std::move(buf));
366+
if (rel_name.has_value())
367+
{
368+
SQLRelation rel(rel_name.value());
369+
370+
flatColumnPath(flat_tuple | flat_nested | flat_json | to_table_entries | collect_generated, centries);
371+
for (const auto & entry : this->table_entries)
372+
{
373+
DB::Strings names;
374+
375+
names.reserve(entry.path.size());
376+
for (const auto & path : entry.path)
377+
{
378+
names.push_back(path.cname);
379+
}
380+
rel.cols.emplace_back(SQLRelationCol(rel_name.value(), std::move(names)));
381+
}
382+
this->table_entries.clear();
383+
this->levels[this->current_level].rels.emplace_back(rel);
384+
}
385+
}
386+
}
387+
322388
bool StatementGenerator::joinedTableOrFunction(
323389
RandomGenerator & rg, const String & rel_name, const uint32_t allowed_clauses, const bool under_remote, TableOrFunction * tof)
324390
{
@@ -743,66 +809,15 @@ bool StatementGenerator::joinedTableOrFunction(
743809
< (derived_table + cte + table + view + remote_udf + generate_series_udf + system_table + merge_udf + cluster_udf
744810
+ merge_index_udf + loop_udf + values_udf + random_data_udf + 1))
745811
{
746-
SQLRelation rel(rel_name);
747-
const uint32_t ncols = (rg.nextSmallNumber() < 8) ? rg.nextSmallNumber() : rg.nextMediumNumber();
748812
GenerateRandomFunc * grf = tof->mutable_tfunc()->mutable_grandom();
749813
std::uniform_int_distribution<uint64_t> string_length_dist(1, 8192);
750814
std::uniform_int_distribution<uint64_t> nested_rows_dist(fc.min_nested_rows, fc.max_nested_rows);
751815

752-
if (rg.nextBool())
753-
{
754-
/// Use generateRandomStructure function
755-
SQLFuncCall * sfc = grf->mutable_structure()->mutable_comp_expr()->mutable_func_call();
756-
sfc->mutable_func()->set_catalog_func(FUNCgenerateRandomStructure);
757-
758-
/// Number of columns parameter
759-
sfc->add_args()->mutable_expr()->mutable_lit_val()->mutable_int_lit()->set_uint_lit(static_cast<uint64_t>(ncols));
760-
/// Seed parameter
761-
sfc->add_args()->mutable_expr()->mutable_lit_val()->mutable_int_lit()->set_uint_lit(rg.nextRandomUInt64());
762-
for (uint32_t i = 0; i < ncols; i++)
763-
{
764-
rel.cols.emplace_back(SQLRelationCol(rel_name, {"c" + std::to_string(i + 1)}));
765-
}
766-
}
767-
else
768-
{
769-
/// Use BuzzHouse approach
770-
String buf;
771-
bool first = true;
772-
uint32_t col_counter = 0;
773-
const uint32_t type_mask_backup = this->next_type_mask;
774-
std::unordered_map<uint32_t, std::unique_ptr<SQLType>> centries;
775-
776-
this->next_type_mask = fc.type_mask;
777-
for (uint32_t i = 0; i < ncols; i++)
778-
{
779-
const uint32_t ncame = col_counter++;
780-
auto tp = std::unique_ptr<SQLType>(randomNextType(rg, this->next_type_mask, col_counter, nullptr));
781-
782-
buf += fmt::format("{}c{} {}", first ? "" : ", ", ncame, tp->typeName(false));
783-
first = false;
784-
centries[ncame] = std::move(tp);
785-
}
786-
this->next_type_mask = type_mask_backup;
787-
grf->mutable_structure()->mutable_lit_val()->set_string_lit(std::move(buf));
788-
flatColumnPath(flat_tuple | flat_nested | flat_json | to_table_entries | collect_generated, centries);
789-
for (const auto & entry : this->table_entries)
790-
{
791-
DB::Strings names;
792-
793-
names.reserve(entry.path.size());
794-
for (const auto & path : entry.path)
795-
{
796-
names.push_back(path.cname);
797-
}
798-
rel.cols.emplace_back(SQLRelationCol(rel_name, std::move(names)));
799-
}
800-
this->table_entries.clear();
801-
}
816+
addRandomRelation(
817+
rg, rel_name, (rg.nextSmallNumber() < 8) ? rg.nextSmallNumber() : rg.nextMediumNumber(), grf->mutable_structure());
802818
grf->set_random_seed(rg.nextRandomUInt64());
803819
grf->set_max_string_length(string_length_dist(rg.generator));
804820
grf->set_max_array_length(nested_rows_dist(rg.generator));
805-
this->levels[this->current_level].rels.emplace_back(rel);
806821
}
807822
else if (
808823
dictionary

src/Client/BuzzHouse/Generator/StatementGenerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class StatementGenerator
289289
void flatTableColumnPath(
290290
uint32_t flags, const std::unordered_map<uint32_t, SQLColumn> & cols, std::function<bool(const SQLColumn & c)> col_filter);
291291
void flatColumnPath(uint32_t flags, const std::unordered_map<uint32_t, std::unique_ptr<SQLType>> & centries);
292+
void addRandomRelation(RandomGenerator & rg, const std::optional<String> rel_name, uint32_t ncols, Expr * expr);
292293
void generateStorage(RandomGenerator & rg, Storage * store) const;
293294
void generateNextCodecs(RandomGenerator & rg, CodecList * cl);
294295
void generateTTLExpression(RandomGenerator & rg, const std::optional<SQLTable> & t, Expr * ttl_expr);

src/Client/BuzzHouse/Proto/SQLGrammar.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,13 +2421,13 @@ message FileFunc {
24212421
InFormat informat = 2;
24222422
OutFormat outformat = 3;
24232423
}
2424-
optional string structure = 4;
2424+
optional Expr structure = 4;
24252425
optional FileCompression fcomp = 5;
24262426
}
24272427

24282428
message FormatFunc {
24292429
required InFormat format = 1;
2430-
optional string structure = 2;
2430+
optional Expr structure = 2;
24312431
required string data = 3;
24322432
}
24332433

@@ -2490,7 +2490,7 @@ message S3Func {
24902490
required string user = 2;
24912491
required string password = 3;
24922492
required InOutFormat format = 4;
2493-
required string structure = 5;
2493+
required Expr structure = 5;
24942494
optional string fcomp = 6;
24952495
}
24962496

0 commit comments

Comments
 (0)