FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
orc_fdw/src/orc_wrapper.cpp at master · HighgoSoftware/orc_fdw · GitHub
HighgoSoftware
orc_fdw
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
orc_fdw
/
src
/
orc_wrapper.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
206 lines (177 loc) · 6.25 KB
Breadcrumbs
orc_fdw
/
src
/
orc_wrapper.cpp
Copy path
File metadata and controls
206 lines (177 loc) · 6.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
-------------------------------------------------------------------------
*
* orc_wrapper.cpp
* An encapsulator for ORC file reader/write functions
*
* 2020, Hamid Quddus Akhtar.
*
* Contains functions that wrap Apache ORC library functions to be used
* by ORC FDW.
*
* Copyright (c) 2020, Highgo Software Inc.
*
* IDENTIFICATION
* src/orc_wrapper.cpp
*
*-------------------------------------------------------------------------
*/
/*
ORC FDW header files
*/
#
include
<
orc_wrapper.h
>
#
include
<
orc_interface_typedefs.h
>
/*
Apache ORC header files
*/
#
include
<
orc/Exceptions.hh
>
#
include
<
orc/OrcFile.hh
>
/*
PostgreSQL and FDW header files
*/
extern
"
C
"
{
#
include
"
c.h
"
#
include
"
orc_fdw.h
"
}
/*
Declare the functions to use within this file
*/
static
std::string
IsSupportedVersion
(
ORC_UNIQUE_PTR
<orc::Reader> *p_reader);
/*
* orcCreateReader
* Creates Apache ORC file reader for file in a safe way for fdw.
* Creates a reader for the specified filename and stores it in
* the unique_ptr in p_reader.
*/
bool
orcCreateReader
(std::string filename,
ORC_UNIQUE_PTR
<orc::Reader> *p_reader,
orc::ReaderOptions &options,
bool
blnVersionWarn)
{
/*
Let's catch exceptions and throw an error
*/
try
{
ORC_UNIQUE_PTR
<orc::InputStream> inStream =
orc::readLocalFile
(filename.
c_str
());
*p_reader =
orc::createReader
(
std::move
(inStream), options);
}
catch
(orc::ParseError& err)
{
ereport
(
ERROR
, (
errmsg
(
"
%s: %s
"
,
ORC_FDW_NAME
, err.
what
())));
}
/*
Throw a warning for unsupported ORC version
*/
std::string fileVersion =
IsSupportedVersion
(p_reader);
if
(fileVersion.
empty
() ==
false
&& blnVersionWarn )
{
ereport
(
WARNING
, (
errmsg
(
"
%s: Unsupported ORC file %s version 0.11.
"
,
ORC_FDW_NAME
, filename.
c_str
()),
(
errhint
(
"
This may still work, but it's strongly recommended to use files that are supported by the fdw.
"
))));
}
/*
*p_reader should never by NULL here, but just in case
*/
return
((*p_reader) !=
NULL
);
}
/*
* orcCreateRowReader
* Creates Apache ORC file row reader in safe way for fdw.
*/
bool
orcCreateRowReader
(
ORC_UNIQUE_PTR
<orc::Reader> *p_reader,
ORC_UNIQUE_PTR
<orc::RowReader> *p_rowReader,
orc::RowReaderOptions &rowReaderOptions)
{
*p_rowReader = (*p_reader)->
createRowReader
(rowReaderOptions);
if
(*p_rowReader ==
NULL
)
{
ereport
(
ERROR
, (
errmsg
(
"
%s: Unable to create row reader for ORC file.
"
,
ORC_FDW_NAME
)));
}
return
true
;
}
/*
* orcGetNumberOfRows
* Returns number of rows in the ORC file.
*/
uint64_t
orcGetNumberOfRows
(
ORC_UNIQUE_PTR
<orc::Reader> *p_reader)
{
return
(*p_reader)->
getNumberOfRows
();
}
/*
* orcGetColsInfo
* Get column meta data and return an std::vector of OrcFileColInfo.
*/
std::vector<OrcFileColInfo>
orcGetColsInfo
(std::string file_pathname,
ORC_UNIQUE_PTR
<orc::Reader> *p_reader, orc::StructVectorBatch **p_root)
{
orc::ReaderOptions options;
(
void
)
orcCreateReader
(file_pathname, p_reader, options,
true
);
orc::RowReaderOptions rowReaderOptions;
ORC_UNIQUE_PTR
<orc::RowReader> rowReader;
(
void
)
orcCreateRowReader
(p_reader, &rowReader, rowReaderOptions);
/*
Check for batch for reading
*/
ORC_UNIQUE_PTR
<orc::ColumnVectorBatch> batch = rowReader->
createRowBatch
(
1
);
if
(batch ==
NULL
)
{
ereport
(
ERROR
, (
errmsg
(
"
%s: Unable to create row batch for reading.
"
,
ORC_FDW_NAME
)));
}
/*
Get the batch
*/
*p_root =
dynamic_cast
<orc::StructVectorBatch *>(batch.
get
());
if
((*p_root) ==
NULL
)
{
ereport
(
ERROR
, (
errmsg
(
"
%s: Unable to get batch from ORC file.
"
,
ORC_FDW_NAME
)));
}
/*
Return column information std::tuple
*/
return
orcGetColsInfo
(p_reader, &rowReader, *p_root);
}
/*
* orcGetColsInfo
* Fills and returns a vector of tuples with column meta data.
*/
std::vector<OrcFileColInfo>
orcGetColsInfo
(
ORC_UNIQUE_PTR
<orc::Reader> *p_reader,
ORC_UNIQUE_PTR
<orc::RowReader> *p_rowReader, orc::StructVectorBatch *root)
{
std::vector<OrcFileColInfo> col_list;
//
for (uint col_index = 0; col_index < (*p_rowReader)->getSelectedType().getSubtypeCount(); col_index++)
for
(uint col_index =
0
; col_index < root->
fields
.
size
(); col_index++)
{
OrcFileColInfo col;
auto
orc_col_id = (*p_rowReader)->
getSelectedType
().
getSubtype
(col_index)->
getColumnId
();
col.
hasNull
= (*p_reader)->
getColumnStatistics
(orc_col_id)->
hasNull
();
col.
kind
= (*p_rowReader)->
getSelectedType
().
getSubtype
(col_index)->
getKind
();
col.
max_length
= (*p_rowReader)->
getSelectedType
().
getSubtype
(col_index)->
getMaximumLength
();
col.
precision
= (*p_rowReader)->
getSelectedType
().
getSubtype
(col_index)->
getPrecision
();
col.
scale
= (*p_rowReader)->
getSelectedType
().
getSubtype
(col_index)->
getScale
();
/*
Index must be fixed if the rowReader was created for specific columns
*/
col.
index
= col_index;
col.
name
= (*p_rowReader)->
getSelectedType
().
getFieldName
(col_index);
col_list.
push_back
(col);
}
return
col_list;
}
/*
* IsSupportedVersion
* To be used internally in this file, for a supported version, returns
* emptry string otherwise returns the ORC format version of file
* as string to be used in an message.
*/
static
std::string
IsSupportedVersion
(
ORC_UNIQUE_PTR
<orc::Reader> *p_reader)
{
if
((*p_reader)->
getFormatVersion
() !=
orc::FileVersion
(
0
,
12
))
{
return
(*p_reader)->
getFormatVersion
().
toString
();
}
return
std::string
(
"
"
);
}
/*
* orcGetDefaultDecimalScale
* ORC version 0.11 does not define decimal places for a decimal
* value. This function handles that. Although we don't support
* version 0.11, but having this one additional check improves
* 0.11 support in FDW.
*
* Returns 6 which is the default number of decimal places in
* 0.11 version. Otherwise, return empty so that we can pick
* number of decimal places from schema.
*/
int
orcGetDefaultDecimalScale
(
ORC_UNIQUE_PTR
<orc::Reader> *p_reader)
{
std::string fileVersion =
IsSupportedVersion
(p_reader);
/*
Let's assume that it 0.11
*/
if
(fileVersion.
empty
() ==
false
)
return
6
;
else
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL