duckdb 0.3.5-dev303.0 → 0.3.5-dev324.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "duckdb",
3
3
  "main": "./lib/duckdb.js",
4
- "version": "0.3.5-dev303.0",
4
+ "version": "0.3.5-dev324.0",
5
5
  "description": "DuckDB node.js API",
6
6
  "gypfile": true,
7
7
  "dependencies": {
package/src/duckdb.cpp CHANGED
@@ -22899,40 +22899,57 @@ bool TryCast::Operation(double input, double &result, bool strict) {
22899
22899
  //===--------------------------------------------------------------------===//
22900
22900
  // Cast String -> Numeric
22901
22901
  //===--------------------------------------------------------------------===//
22902
+ template <typename T>
22903
+ struct IntegerCastData {
22904
+ using Result = T;
22905
+ Result result;
22906
+ uint8_t decimal_count;
22907
+ };
22908
+
22902
22909
  struct IntegerCastOperation {
22903
22910
  template <class T, bool NEGATIVE>
22904
- static bool HandleDigit(T &result, uint8_t digit) {
22911
+ static bool HandleDigit(T &state, uint8_t digit) {
22912
+ using result_t = typename T::Result;
22905
22913
  if (NEGATIVE) {
22906
- if (result < (NumericLimits<T>::Minimum() + digit) / 10) {
22914
+ if (state.result < (NumericLimits<result_t>::Minimum() + digit) / 10) {
22907
22915
  return false;
22908
22916
  }
22909
- result = result * 10 - digit;
22917
+ state.result = state.result * 10 - digit;
22910
22918
  } else {
22911
- if (result > (NumericLimits<T>::Maximum() - digit) / 10) {
22919
+ if (state.result > (NumericLimits<result_t>::Maximum() - digit) / 10) {
22912
22920
  return false;
22913
22921
  }
22914
- result = result * 10 + digit;
22922
+ state.result = state.result * 10 + digit;
22915
22923
  }
22916
22924
  return true;
22917
22925
  }
22918
22926
 
22919
22927
  template <class T, bool NEGATIVE>
22920
- static bool HandleExponent(T &result, int64_t exponent) {
22921
- double dbl_res = result * std::pow(10.0L, exponent);
22922
- if (dbl_res < NumericLimits<T>::Minimum() || dbl_res > NumericLimits<T>::Maximum()) {
22928
+ static bool HandleExponent(T &state, int32_t exponent) {
22929
+ using result_t = typename T::Result;
22930
+ double dbl_res = state.result * std::pow(10.0L, exponent);
22931
+ if (dbl_res < NumericLimits<result_t>::Minimum() || dbl_res > NumericLimits<result_t>::Maximum()) {
22923
22932
  return false;
22924
22933
  }
22925
- result = (T)dbl_res;
22934
+ state.result = (result_t)dbl_res;
22926
22935
  return true;
22927
22936
  }
22928
22937
 
22929
22938
  template <class T, bool NEGATIVE>
22930
- static bool HandleDecimal(T &result, uint8_t digit) {
22939
+ static bool HandleDecimal(T &state, uint8_t digit) {
22940
+ if (!state.decimal_count) {
22941
+ if (NEGATIVE) {
22942
+ state.result -= (digit >= 5);
22943
+ } else {
22944
+ state.result += (digit >= 5);
22945
+ }
22946
+ }
22947
+ ++state.decimal_count;
22931
22948
  return true;
22932
22949
  }
22933
22950
 
22934
22951
  template <class T>
22935
- static bool Finalize(T &result) {
22952
+ static bool Finalize(T &state) {
22936
22953
  return true;
22937
22954
  }
22938
22955
  };
@@ -22990,18 +23007,19 @@ static bool IntegerCastLoop(const char *buf, idx_t len, T &result, bool strict)
22990
23007
  if (pos >= len) {
22991
23008
  return false;
22992
23009
  }
22993
- int32_t exponent = 0;
23010
+ using ExponentData = IntegerCastData<int32_t>;
23011
+ ExponentData exponent {0, false};
22994
23012
  int negative = buf[pos] == '-';
22995
23013
  if (negative) {
22996
- if (!IntegerCastLoop<int32_t, true, false>(buf + pos, len - pos, exponent, strict)) {
23014
+ if (!IntegerCastLoop<ExponentData, true, false>(buf + pos, len - pos, exponent, strict)) {
22997
23015
  return false;
22998
23016
  }
22999
23017
  } else {
23000
- if (!IntegerCastLoop<int32_t, false, false>(buf + pos, len - pos, exponent, strict)) {
23018
+ if (!IntegerCastLoop<ExponentData, false, false>(buf + pos, len - pos, exponent, strict)) {
23001
23019
  return false;
23002
23020
  }
23003
23021
  }
23004
- return OP::template HandleExponent<T, NEGATIVE>(result, exponent);
23022
+ return OP::template HandleExponent<T, NEGATIVE>(result, exponent.result);
23005
23023
  }
23006
23024
  }
23007
23025
  return false;
@@ -23049,6 +23067,16 @@ static bool TryIntegerCast(const char *buf, idx_t len, T &result, bool strict) {
23049
23067
  }
23050
23068
  }
23051
23069
 
23070
+ template <typename T, bool IS_SIGNED = true>
23071
+ static inline bool TrySimpleIntegerCast(const char *buf, idx_t len, T &result, bool strict) {
23072
+ IntegerCastData<T> data;
23073
+ if (TryIntegerCast<IntegerCastData<T>, IS_SIGNED>(buf, len, data, strict)) {
23074
+ result = data.result;
23075
+ return true;
23076
+ }
23077
+ return false;
23078
+ }
23079
+
23052
23080
  template <>
23053
23081
  bool TryCast::Operation(string_t input, bool &result, bool strict) {
23054
23082
  auto input_data = input.GetDataUnsafe();
@@ -23095,36 +23123,36 @@ bool TryCast::Operation(string_t input, bool &result, bool strict) {
23095
23123
  }
23096
23124
  template <>
23097
23125
  bool TryCast::Operation(string_t input, int8_t &result, bool strict) {
23098
- return TryIntegerCast<int8_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23126
+ return TrySimpleIntegerCast<int8_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23099
23127
  }
23100
23128
  template <>
23101
23129
  bool TryCast::Operation(string_t input, int16_t &result, bool strict) {
23102
- return TryIntegerCast<int16_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23130
+ return TrySimpleIntegerCast<int16_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23103
23131
  }
23104
23132
  template <>
23105
23133
  bool TryCast::Operation(string_t input, int32_t &result, bool strict) {
23106
- return TryIntegerCast<int32_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23134
+ return TrySimpleIntegerCast<int32_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23107
23135
  }
23108
23136
  template <>
23109
23137
  bool TryCast::Operation(string_t input, int64_t &result, bool strict) {
23110
- return TryIntegerCast<int64_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23138
+ return TrySimpleIntegerCast<int64_t>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23111
23139
  }
23112
23140
 
23113
23141
  template <>
23114
23142
  bool TryCast::Operation(string_t input, uint8_t &result, bool strict) {
23115
- return TryIntegerCast<uint8_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23143
+ return TrySimpleIntegerCast<uint8_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23116
23144
  }
23117
23145
  template <>
23118
23146
  bool TryCast::Operation(string_t input, uint16_t &result, bool strict) {
23119
- return TryIntegerCast<uint16_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23147
+ return TrySimpleIntegerCast<uint16_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23120
23148
  }
23121
23149
  template <>
23122
23150
  bool TryCast::Operation(string_t input, uint32_t &result, bool strict) {
23123
- return TryIntegerCast<uint32_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23151
+ return TrySimpleIntegerCast<uint32_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23124
23152
  }
23125
23153
  template <>
23126
23154
  bool TryCast::Operation(string_t input, uint64_t &result, bool strict) {
23127
- return TryIntegerCast<uint64_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23155
+ return TrySimpleIntegerCast<uint64_t, false>(input.GetDataUnsafe(), input.GetSize(), result, strict);
23128
23156
  }
23129
23157
 
23130
23158
  template <class T>
@@ -23436,6 +23464,7 @@ struct HugeIntCastData {
23436
23464
  hugeint_t hugeint;
23437
23465
  int64_t intermediate;
23438
23466
  uint8_t digits;
23467
+ bool decimal;
23439
23468
 
23440
23469
  bool Flush() {
23441
23470
  if (digits == 0 && intermediate == 0) {
@@ -23482,7 +23511,7 @@ struct HugeIntegerCastOperation {
23482
23511
  }
23483
23512
 
23484
23513
  template <class T, bool NEGATIVE>
23485
- static bool HandleExponent(T &result, int64_t exponent) {
23514
+ static bool HandleExponent(T &result, int32_t exponent) {
23486
23515
  if (!result.Flush()) {
23487
23516
  return false;
23488
23517
  }
@@ -23508,6 +23537,19 @@ struct HugeIntegerCastOperation {
23508
23537
 
23509
23538
  template <class T, bool NEGATIVE>
23510
23539
  static bool HandleDecimal(T &result, uint8_t digit) {
23540
+ // Integer casts round
23541
+ if (!result.decimal) {
23542
+ if (!result.Flush()) {
23543
+ return false;
23544
+ }
23545
+ if (NEGATIVE) {
23546
+ result.intermediate = -(digit >= 5);
23547
+ } else {
23548
+ result.intermediate = (digit >= 5);
23549
+ }
23550
+ }
23551
+ result.decimal = true;
23552
+
23511
23553
  return true;
23512
23554
  }
23513
23555
 
@@ -23561,7 +23603,7 @@ struct DecimalCastOperation {
23561
23603
  }
23562
23604
 
23563
23605
  template <class T, bool NEGATIVE>
23564
- static bool HandleExponent(T &state, int64_t exponent) {
23606
+ static bool HandleExponent(T &state, int32_t exponent) {
23565
23607
  Finalize<T>(state);
23566
23608
  if (exponent < 0) {
23567
23609
  for (idx_t i = 0; i < idx_t(-exponent); i++) {
@@ -24052,7 +24094,12 @@ bool TryCastToDecimal::Operation(double input, hugeint_t &result, string *error_
24052
24094
  //===--------------------------------------------------------------------===//
24053
24095
  template <class SRC, class DST>
24054
24096
  bool TryCastDecimalToNumeric(SRC input, DST &result, string *error_message, uint8_t scale) {
24055
- auto scaled_value = input / NumericHelper::POWERS_OF_TEN[scale];
24097
+ // Round away from 0.
24098
+ const auto power = NumericHelper::POWERS_OF_TEN[scale];
24099
+ // https://graphics.stanford.edu/~seander/bithacks.html#ConditionalNegate
24100
+ const auto fNegate = int64_t(input < 0);
24101
+ const auto rounding = ((power ^ -fNegate) + fNegate) / 2;
24102
+ const auto scaled_value = (input + rounding) / power;
24056
24103
  if (!TryCast::Operation<SRC, DST>(scaled_value, result)) {
24057
24104
  string error = StringUtil::Format("Failed to cast decimal value %d to type %s", scaled_value, GetTypeId<DST>());
24058
24105
  HandleCastError::AssignError(error, error_message);
@@ -24063,7 +24110,9 @@ bool TryCastDecimalToNumeric(SRC input, DST &result, string *error_message, uint
24063
24110
 
24064
24111
  template <class DST>
24065
24112
  bool TryCastHugeDecimalToNumeric(hugeint_t input, DST &result, string *error_message, uint8_t scale) {
24066
- auto scaled_value = input / Hugeint::POWERS_OF_TEN[scale];
24113
+ const auto power = Hugeint::POWERS_OF_TEN[scale];
24114
+ const auto rounding = ((input < 0) ? -power : power) / 2;
24115
+ auto scaled_value = (input + rounding) / power;
24067
24116
  if (!TryCast::Operation<hugeint_t, DST>(scaled_value, result)) {
24068
24117
  string error = StringUtil::Format("Failed to cast decimal value %s to type %s",
24069
24118
  ConvertToString::Operation(scaled_value), GetTypeId<DST>());
@@ -104383,7 +104432,7 @@ static unique_ptr<FunctionData> ReadCSVBind(ClientContext &context, TableFunctio
104383
104432
  if (val.type().id() != LogicalTypeId::VARCHAR) {
104384
104433
  throw BinderException("read_csv requires a type specification as string");
104385
104434
  }
104386
- return_types.emplace_back(TransformStringToLogicalTypeId(StringValue::Get(val)));
104435
+ return_types.emplace_back(TransformStringToLogicalType(StringValue::Get(val)));
104387
104436
  }
104388
104437
  if (names.empty()) {
104389
104438
  throw BinderException("read_csv requires at least a single column as input!");
package/src/duckdb.hpp CHANGED
@@ -11,8 +11,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
11
11
  #pragma once
12
12
  #define DUCKDB_AMALGAMATION 1
13
13
  #define DUCKDB_AMALGAMATION_EXTENDED 1
14
- #define DUCKDB_SOURCE_ID "6408b842a"
15
- #define DUCKDB_VERSION "v0.3.5-dev303"
14
+ #define DUCKDB_SOURCE_ID "fc2413a5e"
15
+ #define DUCKDB_VERSION "v0.3.5-dev324"
16
16
  //===----------------------------------------------------------------------===//
17
17
  // DuckDB
18
18
  //