You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by st...@apache.org on 2020/01/28 16:45:51 UTC
[impala] 01/02: IMPALA-9324: Correctly handle ORC UNION type in
scanner
This is an automated email from the ASF dual-hosted git repository.
stakiar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/impala.git
commit 7a9547262ab0dee9b9d3a2662bcc2fff782e5dcd
Author: stiga-huang <hu...@gmail.com>
AuthorDate: Fri Jan 24 17:18:16 2020 +0800
IMPALA-9324: Correctly handle ORC UNION type in scanner
We don't support reading UNION columns. Queries on tables containing
UNION types will fail in planning. Error message is metadata loading
error. However, scanner may need to read an ORC file with UNION types if
the table schema doesn't map to the UNION columns. Though the UNION
values won't be read, the scanner need to resolve the file schema,
including the UNION types, correctly.
In OrcSchemaResolver::BuildSchemaPath, we create a map from ORC type ids
to Impala SchemaPath representation for all types of the file. We should
deal with UNION types as well.
This patch also include some refactor to improve code readability.
Tests:
- Add tests for table schema and file schema mismatching on all complex
types.
Change-Id: I452d27b4e281eada00b62ac58af773a3479163ec
Reviewed-on: http://gerrit.cloudera.org:8080/15103
Reviewed-by: Impala Public Jenkins <im...@cloudera.com>
Tested-by: Impala Public Jenkins <im...@cloudera.com>
---
be/src/exec/hdfs-orc-scanner.cc | 13 +++-
be/src/exec/orc-metadata-utils.cc | 51 +++++++++------
be/src/exec/orc-metadata-utils.h | 11 ++--
common/thrift/generate_error_codes.py | 8 +++
.../queries/DataErrorsTest/orc-type-checks.test | 75 ++++++++++++++++++++++
tests/query_test/test_scanners.py | 2 +-
6 files changed, 132 insertions(+), 28 deletions(-)
diff --git a/be/src/exec/hdfs-orc-scanner.cc b/be/src/exec/hdfs-orc-scanner.cc
index e8e236a..333ee8d 100644
--- a/be/src/exec/hdfs-orc-scanner.cc
+++ b/be/src/exec/hdfs-orc-scanner.cc
@@ -198,7 +198,11 @@ Status HdfsOrcScanner::Open(ScannerContext* context) {
unique_ptr<orc::RowReader> tmp_row_reader =
reader_->createRowReader(row_reader_options_);
const orc::Type* root_type = &tmp_row_reader->getSelectedType();
- DCHECK_EQ(root_type->getKind(), orc::TypeKind::STRUCT);
+ if (root_type->getKind() != orc::TypeKind::STRUCT) {
+ parse_status_ = Status(TErrorCode::ORC_TYPE_NOT_ROOT_AT_STRUCT, "selected",
+ root_type->toString(), filename());
+ return parse_status_;
+ }
orc_root_reader_ = this->obj_pool_.Add(
new OrcStructReader(root_type, scan_node_->tuple_desc(), this));
} catch (std::exception& e) {
@@ -303,6 +307,7 @@ Status HdfsOrcScanner::ResolveColumns(const TupleDescriptor& tuple_desc,
const orc::Type* node = nullptr;
bool pos_field = false;
bool missing_field = false;
+ // 1. Deal with the tuple descriptor. It should map to an ORC type.
RETURN_IF_ERROR(schema_resolver_->ResolveColumn(tuple_desc.tuple_path(), &node,
&pos_field, &missing_field));
if (missing_field) {
@@ -316,7 +321,7 @@ Status HdfsOrcScanner::ResolveColumns(const TupleDescriptor& tuple_desc,
// reading its items (or subcolumn of its items). So we select the most inner
// subcolumn of the collection (get by orc::Type::getMaximumColumnId()). E.g.
// if 'node' is array<struct<c1:int,c2:int,c3:int>> and we just need the array
- // lengths. We still need to read at least one subcolumn otherwise the ORC lib
+ // lengths, we still need to read at least one subcolumn otherwise the ORC lib
// will skip the whole array column. So we select 'c3' for this case.
selected_type_ids_.push_back(node->getMaximumColumnId());
VLOG(3) << "Add ORC column " << node->getMaximumColumnId() << " for empty tuple "
@@ -324,7 +329,9 @@ Status HdfsOrcScanner::ResolveColumns(const TupleDescriptor& tuple_desc,
return Status::OK();
}
- // Each tuple can have at most one position slot.
+ // 2. Deal with slots of the tuple descriptor. Each slot should map to an ORC type,
+ // otherwise it should be the position slot. Each tuple can have at most one position
+ // slot.
SlotDescriptor* pos_slot_desc = nullptr;
for (SlotDescriptor* slot_desc : tuple_desc.slots()) {
// Skip partition columns
diff --git a/be/src/exec/orc-metadata-utils.cc b/be/src/exec/orc-metadata-utils.cc
index 2add0b5..77c11fe 100644
--- a/be/src/exec/orc-metadata-utils.cc
+++ b/be/src/exec/orc-metadata-utils.cc
@@ -23,48 +23,57 @@
namespace impala {
Status OrcSchemaResolver::BuildSchemaPaths(int num_partition_keys,
- vector<SchemaPath>* paths) {
+ vector<SchemaPath>* col_id_path_map) const {
if (root_->getKind() != orc::TypeKind::STRUCT) {
- return Status(Substitute("Corrupt ORC File '$0': root type is $1 (should be struct)",
- filename_, root_->toString()));
+ return Status(TErrorCode::ORC_TYPE_NOT_ROOT_AT_STRUCT, "file", root_->toString(),
+ filename_);
}
SchemaPath path;
- paths->push_back(path);
+ col_id_path_map->push_back(path);
int num_columns = root_->getSubtypeCount();
for (int i = 0; i < num_columns; ++i) {
path.push_back(i + num_partition_keys);
- BuildSchemaPath(*root_->getSubtype(i), &path, paths);
+ BuildSchemaPathHelper(*root_->getSubtype(i), &path, col_id_path_map);
path.pop_back();
}
return Status::OK();
}
-void OrcSchemaResolver::BuildSchemaPath(const orc::Type& node, SchemaPath* path,
- vector<SchemaPath>* paths) {
- DCHECK_EQ(paths->size(), node.getColumnId());
- paths->push_back(*path);
- if (node.getKind() == orc::TypeKind::STRUCT) {
+void OrcSchemaResolver::BuildSchemaPathHelper(const orc::Type& node, SchemaPath* path,
+ vector<SchemaPath>* col_id_path_map) {
+ DCHECK_EQ(col_id_path_map->size(), node.getColumnId()) << Substitute(
+ "Failed building map from ORC type ids to SchemaPaths. $0 are built but current "
+ "ORC type id is $1, missing ORC type with id=$0.",
+ col_id_path_map->size(), node.getColumnId());
+ // Map ORC type with id=size() to 'path'.
+ col_id_path_map->push_back(*path);
+ // Deal with subtypes recursively.
+ if (node.getKind() == orc::TypeKind::STRUCT
+ || node.getKind() == orc::TypeKind::UNION) {
+ // It's possible the file schema contains more columns than the table schema.
+ // We should deal with UNION type here in case it's not in the table schema and the
+ // query is not reading them. Otherwise the map will miss some ORC types.
int size = node.getSubtypeCount();
for (int i = 0; i < size; ++i) {
path->push_back(i);
const orc::Type* child = node.getSubtype(i);
- BuildSchemaPath(*child, path, paths);
+ BuildSchemaPathHelper(*child, path, col_id_path_map);
path->pop_back();
}
} else if (node.getKind() == orc::TypeKind::LIST) {
DCHECK_EQ(node.getSubtypeCount(), 1);
const orc::Type* child = node.getSubtype(0);
path->push_back(SchemaPathConstants::ARRAY_ITEM);
- BuildSchemaPath(*child, path, paths);
+ BuildSchemaPathHelper(*child, path, col_id_path_map);
path->pop_back();
} else if (node.getKind() == orc::TypeKind::MAP) {
DCHECK_EQ(node.getSubtypeCount(), 2);
const orc::Type* key_child = node.getSubtype(0);
const orc::Type* value_child = node.getSubtype(1);
path->push_back(SchemaPathConstants::MAP_KEY);
- BuildSchemaPath(*key_child, path, paths);
+ BuildSchemaPathHelper(*key_child, path, col_id_path_map);
(*path)[path->size() - 1] = SchemaPathConstants::MAP_VALUE;
- BuildSchemaPath(*value_child, path, paths);
+ BuildSchemaPathHelper(*value_child, path, col_id_path_map);
path->pop_back();
}
}
@@ -101,19 +110,21 @@ Status OrcSchemaResolver::ResolveColumn(const SchemaPath& col_path,
if (table_col_type->type == TYPE_ARRAY) {
DCHECK_EQ(table_col_type->children.size(), 1);
if ((*node)->getKind() != orc::TypeKind::LIST) {
- return Status(Substitute("File '$0' has an incompatible ORC schema for column "
- "'$1', Column type: $2, ORC schema:\\n$3", filename_,
- PrintSubPath(tbl_desc_, col_path, i), "array", (*node)->toString()));
+ return Status(TErrorCode::ORC_NESTED_TYPE_MISMATCH, filename_,
+ PrintSubPath(tbl_desc_, col_path, i), "array", (*node)->toString());
}
} else if (table_col_type->type == TYPE_MAP) {
DCHECK_EQ(table_col_type->children.size(), 2);
if ((*node)->getKind() != orc::TypeKind::MAP) {
- return Status(Substitute("File '$0' has an incompatible ORC schema for column "
- "'$1', Column type: $2, ORC schema:\\n$3", filename_,
- PrintSubPath(tbl_desc_, col_path, i), "map", (*node)->toString()));
+ return Status(TErrorCode::ORC_NESTED_TYPE_MISMATCH, filename_,
+ PrintSubPath(tbl_desc_, col_path, i), "map", (*node)->toString());
}
} else if (table_col_type->type == TYPE_STRUCT) {
DCHECK_GT(table_col_type->children.size(), 0);
+ if ((*node)->getKind() != orc::TypeKind::STRUCT) {
+ return Status(TErrorCode::ORC_NESTED_TYPE_MISMATCH, filename_,
+ PrintSubPath(tbl_desc_, col_path, i), "struct", (*node)->toString());
+ }
} else {
DCHECK(!table_col_type->IsComplexType());
DCHECK_EQ(i, col_path.size() - 1);
diff --git a/be/src/exec/orc-metadata-utils.h b/be/src/exec/orc-metadata-utils.h
index d93ae6f..eaab266 100644
--- a/be/src/exec/orc-metadata-utils.h
+++ b/be/src/exec/orc-metadata-utils.h
@@ -38,8 +38,9 @@ class OrcSchemaResolver {
bool* pos_field, bool* missing_field) const;
/// Build a map from each orc::Type id to a SchemaPath. The map will be used in
- /// creating OrcColumnReaders.
- Status BuildSchemaPaths(int num_partition_keys, std::vector<SchemaPath>* paths);
+ /// creating OrcColumnReaders. It contains all ORC types including unmaterialized ones.
+ Status BuildSchemaPaths(int num_partition_keys,
+ std::vector<SchemaPath>* col_id_path_map) const;
private:
const HdfsTableDescriptor& tbl_desc_;
@@ -50,7 +51,9 @@ class OrcSchemaResolver {
Status ValidateType(const ColumnType& type, const orc::Type& orc_type) const
WARN_UNUSED_RESULT;
- static void BuildSchemaPath(const orc::Type& node, SchemaPath* path,
- std::vector<SchemaPath>* paths);
+ /// Helper function to build 'col_id_path_map' start from 'node' whose corresponding
+ /// SchemaPath is 'path'.
+ static void BuildSchemaPathHelper(const orc::Type& node, SchemaPath* path,
+ std::vector<SchemaPath>* col_id_path_map);
};
}
diff --git a/common/thrift/generate_error_codes.py b/common/thrift/generate_error_codes.py
index 43ccf21..5c774ce 100755
--- a/common/thrift/generate_error_codes.py
+++ b/common/thrift/generate_error_codes.py
@@ -446,6 +446,14 @@ error_codes = (
("ORC_DATE_OUT_OF_RANGE", 146,
"ORC file '$0' column '$1' contains an out of range date. "
"The valid date range is 0001-01-01..9999-12-31."),
+
+ ("ORC_NESTED_TYPE_MISMATCH", 147,
+ "File '$0' has an incompatible ORC schema for column '$1', "
+ "Column type: $2, ORC schema: $3"),
+
+ ("ORC_TYPE_NOT_ROOT_AT_STRUCT", 148,
+ "Root of the $0 type returned by the ORC lib is not STRUCT: $1. "
+ "Either there are bugs in the ORC lib or ORC file '$2' is corrupt."),
)
import sys
diff --git a/testdata/workloads/functional-query/queries/DataErrorsTest/orc-type-checks.test b/testdata/workloads/functional-query/queries/DataErrorsTest/orc-type-checks.test
index 83a1540..253253c 100644
--- a/testdata/workloads/functional-query/queries/DataErrorsTest/orc-type-checks.test
+++ b/testdata/workloads/functional-query/queries/DataErrorsTest/orc-type-checks.test
@@ -135,3 +135,78 @@ decimal
1
1
====
+---- HIVE_QUERY
+# Create a table with UNION, ARRAY, MAP types by Hive and insert values into it.
+create external table $DATABASE.union_complextypes(
+ id int,
+ u uniontype<int, boolean>,
+ int_array array<int>,
+ int_map map<string, int>)
+stored as orc;
+insert into $DATABASE.union_complextypes
+ select 0, create_union(1, 50, false), array(0), map('key0', 0);
+# Create an external table on the ORC files with different schema.
+create external table $DATABASE.ill_complextypes(
+ id int,
+ u map<int, double>,
+ int_map map<string, int>,
+ int_array array<int>)
+stored as orc
+location '$NAMENODE/test-warehouse/$DATABASE.db/union_complextypes';
+====
+---- QUERY
+invalidate metadata ill_complextypes;
+# Should be able to read the first column that match the file schema.
+select id from ill_complextypes
+---- TYPES
+INT
+---- RESULTS
+0
+====
+---- QUERY
+select u.key, u.value from ill_complextypes.u
+---- CATCH
+File '$NAMENODE/test-warehouse/$DATABASE.db/union_complextypes/000000_0'
+ has an incompatible ORC schema for column '$DATABASE.ill_complextypes.u',
+ Column type: map, ORC schema: uniontype<int,boolean>
+====
+---- QUERY
+select m.key, m.value from ill_complextypes.int_map m
+---- CATCH
+File '$NAMENODE/test-warehouse/$DATABASE.db/union_complextypes/000000_0'
+ has an incompatible ORC schema for column '$DATABASE.ill_complextypes.int_map',
+ Column type: map, ORC schema: array<int>
+====
+---- QUERY
+select a.item from ill_complextypes.int_array a
+---- CATCH
+File '$NAMENODE/test-warehouse/$DATABASE.db/union_complextypes/000000_0'
+ has an incompatible ORC schema for column '$DATABASE.ill_complextypes.int_array',
+ Column type: array, ORC schema: map<string,int>
+====
+---- QUERY
+# Create a table whose nested types don't match the underlying ORC files.
+# Reuse the data files of functional_orc_def.complextypestbl.
+create external table ill_complextypes2 (id bigint,
+ int_array map<string, int>,
+ int_array_array array<struct<x:int>>,
+ int_map array<struct<k:string,v:int>>,
+ int_map_array array<array<struct<k:string,v:int>>>
+) stored as orc location '$NAMENODE/test-warehouse/complextypestbl_orc_def';
+select a.item.x from ill_complextypes2.int_array_array a
+---- CATCH
+has an incompatible ORC schema for column '$DATABASE.ill_complextypes2.int_array_array.item',
+ Column type: struct, ORC schema: array<int>
+====
+---- QUERY
+select a.item.k, a.item.v from ill_complextypes2.int_map a
+---- CATCH
+has an incompatible ORC schema for column '$DATABASE.ill_complextypes2.int_map',
+ Column type: array, ORC schema: map<string,int>
+====
+---- QUERY
+select inner_arr.item.k, inner_arr.item.v from ill_complextypes2.int_map_array a, a.item inner_arr
+---- CATCH
+has an incompatible ORC schema for column '$DATABASE.ill_complextypes2.int_map_array.item',
+ Column type: array, ORC schema: map<string,int>
+====
diff --git a/tests/query_test/test_scanners.py b/tests/query_test/test_scanners.py
index 5f41f08..9721538 100644
--- a/tests/query_test/test_scanners.py
+++ b/tests/query_test/test_scanners.py
@@ -1365,7 +1365,7 @@ class TestOrc(ImpalaTestSuite):
self._run_invalid_schema_test(unique_database, "corrupt_schema",
"Encountered parse error during schema selection")
self._run_invalid_schema_test(unique_database, "corrupt_root_type",
- "root type is boolean (should be struct)")
+ "Root of the file type returned by the ORC lib is not STRUCT: boolean.")
def test_date_out_of_range_orc(self, vector, unique_database):