#include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; } StoragePtr ITableFunctionFileLike::executeImpl(const ASTPtr & ast_function, const Context & context) const { // Parse args ASTs & args_func = typeid_cast(*ast_function).children; if (args_func.size() != 1) throw Exception("Table function '" + getName() + "' must have arguments.", ErrorCodes::LOGICAL_ERROR); ASTs & args = typeid_cast(*args_func.at(0)).children; if (args.size() != 3) throw Exception("Table function '" + getName() + "' requires exactly 3 arguments: source, format and structure.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); for (size_t i = 0; i < 3; ++i) args[i] = evaluateConstantExpressionOrIdentifierAsLiteral(args[i], context); std::string source = static_cast(*args[0]).value.safeGet(); std::string format = static_cast(*args[1]).value.safeGet(); std::string structure = static_cast(*args[2]).value.safeGet(); // Create sample block std::vector structure_vals; boost::split(structure_vals, structure, boost::algorithm::is_any_of(" ,"), boost::algorithm::token_compress_on); if (structure_vals.size() % 2 != 0) throw Exception("Odd number of elements in section structure: must be a list of name type pairs", ErrorCodes::LOGICAL_ERROR); Block sample_block; const DataTypeFactory & data_type_factory = DataTypeFactory::instance(); for (size_t i = 0, size = structure_vals.size(); i < size; i += 2) { ColumnWithTypeAndName column; column.name = structure_vals[i]; column.type = data_type_factory.get(structure_vals[i + 1]); column.column = column.type->createColumn(); sample_block.insert(std::move(column)); } // Create table StoragePtr storage = getStorage(source, format, sample_block, const_cast(context)); storage->startup(); return storage; } }