1pub use feldera_ir::SourcePosition;
2use serde::{Deserialize, Deserializer, Serialize, Serializer};
3use std::cmp::Ordering;
4use std::collections::BTreeMap;
5use std::fmt::Display;
6use std::hash::{Hash, Hasher};
7use utoipa::ToSchema;
8use utoipa::openapi::{ObjectBuilder, RefOr, Schema, SchemaType};
9
10#[cfg(feature = "testing")]
11use proptest::{collection::vec, prelude::any};
12
13pub fn canonical_identifier(id: &str) -> String {
21 if id.starts_with('"') && id.ends_with('"') && id.len() >= 2 {
22 id[1..id.len() - 1].to_string()
23 } else {
24 id.to_lowercase()
25 }
26}
27
28#[derive(Serialize, Deserialize, ToSchema, Debug, Clone)]
33#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
34pub struct SqlIdentifier {
35 #[cfg_attr(feature = "testing", proptest(regex = "relation1|relation2|relation3"))]
36 name: String,
37 pub case_sensitive: bool,
38}
39
40impl SqlIdentifier {
41 pub fn new<S: AsRef<str>>(name: S, case_sensitive: bool) -> Self {
42 Self {
43 name: name.as_ref().to_string(),
44 case_sensitive,
45 }
46 }
47
48 pub fn name(&self) -> String {
58 if self.case_sensitive {
59 self.name.clone()
60 } else {
61 self.name.to_lowercase()
62 }
63 }
64
65 pub fn sql_name(&self) -> String {
76 if self.case_sensitive {
77 format!("\"{}\"", self.name)
78 } else {
79 self.name.clone()
80 }
81 }
82}
83
84impl Hash for SqlIdentifier {
85 fn hash<H: Hasher>(&self, state: &mut H) {
86 self.name().hash(state);
87 }
88}
89
90impl PartialEq for SqlIdentifier {
91 fn eq(&self, other: &Self) -> bool {
92 match (self.case_sensitive, other.case_sensitive) {
93 (true, true) => self.name == other.name,
94 (false, false) => self.name.to_lowercase() == other.name.to_lowercase(),
95 (true, false) => self.name == other.name,
96 (false, true) => self.name == other.name,
97 }
98 }
99}
100
101impl Ord for SqlIdentifier {
102 fn cmp(&self, other: &Self) -> Ordering {
103 self.name().cmp(&other.name())
104 }
105}
106
107impl PartialOrd for SqlIdentifier {
108 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
109 Some(self.cmp(other))
110 }
111}
112
113impl<S: AsRef<str>> PartialEq<S> for SqlIdentifier {
114 fn eq(&self, other: &S) -> bool {
115 self == &SqlIdentifier::from(other.as_ref())
116 }
117}
118
119impl Eq for SqlIdentifier {}
120
121impl<S: AsRef<str>> From<S> for SqlIdentifier {
122 fn from(name: S) -> Self {
123 if name.as_ref().starts_with('"')
124 && name.as_ref().ends_with('"')
125 && name.as_ref().len() >= 2
126 {
127 Self {
128 name: name.as_ref()[1..name.as_ref().len() - 1].to_string(),
129 case_sensitive: true,
130 }
131 } else {
132 Self::new(name, false)
133 }
134 }
135}
136
137impl From<SqlIdentifier> for String {
138 fn from(id: SqlIdentifier) -> String {
139 id.name()
140 }
141}
142
143impl From<&SqlIdentifier> for String {
144 fn from(id: &SqlIdentifier) -> String {
145 id.name()
146 }
147}
148
149impl Display for SqlIdentifier {
150 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151 write!(f, "{}", self.name())
152 }
153}
154
155#[derive(Default, Serialize, Deserialize, ToSchema, Debug, Eq, PartialEq, Clone)]
159#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
160pub struct ProgramSchema {
161 #[cfg_attr(
162 feature = "testing",
163 proptest(strategy = "vec(any::<Relation>(), 0..2)")
164 )]
165 pub inputs: Vec<Relation>,
166 #[cfg_attr(
167 feature = "testing",
168 proptest(strategy = "vec(any::<Relation>(), 0..2)")
169 )]
170 pub outputs: Vec<Relation>,
171}
172
173impl ProgramSchema {
174 pub fn relations_with_lateness(&self) -> Vec<SqlIdentifier> {
175 self.inputs
176 .iter()
177 .chain(self.outputs.iter())
178 .filter(|rel| rel.has_lateness())
179 .map(|rel| rel.name.clone())
180 .collect()
181 }
182}
183
184#[derive(Debug, Deserialize)]
189pub struct ProgramSchemaPropertiesOnly {
190 #[serde(default)]
191 pub inputs: Vec<RelationPropertiesOnly>,
192 #[serde(default)]
193 pub outputs: Vec<RelationPropertiesOnly>,
194}
195
196#[derive(Serialize, Deserialize, ToSchema, Debug, Eq, PartialEq, Clone)]
197#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
198pub struct PropertyValue {
199 pub value: String,
200 #[serde(default)]
201 #[schema(required = true)]
202 pub key_position: SourcePosition,
203 #[serde(default)]
204 #[schema(required = true)]
205 pub value_position: SourcePosition,
206}
207
208#[derive(Serialize, Deserialize, ToSchema, Debug, Eq, PartialEq, Clone)]
212#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
213pub struct Relation {
214 #[serde(flatten)]
215 pub name: SqlIdentifier,
216 #[cfg_attr(feature = "testing", proptest(value = "Vec::new()"))]
217 pub fields: Vec<Field>,
218 #[serde(default)]
219 pub materialized: bool,
220 #[serde(default)]
221 pub properties: BTreeMap<String, PropertyValue>,
222 pub primary_key: Option<Vec<String>>,
223}
224
225impl Relation {
226 pub fn empty() -> Self {
227 Self {
228 name: SqlIdentifier::from("".to_string()),
229 fields: Vec::new(),
230 materialized: false,
231 properties: BTreeMap::new(),
232 primary_key: None,
233 }
234 }
235
236 pub fn new(
237 name: SqlIdentifier,
238 fields: Vec<Field>,
239 materialized: bool,
240 properties: BTreeMap<String, PropertyValue>,
241 ) -> Self {
242 Self {
243 name,
244 fields,
245 materialized,
246 properties,
247 primary_key: None,
248 }
249 }
250
251 pub fn field(&self, name: &str) -> Option<&Field> {
253 let name = canonical_identifier(name);
254 self.fields.iter().find(|f| f.name == name)
255 }
256
257 pub fn has_lateness(&self) -> bool {
258 self.fields.iter().any(|f| f.lateness.is_some())
259 }
260
261 pub fn get_property(&self, name: &str) -> Option<&str> {
262 self.properties.get(name).map(|p| p.value.as_str())
263 }
264
265 pub fn skip_unused_columns(&self) -> bool {
269 self.get_property("skip_unused_columns") == Some("true")
270 }
271
272 pub fn with_primary_key<'a>(
273 mut self,
274 primary_key: impl IntoIterator<Item = &'a SqlIdentifier>,
275 ) -> Self {
276 self.primary_key = Some(primary_key.into_iter().map(|id| id.name()).collect());
277 self
278 }
279}
280
281#[derive(Debug, Deserialize)]
285pub struct RelationPropertiesOnly {
286 #[serde(flatten)]
287 pub name: SqlIdentifier,
288 #[serde(default)]
289 pub properties: BTreeMap<String, PropertyValue>,
290 #[serde(default)]
292 pub materialized: bool,
293}
294
295#[derive(Serialize, ToSchema, Debug, Eq, PartialEq, Clone)]
299#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
300pub struct Field {
301 #[serde(flatten)]
302 pub name: SqlIdentifier,
303 pub columntype: ColumnType,
304 pub lateness: Option<String>,
305 pub default: Option<String>,
306 pub unused: bool,
307}
308
309impl Field {
310 pub fn new(name: SqlIdentifier, columntype: ColumnType) -> Self {
311 Self {
312 name,
313 columntype,
314 lateness: None,
315 default: None,
316 unused: false,
317 }
318 }
319
320 pub fn with_lateness(mut self, lateness: &str) -> Self {
321 self.lateness = Some(lateness.to_string());
322 self
323 }
324
325 pub fn with_unused(mut self, unused: bool) -> Self {
326 self.unused = unused;
327 self
328 }
329}
330
331impl<'de> Deserialize<'de> for Field {
336 fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
337 where
338 D: Deserializer<'de>,
339 {
340 const fn default_is_struct() -> Option<SqlType> {
341 Some(SqlType::Struct)
342 }
343
344 #[derive(Debug, Clone, Deserialize)]
345 struct FieldHelper {
346 name: Option<String>,
347 #[serde(default)]
348 case_sensitive: bool,
349 columntype: Option<ColumnType>,
350 #[serde(rename = "type")]
351 #[serde(default = "default_is_struct")]
352 typ: Option<SqlType>,
353 nullable: Option<bool>,
354 precision: Option<i64>,
355 scale: Option<i64>,
356 component: Option<Box<ColumnType>>,
357 fields: Option<serde_json::Value>,
358 key: Option<Box<ColumnType>>,
359 value: Option<Box<ColumnType>>,
360 default: Option<String>,
361 #[serde(default)]
362 unused: bool,
363 lateness: Option<String>,
364 }
365
366 fn helper_to_field(helper: FieldHelper) -> Field {
367 let columntype = if let Some(ctype) = helper.columntype {
368 ctype
369 } else if let Some(serde_json::Value::Array(fields)) = helper.fields {
370 let fields = fields
371 .into_iter()
372 .map(|field| {
373 let field: FieldHelper = serde_json::from_value(field).unwrap();
374 helper_to_field(field)
375 })
376 .collect::<Vec<Field>>();
377
378 ColumnType {
379 typ: helper.typ.unwrap_or(SqlType::Null),
380 nullable: helper.nullable.unwrap_or(false),
381 precision: helper.precision,
382 scale: helper.scale,
383 component: helper.component,
384 fields: Some(fields),
385 key: None,
386 value: None,
387 }
388 } else if let Some(serde_json::Value::Object(obj)) = helper.fields {
389 serde_json::from_value(serde_json::Value::Object(obj))
390 .expect("Failed to deserialize object")
391 } else {
392 ColumnType {
393 typ: helper.typ.unwrap_or(SqlType::Null),
394 nullable: helper.nullable.unwrap_or(false),
395 precision: helper.precision,
396 scale: helper.scale,
397 component: helper.component,
398 fields: None,
399 key: helper.key,
400 value: helper.value,
401 }
402 };
403
404 Field {
405 name: SqlIdentifier::new(helper.name.unwrap(), helper.case_sensitive),
406 columntype,
407 default: helper.default,
408 unused: helper.unused,
409 lateness: helper.lateness,
410 }
411 }
412
413 let helper = FieldHelper::deserialize(deserializer)?;
414 Ok(helper_to_field(helper))
415 }
416}
417
418#[derive(ToSchema, Debug, Eq, PartialEq, Clone, Copy)]
423#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
424pub enum IntervalUnit {
425 Day,
427 DayToHour,
429 DayToMinute,
431 DayToSecond,
433 Hour,
435 HourToMinute,
437 HourToSecond,
439 Minute,
441 MinuteToSecond,
443 Month,
445 Second,
447 Year,
449 YearToMonth,
451}
452
453#[derive(Debug, Eq, PartialEq, Clone, Copy)]
464#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
465pub enum SqlType {
466 Boolean,
468 TinyInt,
470 SmallInt,
472 Int,
474 BigInt,
476 UTinyInt,
478 USmallInt,
480 UInt,
482 UBigInt,
484 Real,
486 Double,
488 Decimal,
490 Char,
492 Varchar,
494 Binary,
496 Varbinary,
498 Time,
500 Date,
502 Timestamp,
504 TimestampTz,
506 Interval(IntervalUnit),
508 Array,
510 Struct,
512 Map,
514 Null,
516 Uuid,
518 Variant,
520}
521
522const SQL_TYPE_VALUES: &[&str] = &[
530 "BOOLEAN",
531 "TINYINT",
532 "SMALLINT",
533 "INTEGER",
534 "BIGINT",
535 "UTINYINT",
536 "USMALLINT",
537 "UINTEGER",
538 "UBIGINT",
539 "REAL",
540 "DOUBLE",
541 "DECIMAL",
542 "CHAR",
543 "VARCHAR",
544 "BINARY",
545 "VARBINARY",
546 "TIME",
547 "DATE",
548 "TIMESTAMP",
549 "TIMESTAMP_TZ",
550 "INTERVAL_DAY",
551 "INTERVAL_DAY_HOUR",
552 "INTERVAL_DAY_MINUTE",
553 "INTERVAL_DAY_SECOND",
554 "INTERVAL_HOUR",
555 "INTERVAL_HOUR_MINUTE",
556 "INTERVAL_HOUR_SECOND",
557 "INTERVAL_MINUTE",
558 "INTERVAL_MINUTE_SECOND",
559 "INTERVAL_MONTH",
560 "INTERVAL_SECOND",
561 "INTERVAL_YEAR",
562 "INTERVAL_YEAR_MONTH",
563 "ARRAY",
564 "STRUCT",
565 "MAP",
566 "NULL",
567 "UUID",
568 "VARIANT",
569];
570
571impl SqlType {
572 pub fn as_wire_str(&self) -> &'static str {
577 match self {
578 SqlType::Boolean => "BOOLEAN",
579 SqlType::TinyInt => "TINYINT",
580 SqlType::SmallInt => "SMALLINT",
581 SqlType::Int => "INTEGER",
582 SqlType::BigInt => "BIGINT",
583 SqlType::UTinyInt => "UTINYINT",
584 SqlType::USmallInt => "USMALLINT",
585 SqlType::UInt => "UINTEGER",
586 SqlType::UBigInt => "UBIGINT",
587 SqlType::Real => "REAL",
588 SqlType::Double => "DOUBLE",
589 SqlType::Decimal => "DECIMAL",
590 SqlType::Char => "CHAR",
591 SqlType::Varchar => "VARCHAR",
592 SqlType::Binary => "BINARY",
593 SqlType::Varbinary => "VARBINARY",
594 SqlType::Time => "TIME",
595 SqlType::Date => "DATE",
596 SqlType::Timestamp => "TIMESTAMP",
597 SqlType::TimestampTz => "TIMESTAMP_TZ",
598 SqlType::Interval(interval_unit) => match interval_unit {
599 IntervalUnit::Day => "INTERVAL_DAY",
600 IntervalUnit::DayToHour => "INTERVAL_DAY_HOUR",
601 IntervalUnit::DayToMinute => "INTERVAL_DAY_MINUTE",
602 IntervalUnit::DayToSecond => "INTERVAL_DAY_SECOND",
603 IntervalUnit::Hour => "INTERVAL_HOUR",
604 IntervalUnit::HourToMinute => "INTERVAL_HOUR_MINUTE",
605 IntervalUnit::HourToSecond => "INTERVAL_HOUR_SECOND",
606 IntervalUnit::Minute => "INTERVAL_MINUTE",
607 IntervalUnit::MinuteToSecond => "INTERVAL_MINUTE_SECOND",
608 IntervalUnit::Month => "INTERVAL_MONTH",
609 IntervalUnit::Second => "INTERVAL_SECOND",
610 IntervalUnit::Year => "INTERVAL_YEAR",
611 IntervalUnit::YearToMonth => "INTERVAL_YEAR_MONTH",
612 },
613 SqlType::Array => "ARRAY",
614 SqlType::Struct => "STRUCT",
615 SqlType::Map => "MAP",
616 SqlType::Null => "NULL",
617 SqlType::Uuid => "UUID",
618 SqlType::Variant => "VARIANT",
619 }
620 }
621}
622
623impl ToSchema<'_> for SqlType {
624 fn schema() -> (&'static str, RefOr<Schema>) {
625 (
626 "SqlType",
627 RefOr::T(Schema::Object(
628 ObjectBuilder::new()
629 .schema_type(SchemaType::String)
630 .description(Some(
631 "The available SQL column type names. Each value is the platform's wire \
632 encoding of the type (e.g. `BIGINT`, `INTEGER`, `INTERVAL_DAY`), not \
633 valid SQL type syntax.",
634 ))
635 .enum_values(Some(SQL_TYPE_VALUES.iter().copied()))
636 .build(),
637 )),
638 )
639 }
640}
641
642impl Display for SqlType {
643 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
644 f.write_str(&serde_json::to_string(self).unwrap())
645 }
646}
647
648impl<'de> Deserialize<'de> for SqlType {
649 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
650 where
651 D: Deserializer<'de>,
652 {
653 let value: String = Deserialize::deserialize(deserializer)?;
654 match value.to_lowercase().as_str() {
655 "interval_day" => Ok(SqlType::Interval(IntervalUnit::Day)),
656 "interval_day_hour" => Ok(SqlType::Interval(IntervalUnit::DayToHour)),
657 "interval_day_minute" => Ok(SqlType::Interval(IntervalUnit::DayToMinute)),
658 "interval_day_second" => Ok(SqlType::Interval(IntervalUnit::DayToSecond)),
659 "interval_hour" => Ok(SqlType::Interval(IntervalUnit::Hour)),
660 "interval_hour_minute" => Ok(SqlType::Interval(IntervalUnit::HourToMinute)),
661 "interval_hour_second" => Ok(SqlType::Interval(IntervalUnit::HourToSecond)),
662 "interval_minute" => Ok(SqlType::Interval(IntervalUnit::Minute)),
663 "interval_minute_second" => Ok(SqlType::Interval(IntervalUnit::MinuteToSecond)),
664 "interval_month" => Ok(SqlType::Interval(IntervalUnit::Month)),
665 "interval_second" => Ok(SqlType::Interval(IntervalUnit::Second)),
666 "interval_year" => Ok(SqlType::Interval(IntervalUnit::Year)),
667 "interval_year_month" => Ok(SqlType::Interval(IntervalUnit::YearToMonth)),
668 "boolean" => Ok(SqlType::Boolean),
669 "tinyint" => Ok(SqlType::TinyInt),
670 "smallint" => Ok(SqlType::SmallInt),
671 "integer" => Ok(SqlType::Int),
672 "bigint" => Ok(SqlType::BigInt),
673 "utinyint" => Ok(SqlType::UTinyInt),
674 "usmallint" => Ok(SqlType::USmallInt),
675 "uinteger" => Ok(SqlType::UInt),
676 "ubigint" => Ok(SqlType::UBigInt),
677 "real" => Ok(SqlType::Real),
678 "double" => Ok(SqlType::Double),
679 "decimal" => Ok(SqlType::Decimal),
680 "char" => Ok(SqlType::Char),
681 "varchar" => Ok(SqlType::Varchar),
682 "binary" => Ok(SqlType::Binary),
683 "varbinary" => Ok(SqlType::Varbinary),
684 "variant" => Ok(SqlType::Variant),
685 "time" => Ok(SqlType::Time),
686 "date" => Ok(SqlType::Date),
687 "timestamp" => Ok(SqlType::Timestamp),
688 "timestamp_tz" => Ok(SqlType::TimestampTz),
689 "array" => Ok(SqlType::Array),
690 "struct" => Ok(SqlType::Struct),
691 "map" => Ok(SqlType::Map),
692 "null" => Ok(SqlType::Null),
693 "uuid" => Ok(SqlType::Uuid),
694 _ => Err(serde::de::Error::custom(format!(
695 "Unknown SQL type: {}",
696 value
697 ))),
698 }
699 }
700}
701
702impl Serialize for SqlType {
703 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
704 where
705 S: Serializer,
706 {
707 serializer.serialize_str(self.as_wire_str())
708 }
709}
710
711impl SqlType {
712 pub fn is_string(&self) -> bool {
714 matches!(self, Self::Char | Self::Varchar)
715 }
716
717 pub fn is_varchar(&self) -> bool {
718 matches!(self, Self::Varchar)
719 }
720
721 pub fn is_varbinary(&self) -> bool {
722 matches!(self, Self::Varbinary)
723 }
724}
725
726const fn default_is_struct() -> SqlType {
729 SqlType::Struct
730}
731
732#[derive(Serialize, Deserialize, ToSchema, Debug, Eq, PartialEq, Clone)]
736#[cfg_attr(feature = "testing", derive(proptest_derive::Arbitrary))]
737pub struct ColumnType {
738 #[serde(rename = "type")]
740 #[serde(default = "default_is_struct")]
741 pub typ: SqlType,
742 pub nullable: bool,
744 pub precision: Option<i64>,
753 pub scale: Option<i64>,
758 #[cfg_attr(feature = "testing", proptest(value = "None"))]
765 pub component: Option<Box<ColumnType>>,
766 #[cfg_attr(feature = "testing", proptest(value = "Some(Vec::new())"))]
788 pub fields: Option<Vec<Field>>,
789 #[cfg_attr(feature = "testing", proptest(value = "None"))]
791 pub key: Option<Box<ColumnType>>,
792 #[cfg_attr(feature = "testing", proptest(value = "None"))]
794 pub value: Option<Box<ColumnType>>,
795}
796
797impl ColumnType {
798 pub fn boolean(nullable: bool) -> Self {
799 ColumnType {
800 typ: SqlType::Boolean,
801 nullable,
802 precision: None,
803 scale: None,
804 component: None,
805 fields: None,
806 key: None,
807 value: None,
808 }
809 }
810
811 pub fn uuid(nullable: bool) -> Self {
812 ColumnType {
813 typ: SqlType::Uuid,
814 nullable,
815 precision: None,
816 scale: None,
817 component: None,
818 fields: None,
819 key: None,
820 value: None,
821 }
822 }
823
824 pub fn tinyint(nullable: bool) -> Self {
825 ColumnType {
826 typ: SqlType::TinyInt,
827 nullable,
828 precision: None,
829 scale: None,
830 component: None,
831 fields: None,
832 key: None,
833 value: None,
834 }
835 }
836
837 pub fn smallint(nullable: bool) -> Self {
838 ColumnType {
839 typ: SqlType::SmallInt,
840 nullable,
841 precision: None,
842 scale: None,
843 component: None,
844 fields: None,
845 key: None,
846 value: None,
847 }
848 }
849
850 pub fn int(nullable: bool) -> Self {
851 ColumnType {
852 typ: SqlType::Int,
853 nullable,
854 precision: None,
855 scale: None,
856 component: None,
857 fields: None,
858 key: None,
859 value: None,
860 }
861 }
862
863 pub fn bigint(nullable: bool) -> Self {
864 ColumnType {
865 typ: SqlType::BigInt,
866 nullable,
867 precision: None,
868 scale: None,
869 component: None,
870 fields: None,
871 key: None,
872 value: None,
873 }
874 }
875
876 pub fn utinyint(nullable: bool) -> Self {
877 ColumnType {
878 typ: SqlType::UTinyInt,
879 nullable,
880 precision: None,
881 scale: None,
882 component: None,
883 fields: None,
884 key: None,
885 value: None,
886 }
887 }
888
889 pub fn usmallint(nullable: bool) -> Self {
890 ColumnType {
891 typ: SqlType::USmallInt,
892 nullable,
893 precision: None,
894 scale: None,
895 component: None,
896 fields: None,
897 key: None,
898 value: None,
899 }
900 }
901
902 pub fn uint(nullable: bool) -> Self {
903 ColumnType {
904 typ: SqlType::UInt,
905 nullable,
906 precision: None,
907 scale: None,
908 component: None,
909 fields: None,
910 key: None,
911 value: None,
912 }
913 }
914
915 pub fn ubigint(nullable: bool) -> Self {
916 ColumnType {
917 typ: SqlType::UBigInt,
918 nullable,
919 precision: None,
920 scale: None,
921 component: None,
922 fields: None,
923 key: None,
924 value: None,
925 }
926 }
927
928 pub fn double(nullable: bool) -> Self {
929 ColumnType {
930 typ: SqlType::Double,
931 nullable,
932 precision: None,
933 scale: None,
934 component: None,
935 fields: None,
936 key: None,
937 value: None,
938 }
939 }
940
941 pub fn real(nullable: bool) -> Self {
942 ColumnType {
943 typ: SqlType::Real,
944 nullable,
945 precision: None,
946 scale: None,
947 component: None,
948 fields: None,
949 key: None,
950 value: None,
951 }
952 }
953
954 pub fn decimal(precision: i64, scale: i64, nullable: bool) -> Self {
955 ColumnType {
956 typ: SqlType::Decimal,
957 nullable,
958 precision: Some(precision),
959 scale: Some(scale),
960 component: None,
961 fields: None,
962 key: None,
963 value: None,
964 }
965 }
966
967 pub fn varchar(nullable: bool) -> Self {
968 ColumnType {
969 typ: SqlType::Varchar,
970 nullable,
971 precision: None,
972 scale: None,
973 component: None,
974 fields: None,
975 key: None,
976 value: None,
977 }
978 }
979
980 pub fn varbinary(nullable: bool) -> Self {
981 ColumnType {
982 typ: SqlType::Varbinary,
983 nullable,
984 precision: None,
985 scale: None,
986 component: None,
987 fields: None,
988 key: None,
989 value: None,
990 }
991 }
992
993 pub fn fixed(width: i64, nullable: bool) -> Self {
994 ColumnType {
995 typ: SqlType::Binary,
996 nullable,
997 precision: Some(width),
998 scale: None,
999 component: None,
1000 fields: None,
1001 key: None,
1002 value: None,
1003 }
1004 }
1005
1006 pub fn date(nullable: bool) -> Self {
1007 ColumnType {
1008 typ: SqlType::Date,
1009 nullable,
1010 precision: None,
1011 scale: None,
1012 component: None,
1013 fields: None,
1014 key: None,
1015 value: None,
1016 }
1017 }
1018
1019 pub fn time(nullable: bool) -> Self {
1020 ColumnType {
1021 typ: SqlType::Time,
1022 nullable,
1023 precision: None,
1024 scale: None,
1025 component: None,
1026 fields: None,
1027 key: None,
1028 value: None,
1029 }
1030 }
1031
1032 pub fn timestamp(nullable: bool) -> Self {
1033 ColumnType {
1034 typ: SqlType::Timestamp,
1035 nullable,
1036 precision: None,
1037 scale: None,
1038 component: None,
1039 fields: None,
1040 key: None,
1041 value: None,
1042 }
1043 }
1044
1045 pub fn timestamp_tz(nullable: bool) -> Self {
1046 ColumnType {
1047 typ: SqlType::TimestampTz,
1048 nullable,
1049 precision: None,
1050 scale: None,
1051 component: None,
1052 fields: None,
1053 key: None,
1054 value: None,
1055 }
1056 }
1057
1058 pub fn variant(nullable: bool) -> Self {
1059 ColumnType {
1060 typ: SqlType::Variant,
1061 nullable,
1062 precision: None,
1063 scale: None,
1064 component: None,
1065 fields: None,
1066 key: None,
1067 value: None,
1068 }
1069 }
1070
1071 pub fn array(nullable: bool, element: ColumnType) -> Self {
1072 ColumnType {
1073 typ: SqlType::Array,
1074 nullable,
1075 precision: None,
1076 scale: None,
1077 component: Some(Box::new(element)),
1078 fields: None,
1079 key: None,
1080 value: None,
1081 }
1082 }
1083
1084 pub fn structure(nullable: bool, fields: &[Field]) -> Self {
1085 ColumnType {
1086 typ: SqlType::Struct,
1087 nullable,
1088 precision: None,
1089 scale: None,
1090 component: None,
1091 fields: Some(fields.to_vec()),
1092 key: None,
1093 value: None,
1094 }
1095 }
1096
1097 pub fn map(nullable: bool, key: ColumnType, val: ColumnType) -> Self {
1098 ColumnType {
1099 typ: SqlType::Map,
1100 nullable,
1101 precision: None,
1102 scale: None,
1103 component: None,
1104 fields: None,
1105 key: Some(Box::new(key)),
1106 value: Some(Box::new(val)),
1107 }
1108 }
1109
1110 pub fn is_integral_type(&self) -> bool {
1111 matches!(
1112 &self.typ,
1113 SqlType::TinyInt
1114 | SqlType::SmallInt
1115 | SqlType::Int
1116 | SqlType::BigInt
1117 | SqlType::UTinyInt
1118 | SqlType::USmallInt
1119 | SqlType::UInt
1120 | SqlType::UBigInt
1121 )
1122 }
1123
1124 pub fn is_fp_type(&self) -> bool {
1125 matches!(&self.typ, SqlType::Double | SqlType::Real)
1126 }
1127
1128 pub fn is_decimal_type(&self) -> bool {
1129 matches!(&self.typ, SqlType::Decimal)
1130 }
1131
1132 pub fn is_numeric_type(&self) -> bool {
1133 self.is_integral_type() || self.is_fp_type() || self.is_decimal_type()
1134 }
1135}
1136
1137#[cfg(test)]
1138mod tests {
1139 use super::{IntervalUnit, SqlIdentifier};
1140 use crate::program_schema::{ColumnType, Field, SQL_TYPE_VALUES, SqlType};
1141
1142 fn all_sql_types() -> Vec<SqlType> {
1144 let mut types = vec![
1145 SqlType::Boolean,
1146 SqlType::TinyInt,
1147 SqlType::SmallInt,
1148 SqlType::Int,
1149 SqlType::BigInt,
1150 SqlType::UTinyInt,
1151 SqlType::USmallInt,
1152 SqlType::UInt,
1153 SqlType::UBigInt,
1154 SqlType::Real,
1155 SqlType::Double,
1156 SqlType::Decimal,
1157 SqlType::Char,
1158 SqlType::Varchar,
1159 SqlType::Binary,
1160 SqlType::Varbinary,
1161 SqlType::Time,
1162 SqlType::Date,
1163 SqlType::Timestamp,
1164 SqlType::TimestampTz,
1165 ];
1166 types.extend(
1167 [
1168 IntervalUnit::Day,
1169 IntervalUnit::DayToHour,
1170 IntervalUnit::DayToMinute,
1171 IntervalUnit::DayToSecond,
1172 IntervalUnit::Hour,
1173 IntervalUnit::HourToMinute,
1174 IntervalUnit::HourToSecond,
1175 IntervalUnit::Minute,
1176 IntervalUnit::MinuteToSecond,
1177 IntervalUnit::Month,
1178 IntervalUnit::Second,
1179 IntervalUnit::Year,
1180 IntervalUnit::YearToMonth,
1181 ]
1182 .map(SqlType::Interval),
1183 );
1184 types.extend([
1185 SqlType::Array,
1186 SqlType::Struct,
1187 SqlType::Map,
1188 SqlType::Null,
1189 SqlType::Uuid,
1190 SqlType::Variant,
1191 ]);
1192 types
1193 }
1194
1195 #[test]
1200 fn sql_type_schema_matches_serialization() {
1201 let serialized: Vec<String> = all_sql_types()
1202 .iter()
1203 .map(|t| {
1204 serde_json::to_value(t)
1205 .unwrap()
1206 .as_str()
1207 .unwrap()
1208 .to_owned()
1209 })
1210 .collect();
1211
1212 assert_eq!(
1214 serialized,
1215 SQL_TYPE_VALUES
1216 .iter()
1217 .map(|s| s.to_string())
1218 .collect::<Vec<_>>(),
1219 "SQL_TYPE_VALUES (the OpenAPI enum) must match `Serialize` output exactly"
1220 );
1221
1222 for t in all_sql_types() {
1224 assert_eq!(
1225 serde_json::to_value(t).unwrap().as_str().unwrap(),
1226 t.as_wire_str()
1227 );
1228 }
1229 }
1230
1231 #[test]
1234 fn relation_skip_unused_columns_property() {
1235 use super::{PropertyValue, Relation, SourcePosition};
1236 use std::collections::BTreeMap;
1237
1238 let zero = SourcePosition {
1239 start_line_number: 0,
1240 start_column: 0,
1241 end_line_number: 0,
1242 end_column: 0,
1243 };
1244 let relation = |value: Option<&str>| {
1245 let mut properties = BTreeMap::new();
1246 if let Some(value) = value {
1247 properties.insert(
1248 "skip_unused_columns".to_string(),
1249 PropertyValue {
1250 value: value.to_string(),
1251 key_position: zero,
1252 value_position: zero,
1253 },
1254 );
1255 }
1256 Relation::new(
1257 SqlIdentifier::new("t", false),
1258 Vec::new(),
1259 false,
1260 properties,
1261 )
1262 };
1263
1264 assert!(relation(Some("true")).skip_unused_columns());
1265 assert!(!relation(Some("false")).skip_unused_columns());
1266 assert!(!relation(Some("TRUE")).skip_unused_columns());
1267 assert!(!relation(None).skip_unused_columns());
1268 }
1269
1270 #[test]
1271 fn serde_sql_type() {
1272 for (sql_str_base, expected_value) in [
1273 ("Boolean", SqlType::Boolean),
1274 ("Uuid", SqlType::Uuid),
1275 ("TinyInt", SqlType::TinyInt),
1276 ("SmallInt", SqlType::SmallInt),
1277 ("Integer", SqlType::Int),
1278 ("BigInt", SqlType::BigInt),
1279 ("UTinyInt", SqlType::UTinyInt),
1280 ("USmallInt", SqlType::USmallInt),
1281 ("UInteger", SqlType::UInt),
1282 ("UBigInt", SqlType::UBigInt),
1283 ("Real", SqlType::Real),
1284 ("Double", SqlType::Double),
1285 ("Decimal", SqlType::Decimal),
1286 ("Char", SqlType::Char),
1287 ("Varchar", SqlType::Varchar),
1288 ("Binary", SqlType::Binary),
1289 ("Varbinary", SqlType::Varbinary),
1290 ("Time", SqlType::Time),
1291 ("Date", SqlType::Date),
1292 ("Timestamp", SqlType::Timestamp),
1293 ("Timestamp_Tz", SqlType::TimestampTz),
1294 ("Interval_Day", SqlType::Interval(IntervalUnit::Day)),
1295 (
1296 "Interval_Day_Hour",
1297 SqlType::Interval(IntervalUnit::DayToHour),
1298 ),
1299 (
1300 "Interval_Day_Minute",
1301 SqlType::Interval(IntervalUnit::DayToMinute),
1302 ),
1303 (
1304 "Interval_Day_Second",
1305 SqlType::Interval(IntervalUnit::DayToSecond),
1306 ),
1307 ("Interval_Hour", SqlType::Interval(IntervalUnit::Hour)),
1308 (
1309 "Interval_Hour_Minute",
1310 SqlType::Interval(IntervalUnit::HourToMinute),
1311 ),
1312 (
1313 "Interval_Hour_Second",
1314 SqlType::Interval(IntervalUnit::HourToSecond),
1315 ),
1316 ("Interval_Minute", SqlType::Interval(IntervalUnit::Minute)),
1317 (
1318 "Interval_Minute_Second",
1319 SqlType::Interval(IntervalUnit::MinuteToSecond),
1320 ),
1321 ("Interval_Month", SqlType::Interval(IntervalUnit::Month)),
1322 ("Interval_Second", SqlType::Interval(IntervalUnit::Second)),
1323 ("Interval_Year", SqlType::Interval(IntervalUnit::Year)),
1324 (
1325 "Interval_Year_Month",
1326 SqlType::Interval(IntervalUnit::YearToMonth),
1327 ),
1328 ("Array", SqlType::Array),
1329 ("Struct", SqlType::Struct),
1330 ("Map", SqlType::Map),
1331 ("Null", SqlType::Null),
1332 ("Variant", SqlType::Variant),
1333 ] {
1334 for sql_str in [
1335 sql_str_base, &sql_str_base.to_lowercase(), &sql_str_base.to_uppercase(), ] {
1339 let value1: SqlType = serde_json::from_str(&format!("\"{}\"", sql_str))
1340 .unwrap_or_else(|e| {
1341 panic!(
1342 "\"{sql_str}\" should deserialize into its SQL type: {}",
1343 e.to_string()
1344 )
1345 });
1346 assert_eq!(value1, expected_value);
1347 let serialized_str =
1348 serde_json::to_string(&value1).expect("Value should serialize into JSON");
1349 let value2: SqlType = serde_json::from_str(&serialized_str).unwrap_or_else(|_| {
1350 panic!(
1351 "{} should deserialize back into its SQL type",
1352 serialized_str
1353 )
1354 });
1355 assert_eq!(value1, value2);
1356 }
1357 }
1358 }
1359
1360 #[test]
1361 fn deserialize_interval_types() {
1362 use super::IntervalUnit::*;
1363 use super::SqlType::*;
1364
1365 let schema = r#"
1366{
1367 "inputs" : [ {
1368 "name" : "sales",
1369 "case_sensitive" : false,
1370 "fields" : [ {
1371 "name" : "sales_id",
1372 "case_sensitive" : false,
1373 "columntype" : {
1374 "type" : "INTEGER",
1375 "nullable" : true
1376 }
1377 }, {
1378 "name" : "customer_id",
1379 "case_sensitive" : false,
1380 "columntype" : {
1381 "type" : "INTEGER",
1382 "nullable" : true
1383 }
1384 }, {
1385 "name" : "age",
1386 "case_sensitive" : false,
1387 "columntype" : {
1388 "type" : "UINTEGER",
1389 "nullable" : true
1390 }
1391 }, {
1392 "name" : "amount",
1393 "case_sensitive" : false,
1394 "columntype" : {
1395 "type" : "DECIMAL",
1396 "nullable" : true,
1397 "precision" : 10,
1398 "scale" : 2
1399 }
1400 }, {
1401 "name" : "sale_date",
1402 "case_sensitive" : false,
1403 "columntype" : {
1404 "type" : "DATE",
1405 "nullable" : true
1406 }
1407 } ],
1408 "primary_key" : [ "sales_id" ]
1409 } ],
1410 "outputs" : [ {
1411 "name" : "salessummary",
1412 "case_sensitive" : false,
1413 "fields" : [ {
1414 "name" : "customer_id",
1415 "case_sensitive" : false,
1416 "columntype" : {
1417 "type" : "INTEGER",
1418 "nullable" : true
1419 }
1420 }, {
1421 "name" : "total_sales",
1422 "case_sensitive" : false,
1423 "columntype" : {
1424 "type" : "DECIMAL",
1425 "nullable" : true,
1426 "precision" : 38,
1427 "scale" : 2
1428 }
1429 }, {
1430 "name" : "interval_day",
1431 "case_sensitive" : false,
1432 "columntype" : {
1433 "type" : "INTERVAL_DAY",
1434 "nullable" : false,
1435 "precision" : 2,
1436 "scale" : 6
1437 }
1438 }, {
1439 "name" : "interval_day_to_hour",
1440 "case_sensitive" : false,
1441 "columntype" : {
1442 "type" : "INTERVAL_DAY_HOUR",
1443 "nullable" : false,
1444 "precision" : 2,
1445 "scale" : 6
1446 }
1447 }, {
1448 "name" : "interval_day_to_minute",
1449 "case_sensitive" : false,
1450 "columntype" : {
1451 "type" : "INTERVAL_DAY_MINUTE",
1452 "nullable" : false,
1453 "precision" : 2,
1454 "scale" : 6
1455 }
1456 }, {
1457 "name" : "interval_day_to_second",
1458 "case_sensitive" : false,
1459 "columntype" : {
1460 "type" : "INTERVAL_DAY_SECOND",
1461 "nullable" : false,
1462 "precision" : 2,
1463 "scale" : 6
1464 }
1465 }, {
1466 "name" : "interval_hour",
1467 "case_sensitive" : false,
1468 "columntype" : {
1469 "type" : "INTERVAL_HOUR",
1470 "nullable" : false,
1471 "precision" : 2,
1472 "scale" : 6
1473 }
1474 }, {
1475 "name" : "interval_hour_to_minute",
1476 "case_sensitive" : false,
1477 "columntype" : {
1478 "type" : "INTERVAL_HOUR_MINUTE",
1479 "nullable" : false,
1480 "precision" : 2,
1481 "scale" : 6
1482 }
1483 }, {
1484 "name" : "interval_hour_to_second",
1485 "case_sensitive" : false,
1486 "columntype" : {
1487 "type" : "INTERVAL_HOUR_SECOND",
1488 "nullable" : false,
1489 "precision" : 2,
1490 "scale" : 6
1491 }
1492 }, {
1493 "name" : "interval_minute",
1494 "case_sensitive" : false,
1495 "columntype" : {
1496 "type" : "INTERVAL_MINUTE",
1497 "nullable" : false,
1498 "precision" : 2,
1499 "scale" : 6
1500 }
1501 }, {
1502 "name" : "interval_minute_to_second",
1503 "case_sensitive" : false,
1504 "columntype" : {
1505 "type" : "INTERVAL_MINUTE_SECOND",
1506 "nullable" : false,
1507 "precision" : 2,
1508 "scale" : 6
1509 }
1510 }, {
1511 "name" : "interval_month",
1512 "case_sensitive" : false,
1513 "columntype" : {
1514 "type" : "INTERVAL_MONTH",
1515 "nullable" : false
1516 }
1517 }, {
1518 "name" : "interval_second",
1519 "case_sensitive" : false,
1520 "columntype" : {
1521 "type" : "INTERVAL_SECOND",
1522 "nullable" : false,
1523 "precision" : 2,
1524 "scale" : 6
1525 }
1526 }, {
1527 "name" : "interval_year",
1528 "case_sensitive" : false,
1529 "columntype" : {
1530 "type" : "INTERVAL_YEAR",
1531 "nullable" : false
1532 }
1533 }, {
1534 "name" : "interval_year_to_month",
1535 "case_sensitive" : false,
1536 "columntype" : {
1537 "type" : "INTERVAL_YEAR_MONTH",
1538 "nullable" : false
1539 }
1540 } ]
1541 } ]
1542}
1543"#;
1544
1545 let schema: super::ProgramSchema = serde_json::from_str(schema).unwrap();
1546 let types = schema
1547 .outputs
1548 .iter()
1549 .flat_map(|r| r.fields.iter().map(|f| f.columntype.typ));
1550 let expected_types = [
1551 Int,
1552 Decimal,
1553 Interval(Day),
1554 Interval(DayToHour),
1555 Interval(DayToMinute),
1556 Interval(DayToSecond),
1557 Interval(Hour),
1558 Interval(HourToMinute),
1559 Interval(HourToSecond),
1560 Interval(Minute),
1561 Interval(MinuteToSecond),
1562 Interval(Month),
1563 Interval(Second),
1564 Interval(Year),
1565 Interval(YearToMonth),
1566 ];
1567
1568 assert_eq!(types.collect::<Vec<_>>(), &expected_types);
1569 }
1570
1571 #[test]
1572 fn serialize_struct_schemas() {
1573 let schema = r#"{
1574 "inputs" : [ {
1575 "name" : "PERS",
1576 "case_sensitive" : false,
1577 "fields" : [ {
1578 "name" : "P0",
1579 "case_sensitive" : false,
1580 "columntype" : {
1581 "fields" : [ {
1582 "type" : "VARCHAR",
1583 "nullable" : true,
1584 "precision" : 30,
1585 "name" : "FIRSTNAME"
1586 }, {
1587 "type" : "VARCHAR",
1588 "nullable" : true,
1589 "precision" : 30,
1590 "name" : "LASTNAME"
1591 }, {
1592 "type" : "UINTEGER",
1593 "nullable" : true,
1594 "name" : "AGE"
1595 }, {
1596 "fields" : {
1597 "fields" : [ {
1598 "type" : "VARCHAR",
1599 "nullable" : true,
1600 "precision" : 30,
1601 "name" : "STREET"
1602 }, {
1603 "type" : "VARCHAR",
1604 "nullable" : true,
1605 "precision" : 30,
1606 "name" : "CITY"
1607 }, {
1608 "type" : "CHAR",
1609 "nullable" : true,
1610 "precision" : 2,
1611 "name" : "STATE"
1612 }, {
1613 "type" : "VARCHAR",
1614 "nullable" : true,
1615 "precision" : 6,
1616 "name" : "POSTAL_CODE"
1617 } ],
1618 "nullable" : false
1619 },
1620 "nullable" : false,
1621 "name" : "ADDRESS"
1622 } ],
1623 "nullable" : false
1624 }
1625 }]
1626 } ],
1627 "outputs" : [ ]
1628}
1629"#;
1630 let schema: super::ProgramSchema = serde_json::from_str(schema).unwrap();
1631 eprintln!("{:#?}", schema);
1632 let pers = schema.inputs.iter().find(|r| r.name == "PERS").unwrap();
1633 let p0 = pers.fields.iter().find(|f| f.name == "P0").unwrap();
1634 assert_eq!(p0.columntype.typ, SqlType::Struct);
1635 let p0_fields = p0.columntype.fields.as_ref().unwrap();
1636 assert_eq!(p0_fields[0].columntype.typ, SqlType::Varchar);
1637 assert_eq!(p0_fields[1].columntype.typ, SqlType::Varchar);
1638 assert_eq!(p0_fields[2].columntype.typ, SqlType::UInt);
1639 assert_eq!(p0_fields[3].columntype.typ, SqlType::Struct);
1640 assert_eq!(p0_fields[3].name, "ADDRESS");
1641 let address = &p0_fields[3].columntype.fields.as_ref().unwrap();
1642 assert_eq!(address.len(), 4);
1643 assert_eq!(address[0].name, "STREET");
1644 assert_eq!(address[0].columntype.typ, SqlType::Varchar);
1645 assert_eq!(address[1].columntype.typ, SqlType::Varchar);
1646 assert_eq!(address[2].columntype.typ, SqlType::Char);
1647 assert_eq!(address[3].columntype.typ, SqlType::Varchar);
1648 }
1649
1650 #[test]
1651 fn sql_identifier_cmp() {
1652 assert_eq!(SqlIdentifier::from("foo"), SqlIdentifier::from("foo"));
1653 assert_ne!(SqlIdentifier::from("foo"), SqlIdentifier::from("bar"));
1654 assert_eq!(SqlIdentifier::from("bar"), SqlIdentifier::from("BAR"));
1655 assert_eq!(SqlIdentifier::from("foo"), SqlIdentifier::from("\"foo\""));
1656 assert_eq!(SqlIdentifier::from("bar"), SqlIdentifier::from("\"bar\""));
1657 assert_eq!(SqlIdentifier::from("bAr"), SqlIdentifier::from("\"bAr\""));
1658 assert_eq!(
1659 SqlIdentifier::new("bAr", true),
1660 SqlIdentifier::from("\"bAr\"")
1661 );
1662
1663 assert_eq!(SqlIdentifier::from("bAr"), "bar");
1664 assert_eq!(SqlIdentifier::from("bAr"), "bAr");
1665 }
1666
1667 #[test]
1668 fn sql_identifier_ord() {
1669 let mut btree = std::collections::BTreeSet::new();
1670 assert!(btree.insert(SqlIdentifier::from("foo")));
1671 assert!(btree.insert(SqlIdentifier::from("bar")));
1672 assert!(!btree.insert(SqlIdentifier::from("BAR")));
1673 assert!(!btree.insert(SqlIdentifier::from("\"foo\"")));
1674 assert!(!btree.insert(SqlIdentifier::from("\"bar\"")));
1675 }
1676
1677 #[test]
1678 fn sql_identifier_hash() {
1679 let mut hs = std::collections::HashSet::new();
1680 assert!(hs.insert(SqlIdentifier::from("foo")));
1681 assert!(hs.insert(SqlIdentifier::from("bar")));
1682 assert!(!hs.insert(SqlIdentifier::from("BAR")));
1683 assert!(!hs.insert(SqlIdentifier::from("\"foo\"")));
1684 assert!(!hs.insert(SqlIdentifier::from("\"bar\"")));
1685 }
1686
1687 #[test]
1688 fn sql_identifier_name() {
1689 assert_eq!(SqlIdentifier::from("foo").name(), "foo");
1690 assert_eq!(SqlIdentifier::from("bAr").name(), "bar");
1691 assert_eq!(SqlIdentifier::from("\"bAr\"").name(), "bAr");
1692 assert_eq!(SqlIdentifier::from("foo").sql_name(), "foo");
1693 assert_eq!(SqlIdentifier::from("bAr").sql_name(), "bAr");
1694 assert_eq!(SqlIdentifier::from("\"bAr\"").sql_name(), "\"bAr\"");
1695 }
1696
1697 #[test]
1698 fn issue3277() {
1699 let schema = r#"{
1700 "name" : "j",
1701 "case_sensitive" : false,
1702 "columntype" : {
1703 "fields" : [ {
1704 "key" : {
1705 "nullable" : false,
1706 "precision" : -1,
1707 "type" : "VARCHAR"
1708 },
1709 "name" : "s",
1710 "nullable" : true,
1711 "type" : "MAP",
1712 "value" : {
1713 "nullable" : true,
1714 "precision" : -1,
1715 "type" : "VARCHAR"
1716 }
1717 } ],
1718 "nullable" : true
1719 }
1720 }"#;
1721 let field: Field = serde_json::from_str(schema).unwrap();
1722 println!("field: {:#?}", field);
1723 assert_eq!(
1724 field,
1725 Field {
1726 name: SqlIdentifier {
1727 name: "j".to_string(),
1728 case_sensitive: false,
1729 },
1730 columntype: ColumnType {
1731 typ: SqlType::Struct,
1732 nullable: true,
1733 precision: None,
1734 scale: None,
1735 component: None,
1736 fields: Some(vec![Field {
1737 name: SqlIdentifier {
1738 name: "s".to_string(),
1739 case_sensitive: false,
1740 },
1741 columntype: ColumnType {
1742 typ: SqlType::Map,
1743 nullable: true,
1744 precision: None,
1745 scale: None,
1746 component: None,
1747 fields: None,
1748 key: Some(Box::new(ColumnType {
1749 typ: SqlType::Varchar,
1750 nullable: false,
1751 precision: Some(-1),
1752 scale: None,
1753 component: None,
1754 fields: None,
1755 key: None,
1756 value: None,
1757 })),
1758 value: Some(Box::new(ColumnType {
1759 typ: SqlType::Varchar,
1760 nullable: true,
1761 precision: Some(-1),
1762 scale: None,
1763 component: None,
1764 fields: None,
1765 key: None,
1766 value: None,
1767 })),
1768 },
1769 lateness: None,
1770 default: None,
1771 unused: false,
1772 }]),
1773 key: None,
1774 value: None,
1775 },
1776 lateness: None,
1777 default: None,
1778 unused: false,
1779 }
1780 );
1781 }
1782}