packages feed

duckdb-simple 1.0.0.0 → 1.0.1.0

raw patch · 31 files changed

+697/−336 lines, 31 filesdep ~duckdb-ffiPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependency ranges changed: duckdb-ffi

API changes (from Hackage documentation)

+ Database.DuckDB.Simple.FromField: FieldType :: LogicalTypeRep -> FieldValue
+ Database.DuckDB.Simple.FromField: instance Database.DuckDB.Simple.FromField.FromField Database.DuckDB.Simple.LogicalRep.LogicalTypeRep
+ Database.DuckDB.Simple.Internal: DuckDBV2AggregateFunctionStatsCallbackFn :: FunPtr DuckDBV2AggregateFunctionStatsCallbackFn_Aux -> DuckDBV2AggregateFunctionStatsCallbackFn
+ Database.DuckDB.Simple.Internal: DuckDBV2AggregateFunctionStatsCallbackFn_Aux :: (DuckDBV2AggregateFunctionStatsInfoHandle -> DuckDBV2ContextHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO ()) -> DuckDBV2AggregateFunctionStatsCallbackFn_Aux
+ Database.DuckDB.Simple.Internal: DuckDBV2AggregateFunctionStatsInfoHandle :: Ptr DuckDBV2AggregateFunctionStatsInfoStruct -> DuckDBV2AggregateFunctionStatsInfoHandle
+ Database.DuckDB.Simple.Internal: DuckDBV2ScalarFunctionStatsCallbackFn :: FunPtr DuckDBV2ScalarFunctionStatsCallbackFn_Aux -> DuckDBV2ScalarFunctionStatsCallbackFn
+ Database.DuckDB.Simple.Internal: DuckDBV2ScalarFunctionStatsCallbackFn_Aux :: (DuckDBV2ScalarFunctionStatsInfoHandle -> DuckDBV2ContextHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO ()) -> DuckDBV2ScalarFunctionStatsCallbackFn_Aux
+ Database.DuckDB.Simple.Internal: DuckDBV2ScalarFunctionStatsInfoHandle :: Ptr DuckDBV2ScalarFunctionStatsInfoStruct -> DuckDBV2ScalarFunctionStatsInfoHandle
+ Database.DuckDB.Simple.Internal: DuckDBV2StatsHandle :: Ptr DuckDBV2StatsStruct -> DuckDBV2StatsHandle
+ Database.DuckDB.Simple.Internal: DuckDBV2TableFunctionStatsCallbackFn :: FunPtr DuckDBV2TableFunctionStatsCallbackFn_Aux -> DuckDBV2TableFunctionStatsCallbackFn
+ Database.DuckDB.Simple.Internal: DuckDBV2TableFunctionStatsCallbackFn_Aux :: (DuckDBV2TableFunctionStatsInfoHandle -> DuckDBV2ContextHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO ()) -> DuckDBV2TableFunctionStatsCallbackFn_Aux
+ Database.DuckDB.Simple.Internal: DuckDBV2TableFunctionStatsInfoHandle :: Ptr DuckDBV2TableFunctionStatsInfoStruct -> DuckDBV2TableFunctionStatsInfoHandle
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_aggregate_function_set_stats_callback :: DuckDBV2AggregateFunctionHandle -> DuckDBV2AggregateFunctionStatsCallbackFn -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_aggregate_function_stats_get_arg_count :: DuckDBV2AggregateFunctionStatsInfoHandle -> Ptr DuckDBIdx -> Ptr DuckDBIdx -> Ptr DuckDBIdx -> Ptr DuckDBIdx -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_aggregate_function_stats_get_arg_stats :: DuckDBV2AggregateFunctionStatsInfoHandle -> DuckDBIdx -> Ptr DuckDBV2StatsHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_aggregate_function_stats_get_bind_data :: DuckDBV2AggregateFunctionStatsInfoHandle -> Ptr (Ptr Void) -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_aggregate_function_stats_get_result_stats :: DuckDBV2AggregateFunctionStatsInfoHandle -> Ptr DuckDBV2StatsHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_aggregate_function_stats_get_user_data :: DuckDBV2AggregateFunctionStatsInfoHandle -> Ptr (Ptr Void) -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_scalar_function_set_stats_callback :: DuckDBV2ScalarFunctionHandle -> DuckDBV2ScalarFunctionStatsCallbackFn -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_scalar_function_stats_get_arg_count :: DuckDBV2ScalarFunctionStatsInfoHandle -> Ptr DuckDBIdx -> Ptr DuckDBIdx -> Ptr DuckDBIdx -> Ptr DuckDBIdx -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_scalar_function_stats_get_arg_stats :: DuckDBV2ScalarFunctionStatsInfoHandle -> DuckDBIdx -> Ptr DuckDBV2StatsHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_scalar_function_stats_get_bind_data :: DuckDBV2ScalarFunctionStatsInfoHandle -> Ptr (Ptr Void) -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_scalar_function_stats_get_result_stats :: DuckDBV2ScalarFunctionStatsInfoHandle -> Ptr DuckDBV2StatsHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_scalar_function_stats_get_user_data :: DuckDBV2ScalarFunctionStatsInfoHandle -> Ptr (Ptr Void) -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_stats_can_have_null :: DuckDBV2StatsHandle -> Ptr CBool -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_stats_can_have_valid :: DuckDBV2StatsHandle -> Ptr CBool -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_stats_set_can_have_null :: DuckDBV2StatsHandle -> CBool -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_stats_set_can_have_valid :: DuckDBV2StatsHandle -> CBool -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_table_function_set_stats_callback :: DuckDBV2TableFunctionHandle -> DuckDBV2TableFunctionStatsCallbackFn -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_table_function_stats_get_bind_data :: DuckDBV2TableFunctionStatsInfoHandle -> Ptr (Ptr Void) -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_table_function_stats_get_column_index :: DuckDBV2TableFunctionStatsInfoHandle -> Ptr DuckDBIdx -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_table_function_stats_get_result_stats :: DuckDBV2TableFunctionStatsInfoHandle -> Ptr DuckDBV2StatsHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_table_function_stats_get_user_data :: DuckDBV2TableFunctionStatsInfoHandle -> Ptr (Ptr Void) -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: data DuckDBV2AggregateFunctionStatsInfoStruct
+ Database.DuckDB.Simple.Internal: data DuckDBV2ScalarFunctionStatsInfoStruct
+ Database.DuckDB.Simple.Internal: data DuckDBV2StatsStruct
+ Database.DuckDB.Simple.Internal: data DuckDBV2TableFunctionStatsInfoStruct
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2AggregateFunctionStatsCallbackFn
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2AggregateFunctionStatsCallbackFn_Aux
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2AggregateFunctionStatsInfoHandle
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2ScalarFunctionStatsCallbackFn
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2ScalarFunctionStatsCallbackFn_Aux
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2ScalarFunctionStatsInfoHandle
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2StatsHandle
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2TableFunctionStatsCallbackFn
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2TableFunctionStatsCallbackFn_Aux
+ Database.DuckDB.Simple.Internal: newtype DuckDBV2TableFunctionStatsInfoHandle
+ Database.DuckDB.Simple.ToField: instance Database.DuckDB.Simple.ToField.DuckDBColumnType Database.DuckDB.Simple.LogicalRep.LogicalTypeRep
+ Database.DuckDB.Simple.ToField: instance Database.DuckDB.Simple.ToField.ToDuckValue Database.DuckDB.Simple.LogicalRep.LogicalTypeRep
+ Database.DuckDB.Simple.ToField: instance Database.DuckDB.Simple.ToField.ToField Database.DuckDB.Simple.LogicalRep.LogicalTypeRep
+ Database.DuckDB.Simple.V2.Registration: registerAggregateFunctionWithStats :: RegistrationTarget -> (DuckDBV2AggregateFunctionHandle -> IO ()) -> ContextCallback DuckDBV2AggregateFunctionStatsInfoHandle -> AggregateCallbacks -> IO ()
+ Database.DuckDB.Simple.V2.Registration: registerScalarFunctionWithStats :: RegistrationTarget -> (DuckDBV2ScalarFunctionHandle -> IO ()) -> ContextCallback DuckDBV2ScalarFunctionStatsInfoHandle -> ScalarCallbacks -> IO ()
+ Database.DuckDB.Simple.V2.Registration: registerTableFunctionWithStats :: RegistrationTarget -> (DuckDBV2TableFunctionHandle -> IO ()) -> ContextCallback DuckDBV2TableFunctionStatsInfoHandle -> TableCallbacks -> IO ()
- Database.DuckDB.Simple.Internal: c_duckdb_v2_parse_sql :: DuckDBV2ConnectionHandle -> PtrConst CChar -> Ptr DuckDBV2StatementIteratorHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error
+ Database.DuckDB.Simple.Internal: c_duckdb_v2_parse_sql :: DuckDBV2ConnectionHandle -> PtrConst CChar -> CBool -> Ptr DuckDBV2StatementIteratorHandle -> Ptr DuckDBV2ErrorInfoHandle -> IO DuckDBV2Error

Files

CHANGELOG.md view
@@ -3,6 +3,27 @@ See [MIGRATION.md](https://github.com/Tritlo/duckdb-haskell/blob/main/MIGRATION.md) for the upgrade instructions. +## 1.0.1.0++- Read and bind SQL `TYPE` values as `LogicalTypeRep`. This includes the+  column metadata returned by `COPY ... TO (RETURN_COLUMN_TYPES)`. Reads+  through `FieldValue` return the new `FieldType` constructor.+- Add managed optimizer-statistics callbacks with+  `registerScalarFunctionWithStats`, `registerAggregateFunctionWithStats`, and+  `registerTableFunctionWithStats` in `Database.DuckDB.Simple.V2.Registration`.+  They let the planner use guaranteed nullability facts. Existing registration+  functions and callback records keep their types and constructors.+- Use DuckDB 2.0.1 and `duckdb-ffi` 2.0.1.x. Rebuild loadable extensions+  for this native release.+- Parse errors in user SQL follow the connection's transaction invalidation+  policy. Under `ALL_ERRORS_INVALIDATE_TRANSACTION`, roll back the transaction+  after a syntax error.+- Accept the wider finite timestamp range in DuckDB 2.0.1 and match its+  rounding when reading wide, scale-zero DECIMAL values as floating point.+- Accept the new `logging_sink` setting when opening a connection with configuration.+- Binding a `BigNum` uses DuckDB's corrected integer casts. A negative BIGNUM+  cast to an unsigned integer now raises a conversion error.+ ## 1.0.0.0  - Target DuckDB 2.0 with `duckdb-ffi` 2.0.0.x. The backend uses the
README.md view
@@ -11,8 +11,10 @@  The backend uses the generated DuckDB V2 API. Upstream marks the complete V2 API as unstable: "The C API v2 is not yet stable. Its interface may change in-upcoming releases." This release therefore requires exactly DuckDB 2.0.0.-Creating an environment raises `SQLError` for another library version.+upcoming releases." This release therefore requires exactly DuckDB 2.0.1.+Creating an environment checks the loaded library version and raises `SQLError`+on a mismatch. A native library missing required V2 symbols can fail to link+before this check runs.  The examples use the `OverloadedStrings` extension for `Query` and `Text` literals.@@ -69,7 +71,9 @@   names, and defaults. Haskell callbacks use typed V2 info handles. The library   retains their function pointers and contains callback exceptions. The advanced   extension builders accept a borrowed native extension handle and context.-  Scalar type resolution runs before binding. Table callbacks can report+  Managed statistics callbacks report nullability to the planner. See+  [Optimizer statistics](#optimizer-statistics). Scalar type resolution runs+  before binding. Table callbacks can report   multi-file column identifiers and metadata after binding. - `Copy` registers COPY formats and multi-file table functions with Haskell   values. `V2.Copy` has the native COPY callbacks, with batch transfer, flush,@@ -170,13 +174,10 @@ BIGNUM value, or raises an error, depending on its size and target type: note 1 of [Types](#types) gives the rule. -In DuckDB 2.0.0 the casts from BIGNUM to the integer types have defects: some-fail, and some store a wrong value with no error ([issue-41](https://github.com/Tritlo/duckdb-haskell/issues/41)). A `SizedArray` or a+A `BigNum` value, and a `SizedArray` or a list of `BigNum` values, use checked+casts for integer targets. BIGNUM has no cast to DECIMAL. A `SizedArray` or a list of `Integer` or `Natural` values takes the element type of its target when-that type is an integer type or DECIMAL, so DuckDB checks each element. A-`BigNum` value, and a `SizedArray` or a list of `BigNum` values, still have-these defects for an integer target. For each other target, and for a parameter+that type is an integer type or DECIMAL. For each other target, and for a parameter with no known type, a `SizedArray`, a list, or a `Map` of `Integer` or `Natural` values takes the type that its elements give, as a scalar `Integer` and a SQL list literal of the HUGEINT and BIGNUM values do: `[1, 2^64]` binds as@@ -419,7 +420,7 @@    that has no integer or DECIMAL type. For each other integer or DECIMAL    parameter, it raises an `SQLError` of the type `ErrorTypeConversion`. Bind a    `BigNum` to bind a BIGNUM value in each case. [Querying Data](#querying-data)-   gives the defects of the casts from BIGNUM in DuckDB 2.0.0, and the rule for+   describes the casts from BIGNUM in DuckDB 2.0.1, and the rule for    a list, a `SizedArray`, or a `Map` of `Integer` values. 2. The value 0 reads as `False`, and each other value reads as `True`. 3. "Each integer type" is TINYINT to UHUGEINT. A value that the Haskell type@@ -428,9 +429,9 @@    value does not read as an integer type. Write `enum_code(c)::BIGINT` in SQL    for its index, or `c::INTEGER` for the value of its label. 4. A read returns the value of DuckDB's `c::DOUBLE` or `c::FLOAT` cast. This-   includes a DECIMAL, and a BIGINT read as `Float`. As seen in CI, the DuckDB-   2.0.0 build for macOS on Apple Silicon rounds some BIGINT values above 2^53 two-   times in its cast to FLOAT, and the library gives the nearest value there. A+   includes a DECIMAL. A scale-zero DECIMAL rounds directly to the target type,+   including HUGEINT storage. On macOS on Apple Silicon, the library gives+   the nearest `Float` value for a BIGINT conversion. A    finite value that `Float` cannot hold raises a conversion error of the    library, an `SQLError` with no type. 5. A read of another SQL type returns the value of DuckDB's cast to the Haskell@@ -468,8 +469,8 @@    for a `VARIANT` parameter: [Infinite dates and    timestamps](#infinite-dates-and-timestamps) gives the rule. The range of    `Day` is 5877642-06-25 (BC), the year -5877641 of `Data.Time`, to-   5881580-07-10. The range of `LocalTime` and `UTCTime` is 290309-12-22 (BC)-   00:00:00 to 294247-01-10 04:00:54.775806. A value outside its range raises an+   5881580-07-10. The range of `LocalTime` and `UTCTime` is 290309-12-21 (BC)+   19:59:05.224192 to 294247-01-10 04:00:54.775806. A value outside its range raises an    `SQLError` of the type `ErrorTypeConversion`. A target of the type    TIMESTAMP_NS or TIMESTAMPTZ_NS has a smaller range, about the years 1677 to    2262, and a value outside it gets the same type in each position. A@@ -721,7 +722,7 @@   database after them. - `ErrorApi`, `ErrorIOFileNotFound`, `ErrorIOReadFailure`, `ErrorIOEof`,   `ErrorInputParameterInvalid`, `ErrorInputMetadataDeferred`, and-  `ErrorIONetwork`. DuckDB 2.0.0 cannot raise them again with their own type.+  `ErrorIONetwork`. DuckDB cannot raise them again with their own type.  When an error of a callback fails a statement in an explicit transaction, DuckDB aborts that transaction, also under the setting@@ -736,7 +737,7 @@ literal of `range(10)` with a cast from INTEGER to BIGINT, the error has the type `ErrorInputInvalid` and the text `Failed to cast value: ` before the text of the exception, and the database stays usable. The type `ErrorTypeMismatch`-is the same for each pair of types, because of a property of DuckDB 2.0.0.+is the same for each pair of types. DuckDB plans casts between some pairs of types as casts that cannot fail, including registered casts. Examples are casts to VARCHAR, including from an ENUM, a LIST, or a STRUCT, and a widening cast such as INTEGER to@@ -754,11 +755,10 @@ `ErrorInputOutOfRange`, and `ErrorTypeConversion`. DuckDB still plans such a cast as one that cannot fail, so it can run the cast for a row that another condition of the query excludes, for example in-`WHERE c < 2500 AND CAST(c AS VARCHAR) <> 'x'`. In DuckDB 2.0.0, only a guard in-a CASE expression, for example-`CASE WHEN d <> 0 THEN CAST(c AS VARCHAR) <> 'x' ELSE false END`, kept the cast-from the excluded rows. A cast that a program registers with the native API of-`duckdb-ffi` also gets the INTERNAL error for such a pair and such an error+`WHERE c < 2500 AND CAST(c AS VARCHAR) <> 'x'`. Use a guard in a CASE+expression to exclude rows from the cast, for example+`CASE WHEN d <> 0 THEN CAST(c AS VARCHAR) <> 'x' ELSE false END`. A cast that a+program registers with the native API of `duckdb-ffi` also gets the INTERNAL error for such a pair and such an error type. A V2 cast can report the code `DUCKDB_V2_ERROR_TYPE_MISMATCH` in their place. @@ -901,7 +901,11 @@ When their types are different, DuckDB uses their common type: `{'a': 1}` and `{'b': 2}` give `STRUCT(a INTEGER, b INTEGER)[]`, with NULL for a field that an element does not have. A `BIGINT` field and a `DOUBLE` field give a `DOUBLE`-field, so `9007199254740993` becomes `9007199254740992.0`, as in SQL. Elements+field, so `9007199254740993` becomes `9007199254740992.0`, as in SQL.+DuckDB 2.0.1 also merges UNION members. For example, `UNION(a INTEGER)` and+`UNION(b VARCHAR)` give `UNION(a INTEGER, b VARCHAR)`. Matching members use+the common type of their payloads. This rule also applies inside nested+collections and to the values of a `Map`. Elements with no common type, such as `{'a': 1}` and `{'a': 'x'}`, raise the `SQLError` of DuckDB. The column type of an `INSERT` does not change the type of the list: DuckDB casts the list to the column type after it gives the list its@@ -973,8 +977,10 @@ `SizedArray`, or a `Map`, inside a STRUCT, a TUPLE, or a UNION, and in a VARIANT. For a `Day`, that range is the range of DATE, from -5877641-06-25 to 5881580-07-10. For a `LocalTime` or a `UTCTime` (in UTC), it is the range of TIMESTAMP,-TIMESTAMPTZ, TIMESTAMP_S, and TIMESTAMP_MS, from -290308-12-22 00:00:00 to-294247-01-10 04:00:54.775806. The dates are in the notation of `Data.Time`. A+TIMESTAMPTZ, TIMESTAMP_S, and TIMESTAMP_MS, from -290308-12-21 19:59:05.224192 to+294247-01-10 04:00:54.775806, except the negative infinity sentinel at+-290308-12-21 19:59:05.224193. Second and millisecond targets accept whole+units within this range. The dates are in the notation of `Data.Time`. A TIMESTAMP_NS or TIMESTAMPTZ_NS target has a smaller range, about the years 1677 to 2262. A value outside that range also gets the type `ErrorTypeConversion` in each position. The position of the value determines@@ -1061,7 +1067,7 @@  TIME aliases keep their alias and exact microsecond value when they bind inside a composite. The library uses public vector storage because the-default TIME-to-alias cast fails in DuckDB 2.0.0. Other aliases require native+default TIME-to-alias cast fails. Other aliases require native cast support.  Some values bind with another type than the column that they came from. A@@ -1587,15 +1593,12 @@ import Database.DuckDB.Simple.Registration ``` -The library has no typed cast in this release. A registered cast can give-wrong rows with no error, because the optimizer of DuckDB 2.0.0 decides from-the two types of a cast alone whether it can move the cast away from a column-in a filter or a join. In a run, it also gave min and max from the statistics-of the column without the cast. The cast function then does not run on that-column, or, in a comparison of two columns, it runs on one side only. In runs-with DuckDB 2.0.0, a cast from INTEGER to BIGINT, and a cast between BIGINT and-a custom type with the base type BIGINT, gave wrong rows when its function-changed the value. `registerCast` in+The library has no typed cast in this release. The native optimizer can move a+registered cast away from a column in a filter or a join based on the two types.+It can also use column statistics without applying the cast. A cast that changes+the value can therefore give wrong rows without an error. This affects widening+casts and casts between an integer type and a custom type with the same base+type. `registerCast` in `Database.DuckDB.Simple.V2.Registration` registers a native cast, and its Haddock gives the warning. For a conversion that changes the value, register a scalar function with `createFunction` (see@@ -1728,6 +1731,36 @@ scalar functions with a bind callback or variadic parameters. A typed table function has projection pushdown (see `createTableFunction` above), but no filter pushdown.++### Optimizer statistics++Use `registerScalarFunctionWithStats`, `registerAggregateFunctionWithStats`, or+`registerTableFunctionWithStats` from `Database.DuckDB.Simple.V2.Registration`+when a native registration can guarantee whether its result can contain NULL+or non-NULL values. Each takes `target configure stats callbacks`. The planner+can use these facts to simplify queries.++For example, add a statistics callback to a scalar registration that always+returns a non-NULL value, including for NULL inputs. Here `configure` and+`callbacks` are the configuration action and execution callbacks of that+registration. `R` names `Database.DuckDB.Simple.V2.Registration`, and the native+functions come from `Database.DuckDB.FFI.V2`:++```haskell+R.registerScalarFunctionWithStats target configure+  (\info _ -> do+    result <- R.output (c_duckdb_v2_scalar_function_stats_get_result_stats info)+    R.checked (c_duckdb_v2_stats_set_can_have_null result 0))+  callbacks+```++Report only facts that hold for every result. An incorrect guarantee can give+wrong query results. Leave unknown statistics unchanged. An ungrouped aggregate+can produce a result for empty input, so its statistics must cover that result+too. The info, context, and statistics handles are borrowed. Do not destroy them+or keep them after the callback returns. Argument statistics are read-only;+change only result statistics. The module's Haddock describes the callback+contracts and ownership rules.  ## Loadable extensions 
duckdb-simple.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.4 name: duckdb-simple-version: 1.0.0.0+version: 1.0.1.0 license: MPL-2.0 license-file: LICENSE author: Matthias Pall Gissurarson@@ -23,7 +23,7 @@   functions. The API follows the style of sqlite-simple and postgresql-simple.    The backend uses the generated V2 API from duckdb-ffi. Upstream marks-  that API as unstable, so this release requires exactly DuckDB 2.0.0.+  that API as unstable, so this release requires exactly DuckDB 2.0.1.   It provides shared environments and instances, function registration,   native streaming, and Arrow batches. @@ -125,7 +125,7 @@     base >=4.18 && <5,     bytestring >=0.11 && <0.13,     containers >=0.6 && <0.9,-    duckdb-ffi >=2.0.0.0 && <2.0.1,+    duckdb-ffi >=2.0.1.0 && <2.0.2,     geometry-simple >=0.1.1.0 && <0.2,     hs-bindgen-runtime >=1.0.0.0 && <1.1,     text >=2.0 && <2.2,@@ -169,7 +169,7 @@     bytestring,     containers >=0.6 && <0.9,     directory >=1.3 && <1.4,-    duckdb-ffi >=2.0.0.0 && <2.0.1,+    duckdb-ffi >=2.0.1.0 && <2.0.2,     duckdb-simple,     filepath >=1.4 && <1.6,     geometry-simple >=0.1.1.0 && <0.2,@@ -226,7 +226,7 @@     base >=4.18 && <5,     dataframe-arrow-bridge >=1.0 && <1.1,     dataframe-core >=2.5 && <2.6,-    duckdb-ffi >=2.0.0.0 && <2.0.1,+    duckdb-ffi >=2.0.1.0 && <2.0.2,     duckdb-simple,     hs-bindgen-runtime >=1.0.0.0 && <1.1,     tasty >=1.4 && <1.6,
src/Database/DuckDB/Simple/Callback.hs view
@@ -150,7 +150,7 @@   pointer. readCallbackState reads the slot and dereferences the pointer. The   caller supplies the type, so it must be the type of the value in the slot. -These two rules are the behaviour of DuckDB 2.0.0. The header does not state+These two rules are the behaviour of DuckDB. The header does not state them.  - A setter that fails has not taken the pointer. Thus the handlers of@@ -235,7 +235,7 @@ the input error of the library when the setting is above 1. A registration with no callback, for example a custom type, a macro, or a custom option with no update function, does not check. The setting threads has only the GLOBAL scope-("cannot be set locally" in DuckDB 2.0.0), so each connection of an instance and+("cannot be set locally" in DuckDB), so each connection of an instance and the instance itself give the same value.  The check cannot see a later change. A statement SET threads = 4 after the@@ -353,9 +353,9 @@   them. With DuckDBV2ErrorApi, DuckDB invalidates the explicit transaction, and   COMMIT stores none of its rows. - ErrorRuntimeInternal and ErrorRuntimeFatal invalidate the database. For these-  codes, DuckDB 2.0.0 invalidates the database of the outer statement, also+  codes, DuckDB invalidates the database of the outer statement, also   when the error came from another database.-- ErrorIONetwork has no name. Its exception type has no name in DuckDB 2.0.0+- ErrorIONetwork has no name. Its exception type has no name in DuckDB   (EXCEPTION_MAP in exception.cpp). With its code, the outer statement fails   with ErrorApi and the prefix "Invalid Error: " of a type with no name. With   DuckDBV2ErrorApi, it fails with ErrorInputInvalid, as for an SQLError without@@ -377,7 +377,7 @@   and its body in the same way. - An SQLError of a type of typesWithoutOwnCode writes the code DuckDBV2ErrorApi   and its message.-- DuckDB 2.0.0 cannot raise some codes again with their own type. Six codes+- DuckDB cannot raise some codes again with their own type. Six codes   have no exception type in the map of the C API (TryGetExceptionTypeFromErrorCode   in capi_v2.cpp): the codes of ErrorApi, ErrorIOFileNotFound,   ErrorIOReadFailure, ErrorIOEof, ErrorInputParameterInvalid, and
src/Database/DuckDB/Simple/Catalog.hs view
@@ -7,7 +7,7 @@ -- -- 'catalogTypeName' and 'lookupCatalogEntry' run SQL on the V2 connection. -- The SQL reads the @duckdb_*()@ table functions, which are the metadata--- interface of DuckDB. The C API V2 of DuckDB 2.0.0 has no call that looks up+-- interface of DuckDB. The C API V2 of DuckDB has no call that looks up -- a catalog entry. The V1 C API has such calls, but it has no supported way -- to use a V2 connection. In a reply of 2026-10-20 to an extension author, -- DuckDB wrote: "we'll add a catalog access interface eventually"
src/Database/DuckDB/Simple/Config.hs view
@@ -66,7 +66,7 @@ -- 'configOptionDefault' is the static default of the declaration. 'Nothing' -- means that the declaration has no default. The value after @RESET@ can -- differ from the static default: @progress_bar_time@ has no static default,--- and DuckDB 2.0.0 gives 2000 after @RESET@.+-- and DuckDB gives 2000 after @RESET@. data ConfigOption = ConfigOption   { configOptionName :: Text   , configOptionDescription :: Text
src/Database/DuckDB/Simple/Config/Internal.hs view
@@ -37,7 +37,9 @@ -- exception in the function rejects the value. The text of the exception is the -- SQL error. A RESET gives the default value to the function, or -- 'Database.DuckDB.Simple.FromField.FieldNull' for an option without a default.--- Thus the function must accept the default value. A VARIANT gives its+-- Thus the function must accept the default value. RESET removes the override+-- at its scope. A SESSION reset can expose a GLOBAL value instead of the+-- declared default. A VARIANT gives its -- contained value, as in 'Database.DuckDB.Simple.Config.getConfigValue'. data CustomOption = CustomOption   { customOptionName :: !Text
src/Database/DuckDB/Simple/Element.hs view
@@ -69,7 +69,7 @@  - Read (chunkIsRowValid). The V2 API has no call that reads the validity of   one row, so the reader reads the mask memory from duckdb_v2_vector_get_view.-  The header does not say what a null mask pointer means. In DuckDB 2.0.0 a+  The header does not say what a null mask pointer means. In DuckDB a   null pointer means that the vector has no mask, and then every row is valid.  - Write (markValid and markInvalid). The writer sets or clears one bit of the@@ -218,7 +218,7 @@ decodeDuckDBTimeNs :: Int64 -> TimeOfDay decodeDuckDBTimeNs = decodeTimeUnits 1000000000 --- | The largest UTC offset of a TIMETZ value in seconds: 15:59:59. DuckDB 2.0.0+-- | The largest UTC offset of a TIMETZ value in seconds: 15:59:59. DuckDB -- defines it as @dtime_tz_t::MAX_OFFSET@ in -- @src/include/duckdb/common/types/datetime.hpp@. The low 24 bits of a TIMETZ -- value hold this maximum minus the offset (@encode_offset@ and@@ -230,7 +230,7 @@ timeTzMaxOffsetSeconds = 16 * 60 * 60 - 1  -- | The number of low bits of a TIMETZ value that hold the biased offset. The--- high 40 bits hold the time of day in microseconds. DuckDB 2.0.0 names the two+-- high 40 bits hold the time of day in microseconds. DuckDB names the two -- widths @dtime_tz_t::OFFSET_BITS@ and @dtime_tz_t::TIME_BITS@. timeTzOffsetBits :: Int timeTzOffsetBits = 24
src/Database/DuckDB/Simple/Extension.hs view
@@ -70,7 +70,7 @@ --   loaded extension apply. Extensions built with different GHC versions in one process are not tested. The runtime --   stays until the process ends. Do not call @hs_exit@. -- - The process supports one DuckDB implementation.--- - The extension loads only into the DuckDB release of the pinned header, 2.0.0.+-- - The extension loads only into the DuckDB release of the pinned header, 2.0.1. -- - In extension mode, duckdb-ffi exposes only "Database.DuckDB.FFI.V2". -- - A replacement scan of an extension belongs to the instance, not to one connection. -- - The example links the GHC and package libraries dynamically. It loads only on a machine where its run paths
src/Database/DuckDB/Simple/FromField.hs view
@@ -90,12 +90,13 @@  ) import Database.DuckDB.Simple.Native (arraySizeReason) import Database.DuckDB.Simple.Time (Date, LocalTimestamp, UTCTimestamp, Unbounded (..))-import GHC.Float (double2Float, float2Double, int2Double, int2Float, rationalToDouble, rationalToFloat, word2Double)+import GHC.Float (double2Float, float2Double, int2Float, rationalToDouble, rationalToFloat) import GHC.Num.Integer (integerFromWordList) import GHC.TypeLits (KnownNat, Nat, natVal) import Numeric.Natural (Natural)  -- | Internal representation of a column value.+-- 'FieldType' contains the logical type carried by a SQL @TYPE@ value. -- 'FieldGeometry' contains raw WKB and CRS metadata. A STRUCT, TUPLE, or UNION -- parameter binds it as a GEOMETRY with the same CRS. A VARIANT parameter -- binds it only when it has no CRS. A top-level t'RawGeometry' parameter binds@@ -129,6 +130,7 @@   | FieldTimeTZ TimeWithZone   | FieldBit BitString   | FieldBigNum BigNum+  | FieldType LogicalTypeRep   | FieldEnum Word32 Text   | FieldArray (Array Int FieldValue)   | FieldList [FieldValue]@@ -168,7 +170,7 @@ -- header and a big-endian payload. Negative values have a complemented payload. -- Restore that payload, then split it into machine-word limbs for -- @integerFromWordList@, with the most significant limb first.--- This is the BIGNUM storage of DuckDB 2.0.0. The V2 API does not commit to this+-- This is the BIGNUM storage of DuckDB. The V2 API does not commit to this -- layout, and this library does not use this function. fromBigNumBytes :: [Word8] -> Integer fromBigNumBytes bytes =@@ -194,7 +196,7 @@ -- | Encode an @Integer@ as a DuckDB BIGNUM blob. Write the 3-byte header -- (sign bit and payload length), then the magnitude bytes in big-endian order. -- Complement the header and the payload for a negative value.--- This is the BIGNUM storage of DuckDB 2.0.0. The V2 API does not commit to+-- This is the BIGNUM storage of DuckDB. The V2 API does not commit to -- this layout, and this library does not use this function. toBigNumBytes :: Integer -> [Word8] toBigNumBytes value =@@ -502,7 +504,8 @@   another value. Write c::VARIANT. A VARIANT column decodes only to values of   the VARIANT shape. A JSON type at any depth raises Incompatible, because   DuckDB parses its text when it casts to VARIANT. Write c::VARIANT for the-  parsed value. The bind side+  parsed value. For a type that contains TYPE, the cast only accepts SQL NULL.+  The bind side   (variantPayloadType in "Database.DuckDB.Simple.ToField") accepts more: it   binds a FieldArray and a FieldTuple as a VARIANT list, and the fields of each   FieldStruct as VARIANT values.@@ -522,6 +525,13 @@ instance FromField FieldValue where   fromField Field{fieldValue} = pure fieldValue +-- | Read the logical type carried by a SQL @TYPE@ value.+instance FromField LogicalTypeRep where+  fromField f@Field{fieldValue} = case fieldValue of+    FieldType logical -> pure logical+    FieldNull -> returnError UnexpectedNull f ""+    _ -> returnError Incompatible f "expected TYPE"+ -- | Reads GEOMETRY, with its CRS. instance FromField RawGeometry where   fromField f@Field{fieldValue} =@@ -790,9 +800,8 @@     case fieldValue of       -- DuckDB casts an integer to FLOAT with static_cast<float>, which       -- rounds one time to the nearest value, ties to even. int2Float rounds-      -- in the same way. As seen in CI, the DuckDB 2.0.0 build for macOS on-      -- Apple Silicon rounds some BIGINT values above 2^53 two times in its-      -- cast to FLOAT, and the library gives the nearest value there.+      -- in the same way. The library gives the nearest value independently+      -- of platform-specific native cast rounding.       FieldInt i -> pure (int2Float (fromIntegral i))       FieldDecimal value -> pure (decimalToFloat value)       _ -> do@@ -804,40 +813,23 @@ {- Note [DECIMAL as DOUBLE and FLOAT] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A DECIMAL that a Double or a Float reads gives the value of the cast of DuckDB-2.0.0 to DOUBLE or FLOAT. The cast is TryCastDecimalToFloatingPoint in-src/common/operator/cast_operators.cpp of DuckDB. decimalToDouble and-decimalToFloat use the same three paths:--1. Scale 0 and width 19 to 38. DuckDB stores the value as a HUGEINT and casts-   it with CastBigintToFloating in src/common/types/hugeint.cpp:--     case -1: result = -REAL_T(NumericLimits<uint64_t>::Maximum() - input.lower) - 1;-     default: result = REAL_T(input.lower) + REAL_T(input.upper) * (REAL_T(NumericLimits<uint64_t>::Maximum()) + 1);--   The case is the value of input.upper. A FLOAT result is this DOUBLE value-   cast to float (Hugeint::TryCast for float). hugeIntToDouble copies the-   formula. The formula rounds more than one time, so it does not always give-   the nearest value. DuckDB casts -9007199254740994::DECIMAL(19,0) to-   -9007199254740992.0, and 3356951675583485076575379654993209529::DECIMAL(38,0)-   to 3.356951675583485e36. The nearest values are -9007199254740994.0 and-   3.3569516755834854e36. This path exists only to give the value of the cast.-2. Scale 0 with a width of at most 18, or an integer of at most 2^53 (2^24-   for FLOAT) with a scale of at most 22 (10 for FLOAT). DuckDB converts the-   integer to the floating-point type and divides it by the power of ten in-   that type. Under the limits, the integer and the power of ten are exact in-   the type, so only the division rounds. With scale 0, the division by 1 is-   exact, so only the conversion of the integer rounds. Each step rounds one-   time to the nearest value.-3. Each other value. DuckDB converts the decimal digits with fast_float, which-   gives the nearest value.+to DOUBLE or FLOAT. TryCastDecimalToFloatingPoint in+src/common/operator/cast_operators.cpp uses these paths: -Thus paths 2 and 3 give the nearest value, with ties to even. In path 2 the-library does the same steps. With scale 0 and a width of at most 18, the-integer is an Int64 value, and fromInteger converts it with int2Double or-int2Float, which round one time as the C++ conversion of int64_t does. In path-3 the library uses rationalToDouble or rationalToFloat. fromRational uses these-functions too. They give the nearest value with ties to even, and they do not-reduce the fraction first.+1. Scale 0. DuckDB converts the stored integer directly to the target type.+   For a HUGEINT, CastBigintToFloating in src/common/types/hugeint.cpp converts+   its unsigned magnitude with Uhugeint::TryCast. That conversion folds the+   discarded bits into a sticky bit before rounding. It gives the nearest+   value, with ties to even. The conversion to FLOAT does not pass through DOUBLE. The+   library uses fromInteger for both target types.+2. An integer with magnitude at most 2^53 (2^24 for FLOAT) and scale at most 22+   (10 for FLOAT). DuckDB converts the integer to the target type and divides+   it by the power of ten in that type. Both inputs are exact, so only the+   division rounds. The library uses the same steps.+3. Each other value. DuckDB converts the decimal digits with fast_float,+   which gives the nearest value, with ties to even. The library uses+   rationalToDouble or rationalToFloat with the unscaled integer and 10^scale.+   These functions give the same rounding without reducing the fraction first.  Path 3 is slower than path 2. It applies to a Double with a scale above 0 when the unscaled integer is above 2^53 or the scale is above 22: at scale 2 for an@@ -849,8 +841,7 @@ -- | The value of the cast of DuckDB from a DECIMAL to DOUBLE. -- See Note [DECIMAL as DOUBLE and FLOAT]. decimalToDouble :: DecimalValue -> Double-decimalToDouble DecimalValue{decimalWidth, decimalScale, decimalInteger}-  | decimalScale == 0 && decimalWidth > 18 = hugeIntToDouble decimalInteger+decimalToDouble DecimalValue{decimalScale, decimalInteger}   | decimalScale == 0 = fromInteger decimalInteger   | decimalScale <= 22 && abs decimalInteger <= 9007199254740992 = fromInteger decimalInteger / 10 ^ decimalScale   | otherwise = rationalToDouble decimalInteger (10 ^ decimalScale)@@ -858,27 +849,11 @@ -- | The value of the cast of DuckDB from a DECIMAL to FLOAT. -- See Note [DECIMAL as DOUBLE and FLOAT]. decimalToFloat :: DecimalValue -> Float-decimalToFloat DecimalValue{decimalWidth, decimalScale, decimalInteger}-  | decimalScale == 0 && decimalWidth > 18 = double2Float (hugeIntToDouble decimalInteger)+decimalToFloat DecimalValue{decimalScale, decimalInteger}   | decimalScale == 0 = fromInteger decimalInteger   | decimalScale <= 10 && abs decimalInteger <= 16777216 = fromInteger decimalInteger / 10 ^ decimalScale   | otherwise = rationalToFloat decimalInteger (10 ^ decimalScale) --- | The cast of DuckDB from a HUGEINT to DOUBLE. It is not always the nearest--- value. See Note [DECIMAL as DOUBLE and FLOAT].-hugeIntToDouble :: Integer -> Double-hugeIntToDouble value-  | upper == -1 = negate (word2Double (maxBound - lower)) - 1-  | otherwise = word2Double lower + int2Double upper * 18446744073709551616-  where-    -- The two 64-bit halves of the HUGEINT in two's complement. word2Double-    -- and int2Double round one time to the nearest value, ties to even, as-    -- the C++ conversions of uint64_t and int64_t to double do. The C++-    -- factor is 2^64 after its rounding, so the product is exact. The sum or-    -- the difference rounds one time, as in C++.-    upper = fromInteger (value `shiftR` 64) :: Int-    lower = fromInteger value :: Word- -- | Reads VARCHAR, TINYINT to BIGINT as decimal digits, BOOLEAN as @true@ or -- @false@, and the label of an ENUM value. It does not read FLOAT or DOUBLE. See -- Note [Reads of another SQL type].@@ -1237,6 +1212,7 @@   FieldTimeTZ{} -> scalar LogicalTypeIdTimeTz   FieldBit{} -> scalar LogicalTypeIdBit   FieldBigNum{} -> scalar LogicalTypeIdBigNum+  FieldType{} -> scalar LogicalTypeIdType   FieldEnum{} -> "ENUM"   FieldArray{} -> "ARRAY"   FieldList{} -> "LIST"
src/Database/DuckDB/Simple/Internal.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE BlockArguments #-}+{-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MagicHash #-}@@ -119,7 +120,7 @@ import Data.Coerce (Coercible, coerce) import Data.IORef (IORef, atomicModifyIORef', atomicWriteIORef, mkWeakIORef, modifyIORef', newIORef, readIORef, writeIORef) import qualified Data.IntMap.Strict as IntMap-import Data.List (dropWhileEnd, find, intercalate, partition)+import Data.List (dropWhileEnd, find, partition) import Data.Maybe (fromMaybe, isJust) import Data.Text (Text) import qualified Data.Text as Text@@ -474,6 +475,7 @@  -- | Require the DuckDB release that the V2 bindings describe. Upstream marks -- the V2 API as unstable, so another release can change its functions.+-- Use the package version because the native API version can stay unchanged. checkLibraryVersion :: IO () checkLibraryVersion = do   actual <- output c_duckdb_v2_library_version >>= readText@@ -491,7 +493,7 @@           Nothing       )   where-    expected = Text.pack ('v' : intercalate "." (map show [dUCKDB_V2_API_VERSION_MAJOR, dUCKDB_V2_API_VERSION_MINOR, dUCKDB_V2_API_VERSION_PATCH]))+    expected = "v" <> Text.intercalate "." (take 3 (Text.splitOn "." VERSION_duckdb_ffi))  -- | Close an environment and its instances and connections. -- Wait for child creation and all descendant operations to finish before closing.@@ -564,6 +566,7 @@       [ "enable_logging"       , "logging_mode"       , "logging_level"+      , "logging_sink"       , "logging_storage"       , "enabled_log_types"       , "disabled_log_types"@@ -673,7 +676,7 @@ -- must transfer or release them under a mask. parseStatements :: DuckDBV2ConnectionHandle -> Query -> IO [DuckDBV2SqlStatementHandle] parseStatements raw query = mask_ $ withQueryCString query \sql -> do-  iterator <- output (c_duckdb_v2_parse_sql raw sql)+  iterator <- output (c_duckdb_v2_parse_sql raw sql 1)   let destroyIterator = destroy c_duckdb_v2_statement_iterator_destroy iterator       gather owned = do         parsed <- flip onException (releaseParsed owned) do@@ -712,7 +715,7 @@   flip onException (destroy c_duckdb_v2_prepared_statement_destroy handle) do     -- The caller retains its parsed handle, so keep a separate metadata copy.     text <- output (c_duckdb_v2_sql_statement_get_text parsed) >>= readText-    copied <- withQueryCString (Query text) \sql -> bracket (output (c_duckdb_v2_parse_sql raw sql)) (destroy c_duckdb_v2_statement_iterator_destroy) \iterator ->+    copied <- withQueryCString (Query text) \sql -> bracket (output (c_duckdb_v2_parse_sql raw sql 0)) (destroy c_duckdb_v2_statement_iterator_destroy) \iterator ->       output (c_duckdb_v2_statement_iterator_next iterator)     flip onException (destroy c_duckdb_v2_sql_statement_destroy copied) do       addStatementCollections query collections copied
src/Database/DuckDB/Simple/LogicalRep.hs view
@@ -58,7 +58,7 @@ {- Note [Type aliases] ~~~~~~~~~~~~~~~~~~~~~~ DuckDB can keep an alias on a type. The alias uses the value representation of-its base type, and the type text of DuckDB gives the alias. In DuckDB 2.0.0 these+its base type, and the type text of DuckDB gives the alias. In DuckDB these types have an alias:  - JSON. It is VARCHAR with the alias JSON, also as a child type, for example@@ -100,7 +100,7 @@   aliasBase. A value that the library reads and binds again keeps its type when   the native default cast supports its aliases, for example STRUCT(k JSON). -Alias metadata does not imply native cast support. DuckDB 2.0.0 cannot cast a+Alias metadata does not imply native cast support. DuckDB cannot cast a TIME value to an alias of TIME through the default cast. The library writes TIME alias payloads through public vector storage. See Note [TIME alias values] in "Database.DuckDB.Simple.ToField". LogicalTypeRep stores the alias@@ -148,13 +148,13 @@ {- Note [Type texts] ~~~~~~~~~~~~~~~~~~~~ logicalTypeSQL gives the text of a type that duckdb_v2_logical_type_to_text of-DuckDB 2.0.0 gives, for example DECIMAL(18,3), BIGINT[], INTEGER[3],+DuckDB gives, for example DECIMAL(18,3), BIGINT[], INTEGER[3], MAP(VARCHAR, BIGINT), ENUM('sad', 'happy'), and GEOMETRY('OGC:CRS84'). An alias gives its name, for example JSON. The function is pure, so an error of a FromField instance can name the type with no native handle, and the text costs nothing until an error renders. -These rules follow LogicalType::ToString of DuckDB 2.0.0:+These rules follow LogicalType::ToString of DuckDB:  - A scalar type has the name of its type id (logicalTypeIdName in   "Database.DuckDB.Simple.Native"), and SQL NULL is "NULL" in double quotes. A@@ -166,7 +166,7 @@   quotes when it has a character that is not an ASCII letter, a digit, or an   underscore, when it starts with a digit or an underscore followed by a digit,   or when it is a keyword of DuckDB in any case (SQLIdentifier::ToString).-  The keywords are the 502 names of duckdb_keywords() of DuckDB 2.0.0.+  The keywords are the 502 names from duckdb_keywords(). - An empty STRUCT or TUPLE has only its type name, with no parentheses.  A test case compares the text with duckdb_v2_logical_type_to_text for one type of@@ -210,7 +210,7 @@         || Set.member (Text.toLower name) duckDBKeywords     plain index char = isAsciiLower char || isAsciiUpper char || char == '_' || (index > 0 && isDigit char) --- | The keywords of DuckDB 2.0.0, from duckdb_keywords(). A field name that is+-- | The keywords of DuckDB, from duckdb_keywords(). A field name that is -- a keyword needs quotes. See Note [Type texts]. duckDBKeywords :: Set Text duckDBKeywords =@@ -256,10 +256,9 @@ -- name of a type from 'Database.DuckDB.Simple.Registration.registerCustomType' -- keeps that name, so a cast to it is a cast to the custom type. A name from -- SQL @CREATE TYPE name AS base@ is only another name for the base type, so a--- cast to it is a cast to the base type. In a test with DuckDB 2.0.0, a--- registered cast from BIGINT to a name for BIGINT did not run. The header of--- DuckDB 2.0.0 says that a catalog type name is not kept, but DuckDB 2.0.0--- keeps the name of a type from+-- cast to it is a cast to the base type. A registered cast from BIGINT to+-- a name for BIGINT does not run. The header says that a catalog type name is+-- not kept. DuckDB keeps the name of a type from -- 'Database.DuckDB.Simple.Registration.registerCustomType'. data TypeDeclaration = StructuredType LogicalTypeRep | TypeExpression Text   deriving (Eq, Show)
src/Database/DuckDB/Simple/Materialize.hs view
@@ -252,17 +252,16 @@   call vector_flatten first." The flatten also changes a CONSTANT or DICTIONARY   vector to FLAT. -- In DuckDB 2.0.0 the view of a FLAT vector has no selection+- In DuckDB the view of a FLAT vector has no selection   (duckdb_v2_vector_get_view in src/main/capi/v2/capi_v2_vector.cpp). The header   does not say this. Then the physical row is the logical row. The readers of-  VARIANT and GEOMETRY need this, because they read logical rows (rejectRows,-  prepareVariantReader, and prepareGeometryReader). The reader still applies-  the selection of a view that has one.+  TYPE, VARIANT, and GEOMETRY use native calls that take logical rows.+  The reader still applies the selection of a view that has one.  - The flatten changes the buffers of the vector, so it runs before the view is   read. -- In DuckDB 2.0.0 the flatten is recursive: it also flattens the child of a+- In DuckDB the flatten is recursive: it also flattens the child of a   LIST or MAP vector and the fields of a STRUCT vector (VectorListBuffer::Flatten   and VectorStructBuffer::Flatten). The header does not say this. The MAP   decoder (prepareMapDecoder) depends on this, because it takes the key and@@ -273,7 +272,7 @@ owned vector from withOwnedVector does not flatten (materializeValue, and the writers of one-row vectors in "Database.DuckDB.Simple.ToField"). -- The writer sets the size before the flatten. In DuckDB 2.0.0,+- The writer sets the size before the flatten. In DuckDB,   duckdb_v2_vector_flatten flattens the rows of the current size of the vector:   it calls Vector::Flatten without a row count   (src/main/capi/v2/capi_v2_vector.cpp). duckdb_v2_vector_set_size reserves@@ -283,7 +282,7 @@ - A new vector of a data chunk is FLAT. The header of   duckdb_v2_data_chunk_create says: "Allocates one FLAT vector per element of   the types array". Thus the writer of an owned vector needs no flatten. In-  DuckDB 2.0.0 the children of such a vector are FLAT too: the vector cache+  DuckDB the children of such a vector are FLAT too: the vector cache   resets each child of a LIST, ARRAY, or STRUCT vector to FLAT   (VectorCacheEntry::ResetFromCache in src/common/types/vector_cache.cpp). The   header does not say this for the children. The writer of a UNION value writes@@ -365,6 +364,10 @@ prepareValueReader readers tree labels dtype vector dataPtr validity = case dtype of   DuckDBV2LogicalTypeIdVariant -> variantReader readers vector   DuckDBV2LogicalTypeIdGeometry -> geometryReader readers vector+  DuckDBV2LogicalTypeIdType ->+    pure $ whenValid FieldType \row ->+      bracket (output (c_duckdb_v2_vector_get_value vector (fromIntegral row))) destroyValue \value ->+        bracket (output (c_duckdb_v2_value_get_type value)) destroyLogicalType (typeReader readers)   DuckDBV2LogicalTypeIdArray -> whenValid FieldArray <$> prepareArrayDecoder readers tree labels vector   DuckDBV2LogicalTypeIdList -> whenValid FieldList <$> prepareListDecoder readers tree labels vector dataPtr   DuckDBV2LogicalTypeIdMap -> whenValid FieldMap <$> prepareMapDecoder readers vector dataPtr
src/Database/DuckDB/Simple/Native.hs view
@@ -95,7 +95,7 @@ import Foreign.Storable (Storable, peek, poke, sizeOf)  -- | The largest size of a DuckDB ARRAY: @ArrayType::MAX_ARRAY_SIZE@ in--- @src/include/duckdb/common/types.hpp@ of DuckDB 2.0.0. See+-- @src/include/duckdb/common/types.hpp@ of DuckDB. See -- Note [Sized ARRAY bounds]. maxArraySize :: Int maxArraySize = 100000@@ -600,7 +600,7 @@ DuckDBV2ErrorApi and the message for them. See Note [Errors that leave a callback] in "Database.DuckDB.Simple.Callback". The type ErrorRuntimeInternal, and ErrorRuntimeFatal, must never go into the error-slot of a callback, because DuckDB 2.0.0 then invalidates the database, and+slot of a callback, because DuckDB then invalidates the database, and each later statement raises "FATAL Error: ... database has been invalidated". This holds for a callback at execution time, for example a scalar function, a table function, a cast, or COPY FROM; at bind time, for example in a@@ -621,7 +621,7 @@ withSQLStatement :: DuckDBV2ConnectionHandle -> Text -> (DuckDBV2SqlStatementHandle -> IO a) -> IO a withSQLStatement connection sql action =   TextForeign.withCString sql $ \source ->-    bracket (output (c_duckdb_v2_parse_sql connection (ConstPtr source))) (destroy c_duckdb_v2_statement_iterator_destroy) $ \iterator ->+    bracket (output (c_duckdb_v2_parse_sql connection (ConstPtr source) 0)) (destroy c_duckdb_v2_statement_iterator_destroy) $ \iterator ->       bracket (output (c_duckdb_v2_statement_iterator_next iterator)) (destroy c_duckdb_v2_sql_statement_destroy) action  -- | Borrow a factory during its native callback. Do not retain or destroy it.@@ -894,7 +894,7 @@   DuckDBV2LogicalTypeIdTuple -> pure LogicalTypeIdTuple   DuckDBV2LogicalTypeId value -> throwUnknownNative "logical type id" value --- | The name of a type id in DuckDB 2.0.0. duckdb_v2_logical_type_get_name+-- | The name of a type id in DuckDB. duckdb_v2_logical_type_get_name -- gives this name for a type with no alias. See Note [Type aliases] in -- "Database.DuckDB.Simple.LogicalRep". logicalTypeIdName :: LogicalTypeId -> Text
src/Database/DuckDB/Simple/ToField.hs view
@@ -217,7 +217,7 @@   "Database.DuckDB.Simple.Internal". A microsecond value gives the same result   for each parameter type, so the read has no use for it. -- Why a microsecond parameter gets the microsecond value. In DuckDB 2.0.0, the+- Why a microsecond parameter gets the microsecond value. In DuckDB, the   SQL literal '03:04:05.123456999'::TIME gives 03:04:05.123456, but a cast from   TIME_NS to TIME rounds and gives 03:04:05.123457. The same holds for   TIMESTAMP. A nanosecond value would thus change what a microsecond column@@ -225,9 +225,9 @@  - An explicit cast of the parameter to a nanosecond type, as in   ?::TIMESTAMP_NS, makes it a nanosecond parameter. DuckDB then rounds when it-  casts the value to a microsecond type, for example for a TIMESTAMP column or-  in ?::TIMESTAMP_NS + INTERVAL 1 DAY. A plain parameter for a microsecond-  type cuts the digits.+  casts the value to a microsecond type, for example for a TIMESTAMP column.+  DuckDB keeps nanosecond precision in TIMESTAMP_NS arithmetic with an+  INTERVAL. A plain parameter for a microsecond type cuts the digits.  - A LocalTime for a TIMESTAMP_NS parameter, or for the TIMESTAMP_NS elements   of a SizedArray, a list, or a Map, is in one of three zones:@@ -591,18 +591,9 @@   SQLError with the type ErrorTypeConversion. Each other parameter, and a   parameter with no known type, gets a BIGNUM value. -- Why a HUGEINT. In DuckDB 2.0.0, the casts from BIGNUM to the integer types-  have defects (Bignum::BignumToInt in-  src/include/duckdb/common/types/bignum.hpp):-  - A cast to HUGEINT or UHUGEINT fails for each value except 0 and -1.-  - A cast of a negative value to an unsigned type gives a wrapped value and-    no error. For example, -5 for a UBIGINT gives 18446744073709551611.-  - A cast reduces a value of 2^128 or more modulo 2^128 before it checks the-    range. For example, 2^128 + 5 for a BIGINT gives 5.-  DuckDB 2.0.0 has no cast from BIGNUM to DECIMAL. DuckDB checks the range of-  each cast from HUGEINT and UHUGEINT. For TINYINT to UBIGINT, DOUBLE,-  VARCHAR, BIGNUM, JSON, and VARIANT, the cast from HUGEINT gives the same-  result as the cast from BIGNUM where the cast from BIGNUM has no defect.+- Why a HUGEINT. HUGEINT and UHUGEINT support casts to DECIMAL. BIGNUM+  has no cast to DECIMAL. The binding uses HUGEINT within its range and+  checks the parameter range before it binds a larger integer.  - Why the binding reads the parameter type only outside the HUGEINT range. A   read of the parameter type runs the binder of DuckDB. See@@ -611,8 +602,8 @@  - What stays BIGNUM:   - A value outside the HUGEINT range for a parameter with no known type, as-    in SELECT ?, binds as a BIGNUM. A cast that DuckDB adds later can still-    have the defects above.+    in SELECT ?, binds as a BIGNUM. DuckDB checks the range of a later+    cast to an integer type.   - A BigNum always binds as a BIGNUM.    Collection values infer their common type as described below and in@@ -622,9 +613,9 @@   rule (hugeIntInRange): a value from -2^127 to 2^127 - 1 gives a HUGEINT   value. A Natural from 2^127 to 2^128 - 1 gives a UHUGEINT value. Each other   value raises an SQLError of the type ErrorTypeConversion (outsideRange).-  toDuckValue has no target type. A BIGNUM value would thus reach a HUGEINT-  column, and DuckDB 2.0.0 casts the BIGNUM 2^128 to the HUGEINT 0 with no-  error. The callers of toDuckValue are setConfigValue, defaultValue, the+  toDuckValue has no target type. It checks the range before a value reaches+  the declared HUGEINT or UHUGEINT column. The callers of toDuckValue are+  setConfigValue, defaultValue, the   typed rows of table functions and COPY FROM, and the writer of a scalar   function result. Use BigNum for a larger value.   The default toField of the ToField class also calls toDuckValue. Thus a@@ -652,8 +643,8 @@   of the cast of each element. For example, [5, -6] for a UBIGINT[2] column   raises an error. A SizedArray or a list of BigNum values also takes the element   type of the target, but each element is a BIGNUM value. DuckDB casts each-  element with the casts from BIGNUM, so the result for BigNum elements does-  not improve. For each other target, and for a parameter with no known type,+  element with the checked integer casts from BIGNUM in DuckDB.+  For each other target, and for a parameter with no known type,   a list takes the type that its elements give (withValueType), as each scalar   Integer and as a SQL list literal of the HUGEINT and BIGNUM values: [1, 2^64]   gives HUGEINT[], and [5, 2^130] gives BIGNUM[]. An Integer has no fixed SQL@@ -742,7 +733,7 @@  -- | Bind a local time as a TIMESTAMP, or as a TIMESTAMP_NS. See -- Note [Nanosecond parameters]. A time outside the range of DuckDB, from--- -290308-12-22 00:00:00 (290309-12-22 BC) to 294247-01-10 04:00:54.775806 in+-- -290308-12-21 19:59:05.224192 (290309-12-21 BC) to 294247-01-10 04:00:54.775806 in -- the notation of "Data.Time", raises an t'SQLError' of the type -- 'ErrorTypeConversion'. For a TIMESTAMP_NS target, a time outside the range -- of TIMESTAMP_NS can get another error, also a time outside the range of@@ -787,6 +778,10 @@       ("<geometry length=" <> show (BS.length rawGeometryWKB) <> ">")       \builder -> rawGeometryDuckValue builder rawGeometryCRS raw +-- | Bind a logical type as a SQL @TYPE@ value.+instance ToField LogicalTypeRep where+  toField rep = builderValueBinding (Text.unpack (logicalTypeSQL rep)) (`typeDuckValue` rep)+ -- | Bind the payload as a VARIANT. instance ToField Variant where   toField value =@@ -966,6 +961,7 @@     FieldTimeTZ v -> toField v     FieldBit v -> toField v     FieldBigNum v -> toField v+    FieldType v -> toField v     FieldEnum _ label -> toField label     FieldArray items       | Just failure <- arraySizeError (length items) -> valueBinding (show value) (const failure)@@ -1107,6 +1103,10 @@ instance DuckDBColumnType TimeWithZone where   duckdbColumnTypeFor _ = "TIMETZ" +-- | The SQL type of a value that carries a logical type.+instance DuckDBColumnType LogicalTypeRep where+  duckdbColumnTypeFor _ = "TYPE"+ instance DuckDBColumnType UTCTime where   duckdbColumnTypeFor _ = "TIMESTAMPTZ" @@ -1173,7 +1173,7 @@   raises Incompatible, which names the column type and the SizedArray type. - Sizes. A DuckDB ARRAY has 1 to 100000 elements (maxArraySize). DuckDB   rejects the SQL type T[0] and a larger type, for example with "ARRAY type-  size must be at most 100000". The value API of DuckDB 2.0.0 does not check+  size must be at most 100000". The value API of DuckDB does not check   the largest size, so a parameter of a larger ARRAY would bind a value that   the SQL of DuckDB cannot write. Thus the library checks both sizes, as an   input check: the declaration of a SizedArray of another size gives Left,@@ -1415,7 +1415,7 @@   Native equality distinguishes aliases, including the alias "NULL". When   each remaining element has the type of the first one, that type is the   element type. The check uses native equality to keep aliases and ENUM dictionaries.-  valuesHaveType checks the metadata that equality omits. In DuckDB 2.0.0,+  valuesHaveType checks the metadata that equality omits. In DuckDB,   VARCHAR equality ignores collations. STRUCT and UNION names compare without   ASCII case. GEOMETRY CRS definitions can compare equal through a shared   identifier. The metadata check keeps exact names, collations, and definitions.@@ -1437,7 +1437,9 @@   set with no common type surfaces the cast error."). For example, a STRUCT(a   INTEGER) and a STRUCT(b INTEGER) give STRUCT(a INTEGER, b INTEGER), with NULL   for a field that an element does not have. A BIGINT field and a DOUBLE field-  give a DOUBLE field, which rounds a large BIGINT, as in SQL.+  give a DOUBLE field, which rounds a large BIGINT, as in SQL. DuckDB+  also merges UNION members and gives matching members their common payload+  type. This rule applies inside nested collections. - Elements with no common type raise the SQLError of DuckDB. For a STRUCT(a   INTEGER) and a STRUCT(a VARCHAR), it is "Cannot combine types" of the type   ErrorQueryNotImplemented. The SQL list literal raises a Binder Error for the@@ -1640,7 +1642,7 @@ does not document that cast.  The vector construction uses the documented layout and writes. It requires-FLAT child vectors. DuckDB 2.0.0 supplies them in a new vector, but the header+FLAT child vectors. DuckDB supplies them in a new vector, but the header does not guarantee this for the children. A change can make the writes fail or give a wrong value. See Note [Vector flatten] in "Database.DuckDB.Simple.Materialize". Regression cases check the selected@@ -1678,7 +1680,7 @@  {- Note [TIME alias values] ~~~~~~~~~~~~~~~~~~~~~~~~~~-DuckDB 2.0.0 rejects the default cast from TIME to a TIME alias. The public+DuckDB rejects the default cast from TIME to a TIME alias. The public API specifies that an alias keeps its base representation. TIME vector data uses int64 microseconds since midnight, as encodeTimeOfDay does. Thus a checked TIME payload can be written through vector_get_data_mutable without a cast.@@ -1708,6 +1710,7 @@   -- Note [Type aliases] in "Database.DuckDB.Simple.LogicalRep".   (LogicalTypeAlias _ base, _) -> fieldValueWithTypeDuckValue builder base value   (LogicalTypeScalar LogicalTypeIdVariant, _) -> variantDuckValue builder value+  (LogicalTypeScalar LogicalTypeIdType, FieldType logical) -> typeDuckValue builder logical   (LogicalTypeScalar dtype, _) -> scalarFieldValueDuckValue factory dtype value   (LogicalTypeCollatedVarchar _, FieldText text) -> textDuckValue factory text   (LogicalTypeGeometry crs, FieldGeometry raw) -> rawGeometryDuckValue builder crs raw@@ -1916,7 +1919,7 @@ geometry leaves, including typed NULLs. A connection factory has no such limit. -DuckDB 2.0.0 stores GEOMETRY as WKB. The BLOB cast and ST_GeomFromWKB both use+DuckDB stores GEOMETRY as WKB. The BLOB cast and ST_GeomFromWKB both use Geometry::FromBinary (src/function/cast/geo_casts.cpp and src/function/scalar/geometry/geometry_functions.cpp). The result reader casts GEOMETRY to BLOB with Geometry::ToBinary, which copies the stored bytes@@ -1964,8 +1967,8 @@ -- The C API cannot construct a nonempty object whose keys are all empty. -- DuckDB 2.0 treats that STRUCT type as a tuple and casts it to a VARIANT array. -- A raw geometry without a CRS uses GEOMETRY. See Note [Raw GEOMETRY parameters].--- A VARIANT does not keep a UNION, a MAP, an ENUM, or the CRS of a geometry, so--- each of these payloads raises an error, at each depth.+-- A VARIANT cannot keep TYPE, UNION, MAP, ENUM, or the CRS of a geometry.+-- Each of these payloads raises an error at each depth. variantPayloadType :: FieldValue -> IO (LogicalTypeRep, FieldValue) variantPayloadType value = case value of   FieldNull -> pure (variant, value)@@ -1999,6 +2002,7 @@   FieldTimeTZ{} -> scalar LogicalTypeIdTimeTz   FieldInterval{} -> scalar LogicalTypeIdInterval   FieldBigNum{} -> scalar LogicalTypeIdBigNum+  FieldType{} -> rejectPayload "a TYPE value" "DuckDB VARIANT has no TYPE payload"   FieldBit{} -> scalar LogicalTypeIdBit   FieldList{} -> pure (LogicalTypeList variant, value)   FieldArray items -> pure (LogicalTypeList variant, FieldList (elems items))@@ -2011,17 +2015,30 @@       throwInvalidArgument "DuckDB's C API cannot construct a VARIANT object with only an empty key"     let types = fmap (\field -> field{structFieldValue = variant}) structValueTypes     pure (LogicalTypeStruct types, FieldStruct structValue{structValueTypes = types})-  FieldUnion{} -> throwInvalidArgument "a VARIANT keeps only the member value of a UNION, so a UNION value cannot bind as a VARIANT"+  FieldUnion{} -> rejectPayload "a UNION value" "a VARIANT keeps only the member value of a UNION"   FieldGeometry RawGeometry{rawGeometryCRS = Just crs} ->-    throwInvalidArgument-      ("a VARIANT does not keep the CRS of a GEOMETRY, so the GEOMETRY value with the CRS " <> quotedCRS crs <> " cannot bind as a VARIANT")+    rejectPayload+      ("the GEOMETRY value with the CRS " <> quotedCRS crs)+      "a VARIANT does not keep the CRS of a GEOMETRY"   FieldGeometry{} -> pure (LogicalTypeGeometry Nothing, value)-  FieldMap{} -> throwInvalidArgument "a VARIANT stores a MAP as a list of key and value objects, so a MAP value cannot bind as a VARIANT"-  FieldEnum{} -> throwInvalidArgument "a VARIANT stores an ENUM as its text, so an ENUM value cannot bind as a VARIANT"+  FieldMap{} -> rejectPayload "a MAP value" "a VARIANT stores a MAP as a list of key and value objects"+  FieldEnum{} -> rejectPayload "an ENUM value" "a VARIANT stores an ENUM as its text"   where     variant = LogicalTypeScalar LogicalTypeIdVariant     scalar dtype = pure (LogicalTypeScalar dtype, value)+    rejectPayload payload reason =+      throwInvalidArgument (reason <> ", so " <> payload <> " cannot bind as a VARIANT") +-- | Copy a logical type into an owned SQL TYPE value.+typeDuckValue :: ValueBuilder -> LogicalTypeRep -> IO DuckDBV2ValueHandle+typeDuckValue ValueBuilder{builderFactory, builderType} rep =+  bracket (builderType rep) destroyLogicalType \logical ->+    output (c_duckdb_v2_value_create_type builderFactory logical)++-- | Construct a SQL @TYPE@ value with the supplied factory.+instance ToDuckValue LogicalTypeRep where+  toDuckValue factory = typeDuckValue (plainBuilder factory)+ instance ToDuckValue Null where   toDuckValue factory _ = nullDuckValue factory @@ -2206,41 +2223,32 @@   _ <- timeOfDayUnits units localTimeOfDay   checkTimestampRange units value   let total = timestampUnits units value-  -- Only a nanosecond value reaches this check with a value outside the-  -- storage or at an infinity value. See Note [Time ranges].+  -- Exclude the infinity sentinels. See Note [Time ranges].   checkFiniteRange "TIMESTAMP" (minBound :: Int64) maxBound total   pure (fromInteger total)  {- Note [Time ranges] ~~~~~~~~~~~~~~~~~~~~~-The storage of a DATE and of a TIMESTAMP can hold values that DuckDB 2.0.0-does not accept. The SQL literal of each type, and the casts to it, reject-these values:+DuckDB accepts these finite ranges:  - DATE: from -5877641-06-25 (5877642-06-25 BC) to 5881580-07-10, days   -2147483646 to 2147483646 from the Unix epoch. The INT32 storage also holds-  day -2147483648.--- TIMESTAMP, TIMESTAMPTZ, TIMESTAMP_S, and TIMESTAMP_MS: from -290308-12-22-  00:00:00 (290309-12-22 BC) to 294247-01-10 04:00:54.775806, microseconds-  -9223372022400000000 to 2^63 - 2. The INT64 storage of a microsecond value-  also holds about four hours before that start, and the storage of a second-  or a millisecond value holds much more at each end.+  day -2147483648, which is outside the SQL range.+- TIMESTAMP and TIMESTAMPTZ: from -290308-12-21 19:59:05.224192+  (290309-12-21 BC) to 294247-01-10 04:00:54.775806, microseconds+  -2^63 to 2^63 - 2, except -2^63 + 1, the negative infinity sentinel.+- TIMESTAMP_S and TIMESTAMP_MS: whole seconds or milliseconds inside the+  microsecond range. Their storage holds more values at each end.+- TIMESTAMP_NS and TIMESTAMPTZ_NS: the INT64 nanosecond range, except the+  two infinity sentinels. -A value outside these ranges breaks ordinary SQL in DuckDB 2.0.0. A cast of-such a TIMESTAMP to VARCHAR raises an INTERNAL error, and DuckDB then-invalidates the database. A cast of such a TIMESTAMP to TIMESTAMP_S stores the-first valid value with no error. A DATE before the range sorts before--infinity, and a cast of it to TIMESTAMP fails. Thus encodeDay and-encodeTimestampUnits raise an SQLError of the type ErrorTypeConversion for-each finite value outside these ranges (throwOutsideTimeRange), before any-check of the storage, so a value outside the storage and a value at an-infinity value get this error too. The text names the value and the end of-the range in the notation of Data.Time, where the year -5877641 is 5877642 BC.-A nanosecond value has the range of its INT64 storage, and the literal of-TIMESTAMP_NS accepts that range, so it gets no check here. checkFiniteRange-then catches only a nanosecond value outside its storage or at an infinity-value.+encodeDay and encodeTimestampUnits raise an SQLError of the type+ErrorTypeConversion before binding a finite value outside these ranges.+checkTimestampRange checks the microsecond range for second, millisecond,+and microsecond targets, before and after cutting to whole target units.+checkFiniteRange checks the storage and excludes+both infinity sentinels. The text names calendar values in the notation of+Data.Time, where the year -5877641 is 5877642 BC.  A leap second gets no check. A TimeOfDay with 60 seconds binds as the next second, as make_time(23, 59, 60) and make_timestamp(2016, 12, 31, 23, 59, 60)@@ -2252,16 +2260,20 @@ checkTimestampRange :: Integer -> LocalTime -> IO () checkTimestampRange units value   | units > 1000000 = pure ()-  | micros < -9223372022400000000 =-      throwOutsideTimeRange (show value) ("below the smallest " <> name <> " of DuckDB, -290308-12-22 00:00:00")+  | storedMicros < toInteger (minBound :: Int64) =+      throwOutsideTimeRange (show value) ("below the smallest " <> name <> " of DuckDB, " <> smallest)+  | micros == -9223372036854775807 =+      throwOutsideTimeRange (show value) ("at the negative infinity sentinel of DuckDB for " <> name)   | micros > 9223372036854775806 = throwOutsideTimeRange (show value) ("above the largest " <> name <> " of DuckDB, " <> largest)   | otherwise = pure ()   where     micros = timestampUnits 1000000 value-    (name, largest) = case units of-      1 -> ("TIMESTAMP_S", "294247-01-10 04:00:54")-      1000 -> ("TIMESTAMP_MS", "294247-01-10 04:00:54.775")-      _ -> ("TIMESTAMP", "294247-01-10 04:00:54.775806")+    -- Cutting to whole target units can move a value below the lower bound.+    storedMicros = micros - micros `mod` (1000000 `div` units)+    (name, smallest, largest) = case units of+      1 -> ("TIMESTAMP_S", "-290308-12-21 19:59:06", "294247-01-10 04:00:54")+      1000 -> ("TIMESTAMP_MS", "-290308-12-21 19:59:05.225", "294247-01-10 04:00:54.775")+      _ -> ("TIMESTAMP", "-290308-12-21 19:59:05.224192", "294247-01-10 04:00:54.775806")  -- | Raise the error for a date or a timestamp outside the range of DuckDB. The -- text names the Haskell value and the end of the range. See
src/Database/DuckDB/Simple/Typed/Aggregate.hs view
@@ -100,7 +100,7 @@ the table, and only the caller that removes it frees the stable pointer. This check stops a second free of a stable pointer that a byte copy of the native state also holds. It does not stop the release of a byte copy after the runtime-has given its stable pointer to a new state. DuckDB 2.0.0 destroys each state at+has given its stable pointer to a new state. DuckDB destroys each state at one location. The destructor of the state table frees the stable pointers that remain (releaseStateTable). @@ -109,16 +109,16 @@ only the destroy callback and a failed finalize release the states, and a release frees each stable pointer that it reads. -These are the states that DuckDB 2.0.0 destroys and does not destroy.+These are the states that DuckDB destroys and does not destroy.  - The header says that the destroy callback runs on states that are discarded-  without being finalized. DuckDB 2.0.0 runs the destroy callback on finalized+  without being finalized. DuckDB runs the destroy callback on finalized   states too: a hash aggregate destroys each chunk of states after it finalizes   them, and an ungrouped aggregate and a window destroy their states after   finalize (radix_partitioned_hashtable.cpp, physical_ungrouped_aggregate.cpp,   and window_segment_tree.cpp). -- DuckDB 2.0.0 does not run the destroy callback on these states:+- DuckDB does not run the destroy callback on these states:    - The states of a finalize call that fails, in a hash aggregate and in a     window.@@ -132,7 +132,7 @@   of the other states.  DuckDB runs the destructor of the state table when it drops the bind data of a-call site. In DuckDB 2.0.0 a hash table and an ungrouped aggregate each hold a+call site. In DuckDB a hash table and an ungrouped aggregate each hold a copy of the bind data, and each copy shares the holder of the state table through a shared pointer (CV2AggregateFunctionData::Copy and CV2UserData). Each of them runs the destroy callback on its states before it drops its copy@@ -177,7 +177,7 @@ -- | The live states of one call site, as the keys of their stable pointers. -- 'stateStripe' gives the stripe of a key. See Note [State table stripes]. ----- With the table, the library frees the states that DuckDB 2.0.0 does not+-- With the table, the library frees the states that DuckDB does not -- destroy. See Note [Aggregate state life cycle]. type StateTable = Array Int (IORef IntSet.IntSet) 
src/Database/DuckDB/Simple/Typed/Table.hs view
@@ -70,12 +70,12 @@ (registerMultiFileFunction) over the table function "claims the batches of the function itself, so that the rows of a file keep their order also when several threads scan it" (duckdb_v2_table_function_set_claim_batch_callback in the-header). Then DuckDB 2.0.0 runs several exec callbacks of the scan of one file+header). Then DuckDB runs several exec callbacks of the scan of one file at the same time. The lock of the MVar gives each row to one call only.  A typed table function declares projection pushdown. DuckDB then asks the exec callback for only the columns that the query uses. When the query uses no-column, for example in SELECT count(*), DuckDB 2.0.0 asks for the first column:+column, for example in SELECT count(*), DuckDB asks for the first column: the function declares no virtual column, so LogicalGet::GetAnyColumn gives column 0 (logical_get.cpp), and CV2TableScanColumns gives column 0 for an empty projection (capi_v2_func_table.cpp). writeRows makes the values of the
src/Database/DuckDB/Simple/V2/Registration.hs view
@@ -91,11 +91,14 @@   tableCallbacks,   registerScalarFunction,   registerScalarFunctionWith,+  registerScalarFunctionWithStats,   registerAggregateFunction,   registerAggregateFunctionWith,+  registerAggregateFunctionWithStats,   registerAggregateFunctionWithCleanup,   registerTableFunction,   registerTableFunctionWith,+  registerTableFunctionWithStats,   registerCast,   registerCastWith,   registerCustomType,@@ -302,6 +305,22 @@       )       callbacks +{- Note [Managed statistics callbacks]+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+Statistics callbacks run during query optimization. The registration owns each+callback pointer until DuckDB releases its last native owner. The callback info,+context, and statistics handles are borrowed. Use them only during the callback.+An exception in a callback becomes a SQL error. See Note [Callback ownership] in+Database.DuckDB.Simple.Callback.++Argument statistics are read-only. Result statistics start with both NULL and+non-NULL values possible. Set a possibility to false only if every result meets+that guarantee. An incorrect guarantee can cause wrong query results. Leave+unknown statistics unchanged. Aggregate statistics must also describe the result+of an ungrouped aggregate with no input rows. The table callback receives+the index of the column in the declared schema.+-}+ -- | Configure and register a managed scalar function on a connection or extension. -- The configuration action borrows the builder. Do not set callbacks or user data. --@@ -328,7 +347,37 @@ -- >   rows <- query_ connection "SELECT description FROM duckdb_functions() WHERE function_name = 'managed_target_default'" -- >   print (map fromOnly rows :: [Text.Text]) -- ["Add two BIGINT values."] registerScalarFunctionWith :: RegistrationTarget -> (DuckDBV2ScalarFunctionHandle -> IO ()) -> ScalarCallbacks -> IO ()-registerScalarFunctionWith target configure callbacks =+registerScalarFunctionWith target configure = registerScalarFunctionWithOptionalStats target configure Nothing++-- | Configure and register a managed scalar function with statistics.+-- The arguments are the target, configuration action, statistics callback, and+-- execution callbacks. The configuration action must not set callbacks or user data.+-- See Note [Managed statistics callbacks].+--+-- For a scalar function that always returns a non-NULL value, use this callback+-- as the third argument. Leave the statistics unchanged if NULL is possible.+--+-- > nonNullStats info _ = do+-- >   result <- R.output (c_duckdb_v2_scalar_function_stats_get_result_stats info)+-- >   R.checked (c_duckdb_v2_stats_set_can_have_null result 0)+-- >+-- > R.registerScalarFunctionWithStats target configure nonNullStats callbacks+registerScalarFunctionWithStats+  :: RegistrationTarget+  -> (DuckDBV2ScalarFunctionHandle -> IO ())+  -> ContextCallback DuckDBV2ScalarFunctionStatsInfoHandle+  -> ScalarCallbacks+  -> IO ()+registerScalarFunctionWithStats target configure stats = registerScalarFunctionWithOptionalStats target configure (Just stats)++-- | Register with an optional statistics callback in the same resource owner.+registerScalarFunctionWithOptionalStats+  :: RegistrationTarget+  -> (DuckDBV2ScalarFunctionHandle -> IO ())+  -> Maybe (ContextCallback DuckDBV2ScalarFunctionStatsInfoHandle)+  -> ScalarCallbacks+  -> IO ()+registerScalarFunctionWithOptionalStats target configure stats callbacks =   withTarget     target     c_duckdb_v2_scalar_function_create@@ -363,6 +412,12 @@                   DuckDBV2ScalarFunctionExecCallbackFn                   (DuckDBV2ScalarFunctionExecCallbackFn_Aux (guardContext (scalarExecCallback callbacks)))                   (c_duckdb_v2_scalar_function_set_exec_callback function)+              , installOptional stats $ \callback ->+                  install+                    allocate+                    DuckDBV2ScalarFunctionStatsCallbackFn+                    (DuckDBV2ScalarFunctionStatsCallbackFn_Aux (guardContext callback))+                    (c_duckdb_v2_scalar_function_set_stats_callback function)               ]         )         (attachOpaque (c_duckdb_v2_scalar_function_set_user_data function))@@ -446,7 +501,32 @@ -- or user data. registerAggregateFunctionWithCleanup   :: RegistrationTarget -> IO () -> (DuckDBV2AggregateFunctionHandle -> IO ()) -> AggregateCallbacks -> IO ()-registerAggregateFunctionWithCleanup target cleanup configure callbacks =+registerAggregateFunctionWithCleanup target cleanup configure =+  registerAggregateFunctionWithOptionalStats target cleanup configure Nothing++-- | Configure and register a managed aggregate function with statistics.+-- The arguments are the target, configuration action, statistics callback, and+-- execution callbacks. The configuration action must not set callbacks or user data.+-- Statistics must include the result of an ungrouped aggregate with no input rows.+-- See Note [Managed statistics callbacks].+registerAggregateFunctionWithStats+  :: RegistrationTarget+  -> (DuckDBV2AggregateFunctionHandle -> IO ())+  -> ContextCallback DuckDBV2AggregateFunctionStatsInfoHandle+  -> AggregateCallbacks+  -> IO ()+registerAggregateFunctionWithStats target configure stats =+  registerAggregateFunctionWithOptionalStats target (pure ()) configure (Just stats)++-- | Register aggregate callbacks and cleanup in the same resource owner.+registerAggregateFunctionWithOptionalStats+  :: RegistrationTarget+  -> IO ()+  -> (DuckDBV2AggregateFunctionHandle -> IO ())+  -> Maybe (ContextCallback DuckDBV2AggregateFunctionStatsInfoHandle)+  -> AggregateCallbacks+  -> IO ()+registerAggregateFunctionWithOptionalStats target cleanup configure stats callbacks =   withTarget     target     c_duckdb_v2_aggregate_function_create@@ -496,6 +576,12 @@                     DuckDBV2AggregateFunctionDestroyCallbackFn                     (DuckDBV2AggregateFunctionDestroyCallbackFn_Aux (guardCallback callback))                     (c_duckdb_v2_aggregate_function_set_destroy_callback function)+              , installOptional stats $ \callback ->+                  install+                    allocate+                    DuckDBV2AggregateFunctionStatsCallbackFn+                    (DuckDBV2AggregateFunctionStatsCallbackFn_Aux (guardContext callback))+                    (c_duckdb_v2_aggregate_function_set_stats_callback function)               ]         )         (attachOpaque (c_duckdb_v2_aggregate_function_set_user_data function))@@ -554,7 +640,28 @@ -- | Configure and register a managed table function on a connection or extension. -- The configuration action borrows the builder. Do not set callbacks or user data. registerTableFunctionWith :: RegistrationTarget -> (DuckDBV2TableFunctionHandle -> IO ()) -> TableCallbacks -> IO ()-registerTableFunctionWith target configure callbacks =+registerTableFunctionWith target configure = registerTableFunctionWithOptionalStats target configure Nothing++-- | Configure and register a managed table function with statistics.+-- The arguments are the target, configuration action, statistics callback, and+-- execution callbacks. The configuration action must not set callbacks or user data.+-- See Note [Managed statistics callbacks].+registerTableFunctionWithStats+  :: RegistrationTarget+  -> (DuckDBV2TableFunctionHandle -> IO ())+  -> ContextCallback DuckDBV2TableFunctionStatsInfoHandle+  -> TableCallbacks+  -> IO ()+registerTableFunctionWithStats target configure stats = registerTableFunctionWithOptionalStats target configure (Just stats)++-- | Register with an optional statistics callback in the same resource owner.+registerTableFunctionWithOptionalStats+  :: RegistrationTarget+  -> (DuckDBV2TableFunctionHandle -> IO ())+  -> Maybe (ContextCallback DuckDBV2TableFunctionStatsInfoHandle)+  -> TableCallbacks+  -> IO ()+registerTableFunctionWithOptionalStats target configure stats callbacks =   withTarget     target     c_duckdb_v2_table_function_create@@ -624,6 +731,12 @@                     DuckDBV2TableFunctionGetBindInfoCallbackFn                     (DuckDBV2TableFunctionGetBindInfoCallbackFn_Aux (guardContext callback))                     (c_duckdb_v2_table_function_set_get_bind_info_callback function)+              , installOptional stats $ \callback ->+                  install+                    allocate+                    DuckDBV2TableFunctionStatsCallbackFn+                    (DuckDBV2TableFunctionStatsCallbackFn_Aux (guardContext callback))+                    (c_duckdb_v2_table_function_set_stats_callback function)               ]         )         (attachOpaque (c_duckdb_v2_table_function_set_user_data function))@@ -651,7 +764,7 @@ -- with no error and has no effect, because DuckDB keeps the first cast. -- -- Warning: DuckDB does not always run the callback. Three rules of the--- optimizer of DuckDB 2.0.0 use the two types of a cast and not the cast+-- optimizer of DuckDB use the two types of a cast and not the cast -- function: -- -- * expression_rewriter (ComparisonSimplificationRule, with@@ -662,7 +775,7 @@ -- * statistics_propagation gives min and max from the statistics of the --   column through the cast. ----- In runs with DuckDB 2.0.0, a cast from INTEGER to BIGINT, from BIGINT to a+-- In runs with DuckDB, a cast from INTEGER to BIGINT, from BIGINT to a -- custom type with the base type BIGINT, and from that type to BIGINT, with a -- callback that changes the value, gave wrong rows and no error. This occurred -- in a filter, a filter with a parameter, IN, BETWEEN, a comparison of two@@ -735,7 +848,7 @@  {- Note [The error code of a failed cast] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-DuckDB 2.0.0 runs each cast as its scalar function "__cast". It marks this+DuckDB runs each cast as its scalar function "__cast". It marks this function as fallible only from the source type and the target type (CreateCastExpression in src/function/scalar/operator/cast.cpp, lines 174 to 190, and BoundCastExpression::CastCanThrow in@@ -865,7 +978,7 @@ -- | Store Haskell state in a native-owned bind, global, local, or init data slot. -- Pass the corresponding native opaque setter. Do not use registration user data. -- This helper uses native default equality. Use raw opaque callbacks for custom equality.--- Transfer a value at most once to each native state slot. In DuckDB 2.0.0, a+-- Transfer a value at most once to each native state slot. In DuckDB, a -- setter for init data, batch data, global state, or local state does not release -- a pointer that it replaces. See Note [Callback ownership] in -- <src/Database.DuckDB.Simple.Callback.html Database.DuckDB.Simple.Callback>. Store an @IORef@ or another mutable state
src/Database/DuckDB/Simple/Variant.hs view
@@ -22,9 +22,9 @@ -- stored value. Arrays are 'FieldList' values. Objects are 'FieldStruct' values -- whose fields have the VARIANT type. Decoded object keys use DuckDB's native -- lexicographic order. Array elements retain their order. SQL NULL is 'FieldNull'.--- A t'Variant' parameter binds its payload as a VARIANT. A VARIANT does not keep--- a UNION, a MAP, an ENUM, or the CRS of a GEOMETRY, so a parameter with such a--- payload, at any depth, raises an error. Results preserve their native scalar+-- A t'Variant' parameter binds its payload as a VARIANT. A VARIANT cannot hold+-- TYPE, UNION, MAP, ENUM, or the CRS of a GEOMETRY. A parameter with such a+-- payload raises an error at any depth. Results preserve their native scalar -- types and nested values through public V2 operations. newtype Variant = Variant {variantPayload :: FieldValue}   deriving (Eq, Show)@@ -41,29 +41,33 @@       }  -- | Reads a value that a VARIANT can hold: the value of a VARIANT column, or a--- value of another SQL type with that shape. The shape is a scalar that is not--- an ENUM, a GEOMETRY with no CRS, a LIST of such values, or a STRUCT in the--- form of a VARIANT object: its fields have the type VARIANT and hold such--- values, and its keys are in ascending order. A value of another shape fails+-- value of another SQL type with that shape. The shape can be a scalar+-- other than TYPE or ENUM, GEOMETRY with no CRS, or a LIST of such values.+-- A STRUCT can have this shape if its fields have type VARIANT and its keys+-- are in ascending order. A value of another shape fails -- with 'Incompatible', because the cast of DuckDB to VARIANT gives another--- value. Write @c::VARIANT@ in SQL for such a column. A column whose type is or--- contains JSON, at each depth, also fails with 'Incompatible': DuckDB casts--- JSON to VARIANT by parsing the text, so @c::VARIANT@ gives the parsed value,--- not the text. See Note [Reads of another SQL type] in+-- value. Write @c::VARIANT@ if DuckDB supports the cast. When the column+-- type contains TYPE, DuckDB accepts only SQL NULL. A column whose type+-- is or contains JSON also fails with 'Incompatible'. DuckDB parses JSON+-- text when it casts to VARIANT. See Note [Reads of another SQL type] in -- "Database.DuckDB.Simple.FromField". instance FromField Variant where   fromField field@Field{fieldName, fieldValue, fieldType} = do     logical <- fieldType-    if containsType isJson logical-      then-        returnError-          Incompatible-          field-          (Text.concat [Text.pack "DuckDB casts JSON to VARIANT by parsing the text; write ", column, Text.pack "::VARIANT"])-      else-        if variantShape fieldValue-          then pure (Variant fieldValue)-          else returnError Incompatible field (Text.pack "a VARIANT does not hold a value of this shape; write c::VARIANT")+    case fieldValue of+      _+        | containsType isJson logical ->+            returnError+              Incompatible+              field+              (Text.concat [Text.pack "DuckDB casts JSON to VARIANT by parsing the text; write ", column, Text.pack "::VARIANT"])+      FieldNull -> pure (Variant FieldNull)+      _+        | containsType (== LogicalTypeScalar LogicalTypeIdType) logical ->+            returnError Incompatible field (Text.pack "a VARIANT cannot hold a TYPE value")+        | variantShape fieldValue -> pure (Variant fieldValue)+        | otherwise ->+            returnError Incompatible field (Text.pack "a VARIANT does not hold a value of this shape; write c::VARIANT")     where       isJson (LogicalTypeAlias name (LogicalTypeScalar LogicalTypeIdVarchar)) = name == Text.pack "JSON"       isJson (LogicalTypeAlias name LogicalTypeCollatedVarchar{}) = name == Text.pack "JSON"@@ -71,18 +75,19 @@       column = if Text.null fieldName then Text.pack "c" else fieldName  -- | Whether a VARIANT can hold a decoded value. A VARIANT column decodes only to--- these values: a scalar that is not an ENUM, a GEOMETRY with no CRS, a+-- these values: a scalar other than TYPE or ENUM, a GEOMETRY with no CRS, a -- 'FieldList' of such values, and a 'FieldStruct' in the form of a VARIANT -- object. Such an object has fields of the type VARIANT that hold such values, -- and its keys are in ascending order: DuckDB sorts the keys of an object by -- their UTF-8 bytes. The order of 'Text' is the order of code points, and UTF-8 -- keeps that order in its bytes, so the two orders are the same. Each other--- value has no VARIANT form: a 'FieldArray', a 'FieldTuple', another--- 'FieldStruct', a 'FieldEnum', a 'FieldMap', a 'FieldUnion', and a GEOMETRY--- with a CRS.+-- value has no VARIANT form. This includes 'FieldArray', 'FieldTuple',+-- other 'FieldStruct' values, 'FieldType', 'FieldEnum', 'FieldMap',+-- 'FieldUnion', and GEOMETRY with a CRS. variantShape :: FieldValue -> Bool variantShape = \case   FieldEnum{} -> False+  FieldType{} -> False   FieldGeometry RawGeometry{rawGeometryCRS = Just _} -> False   FieldMap{} -> False   FieldUnion{} -> False
src/Database/DuckDB/Simple/VariantCodec.hs view
@@ -386,7 +386,7 @@ parseKept :: DuckDBV2ConnectionHandle -> Text -> (DuckDBV2SqlStatementHandle -> IO a) -> IO a parseKept connection sql keep =   TextForeign.withCString sql \source ->-    bracket (output (c_duckdb_v2_parse_sql connection (ConstPtr source))) (destroy c_duckdb_v2_statement_iterator_destroy) \iterator -> do+    bracket (output (c_duckdb_v2_parse_sql connection (ConstPtr source) 0)) (destroy c_duckdb_v2_statement_iterator_destroy) \iterator -> do       statement <- output (c_duckdb_v2_statement_iterator_next iterator)       keep statement 
test/CopyTests.hs view
@@ -11,6 +11,7 @@  import Control.Exception (bracket, try) import Control.Monad (forM_, void)+import Data.Foldable (toList) import Data.IORef import Data.Int (Int64) import Data.List (sort)@@ -20,7 +21,13 @@ import Database.DuckDB.Simple hiding (bind) import qualified Database.DuckDB.Simple.Catalog as Catalog import qualified Database.DuckDB.Simple.Copy as Copy-import Database.DuckDB.Simple.FromField (Field (..), FieldValue (..), LogicalTypeRep (..))+import Database.DuckDB.Simple.FromField (+  Field (..),+  FieldValue (..),+  LogicalTypeRep (..),+  StructField (..),+  StructValue (..),+ ) import Database.DuckDB.Simple.LogicalRep (withoutAliases) import Database.DuckDB.Simple.Ok (Ok (..)) import qualified Database.DuckDB.Simple.V2.Copy as V2Copy@@ -41,7 +48,8 @@ copyTests =   testGroup     "managed COPY"-    [ testCase "COPY TO scans batches and reports statistics" testCopyTo+    [ testCase "COPY TO returns the types of the columns written" testCopyReturnedTypes+    , testCase "COPY TO scans batches and reports statistics" testCopyTo     , testCase "row callbacks receive per-partition file paths" testPartitionedCopy     , testCase "row callbacks decode VARIANT after the registering connection closes" testVariantCopy     , testCase "COPY FROM runs bind global local and exec callbacks" testCopyFrom@@ -50,6 +58,29 @@     , testCase "COPY TO and table descriptions report the logical type id of each column type" testColumnTypeIds     , testCase "COPY TO snapshots survive but native metadata expires after the callback" testCopyFieldTypes     ]++-- | Read the native TYPE values in the metadata result and verify the file.+testCopyReturnedTypes :: Assertion+testCopyReturnedTypes = withDirectory \directory -> withConnection ":memory:" \connection -> do+  let path = Text.pack (directory </> "columns.csv")+  rows <-+    query+      connection+      "COPY (SELECT 42::INTEGER AS v, '{}'::JSON AS doc) TO ? (RETURN_COLUMN_TYPES)"+      (Only path)+      :: IO [(Int64, [StructValue FieldValue])]+  map (fmap (map (toList . structValueFields))) rows+    @?= [+          ( 1+          ,+            [ [StructField "name" (FieldText "v"), StructField "type" (FieldType (LogicalTypeScalar LogicalTypeIdInteger))]+            , [StructField "name" (FieldText "doc"), StructField "type" (FieldType (LogicalTypeAlias "JSON" (LogicalTypeScalar LogicalTypeIdVarchar)))]+            ]+          )+        ]+  forM_ rows \(_, columns) ->+    query connection "SELECT ?" (Only (FieldList (map FieldStruct columns))) >>= (@?= [Only columns])+  query connection "SELECT v::INTEGER FROM read_csv(?)" (Only path) >>= (@?= [Only (42 :: Int64)])  -- | Run every COPY TO callback and check the reported file statistics. testCopyTo :: Assertion
test/CoreRegressionTests.hs view
@@ -293,6 +293,16 @@             >>= (@?= [(2, 1, 3)])           (queryNamed conn "SELECT $1::BIGINT, $3::BIGINT" ["1" := (11 :: Int64), "3" := (33 :: Int64)] :: IO [(Int64, Int64)]) >>= (@?= [(11, 33)])           assertThrows (query conn "SELECT $1::BIGINT, $3::BIGINT" (11 :: Int64, 33 :: Int64) :: IO [(Int64, Int64)])+    , testCase "parse errors apply the transaction invalidation policy and permit rollback" $+        withConnection ":memory:" $ \conn -> do+          void $ execute_ conn "CREATE TABLE parse_audit (i INTEGER)"+          void $ execute_ conn "BEGIN"+          void $ execute_ conn "SET current_transaction_invalidation_policy = 'ALL_ERRORS_INVALIDATE_TRANSACTION'"+          void $ execute_ conn "INSERT INTO parse_audit VALUES (1)"+          assertSqlErrorType ErrorQueryParser (execute_ conn "SELEKT 1")+          assertSqlErrorType ErrorDatabaseTransaction (execute_ conn "INSERT INTO parse_audit VALUES (2)")+          void $ execute_ conn "ROLLBACK"+          (query_ conn "SELECT count(*) FROM parse_audit" :: IO [Only Int64]) >>= (@?= [Only 0])     , testCase "transaction preserves the original exception if rollback fails" $         withConnection ":memory:" $ \conn -> do           result <- try $ withTransaction conn $ do@@ -456,6 +466,9 @@                   Internal.InstanceClosed -> pure ()                   Internal.InstanceOpen{} -> assertFailure "parent close missed an instance"                 assertThrows (connect instance_)+    , testCase "startup configuration selects a log sink" $+        withConnectionWithConfig ":memory:" [("logging_sink", "stdout")] $ \conn ->+          (query_ conn "SELECT current_setting('logging_sink')" :: IO [Only Text]) >>= (@?= [Only "stdout"])     , testCase "startup configuration sets the whitelist before disabling external access" $ do         path <- canonicalizePath "/tmp/duckdb-whitelist-probe"         withConnectionWithConfig ":memory:" [("enable_external_access", "false"), ("allowed_paths", "['/tmp/duckdb-whitelist-probe']")] $ \conn ->@@ -961,7 +974,7 @@           1           ( \context -> Logging.registerLogSink (contextConnection context) "failing_log" (\_ -> countCall context >> failWith "failing log write") (pure ())           )-          ( \context -> runIn context "CALL enable_logging(); SET logging_storage = 'failing_log'; SELECT write_log('failing'); SET logging_storage = 'memory'"+          ( \context -> runIn context "CALL enable_logging(); SET logging_sink = 'failing_log'; SELECT write_log('failing'); SET logging_sink = 'memory'"           )           Nothing       , FailingCallback@@ -970,7 +983,7 @@           1           ( \context -> Logging.registerLogSink (contextConnection context) "failing_log" (\_ -> pure ()) (countCall context >> failWith "failing log flush")           )-          ( \context -> runIn context "CALL enable_logging(); SET logging_storage = 'failing_log'; SELECT write_log('failing'); SET logging_storage = 'memory'"+          ( \context -> runIn context "CALL enable_logging(); SET logging_sink = 'failing_log'; SELECT write_log('failing'); SET logging_sink = 'memory'"           )           Nothing       ]
test/ExtensionRegressionTests.hs view
@@ -250,7 +250,7 @@     , testCase "a scalar callback failure gives an SQL error of the type ErrorInputInvalid" $         withConnection ":memory:" \conn -> do           -- An untyped callback failure gets the code DuckDBV2ErrorApi.-          -- DuckDB 2.0.0 reports that code as an invalid input.+          -- DuckDB reports that code as an invalid input.           createFunction conn "throw_typed_scalar" (throwIO (userError "typed callback failure") :: IO Int64)           outcome <- try (query_ conn "SELECT throw_typed_scalar()" :: IO [Only Int64])           case outcome of@@ -412,7 +412,7 @@         calls <- newIORef (0 :: Int)         withConnection ":memory:" \conn -> do           Logging.registerLogStorage conn "throwing_log" (\_ -> atomicModifyIORef' calls (\n -> (n + 1, ())) >> throwIO ThreadKilled)-          void $ execute_ conn "CALL enable_logging(); SET logging_storage = 'throwing_log'; SELECT write_log('contained')"+          void $ execute_ conn "CALL enable_logging(); SET logging_sink = 'throwing_log'; SELECT write_log('contained')"           query_ conn "SELECT 42" >>= (@?= [Only (42 :: Int64)])         readIORef calls >>= assertBool "the sink ran" . (> 0)     , testCase "log sinks copy entries and flush when released" do@@ -428,8 +428,8 @@           void $             execute_               conn-              "CALL enable_logging(); SET logging_storage = 'captured_log'; SELECT write_log('Matti λ', level := 'warning', log_type := 'HaskellTest'); SET logging_storage = 'memory'"-          -- DuckDB 2.0.0 flushes the sink five times for these statements.+              "CALL enable_logging(); SET logging_sink = 'captured_log'; SELECT write_log('Matti λ', level := 'warning', log_type := 'HaskellTest'); SET logging_sink = 'memory'"+          -- DuckDB flushes the sink five times for these statements.           readIORef flushes >>= (@?= 5)         end <- getCurrentTime         received <- filter ((== "HaskellTest") . Logging.logEntryType) <$> readIORef entries@@ -452,10 +452,10 @@           setDefaultDatabase instance_ "shared"           Logging.registerInstanceLogSink instance_ "instance_log" (\entry -> atomicModifyIORef' entries (\values -> (entry : values, ()))) (pure ())           withConnectionOn instance_ \conn -> do-            void $ execute_ conn "CALL enable_logging(); SET logging_storage = 'instance_log'"+            void $ execute_ conn "CALL enable_logging(); SET logging_sink = 'instance_log'"             Logging.logConnectionMessage conn Logging.LogLevelWarning "HaskellConnection" "from a connection"             Logging.logInstanceMessage instance_ Logging.LogLevelWarning "HaskellInstance" "from an instance"-            void $ execute_ conn "SET logging_storage = 'memory'"+            void $ execute_ conn "SET logging_sink = 'memory'"         received <- readIORef entries         let ofType kind = filter ((== kind) . Logging.logEntryType) received         map Logging.logEntryMessage (ofType "HaskellConnection") @?= ["from a connection"]@@ -468,7 +468,7 @@         entries <- newIORef []         (connectionId, transactionId, queryId) <- withConnectionWithConfig ":memory:" [("threads", "1")] \conn -> do           Logging.registerLogSink conn "id_log" (\entry -> atomicModifyIORef' entries (\values -> (entry : values, ()))) (pure ())-          void $ execute_ conn "CALL enable_logging(); SET logging_storage = 'id_log'"+          void $ execute_ conn "CALL enable_logging(); SET logging_sink = 'id_log'"           -- In a transaction, the transaction id and the query id differ.           void $ execute_ conn "BEGIN"           _ <- query_ conn "SELECT 1" :: IO [Only Int64]@@ -477,7 +477,7 @@               conn               "SELECT write_log('ids', level := 'warning', log_type := 'HaskellIds'), current_connection_id(), current_transaction_id(), current_query_id()"               :: IO [(FieldValue, Word64, Word64, Word64)]-          void $ execute_ conn "COMMIT; SET logging_storage = 'memory'"+          void $ execute_ conn "COMMIT; SET logging_sink = 'memory'"           case rows of             [(_, connection, transaction, queryNumber)] -> pure (connection, transaction, queryNumber)             _ -> assertFailure ("expected one row, got " <> show rows)@@ -644,10 +644,13 @@                   }           Config.registerCustomOption conn option           Config.getConfigDefaultValue conn "haskell_limit" >>= (@?= FieldInt64 3)+          Config.setConfigValue conn Config.SettingScopeGlobal "haskell_limit" (9 :: Int64)           Config.setConfigValue conn Config.SettingScopeSession "haskell_limit" (7 :: Int64)           Config.getConfigValue conn "haskell_limit" >>= (@?= (FieldInt64 7, Config.SettingScopeSession))           assertSqlErrorContaining "limit must be nonnegative" $ Config.setConfigValue conn Config.SettingScopeSession "haskell_limit" (-1 :: Int64)           void $ execute_ conn "RESET haskell_limit"+          Config.getConfigValue conn "haskell_limit" >>= (@?= (FieldInt64 9, Config.SettingScopeGlobal))+          void $ execute_ conn "RESET GLOBAL haskell_limit"           fmap Config.configValueText <$> Config.getConfigOption conn "haskell_limit" >>= (@?= Just (Just "3"))           assertSqlError $ Config.registerCustomOption conn option     , testCase "listConfigFlags lists a custom option of the connection" $@@ -691,7 +694,7 @@           Config.getConfigValue conn "haskell_variant_update" >>= (@?= (FieldInt64 7, Config.SettingScopeSession))     , testCase "setConfigValue with an Integer or a Natural checks the range of an unsigned option" $         withConnection ":memory:" \conn -> do-          -- DuckDB 2.0.0 gives asof_loop_join_threshold the type UBIGINT.+          -- DuckDB gives asof_loop_join_threshold the type UBIGINT.           let option = "asof_loop_join_threshold"               current = fst <$> Config.getConfigValue conn option           Config.setConfigValue conn Config.SettingScopeSession option (9 :: Integer)@@ -781,9 +784,9 @@         entries <- newIORef []         withConnection ":memory:" \conn -> do           Logging.registerLogSink conn "level_log" (\entry -> atomicModifyIORef' entries (\values -> (entry : values, ()))) (pure ())-          void $ execute_ conn "CALL enable_logging(level := 'trace'); SET logging_storage = 'level_log'"+          void $ execute_ conn "CALL enable_logging(level := 'trace'); SET logging_sink = 'level_log'"           forM_ [minBound .. maxBound] \level -> Logging.logConnectionMessage conn level "HaskellLevels" (Text.pack (show level))-          void $ execute_ conn "SET logging_storage = 'memory'"+          void $ execute_ conn "SET logging_sink = 'memory'"         received <- filter ((== "HaskellLevels") . Logging.logEntryType) <$> readIORef entries         sort [(Logging.logEntrySeverity entry, Logging.logEntryLevel entry, Logging.logEntryMessage entry) | entry <- received]           @?= [ (level, name, Text.pack (show level))
test/GeometryRegressionTests.hs view
@@ -385,7 +385,7 @@           inserted <- try (execute conn "INSERT INTO stored VALUES (?)" (Only other))           case inserted of             Left err -> do-              sqlErrorMessage err @?= mismatch+              Text.takeWhile (/= '\n') (sqlErrorMessage err) @?= mismatch               sqlErrorType err @?= Just ErrorQueryBinder             Right _ -> assertFailure "expected the CRS cast error of DuckDB"           (query_ conn "SELECT count(*) FROM stored" :: IO [Only Int64]) >>= (@?= [Only 0])@@ -395,7 +395,7 @@           compared <- try (query conn "SELECT count(*) FROM stored WHERE v = ?" (Only other) :: IO [Only Int64])           case compared of             Left err -> do-              sqlErrorMessage err @?= mismatch+              Text.takeWhile (/= '\n') (sqlErrorMessage err) @?= mismatch               sqlErrorType err @?= Just ErrorQueryBinder             Right rows -> assertFailure ("expected the CRS cast error of DuckDB, got " <> show rows)     , testCase "a Maybe RawGeometry parameter binds NULL and a value" $
test/RegistrationTests.hs view
@@ -54,6 +54,7 @@     , testCase "pragma query execution errors and documentation" testPragma     , testCase "pragma names must be nonempty and must not contain NUL" testPragmaNames     , testCase "query metric snapshots copy values and survive later queries" testMetrics+    , testCase "managed statistics callbacks preserve results and contain failures" testStatistics     , testCase "aggregate grouped and window execution" testAggregate     , testCase "aggregate registration cleanup follows callback ownership" testAggregateCleanup     , testCase "table state and typed default parameter" testTable@@ -90,6 +91,17 @@   vector <- R.output (c_duckdb_v2_scalar_function_exec_get_result info)   writeIntegers vector [sum [column !! row | column <- columns] | row <- [0 .. fromIntegral count - 1]] +-- | Copy one scalar argument, including NULL values, to the result vector.+scalarIdentity :: R.ContextCallback DuckDBV2ScalarFunctionExecInfoHandle+scalarIdentity info _ = do+  count <- R.output (c_duckdb_v2_scalar_function_exec_get_row_count info)+  argument <- R.output (c_duckdb_v2_scalar_function_exec_get_arg info 0)+  result <- R.output (c_duckdb_v2_scalar_function_exec_get_result info)+  R.checked (c_duckdb_v2_vector_set_size result count)+  forM_ (takeWhile (< count) [0 ..]) $ \index ->+    bracket (R.output (c_duckdb_v2_vector_get_value argument index)) (R.destroy c_duckdb_v2_value_destroy) $ \value ->+      R.checked (c_duckdb_v2_vector_set_value result index value)+ testScalar :: Assertion testScalar = withConnection ":memory:" \connection -> do   let left = (R.parameter "left" bigint){R.parameterDefault = Just (R.defaultValue (7 :: Int64))}@@ -135,18 +147,12 @@       bindState _ result _ = do         mark "bind"         R.setCallbackState (c_duckdb_v2_scalar_function_bind_set_bind_data result) (42 :: Int)-      executeRows info _ = do+      executeRows info context = do         state <- R.getCallbackState (c_duckdb_v2_scalar_function_exec_get_bind_data info)         (state :: Int) @?= 42         mismatch <- try (R.getCallbackState (c_duckdb_v2_scalar_function_exec_get_bind_data info) :: IO Bool)         either (\(_ :: SQLError) -> pure ()) (\_ -> assertFailure "state read with another type") mismatch-        count <- R.output (c_duckdb_v2_scalar_function_exec_get_row_count info)-        argument <- R.output (c_duckdb_v2_scalar_function_exec_get_arg info 0)-        result <- R.output (c_duckdb_v2_scalar_function_exec_get_result info)-        R.checked (c_duckdb_v2_vector_set_size result count)-        forM_ (takeWhile (< count) [0 ..]) $ \index ->-          bracket (R.output (c_duckdb_v2_vector_get_value argument index)) (R.destroy c_duckdb_v2_value_destroy) $ \value ->-            R.checked (c_duckdb_v2_vector_set_value result index value)+        scalarIdentity info context       anyType = R.StructuredType (LogicalTypeScalar LogicalTypeIdAny)       signature = R.FunctionSignature [R.parameter "value" anyType] (Just anyType)       target = R.ConnectionTarget connection@@ -168,6 +174,125 @@   query_ connection "SELECT description FROM duckdb_functions() WHERE function_name='managed_resolved'"     >>= (@?= [Only ("Preserve the input type." :: Text.Text)]) +-- | Statistics callbacks read borrowed arguments and narrow result statistics.+-- A failed callback must leave the registration usable after rollback.+testStatistics :: Assertion+testStatistics = withConnection ":memory:" \connection -> do+  scalarCalls <- newIORef (0 :: Int)+  scalarExecutions <- newIORef (0 :: Int)+  scalarNullability <- newIORef []+  aggregateCalls <- newIORef (0 :: Int)+  tableColumns <- newIORef []+  failing <- newIORef False+  let target = R.ConnectionTarget connection+      mark calls = modifyIORef' calls (+ 1)+      failWhenRequested = readIORef failing >>= \enabled -> when enabled (throwIO (userError "managed statistics failure"))+      argumentCounts getter = allocaArray 4 \counts -> do+        R.checked (getter counts (advancePtr counts 1) (advancePtr counts 2) (advancePtr counts 3))+        peekArray 4 counts >>= (@?= [1, 0, 0, 0])+      setResultFlags canHaveNull canHaveValid result = do+        R.checked (c_duckdb_v2_stats_set_can_have_null result canHaveNull)+        R.checked (c_duckdb_v2_stats_set_can_have_valid result canHaveValid)+        R.output (c_duckdb_v2_stats_can_have_null result) >>= (@?= canHaveNull)+        R.output (c_duckdb_v2_stats_can_have_valid result) >>= (@?= canHaveValid)+      readOnly argument =+        c_duckdb_v2_stats_set_can_have_null argument 0 nullPtr >>= (@?= DuckDBV2ErrorInputInvalid)+      scalarStats info _ = do+        mark scalarCalls+        failWhenRequested+        void (R.output (c_duckdb_v2_scalar_function_stats_get_user_data info))+        R.getCallbackState (c_duckdb_v2_scalar_function_stats_get_bind_data info) >>= (@?= (17 :: Int))+        argumentCounts (c_duckdb_v2_scalar_function_stats_get_arg_count info)+        argument <- R.output (c_duckdb_v2_scalar_function_stats_get_arg_stats info 0)+        readOnly argument+        canHaveNull <- R.output (c_duckdb_v2_stats_can_have_null argument)+        canHaveValid <- R.output (c_duckdb_v2_stats_can_have_valid argument)+        modifyIORef' scalarNullability ((canHaveNull /= 0) :)+        R.output (c_duckdb_v2_scalar_function_stats_get_result_stats info) >>= setResultFlags canHaveNull canHaveValid+      aggregateStats info _ = do+        mark aggregateCalls+        failWhenRequested+        void (R.output (c_duckdb_v2_aggregate_function_stats_get_user_data info))+        R.getCallbackState (c_duckdb_v2_aggregate_function_stats_get_bind_data info) >>= (@?= (23 :: Int))+        argumentCounts (c_duckdb_v2_aggregate_function_stats_get_arg_count info)+        R.output (c_duckdb_v2_aggregate_function_stats_get_arg_stats info 0) >>= readOnly+        -- The sum state starts at zero, including for an empty group.+        R.output (c_duckdb_v2_aggregate_function_stats_get_result_stats info) >>= setResultFlags 0 1+      tableStats info _ = do+        column <- R.output (c_duckdb_v2_table_function_stats_get_column_index info)+        modifyIORef' tableColumns (column :)+        failWhenRequested+        void (R.output (c_duckdb_v2_table_function_stats_get_user_data info))+        R.getCallbackState (c_duckdb_v2_table_function_stats_get_bind_data info) >>= (@?= (3 :: Int64))+        column @?= 0+        R.output (c_duckdb_v2_table_function_stats_get_result_stats info) >>= setResultFlags 0 1+      scalar =+        (R.scalarCallbacks (\info context -> mark scalarExecutions >> scalarIdentity info context))+          { R.scalarBindCallback = Just (\_ result _ -> R.setCallbackState (c_duckdb_v2_scalar_function_bind_set_bind_data result) (17 :: Int))+          }+      aggregate =+        sumCallbacks+          { R.aggregateBindCallback = Just (\input _ _ -> R.setCallbackState (c_duckdb_v2_function_bind_set_bind_data input) (23 :: Int))+          }+  R.withRegistrationFactory target \factory -> do+    R.registerScalarFunctionWithStats+      target+      ( \function -> do+          R.withIdentifier "managed_stats_scalar" (R.checked . c_duckdb_v2_scalar_function_set_name function)+          R.output (c_duckdb_v2_scalar_function_get_signature function)+            >>= R.configureFunctionSignature factory (scalarSignature [R.parameter "value" bigint])+      )+      scalarStats+      scalar+    R.registerAggregateFunctionWithStats+      target+      ( \function -> do+          R.withIdentifier "managed_stats_sum" (R.checked . c_duckdb_v2_aggregate_function_set_name function)+          R.output (c_duckdb_v2_aggregate_function_get_signature function)+            >>= R.configureFunctionSignature factory (scalarSignature [R.parameter "value" bigint])+      )+      aggregateStats+      aggregate+    R.registerTableFunctionWithStats+      target+      ( \function -> do+          R.withIdentifier "managed_stats_scan" (R.checked . c_duckdb_v2_table_function_set_name function)+          R.output (c_duckdb_v2_table_function_get_signature function)+            >>= R.configureFunctionSignature factory (R.FunctionSignature [R.parameter "count" bigint] Nothing)+      )+      tableStats+      tableDefinition+  void (execute_ connection "CREATE TABLE stats_input(v BIGINT NOT NULL)")+  void (execute_ connection "INSERT INTO stats_input VALUES (1), (2)")+  let scalarQuery = "SELECT managed_stats_scalar(v) FROM stats_input ORDER BY v"+      aggregateQuery = "SELECT managed_stats_sum(v) FROM stats_input"+      tableQuery = "SELECT amount FROM managed_stats_scan(3) ORDER BY amount"+  query_ connection scalarQuery >>= (@?= map Only ([1, 2] :: [Int64]))+  readIORef scalarExecutions >>= assertBool "scalar execution ran for non-NULL values" . (> 0)+  query_ connection "SELECT managed_stats_scalar(v) FROM (VALUES (1::BIGINT), (NULL)) t(v) ORDER BY v NULLS LAST"+    >>= (@?= [Only (Just (1 :: Int64)), Only Nothing])+  readIORef scalarNullability >>= \flags -> do+    assertBool "non-NULL argument statistics" (False `elem` flags)+    assertBool "nullable argument statistics" (True `elem` flags)+  query_ connection aggregateQuery >>= (@?= [Only (3 :: Int64)])+  query_ connection "SELECT managed_stats_sum(v) FROM stats_input WHERE false" >>= (@?= [Only (0 :: Int64)])+  query_ connection tableQuery >>= (@?= map Only ([42, 43, 44] :: [Int64]))+  writeIORef scalarExecutions 0+  query_ connection "SELECT count(*) FROM stats_input WHERE managed_stats_scalar(v) IS NULL" >>= (@?= [Only (0 :: Int64)])+  readIORef scalarExecutions >>= (@?= 0)+  query_ connection "SELECT managed_stats_sum(v) IS NULL FROM stats_input WHERE false" >>= (@?= [Only False])+  query_ connection "SELECT count(*) FROM managed_stats_scan(3) WHERE amount IS NULL" >>= (@?= [Only (0 :: Int64)])+  readIORef scalarCalls >>= assertBool "scalar statistics ran" . (> 0)+  readIORef aggregateCalls >>= assertBool "aggregate statistics ran" . (> 0)+  readIORef tableColumns >>= assertBool "table statistics requested a column" . not . null+  forM_ [scalarQuery, aggregateQuery, tableQuery] \sql -> do+    void (execute_ connection "BEGIN TRANSACTION")+    writeIORef failing True+    assertSqlErrorContaining "managed statistics failure" (query_ connection sql :: IO [Only Int64])+    void (execute_ connection "ROLLBACK")+    writeIORef failing False+    query_ connection sql >>= \rows -> assertBool "registration is usable after rollback" (not (null (rows :: [Only Int64])))+ testPragma :: Assertion testPragma = withConnection ":memory:" \connection -> do   let amount = (R.parameter "amount" bigint){R.parameterDefault = Just (R.defaultValue (42 :: Int64))}@@ -550,7 +675,7 @@  -- | A second registration of a cast for the same pair of types returns with no -- error, and CAST still runs the callback of the first registration. DuckDB--- 2.0.0 keeps the first cast for a pair.+-- keeps the first cast for a pair. testCastSecond :: Assertion testCastSecond = withConnection ":memory:" \connection -> do   R.registerCustomType connection "managed_kept" bigint@@ -592,9 +717,9 @@           vector <- R.output (c_duckdb_v2_scalar_function_exec_get_result info)           writeIntegers vector (replicate (fromIntegral count) 1)     R.registerScalarFunction connection "managed_log" (scalarSignature []) [] (R.scalarCallbacks logged)-    void $ execute_ connection "CALL enable_logging(); SET logging_storage = 'context_log'"+    void $ execute_ connection "CALL enable_logging(); SET logging_sink = 'context_log'"     query_ connection "SELECT managed_log()" >>= (@?= [Only (1 :: Int64)])-    void $ execute_ connection "SET logging_storage = 'memory'"+    void $ execute_ connection "SET logging_sink = 'memory'"   received <- filter ((== "HaskellContext") . Logging.logEntryType) <$> readIORef entries   map Logging.logEntryMessage received @?= ["from a callback"]   map Logging.logEntryScope received @?= [Logging.LogScopeConnection]
test/Spec.hs view
@@ -380,7 +380,7 @@         entries <- newIORef []         withConnection ":memory:" \conn -> do           Logging.registerLogStorage conn "hs_log_capture" (\entry -> atomicModifyIORef' entries (\values -> (entry : values, ())))-          void $ execute_ conn "CALL enable_logging(); SET logging_storage = 'hs_log_capture'; SELECT write_log('registered')"+          void $ execute_ conn "CALL enable_logging(); SET logging_sink = 'hs_log_capture'; SELECT write_log('registered')"         -- The sink receives the entry of write_log one time.         readIORef entries >>= (@?= ["registered"]) . filter (== "registered") . map Logging.logEntryMessage     ]
test/StreamingTests.hs view
@@ -270,7 +270,7 @@       where         go rows = next stmt >>= maybe (pure (reverse rows)) (\row -> go (row : rows)) --- | The WKB of the point (x, 0) in the little-endian byte order. DuckDB 2.0.0+-- | The WKB of the point (x, 0) in the little-endian byte order. DuckDB -- gives the same bytes for 'POINT (x 0)'::GEOMETRY::BLOB. pointWkb :: Int64 -> BS.ByteString pointWkb x = BL.toStrict (Builder.toLazyByteString (Builder.word8 1 <> Builder.word32LE 1 <> Builder.doubleLE (fromIntegral x) <> Builder.doubleLE 0))
test/TimeTests.hs view
@@ -338,8 +338,9 @@       stored "TIME" "" time >>= (@?= [Only "03:04:05.123456"])       stored "TIMESTAMPTZ" "::TIMESTAMPTZ_NS" instant >>= (@?= [Only "2026-01-02 03:04:05.123457+00"])       stored "TIMESTAMPTZ" "" instant >>= (@?= [Only "2026-01-02 03:04:05.123456+00"])+      nativeInterval <- query_ conn "SELECT ('2026-01-02 03:04:05.123456999'::TIMESTAMP_NS + INTERVAL 1 DAY)::VARCHAR" :: IO [Only Text.Text]       (query conn "SELECT (?::TIMESTAMP_NS + INTERVAL 1 DAY)::VARCHAR" (Only local) :: IO [Only Text.Text])-        >>= (@?= [Only "2026-01-03 03:04:05.123457"])+        >>= (@?= nativeInterval)       (query conn "SELECT (? + INTERVAL 1 DAY)::VARCHAR" (Only local) :: IO [Only Text.Text]) >>= (@?= [Only "2026-01-03 03:04:05.123456"])   , testCase "nested time values keep their digits below a microsecond only for nanosecond leaves" $ withConnection ":memory:" $ \conn -> do       let day = fromGregorian 2026 1 2@@ -540,8 +541,13 @@       stored array >>= (@?= [("ARRAY(1)", "['2026-01-02 03:04:05.123456']")])       -- An element of a VARIANT list keeps the microsecond type, as the list has one element type.       _ <- execute conn "INSERT INTO l VALUES (?)" (Only [local, LocalTime day (TimeOfDay 3 4 5)])+      nativeList <-+        query_+          conn+          "SELECT variant_typeof(x[1]), x::VARCHAR FROM (SELECT ['2026-01-02 03:04:05.123456'::TIMESTAMP, '2026-01-02 03:04:05'::TIMESTAMP]::VARIANT[] AS x)"+          :: IO [(Text.Text, Text.Text)]       (query_ conn "SELECT variant_typeof(x[1]), x::VARCHAR FROM l" :: IO [(Text.Text, Text.Text)])-        >>= (@?= [("TIMESTAMP_MICROS", "[2026-01-02 03:04:05.123456, 2026-01-02 03:04:05]")])+        >>= (@?= nativeList)   , testCase "a UTCTime outside the nanosecond range binds as a LocalTime does" $ withConnection ":memory:" $ \conn -> do       -- See Note [Nanosecond parameters] in "Database.DuckDB.Simple.ToField".       let at year time = LocalTime (fromGregorian year 1 1) time@@ -561,14 +567,11 @@         >>= (@?= [Only "2000-01-01 00:00:00"])       (query conn "SELECT count(*) FROM present WHERE z = ? OR true" (Only (localTimeToUTC utc (at 2300 digits))) :: IO [Only Int64])         >>= (@?= [Only 1])-      -- DuckDB does not mix TIMESTAMPTZ_NS and TIMESTAMPTZ in coalesce, so a-      -- UTCTime there gets the Binder Error of DuckDB with and without digits.-      let coalesced instant =-            try (query conn "SELECT coalesce(z, ?) FROM present" (Only instant) :: IO [Only UTCTime]) >>= \case-              Left SQLError{sqlErrorType} -> pure sqlErrorType-              Right _ -> pure Nothing-      coalesced (localTimeToUTC utc (at 2300 digits)) >>= (@?= Just ErrorQueryBinder)-      coalesced (localTimeToUTC utc (at 2300 plain)) >>= (@?= Just ErrorQueryBinder)+      -- The non-NULL first argument avoids evaluating an out-of-range fallback.+      nativeCoalesced <- query_ conn "SELECT coalesce(z, '2300-01-01'::TIMESTAMPTZ) FROM present" :: IO [Only UTCTime]+      forM_ [digits, plain] $ \time ->+        (query conn "SELECT coalesce(z, ?) FROM present" (Only (localTimeToUTC utc (at 2300 time))) :: IO [Only UTCTime])+          >>= (@?= nativeCoalesced)       -- A plain parameter and a list element get the same error for each type.       forM_ [(2300, digits), (2300, plain), (300000, digits), (300000, plain)] $ \(year, time) -> do         let local = at year time@@ -630,10 +633,12 @@         >>= (@?= [Only "TIMESTAMP_MICROS"])   , testCase "a Map or an Array of time values for VARIANT elements binds microsecond values" $ withConnection ":memory:" $ \conn -> do       let precise = LocalTime (fromGregorian 2026 1 2) (TimeOfDay 3 4 5.123456789)+      nativeMap <- query_ conn "SELECT MAP {'a': '2026-01-02 03:04:05.123456'::TIMESTAMP}::MAP(VARCHAR, VARIANT)::VARCHAR" :: IO [Only Text.Text]       (query conn "SELECT (?::MAP(VARCHAR, VARIANT))::VARCHAR" (Only (Map.singleton ("a" :: Text.Text) precise)) :: IO [Only Text.Text])-        >>= (@?= [Only "{a=2026-01-02 03:04:05.123456}"])+        >>= (@?= nativeMap)+      nativeArray <- query_ conn "SELECT ['2026-01-02 03:04:05.123456'::TIMESTAMP]::VARIANT[1]::VARCHAR" :: IO [Only Text.Text]       (query conn "SELECT (?::VARIANT[1])::VARCHAR" (Only (sized [precise] :: SizedArray 1 LocalTime)) :: IO [Only Text.Text])-        >>= (@?= [Only "[2026-01-02 03:04:05.123456]"])+        >>= (@?= nativeArray)   , testCase "nanosecond UTC targets reject finite overflow and sentinel collisions" $ withConnection ":memory:" $ \conn -> do       forM_         [ toInteger (minBound :: Int64) - 1@@ -667,8 +672,8 @@           -- Day 2147483647 is infinity.           afterDay = addDays 2147483647 epoch           local units count = utcToLocalTime utc (posixSecondsToUTCTime (fromRational (count % units)))-          firstTime = local 1000000 (-9223372022400000000)-          beforeTime = local 1000000 (-9223372022400000001)+          firstTime = local 1000000 (-9223372036854775808)+          beforeTime = local 1000000 (-9223372036854775809)           lastTime = local 1000000 9223372036854775806           -- The microsecond value 2^63 - 1 is infinity.           afterTime = local 1000000 9223372036854775807@@ -733,10 +738,13 @@         ]         $ \(logical, column, units, largest) -> do           let at count = structOf logical (FieldTimestamp (Finite (local units count)))-          stored column (at (-9223372022400000000 * units `div` 1000000))+          stored column (at (negate largest))           stored column (at largest)-          rejected column (at (-9223372022400000000 * units `div` 1000000 - 1))+          rejected column (at (negate largest - 1))           rejected column (at (largest + 1))+          -- Cutting these finite microsecond values to whole units goes below the range.+          rejected column (structOf logical (FieldTimestamp (Finite firstTime)))+          rejected column (structOf logical (FieldTimestamp (Finite (local 1000000 (negate largest * 1000000 `div` units - 1)))))   , testCase "each bind path checks the range of DuckDB for a date and a timestamp" $ withConnection ":memory:" $ \conn -> do       -- See Note [Time ranges] in "Database.DuckDB.Simple.ToField".       let epoch = fromGregorian 1970 1 1@@ -750,7 +758,7 @@                 assertBool (label <> ": " <> Text.unpack sqlErrorMessage) (bound `Text.isInfixOf` sqlErrorMessage)               Right _ -> assertFailure (label <> ": expected the range error")       forM_-        [ ("below the smallest", addDays (-2147483648) epoch, local (-9223372022400000001))+        [ ("below the smallest", addDays (-2147483648) epoch, local (-9223372036854775809))         , ("above the largest", addDays 2147483647 epoch, local 9223372036854775807)         ]         $ \(bound, day, time) -> do
test/TypedRegistrationTests.hs view
@@ -2012,7 +2012,7 @@     , \token -> withConnection ":memory:" \conn -> do         Logging.registerLogSink conn "closure_log" (\_ -> void (readIORef token)) (void (readIORef token))         void-          (execute_ conn "CALL enable_logging(); SET logging_storage = 'closure_log'; SELECT write_log('closure'); SET logging_storage = 'memory'")+          (execute_ conn "CALL enable_logging(); SET logging_sink = 'closure_log'; SELECT write_log('closure'); SET logging_sink = 'memory'")     )   ] 
test/ValueRegressionTests.hs view
@@ -237,7 +237,15 @@ valueRegressionTests :: TestTree valueRegressionTests =   testGroup "value regressions" $-    [ testCase "REAL decodes to Float and Double" $ withConnection ":memory:" \conn -> do+    [ testCase "TYPE values round-trip with nested aliases and NULLs" $ withConnection ":memory:" \conn -> do+        let json = LogicalTypeAlias "JSON" (LogicalTypeScalar LogicalTypeIdVarchar)+            expected = [Just (LogicalTypeList json), Nothing, Just (LogicalTypeDecimal 8 2)]+            sql = "SELECT [get_type(['{}'::JSON]), NULL, get_type(1::DECIMAL(8,2))]"+        query_ conn sql >>= (@?= [Only expected])+        Streaming.fold_ conn sql [] (\rows (Only types) -> pure (types : rows)) >>= (@?= [expected])+        query conn "SELECT ?" (Only expected) >>= (@?= [Only expected])+        query conn "SELECT ?" (Only (FieldType json)) >>= (@?= [Only json])+    , testCase "REAL decodes to Float and Double" $ withConnection ":memory:" \conn -> do         (query_ conn "SELECT 1.25::REAL" :: IO [Only Float]) >>= (@?= [Only 1.25])         (query_ conn "SELECT 1.25::REAL" :: IO [Only Double]) >>= (@?= [Only 1.25])     , testCase "a Float parameter does not read as Text, and its cast to VARCHAR does" $ withConnection ":memory:" \conn -> do@@ -264,7 +272,7 @@               in  UnionValue (fromIntegral (count - 1)) (name (count - 1)) (FieldInt32 7) members             accepted = lastMember 255         (query conn "SELECT union_tag(?)::VARCHAR, ?" (accepted, accepted) :: IO [(Text, UnionValue FieldValue)]) >>= (@?= [("m254", accepted)])-        -- DuckDB 2.0.0 permits at most 255 members in a UNION type.+        -- DuckDB permits at most 255 members in a UNION type.         forM_ [256, 300] \count ->           try (query conn "SELECT ?" (Only (lastMember count)) :: IO [Only (UnionValue FieldValue)]) >>= \case             Left SQLError{sqlErrorMessage, sqlErrorType} -> do@@ -567,11 +575,14 @@           (sized mismatch :: SizedArray 2 (Maybe (StructValue FieldValue)))           ErrorQueryNotImplemented           "Cannot combine types"-        unions <--          mapM-            (\sql -> query_ conn sql :: IO [Only (UnionValue FieldValue)])-            ["SELECT union_value(a := 1)::UNION(a INTEGER)", "SELECT union_value(b := 'x')::UNION(b VARCHAR)"]-        duckdbError "UNION list" (map fromOnly (concat unions)) ErrorTypeConversion "is not present in target union"+        -- A disjoint member survives the native UNION common-type calculation.+        let unionSQL = ["union_value(a := 1)", "union_value(b := 'x')"]+            unionList = "[" <> Text.intercalate ", " unionSQL <> "]"+        values <- forM unionSQL $ \sql -> do+          [Only value] <- query_ conn (Query ("SELECT " <> sql)) :: IO [Only (UnionValue FieldValue)]+          pure value+        [Only expected] <- query_ conn (Query ("SELECT " <> unionList)) :: IO [Only FieldValue]+        query conn "SELECT ?" (Only values) >>= (@?= [Only expected])         duckdbError           "ARRAY sizes"           ( FieldList@@ -1113,8 +1124,8 @@         _ <- execute conn "INSERT INTO empty_integers VALUES (?)" (Only ([] :: [Integer]))         (query_ conn "SELECT typeof(v), v::VARCHAR FROM empty_integers" :: IO [(Text, Text)]) >>= (@?= [("INTEGER[]", "[]")])         -- In a VARIANT, the elements keep the HUGEINT values, so a cast to an-        -- unsigned type checks their range. A BIGNUM element gave-        -- 18446744073709551611 for -5 with no error.+        -- unsigned type checks their range. DuckDB also checks the range+        -- of a BIGNUM element.         let variantCast :: (ToField a) => String -> a -> Text -> Assertion             variantCast label value target = do               _ <- execute_ conn "CREATE OR REPLACE TABLE variant_integers (v VARIANT)"@@ -1363,17 +1374,23 @@         (query conn "SELECT typeof(?), ?::VARCHAR" (Map.empty :: Map.Map Text [Int64], Map.empty :: Map.Map Text [Int64]) :: IO [(Text, Text)])           >>= (@?= [("MAP(VARCHAR, \"NULL\")", "{}")])     , testCase "an array of BigNum values keeps the BIGNUM casts of DuckDB" $ withConnection ":memory:" \conn -> do-        -- DuckDB 2.0.0 casts the BIGNUM -6 to UBIGINT as a wrapped value with no-        -- error. See Note [Integer parameters] in "Database.DuckDB.Simple.ToField".         forM_           [ ("BIGINT[2]", [BigNum 5, BigNum 6], "[5, 6]")           , ("BIGNUM[2]", [BigNum 5, BigNum 6], "[5, 6]")-          , ("UBIGINT[2]", [BigNum 5, BigNum (-6)], "[5, 18446744073709551610]")+          , ("HUGEINT[2]", [BigNum 5, BigNum (-6)], "[5, -6]")+          , ("UHUGEINT[2]", [BigNum 5, BigNum 6], "[5, 6]")           ]           \(dtype, values, expected) -> do             _ <- execute_ conn (Query ("CREATE OR REPLACE TABLE bignum_array (v " <> dtype <> ")"))             _ <- execute conn "INSERT INTO bignum_array VALUES (?)" (Only (sized values :: SizedArray 2 BigNum))             (query_ conn "SELECT v::VARCHAR FROM bignum_array" :: IO [Only Text]) >>= assertEqual (Text.unpack dtype) [Only expected]+        _ <- execute_ conn "CREATE OR REPLACE TABLE bignum_array (v UBIGINT[2])"+        assertConversionError (execute conn "INSERT INTO bignum_array VALUES (?)" (Only (sized [BigNum 5, BigNum (-6)] :: SizedArray 2 BigNum)))+    , testCase "BIGNUM integer casts check signedness and 128-bit boundaries" $ withConnection ":memory:" \conn -> do+        forM_ [("HUGEINT", negate (2 ^ (127 :: Int))), ("HUGEINT", 2 ^ (127 :: Int) - 1), ("UHUGEINT", 2 ^ (128 :: Int) - 1)] \(dtype, value) ->+          (query conn (Query ("SELECT ?::" <> dtype)) (Only (BigNum value)) :: IO [Only Integer]) >>= (@?= [Only value])+        forM_ [("UBIGINT", -6), ("HUGEINT", 2 ^ (127 :: Int)), ("UHUGEINT", -1), ("BIGINT", 2 ^ (128 :: Int) + 5), ("UHUGEINT", 2 ^ (128 :: Int))] \(dtype, value) ->+          assertConversionError (query conn (Query ("SELECT ?::" <> dtype)) (Only (BigNum value)) :: IO [Only Integer])     , testCase "native timestamp extrema read back in all units, and bind back only in the range of DuckDB" $ withConnectionWithConfig ":memory:" [("threads", "1")] \conn -> do         let checkTemporalFailure :: forall a. (FromField a) => Query -> Proxy a -> Text -> IO ()             checkTemporalFailure sql _ target = do@@ -1472,7 +1489,7 @@               -- A second, millisecond, or microsecond value outside the range of               -- DuckDB raises an error. See Note [Time ranges] in               -- "Database.DuckDB.Simple.ToField".-              if units == 1000000000 || (micros >= -9223372022400000000 && micros <= 9223372036854775806)+              if units == 1000000000 || (micros >= -9223372036854775808 && micros <= 9223372036854775806)                 then rebind >>= (@?= [Only original])                 else assertOutsideTimeRange rebind               _ <- execute_ conn "DROP TABLE native_timestamp"@@ -1531,16 +1548,12 @@               (query conn "SELECT ?" (Only (singleField (LogicalTypeScalar LogicalTypeIdTimeNs) (FieldTime value))) :: IO [Only FieldValue])     , testCase "microsecond timestamps round trip at the ends of the range of DuckDB" $ withConnection ":memory:" \conn -> do         let local (micros :: Int64) = utcToLocalTime utc (posixSecondsToUTCTime (fromRational (toInteger micros % 1000000)))-        forM_ [-9223372022400000000, -1, 0, maxBound - 1] \value -> do+        forM_ [minBound, negate (maxBound :: Int64) + 1, -9223372022400000001, -1, 0, maxBound - 1] \value -> do           _ <- execute_ conn "CREATE TABLE microsecond_timestamp(value TIMESTAMP)"           _ <- execute conn "INSERT INTO microsecond_timestamp VALUES (?)" (Only (local value))           (query_ conn "SELECT value FROM microsecond_timestamp" :: IO [Only LocalTime]) >>= (@?= [Only (local value)])           _ <- execute_ conn "DROP TABLE microsecond_timestamp"           pure ()-        -- The INT64 storage also holds the four hours before the range, but-        -- DuckDB does not accept them.-        forM_ [minBound, negate (maxBound :: Int64) + 1, -9223372022400000001] \value ->-          assertOutsideTimeRange (query conn "SELECT ?" (Only (local value)) :: IO [Only LocalTime])     , testCase "out-of-range dates and timestamps fail without abort" $ withConnection ":memory:" \conn -> do         let hugeDay = fromGregorian 10000000 1 1             local = LocalTime hugeDay (TimeOfDay 0 0 0)@@ -1590,17 +1603,15 @@             query_ conn (Query ("SELECT x::" <> dtype <> " FROM (VALUES ('v0'), (NULL), ('" <> label (size - 1) <> "')) t(x)")) :: IO [Only FieldValue]           rows @?= map Only [FieldEnum 0 (label (0 :: Int)), FieldNull, FieldEnum (fromIntegral (size - 1)) (label (size - 1))]     , testCase "DECIMAL and BIGINT values read as Double and Float give the casts of DuckDB" $ withConnection ":memory:" \conn -> do-        -- Each row has the read of v as Double and Float, and the casts of v-        -- to DOUBLE and FLOAT read at their own types. The value lists are-        -- fixed. See Note [DECIMAL as DOUBLE and FLOAT] in-        -- "Database.DuckDB.Simple.FromField". For scale 0 and a width of 19-        -- to 38, the library copies the cast of DuckDB 2.0.0, which is not-        -- always the nearest value. If a later DuckDB version changes the-        -- rounding of that cast, the first assertion fails and shows the new-        -- values of DuckDB. DuckDB 2.0.0 gives these values. The nearest values-        -- are -9007199254740994 and 3.3569516755834854e36.+        -- Each row compares a read with the native cast. DuckDB rounds+        -- HUGEINT storage directly to the target type at scale 0. These values+        -- detect incorrect DOUBLE results and FLOAT rounding through DOUBLE.+        -- See Note [DECIMAL as DOUBLE and FLOAT] in+        -- "Database.DuckDB.Simple.FromField".         query_ conn "SELECT (-9007199254740994)::DECIMAL(19,0)::DOUBLE, 3356951675583485076575379654993209529::DECIMAL(38,0)::DOUBLE"-          >>= (@?= [(-9007199254740992 :: Double, 3.356951675583485e36 :: Double)])+          >>= (@?= [(-9007199254740994 :: Double, 3.3569516755834854e36 :: Double)])+        query_ conn "SELECT 1267650675786093127411026624513::DECIMAL(38,0)::FLOAT"+          >>= (@?= [Only (fromInteger (2 ^ (100 :: Int) + 2 ^ (76 :: Int) + 1) :: Float)])         let bigints =               filter (\v -> v >= toInteger (minBound :: Int64) && v <= toInteger (maxBound :: Int64)) (toInteger (minBound :: Int64) : decimalIntegers 19)             kinds =@@ -1620,12 +1631,9 @@           if sqlType /= "BIGINT"             then [(sqlType, text, value, cast) | (text, _, _, value, cast) <- rows, castFloatToWord32 value /= castFloatToWord32 cast] @?= []             else do-              -- CI on macOS (Apple Silicon) showed a cast of DuckDB 2.0.0 from-              -- BIGINT to FLOAT that rounds two times, through DOUBLE, for some-              -- values above 2^53: 9007199791611905 gave 9.007199e15, and the-              -- nearest FLOAT is 9.0072e15. On Linux x86-64 and aarch64 the cast-              -- gave the nearest value. The read of the library is the nearest-              -- value on each platform. The cast of DuckDB is one of the two.+              -- The library must give the nearest FLOAT. The native cast may+              -- round directly or through DOUBLE on different platforms.+              -- Accept either native result while checking the library exactly.               let integer text = read (Text.unpack text) :: Integer                   nearest text = fromRational (toRational (integer text)) :: Float                   throughDouble text = realToFrac (fromInteger (integer text) :: Double) :: Float@@ -2434,8 +2442,8 @@               bind stmt [toField struct]               readAllRows next stmt >>= (@?= replicate 3000 (Only struct))     , testCase "an Integer outside the HUGEINT range raises the range error for each integer type" $ withConnection ":memory:" \conn -> do-        -- A BIGNUM value for these types stores a wrong value with no error.-        -- See Note [Integer parameters] in "Database.DuckDB.Simple.ToField".+        -- Keep the parameter range checks when native BIGNUM casts also check+        -- the range. See Note [Integer parameters] in "Database.DuckDB.Simple.ToField".         let huge = 2 ^ (128 :: Int) + 5 :: Integer             insert :: Text -> Integer -> IO (Either SQLError Int)             insert dtype value = do@@ -2667,15 +2675,15 @@       in  z2 `xor` (z2 `shiftR` 31)  -- | Unscaled integers that a DECIMAL of a width holds, with both signs: zero--- and the extremes; two values that DuckDB 2.0.0 does not cast to the nearest--- DOUBLE at scale 0; a value that two roundings change at scale 2; values next+-- and the extremes; values that expose the former HUGEINT rounding errors at+-- scale 0; a value that two roundings change at scale 2; values next -- to each power of two and next to the DOUBLE and FLOAT midpoints above it; and -- 400 values of 'splitMix64' with 1 to width digits. decimalIntegers :: Int -> [Integer] decimalIntegers width = filter (\v -> abs v <= limit) (special <> near <> spread)   where     limit = 10 ^ width - 1-    special = [0, limit, negate limit, -9007199254740994, 270628455292663770, 3356951675583485076575379654993209529]+    special = [0, limit, negate limit, -9007199254740994, 270628455292663770, 3356951675583485076575379654993209529, 1267650675786093127411026624513]     near =       [ sign * (2 ^ k + base + delta)       | k <- [0 .. 127 :: Int]
test/VariantRegressionTests.hs view
@@ -155,8 +155,7 @@               [(30000000000000, 30000000000000001), (-30000000000000, -30000000000000001), (maxBound - 1, maxBound - 1), (minBound, minBound)]                 :: [(Int64, Int64)]         void (executeMany conn "INSERT INTO wide_timestamps VALUES (?, ?)" [(Seconds seconds, Milliseconds millis) | (seconds, millis) <- units])-        -- DuckDB sorts the raw values. A cast of these values to VARCHAR gives an-        -- internal error in DuckDB 2.0.0, so the case does not read their text.+        -- Compare the order of the raw values without a cast to VARCHAR.         forM_ ["seconds", "millis"] \column ->           query_ conn (Query ("SELECT rowid FROM wide_timestamps ORDER BY " <> column)) >>= (@?= map Only [3, 1, 0, 2 :: Int64])         query_ conn "SELECT seconds, millis FROM wide_timestamps ORDER BY rowid"@@ -484,7 +483,7 @@               sqlErrorType err @?= Just ErrorInputInvalid             Right _ -> assertFailure "expected a VARIANT CRS error"         (query_ conn "SELECT 42" :: IO [Only Int64]) >>= (@?= [Only 42])-    , testCase "UNION, MAP, and ENUM payloads raise an error at each depth" $ withConnection ":memory:" \conn -> do+    , testCase "UNION, MAP, ENUM, and TYPE payloads raise an error at each depth" $ withConnection ":memory:" \conn -> do         let members =               listArray                 (0, 1)@@ -501,6 +500,7 @@         forM_           [ (FieldMap [(FieldText "k", FieldInt32 1)], "a VARIANT stores a MAP as a list of key and value objects")           , (FieldEnum 0 "a", "a VARIANT stores an ENUM as its text")+          , (FieldType (LogicalTypeScalar LogicalTypeIdInteger), "DuckDB VARIANT has no TYPE payload, so a TYPE value cannot bind as a VARIANT")           ]           \(payload, reason) -> forM_ (depths payload) \input -> do             result <- try (query conn "SELECT ?" (Only (Variant input)) :: IO [Only Variant])@@ -510,6 +510,12 @@                 assertBool (Text.unpack sqlErrorMessage) (Text.pack reason `Text.isInfixOf` sqlErrorMessage)               Right rows -> assertFailure ("expected the " <> reason <> " error, got " <> show rows)         (query_ conn "SELECT 42" :: IO [Only Int64]) >>= (@?= [Only 42])+        forM_ [("get_type(1)", "TYPE"), ("[NULL::TYPE]", "TYPE[]")] \(expression, sqlName) ->+          try (query_ conn (Query ("SELECT " <> expression <> " AS c")) :: IO [Only Variant]) >>= \case+            Left SQLError{sqlErrorMessage} ->+              sqlErrorMessage @?= ("duckdb-simple: column c has type " <> sqlName <> " but expected Variant: a VARIANT cannot hold a TYPE value")+            Right rows -> assertFailure ("expected Incompatible for " <> Text.unpack sqlName <> ", got " <> show rows)+        query_ conn "SELECT NULL::TYPE" >>= (@?= [Only (Variant FieldNull)])     , testCase "a Variant read of a JSON type raises an error at each depth, and the cast reads the parsed value" $ withConnection ":memory:" \conn -> do         let refused column expression sqlName =               try (query_ conn (Query ("SELECT " <> expression <> " AS " <> column)) :: IO [Only Variant]) >>= \case