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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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.
//! Parquet schema parser.
//! Provides methods to parse and validate string message type into Parquet
//! [`Type`].
//!
//! # Example
//!
//! ```rust
//! use parquet::schema::parser::parse_message_type;
//!
//! let message_type = "
//! message spark_schema {
//! OPTIONAL BYTE_ARRAY a (UTF8);
//! REQUIRED INT32 b;
//! REQUIRED DOUBLE c;
//! REQUIRED BOOLEAN d;
//! OPTIONAL group e (LIST) {
//! REPEATED group list {
//! REQUIRED INT32 element;
//! }
//! }
//! }
//! ";
//!
//! let schema = parse_message_type(message_type).expect("Expected valid schema");
//! println!("{:?}", schema);
//! ```
use std::sync::Arc;
use crate::basic::{ConvertedType, LogicalType, Repetition, TimeUnit, Type as PhysicalType};
use crate::errors::{ParquetError, Result};
use crate::schema::types::{Type, TypePtr};
/// Parses message type as string into a Parquet [`Type`]
/// which, for example, could be used to extract individual columns. Returns Parquet
/// general error when parsing or validation fails.
pub fn parse_message_type(message_type: &str) -> Result<Type> {
let mut parser = Parser {
tokenizer: &mut Tokenizer::from_str(message_type),
};
parser.parse_message_type()
}
/// Tokenizer to split message type string into tokens that are separated using characters
/// defined in `is_schema_delim` method. Tokenizer also preserves delimiters as tokens.
/// Tokenizer provides Iterator interface to process tokens; it also allows to step back
/// to reprocess previous tokens.
struct Tokenizer<'a> {
// List of all tokens for a string
tokens: Vec<&'a str>,
// Current index of vector
index: usize,
}
impl<'a> Tokenizer<'a> {
// Create tokenizer from message type string
pub fn from_str(string: &'a str) -> Self {
let vec = string
.split_whitespace()
.flat_map(Self::split_token)
.collect();
Tokenizer {
tokens: vec,
index: 0,
}
}
// List of all special characters in schema
fn is_schema_delim(c: char) -> bool {
c == ';' || c == '{' || c == '}' || c == '(' || c == ')' || c == '=' || c == ','
}
/// Splits string into tokens; input string can already be token or can contain
/// delimiters, e.g. required" -> Vec("required") and
/// "(UTF8);" -> Vec("(", "UTF8", ")", ";")
fn split_token(string: &str) -> Vec<&str> {
let mut buffer: Vec<&str> = Vec::new();
let mut tail = string;
while let Some(index) = tail.find(Self::is_schema_delim) {
let (h, t) = tail.split_at(index);
if !h.is_empty() {
buffer.push(h);
}
buffer.push(&t[0..1]);
tail = &t[1..];
}
if !tail.is_empty() {
buffer.push(tail);
}
buffer
}
// Move pointer to a previous element
fn backtrack(&mut self) {
self.index -= 1;
}
}
impl<'a> Iterator for Tokenizer<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if self.index < self.tokens.len() {
self.index += 1;
Some(self.tokens[self.index - 1])
} else {
None
}
}
}
/// Internal Schema parser.
/// Traverses message type using tokenizer and parses each group/primitive type
/// recursively.
struct Parser<'a> {
tokenizer: &'a mut Tokenizer<'a>,
}
// Utility function to assert token on validity.
fn assert_token(token: Option<&str>, expected: &str) -> Result<()> {
match token {
Some(value) if value == expected => Ok(()),
Some(other) => Err(general_err!(
"Expected '{}', found token '{}'",
expected,
other
)),
None => Err(general_err!(
"Expected '{}', but no token found (None)",
expected
)),
}
}
// Utility function to parse i32 or return general error.
#[inline]
fn parse_i32(value: Option<&str>, not_found_msg: &str, parse_fail_msg: &str) -> Result<i32> {
value
.ok_or_else(|| general_err!(not_found_msg))
.and_then(|v| v.parse::<i32>().map_err(|_| general_err!(parse_fail_msg)))
}
// Utility function to parse boolean or return general error.
#[inline]
fn parse_bool(value: Option<&str>, not_found_msg: &str, parse_fail_msg: &str) -> Result<bool> {
value
.ok_or_else(|| general_err!(not_found_msg))
.and_then(|v| {
v.to_lowercase()
.parse::<bool>()
.map_err(|_| general_err!(parse_fail_msg))
})
}
// Utility function to parse TimeUnit or return general error.
fn parse_timeunit(
value: Option<&str>,
not_found_msg: &str,
parse_fail_msg: &str,
) -> Result<TimeUnit> {
value
.ok_or_else(|| general_err!(not_found_msg))
.and_then(|v| match v.to_uppercase().as_str() {
"MILLIS" => Ok(TimeUnit::MILLIS(Default::default())),
"MICROS" => Ok(TimeUnit::MICROS(Default::default())),
"NANOS" => Ok(TimeUnit::NANOS(Default::default())),
_ => Err(general_err!(parse_fail_msg)),
})
}
impl Parser<'_> {
// Entry function to parse message type, uses internal tokenizer.
fn parse_message_type(&mut self) -> Result<Type> {
// Check that message type starts with "message".
match self.tokenizer.next() {
Some("message") => {
let name = self
.tokenizer
.next()
.ok_or_else(|| general_err!("Expected name, found None"))?;
Type::group_type_builder(name)
.with_fields(self.parse_child_types()?)
.build()
}
_ => Err(general_err!("Message type does not start with 'message'")),
}
}
// Parses child types for a current group type.
// This is only invoked on root and group types.
fn parse_child_types(&mut self) -> Result<Vec<TypePtr>> {
assert_token(self.tokenizer.next(), "{")?;
let mut vec = Vec::new();
while let Some(value) = self.tokenizer.next() {
if value == "}" {
break;
} else {
self.tokenizer.backtrack();
vec.push(Arc::new(self.add_type()?));
}
}
Ok(vec)
}
fn add_type(&mut self) -> Result<Type> {
// Parse repetition
let repetition = self
.tokenizer
.next()
.ok_or_else(|| general_err!("Expected repetition, found None"))
.and_then(|v| v.to_uppercase().parse::<Repetition>())?;
match self.tokenizer.next() {
Some(group) if group.to_uppercase() == "GROUP" => self.add_group_type(Some(repetition)),
Some(type_string) => {
let physical_type = type_string.to_uppercase().parse::<PhysicalType>()?;
self.add_primitive_type(repetition, physical_type)
}
None => Err(general_err!("Invalid type, could not extract next token")),
}
}
fn add_group_type(&mut self, repetition: Option<Repetition>) -> Result<Type> {
// Parse name of the group type
let name = self
.tokenizer
.next()
.ok_or_else(|| general_err!("Expected name, found None"))?;
// Parse logical or converted type if exists
let (logical_type, converted_type) = if let Some("(") = self.tokenizer.next() {
let tpe = self
.tokenizer
.next()
.ok_or_else(|| general_err!("Expected converted type, found None"))
.and_then(|v| {
// Try logical type first
let upper = v.to_uppercase();
let logical = upper.parse::<LogicalType>();
match logical {
Ok(logical) => {
Ok((Some(logical.clone()), ConvertedType::from(Some(logical))))
}
Err(_) => Ok((None, upper.parse::<ConvertedType>()?)),
}
})?;
assert_token(self.tokenizer.next(), ")")?;
tpe
} else {
self.tokenizer.backtrack();
(None, ConvertedType::NONE)
};
// Parse optional id
let id = if let Some("=") = self.tokenizer.next() {
self.tokenizer.next().and_then(|v| v.parse::<i32>().ok())
} else {
self.tokenizer.backtrack();
None
};
let mut builder = Type::group_type_builder(name)
.with_logical_type(logical_type)
.with_converted_type(converted_type)
.with_fields(self.parse_child_types()?)
.with_id(id);
if let Some(rep) = repetition {
builder = builder.with_repetition(rep);
}
builder.build()
}
fn add_primitive_type(
&mut self,
repetition: Repetition,
physical_type: PhysicalType,
) -> Result<Type> {
// Read type length if the type is FIXED_LEN_BYTE_ARRAY.
let mut length: i32 = -1;
if physical_type == PhysicalType::FIXED_LEN_BYTE_ARRAY {
assert_token(self.tokenizer.next(), "(")?;
length = parse_i32(
self.tokenizer.next(),
"Expected length for FIXED_LEN_BYTE_ARRAY, found None",
"Failed to parse length for FIXED_LEN_BYTE_ARRAY",
)?;
assert_token(self.tokenizer.next(), ")")?;
}
// Parse name of the primitive type
let name = self
.tokenizer
.next()
.ok_or_else(|| general_err!("Expected name, found None"))?;
// Parse converted type
let (logical_type, converted_type, precision, scale) =
if let Some("(") = self.tokenizer.next() {
let (mut logical, mut converted) = self
.tokenizer
.next()
.ok_or_else(|| general_err!("Expected logical or converted type, found None"))
.and_then(|v| {
let upper = v.to_uppercase();
let logical = upper.parse::<LogicalType>();
match logical {
Ok(logical) => {
Ok((Some(logical.clone()), ConvertedType::from(Some(logical))))
}
Err(_) => Ok((None, upper.parse::<ConvertedType>()?)),
}
})?;
// Parse precision and scale for decimals
let mut precision: i32 = -1;
let mut scale: i32 = -1;
// Parse the concrete logical type
if let Some(tpe) = &logical {
match tpe {
LogicalType::Decimal { .. } => {
if let Some("(") = self.tokenizer.next() {
precision = parse_i32(
self.tokenizer.next(),
"Expected precision, found None",
"Failed to parse precision for DECIMAL type",
)?;
if let Some(",") = self.tokenizer.next() {
scale = parse_i32(
self.tokenizer.next(),
"Expected scale, found None",
"Failed to parse scale for DECIMAL type",
)?;
assert_token(self.tokenizer.next(), ")")?;
} else {
scale = 0
}
logical = Some(LogicalType::Decimal { scale, precision });
converted = ConvertedType::from(logical.clone());
}
}
LogicalType::Time { .. } => {
if let Some("(") = self.tokenizer.next() {
let unit = parse_timeunit(
self.tokenizer.next(),
"Invalid timeunit found",
"Failed to parse timeunit for TIME type",
)?;
if let Some(",") = self.tokenizer.next() {
let is_adjusted_to_u_t_c = parse_bool(
self.tokenizer.next(),
"Invalid boolean found",
"Failed to parse timezone info for TIME type",
)?;
assert_token(self.tokenizer.next(), ")")?;
logical = Some(LogicalType::Time {
is_adjusted_to_u_t_c,
unit,
});
converted = ConvertedType::from(logical.clone());
} else {
// Invalid token for unit
self.tokenizer.backtrack();
}
}
}
LogicalType::Timestamp { .. } => {
if let Some("(") = self.tokenizer.next() {
let unit = parse_timeunit(
self.tokenizer.next(),
"Invalid timeunit found",
"Failed to parse timeunit for TIMESTAMP type",
)?;
if let Some(",") = self.tokenizer.next() {
let is_adjusted_to_u_t_c = parse_bool(
self.tokenizer.next(),
"Invalid boolean found",
"Failed to parse timezone info for TIMESTAMP type",
)?;
assert_token(self.tokenizer.next(), ")")?;
logical = Some(LogicalType::Timestamp {
is_adjusted_to_u_t_c,
unit,
});
converted = ConvertedType::from(logical.clone());
} else {
// Invalid token for unit
self.tokenizer.backtrack();
}
}
}
LogicalType::Integer { .. } => {
if let Some("(") = self.tokenizer.next() {
let bit_width = parse_i32(
self.tokenizer.next(),
"Invalid bit_width found",
"Failed to parse bit_width for INTEGER type",
)? as i8;
match physical_type {
PhysicalType::INT32 => match bit_width {
8 | 16 | 32 => {}
_ => {
return Err(general_err!(
"Incorrect bit width {} for INT32",
bit_width
))
}
},
PhysicalType::INT64 => {
if bit_width != 64 {
return Err(general_err!(
"Incorrect bit width {} for INT64",
bit_width
));
}
}
_ => {
return Err(general_err!(
"Logical type Integer cannot be used with physical type {}",
physical_type
))
}
}
if let Some(",") = self.tokenizer.next() {
let is_signed = parse_bool(
self.tokenizer.next(),
"Invalid boolean found",
"Failed to parse is_signed for INTEGER type",
)?;
assert_token(self.tokenizer.next(), ")")?;
logical = Some(LogicalType::Integer {
bit_width,
is_signed,
});
converted = ConvertedType::from(logical.clone());
} else {
// Invalid token for unit
self.tokenizer.backtrack();
}
}
}
_ => {}
}
} else if converted == ConvertedType::DECIMAL {
if let Some("(") = self.tokenizer.next() {
// Parse precision
precision = parse_i32(
self.tokenizer.next(),
"Expected precision, found None",
"Failed to parse precision for DECIMAL type",
)?;
// Parse scale
scale = if let Some(",") = self.tokenizer.next() {
parse_i32(
self.tokenizer.next(),
"Expected scale, found None",
"Failed to parse scale for DECIMAL type",
)?
} else {
// Scale is not provided, set it to 0.
self.tokenizer.backtrack();
0
};
assert_token(self.tokenizer.next(), ")")?;
} else {
self.tokenizer.backtrack();
}
}
assert_token(self.tokenizer.next(), ")")?;
(logical, converted, precision, scale)
} else {
self.tokenizer.backtrack();
(None, ConvertedType::NONE, -1, -1)
};
// Parse optional id
let id = if let Some("=") = self.tokenizer.next() {
self.tokenizer.next().and_then(|v| v.parse::<i32>().ok())
} else {
self.tokenizer.backtrack();
None
};
assert_token(self.tokenizer.next(), ";")?;
Type::primitive_type_builder(name, physical_type)
.with_repetition(repetition)
.with_logical_type(logical_type)
.with_converted_type(converted_type)
.with_length(length)
.with_precision(precision)
.with_scale(scale)
.with_id(id)
.build()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tokenize_empty_string() {
assert_eq!(Tokenizer::from_str("").next(), None);
}
#[test]
fn test_tokenize_delimiters() {
let mut iter = Tokenizer::from_str(",;{}()=");
assert_eq!(iter.next(), Some(","));
assert_eq!(iter.next(), Some(";"));
assert_eq!(iter.next(), Some("{"));
assert_eq!(iter.next(), Some("}"));
assert_eq!(iter.next(), Some("("));
assert_eq!(iter.next(), Some(")"));
assert_eq!(iter.next(), Some("="));
assert_eq!(iter.next(), None);
}
#[test]
fn test_tokenize_delimiters_with_whitespaces() {
let mut iter = Tokenizer::from_str(" , ; { } ( ) = ");
assert_eq!(iter.next(), Some(","));
assert_eq!(iter.next(), Some(";"));
assert_eq!(iter.next(), Some("{"));
assert_eq!(iter.next(), Some("}"));
assert_eq!(iter.next(), Some("("));
assert_eq!(iter.next(), Some(")"));
assert_eq!(iter.next(), Some("="));
assert_eq!(iter.next(), None);
}
#[test]
fn test_tokenize_words() {
let mut iter = Tokenizer::from_str("abc def ghi jkl mno");
assert_eq!(iter.next(), Some("abc"));
assert_eq!(iter.next(), Some("def"));
assert_eq!(iter.next(), Some("ghi"));
assert_eq!(iter.next(), Some("jkl"));
assert_eq!(iter.next(), Some("mno"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_tokenize_backtrack() {
let mut iter = Tokenizer::from_str("abc;");
assert_eq!(iter.next(), Some("abc"));
assert_eq!(iter.next(), Some(";"));
iter.backtrack();
assert_eq!(iter.next(), Some(";"));
assert_eq!(iter.next(), None);
}
#[test]
fn test_tokenize_message_type() {
let schema = "
message schema {
required int32 a;
optional binary c (UTF8);
required group d {
required int32 a;
optional binary c (UTF8);
}
required group e (LIST) {
repeated group list {
required int32 element;
}
}
}
";
let iter = Tokenizer::from_str(schema);
let mut res = Vec::new();
for token in iter {
res.push(token);
}
assert_eq!(
res,
vec![
"message", "schema", "{", "required", "int32", "a", ";", "optional", "binary", "c",
"(", "UTF8", ")", ";", "required", "group", "d", "{", "required", "int32", "a",
";", "optional", "binary", "c", "(", "UTF8", ")", ";", "}", "required", "group",
"e", "(", "LIST", ")", "{", "repeated", "group", "list", "{", "required", "int32",
"element", ";", "}", "}", "}"
]
);
}
#[test]
fn test_assert_token() {
assert!(assert_token(Some("a"), "a").is_ok());
assert!(assert_token(Some("a"), "b").is_err());
assert!(assert_token(None, "b").is_err());
}
fn parse(schema: &str) -> Result<Type, ParquetError> {
let mut iter = Tokenizer::from_str(schema);
Parser {
tokenizer: &mut iter,
}
.parse_message_type()
}
#[test]
fn test_parse_message_type_invalid() {
assert_eq!(
parse("test").unwrap_err().to_string(),
"Parquet error: Message type does not start with 'message'"
);
}
#[test]
fn test_parse_message_type_no_name() {
assert_eq!(
parse("message").unwrap_err().to_string(),
"Parquet error: Expected name, found None"
);
}
#[test]
fn test_parse_message_type_fixed_byte_array() {
let schema = "
message schema {
REQUIRED FIXED_LEN_BYTE_ARRAY col;
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Expected '(', found token 'col'"
);
let schema = "
message schema {
REQUIRED FIXED_LEN_BYTE_ARRAY(16) col;
}
";
parse(schema).unwrap();
}
#[test]
fn test_parse_message_type_integer() {
// Invalid integer syntax
let schema = "
message root {
optional int64 f1 (INTEGER());
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse bit_width for INTEGER type"
);
// Invalid integer syntax, needs both bit-width and UTC sign
let schema = "
message root {
optional int64 f1 (INTEGER(32,));
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Incorrect bit width 32 for INT64"
);
// Invalid integer because of non-numeric bit width
let schema = "
message root {
optional int32 f1 (INTEGER(eight,true));
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse bit_width for INTEGER type"
);
// Valid types
let schema = "
message root {
optional int32 f1 (INTEGER(8,false));
optional int32 f2 (INTEGER(8,true));
optional int32 f3 (INTEGER(16,false));
optional int32 f4 (INTEGER(16,true));
optional int32 f5 (INTEGER(32,false));
optional int32 f6 (INTEGER(32,true));
optional int64 f7 (INTEGER(64,false));
optional int64 f7 (INTEGER(64,true));
}
";
parse(schema).unwrap();
}
#[test]
fn test_parse_message_type_temporal() {
// Invalid timestamp syntax
let schema = "
message root {
optional int64 f1 (TIMESTAMP();
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse timeunit for TIMESTAMP type"
);
// Invalid timestamp syntax, needs both unit and UTC adjustment
let schema = "
message root {
optional int64 f1 (TIMESTAMP(MILLIS,));
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse timezone info for TIMESTAMP type"
);
// Invalid timestamp because of unknown unit
let schema = "
message root {
optional int64 f1 (TIMESTAMP(YOCTOS,));
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse timeunit for TIMESTAMP type"
);
// Valid types
let schema = "
message root {
optional int32 f1 (DATE);
optional int32 f2 (TIME(MILLIS,true));
optional int64 f3 (TIME(MICROS,false));
optional int64 f4 (TIME(NANOS,true));
optional int64 f5 (TIMESTAMP(MILLIS,true));
optional int64 f6 (TIMESTAMP(MICROS,true));
optional int64 f7 (TIMESTAMP(NANOS,false));
}
";
parse(schema).unwrap();
}
#[test]
fn test_parse_message_type_decimal() {
// It is okay for decimal to omit precision and scale with right syntax.
// Here we test wrong syntax of decimal type
// Invalid decimal syntax
let schema = "
message root {
optional int32 f1 (DECIMAL();
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse precision for DECIMAL type"
);
// Invalid decimal, need precision and scale
let schema = "
message root {
optional int32 f1 (DECIMAL());
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse precision for DECIMAL type"
);
// Invalid decimal because of `,` - has precision, needs scale
let schema = "
message root {
optional int32 f1 (DECIMAL(8,));
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Failed to parse scale for DECIMAL type"
);
// Invalid decimal because, we always require either precision or scale to be
// specified as part of converted type
let schema = "
message root {
optional int32 f3 (DECIMAL);
}
";
assert_eq!(
parse(schema).unwrap_err().to_string(),
"Parquet error: Expected ')', found token ';'"
);
// Valid decimal (precision, scale)
let schema = "
message root {
optional int32 f1 (DECIMAL(8, 3));
optional int32 f2 (DECIMAL(8));
}
";
parse(schema).unwrap();
}
#[test]
fn test_parse_message_type_compare_1() {
let schema = "
message root {
optional fixed_len_byte_array(5) f1 (DECIMAL(9, 3));
optional fixed_len_byte_array (16) f2 (DECIMAL (38, 18));
optional fixed_len_byte_array (2) f3 (FLOAT16);
}
";
let message = parse(schema).unwrap();
let expected = Type::group_type_builder("root")
.with_fields(vec![
Arc::new(
Type::primitive_type_builder("f1", PhysicalType::FIXED_LEN_BYTE_ARRAY)
.with_logical_type(Some(LogicalType::Decimal {
precision: 9,
scale: 3,
}))
.with_converted_type(ConvertedType::DECIMAL)
.with_length(5)
.with_precision(9)
.with_scale(3)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("f2", PhysicalType::FIXED_LEN_BYTE_ARRAY)
.with_logical_type(Some(LogicalType::Decimal {
precision: 38,
scale: 18,
}))
.with_converted_type(ConvertedType::DECIMAL)
.with_length(16)
.with_precision(38)
.with_scale(18)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("f3", PhysicalType::FIXED_LEN_BYTE_ARRAY)
.with_logical_type(Some(LogicalType::Float16))
.with_length(2)
.build()
.unwrap(),
),
])
.build()
.unwrap();
assert_eq!(message, expected);
}
#[test]
fn test_parse_message_type_compare_2() {
let schema = "
message root {
required group a0 {
optional group a1 (LIST) {
repeated binary a2 (UTF8);
}
optional group b1 (LIST) {
repeated group b2 {
optional int32 b3;
optional double b4;
}
}
}
}
";
let message = parse(schema).unwrap();
let expected = Type::group_type_builder("root")
.with_fields(vec![Arc::new(
Type::group_type_builder("a0")
.with_repetition(Repetition::REQUIRED)
.with_fields(vec![
Arc::new(
Type::group_type_builder("a1")
.with_repetition(Repetition::OPTIONAL)
.with_logical_type(Some(LogicalType::List))
.with_converted_type(ConvertedType::LIST)
.with_fields(vec![Arc::new(
Type::primitive_type_builder("a2", PhysicalType::BYTE_ARRAY)
.with_repetition(Repetition::REPEATED)
.with_converted_type(ConvertedType::UTF8)
.build()
.unwrap(),
)])
.build()
.unwrap(),
),
Arc::new(
Type::group_type_builder("b1")
.with_repetition(Repetition::OPTIONAL)
.with_logical_type(Some(LogicalType::List))
.with_converted_type(ConvertedType::LIST)
.with_fields(vec![Arc::new(
Type::group_type_builder("b2")
.with_repetition(Repetition::REPEATED)
.with_fields(vec![
Arc::new(
Type::primitive_type_builder(
"b3",
PhysicalType::INT32,
)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder(
"b4",
PhysicalType::DOUBLE,
)
.build()
.unwrap(),
),
])
.build()
.unwrap(),
)])
.build()
.unwrap(),
),
])
.build()
.unwrap(),
)])
.build()
.unwrap();
assert_eq!(message, expected);
}
#[test]
fn test_parse_message_type_compare_3() {
let schema = "
message root {
required int32 _1 (INT_8);
required int32 _2 (INT_16);
required float _3;
required double _4;
optional int32 _5 (DATE);
optional binary _6 (UTF8);
}
";
let message = parse(schema).unwrap();
let fields = vec![
Arc::new(
Type::primitive_type_builder("_1", PhysicalType::INT32)
.with_repetition(Repetition::REQUIRED)
.with_converted_type(ConvertedType::INT_8)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_2", PhysicalType::INT32)
.with_repetition(Repetition::REQUIRED)
.with_converted_type(ConvertedType::INT_16)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_3", PhysicalType::FLOAT)
.with_repetition(Repetition::REQUIRED)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_4", PhysicalType::DOUBLE)
.with_repetition(Repetition::REQUIRED)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_5", PhysicalType::INT32)
.with_logical_type(Some(LogicalType::Date))
.with_converted_type(ConvertedType::DATE)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_6", PhysicalType::BYTE_ARRAY)
.with_converted_type(ConvertedType::UTF8)
.build()
.unwrap(),
),
];
let expected = Type::group_type_builder("root")
.with_fields(fields)
.build()
.unwrap();
assert_eq!(message, expected);
}
#[test]
fn test_parse_message_type_compare_4() {
let schema = "
message root {
required int32 _1 (INTEGER(8,true));
required int32 _2 (INTEGER(16,false));
required float _3;
required double _4;
optional int32 _5 (DATE);
optional int32 _6 (TIME(MILLIS,false));
optional int64 _7 (TIME(MICROS,true));
optional int64 _8 (TIMESTAMP(MILLIS,true));
optional int64 _9 (TIMESTAMP(NANOS,false));
optional binary _10 (STRING);
}
";
let message = parse(schema).unwrap();
let fields = vec![
Arc::new(
Type::primitive_type_builder("_1", PhysicalType::INT32)
.with_repetition(Repetition::REQUIRED)
.with_logical_type(Some(LogicalType::Integer {
bit_width: 8,
is_signed: true,
}))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_2", PhysicalType::INT32)
.with_repetition(Repetition::REQUIRED)
.with_logical_type(Some(LogicalType::Integer {
bit_width: 16,
is_signed: false,
}))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_3", PhysicalType::FLOAT)
.with_repetition(Repetition::REQUIRED)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_4", PhysicalType::DOUBLE)
.with_repetition(Repetition::REQUIRED)
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_5", PhysicalType::INT32)
.with_logical_type(Some(LogicalType::Date))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_6", PhysicalType::INT32)
.with_logical_type(Some(LogicalType::Time {
unit: TimeUnit::MILLIS(Default::default()),
is_adjusted_to_u_t_c: false,
}))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_7", PhysicalType::INT64)
.with_logical_type(Some(LogicalType::Time {
unit: TimeUnit::MICROS(Default::default()),
is_adjusted_to_u_t_c: true,
}))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_8", PhysicalType::INT64)
.with_logical_type(Some(LogicalType::Timestamp {
unit: TimeUnit::MILLIS(Default::default()),
is_adjusted_to_u_t_c: true,
}))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_9", PhysicalType::INT64)
.with_logical_type(Some(LogicalType::Timestamp {
unit: TimeUnit::NANOS(Default::default()),
is_adjusted_to_u_t_c: false,
}))
.build()
.unwrap(),
),
Arc::new(
Type::primitive_type_builder("_10", PhysicalType::BYTE_ARRAY)
.with_logical_type(Some(LogicalType::String))
.build()
.unwrap(),
),
];
let expected = Type::group_type_builder("root")
.with_fields(fields)
.build()
.unwrap();
assert_eq!(message, expected);
}
}