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
// Copyright 2018 Flavien Raynaud
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.
//
// Portions of this file are derived from the ToAvro implementation for
// serde_json::Value that is shipped with the avro_rs project. The original
// source code was retrieved on April 25, 2019 from:
//
//     https://github.com/flavray/avro-rs/blob/c4971ac08f52750db6bc95559c2b5faa6c0c9a06/src/types.rs
//
// The original source code is subject to the terms of the MIT license, a copy
// of which can be found in the LICENSE file at the root of this repository.

use std::collections::BTreeMap;
use std::convert::{TryFrom, TryInto};
use std::fmt::Debug;

use anyhow::{anyhow, bail, Context};
use byteorder::{BigEndian, ByteOrder};
use chrono::NaiveDate;
// Re-export components from the various other Avro libraries, so that other
// testdrive modules can import just this one.
pub use mz_avro::schema::{Schema, SchemaKind, SchemaNode, SchemaPiece, SchemaPieceOrNamed};
pub use mz_avro::types::{DecimalValue, ToAvro, Value};
pub use mz_avro::{from_avro_datum, to_avro_datum};
pub use mz_interchange::avro::parse_schema;
use serde_json::Value as JsonValue;

// This function is derived from code in the avro_rs project. Update the license
// header on this file accordingly if you move it to a new home.
pub fn from_json(json: &JsonValue, schema: SchemaNode) -> Result<Value, anyhow::Error> {
    match (json, schema.inner) {
        (JsonValue::Null, SchemaPiece::Null) => Ok(Value::Null),
        (JsonValue::Bool(b), SchemaPiece::Boolean) => Ok(Value::Boolean(*b)),
        (JsonValue::Number(ref n), SchemaPiece::Int) => {
            Ok(Value::Int(n.as_i64().unwrap().try_into()?))
        }
        (JsonValue::Number(ref n), SchemaPiece::Long) => Ok(Value::Long(n.as_i64().unwrap())),
        (JsonValue::Number(ref n), SchemaPiece::Float) => {
            // No other known way to cast an `f64` to an `f32`.
            #[allow(clippy::as_conversions)]
            Ok(Value::Float(n.as_f64().unwrap() as f32))
        }
        (JsonValue::Number(ref n), SchemaPiece::Double) => Ok(Value::Double(n.as_f64().unwrap())),
        (JsonValue::Number(ref n), SchemaPiece::Date) => {
            Ok(Value::Date(i32::try_from(n.as_i64().unwrap())?))
        }
        (JsonValue::Number(ref n), SchemaPiece::TimestampMilli) => {
            let ts = n.as_i64().unwrap();
            Ok(Value::Timestamp(
                chrono::DateTime::from_timestamp_millis(ts)
                    .ok_or(anyhow!("timestamp out of bounds"))?
                    .naive_utc(),
            ))
        }
        (JsonValue::Number(ref n), SchemaPiece::TimestampMicro) => {
            let ts = n.as_i64().unwrap();
            Ok(Value::Timestamp(
                chrono::DateTime::from_timestamp_micros(ts)
                    .ok_or(anyhow!("timestamp out of bounds"))?
                    .naive_utc(),
            ))
        }
        (JsonValue::Array(items), SchemaPiece::Array(inner)) => Ok(Value::Array(
            items
                .iter()
                .map(|x| from_json(x, schema.step(&**inner)))
                .collect::<Result<_, _>>()?,
        )),
        (JsonValue::String(s), SchemaPiece::String) => Ok(Value::String(s.clone())),
        (
            JsonValue::Array(items),
            SchemaPiece::Decimal {
                precision, scale, ..
            },
        ) => {
            let bytes = match items
                .iter()
                .map(|x| x.as_i64().and_then(|x| u8::try_from(x).ok()))
                .collect::<Option<Vec<u8>>>()
            {
                Some(bytes) => bytes,
                None => bail!("decimal was not represented by byte array"),
            };
            Ok(Value::Decimal(DecimalValue {
                unscaled: bytes,
                precision: *precision,
                scale: *scale,
            }))
        }
        (JsonValue::Array(items), SchemaPiece::Fixed { size }) => {
            let bytes = match items
                .iter()
                .map(|x| x.as_i64().and_then(|x| u8::try_from(x).ok()))
                .collect::<Option<Vec<u8>>>()
            {
                Some(bytes) => bytes,
                None => bail!("fixed was not represented by byte array"),
            };
            if *size != bytes.len() {
                bail!("expected fixed size {}, got {}", *size, bytes.len())
            } else {
                Ok(Value::Fixed(*size, bytes))
            }
        }
        (JsonValue::String(s), SchemaPiece::Json) => {
            let j = serde_json::from_str(s)?;
            Ok(Value::Json(j))
        }
        (JsonValue::String(s), SchemaPiece::Uuid) => {
            let u = uuid::Uuid::parse_str(s)?;
            Ok(Value::Uuid(u))
        }
        (JsonValue::String(s), SchemaPiece::Enum { symbols, .. }) => {
            if symbols.contains(s) {
                Ok(Value::String(s.clone()))
            } else {
                bail!("Unrecognized enum variant: {}", s)
            }
        }
        (JsonValue::Object(items), SchemaPiece::Record { .. }) => {
            let mut builder = mz_avro::types::Record::new(schema)
                .expect("`Record::new` should never fail if schema piece is a record!");
            for (key, val) in items {
                let field = builder
                    .field_by_name(key)
                    .ok_or_else(|| anyhow!("No such field {} in record: {}", key, val))?;
                let val = from_json(val, schema.step(&field.schema))?;
                builder.put(key, val);
            }
            Ok(builder.avro())
        }
        (JsonValue::Object(items), SchemaPiece::Map(m)) => {
            let mut map = BTreeMap::new();
            for (k, v) in items {
                let (inner, name) = m.get_piece_and_name(schema.root);
                map.insert(
                    k.to_owned(),
                    from_json(
                        v,
                        SchemaNode {
                            root: schema.root,
                            inner,
                            name,
                        },
                    )?,
                );
            }
            Ok(Value::Map(map))
        }
        (val, SchemaPiece::Union(us)) => {
            let variants = us.variants();
            let null_variant = variants
                .iter()
                .position(|v| v == &SchemaPieceOrNamed::Piece(SchemaPiece::Null));
            if let JsonValue::Null = val {
                return if let Some(nv) = null_variant {
                    Ok(Value::Union {
                        index: nv,
                        inner: Box::new(Value::Null),
                        n_variants: variants.len(),
                        null_variant,
                    })
                } else {
                    bail!("No `null` value in union schema.")
                };
            }
            let items = match val {
                JsonValue::Object(items) => items,
                _ => bail!("Union schema element must be `null` or a map from type name to value; found {:?}", val),
            };
            let (name, val) = if items.len() == 1 {
                (items.keys().next().unwrap(), items.values().next().unwrap())
            } else {
                bail!(
                    "Expected one-element object to match union schema: {:?} vs {:?}",
                    json,
                    schema
                );
            };
            for (i, variant) in variants.iter().enumerate() {
                let name_matches = match variant {
                    SchemaPieceOrNamed::Piece(piece) => SchemaKind::from(piece).name() == name,
                    SchemaPieceOrNamed::Named(idx) => {
                        let schema_name = &schema.root.lookup(*idx).name;
                        if name.chars().any(|ch| ch == '.') {
                            name == &format!(
                                "{}.{}",
                                schema_name.namespace(),
                                schema_name.base_name()
                            )
                        } else {
                            name == schema_name.base_name()
                        }
                    }
                };
                if name_matches {
                    match from_json(val, schema.step(variant)) {
                        Ok(avro) => {
                            return Ok(Value::Union {
                                index: i,
                                inner: Box::new(avro),
                                n_variants: variants.len(),
                                null_variant,
                            })
                        }
                        Err(msg) => return Err(msg),
                    }
                }
            }
            bail!(
                "Type not found in union: {}. variants: {:#?}",
                name,
                variants
            )
        }
        _ => bail!(
            "unable to match JSON value to schema: {:?} vs {:?}",
            json,
            schema
        ),
    }
}

/// Decodes an Avro datum from its Confluent-formatted byte representation.
///
/// The Confluent format includes a verbsion byte, followed by a 32-bit schema
/// ID, followed by the encoded Avro value. This function validates the version
/// byte but ignores the schema ID.
pub fn from_confluent_bytes(schema: &Schema, mut bytes: &[u8]) -> Result<Value, anyhow::Error> {
    if bytes.len() < 5 {
        bail!(
            "avro datum is too few bytes: expected at least 5 bytes, got {}",
            bytes.len()
        );
    }
    let magic = bytes[0];
    let _schema_id = BigEndian::read_i32(&bytes[1..5]);
    bytes = &bytes[5..];

    if magic != 0 {
        bail!(
            "wrong avro serialization magic: expected 0, got {}",
            bytes[0]
        );
    }

    let datum = from_avro_datum(schema, &mut bytes).context("decoding avro datum")?;
    Ok(datum)
}

/// A struct to enhance the debug output of various Avro types.
///
/// Testdrive scripts, for example, specify timestamps in micros, but debug
/// output happens in Y-M-D format, which can be very difficult to map back to
/// the correct input number. Similarly, dates are represented in Avro as
/// `i32`s, but we would like to see the Y-M-D format as well.
#[derive(Clone)]
pub struct DebugValue(pub Value);

impl Debug for DebugValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.0 {
            Value::Timestamp(t) => write!(
                f,
                "Timestamp(\"{:?}\", {} micros, {} millis)",
                t,
                t.and_utc().timestamp_micros(),
                t.and_utc().timestamp_millis()
            ),
            Value::Date(d) => write!(
                f,
                "Date({:?}, \"{}\")",
                d,
                NaiveDate::from_num_days_from_ce_opt(*d).unwrap()
            ),

            // Re-wrap types that contain a Value.
            Value::Record(r) => f
                .debug_set()
                .entries(r.iter().map(|(s, v)| (s, DebugValue(v.clone()))))
                .finish(),
            Value::Array(a) => f
                .debug_set()
                .entries(a.iter().map(|v| DebugValue(v.clone())))
                .finish(),
            Value::Union {
                index,
                inner,
                n_variants,
                null_variant,
            } => f
                .debug_struct("Union")
                .field("index", index)
                .field("inner", &DebugValue(*inner.clone()))
                .field("n_variants", n_variants)
                .field("null_variant", null_variant)
                .finish(),

            _ => write!(f, "{:?}", self.0),
        }
    }
}