forked from EnterpriseDB/hdfs_fdw
/
hdfs_client.c
195 lines (176 loc) · 4.8 KB
/
hdfs_client.c
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
/*-------------------------------------------------------------------------
*
* hdfs_client.c
* Foreign-data wrapper for remote Hadoop servers
*
* Portions Copyright (c) 2004-2014, EnterpriseDB Corporation.
*
* IDENTIFICATION
* hdfs_client.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/lsyscache.h"
#include "funcapi.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "utils/syscache.h"
#include "libhive/odbc/hiveclient.h"
#include "hdfs_fdw.h"
HiveReturn
hdfs_fetch(hdfs_opt *opt, HiveResultSet *rs)
{
HiveReturn r;
char err_buf[512];
r = DBFetch(rs, err_buf, sizeof(err_buf));
if (r == HIVE_ERROR)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to fetch data from HiveServer: %s", err_buf)));
return r;
}
size_t
hdfs_get_column_count(hdfs_opt *opt, HiveResultSet *rs)
{
size_t count = 0;
char err_buf[512];
if (DBGetColumnCount(rs, &count, err_buf, sizeof(err_buf)) == HIVE_ERROR)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to get column count HiveServer: %s", err_buf)));
return count;
}
size_t
hdfs_get_field_data_len(hdfs_opt *opt, HiveResultSet *rs, int col)
{
size_t len;
char err_buf[512];
if (DBGetFieldDataLen(rs, col, &len, err_buf, sizeof(err_buf)) == HIVE_ERROR)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to fetch field length from HiveServer: %s", err_buf)));
return len;
}
Datum
hdfs_get_value(hdfs_opt *opt, Oid pgtyp, int pgtypmod, HiveResultSet *rs, int idx, bool *is_null, int len, int col_type)
{
Datum value_datum = 0;
Datum valueDatum = 0;
regproc typeinput;
HeapTuple tuple;
int typemod;
char *value;
/* get the type's output function */
tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(pgtyp));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for type %u", pgtyp);
typeinput = ((Form_pg_type)GETSTRUCT(tuple))->typinput;
typemod = ((Form_pg_type)GETSTRUCT(tuple))->typtypmod;
ReleaseSysCache(tuple);
switch (pgtyp)
{
case BITOID:
case BOOLOID:
case INT2OID:
case INT4OID:
case INT8OID:
case BYTEAOID:
case DATEOID:
case TIMEOID:
case TIMESTAMPOID:
case TIMESTAMPTZOID:
case FLOAT4OID:
case FLOAT8OID:
{
char *value;
value = hdfs_get_field_as_cstring(opt, rs, idx, is_null, len);
/* libhive return an empty string for null value */
if (strlen(value) == 0)
{
*is_null = true;
}
else
{
valueDatum = CStringGetDatum((char*)value);
value_datum = OidFunctionCall3(typeinput, valueDatum, ObjectIdGetDatum(pgtyp), Int32GetDatum(typemod));
}
}
break;
case CHAROID:
case NAMEOID:
case TEXTOID:
case BPCHAROID:
case VARCHAROID:
{
switch (col_type)
{
case HDFS_TINYINT:
{
char str[10];
value = hdfs_get_field_as_cstring(opt, rs, idx, is_null, len);
if (strlen(value) == 0)
{
*is_null = true;
}
else
{
sprintf(str, "%d", value[0]);
valueDatum = CStringGetDatum((char*)str);
value_datum = OidFunctionCall3(typeinput, valueDatum, ObjectIdGetDatum(pgtyp), Int32GetDatum(typemod));
}
}
break;
default:
{
value = hdfs_get_field_as_cstring(opt, rs, idx, is_null, len);
valueDatum = CStringGetDatum((char*)value);
value_datum = OidFunctionCall3(typeinput, valueDatum, ObjectIdGetDatum(pgtyp), Int32GetDatum(typemod));
}
break;
}
}
break;
default:
ereport(ERROR, (errcode(ERRCODE_FDW_INVALID_DATA_TYPE),
errmsg("unknown or unsupported PostgreSQL data type"),
errhint("Supported data types are BOOL, INT, DATE, TIME, FLOAT, CHAR, TEXT and VARCHAR : %u", pgtyp)));
break;
}
return value_datum;
}
char *
hdfs_get_field_as_cstring(hdfs_opt *opt, HiveResultSet *rs, int idx, bool *is_null, int len)
{
size_t bs;
char *value = NULL;
int isnull = 1;
char err_buf[512];
value = (char*) palloc(len);
if (DBGetFieldAsCString(rs, idx, value, len, &bs, &isnull, err_buf, sizeof(err_buf)) == HIVE_ERROR)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to fetch field from HiveServer: %s", err_buf)));
if (isnull == 0)
*is_null = false;
else
*is_null = true;
return value;
}
HiveResultSet*
hdfs_query_execute(HiveConnection *conn, hdfs_opt *opt, char *query)
{
HiveResultSet *rs = NULL;
char err_buf[512];
if (DBExecute(conn, query, &rs, 1000, err_buf, sizeof(err_buf)) != HIVE_SUCCESS)
ereport(ERROR,
(errcode(ERRCODE_FDW_UNABLE_TO_CREATE_EXECUTION),
errmsg("failed to fetch execute query: %s", err_buf)));
return rs;
}
void
hdfs_close_result_set(hdfs_opt *opt, HiveResultSet *rs)
{
char err_buf[512];
DBCloseResultSet(rs, err_buf, sizeof(err_buf));
}