duckdb 0.3.3-dev1312.0 → 0.3.3-dev1350.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.3-dev1312.0",
4
+ "version": "0.3.3-dev1350.0",
5
5
  "description": "DuckDB node.js API",
6
6
  "gypfile": true,
7
7
  "dependencies": {
package/src/duckdb.cpp CHANGED
@@ -4681,7 +4681,44 @@ static DefaultMacro internal_macros[] = {
4681
4681
  {DEFAULT_SCHEMA, "generate_subscripts", {"arr", "dim", nullptr}, "unnest(generate_series(1, array_length(arr, dim)))"},
4682
4682
  {DEFAULT_SCHEMA, "fdiv", {"x", "y", nullptr}, "floor(x/y)"},
4683
4683
  {DEFAULT_SCHEMA, "fmod", {"x", "y", nullptr}, "(x-y*floor(x/y))"},
4684
- {nullptr, nullptr, {nullptr}, nullptr}};
4684
+
4685
+ // algebraic list aggregates
4686
+ {DEFAULT_SCHEMA, "list_avg", {"l", nullptr}, "list_aggr(l, 'avg')"},
4687
+ {DEFAULT_SCHEMA, "list_var_samp", {"l", nullptr}, "list_aggr(l, 'var_samp')"},
4688
+ {DEFAULT_SCHEMA, "list_var_pop", {"l", nullptr}, "list_aggr(l, 'var_pop')"},
4689
+ {DEFAULT_SCHEMA, "list_stddev_pop", {"l", nullptr}, "list_aggr(l, 'stddev_pop')"},
4690
+ {DEFAULT_SCHEMA, "list_stddev_samp", {"l", nullptr}, "list_aggr(l, 'stddev_samp')"},
4691
+ {DEFAULT_SCHEMA, "list_sem", {"l", nullptr}, "list_aggr(l, 'sem')"},
4692
+
4693
+ // distributive list aggregates
4694
+ {DEFAULT_SCHEMA, "list_approx_count_distinct", {"l", nullptr}, "list_aggr(l, 'approx_count_distinct')"},
4695
+ {DEFAULT_SCHEMA, "list_bit_xor", {"l", nullptr}, "list_aggr(l, 'bit_xor')"},
4696
+ {DEFAULT_SCHEMA, "list_bit_or", {"l", nullptr}, "list_aggr(l, 'bit_or')"},
4697
+ {DEFAULT_SCHEMA, "list_bit_and", {"l", nullptr}, "list_aggr(l, 'bit_and')"},
4698
+ {DEFAULT_SCHEMA, "list_bool_and", {"l", nullptr}, "list_aggr(l, 'bool_and')"},
4699
+ {DEFAULT_SCHEMA, "list_bool_or", {"l", nullptr}, "list_aggr(l, 'bool_or')"},
4700
+ {DEFAULT_SCHEMA, "list_count", {"l", nullptr}, "list_aggr(l, 'count')"},
4701
+ {DEFAULT_SCHEMA, "list_entropy", {"l", nullptr}, "list_aggr(l, 'entropy')"},
4702
+ {DEFAULT_SCHEMA, "list_last", {"l", nullptr}, "list_aggr(l, 'last')"},
4703
+ {DEFAULT_SCHEMA, "list_first", {"l", nullptr}, "list_aggr(l, 'first')"},
4704
+ {DEFAULT_SCHEMA, "list_kurtosis", {"l", nullptr}, "list_aggr(l, 'kurtosis')"},
4705
+ {DEFAULT_SCHEMA, "list_min", {"l", nullptr}, "list_aggr(l, 'min')"},
4706
+ {DEFAULT_SCHEMA, "list_max", {"l", nullptr}, "list_aggr(l, 'max')"},
4707
+ {DEFAULT_SCHEMA, "list_product", {"l", nullptr}, "list_aggr(l, 'product')"},
4708
+ {DEFAULT_SCHEMA, "list_skewness", {"l", nullptr}, "list_aggr(l, 'skewness')"},
4709
+ {DEFAULT_SCHEMA, "list_sum", {"l", nullptr}, "list_aggr(l, 'sum')"},
4710
+ {DEFAULT_SCHEMA, "list_string_agg", {"l", nullptr}, "list_aggr(l, 'string_agg')"},
4711
+
4712
+ // holistic list aggregates
4713
+ {DEFAULT_SCHEMA, "list_mode", {"l", nullptr}, "list_aggr(l, 'mode')"},
4714
+ {DEFAULT_SCHEMA, "list_median", {"l", nullptr}, "list_aggr(l, 'median')"},
4715
+ {DEFAULT_SCHEMA, "list_mad", {"l", nullptr}, "list_aggr(l, 'mad')"},
4716
+
4717
+ // nested list aggregates
4718
+ {DEFAULT_SCHEMA, "list_histogram", {"l", nullptr}, "list_aggr(l, 'histogram')"},
4719
+
4720
+ {nullptr, nullptr, {nullptr}, nullptr}
4721
+ };
4685
4722
 
4686
4723
  unique_ptr<CreateMacroInfo> DefaultFunctionGenerator::CreateInternalMacroInfo(DefaultMacro &default_macro) {
4687
4724
  // parse the expression
@@ -78309,6 +78346,11 @@ struct ListPositionFun {
78309
78346
  static void RegisterFunction(BuiltinFunctions &set);
78310
78347
  };
78311
78348
 
78349
+ struct ListAggregateFun {
78350
+ static ScalarFunction GetFunction();
78351
+ static void RegisterFunction(BuiltinFunctions &set);
78352
+ };
78353
+
78312
78354
  struct CardinalityFun {
78313
78355
  static void RegisterFunction(BuiltinFunctions &set);
78314
78356
  };
@@ -86746,6 +86788,227 @@ void ListFlattenFun::RegisterFunction(BuiltinFunctions &set) {
86746
86788
 
86747
86789
 
86748
86790
 
86791
+
86792
+ namespace duckdb {
86793
+
86794
+ // FIXME: use a local state for each thread to increase performance?
86795
+ // FIXME: benchmark the use of simple_update against using update (if applicable)
86796
+
86797
+ struct ListAggregatesBindData : public FunctionData {
86798
+ ListAggregatesBindData(const LogicalType &stype_p, unique_ptr<Expression> aggr_expr_p);
86799
+ ~ListAggregatesBindData() override;
86800
+
86801
+ LogicalType stype;
86802
+ unique_ptr<Expression> aggr_expr;
86803
+
86804
+ unique_ptr<FunctionData> Copy() override;
86805
+ };
86806
+
86807
+ ListAggregatesBindData::ListAggregatesBindData(const LogicalType &stype_p, unique_ptr<Expression> aggr_expr_p)
86808
+ : stype(stype_p), aggr_expr(move(aggr_expr_p)) {
86809
+ }
86810
+
86811
+ unique_ptr<FunctionData> ListAggregatesBindData::Copy() {
86812
+ return make_unique<ListAggregatesBindData>(stype, aggr_expr->Copy());
86813
+ }
86814
+
86815
+ ListAggregatesBindData::~ListAggregatesBindData() {
86816
+ }
86817
+
86818
+ struct StateVector {
86819
+ StateVector(idx_t count_p, unique_ptr<Expression> aggr_expr_p)
86820
+ : count(count_p), aggr_expr(move(aggr_expr_p)), state_vector(Vector(LogicalType::POINTER, count_p)) {
86821
+ }
86822
+
86823
+ ~StateVector() {
86824
+ // destroy objects within the aggregate states
86825
+ auto &aggr = (BoundAggregateExpression &)*aggr_expr;
86826
+ if (aggr.function.destructor) {
86827
+ aggr.function.destructor(state_vector, count);
86828
+ }
86829
+ }
86830
+
86831
+ idx_t count;
86832
+ unique_ptr<Expression> aggr_expr;
86833
+ Vector state_vector;
86834
+ };
86835
+
86836
+ static void ListAggregateFunction(DataChunk &args, ExpressionState &state, Vector &result) {
86837
+
86838
+ D_ASSERT(args.ColumnCount() == 2);
86839
+ auto count = args.size();
86840
+ Vector &lists = args.data[0];
86841
+
86842
+ // set the result vector
86843
+ result.SetVectorType(VectorType::FLAT_VECTOR);
86844
+ auto &result_validity = FlatVector::Validity(result);
86845
+
86846
+ if (lists.GetType().id() == LogicalTypeId::SQLNULL) {
86847
+ result_validity.SetInvalid(0);
86848
+ return;
86849
+ }
86850
+
86851
+ // get the aggregate function
86852
+ auto &func_expr = (BoundFunctionExpression &)state.expr;
86853
+ auto &info = (ListAggregatesBindData &)*func_expr.bind_info;
86854
+ auto &aggr = (BoundAggregateExpression &)*info.aggr_expr;
86855
+
86856
+ D_ASSERT(aggr.function.update);
86857
+
86858
+ auto lists_size = ListVector::GetListSize(lists);
86859
+ auto &child_vector = ListVector::GetEntry(lists);
86860
+
86861
+ VectorData child_data;
86862
+ child_vector.Orrify(lists_size, child_data);
86863
+
86864
+ VectorData lists_data;
86865
+ lists.Orrify(count, lists_data);
86866
+ auto list_entries = (list_entry_t *)lists_data.data;
86867
+
86868
+ // state_buffer holds the state for each list of this chunk
86869
+ idx_t size = aggr.function.state_size();
86870
+ auto state_buffer = unique_ptr<data_t[]>(new data_t[size * count]);
86871
+
86872
+ // state vector for initialize and finalize
86873
+ StateVector state_vector(count, info.aggr_expr->Copy());
86874
+ auto states = FlatVector::GetData<data_ptr_t>(state_vector.state_vector);
86875
+
86876
+ // state vector of STANDARD_VECTOR_SIZE holds the pointers to the states
86877
+ Vector state_vector_update = Vector(LogicalType::POINTER);
86878
+ auto states_update = FlatVector::GetData<data_ptr_t>(state_vector_update);
86879
+
86880
+ // selection vector pointing to the data
86881
+ SelectionVector sel_vector(STANDARD_VECTOR_SIZE);
86882
+ idx_t states_idx = 0;
86883
+
86884
+ for (idx_t i = 0; i < count; i++) {
86885
+
86886
+ // initialize the state for this list
86887
+ auto state_ptr = state_buffer.get() + size * i;
86888
+ states[i] = state_ptr;
86889
+ aggr.function.initialize(states[i]);
86890
+
86891
+ auto lists_index = lists_data.sel->get_index(i);
86892
+ const auto &list_entry = list_entries[lists_index];
86893
+
86894
+ // nothing to do for this list
86895
+ if (!lists_data.validity.RowIsValid(lists_index)) {
86896
+ result_validity.SetInvalid(i);
86897
+ continue;
86898
+ }
86899
+
86900
+ // skip empty list
86901
+ if (list_entry.length == 0) {
86902
+ continue;
86903
+ }
86904
+
86905
+ auto source_idx = child_data.sel->get_index(list_entry.offset);
86906
+ idx_t child_idx = 0;
86907
+
86908
+ while (child_idx < list_entry.length) {
86909
+
86910
+ // states vector is full, update
86911
+ if (states_idx == STANDARD_VECTOR_SIZE) {
86912
+
86913
+ // update the aggregate state(s)
86914
+ Vector slice = Vector(child_vector, sel_vector, states_idx);
86915
+ aggr.function.update(&slice, aggr.bind_info.get(), 1, state_vector_update, states_idx);
86916
+
86917
+ // reset values
86918
+ states_idx = 0;
86919
+ }
86920
+
86921
+ sel_vector.set_index(states_idx, source_idx + child_idx);
86922
+ states_update[states_idx] = state_ptr;
86923
+ states_idx++;
86924
+ child_idx++;
86925
+ }
86926
+ }
86927
+
86928
+ // update the remaining elements of the last list(s)
86929
+ if (states_idx != 0) {
86930
+ Vector slice = Vector(child_vector, sel_vector, states_idx);
86931
+ aggr.function.update(&slice, aggr.bind_info.get(), 1, state_vector_update, states_idx);
86932
+ }
86933
+
86934
+ // finalize all the aggregate states
86935
+ aggr.function.finalize(state_vector.state_vector, aggr.bind_info.get(), result, count, 0);
86936
+ }
86937
+
86938
+ static unique_ptr<FunctionData> ListAggregateBind(ClientContext &context, ScalarFunction &bound_function,
86939
+ vector<unique_ptr<Expression>> &arguments) {
86940
+
86941
+ // the list column and the name of the aggregate function
86942
+ D_ASSERT(bound_function.arguments.size() == 2);
86943
+ D_ASSERT(arguments.size() == 2);
86944
+
86945
+ if (arguments[0]->return_type.id() == LogicalTypeId::SQLNULL) {
86946
+ bound_function.arguments[0] = LogicalType::SQLNULL;
86947
+ bound_function.return_type = LogicalType::SQLNULL;
86948
+ return make_unique<VariableReturnBindData>(bound_function.return_type);
86949
+ }
86950
+
86951
+ D_ASSERT(LogicalTypeId::LIST == arguments[0]->return_type.id());
86952
+ auto list_child_type = ListType::GetChildType(arguments[0]->return_type);
86953
+ bound_function.return_type = list_child_type;
86954
+
86955
+ if (!arguments[1]->IsFoldable()) {
86956
+ throw InvalidInputException("Aggregate function name must be a constant");
86957
+ }
86958
+
86959
+ // get the function name
86960
+ Value function_value = ExpressionExecutor::EvaluateScalar(*arguments[1]);
86961
+ auto function_name = StringValue::Get(function_value);
86962
+
86963
+ vector<LogicalType> types;
86964
+ types.push_back(list_child_type);
86965
+
86966
+ // create the child expression and its type
86967
+ vector<unique_ptr<Expression>> children;
86968
+ auto expr = make_unique<BoundConstantExpression>(Value(LogicalType::SQLNULL));
86969
+ expr->return_type = list_child_type;
86970
+ children.push_back(move(expr));
86971
+
86972
+ // look up the aggregate function in the catalog
86973
+ QueryErrorContext error_context(nullptr, 0);
86974
+ auto func = (AggregateFunctionCatalogEntry *)Catalog::GetCatalog(context).GetEntry<AggregateFunctionCatalogEntry>(
86975
+ context, DEFAULT_SCHEMA, function_name, false, error_context);
86976
+ D_ASSERT(func->type == CatalogType::AGGREGATE_FUNCTION_ENTRY);
86977
+
86978
+ // find a matching aggregate function
86979
+ string error;
86980
+ auto best_function_idx = Function::BindFunction(func->name, func->functions, types, error);
86981
+ if (best_function_idx == DConstants::INVALID_INDEX) {
86982
+ throw BinderException("No matching aggregate function");
86983
+ }
86984
+
86985
+ // found a matching function, bind it as an aggregate
86986
+ auto &best_function = func->functions[best_function_idx];
86987
+ auto bound_aggr_function = AggregateFunction::BindAggregateFunction(context, best_function, move(children));
86988
+
86989
+ bound_function.arguments[0] =
86990
+ LogicalType::LIST(bound_aggr_function->function.arguments[0]); // for proper casting of the vectors
86991
+ bound_function.return_type = bound_aggr_function->function.return_type;
86992
+ return make_unique<ListAggregatesBindData>(bound_function.return_type, move(bound_aggr_function));
86993
+ }
86994
+
86995
+ ScalarFunction ListAggregateFun::GetFunction() {
86996
+ return ScalarFunction({LogicalType::LIST(LogicalType::ANY), LogicalType::VARCHAR}, LogicalType::ANY,
86997
+ ListAggregateFunction, false, ListAggregateBind, nullptr, nullptr, nullptr);
86998
+ }
86999
+
87000
+ void ListAggregateFun::RegisterFunction(BuiltinFunctions &set) {
87001
+ set.AddFunction({"list_aggregate", "array_aggregate", "list_aggr", "array_aggr"}, GetFunction());
87002
+ }
87003
+
87004
+ } // namespace duckdb
87005
+
87006
+
87007
+
87008
+
87009
+
87010
+
87011
+
86749
87012
  namespace duckdb {
86750
87013
 
86751
87014
  static void ListConcatFunction(DataChunk &args, ExpressionState &state, Vector &result) {
@@ -89027,6 +89290,7 @@ void BuiltinFunctions::RegisterNestedFunctions() {
89027
89290
  Register<ListConcatFun>();
89028
89291
  Register<ListContainsFun>();
89029
89292
  Register<ListPositionFun>();
89293
+ Register<ListAggregateFun>();
89030
89294
  Register<ListValueFun>();
89031
89295
  Register<ListExtractFun>();
89032
89296
  Register<ListRangeFun>();
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 "452b83160"
15
- #define DUCKDB_VERSION "v0.3.3-dev1312"
14
+ #define DUCKDB_SOURCE_ID "9d561d4d9"
15
+ #define DUCKDB_VERSION "v0.3.3-dev1350"
16
16
  //===----------------------------------------------------------------------===//
17
17
  // DuckDB
18
18
  //