2018-03-03 20:44:01 +00:00
|
|
|
#include "parquet_table.h"
|
|
|
|
|
|
|
|
#include "parquet/api/reader.h"
|
|
|
|
|
2018-03-25 03:51:15 +00:00
|
|
|
ParquetTable::ParquetTable(std::string file, std::string tableName): file(file), tableName(tableName) {
|
2018-03-15 23:57:38 +00:00
|
|
|
std::unique_ptr<parquet::ParquetFileReader> reader = parquet::ParquetFileReader::OpenFile(file.data());
|
|
|
|
metadata = reader->metadata();
|
2018-03-03 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 18:21:33 +00:00
|
|
|
std::string ParquetTable::columnName(int i) {
|
|
|
|
if(i == -1)
|
|
|
|
return "rowid";
|
|
|
|
return columnNames[i];
|
|
|
|
}
|
|
|
|
|
2018-06-23 23:10:23 +00:00
|
|
|
unsigned int ParquetTable::getNumColumns() {
|
|
|
|
return columnNames.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-03 20:44:01 +00:00
|
|
|
std::string ParquetTable::CreateStatement() {
|
2018-03-15 23:57:38 +00:00
|
|
|
std::unique_ptr<parquet::ParquetFileReader> reader = parquet::ParquetFileReader::OpenFile(
|
|
|
|
file.data(),
|
|
|
|
true,
|
|
|
|
parquet::default_reader_properties(),
|
|
|
|
metadata);
|
2018-03-03 20:44:01 +00:00
|
|
|
std::string text("CREATE TABLE x(");
|
|
|
|
auto schema = reader->metadata()->schema();
|
2018-03-08 18:21:33 +00:00
|
|
|
|
|
|
|
for(auto i = 0; i < schema->num_columns(); i++) {
|
|
|
|
auto _col = schema->GetColumnRoot(i);
|
|
|
|
columnNames.push_back(_col->name());
|
|
|
|
}
|
|
|
|
|
2018-03-03 20:44:01 +00:00
|
|
|
for(auto i = 0; i < schema->num_columns(); i++) {
|
|
|
|
auto _col = schema->GetColumnRoot(i);
|
|
|
|
|
|
|
|
if(!_col->is_primitive()) {
|
2018-03-04 01:00:50 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << __FILE__ << ":" << __LINE__ << ": column " << i << " has non-primitive type";
|
|
|
|
throw std::invalid_argument(ss.str());
|
2018-03-03 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(_col->is_repeated()) {
|
2018-03-04 01:00:50 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << __FILE__ << ":" << __LINE__ << ": column " << i << " has non-scalar type";
|
|
|
|
throw std::invalid_argument(ss.str());
|
2018-03-03 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parquet::schema::PrimitiveNode* col = (parquet::schema::PrimitiveNode*)_col;
|
|
|
|
|
|
|
|
if(i > 0)
|
|
|
|
text += ", ";
|
|
|
|
|
|
|
|
text += col->name();
|
|
|
|
|
|
|
|
std::string type;
|
2018-03-04 01:26:41 +00:00
|
|
|
|
|
|
|
parquet::Type::type physical = col->physical_type();
|
|
|
|
parquet::LogicalType::type logical = col->logical_type();
|
|
|
|
// Be explicit about which types we understand so we don't mislead someone
|
|
|
|
// whose unsigned ints start getting interpreted as signed. (We could
|
|
|
|
// support this for UINT_8/16/32 -- and for UINT_64 we could throw if
|
|
|
|
// the high bit was set.)
|
|
|
|
if(logical == parquet::LogicalType::NONE ||
|
|
|
|
logical == parquet::LogicalType::UTF8 ||
|
|
|
|
logical == parquet::LogicalType::DATE ||
|
|
|
|
logical == parquet::LogicalType::TIME_MILLIS ||
|
|
|
|
logical == parquet::LogicalType::TIMESTAMP_MILLIS ||
|
|
|
|
logical == parquet::LogicalType::TIME_MICROS ||
|
|
|
|
logical == parquet::LogicalType::TIMESTAMP_MICROS ||
|
|
|
|
logical == parquet::LogicalType::INT_8 ||
|
|
|
|
logical == parquet::LogicalType::INT_16 ||
|
|
|
|
logical == parquet::LogicalType::INT_32 ||
|
|
|
|
logical == parquet::LogicalType::INT_64) {
|
|
|
|
switch(physical) {
|
|
|
|
case parquet::Type::BOOLEAN:
|
2018-03-03 20:44:01 +00:00
|
|
|
type = "TINYINT";
|
2018-03-04 01:26:41 +00:00
|
|
|
break;
|
|
|
|
case parquet::Type::INT32:
|
|
|
|
if(logical == parquet::LogicalType::NONE ||
|
|
|
|
logical == parquet::LogicalType::INT_32) {
|
|
|
|
type = "INT";
|
|
|
|
} else if(logical == parquet::LogicalType::INT_8) {
|
|
|
|
type = "TINYINT";
|
|
|
|
} else if(logical == parquet::LogicalType::INT_16) {
|
|
|
|
type = "SMALLINT";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case parquet::Type::INT96:
|
|
|
|
// INT96 is used for nanosecond precision on timestamps; we truncate
|
|
|
|
// to millisecond precision.
|
|
|
|
case parquet::Type::INT64:
|
|
|
|
type = "BIGINT";
|
|
|
|
break;
|
|
|
|
case parquet::Type::FLOAT:
|
|
|
|
type = "REAL";
|
|
|
|
break;
|
|
|
|
case parquet::Type::DOUBLE:
|
|
|
|
type = "DOUBLE";
|
|
|
|
break;
|
|
|
|
case parquet::Type::BYTE_ARRAY:
|
|
|
|
if(logical == parquet::LogicalType::UTF8) {
|
|
|
|
type = "TEXT";
|
2018-03-04 22:20:28 +00:00
|
|
|
} else {
|
|
|
|
type = "BLOB";
|
2018-03-04 01:26:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case parquet::Type::FIXED_LEN_BYTE_ARRAY:
|
2018-03-04 22:20:28 +00:00
|
|
|
type = "BLOB";
|
|
|
|
break;
|
2018-03-04 01:26:41 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-03-03 20:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(type.empty()) {
|
2018-03-04 01:00:50 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << __FILE__ << ":" << __LINE__ << ": column " << i << " has unsupported type: " <<
|
2018-03-04 01:26:41 +00:00
|
|
|
parquet::TypeToString(physical) << "/" << parquet::LogicalTypeToString(logical);
|
2018-03-04 01:00:50 +00:00
|
|
|
|
|
|
|
throw std::invalid_argument(ss.str());
|
2018-03-03 20:44:01 +00:00
|
|
|
}
|
2018-03-04 01:00:50 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("col %d[name=%s, p=%d:%s, l=%d:%s] is %s\n",
|
|
|
|
i,
|
|
|
|
col->name().data(),
|
|
|
|
col->physical_type(),
|
|
|
|
parquet::TypeToString(col->physical_type()).data(),
|
|
|
|
col->logical_type(),
|
|
|
|
parquet::LogicalTypeToString(col->logical_type()).data(),
|
|
|
|
type.data());
|
|
|
|
#endif
|
|
|
|
|
2018-03-03 20:44:01 +00:00
|
|
|
text += " ";
|
|
|
|
text += type;
|
|
|
|
}
|
|
|
|
text +=");";
|
|
|
|
return text;
|
|
|
|
}
|
2018-03-15 23:57:38 +00:00
|
|
|
|
|
|
|
std::shared_ptr<parquet::FileMetaData> ParquetTable::getMetadata() { return metadata; }
|
2018-03-25 03:51:15 +00:00
|
|
|
|
|
|
|
const std::string& ParquetTable::getFile() { return file; }
|
|
|
|
const std::string& ParquetTable::getTableName() { return tableName; }
|