PageRenderTime 36ms CodeModel.GetById 20ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/odbc/src/cpp/HiveResultSet.h

#
C Header | 190 lines | 90 code | 26 blank | 74 comment | 0 complexity | 8dc2512563a7ff71e23961626b21b9e7 MD5 | raw file
  1/**************************************************************************//**
  2 *
  3 * Licensed to the Apache Software Foundation (ASF) under one
  4 * or more contributor license agreements.  See the NOTICE file
  5 * distributed with this work for additional information
  6 * regarding copyright ownership.  The ASF licenses this file
  7 * to you under the Apache License, Version 2.0 (the
  8 * "License"); you may not use this file except in compliance
  9 * with the License.  You may obtain a copy of the License at
 10 *
 11 *   http://www.apache.org/licenses/LICENSE-2.0
 12 *
 13 * Unless required by applicable law or agreed to in writing, software
 14 * distributed under the License is distributed on an "AS IS" BASIS,
 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16 * See the License for the specific language governing permissions and
 17 * limitations under the License.
 18 *
 19 ******************************************************************************
 20 *
 21 * @file HiveResultSet.h
 22 * @brief Provides the HiveResultSet interface definition and subclasses.
 23 *
 24 *****************************************************************************/
 25
 26
 27#ifndef __hive_resultset_h__
 28#define __hive_resultset_h__
 29
 30#include <iostream>
 31
 32#include "hive_metastore_types.h"
 33
 34#include "hiveconstants.h"
 35#include "HiveConnection.h"
 36#include "HiveRowSet.h"
 37#include "HiveColumnDesc.h"
 38
 39using namespace std;
 40
 41
 42/*************************************************************************************************
 43 * Base HiveResultSet Class Abstract Declaration
 44 ************************************************************************************************/
 45
 46/**
 47 * @brief HiveResultSet interface definition.
 48 *
 49 * Abstract base class for Hive resultsets. Does not provide any additional logic for
 50 * subclasses, but defines the interface expected for all HiveResultSets.
 51 * All subclasses extending HiveResultSet need to at least implement the below methods.
 52 */
 53class HiveResultSet {
 54  public:
 55    virtual ~HiveResultSet() {} ///< The constructor should be defined independently by each subclass
 56    virtual HiveReturn fetchNext(char* err_buf, size_t err_buf_len) =0;
 57    virtual HiveReturn hasResults(int* results, char* err_buf, size_t err_buf_len) =0;
 58    virtual HiveReturn getColumnCount(size_t* col_count, char* err_buf, size_t err_buf_len) =0;
 59    virtual HiveReturn createColumnDesc(size_t column_idx, HiveColumnDesc** column_desc_ptr,
 60                                        char* err_buf, size_t err_buf_len) =0;
 61    /// The rowset will ONLY be valid after fetchNext has been called at least once
 62    virtual HiveRowSet& getRowSet() =0;
 63};
 64
 65
 66/*************************************************************************************************
 67 * HiveQueryResultSet Subclass Declaration
 68 ************************************************************************************************/
 69
 70/**
 71 * @brief A container for the resultsets of Hive queries.
 72 *
 73 * Container class for a query result set (the result of a DBExecute).
 74 * This class was only meant to be created by DBExecute and destroyed by DBCloseResultSet.
 75 * Implements a lazy row/field extraction approach.
 76 * A single instance should only belong to no more than one thread.
 77 * All errors messages will be written to err_buf if err_buf and err_buf_len are provided.
 78 */
 79class HiveQueryResultSet: public HiveResultSet {
 80  public:
 81    HiveQueryResultSet(int max_buf_rows);
 82    virtual ~HiveQueryResultSet();
 83    HiveReturn initialize(HiveConnection* connection, char* err_buf, size_t err_buf_len);
 84    HiveReturn fetchNext(char* err_buf, size_t err_buf_len);
 85    HiveReturn hasResults(int* results, char* err_buf, size_t err_buf_len);
 86    HiveReturn getColumnCount(size_t* col_count, char* err_buf, size_t err_buf_len);
 87    HiveReturn createColumnDesc(size_t column_idx, HiveColumnDesc** column_desc_ptr, char* err_buf,
 88                                size_t err_buf_len);
 89    HiveRowSet& getRowSet();
 90
 91  private:
 92    HiveConnection* m_connection; ///< Hive connection handle
 93    HiveSerializedRowSet m_serial_rowset; ///< Rowset associated with the current fetched row (if any)
 94    int m_max_buffered_rows; ///< Max number of rows to buffer in client memory
 95    int m_fetch_idx; ///< Last row fetched by the client
 96    bool m_has_results; ///< Indicates that at least one result row has been successfully fetched
 97    bool m_fetch_attempted; ///< Indicates that a Hive server fetch call has successfully executed
 98    vector<string> m_result_set_data; ///< Vector of serialized rows
 99    Apache::Hadoop::Hive::Schema m_schema; ///< Schema of the result table
100
101    HiveReturn initializeSchema(char* err_buf, size_t err_buf_len);
102    HiveReturn fetchNewResults(char* err_buf, size_t err_buf_len);
103};
104
105
106/*************************************************************************************************
107 * HiveTablesResultSet Subclass Declaration
108 ************************************************************************************************/
109
110/**
111 * @brief A container for resultsets describing the database table catalog.
112 *
113 * Container class for a pre-scripted table list resultset (the result of a DBTables).
114 * This class was only meant to be created by DBTables and destroyed by DBCloseResultSet.
115 * All error messages will be written to err_buf if err_buf and err_buf_len are provided.
116 * This is a very rudimentary implementation b/c not much table info is available.
117 */
118class HiveTablesResultSet: public HiveResultSet {
119  public:
120    HiveTablesResultSet();
121    virtual ~HiveTablesResultSet();
122    HiveReturn initialize(HiveConnection* connection, const char* tbl_search_pattern, char* err_buf,
123                          size_t err_buf_len);
124    HiveReturn fetchNext(char* err_buf, size_t err_buf_len);
125    HiveReturn hasResults(int* results, char* err_buf, size_t err_buf_len);
126    HiveReturn getColumnCount(size_t* col_count, char* err_buf, size_t err_buf_len);
127    HiveReturn createColumnDesc(size_t column_idx, HiveColumnDesc** column_desc_ptr, char* err_buf,
128                                size_t err_buf_len);
129    HiveRowSet& getRowSet();
130
131  private:
132    int m_fetch_idx; ///< Last row fetched by the client
133    /// OK to use vector<string> b/c greatly simplifies work and class not used often
134    vector<string> m_curr_row_data; ///< Vector with row data corresponding to row at m_fetch_idx
135    /// Rowset associated with the current fetched row (if any)
136    HiveStringVectorRowSet m_vecstring_rowset;
137    vector<string> m_tables; ///< Vector of table names
138    Apache::Hadoop::Hive::Schema m_schema; ///< Schema of the result table
139
140    HiveReturn initializeSchema(char* err_buf, size_t err_buf_len);
141    HiveReturn constructCurrentRow(char* err_buf, size_t err_buf_len);
142};
143
144
145/*************************************************************************************************
146 * HiveColumnsResultSet Subclass Declaration
147 ************************************************************************************************/
148
149/**
150 * @brief A container for resultsets describing the columns of table(s).
151 *
152 * Container class for a pre-scripted column info resultset (the result of a DBColumns).
153 * This class was only meant to be created by DBColumns and destroyed by DBCloseResultSet.
154 * All error messages will be written to err_buf if err_buf and err_buf_len are provided.
155 * This is a very rudimentary implementation b/c not much column info is available.
156 */
157class HiveColumnsResultSet: public HiveResultSet {
158  public:
159    /// Constructor requires a Hive-to-SQL type convert function pointer as an argument
160    HiveColumnsResultSet(int(*fpHiveToSQLType)(HiveType));
161    virtual ~HiveColumnsResultSet();
162    HiveReturn initialize(HiveConnection* connection, const char* tbl_search_pattern,
163                          const char* col_search_pattern, char* err_buf, size_t err_buf_len);
164    HiveReturn fetchNext(char* err_buf, size_t err_buf_len);
165    HiveReturn hasResults(int* results, char* err_buf, size_t err_buf_len);
166    HiveReturn getColumnCount(size_t* col_count, char* err_buf, size_t err_buf_len);
167    HiveReturn createColumnDesc(size_t column_idx, HiveColumnDesc** column_desc_ptr, char* err_buf,
168                                size_t err_buf_len);
169    HiveRowSet& getRowSet();
170
171  private:
172    HiveConnection* m_connection; ///< Hive connection handle
173    int (*m_fpHiveToSQLType)(HiveType); ///< Pointer to HiveType to SQLType convert function
174    int m_tbl_fetch_idx; ///< Last table fetched
175    int m_col_fetch_idx; ///< Last column fetched
176    vector<string> m_tables; ///< Vector of table names
177    vector<Apache::Hadoop::Hive::FieldSchema> m_columns; ///< Vector of column field schemas
178    /// OK to use vector<string> b/c greatly simplifies work and class not used often
179    vector<string> m_curr_row_data; ///< Vector with constructed row data
180    /// Rowset associated with the current constructed row (if any)
181    HiveStringVectorRowSet m_vecstring_rowset;
182    Apache::Hadoop::Hive::Schema m_schema; ///< Schema of the result table
183
184    HiveReturn getNextTableFields(char* err_buf, size_t err_buf_len);
185    HiveReturn initializeSchema(char* err_buf, size_t err_buf_len);
186    HiveReturn constructCurrentRow(char* err_buf, size_t err_buf_len);
187};
188
189
190#endif // __hive_resultset_h__