MCPcopy Create free account
hub / github.com/apache/arrow / GetSchema

Method GetSchema

cpp/src/arrow/flight/sql/example/sqlite_statement.cc:108–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106}
107
108arrow::Result<std::shared_ptr<Schema>> SqliteStatement::GetSchema() const {
109 std::vector<std::shared_ptr<Field>> fields;
110 int column_count = sqlite3_column_count(stmt_);
111 for (int i = 0; i < column_count; i++) {
112 const char* column_name = sqlite3_column_name(stmt_, i);
113
114 // SQLite does not always provide column types, especially when the statement has not
115 // been executed yet. Because of this behaviour this method tries to get the column
116 // types in two attempts:
117 // 1. Use sqlite3_column_type(), which return SQLITE_NULL if the statement has not
118 // been executed yet
119 // 2. Use sqlite3_column_decltype(), which returns correctly if given column is
120 // declared in the table.
121 // Because of this limitation, it is not possible to know the column types for some
122 // prepared statements, in this case it returns a dense_union type covering any type
123 // SQLite supports.
124 const int column_type = sqlite3_column_type(stmt_, i);
125 const char* table = sqlite3_column_table_name(stmt_, i);
126 std::shared_ptr<DataType> data_type = GetDataTypeFromSqliteType(column_type);
127 if (data_type->id() == Type::NA) {
128 // Try to retrieve column type from sqlite3_column_decltype
129 const char* column_decltype = sqlite3_column_decltype(stmt_, i);
130 if (column_decltype != NULLPTR) {
131 ARROW_ASSIGN_OR_RAISE(data_type, GetArrowType(column_decltype));
132 } else {
133 // If it cannot determine the actual column type, return a dense_union type
134 // covering any type SQLite supports.
135 data_type = GetUnknownColumnDataType();
136 }
137 }
138 ColumnMetadata column_metadata = GetColumnMetadata(column_type, table);
139
140 fields.push_back(
141 arrow::field(column_name, data_type, column_metadata.metadata_map()));
142 }
143
144 return arrow::schema(fields);
145}
146
147SqliteStatement::~SqliteStatement() { sqlite3_finalize(stmt_); }
148

Callers

nothing calls this directly

Calls 9

GetUnknownColumnDataTypeFunction · 0.85
GetColumnMetadataFunction · 0.85
push_backMethod · 0.80
GetArrowTypeFunction · 0.70
ARROW_ASSIGN_OR_RAISEFunction · 0.50
fieldFunction · 0.50
schemaFunction · 0.50
idMethod · 0.45

Tested by

no test coverage detected