Skip to main content

rudb_functions/
signature.rs

1//! What a function call resolves to.
2//!
3//! This is the smallest thing the binder cannot be written without: given a name and the types of
4//! the arguments, which function is that and what does it return. It is not the function library.
5//! There is no implementation attached to any of these yet, no volatility, no statistics and no
6//! vectorized kernel, and all of that is what this crate grows into.
7//!
8//! The set here is what M0 reaches, which is the operators the transformer emits plus the five
9//! aggregates a first query needs. A name that is not in it produces DuckDB's own error text rather
10//! than a Rust panic or a silent pass through, because a function that binds and then does nothing
11//! is a wrong answer and a function that does not bind is a message.
12//!
13//! Overload resolution here is by shape rather than by an exact signature match. `+` does not have
14//! one entry per pair of numeric types, it has one entry that says both arguments promote and the
15//! result is what they promote to. DuckDB's own table is closer to the former and it needs to be,
16//! because it carries an implementation per pair. Ours does not carry one yet, and inventing 169
17//! rows before there is a kernel behind any of them would be inventing the wrong 169 rows.
18
19use rudb_common::{Error, LogicalType, MAX_DECIMAL_WIDTH, Result};
20
21/// Whether a name is a scalar function or an aggregate.
22///
23/// The binder needs to ask before it knows which slot the call goes in, since an aggregate is only
24/// legal in an aggregate list and the error for one in the wrong place should say so.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum FunctionKind {
27    /// One row in, one row out.
28    Scalar,
29    /// Many rows in, one row out.
30    Aggregate,
31}
32
33/// A resolved call.
34///
35/// `arguments` is what the arguments have to be cast to and not what they were, so the binder can
36/// insert the casts without redoing the resolution. It is the same length as what was passed in.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct Resolved {
39    /// The function's own name, which is what the plan records.
40    pub name: &'static str,
41    /// Scalar or aggregate.
42    pub kind: FunctionKind,
43    /// What each argument has to be cast to.
44    pub arguments: Vec<LogicalType>,
45    /// What the call produces.
46    pub returns: LogicalType,
47}
48
49/// How the argument types decide the return type.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51enum Shape {
52    /// Every argument promotes to one type and the result is that type. `*` and `min`.
53    Promoted,
54    /// Two arguments, and a decimal product is as wide as both operands together. `*`.
55    Multiplied,
56    /// Every argument promotes to one type, and a decimal promotion becomes a double instead. `//`.
57    ///
58    /// `//` is integer division only when there are integers on both sides of it. Upstream answers
59    /// `7.5 // 2.5` with the DOUBLE 3.0 and `7.9 // 1.0` with 7.9, so it does not truncate what it
60    /// divides once a side is not an integer, and it is `/` under another spelling there. The one
61    /// thing it does not do is go to a double the way `/` does whatever it was given, since
62    /// `7 // 2` is 3 and an INTEGER on both engines, so it cannot share `/`'s shape. A FLOAT stays
63    /// a FLOAT, which was measured, so this is a rule about decimals rather than about width.
64    Divided,
65    /// Every argument promotes and a decimal result gains a digit for the carry. `+` and `-`.
66    ///
67    /// Adding two `DECIMAL(18,0)` produces nineteen digits, so a rule that gives the sum eighteen
68    /// of them is a rule that raises an overflow on the largest inputs it accepts. Only a decimal
69    /// moves: an integer result is the promoted type, since promotion already went to a type that
70    /// holds both, and the unary forms of the two operators do not widen because negating a number
71    /// cannot carry.
72    PromotedWithCarry,
73    /// Every argument promotes to one type and the result is fixed. `=` over anything is boolean.
74    PromotedTo(Fixed),
75    /// Every argument is cast to one fixed type and the result is another. `||` over strings.
76    FixedTo(Fixed, Fixed),
77    /// Every argument has to be that type already and the result is fixed. `lower`, `length`,
78    /// `LIKE`, `chr`.
79    ///
80    /// The difference from [`Shape::FixedTo`] is the word already. DuckDB refuses `lower(123)`,
81    /// `length(DATE '2020-01-01')` and `123 LIKE '1%'` with a binder error naming the overloads it
82    /// does have, and it refuses a BLOB as well, so the rule is VARCHAR rather than anything a cast
83    /// can reach. `||` is the one string function that really does take anything, since `1 || 'a'`
84    /// is `1a` upstream, and it keeps [`Shape::FixedTo`] for that reason.
85    ///
86    /// The argument type is part of the shape because the same rule holds away from strings.
87    /// `chr(col0 INTEGER)` is the only overload upstream has and it refuses `chr(65.9)` and
88    /// `chr(65::BIGINT)` rather than narrowing either of them.
89    Exact(Fixed, Fixed),
90    /// Every argument has to reach that type by widening and the result is fixed. `to_days`.
91    ///
92    /// Between [`Shape::Exact`] and [`Shape::FixedTo`], and it is where the interval constructors
93    /// sit. `to_hours(25)` is an INTEGER reaching a BIGINT and upstream answers it, `to_seconds(1.5)`
94    /// is a DECIMAL reaching a DOUBLE and upstream answers that too, and `to_days(1.7)` is a
95    /// DECIMAL that would have to lose its fraction to reach an INTEGER, which upstream refuses with
96    /// a binder error naming both overloads. So the question is whether promotion gets there and not
97    /// whether the type is already right, and not whether a cast exists, since a cast exists for
98    /// every one of the three.
99    Widened(Fixed, Fixed),
100    /// The arguments are whatever they are and the result is fixed. `count(x)` over anything.
101    AnyTo(Fixed),
102    /// The first `n` arguments are cast to one fixed type, the rest are left alone, and the result
103    /// is fixed. `date_part('minute', x)` reads a part of whatever `x` is, and
104    /// `regexp_extract(s, p, 2)` takes two strings and then a number that has to stay one.
105    LeadingFixedTo(usize, Fixed, Fixed),
106    /// The first argument is cast to one fixed type, the rest are left alone, and the result is the
107    /// last argument's own type. `date_trunc('month', x)` gives back whatever kind of date `x` was.
108    LeadingFixedToLast(Fixed),
109    /// Every argument promotes and an integer result widens to the accumulator. `sum`.
110    Accumulated,
111    /// The first argument is a string or a list and the result is one piece of it. `array_extract`.
112    ///
113    /// The index is a BIGINT and nothing is cast to one, which is upstream's rule rather than an
114    /// omission here: `[1, 2, 3][1.5]` is a binder error there listing the four overloads, so a
115    /// decimal index is refused and not rounded. The bounds of a slice are the other way round,
116    /// which is why that is a shape of its own and not this one with a longer arity.
117    Extracted,
118    /// The first argument is a string or a list, the rest are the bounds, and the result is the first
119    /// argument's own type. `array_slice`.
120    Sliced,
121    /// The first `n` arguments have to be strings already, the rest are indexes, and the result is
122    /// fixed. `substring(s, a, b)` and `overlay(s, r, a, b)`.
123    ///
124    /// An index is a BIGINT and nothing is cast to one, which is the same rule
125    /// [`Shape::Extracted`] follows and is upstream's: `substring('abcdef', 2.5, 3)` is a binder
126    /// error there listing the two overloads rather than a substring from the second character.
127    TextThenIndex(usize, Fixed),
128    /// Every argument promotes, and the result is the first argument's own type. `nullif`.
129    ///
130    /// The promotion is for the comparison and not for the answer, which is what makes this its own
131    /// shape: `typeof(nullif(1, 2.5))` is INTEGER upstream and the comparison behind it is still
132    /// `1 = 2.5`, so the two arguments have to meet somewhere and the answer has to come back from
133    /// where it started. Comparing at the first argument's type instead would round the second one
134    /// and answer `nullif(2, 2.5)` with null.
135    PromotedToFirst,
136    /// One string naming a setting, and the result is whatever type that setting holds.
137    ///
138    /// `current_setting` and nothing else. It is a shape rather than a fixed pair because the pin
139    /// declares the return as `ANY` and then works it out from the name that was passed, which is
140    /// why `typeof(current_setting('threads'))` is BIGINT there and
141    /// `typeof(current_setting('memory_limit'))` is VARCHAR. Both come from one overload.
142    ///
143    /// Resolving this is an error and that is the point of it. The binder folds the call to the
144    /// setting's value before it asks this table anything, so the only way a call arrives here is
145    /// the way the fold cannot happen, which is an argument that is not a constant, and that is the
146    /// case the pin refuses in the same words.
147    Setting,
148}
149
150/// The return types a signature can name outright.
151///
152/// A small enum rather than a `LogicalType` so that the table stays a `const` and there is no
153/// allocation behind a lookup that happens once per expression in every query.
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
155enum Fixed {
156    Boolean,
157    Integer,
158    BigInt,
159    Double,
160    Varchar,
161    Date,
162    Timestamp,
163    Interval,
164}
165
166impl Fixed {
167    fn ty(self) -> LogicalType {
168        match self {
169            Self::Boolean => LogicalType::Boolean,
170            Self::Integer => LogicalType::Integer,
171            Self::BigInt => LogicalType::BigInt,
172            Self::Double => LogicalType::Double,
173            Self::Varchar => LogicalType::Varchar,
174            Self::Date => LogicalType::Date,
175            Self::Timestamp => LogicalType::Timestamp,
176            Self::Interval => LogicalType::Interval,
177        }
178    }
179}
180
181/// How many arguments a function takes.
182///
183/// A range rather than a count because `-` is both the negation and the subtraction, and one name
184/// with two arities is much less trouble than two names that the transformer would have to tell
185/// apart before the binder ever sees the call.
186///
187/// `OneOf` is the range with a hole in it. `make_date` takes one argument or three and not two, and
188/// a range that accepted two would bind a call DuckDB refuses and then have nothing to compute.
189#[derive(Debug, Clone, Copy, PartialEq, Eq)]
190enum Arity {
191    Exactly(usize),
192    Between(usize, Option<usize>),
193    OneOf(&'static [usize]),
194}
195
196impl Arity {
197    const fn exactly(count: usize) -> Self {
198        Self::Exactly(count)
199    }
200
201    const fn between(least: usize, most: usize) -> Self {
202        Self::Between(least, Some(most))
203    }
204
205    const fn at_least(least: usize) -> Self {
206        Self::Between(least, None)
207    }
208
209    const fn one_of(counts: &'static [usize]) -> Self {
210        Self::OneOf(counts)
211    }
212
213    fn accepts(self, count: usize) -> bool {
214        match self {
215            Self::Exactly(wanted) => count == wanted,
216            Self::Between(least, most) => count >= least && most.is_none_or(|most| count <= most),
217            Self::OneOf(counts) => counts.contains(&count),
218        }
219    }
220
221    /// Every count this accepts, with an open end stopped one past where it starts, for the tests
222    /// that hold each row of the table to its own shape at each count it claims to take.
223    #[cfg(test)]
224    fn counts(self) -> Vec<usize> {
225        match self {
226            Self::Exactly(count) => vec![count],
227            Self::Between(least, most) => (least..=most.unwrap_or(least + 1)).collect(),
228            Self::OneOf(counts) => counts.to_vec(),
229        }
230    }
231
232    #[cfg(test)]
233    fn least(self) -> usize {
234        match self {
235            Self::Exactly(count) | Self::Between(count, _) => count,
236            Self::OneOf(counts) => counts.iter().copied().min().unwrap_or(0),
237        }
238    }
239}
240
241struct Entry {
242    name: &'static str,
243    kind: FunctionKind,
244    arity: Arity,
245    shape: Shape,
246    /// Whether every argument has to be a number, which is the only argument constraint M0 needs.
247    numeric_only: bool,
248}
249
250/// The whole table.
251///
252/// One row per name. There are no overloads by argument type in here yet, because every name below
253/// has exactly one shape, and a second row for a name would need a rule for which one wins that is
254/// worth writing when there is a name that needs it.
255const TABLE: &[Entry] = &[
256    // Arithmetic. The result is what the operands promote to, so `INTEGER + BIGINT` is a `BIGINT`
257    // and the executor never has to widen mid expression. `+` and `-` take one argument as well as
258    // two, because the unary forms are the same function and DuckDB names them the same way, and
259    // they are the two that carry: a sum of two decimals needs a digit the operands do not have.
260    number("+", Arity::between(1, 2), Shape::PromotedWithCarry),
261    number("-", Arity::between(1, 2), Shape::PromotedWithCarry),
262    number("*", Arity::exactly(2), Shape::Multiplied),
263    number("%", Arity::exactly(2), Shape::Promoted),
264    // `/` is the exception and it is DuckDB's exception too: `7 / 2` is 3.5 and not 3, so the
265    // result is a double whatever went in, and `//` is the operator that keeps the integer.
266    number("/", Arity::exactly(2), Shape::PromotedTo(Fixed::Double)),
267    number("//", Arity::exactly(2), Shape::Divided),
268    number("abs", Arity::exactly(1), Shape::Promoted),
269    // Strings.
270    // `||` is the one that takes anything and turns it into a string, which is why it is a
271    // `FixedTo` and everything under it is a `Text`. `1 || 'a'` is `1a` upstream.
272    Entry {
273        name: "||",
274        kind: FunctionKind::Scalar,
275        arity: Arity::exactly(2),
276        shape: Shape::FixedTo(Fixed::Varchar, Fixed::Varchar),
277        numeric_only: false,
278    },
279    text("lower", Arity::exactly(1), Fixed::Varchar),
280    text("upper", Arity::exactly(1), Fixed::Varchar),
281    text("length", Arity::exactly(1), Fixed::BigInt),
282    // `strlen` is bytes where `length` is characters, and it is a separate row rather than an alias
283    // for that reason. `strlen('héllo')` is 6 upstream and `length('héllo')` is 5. It is here
284    // because DuckDB's own ClickBench entry writes `AVG(STRLEN(URL))` in query 28, so a rudb
285    // that has only `length` cannot run that board at all without the SQL being changed, and the
286    // whole point of the comparison is that it is not changed.
287    text("strlen", Arity::exactly(1), Fixed::BigInt),
288    // The four SQL string functions that have a grammar rule of their own, plus the aliases upstream
289    // answers the same call with. Each alias is a row rather than a pointer at one, because the
290    // column a query gets back is named after the name that was written: `substr('abcdef', 2)` comes
291    // back as `substr('abcdef', 2)` upstream and not as a substring of anything.
292    Entry {
293        name: "substring",
294        kind: FunctionKind::Scalar,
295        arity: Arity::one_of(&[2, 3]),
296        shape: Shape::TextThenIndex(1, Fixed::Varchar),
297        numeric_only: false,
298    },
299    Entry {
300        name: "substr",
301        kind: FunctionKind::Scalar,
302        arity: Arity::one_of(&[2, 3]),
303        shape: Shape::TextThenIndex(1, Fixed::Varchar),
304        numeric_only: false,
305    },
306    Entry {
307        name: "overlay",
308        kind: FunctionKind::Scalar,
309        arity: Arity::one_of(&[3, 4]),
310        shape: Shape::TextThenIndex(2, Fixed::Varchar),
311        numeric_only: false,
312    },
313    // `left` and `right` count characters and clamp, and a negative count is a count from the other
314    // end rather than an error, so `left('abc', -1)` is `ab`. Both are declared
315    // `(VARCHAR, BIGINT)` upstream and neither casts its count, which is what
316    // [`Shape::TextThenIndex`] already says.
317    Entry {
318        name: "left",
319        kind: FunctionKind::Scalar,
320        arity: Arity::exactly(2),
321        shape: Shape::TextThenIndex(1, Fixed::Varchar),
322        numeric_only: false,
323    },
324    Entry {
325        name: "right",
326        kind: FunctionKind::Scalar,
327        arity: Arity::exactly(2),
328        shape: Shape::TextThenIndex(1, Fixed::Varchar),
329        numeric_only: false,
330    },
331    text("replace", Arity::exactly(3), Fixed::Varchar),
332    // `chr` is a code point and not a byte, so `chr(233)` is one character and not two bytes of
333    // something else. Its one overload upstream takes an INTEGER and it narrows nothing to reach
334    // it: `chr(65::BIGINT)` and `chr(65.9)` are both binder errors there.
335    Entry {
336        name: "chr",
337        kind: FunctionKind::Scalar,
338        arity: Arity::exactly(1),
339        shape: Shape::Exact(Fixed::Integer, Fixed::Varchar),
340        numeric_only: false,
341    },
342    // `concat` takes anything, joins it and drops the nulls instead of propagating them, so
343    // `concat('a', 1, NULL)` is `a1`. That last part is what makes it a third exception to the null
344    // in null out rule, next to `coalesce` and `nullif`, and it is the only one of the three that is
345    // an ordinary function rather than sugar for something else.
346    Entry {
347        name: "concat",
348        kind: FunctionKind::Scalar,
349        arity: Arity::at_least(1),
350        shape: Shape::FixedTo(Fixed::Varchar, Fixed::Varchar),
351        numeric_only: false,
352    },
353    text("position", Arity::exactly(2), Fixed::BigInt),
354    text("strpos", Arity::exactly(2), Fixed::BigInt),
355    text("instr", Arity::exactly(2), Fixed::BigInt),
356    text("trim", Arity::between(1, 2), Fixed::Varchar),
357    text("ltrim", Arity::between(1, 2), Fixed::Varchar),
358    text("rtrim", Arity::between(1, 2), Fixed::Varchar),
359    // Pattern matching. The transformer emits the operator spellings, so those are the names, and
360    // `LIKE` is one of them rather than a keyword the binder has to know about separately.
361    text("~~", Arity::exactly(2), Fixed::Boolean),
362    text("!~~", Arity::exactly(2), Fixed::Boolean),
363    text("~~*", Arity::exactly(2), Fixed::Boolean),
364    text("!~~*", Arity::exactly(2), Fixed::Boolean),
365    // Logic. `AND` and `OR` are conjunctions in the plan rather than calls, so only `NOT` is here.
366    Entry {
367        name: "not",
368        kind: FunctionKind::Scalar,
369        arity: Arity::exactly(1),
370        shape: Shape::FixedTo(Fixed::Boolean, Fixed::Boolean),
371        numeric_only: false,
372    },
373    // `coalesce` promotes across every argument, which is exactly what `Shape::Promoted` says, and
374    // it is the one scalar here that takes a variable number of them.
375    Entry {
376        name: "coalesce",
377        kind: FunctionKind::Scalar,
378        arity: Arity::at_least(1),
379        shape: Shape::Promoted,
380        numeric_only: false,
381    },
382    // `nullif(a, b)` is a macro upstream, `CASE WHEN a = b THEN NULL ELSE a END`, and it is a
383    // function here because the column it produces is named after the call rather than after the
384    // expansion. What that costs is the message for the wrong number of arguments: upstream's is a
385    // binder error about a macro listing `"nullif"(a, b)` under `Candidate macros:`, and the one
386    // below is the ordinary sentence about a function. Both refuse, and the reachable spelling of the
387    // mistake is the quoted `"nullif"(1)`, since the grammar has NULLIF with exactly two arguments
388    // and refuses any other count before the binder sees it.
389    Entry {
390        name: "nullif",
391        kind: FunctionKind::Scalar,
392        arity: Arity::exactly(2),
393        shape: Shape::PromotedToFirst,
394        numeric_only: false,
395    },
396    // Dates and times. `EXTRACT(minute FROM x)` is spelled `date_part('minute', x)` by the time it
397    // gets here, because that is what DuckDB's own parser does with it, so there is one entry for
398    // the two spellings. The part is a string and the thing it is a part of is left alone, which is
399    // what the two leading shapes are for: there is nothing to promote a timestamp towards.
400    // The answer is a double here and a bigint by the time the binder is finished with it, for
401    // every part but the two that carry a fraction. See `narrowed_part` in `rudb-bind`, which is
402    // where the value of the first argument gets to decide the type of the call.
403    Entry {
404        name: "date_part",
405        kind: FunctionKind::Scalar,
406        arity: Arity::exactly(2),
407        shape: Shape::LeadingFixedTo(1, Fixed::Varchar, Fixed::Double),
408        numeric_only: false,
409    },
410    Entry {
411        name: "date_trunc",
412        kind: FunctionKind::Scalar,
413        arity: Arity::exactly(2),
414        shape: Shape::LeadingFixedToLast(Fixed::Varchar),
415        numeric_only: false,
416    },
417    // The gap between two moments counted in calendar fields. Upstream has a one argument form as
418    // well, which measures from today, and it is not here because there is no clock in the engine
419    // yet and a function that invents one would be worse than a function that is missing.
420    //
421    // Widening rather than casting is the whole overload: a DATE widens to a TIMESTAMP and upstream
422    // accepts `age(DATE, DATE)`, while a TIME and an INTERVAL do not widen anywhere and upstream
423    // refuses both of those with a binder error rather than reading them as moments.
424    Entry {
425        name: "age",
426        kind: FunctionKind::Scalar,
427        arity: Arity::exactly(2),
428        shape: Shape::Widened(Fixed::Timestamp, Fixed::Interval),
429        numeric_only: false,
430    },
431    // The two that turn a number into a date and a timestamp, which is how every ClickBench entry
432    // on the board reads that data: the Parquet stores four of its columns as integers and every
433    // query in the set treats them as dates and times. DuckDB's own entry wraps them in exactly
434    // these two calls, so these are what let that entry run here unmodified.
435    //
436    // One argument is days since the epoch and three are a year, a month and a day. Upstream reads
437    // the single one as an INTEGER and the triple as three BIGINTs, and both are INTEGER here,
438    // because the column this is called on is an INTEGER and a widening pass over a hundred million
439    // values to reach a function that immediately narrows again is a pass nobody asked for. The
440    // difference shows on a year that does not fit in an INTEGER, where upstream converts and then
441    // complains about the destination and this complains about the cast.
442    Entry {
443        name: "make_date",
444        kind: FunctionKind::Scalar,
445        arity: Arity::one_of(&[1, 3]),
446        shape: Shape::FixedTo(Fixed::Integer, Fixed::Date),
447        numeric_only: true,
448    },
449    // Milliseconds since the epoch. Upstream also has seven overloads that read a date or a time
450    // and give the milliseconds back, which this table has no way to say yet because it is one row
451    // per name and those pick by argument type. `epoch_ms` of a timestamp is the missing half.
452    Entry {
453        name: "epoch_ms",
454        kind: FunctionKind::Scalar,
455        arity: Arity::exactly(1),
456        shape: Shape::FixedTo(Fixed::BigInt, Fixed::Timestamp),
457        numeric_only: true,
458    },
459    // The thirteen ways to build an interval out of a count of one unit, which is what
460    // `INTERVAL 1 DAY` is once the transformer has rewritten it, and `to_days(1)` written out by
461    // hand is the same call. Eleven of them count whole units and the two that can carry a fraction
462    // take a DOUBLE, so `INTERVAL 2.7 SECOND` is two and seven tenths of a second while
463    // `INTERVAL 1.5 DAY` is one day.
464    //
465    // Upstream declares the eight that land in months or days twice, once over an INTEGER and once
466    // over a BIGINT, and only the first is here, for the reason the head of this table gives: one
467    // row per name, and a second row needs a rule for which one wins. The rewrite always casts to
468    // the width the row below wants, so the literal is unaffected and what is missing is a
469    // handwritten `to_days(3::BIGINT)`, which is refused here and answered there. The three that
470    // land in microseconds have the BIGINT overload and no INTEGER one, so those rows are exact.
471    built("to_years", Fixed::Integer),
472    built("to_months", Fixed::Integer),
473    built("to_quarters", Fixed::Integer),
474    built("to_decades", Fixed::Integer),
475    built("to_centuries", Fixed::Integer),
476    built("to_millennia", Fixed::Integer),
477    built("to_days", Fixed::Integer),
478    built("to_weeks", Fixed::Integer),
479    built("to_hours", Fixed::BigInt),
480    built("to_minutes", Fixed::BigInt),
481    built("to_microseconds", Fixed::BigInt),
482    built("to_seconds", Fixed::Double),
483    built("to_milliseconds", Fixed::Double),
484    // `trunc` is here because the interval rewrite writes it, and it is an ordinary function anybody
485    // can write as well. Upstream has twenty six overloads and every one of them gives back the type
486    // it was handed, which is what `Shape::Promoted` says over one argument. The exception is the
487    // decimal, where upstream drops the scale and gives `DECIMAL(2,0)` for `trunc(1.7)` and this
488    // keeps `DECIMAL(2,1)` holding 1.0, since no shape in this table drops a scale.
489    number("trunc", Arity::exactly(1), Shape::Promoted),
490    // Regular expressions. The pattern is a string like the text is, so three of the four are the
491    // plain string shape. `regexp_extract` is not, because its third argument is the group number
492    // and casting that to a string and reading it back would be a way to accept `'two'`.
493    text("regexp_replace", Arity::between(3, 4), Fixed::Varchar),
494    text("regexp_matches", Arity::between(2, 3), Fixed::Boolean),
495    text("regexp_full_match", Arity::between(2, 3), Fixed::Boolean),
496    Entry {
497        name: "regexp_extract",
498        kind: FunctionKind::Scalar,
499        arity: Arity::between(2, 4),
500        shape: Shape::LeadingFixedTo(2, Fixed::Varchar, Fixed::Varchar),
501        numeric_only: false,
502    },
503    // Subscripting. A bracket is one of these two calls by the time the transformer is done with it,
504    // `x[2]` being `array_extract(x, 2)` and `x[1:2]` being `array_slice(x, 1, 2)`, which is what
505    // DuckDB's own transformer writes as well. Both take a string or a list and give back a piece of
506    // the same thing, so neither one can name its return type here: it is read off the argument.
507    Entry {
508        name: "array_extract",
509        kind: FunctionKind::Scalar,
510        arity: Arity::exactly(2),
511        shape: Shape::Extracted,
512        numeric_only: false,
513    },
514    // Three arguments is a range and four is a range with a step. There is no two argument form,
515    // which is why a slice cannot share the row above: `array_slice([1, 2, 3], 1)` is an arity error
516    // upstream rather than the whole list from the first element on.
517    Entry {
518        name: "array_slice",
519        kind: FunctionKind::Scalar,
520        arity: Arity::between(3, 4),
521        shape: Shape::Sliced,
522        numeric_only: false,
523    },
524    // The type of an expression, as a string. Nothing is cast and nothing runs: the binder folds
525    // this to the name of the type it just decided, so the argument is only ever looked at and the
526    // executor never sees the call.
527    Entry {
528        name: "typeof",
529        kind: FunctionKind::Scalar,
530        arity: Arity::exactly(1),
531        shape: Shape::AnyTo(Fixed::Varchar),
532        numeric_only: false,
533    },
534    // The value of a setting, as a value rather than as a row of `duckdb_settings()`. This is the
535    // second function the binder folds and it folds for the same reason `typeof` does: the answer
536    // is settled once the name is known and nothing about it changes per row. Upstream folds it too
537    // and an `EXPLAIN` of a query that calls it shows the literal, which is what makes an `ANY`
538    // return type resolve to something a plan can carry.
539    Entry {
540        name: "current_setting",
541        kind: FunctionKind::Scalar,
542        arity: Arity::exactly(1),
543        shape: Shape::Setting,
544        numeric_only: false,
545    },
546    // Aggregates.
547    aggregate("count_star", Arity::exactly(0), Shape::AnyTo(Fixed::BigInt), false),
548    aggregate("count", Arity::exactly(1), Shape::AnyTo(Fixed::BigInt), false),
549    aggregate("sum", Arity::exactly(1), Shape::Accumulated, true),
550    aggregate("avg", Arity::exactly(1), Shape::PromotedTo(Fixed::Double), true),
551    aggregate("min", Arity::exactly(1), Shape::Promoted, false),
552    aggregate("max", Arity::exactly(1), Shape::Promoted, false),
553];
554
555/// A scalar that takes numbers.
556const fn number(name: &'static str, arity: Arity, shape: Shape) -> Entry {
557    Entry { name, kind: FunctionKind::Scalar, arity, shape, numeric_only: true }
558}
559
560/// A scalar that takes strings and returns `returns`.
561const fn text(name: &'static str, arity: Arity, returns: Fixed) -> Entry {
562    Entry {
563        name,
564        kind: FunctionKind::Scalar,
565        arity,
566        shape: Shape::Exact(Fixed::Varchar, returns),
567        numeric_only: false,
568    }
569}
570
571/// An interval constructor, which takes one count of one unit and gives back an interval.
572const fn built(name: &'static str, count: Fixed) -> Entry {
573    Entry {
574        name,
575        kind: FunctionKind::Scalar,
576        arity: Arity::exactly(1),
577        shape: Shape::Widened(count, Fixed::Interval),
578        numeric_only: false,
579    }
580}
581
582const fn aggregate(name: &'static str, arity: Arity, shape: Shape, numeric_only: bool) -> Entry {
583    Entry { name, kind: FunctionKind::Aggregate, arity, shape, numeric_only }
584}
585
586/// Whether a name is a function at all, and which kind.
587///
588/// The binder asks this before it knows what to do with a call, since `count(x)` in a projection
589/// has to become an error naming the aggregate rather than a lookup failure naming the name.
590#[must_use]
591pub fn kind_of(name: &str) -> Option<FunctionKind> {
592    find(name).map(|entry| entry.kind)
593}
594
595/// What `date_part` answers with when the specifier is known at binding time.
596///
597/// `epoch` counts seconds and `julian` counts days, and both of them carry a fraction, so those two
598/// are doubles and every other part is a whole number. A specifier that names no part at all is a
599/// double as well, since the call is going to fail anyway and the sentence about it belongs to the
600/// one place that knows every spelling.
601///
602/// This is the only place a call's type comes from the value of an argument rather than the type of
603/// one, and it is upstream's rule rather than an optimization: the declared overload there is a
604/// double and the binder narrows it, which is why `date_part(p, ts)` over a column of specifiers is
605/// a double even when every row of it says `year`.
606#[must_use]
607pub fn part_type(spelling: &str) -> LogicalType {
608    let fraction = ["epoch", "julian", "jd"];
609    if fraction.iter().any(|name| name.eq_ignore_ascii_case(spelling)) {
610        LogicalType::Double
611    } else {
612        LogicalType::BigInt
613    }
614}
615
616/// Resolves a call.
617///
618/// # Errors
619///
620/// If there is no function of that name, if the argument count is wrong, if an argument is not a
621/// number where the function needs one, or if the arguments have no type in common. The messages
622/// are DuckDB's, since a great deal of code in the wild asserts on them.
623pub fn resolve(name: &str, arguments: &[LogicalType]) -> Result<Resolved> {
624    let entry = find(name).ok_or_else(|| {
625        Error::catalog(format!("Scalar Function with name {name} does not exist!"))
626    })?;
627    if !entry.arity.accepts(arguments.len()) {
628        return Err(no_match(entry.name, arguments));
629    }
630    if let Some((cast_to, returns)) = temporal(entry.name, arguments) {
631        return Ok(Resolved { name: entry.name, kind: entry.kind, arguments: cast_to, returns });
632    }
633    if entry.numeric_only {
634        for ty in arguments {
635            // A null literal has no type yet and every function accepts one, since the alternative
636            // is that `sum(NULL)` fails to bind rather than returning null.
637            if !ty.is_numeric() && *ty != LogicalType::Null {
638                return Err(Error::binder(format!(
639                    "No function matches the given name and argument types '{name}({ty})'. You might need to add explicit type casts."
640                )));
641            }
642        }
643    }
644    let (cast_to, returns) = match entry.shape {
645        Shape::Promoted => {
646            let common = promote_all(name, arguments)?;
647            (vec![common.clone(); arguments.len()], common)
648        }
649        Shape::Multiplied => {
650            let common = promote_all(name, arguments)?;
651            match product(arguments)? {
652                // Each side keeps its own scale and takes the answer's width, so the two runs are
653                // the same physical type and the unscaled values multiply into the answer with no
654                // rescaling anywhere. That is what the decimal loop in rudb-kernels expects.
655                Some(LogicalType::Decimal { width, scale }) => {
656                    let cast_to = arguments
657                        .iter()
658                        .map(|ty| match ty.decimal_shape() {
659                            Some((_, held)) => LogicalType::Decimal { width, scale: held },
660                            None => ty.clone(),
661                        })
662                        .collect();
663                    (cast_to, LogicalType::Decimal { width, scale })
664                }
665                _ => (vec![common.clone(); arguments.len()], common),
666            }
667        }
668        Shape::Divided => {
669            let common = promote_all(name, arguments)?;
670            // The cast goes with the answer rather than being left where promotion put it, because
671            // a decimal run divided as a decimal and then widened to a double is not the same
672            // number as the same pair of values divided as doubles.
673            let returns = match common {
674                LogicalType::Decimal { .. } => LogicalType::Double,
675                other => other,
676            };
677            (vec![returns.clone(); arguments.len()], returns)
678        }
679        Shape::PromotedWithCarry => {
680            let common = promote_all(name, arguments)?;
681            // One argument is a negation or a unary plus, and neither one can carry. Negating the
682            // smallest value of a type is the exception and it is not a signature's to take, since
683            // the type of the answer depends on the value: the constant folder widens that one
684            // value by a step, per #264, and the signature says the same thing here as upstream's
685            // does.
686            let returns = if arguments.len() > 1 { carrying(common) } else { common };
687            (vec![returns.clone(); arguments.len()], returns)
688        }
689        Shape::PromotedTo(fixed) => {
690            let common = promote_all(name, arguments)?;
691            (vec![common; arguments.len()], fixed.ty())
692        }
693        Shape::FixedTo(argument, result) => (vec![argument.ty(); arguments.len()], result.ty()),
694        Shape::Exact(argument, result) => {
695            let wanted = argument.ty();
696            for ty in arguments {
697                // An untyped null is accepted the way it is everywhere else here. DuckDB answers
698                // `length(NULL)` with NULL rather than refusing it, because a null has no type to
699                // pick an overload with and every overload would return null anyway.
700                if *ty != wanted && *ty != LogicalType::Null {
701                    return Err(no_match(entry.name, arguments));
702                }
703            }
704            (vec![wanted; arguments.len()], result.ty())
705        }
706        Shape::Widened(argument, result) => {
707            let wanted = argument.ty();
708            for ty in arguments {
709                // A null is accepted here for the reason it is accepted above, and it is the only
710                // type that does not have to promote anywhere, since it has nothing to promote.
711                if *ty != LogicalType::Null && ty.promote(&wanted).as_ref() != Some(&wanted) {
712                    return Err(no_match(entry.name, arguments));
713                }
714            }
715            (vec![wanted; arguments.len()], result.ty())
716        }
717        Shape::AnyTo(result) => (arguments.to_vec(), result.ty()),
718        Shape::LeadingFixedTo(count, first, result) => {
719            (leading(count, first, arguments), result.ty())
720        }
721        Shape::LeadingFixedToLast(first) => {
722            // A null literal has no type and DuckDB refuses `date_trunc('month', NULL)` outright,
723            // because it cannot tell the date overload from the interval one. Refusing needs a
724            // table with both overloads in it to refuse from, which this is not yet, so the answer
725            // is the widest of the candidates rather than a message about a choice nobody made.
726            let last = match arguments.last() {
727                Some(LogicalType::Null) | None => LogicalType::Timestamp,
728                Some(ty) => ty.clone(),
729            };
730            (leading(1, first, arguments), last)
731        }
732        Shape::Accumulated => {
733            let common = promote_all(name, arguments)?;
734            let returns = accumulator(&common);
735            (vec![common; arguments.len()], returns)
736        }
737        Shape::Extracted => {
738            let target = &arguments[0];
739            let index = &arguments[1];
740            let Some(element) = element_of(target) else {
741                return Err(no_match(entry.name, arguments));
742            };
743            if !index.is_integer() && *index != LogicalType::Null {
744                return Err(no_match(entry.name, arguments));
745            }
746            (vec![target.clone(), LogicalType::BigInt], element)
747        }
748        Shape::Sliced => {
749            let target = &arguments[0];
750            if element_of(target).is_none() {
751                // Upstream's own sentence, shouted, and it is the same sentence whichever of the two
752                // spellings the call was written with.
753                return Err(Error::binder("ARRAY_SLICE can only operate on LISTs and VARCHARs"));
754            }
755            // A step is declared BIGINT and so it is not cast to one either, while the two bounds
756            // are declared ANY and are: `array_slice([1, 2, 3], 1.5, 2)` is `[2]` upstream, rounded,
757            // and `array_slice([1, 2, 3], 1, 2, 1.5)` is a binder error.
758            if let Some(step) = arguments.get(3) {
759                if !step.is_integer() && *step != LogicalType::Null {
760                    return Err(no_match(entry.name, arguments));
761                }
762            }
763            let mut cast_to = vec![LogicalType::BigInt; arguments.len()];
764            cast_to[0] = target.clone();
765            (cast_to, target.clone())
766        }
767        Shape::TextThenIndex(count, result) => {
768            let (text, indexes) = arguments.split_at(count.min(arguments.len()));
769            for ty in text {
770                if *ty != LogicalType::Varchar && *ty != LogicalType::Null {
771                    return Err(no_match(entry.name, arguments));
772                }
773            }
774            for ty in indexes {
775                if !ty.is_integer() && *ty != LogicalType::Null {
776                    return Err(no_match(entry.name, arguments));
777                }
778            }
779            let mut cast_to = vec![LogicalType::BigInt; arguments.len()];
780            for slot in &mut cast_to[..text.len()] {
781                *slot = LogicalType::Varchar;
782            }
783            (cast_to, result.ty())
784        }
785        Shape::PromotedToFirst => {
786            let common = promote_all(name, arguments)?;
787            // An untyped null keeps nothing to hand back, so it takes the promoted type the way
788            // every other shape here does. Upstream says NULL for `typeof(nullif(NULL, NULL))`
789            // because it has a type for a null literal and this engine does not, which is #244.
790            let first = &arguments[0];
791            let returns = if *first == LogicalType::Null { common.clone() } else { first.clone() };
792            (vec![common; arguments.len()], returns)
793        }
794        // Reaching here means the binder could not fold the call, and the only reason it cannot is
795        // an argument that is not a constant. The pin says exactly this and names the parameter.
796        Shape::Setting => {
797            return Err(Error::binder(format!(
798                "The \"setting_name\" argument in function \"{}\" must be a constant expression",
799                entry.name
800            )));
801        }
802    };
803    Ok(Resolved { name: entry.name, kind: entry.kind, arguments: cast_to, returns })
804}
805
806/// What the arithmetic operators return when a date, a time, a timestamp or an interval is one of
807/// the arguments.
808///
809/// The one place in this file where the argument types pick the overload rather than the name
810/// picking one shape. The table above says a name has exactly one shape and that a second row for
811/// a name needs a rule for which one wins, and this is the rule: a date, a time, a timestamp or an
812/// interval next to one of those, or next to a number, is temporal arithmetic, and everything else
813/// is the numeric row. The answer is the types to cast the arguments to and the type that comes
814/// back.
815///
816/// A date plus an interval is a timestamp and not a date, because the interval carries a time of
817/// day. A time plus an interval is a time, since the months and the days have nowhere to go and it
818/// wraps at midnight. Taking a date off an interval is not a thing on either engine, so only the
819/// commuted addition is here.
820///
821/// Two intervals add and subtract field by field, and a number scales one, in either order for the
822/// multiplication and with the interval on the left for the division.
823///
824/// The multiplication has two overloads of its own and the difference between them shows. A whole
825/// number goes in as a `BIGINT` and multiplies the three fields as they are, and everything else
826/// goes in as a `DOUBLE` and moves what is left over on a field down to the next one. `HUGEINT` and
827/// `UBIGINT` take the double as well, since neither of them fits a `BIGINT` to begin with. Dividing
828/// has only the double, which is why an integer count divided into an interval reports its division
829/// by zero as `0.0`.
830///
831/// A plain number next to a date is a count of days and the answer stays a date, which is the one
832/// shape here that does not become a timestamp. The count is an `INTEGER` and nothing wider, so a
833/// `BIGINT` next to a date has no overload to reach at all, and taking a date off a number is not a
834/// thing. One date taken off another is a count of days as a `BIGINT` and one timestamp taken off
835/// another is an interval, and a date on either side of that subtraction becomes a timestamp first.
836/// A date plus a time is the timestamp they name together, in either order, and taking a time off a
837/// date is refused upstream.
838///
839/// An untyped null next to a date is the count of days and next to a timestamp is the interval,
840/// which is measured rather than picked: `typeof(DATE '2020-01-01' + NULL)` is `DATE` and
841/// `typeof(TIMESTAMP '2020-01-01' - NULL)` is `TIMESTAMP`. A null next to a time or next to an
842/// interval is ambiguous upstream and refused, which we refuse too, with the wrong sentence for now
843/// because the sentence for an ambiguous call is #395.
844fn temporal(name: &str, arguments: &[LogicalType]) -> Option<(Vec<LogicalType>, LogicalType)> {
845    use LogicalType::{
846        BigInt, Date, Double, HugeInt, Integer, Interval, Null, SmallInt, Time, Timestamp, TinyInt,
847        UBigInt, UHugeInt, USmallInt, UTinyInt,
848    };
849    let kept = |returns| Some((arguments.to_vec(), returns));
850    // A null literal has no type yet, so it counts as the number and the cast to a double is what
851    // turns the whole call into a null.
852    let number = |ty: &LogicalType| ty.is_numeric() || *ty == Null;
853    let counted = |ty: &LogicalType| ty.is_integer() && !matches!(ty, HugeInt | UHugeInt | UBigInt);
854    // The days a date moves by are an `INTEGER`, so this is the set of types that widen into one.
855    let days =
856        |ty: &LogicalType| matches!(ty, TinyInt | SmallInt | Integer | UTinyInt | USmallInt | Null);
857    match (name, arguments) {
858        ("-", [Interval]) => kept(Interval),
859        ("+" | "-", [Date | Timestamp, Interval]) | ("+", [Interval, Date | Timestamp]) => {
860            kept(Timestamp)
861        }
862        ("+" | "-", [Time, Interval]) | ("+", [Interval, Time]) => kept(Time),
863        ("+" | "-", [Interval, Interval]) => kept(Interval),
864        ("-", [Date, Date]) => kept(BigInt),
865        ("-", [Timestamp, Timestamp]) => kept(Interval),
866        ("-", [Date, Timestamp] | [Timestamp, Date]) => {
867            Some((vec![Timestamp, Timestamp], Interval))
868        }
869        ("+", [Date, Time] | [Time, Date]) => kept(Timestamp),
870        ("+" | "-", [Date, count]) if days(count) => Some((vec![Date, Integer], Date)),
871        ("+", [count, Date]) if days(count) => Some((vec![Integer, Date], Date)),
872        ("+" | "-", [Timestamp, Null]) | ("+", [Null, Timestamp]) => kept(Timestamp),
873        ("*", [Interval, count]) if counted(count) => Some((vec![Interval, BigInt], Interval)),
874        ("*", [count, Interval]) if counted(count) => Some((vec![BigInt, Interval], Interval)),
875        ("*" | "/", [Interval, scale]) if number(scale) => Some((vec![Interval, Double], Interval)),
876        ("*", [scale, Interval]) if number(scale) => Some((vec![Double, Interval], Interval)),
877        _ => None,
878    }
879}
880
881/// The error for a call that names a real function and does not fit any of its overloads.
882///
883/// The sentence is DuckDB's, and so is the block under it when there is one. A message that says a
884/// call does not match without saying what would match is a message that sends somebody to the
885/// documentation, and the whole argument for copying the reference's errors is that a program
886/// written against one engine should not have to be debugged differently against the other.
887///
888/// The trailing newline is the reference's too. Its message ends after the last candidate with a
889/// line break, which is visible as the second blank line before the shell prints the offending SQL.
890fn no_match(name: &str, arguments: &[LogicalType]) -> Error {
891    let types = arguments.iter().map(ToString::to_string).collect::<Vec<_>>().join(", ");
892    let mut message = format!(
893        "No function matches the given name and argument types '{name}({types})'. You might need to add explicit type casts."
894    );
895    if let Some((_, overloads)) = CANDIDATES.iter().find(|(entry, _)| *entry == name) {
896        message.push_str("\n\tCandidate functions:");
897        for overload in *overloads {
898            message.push_str("\n\t");
899            message.push_str(overload);
900        }
901        message.push('\n');
902    }
903    Error::binder(message)
904}
905
906/// What the reference prints under `Candidate functions:`, per function, byte for byte.
907///
908/// Copied off the pinned binary rather than generated from [`TABLE`], because it is not derivable
909/// from what rudb has. The parameters are called `col0` and `col1` for some functions and `string`,
910/// `regex` and a quoted `"options"` for others, an operator is quoted where a plain name is not, and
911/// `length` lists three overloads of which rudb has one. That last one is the argument for copying
912/// rather than deriving: the list is what DuckDB accepts, rudb is meant to accept the same, and a
913/// list that shrank to what is built today would have to be edited every time a gap closes.
914///
915/// A name missing from here gets the sentence with no block under it, which is what every function
916/// outside the string family does today.
917const CANDIDATES: &[(&str, &[&str])] = &[
918    ("lower", &["lower(col0 VARCHAR) -> VARCHAR"]),
919    ("upper", &["upper(col0 VARCHAR) -> VARCHAR"]),
920    (
921        "length",
922        &[
923            "length(col0 VARCHAR) -> BIGINT",
924            "length(col0 BIT) -> BIGINT",
925            "length(col0 ANY[]) -> BIGINT",
926        ],
927    ),
928    ("strlen", &["strlen(col0 VARCHAR) -> BIGINT"]),
929    ("chr", &["chr(col0 INTEGER) -> VARCHAR"]),
930    ("left", &["\"left\"(col0 VARCHAR, col1 BIGINT) -> VARCHAR"]),
931    ("right", &["\"right\"(col0 VARCHAR, col1 BIGINT) -> VARCHAR"]),
932    ("replace", &["\"replace\"(col0 VARCHAR, col1 VARCHAR, col2 VARCHAR) -> VARCHAR"]),
933    // The one overload upstream prints with a repeated parameter in it, which is how it writes a
934    // variadic. Reachable with no arguments at all, since the grammar has nothing to say about the
935    // count of an ordinary call.
936    ("concat", &["concat(col0 ANY, [ANY...]) -> ANY"]),
937    (
938        "substring",
939        &[
940            "\"substring\"(col0 VARCHAR, col1 BIGINT, col2 BIGINT) -> VARCHAR",
941            "\"substring\"(col0 VARCHAR, col1 BIGINT) -> VARCHAR",
942        ],
943    ),
944    (
945        "substr",
946        &[
947            "substr(col0 VARCHAR, col1 BIGINT, col2 BIGINT) -> VARCHAR",
948            "substr(col0 VARCHAR, col1 BIGINT) -> VARCHAR",
949        ],
950    ),
951    (
952        "overlay",
953        &[
954            "\"overlay\"(col0 VARCHAR, col1 VARCHAR, col2 BIGINT) -> VARCHAR",
955            "\"overlay\"(col0 VARCHAR, col1 VARCHAR, col2 BIGINT, col3 BIGINT) -> VARCHAR",
956        ],
957    ),
958    ("position", &["\"position\"(col0 VARCHAR, col1 VARCHAR) -> BIGINT"]),
959    ("strpos", &["strpos(col0 VARCHAR, col1 VARCHAR) -> BIGINT"]),
960    ("instr", &["instr(col0 VARCHAR, col1 VARCHAR) -> BIGINT"]),
961    (
962        "trim",
963        &["\"trim\"(col0 VARCHAR) -> VARCHAR", "\"trim\"(col0 VARCHAR, col1 VARCHAR) -> VARCHAR"],
964    ),
965    ("ltrim", &["ltrim(col0 VARCHAR) -> VARCHAR", "ltrim(col0 VARCHAR, col1 VARCHAR) -> VARCHAR"]),
966    ("rtrim", &["rtrim(col0 VARCHAR) -> VARCHAR", "rtrim(col0 VARCHAR, col1 VARCHAR) -> VARCHAR"]),
967    ("~~", &["\"~~\"(col0 VARCHAR, col1 VARCHAR) -> BOOLEAN"]),
968    ("!~~", &["\"!~~\"(col0 VARCHAR, col1 VARCHAR) -> BOOLEAN"]),
969    ("~~*", &["\"~~*\"(col0 VARCHAR, col1 VARCHAR) -> BOOLEAN"]),
970    ("!~~*", &["\"!~~*\"(col0 VARCHAR, col1 VARCHAR) -> BOOLEAN"]),
971    (
972        "regexp_replace",
973        &[
974            "regexp_replace(string VARCHAR, regex VARCHAR, replacement VARCHAR) -> VARCHAR",
975            "regexp_replace(string VARCHAR, regex VARCHAR, replacement VARCHAR, \"options\" VARCHAR) -> VARCHAR",
976        ],
977    ),
978    (
979        "regexp_matches",
980        &[
981            "regexp_matches(string VARCHAR, regex VARCHAR) -> BOOLEAN",
982            "regexp_matches(string VARCHAR, regex VARCHAR, \"options\" VARCHAR) -> BOOLEAN",
983        ],
984    ),
985    (
986        "regexp_full_match",
987        &[
988            "regexp_full_match(string VARCHAR, regex VARCHAR) -> BOOLEAN",
989            "regexp_full_match(string VARCHAR, regex VARCHAR, \"options\" VARCHAR) -> BOOLEAN",
990        ],
991    ),
992    // Both overloads of each interval constructor, including the BIGINT one this engine does not
993    // have a row for, because the list is what DuckDB accepts and somebody reading it is being told
994    // what to write rather than what is built here.
995    ("to_years", &["to_years(col0 INTEGER) -> INTERVAL", "to_years(col0 BIGINT) -> INTERVAL"]),
996    ("to_months", &["to_months(col0 INTEGER) -> INTERVAL", "to_months(col0 BIGINT) -> INTERVAL"]),
997    (
998        "to_quarters",
999        &["to_quarters(col0 INTEGER) -> INTERVAL", "to_quarters(col0 BIGINT) -> INTERVAL"],
1000    ),
1001    (
1002        "to_decades",
1003        &["to_decades(col0 INTEGER) -> INTERVAL", "to_decades(col0 BIGINT) -> INTERVAL"],
1004    ),
1005    (
1006        "to_centuries",
1007        &["to_centuries(col0 INTEGER) -> INTERVAL", "to_centuries(col0 BIGINT) -> INTERVAL"],
1008    ),
1009    (
1010        "to_millennia",
1011        &["to_millennia(col0 INTEGER) -> INTERVAL", "to_millennia(col0 BIGINT) -> INTERVAL"],
1012    ),
1013    ("to_days", &["to_days(col0 INTEGER) -> INTERVAL", "to_days(col0 BIGINT) -> INTERVAL"]),
1014    ("to_weeks", &["to_weeks(col0 INTEGER) -> INTERVAL", "to_weeks(col0 BIGINT) -> INTERVAL"]),
1015    // The five that have one overload each, which is why they are not in the pattern above. The
1016    // three that land in microseconds are declared over a BIGINT and never over an INTEGER, since
1017    // an hour of INTEGER hours does not fit the field anyway.
1018    ("to_hours", &["to_hours(col0 BIGINT) -> INTERVAL"]),
1019    ("to_minutes", &["to_minutes(col0 BIGINT) -> INTERVAL"]),
1020    ("to_microseconds", &["to_microseconds(col0 BIGINT) -> INTERVAL"]),
1021    ("to_seconds", &["to_seconds(col0 DOUBLE) -> INTERVAL"]),
1022    ("to_milliseconds", &["to_milliseconds(col0 DOUBLE) -> INTERVAL"]),
1023    // Four overloads of which this engine has two. The STRUCT one is `x.y`, which the transformer
1024    // writes as `struct_extract`, and a TUPLE is the positional half of the same idea.
1025    (
1026        "array_extract",
1027        &[
1028            "array_extract(\"array\" T[], \"index\" BIGINT) -> T",
1029            "array_extract(col0 VARCHAR, col1 BIGINT) -> VARCHAR",
1030            "array_extract(\"struct\" STRUCT, \"key\" VARCHAR) -> ANY",
1031            "array_extract(\"tuple\" TUPLE, \"index\" BIGINT) -> ANY",
1032        ],
1033    ),
1034    (
1035        "array_slice",
1036        &[
1037            "array_slice(col0 ANY, col1 ANY, col2 ANY) -> ANY",
1038            "array_slice(col0 ANY, col1 ANY, col2 ANY, col3 BIGINT) -> ANY",
1039        ],
1040    ),
1041    ("typeof", &["typeof(col0 ANY) -> VARCHAR"]),
1042    ("current_setting", &["current_setting(setting_name VARCHAR) -> ANY"]),
1043];
1044
1045/// What one element of a subscripted value is, or `None` for a value that cannot be subscripted.
1046///
1047/// A string is subscripted by character and a character is a string, so `'abcdef'[2]` is a VARCHAR
1048/// and not a type of its own. An untyped null takes the VARCHAR overload, which was measured:
1049/// `typeof(array_extract(NULL, 1))` is VARCHAR on the pinned binary while
1050/// `typeof(array_slice(NULL, 1, 2))` is NULL, so the null goes here and the slice keeps the type it
1051/// was handed.
1052///
1053/// A STRUCT is subscripted by name rather than by position and is not one of these. `x.y` is
1054/// `struct_extract(x, 'y')` by the time it leaves the transformer, which is a function this table
1055/// does not have yet, so that call fails with the name of the function it is missing.
1056fn element_of(ty: &LogicalType) -> Option<LogicalType> {
1057    match ty {
1058        LogicalType::Varchar | LogicalType::Null => Some(LogicalType::Varchar),
1059        LogicalType::List(element) | LogicalType::Array(element, _) => Some((**element).clone()),
1060        _ => None,
1061    }
1062}
1063
1064/// The cast list for a shape that fixes the leading arguments and leaves the others as they are.
1065fn leading(count: usize, first: Fixed, arguments: &[LogicalType]) -> Vec<LogicalType> {
1066    let mut cast_to = arguments.to_vec();
1067    for head in cast_to.iter_mut().take(count) {
1068        *head = first.ty();
1069    }
1070    cast_to
1071}
1072
1073/// The type of a decimal product, or `None` when no decimal is involved and promotion decides.
1074///
1075/// A product of `DECIMAL(a,b)` and `DECIMAL(c,d)` needs `a + c` digits with `b + d` after the
1076/// point, because the largest pair of inputs multiplies to exactly that, and an integer counts as
1077/// the decimal that holds it. The rest is where upstream stops widening, and both of the places it
1078/// stops were read off `v2.0.0-dev84237` across a grid of seventy two pairs rather than reasoned
1079/// about:
1080///
1081/// A product of two operands that each fit in sixty four bits is kept there when it can be. So
1082/// `DECIMAL(10,0) * DECIMAL(10,0)` is `DECIMAL(18,0)` rather than `DECIMAL(20,0)`, which is a type
1083/// that cannot hold every product of its own inputs and raises an overflow on the ones it cannot,
1084/// and `DECIMAL(18,17) * DECIMAL(10,0)` is `DECIMAL(18,17)`. It is kept there only while a digit is
1085/// left in front of the point, which is why `DECIMAL(10,9) * DECIMAL(10,9)` is `DECIMAL(20,18)` and
1086/// not `DECIMAL(18,18)`: at eighteen decimal places there is no room for the integer part, so the
1087/// answer moves to the wider representation instead.
1088///
1089/// Past that, the width stops at the widest decimal there is and the scale does not, because a
1090/// scale that had to shrink would be an answer with digits missing from the end of it rather than a
1091/// narrower one. A scale of more than thirty eight is refused at bind time with upstream's own
1092/// sentence, since there is no type to put the answer in.
1093fn product(arguments: &[LogicalType]) -> Result<Option<LogicalType>> {
1094    let mut decimals = false;
1095    let (mut width, mut scale, mut widest) = (0u8, 0u8, 0u8);
1096    for ty in arguments {
1097        decimals |= matches!(ty, LogicalType::Decimal { .. });
1098        let Some((one, held)) = ty.decimal_shape() else { return Ok(None) };
1099        width = width.saturating_add(one);
1100        scale = scale.saturating_add(held);
1101        widest = widest.max(one);
1102    }
1103    if !decimals {
1104        return Ok(None);
1105    }
1106    if scale > MAX_DECIMAL_WIDTH {
1107        return Err(Error::out_of_range(format!(
1108            "Needed scale {scale} to accurately represent the multiplication result, but this is out of range of the DECIMAL type. Max scale is {MAX_DECIMAL_WIDTH}; could not perform an accurate multiplication. Either add a cast to DOUBLE, or add an explicit cast to a decimal with a lower scale."
1109        )));
1110    }
1111    if widest <= WIDEST_SIXTY_FOUR_BIT
1112        && width > WIDEST_SIXTY_FOUR_BIT
1113        && scale < WIDEST_SIXTY_FOUR_BIT
1114    {
1115        width = WIDEST_SIXTY_FOUR_BIT;
1116    }
1117    Ok(Some(LogicalType::Decimal { width: width.min(MAX_DECIMAL_WIDTH), scale }))
1118}
1119
1120/// The widest decimal that is still eight bytes a value, which is where a product stops widening.
1121const WIDEST_SIXTY_FOUR_BIT: u8 = 18;
1122
1123/// The type an addition or a subtraction produces from what its operands promote to.
1124///
1125/// A decimal gains the one digit an addition can carry into and everything else is unchanged. At
1126/// the maximum width there is nowhere left to widen into, so the type stays where it is and the
1127/// overflow is raised on the row that overflows rather than on every query that could.
1128///
1129/// Measured on `v2.0.0-dev84237`, which is where each of these numbers comes from:
1130/// `DECIMAL(18,0) + DECIMAL(18,0)` is `DECIMAL(19,0)`, `DECIMAL(38,0) + DECIMAL(38,0)` is
1131/// `DECIMAL(38,0)`, `2.0 + 1::INTEGER` is `DECIMAL(12,1)` and `DECIMAL(18,0) - DECIMAL(4,2)` is
1132/// `DECIMAL(21,2)`. A modulo, a negation and `abs` do not widen and keep [`Shape::Promoted`] for
1133/// that reason, and a product widens by a rule of its own, which is [`product`].
1134fn carrying(common: LogicalType) -> LogicalType {
1135    match common {
1136        LogicalType::Decimal { width, scale } if width < MAX_DECIMAL_WIDTH => {
1137            LogicalType::Decimal { width: width + 1, scale }
1138        }
1139        other => other,
1140    }
1141}
1142
1143/// What a sum of this type accumulates into.
1144///
1145/// Summing a column of `INTEGER` overflows an `INTEGER` after 2^31 of them and there is no useful
1146/// error to raise at that point, so the accumulator is the widest integer there is and the answer
1147/// is right. A float sums into a double for the same reason and a double stays a double, since
1148/// there is nothing wider to go to.
1149fn accumulator(ty: &LogicalType) -> LogicalType {
1150    if ty.is_integer() {
1151        LogicalType::HugeInt
1152    } else if *ty == LogicalType::Float {
1153        LogicalType::Double
1154    } else {
1155        ty.clone()
1156    }
1157}
1158
1159fn promote_all(name: &str, arguments: &[LogicalType]) -> Result<LogicalType> {
1160    // Only reachable for a signature whose arity allows no arguments and whose shape promotes,
1161    // which is a combination the table does not contain and which the test below holds it to.
1162    let mut common = match arguments.first() {
1163        Some(first) => first.clone(),
1164        None => {
1165            return Err(Error::internal(format!("{name} promotes over no arguments")));
1166        }
1167    };
1168    for ty in &arguments[1..] {
1169        common = common.promote(ty).ok_or_else(|| {
1170            Error::binder(format!(
1171                "No function matches the given name and argument types '{name}({})'. You might need to add explicit type casts.",
1172                arguments.iter().map(ToString::to_string).collect::<Vec<_>>().join(", ")
1173            ))
1174        })?;
1175    }
1176    // Every argument was a null literal, which has no type. Untyped null is not a type an executor
1177    // can hold a vector of, so it becomes an integer, which is what DuckDB does with `SELECT NULL`.
1178    if common == LogicalType::Null {
1179        common = LogicalType::Integer;
1180    }
1181    Ok(common)
1182}
1183
1184fn find(name: &str) -> Option<&'static Entry> {
1185    let name = canonical(name);
1186    TABLE.iter().find(|entry| entry.name.eq_ignore_ascii_case(name))
1187}
1188
1189/// One overload of one function, as `duckdb_functions()` reports it.
1190///
1191/// An overload here is a name and an argument count, because that is what an entry in this crate's
1192/// table has one of each. Upstream has an overload per pair of argument types instead and so reports
1193/// 44 rows for `+`, and `types` is where the difference shows up. See [`function_rows`].
1194#[derive(Debug, Clone, PartialEq, Eq)]
1195pub struct FunctionRow {
1196    /// The name as it was written, which is the alias for an alias.
1197    pub name: &'static str,
1198    /// Scalar or aggregate.
1199    pub kind: FunctionKind,
1200    /// The name this one resolves to, and `None` for a name that is its own.
1201    pub alias_of: Option<&'static str>,
1202    /// One per argument, in order.
1203    pub types: Vec<&'static str>,
1204    /// What the call produces.
1205    pub returns: &'static str,
1206    /// The type of the trailing variadic argument, for the names that take one.
1207    pub varargs: Option<&'static str>,
1208}
1209
1210/// Every name in the table and every argument count it takes, for `duckdb_functions()`.
1211///
1212/// The types here are declared types and not resolved ones, which is the whole difference between
1213/// this table and upstream's. The table in this module resolves by shape: `+` is one entry saying
1214/// both arguments promote and the result is what they promote to, where upstream carries an entry
1215/// per pair of numeric types because it carries an implementation per pair. So upstream reports 44
1216/// rows for `+` naming concrete types and this reports two, one per arity, with the type variable.
1217///
1218/// `T` is upstream's own spelling for an argument whose type the call decides, which it uses for
1219/// `list_extract` and `lag` and the rest of the generic functions, and it means the same thing here:
1220/// every argument spelled `T` in one row is the same type as every other. `ANY` is the weaker one
1221/// and means the argument is not constrained and not tied to the others, which is what `count(x)`
1222/// takes. A return of `ANY` means the type is decided by the arguments in a way a name cannot say,
1223/// which is where `sum` is, since it promotes and then widens an integer to the accumulator.
1224///
1225/// Rows come out in the order the table is written in, which is by family. The caller sorts.
1226///
1227/// [`resolve`]: crate::signature::resolve
1228#[must_use]
1229pub fn function_rows() -> Vec<FunctionRow> {
1230    let mut rows = Vec::new();
1231    for entry in TABLE {
1232        for count in entry.arity.every_count() {
1233            let (types, returns) = entry.shape.declared(count);
1234            rows.push(FunctionRow {
1235                name: entry.name,
1236                kind: entry.kind,
1237                alias_of: None,
1238                types,
1239                returns,
1240                varargs: entry.arity.open().then(|| entry.shape.declared(1).0[0]),
1241            });
1242        }
1243    }
1244    // An alias is a row of its own with the same shape, because a client reading this table to find
1245    // out whether `len` works wants a row for `len`. Upstream does the same and fills `alias_of`
1246    // with the name it resolves to, which is how this crate's list was read off in the first place.
1247    for (alias, real) in ALIASES {
1248        let mut aliased: Vec<FunctionRow> = rows
1249            .iter()
1250            .filter(|row| row.name == *real)
1251            .map(|row| FunctionRow { name: alias, alias_of: Some(real), ..row.clone() })
1252            .collect();
1253        rows.append(&mut aliased);
1254    }
1255    rows
1256}
1257
1258impl Arity {
1259    /// Every argument count this accepts, with an open end reported as its shortest form.
1260    ///
1261    /// An open end is `concat` and friends, which take any number, and the row for one says so in
1262    /// `varargs` rather than by having a row per count up to some number nobody picked.
1263    fn every_count(self) -> Vec<usize> {
1264        match self {
1265            Self::Exactly(count) => vec![count],
1266            Self::Between(least, Some(most)) => (least..=most).collect(),
1267            Self::Between(least, None) => vec![least],
1268            Self::OneOf(counts) => counts.to_vec(),
1269        }
1270    }
1271
1272    /// Whether the count has no upper end.
1273    const fn open(self) -> bool {
1274        matches!(self, Self::Between(_, None))
1275    }
1276}
1277
1278impl Fixed {
1279    /// The name this type goes by in a catalog table, which is the name a cast spells.
1280    const fn name(self) -> &'static str {
1281        match self {
1282            Self::Boolean => "BOOLEAN",
1283            Self::Integer => "INTEGER",
1284            Self::BigInt => "BIGINT",
1285            Self::Double => "DOUBLE",
1286            Self::Varchar => "VARCHAR",
1287            Self::Date => "DATE",
1288            Self::Timestamp => "TIMESTAMP",
1289            Self::Interval => "INTERVAL",
1290        }
1291    }
1292}
1293
1294/// The type variable, for an argument whose type the call decides and that every other argument
1295/// spelled the same way has to agree with.
1296const SAME: &str = "T";
1297
1298/// An argument that is not constrained and is not tied to the others, or a result that the
1299/// arguments decide in a way no name can say.
1300const ANY: &str = "ANY";
1301
1302impl Shape {
1303    /// What the arguments and the result are declared to be, at this argument count.
1304    ///
1305    /// Not what a call resolves to. A shape that promotes says `T` here and works out the real type
1306    /// in [`resolve`] from what was passed, and a shape that widens a decimal says `ANY` for the
1307    /// result because the width is not in the name.
1308    fn declared(self, count: usize) -> (Vec<&'static str>, &'static str) {
1309        let all = |name: &'static str| vec![name; count];
1310        let leading = |taken: usize, first: &'static str, rest: &'static str| {
1311            (0..count).map(|at| if at < taken { first } else { rest }).collect::<Vec<_>>()
1312        };
1313        match self {
1314            // Promoting says `T` and the result is that same `T`, exactly.
1315            Self::Promoted | Self::PromotedToFirst => (all(SAME), SAME),
1316            // Promoting and then moving: a decimal product is as wide as both operands, a decimal
1317            // quotient is a double, a decimal sum gains a carry digit and an integer sum widens to
1318            // the accumulator. The arguments still meet at one type and the result is no longer it.
1319            Self::Multiplied | Self::Divided | Self::PromotedWithCarry | Self::Accumulated => {
1320                (all(SAME), ANY)
1321            }
1322            Self::PromotedTo(fixed) => (all(SAME), fixed.name()),
1323            Self::FixedTo(from, to) | Self::Exact(from, to) | Self::Widened(from, to) => {
1324                (all(from.name()), to.name())
1325            }
1326            Self::AnyTo(fixed) => (all(ANY), fixed.name()),
1327            Self::LeadingFixedTo(taken, first, to) => {
1328                (leading(taken, first.name(), ANY), to.name())
1329            }
1330            Self::LeadingFixedToLast(first) => (leading(1, first.name(), SAME), SAME),
1331            // A subscript takes a string or a list and a whole number, and the whole number is not
1332            // cast to one, which is why it is spelled out rather than left as `ANY`.
1333            Self::Extracted => (leading(1, SAME, "BIGINT"), ANY),
1334            Self::Sliced => (leading(1, SAME, "BIGINT"), SAME),
1335            Self::TextThenIndex(taken, to) => {
1336                (leading(taken, Fixed::Varchar.name(), "BIGINT"), to.name())
1337            }
1338            // One overload with an `ANY` return, which is the pin's row for it. The name decides
1339            // the type and a name is not something a signature can hold.
1340            Self::Setting => (all(Fixed::Varchar.name()), ANY),
1341        }
1342    }
1343}
1344
1345/// The name a function is in [`TABLE`] under, which is its own name unless it is an alias.
1346///
1347/// Aliases are resolved here rather than by a second row in the table, so that [`Resolved::name`]
1348/// is always the canonical name and the plan, the executor and every kernel below it see one name
1349/// per function. A kernel that had to know `len` is `length` would be a kernel with a second place
1350/// for the two to drift apart.
1351///
1352/// The list is DuckDB's, read off `duckdb_functions()` where `alias_of` is set, and it is only ever
1353/// as long as the table it points into. There is no point aliasing a name onto a function this
1354/// engine does not have yet, because the error would move from a missing function to a missing
1355/// function under a different name.
1356fn canonical(name: &str) -> &str {
1357    ALIASES
1358        .iter()
1359        .find(|(alias, _)| alias.eq_ignore_ascii_case(name))
1360        .map_or(name, |(_, real)| *real)
1361}
1362
1363/// Every other name DuckDB accepts for a function already in [`TABLE`].
1364///
1365/// `strlen` is deliberately not here. Upstream counts bytes with it and characters with `length`,
1366/// so it is a different function and it has a row of its own.
1367/// The three subscript spellings point the way the transformer writes them rather than the way
1368/// `duckdb_functions()` has them. Upstream is `array_slice` aliased onto `list_slice`, and
1369/// `array_extract` and `list_extract` are two functions there rather than one, differing in the
1370/// overloads they carry for a STRUCT and a TUPLE. Neither of those is here, so they are one function
1371/// here, and the name it is under is the one a bracket produces, which is what keeps the message a
1372/// bracket produces word for word the reference's.
1373///
1374/// What that costs is the same thing every row below costs: the message names the canonical spelling
1375/// and not the written one, so `list_slice(1, 2, 3)` says `array_slice` here where upstream says
1376/// `list_slice`, exactly as `len(1)` says `length`.
1377const ALIASES: &[(&str, &str)] = &[
1378    ("len", "length"),
1379    ("char_length", "length"),
1380    ("character_length", "length"),
1381    ("lcase", "lower"),
1382    ("ucase", "upper"),
1383    ("mean", "avg"),
1384    ("list_extract", "array_extract"),
1385    ("list_element", "array_extract"),
1386    ("list_slice", "array_slice"),
1387];
1388
1389#[cfg(test)]
1390mod tests {
1391    use super::*;
1392
1393    #[test]
1394    fn arithmetic_returns_what_its_operands_promote_to() {
1395        let resolved = resolve("+", &[LogicalType::Integer, LogicalType::BigInt])
1396            .expect("an integer and a bigint add");
1397        assert_eq!(resolved.returns, LogicalType::BigInt);
1398        assert_eq!(resolved.arguments, vec![LogicalType::BigInt, LogicalType::BigInt]);
1399    }
1400
1401    /// Every decimal sum in here was read off `v2.0.0-dev84237` with `typeof`, per #243.
1402    ///
1403    /// The last one is the case the rule exists for. Two `DECIMAL(18,0)` hold numbers that add to
1404    /// nineteen digits, and a result type of eighteen means the largest pair of inputs the operator
1405    /// accepts is a pair it cannot answer.
1406    #[test]
1407    fn a_decimal_sum_is_a_digit_wider_than_what_its_operands_promote_to() {
1408        let decimal = |width, scale| LogicalType::Decimal { width, scale };
1409        let sum = |left: LogicalType, right: LogicalType| {
1410            resolve("+", &[left, right]).expect("adds").returns
1411        };
1412        assert_eq!(sum(decimal(18, 0), decimal(18, 0)), decimal(19, 0));
1413        assert_eq!(sum(decimal(2, 1), LogicalType::Integer), decimal(12, 1));
1414        assert_eq!(sum(decimal(18, 0), decimal(4, 2)), decimal(21, 2));
1415        assert_eq!(sum(decimal(4, 2), LogicalType::BigInt), decimal(22, 2));
1416        assert_eq!(sum(decimal(4, 2), LogicalType::UBigInt), decimal(23, 2));
1417        assert_eq!(sum(decimal(4, 2), LogicalType::HugeInt), decimal(38, 2));
1418        // Both sides are cast to the answer's type, because the kernel underneath adds two runs of
1419        // the same width and the carry digit can move the answer into a wider one.
1420        let resolved = resolve("-", &[decimal(18, 0), decimal(18, 0)]).expect("subtracts");
1421        assert_eq!(resolved.arguments, vec![decimal(19, 0), decimal(19, 0)]);
1422    }
1423
1424    /// At the maximum width there is nowhere to carry into, so the type stops and the row raises.
1425    #[test]
1426    fn a_decimal_sum_at_the_widest_decimal_stays_there() {
1427        let widest = LogicalType::Decimal { width: MAX_DECIMAL_WIDTH, scale: 0 };
1428        let resolved = resolve("+", &[widest.clone(), widest.clone()]).expect("adds");
1429        assert_eq!(resolved.returns, widest);
1430    }
1431
1432    /// Negation cannot carry, and neither can anything that is not an addition.
1433    ///
1434    /// `-1.50` is a `DECIMAL(4,2)` upstream and so is `abs(-1.50)`, and `5.50 % 3` is a
1435    /// `DECIMAL(12,2)`, which is the promotion with no digit added to it.
1436    #[test]
1437    fn nothing_but_a_two_sided_addition_gains_a_digit() {
1438        let decimal = |width, scale| LogicalType::Decimal { width, scale };
1439        assert_eq!(resolve("-", &[decimal(4, 2)]).expect("negates").returns, decimal(4, 2));
1440        assert_eq!(resolve("+", &[decimal(4, 2)]).expect("is unary plus").returns, decimal(4, 2));
1441        assert_eq!(resolve("abs", &[decimal(4, 2)]).expect("has a size").returns, decimal(4, 2));
1442        assert_eq!(
1443            resolve("%", &[decimal(4, 2), LogicalType::Integer]).expect("divides").returns,
1444            decimal(12, 2)
1445        );
1446    }
1447
1448    /// Every product in here was read off `v2.0.0-dev84237` with `typeof`, per #243.
1449    ///
1450    /// The first three are the plain rule, the next two are the pair that stays in sixty four bits
1451    /// and the pair that does not because it has no digit left in front of the point, and the last
1452    /// is the width running into the widest decimal there is while the scale does not move.
1453    #[test]
1454    fn a_decimal_product_is_as_wide_as_both_of_its_operands_together() {
1455        let decimal = |width, scale| LogicalType::Decimal { width, scale };
1456        let times = |left: LogicalType, right: LogicalType| {
1457            resolve("*", &[left, right]).expect("multiplies").returns
1458        };
1459        assert_eq!(times(decimal(4, 2), decimal(4, 2)), decimal(8, 4));
1460        assert_eq!(times(decimal(4, 2), LogicalType::BigInt), decimal(23, 2));
1461        assert_eq!(times(decimal(18, 3), LogicalType::Integer), decimal(18, 3));
1462        assert_eq!(times(decimal(12, 6), decimal(12, 6)), decimal(18, 12));
1463        assert_eq!(times(decimal(10, 9), decimal(10, 9)), decimal(20, 18));
1464        assert_eq!(times(decimal(18, 17), decimal(18, 17)), decimal(36, 34));
1465        assert_eq!(times(decimal(20, 10), decimal(20, 10)), decimal(38, 20));
1466        // Nothing that is not a decimal goes near any of this.
1467        assert_eq!(times(LogicalType::Integer, LogicalType::Integer), LogicalType::Integer);
1468    }
1469
1470    /// Each side takes the answer's width and keeps its own scale, which is what the kernel needs.
1471    ///
1472    /// The unscaled values then multiply into the answer with nothing rescaled on either side of
1473    /// the operator, which a cast of both sides to the answer's scale would not give.
1474    #[test]
1475    fn a_decimal_product_casts_its_operands_to_the_width_of_the_answer() {
1476        let decimal = |width, scale| LogicalType::Decimal { width, scale };
1477        let resolved = resolve("*", &[decimal(4, 2), LogicalType::BigInt]).expect("multiplies");
1478        assert_eq!(resolved.arguments, vec![decimal(23, 2), decimal(23, 0)]);
1479    }
1480
1481    /// There is no type to put the answer in, so it is refused at bind time rather than truncated.
1482    #[test]
1483    fn a_product_that_needs_more_than_thirty_eight_decimal_places_is_refused() {
1484        let wide = LogicalType::Decimal { width: 30, scale: 30 };
1485        let error = resolve("*", &[wide.clone(), wide]).expect_err("has nowhere to put the scale");
1486        assert!(error.to_string().contains("Max scale is 38"), "{error}");
1487    }
1488
1489    /// The one arithmetic result that is not the promotion, and it is DuckDB's rule rather than an
1490    /// invention: `7 / 2` is 3.5 and `7 // 2` is 3.
1491    #[test]
1492    fn division_gives_a_double_and_integer_division_does_not() {
1493        let divide = resolve("/", &[LogicalType::Integer, LogicalType::Integer]).expect("divides");
1494        assert_eq!(divide.returns, LogicalType::Double);
1495        let integer =
1496            resolve("//", &[LogicalType::Integer, LogicalType::Integer]).expect("divides");
1497        assert_eq!(integer.returns, LogicalType::Integer);
1498    }
1499
1500    /// `//` is integer division only when there are integers on both sides of it, which was
1501    /// measured: `7.5 // 2.5` is the DOUBLE 3.0 upstream and `7.5 // 2` is 3.75, so it neither
1502    /// stays a decimal nor truncates what it divided.
1503    #[test]
1504    fn integer_division_of_anything_but_integers_is_ordinary_division() {
1505        let decimal = LogicalType::Decimal { width: 4, scale: 2 };
1506        let divides = |left: LogicalType, right: LogicalType| {
1507            let resolved = resolve("//", &[left, right]).expect("divides");
1508            (resolved.arguments, resolved.returns)
1509        };
1510        let double = || (vec![LogicalType::Double; 2], LogicalType::Double);
1511        assert_eq!(divides(decimal.clone(), decimal.clone()), double());
1512        assert_eq!(divides(decimal.clone(), LogicalType::Integer), double());
1513        assert_eq!(divides(LogicalType::Integer, decimal), double());
1514        assert_eq!(divides(LogicalType::Double, LogicalType::Double), double());
1515        // A float stays a float, so this is a rule about decimals rather than about width.
1516        assert_eq!(
1517            divides(LogicalType::Float, LogicalType::Float),
1518            (vec![LogicalType::Float; 2], LogicalType::Float)
1519        );
1520        assert_eq!(
1521            divides(LogicalType::Integer, LogicalType::BigInt),
1522            (vec![LogicalType::BigInt; 2], LogicalType::BigInt)
1523        );
1524        assert_eq!(
1525            divides(LogicalType::HugeInt, LogicalType::HugeInt),
1526            (vec![LogicalType::HugeInt; 2], LogicalType::HugeInt)
1527        );
1528    }
1529
1530    /// An alias has to come back under the real name, because the name on [`Resolved`] is what the
1531    /// plan interns and what every kernel below it matches on. SQL is case insensitive here, so the
1532    /// shouted spelling has to land in the same place.
1533    #[test]
1534    fn an_alias_resolves_to_the_function_it_is_an_alias_of() {
1535        for (alias, real) in ALIASES {
1536            assert_eq!(canonical(alias), *real);
1537            assert_eq!(canonical(&alias.to_uppercase()), *real);
1538        }
1539        let resolved = resolve("LEN", &[LogicalType::Varchar]).expect("len resolves");
1540        assert_eq!(resolved.name, "length");
1541        assert_eq!(resolved.returns, LogicalType::BigInt);
1542    }
1543
1544    /// Every alias has to point at a row that exists, or the error a caller gets moves from a
1545    /// missing function to a missing function under another name, which is worse.
1546    #[test]
1547    fn every_alias_points_at_a_real_function() {
1548        for (alias, real) in ALIASES {
1549            assert!(
1550                TABLE.iter().any(|entry| entry.name == *real),
1551                "{alias} points at {real}, which is not in the table"
1552            );
1553        }
1554    }
1555
1556    /// DuckDB refuses a string function anything that is not already a string, and the whole point
1557    /// of refusing is the message, so the message is what this checks.
1558    #[test]
1559    fn a_string_function_refuses_a_type_that_is_not_a_string() {
1560        let error = resolve("lower", &[LogicalType::Date]).expect_err("lower takes strings");
1561        assert_eq!(
1562            error.to_string(),
1563            "Binder Error: No function matches the given name and argument types 'lower(DATE)'. \
1564             You might need to add explicit type casts.\n\tCandidate functions:\n\tlower(col0 \
1565             VARCHAR) -> VARCHAR\n"
1566        );
1567        for name in ["upper", "length", "strlen"] {
1568            assert!(resolve(name, &[LogicalType::Integer]).is_err(), "{name} took an integer");
1569        }
1570        for name in ["~~", "!~~", "~~*", "!~~*"] {
1571            let types = [LogicalType::Integer, LogicalType::Varchar];
1572            assert!(resolve(name, &types).is_err(), "{name} took an integer");
1573        }
1574    }
1575
1576    /// The wrong answer this shape was added for. `length([1,2,3])` used to cast the list to a
1577    /// string and count the nine characters of `[1, 2, 3]`, where DuckDB counts three elements.
1578    /// rudb has no list type in the executor yet, so refusing is the honest end of it for now.
1579    #[test]
1580    fn length_of_something_that_is_not_a_string_is_refused_rather_than_stringified() {
1581        let error = resolve("length", &[LogicalType::Blob]).expect_err("length takes strings");
1582        assert!(error.to_string().contains("length(col0 ANY[]) -> BIGINT"), "{error}");
1583    }
1584
1585    /// `||` is the exception and it has to stay one. `1 || 'a'` is `1a` upstream.
1586    #[test]
1587    fn concatenation_still_takes_anything_and_makes_a_string_of_it() {
1588        let resolved = resolve("||", &[LogicalType::Integer, LogicalType::Varchar])
1589            .expect("concatenation takes anything");
1590        assert_eq!(resolved.returns, LogicalType::Varchar);
1591        assert_eq!(resolved.arguments, vec![LogicalType::Varchar, LogicalType::Varchar]);
1592    }
1593
1594    /// A null literal has no type to pick an overload with, and DuckDB answers `length(NULL)` with
1595    /// NULL rather than refusing it.
1596    #[test]
1597    fn a_string_function_takes_an_untyped_null() {
1598        let resolved = resolve("length", &[LogicalType::Null]).expect("length of a null");
1599        assert_eq!(resolved.returns, LogicalType::BigInt);
1600        assert_eq!(resolved.arguments, vec![LogicalType::Varchar]);
1601    }
1602
1603    /// A candidate block for a name nothing resolves to would be a message about a function that
1604    /// does not exist, which is worse than no block at all.
1605    #[test]
1606    fn every_name_with_candidates_is_a_function_this_engine_has() {
1607        for (name, overloads) in CANDIDATES {
1608            assert!(TABLE.iter().any(|entry| entry.name == *name), "{name} has no entry");
1609            assert!(!overloads.is_empty(), "{name} has an empty candidate list");
1610        }
1611    }
1612
1613    /// `strlen` counts bytes and `length` counts characters, so it is a function and not an alias.
1614    /// This is the test that stops someone folding it into [`ALIASES`] to save a row.
1615    #[test]
1616    fn strlen_is_its_own_function_and_not_an_alias_of_length() {
1617        assert!(!ALIASES.iter().any(|(alias, _)| *alias == "strlen"));
1618        let resolved = resolve("strlen", &[LogicalType::Varchar]).expect("strlen resolves");
1619        assert_eq!(resolved.name, "strlen");
1620        assert_eq!(resolved.returns, LogicalType::BigInt);
1621    }
1622
1623    #[test]
1624    fn a_sum_accumulates_wider_than_it_reads() {
1625        assert_eq!(
1626            resolve("sum", &[LogicalType::Integer]).expect("sums").returns,
1627            LogicalType::HugeInt
1628        );
1629        assert_eq!(
1630            resolve("sum", &[LogicalType::Double]).expect("sums").returns,
1631            LogicalType::Double
1632        );
1633        assert_eq!(
1634            resolve("sum", &[LogicalType::Float]).expect("sums").returns,
1635            LogicalType::Double
1636        );
1637    }
1638
1639    #[test]
1640    fn count_takes_anything_and_returns_a_bigint() {
1641        let counted = resolve("count", &[LogicalType::Varchar]).expect("counts strings");
1642        assert_eq!(counted.returns, LogicalType::BigInt);
1643        assert_eq!(counted.arguments, vec![LogicalType::Varchar], "count does not cast its input");
1644        assert_eq!(resolve("count_star", &[]).expect("counts rows").returns, LogicalType::BigInt);
1645    }
1646
1647    /// `date_part` says double whatever it reads and `date_trunc` hands back the type it was given,
1648    /// which is two answers that one shape cannot give and is why there are two new ones. A double
1649    /// rather than a bigint because that is upstream's declared overload, and the narrowing to a
1650    /// bigint happens in the binder, where the specifier can be looked at.
1651    #[test]
1652    fn a_date_function_fixes_the_part_and_leaves_the_date_alone() {
1653        let part = resolve("date_part", &[LogicalType::Varchar, LogicalType::Timestamp])
1654            .expect("a part of a timestamp");
1655        assert_eq!(part.returns, LogicalType::Double);
1656        assert_eq!(part.arguments, vec![LogicalType::Varchar, LogicalType::Timestamp]);
1657        let truncated = resolve("date_trunc", &[LogicalType::Varchar, LogicalType::Date])
1658            .expect("a truncated date");
1659        assert_eq!(truncated.returns, LogicalType::Date);
1660        assert_eq!(truncated.arguments, vec![LogicalType::Varchar, LogicalType::Date]);
1661    }
1662
1663    /// The two constructors, and the arity with a hole in it. Two arguments is not a `make_date`
1664    /// upstream has and it is not one here either.
1665    #[test]
1666    fn a_date_is_made_from_one_number_or_from_three_and_never_from_two() {
1667        let day = resolve("make_date", &[LogicalType::Integer]).expect("days since the epoch");
1668        assert_eq!(day.returns, LogicalType::Date);
1669        assert_eq!(day.arguments, vec![LogicalType::Integer]);
1670        let civil =
1671            resolve("make_date", &vec![LogicalType::BigInt; 3]).expect("a year, a month and a day");
1672        assert_eq!(civil.returns, LogicalType::Date);
1673        assert_eq!(civil.arguments, vec![LogicalType::Integer; 3]);
1674        let error = resolve("make_date", &vec![LogicalType::Integer; 2]).unwrap_err();
1675        assert_eq!(
1676            error.message(),
1677            "No function matches the given name and argument types 'make_date(INTEGER, INTEGER)'. You might need to add explicit type casts."
1678        );
1679    }
1680
1681    #[test]
1682    fn milliseconds_since_the_epoch_are_a_timestamp() {
1683        let stamp = resolve("epoch_ms", &[LogicalType::Integer]).expect("a timestamp");
1684        assert_eq!(stamp.returns, LogicalType::Timestamp);
1685        assert_eq!(stamp.arguments, vec![LogicalType::BigInt], "the argument widens to read it");
1686        let error = resolve("epoch_ms", &[LogicalType::Varchar]).unwrap_err();
1687        assert!(error.message().contains("'epoch_ms(VARCHAR)'"), "{error}");
1688    }
1689
1690    /// The part is cast rather than checked, so a part that arrives as something other than a
1691    /// string is a string by the time the kernel sees it.
1692    #[test]
1693    fn the_part_of_a_date_function_is_cast_to_a_string() {
1694        let resolved = resolve("date_part", &[LogicalType::Integer, LogicalType::Date])
1695            .expect("the part is cast rather than refused");
1696        assert_eq!(resolved.arguments, vec![LogicalType::Varchar, LogicalType::Date]);
1697    }
1698
1699    /// The group number of an extraction has to arrive as a number, since the kernel tells the
1700    /// option string from the group by the type rather than by the position.
1701    #[test]
1702    fn an_extraction_casts_the_text_and_the_pattern_and_leaves_the_group_alone() {
1703        let resolved = resolve(
1704            "regexp_extract",
1705            &[LogicalType::Varchar, LogicalType::Varchar, LogicalType::Integer],
1706        )
1707        .expect("an extraction");
1708        assert_eq!(resolved.returns, LogicalType::Varchar);
1709        assert_eq!(
1710            resolved.arguments,
1711            vec![LogicalType::Varchar, LogicalType::Varchar, LogicalType::Integer]
1712        );
1713        let matched = resolve("regexp_matches", &[LogicalType::Varchar, LogicalType::Varchar])
1714            .expect("a match");
1715        assert_eq!(matched.returns, LogicalType::Boolean);
1716    }
1717
1718    #[test]
1719    fn a_name_that_is_not_a_function_says_so_the_way_duckdb_does() {
1720        let error = resolve("nope", &[]).expect_err("there is no function called nope");
1721        assert_eq!(
1722            error.to_string(),
1723            "Catalog Error: Scalar Function with name nope does not exist!"
1724        );
1725    }
1726
1727    #[test]
1728    fn the_wrong_number_of_arguments_is_caught() {
1729        let error = resolve("abs", &[LogicalType::Integer, LogicalType::Integer])
1730            .expect_err("abs takes one");
1731        assert!(error.message().contains("No function matches"), "{error}");
1732    }
1733
1734    #[test]
1735    fn arithmetic_on_a_string_is_refused() {
1736        let error =
1737            resolve("*", &[LogicalType::Varchar, LogicalType::Integer]).expect_err("no multiply");
1738        assert!(error.message().contains("No function matches"), "{error}");
1739    }
1740
1741    #[test]
1742    fn a_call_over_nothing_but_nulls_lands_on_a_type_an_executor_can_hold() {
1743        let resolved =
1744            resolve("+", &[LogicalType::Null, LogicalType::Null]).expect("null plus null");
1745        assert_eq!(resolved.returns, LogicalType::Integer);
1746    }
1747
1748    /// `nullif` compares at one type and answers at another, both read off the pinned binary with
1749    /// `typeof`. Per #306.
1750    #[test]
1751    fn nullif_answers_the_first_argument_and_compares_at_the_promotion() {
1752        let resolved =
1753            resolve("nullif", &[LogicalType::Integer, LogicalType::Decimal { width: 2, scale: 1 }])
1754                .expect("an integer and a decimal compare");
1755        assert_eq!(resolved.returns, LogicalType::Integer);
1756        let wide = LogicalType::Decimal { width: 11, scale: 1 };
1757        assert_eq!(resolved.arguments, vec![wide.clone(), wide]);
1758        let resolved = resolve("nullif", &[LogicalType::BigInt, LogicalType::SmallInt])
1759            .expect("two integers compare");
1760        assert_eq!(resolved.returns, LogicalType::BigInt);
1761        let resolved =
1762            resolve("nullif", &[LogicalType::Null, LogicalType::Null]).expect("two nulls compare");
1763        assert_eq!(resolved.returns, LogicalType::Integer, "there is nothing else to hand back");
1764        let error = resolve("nullif", &[LogicalType::Varchar, LogicalType::Integer])
1765            .expect_err("a string and a number have nothing in common here");
1766        assert!(error.message().contains("No function matches"), "{error}");
1767    }
1768
1769    #[test]
1770    fn an_aggregate_is_known_to_be_one() {
1771        assert_eq!(kind_of("sum"), Some(FunctionKind::Aggregate));
1772        assert_eq!(kind_of("SUM"), Some(FunctionKind::Aggregate), "names are case insensitive");
1773        assert_eq!(kind_of("abs"), Some(FunctionKind::Scalar));
1774        assert_eq!(kind_of("nope"), None);
1775    }
1776
1777    /// Two rows for one name would need a rule for which one wins, and there is no such rule yet,
1778    /// so the table having none is worth asserting rather than remembering.
1779    #[test]
1780    fn no_name_appears_twice() {
1781        let mut names: Vec<&str> = TABLE.iter().map(|entry| entry.name).collect();
1782        let count = names.len();
1783        names.sort_unstable();
1784        names.dedup();
1785        assert_eq!(names.len(), count, "a name is in the table twice");
1786    }
1787
1788    #[test]
1789    fn every_entry_resolves_at_every_count_it_accepts() {
1790        for entry in TABLE {
1791            // The one row that is meant not to resolve, because the binder answers the call before
1792            // it gets here and the only way here is the case upstream refuses. It has a test of its
1793            // own below rather than an exception with nothing behind it.
1794            if entry.shape == Shape::Setting {
1795                continue;
1796            }
1797            for count in entry.arity.counts() {
1798                // A shape that names the type it wants is asked for it, since `chr` wants an
1799                // INTEGER and refuses a string the way upstream does.
1800                let ty = match (entry.numeric_only, entry.shape) {
1801                    (_, Shape::Exact(argument, _) | Shape::Widened(argument, _)) => argument.ty(),
1802                    (true, _) => LogicalType::Integer,
1803                    (false, _) => LogicalType::Varchar,
1804                };
1805                let mut arguments = vec![ty; count];
1806                // A subscript and a substring are the shapes whose arguments are not all alike. The
1807                // leading ones are the string or the list and everything after them is a whole
1808                // number, so a row of strings is not a call either one accepts and not a call worth
1809                // asserting it accepts.
1810                let leading = match entry.shape {
1811                    Shape::Extracted | Shape::Sliced => 1,
1812                    Shape::TextThenIndex(leading, _) => leading,
1813                    _ => count,
1814                };
1815                for bound in arguments.iter_mut().skip(leading) {
1816                    *bound = LogicalType::BigInt;
1817                }
1818                resolve(entry.name, &arguments).unwrap_or_else(|error| {
1819                    panic!("{} does not resolve at {count} arguments: {error}", entry.name)
1820                });
1821            }
1822        }
1823    }
1824
1825    /// The three answers the pin gives a call to `current_setting`, read off `v2.0.0-dev84237`.
1826    ///
1827    /// The right number of arguments and a name the binder could not fold is the constant
1828    /// expression sentence, and a wrong number is the ordinary arity error with the one overload
1829    /// listed under it. The folded case is not here because it never reaches this table.
1830    #[test]
1831    fn a_setting_read_from_a_column_is_refused_in_the_pins_words() {
1832        let error = resolve("current_setting", &[LogicalType::Varchar]).expect_err("is refused");
1833        assert_eq!(
1834            error.to_string(),
1835            "Binder Error: The \"setting_name\" argument in function \"current_setting\" must be a constant expression"
1836        );
1837        let none = resolve("current_setting", &[]).expect_err("takes one argument");
1838        assert_eq!(
1839            none.to_string(),
1840            "Binder Error: No function matches the given name and argument types 'current_setting()'. \
1841             You might need to add explicit type casts.\n\tCandidate functions:\n\tcurrent_setting(setting_name VARCHAR) -> ANY\n"
1842        );
1843    }
1844
1845    /// One row with an `ANY` return, which is what the pin's `duckdb_functions()` says about it.
1846    #[test]
1847    fn a_setting_is_declared_over_a_string_and_returns_anything() {
1848        let row = function_rows()
1849            .into_iter()
1850            .find(|row| row.name == "current_setting")
1851            .expect("a row for it");
1852        assert_eq!(row.types, ["VARCHAR"]);
1853        assert_eq!(row.returns, "ANY");
1854        assert_eq!(row.varargs, None);
1855    }
1856
1857    /// A signature that promotes over its arguments and accepts none of them would reach the
1858    /// internal error in `promote_all`, which is a message no user should ever see.
1859    #[test]
1860    fn nothing_that_promotes_accepts_no_arguments() {
1861        for entry in TABLE {
1862            let promotes =
1863                matches!(entry.shape, Shape::Promoted | Shape::PromotedTo(_) | Shape::Accumulated);
1864            assert!(
1865                !(promotes && entry.arity.least() == 0),
1866                "{} promotes over its arguments and takes none",
1867                entry.name
1868            );
1869        }
1870    }
1871
1872    /// `-` is the negation and the subtraction under one name, which is the reason arity is a
1873    /// range, so it is worth holding to.
1874    #[test]
1875    fn minus_is_both_the_negation_and_the_subtraction() {
1876        assert_eq!(
1877            resolve("-", &[LogicalType::Integer]).expect("negates").returns,
1878            LogicalType::Integer
1879        );
1880        assert_eq!(
1881            resolve("-", &[LogicalType::Integer, LogicalType::BigInt]).expect("subtracts").returns,
1882            LogicalType::BigInt
1883        );
1884        assert!(
1885            resolve("-", &vec![LogicalType::Integer; 3]).is_err(),
1886            "three is not an arity minus has"
1887        );
1888    }
1889}