| 59 | }; |
| 60 | |
| 61 | void TableFunctionGenerateRandom::parseArguments(const ASTPtr & ast_function, ContextPtr context) |
| 62 | { |
| 63 | ASTs & args_func = ast_function->children; |
| 64 | |
| 65 | if (args_func.size() != 1) |
| 66 | throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function '{}' must have arguments.", getName()); |
| 67 | |
| 68 | ASTs & args = args_func.at(0)->children; |
| 69 | |
| 70 | if (args.empty()) |
| 71 | return; |
| 72 | |
| 73 | /// First, check if first argument is structure or seed. |
| 74 | const auto * first_arg_literal = args[0]->as<const ASTLiteral>(); |
| 75 | bool first_argument_is_structure = !first_arg_literal || first_arg_literal->value.getType() == Field::Types::String; |
| 76 | size_t max_args = first_argument_is_structure ? 4 : 3; |
| 77 | |
| 78 | if (args.size() > max_args) |
| 79 | throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, |
| 80 | "Table function '{}' requires at most four (or three if structure is missing) arguments: " |
| 81 | " [structure, random_seed, max_string_length, max_array_length].", getName()); |
| 82 | |
| 83 | if (first_argument_is_structure) |
| 84 | { |
| 85 | /// Allow constant expression for structure argument, it can be generated using generateRandomStructure function. |
| 86 | args[0] = evaluateConstantExpressionAsLiteral(args[0], context); |
| 87 | } |
| 88 | |
| 89 | // All the arguments must be literals. |
| 90 | for (const auto & arg : args) |
| 91 | { |
| 92 | const IAST * arg_raw = arg.get(); |
| 93 | if (const auto * func = arg_raw->as<const ASTFunction>(); func && func->name == "_CAST") |
| 94 | arg_raw = func->arguments->children.at(0).get(); |
| 95 | |
| 96 | if (!arg_raw->as<const ASTLiteral>()) |
| 97 | { |
| 98 | throw Exception(ErrorCodes::BAD_ARGUMENTS, |
| 99 | "All arguments of table function '{}' except structure argument must be literals. " |
| 100 | "Got '{}' instead", getName(), arg->formatForErrorMessage()); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | size_t arg_index = 0; |
| 105 | |
| 106 | if (first_argument_is_structure) |
| 107 | { |
| 108 | /// Parsing first argument as table structure and creating a sample block |
| 109 | structure = checkAndGetLiteralArgument<String>(args[arg_index], "structure"); |
| 110 | ++arg_index; |
| 111 | } |
| 112 | |
| 113 | if (args.size() >= arg_index + 1) |
| 114 | { |
| 115 | const IAST * arg_raw = args[arg_index].get(); |
| 116 | if (const auto * func = arg_raw->as<const ASTFunction>(); func && func->name == "_CAST") |
| 117 | arg_raw = func->arguments->children.at(0).get(); |
| 118 |
nothing calls this directly
no test coverage detected