1use crate::common::{ColumnName, GenericValue, IndexName};
13
14use super::{
15 Aggregate, AggregateOp, Column, ContentHash, Field, FieldSource, FlussoType, IndexMapping,
16 IndexSchema, Mapping, MappingType, Relation, ResolvedField,
17};
18
19impl IndexSchema {
20 pub fn resolve(&self, index: IndexName) -> IndexMapping {
22 resolve_index(index, self)
23 }
24}
25
26fn resolve_index(index: IndexName, schema: &IndexSchema) -> IndexMapping {
27 IndexMapping {
28 index,
29 hash: ContentHash::of(schema),
32 fields: resolve_fields(&schema.fields, schema.primary_key.as_ref()),
33 }
34}
35
36fn resolve_fields(fields: &[Field], primary_key: Option<&ColumnName>) -> Vec<ResolvedField> {
40 fields
41 .iter()
42 .map(|field| resolve_field(field, primary_key))
43 .collect()
44}
45
46fn resolve_field(field: &Field, primary_key: Option<&ColumnName>) -> ResolvedField {
47 let (child_fields, child_pk): (&[Field], Option<&ColumnName>) = match &field.source {
48 FieldSource::Relation(Relation::Join(join)) => (&join.fields, Some(&join.primary_key)),
49 FieldSource::Group(fields) => (fields, primary_key),
50 _ => (&[], primary_key),
51 };
52 let children = resolve_fields(child_fields, child_pk);
53
54 let (mapping_type, nullable, array) = type_and_nullability(field, primary_key);
55 let mapping = Mapping {
56 mapping_type,
57 extra: field.options.clone(),
58 map_values: map_value_type(field),
59 };
60
61 ResolvedField {
62 name: field.field.clone(),
63 mapping,
64 nullable,
65 array,
66 children,
67 }
68}
69
70fn map_value_type(field: &Field) -> Option<MappingType> {
74 match &field.source {
75 FieldSource::Column(Column {
76 ty: FlussoType::Map { values },
77 ..
78 }) => Some(values.opensearch()),
79 _ => None,
80 }
81}
82
83fn type_and_nullability(
86 field: &Field,
87 primary_key: Option<&ColumnName>,
88) -> (MappingType, bool, bool) {
89 match &field.source {
90 FieldSource::Column(Column {
91 column,
92 ty,
93 nullable,
94 default,
95 ..
96 }) => {
97 let forced_non_null = primary_key == Some(column) || default.is_some();
98 (ty.opensearch(), *nullable && !forced_non_null, false)
99 }
100 FieldSource::Group(_) => (MappingType::Object, false, false),
101 FieldSource::Geo(geo) => (
102 MappingType::Other("geo_point".to_owned()),
103 geo.nullable,
104 false,
105 ),
106 FieldSource::Constant(value) => (
107 constant_mapping_type(value),
108 matches!(value, GenericValue::Null),
109 false,
110 ),
111 FieldSource::Relation(Relation::Join(join)) => {
112 let mapping_type = if join.kind.is_to_many() {
113 MappingType::Nested
114 } else {
115 MappingType::Object
116 };
117 (mapping_type, join.nullable, false)
118 }
119 FieldSource::Relation(Relation::Aggregate(aggregate)) => aggregate_type(aggregate),
120 }
121}
122
123fn aggregate_type(aggregate: &Aggregate) -> (MappingType, bool, bool) {
124 match &aggregate.op {
125 AggregateOp::Count => (MappingType::Long, false, false),
126 AggregateOp::Avg(_) => (MappingType::Double, true, false),
127 AggregateOp::Sum(_) | AggregateOp::Min(_) | AggregateOp::Max(_) => {
128 let mapping_type = aggregate
129 .value_type
130 .as_ref()
131 .map(|ty| ty.opensearch())
132 .unwrap_or(MappingType::Double);
135 (mapping_type, true, false)
136 }
137 AggregateOp::Ids { element_type } => (element_type.opensearch(), false, true),
138 }
139}
140
141#[cfg(test)]
142mod tests;
143
144fn constant_mapping_type(value: &GenericValue) -> MappingType {
146 match value {
147 GenericValue::Bool(_) => MappingType::Boolean,
148 GenericValue::SmallInt(_) => MappingType::Short,
149 GenericValue::Int(_) => MappingType::Integer,
150 GenericValue::BigInt(_) => MappingType::Long,
151 GenericValue::Float(_) => MappingType::Float,
152 GenericValue::Double(_) | GenericValue::Decimal(_) => MappingType::Double,
153 GenericValue::Date(_)
154 | GenericValue::Time(_)
155 | GenericValue::Timestamp(_)
156 | GenericValue::TimestampTz(_) => MappingType::Date,
157 GenericValue::Bytes(_) => MappingType::Other("binary".to_owned()),
158 GenericValue::Array(items) => items
159 .first()
160 .map(constant_mapping_type)
161 .unwrap_or(MappingType::Keyword),
162 GenericValue::Map(_) => MappingType::Object,
163 GenericValue::String(_) | GenericValue::Uuid(_) | GenericValue::Null => {
164 MappingType::Keyword
165 }
166 }
167}