diff --git a/.gitignore b/.gitignore index a4b8eb0..88d0b67 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Cargo.lock venv *.orc +*.jsn.gz diff --git a/Cargo.toml b/Cargo.toml index 9b4aaf0..4e7f797 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ edition = "2021" prost = { version = "0.9.0" } flate2 = "1" fallible-streaming-iterator = { version = "0.1" } + +[dev-dependencies] +json = "0.12" diff --git a/tests/examples/LICENSE b/tests/examples/LICENSE new file mode 100644 index 0000000..629cfda --- /dev/null +++ b/tests/examples/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability contains + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/tests/examples/README b/tests/examples/README new file mode 100644 index 0000000..a619a29 --- /dev/null +++ b/tests/examples/README @@ -0,0 +1 @@ +Files in this directory are imported from https://github.com/apache/orc/tree/main/examples diff --git a/tests/examples/TestCSVFileImport.test10rows.csv b/tests/examples/TestCSVFileImport.test10rows.csv new file mode 100644 index 0000000..5404dac --- /dev/null +++ b/tests/examples/TestCSVFileImport.test10rows.csv @@ -0,0 +1,10 @@ +0,a,0.0 +1,b,1.1 +2,c,2.2 +3,d, +4,,4.4 +,f,5.5 +,, +7,h,7.7 +8,i,8.8 +9,j,9.9 \ No newline at end of file diff --git a/tests/examples/TestCSVFileImport.testTimezoneOption.csv b/tests/examples/TestCSVFileImport.testTimezoneOption.csv new file mode 100644 index 0000000..9e87f23 --- /dev/null +++ b/tests/examples/TestCSVFileImport.testTimezoneOption.csv @@ -0,0 +1 @@ +2021-12-27 00:00:00.000 \ No newline at end of file diff --git a/tests/examples/encrypted/kms.keystore b/tests/examples/encrypted/kms.keystore new file mode 100644 index 0000000..cd9152b Binary files /dev/null and b/tests/examples/encrypted/kms.keystore differ diff --git a/tests/examples/expected/TestOrcFile.columnProjection.jsn.gz b/tests/examples/expected/TestOrcFile.columnProjection.jsn.gz new file mode 100644 index 0000000..ab85152 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.columnProjection.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.emptyFile.jsn.gz b/tests/examples/expected/TestOrcFile.emptyFile.jsn.gz new file mode 100644 index 0000000..91c85cd Binary files /dev/null and b/tests/examples/expected/TestOrcFile.emptyFile.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.metaData.jsn.gz b/tests/examples/expected/TestOrcFile.metaData.jsn.gz new file mode 100644 index 0000000..da9dffb Binary files /dev/null and b/tests/examples/expected/TestOrcFile.metaData.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.test1.jsn.gz b/tests/examples/expected/TestOrcFile.test1.jsn.gz new file mode 100644 index 0000000..5eab19a Binary files /dev/null and b/tests/examples/expected/TestOrcFile.test1.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testDate1900.jsn.gz b/tests/examples/expected/TestOrcFile.testDate1900.jsn.gz new file mode 100644 index 0000000..62dbaba Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testDate1900.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testDate2038.jsn.gz b/tests/examples/expected/TestOrcFile.testDate2038.jsn.gz new file mode 100644 index 0000000..8a84655 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testDate2038.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testMemoryManagementV11.jsn.gz b/tests/examples/expected/TestOrcFile.testMemoryManagementV11.jsn.gz new file mode 100644 index 0000000..591e3c7 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testMemoryManagementV11.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testMemoryManagementV12.jsn.gz b/tests/examples/expected/TestOrcFile.testMemoryManagementV12.jsn.gz new file mode 100644 index 0000000..14ece1d Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testMemoryManagementV12.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testPredicatePushdown.jsn.gz b/tests/examples/expected/TestOrcFile.testPredicatePushdown.jsn.gz new file mode 100644 index 0000000..cf80773 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testPredicatePushdown.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testSeek.jsn.gz b/tests/examples/expected/TestOrcFile.testSeek.jsn.gz new file mode 100644 index 0000000..d6afadd Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testSeek.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testSnappy.jsn.gz b/tests/examples/expected/TestOrcFile.testSnappy.jsn.gz new file mode 100644 index 0000000..1bd2ae9 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testSnappy.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testStringAndBinaryStatistics.jsn.gz b/tests/examples/expected/TestOrcFile.testStringAndBinaryStatistics.jsn.gz new file mode 100644 index 0000000..76d962e Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testStringAndBinaryStatistics.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testStripeLevelStats.jsn.gz b/tests/examples/expected/TestOrcFile.testStripeLevelStats.jsn.gz new file mode 100644 index 0000000..f543713 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testStripeLevelStats.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testTimestamp.jsn.gz b/tests/examples/expected/TestOrcFile.testTimestamp.jsn.gz new file mode 100644 index 0000000..375c9a0 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testTimestamp.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testUnionAndTimestamp.jsn.gz b/tests/examples/expected/TestOrcFile.testUnionAndTimestamp.jsn.gz new file mode 100644 index 0000000..bd0c5a6 Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testUnionAndTimestamp.jsn.gz differ diff --git a/tests/examples/expected/TestOrcFile.testWithoutIndex.jsn.gz b/tests/examples/expected/TestOrcFile.testWithoutIndex.jsn.gz new file mode 100644 index 0000000..f2128ca Binary files /dev/null and b/tests/examples/expected/TestOrcFile.testWithoutIndex.jsn.gz differ diff --git a/tests/examples/expected/TestStringDictionary.testRowIndex.jsn.gz b/tests/examples/expected/TestStringDictionary.testRowIndex.jsn.gz new file mode 100644 index 0000000..5fb46b6 Binary files /dev/null and b/tests/examples/expected/TestStringDictionary.testRowIndex.jsn.gz differ diff --git a/tests/examples/expected/TestVectorOrcFile.testLz4.jsn.gz b/tests/examples/expected/TestVectorOrcFile.testLz4.jsn.gz new file mode 100644 index 0000000..60a846e Binary files /dev/null and b/tests/examples/expected/TestVectorOrcFile.testLz4.jsn.gz differ diff --git a/tests/examples/expected/TestVectorOrcFile.testLzo.jsn.gz b/tests/examples/expected/TestVectorOrcFile.testLzo.jsn.gz new file mode 100644 index 0000000..e002379 Binary files /dev/null and b/tests/examples/expected/TestVectorOrcFile.testLzo.jsn.gz differ diff --git a/tests/examples/expected/decimal.jsn.gz b/tests/examples/expected/decimal.jsn.gz new file mode 100644 index 0000000..e634bd7 Binary files /dev/null and b/tests/examples/expected/decimal.jsn.gz differ diff --git a/tests/examples/expected/demo-12-zlib.jsn.gz b/tests/examples/expected/demo-12-zlib.jsn.gz new file mode 100644 index 0000000..ab53284 Binary files /dev/null and b/tests/examples/expected/demo-12-zlib.jsn.gz differ diff --git a/tests/examples/expected/nulls-at-end-snappy.jsn.gz b/tests/examples/expected/nulls-at-end-snappy.jsn.gz new file mode 100644 index 0000000..a51a95b Binary files /dev/null and b/tests/examples/expected/nulls-at-end-snappy.jsn.gz differ diff --git a/tests/examples/expected/orc-file-11-format.jsn.gz b/tests/examples/expected/orc-file-11-format.jsn.gz new file mode 100644 index 0000000..819f4a2 Binary files /dev/null and b/tests/examples/expected/orc-file-11-format.jsn.gz differ diff --git a/tests/examples/expected/orc_index_int_string.jsn.gz b/tests/examples/expected/orc_index_int_string.jsn.gz new file mode 100644 index 0000000..4e6de3d Binary files /dev/null and b/tests/examples/expected/orc_index_int_string.jsn.gz differ diff --git a/tests/examples/expected/orc_split_elim.jsn.gz b/tests/examples/expected/orc_split_elim.jsn.gz new file mode 100644 index 0000000..5263699 Binary files /dev/null and b/tests/examples/expected/orc_split_elim.jsn.gz differ diff --git a/tests/examples/expected/orc_split_elim_cpp.jsn.gz b/tests/examples/expected/orc_split_elim_cpp.jsn.gz new file mode 100644 index 0000000..5263699 Binary files /dev/null and b/tests/examples/expected/orc_split_elim_cpp.jsn.gz differ diff --git a/tests/examples/expected/orc_split_elim_new.jsn.gz b/tests/examples/expected/orc_split_elim_new.jsn.gz new file mode 100644 index 0000000..5263699 Binary files /dev/null and b/tests/examples/expected/orc_split_elim_new.jsn.gz differ diff --git a/tests/examples/expected/over1k_bloom.jsn.gz b/tests/examples/expected/over1k_bloom.jsn.gz new file mode 100644 index 0000000..b159f0c Binary files /dev/null and b/tests/examples/expected/over1k_bloom.jsn.gz differ diff --git a/tests/it/compare_json.rs b/tests/it/compare_json.rs new file mode 100644 index 0000000..10b2485 --- /dev/null +++ b/tests/it/compare_json.rs @@ -0,0 +1,240 @@ +use std::fmt::Debug; +use std::io::{Cursor, Read}; + +use json::{number::Number, JsonValue}; +use orc_format::{ + proto::r#type::Kind, proto::StripeFooter, read::read_metadata, read::read_stripe_column, + read::read_stripe_footer, read::FileMetadata, +}; + +use super::deserialize::*; + +macro_rules! test_apache( + ($test_name:ident, $file_name: tt) => { + #[test] + #[allow(non_snake_case)] + fn $test_name() { + let orc_data = include_bytes!(concat!("../examples/", $file_name, ".orc")); + let jsn_gz_data: &[u8] = include_bytes!(concat!("../examples/expected/", $file_name, ".jsn.gz")); + inner_test(orc_data, jsn_gz_data); + } + }; + ($name_prefix:ident . $name:ident) => { + #[test] + #[allow(non_snake_case)] + fn $name() { + let orc_data = include_bytes!(concat!("../examples/", stringify!($name_prefix), ".", stringify!($name), ".orc")); + let jsn_gz_data: &[u8] = include_bytes!(concat!("../examples/expected/", stringify!($name_prefix), ".", stringify!($name), ".jsn.gz")); + inner_test(orc_data, jsn_gz_data); + } + }; +); + +macro_rules! test_local( + ($name:ident) => { + #[test] + #[allow(non_snake_case)] + fn $name() { + let orc_data = include_bytes!(concat!("../../", stringify!($name), ".orc")); + let jsn_gz_data: &[u8] = include_bytes!(concat!("../../", stringify!($name), ".jsn.gz")); + inner_test(orc_data, jsn_gz_data); + } + }; +); + +fn inner_test(orc_data: &[u8], jsn_gz_data: &[u8]) { + let mut expected_jsn_bytes = Vec::new(); + let mut decoder = flate2::read::GzDecoder::new(jsn_gz_data); + decoder + .read_to_end(&mut expected_jsn_bytes) + .expect("Could not gunzip .jsn.gz file"); + let expected_jsn = + String::from_utf8(expected_jsn_bytes).expect("Invalid UTF-8 in .jsn.gz file"); + assert_eq!(orc_to_jsn(orc_data), expected_jsn); +} + +fn unwrap_deserialization( + res: Result<(Vec, Vec), E>, + error_msg: &str, +) -> Vec> { + let (validities, valid_values) = res.expect(error_msg); + let mut valid_values_iter = valid_values.into_iter(); + let mut all_values = Vec::new(); + for validity in validities { + all_values.push( + if validity { + Some(valid_values_iter.next().expect("Column too short")) + } + else { + None + } + ); + } + assert!(valid_values_iter.next().is_none()); + all_values +} + +fn orc_to_jsn(orc_data: &[u8]) -> String { + let mut orc_file = Cursor::new(orc_data); + let metadata = read_metadata(&mut orc_file).expect("Failed to unwrap metadata"); + + let stripe = 0; + let stripe_footer = read_stripe_footer(&mut orc_file, &metadata, stripe, &mut vec![]) + .expect(&format!("Could not reater footer of stripe {}", stripe)); + + let data = read_column( + &mut orc_file, + &metadata, + stripe, + &stripe_footer, + 0, + 0, + "", + ); + + let mut lines = data.iter() + .map(JsonValue::dump) + .collect::>(); + lines.push(String::new()); + lines.join("\n") +} + +fn read_column( + orc_file: &mut Cursor<&[u8]>, + metadata: &FileMetadata, + stripe: usize, + stripe_footer: &StripeFooter, + type_id: u32, + column_offset: u32, + column_name: &str, +) -> Vec { + let type_ = metadata + .footer + .types + .get(type_id as usize) + .expect(&format!("missing type {}", type_id)); + + match type_.kind() { + Kind::Struct => { + let mut columns = Vec::new(); + for (relative_column_index, &subtype_id) in type_.subtypes.iter().enumerate() { + columns.push(read_column( + orc_file, + metadata, + stripe, + stripe_footer, + subtype_id, + (relative_column_index as u32) + column_offset + 1, + type_ + .field_names + .get(type_id as usize) + .unwrap_or(&"".to_owned()), + )) + } + + let mut lines = Vec::new(); + 'outer: loop { + let mut row = Vec::new(); + for (column_index, column) in columns.iter_mut().enumerate() { + match column.pop() { + Some(cell) => row.push(cell), + None => { + if column_index == 0 { + break 'outer; + } else { + panic!( + "Column {} is shorter than column {}", + (column_index as u32) + column_offset, + column_offset + ); + } + } + } + } + lines.push(JsonValue::Array(row)); + } + lines.reverse(); + lines + } + _ => { + let column = read_stripe_column( + orc_file, + &metadata, + stripe, + stripe_footer.clone(), + column_offset, + vec![], + ) + .expect(&format!( + "Could not read stripe {} column {} ({})", + stripe, column_offset, column_name, + )); + + let error_msg = format!( + "Could not deserialize stripe {} column {} ({}) as kind {:?}", + stripe, + column_offset, + column_name, + type_.kind() + ); + match type_.kind() { + Kind::Struct => unreachable!("Kind::Struct"), + Kind::Boolean => { + unwrap_deserialization(deserialize_bool_array(&column), &error_msg) + .into_iter() + .map(|v| match v { + Some(b) => JsonValue::Boolean(b), + None => JsonValue::Null, + }) + .collect() + } + Kind::Byte | Kind::Short | Kind::Int | Kind::Long => { + unwrap_deserialization(deserialize_int_array(&column), &error_msg) + .into_iter() + .map(|v| match v { + Some(n) => JsonValue::Number(Number::from(n)), + None => JsonValue::Null, + }) + .collect() + } + Kind::Float => { + unwrap_deserialization(deserialize_f32_array(&column), &error_msg) + .into_iter() + .map(|v| match v { + Some(n) => JsonValue::Number(Number::from(n)), + None => JsonValue::Null, + }) + .collect() + } + Kind::String => { + unwrap_deserialization(deserialize_str_array(&column), &error_msg) + .into_iter() + .map(|v| match v { + Some(n) => JsonValue::String(n), + None => JsonValue::Null, + }) + .collect() + } + _ => panic!("{:?} not implemented", type_.kind()), + } + } + } +} + +test_apache!(TestOrcFile.metaData); +test_apache!(TestOrcFile.test1); +test_apache!(TestOrcFile.testSeek); +test_apache!(TestOrcFile.testTimestamp); +test_apache!(orc_file_11_format, "orc-file-11-format"); + +test_local!(long_bool); +test_local!(long_bool_gzip); +test_local!(string_long); +test_local!(string_dict); +test_local!(string_dict_gzip); +test_local!(string_long_long); +test_local!(string_long_long_gzip); + +// These two fail because Rust's json uses exponent notation but Python's does not. +// test_local!(test); +// test_local!(f32_long_long_gzip); diff --git a/tests/it/deserialize.rs b/tests/it/deserialize.rs index 8016ab7..95442b9 100644 --- a/tests/it/deserialize.rs +++ b/tests/it/deserialize.rs @@ -11,17 +11,21 @@ use orc_format::{ }; fn deserialize_validity(column: &Column, scratch: &mut Vec) -> Result, Error> { - let mut reader = column.get_stream(Kind::Present, std::mem::take(scratch))?; - - let mut validity = Vec::with_capacity(column.number_of_rows()); - BooleanIter::new(&mut reader, column.number_of_rows()).try_for_each(|item| { - validity.push(item?); - Result::<(), Error>::Ok(()) - })?; - - *scratch = std::mem::take(&mut reader.into_inner()); - - Ok(validity) + match column.get_stream(Kind::Present, std::mem::take(scratch)) { + Ok(mut reader) => { + let mut validity = Vec::with_capacity(column.number_of_rows()); + BooleanIter::new(&mut reader, column.number_of_rows()).try_for_each(|item| { + validity.push(item?); + Result::<(), Error>::Ok(()) + })?; + + *scratch = std::mem::take(&mut reader.into_inner()); + + Ok(validity) + }, + Err(Error::InvalidKind(_, _)) => Ok(vec![true; column.number_of_rows()]), + Err(e) => Err(e), + } } pub fn deserialize_f32_array(column: &Column) -> Result<(Vec, Vec), Error> { diff --git a/tests/it/main.rs b/tests/it/main.rs index 755561d..dbcb25b 100644 --- a/tests/it/main.rs +++ b/tests/it/main.rs @@ -3,6 +3,8 @@ use std::fs::File; mod deserialize; use deserialize::*; +mod compare_json; + use orc_format::{error::Error, read, read::Column}; fn get_column(path: &str, column: u32) -> Result { diff --git a/write.py b/write.py index ec55786..9514cc1 100644 --- a/write.py +++ b/write.py @@ -1,3 +1,5 @@ +import gzip +import json import random import pyorc @@ -64,9 +66,11 @@ def _write( compression=compression, ) num_rows = len(list(data.values())[0]) - for x in range(num_rows): - row = tuple(values[x] for values in data.values()) - writer.write(row) + with gzip.open(file_name.replace(".orc", ".jsn.gz"), "wt") as f: + for x in range(num_rows): + row = tuple(values[x] for values in data.values()) + writer.write(row) + f.write(json.dumps(row) + "\n") writer.close() with open(file_name, "rb") as f: