| 1 | #include <TableFunctions/ITableFunction.h> |
| 2 | #include <TableFunctions/ITableFunctionFileLike.h> |
| 3 | #include <TableFunctions/parseColumnsListForTableFunction.h> |
| 4 | |
| 5 | #include <Parsers/ASTFunction.h> |
| 6 | #include <Parsers/ASTLiteral.h> |
| 7 | |
| 8 | #include <Common/Exception.h> |
| 9 | #include <Common/typeid_cast.h> |
| 10 | |
| 11 | #include <Storages/StorageFile.h> |
| 12 | |
| 13 | #include <Interpreters/Context.h> |
| 14 | #include <Interpreters/evaluateConstantExpression.h> |
| 15 | |
| 16 | |
| 17 | namespace DB |
| 18 | { |
| 19 | |
| 20 | namespace ErrorCodes |
| 21 | { |
| 22 | extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; |
| 23 | } |
| 24 | |
| 25 | StoragePtr ITableFunctionFileLike::executeImpl(const ASTPtr & ast_function, const Context & context, const std::string & table_name) const |
| 26 | { |
| 27 | /// Parse args |
| 28 | ASTs & args_func = ast_function->children; |
| 29 | |
| 30 | if (args_func.size() != 1) |
| 31 | throw Exception("Table function '" + getName() + "' must have arguments." , ErrorCodes::LOGICAL_ERROR); |
| 32 | |
| 33 | ASTs & args = args_func.at(0)->children; |
| 34 | |
| 35 | if (args.size() != 3 && args.size() != 4) |
| 36 | throw Exception("Table function '" + getName() + "' requires 3 or 4 arguments: filename, format, structure and compression method (default auto)." , |
| 37 | ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
| 38 | |
| 39 | for (size_t i = 0; i < args.size(); ++i) |
| 40 | args[i] = evaluateConstantExpressionOrIdentifierAsLiteral(args[i], context); |
| 41 | |
| 42 | std::string filename = args[0]->as<ASTLiteral &>().value.safeGet<String>(); |
| 43 | std::string format = args[1]->as<ASTLiteral &>().value.safeGet<String>(); |
| 44 | std::string structure = args[2]->as<ASTLiteral &>().value.safeGet<String>(); |
| 45 | std::string compression_method; |
| 46 | |
| 47 | if (args.size() == 4) |
| 48 | { |
| 49 | compression_method = args[3]->as<ASTLiteral &>().value.safeGet<String>(); |
| 50 | } else compression_method = "auto" ; |
| 51 | |
| 52 | ColumnsDescription columns = parseColumnsListFromString(structure, context); |
| 53 | |
| 54 | /// Create table |
| 55 | StoragePtr storage = getStorage(filename, format, columns, const_cast<Context &>(context), table_name, compression_method); |
| 56 | |
| 57 | storage->startup(); |
| 58 | |
| 59 | return storage; |
| 60 | } |
| 61 | |
| 62 | } |
| 63 | |