queryscript 0.1.4

Queryscript is a SQL-based language that allows you to use higher order abstractions like variables, functions, and modules alongside SQL queries.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use lazy_static::lazy_static;
use snafu::prelude::*;
use std::any::Any;
use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;

use super::{
    error::*,
    inference::mkcref,
    inference::{CRef, Constrainable},
    schema::*,
    sql::get_rowtype,
    Compiler,
};
use crate::ast::SourceLocation;
use crate::compile::coerce::{coerce_types, CoerceOp};
use crate::compile::sql::combine_crefs;
use crate::runtime;
use crate::types;
use crate::types::{AtomicType, Type};

pub trait Generic: Send + Sync + fmt::Debug {
    fn as_any(&self) -> &dyn Any;
    fn name(&self) -> &Ident;

    fn to_runtime_type(&self) -> runtime::error::Result<Type>;
    fn substitute(&self, variables: &BTreeMap<Ident, CRef<MType>>) -> Result<Arc<dyn Generic>>;

    fn unify(&self, other: &MType) -> Result<()>;
    fn get_rowtype(&self, _compiler: crate::compile::Compiler) -> Result<Option<CRef<MType>>> {
        Ok(None)
    }

    fn resolve(&self, loc: &SourceLocation) -> Result<CRef<MType>>;
}

pub trait GenericConstructor: Send + Sync {
    fn static_name() -> &'static Ident;
    fn new(loc: &SourceLocation, args: Vec<CRef<MType>>) -> Result<Arc<dyn Generic>>;
}

pub fn as_generic<T: Generic + 'static>(g: &dyn Generic) -> Option<&T> {
    g.as_any().downcast_ref::<T>()
}

fn debug_fmt_generic(
    f: &mut std::fmt::Formatter<'_>,
    name: &Ident,
    args: Vec<CRef<MType>>,
) -> std::fmt::Result {
    write!(f, "{}<", name)?;
    for (i, arg) in args.into_iter().enumerate() {
        if i > 0 {
            write!(f, ", ")?;
        }

        std::fmt::Debug::fmt(&arg, f)?;
    }
    write!(f, ">")
}

fn validate_args(
    loc: &SourceLocation,
    args: &Vec<CRef<MType>>,
    num: usize,
    name: &Ident,
) -> Result<()> {
    if args.len() != num {
        return Err(CompileError::internal(
            loc.clone(),
            format!("{} expects {} argument", name, num).as_str(),
        ));
    }
    Ok(())
}

pub trait GenericFactory: Send + Sync {
    fn new(&self, loc: &SourceLocation, args: Vec<CRef<MType>>) -> Result<Arc<dyn Generic>>;
    fn name(&self) -> &Ident;
}

pub struct BuiltinGeneric<T: GenericConstructor>(std::marker::PhantomData<T>);

impl<T: GenericConstructor + 'static> BuiltinGeneric<T> {
    pub fn constructor() -> Box<dyn GenericFactory> {
        Box::new(BuiltinGeneric(std::marker::PhantomData::<T>)) as Box<dyn GenericFactory>
    }
}

impl<T: GenericConstructor> GenericFactory for BuiltinGeneric<T> {
    fn new(&self, loc: &SourceLocation, args: Vec<CRef<MType>>) -> Result<Arc<dyn Generic>> {
        T::new(loc, args)
    }

    fn name(&self) -> &Ident {
        T::static_name()
    }
}

// TODO: Some of this boilerplate can be generated by a macro. We may need to create an implementation
// per tuple length...

lazy_static! {
    pub static ref SUM_GENERIC_NAME: Ident = "SumAgg".into();
    pub static ref EXTERNAL_GENERIC_NAME: Ident = "External".into();
    pub static ref CONNECTION_GENERIC_NAME: Ident = "Connection".into();
    pub static ref COERCE_GENERIC_NAME: Ident = "Coerce".into();
    pub static ref GLOBAL_GENERICS: BTreeMap<Ident, Box<dyn GenericFactory>> = [
        BuiltinGeneric::<SumGeneric>::constructor(),
        BuiltinGeneric::<ExternalType>::constructor(),
        BuiltinGeneric::<ConnectionType>::constructor(),
    ]
    .into_iter()
    .map(|builder| (builder.name().clone(), builder))
    .collect::<BTreeMap<Ident, Box<dyn GenericFactory>>>();
}

fn resolve_to_runtime_type<G: Generic + Clone + 'static>(
    loc: &SourceLocation,
    args: Vec<CRef<MType>>,
    g: &G,
) -> Result<CRef<MType>> {
    let loc = loc.clone();
    let resolved_args = args
        .clone()
        .iter()
        .map(|arg| arg.then(|a: Ref<MType>| a.read()?.resolve_generics()))
        .collect::<Result<Vec<_>>>()?;
    let this = g.clone();
    combine_crefs(resolved_args)?.then(move |args: Ref<Vec<Ref<MType>>>| {
        for arg in &*args.read()? {
            // At this point, the arguments are fully known, so if any cannot be converted to
            // runtime types, then they must contain a reference to a type parameter or some other
            // static reason the type cannot be fully known.  In such cases, we'll leave ourselves
            // as a generic.
            if matches!(arg.read()?.to_runtime_type(), Err(_)) {
                return Ok(mkcref(MType::Generic(Located::new(
                    Arc::new(this.clone()),
                    SourceLocation::Unknown,
                ))));
            }
        }
        let rt = this
            .to_runtime_type()
            .context(RuntimeSnafu { loc: loc.clone() })?;

        Ok(mkcref(MType::from_runtime_type(&rt)?))
    })
}

#[derive(Clone)]
pub struct SumGeneric(CRef<MType>);

impl GenericConstructor for SumGeneric {
    fn new(loc: &SourceLocation, mut args: Vec<CRef<MType>>) -> Result<Arc<dyn Generic>> {
        validate_args(loc, &args, 1, Self::static_name())?;
        Ok(Arc::new(SumGeneric(args.swap_remove(0))))
    }

    fn static_name() -> &'static Ident {
        &SUM_GENERIC_NAME
    }
}

impl std::fmt::Debug for SumGeneric {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        debug_fmt_generic(f, Self::static_name(), vec![self.0.clone()])
    }
}

impl Generic for SumGeneric {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn name(&self) -> &Ident {
        Self::static_name()
    }

    fn to_runtime_type(&self) -> runtime::error::Result<types::Type> {
        let arg = self.0.must()?.read()?.to_runtime_type()?;

        // DuckDB's sum function follows the following rules:
        // 	sum(DECIMAL) -> DECIMAL
        //	sum(SMALLINT) -> HUGEINT
        //	sum(INTEGER) -> HUGEINT
        //	sum(BIGINT) -> HUGEINT
        //	sum(HUGEINT) -> HUGEINT
        //	sum(DOUBLE) -> DOUBLE
        match &arg {
            Type::Atom(at) => Ok(Type::Atom(match &at {
                AtomicType::Int8
                | AtomicType::Int16
                | AtomicType::Int32
                | AtomicType::Int64
                | AtomicType::UInt8
                | AtomicType::UInt16
                | AtomicType::UInt32
                | AtomicType::UInt64 => AtomicType::Decimal128(38, 0),
                AtomicType::Float32 | AtomicType::Float64 => AtomicType::Float64,
                AtomicType::Decimal128(..) | AtomicType::Decimal256(..) => at.clone(),
                _ => {
                    return Err(runtime::error::RuntimeError::new(
                        format!(
                            "sum(): expected argument to be a numeric ype, got {:?}",
                            arg
                        )
                        .as_str(),
                    ))
                }
            })),
            _ => {
                return Err(runtime::error::RuntimeError::new(
                    format!(
                        "sum(): expected argument to be an atomic type, got {:?}",
                        arg
                    )
                    .as_str(),
                ))
            }
        }
    }

    fn substitute(&self, variables: &BTreeMap<Ident, CRef<MType>>) -> Result<Arc<dyn Generic>> {
        Ok(Arc::new(Self(self.0.substitute(variables)?)))
    }

    fn unify(&self, other: &MType) -> Result<()> {
        let other = other.clone();
        let arg = self.0.clone();
        arg.constrain(move |arg: Ref<MType>| {
            let arg = arg.read()?.to_runtime_type().context(RuntimeSnafu {
                loc: ErrorLocation::Unknown,
            })?;
            let final_type = MType::from_runtime_type(&arg)?;
            other.unify(&final_type)
        })?;
        Ok(())
    }

    fn resolve(&self, loc: &SourceLocation) -> Result<CRef<MType>> {
        resolve_to_runtime_type(loc, vec![self.0.clone()], self)
    }
}

#[derive(Clone)]
pub struct CoerceGeneric {
    loc: SourceLocation,
    op: CoerceOp,
    args: Vec<CRef<MType>>,
}

impl CoerceGeneric {
    pub fn new(loc: SourceLocation, op: CoerceOp, args: Vec<CRef<MType>>) -> CoerceGeneric {
        CoerceGeneric { loc, op, args }
    }

    fn static_name() -> &'static Ident {
        &COERCE_GENERIC_NAME
    }
}

fn coerce_list(loc: SourceLocation, op: CoerceOp, args: Vec<CRef<MType>>) -> Result<types::Type> {
    if args.len() == 0 {
        return Ok(types::Type::Atom(AtomicType::Null));
    }
    let resolved_args = args
        .iter()
        .map(|arg| -> Result<MType> {
            Ok(arg
                .must()
                .context(RuntimeSnafu { loc: loc.clone() })?
                .read()?
                .clone())
        })
        .collect::<Result<Vec<_>>>()?;
    let runtime_args = resolved_args
        .iter()
        .map(|arg| Ok(arg.to_runtime_type()?))
        .collect::<runtime::error::Result<Vec<_>>>()
        .context(RuntimeSnafu { loc: loc.clone() })?;

    let mut ret = runtime_args[0].clone();
    for arg in &runtime_args[1..] {
        ret = coerce_types(&ret, &op, &arg)
            .ok_or_else(|| CompileError::coercion(loc.clone(), &resolved_args.as_slice()))?;
    }

    Ok(ret)
}

impl std::fmt::Debug for CoerceGeneric {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        debug_fmt_generic(f, Self::static_name(), self.args.clone())
    }
}

impl Generic for CoerceGeneric {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn name(&self) -> &Ident {
        Self::static_name()
    }

    fn substitute(&self, variables: &BTreeMap<Ident, CRef<MType>>) -> Result<Arc<dyn Generic>> {
        Ok(Arc::new(Self {
            loc: self.loc.clone(),
            op: self.op.clone(),
            args: self
                .args
                .iter()
                .map(|arg| arg.substitute(variables))
                .collect::<Result<_>>()?,
        }))
    }

    fn to_runtime_type(&self) -> runtime::error::Result<types::Type> {
        Ok(coerce_list(
            self.loc.clone(),
            self.op.clone(),
            self.args.clone(),
        )?)
    }

    fn unify(&self, other: &MType) -> Result<()> {
        let loc = self.loc.clone();
        let op = self.op.clone();
        let args = self.args.clone();
        let other = other.clone();
        combine_crefs(self.args.clone())?.constrain(move |_: Ref<Vec<Ref<MType>>>| {
            let coerced_type = coerce_list(loc.clone(), op.clone(), args.clone())?;

            MType::unify(&other, &MType::from_runtime_type(&coerced_type)?)?;

            Ok(())
        })?;
        Ok(())
    }

    fn resolve(&self, loc: &SourceLocation) -> Result<CRef<MType>> {
        resolve_to_runtime_type(loc, self.args.clone(), self)
    }
}

#[derive(Clone)]
pub struct ExternalType(CRef<MType>);

impl ExternalType {
    pub fn inner_type(&self) -> CRef<MType> {
        self.0.clone()
    }
}

impl GenericConstructor for ExternalType {
    fn new(loc: &SourceLocation, mut args: Vec<CRef<MType>>) -> Result<Arc<dyn Generic>> {
        validate_args(loc, &args, 1, Self::static_name())?;
        Ok(Arc::new(ExternalType(args.swap_remove(0))))
    }

    fn static_name() -> &'static Ident {
        &EXTERNAL_GENERIC_NAME
    }
}

impl std::fmt::Debug for ExternalType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        debug_fmt_generic(f, Self::static_name(), vec![self.0.clone()])
    }
}

impl Generic for ExternalType {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn name(&self) -> &Ident {
        Self::static_name()
    }

    fn to_runtime_type(&self) -> crate::runtime::error::Result<crate::types::Type> {
        self.0.must()?.read()?.to_runtime_type()
    }

    fn substitute(&self, variables: &BTreeMap<Ident, CRef<MType>>) -> Result<Arc<dyn Generic>> {
        Ok(Arc::new(Self(self.0.substitute(variables)?)))
    }

    fn unify(&self, other: &MType) -> Result<()> {
        let inner_type = &self.0;

        match other {
            MType::Generic(other_inner) => {
                if let Some(other) = as_generic::<Self>(other_inner.get().as_ref()) {
                    inner_type.unify(&other.0)?;
                } else {
                    inner_type.unify(&mkcref(other.clone()))?;
                }
            }
            other => {
                inner_type.unify(&mkcref(other.clone()))?;
            }
        };
        Ok(())
    }

    fn get_rowtype(&self, compiler: Compiler) -> Result<Option<CRef<MType>>> {
        Ok(Some(get_rowtype(compiler, self.0.clone())?))
    }

    fn resolve(&self, loc: &SourceLocation) -> Result<CRef<MType>> {
        resolve_to_runtime_type(loc, vec![self.0.clone()], self)
    }
}

#[derive(Clone)]
pub struct ConnectionType();

impl GenericConstructor for ConnectionType {
    fn new(loc: &SourceLocation, args: Vec<CRef<MType>>) -> Result<Arc<dyn Generic>> {
        validate_args(loc, &args, 0, Self::static_name())?;
        Ok(Arc::new(ConnectionType()))
    }

    fn static_name() -> &'static Ident {
        &CONNECTION_GENERIC_NAME
    }
}

impl std::fmt::Debug for ConnectionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}<>", Self::static_name())
    }
}

impl Generic for ConnectionType {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn name(&self) -> &Ident {
        Self::static_name()
    }

    fn to_runtime_type(&self) -> crate::runtime::error::Result<crate::types::Type> {
        // This is a bit of a hack -- we only really care whether this type is null or not
        Ok(crate::types::Type::Atom(crate::types::AtomicType::Utf8))
    }

    fn substitute(&self, _variables: &BTreeMap<Ident, CRef<MType>>) -> Result<Arc<dyn Generic>> {
        Ok(Arc::new(Self()))
    }

    fn unify(&self, other: &MType) -> Result<()> {
        match other {
            MType::Generic(other_inner) => {
                if let Some(_) = as_generic::<Self>(other_inner.get().as_ref()) {
                    return Ok(());
                }
            }
            _ => {}
        };
        return Err(CompileError::invalid_conn(
            SourceLocation::Unknown,
            "cannot unify a connection type with another kind of type",
        ));
    }

    fn get_rowtype(&self, _compiler: Compiler) -> Result<Option<CRef<MType>>> {
        Err(CompileError::internal(
            SourceLocation::Unknown,
            "connection type should have been optimized away",
        ))
    }

    fn resolve(&self, loc: &SourceLocation) -> Result<CRef<MType>> {
        resolve_to_runtime_type(loc, vec![], self)
    }
}