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!("\"{sql_str}\" should deserialize into its SQL type: {}", e)
1342 });
1343 assert_eq!(value1, expected_value);
1344 let serialized_str =
1345 serde_json::to_string(&value1).expect("Value should serialize into JSON");
1346 let value2: SqlType = serde_json::from_str(&serialized_str).unwrap_or_else(|_| {
1347 panic!(
1348 "{} should deserialize back into its SQL type",
1349 serialized_str
1350 )
1351 });
1352 assert_eq!(value1, value2);
1353 }
1354 }
1355 }
1356
1357 #[test]
1358 fn deserialize_interval_types() {
1359 use super::IntervalUnit::*;
1360 use super::SqlType::*;
1361
1362 let schema = r#"
1363{
1364 "inputs" : [ {
1365 "name" : "sales",
1366 "case_sensitive" : false,
1367 "fields" : [ {
1368 "name" : "sales_id",
1369 "case_sensitive" : false,
1370 "columntype" : {
1371 "type" : "INTEGER",
1372 "nullable" : true
1373 }
1374 }, {
1375 "name" : "customer_id",
1376 "case_sensitive" : false,
1377 "columntype" : {
1378 "type" : "INTEGER",
1379 "nullable" : true
1380 }
1381 }, {
1382 "name" : "age",
1383 "case_sensitive" : false,
1384 "columntype" : {
1385 "type" : "UINTEGER",
1386 "nullable" : true
1387 }
1388 }, {
1389 "name" : "amount",
1390 "case_sensitive" : false,
1391 "columntype" : {
1392 "type" : "DECIMAL",
1393 "nullable" : true,
1394 "precision" : 10,
1395 "scale" : 2
1396 }
1397 }, {
1398 "name" : "sale_date",
1399 "case_sensitive" : false,
1400 "columntype" : {
1401 "type" : "DATE",
1402 "nullable" : true
1403 }
1404 } ],
1405 "primary_key" : [ "sales_id" ]
1406 } ],
1407 "outputs" : [ {
1408 "name" : "salessummary",
1409 "case_sensitive" : false,
1410 "fields" : [ {
1411 "name" : "customer_id",
1412 "case_sensitive" : false,
1413 "columntype" : {
1414 "type" : "INTEGER",
1415 "nullable" : true
1416 }
1417 }, {
1418 "name" : "total_sales",
1419 "case_sensitive" : false,
1420 "columntype" : {
1421 "type" : "DECIMAL",
1422 "nullable" : true,
1423 "precision" : 38,
1424 "scale" : 2
1425 }
1426 }, {
1427 "name" : "interval_day",
1428 "case_sensitive" : false,
1429 "columntype" : {
1430 "type" : "INTERVAL_DAY",
1431 "nullable" : false,
1432 "precision" : 2,
1433 "scale" : 6
1434 }
1435 }, {
1436 "name" : "interval_day_to_hour",
1437 "case_sensitive" : false,
1438 "columntype" : {
1439 "type" : "INTERVAL_DAY_HOUR",
1440 "nullable" : false,
1441 "precision" : 2,
1442 "scale" : 6
1443 }
1444 }, {
1445 "name" : "interval_day_to_minute",
1446 "case_sensitive" : false,
1447 "columntype" : {
1448 "type" : "INTERVAL_DAY_MINUTE",
1449 "nullable" : false,
1450 "precision" : 2,
1451 "scale" : 6
1452 }
1453 }, {
1454 "name" : "interval_day_to_second",
1455 "case_sensitive" : false,
1456 "columntype" : {
1457 "type" : "INTERVAL_DAY_SECOND",
1458 "nullable" : false,
1459 "precision" : 2,
1460 "scale" : 6
1461 }
1462 }, {
1463 "name" : "interval_hour",
1464 "case_sensitive" : false,
1465 "columntype" : {
1466 "type" : "INTERVAL_HOUR",
1467 "nullable" : false,
1468 "precision" : 2,
1469 "scale" : 6
1470 }
1471 }, {
1472 "name" : "interval_hour_to_minute",
1473 "case_sensitive" : false,
1474 "columntype" : {
1475 "type" : "INTERVAL_HOUR_MINUTE",
1476 "nullable" : false,
1477 "precision" : 2,
1478 "scale" : 6
1479 }
1480 }, {
1481 "name" : "interval_hour_to_second",
1482 "case_sensitive" : false,
1483 "columntype" : {
1484 "type" : "INTERVAL_HOUR_SECOND",
1485 "nullable" : false,
1486 "precision" : 2,
1487 "scale" : 6
1488 }
1489 }, {
1490 "name" : "interval_minute",
1491 "case_sensitive" : false,
1492 "columntype" : {
1493 "type" : "INTERVAL_MINUTE",
1494 "nullable" : false,
1495 "precision" : 2,
1496 "scale" : 6
1497 }
1498 }, {
1499 "name" : "interval_minute_to_second",
1500 "case_sensitive" : false,
1501 "columntype" : {
1502 "type" : "INTERVAL_MINUTE_SECOND",
1503 "nullable" : false,
1504 "precision" : 2,
1505 "scale" : 6
1506 }
1507 }, {
1508 "name" : "interval_month",
1509 "case_sensitive" : false,
1510 "columntype" : {
1511 "type" : "INTERVAL_MONTH",
1512 "nullable" : false
1513 }
1514 }, {
1515 "name" : "interval_second",
1516 "case_sensitive" : false,
1517 "columntype" : {
1518 "type" : "INTERVAL_SECOND",
1519 "nullable" : false,
1520 "precision" : 2,
1521 "scale" : 6
1522 }
1523 }, {
1524 "name" : "interval_year",
1525 "case_sensitive" : false,
1526 "columntype" : {
1527 "type" : "INTERVAL_YEAR",
1528 "nullable" : false
1529 }
1530 }, {
1531 "name" : "interval_year_to_month",
1532 "case_sensitive" : false,
1533 "columntype" : {
1534 "type" : "INTERVAL_YEAR_MONTH",
1535 "nullable" : false
1536 }
1537 } ]
1538 } ]
1539}
1540"#;
1541
1542 let schema: super::ProgramSchema = serde_json::from_str(schema).unwrap();
1543 let types = schema
1544 .outputs
1545 .iter()
1546 .flat_map(|r| r.fields.iter().map(|f| f.columntype.typ));
1547 let expected_types = [
1548 Int,
1549 Decimal,
1550 Interval(Day),
1551 Interval(DayToHour),
1552 Interval(DayToMinute),
1553 Interval(DayToSecond),
1554 Interval(Hour),
1555 Interval(HourToMinute),
1556 Interval(HourToSecond),
1557 Interval(Minute),
1558 Interval(MinuteToSecond),
1559 Interval(Month),
1560 Interval(Second),
1561 Interval(Year),
1562 Interval(YearToMonth),
1563 ];
1564
1565 assert_eq!(types.collect::<Vec<_>>(), &expected_types);
1566 }
1567
1568 #[test]
1569 fn serialize_struct_schemas() {
1570 let schema = r#"{
1571 "inputs" : [ {
1572 "name" : "PERS",
1573 "case_sensitive" : false,
1574 "fields" : [ {
1575 "name" : "P0",
1576 "case_sensitive" : false,
1577 "columntype" : {
1578 "fields" : [ {
1579 "type" : "VARCHAR",
1580 "nullable" : true,
1581 "precision" : 30,
1582 "name" : "FIRSTNAME"
1583 }, {
1584 "type" : "VARCHAR",
1585 "nullable" : true,
1586 "precision" : 30,
1587 "name" : "LASTNAME"
1588 }, {
1589 "type" : "UINTEGER",
1590 "nullable" : true,
1591 "name" : "AGE"
1592 }, {
1593 "fields" : {
1594 "fields" : [ {
1595 "type" : "VARCHAR",
1596 "nullable" : true,
1597 "precision" : 30,
1598 "name" : "STREET"
1599 }, {
1600 "type" : "VARCHAR",
1601 "nullable" : true,
1602 "precision" : 30,
1603 "name" : "CITY"
1604 }, {
1605 "type" : "CHAR",
1606 "nullable" : true,
1607 "precision" : 2,
1608 "name" : "STATE"
1609 }, {
1610 "type" : "VARCHAR",
1611 "nullable" : true,
1612 "precision" : 6,
1613 "name" : "POSTAL_CODE"
1614 } ],
1615 "nullable" : false
1616 },
1617 "nullable" : false,
1618 "name" : "ADDRESS"
1619 } ],
1620 "nullable" : false
1621 }
1622 }]
1623 } ],
1624 "outputs" : [ ]
1625}
1626"#;
1627 let schema: super::ProgramSchema = serde_json::from_str(schema).unwrap();
1628 eprintln!("{:#?}", schema);
1629 let pers = schema.inputs.iter().find(|r| r.name == "PERS").unwrap();
1630 let p0 = pers.fields.iter().find(|f| f.name == "P0").unwrap();
1631 assert_eq!(p0.columntype.typ, SqlType::Struct);
1632 let p0_fields = p0.columntype.fields.as_ref().unwrap();
1633 assert_eq!(p0_fields[0].columntype.typ, SqlType::Varchar);
1634 assert_eq!(p0_fields[1].columntype.typ, SqlType::Varchar);
1635 assert_eq!(p0_fields[2].columntype.typ, SqlType::UInt);
1636 assert_eq!(p0_fields[3].columntype.typ, SqlType::Struct);
1637 assert_eq!(p0_fields[3].name, "ADDRESS");
1638 let address = &p0_fields[3].columntype.fields.as_ref().unwrap();
1639 assert_eq!(address.len(), 4);
1640 assert_eq!(address[0].name, "STREET");
1641 assert_eq!(address[0].columntype.typ, SqlType::Varchar);
1642 assert_eq!(address[1].columntype.typ, SqlType::Varchar);
1643 assert_eq!(address[2].columntype.typ, SqlType::Char);
1644 assert_eq!(address[3].columntype.typ, SqlType::Varchar);
1645 }
1646
1647 #[test]
1648 fn sql_identifier_cmp() {
1649 assert_eq!(SqlIdentifier::from("foo"), SqlIdentifier::from("foo"));
1650 assert_ne!(SqlIdentifier::from("foo"), SqlIdentifier::from("bar"));
1651 assert_eq!(SqlIdentifier::from("bar"), SqlIdentifier::from("BAR"));
1652 assert_eq!(SqlIdentifier::from("foo"), SqlIdentifier::from("\"foo\""));
1653 assert_eq!(SqlIdentifier::from("bar"), SqlIdentifier::from("\"bar\""));
1654 assert_eq!(SqlIdentifier::from("bAr"), SqlIdentifier::from("\"bAr\""));
1655 assert_eq!(
1656 SqlIdentifier::new("bAr", true),
1657 SqlIdentifier::from("\"bAr\"")
1658 );
1659
1660 assert_eq!(SqlIdentifier::from("bAr"), "bar");
1661 assert_eq!(SqlIdentifier::from("bAr"), "bAr");
1662 }
1663
1664 #[test]
1665 fn sql_identifier_ord() {
1666 let mut btree = std::collections::BTreeSet::new();
1667 assert!(btree.insert(SqlIdentifier::from("foo")));
1668 assert!(btree.insert(SqlIdentifier::from("bar")));
1669 assert!(!btree.insert(SqlIdentifier::from("BAR")));
1670 assert!(!btree.insert(SqlIdentifier::from("\"foo\"")));
1671 assert!(!btree.insert(SqlIdentifier::from("\"bar\"")));
1672 }
1673
1674 #[test]
1675 fn sql_identifier_hash() {
1676 let mut hs = std::collections::HashSet::new();
1677 assert!(hs.insert(SqlIdentifier::from("foo")));
1678 assert!(hs.insert(SqlIdentifier::from("bar")));
1679 assert!(!hs.insert(SqlIdentifier::from("BAR")));
1680 assert!(!hs.insert(SqlIdentifier::from("\"foo\"")));
1681 assert!(!hs.insert(SqlIdentifier::from("\"bar\"")));
1682 }
1683
1684 #[test]
1685 fn sql_identifier_name() {
1686 assert_eq!(SqlIdentifier::from("foo").name(), "foo");
1687 assert_eq!(SqlIdentifier::from("bAr").name(), "bar");
1688 assert_eq!(SqlIdentifier::from("\"bAr\"").name(), "bAr");
1689 assert_eq!(SqlIdentifier::from("foo").sql_name(), "foo");
1690 assert_eq!(SqlIdentifier::from("bAr").sql_name(), "bAr");
1691 assert_eq!(SqlIdentifier::from("\"bAr\"").sql_name(), "\"bAr\"");
1692 }
1693
1694 #[test]
1695 fn issue3277() {
1696 let schema = r#"{
1697 "name" : "j",
1698 "case_sensitive" : false,
1699 "columntype" : {
1700 "fields" : [ {
1701 "key" : {
1702 "nullable" : false,
1703 "precision" : -1,
1704 "type" : "VARCHAR"
1705 },
1706 "name" : "s",
1707 "nullable" : true,
1708 "type" : "MAP",
1709 "value" : {
1710 "nullable" : true,
1711 "precision" : -1,
1712 "type" : "VARCHAR"
1713 }
1714 } ],
1715 "nullable" : true
1716 }
1717 }"#;
1718 let field: Field = serde_json::from_str(schema).unwrap();
1719 println!("field: {:#?}", field);
1720 assert_eq!(
1721 field,
1722 Field {
1723 name: SqlIdentifier {
1724 name: "j".to_string(),
1725 case_sensitive: false,
1726 },
1727 columntype: ColumnType {
1728 typ: SqlType::Struct,
1729 nullable: true,
1730 precision: None,
1731 scale: None,
1732 component: None,
1733 fields: Some(vec![Field {
1734 name: SqlIdentifier {
1735 name: "s".to_string(),
1736 case_sensitive: false,
1737 },
1738 columntype: ColumnType {
1739 typ: SqlType::Map,
1740 nullable: true,
1741 precision: None,
1742 scale: None,
1743 component: None,
1744 fields: None,
1745 key: Some(Box::new(ColumnType {
1746 typ: SqlType::Varchar,
1747 nullable: false,
1748 precision: Some(-1),
1749 scale: None,
1750 component: None,
1751 fields: None,
1752 key: None,
1753 value: None,
1754 })),
1755 value: Some(Box::new(ColumnType {
1756 typ: SqlType::Varchar,
1757 nullable: true,
1758 precision: Some(-1),
1759 scale: None,
1760 component: None,
1761 fields: None,
1762 key: None,
1763 value: None,
1764 })),
1765 },
1766 lateness: None,
1767 default: None,
1768 unused: false,
1769 }]),
1770 key: None,
1771 value: None,
1772 },
1773 lateness: None,
1774 default: None,
1775 unused: false,
1776 }
1777 );
1778 }
1779}