|
5 | 5 | #include "DdbPythonUtil.h" |
6 | 6 | #include "Pickle.h" |
7 | 7 | #include "Wrappers.h" |
| 8 | +#include "ConstantFactory.h" |
8 | 9 |
|
9 | 10 | #define APIMinVersionRequirement 300 |
10 | 11 |
|
@@ -144,9 +145,11 @@ bool DBConnectionImpl::connect() { |
144 | 145 | if(asynTask_) { |
145 | 146 | SmartPointer<DBConnection> newConn = new DBConnection(false, false); |
146 | 147 | newConn->connect(hostName_, port_, userId_, pwd_); |
147 | | - requiredVersion =newConn->run("getRequiredAPIVersion()"); |
| 148 | + vector<ConstantSP> args; |
| 149 | + requiredVersion =newConn->run("getRequiredAPIVersion", args); |
148 | 150 | }else{ |
149 | | - requiredVersion = run("getRequiredAPIVersion()"); |
| 151 | + vector<ConstantSP> args; |
| 152 | + requiredVersion = run("getRequiredAPIVersion", args); |
150 | 153 | } |
151 | 154 | } |
152 | 155 | catch(...){ |
@@ -198,23 +201,23 @@ ConstantSP DBConnectionImpl::run(const string& funcName, vector<ConstantSP>& arg |
198 | 201 | py::object DBConnectionImpl::runPy( |
199 | 202 | const string &script, int priority, |
200 | 203 | int parallelism, int fetchSize, bool clearMemory, |
201 | | - bool pickleTableToList, bool disableDecimal |
| 204 | + bool pickleTableToList, bool disableDecimal, bool withTableSchema |
202 | 205 | ) { |
203 | 206 | vector<ConstantSP> args; |
204 | 207 | return runPy( |
205 | 208 | script, "script", args, priority, parallelism, |
206 | | - fetchSize, clearMemory, pickleTableToList, disableDecimal |
| 209 | + fetchSize, clearMemory, pickleTableToList, disableDecimal, withTableSchema |
207 | 210 | ); |
208 | 211 | } |
209 | 212 |
|
210 | 213 | py::object DBConnectionImpl::runPy( |
211 | 214 | const string &funcName, vector<ConstantSP> &args, int priority, |
212 | 215 | int parallelism, int fetchSize, bool clearMemory, |
213 | | - bool pickleTableToList, bool disableDecimal |
| 216 | + bool pickleTableToList, bool disableDecimal, bool withTableSchema |
214 | 217 | ) { |
215 | 218 | return runPy( |
216 | 219 | funcName, "function", args, priority, parallelism, |
217 | | - fetchSize, clearMemory, pickleTableToList, disableDecimal |
| 220 | + fetchSize, clearMemory, pickleTableToList, disableDecimal, withTableSchema |
218 | 221 | ); |
219 | 222 | } |
220 | 223 |
|
@@ -423,10 +426,32 @@ ConstantSP DBConnectionImpl::run(const string& script, const string& scriptType, |
423 | 426 | return result; |
424 | 427 | } |
425 | 428 |
|
| 429 | +py::dict getTableSchema(ConstantSP result) |
| 430 | +{ |
| 431 | + TableSP ddbTbl = result; |
| 432 | + py::dict column_types; |
| 433 | + size_t columnSize = ddbTbl->columns(); |
| 434 | + |
| 435 | + for (size_t i = 0; i < columnSize; ++i) |
| 436 | + { |
| 437 | + py::str column_name = ddbTbl->getColumnName(i); |
| 438 | + DATA_TYPE type = ddbTbl->getColumnType(i); |
| 439 | + py::str typeString = Util::getDataTypeString(type); |
| 440 | + |
| 441 | + py::object scale = py::none(); |
| 442 | + if ((type >= DT_DECIMAL32 && type <= DT_DECIMAL128) || (type >= DT_DECIMAL32_ARRAY && type <= DT_DECIMAL128_ARRAY)) { |
| 443 | + scale = py::int_(ddbTbl->getColumn(i)->getExtraParamForType()); |
| 444 | + } |
| 445 | + |
| 446 | + column_types[column_name] = py::make_tuple(typeString, scale); |
| 447 | + } |
| 448 | + return column_types; |
| 449 | +} |
| 450 | + |
426 | 451 | py::object DBConnectionImpl::runPy( |
427 | 452 | const string &script, const string &scriptType, vector<ConstantSP> &args, |
428 | 453 | int priority, int parallelism, int fetchSize, bool clearMemory, |
429 | | - bool pickleTableToList, bool disableDecimal |
| 454 | + bool pickleTableToList, bool disableDecimal, bool withTableSchema |
430 | 455 | ) { |
431 | 456 | //RecordTime record("Db.runPy"+script); |
432 | 457 | DLOG("runPy",script,"start argsize",args.size()); |
@@ -543,6 +568,10 @@ py::object DBConnectionImpl::runPy( |
543 | 568 | } |
544 | 569 |
|
545 | 570 | if (numObject == 0) { |
| 571 | + if (withTableSchema) { |
| 572 | + py::gil_scoped_acquire pgil; |
| 573 | + return py::make_tuple(py::none(), py::none()); |
| 574 | + } |
546 | 575 | return py::none(); |
547 | 576 | } |
548 | 577 |
|
@@ -620,6 +649,15 @@ py::object DBConnectionImpl::runPy( |
620 | 649 | py::gil_scoped_acquire pgil; |
621 | 650 |
|
622 | 651 | converter::ToPythonOption option(pickleTableToList); |
| 652 | + |
| 653 | + if (withTableSchema) { |
| 654 | + py::object dataframe = converter::Converter::toPython_Old(result, option); |
| 655 | + if (result->getForm() == DATA_FORM::DF_TABLE) { |
| 656 | + return py::make_tuple(dataframe, getTableSchema(result)); |
| 657 | + } |
| 658 | + return py::make_tuple(dataframe, py::none()); |
| 659 | + } |
| 660 | + |
623 | 661 | return converter::Converter::toPython_Old(result, option); |
624 | 662 | } |
625 | 663 |
|
|
0 commit comments