From eddc9267c20dc7a22504e0c2d7bc339bfb2e70f8 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Thu, 18 May 2023 21:22:52 +0200 Subject: [PATCH 01/28] Remove C-style casts and add clang-tidy rule (WIP) --- .clang-tidy | 2 +- Makefile | 2 +- .../scalar/list/list_aggregates.cpp | 4 +- .../scalar/list/list_lambdas.cpp | 2 +- .../aggregate/distinct_aggregate_data.cpp | 2 +- .../aggregate/sorted_aggregate_function.cpp | 2 +- .../scalar/system/aggregate_export.cpp | 20 +++---- src/function/table/table_scan.cpp | 2 +- src/include/duckdb/common/typedefs.hpp | 12 +++++ .../execution/expression_executor_state.hpp | 16 +++++- .../join_order/cardinality_estimator.hpp | 4 +- src/include/duckdb/parser/base_expression.hpp | 14 ++--- .../parser/expression/between_expression.hpp | 2 +- .../parser/expression/bound_expression.hpp | 2 +- .../parser/expression/case_expression.hpp | 2 +- .../parser/expression/cast_expression.hpp | 2 +- .../parser/expression/collate_expression.hpp | 2 +- .../expression/columnref_expression.hpp | 2 +- .../expression/comparison_expression.hpp | 2 +- .../expression/conjunction_expression.hpp | 2 +- .../parser/expression/constant_expression.hpp | 2 +- .../parser/expression/function_expression.hpp | 2 +- .../parser/expression/lambda_expression.hpp | 2 +- .../parser/expression/operator_expression.hpp | 2 +- .../expression/parameter_expression.hpp | 2 +- .../positional_reference_expression.hpp | 2 +- .../parser/expression/star_expression.hpp | 2 +- .../parser/expression/subquery_expression.hpp | 2 +- .../parser/expression/window_expression.hpp | 2 +- src/include/duckdb/parser/expression_map.hpp | 2 +- .../duckdb/parser/parsed_expression.hpp | 6 ++- src/include/duckdb/parser/result_modifier.hpp | 41 +++++++++++++-- .../parser/statement/select_statement.hpp | 2 +- src/include/duckdb/parser/tableref.hpp | 3 +- .../duckdb/parser/tableref/basetableref.hpp | 2 +- .../duckdb/parser/tableref/emptytableref.hpp | 2 +- .../parser/tableref/expressionlistref.hpp | 2 +- .../duckdb/parser/tableref/joinref.hpp | 2 +- .../duckdb/parser/tableref/pivotref.hpp | 2 +- .../duckdb/parser/tableref/subqueryref.hpp | 2 +- .../parser/tableref/table_function_ref.hpp | 2 +- src/include/duckdb/planner/bind_context.hpp | 2 +- .../duckdb/planner/bound_result_modifier.hpp | 35 ++++++++++++- src/include/duckdb/planner/expression.hpp | 12 ++--- .../expression/bound_aggregate_expression.hpp | 2 +- .../expression/bound_between_expression.hpp | 2 +- .../expression/bound_case_expression.hpp | 2 +- .../expression/bound_cast_expression.hpp | 2 +- .../expression/bound_columnref_expression.hpp | 2 +- .../bound_comparison_expression.hpp | 2 +- .../bound_conjunction_expression.hpp | 2 +- .../expression/bound_constant_expression.hpp | 2 +- .../expression/bound_function_expression.hpp | 2 +- .../expression/bound_lambda_expression.hpp | 2 +- .../expression/bound_lambdaref_expression.hpp | 2 +- .../expression/bound_operator_expression.hpp | 2 +- .../expression/bound_parameter_expression.hpp | 2 +- .../expression/bound_reference_expression.hpp | 2 +- .../expression/bound_subquery_expression.hpp | 2 +- .../expression/bound_unnest_expression.hpp | 2 +- .../expression/bound_window_expression.hpp | 2 +- .../planner/filter/conjunction_filter.hpp | 6 +++ .../duckdb/planner/filter/constant_filter.hpp | 3 ++ .../duckdb/planner/filter/null_filter.hpp | 6 +++ src/include/duckdb/planner/table_binding.hpp | 26 ++++++++++ src/include/duckdb/planner/table_filter.hpp | 17 ++++++ .../planner/tableref/bound_pivotref.hpp | 3 ++ src/optimizer/deliminator.cpp | 24 ++++----- src/optimizer/filter_combiner.cpp | 6 +-- .../join_order/cardinality_estimator.cpp | 20 +++---- src/optimizer/matcher/expression_matcher.cpp | 2 +- src/optimizer/pullup/pullup_projection.cpp | 4 +- src/optimizer/rule/distributivity.cpp | 4 +- .../statistics/operator/propagate_filter.cpp | 12 ++--- .../statistics/operator/propagate_get.cpp | 4 +- src/parser/base_expression.cpp | 7 +-- src/parser/expression/between_expression.cpp | 8 +-- src/parser/expression/case_expression.cpp | 12 ++--- src/parser/expression/cast_expression.cpp | 8 +-- src/parser/expression/collate_expression.cpp | 6 +-- .../expression/columnref_expression.cpp | 8 +-- .../expression/comparison_expression.cpp | 6 +-- .../expression/conjunction_expression.cpp | 4 +- src/parser/expression/constant_expression.cpp | 4 +- src/parser/expression/function_expression.cpp | 24 ++++----- src/parser/expression/lambda_expression.cpp | 4 +- src/parser/expression/operator_expression.cpp | 8 +-- .../expression/parameter_expression.cpp | 4 +- .../positional_reference_expression.cpp | 6 +-- src/parser/expression/star_expression.cpp | 18 +++---- src/parser/expression/subquery_expression.cpp | 10 ++-- src/parser/expression/window_expression.cpp | 37 +++++-------- src/parser/parsed_expression.cpp | 52 ++++++++++++------- src/parser/parsed_expression_iterator.cpp | 8 +-- src/parser/parser.cpp | 6 +-- src/parser/query_node.cpp | 13 ++--- src/parser/query_node/select_node.cpp | 17 ++---- src/parser/result_modifier.cpp | 43 ++++++++------- src/parser/statement/select_statement.cpp | 6 +-- src/parser/tableref.cpp | 15 ++++-- src/parser/tableref/basetableref.cpp | 8 +-- src/parser/tableref/emptytableref.cpp | 2 +- src/parser/tableref/expressionlistref.cpp | 10 ++-- src/parser/tableref/joinref.cpp | 12 ++--- src/parser/tableref/pivotref.cpp | 25 ++++----- src/parser/tableref/subqueryref.cpp | 6 +-- src/parser/tableref/table_function.cpp | 6 +-- .../expression/transform_function.cpp | 2 +- src/planner/bind_context.cpp | 11 ++-- src/planner/binder.cpp | 2 +- .../expression/bind_function_expression.cpp | 2 +- src/planner/binder/expression/bind_lambda.cpp | 2 +- .../expression/bind_star_expression.cpp | 2 +- .../expression/bind_subquery_expression.cpp | 9 ++-- .../binder/query_node/bind_select_node.cpp | 16 +++--- .../binder/query_node/plan_query_node.cpp | 8 +-- src/planner/binder/statement/bind_create.cpp | 2 +- src/planner/binder/statement/bind_insert.cpp | 4 +- src/planner/binder/tableref/bind_pivot.cpp | 2 +- .../binder/tableref/bind_table_function.cpp | 4 +- src/planner/bound_result_modifier.cpp | 27 ++++++---- src/planner/expression.cpp | 15 ++++++ .../expression/bound_aggregate_expression.cpp | 10 ++-- .../expression/bound_between_expression.cpp | 10 ++-- .../expression/bound_case_expression.cpp | 10 ++-- .../expression/bound_cast_expression.cpp | 6 +-- .../expression/bound_columnref_expression.cpp | 4 +- .../bound_comparison_expression.cpp | 8 +-- .../bound_conjunction_expression.cpp | 4 +- .../expression/bound_constant_expression.cpp | 4 +- src/planner/expression/bound_expression.cpp | 2 +- .../expression/bound_function_expression.cpp | 7 ++- .../expression/bound_lambda_expression.cpp | 9 ++-- .../expression/bound_lambdaref_expression.cpp | 4 +- .../expression/bound_operator_expression.cpp | 7 ++- .../expression/bound_parameter_expression.cpp | 4 +- .../expression/bound_reference_expression.cpp | 4 +- .../expression/bound_subquery_expression.cpp | 2 +- .../expression/bound_unnest_expression.cpp | 6 +-- .../expression/bound_window_expression.cpp | 29 +++-------- .../expression_binder/base_select_binder.cpp | 4 +- src/planner/expression_iterator.cpp | 4 +- src/planner/filter/conjunction_filter.cpp | 4 +- src/planner/filter/constant_filter.cpp | 2 +- src/planner/logical_operator.cpp | 7 ++- src/planner/logical_operator_visitor.cpp | 2 +- src/planner/table_filter.cpp | 2 +- .../prepared_statement_verifier.cpp | 2 +- src/verification/statement_verifier.cpp | 7 ++- tools/pythonpkg/duckdb_python.cpp | 3 +- 150 files changed, 617 insertions(+), 465 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 0ddcce906883..1f0c42bc8937 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '-*,clang-diagnostic-*,bugprone-*,performance-*,google-explicit-constructor,google-build-using-namespace,google-runtime-int,misc-definitions-in-headers,modernize-use-nullptr,modernize-use-override,-bugprone-macro-parentheses,readability-braces-around-statements,-bugprone-branch-clone,readability-identifier-naming,hicpp-exception-baseclass,misc-throw-by-value-catch-by-reference,-bugprone-signed-char-misuse,-bugprone-misplaced-widening-cast,-bugprone-sizeof-expression,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,google-global-names-in-headers,llvm-header-guard,misc-definitions-in-headers,modernize-use-emplace,modernize-use-bool-literals,-performance-inefficient-string-concatenation,-performance-no-int-to-ptr,readability-container-size-empty' +Checks: '-*,clang-diagnostic-*,bugprone-*,performance-*,google-explicit-constructor,google-build-using-namespace,google-runtime-int,misc-definitions-in-headers,modernize-use-nullptr,modernize-use-override,-bugprone-macro-parentheses,readability-braces-around-statements,-bugprone-branch-clone,readability-identifier-naming,hicpp-exception-baseclass,misc-throw-by-value-catch-by-reference,-bugprone-signed-char-misuse,-bugprone-misplaced-widening-cast,-bugprone-sizeof-expression,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,google-global-names-in-headers,llvm-header-guard,misc-definitions-in-headers,modernize-use-emplace,modernize-use-bool-literals,-performance-inefficient-string-concatenation,-performance-no-int-to-ptr,readability-container-size-empty,cppcoreguidelines-pro-type-cstyle-cast' WarningsAsErrors: '*' HeaderFilterRegex: '.*^(re2.h)' AnalyzeTemporaryDtors: false diff --git a/Makefile b/Makefile index 7fe45812a670..6b572445cf95 100644 --- a/Makefile +++ b/Makefile @@ -263,7 +263,7 @@ amaldebug: tidy-check: mkdir -p ./build/tidy && \ cd build/tidy && \ - cmake -DCLANG_TIDY=1 -DDISABLE_UNITY=1 -DBUILD_PARQUET_EXTENSION=TRUE -DBUILD_PYTHON_PKG=TRUE ${EXTRA_CMAKE_VARIABLES} -DBUILD_SHELL=0 ../.. && \ + cmake -DCLANG_TIDY=1 -DDISABLE_UNITY=1 -DBUILD_PARQUET_EXTENSION=TRUE ${EXTRA_CMAKE_VARIABLES} -DBUILD_SHELL=0 ../.. && \ python3 ../../scripts/run-clang-tidy.py -quiet ${TIDY_THREAD_PARAMETER} ${TIDY_BINARY_PARAMETER} tidy-fix: diff --git a/src/core_functions/scalar/list/list_aggregates.cpp b/src/core_functions/scalar/list/list_aggregates.cpp index 415db273de16..cf6526673c73 100644 --- a/src/core_functions/scalar/list/list_aggregates.cpp +++ b/src/core_functions/scalar/list/list_aggregates.cpp @@ -27,8 +27,8 @@ struct ListAggregatesBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const ListAggregatesBindData &)other_p; - return stype == other.stype && aggr_expr->Equals(other.aggr_expr.get()); + auto &other = other_p.Cast(); + return stype == other.stype && aggr_expr->Equals(*other.aggr_expr); } static void Serialize(FieldWriter &writer, const FunctionData *bind_data_p, const ScalarFunction &function) { throw NotImplementedException("FIXME: list aggr serialize"); diff --git a/src/core_functions/scalar/list/list_lambdas.cpp b/src/core_functions/scalar/list/list_lambdas.cpp index 169c7f4e62ab..f66a5b2dca2c 100644 --- a/src/core_functions/scalar/list/list_lambdas.cpp +++ b/src/core_functions/scalar/list/list_lambdas.cpp @@ -40,7 +40,7 @@ unique_ptr ListLambdaBindData::Copy() const { bool ListLambdaBindData::Equals(const FunctionData &other_p) const { auto &other = other_p.Cast(); - return lambda_expr->Equals(other.lambda_expr.get()) && stype == other.stype; + return lambda_expr->Equals(*other.lambda_expr) && stype == other.stype; } ListLambdaBindData::~ListLambdaBindData() { diff --git a/src/execution/operator/aggregate/distinct_aggregate_data.cpp b/src/execution/operator/aggregate/distinct_aggregate_data.cpp index 526ae29138db..cd76deee6482 100644 --- a/src/execution/operator/aggregate/distinct_aggregate_data.cpp +++ b/src/execution/operator/aggregate/distinct_aggregate_data.cpp @@ -124,7 +124,7 @@ struct FindMatchingAggregate { if (other.children.size() != aggr.children.size()) { return false; } - if (!Expression::Equals(aggr.filter.get(), other.filter.get())) { + if (!Expression::Equals(aggr.filter, other.filter)) { return false; } for (idx_t i = 0; i < aggr.children.size(); i++) { diff --git a/src/function/aggregate/sorted_aggregate_function.cpp b/src/function/aggregate/sorted_aggregate_function.cpp index 9bc0e8df04f9..f43dbdbf930c 100644 --- a/src/function/aggregate/sorted_aggregate_function.cpp +++ b/src/function/aggregate/sorted_aggregate_function.cpp @@ -28,7 +28,7 @@ struct SortedAggregateBindData : public FunctionData { } sorted_on_args = (children.size() == order_bys.orders.size()); for (size_t i = 0; sorted_on_args && i < children.size(); ++i) { - sorted_on_args = children[i]->Equals(order_bys.orders[i].expression.get()); + sorted_on_args = children[i]->Equals(*order_bys.orders[i].expression); } } diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index 8e6e00dfe27b..5e3a755f0393 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -23,7 +23,7 @@ struct ExportAggregateBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const ExportAggregateBindData &)other_p; + auto &other = other_p.Cast(); return aggr == other.aggr && state_size == other.state_size; } @@ -49,7 +49,7 @@ struct CombineState : public FunctionLocalState { static unique_ptr InitCombineState(ExpressionState &state, const BoundFunctionExpression &expr, FunctionData *bind_data_p) { - auto &bind_data = *(ExportAggregateBindData *)bind_data_p; + auto &bind_data = bind_data_p->Cast(); return make_uniq(bind_data.state_size); } @@ -67,13 +67,13 @@ struct FinalizeState : public FunctionLocalState { static unique_ptr InitFinalizeState(ExpressionState &state, const BoundFunctionExpression &expr, FunctionData *bind_data_p) { - auto &bind_data = *(ExportAggregateBindData *)bind_data_p; + auto &bind_data = bind_data_p->Cast(); return make_uniq(bind_data.state_size); } static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, Vector &result) { auto &bind_data = ExportAggregateBindData::GetFrom(state_p); - auto &local_state = (FinalizeState &)*((ExecuteFunctionState &)state_p).local_state; + auto &local_state = ExecuteFunctionState::GetFunctionState(state_p)->Cast(); D_ASSERT(bind_data.state_size == bind_data.aggr.state_size()); D_ASSERT(input.data.size() == 1); @@ -113,7 +113,7 @@ static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, V static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Vector &result) { auto &bind_data = ExportAggregateBindData::GetFrom(state_p); - auto &local_state = (CombineState &)*((ExecuteFunctionState &)state_p).local_state; + auto &local_state = ExecuteFunctionState::GetFunctionState(state_p)->Cast(); D_ASSERT(bind_data.state_size == bind_data.aggr.state_size()); @@ -244,8 +244,8 @@ static unique_ptr BindAggregateState(ClientContext &context, Scala static void ExportAggregateFinalize(Vector &state, AggregateInputData &aggr_input_data, Vector &result, idx_t count, idx_t offset) { D_ASSERT(offset == 0); - auto bind_data = (ExportAggregateFunctionBindData *)aggr_input_data.bind_data; - auto state_size = bind_data->aggregate->function.state_size(); + auto &bind_data = aggr_input_data.bind_data->Cast(); + auto state_size = bind_data.aggregate->function.state_size(); auto blob_ptr = FlatVector::GetData(result); auto addresses_ptr = FlatVector::GetData(state); for (idx_t row_idx = 0; row_idx < count; row_idx++) { @@ -256,7 +256,7 @@ static void ExportAggregateFinalize(Vector &state, AggregateInputData &aggr_inpu ExportAggregateFunctionBindData::ExportAggregateFunctionBindData(unique_ptr aggregate_p) { D_ASSERT(aggregate_p->type == ExpressionType::BOUND_AGGREGATE); - aggregate = unique_ptr((BoundAggregateExpression *)aggregate_p.release()); + aggregate = unique_ptr_cast(std::move(aggregate_p)); } unique_ptr ExportAggregateFunctionBindData::Copy() const { @@ -264,8 +264,8 @@ unique_ptr ExportAggregateFunctionBindData::Copy() const { } bool ExportAggregateFunctionBindData::Equals(const FunctionData &other_p) const { - auto &other = (const ExportAggregateFunctionBindData &)other_p; - return aggregate->Equals(other.aggregate.get()); + auto &other = other_p.Cast(); + return aggregate->Equals(*other.aggregate); } static void ExportStateAggregateSerialize(FieldWriter &writer, const FunctionData *bind_data_p, diff --git a/src/function/table/table_scan.cpp b/src/function/table/table_scan.cpp index 8b449ca3db5f..ab8a5d04e60f 100644 --- a/src/function/table/table_scan.cpp +++ b/src/function/table/table_scan.cpp @@ -350,7 +350,7 @@ void TableScanPushdownComplexFilter(ClientContext &context, LogicalGet &get, Fun } else if (expr.type == ExpressionType::COMPARE_BETWEEN) { // BETWEEN expression auto &between = expr.Cast(); - if (!between.input->Equals(index_expression.get())) { + if (!between.input->Equals(*index_expression)) { // expression doesn't match the current index expression continue; } diff --git a/src/include/duckdb/common/typedefs.hpp b/src/include/duckdb/common/typedefs.hpp index a100d8cae16a..806b53c8589b 100644 --- a/src/include/duckdb/common/typedefs.hpp +++ b/src/include/duckdb/common/typedefs.hpp @@ -28,4 +28,16 @@ typedef idx_t column_t; //! Type used for storage (column) identifiers typedef idx_t storage_t; +template +DEST *data_ptr_cast(SRC *src) { + static_assert(sizeof(DEST) == 1, "data_ptr_cast should only be used to cast to char, data_t, unsigned char"); + return (DEST *)src; +} + +template +const DEST *const_data_ptr_cast(SRC *src) { + static_assert(sizeof(DEST) == 1, "data_ptr_cast should only be used to cast to char, data_t, unsigned char"); + return (DEST *)src; +} + } // namespace duckdb diff --git a/src/include/duckdb/execution/expression_executor_state.hpp b/src/include/duckdb/execution/expression_executor_state.hpp index d8c3432d789d..db4d229bded1 100644 --- a/src/include/duckdb/execution/expression_executor_state.hpp +++ b/src/include/duckdb/execution/expression_executor_state.hpp @@ -39,6 +39,18 @@ struct ExpressionState { DUCKDB_API ClientContext &GetContext(); void Verify(ExpressionExecutorState &root); + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct ExecuteFunctionState : public ExpressionState { @@ -48,8 +60,8 @@ struct ExecuteFunctionState : public ExpressionState { unique_ptr local_state; public: - static FunctionLocalState *GetFunctionState(ExpressionState &state) { - return ((ExecuteFunctionState &)state).local_state.get(); + static optional_ptr GetFunctionState(ExpressionState &state) { + return state.Cast().local_state.get(); } }; diff --git a/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp b/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp index b23a5bf64311..3e9673c60b65 100644 --- a/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp +++ b/src/include/duckdb/optimizer/join_order/cardinality_estimator.hpp @@ -111,9 +111,9 @@ class CardinalityEstimator { void AddRelationTdom(FilterInfo &filter_info); bool EmptyFilter(FilterInfo &filter_info); - idx_t InspectConjunctionAND(idx_t cardinality, idx_t column_index, ConjunctionAndFilter *fil, + idx_t InspectConjunctionAND(idx_t cardinality, idx_t column_index, ConjunctionAndFilter &fil, unique_ptr base_stats); - idx_t InspectConjunctionOR(idx_t cardinality, idx_t column_index, ConjunctionOrFilter *fil, + idx_t InspectConjunctionOR(idx_t cardinality, idx_t column_index, ConjunctionOrFilter &fil, unique_ptr base_stats); idx_t InspectTableFilters(idx_t cardinality, LogicalOperator &op, TableFilterSet &table_filters, idx_t table_index); }; diff --git a/src/include/duckdb/parser/base_expression.hpp b/src/include/duckdb/parser/base_expression.hpp index f65b3006909c..b60bb12ed656 100644 --- a/src/include/duckdb/parser/base_expression.hpp +++ b/src/include/duckdb/parser/base_expression.hpp @@ -71,19 +71,13 @@ class BaseExpression { //! Expression::Equals() returns true), that their hash value is identical as well. virtual hash_t Hash() const = 0; //! Returns true if this expression is equal to another expression - virtual bool Equals(const BaseExpression *other) const; + virtual bool Equals(const BaseExpression &other) const; - static bool Equals(const BaseExpression *left, const BaseExpression *right) { - if (left == right) { - return true; - } - if (!left || !right) { - return false; - } - return left->Equals(right); + static bool Equals(const BaseExpression &left, const BaseExpression &right) { + return left.Equals(right); } bool operator==(const BaseExpression &rhs) { - return this->Equals(&rhs); + return Equals(rhs); } virtual void Verify() const; diff --git a/src/include/duckdb/parser/expression/between_expression.hpp b/src/include/duckdb/parser/expression/between_expression.hpp index f4c505441c5c..d670395cb6bc 100644 --- a/src/include/duckdb/parser/expression/between_expression.hpp +++ b/src/include/duckdb/parser/expression/between_expression.hpp @@ -27,7 +27,7 @@ class BetweenExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const BetweenExpression *a, const BetweenExpression *b); + static bool Equal(const BetweenExpression &a, const BetweenExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/bound_expression.hpp b/src/include/duckdb/parser/expression/bound_expression.hpp index 0f86105980d4..5812ad94b3a9 100644 --- a/src/include/duckdb/parser/expression/bound_expression.hpp +++ b/src/include/duckdb/parser/expression/bound_expression.hpp @@ -32,7 +32,7 @@ class BoundExpression : public ParsedExpression { string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; hash_t Hash() const override; unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/case_expression.hpp b/src/include/duckdb/parser/expression/case_expression.hpp index 32c1c23b7816..910ae1cb868d 100644 --- a/src/include/duckdb/parser/expression/case_expression.hpp +++ b/src/include/duckdb/parser/expression/case_expression.hpp @@ -35,7 +35,7 @@ class CaseExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const CaseExpression *a, const CaseExpression *b); + static bool Equal(const CaseExpression &a, const CaseExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/cast_expression.hpp b/src/include/duckdb/parser/expression/cast_expression.hpp index 7646caa9d4d0..37353e793210 100644 --- a/src/include/duckdb/parser/expression/cast_expression.hpp +++ b/src/include/duckdb/parser/expression/cast_expression.hpp @@ -31,7 +31,7 @@ class CastExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const CastExpression *a, const CastExpression *b); + static bool Equal(const CastExpression &a, const CastExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/collate_expression.hpp b/src/include/duckdb/parser/expression/collate_expression.hpp index ac65e55654c4..0194e7e761a3 100644 --- a/src/include/duckdb/parser/expression/collate_expression.hpp +++ b/src/include/duckdb/parser/expression/collate_expression.hpp @@ -28,7 +28,7 @@ class CollateExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const CollateExpression *a, const CollateExpression *b); + static bool Equal(const CollateExpression &a, const CollateExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/columnref_expression.hpp b/src/include/duckdb/parser/expression/columnref_expression.hpp index b7716cc916af..e6c8975d9564 100644 --- a/src/include/duckdb/parser/expression/columnref_expression.hpp +++ b/src/include/duckdb/parser/expression/columnref_expression.hpp @@ -41,7 +41,7 @@ class ColumnRefExpression : public ParsedExpression { string GetName() const override; string ToString() const override; - static bool Equal(const ColumnRefExpression *a, const ColumnRefExpression *b); + static bool Equal(const ColumnRefExpression &a, const ColumnRefExpression &b); hash_t Hash() const override; unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/comparison_expression.hpp b/src/include/duckdb/parser/expression/comparison_expression.hpp index 98fb79a3245c..513e81f9c21d 100644 --- a/src/include/duckdb/parser/expression/comparison_expression.hpp +++ b/src/include/duckdb/parser/expression/comparison_expression.hpp @@ -27,7 +27,7 @@ class ComparisonExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const ComparisonExpression *a, const ComparisonExpression *b); + static bool Equal(const ComparisonExpression &a, const ComparisonExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/conjunction_expression.hpp b/src/include/duckdb/parser/expression/conjunction_expression.hpp index 4cb3c1d6361f..1dbaf30e0612 100644 --- a/src/include/duckdb/parser/expression/conjunction_expression.hpp +++ b/src/include/duckdb/parser/expression/conjunction_expression.hpp @@ -31,7 +31,7 @@ class ConjunctionExpression : public ParsedExpression { string ToString() const override; - static bool Equal(const ConjunctionExpression *a, const ConjunctionExpression *b); + static bool Equal(const ConjunctionExpression &a, const ConjunctionExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/constant_expression.hpp b/src/include/duckdb/parser/expression/constant_expression.hpp index be5e85bd7c92..57ef818386df 100644 --- a/src/include/duckdb/parser/expression/constant_expression.hpp +++ b/src/include/duckdb/parser/expression/constant_expression.hpp @@ -27,7 +27,7 @@ class ConstantExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const ConstantExpression *a, const ConstantExpression *b); + static bool Equal(const ConstantExpression &a, const ConstantExpression &b); hash_t Hash() const override; unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/function_expression.hpp b/src/include/duckdb/parser/expression/function_expression.hpp index 0c15bbc13acc..f27003d81c5e 100644 --- a/src/include/duckdb/parser/expression/function_expression.hpp +++ b/src/include/duckdb/parser/expression/function_expression.hpp @@ -53,7 +53,7 @@ class FunctionExpression : public ParsedExpression { unique_ptr Copy() const override; - static bool Equal(const FunctionExpression *a, const FunctionExpression *b); + static bool Equal(const FunctionExpression &a, const FunctionExpression &b); hash_t Hash() const override; void Serialize(FieldWriter &writer) const override; diff --git a/src/include/duckdb/parser/expression/lambda_expression.hpp b/src/include/duckdb/parser/expression/lambda_expression.hpp index bc4978ac6b75..ee89570567de 100644 --- a/src/include/duckdb/parser/expression/lambda_expression.hpp +++ b/src/include/duckdb/parser/expression/lambda_expression.hpp @@ -33,7 +33,7 @@ class LambdaExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const LambdaExpression *a, const LambdaExpression *b); + static bool Equal(const LambdaExpression &a, const LambdaExpression &b); hash_t Hash() const override; unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/operator_expression.hpp b/src/include/duckdb/parser/expression/operator_expression.hpp index 91967875716f..4d0180151561 100644 --- a/src/include/duckdb/parser/expression/operator_expression.hpp +++ b/src/include/duckdb/parser/expression/operator_expression.hpp @@ -29,7 +29,7 @@ class OperatorExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const OperatorExpression *a, const OperatorExpression *b); + static bool Equal(const OperatorExpression &a, const OperatorExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/parameter_expression.hpp b/src/include/duckdb/parser/expression/parameter_expression.hpp index 24c3d2316626..f9672531b2ba 100644 --- a/src/include/duckdb/parser/expression/parameter_expression.hpp +++ b/src/include/duckdb/parser/expression/parameter_expression.hpp @@ -30,7 +30,7 @@ class ParameterExpression : public ParsedExpression { string ToString() const override; - static bool Equal(const ParameterExpression *a, const ParameterExpression *b); + static bool Equal(const ParameterExpression &a, const ParameterExpression &b); unique_ptr Copy() const override; hash_t Hash() const override; diff --git a/src/include/duckdb/parser/expression/positional_reference_expression.hpp b/src/include/duckdb/parser/expression/positional_reference_expression.hpp index 4a97c64b7751..8f5decdb6137 100644 --- a/src/include/duckdb/parser/expression/positional_reference_expression.hpp +++ b/src/include/duckdb/parser/expression/positional_reference_expression.hpp @@ -27,7 +27,7 @@ class PositionalReferenceExpression : public ParsedExpression { string ToString() const override; - static bool Equal(const PositionalReferenceExpression *a, const PositionalReferenceExpression *b); + static bool Equal(const PositionalReferenceExpression &a, const PositionalReferenceExpression &b); unique_ptr Copy() const override; hash_t Hash() const override; diff --git a/src/include/duckdb/parser/expression/star_expression.hpp b/src/include/duckdb/parser/expression/star_expression.hpp index 5aa48e1b152b..c93a3ba5bb3b 100644 --- a/src/include/duckdb/parser/expression/star_expression.hpp +++ b/src/include/duckdb/parser/expression/star_expression.hpp @@ -35,7 +35,7 @@ class StarExpression : public ParsedExpression { public: string ToString() const override; - static bool Equal(const StarExpression *a, const StarExpression *b); + static bool Equal(const StarExpression &a, const StarExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/subquery_expression.hpp b/src/include/duckdb/parser/expression/subquery_expression.hpp index f060143946a0..31904f7c6339 100644 --- a/src/include/duckdb/parser/expression/subquery_expression.hpp +++ b/src/include/duckdb/parser/expression/subquery_expression.hpp @@ -42,7 +42,7 @@ class SubqueryExpression : public ParsedExpression { string ToString() const override; - static bool Equal(const SubqueryExpression *a, const SubqueryExpression *b); + static bool Equal(const SubqueryExpression &a, const SubqueryExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression/window_expression.hpp b/src/include/duckdb/parser/expression/window_expression.hpp index 56ebdda8dfa6..c575c5880a9d 100644 --- a/src/include/duckdb/parser/expression/window_expression.hpp +++ b/src/include/duckdb/parser/expression/window_expression.hpp @@ -70,7 +70,7 @@ class WindowExpression : public ParsedExpression { //! Convert the Expression to a String string ToString() const override; - static bool Equal(const WindowExpression *a, const WindowExpression *b); + static bool Equal(const WindowExpression &a, const WindowExpression &b); unique_ptr Copy() const override; diff --git a/src/include/duckdb/parser/expression_map.hpp b/src/include/duckdb/parser/expression_map.hpp index 0a53d3cb3567..75ecd78e8b41 100644 --- a/src/include/duckdb/parser/expression_map.hpp +++ b/src/include/duckdb/parser/expression_map.hpp @@ -27,7 +27,7 @@ struct ExpressionHashFunction { template struct ExpressionEquality { bool operator()(const reference &a, const reference &b) const { - return a.get().Equals(&b.get()); + return a.get().Equals(b.get()); } }; diff --git a/src/include/duckdb/parser/parsed_expression.hpp b/src/include/duckdb/parser/parsed_expression.hpp index f761661e7c6a..939f7d8a46ae 100644 --- a/src/include/duckdb/parser/parsed_expression.hpp +++ b/src/include/duckdb/parser/parsed_expression.hpp @@ -47,7 +47,7 @@ class ParsedExpression : public BaseExpression { bool IsScalar() const override; bool HasParameter() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; hash_t Hash() const override; //! Create a copy of this expression @@ -65,6 +65,10 @@ class ParsedExpression : public BaseExpression { virtual void FormatSerialize(FormatSerializer &serializer) const; static unique_ptr FormatDeserialize(FormatDeserializer &deserializer); + static bool Equals(const unique_ptr &left, const unique_ptr &right); + static bool ListEquals(const vector> &left, + const vector> &right); + protected: //! Copy base Expression properties from another expression to this one, //! used in Copy method diff --git a/src/include/duckdb/parser/result_modifier.hpp b/src/include/duckdb/parser/result_modifier.hpp index 246b9eb780e7..8ecd7bfa2d28 100644 --- a/src/include/duckdb/parser/result_modifier.hpp +++ b/src/include/duckdb/parser/result_modifier.hpp @@ -41,7 +41,7 @@ class ResultModifier { public: //! Returns true if the two result modifiers are equivalent - virtual bool Equals(const ResultModifier *other) const; + virtual bool Equals(const ResultModifier &other) const; //! Create a copy of this ResultModifier virtual unique_ptr Copy() const = 0; @@ -54,6 +54,23 @@ class ResultModifier { virtual void FormatSerialize(FormatSerializer &serializer) const; static unique_ptr FormatDeserialize(FormatDeserializer &deserializer); + +public: + template + TARGET &Cast() { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); + } + return (TARGET &)*this; + } + + template + const TARGET &Cast() const { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); + } + return (const TARGET &)*this; + } }; //! Single node in ORDER BY statement @@ -79,6 +96,9 @@ struct OrderByNode { }; class LimitModifier : public ResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::LIMIT_MODIFIER; + public: LimitModifier() : ResultModifier(ResultModifierType::LIMIT_MODIFIER) { } @@ -89,7 +109,7 @@ class LimitModifier : public ResultModifier { unique_ptr offset; public: - bool Equals(const ResultModifier *other) const override; + bool Equals(const ResultModifier &other) const override; unique_ptr Copy() const override; void Serialize(FieldWriter &writer) const override; static unique_ptr Deserialize(FieldReader &reader); @@ -99,6 +119,9 @@ class LimitModifier : public ResultModifier { }; class OrderModifier : public ResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::ORDER_MODIFIER; + public: OrderModifier() : ResultModifier(ResultModifierType::ORDER_MODIFIER) { } @@ -107,16 +130,21 @@ class OrderModifier : public ResultModifier { vector orders; public: - bool Equals(const ResultModifier *other) const override; + bool Equals(const ResultModifier &other) const override; unique_ptr Copy() const override; void Serialize(FieldWriter &writer) const override; static unique_ptr Deserialize(FieldReader &reader); void FormatSerialize(FormatSerializer &serializer) const override; static unique_ptr FormatDeserialize(FormatDeserializer &deserializer); + + static bool Equals(const unique_ptr &left, const unique_ptr &right); }; class DistinctModifier : public ResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::DISTINCT_MODIFIER; + public: DistinctModifier() : ResultModifier(ResultModifierType::DISTINCT_MODIFIER) { } @@ -125,7 +153,7 @@ class DistinctModifier : public ResultModifier { vector> distinct_on_targets; public: - bool Equals(const ResultModifier *other) const override; + bool Equals(const ResultModifier &other) const override; unique_ptr Copy() const override; void Serialize(FieldWriter &writer) const override; static unique_ptr Deserialize(FieldReader &reader); @@ -135,6 +163,9 @@ class DistinctModifier : public ResultModifier { }; class LimitPercentModifier : public ResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::LIMIT_PERCENT_MODIFIER; + public: LimitPercentModifier() : ResultModifier(ResultModifierType::LIMIT_PERCENT_MODIFIER) { } @@ -145,7 +176,7 @@ class LimitPercentModifier : public ResultModifier { unique_ptr offset; public: - bool Equals(const ResultModifier *other) const override; + bool Equals(const ResultModifier &other) const override; unique_ptr Copy() const override; void Serialize(FieldWriter &writer) const override; static unique_ptr Deserialize(FieldReader &reader); diff --git a/src/include/duckdb/parser/statement/select_statement.hpp b/src/include/duckdb/parser/statement/select_statement.hpp index 2a994fb7320e..9e3efb66478c 100644 --- a/src/include/duckdb/parser/statement/select_statement.hpp +++ b/src/include/duckdb/parser/statement/select_statement.hpp @@ -46,7 +46,7 @@ class SelectStatement : public SQLStatement { //! deserialization is not possible static unique_ptr Deserialize(Deserializer &source); //! Whether or not the statements are equivalent - bool Equals(const SQLStatement *other) const; + bool Equals(const SQLStatement &other) const; void FormatSerialize(FormatSerializer &serializer) const; static unique_ptr FormatDeserialize(FormatDeserializer &deserializer); diff --git a/src/include/duckdb/parser/tableref.hpp b/src/include/duckdb/parser/tableref.hpp index 2718f874cbf8..147a1823e70b 100644 --- a/src/include/duckdb/parser/tableref.hpp +++ b/src/include/duckdb/parser/tableref.hpp @@ -41,7 +41,8 @@ class TableRef { string BaseToString(string result, const vector &column_name_alias) const; void Print(); - virtual bool Equals(const TableRef *other) const; + virtual bool Equals(const TableRef &other) const; + static bool Equals(const unique_ptr &left, const unique_ptr &right); virtual unique_ptr Copy() = 0; diff --git a/src/include/duckdb/parser/tableref/basetableref.hpp b/src/include/duckdb/parser/tableref/basetableref.hpp index 72ea3d19d66c..dd47845837e8 100644 --- a/src/include/duckdb/parser/tableref/basetableref.hpp +++ b/src/include/duckdb/parser/tableref/basetableref.hpp @@ -33,7 +33,7 @@ class BaseTableRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/parser/tableref/emptytableref.hpp b/src/include/duckdb/parser/tableref/emptytableref.hpp index c0efb2dfbe5f..cb495f69fdac 100644 --- a/src/include/duckdb/parser/tableref/emptytableref.hpp +++ b/src/include/duckdb/parser/tableref/emptytableref.hpp @@ -22,7 +22,7 @@ class EmptyTableRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/parser/tableref/expressionlistref.hpp b/src/include/duckdb/parser/tableref/expressionlistref.hpp index 4a4198477f15..8c6581faf414 100644 --- a/src/include/duckdb/parser/tableref/expressionlistref.hpp +++ b/src/include/duckdb/parser/tableref/expressionlistref.hpp @@ -32,7 +32,7 @@ class ExpressionListRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/parser/tableref/joinref.hpp b/src/include/duckdb/parser/tableref/joinref.hpp index cf51bbc9e129..d1fd6739a2e9 100644 --- a/src/include/duckdb/parser/tableref/joinref.hpp +++ b/src/include/duckdb/parser/tableref/joinref.hpp @@ -42,7 +42,7 @@ class JoinRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/parser/tableref/pivotref.hpp b/src/include/duckdb/parser/tableref/pivotref.hpp index edfe50ffb663..aa183ac4fd7e 100644 --- a/src/include/duckdb/parser/tableref/pivotref.hpp +++ b/src/include/duckdb/parser/tableref/pivotref.hpp @@ -89,7 +89,7 @@ class PivotRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/parser/tableref/subqueryref.hpp b/src/include/duckdb/parser/tableref/subqueryref.hpp index 0251e33694f2..a5ba12525704 100644 --- a/src/include/duckdb/parser/tableref/subqueryref.hpp +++ b/src/include/duckdb/parser/tableref/subqueryref.hpp @@ -27,7 +27,7 @@ class SubqueryRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/parser/tableref/table_function_ref.hpp b/src/include/duckdb/parser/tableref/table_function_ref.hpp index f236648d1cbc..6f4847e000f4 100644 --- a/src/include/duckdb/parser/tableref/table_function_ref.hpp +++ b/src/include/duckdb/parser/tableref/table_function_ref.hpp @@ -35,7 +35,7 @@ class TableFunctionRef : public TableRef { public: string ToString() const override; - bool Equals(const TableRef *other_p) const override; + bool Equals(const TableRef &other_p) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/bind_context.hpp b/src/include/duckdb/planner/bind_context.hpp index 4068466aca09..41101f2f366f 100644 --- a/src/include/duckdb/planner/bind_context.hpp +++ b/src/include/duckdb/planner/bind_context.hpp @@ -96,7 +96,7 @@ class BindContext { void AddSubquery(idx_t index, const string &alias, TableFunctionRef &ref, BoundQueryNode &subquery); //! Adds a binding to a catalog entry with a given alias to the BindContext. void AddEntryBinding(idx_t index, const string &alias, const vector &names, - const vector &types, StandardEntry *entry); + const vector &types, StandardEntry &entry); //! Adds a base table with the given alias to the BindContext. void AddGenericBinding(idx_t index, const string &alias, const vector &names, const vector &types); diff --git a/src/include/duckdb/planner/bound_result_modifier.hpp b/src/include/duckdb/planner/bound_result_modifier.hpp index a2628a3dd2d7..e92e4b20beaf 100644 --- a/src/include/duckdb/planner/bound_result_modifier.hpp +++ b/src/include/duckdb/planner/bound_result_modifier.hpp @@ -23,9 +23,29 @@ class BoundResultModifier { virtual ~BoundResultModifier(); ResultModifierType type; + +public: + template + TARGET &Cast() { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); + } + return (TARGET &)*this; + } + + template + const TARGET &Cast() const { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); + } + return (const TARGET &)*this; + } }; struct BoundOrderByNode { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::ORDER_MODIFIER; + public: BoundOrderByNode(OrderType type, OrderByNullType null_order, unique_ptr expression); BoundOrderByNode(OrderType type, OrderByNullType null_order, unique_ptr expression, @@ -46,6 +66,9 @@ struct BoundOrderByNode { }; class BoundLimitModifier : public BoundResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::LIMIT_MODIFIER; + public: BoundLimitModifier(); @@ -60,6 +83,9 @@ class BoundLimitModifier : public BoundResultModifier { }; class BoundOrderModifier : public BoundResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::ORDER_MODIFIER; + public: BoundOrderModifier(); @@ -67,12 +93,16 @@ class BoundOrderModifier : public BoundResultModifier { vector orders; unique_ptr Copy() const; - static bool Equals(const BoundOrderModifier *left, const BoundOrderModifier *right); + static bool Equals(const BoundOrderModifier &left, const BoundOrderModifier &right); + static bool Equals(const unique_ptr &left, const unique_ptr &right); }; enum class DistinctType : uint8_t { DISTINCT = 0, DISTINCT_ON = 1 }; class BoundDistinctModifier : public BoundResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::DISTINCT_MODIFIER; + public: BoundDistinctModifier(); @@ -83,6 +113,9 @@ class BoundDistinctModifier : public BoundResultModifier { }; class BoundLimitPercentModifier : public BoundResultModifier { +public: + static constexpr const ResultModifierType TYPE = ResultModifierType::LIMIT_PERCENT_MODIFIER; + public: BoundLimitPercentModifier(); diff --git a/src/include/duckdb/planner/expression.hpp b/src/include/duckdb/planner/expression.hpp index b6eaf0c3268d..57b61a099680 100644 --- a/src/include/duckdb/planner/expression.hpp +++ b/src/include/duckdb/planner/expression.hpp @@ -41,19 +41,17 @@ class Expression : public BaseExpression { hash_t Hash() const override; - bool Equals(const BaseExpression *other) const override { + bool Equals(const BaseExpression &other) const override { if (!BaseExpression::Equals(other)) { return false; } - return return_type == ((Expression *)other)->return_type; - } - - static bool Equals(const Expression *left, const Expression *right) { - return BaseExpression::Equals((const BaseExpression *)left, (const BaseExpression *)right); + return return_type == ((Expression &)other).return_type; } static bool Equals(const Expression &left, const Expression &right) { - return left.Equals(&right); + return left.Equals(right); } + static bool Equals(const unique_ptr &left, const unique_ptr &right); + static bool ListEquals(const vector> &left, const vector> &right); //! Create a copy of this expression virtual unique_ptr Copy() = 0; diff --git a/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp b/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp index 5e9f6eb9b8f2..fbee39204467 100644 --- a/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_aggregate_expression.hpp @@ -53,7 +53,7 @@ class BoundAggregateExpression : public Expression { string ToString() const override; hash_t Hash() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; void Serialize(FieldWriter &writer) const override; static unique_ptr Deserialize(ExpressionDeserializationState &state, FieldReader &reader); diff --git a/src/include/duckdb/planner/expression/bound_between_expression.hpp b/src/include/duckdb/planner/expression/bound_between_expression.hpp index 88160b5b64e7..7c625947a3d9 100644 --- a/src/include/duckdb/planner/expression/bound_between_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_between_expression.hpp @@ -29,7 +29,7 @@ class BoundBetweenExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; void Serialize(FieldWriter &writer) const override; diff --git a/src/include/duckdb/planner/expression/bound_case_expression.hpp b/src/include/duckdb/planner/expression/bound_case_expression.hpp index 6f239b85a0ef..f425968aac37 100644 --- a/src/include/duckdb/planner/expression/bound_case_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_case_expression.hpp @@ -35,7 +35,7 @@ class BoundCaseExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_cast_expression.hpp b/src/include/duckdb/planner/expression/bound_cast_expression.hpp index 523230008623..d1416e2caace 100644 --- a/src/include/duckdb/planner/expression/bound_cast_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_cast_expression.hpp @@ -46,7 +46,7 @@ class BoundCastExpression : public Expression { string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_columnref_expression.hpp b/src/include/duckdb/planner/expression/bound_columnref_expression.hpp index adf8ff357e7a..5bfd7e1e10ec 100644 --- a/src/include/duckdb/planner/expression/bound_columnref_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_columnref_expression.hpp @@ -44,7 +44,7 @@ class BoundColumnRefExpression : public Expression { string ToString() const override; string GetName() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; hash_t Hash() const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_comparison_expression.hpp b/src/include/duckdb/planner/expression/bound_comparison_expression.hpp index 2324b39491ee..3c12e2ce3146 100644 --- a/src/include/duckdb/planner/expression/bound_comparison_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_comparison_expression.hpp @@ -25,7 +25,7 @@ class BoundComparisonExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; void Serialize(FieldWriter &writer) const override; diff --git a/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp b/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp index 36cff0422028..6438900fc48f 100644 --- a/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_conjunction_expression.hpp @@ -25,7 +25,7 @@ class BoundConjunctionExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; bool PropagatesNullValues() const override; diff --git a/src/include/duckdb/planner/expression/bound_constant_expression.hpp b/src/include/duckdb/planner/expression/bound_constant_expression.hpp index f30ec6326c24..e2a76f38c2cd 100644 --- a/src/include/duckdb/planner/expression/bound_constant_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_constant_expression.hpp @@ -25,7 +25,7 @@ class BoundConstantExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; hash_t Hash() const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_function_expression.hpp b/src/include/duckdb/planner/expression/bound_function_expression.hpp index d3e278c9ac3e..50491e828f5c 100644 --- a/src/include/duckdb/planner/expression/bound_function_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_function_expression.hpp @@ -39,7 +39,7 @@ class BoundFunctionExpression : public Expression { string ToString() const override; bool PropagatesNullValues() const override; hash_t Hash() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; void Verify() const override; diff --git a/src/include/duckdb/planner/expression/bound_lambda_expression.hpp b/src/include/duckdb/planner/expression/bound_lambda_expression.hpp index 82268b12100b..ae5623a5ec15 100644 --- a/src/include/duckdb/planner/expression/bound_lambda_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_lambda_expression.hpp @@ -28,7 +28,7 @@ class BoundLambdaExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp b/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp index 4441779ce49a..d0a8897ce9d4 100644 --- a/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_lambdaref_expression.hpp @@ -46,7 +46,7 @@ class BoundLambdaRefExpression : public Expression { string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; hash_t Hash() const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_operator_expression.hpp b/src/include/duckdb/planner/expression/bound_operator_expression.hpp index ca97f0026ab6..768932b9d2a5 100644 --- a/src/include/duckdb/planner/expression/bound_operator_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_operator_expression.hpp @@ -24,7 +24,7 @@ class BoundOperatorExpression : public Expression { public: string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_parameter_expression.hpp b/src/include/duckdb/planner/expression/bound_parameter_expression.hpp index 6a0c5d70175b..72712b4461cb 100644 --- a/src/include/duckdb/planner/expression/bound_parameter_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_parameter_expression.hpp @@ -35,7 +35,7 @@ class BoundParameterExpression : public Expression { string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; hash_t Hash() const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_reference_expression.hpp b/src/include/duckdb/planner/expression/bound_reference_expression.hpp index 8dff2e517010..102522d3de97 100644 --- a/src/include/duckdb/planner/expression/bound_reference_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_reference_expression.hpp @@ -35,7 +35,7 @@ class BoundReferenceExpression : public Expression { string ToString() const override; hash_t Hash() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_subquery_expression.hpp b/src/include/duckdb/planner/expression/bound_subquery_expression.hpp index aa9ab05f8589..c74eb732c8ec 100644 --- a/src/include/duckdb/planner/expression/bound_subquery_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_subquery_expression.hpp @@ -55,7 +55,7 @@ class BoundSubqueryExpression : public Expression { string ToString() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_unnest_expression.hpp b/src/include/duckdb/planner/expression/bound_unnest_expression.hpp index a142c25e1e6a..c305840cc212 100644 --- a/src/include/duckdb/planner/expression/bound_unnest_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_unnest_expression.hpp @@ -27,7 +27,7 @@ class BoundUnnestExpression : public Expression { string ToString() const override; hash_t Hash() const override; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/expression/bound_window_expression.hpp b/src/include/duckdb/planner/expression/bound_window_expression.hpp index 0a485b0348ab..e0cc1f5befbb 100644 --- a/src/include/duckdb/planner/expression/bound_window_expression.hpp +++ b/src/include/duckdb/planner/expression/bound_window_expression.hpp @@ -61,7 +61,7 @@ class BoundWindowExpression : public Expression { string ToString() const override; bool KeysAreCompatible(const BoundWindowExpression &other) const; - bool Equals(const BaseExpression *other) const override; + bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; diff --git a/src/include/duckdb/planner/filter/conjunction_filter.hpp b/src/include/duckdb/planner/filter/conjunction_filter.hpp index d9dbeddac2db..4d58adc483d6 100644 --- a/src/include/duckdb/planner/filter/conjunction_filter.hpp +++ b/src/include/duckdb/planner/filter/conjunction_filter.hpp @@ -33,6 +33,9 @@ class ConjunctionFilter : public TableFilter { }; class ConjunctionOrFilter : public ConjunctionFilter { +public: + static constexpr const TableFilterType TYPE = TableFilterType::CONJUNCTION_OR; + public: ConjunctionOrFilter(); @@ -45,6 +48,9 @@ class ConjunctionOrFilter : public ConjunctionFilter { }; class ConjunctionAndFilter : public ConjunctionFilter { +public: + static constexpr const TableFilterType TYPE = TableFilterType::CONJUNCTION_AND; + public: ConjunctionAndFilter(); diff --git a/src/include/duckdb/planner/filter/constant_filter.hpp b/src/include/duckdb/planner/filter/constant_filter.hpp index 3d23739538dd..7da12c9fc0c6 100644 --- a/src/include/duckdb/planner/filter/constant_filter.hpp +++ b/src/include/duckdb/planner/filter/constant_filter.hpp @@ -15,6 +15,9 @@ namespace duckdb { class ConstantFilter : public TableFilter { +public: + static constexpr const TableFilterType TYPE = TableFilterType::CONSTANT_COMPARISON; + public: ConstantFilter(ExpressionType comparison_type, Value constant); diff --git a/src/include/duckdb/planner/filter/null_filter.hpp b/src/include/duckdb/planner/filter/null_filter.hpp index 658d2422d615..695dc378629b 100644 --- a/src/include/duckdb/planner/filter/null_filter.hpp +++ b/src/include/duckdb/planner/filter/null_filter.hpp @@ -13,6 +13,9 @@ namespace duckdb { class IsNullFilter : public TableFilter { +public: + static constexpr const TableFilterType TYPE = TableFilterType::IS_NULL; + public: IsNullFilter(); @@ -24,6 +27,9 @@ class IsNullFilter : public TableFilter { }; class IsNotNullFilter : public TableFilter { +public: + static constexpr const TableFilterType TYPE = TableFilterType::IS_NOT_NULL; + public: IsNotNullFilter(); diff --git a/src/include/duckdb/planner/table_binding.hpp b/src/include/duckdb/planner/table_binding.hpp index 21478854b3b4..c04d731dea92 100644 --- a/src/include/duckdb/planner/table_binding.hpp +++ b/src/include/duckdb/planner/table_binding.hpp @@ -55,9 +55,29 @@ struct Binding { virtual string ColumnNotFoundError(const string &column_name) const; virtual BindResult Bind(ColumnRefExpression &colref, idx_t depth); virtual optional_ptr GetStandardEntry(); + +public: + template + TARGET &Cast() { + if (binding_type != TARGET::TYPE) { + throw InternalException("Failed to cast binding to type - binding type mismatch"); + } + return (TARGET &)*this; + } + + template + const TARGET &Cast() const { + if (binding_type != TARGET::TYPE) { + throw InternalException("Failed to cast binding to type - binding type mismatch"); + } + return (const TARGET &)*this; + } }; struct EntryBinding : public Binding { +public: + static constexpr const BindingType TYPE = BindingType::CATALOG_ENTRY; + public: EntryBinding(const string &alias, vector types, vector names, idx_t index, StandardEntry &entry); @@ -70,6 +90,10 @@ struct EntryBinding : public Binding { //! TableBinding is exactly like the Binding, except it keeps track of which columns were bound in the linked LogicalGet //! node for projection pushdown purposes. struct TableBinding : public Binding { +public: + static constexpr const BindingType TYPE = BindingType::TABLE; + +public: TableBinding(const string &alias, vector types, vector names, vector &bound_column_ids, optional_ptr entry, idx_t index, bool add_row_id = false); @@ -94,6 +118,8 @@ struct TableBinding : public Binding { //! DummyBinding is like the Binding, except the alias and index are set by default. Used for binding lambdas and macro //! parameters. struct DummyBinding : public Binding { +public: + static constexpr const BindingType TYPE = BindingType::DUMMY; // NOTE: changing this string conflicts with the storage version static constexpr const char *DUMMY_NAME = "0_macro_parameters"; diff --git a/src/include/duckdb/planner/table_filter.hpp b/src/include/duckdb/planner/table_filter.hpp index fc980f391d7a..25ed82ea661b 100644 --- a/src/include/duckdb/planner/table_filter.hpp +++ b/src/include/duckdb/planner/table_filter.hpp @@ -47,6 +47,23 @@ class TableFilter { void Serialize(Serializer &serializer) const; virtual void Serialize(FieldWriter &writer) const = 0; static unique_ptr Deserialize(Deserializer &source); + +public: + template + TARGET &Cast() { + if (filter_type != TARGET::TYPE) { + throw InternalException("Failed to cast table to type - table filter type mismatch"); + } + return (TARGET &)*this; + } + + template + const TARGET &Cast() const { + if (filter_type != TARGET::TYPE) { + throw InternalException("Failed to cast table to type - table filter type mismatch"); + } + return (const TARGET &)*this; + } }; class TableFilterSet { diff --git a/src/include/duckdb/planner/tableref/bound_pivotref.hpp b/src/include/duckdb/planner/tableref/bound_pivotref.hpp index d95c327a4f63..c670155fe5fb 100644 --- a/src/include/duckdb/planner/tableref/bound_pivotref.hpp +++ b/src/include/duckdb/planner/tableref/bound_pivotref.hpp @@ -28,6 +28,9 @@ struct BoundPivotInfo { }; class BoundPivotRef : public BoundTableRef { +public: + static constexpr const TableReferenceType TYPE = TableReferenceType::PIVOT; + public: explicit BoundPivotRef() : BoundTableRef(TableReferenceType::PIVOT) { } diff --git a/src/optimizer/deliminator.cpp b/src/optimizer/deliminator.cpp index 890e52265a82..eacd66869bb8 100644 --- a/src/optimizer/deliminator.cpp +++ b/src/optimizer/deliminator.cpp @@ -67,7 +67,7 @@ void DeliminatorPlanUpdater::VisitOperator(LogicalOperator &op) { VisitOperatorExpressions(op); if (op.type == LogicalOperatorType::LOGICAL_DELIM_JOIN && DelimGetCount(op) == 0) { auto &delim_join = op.Cast(); - auto decs = &delim_join.duplicate_eliminated_columns; + auto &decs = delim_join.duplicate_eliminated_columns; for (auto &cond : delim_join.conditions) { if (!IsEqualityJoinCondition(cond)) { continue; @@ -83,10 +83,10 @@ void DeliminatorPlanUpdater::VisitOperator(LogicalOperator &op) { auto &colref = rhs->Cast(); if (projection_map.find(colref.binding) != projection_map.end()) { // value on the right is a projection of removed DelimGet - for (idx_t i = 0; i < decs->size(); i++) { - if (decs->at(i)->Equals(cond.left.get())) { + for (idx_t i = 0; i < decs.size(); i++) { + if (decs[i]->Equals(*cond.left)) { // the value on the left no longer needs to be a duplicate-eliminated column - decs->erase(decs->begin() + i); + decs.erase(decs.begin() + i); break; } } @@ -95,7 +95,7 @@ void DeliminatorPlanUpdater::VisitOperator(LogicalOperator &op) { } } // change type if there are no more duplicate-eliminated columns - if (decs->empty()) { + if (decs.empty()) { delim_join.type = LogicalOperatorType::LOGICAL_COMPARISON_JOIN; // sub-plans with DelimGets are not re-orderable (yet), however, we removed all DelimGet of this DelimJoin // the DelimGets are on the RHS of the DelimJoin, so we can call the JoinOrderOptimizer on the RHS now @@ -198,9 +198,9 @@ bool Deliminator::RemoveCandidate(unique_ptr *plan, unique_ptr< idx_t delim_idx = OperatorIsDelimGet(*join.children[0]) ? 0 : 1; D_ASSERT(OperatorIsDelimGet(*join.children[delim_idx])); // get the filter (if any) - LogicalFilter *filter = nullptr; + optional_ptr filter; if (join.children[delim_idx]->type == LogicalOperatorType::LOGICAL_FILTER) { - filter = (LogicalFilter *)join.children[delim_idx].get(); + filter = &join.children[delim_idx]->Cast(); } auto &delim_get = (filter ? filter->children[0] : join.children[delim_idx])->Cast(); if (join.conditions.size() != delim_get.chunk_types.size()) { @@ -231,7 +231,7 @@ bool Deliminator::RemoveCandidate(unique_ptr *plan, unique_ptr< updater.projection_map[cb] = true; updater.reverse_proj_or_agg_map[cb] = proj_or_agg.expressions[cb.column_index].get(); for (auto &expr : nulls_are_not_equal_exprs) { - if (proj_or_agg.expressions[cb.column_index]->Equals(&expr.get())) { + if (proj_or_agg.expressions[cb.column_index]->Equals(expr.get())) { updater.projection_map[cb] = false; break; } @@ -254,8 +254,8 @@ bool Deliminator::RemoveCandidate(unique_ptr *plan, unique_ptr< updater.projection_map[cb] = true; updater.reverse_proj_or_agg_map[cb] = all_agg_exprs[cb.column_index]; for (auto &expr : nulls_are_not_equal_exprs) { - if ((cb.table_index == agg.group_index && agg.groups[cb.column_index]->Equals(&expr.get())) || - (cb.table_index == agg.aggregate_index && agg.expressions[cb.column_index]->Equals(&expr.get()))) { + if ((cb.table_index == agg.group_index && agg.groups[cb.column_index]->Equals(expr.get())) || + (cb.table_index == agg.aggregate_index && agg.expressions[cb.column_index]->Equals(expr.get()))) { updater.projection_map[cb] = false; break; } @@ -386,7 +386,7 @@ bool Deliminator::RemoveInequalityCandidate(unique_ptr *plan, u } // try to find the corresponding child condition // TODO: can be more flexible - allow CAST - auto child_expr = it->second; + auto &child_expr = *it->second; bool found = false; for (auto &child_cond : join.conditions) { if (child_cond.left->Equals(child_expr) || child_cond.right->Equals(child_expr)) { @@ -418,7 +418,7 @@ bool Deliminator::RemoveInequalityCandidate(unique_ptr *plan, u auto &parent_expr = parent_delim_get_side == 0 ? parent_cond.left : parent_cond.right; auto &parent_colref = parent_expr->Cast(); auto it = updater.reverse_proj_or_agg_map.find(parent_colref.binding); - auto child_expr = it->second; + auto &child_expr = *it->second; for (auto &child_cond : join.conditions) { if (!child_cond.left->Equals(child_expr) && !child_cond.right->Equals(child_expr)) { continue; diff --git a/src/optimizer/filter_combiner.cpp b/src/optimizer/filter_combiner.cpp index 79d2288cf298..34162724b241 100644 --- a/src/optimizer/filter_combiner.cpp +++ b/src/optimizer/filter_combiner.cpp @@ -635,7 +635,7 @@ FilterResult FilterCombiner::AddBoundComparisonFilter(Expression &expr) { // get the LHS and RHS nodes auto &left_node = GetNode(*comparison.left); auto &right_node = GetNode(*comparison.right); - if (left_node.Equals(&right_node)) { + if (left_node.Equals(right_node)) { return FilterResult::UNSUPPORTED; } // get the equivalence sets of the LHS and RHS @@ -806,7 +806,7 @@ FilterResult FilterCombiner::AddTransitiveFilters(BoundComparisonExpression &com } } - if (left_node.Equals(&right_node.get())) { + if (left_node.Equals(right_node)) { return FilterResult::UNSUPPORTED; } // get the equivalence sets of the LHS and RHS @@ -903,7 +903,7 @@ unique_ptr FilterCombiner::FindTransitiveFilter(Expression &expr) { for (idx_t i = 0; i < remaining_filters.size(); i++) { if (remaining_filters[i]->GetExpressionClass() == ExpressionClass::BOUND_COMPARISON) { auto &comparison = remaining_filters[i]->Cast(); - if (expr.Equals(comparison.right.get()) && comparison.type != ExpressionType::COMPARE_NOTEQUAL) { + if (expr.Equals(*comparison.right) && comparison.type != ExpressionType::COMPARE_NOTEQUAL) { auto filter = std::move(remaining_filters[i]); remaining_filters.erase(remaining_filters.begin() + i); return filter; diff --git a/src/optimizer/join_order/cardinality_estimator.cpp b/src/optimizer/join_order/cardinality_estimator.cpp index 6c37b3fb65bd..acdc0fbdd04c 100644 --- a/src/optimizer/join_order/cardinality_estimator.cpp +++ b/src/optimizer/join_order/cardinality_estimator.cpp @@ -479,15 +479,15 @@ optional_ptr CardinalityEstimator::GetTableFilters(LogicalOperat return get ? &get->table_filters : nullptr; } -idx_t CardinalityEstimator::InspectConjunctionAND(idx_t cardinality, idx_t column_index, ConjunctionAndFilter *filter, +idx_t CardinalityEstimator::InspectConjunctionAND(idx_t cardinality, idx_t column_index, ConjunctionAndFilter &filter, unique_ptr base_stats) { auto has_equality_filter = false; auto cardinality_after_filters = cardinality; - for (auto &child_filter : filter->child_filters) { + for (auto &child_filter : filter.child_filters) { if (child_filter->filter_type != TableFilterType::CONSTANT_COMPARISON) { continue; } - auto comparison_filter = (ConstantFilter &)*child_filter; + auto &comparison_filter = child_filter->Cast(); if (comparison_filter.comparison_type != ExpressionType::COMPARE_EQUAL) { continue; } @@ -510,15 +510,15 @@ idx_t CardinalityEstimator::InspectConjunctionAND(idx_t cardinality, idx_t colum return cardinality_after_filters; } -idx_t CardinalityEstimator::InspectConjunctionOR(idx_t cardinality, idx_t column_index, ConjunctionOrFilter *filter, +idx_t CardinalityEstimator::InspectConjunctionOR(idx_t cardinality, idx_t column_index, ConjunctionOrFilter &filter, unique_ptr base_stats) { auto has_equality_filter = false; auto cardinality_after_filters = cardinality; - for (auto &child_filter : filter->child_filters) { + for (auto &child_filter : filter.child_filters) { if (child_filter->filter_type != TableFilterType::CONSTANT_COMPARISON) { continue; } - auto comparison_filter = (ConstantFilter &)*child_filter; + auto &comparison_filter = child_filter->Cast(); if (comparison_filter.comparison_type == ExpressionType::COMPARE_EQUAL) { auto column_count = cardinality_after_filters; if (base_stats) { @@ -549,14 +549,14 @@ idx_t CardinalityEstimator::InspectTableFilters(idx_t cardinality, LogicalOperat column_statistics = get->function.statistics(context, &table_scan_bind_data, it.first); } if (it.second->filter_type == TableFilterType::CONJUNCTION_AND) { - auto &filter = (ConjunctionAndFilter &)*it.second; + auto &filter = it.second->Cast(); idx_t cardinality_with_and_filter = - InspectConjunctionAND(cardinality, it.first, &filter, std::move(column_statistics)); + InspectConjunctionAND(cardinality, it.first, filter, std::move(column_statistics)); cardinality_after_filters = MinValue(cardinality_after_filters, cardinality_with_and_filter); } else if (it.second->filter_type == TableFilterType::CONJUNCTION_OR) { - auto &filter = (ConjunctionOrFilter &)*it.second; + auto &filter = it.second->Cast(); idx_t cardinality_with_or_filter = - InspectConjunctionOR(cardinality, it.first, &filter, std::move(column_statistics)); + InspectConjunctionOR(cardinality, it.first, filter, std::move(column_statistics)); cardinality_after_filters = MinValue(cardinality_after_filters, cardinality_with_or_filter); } } diff --git a/src/optimizer/matcher/expression_matcher.cpp b/src/optimizer/matcher/expression_matcher.cpp index d2badecb8682..1d8ca93cae25 100644 --- a/src/optimizer/matcher/expression_matcher.cpp +++ b/src/optimizer/matcher/expression_matcher.cpp @@ -19,7 +19,7 @@ bool ExpressionMatcher::Match(Expression &expr, vector> &b } bool ExpressionEqualityMatcher::Match(Expression &expr, vector> &bindings) { - if (!expr.Equals(&expression)) { + if (!expr.Equals(expression)) { return false; } bindings.push_back(expr); diff --git a/src/optimizer/pullup/pullup_projection.cpp b/src/optimizer/pullup/pullup_projection.cpp index 7c716dfd2a4f..f2fd71b70206 100644 --- a/src/optimizer/pullup/pullup_projection.cpp +++ b/src/optimizer/pullup/pullup_projection.cpp @@ -24,8 +24,8 @@ static void ReplaceExpressionBinding(vector> &proj_expres BoundColumnRefExpression &colref = expr.Cast(); // find the corresponding column index in the projection expressions for (idx_t proj_idx = 0; proj_idx < proj_expressions.size(); proj_idx++) { - auto proj_expr = proj_expressions[proj_idx].get(); - if (proj_expr->type == ExpressionType::BOUND_COLUMN_REF) { + auto &proj_expr = *proj_expressions[proj_idx]; + if (proj_expr.type == ExpressionType::BOUND_COLUMN_REF) { if (colref.Equals(proj_expr)) { colref.binding.table_index = proj_table_idx; colref.binding.column_index = proj_idx; diff --git a/src/optimizer/rule/distributivity.cpp b/src/optimizer/rule/distributivity.cpp index d7f40ab07d46..509960c03b0d 100644 --- a/src/optimizer/rule/distributivity.cpp +++ b/src/optimizer/rule/distributivity.cpp @@ -33,7 +33,7 @@ unique_ptr DistributivityRule::ExtractExpression(BoundConjunctionExp // AND, remove expression from the list auto &and_expr = child->Cast(); for (idx_t i = 0; i < and_expr.children.size(); i++) { - if (Expression::Equals(and_expr.children[i].get(), &expr)) { + if (and_expr.children[i]->Equals(expr)) { result = std::move(and_expr.children[i]); and_expr.children.erase(and_expr.children.begin() + i); break; @@ -45,7 +45,7 @@ unique_ptr DistributivityRule::ExtractExpression(BoundConjunctionExp } else { // not an AND node! remove the entire expression // this happens in the case of e.g. (X AND B) OR X - D_ASSERT(Expression::Equals(child.get(), &expr)); + D_ASSERT(child->Equals(expr)); result = std::move(child); conj.children[idx] = nullptr; } diff --git a/src/optimizer/statistics/operator/propagate_filter.cpp b/src/optimizer/statistics/operator/propagate_filter.cpp index 0557c1898f3f..a9bfe91c60ec 100644 --- a/src/optimizer/statistics/operator/propagate_filter.cpp +++ b/src/optimizer/statistics/operator/propagate_filter.cpp @@ -161,15 +161,15 @@ void StatisticsPropagator::UpdateFilterStatistics(Expression &left, Expression & SetStatisticsNotNull((right.Cast()).binding); } // check if this is a comparison between a constant and a column ref - BoundConstantExpression *constant = nullptr; - BoundColumnRefExpression *columnref = nullptr; + optional_ptr constant; + optional_ptr columnref; if (left.type == ExpressionType::VALUE_CONSTANT && right.type == ExpressionType::BOUND_COLUMN_REF) { - constant = (BoundConstantExpression *)&left; - columnref = (BoundColumnRefExpression *)&right; + constant = &left.Cast(); + columnref = &right.Cast(); comparison_type = FlipComparisonExpression(comparison_type); } else if (left.type == ExpressionType::BOUND_COLUMN_REF && right.type == ExpressionType::VALUE_CONSTANT) { - columnref = (BoundColumnRefExpression *)&left; - constant = (BoundConstantExpression *)&right; + columnref = &left.Cast(); + constant = &right.Cast(); } else if (left.type == ExpressionType::BOUND_COLUMN_REF && right.type == ExpressionType::BOUND_COLUMN_REF) { // comparison between two column refs auto &left_column_ref = left.Cast(); diff --git a/src/optimizer/statistics/operator/propagate_get.cpp b/src/optimizer/statistics/operator/propagate_get.cpp index f023d78d5e1e..22979f19450c 100644 --- a/src/optimizer/statistics/operator/propagate_get.cpp +++ b/src/optimizer/statistics/operator/propagate_get.cpp @@ -14,14 +14,14 @@ void StatisticsPropagator::UpdateFilterStatistics(BaseStatistics &input, TableFi // FIXME: update stats... switch (filter.filter_type) { case TableFilterType::CONJUNCTION_AND: { - auto &conjunction_and = (ConjunctionAndFilter &)filter; + auto &conjunction_and = filter.Cast(); for (auto &child_filter : conjunction_and.child_filters) { UpdateFilterStatistics(input, *child_filter); } break; } case TableFilterType::CONSTANT_COMPARISON: { - auto &constant_filter = (ConstantFilter &)filter; + auto &constant_filter = filter.Cast(); UpdateFilterStatistics(input, constant_filter.comparison_type, constant_filter.constant); break; } diff --git a/src/parser/base_expression.cpp b/src/parser/base_expression.cpp index 5114f19fc593..92fa254a8a1c 100644 --- a/src/parser/base_expression.cpp +++ b/src/parser/base_expression.cpp @@ -18,11 +18,8 @@ string BaseExpression::GetName() const { return !alias.empty() ? alias : ToString(); } -bool BaseExpression::Equals(const BaseExpression *other) const { - if (!other) { - return false; - } - if (this->expression_class != other->expression_class || this->type != other->type) { +bool BaseExpression::Equals(const BaseExpression &other) const { + if (expression_class != other.expression_class || type != other.type) { return false; } return true; diff --git a/src/parser/expression/between_expression.cpp b/src/parser/expression/between_expression.cpp index 30e8d458a12e..350f2c9d30f8 100644 --- a/src/parser/expression/between_expression.cpp +++ b/src/parser/expression/between_expression.cpp @@ -15,14 +15,14 @@ string BetweenExpression::ToString() const { return ToString(*this); } -bool BetweenExpression::Equal(const BetweenExpression *a, const BetweenExpression *b) { - if (!a->input->Equals(b->input.get())) { +bool BetweenExpression::Equal(const BetweenExpression &a, const BetweenExpression &b) { + if (!a.input->Equals(*b.input)) { return false; } - if (!a->lower->Equals(b->lower.get())) { + if (!a.lower->Equals(*b.lower)) { return false; } - if (!a->upper->Equals(b->upper.get())) { + if (!a.upper->Equals(*b.upper)) { return false; } return true; diff --git a/src/parser/expression/case_expression.cpp b/src/parser/expression/case_expression.cpp index 36cf85d9639f..18fe781baf84 100644 --- a/src/parser/expression/case_expression.cpp +++ b/src/parser/expression/case_expression.cpp @@ -27,19 +27,19 @@ string CaseExpression::ToString() const { return ToString(*this); } -bool CaseExpression::Equal(const CaseExpression *a, const CaseExpression *b) { - if (a->case_checks.size() != b->case_checks.size()) { +bool CaseExpression::Equal(const CaseExpression &a, const CaseExpression &b) { + if (a.case_checks.size() != b.case_checks.size()) { return false; } - for (idx_t i = 0; i < a->case_checks.size(); i++) { - if (!a->case_checks[i].when_expr->Equals(b->case_checks[i].when_expr.get())) { + for (idx_t i = 0; i < a.case_checks.size(); i++) { + if (!a.case_checks[i].when_expr->Equals(*b.case_checks[i].when_expr)) { return false; } - if (!a->case_checks[i].then_expr->Equals(b->case_checks[i].then_expr.get())) { + if (!a.case_checks[i].then_expr->Equals(*b.case_checks[i].then_expr)) { return false; } } - if (!a->else_expr->Equals(b->else_expr.get())) { + if (!a.else_expr->Equals(*b.else_expr)) { return false; } return true; diff --git a/src/parser/expression/cast_expression.cpp b/src/parser/expression/cast_expression.cpp index c3e4b5507229..954e1c786d78 100644 --- a/src/parser/expression/cast_expression.cpp +++ b/src/parser/expression/cast_expression.cpp @@ -19,14 +19,14 @@ string CastExpression::ToString() const { return ToString(*this); } -bool CastExpression::Equal(const CastExpression *a, const CastExpression *b) { - if (!a->child->Equals(b->child.get())) { +bool CastExpression::Equal(const CastExpression &a, const CastExpression &b) { + if (!a.child->Equals(*b.child)) { return false; } - if (a->cast_type != b->cast_type) { + if (a.cast_type != b.cast_type) { return false; } - if (a->try_cast != b->try_cast) { + if (a.try_cast != b.try_cast) { return false; } return true; diff --git a/src/parser/expression/collate_expression.cpp b/src/parser/expression/collate_expression.cpp index 863ec09bd8b7..52f316d87a7f 100644 --- a/src/parser/expression/collate_expression.cpp +++ b/src/parser/expression/collate_expression.cpp @@ -18,11 +18,11 @@ string CollateExpression::ToString() const { return StringUtil::Format("%s COLLATE %s", child->ToString(), SQLIdentifier(collation)); } -bool CollateExpression::Equal(const CollateExpression *a, const CollateExpression *b) { - if (!a->child->Equals(b->child.get())) { +bool CollateExpression::Equal(const CollateExpression &a, const CollateExpression &b) { + if (!a.child->Equals(*b.child)) { return false; } - if (a->collation != b->collation) { + if (a.collation != b.collation) { return false; } return true; diff --git a/src/parser/expression/columnref_expression.cpp b/src/parser/expression/columnref_expression.cpp index 3ce20d002493..a4a382016cbd 100644 --- a/src/parser/expression/columnref_expression.cpp +++ b/src/parser/expression/columnref_expression.cpp @@ -64,12 +64,12 @@ string ColumnRefExpression::ToString() const { return result; } -bool ColumnRefExpression::Equal(const ColumnRefExpression *a, const ColumnRefExpression *b) { - if (a->column_names.size() != b->column_names.size()) { +bool ColumnRefExpression::Equal(const ColumnRefExpression &a, const ColumnRefExpression &b) { + if (a.column_names.size() != b.column_names.size()) { return false; } - for (idx_t i = 0; i < a->column_names.size(); i++) { - if (!StringUtil::CIEquals(a->column_names[i], b->column_names[i])) { + for (idx_t i = 0; i < a.column_names.size(); i++) { + if (!StringUtil::CIEquals(a.column_names[i], b.column_names[i])) { return false; } } diff --git a/src/parser/expression/comparison_expression.cpp b/src/parser/expression/comparison_expression.cpp index eb7b566c2ded..7bb67887cc6c 100644 --- a/src/parser/expression/comparison_expression.cpp +++ b/src/parser/expression/comparison_expression.cpp @@ -18,11 +18,11 @@ string ComparisonExpression::ToString() const { return ToString(*this); } -bool ComparisonExpression::Equal(const ComparisonExpression *a, const ComparisonExpression *b) { - if (!a->left->Equals(b->left.get())) { +bool ComparisonExpression::Equal(const ComparisonExpression &a, const ComparisonExpression &b) { + if (!a.left->Equals(*b.left)) { return false; } - if (!a->right->Equals(b->right.get())) { + if (!a.right->Equals(*b.right)) { return false; } return true; diff --git a/src/parser/expression/conjunction_expression.cpp b/src/parser/expression/conjunction_expression.cpp index b31c52a55648..cdbd22c60605 100644 --- a/src/parser/expression/conjunction_expression.cpp +++ b/src/parser/expression/conjunction_expression.cpp @@ -42,8 +42,8 @@ string ConjunctionExpression::ToString() const { return ToString(*this); } -bool ConjunctionExpression::Equal(const ConjunctionExpression *a, const ConjunctionExpression *b) { - return ExpressionUtil::SetEquals(a->children, b->children); +bool ConjunctionExpression::Equal(const ConjunctionExpression &a, const ConjunctionExpression &b) { + return ExpressionUtil::SetEquals(a.children, b.children); } unique_ptr ConjunctionExpression::Copy() const { diff --git a/src/parser/expression/constant_expression.cpp b/src/parser/expression/constant_expression.cpp index 720e851f239c..b66f0101522e 100644 --- a/src/parser/expression/constant_expression.cpp +++ b/src/parser/expression/constant_expression.cpp @@ -18,8 +18,8 @@ string ConstantExpression::ToString() const { return value.ToSQLString(); } -bool ConstantExpression::Equal(const ConstantExpression *a, const ConstantExpression *b) { - return a->value.type() == b->value.type() && !ValueOperations::DistinctFrom(a->value, b->value); +bool ConstantExpression::Equal(const ConstantExpression &a, const ConstantExpression &b) { + return a.value.type() == b.value.type() && !ValueOperations::DistinctFrom(a.value, b.value); } hash_t ConstantExpression::Hash() const { diff --git a/src/parser/expression/function_expression.cpp b/src/parser/expression/function_expression.cpp index 8c539ae8b8f9..15ad847750b1 100644 --- a/src/parser/expression/function_expression.cpp +++ b/src/parser/expression/function_expression.cpp @@ -37,26 +37,26 @@ string FunctionExpression::ToString() const { filter.get(), order_bys.get(), export_state, true); } -bool FunctionExpression::Equal(const FunctionExpression *a, const FunctionExpression *b) { - if (a->catalog != b->catalog || a->schema != b->schema || a->function_name != b->function_name || - b->distinct != a->distinct) { +bool FunctionExpression::Equal(const FunctionExpression &a, const FunctionExpression &b) { + if (a.catalog != b.catalog || a.schema != b.schema || a.function_name != b.function_name || + b.distinct != a.distinct) { return false; } - if (b->children.size() != a->children.size()) { + if (b.children.size() != a.children.size()) { return false; } - for (idx_t i = 0; i < a->children.size(); i++) { - if (!a->children[i]->Equals(b->children[i].get())) { + for (idx_t i = 0; i < a.children.size(); i++) { + if (!a.children[i]->Equals(*b.children[i])) { return false; } } - if (!BaseExpression::Equals(a->filter.get(), b->filter.get())) { + if (!ParsedExpression::Equals(a.filter, b.filter)) { return false; } - if (!a->order_bys->Equals(b->order_bys.get())) { + if (!OrderModifier::Equals(a.order_bys, b.order_bys)) { return false; } - if (a->export_state != b->export_state) { + if (a.export_state != b.export_state) { return false; } return true; @@ -81,11 +81,7 @@ unique_ptr FunctionExpression::Copy() const { if (filter) { filter_copy = filter->Copy(); } - unique_ptr order_copy; - if (order_bys) { - order_copy.reset(static_cast(order_bys->Copy().release())); - } - + auto order_copy = order_bys ? unique_ptr_cast(order_bys->Copy()) : nullptr; auto copy = make_uniq(catalog, schema, function_name, std::move(copy_children), std::move(filter_copy), std::move(order_copy), distinct, is_operator, export_state); diff --git a/src/parser/expression/lambda_expression.cpp b/src/parser/expression/lambda_expression.cpp index 8e47718f6866..eb57586a7322 100644 --- a/src/parser/expression/lambda_expression.cpp +++ b/src/parser/expression/lambda_expression.cpp @@ -16,8 +16,8 @@ string LambdaExpression::ToString() const { return lhs->ToString() + " -> " + expr->ToString(); } -bool LambdaExpression::Equal(const LambdaExpression *a, const LambdaExpression *b) { - return a->lhs->Equals(b->lhs.get()) && a->expr->Equals(b->expr.get()); +bool LambdaExpression::Equal(const LambdaExpression &a, const LambdaExpression &b) { + return a.lhs->Equals(*b.lhs) && a.expr->Equals(*b.expr); } hash_t LambdaExpression::Hash() const { diff --git a/src/parser/expression/operator_expression.cpp b/src/parser/expression/operator_expression.cpp index 82666ed9ee88..d232f767a383 100644 --- a/src/parser/expression/operator_expression.cpp +++ b/src/parser/expression/operator_expression.cpp @@ -27,12 +27,12 @@ string OperatorExpression::ToString() const { return ToString(*this); } -bool OperatorExpression::Equal(const OperatorExpression *a, const OperatorExpression *b) { - if (a->children.size() != b->children.size()) { +bool OperatorExpression::Equal(const OperatorExpression &a, const OperatorExpression &b) { + if (a.children.size() != b.children.size()) { return false; } - for (idx_t i = 0; i < a->children.size(); i++) { - if (!a->children[i]->Equals(b->children[i].get())) { + for (idx_t i = 0; i < a.children.size(); i++) { + if (!a.children[i]->Equals(*b.children[i])) { return false; } } diff --git a/src/parser/expression/parameter_expression.cpp b/src/parser/expression/parameter_expression.cpp index 619769fd8226..99a7267363ca 100644 --- a/src/parser/expression/parameter_expression.cpp +++ b/src/parser/expression/parameter_expression.cpp @@ -25,8 +25,8 @@ unique_ptr ParameterExpression::Copy() const { return std::move(copy); } -bool ParameterExpression::Equal(const ParameterExpression *a, const ParameterExpression *b) { - return a->parameter_nr == b->parameter_nr; +bool ParameterExpression::Equal(const ParameterExpression &a, const ParameterExpression &b) { + return a.parameter_nr == b.parameter_nr; } hash_t ParameterExpression::Hash() const { diff --git a/src/parser/expression/positional_reference_expression.cpp b/src/parser/expression/positional_reference_expression.cpp index a6559a7820f4..aa979e7174f9 100644 --- a/src/parser/expression/positional_reference_expression.cpp +++ b/src/parser/expression/positional_reference_expression.cpp @@ -18,9 +18,9 @@ string PositionalReferenceExpression::ToString() const { return "#" + to_string(index); } -bool PositionalReferenceExpression::Equal(const PositionalReferenceExpression *a, - const PositionalReferenceExpression *b) { - return a->index == b->index; +bool PositionalReferenceExpression::Equal(const PositionalReferenceExpression &a, + const PositionalReferenceExpression &b) { + return a.index == b.index; } unique_ptr PositionalReferenceExpression::Copy() const { diff --git a/src/parser/expression/star_expression.cpp b/src/parser/expression/star_expression.cpp index 506f7c1b6deb..d03167c71780 100644 --- a/src/parser/expression/star_expression.cpp +++ b/src/parser/expression/star_expression.cpp @@ -54,26 +54,26 @@ string StarExpression::ToString() const { return result; } -bool StarExpression::Equal(const StarExpression *a, const StarExpression *b) { - if (a->relation_name != b->relation_name || a->exclude_list != b->exclude_list) { +bool StarExpression::Equal(const StarExpression &a, const StarExpression &b) { + if (a.relation_name != b.relation_name || a.exclude_list != b.exclude_list) { return false; } - if (a->columns != b->columns) { + if (a.columns != b.columns) { return false; } - if (a->replace_list.size() != b->replace_list.size()) { + if (a.replace_list.size() != b.replace_list.size()) { return false; } - for (auto &entry : a->replace_list) { - auto other_entry = b->replace_list.find(entry.first); - if (other_entry == b->replace_list.end()) { + for (auto &entry : a.replace_list) { + auto other_entry = b.replace_list.find(entry.first); + if (other_entry == b.replace_list.end()) { return false; } - if (!entry.second->Equals(other_entry->second.get())) { + if (!entry.second->Equals(*other_entry->second)) { return false; } } - if (!BaseExpression::Equals(a->expr.get(), b->expr.get())) { + if (!ParsedExpression::Equals(a.expr, b.expr)) { return false; } return true; diff --git a/src/parser/expression/subquery_expression.cpp b/src/parser/expression/subquery_expression.cpp index 1d37347c2bbe..02d363484093 100644 --- a/src/parser/expression/subquery_expression.cpp +++ b/src/parser/expression/subquery_expression.cpp @@ -28,15 +28,15 @@ string SubqueryExpression::ToString() const { } } -bool SubqueryExpression::Equal(const SubqueryExpression *a, const SubqueryExpression *b) { - if (!a->subquery || !b->subquery) { +bool SubqueryExpression::Equal(const SubqueryExpression &a, const SubqueryExpression &b) { + if (!a.subquery || !b.subquery) { return false; } - if (!BaseExpression::Equals(a->child.get(), b->child.get())) { + if (!ParsedExpression::Equals(a.child, b.child)) { return false; } - return a->comparison_type == b->comparison_type && a->subquery_type == b->subquery_type && - a->subquery->Equals(b->subquery.get()); + return a.comparison_type == b.comparison_type && a.subquery_type == b.subquery_type && + a.subquery->Equals(*b.subquery); } unique_ptr SubqueryExpression::Copy() const { diff --git a/src/parser/expression/window_expression.cpp b/src/parser/expression/window_expression.cpp index da6f9be015db..028eca7d5141 100644 --- a/src/parser/expression/window_expression.cpp +++ b/src/parser/expression/window_expression.cpp @@ -63,53 +63,42 @@ string WindowExpression::ToString() const { return ToString(*this, schema, function_name); } -bool WindowExpression::Equal(const WindowExpression *a, const WindowExpression *b) { +bool WindowExpression::Equal(const WindowExpression &a, const WindowExpression &b) { // check if the child expressions are equivalent - if (b->children.size() != a->children.size()) { + if (a.ignore_nulls != b.ignore_nulls) { return false; } - if (a->ignore_nulls != b->ignore_nulls) { + if (!ParsedExpression::ListEquals(a.children, b.children)) { return false; } - for (idx_t i = 0; i < a->children.size(); i++) { - if (!a->children[i]->Equals(b->children[i].get())) { - return false; - } - } - if (a->start != b->start || a->end != b->end) { + if (a.start != b.start || a.end != b.end) { return false; } // check if the framing expressions are equivalentbind_ - if (!BaseExpression::Equals(a->start_expr.get(), b->start_expr.get()) || - !BaseExpression::Equals(a->end_expr.get(), b->end_expr.get()) || - !BaseExpression::Equals(a->offset_expr.get(), b->offset_expr.get()) || - !BaseExpression::Equals(a->default_expr.get(), b->default_expr.get())) { + if (!ParsedExpression::Equals(a.start_expr, b.start_expr) || !ParsedExpression::Equals(a.end_expr, b.end_expr) || + !ParsedExpression::Equals(a.offset_expr, b.offset_expr) || + !ParsedExpression::Equals(a.default_expr, b.default_expr)) { return false; } // check if the partitions are equivalent - if (a->partitions.size() != b->partitions.size()) { + if (!ParsedExpression::ListEquals(a.partitions, b.partitions)) { return false; } - for (idx_t i = 0; i < a->partitions.size(); i++) { - if (!a->partitions[i]->Equals(b->partitions[i].get())) { - return false; - } - } // check if the orderings are equivalent - if (a->orders.size() != b->orders.size()) { + if (a.orders.size() != b.orders.size()) { return false; } - for (idx_t i = 0; i < a->orders.size(); i++) { - if (a->orders[i].type != b->orders[i].type) { + for (idx_t i = 0; i < a.orders.size(); i++) { + if (a.orders[i].type != b.orders[i].type) { return false; } - if (!a->orders[i].expression->Equals(b->orders[i].expression.get())) { + if (!a.orders[i].expression->Equals(*b.orders[i].expression)) { return false; } } // check if the filter clauses are equivalent - if (!BaseExpression::Equals(a->filter_expr.get(), b->filter_expr.get())) { + if (!ParsedExpression::Equals(a.filter_expr, b.filter_expr)) { return false; } diff --git a/src/parser/parsed_expression.cpp b/src/parser/parsed_expression.cpp index 4846b51df234..e6ad1960146d 100644 --- a/src/parser/parsed_expression.cpp +++ b/src/parser/parsed_expression.cpp @@ -7,6 +7,7 @@ #include "duckdb/parser/parsed_expression_iterator.hpp" #include "duckdb/common/serializer/format_serializer.hpp" #include "duckdb/common/serializer/format_deserializer.hpp" +#include "duckdb/parser/expression_util.hpp" namespace duckdb { @@ -48,46 +49,46 @@ bool ParsedExpression::HasSubquery() const { return has_subquery; } -bool ParsedExpression::Equals(const BaseExpression *other) const { +bool ParsedExpression::Equals(const BaseExpression &other) const { if (!BaseExpression::Equals(other)) { return false; } switch (expression_class) { case ExpressionClass::BETWEEN: - return BetweenExpression::Equal((BetweenExpression *)this, (BetweenExpression *)other); + return BetweenExpression::Equal(Cast(), other.Cast()); case ExpressionClass::CASE: - return CaseExpression::Equal((CaseExpression *)this, (CaseExpression *)other); + return CaseExpression::Equal(Cast(), other.Cast()); case ExpressionClass::CAST: - return CastExpression::Equal((CastExpression *)this, (CastExpression *)other); + return CastExpression::Equal(Cast(), other.Cast()); case ExpressionClass::COLLATE: - return CollateExpression::Equal((CollateExpression *)this, (CollateExpression *)other); + return CollateExpression::Equal(Cast(), other.Cast()); case ExpressionClass::COLUMN_REF: - return ColumnRefExpression::Equal((ColumnRefExpression *)this, (ColumnRefExpression *)other); + return ColumnRefExpression::Equal(Cast(), other.Cast()); case ExpressionClass::COMPARISON: - return ComparisonExpression::Equal((ComparisonExpression *)this, (ComparisonExpression *)other); + return ComparisonExpression::Equal(Cast(), other.Cast()); case ExpressionClass::CONJUNCTION: - return ConjunctionExpression::Equal((ConjunctionExpression *)this, (ConjunctionExpression *)other); + return ConjunctionExpression::Equal(Cast(), other.Cast()); case ExpressionClass::CONSTANT: - return ConstantExpression::Equal((ConstantExpression *)this, (ConstantExpression *)other); + return ConstantExpression::Equal(Cast(), other.Cast()); case ExpressionClass::DEFAULT: return true; case ExpressionClass::FUNCTION: - return FunctionExpression::Equal((FunctionExpression *)this, (FunctionExpression *)other); + return FunctionExpression::Equal(Cast(), other.Cast()); case ExpressionClass::LAMBDA: - return LambdaExpression::Equal((LambdaExpression *)this, (LambdaExpression *)other); + return LambdaExpression::Equal(Cast(), other.Cast()); case ExpressionClass::OPERATOR: - return OperatorExpression::Equal((OperatorExpression *)this, (OperatorExpression *)other); + return OperatorExpression::Equal(Cast(), other.Cast()); case ExpressionClass::PARAMETER: - return ParameterExpression::Equal((ParameterExpression *)this, (ParameterExpression *)other); + return ParameterExpression::Equal(Cast(), other.Cast()); case ExpressionClass::POSITIONAL_REFERENCE: - return PositionalReferenceExpression::Equal((PositionalReferenceExpression *)this, - (PositionalReferenceExpression *)other); + return PositionalReferenceExpression::Equal(Cast(), + other.Cast()); case ExpressionClass::STAR: - return StarExpression::Equal((StarExpression *)this, (StarExpression *)other); + return StarExpression::Equal(Cast(), other.Cast()); case ExpressionClass::SUBQUERY: - return SubqueryExpression::Equal((SubqueryExpression *)this, (SubqueryExpression *)other); + return SubqueryExpression::Equal(Cast(), other.Cast()); case ExpressionClass::WINDOW: - return WindowExpression::Equal((WindowExpression *)this, (WindowExpression *)other); + return WindowExpression::Equal(Cast(), other.Cast()); default: throw SerializationException("Unsupported type for expression comparison!"); } @@ -246,4 +247,19 @@ unique_ptr ParsedExpression::Deserialize(Deserializer &source) return result; } +bool ParsedExpression::Equals(const unique_ptr &left, const unique_ptr &right) { + if (left.get() == right.get()) { + return true; + } + if (!left || !right) { + return false; + } + return left->Equals(*right); +} + +bool ParsedExpression::ListEquals(const vector> &left, + const vector> &right) { + return ExpressionUtil::ListEquals(left, right); +} + } // namespace duckdb diff --git a/src/parser/parsed_expression_iterator.cpp b/src/parser/parsed_expression_iterator.cpp index 61dda4e1ace6..0b113c9d7f59 100644 --- a/src/parser/parsed_expression_iterator.cpp +++ b/src/parser/parsed_expression_iterator.cpp @@ -158,7 +158,7 @@ void ParsedExpressionIterator::EnumerateQueryNodeModifiers( for (auto &modifier : node.modifiers) { switch (modifier->type) { case ResultModifierType::LIMIT_MODIFIER: { - auto &limit_modifier = (LimitModifier &)*modifier; + auto &limit_modifier = modifier->Cast(); if (limit_modifier.limit) { callback(limit_modifier.limit); } @@ -168,7 +168,7 @@ void ParsedExpressionIterator::EnumerateQueryNodeModifiers( } break; case ResultModifierType::LIMIT_PERCENT_MODIFIER: { - auto &limit_modifier = (LimitPercentModifier &)*modifier; + auto &limit_modifier = modifier->Cast(); if (limit_modifier.limit) { callback(limit_modifier.limit); } @@ -178,14 +178,14 @@ void ParsedExpressionIterator::EnumerateQueryNodeModifiers( } break; case ResultModifierType::ORDER_MODIFIER: { - auto &order_modifier = (OrderModifier &)*modifier; + auto &order_modifier = modifier->Cast(); for (auto &order : order_modifier.orders) { callback(order.expression); } } break; case ResultModifierType::DISTINCT_MODIFIER: { - auto &distinct_modifier = (DistinctModifier &)*modifier; + auto &distinct_modifier = modifier->Cast(); for (auto &target : distinct_modifier.distinct_on_targets) { callback(target); } diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index 560e01ace0c3..b16a81356eee 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -51,7 +51,7 @@ static bool StripUnicodeSpaces(const string &query_str, string &new_query) { idx_t pos = 0; unsigned char quote; vector unicode_spaces; - auto query = (unsigned char *)query_str.c_str(); + auto query = data_ptr_cast(query_str.c_str()); auto qsize = query_str.size(); regular: @@ -288,7 +288,7 @@ vector Parser::ParseOrderList(const string &select_list, ParserOpti select_node.modifiers.size() != 1) { throw ParserException("Expected a single ORDER clause"); } - auto &order = (OrderModifier &)*select_node.modifiers[0]; + auto &order = select_node.modifiers[0]->Cast(); return std::move(order.orders); } @@ -341,7 +341,7 @@ ColumnList Parser::ParseColumnList(const string &column_list, ParserOptions opti if (create.info->type != CatalogType::TABLE_ENTRY) { throw InternalException("Expected a single CREATE TABLE statement"); } - auto &info = ((CreateTableInfo &)*create.info); + auto &info = create.info->Cast(); return std::move(info.columns); } diff --git a/src/parser/query_node.cpp b/src/parser/query_node.cpp index 153ae6c16418..afc84b4483e3 100644 --- a/src/parser/query_node.cpp +++ b/src/parser/query_node.cpp @@ -82,7 +82,7 @@ string QueryNode::ResultModifiersToString() const { for (idx_t modifier_idx = 0; modifier_idx < modifiers.size(); modifier_idx++) { auto &modifier = *modifiers[modifier_idx]; if (modifier.type == ResultModifierType::ORDER_MODIFIER) { - auto &order_modifier = (OrderModifier &)modifier; + auto &order_modifier = modifier.Cast(); result += " ORDER BY "; for (idx_t k = 0; k < order_modifier.orders.size(); k++) { if (k > 0) { @@ -91,7 +91,7 @@ string QueryNode::ResultModifiersToString() const { result += order_modifier.orders[k].ToString(); } } else if (modifier.type == ResultModifierType::LIMIT_MODIFIER) { - auto &limit_modifier = (LimitModifier &)modifier; + auto &limit_modifier = modifier.Cast(); if (limit_modifier.limit) { result += " LIMIT " + limit_modifier.limit->ToString(); } @@ -99,7 +99,7 @@ string QueryNode::ResultModifiersToString() const { result += " OFFSET " + limit_modifier.offset->ToString(); } } else if (modifier.type == ResultModifierType::LIMIT_PERCENT_MODIFIER) { - auto &limit_p_modifier = (LimitPercentModifier &)modifier; + auto &limit_p_modifier = modifier.Cast(); if (limit_p_modifier.limit) { result += " LIMIT (" + limit_p_modifier.limit->ToString() + ") %"; } @@ -121,11 +121,12 @@ bool QueryNode::Equals(const QueryNode *other) const { if (other->type != this->type) { return false; } + if (modifiers.size() != other->modifiers.size()) { return false; } for (idx_t i = 0; i < modifiers.size(); i++) { - if (!modifiers[i]->Equals(other->modifiers[i].get())) { + if (!modifiers[i]->Equals(*other->modifiers[i])) { return false; } } @@ -141,7 +142,7 @@ bool QueryNode::Equals(const QueryNode *other) const { if (entry.second->aliases != other_entry->second->aliases) { return false; } - if (!entry.second->query->Equals(other_entry->second->query.get())) { + if (!entry.second->query->Equals(*other_entry->second->query)) { return false; } } @@ -257,7 +258,7 @@ void QueryNode::AddDistinct() { for (idx_t modifier_idx = modifiers.size(); modifier_idx > 0; modifier_idx--) { auto &modifier = *modifiers[modifier_idx - 1]; if (modifier.type == ResultModifierType::DISTINCT_MODIFIER) { - auto &distinct_modifier = (DistinctModifier &)modifier; + auto &distinct_modifier = modifier.Cast(); if (distinct_modifier.distinct_on_targets.empty()) { // we have a DISTINCT without an ON clause - this distinct does not need to be added return; diff --git a/src/parser/query_node/select_node.cpp b/src/parser/query_node/select_node.cpp index 635878d2a72c..81ae9b885516 100644 --- a/src/parser/query_node/select_node.cpp +++ b/src/parser/query_node/select_node.cpp @@ -120,22 +120,15 @@ bool SelectNode::Equals(const QueryNode *other_p) const { return false; } // FROM - if (from_table) { - // we have a FROM clause, compare to the other one - if (!from_table->Equals(other.from_table.get())) { - return false; - } - } else if (other.from_table) { - // we don't have a FROM clause, if the other statement has one they are - // not equal + if (!TableRef::Equals(from_table, other.from_table)) { return false; } // WHERE - if (!BaseExpression::Equals(where_clause.get(), other.where_clause.get())) { + if (!ParsedExpression::Equals(where_clause, other.where_clause)) { return false; } // GROUP BY - if (!ExpressionUtil::ListEquals(groups.group_expressions, other.groups.group_expressions)) { + if (!ParsedExpression::ListEquals(groups.group_expressions, other.groups.group_expressions)) { return false; } if (groups.grouping_sets != other.groups.grouping_sets) { @@ -145,11 +138,11 @@ bool SelectNode::Equals(const QueryNode *other_p) const { return false; } // HAVING - if (!BaseExpression::Equals(having.get(), other.having.get())) { + if (!ParsedExpression::Equals(having, other.having)) { return false; } // QUALIFY - if (!BaseExpression::Equals(qualify.get(), other.qualify.get())) { + if (!ParsedExpression::Equals(qualify, other.qualify)) { return false; } return true; diff --git a/src/parser/result_modifier.cpp b/src/parser/result_modifier.cpp index 583382093771..e96eec2114fe 100644 --- a/src/parser/result_modifier.cpp +++ b/src/parser/result_modifier.cpp @@ -6,11 +6,8 @@ namespace duckdb { -bool ResultModifier::Equals(const ResultModifier *other) const { - if (!other) { - return false; - } - return type == other->type; +bool ResultModifier::Equals(const ResultModifier &other) const { + return type == other.type; } void ResultModifier::Serialize(Serializer &serializer) const { @@ -72,15 +69,15 @@ unique_ptr ResultModifier::Deserialize(Deserializer &source) { return result; } -bool LimitModifier::Equals(const ResultModifier *other_p) const { +bool LimitModifier::Equals(const ResultModifier &other_p) const { if (!ResultModifier::Equals(other_p)) { return false; } - auto &other = (LimitModifier &)*other_p; - if (!BaseExpression::Equals(limit.get(), other.limit.get())) { + auto &other = other_p.Cast(); + if (!ParsedExpression::Equals(limit, other.limit)) { return false; } - if (!BaseExpression::Equals(offset.get(), other.offset.get())) { + if (!ParsedExpression::Equals(offset, other.offset)) { return false; } return true; @@ -122,11 +119,11 @@ unique_ptr LimitModifier::Deserialize(FieldReader &reader) { return std::move(mod); } -bool DistinctModifier::Equals(const ResultModifier *other_p) const { +bool DistinctModifier::Equals(const ResultModifier &other_p) const { if (!ResultModifier::Equals(other_p)) { return false; } - auto &other = (DistinctModifier &)*other_p; + auto &other = other_p.Cast(); if (!ExpressionUtil::ListEquals(distinct_on_targets, other.distinct_on_targets)) { return false; } @@ -162,11 +159,11 @@ unique_ptr DistinctModifier::Deserialize(FieldReader &reader) { return std::move(mod); } -bool OrderModifier::Equals(const ResultModifier *other_p) const { +bool OrderModifier::Equals(const ResultModifier &other_p) const { if (!ResultModifier::Equals(other_p)) { return false; } - auto &other = (OrderModifier &)*other_p; + auto &other = other_p.Cast(); if (orders.size() != other.orders.size()) { return false; } @@ -174,13 +171,23 @@ bool OrderModifier::Equals(const ResultModifier *other_p) const { if (orders[i].type != other.orders[i].type) { return false; } - if (!BaseExpression::Equals(orders[i].expression.get(), other.orders[i].expression.get())) { + if (!BaseExpression::Equals(*orders[i].expression, *other.orders[i].expression)) { return false; } } return true; } +bool OrderModifier::Equals(const unique_ptr &left, const unique_ptr &right) { + if (left.get() == right.get()) { + return true; + } + if (!left || !right) { + return false; + } + return left->Equals(*right); +} + unique_ptr OrderModifier::Copy() const { auto copy = make_uniq(); for (auto &order : orders) { @@ -265,15 +272,15 @@ unique_ptr OrderModifier::Deserialize(FieldReader &reader) { return std::move(mod); } -bool LimitPercentModifier::Equals(const ResultModifier *other_p) const { +bool LimitPercentModifier::Equals(const ResultModifier &other_p) const { if (!ResultModifier::Equals(other_p)) { return false; } - auto &other = (LimitPercentModifier &)*other_p; - if (!BaseExpression::Equals(limit.get(), other.limit.get())) { + auto &other = other_p.Cast(); + if (!BaseExpression::Equals(*limit, *other.limit)) { return false; } - if (!BaseExpression::Equals(offset.get(), other.offset.get())) { + if (!BaseExpression::Equals(*offset, *other.offset)) { return false; } return true; diff --git a/src/parser/statement/select_statement.cpp b/src/parser/statement/select_statement.cpp index a20432ba63be..c5cb2546957f 100644 --- a/src/parser/statement/select_statement.cpp +++ b/src/parser/statement/select_statement.cpp @@ -33,11 +33,11 @@ unique_ptr SelectStatement::FormatDeserialize(FormatDeserialize return result; } -bool SelectStatement::Equals(const SQLStatement *other_p) const { - if (type != other_p->type) { +bool SelectStatement::Equals(const SQLStatement &other_p) const { + if (type != other_p.type) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return node->Equals(other.node.get()); } diff --git a/src/parser/tableref.cpp b/src/parser/tableref.cpp index 0815ee20a1d8..3e539ffa0073 100644 --- a/src/parser/tableref.cpp +++ b/src/parser/tableref.cpp @@ -40,9 +40,8 @@ string TableRef::BaseToString(string result, const vector &column_name_a return result; } -bool TableRef::Equals(const TableRef *other) const { - return other && type == other->type && alias == other->alias && - SampleOptions::Equals(sample.get(), other->sample.get()); +bool TableRef::Equals(const TableRef &other) const { + return type == other.type && alias == other.alias && SampleOptions::Equals(sample.get(), other.sample.get()); } void TableRef::Serialize(Serializer &serializer) const { @@ -149,4 +148,14 @@ void TableRef::Print() { Printer::Print(ToString()); } +bool TableRef::Equals(const unique_ptr &left, const unique_ptr &right) { + if (left.get() == right.get()) { + return true; + } + if (!left || !right) { + return false; + } + return left->Equals(*right); +} + } // namespace duckdb diff --git a/src/parser/tableref/basetableref.cpp b/src/parser/tableref/basetableref.cpp index f1961c7644bc..99dd63061500 100644 --- a/src/parser/tableref/basetableref.cpp +++ b/src/parser/tableref/basetableref.cpp @@ -15,13 +15,13 @@ string BaseTableRef::ToString() const { return BaseToString(result, column_name_alias); } -bool BaseTableRef::Equals(const TableRef *other_p) const { +bool BaseTableRef::Equals(const TableRef &other_p) const { if (!TableRef::Equals(other_p)) { return false; } - auto other = (BaseTableRef *)other_p; - return other->catalog_name == catalog_name && other->schema_name == schema_name && - other->table_name == table_name && column_name_alias == other->column_name_alias; + auto &other = other_p.Cast(); + return other.catalog_name == catalog_name && other.schema_name == schema_name && other.table_name == table_name && + column_name_alias == other.column_name_alias; } void BaseTableRef::Serialize(FieldWriter &writer) const { diff --git a/src/parser/tableref/emptytableref.cpp b/src/parser/tableref/emptytableref.cpp index f672c4aa000e..51bdf5c2fd15 100644 --- a/src/parser/tableref/emptytableref.cpp +++ b/src/parser/tableref/emptytableref.cpp @@ -8,7 +8,7 @@ string EmptyTableRef::ToString() const { return ""; } -bool EmptyTableRef::Equals(const TableRef *other) const { +bool EmptyTableRef::Equals(const TableRef &other) const { return TableRef::Equals(other); } diff --git a/src/parser/tableref/expressionlistref.cpp b/src/parser/tableref/expressionlistref.cpp index 966fd19dee13..c33ca0ece6a9 100644 --- a/src/parser/tableref/expressionlistref.cpp +++ b/src/parser/tableref/expressionlistref.cpp @@ -27,20 +27,20 @@ string ExpressionListRef::ToString() const { return BaseToString(result, expected_names); } -bool ExpressionListRef::Equals(const TableRef *other_p) const { +bool ExpressionListRef::Equals(const TableRef &other_p) const { if (!TableRef::Equals(other_p)) { return false; } - auto other = (ExpressionListRef *)other_p; - if (values.size() != other->values.size()) { + auto &other = other_p.Cast(); + if (values.size() != other.values.size()) { return false; } for (idx_t i = 0; i < values.size(); i++) { - if (values[i].size() != other->values[i].size()) { + if (values[i].size() != other.values[i].size()) { return false; } for (idx_t j = 0; j < values[i].size(); j++) { - if (!values[i][j]->Equals(other->values[i][j].get())) { + if (!values[i][j]->Equals(*other.values[i][j])) { return false; } } diff --git a/src/parser/tableref/joinref.cpp b/src/parser/tableref/joinref.cpp index abd6b69cb7c7..38de7f579f35 100644 --- a/src/parser/tableref/joinref.cpp +++ b/src/parser/tableref/joinref.cpp @@ -48,21 +48,21 @@ string JoinRef::ToString() const { return result; } -bool JoinRef::Equals(const TableRef *other_p) const { +bool JoinRef::Equals(const TableRef &other_p) const { if (!TableRef::Equals(other_p)) { return false; } - auto other = (JoinRef *)other_p; - if (using_columns.size() != other->using_columns.size()) { + auto &other = other_p.Cast(); + if (using_columns.size() != other.using_columns.size()) { return false; } for (idx_t i = 0; i < using_columns.size(); i++) { - if (using_columns[i] != other->using_columns[i]) { + if (using_columns[i] != other.using_columns[i]) { return false; } } - return left->Equals(other->left.get()) && right->Equals(other->right.get()) && - BaseExpression::Equals(condition.get(), other->condition.get()) && type == other->type; + return left->Equals(*other.left) && right->Equals(*other.right) && + ParsedExpression::Equals(condition, other.condition) && type == other.type; } unique_ptr JoinRef::Copy() { diff --git a/src/parser/tableref/pivotref.cpp b/src/parser/tableref/pivotref.cpp index c8f3dec88aeb..32e3c1d00a63 100644 --- a/src/parser/tableref/pivotref.cpp +++ b/src/parser/tableref/pivotref.cpp @@ -269,40 +269,35 @@ string PivotRef::ToString() const { return result; } -bool PivotRef::Equals(const TableRef *other_p) const { +bool PivotRef::Equals(const TableRef &other_p) const { if (!TableRef::Equals(other_p)) { return false; } - auto other = (PivotRef *)other_p; - if (!source->Equals(other->source.get())) { + auto &other = other_p.Cast(); + if (!source->Equals(*other.source)) { return false; } - if (aggregates.size() != other->aggregates.size()) { + if (!ParsedExpression::ListEquals(aggregates, other.aggregates)) { return false; } - for (idx_t i = 0; i < aggregates.size(); i++) { - if (!BaseExpression::Equals(aggregates[i].get(), other->aggregates[i].get())) { - return false; - } - } - if (pivots.size() != other->pivots.size()) { + if (pivots.size() != other.pivots.size()) { return false; } for (idx_t i = 0; i < pivots.size(); i++) { - if (!pivots[i].Equals(other->pivots[i])) { + if (!pivots[i].Equals(other.pivots[i])) { return false; } } - if (unpivot_names != other->unpivot_names) { + if (unpivot_names != other.unpivot_names) { return false; } - if (alias != other->alias) { + if (alias != other.alias) { return false; } - if (groups != other->groups) { + if (groups != other.groups) { return false; } - if (include_nulls != other->include_nulls) { + if (include_nulls != other.include_nulls) { return false; } return true; diff --git a/src/parser/tableref/subqueryref.cpp b/src/parser/tableref/subqueryref.cpp index fd1d85e79044..115cd935777b 100644 --- a/src/parser/tableref/subqueryref.cpp +++ b/src/parser/tableref/subqueryref.cpp @@ -17,12 +17,12 @@ SubqueryRef::SubqueryRef(unique_ptr subquery_p, string alias_p) this->alias = std::move(alias_p); } -bool SubqueryRef::Equals(const TableRef *other_p) const { +bool SubqueryRef::Equals(const TableRef &other_p) const { if (!TableRef::Equals(other_p)) { return false; } - auto other = (SubqueryRef *)other_p; - return subquery->Equals(other->subquery.get()); + auto &other = other_p.Cast(); + return subquery->Equals(*other.subquery); } unique_ptr SubqueryRef::Copy() { diff --git a/src/parser/tableref/table_function.cpp b/src/parser/tableref/table_function.cpp index c0d2a5df6347..adb6a9a3245e 100644 --- a/src/parser/tableref/table_function.cpp +++ b/src/parser/tableref/table_function.cpp @@ -13,12 +13,12 @@ string TableFunctionRef::ToString() const { return BaseToString(function->ToString(), column_name_alias); } -bool TableFunctionRef::Equals(const TableRef *other_p) const { +bool TableFunctionRef::Equals(const TableRef &other_p) const { if (!TableRef::Equals(other_p)) { return false; } - auto other = (TableFunctionRef *)other_p; - return function->Equals(other->function.get()); + auto &other = other_p.Cast(); + return function->Equals(*other.function); } void TableFunctionRef::Serialize(FieldWriter &writer) const { diff --git a/src/parser/transform/expression/transform_function.cpp b/src/parser/transform/expression/transform_function.cpp index 175b3a7981cc..1403bd2317d8 100644 --- a/src/parser/transform/expression/transform_function.cpp +++ b/src/parser/transform/expression/transform_function.cpp @@ -304,7 +304,7 @@ unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::P } auto arg_expr = children[0].get(); auto &order_by = order_bys->orders[0]; - if (arg_expr->Equals(order_by.expression.get())) { + if (arg_expr->Equals(*order_by.expression)) { auto sense = make_uniq(EnumUtil::ToChars(order_by.type)); auto nulls = make_uniq(EnumUtil::ToChars(order_by.null_order)); order_bys = nullptr; diff --git a/src/planner/bind_context.cpp b/src/planner/bind_context.cpp index 622b589f04c0..68b4de2a7457 100644 --- a/src/planner/bind_context.cpp +++ b/src/planner/bind_context.cpp @@ -161,7 +161,7 @@ unique_ptr BindContext::ExpandGeneratedColumn(const string &ta auto binding = GetBinding(table_name, error_message); D_ASSERT(binding); - auto &table_binding = (TableBinding &)*binding; + auto &table_binding = binding->Cast(); auto result = table_binding.ExpandGeneratedColumn(column_name); result->alias = column_name; return result; @@ -176,7 +176,7 @@ static bool ColumnIsGenerated(Binding &binding, column_t index) { if (binding.binding_type != BindingType::TABLE) { return false; } - auto &table_binding = (TableBinding &)binding; + auto &table_binding = binding.Cast(); auto catalog_entry = table_binding.GetStandardEntry(); if (!catalog_entry) { return false; @@ -483,15 +483,14 @@ void BindContext::AddSubquery(idx_t index, const string &alias, SubqueryRef &ref } void BindContext::AddEntryBinding(idx_t index, const string &alias, const vector &names, - const vector &types, StandardEntry *entry) { - D_ASSERT(entry); - AddBinding(alias, make_uniq(alias, types, names, index, *entry)); + const vector &types, StandardEntry &entry) { + AddBinding(alias, make_uniq(alias, types, names, index, entry)); } void BindContext::AddView(idx_t index, const string &alias, SubqueryRef &ref, BoundQueryNode &subquery, ViewCatalogEntry *view) { auto names = AliasColumnNames(alias, subquery.names, ref.column_name_alias); - AddEntryBinding(index, alias, names, subquery.types, (StandardEntry *)view); + AddEntryBinding(index, alias, names, subquery.types, view->Cast()); } void BindContext::AddSubquery(idx_t index, const string &alias, TableFunctionRef &ref, BoundQueryNode &subquery) { diff --git a/src/planner/binder.cpp b/src/planner/binder.cpp index 46fe383c79cc..306b0426e574 100644 --- a/src/planner/binder.cpp +++ b/src/planner/binder.cpp @@ -208,7 +208,7 @@ unique_ptr Binder::CreatePlan(BoundTableRef &ref) { root = CreatePlan(ref.Cast()); break; case TableReferenceType::PIVOT: - root = CreatePlan((BoundPivotRef &)ref); + root = CreatePlan(ref.Cast()); break; case TableReferenceType::INVALID: default: diff --git a/src/planner/binder/expression/bind_function_expression.cpp b/src/planner/binder/expression/bind_function_expression.cpp index a599b5c0fbec..299b0334e858 100644 --- a/src/planner/binder/expression/bind_function_expression.cpp +++ b/src/planner/binder/expression/bind_function_expression.cpp @@ -189,7 +189,7 @@ BindResult ExpressionBinder::BindLambdaFunction(FunctionExpression &function, Sc } // capture the (lambda) columns - auto &bound_lambda_expr = (BoundLambdaExpression &)*children.back(); + auto &bound_lambda_expr = children.back()->Cast(); CaptureLambdaColumns(bound_lambda_expr.captures, list_child_type, bound_lambda_expr.lambda_expr); FunctionBinder function_binder(context); diff --git a/src/planner/binder/expression/bind_lambda.cpp b/src/planner/binder/expression/bind_lambda.cpp index 9328dc192730..56bb87edc8c3 100644 --- a/src/planner/binder/expression/bind_lambda.cpp +++ b/src/planner/binder/expression/bind_lambda.cpp @@ -110,7 +110,7 @@ void ExpressionBinder::TransformCapturedLambdaColumn(unique_ptr &ori if (original->expression_class == ExpressionClass::BOUND_LAMBDA_REF) { // determine if this is the lambda parameter - auto &bound_lambda_ref = (BoundLambdaRefExpression &)*original; + auto &bound_lambda_ref = original->Cast(); auto alias = bound_lambda_ref.alias; if (lambda_bindings && bound_lambda_ref.lambda_index != lambda_bindings->size()) { diff --git a/src/planner/binder/expression/bind_star_expression.cpp b/src/planner/binder/expression/bind_star_expression.cpp index 8674467a3818..c0a25d937e6d 100644 --- a/src/planner/binder/expression/bind_star_expression.cpp +++ b/src/planner/binder/expression/bind_star_expression.cpp @@ -55,7 +55,7 @@ bool Binder::FindStarExpression(unique_ptr &expr, StarExpressi in_columns = true; if (*star) { // we can have multiple - if (!StarExpression::Equal(*star, ¤t_star)) { + if (!(*star)->Equals(current_star)) { throw BinderException( FormatError(*expr, "Multiple different STAR/COLUMNS in the same expression are not supported")); } diff --git a/src/planner/binder/expression/bind_subquery_expression.cpp b/src/planner/binder/expression/bind_subquery_expression.cpp index 14189b197367..814ef59b7c18 100644 --- a/src/planner/binder/expression/bind_subquery_expression.cpp +++ b/src/planner/binder/expression/bind_subquery_expression.cpp @@ -8,6 +8,9 @@ namespace duckdb { class BoundSubqueryNode : public QueryNode { +public: + static constexpr const QueryNodeType TYPE = QueryNodeType::BOUND_SUBQUERY_NODE; + public: BoundSubqueryNode(shared_ptr subquery_binder, unique_ptr bound_node, unique_ptr subquery) @@ -74,9 +77,9 @@ BindResult ExpressionBinder::BindExpression(SubqueryExpression &expr, idx_t dept } // both binding the child and binding the subquery was successful D_ASSERT(expr.subquery->node->type == QueryNodeType::BOUND_SUBQUERY_NODE); - auto bound_subquery = (BoundSubqueryNode *)expr.subquery->node.get(); - auto subquery_binder = std::move(bound_subquery->subquery_binder); - auto bound_node = std::move(bound_subquery->bound_node); + auto &bound_subquery = expr.subquery->node->Cast(); + auto subquery_binder = std::move(bound_subquery.subquery_binder); + auto bound_node = std::move(bound_subquery.bound_node); LogicalType return_type = expr.subquery_type == SubqueryType::SCALAR ? bound_node->types[0] : LogicalType(LogicalTypeId::BOOLEAN); if (return_type.id() == LogicalTypeId::UNKNOWN) { diff --git a/src/planner/binder/query_node/bind_select_node.cpp b/src/planner/binder/query_node/bind_select_node.cpp index b21c9534f0bb..93ef633fad22 100644 --- a/src/planner/binder/query_node/bind_select_node.cpp +++ b/src/planner/binder/query_node/bind_select_node.cpp @@ -113,7 +113,7 @@ void Binder::BindModifiers(OrderBinder &order_binder, QueryNode &statement, Boun unique_ptr bound_modifier; switch (mod->type) { case ResultModifierType::DISTINCT_MODIFIER: { - auto &distinct = (DistinctModifier &)*mod; + auto &distinct = mod->Cast(); auto bound_distinct = make_uniq(); bound_distinct->distinct_type = distinct.distinct_on_targets.empty() ? DistinctType::DISTINCT : DistinctType::DISTINCT_ON; @@ -133,7 +133,7 @@ void Binder::BindModifiers(OrderBinder &order_binder, QueryNode &statement, Boun break; } case ResultModifierType::ORDER_MODIFIER: { - auto &order = (OrderModifier &)*mod; + auto &order = mod->Cast(); auto bound_order = make_uniq(); auto &config = DBConfig::GetConfig(context); D_ASSERT(!order.orders.empty()); @@ -174,10 +174,10 @@ void Binder::BindModifiers(OrderBinder &order_binder, QueryNode &statement, Boun break; } case ResultModifierType::LIMIT_MODIFIER: - bound_modifier = BindLimit(order_binder, (LimitModifier &)*mod); + bound_modifier = BindLimit(order_binder, mod->Cast()); break; case ResultModifierType::LIMIT_PERCENT_MODIFIER: - bound_modifier = BindLimitPercent(order_binder, (LimitPercentModifier &)*mod); + bound_modifier = BindLimitPercent(order_binder, mod->Cast()); break; default: throw Exception("Unsupported result modifier"); @@ -203,7 +203,7 @@ void Binder::BindModifierTypes(BoundQueryNode &result, const vector for (auto &bound_mod : result.modifiers) { switch (bound_mod->type) { case ResultModifierType::DISTINCT_MODIFIER: { - auto &distinct = (BoundDistinctModifier &)*bound_mod; + auto &distinct = bound_mod->Cast(); D_ASSERT(!distinct.target_distincts.empty()); // set types of distinct targets for (auto &expr : distinct.target_distincts) { @@ -226,19 +226,19 @@ void Binder::BindModifierTypes(BoundQueryNode &result, const vector break; } case ResultModifierType::LIMIT_MODIFIER: { - auto &limit = (BoundLimitModifier &)*bound_mod; + auto &limit = bound_mod->Cast(); AssignReturnType(limit.limit, sql_types); AssignReturnType(limit.offset, sql_types); break; } case ResultModifierType::LIMIT_PERCENT_MODIFIER: { - auto &limit = (BoundLimitPercentModifier &)*bound_mod; + auto &limit = bound_mod->Cast(); AssignReturnType(limit.limit, sql_types); AssignReturnType(limit.offset, sql_types); break; } case ResultModifierType::ORDER_MODIFIER: { - auto &order = (BoundOrderModifier &)*bound_mod; + auto &order = bound_mod->Cast(); for (auto &order_node : order.orders) { auto &expr = order_node.expression; D_ASSERT(expr->type == ExpressionType::BOUND_COLUMN_REF); diff --git a/src/planner/binder/query_node/plan_query_node.cpp b/src/planner/binder/query_node/plan_query_node.cpp index 796c9a0a6af6..ff29a54829c9 100644 --- a/src/planner/binder/query_node/plan_query_node.cpp +++ b/src/planner/binder/query_node/plan_query_node.cpp @@ -13,14 +13,14 @@ unique_ptr Binder::VisitQueryNode(BoundQueryNode &node, unique_ for (auto &mod : node.modifiers) { switch (mod->type) { case ResultModifierType::DISTINCT_MODIFIER: { - auto &bound = (BoundDistinctModifier &)*mod; + auto &bound = mod->Cast(); auto distinct = make_uniq(std::move(bound.target_distincts), bound.distinct_type); distinct->AddChild(std::move(root)); root = std::move(distinct); break; } case ResultModifierType::ORDER_MODIFIER: { - auto &bound = (BoundOrderModifier &)*mod; + auto &bound = mod->Cast(); if (root->type == LogicalOperatorType::LOGICAL_DISTINCT) { auto &distinct = root->Cast(); if (distinct.distinct_type == DistinctType::DISTINCT_ON) { @@ -37,7 +37,7 @@ unique_ptr Binder::VisitQueryNode(BoundQueryNode &node, unique_ break; } case ResultModifierType::LIMIT_MODIFIER: { - auto &bound = (BoundLimitModifier &)*mod; + auto &bound = mod->Cast(); auto limit = make_uniq(bound.limit_val, bound.offset_val, std::move(bound.limit), std::move(bound.offset)); limit->AddChild(std::move(root)); @@ -45,7 +45,7 @@ unique_ptr Binder::VisitQueryNode(BoundQueryNode &node, unique_ break; } case ResultModifierType::LIMIT_PERCENT_MODIFIER: { - auto &bound = (BoundLimitPercentModifier &)*mod; + auto &bound = mod->Cast(); auto limit = make_uniq(bound.limit_percent, bound.offset_val, std::move(bound.limit), std::move(bound.offset)); limit->AddChild(std::move(root)); diff --git a/src/planner/binder/statement/bind_create.cpp b/src/planner/binder/statement/bind_create.cpp index 1f17254ecda8..0efb67a95d01 100644 --- a/src/planner/binder/statement/bind_create.cpp +++ b/src/planner/binder/statement/bind_create.cpp @@ -493,7 +493,7 @@ BoundStatement Binder::Bind(CreateStatement &stmt) { result.plan = make_uniq(LogicalOperatorType::LOGICAL_CREATE_SCHEMA, std::move(stmt.info)); break; case CatalogType::VIEW_ENTRY: { - auto &base = (CreateViewInfo &)*stmt.info; + auto &base = stmt.info->Cast(); // bind the schema auto &schema = BindCreateSchema(*stmt.info); BindCreateViewInfo(base); diff --git a/src/planner/binder/statement/bind_insert.cpp b/src/planner/binder/statement/bind_insert.cpp index 0534d80c791f..599b23aa9b6c 100644 --- a/src/planner/binder/statement/bind_insert.cpp +++ b/src/planner/binder/statement/bind_insert.cpp @@ -350,7 +350,7 @@ void Binder::BindOnConflictClause(LogicalInsert &insert, TableCatalogEntry &tabl // Get the column_ids we need to fetch later on from the conflicting tuples // of the original table, to execute the expressions D_ASSERT(original_binding->binding_type == BindingType::TABLE); - auto &table_binding = (TableBinding &)*original_binding; + auto &table_binding = original_binding->Cast(); insert.columns_to_fetch = table_binding.GetBoundColumnIds(); return; } @@ -376,7 +376,7 @@ void Binder::BindOnConflictClause(LogicalInsert &insert, TableCatalogEntry &tabl // Get the column_ids we need to fetch later on from the conflicting tuples // of the original table, to execute the expressions D_ASSERT(original_binding->binding_type == BindingType::TABLE); - auto &table_binding = (TableBinding &)*original_binding; + auto &table_binding = original_binding->Cast(); insert.columns_to_fetch = table_binding.GetBoundColumnIds(); // Replace the column bindings to refer to the child operator diff --git a/src/planner/binder/tableref/bind_pivot.cpp b/src/planner/binder/tableref/bind_pivot.cpp index 88426e2d3169..6a8791875dc6 100644 --- a/src/planner/binder/tableref/bind_pivot.cpp +++ b/src/planner/binder/tableref/bind_pivot.cpp @@ -84,7 +84,7 @@ static unique_ptr PivotInitialAggregate(PivotBindState &bind_state, if (entry->type != ExpressionType::COLUMN_REF) { throw InternalException("Unexpected child of pivot source - not a ColumnRef"); } - auto &columnref = (ColumnRefExpression &)*entry; + auto &columnref = entry->Cast(); if (handled_columns.find(columnref.GetColumnName()) == handled_columns.end()) { // not handled - add to grouping set subquery_stage1->groups.group_expressions.push_back( diff --git a/src/planner/binder/tableref/bind_table_function.cpp b/src/planner/binder/tableref/bind_table_function.cpp index bdd3d4f9e7a7..0e4dffc0d5b5 100644 --- a/src/planner/binder/tableref/bind_table_function.cpp +++ b/src/planner/binder/tableref/bind_table_function.cpp @@ -141,8 +141,8 @@ Binder::BindTableFunctionInternal(TableFunction &table_function, const string &f } bind_data = table_function.bind(context, bind_input, return_types, return_names); if (table_function.name == "pandas_scan" || table_function.name == "arrow_scan") { - auto arrow_bind = (PyTableFunctionData *)bind_data.get(); - arrow_bind->external_dependency = std::move(external_dependency); + auto &arrow_bind = bind_data->Cast(); + arrow_bind.external_dependency = std::move(external_dependency); } if (table_function.name == "read_csv" || table_function.name == "read_csv_auto") { auto &csv_bind = bind_data->Cast(); diff --git a/src/planner/bound_result_modifier.cpp b/src/planner/bound_result_modifier.cpp index 97d9cb9dd00a..c7c843c65c1c 100644 --- a/src/planner/bound_result_modifier.cpp +++ b/src/planner/bound_result_modifier.cpp @@ -29,7 +29,7 @@ bool BoundOrderByNode::Equals(const BoundOrderByNode &other) const { if (type != other.type || null_order != other.null_order) { return false; } - if (!expression->Equals(other.expression.get())) { + if (!expression->Equals(*other.expression)) { return false; } @@ -88,24 +88,29 @@ unique_ptr BoundOrderModifier::Copy() const { return result; } -bool BoundOrderModifier::Equals(const BoundOrderModifier *left, const BoundOrderModifier *right) { - if (left == right) { - return true; - } - if (!left || !right) { +bool BoundOrderModifier::Equals(const BoundOrderModifier &left, const BoundOrderModifier &right) { + if (left.orders.size() != right.orders.size()) { return false; } - if (left->orders.size() != right->orders.size()) { - return false; - } - for (idx_t i = 0; i < left->orders.size(); i++) { - if (!left->orders[i].Equals(right->orders[i])) { + for (idx_t i = 0; i < left.orders.size(); i++) { + if (!left.orders[i].Equals(right.orders[i])) { return false; } } return true; } +bool BoundOrderModifier::Equals(const unique_ptr &left, + const unique_ptr &right) { + if (left.get() == right.get()) { + return true; + } + if (!left || !right) { + return false; + } + return BoundOrderModifier::Equals(*left, *right); +} + BoundLimitModifier::BoundLimitModifier() : BoundResultModifier(ResultModifierType::LIMIT_MODIFIER) { } diff --git a/src/planner/expression.cpp b/src/planner/expression.cpp index 3aaeaef5bd9f..e6f3f3edce7b 100644 --- a/src/planner/expression.cpp +++ b/src/planner/expression.cpp @@ -6,6 +6,7 @@ #include "duckdb/planner/expression_iterator.hpp" #include "duckdb/storage/statistics/base_statistics.hpp" #include "duckdb/planner/expression/list.hpp" +#include "duckdb/parser/expression_util.hpp" namespace duckdb { @@ -165,4 +166,18 @@ unique_ptr Expression::Deserialize(Deserializer &source, PlanDeseria return result; } +bool Expression::Equals(const unique_ptr &left, const unique_ptr &right) { + if (left.get() == right.get()) { + return true; + } + if (!left || !right) { + return false; + } + return left->Equals(*right); +} + +bool Expression::ListEquals(const vector> &left, const vector> &right) { + return ExpressionUtil::ListEquals(left, right); +} + } // namespace duckdb diff --git a/src/planner/expression/bound_aggregate_expression.cpp b/src/planner/expression/bound_aggregate_expression.cpp index 9bf5708effb0..a3086cacbe5b 100644 --- a/src/planner/expression/bound_aggregate_expression.cpp +++ b/src/planner/expression/bound_aggregate_expression.cpp @@ -30,11 +30,11 @@ hash_t BoundAggregateExpression::Hash() const { return result; } -bool BoundAggregateExpression::Equals(const BaseExpression *other_p) const { +bool BoundAggregateExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); if (other.aggr_type != aggr_type) { return false; } @@ -44,18 +44,18 @@ bool BoundAggregateExpression::Equals(const BaseExpression *other_p) const { if (children.size() != other.children.size()) { return false; } - if (!Expression::Equals(other.filter.get(), filter.get())) { + if (!Expression::Equals(other.filter, filter)) { return false; } for (idx_t i = 0; i < children.size(); i++) { - if (!Expression::Equals(children[i].get(), other.children[i].get())) { + if (!Expression::Equals(*children[i], *other.children[i])) { return false; } } if (!FunctionData::Equals(bind_info.get(), other.bind_info.get())) { return false; } - if (!BoundOrderModifier::Equals(order_bys.get(), other.order_bys.get())) { + if (!BoundOrderModifier::Equals(order_bys, other.order_bys)) { return false; } return true; diff --git a/src/planner/expression/bound_between_expression.cpp b/src/planner/expression/bound_between_expression.cpp index 02cbd4ae95b7..b52fedb4a00c 100644 --- a/src/planner/expression/bound_between_expression.cpp +++ b/src/planner/expression/bound_between_expression.cpp @@ -15,18 +15,18 @@ string BoundBetweenExpression::ToString() const { return BetweenExpression::ToString(*this); } -bool BoundBetweenExpression::Equals(const BaseExpression *other_p) const { +bool BoundBetweenExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); - if (!Expression::Equals(input.get(), other.input.get())) { + auto &other = other_p.Cast(); + if (!Expression::Equals(*input, *other.input)) { return false; } - if (!Expression::Equals(lower.get(), other.lower.get())) { + if (!Expression::Equals(*lower, *other.lower)) { return false; } - if (!Expression::Equals(upper.get(), other.upper.get())) { + if (!Expression::Equals(*upper, *other.upper)) { return false; } return lower_inclusive == other.lower_inclusive && upper_inclusive == other.upper_inclusive; diff --git a/src/planner/expression/bound_case_expression.cpp b/src/planner/expression/bound_case_expression.cpp index 1ec7d8bb1831..9aaddfad2ad3 100644 --- a/src/planner/expression/bound_case_expression.cpp +++ b/src/planner/expression/bound_case_expression.cpp @@ -22,23 +22,23 @@ string BoundCaseExpression::ToString() const { return CaseExpression::ToString(*this); } -bool BoundCaseExpression::Equals(const BaseExpression *other_p) const { +bool BoundCaseExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); if (case_checks.size() != other.case_checks.size()) { return false; } for (idx_t i = 0; i < case_checks.size(); i++) { - if (!Expression::Equals(case_checks[i].when_expr.get(), other.case_checks[i].when_expr.get())) { + if (!Expression::Equals(*case_checks[i].when_expr, *other.case_checks[i].when_expr)) { return false; } - if (!Expression::Equals(case_checks[i].then_expr.get(), other.case_checks[i].then_expr.get())) { + if (!Expression::Equals(*case_checks[i].then_expr, *other.case_checks[i].then_expr)) { return false; } } - if (!Expression::Equals(else_expr.get(), other.else_expr.get())) { + if (!Expression::Equals(*else_expr, *other.else_expr)) { return false; } return true; diff --git a/src/planner/expression/bound_cast_expression.cpp b/src/planner/expression/bound_cast_expression.cpp index 79136f600af7..420274d26539 100644 --- a/src/planner/expression/bound_cast_expression.cpp +++ b/src/planner/expression/bound_cast_expression.cpp @@ -169,12 +169,12 @@ string BoundCastExpression::ToString() const { return (try_cast ? "TRY_CAST(" : "CAST(") + child->GetName() + " AS " + return_type.ToString() + ")"; } -bool BoundCastExpression::Equals(const BaseExpression *other_p) const { +bool BoundCastExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); - if (!Expression::Equals(child.get(), other.child.get())) { + auto &other = other_p.Cast(); + if (!Expression::Equals(*child, *other.child)) { return false; } if (try_cast != other.try_cast) { diff --git a/src/planner/expression/bound_columnref_expression.cpp b/src/planner/expression/bound_columnref_expression.cpp index 1fff1add333c..e974ccf363c9 100644 --- a/src/planner/expression/bound_columnref_expression.cpp +++ b/src/planner/expression/bound_columnref_expression.cpp @@ -27,11 +27,11 @@ hash_t BoundColumnRefExpression::Hash() const { return CombineHash(result, duckdb::Hash(depth)); } -bool BoundColumnRefExpression::Equals(const BaseExpression *other_p) const { +bool BoundColumnRefExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return other.binding == binding && other.depth == depth; } diff --git a/src/planner/expression/bound_comparison_expression.cpp b/src/planner/expression/bound_comparison_expression.cpp index d08c575a6c78..7b0c63f753fb 100644 --- a/src/planner/expression/bound_comparison_expression.cpp +++ b/src/planner/expression/bound_comparison_expression.cpp @@ -14,15 +14,15 @@ string BoundComparisonExpression::ToString() const { return ComparisonExpression::ToString(*this); } -bool BoundComparisonExpression::Equals(const BaseExpression *other_p) const { +bool BoundComparisonExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); - if (!Expression::Equals(left.get(), other.left.get())) { + auto &other = other_p.Cast(); + if (!Expression::Equals(*left, *other.left)) { return false; } - if (!Expression::Equals(right.get(), other.right.get())) { + if (!Expression::Equals(*right, *other.right)) { return false; } return true; diff --git a/src/planner/expression/bound_conjunction_expression.cpp b/src/planner/expression/bound_conjunction_expression.cpp index 10d832bbbe2a..83cb0b173f90 100644 --- a/src/planner/expression/bound_conjunction_expression.cpp +++ b/src/planner/expression/bound_conjunction_expression.cpp @@ -20,11 +20,11 @@ string BoundConjunctionExpression::ToString() const { return ConjunctionExpression::ToString(*this); } -bool BoundConjunctionExpression::Equals(const BaseExpression *other_p) const { +bool BoundConjunctionExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return ExpressionUtil::SetEquals(children, other.children); } diff --git a/src/planner/expression/bound_constant_expression.cpp b/src/planner/expression/bound_constant_expression.cpp index 449ab0983862..e3b8b3aa9b74 100644 --- a/src/planner/expression/bound_constant_expression.cpp +++ b/src/planner/expression/bound_constant_expression.cpp @@ -14,11 +14,11 @@ string BoundConstantExpression::ToString() const { return value.ToSQLString(); } -bool BoundConstantExpression::Equals(const BaseExpression *other_p) const { +bool BoundConstantExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return value.type() == other.value.type() && !ValueOperations::DistinctFrom(value, other.value); } diff --git a/src/planner/expression/bound_expression.cpp b/src/planner/expression/bound_expression.cpp index 65584990bd37..74aa5fec8aa8 100644 --- a/src/planner/expression/bound_expression.cpp +++ b/src/planner/expression/bound_expression.cpp @@ -22,7 +22,7 @@ string BoundExpression::ToString() const { return expr->ToString(); } -bool BoundExpression::Equals(const BaseExpression *other) const { +bool BoundExpression::Equals(const BaseExpression &other) const { return false; } hash_t BoundExpression::Hash() const { diff --git a/src/planner/expression/bound_function_expression.cpp b/src/planner/expression/bound_function_expression.cpp index 57cd432aed82..fe4f985f781c 100644 --- a/src/planner/expression/bound_function_expression.cpp +++ b/src/planner/expression/bound_function_expression.cpp @@ -2,7 +2,6 @@ #include "duckdb/parser/expression/function_expression.hpp" #include "duckdb/catalog/catalog_entry/scalar_function_catalog_entry.hpp" #include "duckdb/common/types/hash.hpp" -#include "duckdb/parser/expression_util.hpp" #include "duckdb/function/function_serialization.hpp" namespace duckdb { @@ -39,15 +38,15 @@ hash_t BoundFunctionExpression::Hash() const { return CombineHash(result, function.Hash()); } -bool BoundFunctionExpression::Equals(const BaseExpression *other_p) const { +bool BoundFunctionExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); if (other.function != function) { return false; } - if (!ExpressionUtil::ListEquals(children, other.children)) { + if (!Expression::ListEquals(children, other.children)) { return false; } if (!FunctionData::Equals(bind_info.get(), other.bind_info.get())) { diff --git a/src/planner/expression/bound_lambda_expression.cpp b/src/planner/expression/bound_lambda_expression.cpp index 7d99aef21426..a0af1c7f07e9 100644 --- a/src/planner/expression/bound_lambda_expression.cpp +++ b/src/planner/expression/bound_lambda_expression.cpp @@ -1,6 +1,5 @@ #include "duckdb/planner/expression/bound_lambda_expression.hpp" #include "duckdb/common/string_util.hpp" -#include "duckdb/parser/expression_util.hpp" namespace duckdb { @@ -14,15 +13,15 @@ string BoundLambdaExpression::ToString() const { return lambda_expr->ToString(); } -bool BoundLambdaExpression::Equals(const BaseExpression *other_p) const { +bool BoundLambdaExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); - if (!Expression::Equals(lambda_expr.get(), other.lambda_expr.get())) { + auto &other = other_p.Cast(); + if (!Expression::Equals(*lambda_expr, *other.lambda_expr)) { return false; } - if (!ExpressionUtil::ListEquals(captures, other.captures)) { + if (!Expression::ListEquals(captures, other.captures)) { return false; } if (parameter_count != other.parameter_count) { diff --git a/src/planner/expression/bound_lambdaref_expression.cpp b/src/planner/expression/bound_lambdaref_expression.cpp index 90dd41c16548..a95d7284a1c9 100644 --- a/src/planner/expression/bound_lambdaref_expression.cpp +++ b/src/planner/expression/bound_lambdaref_expression.cpp @@ -30,11 +30,11 @@ hash_t BoundLambdaRefExpression::Hash() const { return CombineHash(result, duckdb::Hash(depth)); } -bool BoundLambdaRefExpression::Equals(const BaseExpression *other_p) const { +bool BoundLambdaRefExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return other.binding == binding && other.lambda_index == lambda_index && other.depth == depth; } diff --git a/src/planner/expression/bound_operator_expression.cpp b/src/planner/expression/bound_operator_expression.cpp index 0edcb711da28..077c9a112c7c 100644 --- a/src/planner/expression/bound_operator_expression.cpp +++ b/src/planner/expression/bound_operator_expression.cpp @@ -1,6 +1,5 @@ #include "duckdb/planner/expression/bound_operator_expression.hpp" #include "duckdb/common/string_util.hpp" -#include "duckdb/parser/expression_util.hpp" #include "duckdb/parser/expression/operator_expression.hpp" #include "duckdb/common/field_writer.hpp" @@ -14,12 +13,12 @@ string BoundOperatorExpression::ToString() const { return OperatorExpression::ToString(*this); } -bool BoundOperatorExpression::Equals(const BaseExpression *other_p) const { +bool BoundOperatorExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); - if (!ExpressionUtil::ListEquals(children, other.children)) { + auto &other = other_p.Cast(); + if (!Expression::ListEquals(children, other.children)) { return false; } return true; diff --git a/src/planner/expression/bound_parameter_expression.cpp b/src/planner/expression/bound_parameter_expression.cpp index 951529cab871..d21217b1684a 100644 --- a/src/planner/expression/bound_parameter_expression.cpp +++ b/src/planner/expression/bound_parameter_expression.cpp @@ -43,11 +43,11 @@ string BoundParameterExpression::ToString() const { return "$" + to_string(parameter_nr); } -bool BoundParameterExpression::Equals(const BaseExpression *other_p) const { +bool BoundParameterExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return parameter_nr == other.parameter_nr; } diff --git a/src/planner/expression/bound_reference_expression.cpp b/src/planner/expression/bound_reference_expression.cpp index 4b41a0a6ea8a..ab9a14ecfc5b 100644 --- a/src/planner/expression/bound_reference_expression.cpp +++ b/src/planner/expression/bound_reference_expression.cpp @@ -28,11 +28,11 @@ string BoundReferenceExpression::ToString() const { return "#" + to_string(index); } -bool BoundReferenceExpression::Equals(const BaseExpression *other_p) const { +bool BoundReferenceExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); return other.index == index; } diff --git a/src/planner/expression/bound_subquery_expression.cpp b/src/planner/expression/bound_subquery_expression.cpp index 67907e617cff..4be88364d9cb 100644 --- a/src/planner/expression/bound_subquery_expression.cpp +++ b/src/planner/expression/bound_subquery_expression.cpp @@ -12,7 +12,7 @@ string BoundSubqueryExpression::ToString() const { return "SUBQUERY"; } -bool BoundSubqueryExpression::Equals(const BaseExpression *other_p) const { +bool BoundSubqueryExpression::Equals(const BaseExpression &other_p) const { // equality between bound subqueries not implemented currently return false; } diff --git a/src/planner/expression/bound_unnest_expression.cpp b/src/planner/expression/bound_unnest_expression.cpp index 54947561b213..a5ad04438628 100644 --- a/src/planner/expression/bound_unnest_expression.cpp +++ b/src/planner/expression/bound_unnest_expression.cpp @@ -23,12 +23,12 @@ hash_t BoundUnnestExpression::Hash() const { return CombineHash(result, duckdb::Hash("unnest")); } -bool BoundUnnestExpression::Equals(const BaseExpression *other_p) const { +bool BoundUnnestExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); - if (!Expression::Equals(child.get(), other.child.get())) { + auto &other = other_p.Cast(); + if (!Expression::Equals(*child, *other.child)) { return false; } return true; diff --git a/src/planner/expression/bound_window_expression.cpp b/src/planner/expression/bound_window_expression.cpp index 640f15245278..98ee565628e8 100644 --- a/src/planner/expression/bound_window_expression.cpp +++ b/src/planner/expression/bound_window_expression.cpp @@ -21,11 +21,11 @@ string BoundWindowExpression::ToString() const { function_name); } -bool BoundWindowExpression::Equals(const BaseExpression *other_p) const { +bool BoundWindowExpression::Equals(const BaseExpression &other_p) const { if (!Expression::Equals(other_p)) { return false; } - auto &other = other_p->Cast(); + auto &other = other_p.Cast(); if (ignore_nulls != other.ignore_nulls) { return false; @@ -34,24 +34,17 @@ bool BoundWindowExpression::Equals(const BaseExpression *other_p) const { return false; } // check if the child expressions are equivalent - if (other.children.size() != children.size()) { + if (!Expression::ListEquals(children, other.children)) { return false; } - for (idx_t i = 0; i < children.size(); i++) { - if (!Expression::Equals(children[i].get(), other.children[i].get())) { - return false; - } - } // check if the filter expressions are equivalent - if (!Expression::Equals(filter_expr.get(), other.filter_expr.get())) { + if (!Expression::Equals(filter_expr, other.filter_expr)) { return false; } // check if the framing expressions are equivalent - if (!Expression::Equals(start_expr.get(), other.start_expr.get()) || - !Expression::Equals(end_expr.get(), other.end_expr.get()) || - !Expression::Equals(offset_expr.get(), other.offset_expr.get()) || - !Expression::Equals(default_expr.get(), other.default_expr.get())) { + if (!Expression::Equals(start_expr, other.start_expr) || !Expression::Equals(end_expr, other.end_expr) || + !Expression::Equals(offset_expr, other.offset_expr) || !Expression::Equals(default_expr, other.default_expr)) { return false; } @@ -60,14 +53,9 @@ bool BoundWindowExpression::Equals(const BaseExpression *other_p) const { bool BoundWindowExpression::KeysAreCompatible(const BoundWindowExpression &other) const { // check if the partitions are equivalent - if (partitions.size() != other.partitions.size()) { + if (!Expression::ListEquals(partitions, other.partitions)) { return false; } - for (idx_t i = 0; i < partitions.size(); i++) { - if (!Expression::Equals(partitions[i].get(), other.partitions[i].get())) { - return false; - } - } // check if the orderings are equivalent if (orders.size() != other.orders.size()) { return false; @@ -76,8 +64,7 @@ bool BoundWindowExpression::KeysAreCompatible(const BoundWindowExpression &other if (orders[i].type != other.orders[i].type) { return false; } - if (!BaseExpression::Equals((BaseExpression *)orders[i].expression.get(), - (BaseExpression *)other.orders[i].expression.get())) { + if (!Expression::Equals(*orders[i].expression, *other.orders[i].expression)) { return false; } } diff --git a/src/planner/expression_binder/base_select_binder.cpp b/src/planner/expression_binder/base_select_binder.cpp index 022c74083f13..25af231d2cbb 100644 --- a/src/planner/expression_binder/base_select_binder.cpp +++ b/src/planner/expression_binder/base_select_binder.cpp @@ -62,8 +62,8 @@ idx_t BaseSelectBinder::TryBindGroup(ParsedExpression &expr, idx_t depth) { } #ifdef DEBUG for (auto entry : info.map) { - D_ASSERT(!entry.first.get().Equals(&expr)); - D_ASSERT(!expr.Equals(&entry.first.get())); + D_ASSERT(!entry.first.get().Equals(expr)); + D_ASSERT(!expr.Equals(entry.first.get())); } #endif return DConstants::INVALID_INDEX; diff --git a/src/planner/expression_iterator.cpp b/src/planner/expression_iterator.cpp index 635e3e7668b8..fe06a3d66dc2 100644 --- a/src/planner/expression_iterator.cpp +++ b/src/planner/expression_iterator.cpp @@ -230,12 +230,12 @@ void ExpressionIterator::EnumerateQueryNodeChildren(BoundQueryNode &node, for (idx_t i = 0; i < node.modifiers.size(); i++) { switch (node.modifiers[i]->type) { case ResultModifierType::DISTINCT_MODIFIER: - for (auto &expr : ((BoundDistinctModifier &)*node.modifiers[i]).target_distincts) { + for (auto &expr : node.modifiers[i]->Cast().target_distincts) { EnumerateExpression(expr, callback); } break; case ResultModifierType::ORDER_MODIFIER: - for (auto &order : ((BoundOrderModifier &)*node.modifiers[i]).orders) { + for (auto &order : node.modifiers[i]->Cast().orders) { EnumerateExpression(order.expression, callback); } break; diff --git a/src/planner/filter/conjunction_filter.cpp b/src/planner/filter/conjunction_filter.cpp index f8324d822985..d0ceca8dffe5 100644 --- a/src/planner/filter/conjunction_filter.cpp +++ b/src/planner/filter/conjunction_filter.cpp @@ -35,7 +35,7 @@ bool ConjunctionOrFilter::Equals(const TableFilter &other_p) const { if (!ConjunctionFilter::Equals(other_p)) { return false; } - auto &other = (ConjunctionOrFilter &)other_p; + auto &other = other_p.Cast(); if (other.child_filters.size() != child_filters.size()) { return false; } @@ -90,7 +90,7 @@ bool ConjunctionAndFilter::Equals(const TableFilter &other_p) const { if (!ConjunctionFilter::Equals(other_p)) { return false; } - auto &other = (ConjunctionAndFilter &)other_p; + auto &other = other_p.Cast(); if (other.child_filters.size() != child_filters.size()) { return false; } diff --git a/src/planner/filter/constant_filter.cpp b/src/planner/filter/constant_filter.cpp index 155b2790979a..ca18e19e5f92 100644 --- a/src/planner/filter/constant_filter.cpp +++ b/src/planner/filter/constant_filter.cpp @@ -39,7 +39,7 @@ bool ConstantFilter::Equals(const TableFilter &other_p) const { if (!TableFilter::Equals(other_p)) { return false; } - auto &other = (ConstantFilter &)other_p; + auto &other = other_p.Cast(); return other.comparison_type == comparison_type && other.constant == constant; } diff --git a/src/planner/logical_operator.cpp b/src/planner/logical_operator.cpp index ac22f9b3e8c7..d1d3462336b0 100644 --- a/src/planner/logical_operator.cpp +++ b/src/planner/logical_operator.cpp @@ -109,13 +109,12 @@ void LogicalOperator::Verify(ClientContext &context) { // copy should be identical to original D_ASSERT(expressions[expr_idx]->ToString() == copy->ToString()); D_ASSERT(original_hash == copy_hash); - D_ASSERT(Expression::Equals(expressions[expr_idx].get(), copy.get())); + D_ASSERT(Expression::Equals(expressions[expr_idx], copy)); - D_ASSERT(!Expression::Equals(expressions[expr_idx].get(), nullptr)); for (idx_t other_idx = 0; other_idx < expr_idx; other_idx++) { // comparison with other expressions auto other_hash = expressions[other_idx]->Hash(); - bool expr_equal = Expression::Equals(expressions[expr_idx].get(), expressions[other_idx].get()); + bool expr_equal = Expression::Equals(expressions[expr_idx], expressions[other_idx]); if (original_hash != other_hash) { // if the hashes are not equal the expressions should not be equal either D_ASSERT(!expr_equal); @@ -144,7 +143,7 @@ void LogicalOperator::Verify(ClientContext &context) { auto deserialized_expression = Expression::Deserialize(deserializer, state); // FIXME: expressions might not be equal yet because of statistics propagation continue; - D_ASSERT(Expression::Equals(expressions[expr_idx].get(), deserialized_expression.get())); + D_ASSERT(Expression::Equals(expressions[expr_idx], deserialized_expression)); D_ASSERT(expressions[expr_idx]->Hash() == deserialized_expression->Hash()); } D_ASSERT(!ToString().empty()); diff --git a/src/planner/logical_operator_visitor.cpp b/src/planner/logical_operator_visitor.cpp index ba2c95442f8e..c36aadec5af5 100644 --- a/src/planner/logical_operator_visitor.cpp +++ b/src/planner/logical_operator_visitor.cpp @@ -97,7 +97,7 @@ void LogicalOperatorVisitor::EnumerateExpressions(LogicalOperator &op, break; } case LogicalOperatorType::LOGICAL_LIMIT_PERCENT: { - auto &limit = (LogicalLimitPercent &)op; + auto &limit = op.Cast(); if (limit.limit) { callback(&limit.limit); } diff --git a/src/planner/table_filter.cpp b/src/planner/table_filter.cpp index faea27519436..5b40ec4ae4fd 100644 --- a/src/planner/table_filter.cpp +++ b/src/planner/table_filter.cpp @@ -14,7 +14,7 @@ void TableFilterSet::PushFilter(idx_t column_index, unique_ptr filt } else { // there is already a filter: AND it together if (entry->second->filter_type == TableFilterType::CONJUNCTION_AND) { - auto &and_filter = (ConjunctionAndFilter &)*entry->second; + auto &and_filter = entry->second->Cast(); and_filter.child_filters.push_back(std::move(filter)); } else { auto and_filter = make_uniq(); diff --git a/src/verification/prepared_statement_verifier.cpp b/src/verification/prepared_statement_verifier.cpp index 50db38cdfe1c..10b616209ba2 100644 --- a/src/verification/prepared_statement_verifier.cpp +++ b/src/verification/prepared_statement_verifier.cpp @@ -50,7 +50,7 @@ void PreparedStatementVerifier::ConvertConstants(unique_ptr &c // check if the value already exists idx_t index = values.size(); for (idx_t v_idx = 0; v_idx < values.size(); v_idx++) { - if (values[v_idx]->Equals(child.get())) { + if (values[v_idx]->Equals(*child)) { // duplicate value! refer to the original value index = v_idx; break; diff --git a/src/verification/statement_verifier.cpp b/src/verification/statement_verifier.cpp index 982dea75846e..4330c746f32c 100644 --- a/src/verification/statement_verifier.cpp +++ b/src/verification/statement_verifier.cpp @@ -57,7 +57,7 @@ void StatementVerifier::CheckExpressions(const StatementVerifier &other) const { // Check equality if (other.RequireEquality()) { - D_ASSERT(statement->Equals(other.statement.get())); + D_ASSERT(statement->Equals(*other.statement)); } #ifdef DEBUG @@ -66,7 +66,6 @@ void StatementVerifier::CheckExpressions(const StatementVerifier &other) const { const auto expr_count = select_list.size(); if (other.RequireEquality()) { for (idx_t i = 0; i < expr_count; i++) { - D_ASSERT(!select_list[i]->Equals(nullptr)); // Run the ToString, to verify that it doesn't crash select_list[i]->ToString(); @@ -75,7 +74,7 @@ void StatementVerifier::CheckExpressions(const StatementVerifier &other) const { } // Check that the expressions are equivalent - D_ASSERT(select_list[i]->Equals(other.select_list[i].get())); + D_ASSERT(select_list[i]->Equals(*other.select_list[i])); // Check that the hashes are equivalent too D_ASSERT(select_list[i]->Hash() == other.select_list[i]->Hash()); @@ -96,7 +95,7 @@ void StatementVerifier::CheckExpressions() const { auto hash2 = select_list[inner_idx]->Hash(); if (hash != hash2) { // if the hashes are not equivalent, the expressions should not be equivalent - D_ASSERT(!select_list[outer_idx]->Equals(select_list[inner_idx].get())); + D_ASSERT(!select_list[outer_idx]->Equals(*select_list[inner_idx])); } } } diff --git a/tools/pythonpkg/duckdb_python.cpp b/tools/pythonpkg/duckdb_python.cpp index aa6c8cce6cf0..afd8e441bbca 100644 --- a/tools/pythonpkg/duckdb_python.cpp +++ b/tools/pythonpkg/duckdb_python.cpp @@ -269,8 +269,7 @@ static void InitializeConnectionMethods(py::module_ &m) { py::arg("connection") = py::none()); } -PYBIND11_MODULE(DUCKDB_PYTHON_LIB_NAME, m) { - +PYBIND11_MODULE(DUCKDB_PYTHON_LIB_NAME, m) { // NOLINT py::enum_(m, "ExplainType") .value("STANDARD", duckdb::ExplainType::EXPLAIN_STANDARD) .value("ANALYZE", duckdb::ExplainType::EXPLAIN_ANALYZE) From 518823248ab59080b1c4ce38577ce3a0c7e537f0 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Sat, 20 May 2023 13:19:59 +0200 Subject: [PATCH 02/28] Remove clang-tidy rule again for now --- .clang-tidy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 1f0c42bc8937..0ddcce906883 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '-*,clang-diagnostic-*,bugprone-*,performance-*,google-explicit-constructor,google-build-using-namespace,google-runtime-int,misc-definitions-in-headers,modernize-use-nullptr,modernize-use-override,-bugprone-macro-parentheses,readability-braces-around-statements,-bugprone-branch-clone,readability-identifier-naming,hicpp-exception-baseclass,misc-throw-by-value-catch-by-reference,-bugprone-signed-char-misuse,-bugprone-misplaced-widening-cast,-bugprone-sizeof-expression,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,google-global-names-in-headers,llvm-header-guard,misc-definitions-in-headers,modernize-use-emplace,modernize-use-bool-literals,-performance-inefficient-string-concatenation,-performance-no-int-to-ptr,readability-container-size-empty,cppcoreguidelines-pro-type-cstyle-cast' +Checks: '-*,clang-diagnostic-*,bugprone-*,performance-*,google-explicit-constructor,google-build-using-namespace,google-runtime-int,misc-definitions-in-headers,modernize-use-nullptr,modernize-use-override,-bugprone-macro-parentheses,readability-braces-around-statements,-bugprone-branch-clone,readability-identifier-naming,hicpp-exception-baseclass,misc-throw-by-value-catch-by-reference,-bugprone-signed-char-misuse,-bugprone-misplaced-widening-cast,-bugprone-sizeof-expression,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,google-global-names-in-headers,llvm-header-guard,misc-definitions-in-headers,modernize-use-emplace,modernize-use-bool-literals,-performance-inefficient-string-concatenation,-performance-no-int-to-ptr,readability-container-size-empty' WarningsAsErrors: '*' HeaderFilterRegex: '.*^(re2.h)' AnalyzeTemporaryDtors: false From 6b9112fe96eb8c9e3493022730ae9b0ee1e4a0d3 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Sat, 20 May 2023 13:20:37 +0200 Subject: [PATCH 03/28] Revert Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6b572445cf95..7fe45812a670 100644 --- a/Makefile +++ b/Makefile @@ -263,7 +263,7 @@ amaldebug: tidy-check: mkdir -p ./build/tidy && \ cd build/tidy && \ - cmake -DCLANG_TIDY=1 -DDISABLE_UNITY=1 -DBUILD_PARQUET_EXTENSION=TRUE ${EXTRA_CMAKE_VARIABLES} -DBUILD_SHELL=0 ../.. && \ + cmake -DCLANG_TIDY=1 -DDISABLE_UNITY=1 -DBUILD_PARQUET_EXTENSION=TRUE -DBUILD_PYTHON_PKG=TRUE ${EXTRA_CMAKE_VARIABLES} -DBUILD_SHELL=0 ../.. && \ python3 ../../scripts/run-clang-tidy.py -quiet ${TIDY_THREAD_PARAMETER} ${TIDY_BINARY_PARAMETER} tidy-fix: From 19f65c55d9155e378966063d2e59bcae8dbb50f0 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Sun, 21 May 2023 18:54:07 +0200 Subject: [PATCH 04/28] Transformer - avoid using C-style casts, and move to using references and optional_ptr --- .clang-tidy | 2 +- src/include/duckdb/parser/transformer.hpp | 183 ++++++++++-------- src/optimizer/regex_range_filter.cpp | 6 +- src/parser/result_modifier.cpp | 4 +- .../expression/transform_array_access.cpp | 14 +- .../expression/transform_bool_expr.cpp | 8 +- .../expression/transform_boolean_test.cpp | 8 +- .../transform/expression/transform_case.cpp | 18 +- .../transform/expression/transform_cast.cpp | 14 +- .../expression/transform_coalesce.cpp | 8 +- .../expression/transform_columnref.cpp | 41 ++-- .../expression/transform_constant.cpp | 4 +- .../expression/transform_expression.cpp | 89 ++++----- .../expression/transform_function.cpp | 142 +++++++------- .../transform_grouping_function.cpp | 8 +- .../expression/transform_interval.cpp | 14 +- .../expression/transform_is_null.cpp | 9 +- .../transform/expression/transform_lambda.cpp | 11 +- .../expression/transform_operator.cpp | 57 +++--- .../expression/transform_param_ref.cpp | 27 ++- .../transform_positional_reference.cpp | 8 +- .../expression/transform_subquery.cpp | 19 +- .../transform/helpers/transform_cte.cpp | 59 +++--- .../statement/transform_alter_sequence.cpp | 25 ++- .../statement/transform_alter_table.cpp | 30 ++- .../transform/statement/transform_attach.cpp | 17 +- .../transform/statement/transform_call.cpp | 7 +- .../statement/transform_checkpoint.cpp | 10 +- .../transform/statement/transform_copy.cpp | 45 +++-- .../statement/transform_create_database.cpp | 7 +- .../statement/transform_create_function.cpp | 30 ++- .../statement/transform_create_index.cpp | 22 +-- .../statement/transform_create_schema.cpp | 18 +- .../statement/transform_create_sequence.cpp | 18 +- .../statement/transform_create_table.cpp | 54 +++--- .../statement/transform_create_table_as.cpp | 18 +- .../statement/transform_create_type.cpp | 21 +- .../statement/transform_create_view.cpp | 31 ++- .../transform/statement/transform_delete.cpp | 24 ++- .../transform/statement/transform_detach.cpp | 7 +- .../transform/statement/transform_drop.cpp | 42 ++-- .../transform/statement/transform_explain.cpp | 12 +- .../transform/statement/transform_export.cpp | 11 +- .../transform/statement/transform_import.cpp | 5 +- .../transform/statement/transform_insert.cpp | 45 ++--- .../transform/statement/transform_load.cpp | 9 +- .../statement/transform_pivot_stmt.cpp | 14 +- .../transform/statement/transform_pragma.cpp | 16 +- .../transform/statement/transform_prepare.cpp | 30 ++- .../transform/statement/transform_rename.cpp | 26 ++- .../transform/statement/transform_select.cpp | 21 +- .../statement/transform_select_node.cpp | 68 +++---- .../transform/statement/transform_set.cpp | 37 ++-- .../transform/statement/transform_show.cpp | 12 +- .../statement/transform_show_select.cpp | 9 +- .../statement/transform_transaction.cpp | 8 +- .../transform/statement/transform_update.cpp | 23 +-- .../transform/statement/transform_use.cpp | 5 +- .../transform/statement/transform_vacuum.cpp | 16 +- .../tableref/transform_base_tableref.cpp | 36 ++-- .../transform/tableref/transform_from.cpp | 10 +- .../transform/tableref/transform_join.cpp | 22 +-- .../transform/tableref/transform_pivot.cpp | 54 +++--- .../transform/tableref/transform_subquery.cpp | 10 +- .../tableref/transform_table_function.cpp | 24 +-- .../transform/tableref/transform_tableref.cpp | 16 +- src/parser/transformer.cpp | 92 ++++----- src/planner/bind_context.cpp | 1 + 68 files changed, 863 insertions(+), 948 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 0ddcce906883..1f0c42bc8937 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,4 +1,4 @@ -Checks: '-*,clang-diagnostic-*,bugprone-*,performance-*,google-explicit-constructor,google-build-using-namespace,google-runtime-int,misc-definitions-in-headers,modernize-use-nullptr,modernize-use-override,-bugprone-macro-parentheses,readability-braces-around-statements,-bugprone-branch-clone,readability-identifier-naming,hicpp-exception-baseclass,misc-throw-by-value-catch-by-reference,-bugprone-signed-char-misuse,-bugprone-misplaced-widening-cast,-bugprone-sizeof-expression,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,google-global-names-in-headers,llvm-header-guard,misc-definitions-in-headers,modernize-use-emplace,modernize-use-bool-literals,-performance-inefficient-string-concatenation,-performance-no-int-to-ptr,readability-container-size-empty' +Checks: '-*,clang-diagnostic-*,bugprone-*,performance-*,google-explicit-constructor,google-build-using-namespace,google-runtime-int,misc-definitions-in-headers,modernize-use-nullptr,modernize-use-override,-bugprone-macro-parentheses,readability-braces-around-statements,-bugprone-branch-clone,readability-identifier-naming,hicpp-exception-baseclass,misc-throw-by-value-catch-by-reference,-bugprone-signed-char-misuse,-bugprone-misplaced-widening-cast,-bugprone-sizeof-expression,-bugprone-narrowing-conversions,-bugprone-easily-swappable-parameters,google-global-names-in-headers,llvm-header-guard,misc-definitions-in-headers,modernize-use-emplace,modernize-use-bool-literals,-performance-inefficient-string-concatenation,-performance-no-int-to-ptr,readability-container-size-empty,cppcoreguidelines-pro-type-cstyle-cast' WarningsAsErrors: '*' HeaderFilterRegex: '.*^(re2.h)' AnalyzeTemporaryDtors: false diff --git a/src/include/duckdb/parser/transformer.hpp b/src/include/duckdb/parser/transformer.hpp index 173609542ebd..be5f17c4114a 100644 --- a/src/include/duckdb/parser/transformer.hpp +++ b/src/include/duckdb/parser/transformer.hpp @@ -97,42 +97,44 @@ class Transformer { private: //! Transforms a Postgres statement into a single SQL statement - unique_ptr TransformStatement(duckdb_libpgquery::PGNode *stmt); + unique_ptr TransformStatement(duckdb_libpgquery::PGNode &stmt); //! Transforms a Postgres statement into a single SQL statement - unique_ptr TransformStatementInternal(duckdb_libpgquery::PGNode *stmt); + unique_ptr TransformStatementInternal(duckdb_libpgquery::PGNode &stmt); //===--------------------------------------------------------------------===// // Statement transformation //===--------------------------------------------------------------------===// //! Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a SelectStatement - unique_ptr TransformSelect(duckdb_libpgquery::PGNode *node, bool isSelect = true); + unique_ptr TransformSelect(optional_ptr node, bool is_select = true); + //! Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a SelectStatement + unique_ptr TransformSelect(duckdb_libpgquery::PGSelectStmt &select, bool is_select = true); //! Transform a Postgres T_AlterStmt node into a AlterStatement - unique_ptr TransformAlter(duckdb_libpgquery::PGNode *node); + unique_ptr TransformAlter(duckdb_libpgquery::PGAlterTableStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGRenameStmt node into a RenameStatement - unique_ptr TransformRename(duckdb_libpgquery::PGNode *node); + unique_ptr TransformRename(duckdb_libpgquery::PGRenameStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement - unique_ptr TransformCreateTable(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateTable(duckdb_libpgquery::PGCreateStmt &node); //! Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement - unique_ptr TransformCreateTableAs(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateTableAs(duckdb_libpgquery::PGCreateTableAsStmt &stmt); //! Transform a Postgres node into a CreateStatement - unique_ptr TransformCreateSchema(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateSchema(duckdb_libpgquery::PGCreateSchemaStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGCreateSeqStmt node into a CreateStatement - unique_ptr TransformCreateSequence(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateSequence(duckdb_libpgquery::PGCreateSeqStmt &node); //! Transform a Postgres duckdb_libpgquery::T_PGViewStmt node into a CreateStatement - unique_ptr TransformCreateView(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateView(duckdb_libpgquery::PGViewStmt &node); //! Transform a Postgres duckdb_libpgquery::T_PGIndexStmt node into CreateStatement - unique_ptr TransformCreateIndex(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateIndex(duckdb_libpgquery::PGIndexStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGCreateFunctionStmt node into CreateStatement - unique_ptr TransformCreateFunction(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateFunction(duckdb_libpgquery::PGCreateFunctionStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement - unique_ptr TransformCreateType(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateType(duckdb_libpgquery::PGCreateTypeStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGCreateDatabaseStmt node into a CreateStatement - unique_ptr TransformCreateDatabase(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCreateDatabase(duckdb_libpgquery::PGCreateDatabaseStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGAlterSeqStmt node into CreateStatement - unique_ptr TransformAlterSequence(duckdb_libpgquery::PGNode *node); + unique_ptr TransformAlterSequence(duckdb_libpgquery::PGAlterSeqStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGDropStmt node into a Drop[Table,Schema]Statement - unique_ptr TransformDrop(duckdb_libpgquery::PGNode *node); + unique_ptr TransformDrop(duckdb_libpgquery::PGDropStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGInsertStmt node into a InsertStatement - unique_ptr TransformInsert(duckdb_libpgquery::PGNode *node); + unique_ptr TransformInsert(duckdb_libpgquery::PGInsertStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGOnConflictClause node into a OnConflictInfo unique_ptr TransformOnConflictClause(duckdb_libpgquery::PGOnConflictClause *node, @@ -141,99 +143,100 @@ class Transformer { unique_ptr DummyOnConflictClause(duckdb_libpgquery::PGOnConflictActionAlias type, const string &relname); //! Transform a Postgres duckdb_libpgquery::T_PGCopyStmt node into a CopyStatement - unique_ptr TransformCopy(duckdb_libpgquery::PGNode *node); - void TransformCopyOptions(CopyInfo &info, duckdb_libpgquery::PGList *options); + unique_ptr TransformCopy(duckdb_libpgquery::PGCopyStmt &stmt); + void TransformCopyOptions(CopyInfo &info, optional_ptr options); //! Transform a Postgres duckdb_libpgquery::T_PGTransactionStmt node into a TransactionStatement - unique_ptr TransformTransaction(duckdb_libpgquery::PGNode *node); + unique_ptr TransformTransaction(duckdb_libpgquery::PGTransactionStmt &stmt); //! Transform a Postgres T_DeleteStatement node into a DeleteStatement - unique_ptr TransformDelete(duckdb_libpgquery::PGNode *node); + unique_ptr TransformDelete(duckdb_libpgquery::PGDeleteStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGUpdateStmt node into a UpdateStatement - unique_ptr TransformUpdate(duckdb_libpgquery::PGNode *node); + unique_ptr TransformUpdate(duckdb_libpgquery::PGUpdateStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGPragmaStmt node into a PragmaStatement - unique_ptr TransformPragma(duckdb_libpgquery::PGNode *node); + unique_ptr TransformPragma(duckdb_libpgquery::PGPragmaStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGExportStmt node into a ExportStatement - unique_ptr TransformExport(duckdb_libpgquery::PGNode *node); + unique_ptr TransformExport(duckdb_libpgquery::PGExportStmt &stmt); //! Transform a Postgres duckdb_libpgquery::T_PGImportStmt node into a PragmaStatement - unique_ptr TransformImport(duckdb_libpgquery::PGNode *node); - unique_ptr TransformExplain(duckdb_libpgquery::PGNode *node); - unique_ptr TransformVacuum(duckdb_libpgquery::PGNode *node); - unique_ptr TransformShow(duckdb_libpgquery::PGNode *node); - unique_ptr TransformShowSelect(duckdb_libpgquery::PGNode *node); - unique_ptr TransformAttach(duckdb_libpgquery::PGNode *node); - unique_ptr TransformDetach(duckdb_libpgquery::PGNode *node); - unique_ptr TransformUse(duckdb_libpgquery::PGNode *node); - - unique_ptr TransformPrepare(duckdb_libpgquery::PGNode *node); - unique_ptr TransformExecute(duckdb_libpgquery::PGNode *node); - unique_ptr TransformCall(duckdb_libpgquery::PGNode *node); - unique_ptr TransformDeallocate(duckdb_libpgquery::PGNode *node); - unique_ptr TransformPivotStatement(duckdb_libpgquery::PGSelectStmt *stmt); + unique_ptr TransformImport(duckdb_libpgquery::PGImportStmt &stmt); + unique_ptr TransformExplain(duckdb_libpgquery::PGExplainStmt &stmt); + unique_ptr TransformVacuum(duckdb_libpgquery::PGVacuumStmt &stmt); + unique_ptr TransformShow(duckdb_libpgquery::PGVariableShowStmt &stmt); + unique_ptr TransformShowSelect(duckdb_libpgquery::PGVariableShowSelectStmt &stmt); + unique_ptr TransformAttach(duckdb_libpgquery::PGAttachStmt &stmt); + unique_ptr TransformDetach(duckdb_libpgquery::PGDetachStmt &stmt); + unique_ptr TransformUse(duckdb_libpgquery::PGUseStmt &stmt); + + unique_ptr TransformPrepare(duckdb_libpgquery::PGPrepareStmt &stmt); + unique_ptr TransformExecute(duckdb_libpgquery::PGExecuteStmt &stmt); + unique_ptr TransformCall(duckdb_libpgquery::PGCallStmt &stmt); + unique_ptr TransformDeallocate(duckdb_libpgquery::PGDeallocateStmt &stmt); + unique_ptr TransformPivotStatement(duckdb_libpgquery::PGSelectStmt &select); unique_ptr CreatePivotStatement(unique_ptr statement); - PivotColumn TransformPivotColumn(duckdb_libpgquery::PGPivot *pivot); - vector TransformPivotList(duckdb_libpgquery::PGList *list); + PivotColumn TransformPivotColumn(duckdb_libpgquery::PGPivot &pivot); + vector TransformPivotList(duckdb_libpgquery::PGList &list); //===--------------------------------------------------------------------===// // SetStatement Transform //===--------------------------------------------------------------------===// - unique_ptr TransformSet(duckdb_libpgquery::PGNode *node); - unique_ptr TransformSetVariable(duckdb_libpgquery::PGVariableSetStmt *stmt); - unique_ptr TransformResetVariable(duckdb_libpgquery::PGVariableSetStmt *stmt); + unique_ptr TransformSet(duckdb_libpgquery::PGVariableSetStmt &set); + unique_ptr TransformSetVariable(duckdb_libpgquery::PGVariableSetStmt &stmt); + unique_ptr TransformResetVariable(duckdb_libpgquery::PGVariableSetStmt &stmt); - unique_ptr TransformCheckpoint(duckdb_libpgquery::PGNode *node); - unique_ptr TransformLoad(duckdb_libpgquery::PGNode *node); + unique_ptr TransformCheckpoint(duckdb_libpgquery::PGCheckPointStmt &stmt); + unique_ptr TransformLoad(duckdb_libpgquery::PGLoadStmt &stmt); //===--------------------------------------------------------------------===// // Query Node Transform //===--------------------------------------------------------------------===// //! Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a QueryNode - unique_ptr TransformSelectNode(duckdb_libpgquery::PGSelectStmt *node); - unique_ptr TransformSelectInternal(duckdb_libpgquery::PGSelectStmt *node); + unique_ptr TransformSelectNode(duckdb_libpgquery::PGSelectStmt &select); + unique_ptr TransformSelectInternal(duckdb_libpgquery::PGSelectStmt &select); void TransformModifiers(duckdb_libpgquery::PGSelectStmt &stmt, QueryNode &node); //===--------------------------------------------------------------------===// // Expression Transform //===--------------------------------------------------------------------===// //! Transform a Postgres boolean expression into an Expression - unique_ptr TransformBoolExpr(duckdb_libpgquery::PGBoolExpr *root); + unique_ptr TransformBoolExpr(duckdb_libpgquery::PGBoolExpr &root); //! Transform a Postgres case expression into an Expression - unique_ptr TransformCase(duckdb_libpgquery::PGCaseExpr *root); + unique_ptr TransformCase(duckdb_libpgquery::PGCaseExpr &root); //! Transform a Postgres type cast into an Expression - unique_ptr TransformTypeCast(duckdb_libpgquery::PGTypeCast *root); + unique_ptr TransformTypeCast(duckdb_libpgquery::PGTypeCast &root); //! Transform a Postgres coalesce into an Expression - unique_ptr TransformCoalesce(duckdb_libpgquery::PGAExpr *root); + unique_ptr TransformCoalesce(duckdb_libpgquery::PGAExpr &root); //! Transform a Postgres column reference into an Expression - unique_ptr TransformColumnRef(duckdb_libpgquery::PGColumnRef *root); + unique_ptr TransformColumnRef(duckdb_libpgquery::PGColumnRef &root); //! Transform a Postgres constant value into an Expression unique_ptr TransformValue(duckdb_libpgquery::PGValue val); //! Transform a Postgres operator into an Expression - unique_ptr TransformAExpr(duckdb_libpgquery::PGAExpr *root); - unique_ptr TransformAExprInternal(duckdb_libpgquery::PGAExpr *root); + unique_ptr TransformAExpr(duckdb_libpgquery::PGAExpr &root); + unique_ptr TransformAExprInternal(duckdb_libpgquery::PGAExpr &root); //! Transform a Postgres abstract expression into an Expression - unique_ptr TransformExpression(duckdb_libpgquery::PGNode *node); + unique_ptr TransformExpression(optional_ptr node); + unique_ptr TransformExpression(duckdb_libpgquery::PGNode &node); //! Transform a Postgres function call into an Expression - unique_ptr TransformFuncCall(duckdb_libpgquery::PGFuncCall *root); + unique_ptr TransformFuncCall(duckdb_libpgquery::PGFuncCall &root); //! Transform a Postgres boolean expression into an Expression - unique_ptr TransformInterval(duckdb_libpgquery::PGIntervalConstant *root); + unique_ptr TransformInterval(duckdb_libpgquery::PGIntervalConstant &root); //! Transform a Postgres lambda node [e.g. (x, y) -> x + y] into a lambda expression - unique_ptr TransformLambda(duckdb_libpgquery::PGLambdaFunction *node); + unique_ptr TransformLambda(duckdb_libpgquery::PGLambdaFunction &node); //! Transform a Postgres array access node (e.g. x[1] or x[1:3]) - unique_ptr TransformArrayAccess(duckdb_libpgquery::PGAIndirection *node); + unique_ptr TransformArrayAccess(duckdb_libpgquery::PGAIndirection &node); //! Transform a positional reference (e.g. #1) - unique_ptr TransformPositionalReference(duckdb_libpgquery::PGPositionalReference *node); - unique_ptr TransformStarExpression(duckdb_libpgquery::PGNode *node); - unique_ptr TransformBooleanTest(duckdb_libpgquery::PGBooleanTest *node); + unique_ptr TransformPositionalReference(duckdb_libpgquery::PGPositionalReference &node); + unique_ptr TransformStarExpression(duckdb_libpgquery::PGAStar &node); + unique_ptr TransformBooleanTest(duckdb_libpgquery::PGBooleanTest &node); //! Transform a Postgres constant value into an Expression - unique_ptr TransformConstant(duckdb_libpgquery::PGAConst *c); - unique_ptr TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc *n); - unique_ptr TransformResTarget(duckdb_libpgquery::PGResTarget *root); - unique_ptr TransformNullTest(duckdb_libpgquery::PGNullTest *root); - unique_ptr TransformParamRef(duckdb_libpgquery::PGParamRef *node); - unique_ptr TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr *root); + unique_ptr TransformConstant(duckdb_libpgquery::PGAConst &c); + unique_ptr TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc &n); + unique_ptr TransformResTarget(duckdb_libpgquery::PGResTarget &root); + unique_ptr TransformNullTest(duckdb_libpgquery::PGNullTest &root); + unique_ptr TransformParamRef(duckdb_libpgquery::PGParamRef &node); + unique_ptr TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr &root); - unique_ptr TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction *node); + unique_ptr TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction &node); - unique_ptr TransformSubquery(duckdb_libpgquery::PGSubLink *root); + unique_ptr TransformSubquery(duckdb_libpgquery::PGSubLink &root); //===--------------------------------------------------------------------===// // Constraints transform //===--------------------------------------------------------------------===// @@ -257,19 +260,19 @@ class Transformer { //===--------------------------------------------------------------------===// // Collation transform //===--------------------------------------------------------------------===// - unique_ptr TransformCollateExpr(duckdb_libpgquery::PGCollateClause *collate); + unique_ptr TransformCollateExpr(duckdb_libpgquery::PGCollateClause &collate); - string TransformCollation(duckdb_libpgquery::PGCollateClause *collate); + string TransformCollation(optional_ptr collate); - ColumnDefinition TransformColumnDefinition(duckdb_libpgquery::PGColumnDef *cdef); + ColumnDefinition TransformColumnDefinition(duckdb_libpgquery::PGColumnDef &cdef); //===--------------------------------------------------------------------===// // Helpers //===--------------------------------------------------------------------===// OnCreateConflict TransformOnConflict(duckdb_libpgquery::PGOnCreateConflict conflict); string TransformAlias(duckdb_libpgquery::PGAlias *root, vector &column_name_alias); vector TransformStringList(duckdb_libpgquery::PGList *list); - void TransformCTE(duckdb_libpgquery::PGWithClause *de_with_clause, CommonTableExpressionMap &cte_map); - unique_ptr TransformRecursiveCTE(duckdb_libpgquery::PGCommonTableExpr *node, + void TransformCTE(duckdb_libpgquery::PGWithClause &de_with_clause, CommonTableExpressionMap &cte_map); + unique_ptr TransformRecursiveCTE(duckdb_libpgquery::PGCommonTableExpr &cte, CommonTableExpressionInfo &info); unique_ptr TransformUnaryOperator(const string &op, unique_ptr child); @@ -279,24 +282,24 @@ class Transformer { // TableRef transform //===--------------------------------------------------------------------===// //! Transform a Postgres node into a TableRef - unique_ptr TransformTableRefNode(duckdb_libpgquery::PGNode *node); + unique_ptr TransformTableRefNode(duckdb_libpgquery::PGNode &n); //! Transform a Postgres FROM clause into a TableRef - unique_ptr TransformFrom(duckdb_libpgquery::PGList *root); + unique_ptr TransformFrom(optional_ptr root); //! Transform a Postgres table reference into a TableRef - unique_ptr TransformRangeVar(duckdb_libpgquery::PGRangeVar *root); + unique_ptr TransformRangeVar(duckdb_libpgquery::PGRangeVar &root); //! Transform a Postgres table-producing function into a TableRef - unique_ptr TransformRangeFunction(duckdb_libpgquery::PGRangeFunction *root); + unique_ptr TransformRangeFunction(duckdb_libpgquery::PGRangeFunction &root); //! Transform a Postgres join node into a TableRef - unique_ptr TransformJoin(duckdb_libpgquery::PGJoinExpr *root); + unique_ptr TransformJoin(duckdb_libpgquery::PGJoinExpr &root); //! Transform a Postgres pivot node into a TableRef - unique_ptr TransformPivot(duckdb_libpgquery::PGPivotExpr *root); + unique_ptr TransformPivot(duckdb_libpgquery::PGPivotExpr &root); //! Transform a table producing subquery into a TableRef - unique_ptr TransformRangeSubselect(duckdb_libpgquery::PGRangeSubselect *root); + unique_ptr TransformRangeSubselect(duckdb_libpgquery::PGRangeSubselect &root); //! Transform a VALUES list into a set of expressions unique_ptr TransformValuesList(duckdb_libpgquery::PGList *list); //! Transform a range var into a (schema) qualified name - QualifiedName TransformQualifiedName(duckdb_libpgquery::PGRangeVar *root); + QualifiedName TransformQualifiedName(duckdb_libpgquery::PGRangeVar &root); //! Transform a Postgres TypeName string into a LogicalType LogicalType TransformTypeName(duckdb_libpgquery::PGTypeName *name); @@ -316,10 +319,10 @@ class Transformer { void TransformExpressionList(duckdb_libpgquery::PGList &list, vector> &result); //! Transform a Postgres PARTITION BY/ORDER BY specification into lists of expressions - void TransformWindowDef(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr, + void TransformWindowDef(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr, const char *window_name = nullptr); //! Transform a Postgres window frame specification into frame expressions - void TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr); + void TransformWindowFrame(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr); unique_ptr TransformSampleOptions(duckdb_libpgquery::PGNode *options); //! Returns true if an expression is only a star (i.e. "*", without any other decorators) @@ -333,6 +336,16 @@ class Transformer { void InitializeStackCheck(); StackChecker StackCheck(idx_t extra_stack = 1); + +public: + template + static T &PGCast(duckdb_libpgquery::PGNode &node) { + return reinterpret_cast(node); + } + template + static optional_ptr PGPointerCast(void *ptr) { + return optional_ptr(reinterpret_cast(ptr)); + } }; class StackChecker { diff --git a/src/optimizer/regex_range_filter.cpp b/src/optimizer/regex_range_filter.cpp index 761b94373cfa..19d0e059e9a7 100644 --- a/src/optimizer/regex_range_filter.cpp +++ b/src/optimizer/regex_range_filter.cpp @@ -39,12 +39,10 @@ unique_ptr RegexRangeFilter::Rewrite(unique_ptr( ExpressionType::COMPARE_GREATERTHANOREQUALTO, func.children[0]->Copy(), - make_uniq( - Value::BLOB((const_data_ptr_t)info.range_min.c_str(), info.range_min.size()))); + make_uniq(Value::BLOB_RAW(info.range_min))); auto filter_right = make_uniq( ExpressionType::COMPARE_LESSTHANOREQUALTO, func.children[0]->Copy(), - make_uniq( - Value::BLOB((const_data_ptr_t)info.range_max.c_str(), info.range_max.size()))); + make_uniq(Value::BLOB_RAW(info.range_max))); auto filter_expr = make_uniq(ExpressionType::CONJUNCTION_AND, std::move(filter_left), std::move(filter_right)); diff --git a/src/parser/result_modifier.cpp b/src/parser/result_modifier.cpp index e96eec2114fe..b60e1a180e06 100644 --- a/src/parser/result_modifier.cpp +++ b/src/parser/result_modifier.cpp @@ -277,10 +277,10 @@ bool LimitPercentModifier::Equals(const ResultModifier &other_p) const { return false; } auto &other = other_p.Cast(); - if (!BaseExpression::Equals(*limit, *other.limit)) { + if (!ParsedExpression::Equals(limit, other.limit)) { return false; } - if (!BaseExpression::Equals(*offset, *other.offset)) { + if (!ParsedExpression::Equals(offset, other.offset)) { return false; } return true; diff --git a/src/parser/transform/expression/transform_array_access.cpp b/src/parser/transform/expression/transform_array_access.cpp index bc591ab2a0bc..142b3d9aaef2 100644 --- a/src/parser/transform/expression/transform_array_access.cpp +++ b/src/parser/transform/expression/transform_array_access.cpp @@ -6,23 +6,23 @@ namespace duckdb { -unique_ptr Transformer::TransformArrayAccess(duckdb_libpgquery::PGAIndirection *indirection_node) { +unique_ptr Transformer::TransformArrayAccess(duckdb_libpgquery::PGAIndirection &indirection_node) { // transform the source expression unique_ptr result; - result = TransformExpression(indirection_node->arg); + result = TransformExpression(indirection_node.arg); // now go over the indices // note that a single indirection node can contain multiple indices // this happens for e.g. more complex accesses (e.g. (foo).field1[42]) idx_t list_size = 0; - for (auto node = indirection_node->indirection->head; node != nullptr; node = node->next) { + for (auto node = indirection_node.indirection->head; node != nullptr; node = node->next) { auto target = reinterpret_cast(node->data.ptr_value); D_ASSERT(target); switch (target->type) { case duckdb_libpgquery::T_PGAIndices: { // index access (either slice or extract) - auto index = (duckdb_libpgquery::PGAIndices *)target; + auto index = PGPointerCast(target); vector> children; children.push_back(std::move(result)); if (index->is_slice) { @@ -42,7 +42,7 @@ unique_ptr Transformer::TransformArrayAccess(duckdb_libpgquery break; } case duckdb_libpgquery::T_PGString: { - auto val = (duckdb_libpgquery::PGValue *)target; + auto val = PGPointerCast(target); vector> children; children.push_back(std::move(result)); children.push_back(TransformValue(*val)); @@ -50,8 +50,8 @@ unique_ptr Transformer::TransformArrayAccess(duckdb_libpgquery break; } case duckdb_libpgquery::T_PGFuncCall: { - auto func = (duckdb_libpgquery::PGFuncCall *)target; - auto function = TransformFuncCall(func); + auto func = PGPointerCast(target); + auto function = TransformFuncCall(*func); if (function->type != ExpressionType::FUNCTION) { throw ParserException("%s.%s() call must be a function", result->ToString(), function->ToString()); } diff --git a/src/parser/transform/expression/transform_bool_expr.cpp b/src/parser/transform/expression/transform_bool_expr.cpp index 106d22603066..b7de69fab180 100644 --- a/src/parser/transform/expression/transform_bool_expr.cpp +++ b/src/parser/transform/expression/transform_bool_expr.cpp @@ -4,12 +4,12 @@ namespace duckdb { -unique_ptr Transformer::TransformBoolExpr(duckdb_libpgquery::PGBoolExpr *root) { +unique_ptr Transformer::TransformBoolExpr(duckdb_libpgquery::PGBoolExpr &root) { unique_ptr result; - for (auto node = root->args->head; node != nullptr; node = node->next) { - auto next = TransformExpression(reinterpret_cast(node->data.ptr_value)); + for (auto node = root.args->head; node != nullptr; node = node->next) { + auto next = TransformExpression(PGPointerCast(node->data.ptr_value)); - switch (root->boolop) { + switch (root.boolop) { case duckdb_libpgquery::PG_AND_EXPR: { if (!result) { result = std::move(next); diff --git a/src/parser/transform/expression/transform_boolean_test.cpp b/src/parser/transform/expression/transform_boolean_test.cpp index 88c67e3d646f..78ee75abaa8b 100644 --- a/src/parser/transform/expression/transform_boolean_test.cpp +++ b/src/parser/transform/expression/transform_boolean_test.cpp @@ -6,15 +6,15 @@ namespace duckdb { -unique_ptr Transformer::TransformBooleanTest(duckdb_libpgquery::PGBooleanTest *node) { - auto argument = TransformExpression(reinterpret_cast(node->arg)); +unique_ptr Transformer::TransformBooleanTest(duckdb_libpgquery::PGBooleanTest &node) { + auto argument = TransformExpression(PGPointerCast(node.arg)); auto expr_true = make_uniq(Value::BOOLEAN(true)); auto expr_false = make_uniq(Value::BOOLEAN(false)); // we cast the argument to bool to remove ambiguity wrt function binding on the comparision auto cast_argument = make_uniq(LogicalType::BOOLEAN, argument->Copy()); - switch (node->booltesttype) { + switch (node.booltesttype) { case duckdb_libpgquery::PGBoolTestType::PG_IS_TRUE: return make_uniq(ExpressionType::COMPARE_NOT_DISTINCT_FROM, std::move(cast_argument), std::move(expr_true)); @@ -32,7 +32,7 @@ unique_ptr Transformer::TransformBooleanTest(duckdb_libpgquery case duckdb_libpgquery::PGBoolTestType::IS_NOT_UNKNOWN: // IS NOT NULL return make_uniq(ExpressionType::OPERATOR_IS_NOT_NULL, std::move(argument)); default: - throw NotImplementedException("Unknown boolean test type %d", node->booltesttype); + throw NotImplementedException("Unknown boolean test type %d", node.booltesttype); } } diff --git a/src/parser/transform/expression/transform_case.cpp b/src/parser/transform/expression/transform_case.cpp index 498979cad39b..2902fa43613d 100644 --- a/src/parser/transform/expression/transform_case.cpp +++ b/src/parser/transform/expression/transform_case.cpp @@ -5,16 +5,14 @@ namespace duckdb { -unique_ptr Transformer::TransformCase(duckdb_libpgquery::PGCaseExpr *root) { - D_ASSERT(root); - +unique_ptr Transformer::TransformCase(duckdb_libpgquery::PGCaseExpr &root) { auto case_node = make_uniq(); - auto root_arg = TransformExpression(reinterpret_cast(root->arg)); - for (auto cell = root->args->head; cell != nullptr; cell = cell->next) { + auto root_arg = TransformExpression(PGPointerCast(root.arg)); + for (auto cell = root.args->head; cell != nullptr; cell = cell->next) { CaseCheck case_check; - auto w = reinterpret_cast(cell->data.ptr_value); - auto test_raw = TransformExpression(reinterpret_cast(w->expr)); + auto w = PGPointerCast(cell->data.ptr_value); + auto test_raw = TransformExpression(PGPointerCast(w->expr)); unique_ptr test; if (root_arg) { case_check.when_expr = @@ -22,12 +20,12 @@ unique_ptr Transformer::TransformCase(duckdb_libpgquery::PGCas } else { case_check.when_expr = std::move(test_raw); } - case_check.then_expr = TransformExpression(reinterpret_cast(w->result)); + case_check.then_expr = TransformExpression(PGPointerCast(w->result)); case_node->case_checks.push_back(std::move(case_check)); } - if (root->defresult) { - case_node->else_expr = TransformExpression(reinterpret_cast(root->defresult)); + if (root.defresult) { + case_node->else_expr = TransformExpression(PGPointerCast(root.defresult)); } else { case_node->else_expr = make_uniq(Value(LogicalType::SQLNULL)); } diff --git a/src/parser/transform/expression/transform_cast.cpp b/src/parser/transform/expression/transform_cast.cpp index 019839baea1e..688ebee08d47 100644 --- a/src/parser/transform/expression/transform_cast.cpp +++ b/src/parser/transform/expression/transform_cast.cpp @@ -6,23 +6,21 @@ namespace duckdb { -unique_ptr Transformer::TransformTypeCast(duckdb_libpgquery::PGTypeCast *root) { - D_ASSERT(root); - +unique_ptr Transformer::TransformTypeCast(duckdb_libpgquery::PGTypeCast &root) { // get the type to cast to - auto type_name = root->typeName; + auto type_name = root.typeName; LogicalType target_type = TransformTypeName(type_name); // check for a constant BLOB value, then return ConstantExpression with BLOB - if (!root->tryCast && target_type == LogicalType::BLOB && root->arg->type == duckdb_libpgquery::T_PGAConst) { - auto c = reinterpret_cast(root->arg); + if (!root.tryCast && target_type == LogicalType::BLOB && root.arg->type == duckdb_libpgquery::T_PGAConst) { + auto c = PGPointerCast(root.arg); if (c->val.type == duckdb_libpgquery::T_PGString) { return make_uniq(Value::BLOB(string(c->val.val.str))); } } // transform the expression node - auto expression = TransformExpression(root->arg); - bool try_cast = root->tryCast; + auto expression = TransformExpression(root.arg); + bool try_cast = root.tryCast; // now create a cast operation return make_uniq(target_type, std::move(expression), try_cast); diff --git a/src/parser/transform/expression/transform_coalesce.cpp b/src/parser/transform/expression/transform_coalesce.cpp index 63894513364f..bc42a2b22820 100644 --- a/src/parser/transform/expression/transform_coalesce.cpp +++ b/src/parser/transform/expression/transform_coalesce.cpp @@ -5,16 +5,14 @@ namespace duckdb { // COALESCE(a,b,c) returns the first argument that is NOT NULL, so // rewrite into CASE(a IS NOT NULL, a, CASE(b IS NOT NULL, b, c)) -unique_ptr Transformer::TransformCoalesce(duckdb_libpgquery::PGAExpr *root) { - D_ASSERT(root); - - auto coalesce_args = reinterpret_cast(root->lexpr); +unique_ptr Transformer::TransformCoalesce(duckdb_libpgquery::PGAExpr &root) { + auto coalesce_args = PGPointerCast(root.lexpr); D_ASSERT(coalesce_args->length > 0); // parser ensures this already auto coalesce_op = make_uniq(ExpressionType::OPERATOR_COALESCE); for (auto cell = coalesce_args->head; cell; cell = cell->next) { // get the value of the COALESCE - auto value_expr = TransformExpression(reinterpret_cast(cell->data.ptr_value)); + auto value_expr = TransformExpression(PGPointerCast(cell->data.ptr_value)); coalesce_op->children.push_back(std::move(value_expr)); } return std::move(coalesce_op); diff --git a/src/parser/transform/expression/transform_columnref.cpp b/src/parser/transform/expression/transform_columnref.cpp index f910346ea828..95629296b03f 100644 --- a/src/parser/transform/expression/transform_columnref.cpp +++ b/src/parser/transform/expression/transform_columnref.cpp @@ -6,12 +6,11 @@ namespace duckdb { -unique_ptr Transformer::TransformStarExpression(duckdb_libpgquery::PGNode *node) { - auto star = (duckdb_libpgquery::PGAStar *)node; - auto result = make_uniq(star->relation ? star->relation : string()); - if (star->except_list) { - for (auto head = star->except_list->head; head; head = head->next) { - auto value = (duckdb_libpgquery::PGValue *)head->data.ptr_value; +unique_ptr Transformer::TransformStarExpression(duckdb_libpgquery::PGAStar &star) { + auto result = make_uniq(star.relation ? star.relation : string()); + if (star.except_list) { + for (auto head = star.except_list->head; head; head = head->next) { + auto value = PGPointerCast(head->data.ptr_value); D_ASSERT(value->type == duckdb_libpgquery::T_PGString); string exclude_entry = value->val.str; if (result->exclude_list.find(exclude_entry) != result->exclude_list.end()) { @@ -20,12 +19,12 @@ unique_ptr Transformer::TransformStarExpression(duckdb_libpgqu result->exclude_list.insert(std::move(exclude_entry)); } } - if (star->replace_list) { - for (auto head = star->replace_list->head; head; head = head->next) { - auto list = (duckdb_libpgquery::PGList *)head->data.ptr_value; + if (star.replace_list) { + for (auto head = star.replace_list->head; head; head = head->next) { + auto list = PGPointerCast(head->data.ptr_value); D_ASSERT(list->length == 2); auto replace_expression = TransformExpression((duckdb_libpgquery::PGNode *)list->head->data.ptr_value); - auto value = (duckdb_libpgquery::PGValue *)list->tail->data.ptr_value; + auto value = PGPointerCast(list->tail->data.ptr_value); D_ASSERT(value->type == duckdb_libpgquery::T_PGString); string exclude_entry = value->val.str; if (result->replace_list.find(exclude_entry) != result->replace_list.end()) { @@ -37,12 +36,12 @@ unique_ptr Transformer::TransformStarExpression(duckdb_libpgqu result->replace_list.insert(make_pair(std::move(exclude_entry), std::move(replace_expression))); } } - if (star->expr) { - D_ASSERT(star->columns); + if (star.expr) { + D_ASSERT(star.columns); D_ASSERT(result->relation_name.empty()); D_ASSERT(result->exclude_list.empty()); D_ASSERT(result->replace_list.empty()); - result->expr = TransformExpression(star->expr); + result->expr = TransformExpression(star.expr); if (result->expr->type == ExpressionType::STAR) { auto &child_star = result->expr->Cast(); result->exclude_list = std::move(child_star.exclude_list); @@ -56,14 +55,14 @@ unique_ptr Transformer::TransformStarExpression(duckdb_libpgqu result->expr = std::move(list_filter); } } - result->columns = star->columns; - result->query_location = star->location; + result->columns = star.columns; + result->query_location = star.location; return std::move(result); } -unique_ptr Transformer::TransformColumnRef(duckdb_libpgquery::PGColumnRef *root) { - auto fields = root->fields; - auto head_node = (duckdb_libpgquery::PGNode *)fields->head->data.ptr_value; +unique_ptr Transformer::TransformColumnRef(duckdb_libpgquery::PGColumnRef &root) { + auto fields = root.fields; + auto head_node = PGPointerCast(fields->head->data.ptr_value); switch (head_node->type) { case duckdb_libpgquery::T_PGString: { if (fields->length < 1) { @@ -71,14 +70,14 @@ unique_ptr Transformer::TransformColumnRef(duckdb_libpgquery:: } vector column_names; for (auto node = fields->head; node; node = node->next) { - column_names.emplace_back(reinterpret_cast(node->data.ptr_value)->val.str); + column_names.emplace_back(PGPointerCast(node->data.ptr_value)->val.str); } auto colref = make_uniq(std::move(column_names)); - colref->query_location = root->location; + colref->query_location = root.location; return std::move(colref); } case duckdb_libpgquery::T_PGAStar: { - return TransformStarExpression(head_node); + return TransformStarExpression(PGCast(*head_node)); } default: throw NotImplementedException("ColumnRef not implemented!"); diff --git a/src/parser/transform/expression/transform_constant.cpp b/src/parser/transform/expression/transform_constant.cpp index 398aa414082c..6794173e74cb 100644 --- a/src/parser/transform/expression/transform_constant.cpp +++ b/src/parser/transform/expression/transform_constant.cpp @@ -72,8 +72,8 @@ unique_ptr Transformer::TransformValue(duckdb_libpgquery::PG } } -unique_ptr Transformer::TransformConstant(duckdb_libpgquery::PGAConst *c) { - return TransformValue(c->val); +unique_ptr Transformer::TransformConstant(duckdb_libpgquery::PGAConst &c) { + return TransformValue(c.val); } } // namespace duckdb diff --git a/src/parser/transform/expression/transform_expression.cpp b/src/parser/transform/expression/transform_expression.cpp index 729d28dada75..4e2527ef0e79 100644 --- a/src/parser/transform/expression/transform_expression.cpp +++ b/src/parser/transform/expression/transform_expression.cpp @@ -4,98 +4,95 @@ namespace duckdb { -unique_ptr Transformer::TransformResTarget(duckdb_libpgquery::PGResTarget *root) { - D_ASSERT(root); - - auto expr = TransformExpression(root->val); +unique_ptr Transformer::TransformResTarget(duckdb_libpgquery::PGResTarget &root) { + auto expr = TransformExpression(root.val); if (!expr) { return nullptr; } - if (root->name) { - expr->alias = string(root->name); + if (root.name) { + expr->alias = string(root.name); } return expr; } -unique_ptr Transformer::TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr *root) { - D_ASSERT(root); +unique_ptr Transformer::TransformNamedArg(duckdb_libpgquery::PGNamedArgExpr &root) { - auto expr = TransformExpression((duckdb_libpgquery::PGNode *)root->arg); - if (root->name) { - expr->alias = string(root->name); + auto expr = TransformExpression(PGPointerCast(root.arg)); + if (root.name) { + expr->alias = string(root.name); } return expr; } -unique_ptr Transformer::TransformExpression(duckdb_libpgquery::PGNode *node) { - if (!node) { - return nullptr; - } +unique_ptr Transformer::TransformExpression(duckdb_libpgquery::PGNode &node) { auto stack_checker = StackCheck(); - switch (node->type) { + switch (node.type) { case duckdb_libpgquery::T_PGColumnRef: - return TransformColumnRef(reinterpret_cast(node)); + return TransformColumnRef(PGCast(node)); case duckdb_libpgquery::T_PGAConst: - return TransformConstant(reinterpret_cast(node)); + return TransformConstant(PGCast(node)); case duckdb_libpgquery::T_PGAExpr: - return TransformAExpr(reinterpret_cast(node)); + return TransformAExpr(PGCast(node)); case duckdb_libpgquery::T_PGFuncCall: - return TransformFuncCall(reinterpret_cast(node)); + return TransformFuncCall(PGCast(node)); case duckdb_libpgquery::T_PGBoolExpr: - return TransformBoolExpr(reinterpret_cast(node)); + return TransformBoolExpr(PGCast(node)); case duckdb_libpgquery::T_PGTypeCast: - return TransformTypeCast(reinterpret_cast(node)); + return TransformTypeCast(PGCast(node)); case duckdb_libpgquery::T_PGCaseExpr: - return TransformCase(reinterpret_cast(node)); + return TransformCase(PGCast(node)); case duckdb_libpgquery::T_PGSubLink: - return TransformSubquery(reinterpret_cast(node)); + return TransformSubquery(PGCast(node)); case duckdb_libpgquery::T_PGCoalesceExpr: - return TransformCoalesce(reinterpret_cast(node)); + return TransformCoalesce(PGCast(node)); case duckdb_libpgquery::T_PGNullTest: - return TransformNullTest(reinterpret_cast(node)); + return TransformNullTest(PGCast(node)); case duckdb_libpgquery::T_PGResTarget: - return TransformResTarget(reinterpret_cast(node)); + return TransformResTarget(PGCast(node)); case duckdb_libpgquery::T_PGParamRef: - return TransformParamRef(reinterpret_cast(node)); + return TransformParamRef(PGCast(node)); case duckdb_libpgquery::T_PGNamedArgExpr: - return TransformNamedArg(reinterpret_cast(node)); + return TransformNamedArg(PGCast(node)); case duckdb_libpgquery::T_PGSQLValueFunction: - return TransformSQLValueFunction(reinterpret_cast(node)); + return TransformSQLValueFunction(PGCast(node)); case duckdb_libpgquery::T_PGSetToDefault: return make_uniq(); case duckdb_libpgquery::T_PGCollateClause: - return TransformCollateExpr(reinterpret_cast(node)); + return TransformCollateExpr(PGCast(node)); case duckdb_libpgquery::T_PGIntervalConstant: - return TransformInterval(reinterpret_cast(node)); + return TransformInterval(PGCast(node)); case duckdb_libpgquery::T_PGLambdaFunction: - return TransformLambda(reinterpret_cast(node)); + return TransformLambda(PGCast(node)); case duckdb_libpgquery::T_PGAIndirection: - return TransformArrayAccess(reinterpret_cast(node)); + return TransformArrayAccess(PGCast(node)); case duckdb_libpgquery::T_PGPositionalReference: - return TransformPositionalReference(reinterpret_cast(node)); + return TransformPositionalReference(PGCast(node)); case duckdb_libpgquery::T_PGGroupingFunc: - return TransformGroupingFunction(reinterpret_cast(node)); + return TransformGroupingFunction(PGCast(node)); case duckdb_libpgquery::T_PGAStar: - return TransformStarExpression(node); + return TransformStarExpression(PGCast(node)); case duckdb_libpgquery::T_PGBooleanTest: - return TransformBooleanTest(reinterpret_cast(node)); + return TransformBooleanTest(PGCast(node)); default: - throw NotImplementedException("Expression type %s (%d)", NodetypeToString(node->type), (int)node->type); + throw NotImplementedException("Expression type %s (%d)", NodetypeToString(node.type), (int)node.type); } } -void Transformer::TransformExpressionList(duckdb_libpgquery::PGList &list, - vector> &result) { - for (auto node = list.head; node != nullptr; node = node->next) { - auto target = reinterpret_cast(node->data.ptr_value); - D_ASSERT(target); +unique_ptr Transformer::TransformExpression(optional_ptr node) { + if (!node) { + return nullptr; + } + return TransformExpression(*node); +} - auto expr = TransformExpression(target); - D_ASSERT(expr); +void Transformer::TransformExpressionList(duckdb_libpgquery::PGList &list, vector> &result) { + for (auto node = list.head; node != nullptr; node = node->next) { + auto target = PGPointerCast(node->data.ptr_value); + auto expr = TransformExpression(*target); result.push_back(std::move(expr)); } } diff --git a/src/parser/transform/expression/transform_function.cpp b/src/parser/transform/expression/transform_function.cpp index 1403bd2317d8..b6803c9fcd8d 100644 --- a/src/parser/transform/expression/transform_function.cpp +++ b/src/parser/transform/expression/transform_function.cpp @@ -13,66 +13,60 @@ namespace duckdb { -void Transformer::TransformWindowDef(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr, +void Transformer::TransformWindowDef(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr, const char *window_name) { - D_ASSERT(window_spec); - D_ASSERT(expr); - // next: partitioning/ordering expressions - if (window_spec->partitionClause) { - if (window_name && !expr->partitions.empty()) { + if (window_spec.partitionClause) { + if (window_name && !expr.partitions.empty()) { throw ParserException("Cannot override PARTITION BY clause of window \"%s\"", window_name); } - TransformExpressionList(*window_spec->partitionClause, expr->partitions); + TransformExpressionList(*window_spec.partitionClause, expr.partitions); } - if (window_spec->orderClause) { - if (window_name && !expr->orders.empty()) { + if (window_spec.orderClause) { + if (window_name && !expr.orders.empty()) { throw ParserException("Cannot override ORDER BY clause of window \"%s\"", window_name); } - TransformOrderBy(window_spec->orderClause, expr->orders); + TransformOrderBy(window_spec.orderClause, expr.orders); } } -void Transformer::TransformWindowFrame(duckdb_libpgquery::PGWindowDef *window_spec, WindowExpression *expr) { - D_ASSERT(window_spec); - D_ASSERT(expr); - +void Transformer::TransformWindowFrame(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr) { // finally: specifics of bounds - expr->start_expr = TransformExpression(window_spec->startOffset); - expr->end_expr = TransformExpression(window_spec->endOffset); + expr.start_expr = TransformExpression(window_spec.startOffset); + expr.end_expr = TransformExpression(window_spec.endOffset); - if ((window_spec->frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING) || - (window_spec->frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)) { + if ((window_spec.frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING) || + (window_spec.frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)) { throw InternalException( "Window frames starting with unbounded following or ending in unbounded preceding make no sense"); } - const bool rangeMode = (window_spec->frameOptions & FRAMEOPTION_RANGE) != 0; - if (window_spec->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) { - expr->start = WindowBoundary::UNBOUNDED_PRECEDING; - } else if (window_spec->frameOptions & FRAMEOPTION_START_VALUE_PRECEDING) { - expr->start = rangeMode ? WindowBoundary::EXPR_PRECEDING_RANGE : WindowBoundary::EXPR_PRECEDING_ROWS; - } else if (window_spec->frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) { - expr->start = rangeMode ? WindowBoundary::EXPR_FOLLOWING_RANGE : WindowBoundary::EXPR_FOLLOWING_ROWS; - } else if (window_spec->frameOptions & FRAMEOPTION_START_CURRENT_ROW) { - expr->start = rangeMode ? WindowBoundary::CURRENT_ROW_RANGE : WindowBoundary::CURRENT_ROW_ROWS; + const bool rangeMode = (window_spec.frameOptions & FRAMEOPTION_RANGE) != 0; + if (window_spec.frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) { + expr.start = WindowBoundary::UNBOUNDED_PRECEDING; + } else if (window_spec.frameOptions & FRAMEOPTION_START_VALUE_PRECEDING) { + expr.start = rangeMode ? WindowBoundary::EXPR_PRECEDING_RANGE : WindowBoundary::EXPR_PRECEDING_ROWS; + } else if (window_spec.frameOptions & FRAMEOPTION_START_VALUE_FOLLOWING) { + expr.start = rangeMode ? WindowBoundary::EXPR_FOLLOWING_RANGE : WindowBoundary::EXPR_FOLLOWING_ROWS; + } else if (window_spec.frameOptions & FRAMEOPTION_START_CURRENT_ROW) { + expr.start = rangeMode ? WindowBoundary::CURRENT_ROW_RANGE : WindowBoundary::CURRENT_ROW_ROWS; } - if (window_spec->frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING) { - expr->end = WindowBoundary::UNBOUNDED_FOLLOWING; - } else if (window_spec->frameOptions & FRAMEOPTION_END_VALUE_PRECEDING) { - expr->end = rangeMode ? WindowBoundary::EXPR_PRECEDING_RANGE : WindowBoundary::EXPR_PRECEDING_ROWS; - } else if (window_spec->frameOptions & FRAMEOPTION_END_VALUE_FOLLOWING) { - expr->end = rangeMode ? WindowBoundary::EXPR_FOLLOWING_RANGE : WindowBoundary::EXPR_FOLLOWING_ROWS; - } else if (window_spec->frameOptions & FRAMEOPTION_END_CURRENT_ROW) { - expr->end = rangeMode ? WindowBoundary::CURRENT_ROW_RANGE : WindowBoundary::CURRENT_ROW_ROWS; + if (window_spec.frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING) { + expr.end = WindowBoundary::UNBOUNDED_FOLLOWING; + } else if (window_spec.frameOptions & FRAMEOPTION_END_VALUE_PRECEDING) { + expr.end = rangeMode ? WindowBoundary::EXPR_PRECEDING_RANGE : WindowBoundary::EXPR_PRECEDING_ROWS; + } else if (window_spec.frameOptions & FRAMEOPTION_END_VALUE_FOLLOWING) { + expr.end = rangeMode ? WindowBoundary::EXPR_FOLLOWING_RANGE : WindowBoundary::EXPR_FOLLOWING_ROWS; + } else if (window_spec.frameOptions & FRAMEOPTION_END_CURRENT_ROW) { + expr.end = rangeMode ? WindowBoundary::CURRENT_ROW_RANGE : WindowBoundary::CURRENT_ROW_ROWS; } - D_ASSERT(expr->start != WindowBoundary::INVALID && expr->end != WindowBoundary::INVALID); - if (((window_spec->frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING | FRAMEOPTION_START_VALUE_FOLLOWING)) && - !expr->start_expr) || - ((window_spec->frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING | FRAMEOPTION_END_VALUE_FOLLOWING)) && - !expr->end_expr)) { + D_ASSERT(expr.start != WindowBoundary::INVALID && expr.end != WindowBoundary::INVALID); + if (((window_spec.frameOptions & (FRAMEOPTION_START_VALUE_PRECEDING | FRAMEOPTION_START_VALUE_FOLLOWING)) && + !expr.start_expr) || + ((window_spec.frameOptions & (FRAMEOPTION_END_VALUE_PRECEDING | FRAMEOPTION_END_VALUE_FOLLOWING)) && + !expr.end_expr)) { throw InternalException("Failed to transform window boundary expression"); } } @@ -98,40 +92,40 @@ bool Transformer::InWindowDefinition() { return false; } -unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::PGFuncCall *root) { - auto name = root->funcname; +unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::PGFuncCall &root) { + auto name = root.funcname; string catalog, schema, function_name; if (name->length == 3) { // catalog + schema + name - catalog = reinterpret_cast(name->head->data.ptr_value)->val.str; - schema = reinterpret_cast(name->head->next->data.ptr_value)->val.str; - function_name = reinterpret_cast(name->head->next->next->data.ptr_value)->val.str; + catalog = PGPointerCast(name->head->data.ptr_value)->val.str; + schema = PGPointerCast(name->head->next->data.ptr_value)->val.str; + function_name = PGPointerCast(name->head->next->next->data.ptr_value)->val.str; } else if (name->length == 2) { // schema + name catalog = INVALID_CATALOG; - schema = reinterpret_cast(name->head->data.ptr_value)->val.str; - function_name = reinterpret_cast(name->head->next->data.ptr_value)->val.str; + schema = PGPointerCast(name->head->data.ptr_value)->val.str; + function_name = PGPointerCast(name->head->next->data.ptr_value)->val.str; } else if (name->length == 1) { // unqualified name catalog = INVALID_CATALOG; schema = INVALID_SCHEMA; - function_name = reinterpret_cast(name->head->data.ptr_value)->val.str; + function_name = PGPointerCast(name->head->data.ptr_value)->val.str; } else { throw ParserException("TransformFuncCall - Expected 1, 2 or 3 qualifications"); } // transform children vector> children; - if (root->args) { - TransformExpressionList(*root->args, children); + if (root.args) { + TransformExpressionList(*root.args, children); } - if (children.size() == 1 && ExpressionIsEmptyStar(*children[0]) && !root->agg_distinct && !root->agg_order) { + if (children.size() == 1 && ExpressionIsEmptyStar(*children[0]) && !root.agg_distinct && !root.agg_order) { // COUNT(*) gets translated into COUNT() children.clear(); } auto lowercase_name = StringUtil::Lower(function_name); - if (root->over) { + if (root.over) { if (InWindowDefinition()) { throw ParserException("window functions are not allowed in window definitions"); } @@ -141,30 +135,30 @@ unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::P throw InternalException("Unknown/unsupported window function"); } - if (root->agg_distinct) { + if (root.agg_distinct) { throw ParserException("DISTINCT is not implemented for window functions!"); } - if (root->agg_order) { + if (root.agg_order) { throw ParserException("ORDER BY is not implemented for window functions!"); } - if (win_fun_type != ExpressionType::WINDOW_AGGREGATE && root->agg_filter) { + if (win_fun_type != ExpressionType::WINDOW_AGGREGATE && root.agg_filter) { throw ParserException("FILTER is not implemented for non-aggregate window functions!"); } - if (root->export_state) { + if (root.export_state) { throw ParserException("EXPORT_STATE is not supported for window functions!"); } - if (win_fun_type == ExpressionType::WINDOW_AGGREGATE && root->agg_ignore_nulls) { + if (win_fun_type == ExpressionType::WINDOW_AGGREGATE && root.agg_ignore_nulls) { throw ParserException("IGNORE NULLS is not supported for windowed aggregates"); } auto expr = make_uniq(win_fun_type, std::move(catalog), std::move(schema), lowercase_name); - expr->ignore_nulls = root->agg_ignore_nulls; + expr->ignore_nulls = root.agg_ignore_nulls; - if (root->agg_filter) { - auto filter_expr = TransformExpression(root->agg_filter); + if (root.agg_filter) { + auto filter_expr = TransformExpression(root.agg_filter); expr->filter_expr = std::move(filter_expr); } @@ -197,7 +191,7 @@ unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::P } } } - auto window_spec = reinterpret_cast(root->over); + auto window_spec = PGPointerCast(root.over); if (window_spec->name) { auto it = window_clauses.find(StringUtil::Lower(string(window_spec->name))); if (it == window_clauses.end()) { @@ -217,30 +211,30 @@ unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::P D_ASSERT(window_ref); } in_window_definition = true; - TransformWindowDef(window_ref, expr.get()); + TransformWindowDef(*window_ref, *expr); if (window_ref != window_spec) { - TransformWindowDef(window_spec, expr.get(), window_name); + TransformWindowDef(*window_spec, *expr, window_name); } - TransformWindowFrame(window_spec, expr.get()); + TransformWindowFrame(*window_spec, *expr); in_window_definition = false; - expr->query_location = root->location; + expr->query_location = root.location; return std::move(expr); } - if (root->agg_ignore_nulls) { + if (root.agg_ignore_nulls) { throw ParserException("IGNORE NULLS is not supported for non-window functions"); } unique_ptr filter_expr; - if (root->agg_filter) { - filter_expr = TransformExpression(root->agg_filter); + if (root.agg_filter) { + filter_expr = TransformExpression(root.agg_filter); } auto order_bys = make_uniq(); - TransformOrderBy(root->agg_order, order_bys->orders); + TransformOrderBy(root.agg_order, order_bys->orders); // Ordered aggregates can be either WITHIN GROUP or after the function arguments - if (root->agg_within_group) { + if (root.agg_within_group) { // https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-ORDEREDSET-TABLE // Since we implement "ordered aggregates" without sorting, // we map all the ones we support to the corresponding aggregate function. @@ -310,7 +304,7 @@ unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::P order_bys = nullptr; auto unordered = make_uniq(catalog, schema, lowercase_name.c_str(), std::move(children), std::move(filter_expr), std::move(order_bys), - root->agg_distinct, false, root->export_state); + root.agg_distinct, false, root.export_state); lowercase_name = "list_sort"; order_bys.reset(); // NOLINT filter_expr.reset(); // NOLINT @@ -323,13 +317,13 @@ unique_ptr Transformer::TransformFuncCall(duckdb_libpgquery::P auto function = make_uniq(std::move(catalog), std::move(schema), lowercase_name.c_str(), std::move(children), std::move(filter_expr), std::move(order_bys), - root->agg_distinct, false, root->export_state); - function->query_location = root->location; + root.agg_distinct, false, root.export_state); + function->query_location = root.location; return std::move(function); } -unique_ptr Transformer::TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction *node) { +unique_ptr Transformer::TransformSQLValueFunction(duckdb_libpgquery::PGSQLValueFunction &node) { throw InternalException("SQL value functions should not be emitted by the parser"); } diff --git a/src/parser/transform/expression/transform_grouping_function.cpp b/src/parser/transform/expression/transform_grouping_function.cpp index 6d33becbf895..36751a9e5978 100644 --- a/src/parser/transform/expression/transform_grouping_function.cpp +++ b/src/parser/transform/expression/transform_grouping_function.cpp @@ -3,13 +3,13 @@ namespace duckdb { -unique_ptr Transformer::TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc *n) { +unique_ptr Transformer::TransformGroupingFunction(duckdb_libpgquery::PGGroupingFunc &grouping) { auto op = make_uniq(ExpressionType::GROUPING_FUNCTION); - for (auto node = n->args->head; node; node = node->next) { - auto n = (duckdb_libpgquery::PGNode *)node->data.ptr_value; + for (auto node = grouping.args->head; node; node = node->next) { + auto n = PGPointerCast(node->data.ptr_value); op->children.push_back(TransformExpression(n)); } - op->query_location = n->location; + op->query_location = grouping.location; return std::move(op); } diff --git a/src/parser/transform/expression/transform_interval.cpp b/src/parser/transform/expression/transform_interval.cpp index b23115573753..1a08c1f0d0a8 100644 --- a/src/parser/transform/expression/transform_interval.cpp +++ b/src/parser/transform/expression/transform_interval.cpp @@ -6,7 +6,7 @@ namespace duckdb { -unique_ptr Transformer::TransformInterval(duckdb_libpgquery::PGIntervalConstant *node) { +unique_ptr Transformer::TransformInterval(duckdb_libpgquery::PGIntervalConstant &node) { // handle post-fix notation of INTERVAL // three scenarios @@ -14,25 +14,25 @@ unique_ptr Transformer::TransformInterval(duckdb_libpgquery::P // interval 'string' year // interval int year unique_ptr expr; - switch (node->val_type) { + switch (node.val_type) { case duckdb_libpgquery::T_PGAExpr: - expr = TransformExpression(node->eval); + expr = TransformExpression(node.eval); break; case duckdb_libpgquery::T_PGString: - expr = make_uniq(Value(node->sval)); + expr = make_uniq(Value(node.sval)); break; case duckdb_libpgquery::T_PGInteger: - expr = make_uniq(Value(node->ival)); + expr = make_uniq(Value(node.ival)); break; default: throw InternalException("Unsupported interval transformation"); } - if (!node->typmods) { + if (!node.typmods) { return make_uniq(LogicalType::INTERVAL, std::move(expr)); } - int32_t mask = ((duckdb_libpgquery::PGAConst *)node->typmods->head->data.ptr_value)->val.val.ival; + int32_t mask = PGPointerCast(node.typmods->head->data.ptr_value)->val.val.ival; // these seemingly random constants are from datetime.hpp // they are copied here to avoid having to include this header // the bitshift is from the function INTERVAL_MASK in the parser diff --git a/src/parser/transform/expression/transform_is_null.cpp b/src/parser/transform/expression/transform_is_null.cpp index 7cf847f3724f..a6bd014c2813 100644 --- a/src/parser/transform/expression/transform_is_null.cpp +++ b/src/parser/transform/expression/transform_is_null.cpp @@ -4,13 +4,12 @@ namespace duckdb { -unique_ptr Transformer::TransformNullTest(duckdb_libpgquery::PGNullTest *root) { - D_ASSERT(root); - auto arg = TransformExpression(reinterpret_cast(root->arg)); - if (root->argisrow) { +unique_ptr Transformer::TransformNullTest(duckdb_libpgquery::PGNullTest &root) { + auto arg = TransformExpression(PGPointerCast(root.arg)); + if (root.argisrow) { throw NotImplementedException("IS NULL argisrow"); } - ExpressionType expr_type = (root->nulltesttype == duckdb_libpgquery::PG_IS_NULL) + ExpressionType expr_type = (root.nulltesttype == duckdb_libpgquery::PG_IS_NULL) ? ExpressionType::OPERATOR_IS_NULL : ExpressionType::OPERATOR_IS_NOT_NULL; diff --git a/src/parser/transform/expression/transform_lambda.cpp b/src/parser/transform/expression/transform_lambda.cpp index edeed09fe273..4f980a12dd2c 100644 --- a/src/parser/transform/expression/transform_lambda.cpp +++ b/src/parser/transform/expression/transform_lambda.cpp @@ -4,13 +4,12 @@ namespace duckdb { -unique_ptr Transformer::TransformLambda(duckdb_libpgquery::PGLambdaFunction *node) { +unique_ptr Transformer::TransformLambda(duckdb_libpgquery::PGLambdaFunction &node) { + D_ASSERT(node.lhs); + D_ASSERT(node.rhs); - D_ASSERT(node->lhs); - D_ASSERT(node->rhs); - - auto lhs = TransformExpression(node->lhs); - auto rhs = TransformExpression(node->rhs); + auto lhs = TransformExpression(node.lhs); + auto rhs = TransformExpression(node.rhs); D_ASSERT(lhs); D_ASSERT(rhs); return make_uniq(std::move(lhs), std::move(rhs)); diff --git a/src/parser/transform/expression/transform_operator.cpp b/src/parser/transform/expression/transform_operator.cpp index 9f9ca4c21252..19479b253e98 100644 --- a/src/parser/transform/expression/transform_operator.cpp +++ b/src/parser/transform/expression/transform_operator.cpp @@ -57,17 +57,16 @@ unique_ptr Transformer::TransformBinaryOperator(string op, uni } } -unique_ptr Transformer::TransformAExprInternal(duckdb_libpgquery::PGAExpr *root) { - D_ASSERT(root); - auto name = string((reinterpret_cast(root->name->head->data.ptr_value))->val.str); +unique_ptr Transformer::TransformAExprInternal(duckdb_libpgquery::PGAExpr &root) { + auto name = string(PGPointerCast(root.name->head->data.ptr_value)->val.str); - switch (root->kind) { + switch (root.kind) { case duckdb_libpgquery::PG_AEXPR_OP_ALL: case duckdb_libpgquery::PG_AEXPR_OP_ANY: { // left=ANY(right) // we turn this into left=ANY((SELECT UNNEST(right))) - auto left_expr = TransformExpression(root->lexpr); - auto right_expr = TransformExpression(root->rexpr); + auto left_expr = TransformExpression(root.lexpr); + auto right_expr = TransformExpression(root.rexpr); auto subquery_expr = make_uniq(); auto select_statement = make_uniq(); @@ -82,9 +81,9 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque subquery_expr->subquery_type = SubqueryType::ANY; subquery_expr->child = std::move(left_expr); subquery_expr->comparison_type = OperatorToExpressionType(name); - subquery_expr->query_location = root->location; + subquery_expr->query_location = root.location; - if (root->kind == duckdb_libpgquery::PG_AEXPR_OP_ALL) { + if (root.kind == duckdb_libpgquery::PG_AEXPR_OP_ALL) { // ALL sublink is equivalent to NOT(ANY) with inverted comparison // e.g. [= ALL()] is equivalent to [NOT(<> ANY())] // first invert the comparison type @@ -94,7 +93,7 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque return std::move(subquery_expr); } case duckdb_libpgquery::PG_AEXPR_IN: { - auto left_expr = TransformExpression(root->lexpr); + auto left_expr = TransformExpression(root.lexpr); ExpressionType operator_type; // this looks very odd, but seems to be the way to find out its NOT IN if (name == "<>") { @@ -105,35 +104,35 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque operator_type = ExpressionType::COMPARE_IN; } auto result = make_uniq(operator_type, std::move(left_expr)); - result->query_location = root->location; - TransformExpressionList(*((duckdb_libpgquery::PGList *)root->rexpr), result->children); + result->query_location = root.location; + TransformExpressionList(*PGPointerCast(root.rexpr), result->children); return std::move(result); } // rewrite NULLIF(a, b) into CASE WHEN a=b THEN NULL ELSE a END case duckdb_libpgquery::PG_AEXPR_NULLIF: { vector> children; - children.push_back(TransformExpression(root->lexpr)); - children.push_back(TransformExpression(root->rexpr)); + children.push_back(TransformExpression(root.lexpr)); + children.push_back(TransformExpression(root.rexpr)); return make_uniq("nullif", std::move(children)); } // rewrite (NOT) X BETWEEN A AND B into (NOT) AND(GREATERTHANOREQUALTO(X, // A), LESSTHANOREQUALTO(X, B)) case duckdb_libpgquery::PG_AEXPR_BETWEEN: case duckdb_libpgquery::PG_AEXPR_NOT_BETWEEN: { - auto between_args = reinterpret_cast(root->rexpr); + auto between_args = PGPointerCast(root.rexpr); if (between_args->length != 2 || !between_args->head->data.ptr_value || !between_args->tail->data.ptr_value) { throw InternalException("(NOT) BETWEEN needs two args"); } - auto input = TransformExpression(root->lexpr); + auto input = TransformExpression(root.lexpr); auto between_left = - TransformExpression(reinterpret_cast(between_args->head->data.ptr_value)); + TransformExpression(PGPointerCast(between_args->head->data.ptr_value)); auto between_right = - TransformExpression(reinterpret_cast(between_args->tail->data.ptr_value)); + TransformExpression(PGPointerCast(between_args->tail->data.ptr_value)); auto compare_between = make_uniq(std::move(input), std::move(between_left), std::move(between_right)); - if (root->kind == duckdb_libpgquery::PG_AEXPR_BETWEEN) { + if (root.kind == duckdb_libpgquery::PG_AEXPR_BETWEEN) { return std::move(compare_between); } else { return make_uniq(ExpressionType::OPERATOR_NOT, std::move(compare_between)); @@ -141,13 +140,13 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque } // rewrite SIMILAR TO into regexp_full_match('asdf', '.*sd.*') case duckdb_libpgquery::PG_AEXPR_SIMILAR: { - auto left_expr = TransformExpression(root->lexpr); - auto right_expr = TransformExpression(root->rexpr); + auto left_expr = TransformExpression(root.lexpr); + auto right_expr = TransformExpression(root.rexpr); vector> children; children.push_back(std::move(left_expr)); - auto &similar_func = reinterpret_cast(*right_expr); + auto &similar_func = right_expr->Cast(); D_ASSERT(similar_func.function_name == "similar_escape"); D_ASSERT(similar_func.children.size() == 2); if (similar_func.children[1]->type != ExpressionType::VALUE_CONSTANT) { @@ -176,14 +175,14 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque } } case duckdb_libpgquery::PG_AEXPR_NOT_DISTINCT: { - auto left_expr = TransformExpression(root->lexpr); - auto right_expr = TransformExpression(root->rexpr); + auto left_expr = TransformExpression(root.lexpr); + auto right_expr = TransformExpression(root.rexpr); return make_uniq(ExpressionType::COMPARE_NOT_DISTINCT_FROM, std::move(left_expr), std::move(right_expr)); } case duckdb_libpgquery::PG_AEXPR_DISTINCT: { - auto left_expr = TransformExpression(root->lexpr); - auto right_expr = TransformExpression(root->rexpr); + auto left_expr = TransformExpression(root.lexpr); + auto right_expr = TransformExpression(root.rexpr); return make_uniq(ExpressionType::COMPARE_DISTINCT_FROM, std::move(left_expr), std::move(right_expr)); } @@ -191,8 +190,8 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque default: break; } - auto left_expr = TransformExpression(root->lexpr); - auto right_expr = TransformExpression(root->rexpr); + auto left_expr = TransformExpression(root.lexpr); + auto right_expr = TransformExpression(root.rexpr); if (!left_expr) { // prefix operator @@ -205,10 +204,10 @@ unique_ptr Transformer::TransformAExprInternal(duckdb_libpgque } } -unique_ptr Transformer::TransformAExpr(duckdb_libpgquery::PGAExpr *root) { +unique_ptr Transformer::TransformAExpr(duckdb_libpgquery::PGAExpr &root) { auto result = TransformAExprInternal(root); if (result) { - result->query_location = root->location; + result->query_location = root.location; } return result; } diff --git a/src/parser/transform/expression/transform_param_ref.cpp b/src/parser/transform/expression/transform_param_ref.cpp index 505af8f31491..c7a2aff1216b 100644 --- a/src/parser/transform/expression/transform_param_ref.cpp +++ b/src/parser/transform/expression/transform_param_ref.cpp @@ -4,39 +4,38 @@ namespace duckdb { -unique_ptr Transformer::TransformParamRef(duckdb_libpgquery::PGParamRef *node) { - D_ASSERT(node); +unique_ptr Transformer::TransformParamRef(duckdb_libpgquery::PGParamRef &node) { auto expr = make_uniq(); - if (node->number < 0) { + if (node.number < 0) { throw ParserException("Parameter numbers cannot be negative"); } - if (node->name) { + if (node.name) { // This is a named parameter, try to find an entry for it - D_ASSERT(node->number == 0); + D_ASSERT(node.number == 0); int32_t index; - if (GetNamedParam(node->name, index)) { + if (GetNamedParam(node.name, index)) { // We've seen this named parameter before and assigned it an index! - node->number = index; + node.number = index; } } - if (node->number == 0) { + if (node.number == 0) { expr->parameter_nr = ParamCount() + 1; - if (node->name && !HasNamedParameters() && ParamCount() != 0) { + if (node.name && !HasNamedParameters() && ParamCount() != 0) { // This parameter is named, but there were other parameter before it, and they were not named throw NotImplementedException("Mixing positional and named parameters is not supported yet"); } - if (node->name) { - D_ASSERT(!named_param_map.count(node->name)); + if (node.name) { + D_ASSERT(!named_param_map.count(node.name)); // Add it to the named parameter map so we can find it next time it's referenced - SetNamedParam(node->name, expr->parameter_nr); + SetNamedParam(node.name, expr->parameter_nr); } } else { - if (!node->name && HasNamedParameters()) { + if (!node.name && HasNamedParameters()) { // This parameter does not have a name, but the named param map is not empty throw NotImplementedException("Mixing positional and named parameters is not supported yet"); } - expr->parameter_nr = node->number; + expr->parameter_nr = node.number; } SetParamCount(MaxValue(ParamCount(), expr->parameter_nr)); return std::move(expr); diff --git a/src/parser/transform/expression/transform_positional_reference.cpp b/src/parser/transform/expression/transform_positional_reference.cpp index 09273290fcb0..55cb64724a32 100644 --- a/src/parser/transform/expression/transform_positional_reference.cpp +++ b/src/parser/transform/expression/transform_positional_reference.cpp @@ -4,12 +4,12 @@ namespace duckdb { -unique_ptr Transformer::TransformPositionalReference(duckdb_libpgquery::PGPositionalReference *node) { - if (node->position <= 0) { +unique_ptr Transformer::TransformPositionalReference(duckdb_libpgquery::PGPositionalReference &node) { + if (node.position <= 0) { throw ParserException("Positional reference node needs to be >= 1"); } - auto result = make_uniq(node->position); - result->query_location = node->location; + auto result = make_uniq(node.position); + result->query_location = node.location; return std::move(result); } diff --git a/src/parser/transform/expression/transform_subquery.cpp b/src/parser/transform/expression/transform_subquery.cpp index 234a64b9b0b9..c4ab20a8a48f 100644 --- a/src/parser/transform/expression/transform_subquery.cpp +++ b/src/parser/transform/expression/transform_subquery.cpp @@ -5,15 +5,14 @@ namespace duckdb { -unique_ptr Transformer::TransformSubquery(duckdb_libpgquery::PGSubLink *root) { - D_ASSERT(root); +unique_ptr Transformer::TransformSubquery(duckdb_libpgquery::PGSubLink &root) { auto subquery_expr = make_uniq(); - subquery_expr->subquery = TransformSelect(root->subselect); + subquery_expr->subquery = TransformSelect(root.subselect); D_ASSERT(subquery_expr->subquery); D_ASSERT(subquery_expr->subquery->node->GetSelectList().size() > 0); - switch (root->subLinkType) { + switch (root.subLinkType) { case duckdb_libpgquery::PG_EXISTS_SUBLINK: { subquery_expr->subquery_type = SubqueryType::EXISTS; break; @@ -22,14 +21,14 @@ unique_ptr Transformer::TransformSubquery(duckdb_libpgquery::P case duckdb_libpgquery::PG_ALL_SUBLINK: { // comparison with ANY() or ALL() subquery_expr->subquery_type = SubqueryType::ANY; - subquery_expr->child = TransformExpression(root->testexpr); + subquery_expr->child = TransformExpression(root.testexpr); // get the operator name - if (!root->operName) { + if (!root.operName) { // simple IN subquery_expr->comparison_type = ExpressionType::COMPARE_EQUAL; } else { auto operator_name = - string((reinterpret_cast(root->operName->head->data.ptr_value))->val.str); + string((PGPointerCast(root.operName->head->data.ptr_value))->val.str); subquery_expr->comparison_type = OperatorToExpressionType(operator_name); } if (subquery_expr->comparison_type != ExpressionType::COMPARE_EQUAL && @@ -40,7 +39,7 @@ unique_ptr Transformer::TransformSubquery(duckdb_libpgquery::P subquery_expr->comparison_type != ExpressionType::COMPARE_LESSTHANOREQUALTO) { throw ParserException("ANY and ALL operators require one of =,<>,>,<,>=,<= comparisons!"); } - if (root->subLinkType == duckdb_libpgquery::PG_ALL_SUBLINK) { + if (root.subLinkType == duckdb_libpgquery::PG_ALL_SUBLINK) { // ALL sublink is equivalent to NOT(ANY) with inverted comparison // e.g. [= ALL()] is equivalent to [NOT(<> ANY())] // first invert the comparison type @@ -96,9 +95,9 @@ unique_ptr Transformer::TransformSubquery(duckdb_libpgquery::P break; } default: - throw NotImplementedException("Subquery of type %d not implemented\n", (int)root->subLinkType); + throw NotImplementedException("Subquery of type %d not implemented\n", (int)root.subLinkType); } - subquery_expr->query_location = root->location; + subquery_expr->query_location = root.location; return std::move(subquery_expr); } diff --git a/src/parser/transform/helpers/transform_cte.cpp b/src/parser/transform/helpers/transform_cte.cpp index 769b15aed604..1d3faa546d6d 100644 --- a/src/parser/transform/helpers/transform_cte.cpp +++ b/src/parser/transform/helpers/transform_cte.cpp @@ -29,50 +29,49 @@ void Transformer::ExtractCTEsRecursive(CommonTableExpressionMap &cte_map) { } } -void Transformer::TransformCTE(duckdb_libpgquery::PGWithClause *de_with_clause, CommonTableExpressionMap &cte_map) { +void Transformer::TransformCTE(duckdb_libpgquery::PGWithClause &de_with_clause, CommonTableExpressionMap &cte_map) { // TODO: might need to update in case of future lawsuit - D_ASSERT(de_with_clause); stored_cte_map.push_back(&cte_map); - D_ASSERT(de_with_clause->ctes); - for (auto cte_ele = de_with_clause->ctes->head; cte_ele != nullptr; cte_ele = cte_ele->next) { + D_ASSERT(de_with_clause.ctes); + for (auto cte_ele = de_with_clause.ctes->head; cte_ele != nullptr; cte_ele = cte_ele->next) { auto info = make_uniq(); - auto cte = reinterpret_cast(cte_ele->data.ptr_value); - if (cte->aliascolnames) { - for (auto node = cte->aliascolnames->head; node != nullptr; node = node->next) { + auto &cte = *PGPointerCast(cte_ele->data.ptr_value); + if (cte.aliascolnames) { + for (auto node = cte.aliascolnames->head; node != nullptr; node = node->next) { info->aliases.emplace_back( reinterpret_cast(node->data.ptr_value)->val.str); } } // lets throw some errors on unsupported features early - if (cte->ctecolnames) { + if (cte.ctecolnames) { throw NotImplementedException("Column name setting not supported in CTEs"); } - if (cte->ctecoltypes) { + if (cte.ctecoltypes) { throw NotImplementedException("Column type setting not supported in CTEs"); } - if (cte->ctecoltypmods) { + if (cte.ctecoltypmods) { throw NotImplementedException("Column type modification not supported in CTEs"); } - if (cte->ctecolcollations) { + if (cte.ctecolcollations) { throw NotImplementedException("CTE collations not supported"); } // we need a query - if (!cte->ctequery || cte->ctequery->type != duckdb_libpgquery::T_PGSelectStmt) { + if (!cte.ctequery || cte.ctequery->type != duckdb_libpgquery::T_PGSelectStmt) { throw NotImplementedException("A CTE needs a SELECT"); } // CTE transformation can either result in inlining for non recursive CTEs, or in recursive CTE bindings // otherwise. - if (cte->cterecursive || de_with_clause->recursive) { + if (cte.cterecursive || de_with_clause.recursive) { info->query = TransformRecursiveCTE(cte, *info); } else { Transformer cte_transformer(*this); - info->query = cte_transformer.TransformSelect(cte->ctequery); + info->query = cte_transformer.TransformSelect(*PGPointerCast(cte.ctequery)); } D_ASSERT(info->query); - auto cte_name = string(cte->ctename); + auto cte_name = string(cte.ctename); auto it = cte_map.map.find(cte_name); if (it != cte_map.map.end()) { @@ -83,41 +82,37 @@ void Transformer::TransformCTE(duckdb_libpgquery::PGWithClause *de_with_clause, } } -unique_ptr Transformer::TransformRecursiveCTE(duckdb_libpgquery::PGCommonTableExpr *cte, +unique_ptr Transformer::TransformRecursiveCTE(duckdb_libpgquery::PGCommonTableExpr &cte, CommonTableExpressionInfo &info) { - auto stmt = (duckdb_libpgquery::PGSelectStmt *)cte->ctequery; + auto &stmt = *PGPointerCast(cte.ctequery); unique_ptr select; - switch (stmt->op) { + switch (stmt.op) { case duckdb_libpgquery::PG_SETOP_UNION: case duckdb_libpgquery::PG_SETOP_EXCEPT: case duckdb_libpgquery::PG_SETOP_INTERSECT: { select = make_uniq(); select->node = make_uniq_base(); - auto result = (RecursiveCTENode *)select->node.get(); - result->ctename = string(cte->ctename); - result->union_all = stmt->all; - result->left = TransformSelectNode(stmt->larg); - result->right = TransformSelectNode(stmt->rarg); - result->aliases = info.aliases; - - D_ASSERT(result->left); - D_ASSERT(result->right); - - if (stmt->op != duckdb_libpgquery::PG_SETOP_UNION) { + auto &result = select->node->Cast(); + result.ctename = string(cte.ctename); + result.union_all = stmt.all; + result.left = TransformSelectNode(*PGPointerCast(stmt.larg)); + result.right = TransformSelectNode(*PGPointerCast(stmt.rarg)); + result.aliases = info.aliases; + if (stmt.op != duckdb_libpgquery::PG_SETOP_UNION) { throw ParserException("Unsupported setop type for recursive CTE: only UNION or UNION ALL are supported"); } break; } default: // This CTE is not recursive. Fallback to regular query transformation. - return TransformSelect(cte->ctequery); + return TransformSelect(*PGPointerCast(cte.ctequery)); } - if (stmt->limitCount || stmt->limitOffset) { + if (stmt.limitCount || stmt.limitOffset) { throw ParserException("LIMIT or OFFSET in a recursive query is not allowed"); } - if (stmt->sortClause) { + if (stmt.sortClause) { throw ParserException("ORDER BY in a recursive query is not allowed"); } return select; diff --git a/src/parser/transform/statement/transform_alter_sequence.cpp b/src/parser/transform/statement/transform_alter_sequence.cpp index 78767f62c662..b80c5a92754a 100644 --- a/src/parser/transform/statement/transform_alter_sequence.cpp +++ b/src/parser/transform/statement/transform_alter_sequence.cpp @@ -7,24 +7,22 @@ namespace duckdb { -unique_ptr Transformer::TransformAlterSequence(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformAlterSequence(duckdb_libpgquery::PGAlterSeqStmt &stmt) { auto result = make_uniq(); - auto qname = TransformQualifiedName(stmt->sequence); + auto qname = TransformQualifiedName(*stmt.sequence); auto sequence_catalog = qname.catalog; auto sequence_schema = qname.schema; auto sequence_name = qname.name; - if (!stmt->options) { + if (!stmt.options) { throw InternalException("Expected an argument for ALTER SEQUENCE."); } unordered_set used; - duckdb_libpgquery::PGListCell *cell = nullptr; - for_each_cell(cell, stmt->options->head) { - auto *def_elem = reinterpret_cast(cell->data.ptr_value); + duckdb_libpgquery::PGListCell *cell; + for_each_cell(cell, stmt.options->head) { + auto def_elem = PGPointerCast(cell->data.ptr_value); string opt_name = string(def_elem->defname); if (opt_name == "owned_by") { @@ -33,7 +31,7 @@ unique_ptr Transformer::TransformAlterSequence(duckdb_libpgquery } used.insert(SequenceInfo::SEQ_OWN); - auto val = (duckdb_libpgquery::PGValue *)def_elem->arg; + auto val = PGPointerCast(def_elem->arg); if (!val) { throw InternalException("Expected an argument for option %s", opt_name); } @@ -43,9 +41,8 @@ unique_ptr Transformer::TransformAlterSequence(duckdb_libpgquery } auto opt_values = vector(); - auto opt_value_list = (duckdb_libpgquery::PGList *)(val); - for (auto c = opt_value_list->head; c != nullptr; c = lnext(c)) { - auto target = (duckdb_libpgquery::PGResTarget *)(c->data.ptr_value); + for (auto c = val->head; c != nullptr; c = lnext(c)) { + auto target = PGPointerCast(c->data.ptr_value); opt_values.emplace_back(target->name); } D_ASSERT(!opt_values.empty()); @@ -62,13 +59,13 @@ unique_ptr Transformer::TransformAlterSequence(duckdb_libpgquery } auto info = make_uniq(CatalogType::SEQUENCE_ENTRY, sequence_catalog, sequence_schema, sequence_name, owner_schema, owner_name, - TransformOnEntryNotFound(stmt->missing_ok)); + TransformOnEntryNotFound(stmt.missing_ok)); result->info = std::move(info); } else { throw NotImplementedException("ALTER SEQUENCE option not supported yet!"); } } - result->info->if_not_found = TransformOnEntryNotFound(stmt->missing_ok); + result->info->if_not_found = TransformOnEntryNotFound(stmt.missing_ok); return result; } } // namespace duckdb diff --git a/src/parser/transform/statement/transform_alter_table.cpp b/src/parser/transform/statement/transform_alter_table.cpp index 44c434614262..ba899ba0c43e 100644 --- a/src/parser/transform/statement/transform_alter_table.cpp +++ b/src/parser/transform/statement/transform_alter_table.cpp @@ -10,34 +10,32 @@ OnEntryNotFound Transformer::TransformOnEntryNotFound(bool missing_ok) { return missing_ok ? OnEntryNotFound::RETURN_NULL : OnEntryNotFound::THROW_EXCEPTION; } -unique_ptr Transformer::TransformAlter(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - D_ASSERT(stmt->relation); +unique_ptr Transformer::TransformAlter(duckdb_libpgquery::PGAlterTableStmt &stmt) { + D_ASSERT(stmt.relation); - if (stmt->cmds->length != 1) { + if (stmt.cmds->length != 1) { throw ParserException("Only one ALTER command per statement is supported"); } auto result = make_uniq(); - auto qname = TransformQualifiedName(stmt->relation); + auto qname = TransformQualifiedName(*stmt.relation); // first we check the type of ALTER - for (auto c = stmt->cmds->head; c != nullptr; c = c->next) { + for (auto c = stmt.cmds->head; c != nullptr; c = c->next) { auto command = reinterpret_cast(lfirst(c)); - AlterEntryData data(qname.catalog, qname.schema, qname.name, TransformOnEntryNotFound(stmt->missing_ok)); + AlterEntryData data(qname.catalog, qname.schema, qname.name, TransformOnEntryNotFound(stmt.missing_ok)); // TODO: Include more options for command->subtype switch (command->subtype) { case duckdb_libpgquery::PG_AT_AddColumn: { - auto cdef = (duckdb_libpgquery::PGColumnDef *)command->def; + auto cdef = PGPointerCast(command->def); - if (stmt->relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { + if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { throw ParserException("Adding columns is only supported for tables"); } if (cdef->category == duckdb_libpgquery::COL_GENERATED) { throw ParserException("Adding generated columns after table creation is not supported yet"); } - auto centry = TransformColumnDefinition(cdef); + auto centry = TransformColumnDefinition(*cdef); if (cdef->constraints) { for (auto constr = cdef->constraints->head; constr != nullptr; constr = constr->next) { @@ -54,7 +52,7 @@ unique_ptr Transformer::TransformAlter(duckdb_libpgquery::PGNode case duckdb_libpgquery::PG_AT_DropColumn: { bool cascade = command->behavior == duckdb_libpgquery::PG_DROP_CASCADE; - if (stmt->relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { + if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { throw ParserException("Dropping columns is only supported for tables"); } result->info = make_uniq(std::move(data), command->name, command->missing_ok, cascade); @@ -63,18 +61,18 @@ unique_ptr Transformer::TransformAlter(duckdb_libpgquery::PGNode case duckdb_libpgquery::PG_AT_ColumnDefault: { auto expr = TransformExpression(command->def); - if (stmt->relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { + if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { throw ParserException("Alter column's default is only supported for tables"); } result->info = make_uniq(std::move(data), command->name, std::move(expr)); break; } case duckdb_libpgquery::PG_AT_AlterColumnType: { - auto cdef = (duckdb_libpgquery::PGColumnDef *)command->def; - auto column_definition = TransformColumnDefinition(cdef); + auto cdef = PGPointerCast(command->def); + auto column_definition = TransformColumnDefinition(*cdef); unique_ptr expr; - if (stmt->relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { + if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) { throw ParserException("Alter column's type is only supported for tables"); } if (cdef->raw_default) { diff --git a/src/parser/transform/statement/transform_attach.cpp b/src/parser/transform/statement/transform_attach.cpp index 1d45018a5034..b3ecde3038d2 100644 --- a/src/parser/transform/statement/transform_attach.cpp +++ b/src/parser/transform/statement/transform_attach.cpp @@ -5,20 +5,19 @@ namespace duckdb { -unique_ptr Transformer::TransformAttach(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformAttach(duckdb_libpgquery::PGAttachStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - info->name = stmt->name ? stmt->name : string(); - info->path = stmt->path; + info->name = stmt.name ? stmt.name : string(); + info->path = stmt.path; - if (stmt->options) { - duckdb_libpgquery::PGListCell *cell = nullptr; - for_each_cell(cell, stmt->options->head) { - auto *def_elem = reinterpret_cast(cell->data.ptr_value); + if (stmt.options) { + duckdb_libpgquery::PGListCell *cell; + for_each_cell(cell, stmt.options->head) { + auto def_elem = PGPointerCast(cell->data.ptr_value); Value val; if (def_elem->arg) { - val = TransformValue(*((duckdb_libpgquery::PGValue *)def_elem->arg))->value; + val = TransformValue(*PGPointerCast(def_elem->arg))->value; } else { val = Value::BOOLEAN(true); } diff --git a/src/parser/transform/statement/transform_call.cpp b/src/parser/transform/statement/transform_call.cpp index 1c705757fc80..54d1a4f34932 100644 --- a/src/parser/transform/statement/transform_call.cpp +++ b/src/parser/transform/statement/transform_call.cpp @@ -3,12 +3,9 @@ namespace duckdb { -unique_ptr Transformer::TransformCall(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - +unique_ptr Transformer::TransformCall(duckdb_libpgquery::PGCallStmt &stmt) { auto result = make_uniq(); - result->function = TransformFuncCall((duckdb_libpgquery::PGFuncCall *)stmt->func); + result->function = TransformFuncCall(*PGPointerCast(stmt.func)); return result; } diff --git a/src/parser/transform/statement/transform_checkpoint.cpp b/src/parser/transform/statement/transform_checkpoint.cpp index 2d8992432060..e52d1df21b3b 100644 --- a/src/parser/transform/statement/transform_checkpoint.cpp +++ b/src/parser/transform/statement/transform_checkpoint.cpp @@ -5,16 +5,14 @@ namespace duckdb { -unique_ptr Transformer::TransformCheckpoint(duckdb_libpgquery::PGNode *node) { - auto checkpoint = (duckdb_libpgquery::PGCheckPointStmt *)node; - +unique_ptr Transformer::TransformCheckpoint(duckdb_libpgquery::PGCheckPointStmt &stmt) { vector> children; // transform into "CALL checkpoint()" or "CALL force_checkpoint()" - auto checkpoint_name = checkpoint->force ? "force_checkpoint" : "checkpoint"; + auto checkpoint_name = stmt.force ? "force_checkpoint" : "checkpoint"; auto result = make_uniq(); auto function = make_uniq(checkpoint_name, std::move(children)); - if (checkpoint->name) { - function->children.push_back(make_uniq(Value(checkpoint->name))); + if (stmt.name) { + function->children.push_back(make_uniq(Value(stmt.name))); } result->function = std::move(function); return std::move(result); diff --git a/src/parser/transform/statement/transform_copy.cpp b/src/parser/transform/statement/transform_copy.cpp index 07e7311d27f3..1445b2119cc0 100644 --- a/src/parser/transform/statement/transform_copy.cpp +++ b/src/parser/transform/statement/transform_copy.cpp @@ -9,18 +9,18 @@ namespace duckdb { -void Transformer::TransformCopyOptions(CopyInfo &info, duckdb_libpgquery::PGList *options) { +void Transformer::TransformCopyOptions(CopyInfo &info, optional_ptr options) { if (!options) { return; } - duckdb_libpgquery::PGListCell *cell = nullptr; // iterate over each option + duckdb_libpgquery::PGListCell *cell; for_each_cell(cell, options->head) { - auto *def_elem = reinterpret_cast(cell->data.ptr_value); + auto def_elem = PGPointerCast(cell->data.ptr_value); if (StringUtil::Lower(def_elem->defname) == "format") { // format specifier: interpret this option - auto *format_val = (duckdb_libpgquery::PGValue *)(def_elem->arg); + auto format_val = PGPointerCast(def_elem->arg); if (!format_val || format_val->type != duckdb_libpgquery::T_PGString) { throw ParserException("Unsupported parameter type for FORMAT: expected e.g. FORMAT 'csv', 'parquet'"); } @@ -37,9 +37,9 @@ void Transformer::TransformCopyOptions(CopyInfo &info, duckdb_libpgquery::PGList } switch (def_elem->arg->type) { case duckdb_libpgquery::T_PGList: { - auto column_list = (duckdb_libpgquery::PGList *)(def_elem->arg); + auto column_list = PGPointerCast(def_elem->arg); for (auto c = column_list->head; c != nullptr; c = lnext(c)) { - auto target = (duckdb_libpgquery::PGResTarget *)(c->data.ptr_value); + auto target = PGPointerCast(c->data.ptr_value); info.options[def_elem->defname].push_back(Value(target->name)); } break; @@ -47,28 +47,27 @@ void Transformer::TransformCopyOptions(CopyInfo &info, duckdb_libpgquery::PGList case duckdb_libpgquery::T_PGAStar: info.options[def_elem->defname].push_back(Value("*")); break; - default: - info.options[def_elem->defname].push_back( - TransformValue(*((duckdb_libpgquery::PGValue *)def_elem->arg))->value); + default: { + auto val = PGPointerCast(def_elem->arg); + info.options[def_elem->defname].push_back(TransformValue(*val)->value); break; } + } } } -unique_ptr Transformer::TransformCopy(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformCopy(duckdb_libpgquery::PGCopyStmt &stmt) { auto result = make_uniq(); auto &info = *result->info; // get file_path and is_from - info.is_from = stmt->is_from; - if (!stmt->filename) { + info.is_from = stmt.is_from; + if (!stmt.filename) { // stdin/stdout info.file_path = info.is_from ? "/dev/stdin" : "/dev/stdout"; } else { // copy to a file - info.file_path = stmt->filename; + info.file_path = stmt.filename; } if (StringUtil::EndsWith(info.file_path, ".parquet")) { info.format = "parquet"; @@ -79,27 +78,27 @@ unique_ptr Transformer::TransformCopy(duckdb_libpgquery::PGNode * } // get select_list - if (stmt->attlist) { - for (auto n = stmt->attlist->head; n != nullptr; n = n->next) { - auto target = reinterpret_cast(n->data.ptr_value); + if (stmt.attlist) { + for (auto n = stmt.attlist->head; n != nullptr; n = n->next) { + auto target = PGPointerCast(n->data.ptr_value); if (target->name) { info.select_list.emplace_back(target->name); } } } - if (stmt->relation) { - auto ref = TransformRangeVar(stmt->relation); - auto &table = *reinterpret_cast(ref.get()); + if (stmt.relation) { + auto ref = TransformRangeVar(*stmt.relation); + auto &table = ref->Cast(); info.table = table.table_name; info.schema = table.schema_name; info.catalog = table.catalog_name; } else { - result->select_statement = TransformSelectNode((duckdb_libpgquery::PGSelectStmt *)stmt->query); + result->select_statement = TransformSelectNode(*PGPointerCast(stmt.query)); } // handle the different options of the COPY statement - TransformCopyOptions(info, stmt->options); + TransformCopyOptions(info, stmt.options); return result; } diff --git a/src/parser/transform/statement/transform_create_database.cpp b/src/parser/transform/statement/transform_create_database.cpp index 3ac00ac1782a..3e2d88b9c12a 100644 --- a/src/parser/transform/statement/transform_create_database.cpp +++ b/src/parser/transform/statement/transform_create_database.cpp @@ -6,14 +6,13 @@ namespace duckdb { -unique_ptr Transformer::TransformCreateDatabase(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformCreateDatabase(duckdb_libpgquery::PGCreateDatabaseStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - info->path = stmt->path ? stmt->path : string(); + info->path = stmt.path ? stmt.path : string(); - auto qualified_name = TransformQualifiedName(stmt->name); + auto qualified_name = TransformQualifiedName(*stmt.name); if (!IsInvalidCatalog(qualified_name.catalog)) { throw ParserException("Expected \"CREATE DATABASE database\" "); } diff --git a/src/parser/transform/statement/transform_create_function.cpp b/src/parser/transform/statement/transform_create_function.cpp index aafc60b42012..bf6970f4ba2d 100644 --- a/src/parser/transform/statement/transform_create_function.cpp +++ b/src/parser/transform/statement/transform_create_function.cpp @@ -8,37 +8,33 @@ namespace duckdb { -unique_ptr Transformer::TransformCreateFunction(duckdb_libpgquery::PGNode *node) { - D_ASSERT(node); - D_ASSERT(node->type == duckdb_libpgquery::T_PGCreateFunctionStmt); - - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - D_ASSERT(stmt->function || stmt->query); +unique_ptr Transformer::TransformCreateFunction(duckdb_libpgquery::PGCreateFunctionStmt &stmt) { + D_ASSERT(stmt.type == duckdb_libpgquery::T_PGCreateFunctionStmt); + D_ASSERT(stmt.function || stmt.query); auto result = make_uniq(); - auto qname = TransformQualifiedName(stmt->name); + auto qname = TransformQualifiedName(*stmt.name); unique_ptr macro_func; // function can be null here - if (stmt->function) { - auto expression = TransformExpression(stmt->function); + if (stmt.function) { + auto expression = TransformExpression(stmt.function); macro_func = make_uniq(std::move(expression)); - } else if (stmt->query) { - auto query_node = TransformSelect(stmt->query, true)->node->Copy(); + } else if (stmt.query) { + auto query_node = TransformSelect(*PGPointerCast(stmt.query), true)->node->Copy(); macro_func = make_uniq(std::move(query_node)); } PivotEntryCheck("macro"); auto info = - make_uniq((stmt->function ? CatalogType::MACRO_ENTRY : CatalogType::TABLE_MACRO_ENTRY)); + make_uniq(stmt.function ? CatalogType::MACRO_ENTRY : CatalogType::TABLE_MACRO_ENTRY); info->catalog = qname.catalog; info->schema = qname.schema; info->name = qname.name; // temporary macro - switch (stmt->name->relpersistence) { + switch (stmt.name->relpersistence) { case duckdb_libpgquery::PG_RELPERSISTENCE_TEMP: info->temporary = true; break; @@ -51,11 +47,11 @@ unique_ptr Transformer::TransformCreateFunction(duckdb_libpgque } // what to do on conflict - info->on_conflict = TransformOnConflict(stmt->onconflict); + info->on_conflict = TransformOnConflict(stmt.onconflict); - if (stmt->params) { + if (stmt.params) { vector> parameters; - TransformExpressionList(*stmt->params, parameters); + TransformExpressionList(*stmt.params, parameters); for (auto ¶m : parameters) { if (param->type == ExpressionType::VALUE_CONSTANT) { // parameters with default value (must have an alias) diff --git a/src/parser/transform/statement/transform_create_index.cpp b/src/parser/transform/statement/transform_create_index.cpp index 301ea7d89a1e..a944abe5d2e7 100644 --- a/src/parser/transform/statement/transform_create_index.cpp +++ b/src/parser/transform/statement/transform_create_index.cpp @@ -43,30 +43,28 @@ vector> Transformer::TransformIndexParameters(duckd return expressions; } -unique_ptr Transformer::TransformCreateIndex(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformCreateIndex(duckdb_libpgquery::PGIndexStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - if (stmt->unique) { + if (stmt.unique) { info->constraint_type = IndexConstraintType::UNIQUE; } else { info->constraint_type = IndexConstraintType::NONE; } - info->on_conflict = TransformOnConflict(stmt->onconflict); + info->on_conflict = TransformOnConflict(stmt.onconflict); - info->expressions = TransformIndexParameters(stmt->indexParams, stmt->relation->relname); + info->expressions = TransformIndexParameters(stmt.indexParams, stmt.relation->relname); - info->index_type = StringToIndexType(string(stmt->accessMethod)); + info->index_type = StringToIndexType(string(stmt.accessMethod)); auto tableref = make_uniq(); - tableref->table_name = stmt->relation->relname; - if (stmt->relation->schemaname) { - tableref->schema_name = stmt->relation->schemaname; + tableref->table_name = stmt.relation->relname; + if (stmt.relation->schemaname) { + tableref->schema_name = stmt.relation->schemaname; } info->table = std::move(tableref); - if (stmt->idxname) { - info->index_name = stmt->idxname; + if (stmt.idxname) { + info->index_name = stmt.idxname; } else { throw NotImplementedException("Index without a name not supported yet!"); } diff --git a/src/parser/transform/statement/transform_create_schema.cpp b/src/parser/transform/statement/transform_create_schema.cpp index 77962a5cb514..0dc343abf0f9 100644 --- a/src/parser/transform/statement/transform_create_schema.cpp +++ b/src/parser/transform/statement/transform_create_schema.cpp @@ -4,21 +4,19 @@ namespace duckdb { -unique_ptr Transformer::TransformCreateSchema(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformCreateSchema(duckdb_libpgquery::PGCreateSchemaStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - D_ASSERT(stmt->schemaname); - info->catalog = stmt->catalogname ? stmt->catalogname : INVALID_CATALOG; - info->schema = stmt->schemaname; - info->on_conflict = TransformOnConflict(stmt->onconflict); + D_ASSERT(stmt.schemaname); + info->catalog = stmt.catalogname ? stmt.catalogname : INVALID_CATALOG; + info->schema = stmt.schemaname; + info->on_conflict = TransformOnConflict(stmt.onconflict); - if (stmt->schemaElts) { + if (stmt.schemaElts) { // schema elements - for (auto cell = stmt->schemaElts->head; cell != nullptr; cell = cell->next) { - auto node = reinterpret_cast(cell->data.ptr_value); + for (auto cell = stmt.schemaElts->head; cell != nullptr; cell = cell->next) { + auto node = PGPointerCast(cell->data.ptr_value); switch (node->type) { case duckdb_libpgquery::T_PGCreateStmt: case duckdb_libpgquery::T_PGViewStmt: diff --git a/src/parser/transform/statement/transform_create_sequence.cpp b/src/parser/transform/statement/transform_create_sequence.cpp index 4dd35f1cd734..cfc671f64257 100644 --- a/src/parser/transform/statement/transform_create_sequence.cpp +++ b/src/parser/transform/statement/transform_create_sequence.cpp @@ -7,24 +7,22 @@ namespace duckdb { -unique_ptr Transformer::TransformCreateSequence(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - +unique_ptr Transformer::TransformCreateSequence(duckdb_libpgquery::PGCreateSeqStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - auto qname = TransformQualifiedName(stmt->sequence); + auto qname = TransformQualifiedName(*stmt.sequence); info->catalog = qname.catalog; info->schema = qname.schema; info->name = qname.name; - if (stmt->options) { + if (stmt.options) { unordered_set used; duckdb_libpgquery::PGListCell *cell = nullptr; - for_each_cell(cell, stmt->options->head) { - auto *def_elem = reinterpret_cast(cell->data.ptr_value); + for_each_cell(cell, stmt.options->head) { + auto def_elem = PGPointerCast(cell->data.ptr_value); string opt_name = string(def_elem->defname); - auto val = (duckdb_libpgquery::PGValue *)def_elem->arg; + auto val = PGPointerCast(def_elem->arg); bool nodef = def_elem->defaction == duckdb_libpgquery::PG_DEFELEM_UNSPEC && !val; // e.g. NO MINVALUE int64_t opt_value = 0; @@ -110,8 +108,8 @@ unique_ptr Transformer::TransformCreateSequence(duckdb_libpgque } } } - info->temporary = !stmt->sequence->relpersistence; - info->on_conflict = TransformOnConflict(stmt->onconflict); + info->temporary = !stmt.sequence->relpersistence; + info->on_conflict = TransformOnConflict(stmt.onconflict); if (info->max_value <= info->min_value) { throw ParserException("MINVALUE (%lld) must be less than MAXVALUE (%lld)", info->min_value, info->max_value); } diff --git a/src/parser/transform/statement/transform_create_table.cpp b/src/parser/transform/statement/transform_create_table.cpp index b88dce59f505..2bcaa403ab0b 100644 --- a/src/parser/transform/statement/transform_create_table.cpp +++ b/src/parser/transform/statement/transform_create_table.cpp @@ -8,13 +8,13 @@ namespace duckdb { -string Transformer::TransformCollation(duckdb_libpgquery::PGCollateClause *collate) { +string Transformer::TransformCollation(optional_ptr collate) { if (!collate) { return string(); } string collation; for (auto c = collate->collname->head; c != nullptr; c = lnext(c)) { - auto pgvalue = (duckdb_libpgquery::PGValue *)c->data.ptr_value; + auto pgvalue = PGPointerCast(c->data.ptr_value); if (pgvalue->type != duckdb_libpgquery::T_PGString) { throw ParserException("Expected a string as collation type!"); } @@ -41,67 +41,65 @@ OnCreateConflict Transformer::TransformOnConflict(duckdb_libpgquery::PGOnCreateC } } -unique_ptr Transformer::TransformCollateExpr(duckdb_libpgquery::PGCollateClause *collate) { - auto child = TransformExpression(collate->arg); - auto collation = TransformCollation(collate); +unique_ptr Transformer::TransformCollateExpr(duckdb_libpgquery::PGCollateClause &collate) { + auto child = TransformExpression(collate.arg); + auto collation = TransformCollation(&collate); return make_uniq(collation, std::move(child)); } -ColumnDefinition Transformer::TransformColumnDefinition(duckdb_libpgquery::PGColumnDef *cdef) { +ColumnDefinition Transformer::TransformColumnDefinition(duckdb_libpgquery::PGColumnDef &cdef) { string colname; - if (cdef->colname) { - colname = cdef->colname; + if (cdef.colname) { + colname = cdef.colname; } - bool optional_type = cdef->category == duckdb_libpgquery::COL_GENERATED; - LogicalType target_type = (optional_type && !cdef->typeName) ? LogicalType::ANY : TransformTypeName(cdef->typeName); - if (cdef->collClause) { - if (cdef->category == duckdb_libpgquery::COL_GENERATED) { + bool optional_type = cdef.category == duckdb_libpgquery::COL_GENERATED; + LogicalType target_type = (optional_type && !cdef.typeName) ? LogicalType::ANY : TransformTypeName(cdef.typeName); + if (cdef.collClause) { + if (cdef.category == duckdb_libpgquery::COL_GENERATED) { throw ParserException("Collations are not supported on generated columns"); } if (target_type.id() != LogicalTypeId::VARCHAR) { throw ParserException("Only VARCHAR columns can have collations!"); } - target_type = LogicalType::VARCHAR_COLLATION(TransformCollation(cdef->collClause)); + target_type = LogicalType::VARCHAR_COLLATION(TransformCollation(cdef.collClause)); } return ColumnDefinition(colname, target_type); } -unique_ptr Transformer::TransformCreateTable(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformCreateTable(duckdb_libpgquery::PGCreateStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - if (stmt->inhRelations) { + if (stmt.inhRelations) { throw NotImplementedException("inherited relations not implemented"); } - D_ASSERT(stmt->relation); + D_ASSERT(stmt.relation); info->catalog = INVALID_CATALOG; - auto qname = TransformQualifiedName(stmt->relation); + auto qname = TransformQualifiedName(*stmt.relation); info->catalog = qname.catalog; info->schema = qname.schema; info->table = qname.name; - info->on_conflict = TransformOnConflict(stmt->onconflict); + info->on_conflict = TransformOnConflict(stmt.onconflict); info->temporary = - stmt->relation->relpersistence == duckdb_libpgquery::PGPostgresRelPersistence::PG_RELPERSISTENCE_TEMP; + stmt.relation->relpersistence == duckdb_libpgquery::PGPostgresRelPersistence::PG_RELPERSISTENCE_TEMP; - if (info->temporary && stmt->oncommit != duckdb_libpgquery::PGOnCommitAction::PG_ONCOMMIT_PRESERVE_ROWS && - stmt->oncommit != duckdb_libpgquery::PGOnCommitAction::PG_ONCOMMIT_NOOP) { + if (info->temporary && stmt.oncommit != duckdb_libpgquery::PGOnCommitAction::PG_ONCOMMIT_PRESERVE_ROWS && + stmt.oncommit != duckdb_libpgquery::PGOnCommitAction::PG_ONCOMMIT_NOOP) { throw NotImplementedException("Only ON COMMIT PRESERVE ROWS is supported"); } - if (!stmt->tableElts) { + if (!stmt.tableElts) { throw ParserException("Table must have at least one column!"); } idx_t column_count = 0; - for (auto c = stmt->tableElts->head; c != nullptr; c = lnext(c)) { - auto node = reinterpret_cast(c->data.ptr_value); + for (auto c = stmt.tableElts->head; c != nullptr; c = lnext(c)) { + auto node = PGPointerCast(c->data.ptr_value); switch (node->type) { case duckdb_libpgquery::T_PGColumnDef: { - auto cdef = (duckdb_libpgquery::PGColumnDef *)c->data.ptr_value; - auto centry = TransformColumnDefinition(cdef); + auto cdef = PGPointerCast(c->data.ptr_value); + auto centry = TransformColumnDefinition(*cdef); if (cdef->constraints) { for (auto constr = cdef->constraints->head; constr != nullptr; constr = constr->next) { auto constraint = TransformConstraint(constr, centry, info->columns.LogicalColumnCount()); diff --git a/src/parser/transform/statement/transform_create_table_as.cpp b/src/parser/transform/statement/transform_create_table_as.cpp index 98c88581010c..0c3406c27a92 100644 --- a/src/parser/transform/statement/transform_create_table_as.cpp +++ b/src/parser/transform/statement/transform_create_table_as.cpp @@ -4,29 +4,27 @@ namespace duckdb { -unique_ptr Transformer::TransformCreateTableAs(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - if (stmt->relkind == duckdb_libpgquery::PG_OBJECT_MATVIEW) { +unique_ptr Transformer::TransformCreateTableAs(duckdb_libpgquery::PGCreateTableAsStmt &stmt) { + if (stmt.relkind == duckdb_libpgquery::PG_OBJECT_MATVIEW) { throw NotImplementedException("Materialized view not implemented"); } - if (stmt->is_select_into || stmt->into->colNames || stmt->into->options) { + if (stmt.is_select_into || stmt.into->colNames || stmt.into->options) { throw NotImplementedException("Unimplemented features for CREATE TABLE as"); } - auto qname = TransformQualifiedName(stmt->into->rel); - if (stmt->query->type != duckdb_libpgquery::T_PGSelectStmt) { + auto qname = TransformQualifiedName(*stmt.into->rel); + if (stmt.query->type != duckdb_libpgquery::T_PGSelectStmt) { throw ParserException("CREATE TABLE AS requires a SELECT clause"); } - auto query = TransformSelect(stmt->query, false); + auto query = TransformSelect(stmt.query, false); auto result = make_uniq(); auto info = make_uniq(); info->catalog = qname.catalog; info->schema = qname.schema; info->table = qname.name; - info->on_conflict = TransformOnConflict(stmt->onconflict); + info->on_conflict = TransformOnConflict(stmt.onconflict); info->temporary = - stmt->into->rel->relpersistence == duckdb_libpgquery::PGPostgresRelPersistence::PG_RELPERSISTENCE_TEMP; + stmt.into->rel->relpersistence == duckdb_libpgquery::PGPostgresRelPersistence::PG_RELPERSISTENCE_TEMP; info->query = std::move(query); result->info = std::move(info); return result; diff --git a/src/parser/transform/statement/transform_create_type.cpp b/src/parser/transform/statement/transform_create_type.cpp index add6cebacc8e..a8dea8b18791 100644 --- a/src/parser/transform/statement/transform_create_type.cpp +++ b/src/parser/transform/statement/transform_create_type.cpp @@ -34,43 +34,40 @@ Vector ReadPgListToVector(duckdb_libpgquery::PGList *column_list, idx_t &size) { return result; } -unique_ptr Transformer::TransformCreateType(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformCreateType(duckdb_libpgquery::PGCreateTypeStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - auto qualified_name = TransformQualifiedName(stmt->typeName); + auto qualified_name = TransformQualifiedName(*stmt.typeName); info->catalog = qualified_name.catalog; info->schema = qualified_name.schema; info->name = qualified_name.name; - switch (stmt->kind) { + switch (stmt.kind) { case duckdb_libpgquery::PG_NEWTYPE_ENUM: { info->internal = false; - if (stmt->query) { + if (stmt.query) { // CREATE TYPE mood AS ENUM (SELECT ...) - D_ASSERT(stmt->vals == nullptr); - auto query = TransformSelect(stmt->query, false); + D_ASSERT(stmt.vals == nullptr); + auto query = TransformSelect(stmt.query, false); info->query = std::move(query); info->type = LogicalType::INVALID; } else { - D_ASSERT(stmt->query == nullptr); + D_ASSERT(stmt.query == nullptr); idx_t size = 0; - auto ordered_array = ReadPgListToVector(stmt->vals, size); + auto ordered_array = ReadPgListToVector(stmt.vals, size); info->type = LogicalType::ENUM(info->name, ordered_array, size); } } break; case duckdb_libpgquery::PG_NEWTYPE_ALIAS: { - LogicalType target_type = TransformTypeName(stmt->ofType); + LogicalType target_type = TransformTypeName(stmt.ofType); info->type = target_type; } break; default: throw InternalException("Unknown kind of new type"); } - result->info = std::move(info); return result; } diff --git a/src/parser/transform/statement/transform_create_view.cpp b/src/parser/transform/statement/transform_create_view.cpp index 11e5247fc8cc..0f3cac0031e3 100644 --- a/src/parser/transform/statement/transform_create_view.cpp +++ b/src/parser/transform/statement/transform_create_view.cpp @@ -4,37 +4,32 @@ namespace duckdb { -unique_ptr Transformer::TransformCreateView(duckdb_libpgquery::PGNode *node) { - D_ASSERT(node); - D_ASSERT(node->type == duckdb_libpgquery::T_PGViewStmt); - - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - D_ASSERT(stmt->view); +unique_ptr Transformer::TransformCreateView(duckdb_libpgquery::PGViewStmt &stmt) { + D_ASSERT(stmt.type == duckdb_libpgquery::T_PGViewStmt); + D_ASSERT(stmt.view); auto result = make_uniq(); auto info = make_uniq(); - auto qname = TransformQualifiedName(stmt->view); + auto qname = TransformQualifiedName(*stmt.view); info->catalog = qname.catalog; info->schema = qname.schema; info->view_name = qname.name; - info->temporary = !stmt->view->relpersistence; + info->temporary = !stmt.view->relpersistence; if (info->temporary && IsInvalidCatalog(info->catalog)) { info->catalog = TEMP_CATALOG; } - info->on_conflict = TransformOnConflict(stmt->onconflict); + info->on_conflict = TransformOnConflict(stmt.onconflict); - info->query = TransformSelect(stmt->query, false); + info->query = TransformSelect(*PGPointerCast(stmt.query), false); PivotEntryCheck("view"); - if (stmt->aliases && stmt->aliases->length > 0) { - for (auto c = stmt->aliases->head; c != nullptr; c = lnext(c)) { - auto node = reinterpret_cast(c->data.ptr_value); - switch (node->type) { + if (stmt.aliases && stmt.aliases->length > 0) { + for (auto c = stmt.aliases->head; c != nullptr; c = lnext(c)) { + auto val = PGPointerCast(c->data.ptr_value); + switch (val->type) { case duckdb_libpgquery::T_PGString: { - auto val = (duckdb_libpgquery::PGValue *)node; info->aliases.emplace_back(val->val.str); break; } @@ -47,11 +42,11 @@ unique_ptr Transformer::TransformCreateView(duckdb_libpgquery:: } } - if (stmt->options && stmt->options->length > 0) { + if (stmt.options && stmt.options->length > 0) { throw NotImplementedException("VIEW options"); } - if (stmt->withCheckOption != duckdb_libpgquery::PGViewCheckOption::PG_NO_CHECK_OPTION) { + if (stmt.withCheckOption != duckdb_libpgquery::PGViewCheckOption::PG_NO_CHECK_OPTION) { throw NotImplementedException("VIEW CHECK options"); } result->info = std::move(info); diff --git a/src/parser/transform/statement/transform_delete.cpp b/src/parser/transform/statement/transform_delete.cpp index a6c3825556ca..643e7c17ff4a 100644 --- a/src/parser/transform/statement/transform_delete.cpp +++ b/src/parser/transform/statement/transform_delete.cpp @@ -3,29 +3,27 @@ namespace duckdb { -unique_ptr Transformer::TransformDelete(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformDelete(duckdb_libpgquery::PGDeleteStmt &stmt) { auto result = make_uniq(); - if (stmt->withClause) { - TransformCTE(reinterpret_cast(stmt->withClause), result->cte_map); + if (stmt.withClause) { + TransformCTE(*PGPointerCast(stmt.withClause), result->cte_map); } - result->condition = TransformExpression(stmt->whereClause); - result->table = TransformRangeVar(stmt->relation); + result->condition = TransformExpression(stmt.whereClause); + result->table = TransformRangeVar(*stmt.relation); if (result->table->type != TableReferenceType::BASE_TABLE) { throw Exception("Can only delete from base tables!"); } - if (stmt->usingClause) { - for (auto n = stmt->usingClause->head; n != nullptr; n = n->next) { - auto target = reinterpret_cast(n->data.ptr_value); - auto using_entry = TransformTableRefNode(target); + if (stmt.usingClause) { + for (auto n = stmt.usingClause->head; n != nullptr; n = n->next) { + auto target = PGPointerCast(n->data.ptr_value); + auto using_entry = TransformTableRefNode(*target); result->using_clauses.push_back(std::move(using_entry)); } } - if (stmt->returningList) { - Transformer::TransformExpressionList(*(stmt->returningList), result->returning_list); + if (stmt.returningList) { + TransformExpressionList(*stmt.returningList, result->returning_list); } return result; } diff --git a/src/parser/transform/statement/transform_detach.cpp b/src/parser/transform/statement/transform_detach.cpp index bac075b32b1c..8a3e00bdfa0d 100644 --- a/src/parser/transform/statement/transform_detach.cpp +++ b/src/parser/transform/statement/transform_detach.cpp @@ -5,12 +5,11 @@ namespace duckdb { -unique_ptr Transformer::TransformDetach(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformDetach(duckdb_libpgquery::PGDetachStmt &stmt) { auto result = make_uniq(); auto info = make_uniq(); - info->name = stmt->db_name; - info->if_not_found = TransformOnEntryNotFound(stmt->missing_ok); + info->name = stmt.db_name; + info->if_not_found = TransformOnEntryNotFound(stmt.missing_ok); result->info = std::move(info); return result; diff --git a/src/parser/transform/statement/transform_drop.cpp b/src/parser/transform/statement/transform_drop.cpp index 8e824704a65d..875b1d285017 100644 --- a/src/parser/transform/statement/transform_drop.cpp +++ b/src/parser/transform/statement/transform_drop.cpp @@ -3,15 +3,13 @@ namespace duckdb { -unique_ptr Transformer::TransformDrop(duckdb_libpgquery::PGNode *node) { - auto stmt = (duckdb_libpgquery::PGDropStmt *)(node); +unique_ptr Transformer::TransformDrop(duckdb_libpgquery::PGDropStmt &stmt) { auto result = make_uniq(); auto &info = *result->info.get(); - D_ASSERT(stmt); - if (stmt->objects->length != 1) { + if (stmt.objects->length != 1) { throw NotImplementedException("Can only drop one object at a time"); } - switch (stmt->removeType) { + switch (stmt.removeType) { case duckdb_libpgquery::PG_OBJECT_TABLE: info.type = CatalogType::TABLE_ENTRY; break; @@ -43,44 +41,44 @@ unique_ptr Transformer::TransformDrop(duckdb_libpgquery::PGNode *n throw NotImplementedException("Cannot drop this type yet"); } - switch (stmt->removeType) { + switch (stmt.removeType) { case duckdb_libpgquery::PG_OBJECT_TYPE: { - auto view_list = (duckdb_libpgquery::PGList *)stmt->objects; - auto target = (duckdb_libpgquery::PGTypeName *)(view_list->head->data.ptr_value); - info.name = (reinterpret_cast(target->names->tail->data.ptr_value)->val.str); + auto view_list = PGPointerCast(stmt.objects); + auto target = PGPointerCast(view_list->head->data.ptr_value); + info.name = PGPointerCast(target->names->tail->data.ptr_value)->val.str; break; } case duckdb_libpgquery::PG_OBJECT_SCHEMA: { - auto view_list = (duckdb_libpgquery::PGList *)stmt->objects->head->data.ptr_value; + auto view_list = PGPointerCast(stmt.objects->head->data.ptr_value); if (view_list->length == 2) { - info.catalog = ((duckdb_libpgquery::PGValue *)view_list->head->data.ptr_value)->val.str; - info.name = ((duckdb_libpgquery::PGValue *)view_list->head->next->data.ptr_value)->val.str; + info.catalog = PGPointerCast(view_list->head->data.ptr_value)->val.str; + info.name = PGPointerCast(view_list->head->next->data.ptr_value)->val.str; } else if (view_list->length == 1) { - info.name = ((duckdb_libpgquery::PGValue *)view_list->head->data.ptr_value)->val.str; + info.name = PGPointerCast(view_list->head->data.ptr_value)->val.str; } else { throw ParserException("Expected \"catalog.schema\" or \"schema\""); } break; } default: { - auto view_list = (duckdb_libpgquery::PGList *)stmt->objects->head->data.ptr_value; + auto view_list = PGPointerCast(stmt.objects->head->data.ptr_value); if (view_list->length == 3) { - info.catalog = ((duckdb_libpgquery::PGValue *)view_list->head->data.ptr_value)->val.str; - info.schema = ((duckdb_libpgquery::PGValue *)view_list->head->next->data.ptr_value)->val.str; - info.name = ((duckdb_libpgquery::PGValue *)view_list->head->next->next->data.ptr_value)->val.str; + info.catalog = PGPointerCast(view_list->head->data.ptr_value)->val.str; + info.schema = PGPointerCast(view_list->head->next->data.ptr_value)->val.str; + info.name = PGPointerCast(view_list->head->next->next->data.ptr_value)->val.str; } else if (view_list->length == 2) { - info.schema = ((duckdb_libpgquery::PGValue *)view_list->head->data.ptr_value)->val.str; - info.name = ((duckdb_libpgquery::PGValue *)view_list->head->next->data.ptr_value)->val.str; + info.schema = PGPointerCast(view_list->head->data.ptr_value)->val.str; + info.name = PGPointerCast(view_list->head->next->data.ptr_value)->val.str; } else if (view_list->length == 1) { - info.name = ((duckdb_libpgquery::PGValue *)view_list->head->data.ptr_value)->val.str; + info.name = PGPointerCast(view_list->head->data.ptr_value)->val.str; } else { throw ParserException("Expected \"catalog.schema.name\", \"schema.name\"or \"name\""); } break; } } - info.cascade = stmt->behavior == duckdb_libpgquery::PGDropBehavior::PG_DROP_CASCADE; - info.if_not_found = TransformOnEntryNotFound(stmt->missing_ok); + info.cascade = stmt.behavior == duckdb_libpgquery::PGDropBehavior::PG_DROP_CASCADE; + info.if_not_found = TransformOnEntryNotFound(stmt.missing_ok); return std::move(result); } diff --git a/src/parser/transform/statement/transform_explain.cpp b/src/parser/transform/statement/transform_explain.cpp index aa7d99f490ca..c68c69e7be27 100644 --- a/src/parser/transform/statement/transform_explain.cpp +++ b/src/parser/transform/statement/transform_explain.cpp @@ -3,13 +3,11 @@ namespace duckdb { -unique_ptr Transformer::TransformExplain(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformExplain(duckdb_libpgquery::PGExplainStmt &stmt) { auto explain_type = ExplainType::EXPLAIN_STANDARD; - if (stmt->options) { - for (auto n = stmt->options->head; n; n = n->next) { - auto def_elem = ((duckdb_libpgquery::PGDefElem *)n->data.ptr_value)->defname; + if (stmt.options) { + for (auto n = stmt.options->head; n; n = n->next) { + auto def_elem = PGPointerCast(n->data.ptr_value)->defname; string elem(def_elem); if (elem == "analyze") { explain_type = ExplainType::EXPLAIN_ANALYZE; @@ -18,7 +16,7 @@ unique_ptr Transformer::TransformExplain(duckdb_libpgquery::PG } } } - return make_uniq(TransformStatement(stmt->query), explain_type); + return make_uniq(TransformStatement(*stmt.query), explain_type); } } // namespace duckdb diff --git a/src/parser/transform/statement/transform_export.cpp b/src/parser/transform/statement/transform_export.cpp index 71ce78f3442d..24c819da6c35 100644 --- a/src/parser/transform/statement/transform_export.cpp +++ b/src/parser/transform/statement/transform_export.cpp @@ -3,18 +3,17 @@ namespace duckdb { -unique_ptr Transformer::TransformExport(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformExport(duckdb_libpgquery::PGExportStmt &stmt) { auto info = make_uniq(); - info->file_path = stmt->filename; + info->file_path = stmt.filename; info->format = "csv"; info->is_from = false; // handle export options - TransformCopyOptions(*info, stmt->options); + TransformCopyOptions(*info, stmt.options); auto result = make_uniq(std::move(info)); - if (stmt->database) { - result->database = stmt->database; + if (stmt.database) { + result->database = stmt.database; } return result; } diff --git a/src/parser/transform/statement/transform_import.cpp b/src/parser/transform/statement/transform_import.cpp index 861492f37034..29092b2e022c 100644 --- a/src/parser/transform/statement/transform_import.cpp +++ b/src/parser/transform/statement/transform_import.cpp @@ -3,11 +3,10 @@ namespace duckdb { -unique_ptr Transformer::TransformImport(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformImport(duckdb_libpgquery::PGImportStmt &stmt) { auto result = make_uniq(); result->info->name = "import_database"; - result->info->parameters.emplace_back(stmt->filename); + result->info->parameters.emplace_back(stmt.filename); return result; } diff --git a/src/parser/transform/statement/transform_insert.cpp b/src/parser/transform/statement/transform_insert.cpp index 2bdfbecc6e3c..dfa3c25012c9 100644 --- a/src/parser/transform/statement/transform_insert.cpp +++ b/src/parser/transform/statement/transform_insert.cpp @@ -7,7 +7,7 @@ namespace duckdb { unique_ptr Transformer::TransformValuesList(duckdb_libpgquery::PGList *list) { auto result = make_uniq(); for (auto value_list = list->head; value_list != nullptr; value_list = value_list->next) { - auto target = (duckdb_libpgquery::PGList *)(value_list->data.ptr_value); + auto target = PGPointerCast(value_list->data.ptr_value); vector> insert_values; TransformExpressionList(*target, insert_values); @@ -22,52 +22,49 @@ unique_ptr Transformer::TransformValuesList(duckdb_libpgquery::PGList return std::move(result); } -unique_ptr Transformer::TransformInsert(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - +unique_ptr Transformer::TransformInsert(duckdb_libpgquery::PGInsertStmt &stmt) { auto result = make_uniq(); - if (stmt->withClause) { - TransformCTE(reinterpret_cast(stmt->withClause), result->cte_map); + if (stmt.withClause) { + TransformCTE(*PGPointerCast(stmt.withClause), result->cte_map); } // first check if there are any columns specified - if (stmt->cols) { - for (auto c = stmt->cols->head; c != nullptr; c = lnext(c)) { - auto target = (duckdb_libpgquery::PGResTarget *)(c->data.ptr_value); + if (stmt.cols) { + for (auto c = stmt.cols->head; c != nullptr; c = lnext(c)) { + auto target = PGPointerCast(c->data.ptr_value); result->columns.emplace_back(target->name); } } // Grab and transform the returning columns from the parser. - if (stmt->returningList) { - Transformer::TransformExpressionList(*(stmt->returningList), result->returning_list); + if (stmt.returningList) { + TransformExpressionList(*stmt.returningList, result->returning_list); } - if (stmt->selectStmt) { - result->select_statement = TransformSelect(stmt->selectStmt, false); + if (stmt.selectStmt) { + result->select_statement = TransformSelect(stmt.selectStmt, false); } else { result->default_values = true; } - auto qname = TransformQualifiedName(stmt->relation); + auto qname = TransformQualifiedName(*stmt.relation); result->table = qname.name; result->schema = qname.schema; - if (stmt->onConflictClause) { - if (stmt->onConflictAlias != duckdb_libpgquery::PG_ONCONFLICT_ALIAS_NONE) { + if (stmt.onConflictClause) { + if (stmt.onConflictAlias != duckdb_libpgquery::PG_ONCONFLICT_ALIAS_NONE) { // OR REPLACE | OR IGNORE are shorthands for the ON CONFLICT clause throw ParserException("You can not provide both OR REPLACE|IGNORE and an ON CONFLICT clause, please remove " "the first if you want to have more granual control"); } - result->on_conflict_info = TransformOnConflictClause(stmt->onConflictClause, result->schema); - result->table_ref = TransformRangeVar(stmt->relation); + result->on_conflict_info = TransformOnConflictClause(stmt.onConflictClause, result->schema); + result->table_ref = TransformRangeVar(*stmt.relation); } - if (stmt->onConflictAlias != duckdb_libpgquery::PG_ONCONFLICT_ALIAS_NONE) { - D_ASSERT(!stmt->onConflictClause); - result->on_conflict_info = DummyOnConflictClause(stmt->onConflictAlias, result->schema); - result->table_ref = TransformRangeVar(stmt->relation); + if (stmt.onConflictAlias != duckdb_libpgquery::PG_ONCONFLICT_ALIAS_NONE) { + D_ASSERT(!stmt.onConflictClause); + result->on_conflict_info = DummyOnConflictClause(stmt.onConflictAlias, result->schema); + result->table_ref = TransformRangeVar(*stmt.relation); } - switch (stmt->insert_column_order) { + switch (stmt.insert_column_order) { case duckdb_libpgquery::PG_INSERT_BY_POSITION: result->column_order = InsertColumnOrder::INSERT_BY_POSITION; break; diff --git a/src/parser/transform/statement/transform_load.cpp b/src/parser/transform/statement/transform_load.cpp index 7468b7775186..e5ab7617d85b 100644 --- a/src/parser/transform/statement/transform_load.cpp +++ b/src/parser/transform/statement/transform_load.cpp @@ -3,14 +3,13 @@ namespace duckdb { -unique_ptr Transformer::TransformLoad(duckdb_libpgquery::PGNode *node) { - D_ASSERT(node->type == duckdb_libpgquery::T_PGLoadStmt); - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformLoad(duckdb_libpgquery::PGLoadStmt &stmt) { + D_ASSERT(stmt.type == duckdb_libpgquery::T_PGLoadStmt); auto load_stmt = make_uniq(); auto load_info = make_uniq(); - load_info->filename = std::string(stmt->filename); - switch (stmt->load_type) { + load_info->filename = std::string(stmt.filename); + switch (stmt.load_type) { case duckdb_libpgquery::PG_LOAD_TYPE_LOAD: load_info->load_type = LoadType::LOAD; break; diff --git a/src/parser/transform/statement/transform_pivot_stmt.cpp b/src/parser/transform/statement/transform_pivot_stmt.cpp index e062a46c85d2..fc534756d895 100644 --- a/src/parser/transform/statement/transform_pivot_stmt.cpp +++ b/src/parser/transform/statement/transform_pivot_stmt.cpp @@ -123,14 +123,14 @@ unique_ptr Transformer::CreatePivotStatement(unique_ptr Transformer::TransformPivotStatement(duckdb_libpgquery::PGSelectStmt *stmt) { - auto pivot = stmt->pivot; - auto source = TransformTableRefNode(pivot->source); +unique_ptr Transformer::TransformPivotStatement(duckdb_libpgquery::PGSelectStmt &select) { + auto pivot = select.pivot; + auto source = TransformTableRefNode(*pivot->source); auto select_node = make_uniq(); // handle the CTEs - if (stmt->withClause) { - TransformCTE(reinterpret_cast(stmt->withClause), select_node->cte_map); + if (select.withClause) { + TransformCTE(*PGPointerCast(select.withClause), select_node->cte_map); } if (!pivot->columns) { // no pivot columns - not actually a pivot @@ -154,7 +154,7 @@ unique_ptr Transformer::TransformPivotStatement(duckdb_libpgquery::PG } // generate CREATE TYPE statements for each of the columns that do not have an IN list - auto columns = TransformPivotList(pivot->columns); + auto columns = TransformPivotList(*pivot->columns); auto pivot_idx = PivotEntryCount(); for (idx_t c = 0; c < columns.size(); c++) { auto &col = columns[c]; @@ -196,7 +196,7 @@ unique_ptr Transformer::TransformPivotStatement(duckdb_libpgquery::PG pivot_ref->pivots = std::move(columns); select_node->from_table = std::move(pivot_ref); // transform order by/limit modifiers - TransformModifiers(*stmt, *select_node); + TransformModifiers(select, *select_node); return std::move(select_node); } diff --git a/src/parser/transform/statement/transform_pragma.cpp b/src/parser/transform/statement/transform_pragma.cpp index f2dd01e2a75a..f684939395e6 100644 --- a/src/parser/transform/statement/transform_pragma.cpp +++ b/src/parser/transform/statement/transform_pragma.cpp @@ -9,17 +9,15 @@ namespace duckdb { -unique_ptr Transformer::TransformPragma(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - +unique_ptr Transformer::TransformPragma(duckdb_libpgquery::PGPragmaStmt &stmt) { auto result = make_uniq(); auto &info = *result->info; - info.name = stmt->name; + info.name = stmt.name; // parse the arguments, if any - if (stmt->args) { - for (auto cell = stmt->args->head; cell != nullptr; cell = cell->next) { - auto node = reinterpret_cast(cell->data.ptr_value); + if (stmt.args) { + for (auto cell = stmt.args->head; cell != nullptr; cell = cell->next) { + auto node = PGPointerCast(cell->data.ptr_value); auto expr = TransformExpression(node); if (expr->type == ExpressionType::COMPARE_EQUAL) { @@ -34,7 +32,7 @@ unique_ptr Transformer::TransformPragma(duckdb_libpgquery::PGNode auto &constant = comp.right->Cast(); info.named_parameters[columnref.GetName()] = constant.value; } else if (node->type == duckdb_libpgquery::T_PGAConst) { - auto constant = TransformConstant((duckdb_libpgquery::PGAConst *)node); + auto constant = TransformConstant(*PGPointerCast(node.get())); info.parameters.push_back((constant->Cast()).value); } else if (expr->type == ExpressionType::COLUMN_REF) { auto &colref = expr->Cast(); @@ -49,7 +47,7 @@ unique_ptr Transformer::TransformPragma(duckdb_libpgquery::PGNode } } // now parse the pragma type - switch (stmt->kind) { + switch (stmt.kind) { case duckdb_libpgquery::PG_PRAGMA_TYPE_NOTHING: { if (!info.parameters.empty() || !info.named_parameters.empty()) { throw InternalException("PRAGMA statement that is not a call or assignment cannot contain parameters"); diff --git a/src/parser/transform/statement/transform_prepare.cpp b/src/parser/transform/statement/transform_prepare.cpp index a30228702329..81b5f9a798d1 100644 --- a/src/parser/transform/statement/transform_prepare.cpp +++ b/src/parser/transform/statement/transform_prepare.cpp @@ -5,17 +5,14 @@ namespace duckdb { -unique_ptr Transformer::TransformPrepare(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - - if (stmt->argtypes && stmt->argtypes->length > 0) { +unique_ptr Transformer::TransformPrepare(duckdb_libpgquery::PGPrepareStmt &stmt) { + if (stmt.argtypes && stmt.argtypes->length > 0) { throw NotImplementedException("Prepared statement argument types are not supported, use CAST"); } auto result = make_uniq(); - result->name = string(stmt->name); - result->statement = TransformStatement(stmt->query); + result->name = string(stmt.name); + result->statement = TransformStatement(*stmt.query); if (!result->statement->named_param_map.empty()) { throw NotImplementedException("Named parameters are not supported in this client yet"); } @@ -24,15 +21,12 @@ unique_ptr Transformer::TransformPrepare(duckdb_libpgquery::PG return result; } -unique_ptr Transformer::TransformExecute(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - +unique_ptr Transformer::TransformExecute(duckdb_libpgquery::PGExecuteStmt &stmt) { auto result = make_uniq(); - result->name = string(stmt->name); + result->name = string(stmt.name); - if (stmt->params) { - TransformExpressionList(*stmt->params, result->values); + if (stmt.params) { + TransformExpressionList(*stmt.params, result->values); } for (auto &expr : result->values) { if (!expr->IsScalar()) { @@ -42,16 +36,14 @@ unique_ptr Transformer::TransformExecute(duckdb_libpgquery::PG return result; } -unique_ptr Transformer::TransformDeallocate(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - if (!stmt->name) { +unique_ptr Transformer::TransformDeallocate(duckdb_libpgquery::PGDeallocateStmt &stmt) { + if (!stmt.name) { throw ParserException("DEALLOCATE requires a name"); } auto result = make_uniq(); result->info->type = CatalogType::PREPARED_STATEMENT; - result->info->name = string(stmt->name); + result->info->name = string(stmt.name); return result; } diff --git a/src/parser/transform/statement/transform_rename.cpp b/src/parser/transform/statement/transform_rename.cpp index 64c5e9beb7ae..25b72d85a3ac 100644 --- a/src/parser/transform/statement/transform_rename.cpp +++ b/src/parser/transform/statement/transform_rename.cpp @@ -3,42 +3,40 @@ namespace duckdb { -unique_ptr Transformer::TransformRename(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - if (!stmt->relation) { +unique_ptr Transformer::TransformRename(duckdb_libpgquery::PGRenameStmt &stmt) { + if (!stmt.relation) { throw NotImplementedException("Altering schemas is not yet supported"); } unique_ptr info; AlterEntryData data; - data.if_not_found = TransformOnEntryNotFound(stmt->missing_ok); - data.catalog = stmt->relation->catalogname ? stmt->relation->catalogname : INVALID_CATALOG; - data.schema = stmt->relation->schemaname ? stmt->relation->schemaname : INVALID_SCHEMA; - if (stmt->relation->relname) { - data.name = stmt->relation->relname; + data.if_not_found = TransformOnEntryNotFound(stmt.missing_ok); + data.catalog = stmt.relation->catalogname ? stmt.relation->catalogname : INVALID_CATALOG; + data.schema = stmt.relation->schemaname ? stmt.relation->schemaname : INVALID_SCHEMA; + if (stmt.relation->relname) { + data.name = stmt.relation->relname; } // first we check the type of ALTER - switch (stmt->renameType) { + switch (stmt.renameType) { case duckdb_libpgquery::PG_OBJECT_COLUMN: { // change column name // get the old name and the new name - string old_name = stmt->subname; - string new_name = stmt->newname; + string old_name = stmt.subname; + string new_name = stmt.newname; info = make_uniq(std::move(data), old_name, new_name); break; } case duckdb_libpgquery::PG_OBJECT_TABLE: { // change table name - string new_name = stmt->newname; + string new_name = stmt.newname; info = make_uniq(std::move(data), new_name); break; } case duckdb_libpgquery::PG_OBJECT_VIEW: { // change view name - string new_name = stmt->newname; + string new_name = stmt.newname; info = make_uniq(std::move(data), new_name); break; } diff --git a/src/parser/transform/statement/transform_select.cpp b/src/parser/transform/statement/transform_select.cpp index 0de0e0fba9b2..5473c9c1d8d5 100644 --- a/src/parser/transform/statement/transform_select.cpp +++ b/src/parser/transform/statement/transform_select.cpp @@ -4,30 +4,33 @@ namespace duckdb { -unique_ptr Transformer::TransformSelectNode(duckdb_libpgquery::PGSelectStmt *stmt) { - if (stmt->pivot) { - return TransformPivotStatement(stmt); +unique_ptr Transformer::TransformSelectNode(duckdb_libpgquery::PGSelectStmt &select) { + if (select.pivot) { + return TransformPivotStatement(select); } else { - return TransformSelectInternal(stmt); + return TransformSelectInternal(select); } } -unique_ptr Transformer::TransformSelect(duckdb_libpgquery::PGNode *node, bool is_select) { - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformSelect(duckdb_libpgquery::PGSelectStmt &select, bool is_select) { auto result = make_uniq(); // Both Insert/Create Table As uses this. if (is_select) { - if (stmt->intoClause) { + if (select.intoClause) { throw ParserException("SELECT INTO not supported!"); } - if (stmt->lockingClause) { + if (select.lockingClause) { throw ParserException("SELECT locking clause is not supported!"); } } - result->node = TransformSelectNode(stmt); + result->node = TransformSelectNode(select); return result; } +unique_ptr Transformer::TransformSelect(optional_ptr node, bool is_select) { + return TransformSelect(PGCast(*node), is_select); +} + } // namespace duckdb diff --git a/src/parser/transform/statement/transform_select_node.cpp b/src/parser/transform/statement/transform_select_node.cpp index b32f8236448f..cc5b21b36a97 100644 --- a/src/parser/transform/statement/transform_select_node.cpp +++ b/src/parser/transform/statement/transform_select_node.cpp @@ -21,7 +21,7 @@ void Transformer::TransformModifiers(duckdb_libpgquery::PGSelectStmt &stmt, Quer if (stmt.limitCount || stmt.limitOffset) { if (stmt.limitCount && stmt.limitCount->type == duckdb_libpgquery::T_PGLimitPercent) { auto limit_percent_modifier = make_uniq(); - auto expr_node = reinterpret_cast(stmt.limitCount)->limit_percent; + auto expr_node = PGPointerCast(stmt.limitCount)->limit_percent; limit_percent_modifier->limit = TransformExpression(expr_node); if (stmt.limitOffset) { limit_percent_modifier->offset = TransformExpression(stmt.limitOffset); @@ -40,22 +40,22 @@ void Transformer::TransformModifiers(duckdb_libpgquery::PGSelectStmt &stmt, Quer } } -unique_ptr Transformer::TransformSelectInternal(duckdb_libpgquery::PGSelectStmt *stmt) { - D_ASSERT(stmt->type == duckdb_libpgquery::T_PGSelectStmt); +unique_ptr Transformer::TransformSelectInternal(duckdb_libpgquery::PGSelectStmt &stmt) { + D_ASSERT(stmt.type == duckdb_libpgquery::T_PGSelectStmt); auto stack_checker = StackCheck(); unique_ptr node; - switch (stmt->op) { + switch (stmt.op) { case duckdb_libpgquery::PG_SETOP_NONE: { node = make_uniq(); auto &result = node->Cast(); - if (stmt->withClause) { - TransformCTE(reinterpret_cast(stmt->withClause), node->cte_map); + if (stmt.withClause) { + TransformCTE(*PGPointerCast(stmt.withClause), node->cte_map); } - if (stmt->windowClause) { - for (auto window_ele = stmt->windowClause->head; window_ele != nullptr; window_ele = window_ele->next) { - auto window_def = reinterpret_cast(window_ele->data.ptr_value); + if (stmt.windowClause) { + for (auto window_ele = stmt.windowClause->head; window_ele != nullptr; window_ele = window_ele->next) { + auto window_def = PGPointerCast(window_ele->data.ptr_value); D_ASSERT(window_def); D_ASSERT(window_def->name); string window_name(window_def->name); @@ -63,47 +63,47 @@ unique_ptr Transformer::TransformSelectInternal(duckdb_libpgquery::PG if (it != window_clauses.end()) { throw ParserException("window \"%s\" is already defined", window_name); } - window_clauses[window_name] = window_def; + window_clauses[window_name] = window_def.get(); } } // checks distinct clause - if (stmt->distinctClause != nullptr) { + if (stmt.distinctClause != nullptr) { auto modifier = make_uniq(); // checks distinct on clause - auto target = reinterpret_cast(stmt->distinctClause->head->data.ptr_value); + auto target = PGPointerCast(stmt.distinctClause->head->data.ptr_value); if (target) { // add the columns defined in the ON clause to the select list - TransformExpressionList(*stmt->distinctClause, modifier->distinct_on_targets); + TransformExpressionList(*stmt.distinctClause, modifier->distinct_on_targets); } result.modifiers.push_back(std::move(modifier)); } // do this early so the value lists also have a `FROM` - if (stmt->valuesLists) { + if (stmt.valuesLists) { // VALUES list, create an ExpressionList - D_ASSERT(!stmt->fromClause); - result.from_table = TransformValuesList(stmt->valuesLists); + D_ASSERT(!stmt.fromClause); + result.from_table = TransformValuesList(stmt.valuesLists); result.select_list.push_back(make_uniq()); } else { - if (!stmt->targetList) { + if (!stmt.targetList) { throw ParserException("SELECT clause without selection list"); } // select list - TransformExpressionList(*stmt->targetList, result.select_list); - result.from_table = TransformFrom(stmt->fromClause); + TransformExpressionList(*stmt.targetList, result.select_list); + result.from_table = TransformFrom(stmt.fromClause); } // where - result.where_clause = TransformExpression(stmt->whereClause); + result.where_clause = TransformExpression(stmt.whereClause); // group by - TransformGroupBy(stmt->groupClause, result); + TransformGroupBy(stmt.groupClause, result); // having - result.having = TransformExpression(stmt->havingClause); + result.having = TransformExpression(stmt.havingClause); // qualify - result.qualify = TransformExpression(stmt->qualifyClause); + result.qualify = TransformExpression(stmt.qualifyClause); // sample - result.sample = TransformSampleOptions(stmt->sampleOptions); + result.sample = TransformSampleOptions(stmt.sampleOptions); break; } case duckdb_libpgquery::PG_SETOP_UNION: @@ -112,19 +112,19 @@ unique_ptr Transformer::TransformSelectInternal(duckdb_libpgquery::PG case duckdb_libpgquery::PG_SETOP_UNION_BY_NAME: { node = make_uniq(); auto &result = node->Cast(); - if (stmt->withClause) { - TransformCTE(reinterpret_cast(stmt->withClause), node->cte_map); + if (stmt.withClause) { + TransformCTE(*PGPointerCast(stmt.withClause), node->cte_map); } - result.left = TransformSelectNode(stmt->larg); - result.right = TransformSelectNode(stmt->rarg); + result.left = TransformSelectNode(*stmt.larg); + result.right = TransformSelectNode(*stmt.rarg); if (!result.left || !result.right) { throw Exception("Failed to transform setop children."); } bool select_distinct = true; - switch (stmt->op) { + switch (stmt.op) { case duckdb_libpgquery::PG_SETOP_UNION: - select_distinct = !stmt->all; + select_distinct = !stmt.all; result.setop_type = SetOperationType::UNION; break; case duckdb_libpgquery::PG_SETOP_EXCEPT: @@ -134,7 +134,7 @@ unique_ptr Transformer::TransformSelectInternal(duckdb_libpgquery::PG result.setop_type = SetOperationType::INTERSECT; break; case duckdb_libpgquery::PG_SETOP_UNION_BY_NAME: - select_distinct = !stmt->all; + select_distinct = !stmt.all; result.setop_type = SetOperationType::UNION_BY_NAME; break; default: @@ -143,15 +143,15 @@ unique_ptr Transformer::TransformSelectInternal(duckdb_libpgquery::PG if (select_distinct) { result.modifiers.push_back(make_uniq()); } - if (stmt->sampleOptions) { + if (stmt.sampleOptions) { throw ParserException("SAMPLE clause is only allowed in regular SELECT statements"); } break; } default: - throw NotImplementedException("Statement type %d not implemented!", stmt->op); + throw NotImplementedException("Statement type %d not implemented!", stmt.op); } - TransformModifiers(*stmt, *node); + TransformModifiers(stmt, *node); return node; } diff --git a/src/parser/transform/statement/transform_set.cpp b/src/parser/transform/statement/transform_set.cpp index 4636218241bb..a1389bd6aa7d 100644 --- a/src/parser/transform/statement/transform_set.cpp +++ b/src/parser/transform/statement/transform_set.cpp @@ -35,44 +35,43 @@ SetType ToSetType(duckdb_libpgquery::VariableSetKind pg_kind) { } // namespace -unique_ptr Transformer::TransformSetVariable(duckdb_libpgquery::PGVariableSetStmt *stmt) { - D_ASSERT(stmt->kind == duckdb_libpgquery::VariableSetKind::VAR_SET_VALUE); +unique_ptr Transformer::TransformSetVariable(duckdb_libpgquery::PGVariableSetStmt &stmt) { + D_ASSERT(stmt.kind == duckdb_libpgquery::VariableSetKind::VAR_SET_VALUE); - if (stmt->scope == duckdb_libpgquery::VariableSetScope::VAR_SET_SCOPE_LOCAL) { + if (stmt.scope == duckdb_libpgquery::VariableSetScope::VAR_SET_SCOPE_LOCAL) { throw NotImplementedException("SET LOCAL is not implemented."); } - auto name = std::string(stmt->name); + auto name = std::string(stmt.name); D_ASSERT(!name.empty()); // parser protect us! - if (stmt->args->length != 1) { + if (stmt.args->length != 1) { throw ParserException("SET needs a single scalar value parameter"); } - D_ASSERT(stmt->args->head && stmt->args->head->data.ptr_value); - D_ASSERT(((duckdb_libpgquery::PGNode *)stmt->args->head->data.ptr_value)->type == duckdb_libpgquery::T_PGAConst); + D_ASSERT(stmt.args->head && stmt.args->head->data.ptr_value); + auto const_val = PGPointerCast(stmt.args->head->data.ptr_value); + D_ASSERT(const_val->type == duckdb_libpgquery::T_PGAConst); - auto value = TransformValue(((duckdb_libpgquery::PGAConst *)stmt->args->head->data.ptr_value)->val)->value; - - return make_uniq(name, value, ToSetScope(stmt->scope)); + auto value = TransformValue(const_val->val)->value; + return make_uniq(name, value, ToSetScope(stmt.scope)); } -unique_ptr Transformer::TransformResetVariable(duckdb_libpgquery::PGVariableSetStmt *stmt) { - D_ASSERT(stmt->kind == duckdb_libpgquery::VariableSetKind::VAR_RESET); +unique_ptr Transformer::TransformResetVariable(duckdb_libpgquery::PGVariableSetStmt &stmt) { + D_ASSERT(stmt.kind == duckdb_libpgquery::VariableSetKind::VAR_RESET); - if (stmt->scope == duckdb_libpgquery::VariableSetScope::VAR_SET_SCOPE_LOCAL) { + if (stmt.scope == duckdb_libpgquery::VariableSetScope::VAR_SET_SCOPE_LOCAL) { throw NotImplementedException("RESET LOCAL is not implemented."); } - auto name = std::string(stmt->name); + auto name = std::string(stmt.name); D_ASSERT(!name.empty()); // parser protect us! - return make_uniq(name, ToSetScope(stmt->scope)); + return make_uniq(name, ToSetScope(stmt.scope)); } -unique_ptr Transformer::TransformSet(duckdb_libpgquery::PGNode *node) { - D_ASSERT(node->type == duckdb_libpgquery::T_PGVariableSetStmt); - auto stmt = reinterpret_cast(node); +unique_ptr Transformer::TransformSet(duckdb_libpgquery::PGVariableSetStmt &stmt) { + D_ASSERT(stmt.type == duckdb_libpgquery::T_PGVariableSetStmt); - SetType set_type = ToSetType(stmt->kind); + SetType set_type = ToSetType(stmt.kind); switch (set_type) { case SetType::SET: diff --git a/src/parser/transform/statement/transform_show.cpp b/src/parser/transform/statement/transform_show.cpp index 0712560ac3bb..59ede816ce73 100644 --- a/src/parser/transform/statement/transform_show.cpp +++ b/src/parser/transform/statement/transform_show.cpp @@ -25,19 +25,17 @@ static void TransformShowName(unique_ptr &result, const string } } -unique_ptr Transformer::TransformShow(duckdb_libpgquery::PGNode *node) { +unique_ptr Transformer::TransformShow(duckdb_libpgquery::PGVariableShowStmt &stmt) { // we transform SHOW x into PRAGMA SHOW('x') - - auto stmt = reinterpret_cast(node); - if (stmt->is_summary) { + if (stmt.is_summary) { auto result = make_uniq(); auto &info = *result->info; - info.is_summary = stmt->is_summary; + info.is_summary = stmt.is_summary; auto select = make_uniq(); select->select_list.push_back(make_uniq()); auto basetable = make_uniq(); - auto qualified_name = QualifiedName::Parse(stmt->name); + auto qualified_name = QualifiedName::Parse(stmt.name); basetable->schema_name = qualified_name.schema; basetable->table_name = qualified_name.name; select->from_table = std::move(basetable); @@ -48,7 +46,7 @@ unique_ptr Transformer::TransformShow(duckdb_libpgquery::PGNode *n auto result = make_uniq(); - auto show_name = stmt->name; + auto show_name = stmt.name; TransformShowName(result, show_name); return std::move(result); } diff --git a/src/parser/transform/statement/transform_show_select.cpp b/src/parser/transform/statement/transform_show_select.cpp index 440dabd489ab..5fee8a7afb43 100644 --- a/src/parser/transform/statement/transform_show_select.cpp +++ b/src/parser/transform/statement/transform_show_select.cpp @@ -4,16 +4,15 @@ namespace duckdb { -unique_ptr Transformer::TransformShowSelect(duckdb_libpgquery::PGNode *node) { +unique_ptr Transformer::TransformShowSelect(duckdb_libpgquery::PGVariableShowSelectStmt &stmt) { // we capture the select statement of SHOW - auto stmt = reinterpret_cast(node); - auto select_stmt = reinterpret_cast(stmt->stmt); + auto select_stmt = PGPointerCast(stmt.stmt); auto result = make_uniq(); auto &info = *result->info; - info.is_summary = stmt->is_summary; + info.is_summary = stmt.is_summary; - info.query = TransformSelectNode(select_stmt); + info.query = TransformSelectNode(*select_stmt); return result; } diff --git a/src/parser/transform/statement/transform_transaction.cpp b/src/parser/transform/statement/transform_transaction.cpp index ce0efca099d9..e35bf9ac17fa 100644 --- a/src/parser/transform/statement/transform_transaction.cpp +++ b/src/parser/transform/statement/transform_transaction.cpp @@ -3,10 +3,8 @@ namespace duckdb { -unique_ptr Transformer::TransformTransaction(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - switch (stmt->kind) { +unique_ptr Transformer::TransformTransaction(duckdb_libpgquery::PGTransactionStmt &stmt) { + switch (stmt.kind) { case duckdb_libpgquery::PG_TRANS_STMT_BEGIN: case duckdb_libpgquery::PG_TRANS_STMT_START: return make_uniq(TransactionType::BEGIN_TRANSACTION); @@ -15,7 +13,7 @@ unique_ptr Transformer::TransformTransaction(duckdb_libpgq case duckdb_libpgquery::PG_TRANS_STMT_ROLLBACK: return make_uniq(TransactionType::ROLLBACK); default: - throw NotImplementedException("Transaction type %d not implemented yet", stmt->kind); + throw NotImplementedException("Transaction type %d not implemented yet", stmt.kind); } } diff --git a/src/parser/transform/statement/transform_update.cpp b/src/parser/transform/statement/transform_update.cpp index 04b20e57418b..6d7048355e10 100644 --- a/src/parser/transform/statement/transform_update.cpp +++ b/src/parser/transform/statement/transform_update.cpp @@ -9,7 +9,7 @@ unique_ptr Transformer::TransformUpdateSetInfo(duckdb_libpgquery: auto root = target_list; for (auto cell = root->head; cell != nullptr; cell = cell->next) { - auto target = (duckdb_libpgquery::PGResTarget *)(cell->data.ptr_value); + auto target = PGPointerCast(cell->data.ptr_value); result->columns.emplace_back(target->name); result->expressions.push_back(TransformExpression(target->val)); } @@ -17,25 +17,22 @@ unique_ptr Transformer::TransformUpdateSetInfo(duckdb_libpgquery: return result; } -unique_ptr Transformer::TransformUpdate(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); - +unique_ptr Transformer::TransformUpdate(duckdb_libpgquery::PGUpdateStmt &stmt) { auto result = make_uniq(); - if (stmt->withClause) { - TransformCTE(reinterpret_cast(stmt->withClause), result->cte_map); + if (stmt.withClause) { + TransformCTE(*PGPointerCast(stmt.withClause), result->cte_map); } - result->table = TransformRangeVar(stmt->relation); - if (stmt->fromClause) { - result->from_table = TransformFrom(stmt->fromClause); + result->table = TransformRangeVar(*stmt.relation); + if (stmt.fromClause) { + result->from_table = TransformFrom(stmt.fromClause); } - result->set_info = TransformUpdateSetInfo(stmt->targetList, stmt->whereClause); + result->set_info = TransformUpdateSetInfo(stmt.targetList, stmt.whereClause); // Grab and transform the returning columns from the parser. - if (stmt->returningList) { - Transformer::TransformExpressionList(*(stmt->returningList), result->returning_list); + if (stmt.returningList) { + TransformExpressionList(*stmt.returningList, result->returning_list); } return result; diff --git a/src/parser/transform/statement/transform_use.cpp b/src/parser/transform/statement/transform_use.cpp index ace7e1a2999d..5efd5db0dd57 100644 --- a/src/parser/transform/statement/transform_use.cpp +++ b/src/parser/transform/statement/transform_use.cpp @@ -3,9 +3,8 @@ namespace duckdb { -unique_ptr Transformer::TransformUse(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - auto qualified_name = TransformQualifiedName(stmt->name); +unique_ptr Transformer::TransformUse(duckdb_libpgquery::PGUseStmt &stmt) { + auto qualified_name = TransformQualifiedName(*stmt.name); if (!IsInvalidCatalog(qualified_name.catalog)) { throw ParserException("Expected \"USE database\" or \"USE database.schema\""); } diff --git a/src/parser/transform/statement/transform_vacuum.cpp b/src/parser/transform/statement/transform_vacuum.cpp index 4d4274069a2e..c61934cca7ce 100644 --- a/src/parser/transform/statement/transform_vacuum.cpp +++ b/src/parser/transform/statement/transform_vacuum.cpp @@ -32,25 +32,21 @@ VacuumOptions ParseOptions(int options) { return result; } -unique_ptr Transformer::TransformVacuum(duckdb_libpgquery::PGNode *node) { - auto stmt = reinterpret_cast(node); - D_ASSERT(stmt); +unique_ptr Transformer::TransformVacuum(duckdb_libpgquery::PGVacuumStmt &stmt) { + auto result = make_uniq(ParseOptions(stmt.options)); - auto result = make_uniq(ParseOptions(stmt->options)); - - if (stmt->relation) { - result->info->ref = TransformRangeVar(stmt->relation); + if (stmt.relation) { + result->info->ref = TransformRangeVar(*stmt.relation); result->info->has_table = true; } - if (stmt->va_cols) { + if (stmt.va_cols) { D_ASSERT(result->info->has_table); - for (auto col_node = stmt->va_cols->head; col_node != nullptr; col_node = col_node->next) { + for (auto col_node = stmt.va_cols->head; col_node != nullptr; col_node = col_node->next) { result->info->columns.emplace_back( reinterpret_cast(col_node->data.ptr_value)->val.str); } } - return std::move(result); } diff --git a/src/parser/transform/tableref/transform_base_tableref.cpp b/src/parser/transform/tableref/transform_base_tableref.cpp index b6ca94cf9be1..6e6cbf1aecb6 100644 --- a/src/parser/transform/tableref/transform_base_tableref.cpp +++ b/src/parser/transform/tableref/transform_base_tableref.cpp @@ -3,40 +3,40 @@ namespace duckdb { -unique_ptr Transformer::TransformRangeVar(duckdb_libpgquery::PGRangeVar *root) { +unique_ptr Transformer::TransformRangeVar(duckdb_libpgquery::PGRangeVar &root) { auto result = make_uniq(); - result->alias = TransformAlias(root->alias, result->column_name_alias); - if (root->relname) { - result->table_name = root->relname; + result->alias = TransformAlias(root.alias, result->column_name_alias); + if (root.relname) { + result->table_name = root.relname; } - if (root->catalogname) { - result->catalog_name = root->catalogname; + if (root.catalogname) { + result->catalog_name = root.catalogname; } - if (root->schemaname) { - result->schema_name = root->schemaname; + if (root.schemaname) { + result->schema_name = root.schemaname; } - if (root->sample) { - result->sample = TransformSampleOptions(root->sample); + if (root.sample) { + result->sample = TransformSampleOptions(root.sample); } - result->query_location = root->location; + result->query_location = root.location; return std::move(result); } -QualifiedName Transformer::TransformQualifiedName(duckdb_libpgquery::PGRangeVar *root) { +QualifiedName Transformer::TransformQualifiedName(duckdb_libpgquery::PGRangeVar &root) { QualifiedName qname; - if (root->catalogname) { - qname.catalog = root->catalogname; + if (root.catalogname) { + qname.catalog = root.catalogname; } else { qname.catalog = INVALID_CATALOG; } - if (root->schemaname) { - qname.schema = root->schemaname; + if (root.schemaname) { + qname.schema = root.schemaname; } else { qname.schema = INVALID_SCHEMA; } - if (root->relname) { - qname.name = root->relname; + if (root.relname) { + qname.name = root.relname; } else { qname.name = string(); } diff --git a/src/parser/transform/tableref/transform_from.cpp b/src/parser/transform/tableref/transform_from.cpp index 9ea830fe10a6..84ec8ea51542 100644 --- a/src/parser/transform/tableref/transform_from.cpp +++ b/src/parser/transform/tableref/transform_from.cpp @@ -4,7 +4,7 @@ namespace duckdb { -unique_ptr Transformer::TransformFrom(duckdb_libpgquery::PGList *root) { +unique_ptr Transformer::TransformFrom(optional_ptr root) { if (!root) { return make_uniq(); } @@ -15,8 +15,8 @@ unique_ptr Transformer::TransformFrom(duckdb_libpgquery::PGList *root) JoinRef *cur_root = result.get(); idx_t list_size = 0; for (auto node = root->head; node != nullptr; node = node->next) { - auto n = reinterpret_cast(node->data.ptr_value); - unique_ptr next = TransformTableRefNode(n); + auto n = PGPointerCast(node->data.ptr_value); + unique_ptr next = TransformTableRefNode(*n); if (!cur_root->left) { cur_root->left = std::move(next); } else if (!cur_root->right) { @@ -34,8 +34,8 @@ unique_ptr Transformer::TransformFrom(duckdb_libpgquery::PGList *root) return std::move(result); } - auto n = reinterpret_cast(root->head->data.ptr_value); - return TransformTableRefNode(n); + auto n = PGPointerCast(root->head->data.ptr_value); + return TransformTableRefNode(*n); } } // namespace duckdb diff --git a/src/parser/transform/tableref/transform_join.cpp b/src/parser/transform/tableref/transform_join.cpp index a94cbce5199f..bd79241042b3 100644 --- a/src/parser/transform/tableref/transform_join.cpp +++ b/src/parser/transform/tableref/transform_join.cpp @@ -5,9 +5,9 @@ namespace duckdb { -unique_ptr Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr *root) { +unique_ptr Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr &root) { auto result = make_uniq(JoinRefType::REGULAR); - switch (root->jointype) { + switch (root.jointype) { case duckdb_libpgquery::PG_JOIN_INNER: { result->type = JoinType::INNER; break; @@ -37,14 +37,14 @@ unique_ptr Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr *r break; } default: { - throw NotImplementedException("Join type %d not supported\n", root->jointype); + throw NotImplementedException("Join type %d not supported\n", root.jointype); } } // Check the type of left arg and right arg before transform - result->left = TransformTableRefNode(root->larg); - result->right = TransformTableRefNode(root->rarg); - switch (root->joinreftype) { + result->left = TransformTableRefNode(*root.larg); + result->right = TransformTableRefNode(*root.rarg); + switch (root.joinreftype) { case duckdb_libpgquery::PG_JOIN_NATURAL: result->ref_type = JoinRefType::NATURAL; break; @@ -54,11 +54,11 @@ unique_ptr Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr *r default: break; } - result->query_location = root->location; + result->query_location = root.location; - if (root->usingClause && root->usingClause->length > 0) { + if (root.usingClause && root.usingClause->length > 0) { // usingClause is a list of strings - for (auto node = root->usingClause->head; node != nullptr; node = node->next) { + for (auto node = root.usingClause->head; node != nullptr; node = node->next) { auto target = reinterpret_cast(node->data.ptr_value); D_ASSERT(target->type == duckdb_libpgquery::T_PGString); auto column_name = string(reinterpret_cast(target)->val.str); @@ -67,10 +67,10 @@ unique_ptr Transformer::TransformJoin(duckdb_libpgquery::PGJoinExpr *r return std::move(result); } - if (!root->quals && result->using_columns.empty() && result->ref_type == JoinRefType::REGULAR) { // CROSS PRODUCT + if (!root.quals && result->using_columns.empty() && result->ref_type == JoinRefType::REGULAR) { // CROSS PRODUCT result->ref_type = JoinRefType::CROSS; } - result->condition = TransformExpression(root->quals); + result->condition = TransformExpression(root.quals); return std::move(result); } diff --git a/src/parser/transform/tableref/transform_pivot.cpp b/src/parser/transform/tableref/transform_pivot.cpp index bfbed47c037c..bc7e6e07f64e 100644 --- a/src/parser/transform/tableref/transform_pivot.cpp +++ b/src/parser/transform/tableref/transform_pivot.cpp @@ -32,10 +32,10 @@ static void TransformPivotInList(unique_ptr &expr, PivotColumn } } -PivotColumn Transformer::TransformPivotColumn(duckdb_libpgquery::PGPivot *pivot) { +PivotColumn Transformer::TransformPivotColumn(duckdb_libpgquery::PGPivot &pivot) { PivotColumn col; - if (pivot->pivot_columns) { - TransformExpressionList(*pivot->pivot_columns, col.pivot_expressions); + if (pivot.pivot_columns) { + TransformExpressionList(*pivot.pivot_columns, col.pivot_expressions); for (auto &expr : col.pivot_expressions) { if (expr->IsScalar()) { throw ParserException("Cannot pivot on constant value \"%s\"", expr->ToString()); @@ -44,14 +44,14 @@ PivotColumn Transformer::TransformPivotColumn(duckdb_libpgquery::PGPivot *pivot) throw ParserException("Cannot pivot on subquery \"%s\"", expr->ToString()); } } - } else if (pivot->unpivot_columns) { - col.unpivot_names = TransformStringList(pivot->unpivot_columns); + } else if (pivot.unpivot_columns) { + col.unpivot_names = TransformStringList(pivot.unpivot_columns); } else { throw InternalException("Either pivot_columns or unpivot_columns must be defined"); } - if (pivot->pivot_value) { - for (auto node = pivot->pivot_value->head; node != nullptr; node = node->next) { - auto n = (duckdb_libpgquery::PGNode *)node->data.ptr_value; + if (pivot.pivot_value) { + for (auto node = pivot.pivot_value->head; node != nullptr; node = node->next) { + auto n = PGPointerCast(node->data.ptr_value); auto expr = TransformExpression(n); PivotColumnEntry entry; entry.alias = expr->alias; @@ -59,36 +59,36 @@ PivotColumn Transformer::TransformPivotColumn(duckdb_libpgquery::PGPivot *pivot) col.entries.push_back(std::move(entry)); } } - if (pivot->subquery) { - col.subquery = TransformSelectNode(reinterpret_cast(pivot->subquery)); + if (pivot.subquery) { + col.subquery = TransformSelectNode(*PGPointerCast(pivot.subquery)); } - if (pivot->pivot_enum) { - col.pivot_enum = pivot->pivot_enum; + if (pivot.pivot_enum) { + col.pivot_enum = pivot.pivot_enum; } return col; } -vector Transformer::TransformPivotList(duckdb_libpgquery::PGList *list) { +vector Transformer::TransformPivotList(duckdb_libpgquery::PGList &list) { vector result; - for (auto node = list->head; node != nullptr; node = node->next) { - auto pivot = (duckdb_libpgquery::PGPivot *)node->data.ptr_value; - result.push_back(TransformPivotColumn(pivot)); + for (auto node = list.head; node != nullptr; node = node->next) { + auto pivot = PGPointerCast(node->data.ptr_value); + result.push_back(TransformPivotColumn(*pivot)); } return result; } -unique_ptr Transformer::TransformPivot(duckdb_libpgquery::PGPivotExpr *root) { +unique_ptr Transformer::TransformPivot(duckdb_libpgquery::PGPivotExpr &root) { auto result = make_uniq(); - result->source = TransformTableRefNode(root->source); - if (root->aggrs) { - TransformExpressionList(*root->aggrs, result->aggregates); + result->source = TransformTableRefNode(*root.source); + if (root.aggrs) { + TransformExpressionList(*root.aggrs, result->aggregates); } - if (root->unpivots) { - result->unpivot_names = TransformStringList(root->unpivots); + if (root.unpivots) { + result->unpivot_names = TransformStringList(root.unpivots); } - result->pivots = TransformPivotList(root->pivots); - if (root->groups) { - result->groups = TransformStringList(root->groups); + result->pivots = TransformPivotList(*root.pivots); + if (root.groups) { + result->groups = TransformStringList(root.groups); } for (auto &pivot : result->pivots) { idx_t expected_size; @@ -116,8 +116,8 @@ unique_ptr Transformer::TransformPivot(duckdb_libpgquery::PGPivotExpr } } } - result->include_nulls = root->include_nulls; - result->alias = TransformAlias(root->alias, result->column_name_alias); + result->include_nulls = root.include_nulls; + result->alias = TransformAlias(root.alias, result->column_name_alias); return std::move(result); } diff --git a/src/parser/transform/tableref/transform_subquery.cpp b/src/parser/transform/tableref/transform_subquery.cpp index b8ded2fb013f..1d5105adf725 100644 --- a/src/parser/transform/tableref/transform_subquery.cpp +++ b/src/parser/transform/tableref/transform_subquery.cpp @@ -3,16 +3,16 @@ namespace duckdb { -unique_ptr Transformer::TransformRangeSubselect(duckdb_libpgquery::PGRangeSubselect *root) { +unique_ptr Transformer::TransformRangeSubselect(duckdb_libpgquery::PGRangeSubselect &root) { Transformer subquery_transformer(*this); - auto subquery = subquery_transformer.TransformSelect(root->subquery); + auto subquery = subquery_transformer.TransformSelect(root.subquery); if (!subquery) { return nullptr; } auto result = make_uniq(std::move(subquery)); - result->alias = TransformAlias(root->alias, result->column_name_alias); - if (root->sample) { - result->sample = TransformSampleOptions(root->sample); + result->alias = TransformAlias(root.alias, result->column_name_alias); + if (root.sample) { + result->sample = TransformSampleOptions(root.sample); } return std::move(result); } diff --git a/src/parser/transform/tableref/transform_table_function.cpp b/src/parser/transform/tableref/transform_table_function.cpp index 5a66b5c1358a..d2a1c5d764a9 100644 --- a/src/parser/transform/tableref/transform_table_function.cpp +++ b/src/parser/transform/tableref/transform_table_function.cpp @@ -4,19 +4,19 @@ namespace duckdb { -unique_ptr Transformer::TransformRangeFunction(duckdb_libpgquery::PGRangeFunction *root) { - if (root->ordinality) { +unique_ptr Transformer::TransformRangeFunction(duckdb_libpgquery::PGRangeFunction &root) { + if (root.ordinality) { throw NotImplementedException("WITH ORDINALITY not implemented"); } - if (root->is_rowsfrom) { + if (root.is_rowsfrom) { throw NotImplementedException("ROWS FROM() not implemented"); } - if (root->functions->length != 1) { + if (root.functions->length != 1) { throw NotImplementedException("Need exactly one function"); } - auto function_sublist = (duckdb_libpgquery::PGList *)root->functions->head->data.ptr_value; + auto function_sublist = PGPointerCast(root.functions->head->data.ptr_value); D_ASSERT(function_sublist->length == 2); - auto call_tree = (duckdb_libpgquery::PGNode *)function_sublist->head->data.ptr_value; + auto call_tree = PGPointerCast(function_sublist->head->data.ptr_value); auto coldef = function_sublist->head->next->data.ptr_value; if (coldef) { @@ -26,20 +26,20 @@ unique_ptr Transformer::TransformRangeFunction(duckdb_libpgquery::PGRa auto result = make_uniq(); switch (call_tree->type) { case duckdb_libpgquery::T_PGFuncCall: { - auto func_call = (duckdb_libpgquery::PGFuncCall *)call_tree; - result->function = TransformFuncCall(func_call); + auto func_call = PGPointerCast(call_tree.get()); + result->function = TransformFuncCall(*func_call); result->query_location = func_call->location; break; } case duckdb_libpgquery::T_PGSQLValueFunction: - result->function = TransformSQLValueFunction((duckdb_libpgquery::PGSQLValueFunction *)call_tree); + result->function = TransformSQLValueFunction(*PGPointerCast(call_tree.get())); break; default: throw ParserException("Not a function call or value function"); } - result->alias = TransformAlias(root->alias, result->column_name_alias); - if (root->sample) { - result->sample = TransformSampleOptions(root->sample); + result->alias = TransformAlias(root.alias, result->column_name_alias); + if (root.sample) { + result->sample = TransformSampleOptions(root.sample); } return std::move(result); } diff --git a/src/parser/transform/tableref/transform_tableref.cpp b/src/parser/transform/tableref/transform_tableref.cpp index 95d90870e601..ee4de7007363 100644 --- a/src/parser/transform/tableref/transform_tableref.cpp +++ b/src/parser/transform/tableref/transform_tableref.cpp @@ -4,22 +4,22 @@ namespace duckdb { -unique_ptr Transformer::TransformTableRefNode(duckdb_libpgquery::PGNode *n) { +unique_ptr Transformer::TransformTableRefNode(duckdb_libpgquery::PGNode &n) { auto stack_checker = StackCheck(); - switch (n->type) { + switch (n.type) { case duckdb_libpgquery::T_PGRangeVar: - return TransformRangeVar(reinterpret_cast(n)); + return TransformRangeVar(PGCast(n)); case duckdb_libpgquery::T_PGJoinExpr: - return TransformJoin(reinterpret_cast(n)); + return TransformJoin(PGCast(n)); case duckdb_libpgquery::T_PGRangeSubselect: - return TransformRangeSubselect(reinterpret_cast(n)); + return TransformRangeSubselect(PGCast(n)); case duckdb_libpgquery::T_PGRangeFunction: - return TransformRangeFunction(reinterpret_cast(n)); + return TransformRangeFunction(PGCast(n)); case duckdb_libpgquery::T_PGPivotExpr: - return TransformPivot(reinterpret_cast(n)); + return TransformPivot(PGCast(n)); default: - throw NotImplementedException("From Type %d not supported", n->type); + throw NotImplementedException("From Type %d not supported", n.type); } } diff --git a/src/parser/transformer.cpp b/src/parser/transformer.cpp index ab23fe4ca0d8..f9ee687284ee 100644 --- a/src/parser/transformer.cpp +++ b/src/parser/transformer.cpp @@ -42,8 +42,8 @@ bool Transformer::TransformParseTree(duckdb_libpgquery::PGList *tree, vectorhead; entry != nullptr; entry = entry->next) { Clear(); - auto n = (duckdb_libpgquery::PGNode *)entry->data.ptr_value; - auto stmt = TransformStatement(n); + auto n = PGPointerCast(entry->data.ptr_value); + auto stmt = TransformStatement(*n); D_ASSERT(stmt); if (HasPivotEntries()) { stmt = CreatePivotStatement(std::move(stmt)); @@ -69,7 +69,7 @@ StackChecker Transformer::StackCheck(idx_t extra_stack) { return StackChecker(root, extra_stack); } -unique_ptr Transformer::TransformStatement(duckdb_libpgquery::PGNode *stmt) { +unique_ptr Transformer::TransformStatement(duckdb_libpgquery::PGNode &stmt) { auto result = TransformStatementInternal(stmt); result->n_param = ParamCount(); if (!named_param_map.empty()) { @@ -123,91 +123,91 @@ bool Transformer::HasNamedParameters() const { return !root.named_param_map.empty(); } -unique_ptr Transformer::TransformStatementInternal(duckdb_libpgquery::PGNode *stmt) { - switch (stmt->type) { +unique_ptr Transformer::TransformStatementInternal(duckdb_libpgquery::PGNode &stmt) { + switch (stmt.type) { case duckdb_libpgquery::T_PGRawStmt: { - auto raw_stmt = (duckdb_libpgquery::PGRawStmt *)stmt; - auto result = TransformStatement(raw_stmt->stmt); + auto &raw_stmt = PGCast(stmt); + auto result = TransformStatement(*raw_stmt.stmt); if (result) { - result->stmt_location = raw_stmt->stmt_location; - result->stmt_length = raw_stmt->stmt_len; + result->stmt_location = raw_stmt.stmt_location; + result->stmt_length = raw_stmt.stmt_len; } return result; } case duckdb_libpgquery::T_PGSelectStmt: - return TransformSelect(stmt); + return TransformSelect(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateStmt: - return TransformCreateTable(stmt); + return TransformCreateTable(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateSchemaStmt: - return TransformCreateSchema(stmt); + return TransformCreateSchema(PGCast(stmt)); case duckdb_libpgquery::T_PGViewStmt: - return TransformCreateView(stmt); + return TransformCreateView(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateSeqStmt: - return TransformCreateSequence(stmt); + return TransformCreateSequence(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateFunctionStmt: - return TransformCreateFunction(stmt); + return TransformCreateFunction(PGCast(stmt)); case duckdb_libpgquery::T_PGDropStmt: - return TransformDrop(stmt); + return TransformDrop(PGCast(stmt)); case duckdb_libpgquery::T_PGInsertStmt: - return TransformInsert(stmt); + return TransformInsert(PGCast(stmt)); case duckdb_libpgquery::T_PGCopyStmt: - return TransformCopy(stmt); + return TransformCopy(PGCast(stmt)); case duckdb_libpgquery::T_PGTransactionStmt: - return TransformTransaction(stmt); + return TransformTransaction(PGCast(stmt)); case duckdb_libpgquery::T_PGDeleteStmt: - return TransformDelete(stmt); + return TransformDelete(PGCast(stmt)); case duckdb_libpgquery::T_PGUpdateStmt: - return TransformUpdate(stmt); + return TransformUpdate(PGCast(stmt)); case duckdb_libpgquery::T_PGIndexStmt: - return TransformCreateIndex(stmt); + return TransformCreateIndex(PGCast(stmt)); case duckdb_libpgquery::T_PGAlterTableStmt: - return TransformAlter(stmt); + return TransformAlter(PGCast(stmt)); case duckdb_libpgquery::T_PGRenameStmt: - return TransformRename(stmt); + return TransformRename(PGCast(stmt)); case duckdb_libpgquery::T_PGPrepareStmt: - return TransformPrepare(stmt); + return TransformPrepare(PGCast(stmt)); case duckdb_libpgquery::T_PGExecuteStmt: - return TransformExecute(stmt); + return TransformExecute(PGCast(stmt)); case duckdb_libpgquery::T_PGDeallocateStmt: - return TransformDeallocate(stmt); + return TransformDeallocate(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateTableAsStmt: - return TransformCreateTableAs(stmt); + return TransformCreateTableAs(PGCast(stmt)); case duckdb_libpgquery::T_PGPragmaStmt: - return TransformPragma(stmt); + return TransformPragma(PGCast(stmt)); case duckdb_libpgquery::T_PGExportStmt: - return TransformExport(stmt); + return TransformExport(PGCast(stmt)); case duckdb_libpgquery::T_PGImportStmt: - return TransformImport(stmt); + return TransformImport(PGCast(stmt)); case duckdb_libpgquery::T_PGExplainStmt: - return TransformExplain(stmt); + return TransformExplain(PGCast(stmt)); case duckdb_libpgquery::T_PGVacuumStmt: - return TransformVacuum(stmt); + return TransformVacuum(PGCast(stmt)); case duckdb_libpgquery::T_PGVariableShowStmt: - return TransformShow(stmt); + return TransformShow(PGCast(stmt)); case duckdb_libpgquery::T_PGVariableShowSelectStmt: - return TransformShowSelect(stmt); + return TransformShowSelect(PGCast(stmt)); case duckdb_libpgquery::T_PGCallStmt: - return TransformCall(stmt); + return TransformCall(PGCast(stmt)); case duckdb_libpgquery::T_PGVariableSetStmt: - return TransformSet(stmt); + return TransformSet(PGCast(stmt)); case duckdb_libpgquery::T_PGCheckPointStmt: - return TransformCheckpoint(stmt); + return TransformCheckpoint(PGCast(stmt)); case duckdb_libpgquery::T_PGLoadStmt: - return TransformLoad(stmt); + return TransformLoad(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateTypeStmt: - return TransformCreateType(stmt); + return TransformCreateType(PGCast(stmt)); case duckdb_libpgquery::T_PGAlterSeqStmt: - return TransformAlterSequence(stmt); + return TransformAlterSequence(PGCast(stmt)); case duckdb_libpgquery::T_PGAttachStmt: - return TransformAttach(stmt); + return TransformAttach(PGCast(stmt)); case duckdb_libpgquery::T_PGDetachStmt: - return TransformDetach(stmt); + return TransformDetach(PGCast(stmt)); case duckdb_libpgquery::T_PGUseStmt: - return TransformUse(stmt); + return TransformUse(PGCast(stmt)); case duckdb_libpgquery::T_PGCreateDatabaseStmt: - return TransformCreateDatabase(stmt); + return TransformCreateDatabase(PGCast(stmt)); default: - throw NotImplementedException(NodetypeToString(stmt->type)); + throw NotImplementedException(NodetypeToString(stmt.type)); } } diff --git a/src/planner/bind_context.cpp b/src/planner/bind_context.cpp index 68b4de2a7457..9f1f1e7cfc5c 100644 --- a/src/planner/bind_context.cpp +++ b/src/planner/bind_context.cpp @@ -12,6 +12,7 @@ #include "duckdb/parser/tableref/table_function_ref.hpp" #include "duckdb/planner/bound_query_node.hpp" #include "duckdb/planner/expression/bound_columnref_expression.hpp" +#include "duckdb/catalog/catalog_entry/view_catalog_entry.hpp" #include "duckdb/catalog/catalog_entry/table_catalog_entry.hpp" #include "duckdb/planner/expression_binder/constant_binder.hpp" From 45846c79c18ac49d0254428736e961c1250c8f40 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Sun, 21 May 2023 20:58:53 +0200 Subject: [PATCH 05/28] Get rid of more C-style casts --- .../aggregate/distributive/approx_count.cpp | 6 +-- .../aggregate/distributive/bitstring_agg.cpp | 18 +++---- .../aggregate/distributive/minmax.cpp | 4 +- .../holistic/approximate_quantile.cpp | 24 ++++----- .../aggregate/holistic/quantile.cpp | 40 +++++++-------- .../aggregate/holistic/reservoir_quantile.cpp | 33 ++++++------ .../aggregate/nested/histogram.cpp | 5 +- src/core_functions/aggregate/nested/list.cpp | 8 +-- src/core_functions/scalar/date/date_part.cpp | 4 +- src/core_functions/scalar/date/strftime.cpp | 4 +- .../scalar/generic/current_setting.cpp | 2 +- src/core_functions/scalar/generic/stats.cpp | 2 +- src/core_functions/scalar/list/flatten.cpp | 6 +-- .../scalar/list/list_lambdas.cpp | 11 ++-- src/core_functions/scalar/list/list_sort.cpp | 2 +- src/core_functions/scalar/map/cardinality.cpp | 2 +- src/core_functions/scalar/map/map.cpp | 4 +- src/core_functions/scalar/map/map_concat.cpp | 2 +- src/core_functions/scalar/map/map_extract.cpp | 14 ++--- src/core_functions/scalar/math/numeric.cpp | 6 +-- .../scalar/string/starts_with.cpp | 5 +- .../scalar/string/string_split.cpp | 6 +-- src/core_functions/scalar/string/trim.cpp | 2 +- .../scalar/union/union_extract.cpp | 2 +- src/function/aggregate/distributive/first.cpp | 2 +- .../aggregate/sorted_aggregate_function.cpp | 12 ++--- .../scalar/generic/constant_or_null.cpp | 2 +- src/function/scalar_function.cpp | 4 +- src/include/duckdb/common/types/vector.hpp | 9 ++++ src/include/duckdb/parser/transformer.hpp | 15 +++--- src/parser/query_node/select_node.cpp | 2 +- .../transform/expression/transform_cast.cpp | 2 +- .../expression/transform_columnref.cpp | 2 +- .../transform/helpers/transform_groupby.cpp | 36 ++++++------- .../transform/helpers/transform_sample.cpp | 6 +-- .../transform/helpers/transform_typename.cpp | 51 +++++++++---------- .../statement/transform_create_index.cpp | 8 +-- .../statement/transform_create_table.cpp | 2 +- .../statement/transform_create_type.cpp | 10 ++-- .../transform/statement/transform_upsert.cpp | 8 +-- 40 files changed, 193 insertions(+), 190 deletions(-) diff --git a/src/core_functions/aggregate/distributive/approx_count.cpp b/src/core_functions/aggregate/distributive/approx_count.cpp index 89de504a1ce1..ca4154456e91 100644 --- a/src/core_functions/aggregate/distributive/approx_count.cpp +++ b/src/core_functions/aggregate/distributive/approx_count.cpp @@ -69,7 +69,7 @@ static void ApproxCountDistinctSimpleUpdateFunction(Vector inputs[], AggregateIn data_ptr_t state, idx_t count) { D_ASSERT(input_count == 1); - auto agg_state = (ApproxDistinctCountState *)state; + auto agg_state = reinterpret_cast(state); if (!agg_state->log) { agg_state->log = new HyperLogLog(); } @@ -91,7 +91,7 @@ static void ApproxCountDistinctUpdateFunction(Vector inputs[], AggregateInputDat UnifiedVectorFormat sdata; state_vector.ToUnifiedFormat(count, sdata); - auto states = (ApproxDistinctCountState **)sdata.data; + auto states = UnifiedVectorFormat::GetDataNoConst(sdata); uint64_t *indices = nullptr; uint8_t *counts = nullptr; @@ -111,7 +111,7 @@ static void ApproxCountDistinctUpdateFunction(Vector inputs[], AggregateInputDat inputs[0].ToUnifiedFormat(count, vdata); HyperLogLog::ProcessEntries(vdata, inputs[0].GetType(), indices, counts, count); - HyperLogLog::AddToLogs(vdata, count, indices, counts, (HyperLogLog ***)states, sdata.sel); + HyperLogLog::AddToLogs(vdata, count, indices, counts, reinterpret_cast(states), sdata.sel); } AggregateFunction GetApproxCountDistinctFunction(const LogicalType &input_type) { diff --git a/src/core_functions/aggregate/distributive/bitstring_agg.cpp b/src/core_functions/aggregate/distributive/bitstring_agg.cpp index f4d88fd6f170..31d5f0115f92 100644 --- a/src/core_functions/aggregate/distributive/bitstring_agg.cpp +++ b/src/core_functions/aggregate/distributive/bitstring_agg.cpp @@ -54,17 +54,17 @@ struct BitStringAggOperation { template static void Operation(STATE *state, AggregateInputData &data, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { - auto bind_agg_data = (BitstringAggBindData *)data.bind_data; + auto &bind_agg_data = data.bind_data->template Cast(); if (!state->is_set) { - if (bind_agg_data->min.IsNull() || bind_agg_data->max.IsNull()) { + if (bind_agg_data.min.IsNull() || bind_agg_data.max.IsNull()) { throw BinderException( "Could not retrieve required statistics. Alternatively, try by providing the statistics " "explicitly: BITSTRING_AGG(col, min, max) "); } - state->min = bind_agg_data->min.GetValue(); - state->max = bind_agg_data->max.GetValue(); + state->min = bind_agg_data.min.GetValue(); + state->max = bind_agg_data.max.GetValue(); idx_t bit_range = - GetRange(bind_agg_data->min.GetValue(), bind_agg_data->max.GetValue()); + GetRange(bind_agg_data.min.GetValue(), bind_agg_data.max.GetValue()); if (bit_range > MAX_BIT_RANGE) { throw OutOfRangeException( "The range between min and max value (%s <-> %s) is too large for bitstring aggregation", @@ -78,7 +78,7 @@ struct BitStringAggOperation { state->is_set = true; } if (input[idx] >= state->min && input[idx] <= state->max) { - Execute(state, input[idx], bind_agg_data->min.GetValue()); + Execute(state, input[idx], bind_agg_data.min.GetValue()); } else { throw OutOfRangeException("Value %s is outside of provided min and max range (%s <-> %s)", NumericHelper::ToString(input[idx]), NumericHelper::ToString(state->min), @@ -190,9 +190,9 @@ unique_ptr BitstringPropagateStats(ClientContext &context, Bound throw BinderException("Could not retrieve required statistics. Alternatively, try by providing the statistics " "explicitly: BITSTRING_AGG(col, min, max) "); } - auto bind_agg_data = (BitstringAggBindData *)input.bind_data; - bind_agg_data->min = NumericStats::Min(input.child_stats[0]); - bind_agg_data->max = NumericStats::Max(input.child_stats[0]); + auto &bind_agg_data = input.bind_data->Cast(); + bind_agg_data.min = NumericStats::Min(input.child_stats[0]); + bind_agg_data.max = NumericStats::Max(input.child_stats[0]); return nullptr; } diff --git a/src/core_functions/aggregate/distributive/minmax.cpp b/src/core_functions/aggregate/distributive/minmax.cpp index d8a62d2bef59..ef9e334d2fa9 100644 --- a/src/core_functions/aggregate/distributive/minmax.cpp +++ b/src/core_functions/aggregate/distributive/minmax.cpp @@ -341,8 +341,8 @@ static bool TemplatedOptimumList(Vector &left, idx_t lidx, idx_t lcount, Vector auto &lchild = ListVector::GetEntry(left); auto &rchild = ListVector::GetEntry(right); - auto ldata = (const list_entry_t *)lvdata.data; - auto rdata = (const list_entry_t *)rvdata.data; + auto ldata = UnifiedVectorFormat::GetData(lvdata); + auto rdata = UnifiedVectorFormat::GetData(rvdata); auto &lval = ldata[lidx]; auto &rval = rdata[ridx]; diff --git a/src/core_functions/aggregate/holistic/approximate_quantile.cpp b/src/core_functions/aggregate/holistic/approximate_quantile.cpp index 49a35435fab9..822909506460 100644 --- a/src/core_functions/aggregate/holistic/approximate_quantile.cpp +++ b/src/core_functions/aggregate/holistic/approximate_quantile.cpp @@ -38,8 +38,8 @@ struct ApproximateQuantileBindData : public FunctionData { static void Serialize(FieldWriter &writer, const FunctionData *bind_data_p, const AggregateFunction &function) { D_ASSERT(bind_data_p); - auto bind_data = (ApproximateQuantileBindData *)bind_data_p; - writer.WriteList(bind_data->quantiles); + auto &bind_data = bind_data_p->Cast(); + writer.WriteList(bind_data.quantiles); } static unique_ptr Deserialize(ClientContext &context, FieldReader &reader, @@ -119,9 +119,9 @@ struct ApproxQuantileScalarOperation : public ApproxQuantileOperation { D_ASSERT(state->h); D_ASSERT(aggr_input_data.bind_data); state->h->compress(); - auto bind_data = (ApproximateQuantileBindData *)aggr_input_data.bind_data; - D_ASSERT(bind_data->quantiles.size() == 1); - target[idx] = Cast::template Operation(state->h->quantile(bind_data->quantiles[0])); + auto &bind_data = aggr_input_data.bind_data->template Cast(); + D_ASSERT(bind_data.quantiles.size() == 1); + target[idx] = Cast::template Operation(state->h->quantile(bind_data.quantiles[0])); } }; @@ -220,11 +220,11 @@ struct ApproxQuantileListOperation : public ApproxQuantileOperation { } D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (ApproximateQuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->template Cast(); auto &result = ListVector::GetEntry(result_list); auto ridx = ListVector::GetListSize(result_list); - ListVector::Reserve(result_list, ridx + bind_data->quantiles.size()); + ListVector::Reserve(result_list, ridx + bind_data.quantiles.size()); auto rdata = FlatVector::GetData(result); D_ASSERT(state->h); @@ -232,9 +232,9 @@ struct ApproxQuantileListOperation : public ApproxQuantileOperation { auto &entry = target[idx]; entry.offset = ridx; - entry.length = bind_data->quantiles.size(); + entry.length = bind_data.quantiles.size(); for (size_t q = 0; q < entry.length; ++q) { - const auto &quantile = bind_data->quantiles[q]; + const auto &quantile = bind_data.quantiles[q]; rdata[ridx + q] = Cast::template Operation(state->h->quantile(quantile)); } @@ -247,11 +247,11 @@ struct ApproxQuantileListOperation : public ApproxQuantileOperation { D_ASSERT(result.GetType().id() == LogicalTypeId::LIST); D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (ApproximateQuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->template Cast(); if (states.GetVectorType() == VectorType::CONSTANT_VECTOR) { result.SetVectorType(VectorType::CONSTANT_VECTOR); - ListVector::Reserve(result, bind_data->quantiles.size()); + ListVector::Reserve(result, bind_data.quantiles.size()); auto sdata = ConstantVector::GetData(states); auto rdata = ConstantVector::GetData(result); @@ -260,7 +260,7 @@ struct ApproxQuantileListOperation : public ApproxQuantileOperation { } else { D_ASSERT(states.GetVectorType() == VectorType::FLAT_VECTOR); result.SetVectorType(VectorType::FLAT_VECTOR); - ListVector::Reserve(result, (offset + count) * bind_data->quantiles.size()); + ListVector::Reserve(result, (offset + count) * bind_data.quantiles.size()); auto sdata = FlatVector::GetData(states); auto rdata = FlatVector::GetData(result); diff --git a/src/core_functions/aggregate/holistic/quantile.cpp b/src/core_functions/aggregate/holistic/quantile.cpp index 0430fe4d7f22..b178bfa92fa1 100644 --- a/src/core_functions/aggregate/holistic/quantile.cpp +++ b/src/core_functions/aggregate/holistic/quantile.cpp @@ -493,11 +493,11 @@ static void ExecuteListFinalize(Vector &states, AggregateInputData &aggr_input_d D_ASSERT(result.GetType().id() == LogicalTypeId::LIST); D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (QuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->Cast(); if (states.GetVectorType() == VectorType::CONSTANT_VECTOR) { result.SetVectorType(VectorType::CONSTANT_VECTOR); - ListVector::Reserve(result, bind_data->quantiles.size()); + ListVector::Reserve(result, bind_data.quantiles.size()); auto sdata = ConstantVector::GetData(states); auto rdata = ConstantVector::GetData(result); @@ -506,7 +506,7 @@ static void ExecuteListFinalize(Vector &states, AggregateInputData &aggr_input_d } else { D_ASSERT(states.GetVectorType() == VectorType::FLAT_VECTOR); result.SetVectorType(VectorType::FLAT_VECTOR); - ListVector::Reserve(result, (offset + count) * bind_data->quantiles.size()); + ListVector::Reserve(result, (offset + count) * bind_data.quantiles.size()); auto sdata = FlatVector::GetData(states); auto rdata = FlatVector::GetData(result); @@ -540,9 +540,9 @@ struct QuantileScalarOperation : public QuantileOperation { return; } D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (QuantileBindData *)aggr_input_data.bind_data; - D_ASSERT(bind_data->quantiles.size() == 1); - Interpolator interp(bind_data->quantiles[0], state->v.size(), bind_data->desc); + auto &bind_data = aggr_input_data.bind_data->Cast(); + D_ASSERT(bind_data.quantiles.size() == 1); + Interpolator interp(bind_data.quantiles[0], state->v.size(), bind_data.desc); target[idx] = interp.template Operation(state->v.data(), result); } @@ -563,10 +563,10 @@ struct QuantileScalarOperation : public QuantileOperation { D_ASSERT(index); D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (QuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->Cast(); // Find the two positions needed - const auto q = bind_data->quantiles[0]; + const auto q = bind_data.quantiles[0]; bool replace = false; if (frame.first == prev.first + 1 && frame.second == prev.second + 1) { @@ -670,11 +670,11 @@ struct QuantileListOperation : public QuantileOperation { } D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (QuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->Cast(); auto &result = ListVector::GetEntry(result_list); auto ridx = ListVector::GetListSize(result_list); - ListVector::Reserve(result_list, ridx + bind_data->quantiles.size()); + ListVector::Reserve(result_list, ridx + bind_data.quantiles.size()); auto rdata = FlatVector::GetData(result); auto v_t = state->v.data(); @@ -683,14 +683,14 @@ struct QuantileListOperation : public QuantileOperation { auto &entry = target[idx]; entry.offset = ridx; idx_t lower = 0; - for (const auto &q : bind_data->order) { - const auto &quantile = bind_data->quantiles[q]; - Interpolator interp(quantile, state->v.size(), bind_data->desc); + for (const auto &q : bind_data.order) { + const auto &quantile = bind_data.quantiles[q]; + Interpolator interp(quantile, state->v.size(), bind_data.desc); interp.begin = lower; rdata[ridx + q] = interp.template Operation(v_t, result); lower = interp.FRN; } - entry.length = bind_data->quantiles.size(); + entry.length = bind_data.quantiles.size(); ListVector::SetListSize(result_list, entry.offset + entry.length); } @@ -700,7 +700,7 @@ struct QuantileListOperation : public QuantileOperation { AggregateInputData &aggr_input_data, STATE *state, const FrameBounds &frame, const FrameBounds &prev, Vector &list, idx_t lidx, idx_t bias) { D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (QuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->Cast(); QuantileIncluded included(fmask, dmask, bias); @@ -709,7 +709,7 @@ struct QuantileListOperation : public QuantileOperation { auto &lmask = FlatVector::Validity(list); auto &lentry = ldata[lidx]; lentry.offset = ListVector::GetListSize(list); - lentry.length = bind_data->quantiles.size(); + lentry.length = bind_data.quantiles.size(); ListVector::Reserve(list, lentry.offset + lentry.length); ListVector::SetListSize(list, lentry.offset + lentry.length); @@ -734,8 +734,8 @@ struct QuantileListOperation : public QuantileOperation { const auto j = ReplaceIndex(index, frame, prev); // We can only replace if the number of NULLs has not changed if (included.AllValid() || included(prev.first) == included(prev.second)) { - for (const auto &q : bind_data->order) { - const auto &quantile = bind_data->quantiles[q]; + for (const auto &q : bind_data.order) { + const auto &quantile = bind_data.quantiles[q]; Interpolator interp(quantile, prev_pos, false); const auto replace = CanReplace(index, data, j, interp.FRN, interp.CRN, included); if (replace < 0) { @@ -765,8 +765,8 @@ struct QuantileListOperation : public QuantileOperation { if (state->pos) { using ID = QuantileIndirect; ID indirect(data); - for (const auto &q : bind_data->order) { - const auto &quantile = bind_data->quantiles[q]; + for (const auto &q : bind_data.order) { + const auto &quantile = bind_data.quantiles[q]; Interpolator interp(quantile, state->pos, false); if (replaceable.first <= interp.FRN && interp.CRN <= replaceable.second) { rdata[lentry.offset + q] = interp.template Replace(index, result, indirect); diff --git a/src/core_functions/aggregate/holistic/reservoir_quantile.cpp b/src/core_functions/aggregate/holistic/reservoir_quantile.cpp index c8c7950f0ba0..7b3369f49fc2 100644 --- a/src/core_functions/aggregate/holistic/reservoir_quantile.cpp +++ b/src/core_functions/aggregate/holistic/reservoir_quantile.cpp @@ -66,9 +66,9 @@ struct ReservoirQuantileBindData : public FunctionData { static void Serialize(FieldWriter &writer, const FunctionData *bind_data_p, const AggregateFunction &function) { D_ASSERT(bind_data_p); - auto bind_data = (ReservoirQuantileBindData *)bind_data_p; - writer.WriteList(bind_data->quantiles); - writer.WriteField(bind_data->sample_size); + auto &bind_data = bind_data_p->Cast(); + writer.WriteList(bind_data.quantiles); + writer.WriteField(bind_data.sample_size); } static unique_ptr Deserialize(ClientContext &context, FieldReader &reader, @@ -102,16 +102,15 @@ struct ReservoirQuantileOperation { template static void Operation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { - auto bind_data = (ReservoirQuantileBindData *)aggr_input_data.bind_data; - D_ASSERT(bind_data); + auto &bind_data = aggr_input_data.bind_data->template Cast(); if (state->pos == 0) { - state->Resize(bind_data->sample_size); + state->Resize(bind_data.sample_size); } if (!state->r_samp) { state->r_samp = new BaseReservoirSampling(); } D_ASSERT(state->v); - state->FillReservoir(bind_data->sample_size, data[idx]); + state->FillReservoir(bind_data.sample_size, data[idx]); } template @@ -157,10 +156,10 @@ struct ReservoirQuantileScalarOperation : public ReservoirQuantileOperation { } D_ASSERT(state->v); D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (ReservoirQuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->template Cast(); auto v_t = state->v; - D_ASSERT(bind_data->quantiles.size() == 1); - auto offset = (idx_t)((double)(state->pos - 1) * bind_data->quantiles[0]); + D_ASSERT(bind_data.quantiles.size() == 1); + auto offset = (idx_t)((double)(state->pos - 1) * bind_data.quantiles[0]); std::nth_element(v_t, v_t + offset, v_t + state->pos); target[idx] = v_t[offset]; } @@ -217,11 +216,11 @@ struct ReservoirQuantileListOperation : public ReservoirQuantileOperation { } D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (ReservoirQuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->template Cast(); auto &result = ListVector::GetEntry(result_list); auto ridx = ListVector::GetListSize(result_list); - ListVector::Reserve(result_list, ridx + bind_data->quantiles.size()); + ListVector::Reserve(result_list, ridx + bind_data.quantiles.size()); auto rdata = FlatVector::GetData(result); auto v_t = state->v; @@ -229,9 +228,9 @@ struct ReservoirQuantileListOperation : public ReservoirQuantileOperation { auto &entry = target[idx]; entry.offset = ridx; - entry.length = bind_data->quantiles.size(); + entry.length = bind_data.quantiles.size(); for (size_t q = 0; q < entry.length; ++q) { - const auto &quantile = bind_data->quantiles[q]; + const auto &quantile = bind_data.quantiles[q]; auto offset = (idx_t)((double)(state->pos - 1) * quantile); std::nth_element(v_t, v_t + offset, v_t + state->pos); rdata[ridx + q] = v_t[offset]; @@ -246,11 +245,11 @@ struct ReservoirQuantileListOperation : public ReservoirQuantileOperation { D_ASSERT(result.GetType().id() == LogicalTypeId::LIST); D_ASSERT(aggr_input_data.bind_data); - auto bind_data = (ReservoirQuantileBindData *)aggr_input_data.bind_data; + auto &bind_data = aggr_input_data.bind_data->Cast(); if (states.GetVectorType() == VectorType::CONSTANT_VECTOR) { result.SetVectorType(VectorType::CONSTANT_VECTOR); - ListVector::Reserve(result, bind_data->quantiles.size()); + ListVector::Reserve(result, bind_data.quantiles.size()); auto sdata = ConstantVector::GetData(states); auto rdata = ConstantVector::GetData(result); @@ -259,7 +258,7 @@ struct ReservoirQuantileListOperation : public ReservoirQuantileOperation { } else { D_ASSERT(states.GetVectorType() == VectorType::FLAT_VECTOR); result.SetVectorType(VectorType::FLAT_VECTOR); - ListVector::Reserve(result, (offset + count) * bind_data->quantiles.size()); + ListVector::Reserve(result, (offset + count) * bind_data.quantiles.size()); auto sdata = FlatVector::GetData(states); auto rdata = FlatVector::GetData(result); diff --git a/src/core_functions/aggregate/nested/histogram.cpp b/src/core_functions/aggregate/nested/histogram.cpp index 141f5478d3a1..22c3b0b6b145 100644 --- a/src/core_functions/aggregate/nested/histogram.cpp +++ b/src/core_functions/aggregate/nested/histogram.cpp @@ -33,16 +33,15 @@ struct HistogramFunctor { struct HistogramStringFunctor { template > static void HistogramUpdate(UnifiedVectorFormat &sdata, UnifiedVectorFormat &input_data, idx_t count) { - auto states = (HistogramAggState **)sdata.data; + auto input_strings = UnifiedVectorFormat::GetData(input_data); for (idx_t i = 0; i < count; i++) { if (input_data.validity.RowIsValid(input_data.sel->get_index(i))) { auto state = states[sdata.sel->get_index(i)]; if (!state->hist) { state->hist = new MAP_TYPE(); } - auto value = (string_t *)input_data.data; - (*state->hist)[value[input_data.sel->get_index(i)].GetString()]++; + (*state->hist)[input_strings[input_data.sel->get_index(i)].GetString()]++; } } } diff --git a/src/core_functions/aggregate/nested/list.cpp b/src/core_functions/aggregate/nested/list.cpp index 617f18a15595..bdbf44c864ff 100644 --- a/src/core_functions/aggregate/nested/list.cpp +++ b/src/core_functions/aggregate/nested/list.cpp @@ -35,7 +35,7 @@ struct ListBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const ListBindData &)other_p; + auto &other = other_p.Cast(); return stype == other.stype; } }; @@ -80,7 +80,7 @@ static void ListUpdateFunction(Vector inputs[], AggregateInputData &aggr_input_d UnifiedVectorFormat sdata; state_vector.ToUnifiedFormat(count, sdata); - auto states = (ListAggState **)sdata.data; + auto states = UnifiedVectorFormat::GetData(sdata); RecursiveFlatten(input, count); auto &list_bind_data = aggr_input_data.bind_data->Cast(); @@ -94,7 +94,7 @@ static void ListUpdateFunction(Vector inputs[], AggregateInputData &aggr_input_d static void ListCombineFunction(Vector &state, Vector &combined, AggregateInputData &aggr_input_data, idx_t count) { UnifiedVectorFormat sdata; state.ToUnifiedFormat(count, sdata); - auto states_ptr = (ListAggState **)sdata.data; + auto states_ptr = UnifiedVectorFormat::GetData(sdata); auto &list_bind_data = aggr_input_data.bind_data->Cast(); @@ -125,7 +125,7 @@ static void ListFinalize(Vector &state_vector, AggregateInputData &aggr_input_da idx_t offset) { UnifiedVectorFormat sdata; state_vector.ToUnifiedFormat(count, sdata); - auto states = (ListAggState **)sdata.data; + auto states = UnifiedVectorFormat::GetData(sdata); D_ASSERT(result.GetType().id() == LogicalTypeId::LIST); diff --git a/src/core_functions/scalar/date/date_part.cpp b/src/core_functions/scalar/date/date_part.cpp index 07d94581c1de..e07a7c5dccdb 100644 --- a/src/core_functions/scalar/date/date_part.cpp +++ b/src/core_functions/scalar/date/date_part.cpp @@ -1264,7 +1264,7 @@ struct StructDatePart { template static void Function(DataChunk &args, ExpressionState &state, Vector &result) { auto &func_expr = state.expr.Cast(); - auto &info = (BindData &)*func_expr.bind_info; + auto &info = func_expr.bind_info->Cast(); D_ASSERT(args.ColumnCount() == 1); const auto count = args.size(); @@ -1372,7 +1372,7 @@ struct StructDatePart { static void SerializeFunction(FieldWriter &writer, const FunctionData *bind_data_p, const ScalarFunction &function) { D_ASSERT(bind_data_p); - auto &info = (BindData &)*bind_data_p; + auto &info = bind_data_p->Cast(); writer.WriteSerializable(info.stype); writer.WriteList(info.part_codes); } diff --git a/src/core_functions/scalar/date/strftime.cpp b/src/core_functions/scalar/date/strftime.cpp index 96c43277c29e..2a586b47e97d 100644 --- a/src/core_functions/scalar/date/strftime.cpp +++ b/src/core_functions/scalar/date/strftime.cpp @@ -25,7 +25,7 @@ struct StrfTimeBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const StrfTimeBindData &)other_p; + auto &other = other_p.Cast(); return format_string == other.format_string; } }; @@ -111,7 +111,7 @@ struct StrpTimeBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const StrpTimeBindData &)other_p; + auto &other = other_p.Cast(); return format_strings == other.format_strings; } }; diff --git a/src/core_functions/scalar/generic/current_setting.cpp b/src/core_functions/scalar/generic/current_setting.cpp index 4250576a8113..a9713e532022 100644 --- a/src/core_functions/scalar/generic/current_setting.cpp +++ b/src/core_functions/scalar/generic/current_setting.cpp @@ -19,7 +19,7 @@ struct CurrentSettingBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const CurrentSettingBindData &)other_p; + auto &other = other_p.Cast(); return Value::NotDistinctFrom(value, other.value); } }; diff --git a/src/core_functions/scalar/generic/stats.cpp b/src/core_functions/scalar/generic/stats.cpp index 4bd514475c84..d19dcfc9fda5 100644 --- a/src/core_functions/scalar/generic/stats.cpp +++ b/src/core_functions/scalar/generic/stats.cpp @@ -15,7 +15,7 @@ struct StatsBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const StatsBindData &)other_p; + auto &other = other_p.Cast(); return stats == other.stats; } }; diff --git a/src/core_functions/scalar/list/flatten.cpp b/src/core_functions/scalar/list/flatten.cpp index 027d0dbdf8ed..34cd55157cd5 100644 --- a/src/core_functions/scalar/list/flatten.cpp +++ b/src/core_functions/scalar/list/flatten.cpp @@ -19,8 +19,7 @@ void ListFlattenFunction(DataChunk &args, ExpressionState &state, Vector &result UnifiedVectorFormat list_data; input.ToUnifiedFormat(count, list_data); - auto list_entries = (list_entry_t *)list_data.data; - + auto list_entries = UnifiedVectorFormat::GetData(list_data); auto &child_vector = ListVector::GetEntry(input); result.SetVectorType(VectorType::FLAT_VECTOR); @@ -28,7 +27,6 @@ void ListFlattenFunction(DataChunk &args, ExpressionState &state, Vector &result auto &result_validity = FlatVector::Validity(result); if (child_vector.GetType().id() == LogicalTypeId::SQLNULL) { - auto result_entries = FlatVector::GetData(result); for (idx_t i = 0; i < count; i++) { auto list_index = list_data.sel->get_index(i); if (!list_data.validity.RowIsValid(list_index)) { @@ -47,7 +45,7 @@ void ListFlattenFunction(DataChunk &args, ExpressionState &state, Vector &result auto child_size = ListVector::GetListSize(input); UnifiedVectorFormat child_data; child_vector.ToUnifiedFormat(child_size, child_data); - auto child_entries = (list_entry_t *)child_data.data; + auto child_entries = UnifiedVectorFormat::GetData(child_data); auto &data_vector = ListVector::GetEntry(child_vector); idx_t offset = 0; diff --git a/src/core_functions/scalar/list/list_lambdas.cpp b/src/core_functions/scalar/list/list_lambdas.cpp index f66a5b2dca2c..14f44c97197b 100644 --- a/src/core_functions/scalar/list/list_lambdas.cpp +++ b/src/core_functions/scalar/list/list_lambdas.cpp @@ -63,7 +63,7 @@ static void AppendFilteredToResult(Vector &lambda_vector, list_entry_t *result_e UnifiedVectorFormat lambda_data; lambda_vector.ToUnifiedFormat(elem_cnt, lambda_data); - auto lambda_values = (bool *)lambda_data.data; + auto lambda_values = UnifiedVectorFormat::GetData(lambda_data); auto &lambda_validity = lambda_data.validity; // compute the new lengths and offsets, and create a selection vector @@ -167,7 +167,7 @@ static void ListLambdaFunction(DataChunk &args, ExpressionState &state, Vector & // get the lists data UnifiedVectorFormat lists_data; lists.ToUnifiedFormat(count, lists_data); - auto list_entries = (list_entry_t *)lists_data.data; + auto list_entries = UnifiedVectorFormat::GetData(lists_data); // get the lambda expression auto &func_expr = state.expr.Cast(); @@ -321,8 +321,7 @@ static void ListFilterFunction(DataChunk &args, ExpressionState &state, Vector & template static unique_ptr ListLambdaBind(ClientContext &context, ScalarFunction &bound_function, vector> &arguments) { - - auto &bound_lambda_expr = (BoundLambdaExpression &)*arguments[1]; + auto &bound_lambda_expr = arguments[1]->Cast(); if (bound_lambda_expr.parameter_count != LAMBDA_PARAM_CNT) { throw BinderException("Incorrect number of parameters in lambda function! " + bound_function.name + " expects " + to_string(LAMBDA_PARAM_CNT) + " parameter(s)."); @@ -355,7 +354,7 @@ static unique_ptr ListTransformBind(ClientContext &context, Scalar throw BinderException("Invalid lambda expression!"); } - auto &bound_lambda_expr = (BoundLambdaExpression &)*arguments[1]; + auto &bound_lambda_expr = arguments[1]->Cast(); bound_function.return_type = LogicalType::LIST(bound_lambda_expr.lambda_expr->return_type); return ListLambdaBind<1>(context, bound_function, arguments); } @@ -370,7 +369,7 @@ static unique_ptr ListFilterBind(ClientContext &context, ScalarFun } // try to cast to boolean, if the return type of the lambda filter expression is not already boolean - auto &bound_lambda_expr = (BoundLambdaExpression &)*arguments[1]; + auto &bound_lambda_expr = arguments[1]->Cast(); if (bound_lambda_expr.lambda_expr->return_type != LogicalType::BOOLEAN) { auto cast_lambda_expr = BoundCastExpression::AddCastToType(context, std::move(bound_lambda_expr.lambda_expr), LogicalType::BOOLEAN); diff --git a/src/core_functions/scalar/list/list_sort.cpp b/src/core_functions/scalar/list/list_sort.cpp index a87b3265bfd9..eb3c6c8b8e18 100644 --- a/src/core_functions/scalar/list/list_sort.cpp +++ b/src/core_functions/scalar/list/list_sort.cpp @@ -132,7 +132,7 @@ static void ListSortFunction(DataChunk &args, ExpressionState &state, Vector &re // get the lists data UnifiedVectorFormat lists_data; result.ToUnifiedFormat(count, lists_data); - auto list_entries = (list_entry_t *)lists_data.data; + auto list_entries = UnifiedVectorFormat::GetData(lists_data); // create the lists_indices vector, this contains an element for each list's entry, // the element corresponds to the list's index, e.g. for [1, 2, 4], [5, 4] diff --git a/src/core_functions/scalar/map/cardinality.cpp b/src/core_functions/scalar/map/cardinality.cpp index 63038cfe11cc..8bf0dbd1108f 100644 --- a/src/core_functions/scalar/map/cardinality.cpp +++ b/src/core_functions/scalar/map/cardinality.cpp @@ -16,7 +16,7 @@ static void CardinalityFunction(DataChunk &args, ExpressionState &state, Vector map.ToUnifiedFormat(args.size(), map_data); for (idx_t row = 0; row < args.size(); row++) { - auto list_entry = ((list_entry_t *)map_data.data)[map_data.sel->get_index(row)]; + auto list_entry = UnifiedVectorFormat::GetData(map_data)[map_data.sel->get_index(row)]; result_data[row] = list_entry.length; result_validity.Set(row, map_data.validity.RowIsValid(map_data.sel->get_index(row))); } diff --git a/src/core_functions/scalar/map/map.cpp b/src/core_functions/scalar/map/map.cpp index 9b0b214ca2dd..b4c5669a73f2 100644 --- a/src/core_functions/scalar/map/map.cpp +++ b/src/core_functions/scalar/map/map.cpp @@ -55,8 +55,8 @@ static bool ListEntriesEqual(Vector &keys, Vector &values, idx_t count) { keys.ToUnifiedFormat(count, keys_data); values.ToUnifiedFormat(count, values_data); - auto keys_entries = (list_entry_t *)keys_data.data; - auto values_entries = (list_entry_t *)values_data.data; + auto keys_entries = UnifiedVectorFormat::GetData(keys_data); + auto values_entries = UnifiedVectorFormat::GetData(values_data); if (same_vector_type) { const auto key_data = keys_data.data; diff --git a/src/core_functions/scalar/map/map_concat.cpp b/src/core_functions/scalar/map/map_concat.cpp index 0391065a8f25..cd207aecaec8 100644 --- a/src/core_functions/scalar/map/map_concat.cpp +++ b/src/core_functions/scalar/map/map_concat.cpp @@ -69,7 +69,7 @@ static void MapConcatFunction(DataChunk &args, ExpressionState &state, Vector &r auto &keys = MapVector::GetKeys(args.data[map_idx]); auto index = map_format.sel->get_index(i); - auto entry = ((list_entry_t *)map_format.data)[index]; + auto entry = UnifiedVectorFormat::GetData(map_format)[index]; // Update the list for this row for (idx_t list_idx = 0; list_idx < entry.length; list_idx++) { diff --git a/src/core_functions/scalar/map/map_extract.cpp b/src/core_functions/scalar/map/map_extract.cpp index 2c4e261728c8..2986a7f6b48a 100644 --- a/src/core_functions/scalar/map/map_extract.cpp +++ b/src/core_functions/scalar/map/map_extract.cpp @@ -30,9 +30,7 @@ void FillResult(Vector &map, Vector &offsets, Vector &result, idx_t count) { UnifiedVectorFormat offset_data; offsets.ToUnifiedFormat(count, offset_data); - UnifiedVectorFormat result_data; - result.ToUnifiedFormat(count, result_data); - + auto result_data = FlatVector::GetData(result); auto entry_count = ListVector::GetListSize(map); auto &values_entries = MapVector::GetValues(map); UnifiedVectorFormat values_entry_data; @@ -41,14 +39,13 @@ void FillResult(Vector &map, Vector &offsets, Vector &result, idx_t count) { for (idx_t row = 0; row < count; row++) { idx_t offset_idx = offset_data.sel->get_index(row); - auto offset = ((int32_t *)offset_data.data)[offset_idx]; + auto offset = UnifiedVectorFormat::GetData(offset_data)[offset_idx]; // Get the current size of the list, for the offset idx_t current_offset = ListVector::GetListSize(result); if (!offset_data.validity.RowIsValid(offset_idx) || !offset) { // Set the entry data for this result row - idx_t result_index = result_data.sel->get_index(row); - auto &entry = ((list_entry_t *)result_data.data)[result_index]; + auto &entry = result_data[row]; entry.length = 0; entry.offset = current_offset; continue; @@ -58,7 +55,7 @@ void FillResult(Vector &map, Vector &offsets, Vector &result, idx_t count) { // Get the 'values' list entry corresponding to the offset idx_t value_index = map_data.sel->get_index(row); - auto &value_list_entry = ((list_entry_t *)map_data.data)[value_index]; + auto &value_list_entry = UnifiedVectorFormat::GetData(map_data)[value_index]; // Add the values to the result idx_t list_offset = value_list_entry.offset + offset; @@ -67,8 +64,7 @@ void FillResult(Vector &map, Vector &offsets, Vector &result, idx_t count) { ListVector::Append(result, values_entries, length + list_offset, list_offset); // Set the entry data for this result row - idx_t result_index = result_data.sel->get_index(row); - auto &entry = ((list_entry_t *)result_data.data)[result_index]; + auto &entry = result_data[row]; entry.length = length; entry.offset = current_offset; } diff --git a/src/core_functions/scalar/math/numeric.cpp b/src/core_functions/scalar/math/numeric.cpp index f4c68edb9c7d..73dc6f0f190a 100644 --- a/src/core_functions/scalar/math/numeric.cpp +++ b/src/core_functions/scalar/math/numeric.cpp @@ -563,7 +563,7 @@ struct RoundPrecisionFunctionData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const RoundPrecisionFunctionData &)other_p; + auto &other = other_p.Cast(); return target_scale == other.target_scale; } }; @@ -571,7 +571,7 @@ struct RoundPrecisionFunctionData : public FunctionData { template static void DecimalRoundNegativePrecisionFunction(DataChunk &input, ExpressionState &state, Vector &result) { auto &func_expr = state.expr.Cast(); - auto &info = (RoundPrecisionFunctionData &)*func_expr.bind_info; + auto &info = func_expr.bind_info->Cast(); auto source_scale = DecimalType::GetScale(func_expr.children[0]->return_type); auto width = DecimalType::GetWidth(func_expr.children[0]->return_type); if (-info.target_scale >= width) { @@ -597,7 +597,7 @@ static void DecimalRoundNegativePrecisionFunction(DataChunk &input, ExpressionSt template static void DecimalRoundPositivePrecisionFunction(DataChunk &input, ExpressionState &state, Vector &result) { auto &func_expr = state.expr.Cast(); - auto &info = (RoundPrecisionFunctionData &)*func_expr.bind_info; + auto &info = func_expr.bind_info->Cast(); auto source_scale = DecimalType::GetScale(func_expr.children[0]->return_type); T power_of_ten = POWERS_OF_TEN_CLASS::POWERS_OF_TEN[source_scale - info.target_scale]; T addition = power_of_ten / 2; diff --git a/src/core_functions/scalar/string/starts_with.cpp b/src/core_functions/scalar/string/starts_with.cpp index 6e6cdf98afd3..dad54180b08f 100644 --- a/src/core_functions/scalar/string/starts_with.cpp +++ b/src/core_functions/scalar/string/starts_with.cpp @@ -17,9 +17,10 @@ static bool StartsWith(const unsigned char *haystack, idx_t haystack_size, const } static bool StartsWith(const string_t &haystack_s, const string_t &needle_s) { - auto haystack = (const unsigned char *)haystack_s.GetData(); + + auto haystack = data_ptr_cast(haystack_s.GetData()); auto haystack_size = haystack_s.GetSize(); - auto needle = (const unsigned char *)needle_s.GetData(); + auto needle = data_ptr_cast(needle_s.GetData()); auto needle_size = needle_s.GetSize(); if (needle_size == 0) { // empty needle: always true diff --git a/src/core_functions/scalar/string/string_split.cpp b/src/core_functions/scalar/string/string_split.cpp index 5105cff24f5b..e7d0def26df5 100644 --- a/src/core_functions/scalar/string/string_split.cpp +++ b/src/core_functions/scalar/string/string_split.cpp @@ -36,7 +36,7 @@ struct RegularStringSplit { if (delim_size == 0) { return 0; } - return ContainsFun::Find((const unsigned char *)input_data, input_size, (const unsigned char *)delim_data, + return ContainsFun::Find(data_ptr_cast(input_data), input_size, data_ptr_cast(delim_data), delim_size); } }; @@ -109,11 +109,11 @@ template static void StringSplitExecutor(DataChunk &args, ExpressionState &state, Vector &result, void *data = nullptr) { UnifiedVectorFormat input_data; args.data[0].ToUnifiedFormat(args.size(), input_data); - auto inputs = (string_t *)input_data.data; + auto inputs = UnifiedVectorFormat::GetData(input_data); UnifiedVectorFormat delim_data; args.data[1].ToUnifiedFormat(args.size(), delim_data); - auto delims = (string_t *)delim_data.data; + auto delims = UnifiedVectorFormat::GetData(delim_data); D_ASSERT(result.GetType().id() == LogicalTypeId::LIST); diff --git a/src/core_functions/scalar/string/trim.cpp b/src/core_functions/scalar/string/trim.cpp index 1578bf8a86f5..7f61391034ff 100644 --- a/src/core_functions/scalar/string/trim.cpp +++ b/src/core_functions/scalar/string/trim.cpp @@ -64,7 +64,7 @@ static void UnaryTrimFunction(DataChunk &args, ExpressionState &state, Vector &r } static void GetIgnoredCodepoints(string_t ignored, unordered_set &ignored_codepoints) { - auto dataptr = (utf8proc_uint8_t *)ignored.GetData(); + auto dataptr = data_ptr_cast(ignored.GetData()); auto size = ignored.GetSize(); idx_t pos = 0; while (pos < size) { diff --git a/src/core_functions/scalar/union/union_extract.cpp b/src/core_functions/scalar/union/union_extract.cpp index 38ce5e52bb67..fe838cb1a8bf 100644 --- a/src/core_functions/scalar/union/union_extract.cpp +++ b/src/core_functions/scalar/union/union_extract.cpp @@ -20,7 +20,7 @@ struct UnionExtractBindData : public FunctionData { return make_uniq(key, index, type); } bool Equals(const FunctionData &other_p) const override { - auto &other = (const UnionExtractBindData &)other_p; + auto &other = other_p.Cast(); return key == other.key && index == other.index && type == other.type; } }; diff --git a/src/function/aggregate/distributive/first.cpp b/src/function/aggregate/distributive/first.cpp index 0d7d14bb8950..8d2ae17aaadb 100644 --- a/src/function/aggregate/distributive/first.cpp +++ b/src/function/aggregate/distributive/first.cpp @@ -171,7 +171,7 @@ struct FirstVectorFunction { UnifiedVectorFormat sdata; state_vector.ToUnifiedFormat(count, sdata); - auto states = (FirstStateVector **)sdata.data; + auto states = UnifiedVectorFormat::GetData(sdata); for (idx_t i = 0; i < count; i++) { const auto idx = idata.sel->get_index(i); if (SKIP_NULLS && !idata.validity.RowIsValid(idx)) { diff --git a/src/function/aggregate/sorted_aggregate_function.cpp b/src/function/aggregate/sorted_aggregate_function.cpp index f43dbdbf930c..4be4bc3ba521 100644 --- a/src/function/aggregate/sorted_aggregate_function.cpp +++ b/src/function/aggregate/sorted_aggregate_function.cpp @@ -49,7 +49,7 @@ struct SortedAggregateBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const SortedAggregateBindData &)other_p; + auto &other = other_p.Cast(); if (bind_info && other.bind_info) { if (!bind_info->Equals(*other.bind_info)) { return false; @@ -288,7 +288,7 @@ struct SortedAggregateFunction { DataChunk sort_chunk; ProjectInputs(inputs, order_bind, input_count, count, arg_chunk, sort_chunk); - const auto order_state = (SortedAggregateState *)state; + const auto order_state = reinterpret_cast(state); order_state->Update(order_bind, sort_chunk, arg_chunk); } @@ -310,7 +310,7 @@ struct SortedAggregateFunction { states.ToUnifiedFormat(count, svdata); // Size the selection vector for each state. - auto sdata = (SortedAggregateState **)svdata.data; + auto sdata = UnifiedVectorFormat::GetDataNoConst(svdata); for (idx_t i = 0; i < count; ++i) { auto sidx = svdata.sel->get_index(i); auto order_state = sdata[sidx]; @@ -346,9 +346,9 @@ struct SortedAggregateFunction { template static void Combine(const STATE &source, STATE *target, AggregateInputData &aggr_input_data) { - const auto order_bind = (SortedAggregateBindData *)aggr_input_data.bind_data; + auto &order_bind = aggr_input_data.bind_data->Cast(); auto &other = const_cast(source); - target->Combine(*order_bind, other); + target->Combine(order_bind, other); } static void Window(Vector inputs[], const ValidityMask &filter_mask, AggregateInputData &aggr_input_data, @@ -359,7 +359,7 @@ struct SortedAggregateFunction { static void Finalize(Vector &states, AggregateInputData &aggr_input_data, Vector &result, idx_t count, const idx_t offset) { - const auto &order_bind = aggr_input_data.bind_data->Cast(); + auto &order_bind = aggr_input_data.bind_data->Cast(); auto &buffer_manager = order_bind.buffer_manager; RowLayout payload_layout; payload_layout.Initialize(order_bind.arg_types); diff --git a/src/function/scalar/generic/constant_or_null.cpp b/src/function/scalar/generic/constant_or_null.cpp index 05feb9e850f8..05b0ebce6f7e 100644 --- a/src/function/scalar/generic/constant_or_null.cpp +++ b/src/function/scalar/generic/constant_or_null.cpp @@ -17,7 +17,7 @@ struct ConstantOrNullBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const ConstantOrNullBindData &)other_p; + auto &other = other_p.Cast(); return value == other.value; } }; diff --git a/src/function/scalar_function.cpp b/src/function/scalar_function.cpp index 98e1b7150058..1741c284a783 100644 --- a/src/function/scalar_function.cpp +++ b/src/function/scalar_function.cpp @@ -59,8 +59,8 @@ bool ScalarFunction::Equal(const ScalarFunction &rhs) const { bool ScalarFunction::CompareScalarFunctionT(const scalar_function_t &other) const { typedef void(scalar_function_ptr_t)(DataChunk &, ExpressionState &, Vector &); - auto func_ptr = (scalar_function_ptr_t **)function.template target(); - auto other_ptr = (scalar_function_ptr_t **)other.template target(); + auto func_ptr = (scalar_function_ptr_t **)function.template target(); // NOLINT + auto other_ptr = (scalar_function_ptr_t **)other.template target(); // NOLINT // Case the functions were created from lambdas the target will return a nullptr if (!func_ptr && !other_ptr) { diff --git a/src/include/duckdb/common/types/vector.hpp b/src/include/duckdb/common/types/vector.hpp index 15c698914def..68d03d07b7f5 100644 --- a/src/include/duckdb/common/types/vector.hpp +++ b/src/include/duckdb/common/types/vector.hpp @@ -24,6 +24,15 @@ struct UnifiedVectorFormat { data_ptr_t data; ValidityMask validity; SelectionVector owned_sel; + + template + static inline const T *GetData(const UnifiedVectorFormat &format) { + return reinterpret_cast(format.data); + } + template + static inline T *GetDataNoConst(UnifiedVectorFormat &format) { + return reinterpret_cast(format.data); + } }; class VectorCache; diff --git a/src/include/duckdb/parser/transformer.hpp b/src/include/duckdb/parser/transformer.hpp index be5f17c4114a..df845dcb873f 100644 --- a/src/include/duckdb/parser/transformer.hpp +++ b/src/include/duckdb/parser/transformer.hpp @@ -254,7 +254,7 @@ class Transformer { //===--------------------------------------------------------------------===// // Index transform //===--------------------------------------------------------------------===// - vector> TransformIndexParameters(duckdb_libpgquery::PGList *list, + vector> TransformIndexParameters(duckdb_libpgquery::PGList &list, const string &relation_name); //===--------------------------------------------------------------------===// @@ -302,15 +302,15 @@ class Transformer { QualifiedName TransformQualifiedName(duckdb_libpgquery::PGRangeVar &root); //! Transform a Postgres TypeName string into a LogicalType - LogicalType TransformTypeName(duckdb_libpgquery::PGTypeName *name); + LogicalType TransformTypeName(duckdb_libpgquery::PGTypeName &name); //! Transform a Postgres GROUP BY expression into a list of Expression - bool TransformGroupBy(duckdb_libpgquery::PGList *group, SelectNode &result); - void TransformGroupByNode(duckdb_libpgquery::PGNode *n, GroupingExpressionMap &map, SelectNode &result, + bool TransformGroupBy(optional_ptr group, SelectNode &result); + void TransformGroupByNode(duckdb_libpgquery::PGNode &n, GroupingExpressionMap &map, SelectNode &result, vector &result_sets); void AddGroupByExpression(unique_ptr expression, GroupingExpressionMap &map, GroupByNode &result, vector &result_set); - void TransformGroupByExpression(duckdb_libpgquery::PGNode *n, GroupingExpressionMap &map, GroupByNode &result, + void TransformGroupByExpression(duckdb_libpgquery::PGNode &n, GroupingExpressionMap &map, GroupByNode &result, vector &result_set); //! Transform a Postgres ORDER BY expression into an OrderByDescription bool TransformOrderBy(duckdb_libpgquery::PGList *order, vector &result); @@ -324,12 +324,15 @@ class Transformer { //! Transform a Postgres window frame specification into frame expressions void TransformWindowFrame(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr); - unique_ptr TransformSampleOptions(duckdb_libpgquery::PGNode *options); + unique_ptr TransformSampleOptions(optional_ptr options); //! Returns true if an expression is only a star (i.e. "*", without any other decorators) bool ExpressionIsEmptyStar(ParsedExpression &expr); OnEntryNotFound TransformOnEntryNotFound(bool missing_ok); + Vector PGListToVector(optional_ptr column_list, idx_t &size); + vector TransformConflictTarget(duckdb_libpgquery::PGList &list); + private: //! Current stack depth idx_t stack_depth; diff --git a/src/parser/query_node/select_node.cpp b/src/parser/query_node/select_node.cpp index 81ae9b885516..e295ab60a6d1 100644 --- a/src/parser/query_node/select_node.cpp +++ b/src/parser/query_node/select_node.cpp @@ -19,7 +19,7 @@ string SelectNode::ToString() const { // search for a distinct modifier for (idx_t modifier_idx = 0; modifier_idx < modifiers.size(); modifier_idx++) { if (modifiers[modifier_idx]->type == ResultModifierType::DISTINCT_MODIFIER) { - auto &distinct_modifier = (DistinctModifier &)*modifiers[modifier_idx]; + auto &distinct_modifier = modifiers[modifier_idx]->Cast(); result += "DISTINCT "; if (!distinct_modifier.distinct_on_targets.empty()) { result += "ON ("; diff --git a/src/parser/transform/expression/transform_cast.cpp b/src/parser/transform/expression/transform_cast.cpp index 688ebee08d47..d884290d35fa 100644 --- a/src/parser/transform/expression/transform_cast.cpp +++ b/src/parser/transform/expression/transform_cast.cpp @@ -9,7 +9,7 @@ namespace duckdb { unique_ptr Transformer::TransformTypeCast(duckdb_libpgquery::PGTypeCast &root) { // get the type to cast to auto type_name = root.typeName; - LogicalType target_type = TransformTypeName(type_name); + LogicalType target_type = TransformTypeName(*type_name); // check for a constant BLOB value, then return ConstantExpression with BLOB if (!root.tryCast && target_type == LogicalType::BLOB && root.arg->type == duckdb_libpgquery::T_PGAConst) { diff --git a/src/parser/transform/expression/transform_columnref.cpp b/src/parser/transform/expression/transform_columnref.cpp index 95629296b03f..f8ef3f956cb6 100644 --- a/src/parser/transform/expression/transform_columnref.cpp +++ b/src/parser/transform/expression/transform_columnref.cpp @@ -23,7 +23,7 @@ unique_ptr Transformer::TransformStarExpression(duckdb_libpgqu for (auto head = star.replace_list->head; head; head = head->next) { auto list = PGPointerCast(head->data.ptr_value); D_ASSERT(list->length == 2); - auto replace_expression = TransformExpression((duckdb_libpgquery::PGNode *)list->head->data.ptr_value); + auto replace_expression = TransformExpression(PGPointerCast(list->head->data.ptr_value)); auto value = PGPointerCast(list->tail->data.ptr_value); D_ASSERT(value->type == duckdb_libpgquery::T_PGString); string exclude_entry = value->val.str; diff --git a/src/parser/transform/helpers/transform_groupby.cpp b/src/parser/transform/helpers/transform_groupby.cpp index 87a5697c0bfd..c61071c4a16b 100644 --- a/src/parser/transform/helpers/transform_groupby.cpp +++ b/src/parser/transform/helpers/transform_groupby.cpp @@ -72,7 +72,7 @@ static void AddCubeSets(const GroupingSet ¤t_set, vector &res } } -void Transformer::TransformGroupByExpression(duckdb_libpgquery::PGNode *n, GroupingExpressionMap &map, +void Transformer::TransformGroupByExpression(duckdb_libpgquery::PGNode &n, GroupingExpressionMap &map, GroupByNode &result, vector &indexes) { auto expression = TransformExpression(n); AddGroupByExpression(std::move(expression), map, result, indexes); @@ -80,11 +80,11 @@ void Transformer::TransformGroupByExpression(duckdb_libpgquery::PGNode *n, Group // If one GROUPING SETS clause is nested inside another, // the effect is the same as if all the elements of the inner clause had been written directly in the outer clause. -void Transformer::TransformGroupByNode(duckdb_libpgquery::PGNode *n, GroupingExpressionMap &map, SelectNode &result, +void Transformer::TransformGroupByNode(duckdb_libpgquery::PGNode &n, GroupingExpressionMap &map, SelectNode &result, vector &result_sets) { - if (n->type == duckdb_libpgquery::T_PGGroupingSet) { - auto grouping_set = (duckdb_libpgquery::PGGroupingSet *)n; - switch (grouping_set->kind) { + if (n.type == duckdb_libpgquery::T_PGGroupingSet) { + auto &grouping_set = PGCast(n); + switch (grouping_set.kind) { case duckdb_libpgquery::GROUPING_SET_EMPTY: result_sets.emplace_back(); break; @@ -93,18 +93,18 @@ void Transformer::TransformGroupByNode(duckdb_libpgquery::PGNode *n, GroupingExp break; } case duckdb_libpgquery::GROUPING_SET_SETS: { - for (auto node = grouping_set->content->head; node; node = node->next) { - auto pg_node = (duckdb_libpgquery::PGNode *)node->data.ptr_value; - TransformGroupByNode(pg_node, map, result, result_sets); + for (auto node = grouping_set.content->head; node; node = node->next) { + auto pg_node = PGPointerCast(node->data.ptr_value); + TransformGroupByNode(*pg_node, map, result, result_sets); } break; } case duckdb_libpgquery::GROUPING_SET_ROLLUP: { vector rollup_sets; - for (auto node = grouping_set->content->head; node; node = node->next) { - auto pg_node = (duckdb_libpgquery::PGNode *)node->data.ptr_value; + for (auto node = grouping_set.content->head; node; node = node->next) { + auto pg_node = PGPointerCast(node->data.ptr_value); vector rollup_set; - TransformGroupByExpression(pg_node, map, result.groups, rollup_set); + TransformGroupByExpression(*pg_node, map, result.groups, rollup_set); rollup_sets.push_back(VectorToGroupingSet(rollup_set)); } // generate the subsets of the rollup set and add them to the grouping sets @@ -118,10 +118,10 @@ void Transformer::TransformGroupByNode(duckdb_libpgquery::PGNode *n, GroupingExp } case duckdb_libpgquery::GROUPING_SET_CUBE: { vector cube_sets; - for (auto node = grouping_set->content->head; node; node = node->next) { - auto pg_node = (duckdb_libpgquery::PGNode *)node->data.ptr_value; + for (auto node = grouping_set.content->head; node; node = node->next) { + auto pg_node = PGPointerCast(node->data.ptr_value); vector cube_set; - TransformGroupByExpression(pg_node, map, result.groups, cube_set); + TransformGroupByExpression(*pg_node, map, result.groups, cube_set); cube_sets.push_back(VectorToGroupingSet(cube_set)); } // generate the subsets of the rollup set and add them to the grouping sets @@ -132,7 +132,7 @@ void Transformer::TransformGroupByNode(duckdb_libpgquery::PGNode *n, GroupingExp break; } default: - throw InternalException("Unsupported GROUPING SET type %d", grouping_set->kind); + throw InternalException("Unsupported GROUPING SET type %d", grouping_set.kind); } } else { vector indexes; @@ -143,16 +143,16 @@ void Transformer::TransformGroupByNode(duckdb_libpgquery::PGNode *n, GroupingExp // If multiple grouping items are specified in a single GROUP BY clause, // then the final list of grouping sets is the cross product of the individual items. -bool Transformer::TransformGroupBy(duckdb_libpgquery::PGList *group, SelectNode &select_node) { +bool Transformer::TransformGroupBy(optional_ptr group, SelectNode &select_node) { if (!group) { return false; } auto &result = select_node.groups; GroupingExpressionMap map; for (auto node = group->head; node != nullptr; node = node->next) { - auto n = reinterpret_cast(node->data.ptr_value); + auto n = PGPointerCast(node->data.ptr_value); vector result_sets; - TransformGroupByNode(n, map, select_node, result_sets); + TransformGroupByNode(*n, map, select_node, result_sets); CheckGroupingSetMax(result_sets.size()); if (result.grouping_sets.empty()) { // no grouping sets yet: use the current set of grouping sets diff --git a/src/parser/transform/helpers/transform_sample.cpp b/src/parser/transform/helpers/transform_sample.cpp index ba3dbf13495a..0cffebfed4a5 100644 --- a/src/parser/transform/helpers/transform_sample.cpp +++ b/src/parser/transform/helpers/transform_sample.cpp @@ -18,13 +18,13 @@ static SampleMethod GetSampleMethod(const string &method) { } } -unique_ptr Transformer::TransformSampleOptions(duckdb_libpgquery::PGNode *options) { +unique_ptr Transformer::TransformSampleOptions(optional_ptr options) { if (!options) { return nullptr; } auto result = make_uniq(); - auto &sample_options = (duckdb_libpgquery::PGSampleOptions &)*options; - auto &sample_size = (duckdb_libpgquery::PGSampleSize &)*sample_options.sample_size; + auto &sample_options = PGCast(*options); + auto &sample_size = *PGPointerCast(sample_options.sample_size); auto sample_value = TransformValue(sample_size.sample_size)->value; result->is_percentage = sample_size.is_percentage; if (sample_size.is_percentage) { diff --git a/src/parser/transform/helpers/transform_typename.cpp b/src/parser/transform/helpers/transform_typename.cpp index 84530f3254d5..c43ef2ce0a4a 100644 --- a/src/parser/transform/helpers/transform_typename.cpp +++ b/src/parser/transform/helpers/transform_typename.cpp @@ -7,13 +7,13 @@ namespace duckdb { -LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_name) { - if (!type_name || type_name->type != duckdb_libpgquery::T_PGTypeName) { +LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName &type_name) { + if (type_name.type != duckdb_libpgquery::T_PGTypeName) { throw ParserException("Expected a type"); } auto stack_checker = StackCheck(); - auto name = (reinterpret_cast(type_name->names->tail->data.ptr_value)->val.str); + auto name = PGPointerCast(type_name.names->tail->data.ptr_value)->val.str; // transform it to the SQL type LogicalTypeId base_type = TransformStringToLogicalTypeId(name); @@ -23,21 +23,21 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n } else if (base_type == LogicalTypeId::ENUM) { throw ParserException("ENUM is not valid as a stand-alone type"); } else if (base_type == LogicalTypeId::STRUCT) { - if (!type_name->typmods || type_name->typmods->length == 0) { + if (!type_name.typmods || type_name.typmods->length == 0) { throw ParserException("Struct needs a name and entries"); } child_list_t children; case_insensitive_set_t name_collision_set; - for (auto node = type_name->typmods->head; node; node = node->next) { - auto &type_val = *((duckdb_libpgquery::PGList *)node->data.ptr_value); + for (auto node = type_name.typmods->head; node; node = node->next) { + auto &type_val = *PGPointerCast(node->data.ptr_value); if (type_val.length != 2) { throw ParserException("Struct entry needs an entry name and a type name"); } - auto entry_name_node = (duckdb_libpgquery::PGValue *)(type_val.head->data.ptr_value); + auto entry_name_node = PGPointerCast(type_val.head->data.ptr_value); D_ASSERT(entry_name_node->type == duckdb_libpgquery::T_PGString); - auto entry_type_node = (duckdb_libpgquery::PGValue *)(type_val.tail->data.ptr_value); + auto entry_type_node = PGPointerCast(type_val.tail->data.ptr_value); D_ASSERT(entry_type_node->type == duckdb_libpgquery::T_PGTypeName); auto entry_name = string(entry_name_node->val.str); @@ -47,42 +47,41 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n throw ParserException("Duplicate struct entry name \"%s\"", entry_name); } name_collision_set.insert(entry_name); + auto entry_type = TransformTypeName(*entry_type_node); - auto entry_type = TransformTypeName((duckdb_libpgquery::PGTypeName *)entry_type_node); children.push_back(make_pair(entry_name, entry_type)); } D_ASSERT(!children.empty()); result_type = LogicalType::STRUCT(children); } else if (base_type == LogicalTypeId::MAP) { - - if (!type_name->typmods || type_name->typmods->length != 2) { + if (!type_name.typmods || type_name.typmods->length != 2) { throw ParserException("Map type needs exactly two entries, key and value type"); } - auto key_type = TransformTypeName((duckdb_libpgquery::PGTypeName *)type_name->typmods->head->data.ptr_value); - auto value_type = TransformTypeName((duckdb_libpgquery::PGTypeName *)type_name->typmods->tail->data.ptr_value); + auto key_type = TransformTypeName(*PGPointerCast(type_name.typmods->head->data.ptr_value)); + auto value_type = TransformTypeName(*PGPointerCast(type_name.typmods->tail->data.ptr_value)); result_type = LogicalType::MAP(std::move(key_type), std::move(value_type)); } else if (base_type == LogicalTypeId::UNION) { - if (!type_name->typmods || type_name->typmods->length == 0) { + if (!type_name.typmods || type_name.typmods->length == 0) { throw ParserException("Union type needs at least one member"); } - if (type_name->typmods->length > (int)UnionType::MAX_UNION_MEMBERS) { + if (type_name.typmods->length > (int)UnionType::MAX_UNION_MEMBERS) { throw ParserException("Union types can have at most %d members", UnionType::MAX_UNION_MEMBERS); } child_list_t children; case_insensitive_set_t name_collision_set; - for (auto node = type_name->typmods->head; node; node = node->next) { - auto &type_val = *((duckdb_libpgquery::PGList *)node->data.ptr_value); + for (auto node = type_name.typmods->head; node; node = node->next) { + auto &type_val = *PGPointerCast(node->data.ptr_value); if (type_val.length != 2) { throw ParserException("Union type member needs a tag name and a type name"); } - auto entry_name_node = (duckdb_libpgquery::PGValue *)(type_val.head->data.ptr_value); + auto entry_name_node = PGPointerCast(type_val.head->data.ptr_value); D_ASSERT(entry_name_node->type == duckdb_libpgquery::T_PGString); - auto entry_type_node = (duckdb_libpgquery::PGValue *)(type_val.tail->data.ptr_value); + auto entry_type_node = PGPointerCast(type_val.tail->data.ptr_value); D_ASSERT(entry_type_node->type == duckdb_libpgquery::T_PGTypeName); auto entry_name = string(entry_name_node->val.str); @@ -94,7 +93,7 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n name_collision_set.insert(entry_name); - auto entry_type = TransformTypeName((duckdb_libpgquery::PGTypeName *)entry_type_node); + auto entry_type = TransformTypeName(*entry_type_node); children.push_back(make_pair(entry_name, entry_type)); } D_ASSERT(!children.empty()); @@ -111,9 +110,9 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n } // check any modifiers int modifier_idx = 0; - if (type_name->typmods) { - for (auto node = type_name->typmods->head; node; node = node->next) { - auto &const_val = *((duckdb_libpgquery::PGAConst *)node->data.ptr_value); + if (type_name.typmods) { + for (auto node = type_name.typmods->head; node; node = node->next) { + auto &const_val = *PGPointerCast(node->data.ptr_value); if (const_val.type != duckdb_libpgquery::T_PGAConst || const_val.val.type != duckdb_libpgquery::T_PGInteger) { throw ParserException("Expected an integer constant as type modifier"); @@ -169,7 +168,7 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n break; } case LogicalTypeId::BIT: { - if (!width && type_name->typmods) { + if (!width && type_name.typmods) { throw ParserException("Type %s does not support any modifiers!", LogicalType(base_type).ToString()); } result_type = LogicalType(base_type); @@ -204,10 +203,10 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName *type_n break; } } - if (type_name->arrayBounds) { + if (type_name.arrayBounds) { // array bounds: turn the type into a list idx_t extra_stack = 0; - for (auto cell = type_name->arrayBounds->head; cell != nullptr; cell = cell->next) { + for (auto cell = type_name.arrayBounds->head; cell != nullptr; cell = cell->next) { result_type = LogicalType::LIST(result_type); StackCheck(extra_stack++); } diff --git a/src/parser/transform/statement/transform_create_index.cpp b/src/parser/transform/statement/transform_create_index.cpp index a944abe5d2e7..a94888a92f54 100644 --- a/src/parser/transform/statement/transform_create_index.cpp +++ b/src/parser/transform/statement/transform_create_index.cpp @@ -19,11 +19,11 @@ static IndexType StringToIndexType(const string &str) { return IndexType::INVALID; } -vector> Transformer::TransformIndexParameters(duckdb_libpgquery::PGList *list, +vector> Transformer::TransformIndexParameters(duckdb_libpgquery::PGList &list, const string &relation_name) { vector> expressions; - for (auto cell = list->head; cell != nullptr; cell = cell->next) { - auto index_element = (duckdb_libpgquery::PGIndexElem *)cell->data.ptr_value; + for (auto cell = list.head; cell != nullptr; cell = cell->next) { + auto index_element = PGPointerCast(cell->data.ptr_value); if (index_element->collation) { throw NotImplementedException("Index with collation not supported yet!"); } @@ -54,7 +54,7 @@ unique_ptr Transformer::TransformCreateIndex(duckdb_libpgquery: info->on_conflict = TransformOnConflict(stmt.onconflict); - info->expressions = TransformIndexParameters(stmt.indexParams, stmt.relation->relname); + info->expressions = TransformIndexParameters(*stmt.indexParams, stmt.relation->relname); info->index_type = StringToIndexType(string(stmt.accessMethod)); auto tableref = make_uniq(); diff --git a/src/parser/transform/statement/transform_create_table.cpp b/src/parser/transform/statement/transform_create_table.cpp index 2bcaa403ab0b..8c7070606ca1 100644 --- a/src/parser/transform/statement/transform_create_table.cpp +++ b/src/parser/transform/statement/transform_create_table.cpp @@ -53,7 +53,7 @@ ColumnDefinition Transformer::TransformColumnDefinition(duckdb_libpgquery::PGCol colname = cdef.colname; } bool optional_type = cdef.category == duckdb_libpgquery::COL_GENERATED; - LogicalType target_type = (optional_type && !cdef.typeName) ? LogicalType::ANY : TransformTypeName(cdef.typeName); + LogicalType target_type = (optional_type && !cdef.typeName) ? LogicalType::ANY : TransformTypeName(*cdef.typeName); if (cdef.collClause) { if (cdef.category == duckdb_libpgquery::COL_GENERATED) { throw ParserException("Collations are not supported on generated columns"); diff --git a/src/parser/transform/statement/transform_create_type.cpp b/src/parser/transform/statement/transform_create_type.cpp index a8dea8b18791..95f26fa6205a 100644 --- a/src/parser/transform/statement/transform_create_type.cpp +++ b/src/parser/transform/statement/transform_create_type.cpp @@ -6,7 +6,7 @@ namespace duckdb { -Vector ReadPgListToVector(duckdb_libpgquery::PGList *column_list, idx_t &size) { +Vector Transformer::PGListToVector(optional_ptr column_list, idx_t &size) { if (!column_list) { Vector result(LogicalType::VARCHAR); return result; @@ -21,8 +21,8 @@ Vector ReadPgListToVector(duckdb_libpgquery::PGList *column_list, idx_t &size) { size = 0; for (auto c = column_list->head; c != nullptr; c = lnext(c)) { - auto &type_val = *((duckdb_libpgquery::PGAConst *)c->data.ptr_value); - auto entry_value_node = (duckdb_libpgquery::PGValue)(type_val.val); + auto &type_val = *PGPointerCast(c->data.ptr_value); + auto &entry_value_node = type_val.val; if (entry_value_node.type != duckdb_libpgquery::T_PGString) { throw ParserException("Expected a string constant as value"); } @@ -55,13 +55,13 @@ unique_ptr Transformer::TransformCreateType(duckdb_libpgquery:: } else { D_ASSERT(stmt.query == nullptr); idx_t size = 0; - auto ordered_array = ReadPgListToVector(stmt.vals, size); + auto ordered_array = PGListToVector(stmt.vals, size); info->type = LogicalType::ENUM(info->name, ordered_array, size); } } break; case duckdb_libpgquery::PG_NEWTYPE_ALIAS: { - LogicalType target_type = TransformTypeName(stmt.ofType); + LogicalType target_type = TransformTypeName(*stmt.ofType); info->type = target_type; } break; diff --git a/src/parser/transform/statement/transform_upsert.cpp b/src/parser/transform/statement/transform_upsert.cpp index 8fcd1168b4c8..3c550cb083bb 100644 --- a/src/parser/transform/statement/transform_upsert.cpp +++ b/src/parser/transform/statement/transform_upsert.cpp @@ -21,10 +21,10 @@ OnConflictAction TransformOnConflictAction(duckdb_libpgquery::PGOnConflictClause } } -vector TransformConflictTarget(duckdb_libpgquery::PGList *list) { +vector Transformer::TransformConflictTarget(duckdb_libpgquery::PGList &list) { vector columns; - for (auto cell = list->head; cell != nullptr; cell = cell->next) { - auto index_element = (duckdb_libpgquery::PGIndexElem *)cell->data.ptr_value; + for (auto cell = list.head; cell != nullptr; cell = cell->next) { + auto index_element = PGPointerCast(cell->data.ptr_value); if (index_element->collation) { throw NotImplementedException("Index with collation not supported yet!"); } @@ -77,7 +77,7 @@ unique_ptr Transformer::TransformOnConflictClause(duckdb_libpgqu // A filter for the ON CONFLICT ... is specified if (stmt->infer->indexElems) { // Columns are specified - result->indexed_columns = TransformConflictTarget(stmt->infer->indexElems); + result->indexed_columns = TransformConflictTarget(*stmt->infer->indexElems); if (stmt->infer->whereClause) { result->condition = TransformExpression(stmt->infer->whereClause); } From 2e09719aabc168b66e7ce0702c4351fce82535e5 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Mon, 22 May 2023 10:34:27 +0200 Subject: [PATCH 06/28] UnifiedVectorFormat::GetData --- src/common/arrow/arrow_appender.cpp | 8 ++--- src/common/hive_partitioning.cpp | 2 +- .../row_operations/row_heap_scatter.cpp | 6 ++-- src/common/row_operations/row_match.cpp | 2 +- .../row_operations/row_radix_scatter.cpp | 4 +-- src/common/row_operations/row_scatter.cpp | 4 +-- src/common/types.cpp | 2 +- src/common/types/hyperloglog.cpp | 6 ++-- src/common/types/vector.cpp | 16 +++++----- .../vector_operations/boolean_operators.cpp | 4 +-- .../vector_operations/is_distinct_from.cpp | 18 +++++------ src/common/vector_operations/vector_hash.cpp | 22 +++++++------ .../vector_operations/vector_storage.cpp | 2 +- .../scalar/string/string_split.cpp | 4 +-- src/execution/expression_executor.cpp | 14 ++++----- .../expression_executor/execute_case.cpp | 2 +- src/execution/index/art/art.cpp | 4 +-- src/execution/join_hashtable.cpp | 2 +- .../nested_loop_join_inner.cpp | 8 ++--- .../nested_loop_join_mark.cpp | 4 +-- .../operator/projection/physical_unnest.cpp | 6 ++-- .../operator/schema/physical_create_type.cpp | 2 +- src/execution/perfect_aggregate_hashtable.cpp | 2 +- src/function/cast/enum_casts.cpp | 4 +-- src/function/cast/string_cast.cpp | 22 ++++++------- src/function/cast/union_casts.cpp | 4 +-- src/function/cast/vector_cast_helpers.cpp | 5 +-- src/function/scalar/list/list_concat.cpp | 4 +-- src/function/scalar/list/list_extract.cpp | 6 ++-- src/function/scalar/string/concat.cpp | 15 ++++----- .../scalar/system/aggregate_export.cpp | 2 +- src/function/scalar_function.cpp | 2 +- src/include/duckdb/common/exception.hpp | 4 +-- .../vector_operations/binary_executor.hpp | 31 ++++++++++--------- .../vector_operations/ternary_executor.hpp | 27 ++++++++-------- .../vector_operations/unary_executor.hpp | 6 ++-- .../function/cast/vector_cast_helpers.hpp | 8 ++--- src/include/duckdb/parser/transformer.hpp | 4 +-- src/main/capi/result-c.cpp | 2 +- .../expression/transform_columnref.cpp | 3 +- .../expression/transform_expression.cpp | 3 +- .../transform/helpers/transform_cte.cpp | 3 +- .../transform/helpers/transform_typename.cpp | 6 ++-- .../statement/transform_create_function.cpp | 6 ++-- .../tableref/transform_table_function.cpp | 3 +- src/storage/compression/bitpacking.cpp | 4 +-- .../compression/dictionary_compression.cpp | 2 +- .../compression/fixed_size_uncompressed.cpp | 4 +-- src/storage/compression/fsst.cpp | 4 +-- src/storage/compression/rle.cpp | 6 ++-- .../compression/string_uncompressed.cpp | 2 +- src/storage/data_table.cpp | 2 +- src/storage/table/list_column_data.cpp | 2 +- .../sqlite3_udf_api/include/cast_sqlite.hpp | 2 +- 54 files changed, 176 insertions(+), 166 deletions(-) diff --git a/src/common/arrow/arrow_appender.cpp b/src/common/arrow/arrow_appender.cpp index cb8ccfb2f121..084f1b2f8aff 100644 --- a/src/common/arrow/arrow_appender.cpp +++ b/src/common/arrow/arrow_appender.cpp @@ -162,7 +162,7 @@ struct ArrowScalarBaseData { // append the main data append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(TGT) * size); - auto data = (SRC *)format.data; + auto data = UnifiedVectorFormat::GetData(format); auto result_data = (TGT *)append_data.main_buffer.data(); for (idx_t i = from; i < to; i++) { @@ -269,7 +269,7 @@ struct ArrowBoolData { // we initialize both the validity and the bit set to 1's ResizeValidity(append_data.validity, append_data.row_count + size); ResizeValidity(append_data.main_buffer, append_data.row_count + size); - auto data = (bool *)format.data; + auto data = UnifiedVectorFormat::GetData(format); auto result_data = (uint8_t *)append_data.main_buffer.data(); auto validity_data = (uint8_t *)append_data.validity.data(); @@ -341,7 +341,7 @@ struct ArrowVarcharData { // resize the offset buffer - the offset buffer holds the offsets into the child array append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(BUFTYPE) * (size + 1)); - auto data = (SRC *)format.data; + auto data = UnifiedVectorFormat::GetData(format); auto offset_data = (BUFTYPE *)append_data.main_buffer.data(); if (append_data.row_count == 0) { // first entry @@ -441,7 +441,7 @@ void AppendListOffsets(ArrowAppendData &append_data, UnifiedVectorFormat &format // resize the offset buffer - the offset buffer holds the offsets into the child array idx_t size = to - from; append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(uint32_t) * (size + 1)); - auto data = (list_entry_t *)format.data; + auto data = UnifiedVectorFormat::GetData(format); auto offset_data = (uint32_t *)append_data.main_buffer.data(); if (append_data.row_count == 0) { // first entry diff --git a/src/common/hive_partitioning.cpp b/src/common/hive_partitioning.cpp index 9e97cbae8ff0..cf01c823789d 100644 --- a/src/common/hive_partitioning.cpp +++ b/src/common/hive_partitioning.cpp @@ -183,7 +183,7 @@ static void TemplatedGetHivePartitionValues(Vector &input, vector(format); const auto &validity = format.validity; const auto &type = input.GetType(); diff --git a/src/common/row_operations/row_heap_scatter.cpp b/src/common/row_operations/row_heap_scatter.cpp index 6f64761d4298..2b84043eaea6 100644 --- a/src/common/row_operations/row_heap_scatter.cpp +++ b/src/common/row_operations/row_heap_scatter.cpp @@ -8,7 +8,7 @@ using ValidityBytes = TemplatedValidityMask; static void ComputeStringEntrySizes(UnifiedVectorFormat &vdata, idx_t entry_sizes[], const idx_t ser_count, const SelectionVector &sel, const idx_t offset) { - auto strings = (string_t *)vdata.data; + auto strings = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < ser_count; i++) { auto idx = sel.get_index(i); auto str_idx = vdata.sel->get_index(idx + offset); @@ -116,7 +116,7 @@ void RowOperations::ComputeEntrySizes(Vector &v, idx_t entry_sizes[], idx_t vcou template static void TemplatedHeapScatter(UnifiedVectorFormat &vdata, const SelectionVector &sel, idx_t count, idx_t col_idx, data_ptr_t *key_locations, data_ptr_t *validitymask_locations, idx_t offset) { - auto source = (T *)vdata.data; + auto source = UnifiedVectorFormat::GetData(vdata); if (!validitymask_locations) { for (idx_t i = 0; i < count; i++) { auto idx = sel.get_index(i); @@ -152,7 +152,7 @@ static void HeapScatterStringVector(Vector &v, idx_t vcount, const SelectionVect UnifiedVectorFormat vdata; v.ToUnifiedFormat(vcount, vdata); - auto strings = (string_t *)vdata.data; + auto strings = UnifiedVectorFormat::GetData(vdata); if (!validitymask_locations) { for (idx_t i = 0; i < ser_count; i++) { auto idx = sel.get_index(i); diff --git a/src/common/row_operations/row_match.cpp b/src/common/row_operations/row_match.cpp index 7d0722e0dcf8..b7727e79dfe9 100644 --- a/src/common/row_operations/row_match.cpp +++ b/src/common/row_operations/row_match.cpp @@ -64,7 +64,7 @@ static void TemplatedMatchType(UnifiedVectorFormat &col, Vector &rows, Selection idx_t idx_in_entry; ValidityBytes::GetEntryIndex(col_no, entry_idx, idx_in_entry); - auto data = (T *)col.data; + auto data = UnifiedVectorFormat::GetData(col); auto ptrs = FlatVector::GetData(rows); idx_t match_count = 0; if (!col.validity.AllValid()) { diff --git a/src/common/row_operations/row_radix_scatter.cpp b/src/common/row_operations/row_radix_scatter.cpp index 74774926d5e2..1a8acaec832c 100644 --- a/src/common/row_operations/row_radix_scatter.cpp +++ b/src/common/row_operations/row_radix_scatter.cpp @@ -9,7 +9,7 @@ template void TemplatedRadixScatter(UnifiedVectorFormat &vdata, const SelectionVector &sel, idx_t add_count, data_ptr_t *key_locations, const bool desc, const bool has_null, const bool nulls_first, const idx_t offset) { - auto source = (T *)vdata.data; + auto source = UnifiedVectorFormat::GetData(vdata); if (has_null) { auto &validity = vdata.validity; const data_t valid = nulls_first ? 1 : 0; @@ -54,7 +54,7 @@ void TemplatedRadixScatter(UnifiedVectorFormat &vdata, const SelectionVector &se void RadixScatterStringVector(UnifiedVectorFormat &vdata, const SelectionVector &sel, idx_t add_count, data_ptr_t *key_locations, const bool desc, const bool has_null, const bool nulls_first, const idx_t prefix_len, idx_t offset) { - auto source = (string_t *)vdata.data; + auto source = UnifiedVectorFormat::GetData(vdata); if (has_null) { auto &validity = vdata.validity; const data_t valid = nulls_first ? 1 : 0; diff --git a/src/common/row_operations/row_scatter.cpp b/src/common/row_operations/row_scatter.cpp index 79db9a2e01d3..691bce0e30a1 100644 --- a/src/common/row_operations/row_scatter.cpp +++ b/src/common/row_operations/row_scatter.cpp @@ -20,7 +20,7 @@ using ValidityBytes = RowLayout::ValidityBytes; template static void TemplatedScatter(UnifiedVectorFormat &col, Vector &rows, const SelectionVector &sel, const idx_t count, const idx_t col_offset, const idx_t col_no) { - auto data = (T *)col.data; + auto data = UnifiedVectorFormat::GetData(col); auto ptrs = FlatVector::GetData(rows); if (!col.validity.AllValid()) { @@ -64,7 +64,7 @@ static void ComputeStringEntrySizes(const UnifiedVectorFormat &col, idx_t entry_ static void ScatterStringVector(UnifiedVectorFormat &col, Vector &rows, data_ptr_t str_locations[], const SelectionVector &sel, const idx_t count, const idx_t col_offset, const idx_t col_no) { - auto string_data = (string_t *)col.data; + auto string_data = UnifiedVectorFormat::GetData(col); auto ptrs = FlatVector::GetData(rows); // Write out zero length to avoid swizzling problems. diff --git a/src/common/types.cpp b/src/common/types.cpp index 3030cb3be23b..ab3c1771ae3b 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -1400,7 +1400,7 @@ struct EnumTypeInfoTemplated : public EnumTypeInfo { UnifiedVectorFormat vdata; values_insert_order.ToUnifiedFormat(size_p, vdata); - auto data = (string_t *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < size_p; i++) { auto idx = vdata.sel->get_index(i); if (!vdata.validity.RowIsValid(idx)) { diff --git a/src/common/types/hyperloglog.cpp b/src/common/types/hyperloglog.cpp index 3b7893d326f0..a21a6204e9aa 100644 --- a/src/common/types/hyperloglog.cpp +++ b/src/common/types/hyperloglog.cpp @@ -58,9 +58,7 @@ HyperLogLog *HyperLogLog::MergePointer(HyperLogLog &other) { } unique_ptr HyperLogLog::Merge(HyperLogLog logs[], idx_t count) { - auto hlls_uptr = unique_ptr { - new duckdb_hll::robj *[count] - }; + auto hlls_uptr = unique_ptr {new duckdb_hll::robj *[count]}; auto hlls = hlls_uptr.get(); for (idx_t i = 0; i < count; i++) { hlls[i] = (duckdb_hll::robj *)logs[i].hll; @@ -194,7 +192,7 @@ inline uint64_t TemplatedHash(const string_t &elem) { template void TemplatedComputeHashes(UnifiedVectorFormat &vdata, const idx_t &count, uint64_t hashes[]) { - T *data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); if (vdata.validity.RowIsValid(idx)) { diff --git a/src/common/types/vector.cpp b/src/common/types/vector.cpp index 03345c03cfe4..20d1e7fc361e 100644 --- a/src/common/types/vector.cpp +++ b/src/common/types/vector.cpp @@ -926,7 +926,7 @@ void Vector::Serialize(idx_t count, Serializer &serializer) { } else { switch (type.InternalType()) { case PhysicalType::VARCHAR: { - auto strings = (string_t *)vdata.data; + auto strings = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); auto source = !vdata.validity.RowIsValid(idx) ? NullValue() : strings[idx]; @@ -948,7 +948,7 @@ void Vector::Serialize(idx_t count, Serializer &serializer) { // serialize the list entries in a flat array auto data = make_unsafe_uniq_array(count); - auto source_array = (list_entry_t *)vdata.data; + auto source_array = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); auto source = source_array[idx]; @@ -994,7 +994,7 @@ void Vector::FormatSerialize(FormatSerializer &serializer, idx_t count) { } else { switch (logical_type.InternalType()) { case PhysicalType::VARCHAR: { - auto strings = (string_t *)vdata.data; + auto strings = UnifiedVectorFormat::GetData(vdata); // Serialize data as a list serializer.SetTag("data"); @@ -1028,7 +1028,7 @@ void Vector::FormatSerialize(FormatSerializer &serializer, idx_t count) { // serialize the list entries in a flat array auto entries = make_unsafe_uniq_array(count); - auto source_array = (list_entry_t *)vdata.data; + auto source_array = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); auto source = source_array[idx]; @@ -1503,7 +1503,7 @@ void ConstantVector::Reference(Vector &vector, Vector &source, idx_t position, i break; } - auto list_data = (list_entry_t *)vdata.data; + auto list_data = UnifiedVectorFormat::GetData(vdata); auto list_entry = list_data[list_index]; // add the list entry as the first element of "vector" @@ -1939,7 +1939,7 @@ ConsecutiveChildListInfo ListVector::GetConsecutiveChildListInfo(Vector &list, i ConsecutiveChildListInfo info; UnifiedVectorFormat unified_list_data; list.ToUnifiedFormat(offset + count, unified_list_data); - auto list_data = (list_entry_t *)unified_list_data.data; + auto list_data = UnifiedVectorFormat::GetData(unified_list_data); // find the first non-NULL entry idx_t first_length = 0; @@ -1992,7 +1992,7 @@ void ListVector::GetConsecutiveChildSelVector(Vector &list, SelectionVector &sel UnifiedVectorFormat unified_list_data; list.ToUnifiedFormat(offset + count, unified_list_data); - auto list_data = (list_entry_t *)unified_list_data.data; + auto list_data = UnifiedVectorFormat::GetData(unified_list_data); // SelectionVector child_sel(info.second.length); idx_t entry = 0; @@ -2127,7 +2127,7 @@ UnionInvalidReason UnionVector::CheckUnionValidity(Vector &vector, idx_t count, continue; } - auto tag = ((union_tag_t *)tags_vdata.data)[tag_mapped_row_idx]; + auto tag = (UnifiedVectorFormat::GetData(tags_vdata))[tag_mapped_row_idx]; if (tag >= member_count) { return UnionInvalidReason::TAG_OUT_OF_RANGE; } diff --git a/src/common/vector_operations/boolean_operators.cpp b/src/common/vector_operations/boolean_operators.cpp index 591c48d932e8..667c920858c5 100644 --- a/src/common/vector_operations/boolean_operators.cpp +++ b/src/common/vector_operations/boolean_operators.cpp @@ -35,8 +35,8 @@ static void TemplatedBooleanNullmask(Vector &left, Vector &right, Vector &result right.ToUnifiedFormat(count, rdata); result.SetVectorType(VectorType::FLAT_VECTOR); - auto left_data = (uint8_t *)ldata.data; // we use uint8 to avoid load of gunk bools - auto right_data = (uint8_t *)rdata.data; + auto left_data = UnifiedVectorFormat::GetData(ldata); // we use uint8 to avoid load of gunk bools + auto right_data = UnifiedVectorFormat::GetData(rdata); auto result_data = FlatVector::GetData(result); auto &result_mask = FlatVector::Validity(result); if (!ldata.validity.AllValid() || !rdata.validity.AllValid()) { diff --git a/src/common/vector_operations/is_distinct_from.cpp b/src/common/vector_operations/is_distinct_from.cpp index 06fd5d1a8a95..eacc8de4d4c7 100644 --- a/src/common/vector_operations/is_distinct_from.cpp +++ b/src/common/vector_operations/is_distinct_from.cpp @@ -11,7 +11,7 @@ struct DistinctBinaryLambdaWrapper { }; template -static void DistinctExecuteGenericLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, +static void DistinctExecuteGenericLoop(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, RESULT_TYPE *__restrict result_data, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, idx_t count, ValidityMask &lmask, ValidityMask &rmask, ValidityMask &result_mask) { @@ -49,8 +49,8 @@ static void DistinctExecuteGeneric(Vector &left, Vector &right, Vector &result, result.SetVectorType(VectorType::FLAT_VECTOR); auto result_data = FlatVector::GetData(result); DistinctExecuteGenericLoop( - (LEFT_TYPE *)ldata.data, (RIGHT_TYPE *)rdata.data, result_data, ldata.sel, rdata.sel, count, ldata.validity, - rdata.validity, FlatVector::Validity(result)); + UnifiedVectorFormat::GetData(ldata), UnifiedVectorFormat::GetData(rdata), + result_data, ldata.sel, rdata.sel, count, ldata.validity, rdata.validity, FlatVector::Validity(result)); } } @@ -66,7 +66,7 @@ static void DistinctExecute(Vector &left, Vector &right, Vector &result, idx_t c template static inline idx_t -DistinctSelectGenericLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, +DistinctSelectGenericLoop(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, const SelectionVector *__restrict result_sel, idx_t count, ValidityMask &lmask, ValidityMask &rmask, SelectionVector *true_sel, SelectionVector *false_sel) { @@ -105,7 +105,7 @@ DistinctSelectGenericLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rd } template static inline idx_t -DistinctSelectGenericLoopSelSwitch(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, +DistinctSelectGenericLoopSelSwitch(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, const SelectionVector *__restrict result_sel, idx_t count, ValidityMask &lmask, ValidityMask &rmask, SelectionVector *true_sel, SelectionVector *false_sel) { @@ -124,7 +124,7 @@ DistinctSelectGenericLoopSelSwitch(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__re template static inline idx_t -DistinctSelectGenericLoopSwitch(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, +DistinctSelectGenericLoopSwitch(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, const SelectionVector *__restrict result_sel, idx_t count, ValidityMask &lmask, ValidityMask &rmask, SelectionVector *true_sel, SelectionVector *false_sel) { @@ -145,9 +145,9 @@ static idx_t DistinctSelectGeneric(Vector &left, Vector &right, const SelectionV left.ToUnifiedFormat(count, ldata); right.ToUnifiedFormat(count, rdata); - return DistinctSelectGenericLoopSwitch((LEFT_TYPE *)ldata.data, (RIGHT_TYPE *)rdata.data, - ldata.sel, rdata.sel, sel, count, ldata.validity, - rdata.validity, true_sel, false_sel); + return DistinctSelectGenericLoopSwitch( + UnifiedVectorFormat::GetData(ldata), UnifiedVectorFormat::GetData(rdata), ldata.sel, + rdata.sel, sel, count, ldata.validity, rdata.validity, true_sel, false_sel); } template diff --git a/src/common/vector_operations/vector_hash.cpp b/src/common/vector_operations/vector_hash.cpp index 844e53195fda..2dc5d2092341 100644 --- a/src/common/vector_operations/vector_hash.cpp +++ b/src/common/vector_operations/vector_hash.cpp @@ -25,7 +25,7 @@ static inline hash_t CombineHashScalar(hash_t a, hash_t b) { } template -static inline void TightLoopHash(T *__restrict ldata, hash_t *__restrict result_data, const SelectionVector *rsel, +static inline void TightLoopHash(const T *__restrict ldata, hash_t *__restrict result_data, const SelectionVector *rsel, idx_t count, const SelectionVector *__restrict sel_vector, ValidityMask &mask) { if (!mask.AllValid()) { for (idx_t i = 0; i < count; i++) { @@ -56,8 +56,8 @@ static inline void TemplatedLoopHash(Vector &input, Vector &result, const Select UnifiedVectorFormat idata; input.ToUnifiedFormat(count, idata); - TightLoopHash((T *)idata.data, FlatVector::GetData(result), rsel, count, idata.sel, - idata.validity); + TightLoopHash(UnifiedVectorFormat::GetData(idata), FlatVector::GetData(result), rsel, + count, idata.sel, idata.validity); } } @@ -240,8 +240,8 @@ void VectorOperations::Hash(Vector &input, Vector &result, const SelectionVector } template -static inline void TightLoopCombineHashConstant(T *__restrict ldata, hash_t constant_hash, hash_t *__restrict hash_data, - const SelectionVector *rsel, idx_t count, +static inline void TightLoopCombineHashConstant(const T *__restrict ldata, hash_t constant_hash, + hash_t *__restrict hash_data, const SelectionVector *rsel, idx_t count, const SelectionVector *__restrict sel_vector, ValidityMask &mask) { if (!mask.AllValid()) { for (idx_t i = 0; i < count; i++) { @@ -261,8 +261,9 @@ static inline void TightLoopCombineHashConstant(T *__restrict ldata, hash_t cons } template -static inline void TightLoopCombineHash(T *__restrict ldata, hash_t *__restrict hash_data, const SelectionVector *rsel, - idx_t count, const SelectionVector *__restrict sel_vector, ValidityMask &mask) { +static inline void TightLoopCombineHash(const T *__restrict ldata, hash_t *__restrict hash_data, + const SelectionVector *rsel, idx_t count, + const SelectionVector *__restrict sel_vector, ValidityMask &mask) { if (!mask.AllValid()) { for (idx_t i = 0; i < count; i++) { auto ridx = HAS_RSEL ? rsel->get_index(i) : i; @@ -296,13 +297,14 @@ void TemplatedLoopCombineHash(Vector &input, Vector &hashes, const SelectionVect auto constant_hash = *ConstantVector::GetData(hashes); // now re-initialize the hashes vector to an empty flat vector hashes.SetVectorType(VectorType::FLAT_VECTOR); - TightLoopCombineHashConstant((T *)idata.data, constant_hash, + TightLoopCombineHashConstant(UnifiedVectorFormat::GetData(idata), constant_hash, FlatVector::GetData(hashes), rsel, count, idata.sel, idata.validity); } else { D_ASSERT(hashes.GetVectorType() == VectorType::FLAT_VECTOR); - TightLoopCombineHash((T *)idata.data, FlatVector::GetData(hashes), rsel, count, - idata.sel, idata.validity); + TightLoopCombineHash(UnifiedVectorFormat::GetData(idata), + FlatVector::GetData(hashes), rsel, count, idata.sel, + idata.validity); } } } diff --git a/src/common/vector_operations/vector_storage.cpp b/src/common/vector_operations/vector_storage.cpp index dae178a41f87..be7c97c71ca7 100644 --- a/src/common/vector_operations/vector_storage.cpp +++ b/src/common/vector_operations/vector_storage.cpp @@ -6,7 +6,7 @@ namespace duckdb { template static void CopyToStorageLoop(UnifiedVectorFormat &vdata, idx_t count, data_ptr_t target) { - auto ldata = (T *)vdata.data; + auto ldata = UnifiedVectorFormat::GetData(vdata); auto result_data = (T *)target; for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); diff --git a/src/core_functions/scalar/string/string_split.cpp b/src/core_functions/scalar/string/string_split.cpp index e7d0def26df5..02f8065a6f5b 100644 --- a/src/core_functions/scalar/string/string_split.cpp +++ b/src/core_functions/scalar/string/string_split.cpp @@ -36,8 +36,8 @@ struct RegularStringSplit { if (delim_size == 0) { return 0; } - return ContainsFun::Find(data_ptr_cast(input_data), input_size, data_ptr_cast(delim_data), - delim_size); + return ContainsFun::Find(data_ptr_cast(input_data), input_size, + data_ptr_cast(delim_data), delim_size); } }; diff --git a/src/execution/expression_executor.cpp b/src/execution/expression_executor.cpp index 04932265a8cc..a3da2050097b 100644 --- a/src/execution/expression_executor.cpp +++ b/src/execution/expression_executor.cpp @@ -237,7 +237,7 @@ idx_t ExpressionExecutor::Select(const Expression &expr, ExpressionState *state, } template -static inline idx_t DefaultSelectLoop(const SelectionVector *bsel, uint8_t *__restrict bdata, ValidityMask &mask, +static inline idx_t DefaultSelectLoop(const SelectionVector *bsel, const uint8_t *__restrict bdata, ValidityMask &mask, const SelectionVector *sel, idx_t count, SelectionVector *true_sel, SelectionVector *false_sel) { idx_t true_count = 0, false_count = 0; @@ -265,15 +265,15 @@ template static inline idx_t DefaultSelectSwitch(UnifiedVectorFormat &idata, const SelectionVector *sel, idx_t count, SelectionVector *true_sel, SelectionVector *false_sel) { if (true_sel && false_sel) { - return DefaultSelectLoop(idata.sel, (uint8_t *)idata.data, idata.validity, sel, count, - true_sel, false_sel); + return DefaultSelectLoop(idata.sel, UnifiedVectorFormat::GetData(idata), + idata.validity, sel, count, true_sel, false_sel); } else if (true_sel) { - return DefaultSelectLoop(idata.sel, (uint8_t *)idata.data, idata.validity, sel, count, - true_sel, false_sel); + return DefaultSelectLoop(idata.sel, UnifiedVectorFormat::GetData(idata), + idata.validity, sel, count, true_sel, false_sel); } else { D_ASSERT(false_sel); - return DefaultSelectLoop(idata.sel, (uint8_t *)idata.data, idata.validity, sel, count, - true_sel, false_sel); + return DefaultSelectLoop(idata.sel, UnifiedVectorFormat::GetData(idata), + idata.validity, sel, count, true_sel, false_sel); } } diff --git a/src/execution/expression_executor/execute_case.cpp b/src/execution/expression_executor/execute_case.cpp index c08ebe84ad61..073cd05bad7c 100644 --- a/src/execution/expression_executor/execute_case.cpp +++ b/src/execution/expression_executor/execute_case.cpp @@ -105,7 +105,7 @@ void TemplatedFillLoop(Vector &vector, Vector &result, const SelectionVector &se } else { UnifiedVectorFormat vdata; vector.ToUnifiedFormat(count, vdata); - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto source_idx = vdata.sel->get_index(i); auto res_idx = sel.get_index(i); diff --git a/src/execution/index/art/art.cpp b/src/execution/index/art/art.cpp index 80a427af1d9a..1745b92c070e 100644 --- a/src/execution/index/art/art.cpp +++ b/src/execution/index/art/art.cpp @@ -125,7 +125,7 @@ static void TemplatedGenerateKeys(ArenaAllocator &allocator, Vector &input, idx_ input.ToUnifiedFormat(count, idata); D_ASSERT(keys.size() >= count); - auto input_data = (T *)idata.data; + auto input_data = UnifiedVectorFormat::GetData(idata); for (idx_t i = 0; i < count; i++) { auto idx = idata.sel->get_index(i); if (idata.validity.RowIsValid(idx)) { @@ -142,7 +142,7 @@ static void ConcatenateKeys(ArenaAllocator &allocator, Vector &input, idx_t coun UnifiedVectorFormat idata; input.ToUnifiedFormat(count, idata); - auto input_data = (T *)idata.data; + auto input_data = UnifiedVectorFormat::GetData(idata); for (idx_t i = 0; i < count; i++) { auto idx = idata.sel->get_index(i); diff --git a/src/execution/join_hashtable.cpp b/src/execution/join_hashtable.cpp index 9bf872dd0db7..8943e88fee93 100644 --- a/src/execution/join_hashtable.cpp +++ b/src/execution/join_hashtable.cpp @@ -102,7 +102,7 @@ void JoinHashTable::ApplyBitmask(Vector &hashes, const SelectionVector &sel, idx UnifiedVectorFormat hdata; hashes.ToUnifiedFormat(count, hdata); - auto hash_data = (hash_t *)hdata.data; + auto hash_data = UnifiedVectorFormat::GetData(hdata); auto result_data = FlatVector::GetData(pointers); auto main_ht = (data_ptr_t *)hash_map.get(); for (idx_t i = 0; i < count; i++) { diff --git a/src/execution/nested_loop_join/nested_loop_join_inner.cpp b/src/execution/nested_loop_join/nested_loop_join_inner.cpp index 7a49cf7660dd..9343ddb847f6 100644 --- a/src/execution/nested_loop_join/nested_loop_join_inner.cpp +++ b/src/execution/nested_loop_join/nested_loop_join_inner.cpp @@ -24,8 +24,8 @@ struct InitialNestedLoopJoin { left.ToUnifiedFormat(left_size, left_data); right.ToUnifiedFormat(right_size, right_data); - auto ldata = (T *)left_data.data; - auto rdata = (T *)right_data.data; + auto ldata = UnifiedVectorFormat::GetData(left_data); + auto rdata = UnifiedVectorFormat::GetData(right_data); idx_t result_count = 0; for (; rpos < right_size; rpos++) { idx_t right_position = right_data.sel->get_index(rpos); @@ -62,8 +62,8 @@ struct RefineNestedLoopJoin { // refine lvector and rvector based on matches of subsequent conditions (in case there are multiple conditions // in the join) D_ASSERT(current_match_count > 0); - auto ldata = (T *)left_data.data; - auto rdata = (T *)right_data.data; + auto ldata = UnifiedVectorFormat::GetData(left_data); + auto rdata = UnifiedVectorFormat::GetData(right_data); idx_t result_count = 0; for (idx_t i = 0; i < current_match_count; i++) { auto lidx = lvector.get_index(i); diff --git a/src/execution/nested_loop_join/nested_loop_join_mark.cpp b/src/execution/nested_loop_join/nested_loop_join_mark.cpp index 5eb4f182aac2..001897502afd 100644 --- a/src/execution/nested_loop_join/nested_loop_join_mark.cpp +++ b/src/execution/nested_loop_join/nested_loop_join_mark.cpp @@ -10,8 +10,8 @@ static void TemplatedMarkJoin(Vector &left, Vector &right, idx_t lcount, idx_t r left.ToUnifiedFormat(lcount, left_data); right.ToUnifiedFormat(rcount, right_data); - auto ldata = (T *)left_data.data; - auto rdata = (T *)right_data.data; + auto ldata = UnifiedVectorFormat::GetData(left_data); + auto rdata = UnifiedVectorFormat::GetData(right_data); for (idx_t i = 0; i < lcount; i++) { if (found_match[i]) { continue; diff --git a/src/execution/operator/projection/physical_unnest.cpp b/src/execution/operator/projection/physical_unnest.cpp index 71a6aaa1955a..93be4538981e 100644 --- a/src/execution/operator/projection/physical_unnest.cpp +++ b/src/execution/operator/projection/physical_unnest.cpp @@ -65,7 +65,7 @@ void UnnestOperatorState::SetLongestListLength() { if (vector_data.validity.RowIsValid(current_idx)) { // check if this list is longer - auto list_data = (list_entry_t *)vector_data.data; + auto list_data = UnifiedVectorFormat::GetData(vector_data); auto list_entry = list_data[current_idx]; if (list_entry.length > longest_list_length) { longest_list_length = list_entry.length; @@ -98,7 +98,7 @@ static void UnnestNull(idx_t start, idx_t end, Vector &result) { template static void TemplatedUnnest(UnifiedVectorFormat &vector_data, idx_t start, idx_t end, Vector &result) { - auto source_data = (T *)vector_data.data; + auto source_data = UnifiedVectorFormat::GetData(vector_data); auto &source_mask = vector_data.validity; D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR); @@ -314,7 +314,7 @@ OperatorResultType PhysicalUnnest::ExecuteInternal(ExecutionContext &context, Da } else { - auto list_data = (list_entry_t *)vector_data.data; + auto list_data = UnifiedVectorFormat::GetData(vector_data); auto list_entry = list_data[current_idx]; idx_t list_count = 0; diff --git a/src/execution/operator/schema/physical_create_type.cpp b/src/execution/operator/schema/physical_create_type.cpp index 483904427f60..62ef0295ebe1 100644 --- a/src/execution/operator/schema/physical_create_type.cpp +++ b/src/execution/operator/schema/physical_create_type.cpp @@ -45,7 +45,7 @@ SinkResultType PhysicalCreateType::Sink(ExecutionContext &context, DataChunk &ch gstate.capacity *= 2; } - auto src_ptr = (string_t *)sdata.data; + auto src_ptr = UnifiedVectorFormat::GetData(sdata); auto result_ptr = FlatVector::GetData(gstate.result); // Input vector has NULL value, we just throw an exception for (idx_t i = 0; i < chunk.size(); i++) { diff --git a/src/execution/perfect_aggregate_hashtable.cpp b/src/execution/perfect_aggregate_hashtable.cpp index defa0a93fa6b..196a4400c937 100644 --- a/src/execution/perfect_aggregate_hashtable.cpp +++ b/src/execution/perfect_aggregate_hashtable.cpp @@ -51,7 +51,7 @@ PerfectAggregateHashTable::~PerfectAggregateHashTable() { template static void ComputeGroupLocationTemplated(UnifiedVectorFormat &group_data, Value &min, uintptr_t *address_data, idx_t current_shift, idx_t count) { - auto data = (T *)group_data.data; + auto data = UnifiedVectorFormat::GetData(group_data); auto min_val = min.GetValueUnsafe(); if (!group_data.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { diff --git a/src/function/cast/enum_casts.cpp b/src/function/cast/enum_casts.cpp index e0139d790595..062e83d7ab2d 100644 --- a/src/function/cast/enum_casts.cpp +++ b/src/function/cast/enum_casts.cpp @@ -17,7 +17,7 @@ bool EnumEnumCast(Vector &source, Vector &result, idx_t count, CastParameters &p UnifiedVectorFormat vdata; source.ToUnifiedFormat(count, vdata); - auto source_data = (SRC_TYPE *)vdata.data; + auto source_data = UnifiedVectorFormat::GetData(vdata); auto source_sel = vdata.sel; auto source_mask = vdata.validity; @@ -71,7 +71,7 @@ static bool EnumToVarcharCast(Vector &source, Vector &result, idx_t count, CastP UnifiedVectorFormat vdata; source.ToUnifiedFormat(count, vdata); - auto source_data = (SRC *)vdata.data; + auto source_data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto source_idx = vdata.sel->get_index(i); if (!vdata.validity.RowIsValid(source_idx)) { diff --git a/src/function/cast/string_cast.cpp b/src/function/cast/string_cast.cpp index f2abfae5fccf..716676d43aaa 100644 --- a/src/function/cast/string_cast.cpp +++ b/src/function/cast/string_cast.cpp @@ -8,7 +8,7 @@ namespace duckdb { template -bool StringEnumCastLoop(string_t *source_data, ValidityMask &source_mask, const LogicalType &source_type, +bool StringEnumCastLoop(const string_t *source_data, ValidityMask &source_mask, const LogicalType &source_type, T *result_data, ValidityMask &result_mask, const LogicalType &result_type, idx_t count, string *error_message, const SelectionVector *sel) { bool all_converted = true; @@ -55,7 +55,7 @@ bool StringEnumCast(Vector &source, Vector &result, idx_t count, CastParameters result.SetVectorType(VectorType::FLAT_VECTOR); - auto source_data = (string_t *)vdata.data; + auto source_data = UnifiedVectorFormat::GetData(vdata); auto source_sel = vdata.sel; auto source_mask = vdata.validity; auto result_data = FlatVector::GetData(result); @@ -119,9 +119,9 @@ static BoundCastInfo VectorStringCastNumericSwitch(BindCastInput &input, const L //===--------------------------------------------------------------------===// // string -> list casting //===--------------------------------------------------------------------===// -bool VectorStringToList::StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result, - ValidityMask &result_mask, idx_t count, CastParameters ¶meters, - const SelectionVector *sel) { +bool VectorStringToList::StringToNestedTypeCastLoop(const string_t *source_data, ValidityMask &source_mask, + Vector &result, ValidityMask &result_mask, idx_t count, + CastParameters ¶meters, const SelectionVector *sel) { idx_t total_list_size = 0; for (idx_t i = 0; i < count; i++) { idx_t idx = i; @@ -183,8 +183,8 @@ static LogicalType InitVarcharStructType(const LogicalType &target) { //===--------------------------------------------------------------------===// // string -> struct casting //===--------------------------------------------------------------------===// -bool VectorStringToStruct::StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result, - ValidityMask &result_mask, idx_t count, +bool VectorStringToStruct::StringToNestedTypeCastLoop(const string_t *source_data, ValidityMask &source_mask, + Vector &result, ValidityMask &result_mask, idx_t count, CastParameters ¶meters, const SelectionVector *sel) { auto varchar_struct_type = InitVarcharStructType(result.GetType()); Vector varchar_vector(varchar_struct_type, count); @@ -253,9 +253,9 @@ unique_ptr InitMapCastLocalState(CastLocalStateParameters &p return std::move(result); } -bool VectorStringToMap::StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result, - ValidityMask &result_mask, idx_t count, CastParameters ¶meters, - const SelectionVector *sel) { +bool VectorStringToMap::StringToNestedTypeCastLoop(const string_t *source_data, ValidityMask &source_mask, + Vector &result, ValidityMask &result_mask, idx_t count, + CastParameters ¶meters, const SelectionVector *sel) { idx_t total_elements = 0; for (idx_t i = 0; i < count; i++) { idx_t idx = i; @@ -352,7 +352,7 @@ bool StringToNestedTypeCast(Vector &source, Vector &result, idx_t count, CastPar source.ToUnifiedFormat(count, unified_source); auto source_sel = unified_source.sel; - auto source_data = (string_t *)unified_source.data; + auto source_data = UnifiedVectorFormat::GetData(unified_source); auto &source_mask = unified_source.validity; auto &result_mask = FlatVector::Validity(result); diff --git a/src/function/cast/union_casts.cpp b/src/function/cast/union_casts.cpp index 57c78637d057..d7e4436551a4 100644 --- a/src/function/cast/union_casts.cpp +++ b/src/function/cast/union_casts.cpp @@ -293,7 +293,7 @@ static bool UnionToUnionCast(Vector &source, Vector &result, idx_t count, CastPa auto source_row_idx = source_tag_format.sel->get_index(row_idx); if (source_tag_format.validity.RowIsValid(source_row_idx)) { // map the tag - auto source_tag = ((union_tag_t *)source_tag_format.data)[source_row_idx]; + auto source_tag = (UnifiedVectorFormat::GetData(source_tag_format))[source_row_idx]; auto target_tag = cast_data.tag_map[source_tag]; FlatVector::GetData(result_tag_vector)[row_idx] = target_tag; } else { @@ -339,7 +339,7 @@ static bool UnionToVarcharCast(Vector &source, Vector &result, idx_t count, Cast auto mapped_idx = member_vdata.sel->get_index(i); auto member_valid = member_vdata.validity.RowIsValid(mapped_idx); if (member_valid) { - auto member_str = ((string_t *)member_vdata.data)[mapped_idx]; + auto member_str = (UnifiedVectorFormat::GetData(member_vdata))[mapped_idx]; result_data[i] = StringVector::AddString(result, member_str); } else { result_data[i] = StringVector::AddString(result, "NULL"); diff --git a/src/function/cast/vector_cast_helpers.cpp b/src/function/cast/vector_cast_helpers.cpp index f2c204ff3dc7..bfb6360528a6 100644 --- a/src/function/cast/vector_cast_helpers.cpp +++ b/src/function/cast/vector_cast_helpers.cpp @@ -299,8 +299,9 @@ static bool FindValueStruct(const char *buf, idx_t len, idx_t &pos, Vector &varc return false; } -bool VectorStringToStruct::SplitStruct(string_t &input, vector> &varchar_vectors, idx_t &row_idx, - string_map_t &child_names, vector &child_masks) { +bool VectorStringToStruct::SplitStruct(const string_t &input, vector> &varchar_vectors, + idx_t &row_idx, string_map_t &child_names, + vector &child_masks) { const char *buf = input.GetData(); idx_t len = input.GetSize(); idx_t pos = 0; diff --git a/src/function/scalar/list/list_concat.cpp b/src/function/scalar/list/list_concat.cpp index 419dfc550478..0898f513fd59 100644 --- a/src/function/scalar/list/list_concat.cpp +++ b/src/function/scalar/list/list_concat.cpp @@ -25,8 +25,8 @@ static void ListConcatFunction(DataChunk &args, ExpressionState &state, Vector & UnifiedVectorFormat rhs_data; lhs.ToUnifiedFormat(count, lhs_data); rhs.ToUnifiedFormat(count, rhs_data); - auto lhs_entries = (list_entry_t *)lhs_data.data; - auto rhs_entries = (list_entry_t *)rhs_data.data; + auto lhs_entries = UnifiedVectorFormat::GetData(lhs_data); + auto rhs_entries = UnifiedVectorFormat::GetData(rhs_data); auto lhs_list_size = ListVector::GetListSize(lhs); auto rhs_list_size = ListVector::GetListSize(rhs); diff --git a/src/function/scalar/list/list_extract.cpp b/src/function/scalar/list/list_extract.cpp index 276848b88368..dc5a394ae4a5 100644 --- a/src/function/scalar/list/list_extract.cpp +++ b/src/function/scalar/list/list_extract.cpp @@ -32,7 +32,7 @@ void ListExtractTemplate(idx_t count, UnifiedVectorFormat &list_data, UnifiedVec // this is lifted from ExecuteGenericLoop because we can't push the list child data into this otherwise // should have gone with GetValue perhaps - auto child_data = (T *)child_format.data; + auto child_data = UnifiedVectorFormat::GetData(child_format); for (idx_t i = 0; i < count; i++) { auto list_index = list_data.sel->get_index(i); auto offsets_index = offsets_data.sel->get_index(i); @@ -44,8 +44,8 @@ void ListExtractTemplate(idx_t count, UnifiedVectorFormat &list_data, UnifiedVec result_mask.SetInvalid(i); continue; } - auto list_entry = ((list_entry_t *)list_data.data)[list_index]; - auto offsets_entry = ((int64_t *)offsets_data.data)[offsets_index]; + auto list_entry = (UnifiedVectorFormat::GetData(list_data))[list_index]; + auto offsets_entry = (UnifiedVectorFormat::GetData(offsets_data))[offsets_index]; // 1-based indexing if (offsets_entry == 0) { diff --git a/src/function/scalar/string/concat.cpp b/src/function/scalar/string/concat.cpp index 764e1da7cf47..fc3b41142f41 100644 --- a/src/function/scalar/string/concat.cpp +++ b/src/function/scalar/string/concat.cpp @@ -31,7 +31,7 @@ static void ConcatFunction(DataChunk &args, ExpressionState &state, Vector &resu UnifiedVectorFormat vdata; input.ToUnifiedFormat(args.size(), vdata); - auto input_data = (string_t *)vdata.data; + auto input_data = UnifiedVectorFormat::GetData(vdata); // now add the length of each vector to the result length for (idx_t i = 0; i < args.size(); i++) { auto idx = vdata.sel->get_index(i); @@ -77,7 +77,7 @@ static void ConcatFunction(DataChunk &args, ExpressionState &state, Vector &resu UnifiedVectorFormat idata; input.ToUnifiedFormat(args.size(), idata); - auto input_data = (string_t *)idata.data; + auto input_data = UnifiedVectorFormat::GetData(idata); for (idx_t i = 0; i < args.size(); i++) { auto idx = idata.sel->get_index(i); if (!idata.validity.RowIsValid(idx)) { @@ -114,7 +114,7 @@ static void ConcatOperator(DataChunk &args, ExpressionState &state, Vector &resu }); } -static void TemplatedConcatWS(DataChunk &args, string_t *sep_data, const SelectionVector &sep_sel, +static void TemplatedConcatWS(DataChunk &args, const string_t *sep_data, const SelectionVector &sep_sel, const SelectionVector &rsel, idx_t count, Vector &result) { vector result_lengths(args.size(), 0); vector has_results(args.size(), false); @@ -127,7 +127,7 @@ static void TemplatedConcatWS(DataChunk &args, string_t *sep_data, const Selecti for (idx_t col_idx = 1; col_idx < args.ColumnCount(); col_idx++) { auto &idata = orrified_data[col_idx - 1]; - auto input_data = (string_t *)idata.data; + auto input_data = UnifiedVectorFormat::GetData(idata); for (idx_t i = 0; i < count; i++) { auto ridx = rsel.get_index(i); auto sep_idx = sep_sel.get_index(ridx); @@ -157,7 +157,7 @@ static void TemplatedConcatWS(DataChunk &args, string_t *sep_data, const Selecti // now that the empty space for the strings has been allocated, perform the concatenation for (idx_t col_idx = 1; col_idx < args.ColumnCount(); col_idx++) { auto &idata = orrified_data[col_idx - 1]; - auto input_data = (string_t *)idata.data; + auto input_data = UnifiedVectorFormat::GetData(idata); for (idx_t i = 0; i < count; i++) { auto ridx = rsel.get_index(i); auto sep_idx = sep_sel.get_index(ridx); @@ -206,7 +206,7 @@ static void ConcatWSFunction(DataChunk &args, ExpressionState &state, Vector &re } // no null values auto sel = FlatVector::IncrementalSelectionVector(); - TemplatedConcatWS(args, (string_t *)vdata.data, *vdata.sel, *sel, args.size(), result); + TemplatedConcatWS(args, UnifiedVectorFormat::GetData(vdata), *vdata.sel, *sel, args.size(), result); return; } default: { @@ -221,7 +221,8 @@ static void ConcatWSFunction(DataChunk &args, ExpressionState &state, Vector &re not_null_vector.set_index(not_null_count++, i); } } - TemplatedConcatWS(args, (string_t *)vdata.data, *vdata.sel, not_null_vector, not_null_count, result); + TemplatedConcatWS(args, UnifiedVectorFormat::GetData(vdata), *vdata.sel, not_null_vector, + not_null_count, result); return; } } diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index 5e3a755f0393..72fd01201b77 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -86,7 +86,7 @@ static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, V input.data[0].ToUnifiedFormat(input.size(), state_data); for (idx_t i = 0; i < input.size(); i++) { auto state_idx = state_data.sel->get_index(i); - auto state_entry = &((string_t *)state_data.data)[state_idx]; + auto state_entry = &(UnifiedVectorFormat::GetData(state_data))[state_idx]; auto target_ptr = (const char *)local_state.state_buffer.get() + aligned_state_size * i; if (state_data.validity.RowIsValid(state_idx)) { diff --git a/src/function/scalar_function.cpp b/src/function/scalar_function.cpp index 1741c284a783..8be41b442273 100644 --- a/src/function/scalar_function.cpp +++ b/src/function/scalar_function.cpp @@ -60,7 +60,7 @@ bool ScalarFunction::CompareScalarFunctionT(const scalar_function_t &other) cons typedef void(scalar_function_ptr_t)(DataChunk &, ExpressionState &, Vector &); auto func_ptr = (scalar_function_ptr_t **)function.template target(); // NOLINT - auto other_ptr = (scalar_function_ptr_t **)other.template target(); // NOLINT + auto other_ptr = (scalar_function_ptr_t **)other.template target(); // NOLINT // Case the functions were created from lambdas the target will return a nullptr if (!func_ptr && !other_ptr) { diff --git a/src/include/duckdb/common/exception.hpp b/src/include/duckdb/common/exception.hpp index 8396a067de58..5fba1f8dc488 100644 --- a/src/include/duckdb/common/exception.hpp +++ b/src/include/duckdb/common/exception.hpp @@ -22,8 +22,8 @@ enum class PhysicalType : uint8_t; struct LogicalType; struct hugeint_t; -inline void assert_restrict_function(void *left_start, void *left_end, void *right_start, void *right_end, - const char *fname, int linenr) { +inline void assert_restrict_function(const void *left_start, const void *left_end, const void *right_start, + const void *right_end, const char *fname, int linenr) { // assert that the two pointers do not overlap #ifdef DEBUG if (!(left_end <= right_start || right_end <= left_start)) { diff --git a/src/include/duckdb/common/vector_operations/binary_executor.hpp b/src/include/duckdb/common/vector_operations/binary_executor.hpp index 8107730a04df..55c10bb289d1 100644 --- a/src/include/duckdb/common/vector_operations/binary_executor.hpp +++ b/src/include/duckdb/common/vector_operations/binary_executor.hpp @@ -70,7 +70,7 @@ struct BinaryLambdaWrapperWithNulls { struct BinaryExecutor { template - static void ExecuteFlatLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, + static void ExecuteFlatLoop(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, RESULT_TYPE *__restrict result_data, idx_t count, ValidityMask &mask, FUNC fun) { if (!LEFT_CONSTANT) { ASSERT_RESTRICT(ldata, ldata + count, result_data, result_data + count); @@ -184,7 +184,7 @@ struct BinaryExecutor { } template - static void ExecuteGenericLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, + static void ExecuteGenericLoop(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, RESULT_TYPE *__restrict result_data, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, idx_t count, ValidityMask &lvalidity, ValidityMask &rvalidity, ValidityMask &result_validity, FUNC fun) { @@ -221,8 +221,9 @@ struct BinaryExecutor { result.SetVectorType(VectorType::FLAT_VECTOR); auto result_data = FlatVector::GetData(result); ExecuteGenericLoop( - (LEFT_TYPE *)ldata.data, (RIGHT_TYPE *)rdata.data, result_data, ldata.sel, rdata.sel, count, ldata.validity, - rdata.validity, FlatVector::Validity(result), fun); + UnifiedVectorFormat::GetData(ldata), UnifiedVectorFormat::GetData(rdata), + result_data, ldata.sel, rdata.sel, count, ldata.validity, rdata.validity, FlatVector::Validity(result), + fun); } template @@ -300,7 +301,7 @@ struct BinaryExecutor { template - static inline idx_t SelectFlatLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, + static inline idx_t SelectFlatLoop(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *sel, idx_t count, ValidityMask &validity_mask, SelectionVector *true_sel, SelectionVector *false_sel) { idx_t true_count = 0, false_count = 0; @@ -364,7 +365,7 @@ struct BinaryExecutor { } template - static inline idx_t SelectFlatLoopSwitch(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, + static inline idx_t SelectFlatLoopSwitch(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *sel, idx_t count, ValidityMask &mask, SelectionVector *true_sel, SelectionVector *false_sel) { if (true_sel && false_sel) { @@ -419,10 +420,10 @@ struct BinaryExecutor { template static inline idx_t - SelectGenericLoop(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, const SelectionVector *__restrict lsel, - const SelectionVector *__restrict rsel, const SelectionVector *__restrict result_sel, idx_t count, - ValidityMask &lvalidity, ValidityMask &rvalidity, SelectionVector *true_sel, - SelectionVector *false_sel) { + SelectGenericLoop(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, + const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, + const SelectionVector *__restrict result_sel, idx_t count, ValidityMask &lvalidity, + ValidityMask &rvalidity, SelectionVector *true_sel, SelectionVector *false_sel) { idx_t true_count = 0, false_count = 0; for (idx_t i = 0; i < count; i++) { auto result_idx = result_sel->get_index(i); @@ -447,7 +448,7 @@ struct BinaryExecutor { } template static inline idx_t - SelectGenericLoopSelSwitch(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, + SelectGenericLoopSelSwitch(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, const SelectionVector *__restrict result_sel, idx_t count, ValidityMask &lvalidity, ValidityMask &rvalidity, SelectionVector *true_sel, SelectionVector *false_sel) { @@ -466,7 +467,7 @@ struct BinaryExecutor { template static inline idx_t - SelectGenericLoopSwitch(LEFT_TYPE *__restrict ldata, RIGHT_TYPE *__restrict rdata, + SelectGenericLoopSwitch(const LEFT_TYPE *__restrict ldata, const RIGHT_TYPE *__restrict rdata, const SelectionVector *__restrict lsel, const SelectionVector *__restrict rsel, const SelectionVector *__restrict result_sel, idx_t count, ValidityMask &lvalidity, ValidityMask &rvalidity, SelectionVector *true_sel, SelectionVector *false_sel) { @@ -487,9 +488,9 @@ struct BinaryExecutor { left.ToUnifiedFormat(count, ldata); right.ToUnifiedFormat(count, rdata); - return SelectGenericLoopSwitch((LEFT_TYPE *)ldata.data, (RIGHT_TYPE *)rdata.data, - ldata.sel, rdata.sel, sel, count, ldata.validity, - rdata.validity, true_sel, false_sel); + return SelectGenericLoopSwitch( + UnifiedVectorFormat::GetData(ldata), UnifiedVectorFormat::GetData(rdata), ldata.sel, + rdata.sel, sel, count, ldata.validity, rdata.validity, true_sel, false_sel); } template diff --git a/src/include/duckdb/common/vector_operations/ternary_executor.hpp b/src/include/duckdb/common/vector_operations/ternary_executor.hpp index 8d5ccaf1c458..44ac3142fafe 100644 --- a/src/include/duckdb/common/vector_operations/ternary_executor.hpp +++ b/src/include/duckdb/common/vector_operations/ternary_executor.hpp @@ -41,11 +41,11 @@ struct TernaryLambdaWrapperWithNulls { struct TernaryExecutor { private: template - static inline void ExecuteLoop(A_TYPE *__restrict adata, B_TYPE *__restrict bdata, C_TYPE *__restrict cdata, - RESULT_TYPE *__restrict result_data, idx_t count, const SelectionVector &asel, - const SelectionVector &bsel, const SelectionVector &csel, ValidityMask &avalidity, - ValidityMask &bvalidity, ValidityMask &cvalidity, ValidityMask &result_validity, - FUN fun) { + static inline void ExecuteLoop(const A_TYPE *__restrict adata, const B_TYPE *__restrict bdata, + const C_TYPE *__restrict cdata, RESULT_TYPE *__restrict result_data, idx_t count, + const SelectionVector &asel, const SelectionVector &bsel, + const SelectionVector &csel, ValidityMask &avalidity, ValidityMask &bvalidity, + ValidityMask &cvalidity, ValidityMask &result_validity, FUN fun) { if (!avalidity.AllValid() || !bvalidity.AllValid() || !cvalidity.AllValid()) { for (idx_t i = 0; i < count; i++) { auto aidx = asel.get_index(i); @@ -95,9 +95,10 @@ struct TernaryExecutor { c.ToUnifiedFormat(count, cdata); ExecuteLoop( - (A_TYPE *)adata.data, (B_TYPE *)bdata.data, (C_TYPE *)cdata.data, - FlatVector::GetData(result), count, *adata.sel, *bdata.sel, *cdata.sel, adata.validity, - bdata.validity, cdata.validity, FlatVector::Validity(result), fun); + UnifiedVectorFormat::GetData(adata), UnifiedVectorFormat::GetData(bdata), + UnifiedVectorFormat::GetData(cdata), FlatVector::GetData(result), count, + *adata.sel, *bdata.sel, *cdata.sel, adata.validity, bdata.validity, cdata.validity, + FlatVector::Validity(result), fun); } } @@ -122,11 +123,11 @@ struct TernaryExecutor { private: template - static inline idx_t SelectLoop(A_TYPE *__restrict adata, B_TYPE *__restrict bdata, C_TYPE *__restrict cdata, - const SelectionVector *result_sel, idx_t count, const SelectionVector &asel, - const SelectionVector &bsel, const SelectionVector &csel, ValidityMask &avalidity, - ValidityMask &bvalidity, ValidityMask &cvalidity, SelectionVector *true_sel, - SelectionVector *false_sel) { + static inline idx_t SelectLoop(const A_TYPE *__restrict adata, const B_TYPE *__restrict bdata, + const C_TYPE *__restrict cdata, const SelectionVector *result_sel, idx_t count, + const SelectionVector &asel, const SelectionVector &bsel, + const SelectionVector &csel, ValidityMask &avalidity, ValidityMask &bvalidity, + ValidityMask &cvalidity, SelectionVector *true_sel, SelectionVector *false_sel) { idx_t true_count = 0, false_count = 0; for (idx_t i = 0; i < count; i++) { auto result_idx = result_sel->get_index(i); diff --git a/src/include/duckdb/common/vector_operations/unary_executor.hpp b/src/include/duckdb/common/vector_operations/unary_executor.hpp index 3caf591217d5..d7f16f78b002 100644 --- a/src/include/duckdb/common/vector_operations/unary_executor.hpp +++ b/src/include/duckdb/common/vector_operations/unary_executor.hpp @@ -58,7 +58,7 @@ struct UnaryStringOperator { struct UnaryExecutor { private: template - static inline void ExecuteLoop(INPUT_TYPE *__restrict ldata, RESULT_TYPE *__restrict result_data, idx_t count, + static inline void ExecuteLoop(const INPUT_TYPE *__restrict ldata, RESULT_TYPE *__restrict result_data, idx_t count, const SelectionVector *__restrict sel_vector, ValidityMask &mask, ValidityMask &result_mask, void *dataptr, bool adds_nulls) { #ifdef DEBUG @@ -95,7 +95,7 @@ struct UnaryExecutor { } template - static inline void ExecuteFlat(INPUT_TYPE *__restrict ldata, RESULT_TYPE *__restrict result_data, idx_t count, + static inline void ExecuteFlat(const INPUT_TYPE *__restrict ldata, RESULT_TYPE *__restrict result_data, idx_t count, ValidityMask &mask, ValidityMask &result_mask, void *dataptr, bool adds_nulls) { ASSERT_RESTRICT(ldata, ldata + count, result_data, result_data + count); @@ -175,7 +175,7 @@ struct UnaryExecutor { result.SetVectorType(VectorType::FLAT_VECTOR); auto result_data = FlatVector::GetData(result); - auto ldata = (INPUT_TYPE *)vdata.data; + auto ldata = UnifiedVectorFormat::GetData(vdata); ExecuteLoop(ldata, result_data, count, vdata.sel, vdata.validity, FlatVector::Validity(result), dataptr, adds_nulls); diff --git a/src/include/duckdb/function/cast/vector_cast_helpers.hpp b/src/include/duckdb/function/cast/vector_cast_helpers.hpp index 1256c9099f79..c7138f971c05 100644 --- a/src/include/duckdb/function/cast/vector_cast_helpers.hpp +++ b/src/include/duckdb/function/cast/vector_cast_helpers.hpp @@ -201,15 +201,15 @@ struct VectorCastHelpers { struct VectorStringToList { static idx_t CountPartsList(const string_t &input); static bool SplitStringList(const string_t &input, string_t *child_data, idx_t &child_start, Vector &child); - static bool StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result, + static bool StringToNestedTypeCastLoop(const string_t *source_data, ValidityMask &source_mask, Vector &result, ValidityMask &result_mask, idx_t count, CastParameters ¶meters, const SelectionVector *sel); }; struct VectorStringToStruct { - static bool SplitStruct(string_t &input, vector> &varchar_vectors, idx_t &row_idx, + static bool SplitStruct(const string_t &input, vector> &varchar_vectors, idx_t &row_idx, string_map_t &child_names, vector &child_masks); - static bool StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result, + static bool StringToNestedTypeCastLoop(const string_t *source_data, ValidityMask &source_mask, Vector &result, ValidityMask &result_mask, idx_t count, CastParameters ¶meters, const SelectionVector *sel); }; @@ -218,7 +218,7 @@ struct VectorStringToMap { static idx_t CountPartsMap(const string_t &input); static bool SplitStringMap(const string_t &input, string_t *child_key_data, string_t *child_val_data, idx_t &child_start, Vector &varchar_key, Vector &varchar_val); - static bool StringToNestedTypeCastLoop(string_t *source_data, ValidityMask &source_mask, Vector &result, + static bool StringToNestedTypeCastLoop(const string_t *source_data, ValidityMask &source_mask, Vector &result, ValidityMask &result_mask, idx_t count, CastParameters ¶meters, const SelectionVector *sel); }; diff --git a/src/include/duckdb/parser/transformer.hpp b/src/include/duckdb/parser/transformer.hpp index df845dcb873f..1ad94af61220 100644 --- a/src/include/duckdb/parser/transformer.hpp +++ b/src/include/duckdb/parser/transformer.hpp @@ -341,11 +341,11 @@ class Transformer { StackChecker StackCheck(idx_t extra_stack = 1); public: - template + template static T &PGCast(duckdb_libpgquery::PGNode &node) { return reinterpret_cast(node); } - template + template static optional_ptr PGPointerCast(void *ptr) { return optional_ptr(reinterpret_cast(ptr)); } diff --git a/src/main/capi/result-c.cpp b/src/main/capi/result-c.cpp index e17197687363..e29e90632d8a 100644 --- a/src/main/capi/result-c.cpp +++ b/src/main/capi/result-c.cpp @@ -40,7 +40,7 @@ struct CBlobConverter { result.data = (char *)duckdb_malloc(input.GetSize()); result.size = input.GetSize(); assert(result.data); - memcpy((void *)result.data, input.GetData(), input.GetSize()); + memcpy(result.data, input.GetData(), input.GetSize()); return result; } diff --git a/src/parser/transform/expression/transform_columnref.cpp b/src/parser/transform/expression/transform_columnref.cpp index f8ef3f956cb6..628c2130863d 100644 --- a/src/parser/transform/expression/transform_columnref.cpp +++ b/src/parser/transform/expression/transform_columnref.cpp @@ -23,7 +23,8 @@ unique_ptr Transformer::TransformStarExpression(duckdb_libpgqu for (auto head = star.replace_list->head; head; head = head->next) { auto list = PGPointerCast(head->data.ptr_value); D_ASSERT(list->length == 2); - auto replace_expression = TransformExpression(PGPointerCast(list->head->data.ptr_value)); + auto replace_expression = + TransformExpression(PGPointerCast(list->head->data.ptr_value)); auto value = PGPointerCast(list->tail->data.ptr_value); D_ASSERT(value->type == duckdb_libpgquery::T_PGString); string exclude_entry = value->val.str; diff --git a/src/parser/transform/expression/transform_expression.cpp b/src/parser/transform/expression/transform_expression.cpp index 4e2527ef0e79..6eaeeed96d2c 100644 --- a/src/parser/transform/expression/transform_expression.cpp +++ b/src/parser/transform/expression/transform_expression.cpp @@ -88,7 +88,8 @@ unique_ptr Transformer::TransformExpression(optional_ptr> &result) { +void Transformer::TransformExpressionList(duckdb_libpgquery::PGList &list, + vector> &result) { for (auto node = list.head; node != nullptr; node = node->next) { auto target = PGPointerCast(node->data.ptr_value); diff --git a/src/parser/transform/helpers/transform_cte.cpp b/src/parser/transform/helpers/transform_cte.cpp index 1d3faa546d6d..0e43d3e80488 100644 --- a/src/parser/transform/helpers/transform_cte.cpp +++ b/src/parser/transform/helpers/transform_cte.cpp @@ -68,7 +68,8 @@ void Transformer::TransformCTE(duckdb_libpgquery::PGWithClause &de_with_clause, info->query = TransformRecursiveCTE(cte, *info); } else { Transformer cte_transformer(*this); - info->query = cte_transformer.TransformSelect(*PGPointerCast(cte.ctequery)); + info->query = + cte_transformer.TransformSelect(*PGPointerCast(cte.ctequery)); } D_ASSERT(info->query); auto cte_name = string(cte.ctename); diff --git a/src/parser/transform/helpers/transform_typename.cpp b/src/parser/transform/helpers/transform_typename.cpp index c43ef2ce0a4a..66be3f382f44 100644 --- a/src/parser/transform/helpers/transform_typename.cpp +++ b/src/parser/transform/helpers/transform_typename.cpp @@ -58,8 +58,10 @@ LogicalType Transformer::TransformTypeName(duckdb_libpgquery::PGTypeName &type_n if (!type_name.typmods || type_name.typmods->length != 2) { throw ParserException("Map type needs exactly two entries, key and value type"); } - auto key_type = TransformTypeName(*PGPointerCast(type_name.typmods->head->data.ptr_value)); - auto value_type = TransformTypeName(*PGPointerCast(type_name.typmods->tail->data.ptr_value)); + auto key_type = + TransformTypeName(*PGPointerCast(type_name.typmods->head->data.ptr_value)); + auto value_type = + TransformTypeName(*PGPointerCast(type_name.typmods->tail->data.ptr_value)); result_type = LogicalType::MAP(std::move(key_type), std::move(value_type)); } else if (base_type == LogicalTypeId::UNION) { diff --git a/src/parser/transform/statement/transform_create_function.cpp b/src/parser/transform/statement/transform_create_function.cpp index bf6970f4ba2d..4bdc21d255e2 100644 --- a/src/parser/transform/statement/transform_create_function.cpp +++ b/src/parser/transform/statement/transform_create_function.cpp @@ -22,13 +22,13 @@ unique_ptr Transformer::TransformCreateFunction(duckdb_libpgque auto expression = TransformExpression(stmt.function); macro_func = make_uniq(std::move(expression)); } else if (stmt.query) { - auto query_node = TransformSelect(*PGPointerCast(stmt.query), true)->node->Copy(); + auto query_node = + TransformSelect(*PGPointerCast(stmt.query), true)->node->Copy(); macro_func = make_uniq(std::move(query_node)); } PivotEntryCheck("macro"); - auto info = - make_uniq(stmt.function ? CatalogType::MACRO_ENTRY : CatalogType::TABLE_MACRO_ENTRY); + auto info = make_uniq(stmt.function ? CatalogType::MACRO_ENTRY : CatalogType::TABLE_MACRO_ENTRY); info->catalog = qname.catalog; info->schema = qname.schema; info->name = qname.name; diff --git a/src/parser/transform/tableref/transform_table_function.cpp b/src/parser/transform/tableref/transform_table_function.cpp index d2a1c5d764a9..5d8b05c2d001 100644 --- a/src/parser/transform/tableref/transform_table_function.cpp +++ b/src/parser/transform/tableref/transform_table_function.cpp @@ -32,7 +32,8 @@ unique_ptr Transformer::TransformRangeFunction(duckdb_libpgquery::PGRa break; } case duckdb_libpgquery::T_PGSQLValueFunction: - result->function = TransformSQLValueFunction(*PGPointerCast(call_tree.get())); + result->function = + TransformSQLValueFunction(*PGPointerCast(call_tree.get())); break; default: throw ParserException("Not a function call or value function"); diff --git a/src/storage/compression/bitpacking.cpp b/src/storage/compression/bitpacking.cpp index a70ccb60e27e..ed472f618875 100644 --- a/src/storage/compression/bitpacking.cpp +++ b/src/storage/compression/bitpacking.cpp @@ -324,7 +324,7 @@ bool BitpackingAnalyze(AnalyzeState &state, Vector &input, idx_t count) { UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); if (!analyze_state.state.template Update(data[idx], vdata.validity.RowIsValid(idx))) { @@ -482,7 +482,7 @@ struct BitpackingCompressState : public CompressionState { } void Append(UnifiedVectorFormat &vdata, idx_t count) { - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); diff --git a/src/storage/compression/dictionary_compression.cpp b/src/storage/compression/dictionary_compression.cpp index 00716d094bfe..2a7d5ddbe447 100644 --- a/src/storage/compression/dictionary_compression.cpp +++ b/src/storage/compression/dictionary_compression.cpp @@ -18,7 +18,7 @@ class DictionaryCompressionState : public CompressionState { bool UpdateState(Vector &scan_vector, idx_t count) { UnifiedVectorFormat vdata; scan_vector.ToUnifiedFormat(count, vdata); - auto data = (string_t *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); Verify(); for (idx_t i = 0; i < count; i++) { diff --git a/src/storage/compression/fixed_size_uncompressed.cpp b/src/storage/compression/fixed_size_uncompressed.cpp index 7b2f428f0415..11f3a3c79e4e 100644 --- a/src/storage/compression/fixed_size_uncompressed.cpp +++ b/src/storage/compression/fixed_size_uncompressed.cpp @@ -185,7 +185,7 @@ struct StandardFixedSizeAppend { template static void Append(SegmentStatistics &stats, data_ptr_t target, idx_t target_offset, UnifiedVectorFormat &adata, idx_t offset, idx_t count) { - auto sdata = (T *)adata.data; + auto sdata = UnifiedVectorFormat::GetData(adata); auto tdata = (T *)target; if (!adata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { @@ -216,7 +216,7 @@ struct ListFixedSizeAppend { template static void Append(SegmentStatistics &stats, data_ptr_t target, idx_t target_offset, UnifiedVectorFormat &adata, idx_t offset, idx_t count) { - auto sdata = (uint64_t *)adata.data; + auto sdata = UnifiedVectorFormat::GetData(adata); auto tdata = (uint64_t *)target; for (idx_t i = 0; i < count; i++) { auto source_idx = adata.sel->get_index(offset + i); diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index fd934ec58e71..dadc1cc600b7 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -99,7 +99,7 @@ bool FSSTStorage::StringAnalyze(AnalyzeState &state_p, Vector &input, idx_t coun input.ToUnifiedFormat(count, vdata); state.count += count; - auto data = (string_t *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); // Note that we ignore the sampling in case we have not found any valid strings yet, this solves the issue of // not having seen any valid strings here leading to an empty fsst symbol table. @@ -413,7 +413,7 @@ void FSSTStorage::Compress(CompressionState &state_p, Vector &scan_vector, idx_t // Get vector data UnifiedVectorFormat vdata; scan_vector.ToUnifiedFormat(count, vdata); - auto data = (string_t *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); // Collect pointers to strings to compress vector sizes_in; diff --git a/src/storage/compression/rle.cpp b/src/storage/compression/rle.cpp index 70ceaeb2278f..f4ceec62e279 100644 --- a/src/storage/compression/rle.cpp +++ b/src/storage/compression/rle.cpp @@ -40,7 +40,7 @@ struct RLEState { } template - void Update(T *data, ValidityMask &validity, idx_t idx) { + void Update(const T *data, ValidityMask &validity, idx_t idx) { if (validity.RowIsValid(idx)) { if (all_null) { // no value seen yet @@ -98,7 +98,7 @@ bool RLEAnalyze(AnalyzeState &state, Vector &input, idx_t count) { UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); rle_state.state.Update(data, vdata.validity, idx); @@ -156,7 +156,7 @@ struct RLECompressState : public CompressionState { } void Append(UnifiedVectorFormat &vdata, idx_t count) { - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); state.template Update::RLEWriter>(data, vdata.validity, idx); diff --git a/src/storage/compression/string_uncompressed.cpp b/src/storage/compression/string_uncompressed.cpp index 2aed65bd858f..e4469a5f6830 100644 --- a/src/storage/compression/string_uncompressed.cpp +++ b/src/storage/compression/string_uncompressed.cpp @@ -38,7 +38,7 @@ bool UncompressedStringStorage::StringAnalyze(AnalyzeState &state_p, Vector &inp input.ToUnifiedFormat(count, vdata); state.count += count; - auto data = (string_t *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); if (vdata.validity.RowIsValid(idx)) { diff --git a/src/storage/data_table.cpp b/src/storage/data_table.cpp index 78be26c1a6ce..79bc73137c62 100644 --- a/src/storage/data_table.cpp +++ b/src/storage/data_table.cpp @@ -302,7 +302,7 @@ static void VerifyCheckConstraint(ClientContext &context, TableCatalogEntry &tab UnifiedVectorFormat vdata; result.ToUnifiedFormat(chunk.size(), vdata); - auto dataptr = (int32_t *)vdata.data; + auto dataptr = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < chunk.size(); i++) { auto idx = vdata.sel->get_index(i); if (vdata.validity.RowIsValid(idx) && dataptr[idx] == 0) { diff --git a/src/storage/table/list_column_data.cpp b/src/storage/table/list_column_data.cpp index 7eed8ff9ad3b..48d8fdaca86c 100644 --- a/src/storage/table/list_column_data.cpp +++ b/src/storage/table/list_column_data.cpp @@ -166,7 +166,7 @@ void ListColumnData::Append(BaseStatistics &stats, ColumnAppendState &state, Vec auto &list_validity = list_data.validity; // construct the list_entry_t entries to append to the column data - auto input_offsets = (list_entry_t *)list_data.data; + auto input_offsets = UnifiedVectorFormat::GetData(list_data); auto start_offset = child_column->GetMaxEntry(); idx_t child_count = 0; diff --git a/tools/sqlite3_api_wrapper/sqlite3_udf_api/include/cast_sqlite.hpp b/tools/sqlite3_api_wrapper/sqlite3_udf_api/include/cast_sqlite.hpp index 4879e6984f4c..62f9e6c720f9 100644 --- a/tools/sqlite3_api_wrapper/sqlite3_udf_api/include/cast_sqlite.hpp +++ b/tools/sqlite3_api_wrapper/sqlite3_udf_api/include/cast_sqlite.hpp @@ -91,7 +91,7 @@ struct CastToVectorSQLiteValue { unique_ptr> result = make_uniq>(count); auto res_data = (*result).data(); - auto input_data = (INPUT_TYPE *)vec_data.data; + auto input_data = UnifiedVectorFormat::GetData(vec_data); if (vec_data.validity.AllValid()) { for (idx_t i = 0; i < count; ++i) { From 4e38e6f9bac5d189de9bf87f4b312df5c58eafa3 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Mon, 22 May 2023 14:35:17 +0200 Subject: [PATCH 07/28] Many more C-style cast removals --- src/catalog/catalog_set.cpp | 2 +- src/common/arrow/arrow_appender.cpp | 16 ++-- src/common/checksum.cpp | 2 +- src/common/compressed_file_system.cpp | 10 +- src/common/field_writer.cpp | 2 +- src/common/gzip_file_system.cpp | 6 +- src/common/local_file_system.cpp | 22 ++--- src/common/pipe_file_system.cpp | 4 +- .../scalar/list/list_aggregates.cpp | 2 +- .../aggregate/sorted_aggregate_function.cpp | 2 +- src/function/cast/cast_function_set.cpp | 2 +- src/function/scalar/operators/arithmetic.cpp | 5 +- src/function/scalar/string/contains.cpp | 8 +- src/function/scalar/string/like.cpp | 10 +- src/function/scalar/string/regexp.cpp | 6 +- .../string/regexp/regexp_extract_all.cpp | 6 +- src/function/scalar/struct/struct_extract.cpp | 2 +- .../scalar/system/aggregate_export.cpp | 14 +-- src/function/scalar_function.cpp | 2 +- src/function/table/arrow.cpp | 8 +- src/function/table/arrow_conversion.cpp | 91 ++++++++++--------- src/function/table/checkpoint.cpp | 2 +- src/function/table/copy_csv.cpp | 12 +-- src/function/table/glob.cpp | 2 +- .../table/pragma_last_profiling_output.cpp | 2 +- src/function/table/range.cpp | 8 +- src/function/table/read_csv.cpp | 8 +- src/function/table/repeat.cpp | 4 +- src/function/table/repeat_row.cpp | 4 +- .../table/system/duckdb_functions.cpp | 2 +- .../table/system/test_vector_types.cpp | 2 +- src/function/table/table_scan.cpp | 2 +- src/function/table/unnest.cpp | 2 +- src/function/table/version/pragma_version.cpp | 2 +- .../duckdb/common/arrow/arrow_buffer.hpp | 6 ++ src/include/duckdb/common/file_system.hpp | 11 +++ src/include/duckdb/common/typedefs.hpp | 5 + .../duckdb/function/cast/default_casts.hpp | 11 +++ 38 files changed, 172 insertions(+), 135 deletions(-) diff --git a/src/catalog/catalog_set.cpp b/src/catalog/catalog_set.cpp index 60768df1d6b2..ed2415379e80 100644 --- a/src/catalog/catalog_set.cpp +++ b/src/catalog/catalog_set.cpp @@ -42,7 +42,7 @@ class EntryDropper { }; CatalogSet::CatalogSet(Catalog &catalog_p, unique_ptr defaults) - : catalog((DuckCatalog &)catalog_p), defaults(std::move(defaults)) { + : catalog(catalog_p.Cast()), defaults(std::move(defaults)) { D_ASSERT(catalog_p.IsDuckCatalog()); } CatalogSet::~CatalogSet() { diff --git a/src/common/arrow/arrow_appender.cpp b/src/common/arrow/arrow_appender.cpp index 084f1b2f8aff..a9beab206f11 100644 --- a/src/common/arrow/arrow_appender.cpp +++ b/src/common/arrow/arrow_appender.cpp @@ -163,7 +163,7 @@ struct ArrowScalarBaseData { // append the main data append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(TGT) * size); auto data = UnifiedVectorFormat::GetData(format); - auto result_data = (TGT *)append_data.main_buffer.data(); + auto result_data = append_data.main_buffer.GetData(); for (idx_t i = from; i < to; i++) { auto source_idx = format.sel->get_index(i); @@ -210,8 +210,8 @@ struct ArrowEnumData : public ArrowScalarBaseData { // resize the offset buffer - the offset buffer holds the offsets into the child array append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(uint32_t) * (size + 1)); - auto data = (string_t *)FlatVector::GetData(input); - auto offset_data = (uint32_t *)append_data.main_buffer.data(); + auto data = FlatVector::GetData(input); + auto offset_data = append_data.main_buffer.GetData(); if (append_data.row_count == 0) { // first entry offset_data[0] = 0; @@ -271,8 +271,8 @@ struct ArrowBoolData { ResizeValidity(append_data.main_buffer, append_data.row_count + size); auto data = UnifiedVectorFormat::GetData(format); - auto result_data = (uint8_t *)append_data.main_buffer.data(); - auto validity_data = (uint8_t *)append_data.validity.data(); + auto result_data = append_data.main_buffer.GetData(); + auto validity_data = append_data.validity.GetData(); uint8_t current_bit; idx_t current_byte; GetBitPosition(append_data.row_count, current_byte, current_bit); @@ -318,7 +318,7 @@ struct ArrowUUIDConverter { template static void WriteData(data_ptr_t target, SRC input) { - UUID::ToString(input, (char *)target); + UUID::ToString(input, data_ptr_cast(target)); } }; @@ -342,7 +342,7 @@ struct ArrowVarcharData { // resize the offset buffer - the offset buffer holds the offsets into the child array append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(BUFTYPE) * (size + 1)); auto data = UnifiedVectorFormat::GetData(format); - auto offset_data = (BUFTYPE *)append_data.main_buffer.data(); + auto offset_data = append_data.main_buffer.GetData(); if (append_data.row_count == 0) { // first entry offset_data[0] = 0; @@ -442,7 +442,7 @@ void AppendListOffsets(ArrowAppendData &append_data, UnifiedVectorFormat &format idx_t size = to - from; append_data.main_buffer.resize(append_data.main_buffer.size() + sizeof(uint32_t) * (size + 1)); auto data = UnifiedVectorFormat::GetData(format); - auto offset_data = (uint32_t *)append_data.main_buffer.data(); + auto offset_data = append_data.main_buffer.GetData(); if (append_data.row_count == 0) { // first entry offset_data[0] = 0; diff --git a/src/common/checksum.cpp b/src/common/checksum.cpp index c1fab841f547..2fbca299e215 100644 --- a/src/common/checksum.cpp +++ b/src/common/checksum.cpp @@ -9,7 +9,7 @@ hash_t Checksum(uint64_t x) { uint64_t Checksum(uint8_t *buffer, size_t size) { uint64_t result = 5381; - uint64_t *ptr = (uint64_t *)buffer; + uint64_t *ptr = reinterpret_cast(buffer); size_t i; // for efficiency, we first checksum uint64_t values for (i = 0; i < size / 8; i++) { diff --git a/src/common/compressed_file_system.cpp b/src/common/compressed_file_system.cpp index b72080b2bf1b..93526e25717f 100644 --- a/src/common/compressed_file_system.cpp +++ b/src/common/compressed_file_system.cpp @@ -115,28 +115,28 @@ void CompressedFile::Close() { } int64_t CompressedFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes) { - auto &compressed_file = (CompressedFile &)handle; + auto &compressed_file = handle.Cast(); return compressed_file.ReadData(buffer, nr_bytes); } int64_t CompressedFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes) { - auto &compressed_file = (CompressedFile &)handle; + auto &compressed_file = handle.Cast(); return compressed_file.WriteData((data_ptr_t)buffer, nr_bytes); } void CompressedFileSystem::Reset(FileHandle &handle) { - auto &compressed_file = (CompressedFile &)handle; + auto &compressed_file = handle.Cast(); compressed_file.child_handle->Reset(); compressed_file.Initialize(compressed_file.write); } int64_t CompressedFileSystem::GetFileSize(FileHandle &handle) { - auto &compressed_file = (CompressedFile &)handle; + auto &compressed_file = handle.Cast(); return compressed_file.child_handle->GetFileSize(); } bool CompressedFileSystem::OnDiskFile(FileHandle &handle) { - auto &compressed_file = (CompressedFile &)handle; + auto &compressed_file = handle.Cast(); return compressed_file.child_handle->OnDiskFile(); } diff --git a/src/common/field_writer.cpp b/src/common/field_writer.cpp index fba4a850abd0..be667b64dd3c 100644 --- a/src/common/field_writer.cpp +++ b/src/common/field_writer.cpp @@ -29,7 +29,7 @@ template <> void FieldWriter::Write(const string &val) { Write((uint32_t)val.size()); if (!val.empty()) { - WriteData((const_data_ptr_t)val.c_str(), val.size()); + WriteData(data_ptr_cast(val.c_str()), val.size()); } } diff --git a/src/common/gzip_file_system.cpp b/src/common/gzip_file_system.cpp index 85fd551cf175..bf0511b44c20 100644 --- a/src/common/gzip_file_system.cpp +++ b/src/common/gzip_file_system.cpp @@ -193,7 +193,7 @@ bool MiniZStreamWrapper::Read(StreamData &sd) { throw IOException("Failed to decode gzip stream: %s", duckdb_miniz::mz_error(ret)); } // update pointers following inflate() - sd.in_buff_start = (data_ptr_t)mz_stream_ptr->next_in; + sd.in_buff_start = data_ptr_cast(mz_stream_ptr->next_in); sd.in_buff_end = sd.in_buff_start + mz_stream_ptr->avail_in; sd.out_buff_end = (data_ptr_t)mz_stream_ptr->next_out; D_ASSERT(sd.out_buff_end + mz_stream_ptr->avail_out == sd.out_buff.get() + sd.out_buf_size); @@ -360,7 +360,7 @@ string GZipFileSystem::UncompressGZIPString(const string &in) { } auto bytes_remaining = in.size() - (body_ptr - in.data()); - mz_stream_ptr->next_in = (unsigned char *)body_ptr; + mz_stream_ptr->next_in = data_ptr_cast(body_ptr); mz_stream_ptr->avail_in = bytes_remaining; unsigned char decompress_buffer[BUFSIZ]; @@ -373,7 +373,7 @@ string GZipFileSystem::UncompressGZIPString(const string &in) { if (status != duckdb_miniz::MZ_STREAM_END && status != duckdb_miniz::MZ_OK) { throw IOException("Failed to uncompress"); } - decompressed.append((char *)decompress_buffer, mz_stream_ptr->total_out - decompressed.size()); + decompressed.append(data_ptr_cast(decompress_buffer), mz_stream_ptr->total_out - decompressed.size()); } duckdb_miniz::mz_inflateEnd(mz_stream_ptr); if (decompressed.empty()) { diff --git a/src/common/local_file_system.cpp b/src/common/local_file_system.cpp index 958d4c345dbd..ab9c35d368cf 100644 --- a/src/common/local_file_system.cpp +++ b/src/common/local_file_system.cpp @@ -242,7 +242,7 @@ unique_ptr LocalFileSystem::OpenFile(const string &path_p, uint8_t f } void LocalFileSystem::SetFilePointer(FileHandle &handle, idx_t location) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; off_t offset = lseek(fd, location, SEEK_SET); if (offset == (off_t)-1) { throw IOException("Could not seek to location %lld for file \"%s\": %s", location, handle.path, @@ -251,7 +251,7 @@ void LocalFileSystem::SetFilePointer(FileHandle &handle, idx_t location) { } idx_t LocalFileSystem::GetFilePointer(FileHandle &handle) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; off_t position = lseek(fd, 0, SEEK_CUR); if (position == (off_t)-1) { throw IOException("Could not get file position file \"%s\": %s", handle.path, strerror(errno)); @@ -260,7 +260,7 @@ idx_t LocalFileSystem::GetFilePointer(FileHandle &handle) { } void LocalFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; int64_t bytes_read = pread(fd, buffer, nr_bytes, location); if (bytes_read == -1) { throw IOException("Could not read from file \"%s\": %s", handle.path, strerror(errno)); @@ -272,7 +272,7 @@ void LocalFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes, i } int64_t LocalFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; int64_t bytes_read = read(fd, buffer, nr_bytes); if (bytes_read == -1) { throw IOException("Could not read from file \"%s\": %s", handle.path, strerror(errno)); @@ -281,7 +281,7 @@ int64_t LocalFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes } void LocalFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes, idx_t location) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; int64_t bytes_written = pwrite(fd, buffer, nr_bytes, location); if (bytes_written == -1) { throw IOException("Could not write file \"%s\": %s", handle.path, strerror(errno)); @@ -293,7 +293,7 @@ void LocalFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes, } int64_t LocalFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; int64_t bytes_written = write(fd, buffer, nr_bytes); if (bytes_written == -1) { throw IOException("Could not write file \"%s\": %s", handle.path, strerror(errno)); @@ -302,7 +302,7 @@ int64_t LocalFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_byte } int64_t LocalFileSystem::GetFileSize(FileHandle &handle) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; struct stat s; if (fstat(fd, &s) == -1) { return -1; @@ -311,7 +311,7 @@ int64_t LocalFileSystem::GetFileSize(FileHandle &handle) { } time_t LocalFileSystem::GetLastModifiedTime(FileHandle &handle) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; struct stat s; if (fstat(fd, &s) == -1) { return -1; @@ -320,12 +320,12 @@ time_t LocalFileSystem::GetLastModifiedTime(FileHandle &handle) { } FileType LocalFileSystem::GetFileType(FileHandle &handle) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; return GetFileTypeInternal(fd); } void LocalFileSystem::Truncate(FileHandle &handle, int64_t new_size) { - int fd = ((UnixFileHandle &)handle).fd; + int fd = handle.Cast().fd; if (ftruncate(fd, new_size) != 0) { throw IOException("Could not truncate file \"%s\": %s", handle.path, strerror(errno)); } @@ -444,7 +444,7 @@ bool LocalFileSystem::ListFiles(const string &directory, const std::function().fd; if (fsync(fd) != 0) { throw FatalException("fsync failed!"); } diff --git a/src/common/pipe_file_system.cpp b/src/common/pipe_file_system.cpp index 200e36847f97..39a1877dd9bf 100644 --- a/src/common/pipe_file_system.cpp +++ b/src/common/pipe_file_system.cpp @@ -33,12 +33,12 @@ void PipeFileSystem::Reset(FileHandle &handle) { } int64_t PipeFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_bytes) { - auto &pipe = (PipeFile &)handle; + auto &pipe = handle.Cast(); return pipe.ReadChunk(buffer, nr_bytes); } int64_t PipeFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes) { - auto &pipe = (PipeFile &)handle; + auto &pipe = handle.Cast(); return pipe.WriteChunk(buffer, nr_bytes); } diff --git a/src/core_functions/scalar/list/list_aggregates.cpp b/src/core_functions/scalar/list/list_aggregates.cpp index cf6526673c73..8600dc609181 100644 --- a/src/core_functions/scalar/list/list_aggregates.cpp +++ b/src/core_functions/scalar/list/list_aggregates.cpp @@ -175,7 +175,7 @@ static void ListAggregatesFunction(DataChunk &args, ExpressionState &state, Vect UnifiedVectorFormat lists_data; lists.ToUnifiedFormat(count, lists_data); - auto list_entries = (list_entry_t *)lists_data.data; + auto list_entries = UnifiedVectorFormat::GetData(lists_data); // state_buffer holds the state for each list of this chunk idx_t size = aggr.function.state_size(); diff --git a/src/function/aggregate/sorted_aggregate_function.cpp b/src/function/aggregate/sorted_aggregate_function.cpp index 4be4bc3ba521..d80c09adde62 100644 --- a/src/function/aggregate/sorted_aggregate_function.cpp +++ b/src/function/aggregate/sorted_aggregate_function.cpp @@ -370,7 +370,7 @@ struct SortedAggregateFunction { // Reusable inner state vector agg_state(order_bind.function.state_size()); - Vector agg_state_vec(Value::POINTER((idx_t)agg_state.data())); + Vector agg_state_vec(Value::POINTER(CastPointerToValue(agg_state.data()))); // State variables auto bind_info = order_bind.bind_info.get(); diff --git a/src/function/cast/cast_function_set.cpp b/src/function/cast/cast_function_set.cpp index b470dde8efa2..7608736c3d8a 100644 --- a/src/function/cast/cast_function_set.cpp +++ b/src/function/cast/cast_function_set.cpp @@ -158,7 +158,7 @@ int64_t CastFunctionSet::ImplicitCastCost(const LogicalType &source, const Logic BoundCastInfo MapCastFunction(BindCastInput &input, const LogicalType &source, const LogicalType &target) { D_ASSERT(input.info); - auto &map_info = (MapCastInfo &)*input.info; + auto &map_info = input.info->Cast(); auto entry = map_info.GetEntry(source, target); if (entry) { if (entry->bind_function) { diff --git a/src/function/scalar/operators/arithmetic.cpp b/src/function/scalar/operators/arithmetic.cpp index 5782ee1fae80..62cf2e6217d7 100644 --- a/src/function/scalar/operators/arithmetic.cpp +++ b/src/function/scalar/operators/arithmetic.cpp @@ -249,9 +249,8 @@ unique_ptr BindDecimalAddSubtract(ClientContext &context, ScalarFu static void SerializeDecimalArithmetic(FieldWriter &writer, const FunctionData *bind_data_p, const ScalarFunction &function) { - D_ASSERT(bind_data_p); - auto bind_data = (DecimalArithmeticBindData *)bind_data_p; - writer.WriteField(bind_data->check_overflow); + auto &bind_data = bind_data_p->Cast(); + writer.WriteField(bind_data.check_overflow); writer.WriteSerializable(function.return_type); writer.WriteRegularSerializableList(function.arguments); } diff --git a/src/function/scalar/string/contains.cpp b/src/function/scalar/string/contains.cpp index 0bb170982277..5ae12ed5b8b3 100644 --- a/src/function/scalar/string/contains.cpp +++ b/src/function/scalar/string/contains.cpp @@ -106,9 +106,9 @@ idx_t ContainsFun::Find(const unsigned char *haystack, idx_t haystack_size, cons if (location == nullptr) { return DConstants::INVALID_INDEX; } - idx_t base_offset = (const unsigned char *)location - haystack; + idx_t base_offset = data_ptr_cast(location) - haystack; haystack_size -= base_offset; - haystack = (const unsigned char *)location; + haystack = data_ptr_cast(location); // switch algorithm depending on needle size switch (needle_size) { case 1: @@ -133,9 +133,9 @@ idx_t ContainsFun::Find(const unsigned char *haystack, idx_t haystack_size, cons } idx_t ContainsFun::Find(const string_t &haystack_s, const string_t &needle_s) { - auto haystack = (const unsigned char *)haystack_s.GetData(); + auto haystack = data_ptr_cast(haystack_s.GetData()); auto haystack_size = haystack_s.GetSize(); - auto needle = (const unsigned char *)needle_s.GetData(); + auto needle = data_ptr_cast(needle_s.GetData()); auto needle_size = needle_s.GetSize(); if (needle_size == 0) { // empty needle: always true diff --git a/src/function/scalar/string/like.cpp b/src/function/scalar/string/like.cpp index bb35ad8fd574..a5be28fe1692 100644 --- a/src/function/scalar/string/like.cpp +++ b/src/function/scalar/string/like.cpp @@ -78,7 +78,7 @@ struct LikeMatcher : public FunctionData { } bool Match(string_t &str) { - auto str_data = (const unsigned char *)str.GetData(); + auto str_data = data_ptr_cast(str.GetData()); auto str_len = str.GetSize(); idx_t segment_idx = 0; idx_t end_idx = segments.size() - 1; @@ -105,7 +105,7 @@ struct LikeMatcher : public FunctionData { for (; segment_idx < end_idx; segment_idx++) { auto &segment = segments[segment_idx]; // find the pattern of the current segment - idx_t next_offset = ContainsFun::Find(str_data, str_len, (const unsigned char *)segment.pattern.c_str(), + idx_t next_offset = ContainsFun::Find(str_data, str_len, data_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); if (next_offset == DConstants::INVALID_INDEX) { // could not find this pattern in the string: no match @@ -130,7 +130,7 @@ struct LikeMatcher : public FunctionData { } else { auto &segment = segments.back(); // find the pattern of the current segment - idx_t next_offset = ContainsFun::Find(str_data, str_len, (const unsigned char *)segment.pattern.c_str(), + idx_t next_offset = ContainsFun::Find(str_data, str_len, data_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); return next_offset != DConstants::INVALID_INDEX; } @@ -179,7 +179,7 @@ struct LikeMatcher : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const LikeMatcher &)other_p; + auto &other = other_p.Cast(); return like_pattern == other.like_pattern; } @@ -491,7 +491,7 @@ template static void RegularLikeFunction(DataChunk &input, ExpressionState &state, Vector &result) { auto &func_expr = state.expr.Cast(); if (func_expr.bind_info) { - auto &matcher = (LikeMatcher &)*func_expr.bind_info; + auto &matcher = func_expr.bind_info->Cast(); // use fast like matcher UnaryExecutor::Execute(input.data[0], result, input.size(), [&](string_t input) { return INVERT ? !matcher.Match(input) : matcher.Match(input); diff --git a/src/function/scalar/string/regexp.cpp b/src/function/scalar/string/regexp.cpp index 9d2ad2b59901..d14ac7ac59fb 100644 --- a/src/function/scalar/string/regexp.cpp +++ b/src/function/scalar/string/regexp.cpp @@ -146,7 +146,7 @@ unique_ptr RegexpReplaceBindData::Copy() const { } bool RegexpReplaceBindData::Equals(const FunctionData &other_p) const { - auto &other = (const RegexpReplaceBindData &)other_p; + auto &other = other_p.Cast(); return RegexpBaseBindData::Equals(other) && global_replace == other.global_replace; } @@ -214,7 +214,7 @@ unique_ptr RegexpExtractBindData::Copy() const { } bool RegexpExtractBindData::Equals(const FunctionData &other_p) const { - auto &other = (const RegexpExtractBindData &)other_p; + auto &other = other_p.Cast(); return RegexpBaseBindData::Equals(other) && group_string == other.group_string; } @@ -286,7 +286,7 @@ static void RegexExtractStructFunction(DataChunk &args, ExpressionState &state, input.ToUnifiedFormat(count, iunified); const auto &ivalidity = iunified.validity; - auto idata = (const string_t *)iunified.data; + auto idata = UnifiedVectorFormat::GetData(iunified); // Start with a valid flat vector result.SetVectorType(VectorType::FLAT_VECTOR); diff --git a/src/function/scalar/string/regexp/regexp_extract_all.cpp b/src/function/scalar/string/regexp/regexp_extract_all.cpp index c8c4a7986b50..9aec5dc7c1bc 100644 --- a/src/function/scalar/string/regexp/regexp_extract_all.cpp +++ b/src/function/scalar/string/regexp/regexp_extract_all.cpp @@ -119,7 +119,7 @@ int32_t GetGroupIndex(DataChunk &args, idx_t row, int32_t &result) { if (!format.validity.RowIsValid(index)) { return false; } - result = ((int32_t *)format.data)[index]; + result = UnifiedVectorFormat::GetData(format)[index]; return true; } @@ -188,7 +188,7 @@ void RegexpExtractAll::Execute(DataChunk &args, ExpressionState &state, Vector & if (!pattern_data.validity.RowIsValid(pattern_idx)) { pattern_valid = false; } else { - auto &pattern_p = ((string_t *)pattern_data.data)[pattern_idx]; + auto &pattern_p = UnifiedVectorFormat::GetData(pattern_data)[pattern_idx]; auto pattern_strpiece = CreateStringPiece(pattern_p); stored_re = make_uniq(pattern_strpiece, info.options); @@ -216,7 +216,7 @@ void RegexpExtractAll::Execute(DataChunk &args, ExpressionState &state, Vector & auto &re = GetPattern(info, state, stored_re); auto &groups = GetGroupsBuffer(info, state, non_const_args); - auto &string = ((string_t *)strings_data.data)[string_idx]; + auto &string = UnifiedVectorFormat::GetData(strings_data)[string_idx]; ExtractSingleTuple(string, re, group_index, groups, result, row); } diff --git a/src/function/scalar/struct/struct_extract.cpp b/src/function/scalar/struct/struct_extract.cpp index 49e39435d016..f94982754a0c 100644 --- a/src/function/scalar/struct/struct_extract.cpp +++ b/src/function/scalar/struct/struct_extract.cpp @@ -21,7 +21,7 @@ struct StructExtractBindData : public FunctionData { return make_uniq(key, index, type); } bool Equals(const FunctionData &other_p) const override { - auto &other = (const StructExtractBindData &)other_p; + auto &other = other_p.Cast(); return key == other.key && index == other.index && type == other.type; } }; diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index 72fd01201b77..22e7822e26a2 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -42,8 +42,8 @@ struct CombineState : public FunctionLocalState { explicit CombineState(idx_t state_size_p) : state_size(state_size_p), state_buffer0(make_unsafe_uniq_array(state_size_p)), state_buffer1(make_unsafe_uniq_array(state_size_p)), - state_vector0(Value::POINTER((uintptr_t)state_buffer0.get())), - state_vector1(Value::POINTER((uintptr_t)state_buffer1.get())) { + state_vector0(Value::POINTER(CastPointerToValue(state_buffer0.get()))), + state_vector1(Value::POINTER(CastPointerToValue(state_buffer1.get()))) { } }; @@ -86,8 +86,8 @@ static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, V input.data[0].ToUnifiedFormat(input.size(), state_data); for (idx_t i = 0; i < input.size(); i++) { auto state_idx = state_data.sel->get_index(i); - auto state_entry = &(UnifiedVectorFormat::GetData(state_data))[state_idx]; - auto target_ptr = (const char *)local_state.state_buffer.get() + aligned_state_size * i; + auto state_entry = UnifiedVectorFormat::GetData(state_data) + state_idx; + auto target_ptr = data_ptr_cast(local_state.state_buffer.get()) + aligned_state_size * i; if (state_data.validity.RowIsValid(state_idx)) { D_ASSERT(state_entry->GetSize() == bind_data.state_size); @@ -136,8 +136,8 @@ static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Ve auto state0_idx = state0_data.sel->get_index(i); auto state1_idx = state1_data.sel->get_index(i); - auto &state0 = ((string_t *)state0_data.data)[state0_idx]; - auto &state1 = ((string_t *)state1_data.data)[state1_idx]; + auto &state0 = UnifiedVectorFormat::GetData(state0_data)[state0_idx]; + auto &state1 = UnifiedVectorFormat::GetData(state1_data)[state1_idx]; // if both are NULL, we return NULL. If either of them is not, the result is that one if (!state0_data.validity.RowIsValid(state0_idx) && !state1_data.validity.RowIsValid(state1_idx)) { @@ -250,7 +250,7 @@ static void ExportAggregateFinalize(Vector &state, AggregateInputData &aggr_inpu auto addresses_ptr = FlatVector::GetData(state); for (idx_t row_idx = 0; row_idx < count; row_idx++) { auto data_ptr = addresses_ptr[row_idx]; - blob_ptr[row_idx] = StringVector::AddStringOrBlob(result, (const char *)data_ptr, state_size); + blob_ptr[row_idx] = StringVector::AddStringOrBlob(result, data_ptr_cast(data_ptr), state_size); } } diff --git a/src/function/scalar_function.cpp b/src/function/scalar_function.cpp index 8be41b442273..0bfeba7d81e5 100644 --- a/src/function/scalar_function.cpp +++ b/src/function/scalar_function.cpp @@ -70,7 +70,7 @@ bool ScalarFunction::CompareScalarFunctionT(const scalar_function_t &other) cons // scalar_function_t (std::functions) from lambdas cannot be compared return false; } - return ((size_t)*func_ptr == (size_t)*other_ptr); + return CastPointerToValue(*func_ptr) == CastPointerToValue(*other_ptr); } void ScalarFunction::NopFunction(DataChunk &input, ExpressionState &state, Vector &result) { diff --git a/src/function/table/arrow.cpp b/src/function/table/arrow.cpp index 122bb216126d..41ca656755d0 100644 --- a/src/function/table/arrow.cpp +++ b/src/function/table/arrow.cpp @@ -193,8 +193,8 @@ void ArrowTableFunction::RenameArrowColumns(vector &names) { unique_ptr ArrowTableFunction::ArrowScanBind(ClientContext &context, TableFunctionBindInput &input, vector &return_types, vector &names) { auto stream_factory_ptr = input.inputs[0].GetPointer(); - auto stream_factory_produce = (stream_factory_produce_t)input.inputs[1].GetPointer(); - auto stream_factory_get_schema = (stream_factory_get_schema_t)input.inputs[2].GetPointer(); + auto stream_factory_produce = (stream_factory_produce_t)input.inputs[1].GetPointer(); // NOLINT + auto stream_factory_get_schema = (stream_factory_get_schema_t)input.inputs[2].GetPointer(); // NOLINT auto res = make_uniq(stream_factory_produce, stream_factory_ptr); @@ -269,7 +269,7 @@ bool ArrowTableFunction::ArrowScanParallelStateNext(ClientContext &context, cons unique_ptr ArrowTableFunction::ArrowScanInitGlobal(ClientContext &context, TableFunctionInitInput &input) { - auto &bind_data = (const ArrowScanFunctionData &)*input.bind_data; + auto &bind_data = input.bind_data->Cast(); auto result = make_uniq(); result->stream = ProduceArrowScan(bind_data, input.column_ids, input.filters.get()); result->max_threads = ArrowScanMaxThreads(context, input.bind_data.get()); @@ -314,7 +314,7 @@ void ArrowTableFunction::ArrowScanFunction(ClientContext &context, TableFunction if (!data_p.local_state) { return; } - auto &data = (ArrowScanFunctionData &)*data_p.bind_data; + auto &data = data_p.bind_data->CastNoConst(); // FIXME auto &state = data_p.local_state->Cast(); auto &global_state = data_p.global_state->Cast(); diff --git a/src/function/table/arrow_conversion.cpp b/src/function/table/arrow_conversion.cpp index 4eadcefc78d1..39fb0c6883db 100644 --- a/src/function/table/arrow_conversion.cpp +++ b/src/function/table/arrow_conversion.cpp @@ -28,6 +28,11 @@ static void ShiftRight(unsigned char *ar, int size, int shift) { } } +template +T *ArrowBufferData(ArrowArray &array, idx_t buffer_idx) { + return (T *) array.buffers[buffer_idx]; // NOLINT +} + static void GetValidityMask(ValidityMask &mask, ArrowArray &array, ArrowScanLocalState &scan_state, idx_t size, int64_t nested_offset = -1, bool add_null = false) { // In certains we don't need to or cannot copy arrow's validity mask to duckdb. @@ -46,18 +51,18 @@ static void GetValidityMask(ValidityMask &mask, ArrowArray &array, ArrowScanLoca auto n_bitmask_bytes = (size + 8 - 1) / 8; if (bit_offset % 8 == 0) { //! just memcpy nullmask - memcpy((void *)mask.GetData(), (uint8_t *)array.buffers[0] + bit_offset / 8, n_bitmask_bytes); + memcpy((void *)mask.GetData(), ArrowBufferData(array, 0) + bit_offset / 8, n_bitmask_bytes); } else { //! need to re-align nullmask vector temp_nullmask(n_bitmask_bytes + 1); - memcpy(temp_nullmask.data(), (uint8_t *)array.buffers[0] + bit_offset / 8, n_bitmask_bytes + 1); + memcpy(temp_nullmask.data(), ArrowBufferData(array, 0) + bit_offset / 8, n_bitmask_bytes + 1); ShiftRight(temp_nullmask.data(), n_bitmask_bytes + 1, bit_offset % 8); //! why this has to be a right shift is a mystery to me - memcpy((void *)mask.GetData(), (data_ptr_t)temp_nullmask.data(), n_bitmask_bytes); + memcpy((void *)mask.GetData(), data_ptr_cast(temp_nullmask.data()), n_bitmask_bytes); } #else auto byte_offset = bit_offset / 8; - auto source_data = (uint8_t *)array.buffers[0]; + auto source_data = ArrowBufferData(array, 0); bit_offset %= 8; for (idx_t i = 0; i < size; i++) { mask.Set(i, source_data[byte_offset] & (1 << bit_offset)); @@ -115,9 +120,9 @@ static void ArrowToDuckDBList(Vector &vector, ArrowArray &array, ArrowScanLocalS } list_size = start_offset + cur_offset; } else if (original_type.first == ArrowVariableSizeType::NORMAL) { - auto offsets = (uint32_t *)array.buffers[1] + array.offset + scan_state.chunk_offset; + auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; if (nested_offset != -1) { - offsets = (uint32_t *)array.buffers[1] + nested_offset; + offsets = ArrowBufferData(array, 1) + nested_offset; } start_offset = offsets[0]; auto list_data = FlatVector::GetData(vector); @@ -129,9 +134,9 @@ static void ArrowToDuckDBList(Vector &vector, ArrowArray &array, ArrowScanLocalS } list_size = offsets[size]; } else { - auto offsets = (uint64_t *)array.buffers[1] + array.offset + scan_state.chunk_offset; + auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; if (nested_offset != -1) { - offsets = (uint64_t *)array.buffers[1] + nested_offset; + offsets = ArrowBufferData(array, 1) + nested_offset; } start_offset = offsets[0]; auto list_data = FlatVector::GetData(vector); @@ -179,7 +184,7 @@ static void ArrowToDuckDBBlob(Vector &vector, ArrowArray &array, ArrowScanLocalS if (nested_offset != -1) { offset = original_type.second * nested_offset; } - auto cdata = (char *)array.buffers[1]; + auto cdata = ArrowBufferData(array, 1); for (idx_t row_idx = 0; row_idx < size; row_idx++) { if (FlatVector::IsNull(vector, row_idx)) { continue; @@ -190,11 +195,11 @@ static void ArrowToDuckDBBlob(Vector &vector, ArrowArray &array, ArrowScanLocalS offset += blob_len; } } else if (original_type.first == ArrowVariableSizeType::NORMAL) { - auto offsets = (uint32_t *)array.buffers[1] + array.offset + scan_state.chunk_offset; + auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; if (nested_offset != -1) { - offsets = (uint32_t *)array.buffers[1] + array.offset + nested_offset; + offsets = ArrowBufferData(array, 1) + array.offset + nested_offset; } - auto cdata = (char *)array.buffers[2]; + auto cdata = ArrowBufferData(array, 2); for (idx_t row_idx = 0; row_idx < size; row_idx++) { if (FlatVector::IsNull(vector, row_idx)) { continue; @@ -208,11 +213,11 @@ static void ArrowToDuckDBBlob(Vector &vector, ArrowArray &array, ArrowScanLocalS if (((uint64_t *)array.buffers[1])[array.length] > NumericLimits::Maximum()) { // LCOV_EXCL_START throw ConversionException("DuckDB does not support Blobs over 4GB"); } // LCOV_EXCL_STOP - auto offsets = (uint64_t *)array.buffers[1] + array.offset + scan_state.chunk_offset; + auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; if (nested_offset != -1) { - offsets = (uint64_t *)array.buffers[1] + array.offset + nested_offset; + offsets = ArrowBufferData(array, 1) + array.offset + nested_offset; } - auto cdata = (char *)array.buffers[2]; + auto cdata = ArrowBufferData(array, 2); for (idx_t row_idx = 0; row_idx < size; row_idx++) { if (FlatVector::IsNull(vector, row_idx)) { continue; @@ -263,9 +268,9 @@ static void SetVectorString(Vector &vector, idx_t size, char *cdata, T *offsets) static void DirectConversion(Vector &vector, ArrowArray &array, ArrowScanLocalState &scan_state, int64_t nested_offset) { auto internal_type = GetTypeIdSize(vector.GetType().InternalType()); - auto data_ptr = (data_ptr_t)array.buffers[1] + internal_type * (scan_state.chunk_offset + array.offset); + auto data_ptr = ArrowBufferData(array, 1) + internal_type * (scan_state.chunk_offset + array.offset); if (nested_offset != -1) { - data_ptr = (data_ptr_t)array.buffers[1] + internal_type * (array.offset + nested_offset); + data_ptr = ArrowBufferData(array, 1) + internal_type * (array.offset + nested_offset); } FlatVector::SetData(vector, data_ptr); } @@ -273,7 +278,7 @@ static void DirectConversion(Vector &vector, ArrowArray &array, ArrowScanLocalSt template static void TimeConversion(Vector &vector, ArrowArray &array, ArrowScanLocalState &scan_state, int64_t nested_offset, idx_t size, int64_t conversion) { - auto tgt_ptr = (dtime_t *)FlatVector::GetData(vector); + auto tgt_ptr = FlatVector::GetData(vector); auto &validity_mask = FlatVector::Validity(vector); auto src_ptr = (T *)array.buffers[1] + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { @@ -291,11 +296,11 @@ static void TimeConversion(Vector &vector, ArrowArray &array, ArrowScanLocalStat static void TimestampTZConversion(Vector &vector, ArrowArray &array, ArrowScanLocalState &scan_state, int64_t nested_offset, idx_t size, int64_t conversion) { - auto tgt_ptr = (timestamp_t *)FlatVector::GetData(vector); + auto tgt_ptr = FlatVector::GetData(vector); auto &validity_mask = FlatVector::Validity(vector); - auto src_ptr = (int64_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (int64_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { if (!validity_mask.RowIsValid(row)) { @@ -309,10 +314,10 @@ static void TimestampTZConversion(Vector &vector, ArrowArray &array, ArrowScanLo static void IntervalConversionUs(Vector &vector, ArrowArray &array, ArrowScanLocalState &scan_state, int64_t nested_offset, idx_t size, int64_t conversion) { - auto tgt_ptr = (interval_t *)FlatVector::GetData(vector); - auto src_ptr = (int64_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto tgt_ptr = FlatVector::GetData(vector); + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (int64_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { tgt_ptr[row].days = 0; @@ -325,10 +330,10 @@ static void IntervalConversionUs(Vector &vector, ArrowArray &array, ArrowScanLoc static void IntervalConversionMonths(Vector &vector, ArrowArray &array, ArrowScanLocalState &scan_state, int64_t nested_offset, idx_t size) { - auto tgt_ptr = (interval_t *)FlatVector::GetData(vector); - auto src_ptr = (int32_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto tgt_ptr = FlatVector::GetData(vector); + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (int32_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { tgt_ptr[row].days = 0; @@ -339,10 +344,10 @@ static void IntervalConversionMonths(Vector &vector, ArrowArray &array, ArrowSca static void IntervalConversionMonthDayNanos(Vector &vector, ArrowArray &array, ArrowScanLocalState &scan_state, int64_t nested_offset, idx_t size) { - auto tgt_ptr = (interval_t *)FlatVector::GetData(vector); - auto src_ptr = (ArrowInterval *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto tgt_ptr = FlatVector::GetData(vector); + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (ArrowInterval *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { tgt_ptr[row].days = src_ptr[row].days; @@ -362,10 +367,10 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca case LogicalTypeId::BOOLEAN: { //! Arrow bit-packs boolean values //! Lets first figure out where we are in the source array - auto src_ptr = (uint8_t *)array.buffers[1] + (scan_state.chunk_offset + array.offset) / 8; + auto src_ptr = ArrowBufferData(array, 1) + (scan_state.chunk_offset + array.offset) / 8; if (nested_offset != -1) { - src_ptr = (uint8_t *)array.buffers[1] + (nested_offset + array.offset) / 8; + src_ptr = ArrowBufferData(array, 1) + (nested_offset + array.offset) / 8; } auto tgt_ptr = (uint8_t *)FlatVector::GetData(vector); int src_pos = 0; @@ -407,17 +412,17 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca } case LogicalTypeId::VARCHAR: { auto original_type = arrow_convert_data[col_idx]->variable_sz_type[arrow_convert_idx.variable_sized_index++]; - auto cdata = (char *)array.buffers[2]; + auto cdata = ArrowBufferData(array, 2); if (original_type.first == ArrowVariableSizeType::SUPER_SIZE) { - auto offsets = (uint64_t *)array.buffers[1] + array.offset + scan_state.chunk_offset; + auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; if (nested_offset != -1) { - offsets = (uint64_t *)array.buffers[1] + array.offset + nested_offset; + offsets = ArrowBufferData(array, 1) + array.offset + nested_offset; } SetVectorString(vector, size, cdata, offsets); } else { - auto offsets = (uint32_t *)array.buffers[1] + array.offset + scan_state.chunk_offset; + auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; if (nested_offset != -1) { - offsets = (uint32_t *)array.buffers[1] + array.offset + nested_offset; + offsets = ArrowBufferData(array, 1) + array.offset + nested_offset; } SetVectorString(vector, size, cdata, offsets); } @@ -554,13 +559,13 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca case LogicalTypeId::DECIMAL: { auto val_mask = FlatVector::Validity(vector); //! We have to convert from INT128 - auto src_ptr = (hugeint_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (hugeint_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } switch (vector.GetType().InternalType()) { case PhysicalType::INT16: { - auto tgt_ptr = (int16_t *)FlatVector::GetData(vector); + auto tgt_ptr = FlatVector::GetData(vector); for (idx_t row = 0; row < size; row++) { if (val_mask.RowIsValid(row)) { auto result = Hugeint::TryCast(src_ptr[row], tgt_ptr[row]); @@ -571,7 +576,7 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case PhysicalType::INT32: { - auto tgt_ptr = (int32_t *)FlatVector::GetData(vector); + auto tgt_ptr = FlatVector::GetData(vector); for (idx_t row = 0; row < size; row++) { if (val_mask.RowIsValid(row)) { auto result = Hugeint::TryCast(src_ptr[row], tgt_ptr[row]); @@ -582,7 +587,7 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case PhysicalType::INT64: { - auto tgt_ptr = (int64_t *)FlatVector::GetData(vector); + auto tgt_ptr = FlatVector::GetData(vector); for (idx_t row = 0; row < size; row++) { if (val_mask.RowIsValid(row)) { auto result = Hugeint::TryCast(src_ptr[row], tgt_ptr[row]); @@ -593,7 +598,7 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case PhysicalType::INT128: { - FlatVector::SetData(vector, (data_ptr_t)array.buffers[1] + GetTypeIdSize(vector.GetType().InternalType()) * + FlatVector::SetData(vector, ArrowBufferData(array, 1) + GetTypeIdSize(vector.GetType().InternalType()) * (scan_state.chunk_offset + array.offset)); break; } diff --git a/src/function/table/checkpoint.cpp b/src/function/table/checkpoint.cpp index 5d288c5ca690..f89a93077f16 100644 --- a/src/function/table/checkpoint.cpp +++ b/src/function/table/checkpoint.cpp @@ -19,7 +19,7 @@ struct CheckpointBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const CheckpointBindData &)other_p; + auto &other = other_p.Cast(); return db == other.db; } }; diff --git a/src/function/table/copy_csv.cpp b/src/function/table/copy_csv.cpp index 7988f534674b..2724bf2d2bb1 100644 --- a/src/function/table/copy_csv.cpp +++ b/src/function/table/copy_csv.cpp @@ -187,13 +187,13 @@ static bool RequiresQuotes(WriteCSVData &csv_data, const char *str, idx_t len) { // check for delimiter if (options.delimiter.length() != 0 && - ContainsFun::Find((const unsigned char *)str, len, (const unsigned char *)options.delimiter.c_str(), + ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.delimiter.c_str()), options.delimiter.size()) != DConstants::INVALID_INDEX) { return true; } // check for quote if (options.quote.length() != 0 && - ContainsFun::Find((const unsigned char *)str, len, (const unsigned char *)options.quote.c_str(), + ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.quote.c_str()), options.quote.size()) != DConstants::INVALID_INDEX) { return true; } @@ -224,11 +224,11 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co // complex CSV // check for quote or escape separately if (options.quote.length() != 0 && - ContainsFun::Find((const unsigned char *)str, len, (const unsigned char *)options.quote.c_str(), + ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.quote.c_str()), options.quote.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } else if (options.escape.length() != 0 && - ContainsFun::Find((const unsigned char *)str, len, (const unsigned char *)options.escape.c_str(), + ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.escape.c_str()), options.escape.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } @@ -236,7 +236,7 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co if (!requires_escape) { // fast path: no need to escape anything serializer.WriteBufferData(options.quote); - serializer.WriteData((const_data_ptr_t)str, len); + serializer.WriteData(data_ptr_cast(str), len); serializer.WriteBufferData(options.quote); return; } @@ -252,7 +252,7 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co serializer.WriteBufferData(new_val); serializer.WriteBufferData(options.quote); } else { - serializer.WriteData((const_data_ptr_t)str, len); + serializer.WriteData(data_ptr_cast(str), len); } } diff --git a/src/function/table/glob.cpp b/src/function/table/glob.cpp index 31f9c8b23ee5..84dbc688ce1e 100644 --- a/src/function/table/glob.cpp +++ b/src/function/table/glob.cpp @@ -33,7 +33,7 @@ static unique_ptr GlobFunctionInit(ClientContext &cont static void GlobFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { auto &bind_data = data_p.bind_data->Cast(); - auto &state = (GlobFunctionState &)*data_p.global_state; + auto &state = data_p.global_state->Cast(); idx_t count = 0; idx_t next_idx = MinValue(state.current_idx + STANDARD_VECTOR_SIZE, bind_data.files.size()); diff --git a/src/function/table/pragma_last_profiling_output.cpp b/src/function/table/pragma_last_profiling_output.cpp index dbc296380459..c822f3ddb653 100644 --- a/src/function/table/pragma_last_profiling_output.cpp +++ b/src/function/table/pragma_last_profiling_output.cpp @@ -62,7 +62,7 @@ unique_ptr PragmaLastProfilingOutputInit(ClientContext static void PragmaLastProfilingOutputFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { auto &state = data_p.global_state->Cast(); - auto &data = (PragmaLastProfilingOutputData &)*data_p.bind_data; + auto &data = data_p.bind_data->CastNoConst(); if (!state.initialized) { // create a ColumnDataCollection auto collection = make_uniq(context, data.types); diff --git a/src/function/table/range.cpp b/src/function/table/range.cpp index fc1224aff129..205ee3ac6aaa 100644 --- a/src/function/table/range.cpp +++ b/src/function/table/range.cpp @@ -18,7 +18,7 @@ struct RangeFunctionBindData : public TableFunctionData { public: bool Equals(const FunctionData &other_p) const override { - auto &other = (const RangeFunctionBindData &)other_p; + auto &other = other_p.Cast(); return other.start == start && other.end == end && other.increment == increment; } }; @@ -92,7 +92,7 @@ static unique_ptr RangeFunctionInit(ClientContext &con static void RangeFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { auto &bind_data = data_p.bind_data->Cast(); - auto &state = (RangeFunctionState &)*data_p.global_state; + auto &state = data_p.global_state->Cast(); auto increment = bind_data.increment; auto end = bind_data.end; @@ -129,7 +129,7 @@ struct RangeDateTimeBindData : public TableFunctionData { public: bool Equals(const FunctionData &other_p) const override { - auto &other = (const RangeDateTimeBindData &)other_p; + auto &other = other_p.Cast(); return other.start == start && other.end == end && other.increment == increment && other.inclusive_bound == inclusive_bound && other.greater_than_check == greater_than_check; } @@ -213,7 +213,7 @@ static unique_ptr RangeDateTimeInit(ClientContext &con static void RangeDateTimeFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { auto &bind_data = data_p.bind_data->Cast(); - auto &state = (RangeDateTimeState &)*data_p.global_state; + auto &state = data_p.global_state->Cast(); if (state.finished) { return; } diff --git a/src/function/table/read_csv.cpp b/src/function/table/read_csv.cpp index ff4b52643e4e..ae6547edef62 100644 --- a/src/function/table/read_csv.cpp +++ b/src/function/table/read_csv.cpp @@ -864,16 +864,16 @@ static unique_ptr SingleThreadedCSVInit(ClientContext unique_ptr SingleThreadedReadCSVInitLocal(ExecutionContext &context, TableFunctionInitInput &input, GlobalTableFunctionState *global_state_p) { - auto &bind_data = (ReadCSVData &)*input.bind_data; - auto &data = (SingleThreadedCSVState &)*global_state_p; + auto &bind_data = input.bind_data->CastNoConst(); + auto &data = global_state_p->Cast(); auto result = make_uniq(); result->csv_reader = data.GetCSVReader(context.client, bind_data, result->file_index, result->total_size); return std::move(result); } static void SingleThreadedCSVFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &bind_data = (ReadCSVData &)*data_p.bind_data; - auto &data = (SingleThreadedCSVState &)*data_p.global_state; + auto &bind_data = data_p.bind_data->CastNoConst(); + auto &data = data_p.global_state->Cast(); auto &lstate = data_p.local_state->Cast(); if (!lstate.csv_reader) { // no csv_reader was set, this can happen when a filename-based filter has filtered out all possible files diff --git a/src/function/table/repeat.cpp b/src/function/table/repeat.cpp index c45d960bddee..7ca1d082176e 100644 --- a/src/function/table/repeat.cpp +++ b/src/function/table/repeat.cpp @@ -31,7 +31,7 @@ static unique_ptr RepeatInit(ClientContext &context, T } static void RepeatFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &bind_data = (const RepeatFunctionData &)*data_p.bind_data; + auto &bind_data = data_p.bind_data->Cast(); auto &state = data_p.global_state->Cast(); idx_t remaining = MinValue(bind_data.target_count - state.current_count, STANDARD_VECTOR_SIZE); @@ -41,7 +41,7 @@ static void RepeatFunction(ClientContext &context, TableFunctionInput &data_p, D } static unique_ptr RepeatCardinality(ClientContext &context, const FunctionData *bind_data_p) { - auto &bind_data = (const RepeatFunctionData &)*bind_data_p; + auto &bind_data = bind_data_p->Cast(); return make_uniq(bind_data.target_count, bind_data.target_count); } diff --git a/src/function/table/repeat_row.cpp b/src/function/table/repeat_row.cpp index e98640ae7714..2a49f1825095 100644 --- a/src/function/table/repeat_row.cpp +++ b/src/function/table/repeat_row.cpp @@ -33,7 +33,7 @@ static unique_ptr RepeatRowInit(ClientContext &context } static void RepeatRowFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &bind_data = (const RepeatRowFunctionData &)*data_p.bind_data; + auto &bind_data = data_p.bind_data->Cast(); auto &state = data_p.global_state->Cast(); idx_t remaining = MinValue(bind_data.target_count - state.current_count, STANDARD_VECTOR_SIZE); @@ -45,7 +45,7 @@ static void RepeatRowFunction(ClientContext &context, TableFunctionInput &data_p } static unique_ptr RepeatRowCardinality(ClientContext &context, const FunctionData *bind_data_p) { - auto &bind_data = (const RepeatRowFunctionData &)*bind_data_p; + auto &bind_data = bind_data_p->Cast(); return make_uniq(bind_data.target_count, bind_data.target_count); } diff --git a/src/function/table/system/duckdb_functions.cpp b/src/function/table/system/duckdb_functions.cpp index 8244640f52a0..2eb6499e768b 100644 --- a/src/function/table/system/duckdb_functions.cpp +++ b/src/function/table/system/duckdb_functions.cpp @@ -456,7 +456,7 @@ bool ExtractFunctionData(FunctionEntry &entry, idx_t function_idx, DataChunk &ou } void DuckDBFunctionsFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &data = (DuckDBFunctionsData &)*data_p.global_state; + auto &data = data_p.global_state->Cast(); if (data.offset >= data.entries.size()) { // finished returning values return; diff --git a/src/function/table/system/test_vector_types.cpp b/src/function/table/system/test_vector_types.cpp index ec0c37af6fc7..9b845eead1d1 100644 --- a/src/function/table/system/test_vector_types.cpp +++ b/src/function/table/system/test_vector_types.cpp @@ -233,7 +233,7 @@ unique_ptr TestVectorTypesInit(ClientContext &context, } void TestVectorTypesFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &data = (TestVectorTypesData &)*data_p.global_state; + auto &data = data_p.global_state->Cast(); if (data.offset >= data.entries.size()) { // finished returning values return; diff --git a/src/function/table/table_scan.cpp b/src/function/table/table_scan.cpp index ab8a5d04e60f..d146baf26fea 100644 --- a/src/function/table/table_scan.cpp +++ b/src/function/table/table_scan.cpp @@ -215,7 +215,7 @@ static unique_ptr IndexScanInitGlobal(ClientContext &c auto &bind_data = input.bind_data->Cast(); data_ptr_t row_id_data = nullptr; if (!bind_data.result_ids.empty()) { - row_id_data = (data_ptr_t)&bind_data.result_ids[0]; + row_id_data = (data_ptr_t)&bind_data.result_ids[0]; // NOLINT - this is not pretty } auto result = make_uniq(row_id_data); auto &local_storage = LocalStorage::Get(context, bind_data.table.catalog); diff --git a/src/function/table/unnest.cpp b/src/function/table/unnest.cpp index 0d53377044da..15f3950893bf 100644 --- a/src/function/table/unnest.cpp +++ b/src/function/table/unnest.cpp @@ -18,7 +18,7 @@ struct UnnestBindData : public FunctionData { } bool Equals(const FunctionData &other_p) const override { - auto &other = (const UnnestBindData &)other_p; + auto &other = other_p.Cast(); return input_type == other.input_type; } }; diff --git a/src/function/table/version/pragma_version.cpp b/src/function/table/version/pragma_version.cpp index 27dae6c35d31..10c7eea83710 100644 --- a/src/function/table/version/pragma_version.cpp +++ b/src/function/table/version/pragma_version.cpp @@ -26,7 +26,7 @@ static unique_ptr PragmaVersionInit(ClientContext &con } static void PragmaVersionFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &data = (PragmaVersionData &)*data_p.global_state; + auto &data = data_p.global_state->Cast(); if (data.finished) { // finished returning values return; diff --git a/src/include/duckdb/common/arrow/arrow_buffer.hpp b/src/include/duckdb/common/arrow/arrow_buffer.hpp index 770f5272a32d..2c39d8b9fed5 100644 --- a/src/include/duckdb/common/arrow/arrow_buffer.hpp +++ b/src/include/duckdb/common/arrow/arrow_buffer.hpp @@ -73,6 +73,12 @@ struct ArrowBuffer { return dataptr; } + template + T *GetData() { + return reinterpret_cast (data()); + } + + private: void ReserveInternal(idx_t bytes) { if (dataptr) { diff --git a/src/include/duckdb/common/file_system.hpp b/src/include/duckdb/common/file_system.hpp index 0f3133f06414..d410fe064760 100644 --- a/src/include/duckdb/common/file_system.hpp +++ b/src/include/duckdb/common/file_system.hpp @@ -77,6 +77,17 @@ struct FileHandle { return path; } + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } + public: FileSystem &file_system; string path; diff --git a/src/include/duckdb/common/typedefs.hpp b/src/include/duckdb/common/typedefs.hpp index 806b53c8589b..026cf9861277 100644 --- a/src/include/duckdb/common/typedefs.hpp +++ b/src/include/duckdb/common/typedefs.hpp @@ -40,4 +40,9 @@ const DEST *const_data_ptr_cast(SRC *src) { return (DEST *)src; } +template +uintptr_t CastPointerToValue(SRC *src) { + return uintptr_t(src); +} + } // namespace duckdb diff --git a/src/include/duckdb/function/cast/default_casts.hpp b/src/include/duckdb/function/cast/default_casts.hpp index 448c9c86f393..12f6b6ab0b56 100644 --- a/src/include/duckdb/function/cast/default_casts.hpp +++ b/src/include/duckdb/function/cast/default_casts.hpp @@ -22,6 +22,17 @@ struct FunctionLocalState; //! Extra data that can be attached to a bind function of a cast, and is available during binding struct BindCastInfo { DUCKDB_API virtual ~BindCastInfo(); + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; //! Extra data that can be returned by the bind of a cast, and is available during execution of a cast From 5fcb727c3beae645a5ba96226881e435b95096e1 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Mon, 22 May 2023 15:18:01 +0200 Subject: [PATCH 08/28] More UnifiedVectorFormat::GetData and other casts --- extension/icu/icu-datepart.cpp | 2 +- extension/inet/inet_functions.cpp | 2 +- extension/json/include/json_executors.hpp | 2 +- extension/json/json_functions/json_create.cpp | 12 ++-- .../json/json_functions/json_merge_patch.cpp | 2 +- .../json/json_functions/json_transform.cpp | 2 +- src/common/row_operations/row_scatter.cpp | 4 +- src/common/serializer/binary_deserializer.cpp | 2 +- src/common/serializer/binary_serializer.cpp | 6 +- src/common/types.cpp | 65 +++++++++++-------- src/common/types/bit.cpp | 8 +-- src/common/types/blob.cpp | 14 ++-- .../types/column/column_data_allocator.cpp | 2 +- .../types/column/column_data_collection.cpp | 2 +- src/common/types/row/tuple_data_allocator.cpp | 2 +- .../types/row/tuple_data_scatter_gather.cpp | 28 ++++---- .../vector_operations/is_distinct_from.cpp | 6 +- src/common/vector_operations/vector_hash.cpp | 2 +- .../aggregate/distributive/arg_min_max.cpp | 6 +- .../aggregate/distributive/bitagg.cpp | 8 +-- .../aggregate/distributive/bitstring_agg.cpp | 5 +- .../aggregate/distributive/bool.cpp | 6 +- .../aggregate/distributive/entropy.cpp | 6 +- .../aggregate/distributive/kurtosis.cpp | 4 +- .../aggregate/distributive/minmax.cpp | 10 +-- .../aggregate/distributive/product.cpp | 4 +- .../aggregate/distributive/skew.cpp | 4 +- .../aggregate/distributive/string_agg.cpp | 4 +- .../holistic/approximate_quantile.cpp | 4 +- .../aggregate/holistic/mode.cpp | 4 +- .../aggregate/holistic/quantile.cpp | 2 +- .../aggregate/holistic/reservoir_quantile.cpp | 4 +- .../aggregate/nested/histogram.cpp | 2 +- .../aggregate/regression/regr_avg.cpp | 8 +-- .../aggregate/regression/regr_intercept.cpp | 2 +- .../aggregate/regression/regr_r2.cpp | 2 +- .../aggregate/regression/regr_sxx_syy.cpp | 4 +- .../aggregate/regression/regr_sxy.cpp | 2 +- src/core_functions/scalar/date/date_part.cpp | 2 +- src/core_functions/scalar/generic/least.cpp | 4 +- .../scalar/list/array_slice.cpp | 6 +- .../operator/join/physical_asof_join.cpp | 2 +- src/function/aggregate/distributive/count.cpp | 4 +- src/function/aggregate/distributive/first.cpp | 8 +-- src/function/scalar/string/like.cpp | 8 +-- src/function/table/arrow.cpp | 2 +- src/function/table/arrow_conversion.cpp | 9 +-- src/function/table/copy_csv.cpp | 15 +++-- .../duckdb/common/arrow/arrow_buffer.hpp | 5 +- .../common/serializer/binary_serializer.hpp | 3 + src/include/duckdb/common/typedefs.hpp | 2 +- .../vector_operations/aggregate_executor.hpp | 39 +++++------ .../vector_operations/generic_executor.hpp | 22 +++---- .../vector_operations/ternary_executor.hpp | 15 +++-- .../aggregate/algebraic/corr.hpp | 2 +- .../aggregate/algebraic/covar.hpp | 2 +- .../aggregate/algebraic/stddev.hpp | 2 +- .../aggregate/regression/regr_count.hpp | 2 +- .../aggregate/regression/regr_slope.hpp | 2 +- .../core_functions/aggregate/sum_helpers.hpp | 4 +- .../scalar/list/contains_or_position.hpp | 6 +- .../compression/chimp/chimp_analyze.hpp | 2 +- .../compression/chimp/chimp_compress.hpp | 2 +- .../compression/patas/patas_analyze.hpp | 2 +- .../compression/patas/patas_compress.hpp | 2 +- .../duckdb/storage/string_uncompressed.hpp | 2 +- src/storage/statistics/list_stats.cpp | 2 +- src/storage/statistics/numeric_stats.cpp | 2 +- src/storage/statistics/string_stats.cpp | 2 +- .../udf_function/udf_functions_to_test.hpp | 12 ++-- tools/pythonpkg/src/numpy/array_wrapper.cpp | 6 +- 71 files changed, 238 insertions(+), 216 deletions(-) diff --git a/extension/icu/icu-datepart.cpp b/extension/icu/icu-datepart.cpp index 7cf3ede4765b..fbf2b7b5f8d5 100644 --- a/extension/icu/icu-datepart.cpp +++ b/extension/icu/icu-datepart.cpp @@ -329,7 +329,7 @@ struct ICUDatePart : public ICUDateFunc { input.ToUnifiedFormat(count, rdata); const auto &arg_valid = rdata.validity; - auto tdata = (const INPUT_TYPE *)rdata.data; + auto tdata = UnifiedVectorFormat::GetData(rdata); result.SetVectorType(VectorType::FLAT_VECTOR); auto &child_entries = StructVector::GetEntries(result); diff --git a/extension/inet/inet_functions.cpp b/extension/inet/inet_functions.cpp index 2aab20136ba9..007b8e1657be 100644 --- a/extension/inet/inet_functions.cpp +++ b/extension/inet/inet_functions.cpp @@ -21,7 +21,7 @@ bool INetFunctions::CastVarcharToINET(Vector &source, Vector &result, idx_t coun auto address_data = FlatVector::GetData(*entries[1]); auto mask_data = FlatVector::GetData(*entries[2]); - auto input = (string_t *)vdata.data; + auto input = UnifiedVectorFormat::GetData(vdata); bool success = true; for (idx_t i = 0; i < (constant ? 1 : count); i++) { auto idx = vdata.sel->get_index(i); diff --git a/extension/json/include/json_executors.hpp b/extension/json/include/json_executors.hpp index 09d196ea8dda..1922ad4b0ac7 100644 --- a/extension/json/include/json_executors.hpp +++ b/extension/json/include/json_executors.hpp @@ -89,7 +89,7 @@ struct JSONExecutors { UnifiedVectorFormat input_data; auto &input_vector = args.data[0]; input_vector.ToUnifiedFormat(count, input_data); - auto inputs = (string_t *)input_data.data; + auto inputs = UnifiedVectorFormat::GetData(input_data); ListVector::Reserve(result, list_size); auto list_entries = FlatVector::GetData(result); diff --git a/extension/json/json_functions/json_create.cpp b/extension/json/json_functions/json_create.cpp index e054990b64c1..9ed6bf1ad068 100644 --- a/extension/json/json_functions/json_create.cpp +++ b/extension/json/json_functions/json_create.cpp @@ -225,7 +225,7 @@ static void AddKeyValuePairs(yyjson_mut_doc *doc, yyjson_mut_val *objs[], Vector idx_t count) { UnifiedVectorFormat key_data; key_v.ToUnifiedFormat(count, key_data); - auto keys = (string_t *)key_data.data; + auto keys = UnifiedVectorFormat::GetData(key_data); for (idx_t i = 0; i < count; i++) { auto key_idx = key_data.sel->get_index(i); @@ -253,7 +253,7 @@ template static void TemplatedCreateValues(yyjson_mut_doc *doc, yyjson_mut_val *vals[], Vector &value_v, idx_t count) { UnifiedVectorFormat value_data; value_v.ToUnifiedFormat(count, value_data); - auto values = (INPUT_TYPE *)value_data.data; + auto values = UnifiedVectorFormat::GetData(value_data); const auto type_is_json = JSONCommon::LogicalTypeIsJSON(value_v.GetType()); for (idx_t i = 0; i < count; i++) { @@ -311,7 +311,7 @@ static void CreateValuesMap(const StructNames &names, yyjson_mut_doc *doc, yyjso // Add the key/value pairs to the values UnifiedVectorFormat map_data; value_v.ToUnifiedFormat(count, map_data); - auto map_key_list_entries = (list_entry_t *)map_data.data; + auto map_key_list_entries = UnifiedVectorFormat::GetData(map_data); for (idx_t i = 0; i < count; i++) { idx_t idx = map_data.sel->get_index(i); if (!map_data.validity.RowIsValid(idx)) { @@ -358,14 +358,14 @@ static void CreateValuesUnion(const StructNames &names, yyjson_mut_doc *doc, yyj // and the rows where the member is not matching the tag UnifiedVectorFormat key_data; member_key_v.ToUnifiedFormat(count, key_data); - auto keys = (string_t *)key_data.data; + auto keys = UnifiedVectorFormat::GetData(key_data); for (idx_t i = 0; i < count; i++) { auto tag_idx = tag_data.sel->get_index(i); if (!tag_data.validity.RowIsValid(tag_idx)) { continue; } - auto tag = ((uint8_t *)tag_data.data)[tag_idx]; + auto tag = (UnifiedVectorFormat::GetData(tag_data))[tag_idx]; if (tag != member_idx) { continue; } @@ -390,7 +390,7 @@ static void CreateValuesList(const StructNames &names, yyjson_mut_doc *doc, yyjs // Now we add the values to the appropriate JSON arrays UnifiedVectorFormat list_data; value_v.ToUnifiedFormat(count, list_data); - auto list_entries = (list_entry_t *)list_data.data; + auto list_entries = UnifiedVectorFormat::GetData(list_data); for (idx_t i = 0; i < count; i++) { idx_t idx = list_data.sel->get_index(i); if (!list_data.validity.RowIsValid(idx)) { diff --git a/extension/json/json_functions/json_merge_patch.cpp b/extension/json/json_functions/json_merge_patch.cpp index 67ed31d5f566..e2f50b6122de 100644 --- a/extension/json/json_functions/json_merge_patch.cpp +++ b/extension/json/json_functions/json_merge_patch.cpp @@ -36,7 +36,7 @@ static inline void ReadObjects(yyjson_mut_doc *doc, Vector &input, yyjson_mut_va UnifiedVectorFormat input_data; auto &input_vector = input; input_vector.ToUnifiedFormat(count, input_data); - auto inputs = (string_t *)input_data.data; + auto inputs = UnifiedVectorFormat::GetData(input_data); // Read the documents for (idx_t i = 0; i < count; i++) { diff --git a/extension/json/json_functions/json_transform.cpp b/extension/json/json_functions/json_transform.cpp index 3896625afeb7..64fbb34d3eba 100644 --- a/extension/json/json_functions/json_transform.cpp +++ b/extension/json/json_functions/json_transform.cpp @@ -776,7 +776,7 @@ static bool TransformFunctionInternal(Vector &input, const idx_t count, Vector & JSONTransformOptions &options) { UnifiedVectorFormat input_data; input.ToUnifiedFormat(count, input_data); - auto inputs = (string_t *)input_data.data; + auto inputs = UnifiedVectorFormat::GetData(input_data); // Read documents auto docs = (yyjson_doc **)alc->malloc(alc->ctx, sizeof(yyjson_doc *) * count); diff --git a/src/common/row_operations/row_scatter.cpp b/src/common/row_operations/row_scatter.cpp index 691bce0e30a1..8478adb3d772 100644 --- a/src/common/row_operations/row_scatter.cpp +++ b/src/common/row_operations/row_scatter.cpp @@ -50,7 +50,7 @@ static void TemplatedScatter(UnifiedVectorFormat &col, Vector &rows, const Selec static void ComputeStringEntrySizes(const UnifiedVectorFormat &col, idx_t entry_sizes[], const SelectionVector &sel, const idx_t count, const idx_t offset = 0) { - auto data = (const string_t *)col.data; + auto data = UnifiedVectorFormat::GetData(col); for (idx_t i = 0; i < count; i++) { auto idx = sel.get_index(i); auto col_idx = col.sel->get_index(idx) + offset; @@ -81,7 +81,7 @@ static void ScatterStringVector(UnifiedVectorFormat &col, Vector &rows, data_ptr Store(string_data[col_idx], row + col_offset); } else { const auto &str = string_data[col_idx]; - string_t inserted((const char *)str_locations[i], str.GetSize()); + string_t inserted(data_ptr_cast(str_locations[i]), str.GetSize()); memcpy(inserted.GetDataWriteable(), str.GetData(), str.GetSize()); str_locations[i] += str.GetSize(); inserted.Finalize(); diff --git a/src/common/serializer/binary_deserializer.cpp b/src/common/serializer/binary_deserializer.cpp index 72a7b9f0bce1..7c5e65cfa992 100644 --- a/src/common/serializer/binary_deserializer.cpp +++ b/src/common/serializer/binary_deserializer.cpp @@ -125,7 +125,7 @@ string BinaryDeserializer::ReadString() { } auto buffer = make_unsafe_uniq_array(size); ReadData(buffer.get(), size); - return string((char *)buffer.get(), size); + return string(data_ptr_cast(buffer.get()), size); } interval_t BinaryDeserializer::ReadInterval() { diff --git a/src/common/serializer/binary_serializer.cpp b/src/common/serializer/binary_serializer.cpp index 83331287ed2a..aac53d7c2bc2 100644 --- a/src/common/serializer/binary_serializer.cpp +++ b/src/common/serializer/binary_serializer.cpp @@ -129,7 +129,7 @@ void BinarySerializer::WriteValue(const string &value) { auto len = value.length(); Write((uint32_t)len); if (len > 0) { - WriteData((const_data_ptr_t)value.c_str(), len); + WriteData(value.c_str(), len); } } @@ -137,7 +137,7 @@ void BinarySerializer::WriteValue(const string_t value) { auto len = value.GetSize(); Write((uint32_t)len); if (len > 0) { - WriteData((const_data_ptr_t)value.GetDataUnsafe(), len); + WriteData(value.GetDataUnsafe(), len); } } @@ -145,7 +145,7 @@ void BinarySerializer::WriteValue(const char *value) { auto len = strlen(value); Write((uint32_t)len); if (len > 0) { - WriteData((const_data_ptr_t)value, len); + WriteData(value, len); } } diff --git a/src/common/types.cpp b/src/common/types.cpp index ab3c1771ae3b..867fb3b4fe01 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -811,6 +811,17 @@ struct ExtraTypeInfo { virtual void FormatSerialize(FormatSerializer &serializer) const; static shared_ptr FormatDeserialize(FormatDeserializer &source); + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } + protected: virtual bool EqualsInternal(ExtraTypeInfo *other_p) const { // Do nothing @@ -888,7 +899,7 @@ struct DecimalTypeInfo : public ExtraTypeInfo { protected: bool EqualsInternal(ExtraTypeInfo *other_p) const override { - auto &other = (DecimalTypeInfo &)*other_p; + auto &other = other_p->Cast(); return width == other.width && scale == other.scale; } }; @@ -897,14 +908,14 @@ uint8_t DecimalType::GetWidth(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::DECIMAL); auto info = type.AuxInfo(); D_ASSERT(info); - return ((DecimalTypeInfo &)*info).width; + return info->Cast().width; } uint8_t DecimalType::GetScale(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::DECIMAL); auto info = type.AuxInfo(); D_ASSERT(info); - return ((DecimalTypeInfo &)*info).scale; + return info->Cast().scale; } uint8_t DecimalType::MaxWidth() { @@ -965,7 +976,7 @@ string StringType::GetCollation(const LogicalType &type) { if (info->type == ExtraTypeInfoType::GENERIC_TYPE_INFO) { return string(); } - return ((StringTypeInfo &)*info).collation; + return info->Cast().collation; } LogicalType LogicalType::VARCHAR_COLLATION(string collation) { // NOLINT @@ -1005,7 +1016,7 @@ struct ListTypeInfo : public ExtraTypeInfo { protected: bool EqualsInternal(ExtraTypeInfo *other_p) const override { - auto &other = (ListTypeInfo &)*other_p; + auto &other = other_p->Cast(); return child_type == other.child_type; } }; @@ -1014,7 +1025,7 @@ const LogicalType &ListType::GetChildType(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::LIST || type.id() == LogicalTypeId::MAP); auto info = type.AuxInfo(); D_ASSERT(info); - return ((ListTypeInfo &)*info).child_type; + return info->Cast().child_type; } LogicalType LogicalType::LIST(const LogicalType &child) { @@ -1066,7 +1077,7 @@ struct StructTypeInfo : public ExtraTypeInfo { protected: bool EqualsInternal(ExtraTypeInfo *other_p) const override { - auto &other = (StructTypeInfo &)*other_p; + auto &other = other_p->Cast(); return child_types == other.child_types; } }; @@ -1122,7 +1133,7 @@ struct AggregateStateTypeInfo : public ExtraTypeInfo { protected: bool EqualsInternal(ExtraTypeInfo *other_p) const override { - auto &other = (AggregateStateTypeInfo &)*other_p; + auto &other = other_p->Cast(); return state_type.function_name == other.state_type.function_name && state_type.return_type == other.state_type.return_type && state_type.bound_argument_types == other.state_type.bound_argument_types; @@ -1133,7 +1144,7 @@ const aggregate_state_t &AggregateStateType::GetStateType(const LogicalType &typ D_ASSERT(type.id() == LogicalTypeId::AGGREGATE_STATE); auto info = type.AuxInfo(); D_ASSERT(info); - return ((AggregateStateTypeInfo &)*info).state_type; + return info->Cast().state_type; } const string AggregateStateType::GetTypeName(const LogicalType &type) { @@ -1142,7 +1153,7 @@ const string AggregateStateType::GetTypeName(const LogicalType &type) { if (!info) { return "AGGREGATE_STATE"; } - auto aggr_state = ((AggregateStateTypeInfo &)*info).state_type; + auto aggr_state = info->Cast().state_type; return "AGGREGATE_STATE<" + aggr_state.function_name + "(" + StringUtil::Join(aggr_state.bound_argument_types, aggr_state.bound_argument_types.size(), ", ", [](const LogicalType &arg_type) { return arg_type.ToString(); }) + @@ -1154,7 +1165,7 @@ const child_list_t &StructType::GetChildTypes(const LogicalType &ty auto info = type.AuxInfo(); D_ASSERT(info); - return ((StructTypeInfo &)*info).child_types; + return info->Cast().child_types; } const LogicalType &StructType::GetChildType(const LogicalType &type, idx_t index) { @@ -1277,7 +1288,7 @@ struct UserTypeInfo : public ExtraTypeInfo { protected: bool EqualsInternal(ExtraTypeInfo *other_p) const override { - auto &other = (UserTypeInfo &)*other_p; + auto &other = other_p->Cast(); return other.user_type_name == user_type_name; } }; @@ -1286,7 +1297,7 @@ const string &UserType::GetTypeName(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::USER); auto info = type.AuxInfo(); D_ASSERT(info); - return ((UserTypeInfo &)*info).user_type_name; + return info->Cast().user_type_name; } LogicalType LogicalType::USER(const string &user_type_name) { @@ -1306,19 +1317,19 @@ struct EnumTypeInfo : public ExtraTypeInfo { dict_type(EnumDictType::VECTOR_DICT), enum_name(std::move(enum_name_p)), dict_size(dict_size_p) { } - const EnumDictType &GetEnumDictType() { + const EnumDictType &GetEnumDictType() const { return dict_type; }; - const string &GetEnumName() { + const string &GetEnumName() const { return enum_name; }; const string GetSchemaName() const { return catalog_entry ? catalog_entry->schema.name : ""; }; - const Vector &GetValuesInsertOrder() { + const Vector &GetValuesInsertOrder() const { return values_insert_order; }; - const idx_t &GetDictSize() { + const idx_t &GetDictSize() const { return dict_size; }; EnumTypeInfo(const EnumTypeInfo &) = delete; @@ -1327,7 +1338,7 @@ struct EnumTypeInfo : public ExtraTypeInfo { protected: // Equalities are only used in enums with different catalog entries bool EqualsInternal(ExtraTypeInfo *other_p) const override { - auto &other = (EnumTypeInfo &)*other_p; + auto &other = other_p->Cast(); if (dict_type != other.dict_type) { return false; } @@ -1360,7 +1371,7 @@ struct EnumTypeInfo : public ExtraTypeInfo { ExtraTypeInfo::FormatSerialize(serializer); serializer.WriteProperty("dict_size", dict_size); serializer.WriteProperty("enum_name", enum_name); - ((Vector &)values_insert_order).FormatSerialize(serializer, dict_size); + ((Vector &)values_insert_order).FormatSerialize(serializer, dict_size); // NOLINT - FIXME } Vector values_insert_order; @@ -1374,7 +1385,7 @@ struct EnumTypeInfo : public ExtraTypeInfo { void EnumType::Serialize(FieldWriter &writer, const ExtraTypeInfo &type_info, bool serialize_internals) { D_ASSERT(type_info.type == ExtraTypeInfoType::ENUM_TYPE_INFO); - auto &enum_info = (EnumTypeInfo &)type_info; + auto &enum_info = type_info.Cast(); // Store Schema Name writer.WriteString(enum_info.GetSchemaName()); // Store Enum Name @@ -1387,7 +1398,7 @@ void EnumType::Serialize(FieldWriter &writer, const ExtraTypeInfo &type_info, bo // Store Dictionary Size writer.WriteField(dict_size); // Store Vector Order By Insertion - ((Vector &)enum_info.GetValuesInsertOrder()).Serialize(dict_size, writer.GetSerializer()); + ((Vector &)enum_info.GetValuesInsertOrder()).Serialize(dict_size, writer.GetSerializer()); // NOLINT - FIXME } } @@ -1443,7 +1454,7 @@ const string &EnumType::GetTypeName(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::ENUM); auto info = type.AuxInfo(); D_ASSERT(info); - return ((EnumTypeInfo &)*info).GetEnumName(); + return info->Cast().GetEnumName(); } static PhysicalType EnumVectorDictType(idx_t size) { @@ -1504,7 +1515,7 @@ int64_t EnumType::GetPos(const LogicalType &type, const string_t &key) { const string EnumType::GetValue(const Value &val) { auto info = val.type().AuxInfo(); - auto &values_insert_order = ((EnumTypeInfo &)*info).GetValuesInsertOrder(); + auto &values_insert_order = info->Cast().GetValuesInsertOrder(); return StringValue::Get(values_insert_order.GetValue(val.GetValue())); } @@ -1512,14 +1523,14 @@ const Vector &EnumType::GetValuesInsertOrder(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::ENUM); auto info = type.AuxInfo(); D_ASSERT(info); - return ((EnumTypeInfo &)*info).GetValuesInsertOrder(); + return info->Cast().GetValuesInsertOrder(); } idx_t EnumType::GetSize(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::ENUM); auto info = type.AuxInfo(); D_ASSERT(info); - return ((EnumTypeInfo &)*info).GetDictSize(); + return info->Cast().GetDictSize(); } void EnumType::SetCatalog(LogicalType &type, optional_ptr catalog_entry) { @@ -1535,7 +1546,7 @@ optional_ptr EnumType::GetCatalog(const LogicalType &type) { if (!info) { return nullptr; } - return ((ExtraTypeInfo &)*info).catalog_entry; + return info->catalog_entry; } string EnumType::GetSchemaName(const LogicalType &type) { @@ -1547,7 +1558,7 @@ PhysicalType EnumType::GetPhysicalType(const LogicalType &type) { D_ASSERT(type.id() == LogicalTypeId::ENUM); auto aux_info = type.AuxInfo(); D_ASSERT(aux_info); - auto &info = (EnumTypeInfo &)*aux_info; + auto &info = aux_info->Cast(); D_ASSERT(info.GetEnumDictType() == EnumDictType::VECTOR_DICT); return EnumVectorDictType(info.GetDictSize()); } diff --git a/src/common/types/bit.cpp b/src/common/types/bit.cpp index f8ffeb46bea6..f8d45dd039eb 100644 --- a/src/common/types/bit.cpp +++ b/src/common/types/bit.cpp @@ -20,7 +20,7 @@ idx_t Bit::ComputeBitstringLen(idx_t len) { } static inline idx_t GetBitPadding(const string_t &bit_string) { - auto data = (const_data_ptr_t)bit_string.GetData(); + auto data = data_ptr_cast(bit_string.GetData()); D_ASSERT(idx_t(data[0]) <= 8); return data[0]; } @@ -61,7 +61,7 @@ void Bit::SetEmptyBitString(string_t &target, idx_t len) { // **** casting functions **** void Bit::ToString(string_t bits, char *output) { - auto data = (const_data_ptr_t)bits.GetData(); + auto data = data_ptr_cast(bits.GetData()); auto len = bits.GetSize(); idx_t padding = GetBitPadding(bits); @@ -84,7 +84,7 @@ string Bit::ToString(string_t str) { } bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_message) { - auto data = (const_data_ptr_t)str.GetData(); + auto data = data_ptr_cast(str.GetData()); auto len = str.GetSize(); str_len = 0; for (idx_t i = 0; i < len; i++) { @@ -107,7 +107,7 @@ bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_messag } void Bit::ToBit(string_t str, string_t &output_str) { - auto data = (const_data_ptr_t)str.GetData(); + auto data = data_ptr_cast(str.GetData()); auto len = str.GetSize(); auto output = output_str.GetDataWriteable(); diff --git a/src/common/types/blob.cpp b/src/common/types/blob.cpp index bb96ab4fa2dc..11935e9aa1e2 100644 --- a/src/common/types/blob.cpp +++ b/src/common/types/blob.cpp @@ -24,7 +24,7 @@ bool IsRegularCharacter(data_t c) { } idx_t Blob::GetStringSize(string_t blob) { - auto data = (const_data_ptr_t)blob.GetData(); + auto data = data_ptr_cast(blob.GetData()); auto len = blob.GetSize(); idx_t str_len = 0; for (idx_t i = 0; i < len; i++) { @@ -40,7 +40,7 @@ idx_t Blob::GetStringSize(string_t blob) { } void Blob::ToString(string_t blob, char *output) { - auto data = (const_data_ptr_t)blob.GetData(); + auto data = data_ptr_cast(blob.GetData()); auto len = blob.GetSize(); idx_t str_idx = 0; for (idx_t i = 0; i < len; i++) { @@ -70,7 +70,7 @@ string Blob::ToString(string_t blob) { } bool Blob::TryGetBlobSize(string_t str, idx_t &str_len, string *error_message) { - auto data = (const_data_ptr_t)str.GetData(); + auto data = data_ptr_cast(str.GetData()); auto len = str.GetSize(); str_len = 0; for (idx_t i = 0; i < len; i++) { @@ -112,7 +112,7 @@ idx_t Blob::GetBlobSize(string_t str) { } void Blob::ToBlob(string_t str, data_ptr_t output) { - auto data = (const_data_ptr_t)str.GetData(); + auto data = data_ptr_cast(str.GetData()); auto len = str.GetSize(); idx_t blob_idx = 0; for (idx_t i = 0; i < len; i++) { @@ -137,7 +137,7 @@ void Blob::ToBlob(string_t str, data_ptr_t output) { string Blob::ToBlob(string_t str) { auto blob_len = GetBlobSize(str); auto buffer = make_unsafe_uniq_array(blob_len); - Blob::ToBlob(str, (data_ptr_t)buffer.get()); + Blob::ToBlob(str, data_ptr_cast(buffer.get())); return string(buffer.get(), blob_len); } @@ -149,7 +149,7 @@ idx_t Blob::ToBase64Size(string_t blob) { } void Blob::ToBase64(string_t blob, char *output) { - auto input_data = (const_data_ptr_t)blob.GetData(); + auto input_data = data_ptr_cast(blob.GetData()); auto input_size = blob.GetSize(); idx_t out_idx = 0; idx_t i; @@ -239,7 +239,7 @@ uint32_t DecodeBase64Bytes(const string_t &str, const_data_ptr_t input_data, idx void Blob::FromBase64(string_t str, data_ptr_t output, idx_t output_size) { D_ASSERT(output_size == FromBase64Size(str)); - auto input_data = (const_data_ptr_t)str.GetData(); + auto input_data = data_ptr_cast(str.GetData()); auto input_size = str.GetSize(); if (input_size == 0) { return; diff --git a/src/common/types/column/column_data_allocator.cpp b/src/common/types/column/column_data_allocator.cpp index 7814563359be..5516196929a7 100644 --- a/src/common/types/column/column_data_allocator.cpp +++ b/src/common/types/column/column_data_allocator.cpp @@ -71,7 +71,7 @@ BufferHandle ColumnDataAllocator::AllocateBlock(idx_t size) { void ColumnDataAllocator::AllocateEmptyBlock(idx_t size) { auto allocation_amount = MaxValue(NextPowerOfTwo(size), 4096); if (!blocks.empty()) { - auto last_capacity = blocks.back().capacity; + idx_t last_capacity = blocks.back().capacity; auto next_capacity = MinValue(last_capacity * 2, last_capacity + Storage::BLOCK_SIZE); allocation_amount = MaxValue(next_capacity, allocation_amount); } diff --git a/src/common/types/column/column_data_collection.cpp b/src/common/types/column/column_data_collection.cpp index cff41943a916..e38dfd4870d0 100644 --- a/src/common/types/column/column_data_collection.cpp +++ b/src/common/types/column/column_data_collection.cpp @@ -454,7 +454,7 @@ void ColumnDataCopy(ColumnDataMetaData &meta_data, const UnifiedVector // 'append_count' is less if we cannot fit that amount of non-inlined strings on one buffer-managed block idx_t append_count; idx_t heap_size = 0; - const auto source_entries = (string_t *)source_data.data; + const auto source_entries = UnifiedVectorFormat::GetData(source_data); for (append_count = 0; append_count < vector_remaining; append_count++) { auto source_idx = source_data.sel->get_index(offset + append_count); if (!source_data.validity.RowIsValid(source_idx)) { diff --git a/src/common/types/row/tuple_data_allocator.cpp b/src/common/types/row/tuple_data_allocator.cpp index e3a7a3ff36a6..7033903057ad 100644 --- a/src/common/types/row/tuple_data_allocator.cpp +++ b/src/common/types/row/tuple_data_allocator.cpp @@ -298,7 +298,7 @@ void TupleDataAllocator::RecomputeHeapPointers(Vector &old_heap_ptrs, const Sele UnifiedVectorFormat new_heap_data; new_heap_ptrs.ToUnifiedFormat(offset + count, new_heap_data); - const auto new_heap_locations = (data_ptr_t *)new_heap_data.data; + const auto new_heap_locations = UnifiedVectorFormat::GetData(new_heap_data); const auto new_heap_sel = *new_heap_data.sel; for (idx_t col_idx = 0; col_idx < layout.ColumnCount(); col_idx++) { diff --git a/src/common/types/row/tuple_data_scatter_gather.cpp b/src/common/types/row/tuple_data_scatter_gather.cpp index a3ff8163be5f..6d940659fe65 100644 --- a/src/common/types/row/tuple_data_scatter_gather.cpp +++ b/src/common/types/row/tuple_data_scatter_gather.cpp @@ -96,7 +96,7 @@ void TupleDataCollection::ComputeHeapSizes(Vector &heap_sizes_v, const Vector &s switch (type) { case PhysicalType::VARCHAR: { // Only non-inlined strings are stored in the heap - const auto source_data = (string_t *)source_vector_data.data; + const auto source_data = UnifiedVectorFormat::GetData(source_vector_data); for (idx_t i = 0; i < append_count; i++) { const auto source_idx = source_sel.get_index(append_sel.get_index(i)); if (source_validity.RowIsValid(source_idx)) { @@ -175,7 +175,7 @@ void TupleDataCollection::ComputeFixedWithinListHeapSizes(Vector &heap_sizes_v, const UnifiedVectorFormat &list_data) { // List data const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Target @@ -206,12 +206,12 @@ void TupleDataCollection::StringWithinListComputeHeapSizes(Vector &heap_sizes_v, // Source const auto &source_data = source_format.data; const auto source_sel = *source_data.sel; - const auto data = (string_t *)source_data.data; + const auto data = UnifiedVectorFormat::GetData(source_data); const auto &source_validity = source_data.validity; // List data const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Target @@ -249,7 +249,7 @@ void TupleDataCollection::StructWithinListComputeHeapSizes(Vector &heap_sizes_v, const UnifiedVectorFormat &list_data) { // List data const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Target @@ -305,13 +305,13 @@ void TupleDataCollection::ListWithinListComputeHeapSizes(Vector &heap_sizes_v, c const UnifiedVectorFormat &list_data) { // List data (of the list Vector that "source_v" is in) const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Child list const auto &child_list_data = source_format.data; const auto child_list_sel = *child_list_data.sel; - const auto child_list_entries = (list_entry_t *)child_list_data.data; + const auto child_list_entries = UnifiedVectorFormat::GetData(child_list_data); const auto &child_list_validity = child_list_data.validity; // Figure out actual child list size (differs from ListVector::GetListSize if dict/const vector) @@ -439,7 +439,7 @@ static void TupleDataTemplatedScatter(const Vector &source, const TupleDataVecto // Source const auto &source_data = source_format.data; const auto source_sel = *source_data.sel; - const auto data = (T *)source_data.data; + const auto data = UnifiedVectorFormat::GetData(source_data); const auto &validity = source_data.validity; // Target @@ -535,7 +535,7 @@ static void TupleDataListScatter(const Vector &source, const TupleDataVectorForm // Source const auto &source_data = source_format.data; const auto source_sel = *source_data.sel; - const auto data = (list_entry_t *)source_data.data; + const auto data = UnifiedVectorFormat::GetData(source_data); const auto &validity = source_data.validity; // Target @@ -582,12 +582,12 @@ static void TupleDataTemplatedWithinListScatter(const Vector &source, const Tupl // Source const auto &source_data = source_format.data; const auto source_sel = *source_data.sel; - const auto data = (T *)source_data.data; + const auto data = UnifiedVectorFormat::GetData(source_data); const auto &source_validity = source_data.validity; // List data const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Target @@ -641,7 +641,7 @@ static void TupleDataStructWithinListScatter(const Vector &source, const TupleDa // List data const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Target @@ -693,13 +693,13 @@ static void TupleDataListWithinListScatter(const Vector &child_list, const Tuple const vector &child_functions) { // List data (of the list Vector that "child_list" is in) const auto list_sel = *list_data.sel; - const auto list_entries = (list_entry_t *)list_data.data; + const auto list_entries = UnifiedVectorFormat::GetData(list_data); const auto &list_validity = list_data.validity; // Child list const auto &child_list_data = child_list_format.data; const auto child_list_sel = *child_list_data.sel; - const auto child_list_entries = (list_entry_t *)child_list_data.data; + const auto child_list_entries = UnifiedVectorFormat::GetData(child_list_data); const auto &child_list_validity = child_list_data.validity; // Target diff --git a/src/common/vector_operations/is_distinct_from.cpp b/src/common/vector_operations/is_distinct_from.cpp index eacc8de4d4c7..ab99c18f854c 100644 --- a/src/common/vector_operations/is_distinct_from.cpp +++ b/src/common/vector_operations/is_distinct_from.cpp @@ -543,7 +543,7 @@ static idx_t DistinctSelectStruct(Vector &left, Vector &right, idx_t count, cons static void PositionListCursor(SelectionVector &cursor, UnifiedVectorFormat &vdata, const idx_t pos, const SelectionVector &slice_sel, const idx_t count) { - const auto data = (const list_entry_t *)vdata.data; + const auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; ++i) { const auto slice_idx = slice_sel.get_index(i); @@ -586,11 +586,11 @@ static idx_t DistinctSelectList(Vector &left, Vector &right, idx_t count, const // Get pointers to the list entries UnifiedVectorFormat lvdata; left.ToUnifiedFormat(count, lvdata); - const auto ldata = (const list_entry_t *)lvdata.data; + const auto ldata = UnifiedVectorFormat::GetData(lvdata); UnifiedVectorFormat rvdata; right.ToUnifiedFormat(count, rvdata); - const auto rdata = (const list_entry_t *)rvdata.data; + const auto rdata = UnifiedVectorFormat::GetData(rvdata); // In order to reuse the comparators, we have to track what passed and failed internally. // To do that, we need local SVs that we then merge back into the real ones after every pass. diff --git a/src/common/vector_operations/vector_hash.cpp b/src/common/vector_operations/vector_hash.cpp index 2dc5d2092341..cae008ca1c25 100644 --- a/src/common/vector_operations/vector_hash.cpp +++ b/src/common/vector_operations/vector_hash.cpp @@ -94,7 +94,7 @@ static inline void ListLoopHash(Vector &input, Vector &hashes, const SelectionVe UnifiedVectorFormat idata; input.ToUnifiedFormat(count, idata); - const auto ldata = (const list_entry_t *)idata.data; + const auto ldata = UnifiedVectorFormat::GetData(idata); // Hash the children into a temporary auto &child = ListVector::GetEntry(input); diff --git a/src/core_functions/aggregate/distributive/arg_min_max.cpp b/src/core_functions/aggregate/distributive/arg_min_max.cpp index a5a072a68392..7585528164d3 100644 --- a/src/core_functions/aggregate/distributive/arg_min_max.cpp +++ b/src/core_functions/aggregate/distributive/arg_min_max.cpp @@ -108,8 +108,8 @@ struct ArgMinMaxBase { } template - static void Operation(STATE *state, AggregateInputData &, A_TYPE *x_data, B_TYPE *y_data, ValidityMask &amask, - ValidityMask &bmask, idx_t xidx, idx_t yidx) { + static void Operation(STATE *state, AggregateInputData &, const A_TYPE *x_data, const B_TYPE *y_data, + ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { if (!state->is_initialized) { STATE::template AssignValue(state->arg, x_data[xidx], false); STATE::template AssignValue(state->value, y_data[yidx], false); @@ -176,7 +176,7 @@ struct VectorArgMinMaxBase : ArgMinMaxBase { auto &by = inputs[1]; UnifiedVectorFormat bdata; by.ToUnifiedFormat(count, bdata); - const auto bys = (BY_TYPE *)bdata.data; + const auto bys = UnifiedVectorFormat::GetData(bdata); UnifiedVectorFormat sdata; state_vector.ToUnifiedFormat(count, sdata); diff --git a/src/core_functions/aggregate/distributive/bitagg.cpp b/src/core_functions/aggregate/distributive/bitagg.cpp index f427ae0199d0..0989e1bfdda3 100644 --- a/src/core_functions/aggregate/distributive/bitagg.cpp +++ b/src/core_functions/aggregate/distributive/bitagg.cpp @@ -48,7 +48,7 @@ struct BitwiseOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (!state->is_set) { OP::template Assign(state, input[idx]); state->is_set = true; @@ -58,7 +58,7 @@ struct BitwiseOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { OP::template Operation(state, aggr_input_data, input, mask, 0); } @@ -118,7 +118,7 @@ struct BitXorOperation : public BitwiseOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); @@ -181,7 +181,7 @@ struct BitStringXorOperation : public BitStringBitwiseOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); diff --git a/src/core_functions/aggregate/distributive/bitstring_agg.cpp b/src/core_functions/aggregate/distributive/bitstring_agg.cpp index 31d5f0115f92..5cffe574750e 100644 --- a/src/core_functions/aggregate/distributive/bitstring_agg.cpp +++ b/src/core_functions/aggregate/distributive/bitstring_agg.cpp @@ -53,7 +53,8 @@ struct BitStringAggOperation { } template - static void Operation(STATE *state, AggregateInputData &data, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &data, const INPUT_TYPE *input, ValidityMask &mask, + idx_t idx) { auto &bind_agg_data = data.bind_data->template Cast(); if (!state->is_set) { if (bind_agg_data.min.IsNull() || bind_agg_data.max.IsNull()) { @@ -87,7 +88,7 @@ struct BitStringAggOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { OP::template Operation(state, aggr_input_data, input, mask, 0); } diff --git a/src/core_functions/aggregate/distributive/bool.cpp b/src/core_functions/aggregate/distributive/bool.cpp index d19fc65fcf06..a1d15ed98919 100644 --- a/src/core_functions/aggregate/distributive/bool.cpp +++ b/src/core_functions/aggregate/distributive/bool.cpp @@ -34,13 +34,13 @@ struct BoolAndFunFunction { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { state->empty = false; state->val = input[idx] && state->val; } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); @@ -73,7 +73,7 @@ struct BoolOrFunFunction { target[idx] = state->val; } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { state->empty = false; state->val = input[idx] || state->val; } diff --git a/src/core_functions/aggregate/distributive/entropy.cpp b/src/core_functions/aggregate/distributive/entropy.cpp index 172f6129e440..d9bae1fb7a21 100644 --- a/src/core_functions/aggregate/distributive/entropy.cpp +++ b/src/core_functions/aggregate/distributive/entropy.cpp @@ -75,7 +75,7 @@ struct EntropyFunctionBase { struct EntropyFunction : EntropyFunctionBase { template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (!state->distinct) { state->distinct = new unordered_map(); } @@ -93,7 +93,7 @@ struct EntropyFunction : EntropyFunctionBase { struct EntropyFunctionString : EntropyFunctionBase { template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (!state->distinct) { state->distinct = new unordered_map(); } @@ -103,7 +103,7 @@ struct EntropyFunctionString : EntropyFunctionBase { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); diff --git a/src/core_functions/aggregate/distributive/kurtosis.cpp b/src/core_functions/aggregate/distributive/kurtosis.cpp index 72ae20145362..3a2c4a188557 100644 --- a/src/core_functions/aggregate/distributive/kurtosis.cpp +++ b/src/core_functions/aggregate/distributive/kurtosis.cpp @@ -22,7 +22,7 @@ struct KurtosisOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); @@ -30,7 +30,7 @@ struct KurtosisOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { state->n++; state->sum += data[idx]; state->sum_sqr += pow(data[idx], 2); diff --git a/src/core_functions/aggregate/distributive/minmax.cpp b/src/core_functions/aggregate/distributive/minmax.cpp index ef9e334d2fa9..0c823fe39222 100644 --- a/src/core_functions/aggregate/distributive/minmax.cpp +++ b/src/core_functions/aggregate/distributive/minmax.cpp @@ -54,8 +54,8 @@ struct MinMaxBase { } template - static void ConstantOperation(STATE *state, AggregateInputData &input_data, INPUT_TYPE *input, ValidityMask &mask, - idx_t count) { + static void ConstantOperation(STATE *state, AggregateInputData &input_data, const INPUT_TYPE *input, + ValidityMask &mask, idx_t count) { D_ASSERT(mask.RowIsValid(0)); if (!state->isset) { OP::template Assign(state, input_data, input[0]); @@ -66,7 +66,7 @@ struct MinMaxBase { } template - static void Operation(STATE *state, AggregateInputData &input_data, INPUT_TYPE *input, ValidityMask &mask, + static void Operation(STATE *state, AggregateInputData &input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (!state->isset) { OP::template Assign(state, input_data, input[idx]); @@ -215,8 +215,8 @@ static bool TemplatedOptimumType(Vector &left, idx_t lidx, idx_t lcount, Vector lidx = lvdata.sel->get_index(lidx); ridx = rvdata.sel->get_index(ridx); - auto ldata = (const T *)lvdata.data; - auto rdata = (const T *)rvdata.data; + auto ldata = UnifiedVectorFormat::GetData(lvdata); + auto rdata = UnifiedVectorFormat::GetData(rvdata); auto &lval = ldata[lidx]; auto &rval = rdata[ridx]; diff --git a/src/core_functions/aggregate/distributive/product.cpp b/src/core_functions/aggregate/distributive/product.cpp index 715fae1dfa5a..fb17fd7ce8bc 100644 --- a/src/core_functions/aggregate/distributive/product.cpp +++ b/src/core_functions/aggregate/distributive/product.cpp @@ -33,7 +33,7 @@ struct ProductFunction { target[idx] = state->val; } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (state->empty) { state->empty = false; } @@ -41,7 +41,7 @@ struct ProductFunction { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); diff --git a/src/core_functions/aggregate/distributive/skew.cpp b/src/core_functions/aggregate/distributive/skew.cpp index 38c2c36cd559..3ee42d713b68 100644 --- a/src/core_functions/aggregate/distributive/skew.cpp +++ b/src/core_functions/aggregate/distributive/skew.cpp @@ -21,7 +21,7 @@ struct SkewnessOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); @@ -29,7 +29,7 @@ struct SkewnessOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { state->n++; state->sum += data[idx]; state->sum_sqr += pow(data[idx], 2); diff --git a/src/core_functions/aggregate/distributive/string_agg.cpp b/src/core_functions/aggregate/distributive/string_agg.cpp index 64a0194e5f80..e2cbbb0087aa 100644 --- a/src/core_functions/aggregate/distributive/string_agg.cpp +++ b/src/core_functions/aggregate/distributive/string_agg.cpp @@ -94,13 +94,13 @@ struct StringAggFunction { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *str_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *str_data, ValidityMask &str_mask, idx_t str_idx) { PerformOperation(state, str_data[str_idx], aggr_input_data.bind_data); } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); diff --git a/src/core_functions/aggregate/holistic/approximate_quantile.cpp b/src/core_functions/aggregate/holistic/approximate_quantile.cpp index 822909506460..94991dbf2a0f 100644 --- a/src/core_functions/aggregate/holistic/approximate_quantile.cpp +++ b/src/core_functions/aggregate/holistic/approximate_quantile.cpp @@ -61,7 +61,7 @@ struct ApproxQuantileOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); @@ -69,7 +69,7 @@ struct ApproxQuantileOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { auto val = Cast::template Operation(data[idx]); if (!Value::DoubleIsFinite(val)) { return; diff --git a/src/core_functions/aggregate/holistic/mode.cpp b/src/core_functions/aggregate/holistic/mode.cpp index 48cac7c6075c..f306e1ce1330 100644 --- a/src/core_functions/aggregate/holistic/mode.cpp +++ b/src/core_functions/aggregate/holistic/mode.cpp @@ -155,7 +155,7 @@ struct ModeFunction { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (!state->frequency_map) { state->frequency_map = new typename STATE::Counts(); } @@ -199,7 +199,7 @@ struct ModeFunction { } } template - static void ConstantOperation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, + static void ConstantOperation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { if (!state->frequency_map) { state->frequency_map = new typename STATE::Counts(); diff --git a/src/core_functions/aggregate/holistic/quantile.cpp b/src/core_functions/aggregate/holistic/quantile.cpp index b178bfa92fa1..3bbea7c9ca16 100644 --- a/src/core_functions/aggregate/holistic/quantile.cpp +++ b/src/core_functions/aggregate/holistic/quantile.cpp @@ -464,7 +464,7 @@ struct QuantileOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { state->v.emplace_back(data[idx]); } diff --git a/src/core_functions/aggregate/holistic/reservoir_quantile.cpp b/src/core_functions/aggregate/holistic/reservoir_quantile.cpp index 7b3369f49fc2..57445d89c766 100644 --- a/src/core_functions/aggregate/holistic/reservoir_quantile.cpp +++ b/src/core_functions/aggregate/holistic/reservoir_quantile.cpp @@ -92,7 +92,7 @@ struct ReservoirQuantileOperation { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { for (idx_t i = 0; i < count; i++) { Operation(state, aggr_input_data, input, mask, 0); @@ -100,7 +100,7 @@ struct ReservoirQuantileOperation { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *data, ValidityMask &mask, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *data, ValidityMask &mask, idx_t idx) { auto &bind_data = aggr_input_data.bind_data->template Cast(); if (state->pos == 0) { diff --git a/src/core_functions/aggregate/nested/histogram.cpp b/src/core_functions/aggregate/nested/histogram.cpp index 22c3b0b6b145..5bfe116d74c3 100644 --- a/src/core_functions/aggregate/nested/histogram.cpp +++ b/src/core_functions/aggregate/nested/histogram.cpp @@ -18,7 +18,7 @@ struct HistogramFunctor { if (!state->hist) { state->hist = new MAP_TYPE(); } - auto value = (T *)input_data.data; + auto value = UnifiedVectorFormat::GetData(input_data); (*state->hist)[value[input_data.sel->get_index(i)]]++; } } diff --git a/src/core_functions/aggregate/regression/regr_avg.cpp b/src/core_functions/aggregate/regression/regr_avg.cpp index c4986de32b08..7ea401c5aeb7 100644 --- a/src/core_functions/aggregate/regression/regr_avg.cpp +++ b/src/core_functions/aggregate/regression/regr_avg.cpp @@ -37,8 +37,8 @@ struct RegrAvgFunction { }; struct RegrAvgXFunction : RegrAvgFunction { template - static void Operation(STATE *state, AggregateInputData &, A_TYPE *x_data, B_TYPE *y_data, ValidityMask &amask, - ValidityMask &bmask, idx_t xidx, idx_t yidx) { + static void Operation(STATE *state, AggregateInputData &, const A_TYPE *x_data, const B_TYPE *y_data, + ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { state->sum += y_data[yidx]; state->count++; } @@ -46,8 +46,8 @@ struct RegrAvgXFunction : RegrAvgFunction { struct RegrAvgYFunction : RegrAvgFunction { template - static void Operation(STATE *state, AggregateInputData &, A_TYPE *x_data, B_TYPE *y_data, ValidityMask &amask, - ValidityMask &bmask, idx_t xidx, idx_t yidx) { + static void Operation(STATE *state, AggregateInputData &, const A_TYPE *x_data, const B_TYPE *y_data, + ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { state->sum += x_data[xidx]; state->count++; } diff --git a/src/core_functions/aggregate/regression/regr_intercept.cpp b/src/core_functions/aggregate/regression/regr_intercept.cpp index 800a7ba51d91..460a0430fa2d 100644 --- a/src/core_functions/aggregate/regression/regr_intercept.cpp +++ b/src/core_functions/aggregate/regression/regr_intercept.cpp @@ -23,7 +23,7 @@ struct RegrInterceptOperation { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { state->count++; state->sum_x += y_data[yidx]; diff --git a/src/core_functions/aggregate/regression/regr_r2.cpp b/src/core_functions/aggregate/regression/regr_r2.cpp index dc0e2f58ae75..416cffca18f2 100644 --- a/src/core_functions/aggregate/regression/regr_r2.cpp +++ b/src/core_functions/aggregate/regression/regr_r2.cpp @@ -24,7 +24,7 @@ struct RegrR2Operation { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { CorrOperation::Operation(&state->corr, aggr_input_data, y_data, x_data, bmask, amask, yidx, xidx); diff --git a/src/core_functions/aggregate/regression/regr_sxx_syy.cpp b/src/core_functions/aggregate/regression/regr_sxx_syy.cpp index b1477e78a147..36aa0011a038 100644 --- a/src/core_functions/aggregate/regression/regr_sxx_syy.cpp +++ b/src/core_functions/aggregate/regression/regr_sxx_syy.cpp @@ -49,7 +49,7 @@ struct RegrBaseOperation { struct RegrSXXOperation : RegrBaseOperation { template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { RegrCountFunction::Operation(&state->count, aggr_input_data, y_data, x_data, bmask, amask, yidx, xidx); @@ -59,7 +59,7 @@ struct RegrSXXOperation : RegrBaseOperation { struct RegrSYYOperation : RegrBaseOperation { template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { RegrCountFunction::Operation(&state->count, aggr_input_data, y_data, x_data, bmask, amask, yidx, xidx); diff --git a/src/core_functions/aggregate/regression/regr_sxy.cpp b/src/core_functions/aggregate/regression/regr_sxy.cpp index adef2b42bcd1..55f65ab381bb 100644 --- a/src/core_functions/aggregate/regression/regr_sxy.cpp +++ b/src/core_functions/aggregate/regression/regr_sxy.cpp @@ -20,7 +20,7 @@ struct RegrSXYOperation { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { RegrCountFunction::Operation(&state->count, aggr_input_data, y_data, x_data, bmask, amask, yidx, xidx); diff --git a/src/core_functions/scalar/date/date_part.cpp b/src/core_functions/scalar/date/date_part.cpp index e07a7c5dccdb..d683a2be00a5 100644 --- a/src/core_functions/scalar/date/date_part.cpp +++ b/src/core_functions/scalar/date/date_part.cpp @@ -1313,7 +1313,7 @@ struct StructDatePart { input.ToUnifiedFormat(count, rdata); const auto &arg_valid = rdata.validity; - auto tdata = (const INPUT_TYPE *)rdata.data; + auto tdata = UnifiedVectorFormat::GetData(rdata); // Start with a valid flat vector result.SetVectorType(VectorType::FLAT_VECTOR); diff --git a/src/core_functions/scalar/generic/least.cpp b/src/core_functions/scalar/generic/least.cpp index 48fdbe5649ab..42e2d68d0e74 100644 --- a/src/core_functions/scalar/generic/least.cpp +++ b/src/core_functions/scalar/generic/least.cpp @@ -37,7 +37,7 @@ static void LeastGreatestFunction(DataChunk &args, ExpressionState &state, Vecto { UnifiedVectorFormat vdata; args.data[0].ToUnifiedFormat(args.size(), vdata); - auto input_data = (T *)vdata.data; + auto input_data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < args.size(); i++) { auto vindex = vdata.sel->get_index(i); if (vdata.validity.RowIsValid(vindex)) { @@ -59,7 +59,7 @@ static void LeastGreatestFunction(DataChunk &args, ExpressionState &state, Vecto UnifiedVectorFormat vdata; args.data[col_idx].ToUnifiedFormat(args.size(), vdata); - auto input_data = (T *)vdata.data; + auto input_data = UnifiedVectorFormat::GetData(vdata); if (!vdata.validity.AllValid()) { // potential new null entries: have to check the null mask for (idx_t i = 0; i < args.size(); i++) { diff --git a/src/core_functions/scalar/list/array_slice.cpp b/src/core_functions/scalar/list/array_slice.cpp index 68c9d76daa01..2c2bd283c5ec 100644 --- a/src/core_functions/scalar/list/array_slice.cpp +++ b/src/core_functions/scalar/list/array_slice.cpp @@ -107,9 +107,9 @@ static void ExecuteSlice(Vector &result, Vector &s, Vector &b, Vector &e, const auto bidx = bdata.sel->get_index(i); auto eidx = edata.sel->get_index(i); - auto sliced = ((INPUT_TYPE *)sdata.data)[sidx]; - auto begin = ((INDEX_TYPE *)bdata.data)[bidx]; - auto end = ((INDEX_TYPE *)edata.data)[eidx]; + auto sliced = (UnifiedVectorFormat::GetData(sdata))[sidx]; + auto begin = (UnifiedVectorFormat::GetData(bdata))[bidx]; + auto end = (UnifiedVectorFormat::GetData(edata))[eidx]; auto svalid = sdata.validity.RowIsValid(sidx); auto bvalid = bdata.validity.RowIsValid(bidx); diff --git a/src/execution/operator/join/physical_asof_join.cpp b/src/execution/operator/join/physical_asof_join.cpp index c154e8f93670..2d02dcf5d14c 100644 --- a/src/execution/operator/join/physical_asof_join.cpp +++ b/src/execution/operator/join/physical_asof_join.cpp @@ -338,7 +338,7 @@ void AsOfLocalState::ResolveJoin(DataChunk &input, bool *found_match, std::pair< // But they may be constant, so unify. UnifiedVectorFormat bin_unified; bin_vector.ToUnifiedFormat(lhs_valid, bin_unified); - const auto bins = (hash_t *)bin_unified.data; + const auto bins = UnifiedVectorFormat::GetData(bin_unified); hash_t prev_bin = global_partition.bin_groups.size(); optional_ptr hash_group; diff --git a/src/function/aggregate/distributive/count.cpp b/src/function/aggregate/distributive/count.cpp index 2da9f276bb48..c920f17b348a 100644 --- a/src/function/aggregate/distributive/count.cpp +++ b/src/function/aggregate/distributive/count.cpp @@ -56,12 +56,12 @@ struct CountStarFunction : public BaseCountFunction { struct CountFunction : public BaseCountFunction { template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { *state += 1; } template - static void ConstantOperation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, + static void ConstantOperation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { *state += count; } diff --git a/src/function/aggregate/distributive/first.cpp b/src/function/aggregate/distributive/first.cpp index 8d2ae17aaadb..3d75c0e417fe 100644 --- a/src/function/aggregate/distributive/first.cpp +++ b/src/function/aggregate/distributive/first.cpp @@ -27,7 +27,7 @@ struct FirstFunctionBase { template struct FirstFunction : public FirstFunctionBase { template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (LAST || !state->is_set) { if (!mask.RowIsValid(idx)) { if (!SKIP_NULLS) { @@ -43,7 +43,7 @@ struct FirstFunction : public FirstFunctionBase { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { Operation(state, aggr_input_data, input, mask, 0); } @@ -94,7 +94,7 @@ struct FirstFunctionString : public FirstFunctionBase { } template - static void Operation(STATE *state, AggregateInputData &input_data, INPUT_TYPE *input, ValidityMask &mask, + static void Operation(STATE *state, AggregateInputData &input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { if (LAST || !state->is_set) { SetValue(state, input_data, input[idx], !mask.RowIsValid(idx)); @@ -102,7 +102,7 @@ struct FirstFunctionString : public FirstFunctionBase { } template - static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, INPUT_TYPE *input, + static void ConstantOperation(STATE *state, AggregateInputData &aggr_input_data, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { Operation(state, aggr_input_data, input, mask, 0); } diff --git a/src/function/scalar/string/like.cpp b/src/function/scalar/string/like.cpp index a5be28fe1692..56829bec2164 100644 --- a/src/function/scalar/string/like.cpp +++ b/src/function/scalar/string/like.cpp @@ -105,8 +105,8 @@ struct LikeMatcher : public FunctionData { for (; segment_idx < end_idx; segment_idx++) { auto &segment = segments[segment_idx]; // find the pattern of the current segment - idx_t next_offset = ContainsFun::Find(str_data, str_len, data_ptr_cast(segment.pattern.c_str()), - segment.pattern.size()); + idx_t next_offset = ContainsFun::Find( + str_data, str_len, data_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); if (next_offset == DConstants::INVALID_INDEX) { // could not find this pattern in the string: no match return false; @@ -130,8 +130,8 @@ struct LikeMatcher : public FunctionData { } else { auto &segment = segments.back(); // find the pattern of the current segment - idx_t next_offset = ContainsFun::Find(str_data, str_len, data_ptr_cast(segment.pattern.c_str()), - segment.pattern.size()); + idx_t next_offset = ContainsFun::Find( + str_data, str_len, data_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); return next_offset != DConstants::INVALID_INDEX; } } diff --git a/src/function/table/arrow.cpp b/src/function/table/arrow.cpp index 41ca656755d0..03aacbabf810 100644 --- a/src/function/table/arrow.cpp +++ b/src/function/table/arrow.cpp @@ -193,7 +193,7 @@ void ArrowTableFunction::RenameArrowColumns(vector &names) { unique_ptr ArrowTableFunction::ArrowScanBind(ClientContext &context, TableFunctionBindInput &input, vector &return_types, vector &names) { auto stream_factory_ptr = input.inputs[0].GetPointer(); - auto stream_factory_produce = (stream_factory_produce_t)input.inputs[1].GetPointer(); // NOLINT + auto stream_factory_produce = (stream_factory_produce_t)input.inputs[1].GetPointer(); // NOLINT auto stream_factory_get_schema = (stream_factory_get_schema_t)input.inputs[2].GetPointer(); // NOLINT auto res = make_uniq(stream_factory_produce, stream_factory_ptr); diff --git a/src/function/table/arrow_conversion.cpp b/src/function/table/arrow_conversion.cpp index 39fb0c6883db..f3ef468905d2 100644 --- a/src/function/table/arrow_conversion.cpp +++ b/src/function/table/arrow_conversion.cpp @@ -28,9 +28,9 @@ static void ShiftRight(unsigned char *ar, int size, int shift) { } } -template +template T *ArrowBufferData(ArrowArray &array, idx_t buffer_idx) { - return (T *) array.buffers[buffer_idx]; // NOLINT + return (T *)array.buffers[buffer_idx]; // NOLINT } static void GetValidityMask(ValidityMask &mask, ArrowArray &array, ArrowScanLocalState &scan_state, idx_t size, @@ -598,8 +598,9 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case PhysicalType::INT128: { - FlatVector::SetData(vector, ArrowBufferData(array, 1) + GetTypeIdSize(vector.GetType().InternalType()) * - (scan_state.chunk_offset + array.offset)); + FlatVector::SetData(vector, + ArrowBufferData(array, 1) + GetTypeIdSize(vector.GetType().InternalType()) * + (scan_state.chunk_offset + array.offset)); break; } default: diff --git a/src/function/table/copy_csv.cpp b/src/function/table/copy_csv.cpp index 2724bf2d2bb1..67f348b56f47 100644 --- a/src/function/table/copy_csv.cpp +++ b/src/function/table/copy_csv.cpp @@ -187,14 +187,15 @@ static bool RequiresQuotes(WriteCSVData &csv_data, const char *str, idx_t len) { // check for delimiter if (options.delimiter.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.delimiter.c_str()), + ContainsFun::Find(data_ptr_cast(str), len, + data_ptr_cast(options.delimiter.c_str()), options.delimiter.size()) != DConstants::INVALID_INDEX) { return true; } // check for quote - if (options.quote.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.quote.c_str()), - options.quote.size()) != DConstants::INVALID_INDEX) { + if (options.quote.length() != 0 && ContainsFun::Find(data_ptr_cast(str), len, + data_ptr_cast(options.quote.c_str()), + options.quote.size()) != DConstants::INVALID_INDEX) { return true; } return false; @@ -224,11 +225,13 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co // complex CSV // check for quote or escape separately if (options.quote.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.quote.c_str()), + ContainsFun::Find(data_ptr_cast(str), len, + data_ptr_cast(options.quote.c_str()), options.quote.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } else if (options.escape.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, data_ptr_cast(options.escape.c_str()), + ContainsFun::Find(data_ptr_cast(str), len, + data_ptr_cast(options.escape.c_str()), options.escape.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } diff --git a/src/include/duckdb/common/arrow/arrow_buffer.hpp b/src/include/duckdb/common/arrow/arrow_buffer.hpp index 2c39d8b9fed5..4f0d887080ea 100644 --- a/src/include/duckdb/common/arrow/arrow_buffer.hpp +++ b/src/include/duckdb/common/arrow/arrow_buffer.hpp @@ -73,12 +73,11 @@ struct ArrowBuffer { return dataptr; } - template + template T *GetData() { - return reinterpret_cast (data()); + return reinterpret_cast(data()); } - private: void ReserveInternal(idx_t bytes) { if (dataptr) { diff --git a/src/include/duckdb/common/serializer/binary_serializer.hpp b/src/include/duckdb/common/serializer/binary_serializer.hpp index dc0431aa5d50..e5d65571975b 100644 --- a/src/include/duckdb/common/serializer/binary_serializer.hpp +++ b/src/include/duckdb/common/serializer/binary_serializer.hpp @@ -30,6 +30,9 @@ struct BinarySerializer : public FormatSerializer { data.insert(data.end(), buffer, buffer + write_size); stack.back().size += write_size; } + void WriteData(const char *ptr, idx_t write_size) { + WriteData((const_data_ptr_t)ptr, write_size); + } explicit BinarySerializer() { serialize_enum_as_string = false; diff --git a/src/include/duckdb/common/typedefs.hpp b/src/include/duckdb/common/typedefs.hpp index 026cf9861277..03b86a54e686 100644 --- a/src/include/duckdb/common/typedefs.hpp +++ b/src/include/duckdb/common/typedefs.hpp @@ -40,7 +40,7 @@ const DEST *const_data_ptr_cast(SRC *src) { return (DEST *)src; } -template +template uintptr_t CastPointerToValue(SRC *src) { return uintptr_t(src); } diff --git a/src/include/duckdb/common/vector_operations/aggregate_executor.hpp b/src/include/duckdb/common/vector_operations/aggregate_executor.hpp index f9cb9e517199..462ab8870515 100644 --- a/src/include/duckdb/common/vector_operations/aggregate_executor.hpp +++ b/src/include/duckdb/common/vector_operations/aggregate_executor.hpp @@ -39,7 +39,7 @@ class AggregateExecutor { } template - static inline void UnaryFlatLoop(INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, + static inline void UnaryFlatLoop(const INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, STATE_TYPE **__restrict states, ValidityMask &mask, idx_t count) { if (!mask.AllValid()) { idx_t base_idx = 0; @@ -76,7 +76,7 @@ class AggregateExecutor { } template - static inline void UnaryScatterLoop(INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, + static inline void UnaryScatterLoop(const INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, STATE_TYPE **__restrict states, const SelectionVector &isel, const SelectionVector &ssel, ValidityMask &mask, idx_t count) { if (OP::IgnoreNull() && !mask.AllValid()) { @@ -99,7 +99,7 @@ class AggregateExecutor { } template - static inline void UnaryFlatUpdateLoop(INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, + static inline void UnaryFlatUpdateLoop(const INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, STATE_TYPE *__restrict state, idx_t count, ValidityMask &mask) { idx_t base_idx = 0; auto entry_count = ValidityMask::EntryCount(count); @@ -129,7 +129,7 @@ class AggregateExecutor { } template - static inline void UnaryUpdateLoop(INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, + static inline void UnaryUpdateLoop(const INPUT_TYPE *__restrict idata, AggregateInputData &aggr_input_data, STATE_TYPE *__restrict state, idx_t count, ValidityMask &mask, const SelectionVector &__restrict sel_vector) { if (OP::IgnoreNull() && !mask.AllValid()) { @@ -150,8 +150,8 @@ class AggregateExecutor { } template - static inline void BinaryScatterLoop(A_TYPE *__restrict adata, AggregateInputData &aggr_input_data, - B_TYPE *__restrict bdata, STATE_TYPE **__restrict states, idx_t count, + static inline void BinaryScatterLoop(const A_TYPE *__restrict adata, AggregateInputData &aggr_input_data, + const B_TYPE *__restrict bdata, STATE_TYPE **__restrict states, idx_t count, const SelectionVector &asel, const SelectionVector &bsel, const SelectionVector &ssel, ValidityMask &avalidity, ValidityMask &bvalidity) { @@ -179,8 +179,8 @@ class AggregateExecutor { } template - static inline void BinaryUpdateLoop(A_TYPE *__restrict adata, AggregateInputData &aggr_input_data, - B_TYPE *__restrict bdata, STATE_TYPE *__restrict state, idx_t count, + static inline void BinaryUpdateLoop(const A_TYPE *__restrict adata, AggregateInputData &aggr_input_data, + const B_TYPE *__restrict bdata, STATE_TYPE *__restrict state, idx_t count, const SelectionVector &asel, const SelectionVector &bsel, ValidityMask &avalidity, ValidityMask &bvalidity) { if (OP::IgnoreNull() && (!avalidity.AllValid() || !bvalidity.AllValid())) { @@ -248,9 +248,9 @@ class AggregateExecutor { UnifiedVectorFormat idata, sdata; input.ToUnifiedFormat(count, idata); states.ToUnifiedFormat(count, sdata); - UnaryScatterLoop((INPUT_TYPE *)idata.data, aggr_input_data, - (STATE_TYPE **)sdata.data, *idata.sel, *sdata.sel, - idata.validity, count); + UnaryScatterLoop(UnifiedVectorFormat::GetData(idata), + aggr_input_data, (STATE_TYPE **)sdata.data, *idata.sel, + *sdata.sel, idata.validity, count); } } @@ -275,8 +275,9 @@ class AggregateExecutor { default: { UnifiedVectorFormat idata; input.ToUnifiedFormat(count, idata); - UnaryUpdateLoop((INPUT_TYPE *)idata.data, aggr_input_data, (STATE_TYPE *)state, - count, idata.validity, *idata.sel); + UnaryUpdateLoop(UnifiedVectorFormat::GetData(idata), + aggr_input_data, (STATE_TYPE *)state, count, idata.validity, + *idata.sel); break; } } @@ -290,9 +291,9 @@ class AggregateExecutor { b.ToUnifiedFormat(count, bdata); states.ToUnifiedFormat(count, sdata); - BinaryScatterLoop((A_TYPE *)adata.data, aggr_input_data, (B_TYPE *)bdata.data, - (STATE_TYPE **)sdata.data, count, *adata.sel, *bdata.sel, - *sdata.sel, adata.validity, bdata.validity); + BinaryScatterLoop( + UnifiedVectorFormat::GetData(adata), aggr_input_data, UnifiedVectorFormat::GetData(bdata), + (STATE_TYPE **)sdata.data, count, *adata.sel, *bdata.sel, *sdata.sel, adata.validity, bdata.validity); } template @@ -302,9 +303,9 @@ class AggregateExecutor { a.ToUnifiedFormat(count, adata); b.ToUnifiedFormat(count, bdata); - BinaryUpdateLoop((A_TYPE *)adata.data, aggr_input_data, (B_TYPE *)bdata.data, - (STATE_TYPE *)state, count, *adata.sel, *bdata.sel, - adata.validity, bdata.validity); + BinaryUpdateLoop( + UnifiedVectorFormat::GetData(adata), aggr_input_data, UnifiedVectorFormat::GetData(bdata), + (STATE_TYPE *)state, count, *adata.sel, *bdata.sel, adata.validity, bdata.validity); } template diff --git a/src/include/duckdb/common/vector_operations/generic_executor.hpp b/src/include/duckdb/common/vector_operations/generic_executor.hpp index f284eb3f5475..55f84e042404 100644 --- a/src/include/duckdb/common/vector_operations/generic_executor.hpp +++ b/src/include/duckdb/common/vector_operations/generic_executor.hpp @@ -38,7 +38,7 @@ struct PrimitiveType { static bool ConstructType(STRUCT_STATE &state, idx_t i, PrimitiveType &result) { auto &vdata = state.main_data; auto idx = vdata.sel->get_index(i); - auto ptr = (INPUT_TYPE *)vdata.data; + auto ptr = UnifiedVectorFormat::GetData(vdata); result.val = ptr[idx]; return true; } @@ -77,7 +77,7 @@ struct StructTypeUnary { if (!a_data.validity.RowIsValid(a_idx)) { return false; } - auto a_ptr = (A_TYPE *)a_data.data; + auto a_ptr = UnifiedVectorFormat::GetData(a_data); result.a_val = a_ptr[a_idx]; return true; } @@ -106,8 +106,8 @@ struct StructTypeBinary { if (!a_data.validity.RowIsValid(a_idx) || !b_data.validity.RowIsValid(b_idx)) { return false; } - auto a_ptr = (A_TYPE *)a_data.data; - auto b_ptr = (B_TYPE *)b_data.data; + auto a_ptr = UnifiedVectorFormat::GetData(a_data); + auto b_ptr = UnifiedVectorFormat::GetData(b_data); result.a_val = a_ptr[a_idx]; result.b_val = b_ptr[b_idx]; return true; @@ -143,9 +143,9 @@ struct StructTypeTernary { !c_data.validity.RowIsValid(c_idx)) { return false; } - auto a_ptr = (A_TYPE *)a_data.data; - auto b_ptr = (B_TYPE *)b_data.data; - auto c_ptr = (C_TYPE *)c_data.data; + auto a_ptr = UnifiedVectorFormat::GetData(a_data); + auto b_ptr = UnifiedVectorFormat::GetData(b_data); + auto c_ptr = UnifiedVectorFormat::GetData(c_data); result.a_val = a_ptr[a_idx]; result.b_val = b_ptr[b_idx]; result.c_val = c_ptr[c_idx]; @@ -188,10 +188,10 @@ struct StructTypeQuaternary { !c_data.validity.RowIsValid(c_idx) || !d_data.validity.RowIsValid(d_idx)) { return false; } - auto a_ptr = (A_TYPE *)a_data.data; - auto b_ptr = (B_TYPE *)b_data.data; - auto c_ptr = (C_TYPE *)c_data.data; - auto d_ptr = (D_TYPE *)d_data.data; + auto a_ptr = UnifiedVectorFormat::GetData(a_data); + auto b_ptr = UnifiedVectorFormat::GetData(b_data); + auto c_ptr = UnifiedVectorFormat::GetData(c_data); + auto d_ptr = UnifiedVectorFormat::GetData(d_data); result.a_val = a_ptr[a_idx]; result.b_val = b_ptr[b_idx]; result.c_val = c_ptr[c_idx]; diff --git a/src/include/duckdb/common/vector_operations/ternary_executor.hpp b/src/include/duckdb/common/vector_operations/ternary_executor.hpp index 44ac3142fafe..5fea2ab4781d 100644 --- a/src/include/duckdb/common/vector_operations/ternary_executor.hpp +++ b/src/include/duckdb/common/vector_operations/ternary_executor.hpp @@ -159,17 +159,20 @@ struct TernaryExecutor { SelectionVector *true_sel, SelectionVector *false_sel) { if (true_sel && false_sel) { return SelectLoop( - (A_TYPE *)adata.data, (B_TYPE *)bdata.data, (C_TYPE *)cdata.data, sel, count, *adata.sel, *bdata.sel, - *cdata.sel, adata.validity, bdata.validity, cdata.validity, true_sel, false_sel); + UnifiedVectorFormat::GetData(adata), UnifiedVectorFormat::GetData(bdata), + UnifiedVectorFormat::GetData(cdata), sel, count, *adata.sel, *bdata.sel, *cdata.sel, + adata.validity, bdata.validity, cdata.validity, true_sel, false_sel); } else if (true_sel) { return SelectLoop( - (A_TYPE *)adata.data, (B_TYPE *)bdata.data, (C_TYPE *)cdata.data, sel, count, *adata.sel, *bdata.sel, - *cdata.sel, adata.validity, bdata.validity, cdata.validity, true_sel, false_sel); + UnifiedVectorFormat::GetData(adata), UnifiedVectorFormat::GetData(bdata), + UnifiedVectorFormat::GetData(cdata), sel, count, *adata.sel, *bdata.sel, *cdata.sel, + adata.validity, bdata.validity, cdata.validity, true_sel, false_sel); } else { D_ASSERT(false_sel); return SelectLoop( - (A_TYPE *)adata.data, (B_TYPE *)bdata.data, (C_TYPE *)cdata.data, sel, count, *adata.sel, *bdata.sel, - *cdata.sel, adata.validity, bdata.validity, cdata.validity, true_sel, false_sel); + UnifiedVectorFormat::GetData(adata), UnifiedVectorFormat::GetData(bdata), + UnifiedVectorFormat::GetData(cdata), sel, count, *adata.sel, *bdata.sel, *cdata.sel, + adata.validity, bdata.validity, cdata.validity, true_sel, false_sel); } } diff --git a/src/include/duckdb/core_functions/aggregate/algebraic/corr.hpp b/src/include/duckdb/core_functions/aggregate/algebraic/corr.hpp index 055615195639..bf5b73d22455 100644 --- a/src/include/duckdb/core_functions/aggregate/algebraic/corr.hpp +++ b/src/include/duckdb/core_functions/aggregate/algebraic/corr.hpp @@ -31,7 +31,7 @@ struct CorrOperation { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { CovarOperation::Operation(&state->cov_pop, aggr_input_data, x_data, y_data, amask, bmask, xidx, yidx); diff --git a/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp b/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp index 8219b9d3702b..67f9bf3bec03 100644 --- a/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp +++ b/src/include/duckdb/core_functions/aggregate/algebraic/covar.hpp @@ -29,7 +29,7 @@ struct CovarOperation { } template - static void Operation(STATE *state, AggregateInputData &, A_TYPE *x_data, B_TYPE *y_data, ValidityMask &amask, + static void Operation(STATE *state, AggregateInputData &, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { // update running mean and d^2 const uint64_t n = ++(state->count); diff --git a/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp b/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp index 644d051e9fe5..9c67aa03f2ba 100644 --- a/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp +++ b/src/include/duckdb/core_functions/aggregate/algebraic/stddev.hpp @@ -30,7 +30,7 @@ struct STDDevBaseOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input_data, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input_data, ValidityMask &mask, idx_t idx) { // update running mean and d^2 state->count++; const double input = input_data[idx]; diff --git a/src/include/duckdb/core_functions/aggregate/regression/regr_count.hpp b/src/include/duckdb/core_functions/aggregate/regression/regr_count.hpp index 7c4db1ff3d89..aa4c1f9acfc5 100644 --- a/src/include/duckdb/core_functions/aggregate/regression/regr_count.hpp +++ b/src/include/duckdb/core_functions/aggregate/regression/regr_count.hpp @@ -33,7 +33,7 @@ struct RegrCountFunction { return true; } template - static void Operation(STATE *state, AggregateInputData &, A_TYPE *x_data, B_TYPE *y_data, ValidityMask &amask, + static void Operation(STATE *state, AggregateInputData &, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { *state += 1; } diff --git a/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp b/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp index 831f2e7f6f31..e6e4039ccb1a 100644 --- a/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp +++ b/src/include/duckdb/core_functions/aggregate/regression/regr_slope.hpp @@ -25,7 +25,7 @@ struct RegrSlopeOperation { } template - static void Operation(STATE *state, AggregateInputData &aggr_input_data, A_TYPE *x_data, B_TYPE *y_data, + static void Operation(STATE *state, AggregateInputData &aggr_input_data, const A_TYPE *x_data, const B_TYPE *y_data, ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { CovarOperation::Operation(&state->cov_pop, aggr_input_data, y_data, x_data, bmask, amask, yidx, xidx); diff --git a/src/include/duckdb/core_functions/aggregate/sum_helpers.hpp b/src/include/duckdb/core_functions/aggregate/sum_helpers.hpp index de4f726baea9..cfb0ebf2340c 100644 --- a/src/include/duckdb/core_functions/aggregate/sum_helpers.hpp +++ b/src/include/duckdb/core_functions/aggregate/sum_helpers.hpp @@ -144,13 +144,13 @@ struct BaseSumOperation { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { STATEOP::template AddValues(state, 1); ADDOP::template AddNumber(*state, input[idx]); } template - static void ConstantOperation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, + static void ConstantOperation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { STATEOP::template AddValues(state, count); ADDOP::template AddConstant(*state, *input, count); diff --git a/src/include/duckdb/function/scalar/list/contains_or_position.hpp b/src/include/duckdb/function/scalar/list/contains_or_position.hpp index d32c0f86a387..047108bf4ecc 100644 --- a/src/include/duckdb/function/scalar/list/contains_or_position.hpp +++ b/src/include/duckdb/function/scalar/list/contains_or_position.hpp @@ -27,14 +27,14 @@ static void TemplatedContainsOrPosition(DataChunk &args, Vector &result, bool is UnifiedVectorFormat list_data; list.ToUnifiedFormat(count, list_data); - auto list_entries = (list_entry_t *)list_data.data; + auto list_entries = UnifiedVectorFormat::GetData(list_data); UnifiedVectorFormat value_data; value_vector.ToUnifiedFormat(count, value_data); // not required for a comparison of nested types - auto child_value = (CHILD_TYPE *)child_data.data; - auto values = (CHILD_TYPE *)value_data.data; + auto child_value = UnifiedVectorFormat::GetData(child_data); + auto values = UnifiedVectorFormat::GetData(value_data); for (idx_t i = 0; i < count; i++) { auto list_index = list_data.sel->get_index(i); diff --git a/src/include/duckdb/storage/compression/chimp/chimp_analyze.hpp b/src/include/duckdb/storage/compression/chimp/chimp_analyze.hpp index dd24f7ced10e..1c2fc3300d96 100644 --- a/src/include/duckdb/storage/compression/chimp/chimp_analyze.hpp +++ b/src/include/duckdb/storage/compression/chimp/chimp_analyze.hpp @@ -113,7 +113,7 @@ bool ChimpAnalyze(AnalyzeState &state, Vector &input, idx_t count) { UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); - auto data = (CHIMP_TYPE *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); analyze_state.WriteValue(data[idx], vdata.validity.RowIsValid(idx)); diff --git a/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp b/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp index 2bdfebdcf156..1218aac6f215 100644 --- a/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp +++ b/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp @@ -127,7 +127,7 @@ struct ChimpCompressionState : public CompressionState { } void Append(UnifiedVectorFormat &vdata, idx_t count) { - auto data = (CHIMP_TYPE *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); diff --git a/src/include/duckdb/storage/compression/patas/patas_analyze.hpp b/src/include/duckdb/storage/compression/patas/patas_analyze.hpp index fd13d686912c..794c9c894699 100644 --- a/src/include/duckdb/storage/compression/patas/patas_analyze.hpp +++ b/src/include/duckdb/storage/compression/patas/patas_analyze.hpp @@ -117,7 +117,7 @@ bool PatasAnalyze(AnalyzeState &state, Vector &input, idx_t count) { UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); analyze_state.state.template Update(data[idx], vdata.validity.RowIsValid(idx)); diff --git a/src/include/duckdb/storage/compression/patas/patas_compress.hpp b/src/include/duckdb/storage/compression/patas/patas_compress.hpp index ce16d796eb88..b6c97b448767 100644 --- a/src/include/duckdb/storage/compression/patas/patas_compress.hpp +++ b/src/include/duckdb/storage/compression/patas/patas_compress.hpp @@ -138,7 +138,7 @@ struct PatasCompressionState : public CompressionState { } void Append(UnifiedVectorFormat &vdata, idx_t count) { - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); diff --git a/src/include/duckdb/storage/string_uncompressed.hpp b/src/include/duckdb/storage/string_uncompressed.hpp index e88603a7c155..fc0ecd9b386b 100644 --- a/src/include/duckdb/storage/string_uncompressed.hpp +++ b/src/include/duckdb/storage/string_uncompressed.hpp @@ -80,7 +80,7 @@ struct UncompressedStringStorage { UnifiedVectorFormat &data, idx_t offset, idx_t count) { D_ASSERT(segment.GetBlockOffset() == 0); auto handle_ptr = handle.Ptr(); - auto source_data = (string_t *)data.data; + auto source_data = UnifiedVectorFormat::GetData(data); auto result_data = (int32_t *)(handle_ptr + DICTIONARY_HEADER_SIZE); uint32_t *dictionary_size = (uint32_t *)handle_ptr; uint32_t *dictionary_end = (uint32_t *)(handle_ptr + sizeof(uint32_t)); diff --git a/src/storage/statistics/list_stats.cpp b/src/storage/statistics/list_stats.cpp index 3ffbdecddb80..29f34c345c5a 100644 --- a/src/storage/statistics/list_stats.cpp +++ b/src/storage/statistics/list_stats.cpp @@ -90,7 +90,7 @@ void ListStats::Verify(const BaseStatistics &stats, Vector &vector, const Select UnifiedVectorFormat vdata; vector.ToUnifiedFormat(count, vdata); - auto list_data = (list_entry_t *)vdata.data; + auto list_data = UnifiedVectorFormat::GetData(vdata); idx_t total_list_count = 0; for (idx_t i = 0; i < count; i++) { auto idx = sel.get_index(i); diff --git a/src/storage/statistics/numeric_stats.cpp b/src/storage/statistics/numeric_stats.cpp index 7bf3ca0c86a2..a37821bd72bb 100644 --- a/src/storage/statistics/numeric_stats.cpp +++ b/src/storage/statistics/numeric_stats.cpp @@ -525,7 +525,7 @@ void NumericStats::TemplatedVerify(const BaseStatistics &stats, Vector &vector, UnifiedVectorFormat vdata; vector.ToUnifiedFormat(count, vdata); - auto data = (T *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); auto min_value = NumericStats::MinOrNull(stats); auto max_value = NumericStats::MaxOrNull(stats); for (idx_t i = 0; i < count; i++) { diff --git a/src/storage/statistics/string_stats.cpp b/src/storage/statistics/string_stats.cpp index 5a8b9b9f6e28..dc596bca35b8 100644 --- a/src/storage/statistics/string_stats.cpp +++ b/src/storage/statistics/string_stats.cpp @@ -230,7 +230,7 @@ void StringStats::Verify(const BaseStatistics &stats, Vector &vector, const Sele UnifiedVectorFormat vdata; vector.ToUnifiedFormat(count, vdata); - auto data = (string_t *)vdata.data; + auto data = UnifiedVectorFormat::GetData(vdata); for (idx_t i = 0; i < count; i++) { auto idx = sel.get_index(i); auto index = vdata.sel->get_index(idx); diff --git a/test/api/udf_function/udf_functions_to_test.hpp b/test/api/udf_function/udf_functions_to_test.hpp index eb6beb4886f3..9b034043c593 100644 --- a/test/api/udf_function/udf_functions_to_test.hpp +++ b/test/api/udf_function/udf_functions_to_test.hpp @@ -324,13 +324,13 @@ struct UDFAverageFunction { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t idx) { state->sum += input[idx]; state->count++; } template - static void ConstantOperation(STATE *state, AggregateInputData &, INPUT_TYPE *input, ValidityMask &mask, + static void ConstantOperation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, ValidityMask &mask, idx_t count) { state->count += count; state->sum += input[0] * count; @@ -376,8 +376,8 @@ struct UDFCovarOperation { } template - static void Operation(STATE *state, AggregateInputData &, A_TYPE *x_data, B_TYPE *y_data, ValidityMask &amask, - ValidityMask &bmask, idx_t xidx, idx_t yidx) { + static void Operation(STATE *state, AggregateInputData &, const A_TYPE *x_data, const B_TYPE *y_data, + ValidityMask &amask, ValidityMask &bmask, idx_t xidx, idx_t yidx) { // update running mean and d^2 const uint64_t n = ++(state->count); @@ -453,13 +453,13 @@ struct UDFSum { } template - static void Operation(STATE *state, AggregateInputData &, INPUT_TYPE *input, idx_t idx) { + static void Operation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, idx_t idx) { state->isset = true; state->value += input[idx]; } template - static void ConstantOperation(STATE *state, AggregateInputData &, INPUT_TYPE *input, idx_t count) { + static void ConstantOperation(STATE *state, AggregateInputData &, const INPUT_TYPE *input, idx_t count) { state->isset = true; state->value += (INPUT_TYPE)input[0] * (INPUT_TYPE)count; } diff --git a/tools/pythonpkg/src/numpy/array_wrapper.cpp b/tools/pythonpkg/src/numpy/array_wrapper.cpp index a0254d0b2b68..0d0915d5c6b7 100644 --- a/tools/pythonpkg/src/numpy/array_wrapper.cpp +++ b/tools/pythonpkg/src/numpy/array_wrapper.cpp @@ -322,7 +322,7 @@ double IntegralConvert::ConvertValue(hugeint_t val) { template static bool ConvertColumn(idx_t target_offset, data_ptr_t target_data, bool *target_mask, UnifiedVectorFormat &idata, idx_t count) { - auto src_ptr = (DUCKDB_T *)idata.data; + auto src_ptr = UnifiedVectorFormat::GetData(idata); auto out_ptr = (NUMPY_T *)target_data; if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { @@ -351,7 +351,7 @@ static bool ConvertColumn(idx_t target_offset, data_ptr_t target_data, bool *tar template static bool ConvertColumnCategoricalTemplate(idx_t target_offset, data_ptr_t target_data, UnifiedVectorFormat &idata, idx_t count) { - auto src_ptr = (DUCKDB_T *)idata.data; + auto src_ptr = UnifiedVectorFormat::GetData(idata); auto out_ptr = (NUMPY_T *)target_data; if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { @@ -427,7 +427,7 @@ static bool ConvertColumnRegular(idx_t target_offset, data_ptr_t target_data, bo template static bool ConvertDecimalInternal(idx_t target_offset, data_ptr_t target_data, bool *target_mask, UnifiedVectorFormat &idata, idx_t count, double division) { - auto src_ptr = (DUCKDB_T *)idata.data; + auto src_ptr = UnifiedVectorFormat::GetData(idata); auto out_ptr = (double *)target_data; if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { From be6d1250a9dc32e5507fb4fbdb835d099e37c17e Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Mon, 22 May 2023 16:14:43 +0200 Subject: [PATCH 09/28] Format and HyperLogLog avoid using void* --- src/common/types/hyperloglog.cpp | 28 +++++++++---------- src/function/table/system/test_all_types.cpp | 6 ++-- src/include/duckdb.h | 28 +++++++++---------- .../duckdb/common/types/hyperloglog.hpp | 8 ++++-- third_party/hyperloglog/hyperloglog.hpp | 4 +-- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/common/types/hyperloglog.cpp b/src/common/types/hyperloglog.cpp index a21a6204e9aa..792902f45a0d 100644 --- a/src/common/types/hyperloglog.cpp +++ b/src/common/types/hyperloglog.cpp @@ -9,18 +9,18 @@ namespace duckdb { HyperLogLog::HyperLogLog() : hll(nullptr) { hll = duckdb_hll::hll_create(); // Insert into a dense hll can be vectorized, sparse cannot, so we immediately convert - duckdb_hll::hllSparseToDense((duckdb_hll::robj *)hll); + duckdb_hll::hllSparseToDense(hll); } -HyperLogLog::HyperLogLog(void *hll) : hll(hll) { +HyperLogLog::HyperLogLog(duckdb_hll::robj *hll) : hll(hll) { } HyperLogLog::~HyperLogLog() { - duckdb_hll::hll_destroy((duckdb_hll::robj *)hll); + duckdb_hll::hll_destroy(hll); } void HyperLogLog::Add(data_ptr_t element, idx_t size) { - if (duckdb_hll::hll_add((duckdb_hll::robj *)hll, element, size) == HLL_C_ERR) { + if (duckdb_hll::hll_add(hll, element, size) == HLL_C_ERR) { throw InternalException("Could not add to HLL?"); } } @@ -29,7 +29,7 @@ idx_t HyperLogLog::Count() const { // exception from size_t ban size_t result; - if (duckdb_hll::hll_count((duckdb_hll::robj *)hll, &result) != HLL_C_OK) { + if (duckdb_hll::hll_count(hll, &result) != HLL_C_OK) { throw InternalException("Could not count HLL?"); } return result; @@ -37,37 +37,37 @@ idx_t HyperLogLog::Count() const { unique_ptr HyperLogLog::Merge(HyperLogLog &other) { duckdb_hll::robj *hlls[2]; - hlls[0] = (duckdb_hll::robj *)hll; - hlls[1] = (duckdb_hll::robj *)other.hll; + hlls[0] = hll; + hlls[1] = other.hll; auto new_hll = duckdb_hll::hll_merge(hlls, 2); if (!new_hll) { throw InternalException("Could not merge HLLs"); } - return unique_ptr(new HyperLogLog((void *)new_hll)); + return unique_ptr(new HyperLogLog(new_hll)); } HyperLogLog *HyperLogLog::MergePointer(HyperLogLog &other) { duckdb_hll::robj *hlls[2]; - hlls[0] = (duckdb_hll::robj *)hll; - hlls[1] = (duckdb_hll::robj *)other.hll; + hlls[0] = hll; + hlls[1] = other.hll; auto new_hll = duckdb_hll::hll_merge(hlls, 2); if (!new_hll) { throw Exception("Could not merge HLLs"); } - return new HyperLogLog((void *)new_hll); + return new HyperLogLog(new_hll); } unique_ptr HyperLogLog::Merge(HyperLogLog logs[], idx_t count) { auto hlls_uptr = unique_ptr {new duckdb_hll::robj *[count]}; auto hlls = hlls_uptr.get(); for (idx_t i = 0; i < count; i++) { - hlls[i] = (duckdb_hll::robj *)logs[i].hll; + hlls[i] = logs[i].hll; } auto new_hll = duckdb_hll::hll_merge(hlls, count); if (!new_hll) { throw InternalException("Could not merge HLLs"); } - return unique_ptr(new HyperLogLog((void *)new_hll)); + return unique_ptr(new HyperLogLog(new_hll)); } idx_t HyperLogLog::GetSize() { @@ -75,7 +75,7 @@ idx_t HyperLogLog::GetSize() { } data_ptr_t HyperLogLog::GetPtr() const { - return (data_ptr_t)((duckdb_hll::robj *)hll)->ptr; + return (data_ptr_t)(hll)->ptr; } unique_ptr HyperLogLog::Copy() { diff --git a/src/function/table/system/test_all_types.cpp b/src/function/table/system/test_all_types.cpp index be429f8eb314..2c0f86f350fc 100644 --- a/src/function/table/system/test_all_types.cpp +++ b/src/function/table/system/test_all_types.cpp @@ -59,8 +59,7 @@ vector TestAllTypesFun::GetTestTypes() { result.emplace_back(LogicalType::INTERVAL, "interval", Value::INTERVAL(min_interval), Value::INTERVAL(max_interval)); // strings/blobs/bitstrings - result.emplace_back(LogicalType::VARCHAR, "varchar", Value("🦆🦆🦆🦆🦆🦆"), - Value(string("goo\x00se", 6))); + result.emplace_back(LogicalType::VARCHAR, "varchar", Value("🦆🦆🦆🦆🦆🦆"), Value(string("goo\x00se", 6))); result.emplace_back(LogicalType::BLOB, "blob", Value::BLOB("thisisalongblob\\x00withnullbytes"), Value::BLOB("\\x00\\x00\\x00a")); result.emplace_back(LogicalType::BIT, "bit", Value::BIT("0010001001011100010101011010111"), Value::BIT("10101")); @@ -124,8 +123,7 @@ vector TestAllTypesFun::GetTestTypes() { auto varchar_list_type = LogicalType::LIST(LogicalType::VARCHAR); auto empty_varchar_list = Value::EMPTYLIST(LogicalType::VARCHAR); - auto varchar_list = - Value::LIST({Value("🦆🦆🦆🦆🦆🦆"), Value("goose"), Value(LogicalType::VARCHAR), Value("")}); + auto varchar_list = Value::LIST({Value("🦆🦆🦆🦆🦆🦆"), Value("goose"), Value(LogicalType::VARCHAR), Value("")}); result.emplace_back(varchar_list_type, "varchar_array", empty_varchar_list, varchar_list); // nested arrays diff --git a/src/include/duckdb.h b/src/include/duckdb.h index 9d870acb3a04..a8b48e254e36 100644 --- a/src/include/duckdb.h +++ b/src/include/duckdb.h @@ -260,46 +260,46 @@ typedef struct { typedef struct _duckdb_database { void *__db; -} * duckdb_database; +} *duckdb_database; typedef struct _duckdb_connection { void *__conn; -} * duckdb_connection; +} *duckdb_connection; typedef struct _duckdb_prepared_statement { void *__prep; -} * duckdb_prepared_statement; +} *duckdb_prepared_statement; typedef struct _duckdb_extracted_statements { void *__extrac; -} * duckdb_extracted_statements; +} *duckdb_extracted_statements; typedef struct _duckdb_pending_result { void *__pend; -} * duckdb_pending_result; +} *duckdb_pending_result; typedef struct _duckdb_appender { void *__appn; -} * duckdb_appender; +} *duckdb_appender; typedef struct _duckdb_arrow { void *__arrw; -} * duckdb_arrow; +} *duckdb_arrow; typedef struct _duckdb_config { void *__cnfg; -} * duckdb_config; +} *duckdb_config; typedef struct _duckdb_arrow_schema { void *__arrs; -} * duckdb_arrow_schema; +} *duckdb_arrow_schema; typedef struct _duckdb_arrow_array { void *__arra; -} * duckdb_arrow_array; +} *duckdb_arrow_array; typedef struct _duckdb_logical_type { void *__lglt; -} * duckdb_logical_type; +} *duckdb_logical_type; typedef struct _duckdb_data_chunk { void *__dtck; -} * duckdb_data_chunk; +} *duckdb_data_chunk; typedef struct _duckdb_vector { void *__vctr; -} * duckdb_vector; +} *duckdb_vector; typedef struct _duckdb_value { void *__val; -} * duckdb_value; +} *duckdb_value; typedef enum { DuckDBSuccess = 0, DuckDBError = 1 } duckdb_state; typedef enum { diff --git a/src/include/duckdb/common/types/hyperloglog.hpp b/src/include/duckdb/common/types/hyperloglog.hpp index ae207197ad29..384f36ed4455 100644 --- a/src/include/duckdb/common/types/hyperloglog.hpp +++ b/src/include/duckdb/common/types/hyperloglog.hpp @@ -12,6 +12,10 @@ #include "duckdb/common/types/vector.hpp" #include "hyperloglog.hpp" +namespace duckdb_hll { +struct robj; +}; + namespace duckdb { enum class HLLStorageType { UNCOMPRESSED = 1 }; @@ -58,9 +62,9 @@ class HyperLogLog { void AddToLog(UnifiedVectorFormat &vdata, idx_t count, uint64_t indices[], uint8_t counts[]); private: - explicit HyperLogLog(void *hll); + explicit HyperLogLog(duckdb_hll::robj *hll); - void *hll; + duckdb_hll::robj *hll; mutex lock; }; } // namespace duckdb diff --git a/third_party/hyperloglog/hyperloglog.hpp b/third_party/hyperloglog/hyperloglog.hpp index cc0985d9ab82..72ae1a17808d 100644 --- a/third_party/hyperloglog/hyperloglog.hpp +++ b/third_party/hyperloglog/hyperloglog.hpp @@ -17,9 +17,9 @@ namespace duckdb_hll { #define HLL_C_OK 0 #define HLL_C_ERR -1 -typedef struct { +struct robj { void *ptr; -} robj; +}; //! Create a new empty HyperLogLog object robj *hll_create(void); From 39d24518f7c0839a4a808e6d038340617dd4d709 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Mon, 22 May 2023 16:43:04 +0200 Subject: [PATCH 10/28] Tidy-fixes in date_part --- src/core_functions/scalar/date/date_part.cpp | 84 ++++++++++---------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/src/core_functions/scalar/date/date_part.cpp b/src/core_functions/scalar/date/date_part.cpp index d683a2be00a5..a42318909912 100644 --- a/src/core_functions/scalar/date/date_part.cpp +++ b/src/core_functions/scalar/date/date_part.cpp @@ -521,8 +521,9 @@ struct DatePart { } template - static inline P HasPartValue(P *part_values, DatePartSpecifier part) { - return part_values[int(part)]; + static inline bool HasPartValue(P *part_values, DatePartSpecifier part, P &value) { + value = part_values[int(part)]; + return value; } template @@ -534,28 +535,28 @@ struct DatePart { int32_t dd = 1; if (mask & YMD) { Date::Convert(input, yyyy, mm, dd); - if ((part_data = HasPartValue(part_values, DatePartSpecifier::YEAR))) { + if (HasPartValue(part_values, DatePartSpecifier::YEAR, part_data)) { part_data[idx] = yyyy; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MONTH))) { + if (HasPartValue(part_values, DatePartSpecifier::MONTH, part_data)) { part_data[idx] = mm; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::DAY))) { + if (HasPartValue(part_values, DatePartSpecifier::DAY, part_data)) { part_data[idx] = dd; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::DECADE))) { + if (HasPartValue(part_values, DatePartSpecifier::DECADE, part_data)) { part_data[idx] = DecadeOperator::DecadeFromYear(yyyy); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::CENTURY))) { + if (HasPartValue(part_values, DatePartSpecifier::CENTURY, part_data)) { part_data[idx] = CenturyOperator::CenturyFromYear(yyyy); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MILLENNIUM))) { + if (HasPartValue(part_values, DatePartSpecifier::MILLENNIUM, part_data)) { part_data[idx] = MillenniumOperator::MillenniumFromYear(yyyy); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::QUARTER))) { + if (HasPartValue(part_values, DatePartSpecifier::QUARTER, part_data)) { part_data[idx] = QuarterOperator::QuarterFromMonth(mm); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::ERA))) { + if (HasPartValue(part_values, DatePartSpecifier::ERA, part_data)) { part_data[idx] = EraOperator::EraFromYear(yyyy); } } @@ -563,10 +564,10 @@ struct DatePart { // Week calculations if (mask & DOW) { auto isodow = Date::ExtractISODayOfTheWeek(input); - if ((part_data = HasPartValue(part_values, DatePartSpecifier::DOW))) { + if (HasPartValue(part_values, DatePartSpecifier::DOW, part_data)) { part_data[idx] = DayOfWeekOperator::DayOfWeekFromISO(isodow); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::ISODOW))) { + if (HasPartValue(part_values, DatePartSpecifier::ISODOW, part_data)) { part_data[idx] = isodow; } } @@ -576,24 +577,24 @@ struct DatePart { int32_t ww = 0; int32_t iyyy = 0; Date::ExtractISOYearWeek(input, iyyy, ww); - if ((part_data = HasPartValue(part_values, DatePartSpecifier::WEEK))) { + if (HasPartValue(part_values, DatePartSpecifier::WEEK, part_data)) { part_data[idx] = ww; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::ISOYEAR))) { + if (HasPartValue(part_values, DatePartSpecifier::ISOYEAR, part_data)) { part_data[idx] = iyyy; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::YEARWEEK))) { + if (HasPartValue(part_values, DatePartSpecifier::YEARWEEK, part_data)) { part_data[idx] = YearWeekOperator::YearWeekFromParts(iyyy, ww); } } if (mask & EPOCH) { - if ((part_data = HasPartValue(part_values, DatePartSpecifier::EPOCH))) { + if (HasPartValue(part_values, DatePartSpecifier::EPOCH, part_data)) { part_data[idx] = Date::Epoch(input); } } if (mask & DOY) { - if ((part_data = HasPartValue(part_values, DatePartSpecifier::DOY))) { + if (HasPartValue(part_values, DatePartSpecifier::DOY, part_data)) { part_data[idx] = Date::ExtractDayOfTheYear(input); } } @@ -948,38 +949,37 @@ void DatePart::StructOperator::Operation(int64_t **part_values, const dtime_t &i int64_t *part_data; if (mask & TIME) { const auto micros = MicrosecondsOperator::Operation(input); - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MICROSECONDS))) { + if (HasPartValue(part_values, DatePartSpecifier::MICROSECONDS, part_data)) { part_data[idx] = micros; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MILLISECONDS))) { + if (HasPartValue(part_values, DatePartSpecifier::MILLISECONDS, part_data)) { part_data[idx] = micros / Interval::MICROS_PER_MSEC; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::SECOND))) { + if (HasPartValue(part_values, DatePartSpecifier::SECOND, part_data)) { part_data[idx] = micros / Interval::MICROS_PER_SEC; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MINUTE))) { + if (HasPartValue(part_values, DatePartSpecifier::MINUTE, part_data)) { part_data[idx] = MinutesOperator::Operation(input); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::HOUR))) { + if (HasPartValue(part_values, DatePartSpecifier::HOUR, part_data)) { part_data[idx] = HoursOperator::Operation(input); } } if (mask & EPOCH) { - if ((part_data = HasPartValue(part_values, DatePartSpecifier::EPOCH))) { + if (HasPartValue(part_values, DatePartSpecifier::EPOCH, part_data)) { part_data[idx] = EpochOperator::Operation(input); - ; } } if (mask & ZONE) { - if ((part_data = HasPartValue(part_values, DatePartSpecifier::TIMEZONE))) { + if (HasPartValue(part_values, DatePartSpecifier::TIMEZONE, part_data)) { part_data[idx] = 0; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::TIMEZONE_HOUR))) { + if (HasPartValue(part_values, DatePartSpecifier::TIMEZONE_HOUR, part_data)) { part_data[idx] = 0; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::TIMEZONE_MINUTE))) { + if (HasPartValue(part_values, DatePartSpecifier::TIMEZONE_MINUTE, part_data)) { part_data[idx] = 0; } } @@ -998,8 +998,8 @@ void DatePart::StructOperator::Operation(int64_t **part_values, const timestamp_ Operation(part_values, t, idx, mask & ~EPOCH); if (mask & EPOCH) { - auto part_data = HasPartValue(part_values, DatePartSpecifier::EPOCH); - if (part_data) { + int64_t *part_data; + if (HasPartValue(part_values, DatePartSpecifier::EPOCH, part_data)) { part_data[idx] = EpochOperator::Operation(input); } } @@ -1011,50 +1011,50 @@ void DatePart::StructOperator::Operation(int64_t **part_values, const interval_t int64_t *part_data; if (mask & YMD) { const auto mm = input.months % Interval::MONTHS_PER_YEAR; - if ((part_data = HasPartValue(part_values, DatePartSpecifier::YEAR))) { + if (HasPartValue(part_values, DatePartSpecifier::YEAR, part_data)) { part_data[idx] = input.months / Interval::MONTHS_PER_YEAR; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MONTH))) { + if (HasPartValue(part_values, DatePartSpecifier::MONTH, part_data)) { part_data[idx] = mm; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::DAY))) { + if (HasPartValue(part_values, DatePartSpecifier::DAY, part_data)) { part_data[idx] = input.days; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::DECADE))) { + if (HasPartValue(part_values, DatePartSpecifier::DECADE, part_data)) { part_data[idx] = input.months / Interval::MONTHS_PER_DECADE; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::CENTURY))) { + if (HasPartValue(part_values, DatePartSpecifier::CENTURY, part_data)) { part_data[idx] = input.months / Interval::MONTHS_PER_CENTURY; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MILLENNIUM))) { + if (HasPartValue(part_values, DatePartSpecifier::MILLENNIUM, part_data)) { part_data[idx] = input.months / Interval::MONTHS_PER_MILLENIUM; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::QUARTER))) { + if (HasPartValue(part_values, DatePartSpecifier::QUARTER, part_data)) { part_data[idx] = mm / Interval::MONTHS_PER_QUARTER + 1; } } if (mask & TIME) { const auto micros = MicrosecondsOperator::Operation(input); - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MICROSECONDS))) { + if (HasPartValue(part_values, DatePartSpecifier::MICROSECONDS, part_data)) { part_data[idx] = micros; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MILLISECONDS))) { + if (HasPartValue(part_values, DatePartSpecifier::MILLISECONDS, part_data)) { part_data[idx] = micros / Interval::MICROS_PER_MSEC; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::SECOND))) { + if (HasPartValue(part_values, DatePartSpecifier::SECOND, part_data)) { part_data[idx] = micros / Interval::MICROS_PER_SEC; } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::MINUTE))) { + if (HasPartValue(part_values, DatePartSpecifier::MINUTE, part_data)) { part_data[idx] = MinutesOperator::Operation(input); } - if ((part_data = HasPartValue(part_values, DatePartSpecifier::HOUR))) { + if (HasPartValue(part_values, DatePartSpecifier::HOUR, part_data)) { part_data[idx] = HoursOperator::Operation(input); } } if (mask & EPOCH) { - if ((part_data = HasPartValue(part_values, DatePartSpecifier::EPOCH))) { + if (HasPartValue(part_values, DatePartSpecifier::EPOCH, part_data)) { part_data[idx] = EpochOperator::Operation(input); } } From 4a13e8a4ffb75f5b4a98362c977cb06fc58c8115 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 10:46:56 +0200 Subject: [PATCH 11/28] Many more C-style casts --- src/catalog/dependency_manager.cpp | 4 +- src/common/arrow/arrow_wrapper.cpp | 14 ++--- src/common/compressed_file_system.cpp | 2 +- src/common/crypto/md5.cpp | 18 +++--- src/common/fsst.cpp | 8 +-- src/common/radix_partitioning.cpp | 4 +- src/common/row_operations/row_heap_gather.cpp | 2 +- src/common/serializer.cpp | 2 +- src/common/sort/comparators.cpp | 2 +- src/common/sort/merge_sorter.cpp | 4 +- src/common/types.cpp | 10 ++-- src/common/types/bit.cpp | 2 +- src/common/types/blob.cpp | 2 +- src/common/types/string_heap.cpp | 2 +- .../scalar/string/string_split.cpp | 2 +- src/execution/adaptive_filter.cpp | 2 +- src/execution/expression_executor.cpp | 42 +++++++------- .../expression_executor/execute_case.cpp | 18 +++--- .../execute_conjunction.cpp | 14 ++--- src/execution/index/art/art.cpp | 58 +++++++++---------- .../index/art/fixed_size_allocator.cpp | 7 +-- src/execution/join_hashtable.cpp | 6 +- .../physical_perfecthash_aggregate.cpp | 2 +- .../aggregate/physical_streaming_window.cpp | 2 +- .../physical_ungrouped_aggregate.cpp | 8 +-- .../operator/filter/physical_filter.cpp | 2 +- .../operator/join/outer_join_marker.cpp | 2 +- .../join/perfect_hash_join_executor.cpp | 2 +- .../operator/join/physical_asof_join.cpp | 2 +- .../join/physical_blockwise_nl_join.cpp | 6 +- .../operator/join/physical_index_join.cpp | 6 +- .../join/physical_nested_loop_join.cpp | 4 +- .../join/physical_piecewise_merge_join.cpp | 6 +- .../operator/order/physical_top_n.cpp | 2 +- .../persistent/buffered_csv_reader.cpp | 4 +- .../operator/persistent/csv_file_handle.cpp | 4 +- .../persistent/physical_batch_insert.cpp | 5 +- .../operator/persistent/physical_insert.cpp | 2 +- .../operator/projection/physical_pivot.cpp | 2 +- .../projection/physical_projection.cpp | 2 +- .../scan/physical_column_data_scan.cpp | 3 +- .../scan/physical_expression_scan.cpp | 2 +- .../scan/physical_positional_scan.cpp | 4 +- .../physical_plan/plan_comparison_join.cpp | 2 +- .../physical_plan/plan_create_table.cpp | 2 +- src/execution/physical_plan_generator.cpp | 4 +- src/execution/radix_partitioned_hashtable.cpp | 2 +- src/execution/reservoir_sample.cpp | 4 +- src/execution/window_segment_tree.cpp | 10 ++-- src/function/cast/decimal_cast.cpp | 2 +- .../scalar/system/aggregate_export.cpp | 6 +- src/function/table/arrow_conversion.cpp | 34 +++++------ src/function/table/copy_csv.cpp | 6 +- src/include/duckdb/common/allocator.hpp | 11 ++++ .../types/row/partitioned_tuple_data.hpp | 12 ++++ .../duckdb/execution/index/art/art.hpp | 6 +- .../helper/physical_result_collector.hpp | 3 + .../duckdb/execution/physical_operator.hpp | 3 + .../duckdb/function/compression_function.hpp | 44 ++++++++++++++ .../duckdb/main/materialized_query_result.hpp | 3 + .../duckdb/main/pending_query_result.hpp | 3 + src/include/duckdb/main/query_result.hpp | 17 ++++++ .../duckdb/main/stream_query_result.hpp | 3 + .../duckdb/planner/operator/logical_pivot.hpp | 3 + .../duckdb/storage/storage_manager.hpp | 12 ++++ .../duckdb/storage/table/chunk_info.hpp | 27 ++++++++- .../storage/table/column_checkpoint_state.hpp | 12 ++++ .../duckdb/storage/table/scan_state.hpp | 22 +++++++ src/parallel/executor.cpp | 2 +- src/storage/arena_allocator.cpp | 4 +- src/storage/buffer/block_handle.cpp | 2 +- .../write_overflow_strings_to_disk.cpp | 4 +- src/storage/checkpoint_manager.cpp | 6 +- .../compression/dictionary_compression.cpp | 28 ++++----- .../compression/fixed_size_uncompressed.cpp | 14 ++--- src/storage/compression/fsst.cpp | 30 +++++----- src/storage/compression/rle.cpp | 10 ++-- .../compression/string_uncompressed.cpp | 18 +++--- .../compression/validity_uncompressed.cpp | 8 +-- src/storage/standard_buffer_manager.cpp | 6 +- src/storage/statistics/string_stats.cpp | 10 ++-- src/storage/table/column_segment.cpp | 6 +- src/storage/table/list_column_data.cpp | 2 +- src/storage/table/row_group.cpp | 8 +-- src/storage/table/standard_column_data.cpp | 2 +- src/storage/wal_replay.cpp | 2 +- src/transaction/duck_transaction_manager.cpp | 2 +- 87 files changed, 443 insertions(+), 271 deletions(-) diff --git a/src/catalog/dependency_manager.cpp b/src/catalog/dependency_manager.cpp index 1f88c4437c14..e04a227c0f6b 100644 --- a/src/catalog/dependency_manager.cpp +++ b/src/catalog/dependency_manager.cpp @@ -220,8 +220,8 @@ void DependencyManager::AddOwnership(CatalogTransaction transaction, CatalogEntr // Emplace guarantees that the same object cannot be inserted twice in the unordered_set // In the case AddOwnership is called twice, because of emplace, the object will not be repeated in the set. // We use an automatic dependency because if the Owner gets deleted, then the owned objects are also deleted - dependents_map[owner].emplace(Dependency(entry, DependencyType::DEPENDENCY_OWNS)); - dependents_map[entry].emplace(Dependency(owner, DependencyType::DEPENDENCY_OWNED_BY)); + dependents_map[owner].emplace(entry, DependencyType::DEPENDENCY_OWNS); + dependents_map[entry].emplace(owner, DependencyType::DEPENDENCY_OWNED_BY); dependencies_map[owner].emplace(entry); } diff --git a/src/common/arrow/arrow_wrapper.cpp b/src/common/arrow/arrow_wrapper.cpp index 0a47412cbf0f..52ea9483c11c 100644 --- a/src/common/arrow/arrow_wrapper.cpp +++ b/src/common/arrow/arrow_wrapper.cpp @@ -77,7 +77,7 @@ int ResultArrowArrayStreamWrapper::MyStreamGetSchema(struct ArrowArrayStream *st if (!stream->release) { return -1; } - auto my_stream = (ResultArrowArrayStreamWrapper *)stream->private_data; + auto my_stream = reinterpret_cast(stream->private_data); if (!my_stream->column_types.empty()) { ArrowConverter::ToArrowSchema(out, my_stream->column_types, my_stream->column_names, my_stream->timezone_config); @@ -90,7 +90,7 @@ int ResultArrowArrayStreamWrapper::MyStreamGetSchema(struct ArrowArrayStream *st return -1; } if (result.type == QueryResultType::STREAM_RESULT) { - auto &stream_result = (StreamQueryResult &)result; + auto &stream_result = result.Cast(); if (!stream_result.IsOpen()) { my_stream->last_error = PreservedError("Query Stream is closed"); return -1; @@ -108,14 +108,14 @@ int ResultArrowArrayStreamWrapper::MyStreamGetNext(struct ArrowArrayStream *stre if (!stream->release) { return -1; } - auto my_stream = (ResultArrowArrayStreamWrapper *)stream->private_data; + auto my_stream = reinterpret_cast(stream->private_data); auto &result = *my_stream->result; if (result.HasError()) { my_stream->last_error = result.GetErrorObject(); return -1; } if (result.type == QueryResultType::STREAM_RESULT) { - auto &stream_result = (StreamQueryResult &)result; + auto &stream_result = result.Cast(); if (!stream_result.IsOpen()) { // Nothing to output out->release = nullptr; @@ -145,7 +145,7 @@ void ResultArrowArrayStreamWrapper::MyStreamRelease(struct ArrowArrayStream *str return; } stream->release = nullptr; - delete (ResultArrowArrayStreamWrapper *)stream->private_data; + delete reinterpret_cast(stream->private_data); } const char *ResultArrowArrayStreamWrapper::MyStreamGetLastError(struct ArrowArrayStream *stream) { @@ -153,7 +153,7 @@ const char *ResultArrowArrayStreamWrapper::MyStreamGetLastError(struct ArrowArra return "stream was released"; } D_ASSERT(stream->private_data); - auto my_stream = (ResultArrowArrayStreamWrapper *)stream->private_data; + auto my_stream = reinterpret_cast(stream->private_data); return my_stream->last_error.Message().c_str(); } @@ -175,7 +175,7 @@ ResultArrowArrayStreamWrapper::ResultArrowArrayStreamWrapper(unique_ptr &chunk, PreservedError &error) { if (result.type == QueryResultType::STREAM_RESULT) { - auto &stream_result = (StreamQueryResult &)result; + auto &stream_result = result.Cast(); if (!stream_result.IsOpen()) { return true; } diff --git a/src/common/compressed_file_system.cpp b/src/common/compressed_file_system.cpp index 93526e25717f..b2660a5706a9 100644 --- a/src/common/compressed_file_system.cpp +++ b/src/common/compressed_file_system.cpp @@ -121,7 +121,7 @@ int64_t CompressedFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_ int64_t CompressedFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes) { auto &compressed_file = handle.Cast(); - return compressed_file.WriteData((data_ptr_t)buffer, nr_bytes); + return compressed_file.WriteData(data_ptr_cast(buffer), nr_bytes); } void CompressedFileSystem::Reset(FileHandle &handle) { diff --git a/src/common/crypto/md5.cpp b/src/common/crypto/md5.cpp index 0f5ad9383a2b..7d21ab7b6c18 100644 --- a/src/common/crypto/md5.cpp +++ b/src/common/crypto/md5.cpp @@ -30,7 +30,7 @@ static void ByteReverse(unsigned char *buf, unsigned longs) { uint32_t t; do { t = (uint32_t)((unsigned)buf[3] << 8 | buf[2]) << 16 | ((unsigned)buf[1] << 8 | buf[0]); - *(uint32_t *)buf = t; + *reinterpret_cast(buf) = t; buf += 4; } while (--longs); } @@ -174,7 +174,7 @@ void MD5Context::MD5Update(const_data_ptr_t input, idx_t len) { } memcpy(p, input, t); ByteReverse(in, 16); - MD5Transform(buf, (uint32_t *)in); + MD5Transform(buf, reinterpret_cast(in)); input += t; len -= t; } @@ -184,7 +184,7 @@ void MD5Context::MD5Update(const_data_ptr_t input, idx_t len) { while (len >= 64) { memcpy(in, input, 64); ByteReverse(in, 16); - MD5Transform(buf, (uint32_t *)in); + MD5Transform(buf, reinterpret_cast(in)); input += 64; len -= 64; } @@ -217,7 +217,7 @@ void MD5Context::Finish(data_ptr_t out_digest) { /* Two lots of padding: Pad the first block to 64 bytes */ memset(p, 0, count); ByteReverse(in, 16); - MD5Transform(buf, (uint32_t *)in); + MD5Transform(buf, reinterpret_cast(in)); /* Now fill the next block with 56 bytes */ memset(in, 0, 56); @@ -228,11 +228,11 @@ void MD5Context::Finish(data_ptr_t out_digest) { ByteReverse(in, 14); /* Append length in bits and transform */ - ((uint32_t *)in)[14] = bits[0]; - ((uint32_t *)in)[15] = bits[1]; + (reinterpret_cast(in))[14] = bits[0]; + (reinterpret_cast(in))[15] = bits[1]; - MD5Transform(buf, (uint32_t *)in); - ByteReverse((unsigned char *)buf, 4); + MD5Transform(buf, reinterpret_cast(in)); + ByteReverse(data_ptr_cast(buf), 4); memcpy(out_digest, buf, 16); } @@ -260,7 +260,7 @@ string MD5Context::FinishHex() { } void MD5Context::Add(const char *data) { - MD5Update((const_data_ptr_t)data, strlen(data)); + MD5Update(data_ptr_cast(data), strlen(data)); } } // namespace duckdb diff --git a/src/common/fsst.cpp b/src/common/fsst.cpp index c662f26c3699..3ecd5e771ae2 100644 --- a/src/common/fsst.cpp +++ b/src/common/fsst.cpp @@ -9,22 +9,22 @@ string_t FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, Vector &resu D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR); unsigned char decompress_buffer[StringUncompressed::STRING_BLOCK_LIMIT + 1]; auto decompressed_string_size = - duckdb_fsst_decompress((duckdb_fsst_decoder_t *)duckdb_fsst_decoder, compressed_string_len, compressed_string, + duckdb_fsst_decompress(reinterpret_cast(duckdb_fsst_decoder), compressed_string_len, compressed_string, StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); D_ASSERT(decompressed_string_size <= StringUncompressed::STRING_BLOCK_LIMIT); - return StringVector::AddStringOrBlob(result, (const char *)decompress_buffer, decompressed_string_size); + return StringVector::AddStringOrBlob(result, data_ptr_cast(decompress_buffer), decompressed_string_size); } Value FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, unsigned char *compressed_string, idx_t compressed_string_len) { unsigned char decompress_buffer[StringUncompressed::STRING_BLOCK_LIMIT + 1]; auto decompressed_string_size = - duckdb_fsst_decompress((duckdb_fsst_decoder_t *)duckdb_fsst_decoder, compressed_string_len, compressed_string, + duckdb_fsst_decompress(reinterpret_cast(duckdb_fsst_decoder), compressed_string_len, compressed_string, StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); D_ASSERT(decompressed_string_size <= StringUncompressed::STRING_BLOCK_LIMIT); - return Value(string((char *)decompress_buffer, decompressed_string_size)); + return Value(string(data_ptr_cast(decompress_buffer), decompressed_string_size)); } } // namespace duckdb diff --git a/src/common/radix_partitioning.cpp b/src/common/radix_partitioning.cpp index 24be724fa358..88dded4b3cdd 100644 --- a/src/common/radix_partitioning.cpp +++ b/src/common/radix_partitioning.cpp @@ -219,8 +219,8 @@ void RadixPartitionedTupleData::RepartitionFinalizeStates(PartitionedTupleData & idx_t finished_partition_idx) const { D_ASSERT(old_partitioned_data.GetType() == PartitionedTupleDataType::RADIX && new_partitioned_data.GetType() == PartitionedTupleDataType::RADIX); - const auto &old_radix_partitions = (RadixPartitionedTupleData &)old_partitioned_data; - const auto &new_radix_partitions = (RadixPartitionedTupleData &)new_partitioned_data; + const auto &old_radix_partitions = old_partitioned_data.Cast(); + const auto &new_radix_partitions = new_partitioned_data.Cast(); const auto old_radix_bits = old_radix_partitions.GetRadixBits(); const auto new_radix_bits = new_radix_partitions.GetRadixBits(); D_ASSERT(new_radix_bits > old_radix_bits); diff --git a/src/common/row_operations/row_heap_gather.cpp b/src/common/row_operations/row_heap_gather.cpp index 399d752581c7..20274b5cadea 100644 --- a/src/common/row_operations/row_heap_gather.cpp +++ b/src/common/row_operations/row_heap_gather.cpp @@ -29,7 +29,7 @@ static void HeapGatherStringVector(Vector &v, const idx_t vcount, const Selectio } auto len = Load(key_locations[i]); key_locations[i] += sizeof(uint32_t); - target[col_idx] = StringVector::AddStringOrBlob(v, string_t((const char *)key_locations[i], len)); + target[col_idx] = StringVector::AddStringOrBlob(v, string_t(data_ptr_cast(key_locations[i]), len)); key_locations[i] += len; } } diff --git a/src/common/serializer.cpp b/src/common/serializer.cpp index b90a0f369f61..21bc2c32486f 100644 --- a/src/common/serializer.cpp +++ b/src/common/serializer.cpp @@ -10,7 +10,7 @@ string Deserializer::Read() { } auto buffer = make_unsafe_uniq_array(size); ReadData(buffer.get(), size); - return string((char *)buffer.get(), size); + return string(data_ptr_cast(buffer.get()), size); } void Deserializer::ReadStringVector(vector &list) { diff --git a/src/common/sort/comparators.cpp b/src/common/sort/comparators.cpp index 625134cbc384..97cd16958e08 100644 --- a/src/common/sort/comparators.cpp +++ b/src/common/sort/comparators.cpp @@ -167,7 +167,7 @@ int Comparators::CompareStringAndAdvance(data_ptr_t &left_ptr, data_ptr_t &right uint32_t right_string_size = Load(right_ptr); left_ptr += sizeof(uint32_t); right_ptr += sizeof(uint32_t); - auto memcmp_res = memcmp((const char *)left_ptr, (const char *)right_ptr, + auto memcmp_res = memcmp(data_ptr_cast(left_ptr), data_ptr_cast(right_ptr), std::min(left_string_size, right_string_size)); left_ptr += left_string_size; diff --git a/src/common/sort/merge_sorter.cpp b/src/common/sort/merge_sorter.cpp index cca0b42e6c68..c7ef208d70df 100644 --- a/src/common/sort/merge_sorter.cpp +++ b/src/common/sort/merge_sorter.cpp @@ -544,7 +544,7 @@ void MergeSorter::MergeData(SortedData &result_data, SortedData &l_data, SortedD const bool &l_smaller = left_smaller[copied + i]; const bool r_smaller = !l_smaller; const auto &entry_size = next_entry_sizes[copied + i]; - memcpy(result_heap_ptr, (data_ptr_t)(l_smaller * (idx_t)l_heap_ptr + r_smaller * (idx_t)r_heap_ptr), + memcpy(result_heap_ptr, reinterpret_cast(l_smaller * CastPointerToValue(l_heap_ptr) + r_smaller * CastPointerToValue(r_heap_ptr)), entry_size); D_ASSERT(Load(result_heap_ptr) == entry_size); result_heap_ptr += entry_size; @@ -585,7 +585,7 @@ void MergeSorter::MergeRows(data_ptr_t &l_ptr, idx_t &l_entry_idx, const idx_t & const bool &l_smaller = left_smaller[copied + i]; const bool r_smaller = !l_smaller; // Use comparison bool (0 or 1) to copy an entry from either side - FastMemcpy(target_ptr, (data_ptr_t)(l_smaller * (idx_t)l_ptr + r_smaller * (idx_t)r_ptr), entry_size); + FastMemcpy(target_ptr, reinterpret_cast(l_smaller * CastPointerToValue(l_ptr) + r_smaller * CastPointerToValue(r_ptr)), entry_size); target_ptr += entry_size; // Use the comparison bool to increment entries and pointers l_entry_idx += l_smaller; diff --git a/src/common/types.cpp b/src/common/types.cpp index 867fb3b4fe01..c4cf057e4845 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -1439,7 +1439,7 @@ struct EnumTypeInfoTemplated : public EnumTypeInfo { return make_shared(std::move(enum_name), values_insert_order, size); } - string_map_t &GetValues() { + const string_map_t &GetValues() const { return values; } @@ -1491,7 +1491,7 @@ LogicalType LogicalType::ENUM(const string &enum_name, Vector &ordered_data, idx } template -int64_t TemplatedGetPos(string_map_t &map, const string_t &key) { +int64_t TemplatedGetPos(const string_map_t &map, const string_t &key) { auto it = map.find(key); if (it == map.end()) { return -1; @@ -1503,11 +1503,11 @@ int64_t EnumType::GetPos(const LogicalType &type, const string_t &key) { auto info = type.AuxInfo(); switch (type.InternalType()) { case PhysicalType::UINT8: - return TemplatedGetPos(((EnumTypeInfoTemplated &)*info).GetValues(), key); + return TemplatedGetPos(info->Cast>().GetValues(), key); case PhysicalType::UINT16: - return TemplatedGetPos(((EnumTypeInfoTemplated &)*info).GetValues(), key); + return TemplatedGetPos(info->Cast>().GetValues(), key); case PhysicalType::UINT32: - return TemplatedGetPos(((EnumTypeInfoTemplated &)*info).GetValues(), key); + return TemplatedGetPos(info->Cast>().GetValues(), key); default: throw InternalException("ENUM can only have unsigned integers (except UINT64) as physical types"); } diff --git a/src/common/types/bit.cpp b/src/common/types/bit.cpp index f8d45dd039eb..5f2e5f6bb52b 100644 --- a/src/common/types/bit.cpp +++ b/src/common/types/bit.cpp @@ -92,7 +92,7 @@ bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_messag str_len++; } else { string error = StringUtil::Format("Invalid character encountered in string -> bit conversion: '%s'", - string((char *)data + i, 1)); + string(data_ptr_cast(data) + i, 1)); HandleCastError::AssignError(error, error_message); return false; } diff --git a/src/common/types/blob.cpp b/src/common/types/blob.cpp index 11935e9aa1e2..c2ec6c55f48b 100644 --- a/src/common/types/blob.cpp +++ b/src/common/types/blob.cpp @@ -84,7 +84,7 @@ bool Blob::TryGetBlobSize(string_t str, idx_t &str_len, string *error_message) { if (data[i + 1] != 'x' || Blob::HEX_MAP[data[i + 2]] < 0 || Blob::HEX_MAP[data[i + 3]] < 0) { string error = StringUtil::Format("Invalid hex escape code encountered in string -> blob conversion: %s", - string((char *)data + i, 4)); + string(data_ptr_cast(data) + i, 4)); HandleCastError::AssignError(error, error_message); return false; } diff --git a/src/common/types/string_heap.cpp b/src/common/types/string_heap.cpp index 6de969c7d471..0e18a7347994 100644 --- a/src/common/types/string_heap.cpp +++ b/src/common/types/string_heap.cpp @@ -51,7 +51,7 @@ string_t StringHeap::AddBlob(const string_t &data) { string_t StringHeap::EmptyString(idx_t len) { D_ASSERT(len > string_t::INLINE_LENGTH); - auto insert_pos = (const char *)allocator.Allocate(len); + auto insert_pos = data_ptr_cast(allocator.Allocate(len)); return string_t(insert_pos, len); } diff --git a/src/core_functions/scalar/string/string_split.cpp b/src/core_functions/scalar/string/string_split.cpp index 02f8065a6f5b..2e0de4d290be 100644 --- a/src/core_functions/scalar/string/string_split.cpp +++ b/src/core_functions/scalar/string/string_split.cpp @@ -45,7 +45,7 @@ struct ConstantRegexpStringSplit { static idx_t Find(const char *input_data, idx_t input_size, const char *delim_data, idx_t delim_size, idx_t &match_size, void *data) { D_ASSERT(data); - auto regex = (duckdb_re2::RE2 *)data; + auto regex = reinterpret_cast(data); duckdb_re2::StringPiece match; if (!regex->Match(duckdb_re2::StringPiece(input_data, input_size), 0, input_size, RE2::UNANCHORED, &match, 1)) { return DConstants::INVALID_INDEX; diff --git a/src/execution/adaptive_filter.cpp b/src/execution/adaptive_filter.cpp index efc57380c82d..bbadeb639e50 100644 --- a/src/execution/adaptive_filter.cpp +++ b/src/execution/adaptive_filter.cpp @@ -7,7 +7,7 @@ namespace duckdb { AdaptiveFilter::AdaptiveFilter(const Expression &expr) : iteration_count(0), observe_interval(10), execute_interval(20), warmup(true) { - auto &conj_expr = (const BoundConjunctionExpression &)expr; + auto &conj_expr = expr.Cast(); D_ASSERT(conj_expr.children.size() > 1); for (idx_t idx = 0; idx < conj_expr.children.size(); idx++) { permutation.push_back(idx); diff --git a/src/execution/expression_executor.cpp b/src/execution/expression_executor.cpp index a3da2050097b..c5a8dc381163 100644 --- a/src/execution/expression_executor.cpp +++ b/src/execution/expression_executor.cpp @@ -144,25 +144,25 @@ unique_ptr ExpressionExecutor::InitializeState(const Expression ExpressionExecutorState &state) { switch (expr.expression_class) { case ExpressionClass::BOUND_REF: - return InitializeState((const BoundReferenceExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_BETWEEN: - return InitializeState((const BoundBetweenExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_CASE: - return InitializeState((const BoundCaseExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_CAST: - return InitializeState((const BoundCastExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_COMPARISON: - return InitializeState((const BoundComparisonExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_CONJUNCTION: - return InitializeState((const BoundConjunctionExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_CONSTANT: - return InitializeState((const BoundConstantExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_FUNCTION: - return InitializeState((const BoundFunctionExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_OPERATOR: - return InitializeState((const BoundOperatorExpression &)expr, state); + return InitializeState(expr.Cast(), state); case ExpressionClass::BOUND_PARAMETER: - return InitializeState((const BoundParameterExpression &)expr, state); + return InitializeState(expr.Cast(), state); default: throw InternalException("Attempting to initialize state of expression of unknown type!"); } @@ -182,34 +182,34 @@ void ExpressionExecutor::Execute(const Expression &expr, ExpressionState *state, } switch (expr.expression_class) { case ExpressionClass::BOUND_BETWEEN: - Execute((const BoundBetweenExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_REF: - Execute((const BoundReferenceExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_CASE: - Execute((const BoundCaseExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_CAST: - Execute((const BoundCastExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_COMPARISON: - Execute((const BoundComparisonExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_CONJUNCTION: - Execute((const BoundConjunctionExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_CONSTANT: - Execute((const BoundConstantExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_FUNCTION: - Execute((const BoundFunctionExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_OPERATOR: - Execute((const BoundOperatorExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; case ExpressionClass::BOUND_PARAMETER: - Execute((const BoundParameterExpression &)expr, state, sel, count, result); + Execute(expr.Cast(), state, sel, count, result); break; default: throw InternalException("Attempting to execute expression of unknown type!"); @@ -283,7 +283,7 @@ idx_t ExpressionExecutor::DefaultSelect(const Expression &expr, ExpressionState // resolve the true/false expression first // then use that to generate the selection vector bool intermediate_bools[STANDARD_VECTOR_SIZE]; - Vector intermediate(LogicalType::BOOLEAN, (data_ptr_t)intermediate_bools); + Vector intermediate(LogicalType::BOOLEAN, data_ptr_cast(intermediate_bools)); Execute(expr, state, sel, count, intermediate); UnifiedVectorFormat idata; diff --git a/src/execution/expression_executor/execute_case.cpp b/src/execution/expression_executor/execute_case.cpp index 073cd05bad7c..16159a69bb7e 100644 --- a/src/execution/expression_executor/execute_case.cpp +++ b/src/execution/expression_executor/execute_case.cpp @@ -27,20 +27,20 @@ unique_ptr ExpressionExecutor::InitializeState(const BoundCaseE void ExpressionExecutor::Execute(const BoundCaseExpression &expr, ExpressionState *state_p, const SelectionVector *sel, idx_t count, Vector &result) { - auto state = (CaseExpressionState *)state_p; + auto &state = state_p->Cast(); - state->intermediate_chunk.Reset(); + state.intermediate_chunk.Reset(); // first execute the check expression - auto current_true_sel = &state->true_sel; - auto current_false_sel = &state->false_sel; + auto current_true_sel = &state.true_sel; + auto current_false_sel = &state.false_sel; auto current_sel = sel; idx_t current_count = count; for (idx_t i = 0; i < expr.case_checks.size(); i++) { auto &case_check = expr.case_checks[i]; - auto &intermediate_result = state->intermediate_chunk.data[i * 2 + 1]; - auto check_state = state->child_states[i * 2].get(); - auto then_state = state->child_states[i * 2 + 1].get(); + auto &intermediate_result = state.intermediate_chunk.data[i * 2 + 1]; + auto check_state = state.child_states[i * 2].get(); + auto then_state = state.child_states[i * 2 + 1].get(); idx_t tcount = Select(*case_check.when_expr, check_state, current_sel, current_count, current_true_sel, current_false_sel); @@ -68,13 +68,13 @@ void ExpressionExecutor::Execute(const BoundCaseExpression &expr, ExpressionStat } } if (current_count > 0) { - auto else_state = state->child_states.back().get(); + auto else_state = state.child_states.back().get(); if (current_count == count) { // everything was false, we can just evaluate the else expression directly Execute(*expr.else_expr, else_state, sel, count, result); return; } else { - auto &intermediate_result = state->intermediate_chunk.data[expr.case_checks.size() * 2]; + auto &intermediate_result = state.intermediate_chunk.data[expr.case_checks.size() * 2]; D_ASSERT(current_sel); Execute(*expr.else_expr, else_state, current_sel, current_count, intermediate_result); diff --git a/src/execution/expression_executor/execute_conjunction.cpp b/src/execution/expression_executor/execute_conjunction.cpp index cb4b774073b0..de1fc8017884 100644 --- a/src/execution/expression_executor/execute_conjunction.cpp +++ b/src/execution/expression_executor/execute_conjunction.cpp @@ -56,7 +56,7 @@ void ExpressionExecutor::Execute(const BoundConjunctionExpression &expr, Express idx_t ExpressionExecutor::Select(const BoundConjunctionExpression &expr, ExpressionState *state_p, const SelectionVector *sel, idx_t count, SelectionVector *true_sel, SelectionVector *false_sel) { - auto state = (ConjunctionState *)state_p; + auto &state = state_p->Cast(); if (expr.type == ExpressionType::CONJUNCTION_AND) { // get runtime statistics @@ -75,8 +75,8 @@ idx_t ExpressionExecutor::Select(const BoundConjunctionExpression &expr, Express true_sel = temp_true.get(); } for (idx_t i = 0; i < expr.children.size(); i++) { - idx_t tcount = Select(*expr.children[state->adaptive_filter->permutation[i]], - state->child_states[state->adaptive_filter->permutation[i]].get(), current_sel, + idx_t tcount = Select(*expr.children[state.adaptive_filter->permutation[i]], + state.child_states[state.adaptive_filter->permutation[i]].get(), current_sel, current_count, true_sel, temp_false.get()); idx_t fcount = current_count - tcount; if (fcount > 0 && false_sel) { @@ -99,7 +99,7 @@ idx_t ExpressionExecutor::Select(const BoundConjunctionExpression &expr, Express // adapt runtime statistics auto end_time = high_resolution_clock::now(); - state->adaptive_filter->AdaptRuntimeStatistics(duration_cast>(end_time - start_time).count()); + state.adaptive_filter->AdaptRuntimeStatistics(duration_cast>(end_time - start_time).count()); return current_count; } else { // get runtime statistics @@ -118,8 +118,8 @@ idx_t ExpressionExecutor::Select(const BoundConjunctionExpression &expr, Express false_sel = temp_false.get(); } for (idx_t i = 0; i < expr.children.size(); i++) { - idx_t tcount = Select(*expr.children[state->adaptive_filter->permutation[i]], - state->child_states[state->adaptive_filter->permutation[i]].get(), current_sel, + idx_t tcount = Select(*expr.children[state.adaptive_filter->permutation[i]], + state.child_states[state.adaptive_filter->permutation[i]].get(), current_sel, current_count, temp_true.get(), false_sel); if (tcount > 0) { if (true_sel) { @@ -136,7 +136,7 @@ idx_t ExpressionExecutor::Select(const BoundConjunctionExpression &expr, Express // adapt runtime statistics auto end_time = high_resolution_clock::now(); - state->adaptive_filter->AdaptRuntimeStatistics(duration_cast>(end_time - start_time).count()); + state.adaptive_filter->AdaptRuntimeStatistics(duration_cast>(end_time - start_time).count()); return result_count; } } diff --git a/src/execution/index/art/art.cpp b/src/execution/index/art/art.cpp index 1745b92c070e..0740a3d6ac65 100644 --- a/src/execution/index/art/art.cpp +++ b/src/execution/index/art/art.cpp @@ -715,16 +715,16 @@ Node ART::Lookup(Node node, const ARTKey &key, idx_t depth) { // False (Otherwise) //===--------------------------------------------------------------------===// -bool ART::SearchGreater(ARTIndexScanState *state, ARTKey &key, bool inclusive, idx_t max_count, +bool ART::SearchGreater(ARTIndexScanState &state, ARTKey &key, bool inclusive, idx_t max_count, vector &result_ids) { - Iterator *it = &state->iterator; + auto &it = state.iterator; // greater than scan: first set the iterator to the node at which we will start our scan by finding the lowest node // that satisfies our requirement - if (!it->art) { - it->art = this; - if (!it->LowerBound(*tree, key, inclusive)) { + if (!it.art) { + it.art = this; + if (!it.LowerBound(*tree, key, inclusive)) { return true; } } @@ -732,74 +732,72 @@ bool ART::SearchGreater(ARTIndexScanState *state, ARTKey &key, bool inclusive, i // after that we continue the scan; we don't need to check the bounds as any value following this value is // automatically bigger and hence satisfies our predicate ARTKey empty_key = ARTKey(); - return it->Scan(empty_key, max_count, result_ids, false); + return it.Scan(empty_key, max_count, result_ids, false); } //===--------------------------------------------------------------------===// // Less Than //===--------------------------------------------------------------------===// -bool ART::SearchLess(ARTIndexScanState *state, ARTKey &upper_bound, bool inclusive, idx_t max_count, +bool ART::SearchLess(ARTIndexScanState &state, ARTKey &upper_bound, bool inclusive, idx_t max_count, vector &result_ids) { if (!tree->IsSet()) { return true; } - Iterator *it = &state->iterator; + auto &it = state.iterator; - if (!it->art) { - it->art = this; + if (!it.art) { + it.art = this; // first find the minimum value in the ART: we start scanning from this value - it->FindMinimum(*tree); + it.FindMinimum(*tree); // early out min value higher than upper bound query - if (it->cur_key > upper_bound) { + if (it.cur_key > upper_bound) { return true; } } // now continue the scan until we reach the upper bound - return it->Scan(upper_bound, max_count, result_ids, inclusive); + return it.Scan(upper_bound, max_count, result_ids, inclusive); } //===--------------------------------------------------------------------===// // Closed Range Query //===--------------------------------------------------------------------===// -bool ART::SearchCloseRange(ARTIndexScanState *state, ARTKey &lower_bound, ARTKey &upper_bound, bool left_inclusive, +bool ART::SearchCloseRange(ARTIndexScanState &state, ARTKey &lower_bound, ARTKey &upper_bound, bool left_inclusive, bool right_inclusive, idx_t max_count, vector &result_ids) { - - Iterator *it = &state->iterator; + auto &it = state.iterator; // first find the first node that satisfies the left predicate - if (!it->art) { - it->art = this; - if (!it->LowerBound(*tree, lower_bound, left_inclusive)) { + if (!it.art) { + it.art = this; + if (!it.LowerBound(*tree, lower_bound, left_inclusive)) { return true; } } // now continue the scan until we reach the upper bound - return it->Scan(upper_bound, max_count, result_ids, right_inclusive); + return it.Scan(upper_bound, max_count, result_ids, right_inclusive); } bool ART::Scan(const Transaction &transaction, const DataTable &table, IndexScanState &table_state, const idx_t max_count, vector &result_ids) { - - auto state = (ARTIndexScanState *)&table_state; + auto &state = table_state.Cast(); vector row_ids; bool success; // FIXME: the key directly owning the data for a single key might be more efficient - D_ASSERT(state->values[0].type().InternalType() == types[0]); + D_ASSERT(state.values[0].type().InternalType() == types[0]); ArenaAllocator arena_allocator(Allocator::Get(db)); - auto key = CreateKey(arena_allocator, types[0], state->values[0]); + auto key = CreateKey(arena_allocator, types[0], state.values[0]); - if (state->values[1].IsNull()) { + if (state.values[1].IsNull()) { // single predicate lock_guard l(lock); - switch (state->expressions[0]) { + switch (state.expressions[0]) { case ExpressionType::COMPARE_EQUAL: success = SearchEqual(key, max_count, row_ids); break; @@ -824,11 +822,11 @@ bool ART::Scan(const Transaction &transaction, const DataTable &table, IndexScan // two predicates lock_guard l(lock); - D_ASSERT(state->values[1].type().InternalType() == types[0]); - auto upper_bound = CreateKey(arena_allocator, types[0], state->values[1]); + D_ASSERT(state.values[1].type().InternalType() == types[0]); + auto upper_bound = CreateKey(arena_allocator, types[0], state.values[1]); - bool left_inclusive = state->expressions[0] == ExpressionType ::COMPARE_GREATERTHANOREQUALTO; - bool right_inclusive = state->expressions[1] == ExpressionType ::COMPARE_LESSTHANOREQUALTO; + bool left_inclusive = state.expressions[0] == ExpressionType ::COMPARE_GREATERTHANOREQUALTO; + bool right_inclusive = state.expressions[1] == ExpressionType ::COMPARE_LESSTHANOREQUALTO; success = SearchCloseRange(state, key, upper_bound, left_inclusive, right_inclusive, max_count, row_ids); } diff --git a/src/execution/index/art/fixed_size_allocator.cpp b/src/execution/index/art/fixed_size_allocator.cpp index 83fc0ed596b2..791e39dca738 100644 --- a/src/execution/index/art/fixed_size_allocator.cpp +++ b/src/execution/index/art/fixed_size_allocator.cpp @@ -59,7 +59,7 @@ SwizzleablePointer FixedSizeAllocator::New() { buffers_with_free_space.insert(buffer_id); // set the bitmask - ValidityMask mask((validity_t *)buffer); + ValidityMask mask(reinterpret_cast(buffer)); mask.SetAllValid(allocations_per_buffer); } @@ -67,7 +67,7 @@ SwizzleablePointer FixedSizeAllocator::New() { D_ASSERT(!buffers_with_free_space.empty()); auto buffer_id = (uint32_t)*buffers_with_free_space.begin(); - auto bitmask_ptr = (validity_t *)buffers[buffer_id].ptr; + auto bitmask_ptr = reinterpret_cast(buffers[buffer_id].ptr); ValidityMask mask(bitmask_ptr); auto offset = GetOffset(mask, buffers[buffer_id].allocation_count); @@ -81,8 +81,7 @@ SwizzleablePointer FixedSizeAllocator::New() { } void FixedSizeAllocator::Free(const SwizzleablePointer ptr) { - - auto bitmask_ptr = (validity_t *)buffers[ptr.buffer_id].ptr; + auto bitmask_ptr = reinterpret_cast(buffers[ptr.buffer_id].ptr); ValidityMask mask(bitmask_ptr); D_ASSERT(!mask.RowIsValid(ptr.offset)); mask.SetValid(ptr.offset); diff --git a/src/execution/join_hashtable.cpp b/src/execution/join_hashtable.cpp index 8943e88fee93..0f31a4d0c389 100644 --- a/src/execution/join_hashtable.cpp +++ b/src/execution/join_hashtable.cpp @@ -104,7 +104,7 @@ void JoinHashTable::ApplyBitmask(Vector &hashes, const SelectionVector &sel, idx auto hash_data = UnifiedVectorFormat::GetData(hdata); auto result_data = FlatVector::GetData(pointers); - auto main_ht = (data_ptr_t *)hash_map.get(); + auto main_ht = reinterpret_cast(hash_map.get()); for (idx_t i = 0; i < count; i++) { auto rindex = sel.get_index(i); auto hindex = hdata.sel->get_index(rindex); @@ -269,7 +269,7 @@ void JoinHashTable::InsertHashes(Vector &hashes, idx_t count, data_ptr_t key_loc hashes.Flatten(count); D_ASSERT(hashes.GetVectorType() == VectorType::FLAT_VECTOR); - auto pointers = (atomic *)hash_map.get(); + auto pointers = reinterpret_cast *>(hash_map.get()); auto indices = FlatVector::GetData(hashes); if (parallel) { @@ -300,7 +300,7 @@ void JoinHashTable::InitializePointerTable() { D_ASSERT(hash_map.GetSize() == capacity * sizeof(data_ptr_t)); // initialize HT with all-zero entries - std::fill_n((data_ptr_t *)hash_map.get(), capacity, nullptr); + std::fill_n(reinterpret_cast(hash_map.get()), capacity, nullptr); bitmask = capacity - 1; } diff --git a/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp b/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp index 95e60a28256d..9d30621003cc 100644 --- a/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp +++ b/src/execution/operator/aggregate/physical_perfecthash_aggregate.cpp @@ -186,7 +186,7 @@ unique_ptr PhysicalPerfectHashAggregate::GetGlobalSourceState SourceResultType PhysicalPerfectHashAggregate::GetData(ExecutionContext &context, DataChunk &chunk, OperatorSourceInput &input) const { - auto &state = (PerfectHashAggregateState &)input.global_state; + auto &state = input.global_state.Cast(); auto &gstate = sink_state->Cast(); gstate.ht->Scan(state.ht_scan_position, chunk); diff --git a/src/execution/operator/aggregate/physical_streaming_window.cpp b/src/execution/operator/aggregate/physical_streaming_window.cpp index a4ae99359676..187a8b00c279 100644 --- a/src/execution/operator/aggregate/physical_streaming_window.cpp +++ b/src/execution/operator/aggregate/physical_streaming_window.cpp @@ -26,7 +26,7 @@ class StreamingWindowState : public OperatorState { public: using StateBuffer = vector; - StreamingWindowState() : initialized(false), statev(LogicalType::POINTER, (data_ptr_t)&state_ptr) { + StreamingWindowState() : initialized(false), statev(LogicalType::POINTER, data_ptr_cast(&state_ptr)) { } ~StreamingWindowState() override { diff --git a/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp b/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp index 6c6bfe1c59c6..eefeeea00479 100644 --- a/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp +++ b/src/execution/operator/aggregate/physical_ungrouped_aggregate.cpp @@ -55,7 +55,7 @@ struct AggregateState { if (!destructors[i]) { continue; } - Vector state_vector(Value::POINTER((uintptr_t)aggregates[i].get())); + Vector state_vector(Value::POINTER(CastPointerToValue(aggregates[i].get()))); state_vector.SetVectorType(VectorType::FLAT_VECTOR); AggregateInputData aggr_input_data(bind_data[i], Allocator::DefaultAllocator()); @@ -332,8 +332,8 @@ void PhysicalUngroupedAggregate::Combine(ExecutionContext &context, GlobalSinkSt continue; } - Vector source_state(Value::POINTER((uintptr_t)source.state.aggregates[aggr_idx].get())); - Vector dest_state(Value::POINTER((uintptr_t)gstate.state.aggregates[aggr_idx].get())); + Vector source_state(Value::POINTER(CastPointerToValue(source.state.aggregates[aggr_idx].get()))); + Vector dest_state(Value::POINTER(CastPointerToValue(gstate.state.aggregates[aggr_idx].get()))); AggregateInputData aggr_input_data(aggregate.bind_info.get(), Allocator::DefaultAllocator()); aggregate.function.combine(source_state, dest_state, aggr_input_data, 1); @@ -565,7 +565,7 @@ SourceResultType PhysicalUngroupedAggregate::GetData(ExecutionContext &context, for (idx_t aggr_idx = 0; aggr_idx < aggregates.size(); aggr_idx++) { auto &aggregate = aggregates[aggr_idx]->Cast(); - Vector state_vector(Value::POINTER((uintptr_t)gstate.state.aggregates[aggr_idx].get())); + Vector state_vector(Value::POINTER(CastPointerToValue(gstate.state.aggregates[aggr_idx].get()))); AggregateInputData aggr_input_data(aggregate.bind_info.get(), Allocator::DefaultAllocator()); aggregate.function.finalize(state_vector, aggr_input_data, chunk.data[aggr_idx], 1, 0); } diff --git a/src/execution/operator/filter/physical_filter.cpp b/src/execution/operator/filter/physical_filter.cpp index d7d1fe3e82cb..1097d46743bb 100644 --- a/src/execution/operator/filter/physical_filter.cpp +++ b/src/execution/operator/filter/physical_filter.cpp @@ -41,7 +41,7 @@ unique_ptr PhysicalFilter::GetOperatorState(ExecutionContext &con OperatorResultType PhysicalFilter::ExecuteInternal(ExecutionContext &context, DataChunk &input, DataChunk &chunk, GlobalOperatorState &gstate, OperatorState &state_p) const { - auto &state = (FilterState &)state_p; + auto &state = state_p.Cast(); idx_t result_count = state.executor.SelectExpression(input, state.sel); if (result_count == input.size()) { // nothing was filtered: skip adding any selection vectors diff --git a/src/execution/operator/join/outer_join_marker.cpp b/src/execution/operator/join/outer_join_marker.cpp index c4aadd6e0b6c..53d7de3d8bc0 100644 --- a/src/execution/operator/join/outer_join_marker.cpp +++ b/src/execution/operator/join/outer_join_marker.cpp @@ -63,7 +63,7 @@ void OuterJoinMarker::ConstructLeftJoinResult(DataChunk &left, DataChunk &result } idx_t OuterJoinMarker::MaxThreads() const { - return count / (STANDARD_VECTOR_SIZE * 10); + return count / (STANDARD_VECTOR_SIZE * 10ULL); } void OuterJoinMarker::InitializeScan(ColumnDataCollection &data, OuterJoinGlobalScanState &gstate) { diff --git a/src/execution/operator/join/perfect_hash_join_executor.cpp b/src/execution/operator/join/perfect_hash_join_executor.cpp index 91f7f408d2e4..7c7ad73e78c1 100644 --- a/src/execution/operator/join/perfect_hash_join_executor.cpp +++ b/src/execution/operator/join/perfect_hash_join_executor.cpp @@ -168,7 +168,7 @@ unique_ptr PerfectHashJoinExecutor::GetOperatorState(ExecutionCon OperatorResultType PerfectHashJoinExecutor::ProbePerfectHashTable(ExecutionContext &context, DataChunk &input, DataChunk &result, OperatorState &state_p) { - auto &state = (PerfectHashJoinState &)state_p; + auto &state = state_p.Cast(); // keeps track of how many probe keys have a match idx_t probe_sel_count = 0; diff --git a/src/execution/operator/join/physical_asof_join.cpp b/src/execution/operator/join/physical_asof_join.cpp index 2d02dcf5d14c..52f2e76b1b48 100644 --- a/src/execution/operator/join/physical_asof_join.cpp +++ b/src/execution/operator/join/physical_asof_join.cpp @@ -305,7 +305,7 @@ void AsOfLocalState::ResolveJoinKeys(DataChunk &input) { DataChunk payload_chunk; payload_chunk.InitializeEmpty({LogicalType::UINTEGER}); - FlatVector::SetData(payload_chunk.data[0], (data_ptr_t)lhs_sel.data()); + FlatVector::SetData(payload_chunk.data[0], data_ptr_cast(lhs_sel.data())); payload_chunk.SetCardinality(lhs_valid); local_sort.SinkChunk(lhs_keys, payload_chunk); diff --git a/src/execution/operator/join/physical_blockwise_nl_join.cpp b/src/execution/operator/join/physical_blockwise_nl_join.cpp index dcdd769a5ca9..26d9fe8a2f19 100644 --- a/src/execution/operator/join/physical_blockwise_nl_join.cpp +++ b/src/execution/operator/join/physical_blockwise_nl_join.cpp @@ -111,7 +111,7 @@ OperatorResultType PhysicalBlockwiseNLJoin::ExecuteInternal(ExecutionContext &co DataChunk &chunk, GlobalOperatorState &gstate_p, OperatorState &state_p) const { D_ASSERT(input.size() > 0); - auto &state = (BlockwiseNLJoinState &)state_p; + auto &state = state_p.Cast(); auto &gstate = sink_state->Cast(); if (gstate.right_chunks.Count() == 0) { @@ -252,8 +252,8 @@ SourceResultType PhysicalBlockwiseNLJoin::GetData(ExecutionContext &context, Dat D_ASSERT(IsRightOuterJoin(join_type)); // check if we need to scan any unmatched tuples from the RHS for the full/right outer join auto &sink = sink_state->Cast(); - auto &gstate = (BlockwiseNLJoinGlobalScanState &)input.global_state; - auto &lstate = (BlockwiseNLJoinLocalScanState &)input.local_state; + auto &gstate = input.global_state.Cast(); + auto &lstate = input.local_state.Cast(); // if the LHS is exhausted in a FULL/RIGHT OUTER JOIN, we scan chunks we still need to output sink.right_outer.Scan(gstate.scan_state, lstate.scan_state, chunk); diff --git a/src/execution/operator/join/physical_index_join.cpp b/src/execution/operator/join/physical_index_join.cpp index 268b28324268..dadb164c6a38 100644 --- a/src/execution/operator/join/physical_index_join.cpp +++ b/src/execution/operator/join/physical_index_join.cpp @@ -67,7 +67,7 @@ PhysicalIndexJoin::PhysicalIndexJoin(LogicalOperator &op, unique_ptrtype == PhysicalOperatorType::TABLE_SCAN); - auto &tbl_scan = (PhysicalTableScan &)*right; + auto &tbl_scan = right->Cast(); column_ids = std::move(column_ids_p); children.push_back(std::move(left)); children.push_back(std::move(right)); @@ -109,7 +109,7 @@ unique_ptr PhysicalIndexJoin::GetOperatorState(ExecutionContext & void PhysicalIndexJoin::Output(ExecutionContext &context, DataChunk &input, DataChunk &chunk, OperatorState &state_p) const { - auto &phy_tbl_scan = (PhysicalTableScan &)*children[1]; + auto &phy_tbl_scan = children[1]->Cast(); auto &bind_tbl = phy_tbl_scan.bind_data->Cast(); auto &transaction = DuckTransaction::Get(context.client, bind_tbl.table.catalog); auto &state = state_p.Cast(); @@ -139,7 +139,7 @@ void PhysicalIndexJoin::Output(ExecutionContext &context, DataChunk &input, Data } state.rhs_chunk.Reset(); state.fetch_state = make_uniq(); - Vector row_ids(LogicalType::ROW_TYPE, (data_ptr_t)&fetch_rows[0]); + Vector row_ids(LogicalType::ROW_TYPE, data_ptr_cast(&fetch_rows[0])); tbl.Fetch(transaction, state.rhs_chunk, fetch_ids, row_ids, output_sel_idx, *state.fetch_state); } diff --git a/src/execution/operator/join/physical_nested_loop_join.cpp b/src/execution/operator/join/physical_nested_loop_join.cpp index 21291fa68b79..85c51f28fbcf 100644 --- a/src/execution/operator/join/physical_nested_loop_join.cpp +++ b/src/execution/operator/join/physical_nested_loop_join.cpp @@ -451,8 +451,8 @@ SourceResultType PhysicalNestedLoopJoin::GetData(ExecutionContext &context, Data D_ASSERT(IsRightOuterJoin(join_type)); // check if we need to scan any unmatched tuples from the RHS for the full/right outer join auto &sink = sink_state->Cast(); - auto &gstate = (NestedLoopJoinGlobalScanState &)input.global_state; - auto &lstate = (NestedLoopJoinLocalScanState &)input.local_state; + auto &gstate = input.global_state.Cast(); + auto &lstate = input.local_state.Cast(); // if the LHS is exhausted in a FULL/RIGHT OUTER JOIN, we scan chunks we still need to output sink.right_outer.Scan(gstate.scan_state, lstate.scan_state, chunk); diff --git a/src/execution/operator/join/physical_piecewise_merge_join.cpp b/src/execution/operator/join/physical_piecewise_merge_join.cpp index 0e5aa3e8b866..abfcfc66b451 100644 --- a/src/execution/operator/join/physical_piecewise_merge_join.cpp +++ b/src/execution/operator/join/physical_piecewise_merge_join.cpp @@ -385,7 +385,7 @@ static idx_t MergeJoinSimpleBlocks(PiecewiseMergeJoinState &lstate, MergeJoinGlo void PhysicalPiecewiseMergeJoin::ResolveSimpleJoin(ExecutionContext &context, DataChunk &input, DataChunk &chunk, OperatorState &state_p) const { - auto &state = (PiecewiseMergeJoinState &)state_p; + auto &state = state_p.Cast(); auto &gstate = sink_state->Cast(); state.ResolveJoinKeys(input); @@ -507,7 +507,7 @@ static idx_t MergeJoinComplexBlocks(BlockMergeInfo &l, BlockMergeInfo &r, const OperatorResultType PhysicalPiecewiseMergeJoin::ResolveComplexJoin(ExecutionContext &context, DataChunk &input, DataChunk &chunk, OperatorState &state_p) const { - auto &state = (PiecewiseMergeJoinState &)state_p; + auto &state = state_p.Cast(); auto &gstate = sink_state->Cast(); auto &rsorted = *gstate.table->global_sort_state.sorted_blocks[0]; const auto left_cols = input.ColumnCount(); @@ -682,7 +682,7 @@ SourceResultType PhysicalPiecewiseMergeJoin::GetData(ExecutionContext &context, D_ASSERT(IsRightOuterJoin(join_type)); // check if we need to scan any unmatched tuples from the RHS for the full/right outer join auto &sink = sink_state->Cast(); - auto &state = (PiecewiseJoinScanState &)input.global_state; + auto &state = input.global_state.Cast(); lock_guard l(state.lock); if (!state.scanner) { diff --git a/src/execution/operator/order/physical_top_n.cpp b/src/execution/operator/order/physical_top_n.cpp index 1d2bdd2a0e4a..a2b405b91a56 100644 --- a/src/execution/operator/order/physical_top_n.cpp +++ b/src/execution/operator/order/physical_top_n.cpp @@ -276,7 +276,7 @@ void TopNHeap::Finalize() { } void TopNHeap::Reduce() { - idx_t min_sort_threshold = MaxValue(STANDARD_VECTOR_SIZE * 5, 2 * (limit + offset)); + idx_t min_sort_threshold = MaxValue(STANDARD_VECTOR_SIZE * 5ULL, 2ULL * (limit + offset)); if (sort_state.count < min_sort_threshold) { // only reduce when we pass two times the limit + offset, or 5 vectors (whichever comes first) return; diff --git a/src/execution/operator/persistent/buffered_csv_reader.cpp b/src/execution/operator/persistent/buffered_csv_reader.cpp index 125ccc091e36..e6af9f1b355c 100644 --- a/src/execution/operator/persistent/buffered_csv_reader.cpp +++ b/src/execution/operator/persistent/buffered_csv_reader.cpp @@ -186,8 +186,8 @@ static string TrimWhitespace(const string &col_name) { static string NormalizeColumnName(const string &col_name) { // normalize UTF8 characters to NFKD - auto nfkd = utf8proc_NFKD((const utf8proc_uint8_t *)col_name.c_str(), col_name.size()); - const string col_name_nfkd = string((const char *)nfkd, strlen((const char *)nfkd)); + auto nfkd = utf8proc_NFKD(reinterpret_cast(col_name.c_str()), col_name.size()); + const string col_name_nfkd = string(data_ptr_cast(nfkd), strlen(data_ptr_cast(nfkd))); free(nfkd); // only keep ASCII characters 0-9 a-z A-Z and replace spaces with regular whitespace diff --git a/src/execution/operator/persistent/csv_file_handle.cpp b/src/execution/operator/persistent/csv_file_handle.cpp index b319c20bc636..e70023f420b6 100644 --- a/src/execution/operator/persistent/csv_file_handle.cpp +++ b/src/execution/operator/persistent/csv_file_handle.cpp @@ -99,7 +99,7 @@ idx_t CSVFileHandle::Read(void *buffer, idx_t nr_bytes) { } // we have data left to read from the file // read directly into the buffer - auto bytes_read = file_handle->Read((char *)buffer + result_offset, nr_bytes - result_offset); + auto bytes_read = file_handle->Read(data_ptr_cast(buffer) + result_offset, nr_bytes - result_offset); file_size = file_handle->GetFileSize(); read_position += bytes_read; if (reset_enabled) { @@ -114,7 +114,7 @@ idx_t CSVFileHandle::Read(void *buffer, idx_t nr_bytes) { } cached_buffer = std::move(new_buffer); } - memcpy(cached_buffer.get() + buffer_size, (char *)buffer + result_offset, bytes_read); + memcpy(cached_buffer.get() + buffer_size, data_ptr_cast(buffer) + result_offset, bytes_read); buffer_size += bytes_read; } diff --git a/src/execution/operator/persistent/physical_batch_insert.cpp b/src/execution/operator/persistent/physical_batch_insert.cpp index e5e9f179c843..e4edc6406ed3 100644 --- a/src/execution/operator/persistent/physical_batch_insert.cpp +++ b/src/execution/operator/persistent/physical_batch_insert.cpp @@ -279,9 +279,8 @@ unique_ptr PhysicalBatchInsert::GetGlobalSinkState(ClientContex // CREATE TABLE AS D_ASSERT(!insert_table); auto &catalog = schema->catalog; - table = (TableCatalogEntry *)catalog - .CreateTable(catalog.GetCatalogTransaction(context), *schema.get_mutable(), *info) - .get(); + auto created_table = catalog.CreateTable(catalog.GetCatalogTransaction(context), *schema.get_mutable(), *info); + table = &created_table->Cast(); } else { D_ASSERT(insert_table); D_ASSERT(insert_table->IsDuckTable()); diff --git a/src/execution/operator/persistent/physical_insert.cpp b/src/execution/operator/persistent/physical_insert.cpp index 82eb031b9157..c587955353f7 100644 --- a/src/execution/operator/persistent/physical_insert.cpp +++ b/src/execution/operator/persistent/physical_insert.cpp @@ -63,7 +63,7 @@ PhysicalInsert::PhysicalInsert(LogicalOperator &op, SchemaCatalogEntry &schema, void PhysicalInsert::GetInsertInfo(const BoundCreateTableInfo &info, vector &insert_types, vector> &bound_defaults) { - auto &create_info = (CreateTableInfo &)*info.base; + auto &create_info = info.base->Cast(); for (auto &col : create_info.columns.Physical()) { insert_types.push_back(col.GetType()); bound_defaults.push_back(make_uniq(Value(col.GetType()))); diff --git a/src/execution/operator/projection/physical_pivot.cpp b/src/execution/operator/projection/physical_pivot.cpp index c0dc7f0cdcfb..855908d49b55 100644 --- a/src/execution/operator/projection/physical_pivot.cpp +++ b/src/execution/operator/projection/physical_pivot.cpp @@ -17,7 +17,7 @@ PhysicalPivot::PhysicalPivot(vector types_p, unique_ptrCast(); // for each aggregate, initialize an empty aggregate state and finalize it immediately auto state = make_unsafe_uniq_array(aggr.function.state_size()); aggr.function.initialize(state.get()); diff --git a/src/execution/operator/projection/physical_projection.cpp b/src/execution/operator/projection/physical_projection.cpp index d64182ca0d78..e6d915e3057b 100644 --- a/src/execution/operator/projection/physical_projection.cpp +++ b/src/execution/operator/projection/physical_projection.cpp @@ -27,7 +27,7 @@ PhysicalProjection::PhysicalProjection(vector types, vector(); state.executor.Execute(input, chunk); return OperatorResultType::NEED_MORE_INPUT; } diff --git a/src/execution/operator/scan/physical_column_data_scan.cpp b/src/execution/operator/scan/physical_column_data_scan.cpp index fd0239df2676..361c2f7e3a1c 100644 --- a/src/execution/operator/scan/physical_column_data_scan.cpp +++ b/src/execution/operator/scan/physical_column_data_scan.cpp @@ -1,6 +1,7 @@ #include "duckdb/execution/operator/scan/physical_column_data_scan.hpp" #include "duckdb/execution/operator/join/physical_delim_join.hpp" +#include "duckdb/execution/operator/aggregate/physical_hash_aggregate.hpp" #include "duckdb/parallel/meta_pipeline.hpp" #include "duckdb/parallel/pipeline.hpp" @@ -60,7 +61,7 @@ void PhysicalColumnDataScan::BuildPipelines(Pipeline ¤t, MetaPipeline &met D_ASSERT(delim_sink->type == PhysicalOperatorType::DELIM_JOIN); auto &delim_join = delim_sink->Cast(); current.AddDependency(delim_dependency); - state.SetPipelineSource(current, (PhysicalOperator &)*delim_join.distinct); + state.SetPipelineSource(current, delim_join.distinct->Cast()); return; } case PhysicalOperatorType::RECURSIVE_CTE_SCAN: diff --git a/src/execution/operator/scan/physical_expression_scan.cpp b/src/execution/operator/scan/physical_expression_scan.cpp index 8eb09178aac2..5e08bc9471fc 100644 --- a/src/execution/operator/scan/physical_expression_scan.cpp +++ b/src/execution/operator/scan/physical_expression_scan.cpp @@ -22,7 +22,7 @@ unique_ptr PhysicalExpressionScan::GetOperatorState(ExecutionCont OperatorResultType PhysicalExpressionScan::Execute(ExecutionContext &context, DataChunk &input, DataChunk &chunk, GlobalOperatorState &gstate, OperatorState &state_p) const { - auto &state = (ExpressionScanState &)state_p; + auto &state = state_p.Cast(); for (; chunk.size() + input.size() <= STANDARD_VECTOR_SIZE && state.expression_index < expressions.size(); state.expression_index++) { diff --git a/src/execution/operator/scan/physical_positional_scan.cpp b/src/execution/operator/scan/physical_positional_scan.cpp index 02b1116883fe..e2a67505ae7c 100644 --- a/src/execution/operator/scan/physical_positional_scan.cpp +++ b/src/execution/operator/scan/physical_positional_scan.cpp @@ -19,7 +19,7 @@ PhysicalPositionalScan::PhysicalPositionalScan(vector types, unique if (left->type == PhysicalOperatorType::TABLE_SCAN) { child_tables.emplace_back(std::move(left)); } else if (left->type == PhysicalOperatorType::POSITIONAL_SCAN) { - auto &left_scan = (PhysicalPositionalScan &)*left; + auto &left_scan = left->Cast(); child_tables = std::move(left_scan.child_tables); } else { throw InternalException("Invalid left input for PhysicalPositionalScan"); @@ -28,7 +28,7 @@ PhysicalPositionalScan::PhysicalPositionalScan(vector types, unique if (right->type == PhysicalOperatorType::TABLE_SCAN) { child_tables.emplace_back(std::move(right)); } else if (right->type == PhysicalOperatorType::POSITIONAL_SCAN) { - auto &right_scan = (PhysicalPositionalScan &)*right; + auto &right_scan = right->Cast(); auto &right_tables = right_scan.child_tables; child_tables.reserve(child_tables.size() + right_tables.size()); std::move(right_tables.begin(), right_tables.end(), std::back_inserter(child_tables)); diff --git a/src/execution/physical_plan/plan_comparison_join.cpp b/src/execution/physical_plan/plan_comparison_join.cpp index ec658ce57c50..f3d4d1270787 100644 --- a/src/execution/physical_plan/plan_comparison_join.cpp +++ b/src/execution/physical_plan/plan_comparison_join.cpp @@ -186,7 +186,7 @@ static bool PlanIndexJoin(ClientContext &context, LogicalComparisonJoin &op, uni } // index joins are not supported if there are pushed down table filters D_ASSERT(right->type == PhysicalOperatorType::TABLE_SCAN); - auto &tbl_scan = (PhysicalTableScan &)*right; + auto &tbl_scan = right->Cast(); // if (tbl_scan.table_filters && !tbl_scan.table_filters->filters.empty()) { // return false; // } diff --git a/src/execution/physical_plan/plan_create_table.cpp b/src/execution/physical_plan/plan_create_table.cpp index c45313e58f6a..c5f809334d54 100644 --- a/src/execution/physical_plan/plan_create_table.cpp +++ b/src/execution/physical_plan/plan_create_table.cpp @@ -34,7 +34,7 @@ unique_ptr DuckCatalog::PlanCreateTableAs(ClientContext &conte } unique_ptr PhysicalPlanGenerator::CreatePlan(LogicalCreateTable &op) { - const auto &create_info = (CreateTableInfo &)*op.info->base; + const auto &create_info = op.info->base->Cast(); auto &catalog = op.info->schema.catalog; auto existing_entry = catalog.GetEntry(context, create_info.schema, create_info.table, OnEntryNotFound::RETURN_NULL); diff --git a/src/execution/physical_plan_generator.cpp b/src/execution/physical_plan_generator.cpp index b975468d136b..9877749578e5 100644 --- a/src/execution/physical_plan_generator.cpp +++ b/src/execution/physical_plan_generator.cpp @@ -207,10 +207,10 @@ unique_ptr PhysicalPlanGenerator::CreatePlan(LogicalOperator & plan = CreatePlan(op.Cast()); break; case LogicalOperatorType::LOGICAL_PIVOT: - plan = CreatePlan((LogicalPivot &)op); + plan = CreatePlan(op.Cast()); break; case LogicalOperatorType::LOGICAL_EXTENSION_OPERATOR: - plan = ((LogicalExtensionOperator &)op).CreatePlan(context, *this); + plan = op.Cast().CreatePlan(context, *this); if (!plan) { throw InternalException("Missing PhysicalOperator for Extension Operator"); diff --git a/src/execution/radix_partitioned_hashtable.cpp b/src/execution/radix_partitioned_hashtable.cpp index 0a052eab9e83..77dc303bbe63 100644 --- a/src/execution/radix_partitioned_hashtable.cpp +++ b/src/execution/radix_partitioned_hashtable.cpp @@ -408,7 +408,7 @@ SourceResultType RadixPartitionedHashTable::GetData(ExecutionContext &context, D aggr.function.initialize(aggr_state.get()); AggregateInputData aggr_input_data(aggr.bind_info.get(), Allocator::DefaultAllocator()); - Vector state_vector(Value::POINTER((uintptr_t)aggr_state.get())); + Vector state_vector(Value::POINTER(CastPointerToValue(aggr_state.get()))); aggr.function.finalize(state_vector, aggr_input_data, chunk.data[null_groups.size() + i], 1, 0); if (aggr.function.destructor) { aggr.function.destructor(state_vector, aggr_input_data, 1); diff --git a/src/execution/reservoir_sample.cpp b/src/execution/reservoir_sample.cpp index 9780fc552b7c..99ae5ebe1455 100644 --- a/src/execution/reservoir_sample.cpp +++ b/src/execution/reservoir_sample.cpp @@ -186,7 +186,7 @@ void BaseReservoirSampling::InitializeReservoir(idx_t cur_size, idx_t sample_siz //! we use a priority queue to extract the minimum key in O(1) time for (idx_t i = 0; i < sample_size; i++) { double k_i = random.NextRandom(); - reservoir_weights.push(std::make_pair(-k_i, i)); + reservoir_weights.emplace(-k_i, i); } SetNextEntry(); } @@ -217,7 +217,7 @@ void BaseReservoirSampling::ReplaceElement() { //! we generate a random number between (min_threshold, 1) double r2 = random.NextRandom(min_threshold, 1); //! now we insert the new weight into the reservoir - reservoir_weights.push(std::make_pair(-r2, min_entry)); + reservoir_weights.emplace(-r2, min_entry); //! we update the min entry with the new min entry in the reservoir SetNextEntry(); } diff --git a/src/execution/window_segment_tree.cpp b/src/execution/window_segment_tree.cpp index 02a2577823ed..495f236a09cf 100644 --- a/src/execution/window_segment_tree.cpp +++ b/src/execution/window_segment_tree.cpp @@ -12,7 +12,7 @@ namespace duckdb { WindowAggregateState::WindowAggregateState(AggregateObject aggr, const LogicalType &result_type_p) : aggr(std::move(aggr)), result_type(result_type_p), state(aggr.function.state_size()), - statev(Value::POINTER((idx_t)state.data())), statep(Value::POINTER((idx_t)state.data())) { + statev(Value::POINTER(CastPointerToValue(state.data()))), statep(Value::POINTER(CastPointerToValue(state.data()))) { statev.SetVectorType(VectorType::FLAT_VECTOR); // Prevent conversion of results to constants } @@ -180,10 +180,10 @@ void WindowConstantAggregate::Compute(Vector &target, idx_t rid, idx_t start, id //===--------------------------------------------------------------------===// // WindowSegmentTree //===--------------------------------------------------------------------===// -WindowSegmentTree::WindowSegmentTree(AggregateObject aggr, const LogicalType &result_type_p, DataChunk *input, +WindowSegmentTree::WindowSegmentTree(AggregateObject aggr_p, const LogicalType &result_type_p, DataChunk *input, const ValidityMask &filter_mask_p, WindowAggregationMode mode_p) - : aggr(std::move(aggr)), result_type(result_type_p), state(aggr.function.state_size()), - statep(Value::POINTER((idx_t)state.data())), frame(0, 0), statev(Value::POINTER((idx_t)state.data())), + : aggr(std::move(aggr_p)), result_type(result_type_p), state(aggr.function.state_size()), + statep(Value::POINTER(CastPointerToValue(state.data()))), frame(0, 0), statev(Value::POINTER(CastPointerToValue(state.data()))), internal_nodes(0), input_ref(input), filter_mask(filter_mask_p), mode(mode_p) { statep.Flatten(input->size()); statev.SetVectorType(VectorType::FLAT_VECTOR); // Prevent conversion of results to constants @@ -212,7 +212,7 @@ WindowSegmentTree::~WindowSegmentTree() { AggregateInputData aggr_input_data(aggr.GetFunctionData(), Allocator::DefaultAllocator()); // call the destructor for all the intermediate states data_ptr_t address_data[STANDARD_VECTOR_SIZE]; - Vector addresses(LogicalType::POINTER, (data_ptr_t)address_data); + Vector addresses(LogicalType::POINTER, data_ptr_cast(address_data)); idx_t count = 0; for (idx_t i = 0; i < internal_nodes; i++) { address_data[count++] = data_ptr_t(levels_flat_native.get() + i * state.size()); diff --git a/src/function/cast/decimal_cast.cpp b/src/function/cast/decimal_cast.cpp index 284a18505ef8..e10a77990bfb 100644 --- a/src/function/cast/decimal_cast.cpp +++ b/src/function/cast/decimal_cast.cpp @@ -207,7 +207,7 @@ struct DecimalCastInput { struct StringCastFromDecimalOperator { template static RESULT_TYPE Operation(INPUT_TYPE input, ValidityMask &mask, idx_t idx, void *dataptr) { - auto data = (DecimalCastInput *)dataptr; + auto data = reinterpret_cast(dataptr); return StringCastFromDecimal::Operation(input, data->width, data->scale, data->result); } }; diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index 22e7822e26a2..653dfd086014 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -95,9 +95,9 @@ static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, V } else { // create a dummy state because finalize does not understand NULLs in its input // we put the NULL back in explicitly below - bind_data.aggr.initialize((data_ptr_t)target_ptr); + bind_data.aggr.initialize(data_ptr_cast(target_ptr)); } - state_vec_ptr[i] = (data_ptr_t)target_ptr; + state_vec_ptr[i] = data_ptr_cast(target_ptr); } AggregateInputData aggr_input_data(nullptr, Allocator::DefaultAllocator()); @@ -166,7 +166,7 @@ static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Ve bind_data.aggr.combine(local_state.state_vector0, local_state.state_vector1, aggr_input_data, 1); result_ptr[i] = - StringVector::AddStringOrBlob(result, (const char *)local_state.state_buffer1.get(), bind_data.state_size); + StringVector::AddStringOrBlob(result, data_ptr_cast(local_state.state_buffer1.get()), bind_data.state_size); } } diff --git a/src/function/table/arrow_conversion.cpp b/src/function/table/arrow_conversion.cpp index f3ef468905d2..55a1a5b9b047 100644 --- a/src/function/table/arrow_conversion.cpp +++ b/src/function/table/arrow_conversion.cpp @@ -210,7 +210,7 @@ static void ArrowToDuckDBBlob(Vector &vector, ArrowArray &array, ArrowScanLocalS } } else { //! Check if last offset is higher than max uint32 - if (((uint64_t *)array.buffers[1])[array.length] > NumericLimits::Maximum()) { // LCOV_EXCL_START + if (ArrowBufferData(array, 1)[array.length] > NumericLimits::Maximum()) { // LCOV_EXCL_START throw ConversionException("DuckDB does not support Blobs over 4GB"); } // LCOV_EXCL_STOP auto offsets = ArrowBufferData(array, 1) + array.offset + scan_state.chunk_offset; @@ -437,11 +437,11 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca } case ArrowDateTimeType::MILLISECONDS: { //! convert date from nanoseconds to days - auto src_ptr = (uint64_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (uint64_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } - auto tgt_ptr = (date_t *)FlatVector::GetData(vector); + auto tgt_ptr = FlatVector::GetData(vector); for (idx_t row = 0; row < size; row++) { tgt_ptr[row] = date_t(int64_t(src_ptr[row]) / static_cast(1000 * 60 * 60 * 24)); } @@ -468,10 +468,10 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case ArrowDateTimeType::NANOSECONDS: { - auto tgt_ptr = (dtime_t *)FlatVector::GetData(vector); - auto src_ptr = (int64_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto tgt_ptr = FlatVector::GetData(vector); + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (int64_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { tgt_ptr[row].micros = src_ptr[row] / 1000; @@ -499,10 +499,10 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case ArrowDateTimeType::NANOSECONDS: { - auto tgt_ptr = (timestamp_t *)FlatVector::GetData(vector); - auto src_ptr = (int64_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto tgt_ptr = FlatVector::GetData(vector); + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (int64_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { tgt_ptr[row].value = src_ptr[row] / 1000; @@ -531,10 +531,10 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca break; } case ArrowDateTimeType::NANOSECONDS: { - auto tgt_ptr = (interval_t *)FlatVector::GetData(vector); - auto src_ptr = (int64_t *)array.buffers[1] + scan_state.chunk_offset + array.offset; + auto tgt_ptr = FlatVector::GetData(vector); + auto src_ptr = ArrowBufferData(array, 1) + scan_state.chunk_offset + array.offset; if (nested_offset != -1) { - src_ptr = (int64_t *)array.buffers[1] + nested_offset + array.offset; + src_ptr = ArrowBufferData(array, 1) + nested_offset + array.offset; } for (idx_t row = 0; row < size; row++) { tgt_ptr[row].micros = src_ptr[row] / 1000; @@ -651,7 +651,7 @@ static void ColumnArrowToDuckDB(Vector &vector, ArrowArray &array, ArrowScanLoca template static void SetSelectionVectorLoop(SelectionVector &sel, data_ptr_t indices_p, idx_t size) { - auto indices = (T *)indices_p; + auto indices = reinterpret_cast(indices_p); for (idx_t row = 0; row < size; row++) { sel.set_index(row, indices[row]); } @@ -660,7 +660,7 @@ static void SetSelectionVectorLoop(SelectionVector &sel, data_ptr_t indices_p, i template static void SetSelectionVectorLoopWithChecks(SelectionVector &sel, data_ptr_t indices_p, idx_t size) { - auto indices = (T *)indices_p; + auto indices = reinterpret_cast(indices_p); for (idx_t row = 0; row < size; row++) { if (indices[row] > NumericLimits::Maximum()) { throw ConversionException("DuckDB only supports indices that fit on an uint32"); @@ -672,7 +672,7 @@ static void SetSelectionVectorLoopWithChecks(SelectionVector &sel, data_ptr_t in template static void SetMaskedSelectionVectorLoop(SelectionVector &sel, data_ptr_t indices_p, idx_t size, ValidityMask &mask, idx_t last_element_pos) { - auto indices = (T *)indices_p; + auto indices = reinterpret_cast(indices_p); for (idx_t row = 0; row < size; row++) { if (mask.RowIsValid(row)) { sel.set_index(row, indices[row]); @@ -788,7 +788,7 @@ static void ColumnArrowToDuckDBDictionary(Vector &vector, ArrowArray &array, Arr } auto dictionary_type = arrow_convert_data[col_idx]->dictionary_type; //! Get Pointer to Indices of Dictionary - auto indices = (data_ptr_t)array.buffers[1] + + auto indices = ArrowBufferData(array, 1) + GetTypeIdSize(dictionary_type.InternalType()) * (scan_state.chunk_offset + array.offset); if (array.null_count > 0) { ValidityMask indices_validity; diff --git a/src/function/table/copy_csv.cpp b/src/function/table/copy_csv.cpp index 67f348b56f47..27b8cee5e453 100644 --- a/src/function/table/copy_csv.cpp +++ b/src/function/table/copy_csv.cpp @@ -284,6 +284,10 @@ struct GlobalWriteCSVData : public GlobalFunctionData { handle->Write((void *)data, size); } + void WriteData(const char *data, idx_t size) { + WriteData(data_ptr_cast(data), size); + } + //! Write rows void WriteRows(const_data_ptr_t data, idx_t size, const string &newline) { lock_guard flock(lock); @@ -324,7 +328,7 @@ static unique_ptr WriteCSVInitializeGlobal(ClientContext &co make_uniq(FileSystem::GetFileSystem(context), file_path, options.compression); if (!options.prefix.empty()) { - global_data->WriteData((const_data_ptr_t)options.prefix.c_str(), options.prefix.size()); + global_data->WriteData(options.prefix.c_str(), options.prefix.size()); } if (options.header) { diff --git a/src/include/duckdb/common/allocator.hpp b/src/include/duckdb/common/allocator.hpp index 77edb89826f2..3f0d5080723f 100644 --- a/src/include/duckdb/common/allocator.hpp +++ b/src/include/duckdb/common/allocator.hpp @@ -26,6 +26,17 @@ struct PrivateAllocatorData { virtual ~PrivateAllocatorData(); unique_ptr debug_info; + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; typedef data_ptr_t (*allocate_function_ptr_t)(PrivateAllocatorData *private_data, idx_t size); diff --git a/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp b/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp index a1b638f21a9a..87487e92e2a1 100644 --- a/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp +++ b/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp @@ -135,6 +135,18 @@ class PartitionedTupleData { mutex lock; shared_ptr allocators; vector> partitions; + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; } // namespace duckdb diff --git a/src/include/duckdb/execution/index/art/art.hpp b/src/include/duckdb/execution/index/art/art.hpp index 433bda7d2bba..b54ce5806d7c 100644 --- a/src/include/duckdb/execution/index/art/art.hpp +++ b/src/include/duckdb/execution/index/art/art.hpp @@ -110,13 +110,13 @@ class ART : public Index { //! Find the node with a matching key, or return nullptr if not found Node Lookup(Node node, const ARTKey &key, idx_t depth); //! Returns all row IDs belonging to a key greater (or equal) than the search key - bool SearchGreater(ARTIndexScanState *state, ARTKey &key, bool inclusive, idx_t max_count, + bool SearchGreater(ARTIndexScanState &state, ARTKey &key, bool inclusive, idx_t max_count, vector &result_ids); //! Returns all row IDs belonging to a key less (or equal) than the upper_bound - bool SearchLess(ARTIndexScanState *state, ARTKey &upper_bound, bool inclusive, idx_t max_count, + bool SearchLess(ARTIndexScanState &state, ARTKey &upper_bound, bool inclusive, idx_t max_count, vector &result_ids); //! Returns all row IDs belonging to a key within the range of lower_bound and upper_bound - bool SearchCloseRange(ARTIndexScanState *state, ARTKey &lower_bound, ARTKey &upper_bound, bool left_inclusive, + bool SearchCloseRange(ARTIndexScanState &state, ARTKey &lower_bound, ARTKey &upper_bound, bool left_inclusive, bool right_inclusive, idx_t max_count, vector &result_ids); //! Initializes a merge operation by returning a set containing the buffer count of each fixed-size allocator diff --git a/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp b/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp index 187b140cfd53..123c53bab696 100644 --- a/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp +++ b/src/include/duckdb/execution/operator/helper/physical_result_collector.hpp @@ -16,6 +16,9 @@ class PreparedStatementData; //! PhysicalResultCollector is an abstract class that is used to generate the final result of a query class PhysicalResultCollector : public PhysicalOperator { +public: + static constexpr const PhysicalOperatorType TYPE = PhysicalOperatorType::RESULT_COLLECTOR; + public: explicit PhysicalResultCollector(PreparedStatementData &data); diff --git a/src/include/duckdb/execution/physical_operator.hpp b/src/include/duckdb/execution/physical_operator.hpp index 4ddd5fbdfa6e..e6d311afb385 100644 --- a/src/include/duckdb/execution/physical_operator.hpp +++ b/src/include/duckdb/execution/physical_operator.hpp @@ -30,6 +30,9 @@ class MetaPipeline; //! PhysicalOperator is the base class of the physical operators present in the //! execution plan class PhysicalOperator { +public: + static constexpr const PhysicalOperatorType TYPE = PhysicalOperatorType::INVALID; + public: PhysicalOperator(PhysicalOperatorType type, vector types, idx_t estimated_cardinality) : type(type), types(std::move(types)), estimated_cardinality(estimated_cardinality) { diff --git a/src/include/duckdb/function/compression_function.hpp b/src/include/duckdb/function/compression_function.hpp index 2a8755947222..012929630b20 100644 --- a/src/include/duckdb/function/compression_function.hpp +++ b/src/include/duckdb/function/compression_function.hpp @@ -29,16 +29,49 @@ struct SegmentScanState; struct AnalyzeState { virtual ~AnalyzeState() { } + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct CompressionState { virtual ~CompressionState() { } + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct CompressedSegmentState { virtual ~CompressedSegmentState() { } + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct CompressionAppendState { @@ -48,6 +81,17 @@ struct CompressionAppendState { } BufferHandle handle; + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; //===--------------------------------------------------------------------===// diff --git a/src/include/duckdb/main/materialized_query_result.hpp b/src/include/duckdb/main/materialized_query_result.hpp index 041dc51db3da..334a7e991c82 100644 --- a/src/include/duckdb/main/materialized_query_result.hpp +++ b/src/include/duckdb/main/materialized_query_result.hpp @@ -17,6 +17,9 @@ namespace duckdb { class ClientContext; class MaterializedQueryResult : public QueryResult { +public: + static constexpr const QueryResultType TYPE = QueryResultType::MATERIALIZED_RESULT; + public: friend class ClientContext; //! Creates a successful query result with the specified names and types diff --git a/src/include/duckdb/main/pending_query_result.hpp b/src/include/duckdb/main/pending_query_result.hpp index 575ffce1a5a6..44de0ca7d560 100644 --- a/src/include/duckdb/main/pending_query_result.hpp +++ b/src/include/duckdb/main/pending_query_result.hpp @@ -20,6 +20,9 @@ class PreparedStatementData; class PendingQueryResult : public BaseQueryResult { friend class ClientContext; +public: + static constexpr const QueryResultType TYPE = QueryResultType::PENDING_RESULT; + public: DUCKDB_API PendingQueryResult(shared_ptr context, PreparedStatementData &statement, vector types, bool allow_stream_result); diff --git a/src/include/duckdb/main/query_result.hpp b/src/include/duckdb/main/query_result.hpp index d38d8ba6dede..1e4bede37cd5 100644 --- a/src/include/duckdb/main/query_result.hpp +++ b/src/include/duckdb/main/query_result.hpp @@ -89,6 +89,23 @@ class QueryResult : public BaseQueryResult { //! To comply, we use the following variable to store the current chunk, and it's position. CurrentChunk current_chunk; +public: + template + TARGET &Cast() { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast query result to type - query result type mismatch"); + } + return (TARGET &)*this; + } + + template + const TARGET &Cast() const { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast query result to type - query result type mismatch"); + } + return (const TARGET &)*this; + } + public: //! Returns the name of the column for the given index DUCKDB_API const string &ColumnName(idx_t index) const; diff --git a/src/include/duckdb/main/stream_query_result.hpp b/src/include/duckdb/main/stream_query_result.hpp index 69e1733be73e..0373f307ec84 100644 --- a/src/include/duckdb/main/stream_query_result.hpp +++ b/src/include/duckdb/main/stream_query_result.hpp @@ -22,6 +22,9 @@ class PreparedStatementData; class StreamQueryResult : public QueryResult { friend class ClientContext; +public: + static constexpr const QueryResultType TYPE = QueryResultType::STREAM_RESULT; + public: //! Create a successful StreamQueryResult. StreamQueryResults should always be successful initially (it makes no //! sense to stream an error). diff --git a/src/include/duckdb/planner/operator/logical_pivot.hpp b/src/include/duckdb/planner/operator/logical_pivot.hpp index c896e07c07fa..13f4e10355db 100644 --- a/src/include/duckdb/planner/operator/logical_pivot.hpp +++ b/src/include/duckdb/planner/operator/logical_pivot.hpp @@ -17,6 +17,9 @@ namespace duckdb { class LogicalPivot : public LogicalOperator { +public: + static constexpr const LogicalOperatorType TYPE = LogicalOperatorType::LOGICAL_PIVOT; + public: LogicalPivot(idx_t pivot_idx, unique_ptr plan, BoundPivotInfo info); diff --git a/src/include/duckdb/storage/storage_manager.hpp b/src/include/duckdb/storage/storage_manager.hpp index 4f039a37ba48..9bab3936ef5a 100644 --- a/src/include/duckdb/storage/storage_manager.hpp +++ b/src/include/duckdb/storage/storage_manager.hpp @@ -82,6 +82,18 @@ class StorageManager { unique_ptr wal; //! Whether or not the database is opened in read-only mode bool read_only; + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; //! Stores database in a single file. diff --git a/src/include/duckdb/storage/table/chunk_info.hpp b/src/include/duckdb/storage/table/chunk_info.hpp index 28f1567208a1..fa4ee9737d91 100644 --- a/src/include/duckdb/storage/table/chunk_info.hpp +++ b/src/include/duckdb/storage/table/chunk_info.hpp @@ -44,11 +44,31 @@ class ChunkInfo { virtual void Serialize(Serializer &serialize) = 0; static unique_ptr Deserialize(Deserializer &source); + +public: + template + TARGET &Cast() { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast chunk info to type - query result type mismatch"); + } + return (TARGET &)*this; + } + + template + const TARGET &Cast() const { + if (type != TARGET::TYPE) { + throw InternalException("Failed to cast chunk info to type - query result type mismatch"); + } + return (const TARGET &)*this; + } }; class ChunkConstantInfo : public ChunkInfo { public: - ChunkConstantInfo(idx_t start); + static constexpr const ChunkInfoType TYPE = ChunkInfoType::CONSTANT_INFO; + +public: + explicit ChunkConstantInfo(idx_t start); atomic insert_id; atomic delete_id; @@ -71,7 +91,10 @@ class ChunkConstantInfo : public ChunkInfo { class ChunkVectorInfo : public ChunkInfo { public: - ChunkVectorInfo(idx_t start); + static constexpr const ChunkInfoType TYPE = ChunkInfoType::VECTOR_INFO; + +public: + explicit ChunkVectorInfo(idx_t start); //! The transaction ids of the transactions that inserted the tuples (if any) atomic inserted[STANDARD_VECTOR_SIZE]; diff --git a/src/include/duckdb/storage/table/column_checkpoint_state.hpp b/src/include/duckdb/storage/table/column_checkpoint_state.hpp index 08da2a1838a8..5500e4b68e77 100644 --- a/src/include/duckdb/storage/table/column_checkpoint_state.hpp +++ b/src/include/duckdb/storage/table/column_checkpoint_state.hpp @@ -42,6 +42,18 @@ struct ColumnCheckpointState { virtual void FlushSegment(unique_ptr segment, idx_t segment_size); virtual void WriteDataPointers(RowGroupWriter &writer); + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct PartialBlockForCheckpoint : public PartialBlock { diff --git a/src/include/duckdb/storage/table/scan_state.hpp b/src/include/duckdb/storage/table/scan_state.hpp index 6c15fbbb4f35..f69ff68df6ea 100644 --- a/src/include/duckdb/storage/table/scan_state.hpp +++ b/src/include/duckdb/storage/table/scan_state.hpp @@ -35,11 +35,33 @@ class RowGroupSegmentTree; struct SegmentScanState { virtual ~SegmentScanState() { } + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct IndexScanState { virtual ~IndexScanState() { } + + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; typedef unordered_map buffer_handle_set_t; diff --git a/src/parallel/executor.cpp b/src/parallel/executor.cpp index 6ecab902a72c..dbde87a420b5 100644 --- a/src/parallel/executor.cpp +++ b/src/parallel/executor.cpp @@ -562,7 +562,7 @@ bool Executor::HasResultCollector() { unique_ptr Executor::GetResult() { D_ASSERT(HasResultCollector()); - auto &result_collector = (PhysicalResultCollector &)*physical_plan; + auto &result_collector = physical_plan->Cast(); D_ASSERT(result_collector.sink_state); return result_collector.GetResult(*result_collector.sink_state); } diff --git a/src/storage/arena_allocator.cpp b/src/storage/arena_allocator.cpp index 1dbb3c6dc53f..3deea346cb29 100644 --- a/src/storage/arena_allocator.cpp +++ b/src/storage/arena_allocator.cpp @@ -31,7 +31,7 @@ struct ArenaAllocatorData : public PrivateAllocatorData { }; static data_ptr_t ArenaAllocatorAllocate(PrivateAllocatorData *private_data, idx_t size) { - auto &allocator_data = (ArenaAllocatorData &)*private_data; + auto &allocator_data = private_data->Cast(); return allocator_data.allocator.Allocate(size); } @@ -41,7 +41,7 @@ static void ArenaAllocatorFree(PrivateAllocatorData *, data_ptr_t, idx_t) { static data_ptr_t ArenaAllocateReallocate(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t old_size, idx_t size) { - auto &allocator_data = (ArenaAllocatorData &)*private_data; + auto &allocator_data = private_data->Cast(); return allocator_data.allocator.Reallocate(pointer, old_size, size); } //===--------------------------------------------------------------------===// diff --git a/src/storage/buffer/block_handle.cpp b/src/storage/buffer/block_handle.cpp index c4450dc11d69..a9c9f96723c7 100644 --- a/src/storage/buffer/block_handle.cpp +++ b/src/storage/buffer/block_handle.cpp @@ -49,7 +49,7 @@ unique_ptr AllocateBlock(BlockManager &block_manager, unique_ptrtype == FileBufferType::BLOCK) { // we can reuse the buffer entirely - auto &block = (Block &)*reusable_buffer; + auto &block = reinterpret_cast(*reusable_buffer); block.id = block_id; return unique_ptr_cast(std::move(reusable_buffer)); } diff --git a/src/storage/checkpoint/write_overflow_strings_to_disk.cpp b/src/storage/checkpoint/write_overflow_strings_to_disk.cpp index e4aae2b8f997..68f3a3c944c4 100644 --- a/src/storage/checkpoint/write_overflow_strings_to_disk.cpp +++ b/src/storage/checkpoint/write_overflow_strings_to_disk.cpp @@ -33,8 +33,8 @@ void WriteOverflowStringsToDisk::WriteString(string_t string, block_id_t &result size_t compressed_size = 0; compressed_size = s.MaxCompressedLength(uncompressed_size); auto compressed_buf = make_unsafe_uniq_array(compressed_size); - s.Compress((const char *)string.GetData(), uncompressed_size, (char *)compressed_buf.get(), &compressed_size); - string_t compressed_string((const char *)compressed_buf.get(), compressed_size); + s.Compress(string.GetData(), uncompressed_size, data_ptr_cast(compressed_buf.get()), &compressed_size); + string_t compressed_string(data_ptr_cast(compressed_buf.get()), compressed_size); // store sizes auto data_ptr = handle.Ptr(); diff --git a/src/storage/checkpoint_manager.cpp b/src/storage/checkpoint_manager.cpp index 3fc2589d128f..c30f8c6acd76 100644 --- a/src/storage/checkpoint_manager.cpp +++ b/src/storage/checkpoint_manager.cpp @@ -41,7 +41,7 @@ SingleFileCheckpointWriter::SingleFileCheckpointWriter(AttachedDatabase &db, Blo } BlockManager &SingleFileCheckpointWriter::GetBlockManager() { - auto &storage_manager = (SingleFileStorageManager &)db.GetStorageManager(); + auto &storage_manager = db.GetStorageManager().Cast(); return *storage_manager.block_manager; } @@ -55,7 +55,7 @@ unique_ptr SingleFileCheckpointWriter::GetTableDataWriter(Table void SingleFileCheckpointWriter::CreateCheckpoint() { auto &config = DBConfig::Get(db); - auto &storage_manager = (SingleFileStorageManager &)db.GetStorageManager(); + auto &storage_manager = db.GetStorageManager().Cast(); if (storage_manager.InMemory()) { return; } @@ -73,7 +73,7 @@ void SingleFileCheckpointWriter::CreateCheckpoint() { vector> schemas; // we scan the set of committed schemas - auto &catalog = (DuckCatalog &)Catalog::GetCatalog(db); + auto &catalog = Catalog::GetCatalog(db).Cast(); catalog.ScanSchemas([&](SchemaCatalogEntry &entry) { schemas.push_back(entry); }); // write the actual data into the database // write the amount of schemas diff --git a/src/storage/compression/dictionary_compression.cpp b/src/storage/compression/dictionary_compression.cpp index 2a7d5ddbe447..6d0c36aa4272 100644 --- a/src/storage/compression/dictionary_compression.cpp +++ b/src/storage/compression/dictionary_compression.cpp @@ -391,12 +391,12 @@ unique_ptr DictionaryCompressionStorage::StringInitAnalyze(ColumnD } bool DictionaryCompressionStorage::StringAnalyze(AnalyzeState &state_p, Vector &input, idx_t count) { - auto &state = (DictionaryCompressionAnalyzeState &)state_p; + auto &state = state_p.Cast(); return state.analyze_state->UpdateState(input, count); } idx_t DictionaryCompressionStorage::StringFinalAnalyze(AnalyzeState &state_p) { - auto &analyze_state = (DictionaryCompressionAnalyzeState &)state_p; + auto &analyze_state = state_p.Cast(); auto &state = *analyze_state.analyze_state; auto width = BitpackingPrimitives::MinimumBitWidth(state.current_unique_count + 1); @@ -415,12 +415,12 @@ unique_ptr DictionaryCompressionStorage::InitCompression(Colum } void DictionaryCompressionStorage::Compress(CompressionState &state_p, Vector &scan_vector, idx_t count) { - auto &state = (DictionaryCompressionCompressState &)state_p; + auto &state = state_p.Cast(); state.UpdateState(scan_vector, count); } void DictionaryCompressionStorage::FinalizeCompress(CompressionState &state_p) { - auto &state = (DictionaryCompressionCompressState &)state_p; + auto &state = state_p.Cast(); state.Flush(true); } @@ -551,10 +551,10 @@ void DictionaryCompressionStorage::StringFetchRow(ColumnSegment &segment, Column auto &handle = state.GetOrInsertHandle(segment); auto baseptr = handle.Ptr() + segment.GetBlockOffset(); - auto header_ptr = (dictionary_compression_header_t *)baseptr; + auto header_ptr = reinterpret_cast(baseptr); auto dict = DictionaryCompressionStorage::GetDictionary(segment, handle); - auto index_buffer_offset = Load((data_ptr_t)&header_ptr->index_buffer_offset); - auto width = (bitpacking_width_t)(Load((data_ptr_t)&header_ptr->bitpacking_width)); + auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); + auto width = (bitpacking_width_t) Load(data_ptr_cast(&header_ptr->bitpacking_width)); auto index_buffer_ptr = (uint32_t *)(baseptr + index_buffer_offset); auto base_data = (data_ptr_t)(baseptr + DICTIONARY_HEADER_SIZE); auto result_data = FlatVector::GetData(result); @@ -595,18 +595,18 @@ idx_t DictionaryCompressionStorage::RequiredSpace(idx_t current_count, idx_t ind } StringDictionaryContainer DictionaryCompressionStorage::GetDictionary(ColumnSegment &segment, BufferHandle &handle) { - auto header_ptr = (dictionary_compression_header_t *)(handle.Ptr() + segment.GetBlockOffset()); + auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); StringDictionaryContainer container; - container.size = Load((data_ptr_t)&header_ptr->dict_size); - container.end = Load((data_ptr_t)&header_ptr->dict_end); + container.size = Load(data_ptr_cast(&header_ptr->dict_size)); + container.end = Load(data_ptr_cast(&header_ptr->dict_end)); return container; } void DictionaryCompressionStorage::SetDictionary(ColumnSegment &segment, BufferHandle &handle, StringDictionaryContainer container) { - auto header_ptr = (dictionary_compression_header_t *)(handle.Ptr() + segment.GetBlockOffset()); - Store(container.size, (data_ptr_t)&header_ptr->dict_size); - Store(container.end, (data_ptr_t)&header_ptr->dict_end); + auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); + Store(container.size, data_ptr_cast(&header_ptr->dict_size)); + Store(container.end, data_ptr_cast(&header_ptr->dict_end)); } string_t DictionaryCompressionStorage::FetchStringFromDict(ColumnSegment &segment, StringDictionaryContainer dict, @@ -621,7 +621,7 @@ string_t DictionaryCompressionStorage::FetchStringFromDict(ColumnSegment &segmen auto dict_end = baseptr + dict.end; auto dict_pos = dict_end - dict_offset; - auto str_ptr = (char *)(dict_pos); + auto str_ptr = data_ptr_cast(dict_pos); return string_t(str_ptr, string_len); } diff --git a/src/storage/compression/fixed_size_uncompressed.cpp b/src/storage/compression/fixed_size_uncompressed.cpp index 11f3a3c79e4e..50a41bfb14a1 100644 --- a/src/storage/compression/fixed_size_uncompressed.cpp +++ b/src/storage/compression/fixed_size_uncompressed.cpp @@ -28,7 +28,7 @@ unique_ptr FixedSizeInitAnalyze(ColumnData &col_data, PhysicalType } bool FixedSizeAnalyze(AnalyzeState &state_p, Vector &input, idx_t count) { - auto &state = (FixedSizeAnalyzeState &)state_p; + auto &state = state_p.Cast(); state.count += count; return true; } @@ -64,7 +64,7 @@ void UncompressedCompressState::CreateEmptySegment(idx_t row_start) { auto &type = checkpointer.GetType(); auto compressed_segment = ColumnSegment::CreateTransientSegment(db, type, row_start); if (type.InternalType() == PhysicalType::VARCHAR) { - auto &state = (UncompressedStringSegmentState &)*compressed_segment->GetSegmentState(); + auto &state = compressed_segment->GetSegmentState()->Cast(); state.overflow_writer = make_uniq(checkpointer.GetColumnData().GetBlockManager()); } current_segment = std::move(compressed_segment); @@ -87,7 +87,7 @@ unique_ptr UncompressedFunctions::InitCompression(ColumnDataCh } void UncompressedFunctions::Compress(CompressionState &state_p, Vector &data, idx_t count) { - auto &state = (UncompressedCompressState &)state_p; + auto &state = state_p.Cast(); UnifiedVectorFormat vdata; data.ToUnifiedFormat(count, vdata); @@ -110,7 +110,7 @@ void UncompressedFunctions::Compress(CompressionState &state_p, Vector &data, id } void UncompressedFunctions::FinalizeCompress(CompressionState &state_p) { - auto &state = (UncompressedCompressState &)state_p; + auto &state = state_p.Cast(); state.Finalize(state.current_segment->FinalizeAppend(state.append_state)); } @@ -147,7 +147,7 @@ void FixedSizeScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t template void FixedSizeScan(ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result) { - auto &scan_state = (FixedSizeScanState &)*state.scan_state; + auto &scan_state = state.scan_state->template Cast(); auto start = segment.GetRelativeIndex(state.row_index); auto data = scan_state.handle.Ptr() + segment.GetBlockOffset(); @@ -186,7 +186,7 @@ struct StandardFixedSizeAppend { static void Append(SegmentStatistics &stats, data_ptr_t target, idx_t target_offset, UnifiedVectorFormat &adata, idx_t offset, idx_t count) { auto sdata = UnifiedVectorFormat::GetData(adata); - auto tdata = (T *)target; + auto tdata = reinterpret_cast(target); if (!adata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { auto source_idx = adata.sel->get_index(offset + i); @@ -217,7 +217,7 @@ struct ListFixedSizeAppend { static void Append(SegmentStatistics &stats, data_ptr_t target, idx_t target_offset, UnifiedVectorFormat &adata, idx_t offset, idx_t count) { auto sdata = UnifiedVectorFormat::GetData(adata); - auto tdata = (uint64_t *)target; + auto tdata = reinterpret_cast(target); for (idx_t i = 0; i < count; i++) { auto source_idx = adata.sel->get_index(offset + i); auto target_idx = target_offset + i; diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index dadc1cc600b7..e337ad2af774 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -94,7 +94,7 @@ unique_ptr FSSTStorage::StringInitAnalyze(ColumnData &col_data, Ph } bool FSSTStorage::StringAnalyze(AnalyzeState &state_p, Vector &input, idx_t count) { - auto &state = (FSSTAnalyzeState &)state_p; + auto &state = state_p.Cast(); UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); @@ -138,7 +138,7 @@ bool FSSTStorage::StringAnalyze(AnalyzeState &state_p, Vector &input, idx_t coun } idx_t FSSTStorage::StringFinalAnalyze(AnalyzeState &state_p) { - auto &state = (FSSTAnalyzeState &)state_p; + auto &state = state_p.Cast(); size_t compressed_dict_size = 0; size_t max_compressed_string_length = 0; @@ -408,7 +408,7 @@ unique_ptr FSSTStorage::InitCompression(ColumnDataCheckpointer } void FSSTStorage::Compress(CompressionState &state_p, Vector &scan_vector, idx_t count) { - auto &state = (FSSTCompressionState &)state_p; + auto &state = state_p.Cast(); // Get vector data UnifiedVectorFormat vdata; @@ -486,7 +486,7 @@ void FSSTStorage::Compress(CompressionState &state_p, Vector &scan_vector, idx_t } void FSSTStorage::FinalizeCompress(CompressionState &state_p) { - auto &state = (FSSTCompressionState &)state_p; + auto &state = state_p.Cast(); state.Flush(true); } @@ -551,7 +551,7 @@ template void FSSTStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result, idx_t result_offset) { - auto &scan_state = (FSSTScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast(); auto start = segment.GetRelativeIndex(state.row_index); bool enable_fsst_vectors; @@ -696,16 +696,16 @@ bool FSSTFun::TypeIsSupported(PhysicalType type) { // Helper Functions //===--------------------------------------------------------------------===// void FSSTStorage::SetDictionary(ColumnSegment &segment, BufferHandle &handle, StringDictionaryContainer container) { - auto header_ptr = (fsst_compression_header_t *)(handle.Ptr() + segment.GetBlockOffset()); - Store(container.size, (data_ptr_t)&header_ptr->dict_size); - Store(container.end, (data_ptr_t)&header_ptr->dict_end); + auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); + Store(container.size, data_ptr_cast(&header_ptr->dict_size)); + Store(container.end, data_ptr_cast(&header_ptr->dict_end)); } StringDictionaryContainer FSSTStorage::GetDictionary(ColumnSegment &segment, BufferHandle &handle) { - auto header_ptr = (fsst_compression_header_t *)(handle.Ptr() + segment.GetBlockOffset()); + auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); StringDictionaryContainer container; - container.size = Load((data_ptr_t)&header_ptr->dict_size); - container.end = Load((data_ptr_t)&header_ptr->dict_end); + container.size = Load(data_ptr_cast(&header_ptr->dict_size)); + container.end = Load(data_ptr_cast(&header_ptr->dict_end)); return container; } @@ -716,15 +716,15 @@ char *FSSTStorage::FetchStringPointer(StringDictionaryContainer dict, data_ptr_t auto dict_end = baseptr + dict.end; auto dict_pos = dict_end - dict_offset; - return (char *)(dict_pos); + return data_ptr_cast(dict_pos); } // Returns false if no symbol table was found. This means all strings are either empty or null bool FSSTStorage::ParseFSSTSegmentHeader(data_ptr_t base_ptr, duckdb_fsst_decoder_t *decoder_out, bitpacking_width_t *width_out) { - auto header_ptr = (fsst_compression_header_t *)base_ptr; - auto fsst_symbol_table_offset = Load((data_ptr_t)&header_ptr->fsst_symbol_table_offset); - *width_out = (bitpacking_width_t)(Load((data_ptr_t)&header_ptr->bitpacking_width)); + auto header_ptr = reinterpret_cast(base_ptr); + auto fsst_symbol_table_offset = Load(data_ptr_cast(&header_ptr->fsst_symbol_table_offset)); + *width_out = (bitpacking_width_t)(Load(data_ptr_cast(&header_ptr->bitpacking_width))); return duckdb_fsst_import(decoder_out, base_ptr + fsst_symbol_table_offset); } diff --git a/src/storage/compression/rle.cpp b/src/storage/compression/rle.cpp index f4ceec62e279..f02e3cd44d56 100644 --- a/src/storage/compression/rle.cpp +++ b/src/storage/compression/rle.cpp @@ -94,7 +94,7 @@ unique_ptr RLEInitAnalyze(ColumnData &col_data, PhysicalType type) template bool RLEAnalyze(AnalyzeState &state, Vector &input, idx_t count) { - auto &rle_state = (RLEAnalyzeState &)state; + auto &rle_state = state.template Cast>(); UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); @@ -108,7 +108,7 @@ bool RLEAnalyze(AnalyzeState &state, Vector &input, idx_t count) { template idx_t RLEFinalAnalyze(AnalyzeState &state) { - auto &rle_state = (RLEAnalyzeState &)state; + auto &rle_state = state.template Cast>(); return (sizeof(rle_count_t) + sizeof(T)) * rle_state.state.seen_count; } @@ -124,7 +124,7 @@ struct RLECompressState : public CompressionState { struct RLEWriter { template static void Operation(VALUE_TYPE value, rle_count_t count, void *dataptr, bool is_null) { - auto state = (RLECompressState *)dataptr; + auto state = reinterpret_cast *>(dataptr); state->WriteValue(value, count, is_null); } }; @@ -289,14 +289,14 @@ unique_ptr RLEInitScan(ColumnSegment &segment) { //===--------------------------------------------------------------------===// template void RLESkip(ColumnSegment &segment, ColumnScanState &state, idx_t skip_count) { - auto &scan_state = (RLEScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast>(); scan_state.Skip(segment, skip_count); } template void RLEScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result, idx_t result_offset) { - auto &scan_state = (RLEScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast>(); auto data = scan_state.handle.Ptr() + segment.GetBlockOffset(); auto data_pointer = (T *)(data + RLEConstants::RLE_HEADER_SIZE); diff --git a/src/storage/compression/string_uncompressed.cpp b/src/storage/compression/string_uncompressed.cpp index e4469a5f6830..b388e603782e 100644 --- a/src/storage/compression/string_uncompressed.cpp +++ b/src/storage/compression/string_uncompressed.cpp @@ -33,7 +33,7 @@ unique_ptr UncompressedStringStorage::StringInitAnalyze(ColumnData } bool UncompressedStringStorage::StringAnalyze(AnalyzeState &state_p, Vector &input, idx_t count) { - auto &state = (StringAnalyzeState &)state_p; + auto &state = state_p.Cast(); UnifiedVectorFormat vdata; input.ToUnifiedFormat(count, vdata); @@ -53,7 +53,7 @@ bool UncompressedStringStorage::StringAnalyze(AnalyzeState &state_p, Vector &inp } idx_t UncompressedStringStorage::StringFinalAnalyze(AnalyzeState &state_p) { - auto &state = (StringAnalyzeState &)state_p; + auto &state = state_p.Cast(); return state.count * sizeof(int32_t) + state.total_string_size + state.overflow_strings * BIG_STRING_MARKER_SIZE; } @@ -124,7 +124,7 @@ void UncompressedStringStorage::StringFetchRow(ColumnSegment &segment, ColumnFet auto baseptr = handle.Ptr() + segment.GetBlockOffset(); auto dict = GetDictionary(segment, handle); - auto base_data = (int32_t *)(baseptr + DICTIONARY_HEADER_SIZE); + auto base_data = reinterpret_cast(baseptr + DICTIONARY_HEADER_SIZE); auto result_data = FlatVector::GetData(result); auto dict_offset = base_data[row_id]; @@ -223,7 +223,7 @@ idx_t UncompressedStringStorage::RemainingSpace(ColumnSegment &segment, BufferHa void UncompressedStringStorage::WriteString(ColumnSegment &segment, string_t string, block_id_t &result_block, int32_t &result_offset) { - auto &state = (UncompressedStringSegmentState &)*segment.GetSegmentState(); + auto &state = segment.GetSegmentState()->Cast(); if (state.overflow_writer) { // overflow writer is set: write string there state.overflow_writer->WriteString(string, result_block, result_offset); @@ -240,7 +240,7 @@ void UncompressedStringStorage::WriteStringMemory(ColumnSegment &segment, string BufferHandle handle; auto &buffer_manager = BufferManager::GetBufferManager(segment.db); - auto &state = (UncompressedStringSegmentState &)*segment.GetSegmentState(); + auto &state = segment.GetSegmentState()->Cast(); // check if the string fits in the current block if (!state.head || state.head->offset + total_length >= state.head->size) { // string does not fit, allocate space for it @@ -278,7 +278,7 @@ string_t UncompressedStringStorage::ReadOverflowString(ColumnSegment &segment, V auto &block_manager = segment.GetBlockManager(); auto &buffer_manager = block_manager.buffer_manager; - auto &state = (UncompressedStringSegmentState &)*segment.GetSegmentState(); + auto &state = segment.GetSegmentState()->Cast(); if (block < MAXIMUM_BLOCK) { // read the overflow string from disk // pin the initial handle and read the length @@ -345,14 +345,14 @@ string_t UncompressedStringStorage::ReadOverflowString(ColumnSegment &segment, V string_t UncompressedStringStorage::ReadString(data_ptr_t target, int32_t offset, uint32_t string_length) { auto ptr = target + offset; - auto str_ptr = (char *)(ptr); + auto str_ptr = data_ptr_cast(ptr); return string_t(str_ptr, string_length); } string_t UncompressedStringStorage::ReadStringWithLength(data_ptr_t target, int32_t offset) { auto ptr = target + offset; auto str_length = Load(ptr); - auto str_ptr = (char *)(ptr + sizeof(uint32_t)); + auto str_ptr = data_ptr_cast(ptr + sizeof(uint32_t)); return string_t(str_ptr, str_length); } @@ -403,7 +403,7 @@ string_t UncompressedStringStorage::FetchString(ColumnSegment &segment, StringDi auto dict_end = baseptr + dict.end; auto dict_pos = dict_end - location.offset; - auto str_ptr = (char *)(dict_pos); + auto str_ptr = data_ptr_cast(dict_pos); return string_t(str_ptr, string_length); } } diff --git a/src/storage/compression/validity_uncompressed.cpp b/src/storage/compression/validity_uncompressed.cpp index 359820b6786f..c458cbff8626 100644 --- a/src/storage/compression/validity_uncompressed.cpp +++ b/src/storage/compression/validity_uncompressed.cpp @@ -188,13 +188,13 @@ unique_ptr ValidityInitAnalyze(ColumnData &col_data, PhysicalType } bool ValidityAnalyze(AnalyzeState &state_p, Vector &input, idx_t count) { - auto &state = (ValidityAnalyzeState &)state_p; + auto &state = state_p.Cast(); state.count += count; return true; } idx_t ValidityFinalAnalyze(AnalyzeState &state_p) { - auto &state = (ValidityAnalyzeState &)state_p; + auto &state = state_p.Cast(); return (state.count + 7) / 8; } @@ -222,7 +222,7 @@ void ValidityScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t s auto start = segment.GetRelativeIndex(state.row_index); static_assert(sizeof(validity_t) == sizeof(uint64_t), "validity_t should be 64-bit"); - auto &scan_state = (ValidityScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast(); auto &result_mask = FlatVector::Validity(result); auto buffer_ptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); @@ -345,7 +345,7 @@ void ValidityScan(ColumnSegment &segment, ColumnScanState &state, idx_t scan_cou auto start = segment.GetRelativeIndex(state.row_index); if (start % ValidityMask::BITS_PER_VALUE == 0) { - auto &scan_state = (ValidityScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast(); // aligned scan: no need to do anything fancy // note: this is only an optimization which avoids having to do messy bitshifting in the common case diff --git a/src/storage/standard_buffer_manager.cpp b/src/storage/standard_buffer_manager.cpp index 3f3933a6846b..96bec25ab459 100644 --- a/src/storage/standard_buffer_manager.cpp +++ b/src/storage/standard_buffer_manager.cpp @@ -751,7 +751,7 @@ void StandardBufferManager::FreeReservedMemory(idx_t size) { // Buffer Allocator //===--------------------------------------------------------------------===// data_ptr_t StandardBufferManager::BufferAllocatorAllocate(PrivateAllocatorData *private_data, idx_t size) { - auto &data = (BufferAllocatorData &)*private_data; + auto &data = private_data->Cast(); auto reservation = data.manager.EvictBlocksOrThrow(size, nullptr, "failed to allocate data of size %s%s", StringUtil::BytesToHumanReadableString(size)); // We rely on manual tracking of this one. :( @@ -760,7 +760,7 @@ data_ptr_t StandardBufferManager::BufferAllocatorAllocate(PrivateAllocatorData * } void StandardBufferManager::BufferAllocatorFree(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t size) { - auto &data = (BufferAllocatorData &)*private_data; + auto &data = private_data->Cast(); BufferPoolReservation r(data.manager.GetBufferPool()); r.size = size; r.Resize(0); @@ -772,7 +772,7 @@ data_ptr_t StandardBufferManager::BufferAllocatorRealloc(PrivateAllocatorData *p if (old_size == size) { return pointer; } - auto &data = (BufferAllocatorData &)*private_data; + auto &data = private_data->Cast(); BufferPoolReservation r(data.manager.GetBufferPool()); r.size = old_size; r.Resize(size); diff --git a/src/storage/statistics/string_stats.cpp b/src/storage/statistics/string_stats.cpp index dc596bca35b8..9042611dedad 100644 --- a/src/storage/statistics/string_stats.cpp +++ b/src/storage/statistics/string_stats.cpp @@ -116,7 +116,7 @@ static void ConstructValue(const_data_ptr_t data, idx_t size, data_t target[]) { } void StringStats::Update(BaseStatistics &stats, const string_t &value) { - auto data = (const_data_ptr_t)value.GetData(); + auto data = data_ptr_cast(value.GetData()); auto size = value.GetSize(); //! we can only fit 8 bytes, so we might need to trim our string @@ -136,12 +136,12 @@ void StringStats::Update(BaseStatistics &stats, const string_t &value) { string_data.max_string_length = size; } if (stats.GetType().id() == LogicalTypeId::VARCHAR && !string_data.has_unicode) { - auto unicode = Utf8Proc::Analyze((const char *)data, size); + auto unicode = Utf8Proc::Analyze(data_ptr_cast(data), size); if (unicode == UnicodeType::UNICODE) { string_data.has_unicode = true; } else if (unicode == UnicodeType::INVALID) { throw InvalidInputException( - ErrorManager::InvalidUnicodeError(string((char *)data, size), "segment statistics update")); + ErrorManager::InvalidUnicodeError(string(data_ptr_cast(data), size), "segment statistics update")); } } } @@ -220,8 +220,8 @@ string StringStats::ToString(const BaseStatistics &stats) { idx_t min_len = GetValidMinMaxSubstring(string_data.min); idx_t max_len = GetValidMinMaxSubstring(string_data.max); return StringUtil::Format( - "[Min: %s, Max: %s, Has Unicode: %s, Max String Length: %s]", string((const char *)string_data.min, min_len), - string((const char *)string_data.max, max_len), string_data.has_unicode ? "true" : "false", + "[Min: %s, Max: %s, Has Unicode: %s, Max String Length: %s]", string(data_ptr_cast(string_data.min), min_len), + string(data_ptr_cast(string_data.max), max_len), string_data.has_unicode ? "true" : "false", string_data.has_max_string_length ? to_string(string_data.max_string_length) : "?"); } diff --git a/src/storage/table/column_segment.cpp b/src/storage/table/column_segment.cpp index fc1c9242f00a..a0d560534cd6 100644 --- a/src/storage/table/column_segment.cpp +++ b/src/storage/table/column_segment.cpp @@ -331,7 +331,7 @@ idx_t ColumnSegment::FilterSelection(SelectionVector &sel, Vector &result, const // similar to the CONJUNCTION_AND, but we need to take care of the SelectionVectors (OR all of them) idx_t count_total = 0; SelectionVector result_sel(approved_tuple_count); - auto &conjunction_or = (const ConjunctionOrFilter &)filter; + auto &conjunction_or = filter.Cast(); for (auto &child_filter : conjunction_or.child_filters) { SelectionVector temp_sel; temp_sel.Initialize(sel); @@ -357,14 +357,14 @@ idx_t ColumnSegment::FilterSelection(SelectionVector &sel, Vector &result, const return approved_tuple_count; } case TableFilterType::CONJUNCTION_AND: { - auto &conjunction_and = (const ConjunctionAndFilter &)filter; + auto &conjunction_and = filter.Cast(); for (auto &child_filter : conjunction_and.child_filters) { FilterSelection(sel, result, *child_filter, approved_tuple_count, mask); } return approved_tuple_count; } case TableFilterType::CONSTANT_COMPARISON: { - auto &constant_filter = (const ConstantFilter &)filter; + auto &constant_filter = filter.Cast(); // the inplace loops take the result as the last parameter switch (result.GetType().InternalType()) { case PhysicalType::UINT8: { diff --git a/src/storage/table/list_column_data.cpp b/src/storage/table/list_column_data.cpp index 48d8fdaca86c..c388cd9b9f8e 100644 --- a/src/storage/table/list_column_data.cpp +++ b/src/storage/table/list_column_data.cpp @@ -336,7 +336,7 @@ unique_ptr ListColumnData::Checkpoint(RowGroup &row_group auto base_state = ColumnData::Checkpoint(row_group, partial_block_manager, checkpoint_info); auto child_state = child_column->Checkpoint(row_group, partial_block_manager, checkpoint_info); - auto &checkpoint_state = (ListColumnCheckpointState &)*base_state; + auto &checkpoint_state = base_state->Cast(); checkpoint_state.validity_state = std::move(validity_state); checkpoint_state.child_state = std::move(child_state); return base_state; diff --git a/src/storage/table/row_group.cpp b/src/storage/table/row_group.cpp index b380518d1f0b..8317913fba4d 100644 --- a/src/storage/table/row_group.cpp +++ b/src/storage/table/row_group.cpp @@ -502,7 +502,7 @@ void RowGroup::TemplatedScan(TransactionData transaction, CollectionScanState &s if (column == COLUMN_IDENTIFIER_ROW_ID) { D_ASSERT(result.data[i].GetType().InternalType() == PhysicalType::INT64); result.data[i].SetVectorType(VectorType::FLAT_VECTOR); - auto result_data = (int64_t *)FlatVector::GetData(result.data[i]); + auto result_data = FlatVector::GetData(result.data[i]); for (size_t sel_idx = 0; sel_idx < approved_tuple_count; sel_idx++) { result_data[sel_idx] = this->start + current_row + sel.get_index(sel_idx); } @@ -650,7 +650,7 @@ void RowGroup::AppendVersionInfo(TransactionData transaction, idx_t count) { } else { D_ASSERT(version_info->info[vector_idx]->type == ChunkInfoType::VECTOR_INFO); // use existing vector - info = (ChunkVectorInfo *)version_info->info[vector_idx].get(); + info = &version_info->info[vector_idx]->Cast(); } info->Append(start, end, transaction.transaction_id); } @@ -985,7 +985,7 @@ void VersionDeleteState::Delete(row_t row_id) { info.version_info->info[vector_idx] = make_uniq(info.start + vector_idx * STANDARD_VECTOR_SIZE); } else if (info.version_info->info[vector_idx]->type == ChunkInfoType::CONSTANT_INFO) { - auto &constant = (ChunkConstantInfo &)*info.version_info->info[vector_idx]; + auto &constant = info.version_info->info[vector_idx]->Cast(); // info exists but it's a constant info: convert to a vector info auto new_info = make_uniq(info.start + vector_idx * STANDARD_VECTOR_SIZE); new_info->insert_id = constant.insert_id.load(); @@ -995,7 +995,7 @@ void VersionDeleteState::Delete(row_t row_id) { info.version_info->info[vector_idx] = std::move(new_info); } D_ASSERT(info.version_info->info[vector_idx]->type == ChunkInfoType::VECTOR_INFO); - current_info = (ChunkVectorInfo *)info.version_info->info[vector_idx].get(); + current_info = &info.version_info->info[vector_idx]->Cast(); current_chunk = vector_idx; chunk_row = vector_idx * STANDARD_VECTOR_SIZE; } diff --git a/src/storage/table/standard_column_data.cpp b/src/storage/table/standard_column_data.cpp index 0e8606d13a1b..ecda8b0f4b79 100644 --- a/src/storage/table/standard_column_data.cpp +++ b/src/storage/table/standard_column_data.cpp @@ -189,7 +189,7 @@ unique_ptr StandardColumnData::Checkpoint(RowGroup &row_g ColumnCheckpointInfo &checkpoint_info) { auto validity_state = validity.Checkpoint(row_group, partial_block_manager, checkpoint_info); auto base_state = ColumnData::Checkpoint(row_group, partial_block_manager, checkpoint_info); - auto &checkpoint_state = (StandardColumnCheckpointState &)*base_state; + auto &checkpoint_state = base_state->Cast(); checkpoint_state.validity_state = std::move(validity_state); return base_state; } diff --git a/src/storage/wal_replay.cpp b/src/storage/wal_replay.cpp index d0d201b7b416..00f20c646d43 100644 --- a/src/storage/wal_replay.cpp +++ b/src/storage/wal_replay.cpp @@ -486,7 +486,7 @@ void ReplayState::ReplayDelete() { D_ASSERT(chunk.ColumnCount() == 1 && chunk.data[0].GetType() == LogicalType::ROW_TYPE); row_t row_ids[1]; - Vector row_identifiers(LogicalType::ROW_TYPE, (data_ptr_t)row_ids); + Vector row_identifiers(LogicalType::ROW_TYPE, data_ptr_cast(row_ids)); auto source_ids = FlatVector::GetData(chunk.data[0]); // delete the tuples from the current table diff --git a/src/transaction/duck_transaction_manager.cpp b/src/transaction/duck_transaction_manager.cpp index b027d0978f36..53ed9ec8d04e 100644 --- a/src/transaction/duck_transaction_manager.cpp +++ b/src/transaction/duck_transaction_manager.cpp @@ -61,7 +61,7 @@ DuckTransactionManager &DuckTransactionManager::Get(AttachedDatabase &db) { if (!transaction_manager.IsDuckTransactionManager()) { throw InternalException("Calling DuckTransactionManager::Get on non-DuckDB transaction manager"); } - return (DuckTransactionManager &)transaction_manager; + return reinterpret_cast(transaction_manager); } Transaction *DuckTransactionManager::StartTransaction(ClientContext &context) { From 2194b6f0732fd61983fe3c1efd74d5ea14978494 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 11:39:19 +0200 Subject: [PATCH 12/28] data_ptr_cast --- extension/jemalloc/jemalloc-extension.cpp | 4 +- extension/json/include/json_common.hpp | 2 +- .../parquet/include/resizable_buffer.hpp | 2 +- extension/parquet/parquet_statistics.cpp | 37 ++++++----- extension/parquet/parquet_timestamp.cpp | 4 +- extension/parquet/zstd_file_system.cpp | 6 +- src/common/arrow/arrow_appender.cpp | 2 +- src/common/compressed_file_system.cpp | 2 +- src/common/crypto/md5.cpp | 4 +- src/common/field_writer.cpp | 2 +- src/common/fsst.cpp | 4 +- src/common/gzip_file_system.cpp | 12 ++-- src/common/operator/cast_operators.cpp | 2 +- src/common/row_operations/row_heap_gather.cpp | 2 +- .../row_operations/row_heap_scatter.cpp | 4 +- src/common/row_operations/row_scatter.cpp | 2 +- src/common/serializer.cpp | 2 +- src/common/serializer/binary_deserializer.cpp | 2 +- src/common/sort/comparators.cpp | 2 +- src/common/types/bit.cpp | 10 +-- src/common/types/blob.cpp | 16 ++--- .../types/column/column_data_allocator.cpp | 4 +- src/common/types/hyperloglog.cpp | 12 ++-- src/common/types/list_segment.cpp | 66 +++++++++---------- .../types/row/tuple_data_scatter_gather.cpp | 2 +- src/common/types/string_heap.cpp | 2 +- src/common/types/vector.cpp | 8 +-- src/core_functions/scalar/blob/base64.cpp | 2 +- .../scalar/string/starts_with.cpp | 4 +- .../scalar/string/string_split.cpp | 4 +- src/core_functions/scalar/string/trim.cpp | 2 +- src/execution/expression_executor.cpp | 2 +- .../aggregate/physical_streaming_window.cpp | 2 +- .../operator/join/physical_asof_join.cpp | 2 +- .../operator/join/physical_index_join.cpp | 2 +- .../persistent/buffered_csv_reader.cpp | 2 +- .../operator/persistent/csv_file_handle.cpp | 4 +- src/execution/window_segment_tree.cpp | 2 +- src/function/scalar/string/contains.cpp | 8 +-- src/function/scalar/string/like.cpp | 6 +- .../scalar/system/aggregate_export.cpp | 10 +-- src/function/table/arrow_conversion.cpp | 2 +- src/function/table/copy_csv.cpp | 22 +++---- src/include/duckdb/common/allocator.hpp | 8 +-- .../duckdb/common/arrow/arrow_buffer.hpp | 4 +- src/include/duckdb/common/serializer.hpp | 2 +- .../common/serializer/binary_deserializer.hpp | 2 +- src/include/duckdb/common/typedefs.hpp | 30 ++++++--- src/parser/parser.cpp | 2 +- src/planner/binder/statement/bind_update.cpp | 2 +- .../write_overflow_strings_to_disk.cpp | 4 +- src/storage/compression/bitpacking.cpp | 10 +-- .../compression/dictionary_compression.cpp | 48 +++++++------- src/storage/compression/fsst.cpp | 26 ++++---- .../compression/string_uncompressed.cpp | 6 +- src/storage/data_table.cpp | 2 +- src/storage/single_file_block_manager.cpp | 2 +- src/storage/statistics/string_stats.cpp | 10 +-- src/storage/table/chunk_info.cpp | 4 +- src/storage/table/list_column_data.cpp | 2 +- src/storage/table/update_segment.cpp | 4 +- src/storage/wal_replay.cpp | 2 +- src/transaction/cleanup_state.cpp | 2 +- src/transaction/commit_state.cpp | 4 +- src/transaction/duck_transaction.cpp | 4 +- test/api/test_custom_allocator.cpp | 4 +- .../loadable_extension_optimizer_demo.cpp | 2 +- test/extension/test_remote_optimizer.cpp | 2 +- third_party/pcg/pcg_extras.hpp | 2 +- tools/pythonpkg/src/numpy/array_wrapper.cpp | 4 +- tools/pythonpkg/src/numpy/numpy_scan.cpp | 4 +- tools/rpkg/src/scan.cpp | 24 +++---- tools/rpkg/src/transform.cpp | 4 +- 73 files changed, 267 insertions(+), 252 deletions(-) diff --git a/extension/jemalloc/jemalloc-extension.cpp b/extension/jemalloc/jemalloc-extension.cpp index 2f9e1374ebdd..26f46500f333 100644 --- a/extension/jemalloc/jemalloc-extension.cpp +++ b/extension/jemalloc/jemalloc-extension.cpp @@ -15,7 +15,7 @@ std::string JEMallocExtension::Name() { } data_ptr_t JEMallocExtension::Allocate(PrivateAllocatorData *private_data, idx_t size) { - return (data_ptr_t)duckdb_jemalloc::je_malloc(size); + return data_ptr_cast(duckdb_jemalloc::je_malloc(size)); } void JEMallocExtension::Free(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t size) { @@ -24,7 +24,7 @@ void JEMallocExtension::Free(PrivateAllocatorData *private_data, data_ptr_t poin data_ptr_t JEMallocExtension::Reallocate(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t old_size, idx_t size) { - return (data_ptr_t)duckdb_jemalloc::je_realloc(pointer, size); + return data_ptr_cast(duckdb_jemalloc::je_realloc(pointer, size)); } } // namespace duckdb diff --git a/extension/json/include/json_common.hpp b/extension/json/include/json_common.hpp index 549a781932b2..a715fb4e58a9 100644 --- a/extension/json/include/json_common.hpp +++ b/extension/json/include/json_common.hpp @@ -38,7 +38,7 @@ class JSONAllocator { static inline void *Reallocate(void *ctx, void *ptr, size_t old_size, size_t size) { auto alloc = (ArenaAllocator *)ctx; - return alloc->ReallocateAligned((data_ptr_t)ptr, old_size, size); + return alloc->ReallocateAligned(data_ptr_cast(ptr), old_size, size); } static inline void Free(void *ctx, void *ptr) { diff --git a/extension/parquet/include/resizable_buffer.hpp b/extension/parquet/include/resizable_buffer.hpp index 2fde1777726e..b2d2b963c539 100644 --- a/extension/parquet/include/resizable_buffer.hpp +++ b/extension/parquet/include/resizable_buffer.hpp @@ -41,7 +41,7 @@ class ByteBuffer { // on to the 10 thousandth impl template T get() { available(sizeof(T)); - T val = Load((data_ptr_t)ptr); + T val = Load(data_ptr_cast(ptr)); return val; } diff --git a/extension/parquet/parquet_statistics.cpp b/extension/parquet/parquet_statistics.cpp index 178780308da3..20aa93f28432 100644 --- a/extension/parquet/parquet_statistics.cpp +++ b/extension/parquet/parquet_statistics.cpp @@ -48,12 +48,13 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, if (stats.empty()) { return Value(); } + auto stats_data = const_data_ptr_cast(stats.c_str()); switch (type.id()) { case LogicalTypeId::BOOLEAN: { if (stats.size() != sizeof(bool)) { throw InternalException("Incorrect stats size for type BOOLEAN"); } - return Value::BOOLEAN(Load((data_ptr_t)stats.c_str())); + return Value::BOOLEAN(Load(stats_data)); } case LogicalTypeId::UTINYINT: case LogicalTypeId::USMALLINT: @@ -61,29 +62,29 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, if (stats.size() != sizeof(uint32_t)) { throw InternalException("Incorrect stats size for type UINTEGER"); } - return Value::UINTEGER(Load((data_ptr_t)stats.c_str())); + return Value::UINTEGER(Load(stats_data)); case LogicalTypeId::UBIGINT: if (stats.size() != sizeof(uint64_t)) { throw InternalException("Incorrect stats size for type UBIGINT"); } - return Value::UBIGINT(Load((data_ptr_t)stats.c_str())); + return Value::UBIGINT(Load(stats_data)); case LogicalTypeId::TINYINT: case LogicalTypeId::SMALLINT: case LogicalTypeId::INTEGER: if (stats.size() != sizeof(int32_t)) { throw InternalException("Incorrect stats size for type INTEGER"); } - return Value::INTEGER(Load((data_ptr_t)stats.c_str())); + return Value::INTEGER(Load(stats_data)); case LogicalTypeId::BIGINT: if (stats.size() != sizeof(int64_t)) { throw InternalException("Incorrect stats size for type BIGINT"); } - return Value::BIGINT(Load((data_ptr_t)stats.c_str())); + return Value::BIGINT(Load(stats_data)); case LogicalTypeId::FLOAT: { if (stats.size() != sizeof(float)) { throw InternalException("Incorrect stats size for type FLOAT"); } - auto val = Load((data_ptr_t)stats.c_str()); + auto val = Load(stats_data); if (!Value::FloatIsFinite(val)) { return Value(); } @@ -93,7 +94,7 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, if (stats.size() != sizeof(double)) { throw InternalException("Incorrect stats size for type DOUBLE"); } - auto val = Load((data_ptr_t)stats.c_str()); + auto val = Load(stats_data); if (!Value::DoubleIsFinite(val)) { return Value(); } @@ -107,13 +108,13 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, if (stats.size() != sizeof(int32_t)) { throw InternalException("Incorrect stats size for type %s", type.ToString()); } - return Value::DECIMAL(Load((data_ptr_t)stats.c_str()), width, scale); + return Value::DECIMAL(Load(stats_data), width, scale); } case Type::INT64: { if (stats.size() != sizeof(int64_t)) { throw InternalException("Incorrect stats size for type %s", type.ToString()); } - return Value::DECIMAL(Load((data_ptr_t)stats.c_str()), width, scale); + return Value::DECIMAL(Load(stats_data), width, scale); } case Type::BYTE_ARRAY: case Type::FIXED_LEN_BYTE_ARRAY: @@ -123,19 +124,19 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, switch (type.InternalType()) { case PhysicalType::INT16: return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue((const_data_ptr_t)stats.c_str(), stats.size()), + ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, scale); case PhysicalType::INT32: return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue((const_data_ptr_t)stats.c_str(), stats.size()), + ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, scale); case PhysicalType::INT64: return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue((const_data_ptr_t)stats.c_str(), stats.size()), + ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, scale); case PhysicalType::INT128: return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue((const_data_ptr_t)stats.c_str(), stats.size()), + ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, scale); default: throw InternalException("Unsupported internal type for decimal"); @@ -155,14 +156,14 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, if (stats.size() != sizeof(int32_t)) { throw InternalException("Incorrect stats size for type DATE"); } - return Value::DATE(date_t(Load((data_ptr_t)stats.c_str()))); + return Value::DATE(date_t(Load(stats_data))); case LogicalTypeId::TIME: case LogicalTypeId::TIME_TZ: { int64_t val; if (stats.size() == sizeof(int32_t)) { - val = Load((data_ptr_t)stats.c_str()); + val = Load(stats_data); } else if (stats.size() == sizeof(int64_t)) { - val = Load((data_ptr_t)stats.c_str()); + val = Load(stats_data); } else { throw InternalException("Incorrect stats size for type TIME"); } @@ -190,13 +191,13 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, if (stats.size() != sizeof(Int96)) { throw InternalException("Incorrect stats size for type TIMESTAMP"); } - return Value::TIMESTAMP(ImpalaTimestampToTimestamp(Load((data_ptr_t)stats.c_str()))); + return Value::TIMESTAMP(ImpalaTimestampToTimestamp(Load(stats_data))); } else { D_ASSERT(schema_ele.type == Type::INT64); if (stats.size() != sizeof(int64_t)) { throw InternalException("Incorrect stats size for type TIMESTAMP"); } - auto val = Load((data_ptr_t)stats.c_str()); + auto val = Load(stats_data); if (schema_ele.__isset.logicalType && schema_ele.logicalType.__isset.TIMESTAMP) { // logical type if (schema_ele.logicalType.TIMESTAMP.unit.__isset.MILLIS) { diff --git a/extension/parquet/parquet_timestamp.cpp b/extension/parquet/parquet_timestamp.cpp index 42b6f83c85e2..67db03486068 100644 --- a/extension/parquet/parquet_timestamp.cpp +++ b/extension/parquet/parquet_timestamp.cpp @@ -17,7 +17,7 @@ static constexpr int64_t NANOSECONDS_PER_MICRO = 1000LL; static int64_t ImpalaTimestampToMicroseconds(const Int96 &impala_timestamp) { int64_t days_since_epoch = impala_timestamp.value[2] - JULIAN_TO_UNIX_EPOCH_DAYS; - auto nanoseconds = Load((data_ptr_t)impala_timestamp.value); + auto nanoseconds = Load(const_data_ptr_cast(impala_timestamp.value)); auto microseconds = nanoseconds / NANOSECONDS_PER_MICRO; return days_since_epoch * MICROSECONDS_PER_DAY + microseconds; } @@ -35,7 +35,7 @@ Int96 TimestampToImpalaTimestamp(timestamp_t &ts) { // first two uint32 in Int96 are nanoseconds since midnights // last uint32 is number of days since year 4713 BC ("Julian date") Int96 impala_ts; - Store(ms_since_midnight * 1000000, (data_ptr_t)impala_ts.value); + Store(ms_since_midnight * 1000000, data_ptr_cast(impala_ts.value)); impala_ts.value[2] = days_since_epoch + JULIAN_TO_UNIX_EPOCH_DAYS; return impala_ts; } diff --git a/extension/parquet/zstd_file_system.cpp b/extension/parquet/zstd_file_system.cpp index 67cf543e8ae8..a6e8b3099e5d 100644 --- a/extension/parquet/zstd_file_system.cpp +++ b/extension/parquet/zstd_file_system.cpp @@ -61,9 +61,9 @@ bool ZstdStreamWrapper::Read(StreamData &sd) { throw IOException(duckdb_zstd::ZSTD_getErrorName(res)); } - sd.in_buff_start = (data_ptr_t)in_buffer.src + in_buffer.pos; - sd.in_buff_end = (data_ptr_t)in_buffer.src + in_buffer.size; - sd.out_buff_end = (data_ptr_t)out_buffer.dst + out_buffer.pos; + sd.in_buff_start = (data_ptr_t)in_buffer.src + in_buffer.pos; // NOLINT + sd.in_buff_end = (data_ptr_t)in_buffer.src + in_buffer.size; // NOLINT + sd.out_buff_end = (data_ptr_t)out_buffer.dst + out_buffer.pos; // NOLINT return false; } diff --git a/src/common/arrow/arrow_appender.cpp b/src/common/arrow/arrow_appender.cpp index a9beab206f11..091c291e8ab3 100644 --- a/src/common/arrow/arrow_appender.cpp +++ b/src/common/arrow/arrow_appender.cpp @@ -318,7 +318,7 @@ struct ArrowUUIDConverter { template static void WriteData(data_ptr_t target, SRC input) { - UUID::ToString(input, data_ptr_cast(target)); + UUID::ToString(input, char_ptr_cast(target)); } }; diff --git a/src/common/compressed_file_system.cpp b/src/common/compressed_file_system.cpp index b2660a5706a9..a9d2893307e7 100644 --- a/src/common/compressed_file_system.cpp +++ b/src/common/compressed_file_system.cpp @@ -121,7 +121,7 @@ int64_t CompressedFileSystem::Read(FileHandle &handle, void *buffer, int64_t nr_ int64_t CompressedFileSystem::Write(FileHandle &handle, void *buffer, int64_t nr_bytes) { auto &compressed_file = handle.Cast(); - return compressed_file.WriteData(data_ptr_cast(buffer), nr_bytes); + return compressed_file.WriteData(data_ptr_cast(buffer), nr_bytes); } void CompressedFileSystem::Reset(FileHandle &handle) { diff --git a/src/common/crypto/md5.cpp b/src/common/crypto/md5.cpp index 7d21ab7b6c18..0262a827bdd9 100644 --- a/src/common/crypto/md5.cpp +++ b/src/common/crypto/md5.cpp @@ -232,7 +232,7 @@ void MD5Context::Finish(data_ptr_t out_digest) { (reinterpret_cast(in))[15] = bits[1]; MD5Transform(buf, reinterpret_cast(in)); - ByteReverse(data_ptr_cast(buf), 4); + ByteReverse(reinterpret_cast(buf), 4); memcpy(out_digest, buf, 16); } @@ -260,7 +260,7 @@ string MD5Context::FinishHex() { } void MD5Context::Add(const char *data) { - MD5Update(data_ptr_cast(data), strlen(data)); + MD5Update(const_data_ptr_cast(data), strlen(data)); } } // namespace duckdb diff --git a/src/common/field_writer.cpp b/src/common/field_writer.cpp index be667b64dd3c..0de7b8a78e12 100644 --- a/src/common/field_writer.cpp +++ b/src/common/field_writer.cpp @@ -29,7 +29,7 @@ template <> void FieldWriter::Write(const string &val) { Write((uint32_t)val.size()); if (!val.empty()) { - WriteData(data_ptr_cast(val.c_str()), val.size()); + WriteData(const_data_ptr_cast(val.c_str()), val.size()); } } diff --git a/src/common/fsst.cpp b/src/common/fsst.cpp index 3ecd5e771ae2..e84e02990f6e 100644 --- a/src/common/fsst.cpp +++ b/src/common/fsst.cpp @@ -13,7 +13,7 @@ string_t FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, Vector &resu StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); D_ASSERT(decompressed_string_size <= StringUncompressed::STRING_BLOCK_LIMIT); - return StringVector::AddStringOrBlob(result, data_ptr_cast(decompress_buffer), decompressed_string_size); + return StringVector::AddStringOrBlob(result, const_char_ptr_cast(decompress_buffer), decompressed_string_size); } Value FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, unsigned char *compressed_string, @@ -24,7 +24,7 @@ Value FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, unsigned char * StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); D_ASSERT(decompressed_string_size <= StringUncompressed::STRING_BLOCK_LIMIT); - return Value(string(data_ptr_cast(decompress_buffer), decompressed_string_size)); + return Value(string(char_ptr_cast(decompress_buffer), decompressed_string_size)); } } // namespace duckdb diff --git a/src/common/gzip_file_system.cpp b/src/common/gzip_file_system.cpp index bf0511b44c20..c59fd1a801e5 100644 --- a/src/common/gzip_file_system.cpp +++ b/src/common/gzip_file_system.cpp @@ -183,19 +183,19 @@ bool MiniZStreamWrapper::Read(StreamData &sd) { } // actually decompress - mz_stream_ptr->next_in = (data_ptr_t)sd.in_buff_start; + mz_stream_ptr->next_in = sd.in_buff_start; D_ASSERT(sd.in_buff_end - sd.in_buff_start < NumericLimits::Maximum()); mz_stream_ptr->avail_in = (uint32_t)(sd.in_buff_end - sd.in_buff_start); - mz_stream_ptr->next_out = (data_ptr_t)sd.out_buff_end; + mz_stream_ptr->next_out = data_ptr_cast(sd.out_buff_end); mz_stream_ptr->avail_out = (uint32_t)((sd.out_buff.get() + sd.out_buf_size) - sd.out_buff_end); auto ret = duckdb_miniz::mz_inflate(mz_stream_ptr, duckdb_miniz::MZ_NO_FLUSH); if (ret != duckdb_miniz::MZ_OK && ret != duckdb_miniz::MZ_STREAM_END) { throw IOException("Failed to decode gzip stream: %s", duckdb_miniz::mz_error(ret)); } // update pointers following inflate() - sd.in_buff_start = data_ptr_cast(mz_stream_ptr->next_in); + sd.in_buff_start = (data_ptr_t) mz_stream_ptr->next_in; // NOLINT sd.in_buff_end = sd.in_buff_start + mz_stream_ptr->avail_in; - sd.out_buff_end = (data_ptr_t)mz_stream_ptr->next_out; + sd.out_buff_end = data_ptr_cast(mz_stream_ptr->next_out); D_ASSERT(sd.out_buff_end + mz_stream_ptr->avail_out == sd.out_buff.get() + sd.out_buf_size); // if stream ended, deallocate inflator @@ -360,7 +360,7 @@ string GZipFileSystem::UncompressGZIPString(const string &in) { } auto bytes_remaining = in.size() - (body_ptr - in.data()); - mz_stream_ptr->next_in = data_ptr_cast(body_ptr); + mz_stream_ptr->next_in = const_uchar_ptr_cast(body_ptr); mz_stream_ptr->avail_in = bytes_remaining; unsigned char decompress_buffer[BUFSIZ]; @@ -373,7 +373,7 @@ string GZipFileSystem::UncompressGZIPString(const string &in) { if (status != duckdb_miniz::MZ_STREAM_END && status != duckdb_miniz::MZ_OK) { throw IOException("Failed to uncompress"); } - decompressed.append(data_ptr_cast(decompress_buffer), mz_stream_ptr->total_out - decompressed.size()); + decompressed.append(char_ptr_cast(decompress_buffer), mz_stream_ptr->total_out - decompressed.size()); } duckdb_miniz::mz_inflateEnd(mz_stream_ptr); if (decompressed.empty()) { diff --git a/src/common/operator/cast_operators.cpp b/src/common/operator/cast_operators.cpp index e25953be436f..faa58278a093 100644 --- a/src/common/operator/cast_operators.cpp +++ b/src/common/operator/cast_operators.cpp @@ -1460,7 +1460,7 @@ bool TryCastToBlob::Operation(string_t input, string_t &result, Vector &result_v } result = StringVector::EmptyString(result_vector, result_size); - Blob::ToBlob(input, (data_ptr_t)result.GetDataWriteable()); + Blob::ToBlob(input, data_ptr_cast(result.GetDataWriteable())); result.Finalize(); return true; } diff --git a/src/common/row_operations/row_heap_gather.cpp b/src/common/row_operations/row_heap_gather.cpp index 20274b5cadea..a8b6e7b9ff24 100644 --- a/src/common/row_operations/row_heap_gather.cpp +++ b/src/common/row_operations/row_heap_gather.cpp @@ -29,7 +29,7 @@ static void HeapGatherStringVector(Vector &v, const idx_t vcount, const Selectio } auto len = Load(key_locations[i]); key_locations[i] += sizeof(uint32_t); - target[col_idx] = StringVector::AddStringOrBlob(v, string_t(data_ptr_cast(key_locations[i]), len)); + target[col_idx] = StringVector::AddStringOrBlob(v, string_t(const_char_ptr_cast(key_locations[i]), len)); key_locations[i] += len; } } diff --git a/src/common/row_operations/row_heap_scatter.cpp b/src/common/row_operations/row_heap_scatter.cpp index 2b84043eaea6..c51e2b5d9e71 100644 --- a/src/common/row_operations/row_heap_scatter.cpp +++ b/src/common/row_operations/row_heap_scatter.cpp @@ -123,7 +123,7 @@ static void TemplatedHeapScatter(UnifiedVectorFormat &vdata, const SelectionVect auto source_idx = vdata.sel->get_index(idx + offset); auto target = (T *)key_locations[i]; - Store(source[source_idx], (data_ptr_t)target); + Store(source[source_idx], data_ptr_cast(target)); key_locations[i] += sizeof(T); } } else { @@ -136,7 +136,7 @@ static void TemplatedHeapScatter(UnifiedVectorFormat &vdata, const SelectionVect auto source_idx = vdata.sel->get_index(idx + offset); auto target = (T *)key_locations[i]; - Store(source[source_idx], (data_ptr_t)target); + Store(source[source_idx], data_ptr_cast(target)); key_locations[i] += sizeof(T); // set the validitymask diff --git a/src/common/row_operations/row_scatter.cpp b/src/common/row_operations/row_scatter.cpp index 8478adb3d772..c5104d4f21f8 100644 --- a/src/common/row_operations/row_scatter.cpp +++ b/src/common/row_operations/row_scatter.cpp @@ -81,7 +81,7 @@ static void ScatterStringVector(UnifiedVectorFormat &col, Vector &rows, data_ptr Store(string_data[col_idx], row + col_offset); } else { const auto &str = string_data[col_idx]; - string_t inserted(data_ptr_cast(str_locations[i]), str.GetSize()); + string_t inserted(const_char_ptr_cast(str_locations[i]), str.GetSize()); memcpy(inserted.GetDataWriteable(), str.GetData(), str.GetSize()); str_locations[i] += str.GetSize(); inserted.Finalize(); diff --git a/src/common/serializer.cpp b/src/common/serializer.cpp index 21bc2c32486f..2321fb80ebee 100644 --- a/src/common/serializer.cpp +++ b/src/common/serializer.cpp @@ -10,7 +10,7 @@ string Deserializer::Read() { } auto buffer = make_unsafe_uniq_array(size); ReadData(buffer.get(), size); - return string(data_ptr_cast(buffer.get()), size); + return string(const_char_ptr_cast(buffer.get()), size); } void Deserializer::ReadStringVector(vector &list) { diff --git a/src/common/serializer/binary_deserializer.cpp b/src/common/serializer/binary_deserializer.cpp index 7c5e65cfa992..7b2b956f7b70 100644 --- a/src/common/serializer/binary_deserializer.cpp +++ b/src/common/serializer/binary_deserializer.cpp @@ -125,7 +125,7 @@ string BinaryDeserializer::ReadString() { } auto buffer = make_unsafe_uniq_array(size); ReadData(buffer.get(), size); - return string(data_ptr_cast(buffer.get()), size); + return string(const_char_ptr_cast(buffer.get()), size); } interval_t BinaryDeserializer::ReadInterval() { diff --git a/src/common/sort/comparators.cpp b/src/common/sort/comparators.cpp index 97cd16958e08..b6cf7b557472 100644 --- a/src/common/sort/comparators.cpp +++ b/src/common/sort/comparators.cpp @@ -167,7 +167,7 @@ int Comparators::CompareStringAndAdvance(data_ptr_t &left_ptr, data_ptr_t &right uint32_t right_string_size = Load(right_ptr); left_ptr += sizeof(uint32_t); right_ptr += sizeof(uint32_t); - auto memcmp_res = memcmp(data_ptr_cast(left_ptr), data_ptr_cast(right_ptr), + auto memcmp_res = memcmp(const_char_ptr_cast(left_ptr), const_char_ptr_cast(right_ptr), std::min(left_string_size, right_string_size)); left_ptr += left_string_size; diff --git a/src/common/types/bit.cpp b/src/common/types/bit.cpp index 5f2e5f6bb52b..ce419c4defb1 100644 --- a/src/common/types/bit.cpp +++ b/src/common/types/bit.cpp @@ -20,7 +20,7 @@ idx_t Bit::ComputeBitstringLen(idx_t len) { } static inline idx_t GetBitPadding(const string_t &bit_string) { - auto data = data_ptr_cast(bit_string.GetData()); + auto data = const_data_ptr_cast(bit_string.GetData()); D_ASSERT(idx_t(data[0]) <= 8); return data[0]; } @@ -61,7 +61,7 @@ void Bit::SetEmptyBitString(string_t &target, idx_t len) { // **** casting functions **** void Bit::ToString(string_t bits, char *output) { - auto data = data_ptr_cast(bits.GetData()); + auto data = const_data_ptr_cast(bits.GetData()); auto len = bits.GetSize(); idx_t padding = GetBitPadding(bits); @@ -84,7 +84,7 @@ string Bit::ToString(string_t str) { } bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_message) { - auto data = data_ptr_cast(str.GetData()); + auto data = const_data_ptr_cast(str.GetData()); auto len = str.GetSize(); str_len = 0; for (idx_t i = 0; i < len; i++) { @@ -92,7 +92,7 @@ bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_messag str_len++; } else { string error = StringUtil::Format("Invalid character encountered in string -> bit conversion: '%s'", - string(data_ptr_cast(data) + i, 1)); + string(const_char_ptr_cast(data) + i, 1)); HandleCastError::AssignError(error, error_message); return false; } @@ -107,7 +107,7 @@ bool Bit::TryGetBitStringSize(string_t str, idx_t &str_len, string *error_messag } void Bit::ToBit(string_t str, string_t &output_str) { - auto data = data_ptr_cast(str.GetData()); + auto data = const_data_ptr_cast(str.GetData()); auto len = str.GetSize(); auto output = output_str.GetDataWriteable(); diff --git a/src/common/types/blob.cpp b/src/common/types/blob.cpp index c2ec6c55f48b..8e5720d12ad9 100644 --- a/src/common/types/blob.cpp +++ b/src/common/types/blob.cpp @@ -24,7 +24,7 @@ bool IsRegularCharacter(data_t c) { } idx_t Blob::GetStringSize(string_t blob) { - auto data = data_ptr_cast(blob.GetData()); + auto data = const_data_ptr_cast(blob.GetData()); auto len = blob.GetSize(); idx_t str_len = 0; for (idx_t i = 0; i < len; i++) { @@ -40,7 +40,7 @@ idx_t Blob::GetStringSize(string_t blob) { } void Blob::ToString(string_t blob, char *output) { - auto data = data_ptr_cast(blob.GetData()); + auto data = const_data_ptr_cast(blob.GetData()); auto len = blob.GetSize(); idx_t str_idx = 0; for (idx_t i = 0; i < len; i++) { @@ -70,7 +70,7 @@ string Blob::ToString(string_t blob) { } bool Blob::TryGetBlobSize(string_t str, idx_t &str_len, string *error_message) { - auto data = data_ptr_cast(str.GetData()); + auto data = const_data_ptr_cast(str.GetData()); auto len = str.GetSize(); str_len = 0; for (idx_t i = 0; i < len; i++) { @@ -84,7 +84,7 @@ bool Blob::TryGetBlobSize(string_t str, idx_t &str_len, string *error_message) { if (data[i + 1] != 'x' || Blob::HEX_MAP[data[i + 2]] < 0 || Blob::HEX_MAP[data[i + 3]] < 0) { string error = StringUtil::Format("Invalid hex escape code encountered in string -> blob conversion: %s", - string(data_ptr_cast(data) + i, 4)); + string(const_char_ptr_cast(data) + i, 4)); HandleCastError::AssignError(error, error_message); return false; } @@ -112,7 +112,7 @@ idx_t Blob::GetBlobSize(string_t str) { } void Blob::ToBlob(string_t str, data_ptr_t output) { - auto data = data_ptr_cast(str.GetData()); + auto data = const_data_ptr_cast(str.GetData()); auto len = str.GetSize(); idx_t blob_idx = 0; for (idx_t i = 0; i < len; i++) { @@ -137,7 +137,7 @@ void Blob::ToBlob(string_t str, data_ptr_t output) { string Blob::ToBlob(string_t str) { auto blob_len = GetBlobSize(str); auto buffer = make_unsafe_uniq_array(blob_len); - Blob::ToBlob(str, data_ptr_cast(buffer.get())); + Blob::ToBlob(str, data_ptr_cast(buffer.get())); return string(buffer.get(), blob_len); } @@ -149,7 +149,7 @@ idx_t Blob::ToBase64Size(string_t blob) { } void Blob::ToBase64(string_t blob, char *output) { - auto input_data = data_ptr_cast(blob.GetData()); + auto input_data = const_data_ptr_cast(blob.GetData()); auto input_size = blob.GetSize(); idx_t out_idx = 0; idx_t i; @@ -239,7 +239,7 @@ uint32_t DecodeBase64Bytes(const string_t &str, const_data_ptr_t input_data, idx void Blob::FromBase64(string_t str, data_ptr_t output, idx_t output_size) { D_ASSERT(output_size == FromBase64Size(str)); - auto input_data = data_ptr_cast(str.GetData()); + auto input_data = const_data_ptr_cast(str.GetData()); auto input_size = str.GetSize(); if (input_size == 0) { return; diff --git a/src/common/types/column/column_data_allocator.cpp b/src/common/types/column/column_data_allocator.cpp index 5516196929a7..ee1334c4bcc0 100644 --- a/src/common/types/column/column_data_allocator.cpp +++ b/src/common/types/column/column_data_allocator.cpp @@ -162,10 +162,10 @@ data_ptr_t ColumnDataAllocator::GetDataPointer(ChunkManagementState &state, uint // in-memory allocator: construct pointer from block_id and offset if (sizeof(uintptr_t) == sizeof(uint32_t)) { uintptr_t pointer_value = uintptr_t(block_id); - return (data_ptr_t)pointer_value; + return (data_ptr_t) pointer_value; // NOLINT - convert from pointer value back to pointer } else if (sizeof(uintptr_t) == sizeof(uint64_t)) { uintptr_t pointer_value = (uintptr_t(offset) << 32) | uintptr_t(block_id); - return (data_ptr_t)pointer_value; + return (data_ptr_t) pointer_value; // NOLINT - convert from pointer value back to pointer } else { throw InternalException("ColumnDataCollection: Architecture not supported!?"); } diff --git a/src/common/types/hyperloglog.cpp b/src/common/types/hyperloglog.cpp index 792902f45a0d..93e53f79e559 100644 --- a/src/common/types/hyperloglog.cpp +++ b/src/common/types/hyperloglog.cpp @@ -75,7 +75,7 @@ idx_t HyperLogLog::GetSize() { } data_ptr_t HyperLogLog::GetPtr() const { - return (data_ptr_t)(hll)->ptr; + return data_ptr_cast((hll)->ptr); } unique_ptr HyperLogLog::Copy() { @@ -121,21 +121,21 @@ inline uint64_t TemplatedHash(const T &elem) { template <> inline uint64_t TemplatedHash(const hugeint_t &elem) { - return TemplatedHash(Load((data_ptr_t)&elem.upper)) ^ TemplatedHash(elem.lower); + return TemplatedHash(Load(const_data_ptr_cast(&elem.upper))) ^ TemplatedHash(elem.lower); } template -inline void CreateIntegerRecursive(const data_ptr_t &data, uint64_t &x) { +inline void CreateIntegerRecursive(const_data_ptr_t &data, uint64_t &x) { x ^= (uint64_t)data[rest - 1] << ((rest - 1) * 8); return CreateIntegerRecursive(data, x); } template <> -inline void CreateIntegerRecursive<1>(const data_ptr_t &data, uint64_t &x) { +inline void CreateIntegerRecursive<1>(const_data_ptr_t &data, uint64_t &x) { x ^= (uint64_t)data[0]; } -inline uint64_t HashOtherSize(const data_ptr_t &data, const idx_t &len) { +inline uint64_t HashOtherSize(const_data_ptr_t &data, const idx_t &len) { uint64_t x = 0; switch (len & 7) { case 7: @@ -167,7 +167,7 @@ inline uint64_t HashOtherSize(const data_ptr_t &data, const idx_t &len) { template <> inline uint64_t TemplatedHash(const string_t &elem) { - data_ptr_t data = (data_ptr_t)elem.GetData(); + auto data = const_data_ptr_cast(elem.GetData()); const auto &len = elem.GetSize(); uint64_t h = 0; for (idx_t i = 0; i + sizeof(uint64_t) <= len; i += sizeof(uint64_t)) { diff --git a/src/common/types/list_segment.cpp b/src/common/types/list_segment.cpp index 7e899b30ac05..9501123c28f1 100644 --- a/src/common/types/list_segment.cpp +++ b/src/common/types/list_segment.cpp @@ -96,7 +96,7 @@ static ListSegment *CreatePrimitiveSegment(const ListSegmentFunctions &, Allocat template void DestroyPrimitiveSegment(const ListSegmentFunctions &, ListSegment *segment, Allocator &allocator) { D_ASSERT(segment); - allocator.FreeData((data_ptr_t)segment, GetAllocationSize(segment->capacity)); + allocator.FreeData(data_ptr_cast(segment), GetAllocationSize(segment->capacity)); } static ListSegment *CreateListSegment(const ListSegmentFunctions &, Allocator &allocator, uint16_t capacity) { @@ -109,18 +109,18 @@ static ListSegment *CreateListSegment(const ListSegmentFunctions &, Allocator &a // create an empty linked list for the child vector auto linked_child_list = GetListChildData(segment); LinkedList linked_list(0, nullptr, nullptr); - Store(linked_list, (data_ptr_t)linked_child_list); + Store(linked_list, data_ptr_cast(linked_child_list)); return segment; } void DestroyListSegment(const ListSegmentFunctions &functions, ListSegment *segment, Allocator &allocator) { // destroy the child list - auto linked_child_list = Load((data_ptr_t)GetListChildData(segment)); + auto linked_child_list = Load(data_ptr_cast(GetListChildData(segment))); DestroyLinkedList(functions.child_functions[0], allocator, linked_child_list); // destroy the list segment itself - allocator.FreeData((data_ptr_t)segment, GetAllocationSizeList(segment->capacity)); + allocator.FreeData(data_ptr_cast(segment), GetAllocationSizeList(segment->capacity)); } static ListSegment *CreateStructSegment(const ListSegmentFunctions &functions, Allocator &allocator, @@ -136,7 +136,7 @@ static ListSegment *CreateStructSegment(const ListSegmentFunctions &functions, A for (idx_t i = 0; i < functions.child_functions.size(); i++) { auto child_function = functions.child_functions[i]; auto child_segment = child_function.create_segment(child_function, allocator, capacity); - Store(child_segment, (data_ptr_t)(child_segments + i)); + Store(child_segment, data_ptr_cast(child_segments + i)); } return segment; @@ -147,12 +147,12 @@ void DestroyStructSegment(const ListSegmentFunctions &functions, ListSegment *se auto child_segments = GetStructData(segment); for (idx_t i = 0; i < functions.child_functions.size(); i++) { auto child_function = functions.child_functions[i]; - auto child_segment = Load((data_ptr_t)(child_segments + i)); + auto child_segment = Load(data_ptr_cast(child_segments + i)); child_function.destroy(child_function, child_segment, allocator); } // destroy the struct segment itself - allocator.FreeData((data_ptr_t)segment, + allocator.FreeData(data_ptr_cast(segment), GetAllocationSizeStruct(segment->capacity, functions.child_functions.size())); } @@ -200,7 +200,7 @@ static void WriteDataToPrimitiveSegment(const ListSegmentFunctions &functions, A // write value if (!is_null) { auto data = GetPrimitiveData(segment); - Store(((T *)input_data)[entry_idx], (data_ptr_t)(data + segment->count)); + Store(((T *)input_data)[entry_idx], data_ptr_cast(data + segment->count)); } } @@ -208,7 +208,7 @@ static void WriteDataToVarcharSegment(const ListSegmentFunctions &functions, All Vector &input, idx_t &entry_idx, idx_t &count) { // get the vector data and the source index of the entry that we want to write - auto input_data = FlatVector::GetData(input); + auto input_data = FlatVector::GetData(input); // write null validity auto null_mask = GetNullMask(segment); @@ -222,19 +222,19 @@ static void WriteDataToVarcharSegment(const ListSegmentFunctions &functions, All // get the string string_t str_t; if (!is_null) { - str_t = ((string_t *)input_data)[entry_idx]; + str_t = input_data[entry_idx]; str_length = str_t.GetSize(); } // we can reconstruct the offset from the length - Store(str_length, (data_ptr_t)(str_length_data + segment->count)); + Store(str_length, data_ptr_cast(str_length_data + segment->count)); if (is_null) { return; } // write the characters to the linked list of child segments - auto child_segments = Load((data_ptr_t)GetListChildData(segment)); + auto child_segments = Load(data_ptr_cast(GetListChildData(segment))); for (char &c : str_t.GetString()) { auto child_segment = GetSegment(functions.child_functions.back(), allocator, child_segments); auto data = GetPrimitiveData(child_segment); @@ -244,14 +244,14 @@ static void WriteDataToVarcharSegment(const ListSegmentFunctions &functions, All } // store the updated linked list - Store(child_segments, (data_ptr_t)GetListChildData(segment)); + Store(child_segments, data_ptr_cast(GetListChildData(segment))); } static void WriteDataToListSegment(const ListSegmentFunctions &functions, Allocator &allocator, ListSegment *segment, Vector &input, idx_t &entry_idx, idx_t &count) { // get the vector data and the source index of the entry that we want to write - auto input_data = FlatVector::GetData(input); + auto input_data = FlatVector::GetData(input); // write null validity auto null_mask = GetNullMask(segment); @@ -264,7 +264,7 @@ static void WriteDataToListSegment(const ListSegmentFunctions &functions, Alloca if (!is_null) { // get list entry information - auto list_entries = (list_entry_t *)input_data; + auto list_entries = input_data; const auto &list_entry = list_entries[entry_idx]; list_length = list_entry.length; @@ -273,7 +273,7 @@ static void WriteDataToListSegment(const ListSegmentFunctions &functions, Alloca auto &child_vector = ListVector::GetEntry(input); // loop over the child vector entries and recurse on them - auto child_segments = Load((data_ptr_t)GetListChildData(segment)); + auto child_segments = Load(data_ptr_cast(GetListChildData(segment))); D_ASSERT(functions.child_functions.size() == 1); for (idx_t child_idx = 0; child_idx < list_entry.length; child_idx++) { auto source_idx_child = list_entry.offset + child_idx; @@ -281,10 +281,10 @@ static void WriteDataToListSegment(const ListSegmentFunctions &functions, Alloca lists_size); } // store the updated linked list - Store(child_segments, (data_ptr_t)GetListChildData(segment)); + Store(child_segments, data_ptr_cast(GetListChildData(segment))); } - Store(list_length, (data_ptr_t)(list_length_data + segment->count)); + Store(list_length, data_ptr_cast(list_length_data + segment->count)); } static void WriteDataToStructSegment(const ListSegmentFunctions &functions, Allocator &allocator, ListSegment *segment, @@ -302,7 +302,7 @@ static void WriteDataToStructSegment(const ListSegmentFunctions &functions, Allo // write the data of each of the children of the struct for (idx_t child_count = 0; child_count < children.size(); child_count++) { - auto child_list_segment = Load((data_ptr_t)(child_list + child_count)); + auto child_list_segment = Load(data_ptr_cast(child_list + child_count)); auto &child_function = functions.child_functions[child_count]; child_function.write_data(child_function, allocator, child_list_segment, *children[child_count], entry_idx, count); @@ -345,7 +345,7 @@ static void ReadDataFromPrimitiveSegment(const ListSegmentFunctions &, const Lis for (idx_t i = 0; i < segment->count; i++) { if (aggr_vector_validity.RowIsValid(total_count + i)) { auto data = GetPrimitiveData(segment); - ((T *)aggr_vector_data)[total_count + i] = Load((data_ptr_t)(data + i)); + ((T *)aggr_vector_data)[total_count + i] = Load(data_ptr_cast(data + i)); } } } @@ -365,7 +365,7 @@ static void ReadDataFromVarcharSegment(const ListSegmentFunctions &, const ListS // append all the child chars to one string string str = ""; - auto linked_child_list = Load((data_ptr_t)GetListChildData(segment)); + auto linked_child_list = Load(data_ptr_cast(GetListChildData(segment))); while (linked_child_list.first_segment) { auto child_segment = linked_child_list.first_segment; auto data = GetPrimitiveData(child_segment); @@ -382,7 +382,7 @@ static void ReadDataFromVarcharSegment(const ListSegmentFunctions &, const ListS idx_t offset = 0; for (idx_t i = 0; i < segment->count; i++) { if (!null_mask[i]) { - auto str_length = Load((data_ptr_t)(str_length_data + i)); + auto str_length = Load(data_ptr_cast(str_length_data + i)); auto substr = str.substr(offset, str_length); auto str_t = StringVector::AddStringOrBlob(result, substr); ((string_t *)aggr_vector_data)[total_count + i] = str_t; @@ -416,14 +416,14 @@ static void ReadDataFromListSegment(const ListSegmentFunctions &functions, const // set length and offsets auto list_length_data = GetListLengthData(segment); for (idx_t i = 0; i < segment->count; i++) { - auto list_length = Load((data_ptr_t)(list_length_data + i)); + auto list_length = Load(data_ptr_cast(list_length_data + i)); list_vector_data[total_count + i].length = list_length; list_vector_data[total_count + i].offset = offset; offset += list_length; } auto &child_vector = ListVector::GetEntry(result); - auto linked_child_list = Load((data_ptr_t)GetListChildData(segment)); + auto linked_child_list = Load(data_ptr_cast(GetListChildData(segment))); ListVector::Reserve(result, offset); // recurse into the linked list of child values @@ -451,7 +451,7 @@ static void ReadDataFromStructSegment(const ListSegmentFunctions &functions, con D_ASSERT(children.size() == functions.child_functions.size()); auto struct_children = GetStructData(segment); for (idx_t child_count = 0; child_count < children.size(); child_count++) { - auto struct_children_segment = Load((data_ptr_t)(struct_children + child_count)); + auto struct_children_segment = Load(data_ptr_cast(struct_children + child_count)); auto &child_function = functions.child_functions[child_count]; child_function.read_data(child_function, struct_children_segment, *children[child_count], total_count); } @@ -487,25 +487,25 @@ static ListSegment *CopyDataFromListSegment(const ListSegmentFunctions &function Allocator &allocator) { // create an empty linked list for the child vector of target - auto source_linked_child_list = Load((data_ptr_t)GetListChildData(source)); + auto source_linked_child_list = Load(data_ptr_cast(GetListChildData(source))); // create the segment - auto target = (ListSegment *)AllocateListData(allocator, source->capacity); + auto target = reinterpret_cast(AllocateListData(allocator, source->capacity)); memcpy(target, source, sizeof(ListSegment) + source->capacity * (sizeof(bool) + sizeof(uint64_t)) + sizeof(LinkedList)); target->next = nullptr; auto target_linked_list = GetListChildData(target); LinkedList linked_list(source_linked_child_list.total_capacity, nullptr, nullptr); - Store(linked_list, (data_ptr_t)target_linked_list); + Store(linked_list, data_ptr_cast(target_linked_list)); // recurse to copy the linked child list - auto target_linked_child_list = Load((data_ptr_t)GetListChildData(target)); + auto target_linked_child_list = Load(data_ptr_cast(GetListChildData(target))); D_ASSERT(functions.child_functions.size() == 1); functions.child_functions[0].CopyLinkedList(source_linked_child_list, target_linked_child_list, allocator); // store the updated linked list - Store(target_linked_child_list, (data_ptr_t)GetListChildData(target)); + Store(target_linked_child_list, data_ptr_cast(GetListChildData(target))); return target; } @@ -513,7 +513,7 @@ static ListSegment *CopyDataFromStructSegment(const ListSegmentFunctions &functi Allocator &allocator) { auto source_child_count = functions.child_functions.size(); - auto target = (ListSegment *)AllocateStructData(allocator, source->capacity, source_child_count); + auto target = reinterpret_cast(AllocateStructData(allocator, source->capacity, source_child_count)); memcpy(target, source, sizeof(ListSegment) + source->capacity * sizeof(bool) + source_child_count * sizeof(ListSegment *)); target->next = nullptr; @@ -524,9 +524,9 @@ static ListSegment *CopyDataFromStructSegment(const ListSegmentFunctions &functi for (idx_t i = 0; i < functions.child_functions.size(); i++) { auto child_function = functions.child_functions[i]; - auto source_child_segment = Load((data_ptr_t)(source_child_segments + i)); + auto source_child_segment = Load(data_ptr_cast(source_child_segments + i)); auto target_child_segment = child_function.copy_data(child_function, source_child_segment, allocator); - Store(target_child_segment, (data_ptr_t)(target_child_segments + i)); + Store(target_child_segment, data_ptr_cast(target_child_segments + i)); } return target; } diff --git a/src/common/types/row/tuple_data_scatter_gather.cpp b/src/common/types/row/tuple_data_scatter_gather.cpp index 6d940659fe65..e965ee4735d5 100644 --- a/src/common/types/row/tuple_data_scatter_gather.cpp +++ b/src/common/types/row/tuple_data_scatter_gather.cpp @@ -384,7 +384,7 @@ void TupleDataCollection::ListWithinListComputeHeapSizes(Vector &heap_sizes_v, c // Create a combined child_list_data to be used as list_data in the recursion auto &combined_child_list_data = combined_list_data.combined_data; combined_child_list_data.sel = list_data.sel; - combined_child_list_data.data = (data_ptr_t)combined_list_entries; + combined_child_list_data.data = data_ptr_cast(combined_list_entries); combined_child_list_data.validity = list_data.validity; // Combine the selection vectors diff --git a/src/common/types/string_heap.cpp b/src/common/types/string_heap.cpp index 0e18a7347994..bff8dc2407bd 100644 --- a/src/common/types/string_heap.cpp +++ b/src/common/types/string_heap.cpp @@ -51,7 +51,7 @@ string_t StringHeap::AddBlob(const string_t &data) { string_t StringHeap::EmptyString(idx_t len) { D_ASSERT(len > string_t::INLINE_LENGTH); - auto insert_pos = data_ptr_cast(allocator.Allocate(len)); + auto insert_pos = const_char_ptr_cast(allocator.Allocate(len)); return string_t(insert_pos, len); } diff --git a/src/common/types/vector.cpp b/src/common/types/vector.cpp index 20d1e7fc361e..670cac8483bd 100644 --- a/src/common/types/vector.cpp +++ b/src/common/types/vector.cpp @@ -958,7 +958,7 @@ void Vector::Serialize(idx_t count, Serializer &serializer) { // write the list size serializer.Write(list_size); - serializer.WriteData((data_ptr_t)data.get(), count * sizeof(list_entry_t)); + serializer.WriteData(const_data_ptr_cast(data.get()), count * sizeof(list_entry_t)); child.Serialize(list_size, serializer); break; @@ -983,7 +983,7 @@ void Vector::FormatSerialize(FormatSerializer &serializer, idx_t count) { auto row_idx = vdata.sel->get_index(i); flat_mask.Set(i, vdata.validity.RowIsValid(row_idx)); } - serializer.WriteProperty("validity", (data_ptr_t)flat_mask.GetData(), flat_mask.ValidityMaskSize(count)); + serializer.WriteProperty("validity", const_data_ptr_cast(flat_mask.GetData()), flat_mask.ValidityMaskSize(count)); } if (TypeIsConstantSize(logical_type.InternalType())) { // constant size type: simple copy @@ -1065,7 +1065,7 @@ void Vector::FormatDeserialize(FormatDeserializer &deserializer, idx_t count) { const auto has_validity = deserializer.ReadProperty("all_valid"); if (has_validity) { validity.Initialize(count); - deserializer.ReadProperty("validity", (data_ptr_t)validity.GetData(), validity.ValidityMaskSize(count)); + deserializer.ReadProperty("validity", data_ptr_cast(validity.GetData()), validity.ValidityMaskSize(count)); } if (TypeIsConstantSize(logical_type.InternalType())) { @@ -1152,7 +1152,7 @@ void Vector::Deserialize(idx_t count, Deserializer &source) { const auto has_validity = source.Read(); if (has_validity) { validity.Initialize(count); - source.ReadData((data_ptr_t)validity.GetData(), validity.ValidityMaskSize(count)); + source.ReadData(data_ptr_cast(validity.GetData()), validity.ValidityMaskSize(count)); } if (TypeIsConstantSize(type.InternalType())) { diff --git a/src/core_functions/scalar/blob/base64.cpp b/src/core_functions/scalar/blob/base64.cpp index 7cd54c8d22ad..3545f3b56dac 100644 --- a/src/core_functions/scalar/blob/base64.cpp +++ b/src/core_functions/scalar/blob/base64.cpp @@ -18,7 +18,7 @@ struct Base64DecodeOperator { static RESULT_TYPE Operation(INPUT_TYPE input, Vector &result) { auto result_size = Blob::FromBase64Size(input); auto result_blob = StringVector::EmptyString(result, result_size); - Blob::FromBase64(input, (data_ptr_t)result_blob.GetDataWriteable(), result_size); + Blob::FromBase64(input, data_ptr_cast(result_blob.GetDataWriteable()), result_size); result_blob.Finalize(); return result_blob; } diff --git a/src/core_functions/scalar/string/starts_with.cpp b/src/core_functions/scalar/string/starts_with.cpp index dad54180b08f..c4661b913569 100644 --- a/src/core_functions/scalar/string/starts_with.cpp +++ b/src/core_functions/scalar/string/starts_with.cpp @@ -18,9 +18,9 @@ static bool StartsWith(const unsigned char *haystack, idx_t haystack_size, const static bool StartsWith(const string_t &haystack_s, const string_t &needle_s) { - auto haystack = data_ptr_cast(haystack_s.GetData()); + auto haystack = const_uchar_ptr_cast(haystack_s.GetData()); auto haystack_size = haystack_s.GetSize(); - auto needle = data_ptr_cast(needle_s.GetData()); + auto needle = const_uchar_ptr_cast(needle_s.GetData()); auto needle_size = needle_s.GetSize(); if (needle_size == 0) { // empty needle: always true diff --git a/src/core_functions/scalar/string/string_split.cpp b/src/core_functions/scalar/string/string_split.cpp index 2e0de4d290be..ef9137d87b9f 100644 --- a/src/core_functions/scalar/string/string_split.cpp +++ b/src/core_functions/scalar/string/string_split.cpp @@ -36,8 +36,8 @@ struct RegularStringSplit { if (delim_size == 0) { return 0; } - return ContainsFun::Find(data_ptr_cast(input_data), input_size, - data_ptr_cast(delim_data), delim_size); + return ContainsFun::Find(const_uchar_ptr_cast(input_data), input_size, + const_uchar_ptr_cast(delim_data), delim_size); } }; diff --git a/src/core_functions/scalar/string/trim.cpp b/src/core_functions/scalar/string/trim.cpp index 7f61391034ff..91e3b5dd1970 100644 --- a/src/core_functions/scalar/string/trim.cpp +++ b/src/core_functions/scalar/string/trim.cpp @@ -64,7 +64,7 @@ static void UnaryTrimFunction(DataChunk &args, ExpressionState &state, Vector &r } static void GetIgnoredCodepoints(string_t ignored, unordered_set &ignored_codepoints) { - auto dataptr = data_ptr_cast(ignored.GetData()); + auto dataptr = reinterpret_cast(ignored.GetData()); auto size = ignored.GetSize(); idx_t pos = 0; while (pos < size) { diff --git a/src/execution/expression_executor.cpp b/src/execution/expression_executor.cpp index c5a8dc381163..c9810c5b9d9a 100644 --- a/src/execution/expression_executor.cpp +++ b/src/execution/expression_executor.cpp @@ -283,7 +283,7 @@ idx_t ExpressionExecutor::DefaultSelect(const Expression &expr, ExpressionState // resolve the true/false expression first // then use that to generate the selection vector bool intermediate_bools[STANDARD_VECTOR_SIZE]; - Vector intermediate(LogicalType::BOOLEAN, data_ptr_cast(intermediate_bools)); + Vector intermediate(LogicalType::BOOLEAN, data_ptr_cast(intermediate_bools)); Execute(expr, state, sel, count, intermediate); UnifiedVectorFormat idata; diff --git a/src/execution/operator/aggregate/physical_streaming_window.cpp b/src/execution/operator/aggregate/physical_streaming_window.cpp index 187a8b00c279..28e1267660d1 100644 --- a/src/execution/operator/aggregate/physical_streaming_window.cpp +++ b/src/execution/operator/aggregate/physical_streaming_window.cpp @@ -26,7 +26,7 @@ class StreamingWindowState : public OperatorState { public: using StateBuffer = vector; - StreamingWindowState() : initialized(false), statev(LogicalType::POINTER, data_ptr_cast(&state_ptr)) { + StreamingWindowState() : initialized(false), statev(LogicalType::POINTER, data_ptr_cast(&state_ptr)) { } ~StreamingWindowState() override { diff --git a/src/execution/operator/join/physical_asof_join.cpp b/src/execution/operator/join/physical_asof_join.cpp index 52f2e76b1b48..1664ce946596 100644 --- a/src/execution/operator/join/physical_asof_join.cpp +++ b/src/execution/operator/join/physical_asof_join.cpp @@ -305,7 +305,7 @@ void AsOfLocalState::ResolveJoinKeys(DataChunk &input) { DataChunk payload_chunk; payload_chunk.InitializeEmpty({LogicalType::UINTEGER}); - FlatVector::SetData(payload_chunk.data[0], data_ptr_cast(lhs_sel.data())); + FlatVector::SetData(payload_chunk.data[0], data_ptr_cast(lhs_sel.data())); payload_chunk.SetCardinality(lhs_valid); local_sort.SinkChunk(lhs_keys, payload_chunk); diff --git a/src/execution/operator/join/physical_index_join.cpp b/src/execution/operator/join/physical_index_join.cpp index dadb164c6a38..3035e0ecd09e 100644 --- a/src/execution/operator/join/physical_index_join.cpp +++ b/src/execution/operator/join/physical_index_join.cpp @@ -139,7 +139,7 @@ void PhysicalIndexJoin::Output(ExecutionContext &context, DataChunk &input, Data } state.rhs_chunk.Reset(); state.fetch_state = make_uniq(); - Vector row_ids(LogicalType::ROW_TYPE, data_ptr_cast(&fetch_rows[0])); + Vector row_ids(LogicalType::ROW_TYPE, data_ptr_cast(&fetch_rows[0])); tbl.Fetch(transaction, state.rhs_chunk, fetch_ids, row_ids, output_sel_idx, *state.fetch_state); } diff --git a/src/execution/operator/persistent/buffered_csv_reader.cpp b/src/execution/operator/persistent/buffered_csv_reader.cpp index e6af9f1b355c..d8ac2b972e9e 100644 --- a/src/execution/operator/persistent/buffered_csv_reader.cpp +++ b/src/execution/operator/persistent/buffered_csv_reader.cpp @@ -187,7 +187,7 @@ static string TrimWhitespace(const string &col_name) { static string NormalizeColumnName(const string &col_name) { // normalize UTF8 characters to NFKD auto nfkd = utf8proc_NFKD(reinterpret_cast(col_name.c_str()), col_name.size()); - const string col_name_nfkd = string(data_ptr_cast(nfkd), strlen(data_ptr_cast(nfkd))); + const string col_name_nfkd = string(const_char_ptr_cast(nfkd), strlen(const_char_ptr_cast(nfkd))); free(nfkd); // only keep ASCII characters 0-9 a-z A-Z and replace spaces with regular whitespace diff --git a/src/execution/operator/persistent/csv_file_handle.cpp b/src/execution/operator/persistent/csv_file_handle.cpp index e70023f420b6..4fa923fa18e1 100644 --- a/src/execution/operator/persistent/csv_file_handle.cpp +++ b/src/execution/operator/persistent/csv_file_handle.cpp @@ -99,7 +99,7 @@ idx_t CSVFileHandle::Read(void *buffer, idx_t nr_bytes) { } // we have data left to read from the file // read directly into the buffer - auto bytes_read = file_handle->Read(data_ptr_cast(buffer) + result_offset, nr_bytes - result_offset); + auto bytes_read = file_handle->Read(char_ptr_cast(buffer) + result_offset, nr_bytes - result_offset); file_size = file_handle->GetFileSize(); read_position += bytes_read; if (reset_enabled) { @@ -114,7 +114,7 @@ idx_t CSVFileHandle::Read(void *buffer, idx_t nr_bytes) { } cached_buffer = std::move(new_buffer); } - memcpy(cached_buffer.get() + buffer_size, data_ptr_cast(buffer) + result_offset, bytes_read); + memcpy(cached_buffer.get() + buffer_size, char_ptr_cast(buffer) + result_offset, bytes_read); buffer_size += bytes_read; } diff --git a/src/execution/window_segment_tree.cpp b/src/execution/window_segment_tree.cpp index 495f236a09cf..2756e51a1aa6 100644 --- a/src/execution/window_segment_tree.cpp +++ b/src/execution/window_segment_tree.cpp @@ -212,7 +212,7 @@ WindowSegmentTree::~WindowSegmentTree() { AggregateInputData aggr_input_data(aggr.GetFunctionData(), Allocator::DefaultAllocator()); // call the destructor for all the intermediate states data_ptr_t address_data[STANDARD_VECTOR_SIZE]; - Vector addresses(LogicalType::POINTER, data_ptr_cast(address_data)); + Vector addresses(LogicalType::POINTER, data_ptr_cast(address_data)); idx_t count = 0; for (idx_t i = 0; i < internal_nodes; i++) { address_data[count++] = data_ptr_t(levels_flat_native.get() + i * state.size()); diff --git a/src/function/scalar/string/contains.cpp b/src/function/scalar/string/contains.cpp index 5ae12ed5b8b3..fb68e00e8dd8 100644 --- a/src/function/scalar/string/contains.cpp +++ b/src/function/scalar/string/contains.cpp @@ -106,9 +106,9 @@ idx_t ContainsFun::Find(const unsigned char *haystack, idx_t haystack_size, cons if (location == nullptr) { return DConstants::INVALID_INDEX; } - idx_t base_offset = data_ptr_cast(location) - haystack; + idx_t base_offset = const_uchar_ptr_cast(location) - haystack; haystack_size -= base_offset; - haystack = data_ptr_cast(location); + haystack = const_uchar_ptr_cast(location); // switch algorithm depending on needle size switch (needle_size) { case 1: @@ -133,9 +133,9 @@ idx_t ContainsFun::Find(const unsigned char *haystack, idx_t haystack_size, cons } idx_t ContainsFun::Find(const string_t &haystack_s, const string_t &needle_s) { - auto haystack = data_ptr_cast(haystack_s.GetData()); + auto haystack = const_uchar_ptr_cast(haystack_s.GetData()); auto haystack_size = haystack_s.GetSize(); - auto needle = data_ptr_cast(needle_s.GetData()); + auto needle = const_uchar_ptr_cast(needle_s.GetData()); auto needle_size = needle_s.GetSize(); if (needle_size == 0) { // empty needle: always true diff --git a/src/function/scalar/string/like.cpp b/src/function/scalar/string/like.cpp index 56829bec2164..ebdb66a0fafa 100644 --- a/src/function/scalar/string/like.cpp +++ b/src/function/scalar/string/like.cpp @@ -78,7 +78,7 @@ struct LikeMatcher : public FunctionData { } bool Match(string_t &str) { - auto str_data = data_ptr_cast(str.GetData()); + auto str_data = const_uchar_ptr_cast(str.GetData()); auto str_len = str.GetSize(); idx_t segment_idx = 0; idx_t end_idx = segments.size() - 1; @@ -106,7 +106,7 @@ struct LikeMatcher : public FunctionData { auto &segment = segments[segment_idx]; // find the pattern of the current segment idx_t next_offset = ContainsFun::Find( - str_data, str_len, data_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); + str_data, str_len, const_uchar_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); if (next_offset == DConstants::INVALID_INDEX) { // could not find this pattern in the string: no match return false; @@ -131,7 +131,7 @@ struct LikeMatcher : public FunctionData { auto &segment = segments.back(); // find the pattern of the current segment idx_t next_offset = ContainsFun::Find( - str_data, str_len, data_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); + str_data, str_len, const_uchar_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); return next_offset != DConstants::INVALID_INDEX; } } diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index 653dfd086014..ed086bf43c4e 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -87,7 +87,7 @@ static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, V for (idx_t i = 0; i < input.size(); i++) { auto state_idx = state_data.sel->get_index(i); auto state_entry = UnifiedVectorFormat::GetData(state_data) + state_idx; - auto target_ptr = data_ptr_cast(local_state.state_buffer.get()) + aligned_state_size * i; + auto target_ptr = char_ptr_cast(local_state.state_buffer.get()) + aligned_state_size * i; if (state_data.validity.RowIsValid(state_idx)) { D_ASSERT(state_entry->GetSize() == bind_data.state_size); @@ -95,9 +95,9 @@ static void AggregateStateFinalize(DataChunk &input, ExpressionState &state_p, V } else { // create a dummy state because finalize does not understand NULLs in its input // we put the NULL back in explicitly below - bind_data.aggr.initialize(data_ptr_cast(target_ptr)); + bind_data.aggr.initialize(data_ptr_cast(target_ptr)); } - state_vec_ptr[i] = data_ptr_cast(target_ptr); + state_vec_ptr[i] = data_ptr_cast(target_ptr); } AggregateInputData aggr_input_data(nullptr, Allocator::DefaultAllocator()); @@ -166,7 +166,7 @@ static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Ve bind_data.aggr.combine(local_state.state_vector0, local_state.state_vector1, aggr_input_data, 1); result_ptr[i] = - StringVector::AddStringOrBlob(result, data_ptr_cast(local_state.state_buffer1.get()), bind_data.state_size); + StringVector::AddStringOrBlob(result, const_char_ptr_cast(local_state.state_buffer1.get()), bind_data.state_size); } } @@ -250,7 +250,7 @@ static void ExportAggregateFinalize(Vector &state, AggregateInputData &aggr_inpu auto addresses_ptr = FlatVector::GetData(state); for (idx_t row_idx = 0; row_idx < count; row_idx++) { auto data_ptr = addresses_ptr[row_idx]; - blob_ptr[row_idx] = StringVector::AddStringOrBlob(result, data_ptr_cast(data_ptr), state_size); + blob_ptr[row_idx] = StringVector::AddStringOrBlob(result, const_char_ptr_cast(data_ptr), state_size); } } diff --git a/src/function/table/arrow_conversion.cpp b/src/function/table/arrow_conversion.cpp index 55a1a5b9b047..3412c2a4c65e 100644 --- a/src/function/table/arrow_conversion.cpp +++ b/src/function/table/arrow_conversion.cpp @@ -58,7 +58,7 @@ static void GetValidityMask(ValidityMask &mask, ArrowArray &array, ArrowScanLoca memcpy(temp_nullmask.data(), ArrowBufferData(array, 0) + bit_offset / 8, n_bitmask_bytes + 1); ShiftRight(temp_nullmask.data(), n_bitmask_bytes + 1, bit_offset % 8); //! why this has to be a right shift is a mystery to me - memcpy((void *)mask.GetData(), data_ptr_cast(temp_nullmask.data()), n_bitmask_bytes); + memcpy((void *)mask.GetData(), data_ptr_cast(temp_nullmask.data()), n_bitmask_bytes); } #else auto byte_offset = bit_offset / 8; diff --git a/src/function/table/copy_csv.cpp b/src/function/table/copy_csv.cpp index 27b8cee5e453..560734dc55bf 100644 --- a/src/function/table/copy_csv.cpp +++ b/src/function/table/copy_csv.cpp @@ -187,14 +187,14 @@ static bool RequiresQuotes(WriteCSVData &csv_data, const char *str, idx_t len) { // check for delimiter if (options.delimiter.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, - data_ptr_cast(options.delimiter.c_str()), + ContainsFun::Find(const_uchar_ptr_cast(str), len, + const_uchar_ptr_cast(options.delimiter.c_str()), options.delimiter.size()) != DConstants::INVALID_INDEX) { return true; } // check for quote - if (options.quote.length() != 0 && ContainsFun::Find(data_ptr_cast(str), len, - data_ptr_cast(options.quote.c_str()), + if (options.quote.length() != 0 && ContainsFun::Find(const_uchar_ptr_cast(str), len, + const_uchar_ptr_cast(options.quote.c_str()), options.quote.size()) != DConstants::INVALID_INDEX) { return true; } @@ -225,13 +225,13 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co // complex CSV // check for quote or escape separately if (options.quote.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, - data_ptr_cast(options.quote.c_str()), + ContainsFun::Find(const_uchar_ptr_cast(str), len, + const_uchar_ptr_cast(options.quote.c_str()), options.quote.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } else if (options.escape.length() != 0 && - ContainsFun::Find(data_ptr_cast(str), len, - data_ptr_cast(options.escape.c_str()), + ContainsFun::Find(const_uchar_ptr_cast(str), len, + const_uchar_ptr_cast(options.escape.c_str()), options.escape.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } @@ -239,7 +239,7 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co if (!requires_escape) { // fast path: no need to escape anything serializer.WriteBufferData(options.quote); - serializer.WriteData(data_ptr_cast(str), len); + serializer.WriteData(const_data_ptr_cast(str), len); serializer.WriteBufferData(options.quote); return; } @@ -255,7 +255,7 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co serializer.WriteBufferData(new_val); serializer.WriteBufferData(options.quote); } else { - serializer.WriteData(data_ptr_cast(str), len); + serializer.WriteData(const_data_ptr_cast(str), len); } } @@ -285,7 +285,7 @@ struct GlobalWriteCSVData : public GlobalFunctionData { } void WriteData(const char *data, idx_t size) { - WriteData(data_ptr_cast(data), size); + WriteData(const_data_ptr_cast(data), size); } //! Write rows diff --git a/src/include/duckdb/common/allocator.hpp b/src/include/duckdb/common/allocator.hpp index 3f0d5080723f..fb6f6e9fa613 100644 --- a/src/include/duckdb/common/allocator.hpp +++ b/src/include/duckdb/common/allocator.hpp @@ -96,14 +96,14 @@ class Allocator { return AllocatedData(*this, AllocateData(size), size); } static data_ptr_t DefaultAllocate(PrivateAllocatorData *private_data, idx_t size) { - return (data_ptr_t)malloc(size); + return data_ptr_cast(malloc(size)); } static void DefaultFree(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t size) { free(pointer); } static data_ptr_t DefaultReallocate(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t old_size, idx_t size) { - return (data_ptr_t)realloc(pointer, size); + return data_ptr_cast(realloc(pointer, size)); } static Allocator &Get(ClientContext &context); static Allocator &Get(DatabaseInstance &db); @@ -131,7 +131,7 @@ T *AllocateArray(idx_t size) { template void DeleteArray(T *ptr, idx_t size) { - Allocator::DefaultAllocator().FreeData((data_ptr_t)ptr, size * sizeof(T)); + Allocator::DefaultAllocator().FreeData(data_ptr_cast(ptr), size * sizeof(T)); } template @@ -143,7 +143,7 @@ T *AllocateObject(ARGS &&... args) { template void DestroyObject(T *ptr) { ptr->~T(); - Allocator::DefaultAllocator().FreeData((data_ptr_t)ptr, sizeof(T)); + Allocator::DefaultAllocator().FreeData(data_ptr_cast(ptr), sizeof(T)); } //! The BufferAllocator is a wrapper around the global allocator class that sends any allocations made through the diff --git a/src/include/duckdb/common/arrow/arrow_buffer.hpp b/src/include/duckdb/common/arrow/arrow_buffer.hpp index 4f0d887080ea..e1624ef64dac 100644 --- a/src/include/duckdb/common/arrow/arrow_buffer.hpp +++ b/src/include/duckdb/common/arrow/arrow_buffer.hpp @@ -81,9 +81,9 @@ struct ArrowBuffer { private: void ReserveInternal(idx_t bytes) { if (dataptr) { - dataptr = (data_ptr_t)realloc(dataptr, bytes); + dataptr = data_ptr_cast(realloc(dataptr, bytes)); } else { - dataptr = (data_ptr_t)malloc(bytes); + dataptr = data_ptr_cast(malloc(bytes)); } capacity = bytes; } diff --git a/src/include/duckdb/common/serializer.hpp b/src/include/duckdb/common/serializer.hpp index ac88ad66fd14..8294edace80d 100644 --- a/src/include/duckdb/common/serializer.hpp +++ b/src/include/duckdb/common/serializer.hpp @@ -127,7 +127,7 @@ class Deserializer { template T Read() { T value; - ReadData((data_ptr_t)&value, sizeof(T)); + ReadData(data_ptr_cast(&value), sizeof(T)); return value; } diff --git a/src/include/duckdb/common/serializer/binary_deserializer.hpp b/src/include/duckdb/common/serializer/binary_deserializer.hpp index 1ad73f356c1e..1cb4dcfba2d5 100644 --- a/src/include/duckdb/common/serializer/binary_deserializer.hpp +++ b/src/include/duckdb/common/serializer/binary_deserializer.hpp @@ -35,7 +35,7 @@ class BinaryDeserializer : public FormatDeserializer { template T ReadPrimitive() { T value; - ReadData((data_ptr_t)&value, sizeof(T)); + ReadData(data_ptr_cast(&value), sizeof(T)); return value; } diff --git a/src/include/duckdb/common/typedefs.hpp b/src/include/duckdb/common/typedefs.hpp index 03b86a54e686..c517436abcc7 100644 --- a/src/include/duckdb/common/typedefs.hpp +++ b/src/include/duckdb/common/typedefs.hpp @@ -28,16 +28,30 @@ typedef idx_t column_t; //! Type used for storage (column) identifiers typedef idx_t storage_t; -template -DEST *data_ptr_cast(SRC *src) { - static_assert(sizeof(DEST) == 1, "data_ptr_cast should only be used to cast to char, data_t, unsigned char"); - return (DEST *)src; +template +data_ptr_t data_ptr_cast(SRC *src) { + return reinterpret_cast(src); +} + +template +const_data_ptr_t const_data_ptr_cast(const SRC *src) { + return reinterpret_cast(src); +} + +template +char *char_ptr_cast(SRC *src) { + return reinterpret_cast(src); +} + +template +const char *const_char_ptr_cast(const SRC *src) { + return reinterpret_cast(src); } -template -const DEST *const_data_ptr_cast(SRC *src) { - static_assert(sizeof(DEST) == 1, "data_ptr_cast should only be used to cast to char, data_t, unsigned char"); - return (DEST *)src; + +template +const unsigned char *const_uchar_ptr_cast(const SRC *src) { + return reinterpret_cast(src); } template diff --git a/src/parser/parser.cpp b/src/parser/parser.cpp index b16a81356eee..a78a7a670308 100644 --- a/src/parser/parser.cpp +++ b/src/parser/parser.cpp @@ -51,7 +51,7 @@ static bool StripUnicodeSpaces(const string &query_str, string &new_query) { idx_t pos = 0; unsigned char quote; vector unicode_spaces; - auto query = data_ptr_cast(query_str.c_str()); + auto query = const_uchar_ptr_cast(query_str.c_str()); auto qsize = query_str.size(); regular: diff --git a/src/planner/binder/statement/bind_update.cpp b/src/planner/binder/statement/bind_update.cpp index 8a5de6d21fd9..aeeabcac8c19 100644 --- a/src/planner/binder/statement/bind_update.cpp +++ b/src/planner/binder/statement/bind_update.cpp @@ -87,7 +87,7 @@ static void BindUpdateConstraints(TableCatalogEntry &table, LogicalGet &get, Log // with a "useless" update (i.e. i=i) so we can verify that the CHECK constraint is not violated for (auto &constraint : table.GetBoundConstraints()) { if (constraint->type == ConstraintType::CHECK) { - auto &check = *reinterpret_cast(constraint.get()); + auto &check = constraint->Cast(); // check constraint! check if we need to add any extra columns to the UPDATE clause BindExtraColumns(table, get, proj, update, check.bound_columns); } diff --git a/src/storage/checkpoint/write_overflow_strings_to_disk.cpp b/src/storage/checkpoint/write_overflow_strings_to_disk.cpp index 68f3a3c944c4..7e931475dcf9 100644 --- a/src/storage/checkpoint/write_overflow_strings_to_disk.cpp +++ b/src/storage/checkpoint/write_overflow_strings_to_disk.cpp @@ -33,8 +33,8 @@ void WriteOverflowStringsToDisk::WriteString(string_t string, block_id_t &result size_t compressed_size = 0; compressed_size = s.MaxCompressedLength(uncompressed_size); auto compressed_buf = make_unsafe_uniq_array(compressed_size); - s.Compress(string.GetData(), uncompressed_size, data_ptr_cast(compressed_buf.get()), &compressed_size); - string_t compressed_string(data_ptr_cast(compressed_buf.get()), compressed_size); + s.Compress(string.GetData(), uncompressed_size, char_ptr_cast(compressed_buf.get()), &compressed_size); + string_t compressed_string(const_char_ptr_cast(compressed_buf.get()), compressed_size); // store sizes auto data_ptr = handle.Ptr(); diff --git a/src/storage/compression/bitpacking.cpp b/src/storage/compression/bitpacking.cpp index ed472f618875..9820694175a0 100644 --- a/src/storage/compression/bitpacking.cpp +++ b/src/storage/compression/bitpacking.cpp @@ -68,7 +68,7 @@ static bitpacking_metadata_encoded_t EncodeMeta(bitpacking_metadata_t metadata) } static bitpacking_metadata_t DecodeMeta(bitpacking_metadata_encoded_t *metadata_encoded) { bitpacking_metadata_t metadata; - metadata.mode = Load((data_ptr_t)(metadata_encoded) + 3); + metadata.mode = Load(data_ptr_cast(metadata_encoded) + 3); metadata.offset = *metadata_encoded & 0x00FFFFFF; return metadata; } @@ -690,7 +690,7 @@ struct BitpackingScanState : public SegmentScanState { idx_t decompress_offset = current_group_offset - extra_count; bool skip_sign_extension = true; - BitpackingPrimitives::UnPackBuffer((data_ptr_t)decompression_buffer, + BitpackingPrimitives::UnPackBuffer(data_ptr_cast(decompression_buffer), current_group_ptr + decompress_offset, decompress_count, current_width, skip_sign_extension); @@ -795,11 +795,11 @@ void BitpackingScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t if (to_scan == BitpackingPrimitives::BITPACKING_ALGORITHM_GROUP_SIZE && offset_in_compression_group == 0) { // Decompress directly into result vector - BitpackingPrimitives::UnPackBlock((data_ptr_t)current_result_ptr, decompression_group_start_pointer, + BitpackingPrimitives::UnPackBlock(data_ptr_cast(current_result_ptr), decompression_group_start_pointer, scan_state.current_width, skip_sign_extend); } else { // Decompress compression algorithm to buffer - BitpackingPrimitives::UnPackBlock((data_ptr_t)scan_state.decompression_buffer, + BitpackingPrimitives::UnPackBlock(data_ptr_cast(scan_state.decompression_buffer), decompression_group_start_pointer, scan_state.current_width, skip_sign_extend); @@ -860,7 +860,7 @@ void BitpackingFetchRow(ColumnSegment &segment, ColumnFetchState &state, row_t r D_ASSERT(scan_state.current_group.mode == BitpackingMode::FOR || scan_state.current_group.mode == BitpackingMode::DELTA_FOR); - BitpackingPrimitives::UnPackBlock((data_ptr_t)scan_state.decompression_buffer, decompression_group_start_pointer, + BitpackingPrimitives::UnPackBlock(data_ptr_cast(scan_state.decompression_buffer), decompression_group_start_pointer, scan_state.current_width, skip_sign_extend); *current_result_ptr = *(T *)(scan_state.decompression_buffer + offset_in_compression_group); diff --git a/src/storage/compression/dictionary_compression.cpp b/src/storage/compression/dictionary_compression.cpp index 6d0c36aa4272..d29e1b0c6f94 100644 --- a/src/storage/compression/dictionary_compression.cpp +++ b/src/storage/compression/dictionary_compression.cpp @@ -287,9 +287,9 @@ struct DictionaryCompressionCompressState : public DictionaryCompressionState { memcpy(base_ptr + index_buffer_offset, index_buffer.data(), index_buffer_size); // Store sizes and offsets in segment header - Store(index_buffer_offset, (data_ptr_t)&header_ptr->index_buffer_offset); - Store(index_buffer.size(), (data_ptr_t)&header_ptr->index_buffer_count); - Store((uint32_t)current_width, (data_ptr_t)&header_ptr->bitpacking_width); + Store(index_buffer_offset, data_ptr_cast(&header_ptr->index_buffer_offset)); + Store(index_buffer.size(), data_ptr_cast(&header_ptr->index_buffer_count)); + Store((uint32_t)current_width, data_ptr_cast(&header_ptr->bitpacking_width)); D_ASSERT(current_width == BitpackingPrimitives::MinimumBitWidth(index_buffer.size() - 1)); D_ASSERT(DictionaryCompressionStorage::HasEnoughSpace(current_segment->count, index_buffer.size(), @@ -445,9 +445,9 @@ unique_ptr DictionaryCompressionStorage::StringInitScan(Column // Load header values auto dict = DictionaryCompressionStorage::GetDictionary(segment, state->handle); auto header_ptr = (dictionary_compression_header_t *)baseptr; - auto index_buffer_offset = Load((data_ptr_t)&header_ptr->index_buffer_offset); - auto index_buffer_count = Load((data_ptr_t)&header_ptr->index_buffer_count); - state->current_width = (bitpacking_width_t)(Load((data_ptr_t)&header_ptr->bitpacking_width)); + auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); + auto index_buffer_count = Load(data_ptr_cast(&header_ptr->index_buffer_count)); + state->current_width = (bitpacking_width_t)(Load(data_ptr_cast(&header_ptr->bitpacking_width))); auto index_buffer_ptr = (uint32_t *)(baseptr + index_buffer_offset); @@ -476,11 +476,11 @@ void DictionaryCompressionStorage::StringScanPartial(ColumnSegment &segment, Col auto baseptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); auto dict = DictionaryCompressionStorage::GetDictionary(segment, scan_state.handle); - auto header_ptr = (dictionary_compression_header_t *)baseptr; - auto index_buffer_offset = Load((data_ptr_t)&header_ptr->index_buffer_offset); - auto index_buffer_ptr = (uint32_t *)(baseptr + index_buffer_offset); + auto header_ptr = reinterpret_cast(baseptr); + auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); + auto index_buffer_ptr = reinterpret_cast(baseptr + index_buffer_offset); - auto base_data = (data_ptr_t)(baseptr + DICTIONARY_HEADER_SIZE); + auto base_data = data_ptr_cast(baseptr + DICTIONARY_HEADER_SIZE); auto result_data = FlatVector::GetData(result); if (!ALLOW_DICT_VECTORS || scan_count != STANDARD_VECTOR_SIZE || @@ -502,7 +502,7 @@ void DictionaryCompressionStorage::StringScanPartial(ColumnSegment &segment, Col data_ptr_t src = &base_data[((start - start_offset) * scan_state.current_width) / 8]; sel_t *sel_vec_ptr = scan_state.sel_vec->data(); - BitpackingPrimitives::UnPackBuffer((data_ptr_t)sel_vec_ptr, src, decompress_count, + BitpackingPrimitives::UnPackBuffer(data_ptr_cast(sel_vec_ptr), src, decompress_count, scan_state.current_width); for (idx_t i = 0; i < scan_count; i++) { @@ -527,8 +527,8 @@ void DictionaryCompressionStorage::StringScanPartial(ColumnSegment &segment, Col } // Scanning 1024 values, emitting a dict vector - data_ptr_t dst = (data_ptr_t)(scan_state.sel_vec->data()); - data_ptr_t src = (data_ptr_t)&base_data[(start * scan_state.current_width) / 8]; + data_ptr_t dst = data_ptr_cast(scan_state.sel_vec->data()); + data_ptr_t src = data_ptr_cast(&base_data[(start * scan_state.current_width) / 8]); BitpackingPrimitives::UnPackBuffer(dst, src, scan_count, scan_state.current_width); @@ -553,10 +553,10 @@ void DictionaryCompressionStorage::StringFetchRow(ColumnSegment &segment, Column auto baseptr = handle.Ptr() + segment.GetBlockOffset(); auto header_ptr = reinterpret_cast(baseptr); auto dict = DictionaryCompressionStorage::GetDictionary(segment, handle); - auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); - auto width = (bitpacking_width_t) Load(data_ptr_cast(&header_ptr->bitpacking_width)); - auto index_buffer_ptr = (uint32_t *)(baseptr + index_buffer_offset); - auto base_data = (data_ptr_t)(baseptr + DICTIONARY_HEADER_SIZE); + auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); + auto width = (bitpacking_width_t) Load(data_ptr_cast(&header_ptr->bitpacking_width)); + auto index_buffer_ptr = reinterpret_cast(baseptr + index_buffer_offset); + auto base_data = data_ptr_cast(baseptr + DICTIONARY_HEADER_SIZE); auto result_data = FlatVector::GetData(result); // Handling non-bitpacking-group-aligned start values; @@ -564,8 +564,8 @@ void DictionaryCompressionStorage::StringFetchRow(ColumnSegment &segment, Column // Decompress part of selection buffer we need for this value. sel_t decompression_buffer[BitpackingPrimitives::BITPACKING_ALGORITHM_GROUP_SIZE]; - data_ptr_t src = (data_ptr_t)&base_data[((row_id - start_offset) * width) / 8]; - BitpackingPrimitives::UnPackBuffer((data_ptr_t)decompression_buffer, src, + data_ptr_t src = data_ptr_cast(&base_data[((row_id - start_offset) * width) / 8]); + BitpackingPrimitives::UnPackBuffer(data_ptr_cast(decompression_buffer), src, BitpackingPrimitives::BITPACKING_ALGORITHM_GROUP_SIZE, width); auto selection_value = decompression_buffer[start_offset]; @@ -597,16 +597,16 @@ idx_t DictionaryCompressionStorage::RequiredSpace(idx_t current_count, idx_t ind StringDictionaryContainer DictionaryCompressionStorage::GetDictionary(ColumnSegment &segment, BufferHandle &handle) { auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); StringDictionaryContainer container; - container.size = Load(data_ptr_cast(&header_ptr->dict_size)); - container.end = Load(data_ptr_cast(&header_ptr->dict_end)); + container.size = Load(data_ptr_cast(&header_ptr->dict_size)); + container.end = Load(data_ptr_cast(&header_ptr->dict_end)); return container; } void DictionaryCompressionStorage::SetDictionary(ColumnSegment &segment, BufferHandle &handle, StringDictionaryContainer container) { auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); - Store(container.size, data_ptr_cast(&header_ptr->dict_size)); - Store(container.end, data_ptr_cast(&header_ptr->dict_end)); + Store(container.size, data_ptr_cast(&header_ptr->dict_size)); + Store(container.end, data_ptr_cast(&header_ptr->dict_end)); } string_t DictionaryCompressionStorage::FetchStringFromDict(ColumnSegment &segment, StringDictionaryContainer dict, @@ -621,7 +621,7 @@ string_t DictionaryCompressionStorage::FetchStringFromDict(ColumnSegment &segmen auto dict_end = baseptr + dict.end; auto dict_pos = dict_end - dict_offset; - auto str_ptr = data_ptr_cast(dict_pos); + auto str_ptr = char_ptr_cast(dict_pos); return string_t(str_ptr, string_len); } diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index e337ad2af774..cd54a4632a58 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -348,8 +348,8 @@ class FSSTCompressionState : public CompressionState { memset(base_ptr + symbol_table_offset, 0, fsst_serialized_symbol_table_size); } - Store(symbol_table_offset, (data_ptr_t)&header_ptr->fsst_symbol_table_offset); - Store((uint32_t)current_width, (data_ptr_t)&header_ptr->bitpacking_width); + Store(symbol_table_offset, data_ptr_cast(&header_ptr->fsst_symbol_table_offset)); + Store((uint32_t)current_width, data_ptr_cast(&header_ptr->bitpacking_width)); if (total_size >= FSSTStorage::COMPACTION_FLUSH_LIMIT) { // the block is full enough, don't bother moving around the dictionary @@ -564,7 +564,7 @@ void FSSTStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &sta auto baseptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); auto dict = GetDictionary(segment, scan_state.handle); - auto base_data = (data_ptr_t)(baseptr + sizeof(fsst_compression_header_t)); + auto base_data = data_ptr_cast(baseptr + sizeof(fsst_compression_header_t)); string_t *result_data; if (scan_count == 0) { @@ -594,7 +594,7 @@ void FSSTStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &sta auto offsets = CalculateBpDeltaOffsets(scan_state.last_known_row, start, scan_count); auto bitunpack_buffer = unique_ptr(new uint32_t[offsets.total_bitunpack_count]); - BitUnpackRange(base_data, (data_ptr_t)bitunpack_buffer.get(), offsets.total_bitunpack_count, + BitUnpackRange(base_data, data_ptr_cast(bitunpack_buffer.get()), offsets.total_bitunpack_count, offsets.bitunpack_start_row, scan_state.current_width); auto delta_decode_buffer = unique_ptr(new uint32_t[offsets.total_delta_decode_count]); DeltaDecodeIndices(bitunpack_buffer.get() + offsets.bitunpack_alignment_offset, delta_decode_buffer.get(), @@ -642,7 +642,7 @@ void FSSTStorage::StringFetchRow(ColumnSegment &segment, ColumnFetchState &state auto &buffer_manager = BufferManager::GetBufferManager(segment.db); auto handle = buffer_manager.Pin(segment.block); auto base_ptr = handle.Ptr() + segment.GetBlockOffset(); - auto base_data = (data_ptr_t)(base_ptr + sizeof(fsst_compression_header_t)); + auto base_data = data_ptr_cast(base_ptr + sizeof(fsst_compression_header_t)); auto dict = GetDictionary(segment, handle); duckdb_fsst_decoder_t decoder; @@ -657,7 +657,7 @@ void FSSTStorage::StringFetchRow(ColumnSegment &segment, ColumnFetchState &state auto offsets = CalculateBpDeltaOffsets(-1, row_id, 1); auto bitunpack_buffer = unique_ptr(new uint32_t[offsets.total_bitunpack_count]); - BitUnpackRange(base_data, (data_ptr_t)bitunpack_buffer.get(), offsets.total_bitunpack_count, + BitUnpackRange(base_data, data_ptr_cast(bitunpack_buffer.get()), offsets.total_bitunpack_count, offsets.bitunpack_start_row, width); auto delta_decode_buffer = unique_ptr(new uint32_t[offsets.total_delta_decode_count]); DeltaDecodeIndices(bitunpack_buffer.get() + offsets.bitunpack_alignment_offset, delta_decode_buffer.get(), @@ -697,15 +697,15 @@ bool FSSTFun::TypeIsSupported(PhysicalType type) { //===--------------------------------------------------------------------===// void FSSTStorage::SetDictionary(ColumnSegment &segment, BufferHandle &handle, StringDictionaryContainer container) { auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); - Store(container.size, data_ptr_cast(&header_ptr->dict_size)); - Store(container.end, data_ptr_cast(&header_ptr->dict_end)); + Store(container.size, data_ptr_cast(&header_ptr->dict_size)); + Store(container.end, data_ptr_cast(&header_ptr->dict_end)); } StringDictionaryContainer FSSTStorage::GetDictionary(ColumnSegment &segment, BufferHandle &handle) { auto header_ptr = reinterpret_cast(handle.Ptr() + segment.GetBlockOffset()); StringDictionaryContainer container; - container.size = Load(data_ptr_cast(&header_ptr->dict_size)); - container.end = Load(data_ptr_cast(&header_ptr->dict_end)); + container.size = Load(data_ptr_cast(&header_ptr->dict_size)); + container.end = Load(data_ptr_cast(&header_ptr->dict_end)); return container; } @@ -716,15 +716,15 @@ char *FSSTStorage::FetchStringPointer(StringDictionaryContainer dict, data_ptr_t auto dict_end = baseptr + dict.end; auto dict_pos = dict_end - dict_offset; - return data_ptr_cast(dict_pos); + return char_ptr_cast(dict_pos); } // Returns false if no symbol table was found. This means all strings are either empty or null bool FSSTStorage::ParseFSSTSegmentHeader(data_ptr_t base_ptr, duckdb_fsst_decoder_t *decoder_out, bitpacking_width_t *width_out) { auto header_ptr = reinterpret_cast(base_ptr); - auto fsst_symbol_table_offset = Load(data_ptr_cast(&header_ptr->fsst_symbol_table_offset)); - *width_out = (bitpacking_width_t)(Load(data_ptr_cast(&header_ptr->bitpacking_width))); + auto fsst_symbol_table_offset = Load(data_ptr_cast(&header_ptr->fsst_symbol_table_offset)); + *width_out = (bitpacking_width_t)(Load(data_ptr_cast(&header_ptr->bitpacking_width))); return duckdb_fsst_import(decoder_out, base_ptr + fsst_symbol_table_offset); } diff --git a/src/storage/compression/string_uncompressed.cpp b/src/storage/compression/string_uncompressed.cpp index b388e603782e..3be0e01dc7f3 100644 --- a/src/storage/compression/string_uncompressed.cpp +++ b/src/storage/compression/string_uncompressed.cpp @@ -345,14 +345,14 @@ string_t UncompressedStringStorage::ReadOverflowString(ColumnSegment &segment, V string_t UncompressedStringStorage::ReadString(data_ptr_t target, int32_t offset, uint32_t string_length) { auto ptr = target + offset; - auto str_ptr = data_ptr_cast(ptr); + auto str_ptr = char_ptr_cast(ptr); return string_t(str_ptr, string_length); } string_t UncompressedStringStorage::ReadStringWithLength(data_ptr_t target, int32_t offset) { auto ptr = target + offset; auto str_length = Load(ptr); - auto str_ptr = data_ptr_cast(ptr + sizeof(uint32_t)); + auto str_ptr = char_ptr_cast(ptr + sizeof(uint32_t)); return string_t(str_ptr, str_length); } @@ -403,7 +403,7 @@ string_t UncompressedStringStorage::FetchString(ColumnSegment &segment, StringDi auto dict_end = baseptr + dict.end; auto dict_pos = dict_end - location.offset; - auto str_ptr = data_ptr_cast(dict_pos); + auto str_ptr = char_ptr_cast(dict_pos); return string_t(str_ptr, string_length); } } diff --git a/src/storage/data_table.cpp b/src/storage/data_table.cpp index 79bc73137c62..98906885a539 100644 --- a/src/storage/data_table.cpp +++ b/src/storage/data_table.cpp @@ -834,7 +834,7 @@ void DataTable::RevertAppend(idx_t start_row, idx_t count) { if (!info->indexes.Empty()) { idx_t current_row_base = start_row; row_t row_data[STANDARD_VECTOR_SIZE]; - Vector row_identifiers(LogicalType::ROW_TYPE, (data_ptr_t)row_data); + Vector row_identifiers(LogicalType::ROW_TYPE, data_ptr_cast(row_data)); ScanTableSegment(start_row, count, [&](DataChunk &chunk) { for (idx_t i = 0; i < chunk.size(); i++) { row_data[i] = current_row_base + i; diff --git a/src/storage/single_file_block_manager.cpp b/src/storage/single_file_block_manager.cpp index 0d43ad3f0eb5..e6859fb7db1b 100644 --- a/src/storage/single_file_block_manager.cpp +++ b/src/storage/single_file_block_manager.cpp @@ -19,7 +19,7 @@ namespace duckdb { const char MainHeader::MAGIC_BYTES[] = "DUCK"; void MainHeader::Serialize(Serializer &ser) { - ser.WriteData((data_ptr_t)MAGIC_BYTES, MAGIC_BYTE_SIZE); + ser.WriteData(const_data_ptr_cast(MAGIC_BYTES), MAGIC_BYTE_SIZE); ser.Write(version_number); FieldWriter writer(ser); for (idx_t i = 0; i < FLAG_COUNT; i++) { diff --git a/src/storage/statistics/string_stats.cpp b/src/storage/statistics/string_stats.cpp index 9042611dedad..d7e53dab303c 100644 --- a/src/storage/statistics/string_stats.cpp +++ b/src/storage/statistics/string_stats.cpp @@ -116,7 +116,7 @@ static void ConstructValue(const_data_ptr_t data, idx_t size, data_t target[]) { } void StringStats::Update(BaseStatistics &stats, const string_t &value) { - auto data = data_ptr_cast(value.GetData()); + auto data = const_data_ptr_cast(value.GetData()); auto size = value.GetSize(); //! we can only fit 8 bytes, so we might need to trim our string @@ -136,12 +136,12 @@ void StringStats::Update(BaseStatistics &stats, const string_t &value) { string_data.max_string_length = size; } if (stats.GetType().id() == LogicalTypeId::VARCHAR && !string_data.has_unicode) { - auto unicode = Utf8Proc::Analyze(data_ptr_cast(data), size); + auto unicode = Utf8Proc::Analyze(const_char_ptr_cast(data), size); if (unicode == UnicodeType::UNICODE) { string_data.has_unicode = true; } else if (unicode == UnicodeType::INVALID) { throw InvalidInputException( - ErrorManager::InvalidUnicodeError(string(data_ptr_cast(data), size), "segment statistics update")); + ErrorManager::InvalidUnicodeError(string(const_char_ptr_cast(data), size), "segment statistics update")); } } } @@ -220,8 +220,8 @@ string StringStats::ToString(const BaseStatistics &stats) { idx_t min_len = GetValidMinMaxSubstring(string_data.min); idx_t max_len = GetValidMinMaxSubstring(string_data.max); return StringUtil::Format( - "[Min: %s, Max: %s, Has Unicode: %s, Max String Length: %s]", string(data_ptr_cast(string_data.min), min_len), - string(data_ptr_cast(string_data.max), max_len), string_data.has_unicode ? "true" : "false", + "[Min: %s, Max: %s, Has Unicode: %s, Max String Length: %s]", string(const_char_ptr_cast(string_data.min), min_len), + string(const_char_ptr_cast(string_data.max), max_len), string_data.has_unicode ? "true" : "false", string_data.has_max_string_length ? to_string(string_data.max_string_length) : "?"); } diff --git a/src/storage/table/chunk_info.cpp b/src/storage/table/chunk_info.cpp index da0e18c9f656..71762e6bd6de 100644 --- a/src/storage/table/chunk_info.cpp +++ b/src/storage/table/chunk_info.cpp @@ -241,7 +241,7 @@ void ChunkVectorInfo::Serialize(Serializer &serializer) { for (idx_t i = 0; i < count; i++) { deleted_tuples[sel.get_index(i)] = false; } - serializer.WriteData((data_ptr_t)deleted_tuples, sizeof(bool) * STANDARD_VECTOR_SIZE); + serializer.WriteData(data_ptr_cast(deleted_tuples), sizeof(bool) * STANDARD_VECTOR_SIZE); } unique_ptr ChunkVectorInfo::Deserialize(Deserializer &source) { @@ -250,7 +250,7 @@ unique_ptr ChunkVectorInfo::Deserialize(Deserializer &source) { auto result = make_uniq(start); result->any_deleted = true; bool deleted_tuples[STANDARD_VECTOR_SIZE]; - source.ReadData((data_ptr_t)deleted_tuples, sizeof(bool) * STANDARD_VECTOR_SIZE); + source.ReadData(data_ptr_cast(deleted_tuples), sizeof(bool) * STANDARD_VECTOR_SIZE); for (idx_t i = 0; i < STANDARD_VECTOR_SIZE; i++) { if (deleted_tuples[i]) { result->deleted[i] = 0; diff --git a/src/storage/table/list_column_data.cpp b/src/storage/table/list_column_data.cpp index c388cd9b9f8e..0cd54514ba53 100644 --- a/src/storage/table/list_column_data.cpp +++ b/src/storage/table/list_column_data.cpp @@ -209,7 +209,7 @@ void ListColumnData::Append(BaseStatistics &stats, ColumnAppendState &state, Vec UnifiedVectorFormat vdata; vdata.sel = FlatVector::IncrementalSelectionVector(); - vdata.data = (data_ptr_t)append_offsets.get(); + vdata.data = data_ptr_cast(append_offsets.get()); // append the list offsets ColumnData::AppendData(stats, state, vdata, count); diff --git a/src/storage/table/update_segment.cpp b/src/storage/table/update_segment.cpp index 81304d40e68d..2757c5343a44 100644 --- a/src/storage/table/update_segment.cpp +++ b/src/storage/table/update_segment.cpp @@ -1047,8 +1047,8 @@ UpdateInfo *CreateEmptyUpdateInfo(TransactionData transaction, idx_t type_size, data = make_unsafe_uniq_array(sizeof(UpdateInfo) + (sizeof(sel_t) + type_size) * STANDARD_VECTOR_SIZE); auto update_info = (UpdateInfo *)data.get(); update_info->max = STANDARD_VECTOR_SIZE; - update_info->tuples = (sel_t *)(((data_ptr_t)update_info) + sizeof(UpdateInfo)); - update_info->tuple_data = ((data_ptr_t)update_info) + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; + update_info->tuples = (sel_t *)((data_ptr_cast(update_info)) + sizeof(UpdateInfo)); + update_info->tuple_data = (data_ptr_cast(update_info)) + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; update_info->version_number = transaction.transaction_id; return update_info; } diff --git a/src/storage/wal_replay.cpp b/src/storage/wal_replay.cpp index 00f20c646d43..0809cc93ac5e 100644 --- a/src/storage/wal_replay.cpp +++ b/src/storage/wal_replay.cpp @@ -486,7 +486,7 @@ void ReplayState::ReplayDelete() { D_ASSERT(chunk.ColumnCount() == 1 && chunk.data[0].GetType() == LogicalType::ROW_TYPE); row_t row_ids[1]; - Vector row_identifiers(LogicalType::ROW_TYPE, data_ptr_cast(row_ids)); + Vector row_identifiers(LogicalType::ROW_TYPE, data_ptr_cast(row_ids)); auto source_ids = FlatVector::GetData(chunk.data[0]); // delete the tuples from the current table diff --git a/src/transaction/cleanup_state.cpp b/src/transaction/cleanup_state.cpp index d1278726005d..8fb0dc22c244 100644 --- a/src/transaction/cleanup_state.cpp +++ b/src/transaction/cleanup_state.cpp @@ -80,7 +80,7 @@ void CleanupState::Flush() { } // set up the row identifiers vector - Vector row_identifiers(LogicalType::ROW_TYPE, (data_ptr_t)row_numbers); + Vector row_identifiers(LogicalType::ROW_TYPE, data_ptr_cast(row_numbers)); // delete the tuples from all the indexes try { diff --git a/src/transaction/commit_state.cpp b/src/transaction/commit_state.cpp index b811bd6b814c..0f68b46f5d53 100644 --- a/src/transaction/commit_state.cpp +++ b/src/transaction/commit_state.cpp @@ -45,7 +45,7 @@ void CommitState::WriteCatalogEntry(CatalogEntry &entry, data_ptr_t dataptr) { D_ASSERT(table_entry.IsDuckTable()); // ALTER TABLE statement, read the extra data after the entry auto extra_data_size = Load(dataptr); - auto extra_data = (data_ptr_t)(dataptr + sizeof(idx_t)); + auto extra_data = data_ptr_cast(dataptr + sizeof(idx_t)); BufferedDeserializer source(extra_data, extra_data_size); string column_name = source.Read(); @@ -72,7 +72,7 @@ void CommitState::WriteCatalogEntry(CatalogEntry &entry, data_ptr_t dataptr) { if (entry.type == CatalogType::VIEW_ENTRY) { // ALTER TABLE statement, read the extra data after the entry auto extra_data_size = Load(dataptr); - auto extra_data = (data_ptr_t)(dataptr + sizeof(idx_t)); + auto extra_data = data_ptr_cast(dataptr + sizeof(idx_t)); // deserialize it BufferedDeserializer source(extra_data, extra_data_size); string column_name = source.Read(); diff --git a/src/transaction/duck_transaction.cpp b/src/transaction/duck_transaction.cpp index c313e80c42b9..3804180f2e84 100644 --- a/src/transaction/duck_transaction.cpp +++ b/src/transaction/duck_transaction.cpp @@ -91,8 +91,8 @@ UpdateInfo *DuckTransaction::CreateUpdateInfo(idx_t type_size, idx_t entries) { auto update_info = (UpdateInfo *)undo_buffer.CreateEntry( UndoFlags::UPDATE_TUPLE, sizeof(UpdateInfo) + (sizeof(sel_t) + type_size) * STANDARD_VECTOR_SIZE); update_info->max = STANDARD_VECTOR_SIZE; - update_info->tuples = (sel_t *)(((data_ptr_t)update_info) + sizeof(UpdateInfo)); - update_info->tuple_data = ((data_ptr_t)update_info) + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; + update_info->tuples = (sel_t *)((data_ptr_cast(update_info)) + sizeof(UpdateInfo)); + update_info->tuple_data = (data_ptr_cast(update_info)) + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; update_info->version_number = transaction_id; return update_info; } diff --git a/test/api/test_custom_allocator.cpp b/test/api/test_custom_allocator.cpp index 3a15dbd17561..8a1d5c8e5ee1 100644 --- a/test/api/test_custom_allocator.cpp +++ b/test/api/test_custom_allocator.cpp @@ -16,7 +16,7 @@ struct MyAllocateData : public PrivateAllocatorData { data_ptr_t my_allocate_function(PrivateAllocatorData *private_data, idx_t size) { auto my_allocate_data = (MyAllocateData *)private_data; *my_allocate_data->memory_counter += size; - return (data_ptr_t)malloc(size); + return data_ptr_cast(malloc(size)); } void my_free_function(PrivateAllocatorData *private_data, data_ptr_t pointer, idx_t size) { @@ -29,7 +29,7 @@ data_ptr_t my_reallocate_function(PrivateAllocatorData *private_data, data_ptr_t auto my_allocate_data = (MyAllocateData *)private_data; *my_allocate_data->memory_counter -= old_size; *my_allocate_data->memory_counter += size; - return (data_ptr_t)realloc(pointer, size); + return data_ptr_cast(realloc(pointer, size)); } TEST_CASE("Test using a custom allocator", "[api][.]") { diff --git a/test/extension/loadable_extension_optimizer_demo.cpp b/test/extension/loadable_extension_optimizer_demo.cpp index eb9a3227d61b..ad648754ec17 100644 --- a/test/extension/loadable_extension_optimizer_demo.cpp +++ b/test/extension/loadable_extension_optimizer_demo.cpp @@ -111,7 +111,7 @@ class WaggleExtension : public OptimizerExtension { auto buffer = malloc(chunk_len); D_ASSERT(buffer); ReadChecked(sockfd, buffer, chunk_len); - BufferedDeserializer deserializer((data_ptr_t)buffer, chunk_len); + BufferedDeserializer deserializer(data_ptr_cast(buffer), chunk_len); DataChunk chunk; chunk.Deserialize(deserializer); diff --git a/test/extension/test_remote_optimizer.cpp b/test/extension/test_remote_optimizer.cpp index d3f07cc48b09..91b52d409552 100644 --- a/test/extension/test_remote_optimizer.cpp +++ b/test/extension/test_remote_optimizer.cpp @@ -79,7 +79,7 @@ TEST_CASE("Test using a remote optimizer pass in case thats important to someone REQUIRE(buffer); REQUIRE(read(connfd, buffer, bytes) == ssize_t(bytes)); - BufferedDeserializer deserializer((data_ptr_t)buffer, bytes); + BufferedDeserializer deserializer(data_ptr_cast(buffer), bytes); con2.BeginTransaction(); PlanDeserializationState state(*con2.context); auto plan = LogicalOperator::Deserialize(deserializer, state); diff --git a/third_party/pcg/pcg_extras.hpp b/third_party/pcg/pcg_extras.hpp index cc002761409a..0524fe21832d 100644 --- a/third_party/pcg/pcg_extras.hpp +++ b/third_party/pcg/pcg_extras.hpp @@ -46,7 +46,7 @@ #include #ifdef __GNUC__ - #include +#include #endif /* diff --git a/tools/pythonpkg/src/numpy/array_wrapper.cpp b/tools/pythonpkg/src/numpy/array_wrapper.cpp index 0d0915d5c6b7..8f7d363aedee 100644 --- a/tools/pythonpkg/src/numpy/array_wrapper.cpp +++ b/tools/pythonpkg/src/numpy/array_wrapper.cpp @@ -601,13 +601,13 @@ void RawArrayWrapper::Initialize(idx_t capacity) { string dtype = DuckDBToNumpyDtype(type); array = py::array(py::dtype(dtype), capacity); - data = (data_ptr_t)array.mutable_data(); + data = data_ptr_cast(array.mutable_data()); } void RawArrayWrapper::Resize(idx_t new_capacity) { vector new_shape {py::ssize_t(new_capacity)}; array.resize(new_shape, false); - data = (data_ptr_t)array.mutable_data(); + data = data_ptr_cast(array.mutable_data()); } ArrayWrapper::ArrayWrapper(const LogicalType &type) : requires_mask(false) { diff --git a/tools/pythonpkg/src/numpy/numpy_scan.cpp b/tools/pythonpkg/src/numpy/numpy_scan.cpp index cba8c417934f..8c0e9362f548 100644 --- a/tools/pythonpkg/src/numpy/numpy_scan.cpp +++ b/tools/pythonpkg/src/numpy/numpy_scan.cpp @@ -20,7 +20,7 @@ template void ScanNumpyColumn(py::array &numpy_col, idx_t stride, idx_t offset, Vector &out, idx_t count) { auto src_ptr = (T *)numpy_col.data(); if (stride == sizeof(T)) { - FlatVector::SetData(out, (data_ptr_t)(src_ptr + offset)); + FlatVector::SetData(out, data_ptr_cast(src_ptr + offset)); } else { auto tgt_ptr = (T *)FlatVector::GetData(out); for (idx_t i = 0; i < count; i++) { @@ -80,7 +80,7 @@ template void ScanNumpyFpColumn(T *src_ptr, idx_t stride, idx_t count, idx_t offset, Vector &out) { auto &mask = FlatVector::Validity(out); if (stride == sizeof(T)) { - FlatVector::SetData(out, (data_ptr_t)(src_ptr + offset)); + FlatVector::SetData(out, data_ptr_cast(src_ptr + offset)); // Turn NaN values into NULL auto tgt_ptr = FlatVector::GetData(out); for (idx_t i = 0; i < count; i++) { diff --git a/tools/rpkg/src/scan.cpp b/tools/rpkg/src/scan.cpp index bef98b2c7edd..51aeac58891f 100644 --- a/tools/rpkg/src/scan.cpp +++ b/tools/rpkg/src/scan.cpp @@ -85,23 +85,23 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context switch (rtypes[col_idx]) { case RType::LOGICAL: duckdb_col_type = LogicalType::BOOLEAN; - coldata_ptr = (data_ptr_t)LOGICAL_POINTER(coldata); + coldata_ptr = data_ptr_cast(LOGICAL_POINTER(coldata)); break; case RType::INTEGER: duckdb_col_type = LogicalType::INTEGER; - coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); + coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); break; case RType::NUMERIC: duckdb_col_type = LogicalType::DOUBLE; - coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); + coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); break; case RType::INTEGER64: duckdb_col_type = LogicalType::BIGINT; - coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); + coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); break; case RType::FACTOR: { // TODO What about factors that use numeric? - coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); + coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); strings levels = GET_LEVELS(coldata); Vector duckdb_levels(LogicalType::VARCHAR, levels.size()); auto levels_ptr = FlatVector::GetData(duckdb_levels); @@ -112,7 +112,7 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context break; } case RType::STRING: - coldata_ptr = (data_ptr_t)DATAPTR_RO(coldata); + coldata_ptr = data_ptr_cast(DATAPTR_RO(coldata)); if (experimental) { duckdb_col_type = RStringsType::Get(); } else { @@ -121,7 +121,7 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context break; case RType::TIMESTAMP: duckdb_col_type = LogicalType::TIMESTAMP; - coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); + coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); break; case RType::TIME_SECONDS: case RType::TIME_MINUTES: @@ -129,7 +129,7 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context case RType::TIME_DAYS: case RType::TIME_WEEKS: duckdb_col_type = LogicalType::TIME; - coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); + coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); break; case RType::TIME_SECONDS_INTEGER: case RType::TIME_MINUTES_INTEGER: @@ -137,24 +137,24 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context case RType::TIME_DAYS_INTEGER: case RType::TIME_WEEKS_INTEGER: duckdb_col_type = LogicalType::TIME; - coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); + coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); break; case RType::DATE: if (!IS_NUMERIC(coldata)) { cpp11::stop("DATE should really be integer"); } - coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); + coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); duckdb_col_type = LogicalType::DATE; break; case RType::DATE_INTEGER: if (!IS_INTEGER(coldata)) { cpp11::stop("DATE_INTEGER should really be integer"); } - coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); + coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); duckdb_col_type = LogicalType::DATE; break; case RType::BLOB: - coldata_ptr = (data_ptr_t)DATAPTR_RO(coldata); + coldata_ptr = data_ptr_cast(DATAPTR_RO(coldata)); duckdb_col_type = LogicalType::BLOB; break; default: diff --git a/tools/rpkg/src/transform.cpp b/tools/rpkg/src/transform.cpp index 9138df48405c..a1fe2770c28a 100644 --- a/tools/rpkg/src/transform.cpp +++ b/tools/rpkg/src/transform.cpp @@ -207,7 +207,7 @@ SEXP ToRString(const string_t &input) { void duckdb_r_transform(Vector &src_vec, const SEXP dest, idx_t dest_offset, idx_t n, bool integer64) { if (src_vec.GetType().GetAlias() == R_STRING_TYPE_NAME) { - ptrdiff_t sexp_header_size = (data_ptr_t)DATAPTR(R_BlankString) - (data_ptr_t)R_BlankString; + ptrdiff_t sexp_header_size = data_ptr_cast(DATAPTR(R_BlankString)) - data_ptr_cast(R_BlankString); auto child_ptr = FlatVector::GetData(src_vec); auto &mask = FlatVector::Validity(src_vec); @@ -216,7 +216,7 @@ void duckdb_r_transform(Vector &src_vec, const SEXP dest, idx_t dest_offset, idx if (!mask.RowIsValid(row_idx)) { SET_STRING_ELT(dest, dest_offset + row_idx, NA_STRING); } else { - SET_STRING_ELT(dest, dest_offset + row_idx, (SEXP)((data_ptr_t)child_ptr[row_idx] - sexp_header_size)); + SET_STRING_ELT(dest, dest_offset + row_idx, (SEXP)(data_ptr_cast(child_ptr[row_idx]) - sexp_header_size)); } } return; From 435606b6019bacae9f7511c05f73cadcad6364c8 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 11:51:38 +0200 Subject: [PATCH 13/28] const_data_ptr_cast --- extension/parquet/column_reader.cpp | 6 +++--- extension/parquet/column_writer.cpp | 6 +++--- extension/parquet/parquet_writer.cpp | 6 +++--- src/common/sort/partition_state.cpp | 4 ++-- src/common/types/hash.cpp | 4 ++-- src/common/types/hyperloglog.cpp | 2 +- src/common/types/list_segment.cpp | 12 ++++++------ src/common/types/vector.cpp | 8 ++++---- src/include/duckdb/common/crypto/md5.hpp | 4 ++-- src/include/duckdb/common/field_writer.hpp | 6 +++--- src/include/duckdb/common/radix.hpp | 4 ++-- src/include/duckdb/common/serializer.hpp | 6 +++--- .../duckdb/common/serializer/binary_serializer.hpp | 4 ++-- .../common/types/column/partitioned_column_data.hpp | 12 ++++++++++++ src/include/duckdb/common/types/string_type.hpp | 12 ++++++------ src/include/duckdb/common/types/value.hpp | 2 +- src/include/duckdb/common/types/vector_buffer.hpp | 12 ++++++++++++ .../storage/compression/chimp/chimp_compress.hpp | 2 +- .../storage/compression/patas/patas_analyze.hpp | 2 +- .../storage/compression/patas/patas_compress.hpp | 2 +- src/main/client_context.cpp | 4 ++-- src/storage/statistics/string_stats.cpp | 6 +++--- test/api/test_prepared_api.cpp | 2 +- tools/pythonpkg/src/native/python_conversion.cpp | 2 +- tools/sqlite3_api_wrapper/sqlite3_api_wrapper.cpp | 2 +- 25 files changed, 78 insertions(+), 54 deletions(-) diff --git a/extension/parquet/column_reader.cpp b/extension/parquet/column_reader.cpp index c0787cf3c005..57e636ca5ab3 100644 --- a/extension/parquet/column_reader.cpp +++ b/extension/parquet/column_reader.cpp @@ -1112,7 +1112,7 @@ struct DecimalParquetValueConversion { } plain_data.available(byte_len); auto res = - ParquetDecimalUtils::ReadDecimalValue((const_data_ptr_t)plain_data.ptr, byte_len); + ParquetDecimalUtils::ReadDecimalValue(const_data_ptr_cast(plain_data.ptr), byte_len); plain_data.inc(byte_len); return res; @@ -1210,7 +1210,7 @@ struct UUIDValueConversion { static hugeint_t PlainRead(ByteBuffer &plain_data, ColumnReader &reader) { idx_t byte_len = sizeof(hugeint_t); plain_data.available(byte_len); - auto res = ReadParquetUUID((const_data_ptr_t)plain_data.ptr); + auto res = ReadParquetUUID(const_data_ptr_cast(plain_data.ptr)); plain_data.inc(byte_len); return res; @@ -1261,7 +1261,7 @@ struct IntervalValueConversion { static interval_t PlainRead(ByteBuffer &plain_data, ColumnReader &reader) { idx_t byte_len = PARQUET_INTERVAL_SIZE; plain_data.available(byte_len); - auto res = ReadParquetInterval((const_data_ptr_t)plain_data.ptr); + auto res = ReadParquetInterval(const_data_ptr_cast(plain_data.ptr)); plain_data.inc(byte_len); return res; diff --git a/extension/parquet/column_writer.cpp b/extension/parquet/column_writer.cpp index cd3287c2186b..2e113ba50682 100644 --- a/extension/parquet/column_writer.cpp +++ b/extension/parquet/column_writer.cpp @@ -1342,7 +1342,7 @@ class StringColumnWriter : public BasicColumnWriter { } stats.Update(ptr[r]); temp_writer.Write(ptr[r].GetSize()); - temp_writer.WriteData((const_data_ptr_t)ptr[r].GetData(), ptr[r].GetSize()); + temp_writer.WriteData(const_data_ptr_cast(ptr[r].GetData()), ptr[r].GetSize()); } } } @@ -1401,7 +1401,7 @@ class StringColumnWriter : public BasicColumnWriter { stats.Update(value); // write this string value to the dictionary temp_writer->Write(value.GetSize()); - temp_writer->WriteData((const_data_ptr_t)(value.GetData()), value.GetSize()); + temp_writer->WriteData(const_data_ptr_cast((value.GetData())), value.GetSize()); } // flush the dictionary page and add it to the to-be-written pages WriteDictionary(state, std::move(temp_writer), values.size()); @@ -1528,7 +1528,7 @@ class EnumColumnWriter : public BasicColumnWriter { stats.Update(string_values[r]); // write this string value to the dictionary temp_writer->Write(string_values[r].GetSize()); - temp_writer->WriteData((const_data_ptr_t)string_values[r].GetData(), string_values[r].GetSize()); + temp_writer->WriteData(const_data_ptr_cast(string_values[r].GetData()), string_values[r].GetSize()); } // flush the dictionary page and add it to the to-be-written pages WriteDictionary(state, std::move(temp_writer), enum_count); diff --git a/extension/parquet/parquet_writer.cpp b/extension/parquet/parquet_writer.cpp index a3fcc6ddf0d4..843e5d4707d2 100644 --- a/extension/parquet/parquet_writer.cpp +++ b/extension/parquet/parquet_writer.cpp @@ -45,7 +45,7 @@ class MyTransport : public TTransport { } void write_virt(const uint8_t *buf, uint32_t len) override { - serializer.WriteData((const_data_ptr_t)buf, len); + serializer.WriteData(const_data_ptr_cast(buf), len); } private: @@ -232,7 +232,7 @@ ParquetWriter::ParquetWriter(FileSystem &fs, string file_name_p, vector(fs, file_name.c_str(), FileFlags::FILE_FLAGS_WRITE | FileFlags::FILE_FLAGS_FILE_CREATE_NEW); // parquet files start with the string "PAR1" - writer->WriteData((const_data_ptr_t) "PAR1", 4); + writer->WriteData(const_data_ptr_cast("PAR1"), 4); TCompactProtocolFactoryT tproto_factory; protocol = tproto_factory.getProtocol(make_shared(*writer)); @@ -327,7 +327,7 @@ void ParquetWriter::Finalize() { writer->Write(writer->GetTotalWritten() - start_offset); // parquet files also end with the string "PAR1" - writer->WriteData((const_data_ptr_t) "PAR1", 4); + writer->WriteData(const_data_ptr_cast("PAR1"), 4); // flush to disk writer->Sync(); diff --git a/src/common/sort/partition_state.cpp b/src/common/sort/partition_state.cpp index 2ddab3dd6653..551d6d017934 100644 --- a/src/common/sort/partition_state.cpp +++ b/src/common/sort/partition_state.cpp @@ -124,8 +124,8 @@ void PartitionGlobalSinkState::ResizeGroupingData(idx_t cardinality) { void PartitionGlobalSinkState::SyncLocalPartition(GroupingPartition &local_partition, GroupingAppend &local_append) { // We are done if the local_partition is right sized. - auto local_radix = (RadixPartitionedColumnData *)local_partition.get(); - if (local_radix->GetRadixBits() == grouping_data->GetRadixBits()) { + auto &local_radix = local_partition->Cast(); + if (local_radix.GetRadixBits() == grouping_data->GetRadixBits()) { return; } diff --git a/src/common/types/hash.cpp b/src/common/types/hash.cpp index 34d39d8e6f61..63e0d66266e3 100644 --- a/src/common/types/hash.cpp +++ b/src/common/types/hash.cpp @@ -40,7 +40,7 @@ template <> hash_t Hash(float val) { static_assert(sizeof(float) == sizeof(uint32_t), ""); FloatingPointEqualityTransform::OP(val); - uint32_t uval = Load((const_data_ptr_t)&val); + uint32_t uval = Load(const_data_ptr_cast(&val)); return murmurhash64(uval); } @@ -48,7 +48,7 @@ template <> hash_t Hash(double val) { static_assert(sizeof(double) == sizeof(uint64_t), ""); FloatingPointEqualityTransform::OP(val); - uint64_t uval = Load((const_data_ptr_t)&val); + uint64_t uval = Load(const_data_ptr_cast(&val)); return murmurhash64(uval); } diff --git a/src/common/types/hyperloglog.cpp b/src/common/types/hyperloglog.cpp index 93e53f79e559..7029831deee5 100644 --- a/src/common/types/hyperloglog.cpp +++ b/src/common/types/hyperloglog.cpp @@ -260,7 +260,7 @@ void HyperLogLog::ProcessEntries(UnifiedVectorFormat &vdata, const LogicalType & void HyperLogLog::AddToLogs(UnifiedVectorFormat &vdata, idx_t count, uint64_t indices[], uint8_t counts[], HyperLogLog **logs[], const SelectionVector *log_sel) { - AddToLogsInternal(vdata, count, indices, counts, (void ****)logs, log_sel); + AddToLogsInternal(vdata, count, indices, counts, reinterpret_cast(logs), log_sel); } void HyperLogLog::AddToLog(UnifiedVectorFormat &vdata, idx_t count, uint64_t indices[], uint8_t counts[]) { diff --git a/src/common/types/list_segment.cpp b/src/common/types/list_segment.cpp index 9501123c28f1..a8635cb987a9 100644 --- a/src/common/types/list_segment.cpp +++ b/src/common/types/list_segment.cpp @@ -18,7 +18,7 @@ static data_ptr_t AllocatePrimitiveData(Allocator &allocator, uint16_t capacity) template static T *GetPrimitiveData(const ListSegment *segment) { - return (T *)(((char *)segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); } //===--------------------------------------------------------------------===// @@ -33,11 +33,11 @@ static data_ptr_t AllocateListData(Allocator &allocator, uint16_t capacity) { } static uint64_t *GetListLengthData(const ListSegment *segment) { - return (uint64_t *)(((char *)segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + segment->capacity * sizeof(bool)); } static LinkedList *GetListChildData(const ListSegment *segment) { - return (LinkedList *)(((char *)segment) + sizeof(ListSegment) + + return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + segment->capacity * (sizeof(bool) + sizeof(uint64_t))); } @@ -53,11 +53,11 @@ static data_ptr_t AllocateStructData(Allocator &allocator, uint16_t capacity, id } static ListSegment **GetStructData(const ListSegment *segment) { - return (ListSegment **)(((char *)segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + segment->capacity * sizeof(bool)); } static bool *GetNullMask(const ListSegment *segment) { - return (bool *)(((char *)segment) + sizeof(ListSegment)); + return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment)); } static uint16_t GetCapacityForNewSegment(uint16_t capacity) { @@ -101,7 +101,7 @@ void DestroyPrimitiveSegment(const ListSegmentFunctions &, ListSegment *segment, static ListSegment *CreateListSegment(const ListSegmentFunctions &, Allocator &allocator, uint16_t capacity) { // allocate data and set the header - auto segment = (ListSegment *)AllocateListData(allocator, capacity); + auto segment = reinterpret_cast(AllocateListData(allocator, capacity)); segment->capacity = capacity; segment->count = 0; segment->next = nullptr; diff --git a/src/common/types/vector.cpp b/src/common/types/vector.cpp index 670cac8483bd..255acf8a9e0a 100644 --- a/src/common/types/vector.cpp +++ b/src/common/types/vector.cpp @@ -544,11 +544,11 @@ Value Vector::GetValueInternal(const Vector &v_p, idx_t index_p) { case LogicalTypeId::AGGREGATE_STATE: case LogicalTypeId::BLOB: { auto str = ((string_t *)data)[index]; - return Value::BLOB((const_data_ptr_t)str.GetData(), str.GetSize()); + return Value::BLOB(const_data_ptr_cast(str.GetData()), str.GetSize()); } case LogicalTypeId::BIT: { auto str = ((string_t *)data)[index]; - return Value::BIT((const_data_ptr_t)str.GetData(), str.GetSize()); + return Value::BIT(const_data_ptr_cast(str.GetData()), str.GetSize()); } case LogicalTypeId::MAP: { auto offlen = ((list_entry_t *)data)[index]; @@ -915,7 +915,7 @@ void Vector::Serialize(idx_t count, Serializer &serializer) { auto row_idx = vdata.sel->get_index(i); flat_mask.Set(i, vdata.validity.RowIsValid(row_idx)); } - serializer.WriteData((const_data_ptr_t)flat_mask.GetData(), flat_mask.ValidityMaskSize(count)); + serializer.WriteData(const_data_ptr_cast(flat_mask.GetData()), flat_mask.ValidityMaskSize(count)); } if (TypeIsConstantSize(type.InternalType())) { // constant size type: simple copy @@ -930,7 +930,7 @@ void Vector::Serialize(idx_t count, Serializer &serializer) { for (idx_t i = 0; i < count; i++) { auto idx = vdata.sel->get_index(i); auto source = !vdata.validity.RowIsValid(idx) ? NullValue() : strings[idx]; - serializer.WriteStringLen((const_data_ptr_t)source.GetData(), source.GetSize()); + serializer.WriteStringLen(const_data_ptr_cast(source.GetData()), source.GetSize()); } break; } diff --git a/src/include/duckdb/common/crypto/md5.hpp b/src/include/duckdb/common/crypto/md5.hpp index 0bca329c8eeb..856015c35830 100644 --- a/src/include/duckdb/common/crypto/md5.hpp +++ b/src/include/duckdb/common/crypto/md5.hpp @@ -26,10 +26,10 @@ class MD5Context { } void Add(const char *data); void Add(string_t string) { - MD5Update((const_data_ptr_t)string.GetData(), string.GetSize()); + MD5Update(const_data_ptr_cast(string.GetData()), string.GetSize()); } void Add(const string &data) { - MD5Update((const_data_ptr_t)data.c_str(), data.size()); + MD5Update(const_data_ptr_cast(data.c_str()), data.size()); } //! Write the 16-byte (binary) digest to the specified location diff --git a/src/include/duckdb/common/field_writer.hpp b/src/include/duckdb/common/field_writer.hpp index a15b1ce478d6..8f3cebfeaba1 100644 --- a/src/include/duckdb/common/field_writer.hpp +++ b/src/include/duckdb/common/field_writer.hpp @@ -34,12 +34,12 @@ class FieldWriter { static_assert(std::is_trivially_destructible(), "WriteField object must be trivially destructible"); AddField(); - WriteData((const_data_ptr_t)&element, sizeof(T)); + WriteData(const_data_ptr_cast(&element), sizeof(T)); } //! Write a string with a length prefix void WriteString(const string &val) { - WriteStringLen((const_data_ptr_t)val.c_str(), val.size()); + WriteStringLen(const_data_ptr_cast(val.c_str()), val.size()); } void WriteStringLen(const_data_ptr_t val, idx_t len) { AddField(); @@ -135,7 +135,7 @@ class FieldWriter { private: template void Write(const T &element) { - WriteData((const_data_ptr_t)&element, sizeof(T)); + WriteData(const_data_ptr_cast(&element), sizeof(T)); } DUCKDB_API void WriteData(const_data_ptr_t buffer, idx_t write_size); diff --git a/src/include/duckdb/common/radix.hpp b/src/include/duckdb/common/radix.hpp index 7b4d42658097..ae54a20eb585 100644 --- a/src/include/duckdb/common/radix.hpp +++ b/src/include/duckdb/common/radix.hpp @@ -81,7 +81,7 @@ struct Radix { if (x < -FLT_MAX) { return 0; } - buff = Load((const_data_ptr_t)&x); + buff = Load(const_data_ptr_cast(&x)); if ((buff & (1u << 31)) == 0) { //! +0 and positive numbers buff |= (1u << 31); } else { //! negative numbers @@ -111,7 +111,7 @@ struct Radix { if (x < -DBL_MAX) { return 0; } - buff = Load((const_data_ptr_t)&x); + buff = Load(const_data_ptr_cast(&x)); if (buff < (1ull << 63)) { //! +0 and positive numbers buff += (1ull << 63); } else { //! negative numbers diff --git a/src/include/duckdb/common/serializer.hpp b/src/include/duckdb/common/serializer.hpp index 8294edace80d..c7ba197c35d4 100644 --- a/src/include/duckdb/common/serializer.hpp +++ b/src/include/duckdb/common/serializer.hpp @@ -46,16 +46,16 @@ class Serializer { void Write(T element) { static_assert(std::is_trivially_destructible(), "Write element must be trivially destructible"); - WriteData((const_data_ptr_t)&element, sizeof(T)); + WriteData(const_data_ptr_cast(&element), sizeof(T)); } //! Write data from a string buffer directly (without length prefix) void WriteBufferData(const string &str) { - WriteData((const_data_ptr_t)str.c_str(), str.size()); + WriteData(const_data_ptr_cast(str.c_str()), str.size()); } //! Write a string with a length prefix void WriteString(const string &val) { - WriteStringLen((const_data_ptr_t)val.c_str(), val.size()); + WriteStringLen(const_data_ptr_cast(val.c_str()), val.size()); } void WriteStringLen(const_data_ptr_t val, idx_t len) { Write((uint32_t)len); diff --git a/src/include/duckdb/common/serializer/binary_serializer.hpp b/src/include/duckdb/common/serializer/binary_serializer.hpp index e5d65571975b..cd9726451c78 100644 --- a/src/include/duckdb/common/serializer/binary_serializer.hpp +++ b/src/include/duckdb/common/serializer/binary_serializer.hpp @@ -24,14 +24,14 @@ struct BinarySerializer : public FormatSerializer { template void Write(T element) { static_assert(std::is_trivially_destructible(), "Write element must be trivially destructible"); - WriteData((const_data_ptr_t)&element, sizeof(T)); + WriteData(const_data_ptr_cast(&element), sizeof(T)); } void WriteData(const_data_ptr_t buffer, idx_t write_size) { data.insert(data.end(), buffer, buffer + write_size); stack.back().size += write_size; } void WriteData(const char *ptr, idx_t write_size) { - WriteData((const_data_ptr_t)ptr, write_size); + WriteData(const_data_ptr_cast(ptr), write_size); } explicit BinarySerializer() { diff --git a/src/include/duckdb/common/types/column/partitioned_column_data.hpp b/src/include/duckdb/common/types/column/partitioned_column_data.hpp index 42b4d225be09..000df62f49e6 100644 --- a/src/include/duckdb/common/types/column/partitioned_column_data.hpp +++ b/src/include/duckdb/common/types/column/partitioned_column_data.hpp @@ -108,6 +108,18 @@ class PartitionedColumnData { mutex lock; shared_ptr allocators; vector> partitions; + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; } // namespace duckdb diff --git a/src/include/duckdb/common/types/string_type.hpp b/src/include/duckdb/common/types/string_type.hpp index ff9c6f62fd4b..87b7ee7fc139 100644 --- a/src/include/duckdb/common/types/string_type.hpp +++ b/src/include/duckdb/common/types/string_type.hpp @@ -128,15 +128,15 @@ struct string_t { return false; return (memcmp(a.GetData(), b.GetData(), a.GetSize()) == 0); #endif - uint64_t A = Load((const_data_ptr_t)&a); - uint64_t B = Load((const_data_ptr_t)&b); + uint64_t A = Load(const_data_ptr_cast(&a)); + uint64_t B = Load(const_data_ptr_cast(&b)); if (A != B) { // Either length or prefix are different -> not equal return false; } // they have the same length and same prefix! - A = Load((const_data_ptr_t)&a + 8u); - B = Load((const_data_ptr_t)&b + 8u); + A = Load(const_data_ptr_cast(&a) + 8u); + B = Load(const_data_ptr_cast(&b) + 8u); if (A == B) { // either they are both inlined (so compare equal) or point to the same string (so compare equal) return true; @@ -159,8 +159,8 @@ struct string_t { const uint32_t min_length = std::min(left_length, right_length); #ifndef DUCKDB_DEBUG_NO_INLINE - uint32_t A = Load((const_data_ptr_t)left.GetPrefix()); - uint32_t B = Load((const_data_ptr_t)right.GetPrefix()); + uint32_t A = Load(const_data_ptr_cast(left.GetPrefix())); + uint32_t B = Load(const_data_ptr_cast(right.GetPrefix())); // Utility to move 0xa1b2c3d4 into 0xd4c3b2a1, basically inverting the order byte-a-byte auto bswap = [](uint32_t v) -> uint32_t { diff --git a/src/include/duckdb/common/types/value.hpp b/src/include/duckdb/common/types/value.hpp index 9c2d17e53fa1..5acdcc726bd9 100644 --- a/src/include/duckdb/common/types/value.hpp +++ b/src/include/duckdb/common/types/value.hpp @@ -166,7 +166,7 @@ class Value { //! Create a blob Value from a data pointer and a length: no bytes are interpreted DUCKDB_API static Value BLOB(const_data_ptr_t data, idx_t len); static Value BLOB_RAW(const string &data) { // NOLINT - return Value::BLOB((const_data_ptr_t)data.c_str(), data.size()); + return Value::BLOB(const_data_ptr_cast(data.c_str()), data.size()); } //! Creates a blob by casting a specified string to a blob (i.e. interpreting \x characters) DUCKDB_API static Value BLOB(const string &data); diff --git a/src/include/duckdb/common/types/vector_buffer.hpp b/src/include/duckdb/common/types/vector_buffer.hpp index 61cf90bae2f8..961e36e9922f 100644 --- a/src/include/duckdb/common/types/vector_buffer.hpp +++ b/src/include/duckdb/common/types/vector_buffer.hpp @@ -121,6 +121,18 @@ class VectorBuffer { VectorBufferType buffer_type; unique_ptr aux_data; unsafe_unique_array data; + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; //! The DictionaryBuffer holds a selection vector diff --git a/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp b/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp index 1218aac6f215..a514505b6d1a 100644 --- a/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp +++ b/src/include/duckdb/storage/compression/chimp/chimp_compress.hpp @@ -145,7 +145,7 @@ struct ChimpCompressionState : public CompressionState { current_segment->count++; if (is_valid) { - T floating_point_value = Load((const_data_ptr_t)&value); + T floating_point_value = Load(const_data_ptr_cast(&value)); NumericStats::Update(current_segment->stats.statistics, floating_point_value); } else { //! FIXME: find a cheaper alternative to storing a NULL diff --git a/src/include/duckdb/storage/compression/patas/patas_analyze.hpp b/src/include/duckdb/storage/compression/patas/patas_analyze.hpp index 794c9c894699..6a6bca9918af 100644 --- a/src/include/duckdb/storage/compression/patas/patas_analyze.hpp +++ b/src/include/duckdb/storage/compression/patas/patas_analyze.hpp @@ -102,7 +102,7 @@ struct EmptyPatasWriter { using EXACT_TYPE = typename FloatingToExact::type; auto state_wrapper = (PatasAnalyzeState *)state_p; - state_wrapper->WriteValue(Load((const_data_ptr_t)&uncompressed_value), is_valid); + state_wrapper->WriteValue(Load(const_data_ptr_cast(&uncompressed_value)), is_valid); } }; diff --git a/src/include/duckdb/storage/compression/patas/patas_compress.hpp b/src/include/duckdb/storage/compression/patas/patas_compress.hpp index b6c97b448767..28579dbb2304 100644 --- a/src/include/duckdb/storage/compression/patas/patas_compress.hpp +++ b/src/include/duckdb/storage/compression/patas/patas_compress.hpp @@ -52,7 +52,7 @@ struct PatasCompressionState : public CompressionState { NumericStats::Update(state_wrapper->current_segment->stats.statistics, value); } - state_wrapper->WriteValue(Load((const_data_ptr_t)&value)); + state_wrapper->WriteValue(Load(const_data_ptr_cast(&value))); } }; diff --git a/src/main/client_context.cpp b/src/main/client_context.cpp index f45d63deeeeb..1779f08e3cbd 100644 --- a/src/main/client_context.cpp +++ b/src/main/client_context.cpp @@ -774,8 +774,8 @@ void ClientContext::LogQueryInternal(ClientContextLock &, const string &query) { #endif } // log query path is set: log the query - client_data->log_query_writer->WriteData((const_data_ptr_t)query.c_str(), query.size()); - client_data->log_query_writer->WriteData((const_data_ptr_t) "\n", 1); + client_data->log_query_writer->WriteData(const_data_ptr_cast(query.c_str()), query.size()); + client_data->log_query_writer->WriteData(const_data_ptr_cast("\n"), 1); client_data->log_query_writer->Flush(); client_data->log_query_writer->Sync(); } diff --git a/src/storage/statistics/string_stats.cpp b/src/storage/statistics/string_stats.cpp index d7e53dab303c..65dfba0be8b5 100644 --- a/src/storage/statistics/string_stats.cpp +++ b/src/storage/statistics/string_stats.cpp @@ -166,7 +166,7 @@ void StringStats::Merge(BaseStatistics &stats, const BaseStatistics &other) { FilterPropagateResult StringStats::CheckZonemap(const BaseStatistics &stats, ExpressionType comparison_type, const string &constant) { auto &string_data = StringStats::GetDataUnsafe(stats); - auto data = (const_data_ptr_t)constant.c_str(); + auto data = const_data_ptr_cast(constant.c_str()); auto size = constant.size(); idx_t value_size = size > StringStatsData::MAX_STRING_MINMAX_SIZE ? StringStatsData::MAX_STRING_MINMAX_SIZE : size; @@ -256,12 +256,12 @@ void StringStats::Verify(const BaseStatistics &stats, Vector &vector, const Sele throw InternalException("Invalid unicode detected in vector: %s", vector.ToString(count)); } } - if (StringValueComparison((const_data_ptr_t)data, MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), + if (StringValueComparison(const_data_ptr_cast(data), MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), string_data.min) < 0) { throw InternalException("Statistics mismatch: value is smaller than min.\nStatistics: %s\nVector: %s", stats.ToString(), vector.ToString(count)); } - if (StringValueComparison((const_data_ptr_t)data, MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), + if (StringValueComparison(const_data_ptr_cast(data), MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), string_data.max) > 0) { throw InternalException("Statistics mismatch: value is bigger than max.\nStatistics: %s\nVector: %s", stats.ToString(), vector.ToString(count)); diff --git a/test/api/test_prepared_api.cpp b/test/api/test_prepared_api.cpp index e2d1f6085fd7..80edbc2d5011 100644 --- a/test/api/test_prepared_api.cpp +++ b/test/api/test_prepared_api.cpp @@ -335,7 +335,7 @@ TEST_CASE("Test BLOB with PreparedStatement", "[api]") { REQUIRE_NO_FAIL(con.Query("CREATE TABLE blobs (b BYTEA);")); // Insert blob values through a PreparedStatement - Value blob_val = Value::BLOB((const_data_ptr_t)blob_chars.get(), num_chars); + Value blob_val = Value::BLOB(const_data_ptr_cast(blob_chars.get()), num_chars); duckdb::unique_ptr ps = con.Prepare("INSERT INTO blobs VALUES (?::BYTEA)"); ps->Execute(blob_val); REQUIRE(!ps->HasError()); diff --git a/tools/pythonpkg/src/native/python_conversion.cpp b/tools/pythonpkg/src/native/python_conversion.cpp index b7408c2c80fe..e679fe311ae5 100644 --- a/tools/pythonpkg/src/native/python_conversion.cpp +++ b/tools/pythonpkg/src/native/python_conversion.cpp @@ -359,7 +359,7 @@ Value TransformPythonValue(py::handle ele, const LogicalType &target_type, bool return ele.cast(); case PythonObjectType::ByteArray: { auto byte_array = ele.ptr(); - const_data_ptr_t bytes = (const_data_ptr_t)PyByteArray_AsString(byte_array); + const_data_ptr_t bytes = const_data_ptr_cast(PyByteArray_AsString(byte_array)); idx_t byte_length = PyByteArray_GET_SIZE(byte_array); return Value::BLOB(bytes, byte_length); } diff --git a/tools/sqlite3_api_wrapper/sqlite3_api_wrapper.cpp b/tools/sqlite3_api_wrapper/sqlite3_api_wrapper.cpp index f839fe0ff97b..08063ac650a2 100644 --- a/tools/sqlite3_api_wrapper/sqlite3_api_wrapper.cpp +++ b/tools/sqlite3_api_wrapper/sqlite3_api_wrapper.cpp @@ -688,7 +688,7 @@ int sqlite3_bind_blob(sqlite3_stmt *stmt, int idx, const void *val, int length, if (length < 0) { blob = Value::BLOB(string((const char *)val)); } else { - blob = Value::BLOB((const_data_ptr_t)val, length); + blob = Value::BLOB(const_data_ptr_cast(val), length); } if (free_func && ((ptrdiff_t)free_func) != -1) { free_func((void *)val); From c8ae629f6c72357292b059e28505fce899e0fd42 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 12:18:45 +0200 Subject: [PATCH 14/28] char* and const char* casts --- extension/parquet/column_reader.cpp | 10 ++-- .../types/column/column_data_allocator.cpp | 2 +- .../types/column/column_data_collection.cpp | 2 +- .../column/column_data_collection_segment.cpp | 2 +- src/common/types/list_segment.cpp | 60 +++++++++++++------ .../types/row/tuple_data_scatter_gather.cpp | 4 +- src/common/types/value.cpp | 4 +- src/common/types/vector_cache.cpp | 12 ++-- src/common/types/vector_constants.cpp | 3 +- .../string/regexp/regexp_extract_all.cpp | 2 +- src/function/scalar/string/strip_accents.cpp | 2 +- .../scalar/system/aggregate_export.cpp | 4 +- src/include/duckdb/common/radix.hpp | 2 +- .../duckdb/common/types/null_value.hpp | 2 +- .../duckdb/common/types/string_type.hpp | 8 +-- .../duckdb/common/types/vector_buffer.hpp | 8 +-- .../operator/persistent/csv_buffer.hpp | 2 +- src/include/duckdb/main/capi/cast/utils.hpp | 2 +- src/main/capi/appender-c.cpp | 10 ++-- src/main/capi/arrow-c.cpp | 20 +++---- src/main/capi/cast/from_decimal-c.cpp | 2 +- src/main/capi/cast/utils-c.cpp | 2 +- src/main/capi/config-c.cpp | 2 +- src/main/capi/data_chunk-c.cpp | 34 +++++------ src/main/capi/duckdb-c.cpp | 8 +-- src/main/capi/duckdb_value-c.cpp | 8 +-- src/main/capi/logical_types-c.cpp | 42 ++++++------- src/main/capi/pending-c.cpp | 12 ++-- src/main/capi/prepared-c.cpp | 20 +++---- src/main/capi/replacement_scan-c.cpp | 12 ++-- src/main/capi/result-c.cpp | 44 +++++++------- src/main/capi/table_function-c.cpp | 2 +- src/main/error_manager.cpp | 2 +- .../compression/string_uncompressed.cpp | 2 +- 34 files changed, 188 insertions(+), 165 deletions(-) diff --git a/extension/parquet/column_reader.cpp b/extension/parquet/column_reader.cpp index 57e636ca5ab3..19c3e40bca33 100644 --- a/extension/parquet/column_reader.cpp +++ b/extension/parquet/column_reader.cpp @@ -284,22 +284,22 @@ void ColumnReader::PreparePageV2(PageHeader &page_hdr) { uncompressed = true; } if (uncompressed) { - trans.read((uint8_t *)block->ptr, page_hdr.compressed_page_size); + trans.read(reinterpret_cast(block->ptr), page_hdr.compressed_page_size); return; } // copy repeats & defines as-is because FOR SOME REASON they are uncompressed auto uncompressed_bytes = page_hdr.data_page_header_v2.repetition_levels_byte_length + page_hdr.data_page_header_v2.definition_levels_byte_length; - trans.read((uint8_t *)block->ptr, uncompressed_bytes); + trans.read(reinterpret_cast(block->ptr), uncompressed_bytes); auto compressed_bytes = page_hdr.compressed_page_size - uncompressed_bytes; AllocateCompressed(compressed_bytes); - trans.read((uint8_t *)compressed_buffer.ptr, compressed_bytes); + trans.read(reinterpret_cast(compressed_buffer.ptr), compressed_bytes); - DecompressInternal(chunk->meta_data.codec, (const char *)compressed_buffer.ptr, compressed_bytes, - (char *)block->ptr + uncompressed_bytes, page_hdr.uncompressed_page_size - uncompressed_bytes); + DecompressInternal(chunk->meta_data.codec, const_char_ptr_cast(compressed_buffer.ptr), compressed_bytes, + char_ptr_cast(block->ptr) + uncompressed_bytes, page_hdr.uncompressed_page_size - uncompressed_bytes); } void ColumnReader::AllocateBlock(idx_t size) { diff --git a/src/common/types/column/column_data_allocator.cpp b/src/common/types/column/column_data_allocator.cpp index ee1334c4bcc0..f17495f33367 100644 --- a/src/common/types/column/column_data_allocator.cpp +++ b/src/common/types/column/column_data_allocator.cpp @@ -196,7 +196,7 @@ void ColumnDataAllocator::UnswizzlePointers(ChunkManagementState &state, Vector // at least one string must be non-inlined, otherwise this function should not be called D_ASSERT(i < end); - auto base_ptr = (char *)GetDataPointer(state, block_id, offset); + auto base_ptr = char_ptr_cast(GetDataPointer(state, block_id, offset)); if (strings[i].GetData() == base_ptr) { // pointers are still valid return; diff --git a/src/common/types/column/column_data_collection.cpp b/src/common/types/column/column_data_collection.cpp index e38dfd4870d0..a71f78a7a54f 100644 --- a/src/common/types/column/column_data_collection.cpp +++ b/src/common/types/column/column_data_collection.cpp @@ -520,7 +520,7 @@ void ColumnDataCopy(ColumnDataMetaData &meta_data, const UnifiedVector } else { D_ASSERT(heap_ptr != nullptr); memcpy(heap_ptr, source_entry.GetData(), source_entry.GetSize()); - target_entry = string_t((const char *)heap_ptr, source_entry.GetSize()); + target_entry = string_t(const_char_ptr_cast(heap_ptr), source_entry.GetSize()); heap_ptr += source_entry.GetSize(); } } diff --git a/src/common/types/column/column_data_collection_segment.cpp b/src/common/types/column/column_data_collection_segment.cpp index 96c21454e71f..fc4868eebcf8 100644 --- a/src/common/types/column/column_data_collection_segment.cpp +++ b/src/common/types/column/column_data_collection_segment.cpp @@ -14,7 +14,7 @@ idx_t ColumnDataCollectionSegment::GetDataSize(idx_t type_size) { } validity_t *ColumnDataCollectionSegment::GetValidityPointer(data_ptr_t base_ptr, idx_t type_size) { - return (validity_t *)(base_ptr + GetDataSize(type_size)); + return reinterpret_cast(base_ptr + GetDataSize(type_size)); } VectorDataIndex ColumnDataCollectionSegment::AllocateVectorInternal(const LogicalType &type, ChunkMetaData &chunk_meta, diff --git a/src/common/types/list_segment.cpp b/src/common/types/list_segment.cpp index a8635cb987a9..dcc160ad8ee2 100644 --- a/src/common/types/list_segment.cpp +++ b/src/common/types/list_segment.cpp @@ -17,10 +17,15 @@ static data_ptr_t AllocatePrimitiveData(Allocator &allocator, uint16_t capacity) } template -static T *GetPrimitiveData(const ListSegment *segment) { +static T *GetPrimitiveData(ListSegment *segment) { return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); } +template +static const T *GetPrimitiveData(const ListSegment *segment) { + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); +} + //===--------------------------------------------------------------------===// // Lists //===--------------------------------------------------------------------===// @@ -32,12 +37,21 @@ static data_ptr_t AllocateListData(Allocator &allocator, uint16_t capacity) { return allocator.AllocateData(GetAllocationSizeList(capacity)); } -static uint64_t *GetListLengthData(const ListSegment *segment) { - return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + segment->capacity * sizeof(bool)); +static uint64_t *GetListLengthData(ListSegment *segment) { + return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); +} + +static const uint64_t *GetListLengthData(const ListSegment *segment) { + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); +} + +static const LinkedList *GetListChildData(const ListSegment *segment) { + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + + segment->capacity * (sizeof(bool) + sizeof(uint64_t))); } -static LinkedList *GetListChildData(const ListSegment *segment) { - return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + +static LinkedList *GetListChildData(ListSegment *segment) { + return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * (sizeof(bool) + sizeof(uint64_t))); } @@ -52,12 +66,20 @@ static data_ptr_t AllocateStructData(Allocator &allocator, uint16_t capacity, id return allocator.AllocateData(GetAllocationSizeStruct(capacity, child_count)); } -static ListSegment **GetStructData(const ListSegment *segment) { +static ListSegment **GetStructData(ListSegment *segment) { return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + segment->capacity * sizeof(bool)); } -static bool *GetNullMask(const ListSegment *segment) { - return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment)); +static const ListSegment * const * GetStructData(const ListSegment *segment) { + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); +} + +static bool *GetNullMask(ListSegment *segment) { + return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment)); +} + +static const bool *GetNullMask(const ListSegment *segment) { + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment)); } static uint16_t GetCapacityForNewSegment(uint16_t capacity) { @@ -339,13 +361,13 @@ static void ReadDataFromPrimitiveSegment(const ListSegmentFunctions &, const Lis } } - auto aggr_vector_data = FlatVector::GetData(result); + auto aggr_vector_data = FlatVector::GetData(result); // load values for (idx_t i = 0; i < segment->count; i++) { if (aggr_vector_validity.RowIsValid(total_count + i)) { auto data = GetPrimitiveData(segment); - ((T *)aggr_vector_data)[total_count + i] = Load(data_ptr_cast(data + i)); + aggr_vector_data[total_count + i] = Load(const_data_ptr_cast(data + i)); } } } @@ -365,7 +387,7 @@ static void ReadDataFromVarcharSegment(const ListSegmentFunctions &, const ListS // append all the child chars to one string string str = ""; - auto linked_child_list = Load(data_ptr_cast(GetListChildData(segment))); + auto linked_child_list = Load(const_data_ptr_cast(GetListChildData(segment))); while (linked_child_list.first_segment) { auto child_segment = linked_child_list.first_segment; auto data = GetPrimitiveData(child_segment); @@ -375,17 +397,17 @@ static void ReadDataFromVarcharSegment(const ListSegmentFunctions &, const ListS linked_child_list.last_segment = nullptr; // use length and (reconstructed) offset to get the correct substrings - auto aggr_vector_data = FlatVector::GetData(result); + auto aggr_vector_data = FlatVector::GetData(result); auto str_length_data = GetListLengthData(segment); // get the substrings and write them to the result vector idx_t offset = 0; for (idx_t i = 0; i < segment->count; i++) { if (!null_mask[i]) { - auto str_length = Load(data_ptr_cast(str_length_data + i)); + auto str_length = Load(const_data_ptr_cast(str_length_data + i)); auto substr = str.substr(offset, str_length); auto str_t = StringVector::AddStringOrBlob(result, substr); - ((string_t *)aggr_vector_data)[total_count + i] = str_t; + aggr_vector_data[total_count + i] = str_t; offset += str_length; } } @@ -416,14 +438,14 @@ static void ReadDataFromListSegment(const ListSegmentFunctions &functions, const // set length and offsets auto list_length_data = GetListLengthData(segment); for (idx_t i = 0; i < segment->count; i++) { - auto list_length = Load(data_ptr_cast(list_length_data + i)); + auto list_length = Load(const_data_ptr_cast(list_length_data + i)); list_vector_data[total_count + i].length = list_length; list_vector_data[total_count + i].offset = offset; offset += list_length; } auto &child_vector = ListVector::GetEntry(result); - auto linked_child_list = Load(data_ptr_cast(GetListChildData(segment))); + auto linked_child_list = Load(const_data_ptr_cast(GetListChildData(segment))); ListVector::Reserve(result, offset); // recurse into the linked list of child values @@ -451,7 +473,7 @@ static void ReadDataFromStructSegment(const ListSegmentFunctions &functions, con D_ASSERT(children.size() == functions.child_functions.size()); auto struct_children = GetStructData(segment); for (idx_t child_count = 0; child_count < children.size(); child_count++) { - auto struct_children_segment = Load(data_ptr_cast(struct_children + child_count)); + auto struct_children_segment = Load(const_data_ptr_cast(struct_children + child_count)); auto &child_function = functions.child_functions[child_count]; child_function.read_data(child_function, struct_children_segment, *children[child_count], total_count); } @@ -487,7 +509,7 @@ static ListSegment *CopyDataFromListSegment(const ListSegmentFunctions &function Allocator &allocator) { // create an empty linked list for the child vector of target - auto source_linked_child_list = Load(data_ptr_cast(GetListChildData(source))); + auto source_linked_child_list = Load(const_data_ptr_cast(GetListChildData(source))); // create the segment auto target = reinterpret_cast(AllocateListData(allocator, source->capacity)); @@ -524,7 +546,7 @@ static ListSegment *CopyDataFromStructSegment(const ListSegmentFunctions &functi for (idx_t i = 0; i < functions.child_functions.size(); i++) { auto child_function = functions.child_functions[i]; - auto source_child_segment = Load(data_ptr_cast(source_child_segments + i)); + auto source_child_segment = Load(const_data_ptr_cast(source_child_segments + i)); auto target_child_segment = child_function.copy_data(child_function, source_child_segment, allocator); Store(target_child_segment, data_ptr_cast(target_child_segments + i)); } diff --git a/src/common/types/row/tuple_data_scatter_gather.cpp b/src/common/types/row/tuple_data_scatter_gather.cpp index e965ee4735d5..3dcecd079d44 100644 --- a/src/common/types/row/tuple_data_scatter_gather.cpp +++ b/src/common/types/row/tuple_data_scatter_gather.cpp @@ -30,7 +30,7 @@ inline void TupleDataValueStore(const string_t &source, const data_ptr_t &row_lo Store(source, row_location + offset_in_row); } else { memcpy(heap_location, source.GetData(), source.GetSize()); - Store(string_t((const char *)heap_location, source.GetSize()), row_location + offset_in_row); + Store(string_t(const_char_ptr_cast(heap_location), source.GetSize()), row_location + offset_in_row); heap_location += source.GetSize(); } } @@ -57,7 +57,7 @@ static inline T TupleDataWithinListValueLoad(const data_ptr_t &location, data_pt template <> inline string_t TupleDataWithinListValueLoad(const data_ptr_t &location, data_ptr_t &heap_location) { const auto size = Load(location); - string_t result((const char *)heap_location, size); + string_t result(const_char_ptr_cast(heap_location), size); heap_location += size; return result; } diff --git a/src/common/types/value.cpp b/src/common/types/value.cpp index 9d9ae9a190fb..e41ecdf2b142 100644 --- a/src/common/types/value.cpp +++ b/src/common/types/value.cpp @@ -730,7 +730,7 @@ Value Value::EMPTYLIST(const LogicalType &child_type) { Value Value::BLOB(const_data_ptr_t data, idx_t len) { Value result(LogicalType::BLOB); result.is_null = false; - result.value_info_ = make_shared(string((const char *)data, len)); + result.value_info_ = make_shared(string(const_char_ptr_cast(data), len)); return result; } @@ -744,7 +744,7 @@ Value Value::BLOB(const string &data) { Value Value::BIT(const_data_ptr_t data, idx_t len) { Value result(LogicalType::BIT); result.is_null = false; - result.value_info_ = make_shared(string((const char *)data, len)); + result.value_info_ = make_shared(string(const_char_ptr_cast(data), len)); return result; } diff --git a/src/common/types/vector_cache.cpp b/src/common/types/vector_cache.cpp index 61f66b7a2d8b..12f8827e6aab 100644 --- a/src/common/types/vector_cache.cpp +++ b/src/common/types/vector_cache.cpp @@ -48,8 +48,8 @@ class VectorCacheBuffer : public VectorBuffer { // reinitialize the VectorListBuffer AssignSharedPointer(result.auxiliary, auxiliary); // propagate through child - auto &child_cache = (VectorCacheBuffer &)*child_caches[0]; - auto &list_buffer = (VectorListBuffer &)*result.auxiliary; + auto &child_cache = child_caches[0]->Cast(); + auto &list_buffer = result.auxiliary->Cast(); list_buffer.SetCapacity(child_cache.capacity); list_buffer.SetSize(0); list_buffer.SetAuxiliaryData(nullptr); @@ -65,9 +65,9 @@ class VectorCacheBuffer : public VectorBuffer { auxiliary->SetAuxiliaryData(nullptr); AssignSharedPointer(result.auxiliary, auxiliary); // propagate through children - auto &children = ((VectorStructBuffer &)*result.auxiliary).GetChildren(); + auto &children = result.auxiliary->Cast().GetChildren(); for (idx_t i = 0; i < children.size(); i++) { - auto &child_cache = (VectorCacheBuffer &)*child_caches[i]; + auto &child_cache = child_caches[i]->Cast(); child_cache.ResetFromCache(*children[i], child_caches[i]); } break; @@ -103,12 +103,12 @@ VectorCache::VectorCache(Allocator &allocator, const LogicalType &type_p, idx_t void VectorCache::ResetFromCache(Vector &result) const { D_ASSERT(buffer); - auto &vcache = (VectorCacheBuffer &)*buffer; + auto &vcache = buffer->Cast(); vcache.ResetFromCache(result, buffer); } const LogicalType &VectorCache::GetType() const { - auto &vcache = (VectorCacheBuffer &)*buffer; + auto &vcache = buffer->Cast(); return vcache.GetType(); } diff --git a/src/common/types/vector_constants.cpp b/src/common/types/vector_constants.cpp index f8aa0a66c0d6..78c7fb5b2cbe 100644 --- a/src/common/types/vector_constants.cpp +++ b/src/common/types/vector_constants.cpp @@ -3,7 +3,8 @@ namespace duckdb { const SelectionVector *ConstantVector::ZeroSelectionVector() { - static const SelectionVector ZERO_SELECTION_VECTOR = SelectionVector((sel_t *)ConstantVector::ZERO_VECTOR); + static const SelectionVector ZERO_SELECTION_VECTOR = + SelectionVector(const_cast(ConstantVector::ZERO_VECTOR)); // NOLINT return &ZERO_SELECTION_VECTOR; } diff --git a/src/function/scalar/string/regexp/regexp_extract_all.cpp b/src/function/scalar/string/regexp/regexp_extract_all.cpp index 9aec5dc7c1bc..98b50a7952be 100644 --- a/src/function/scalar/string/regexp/regexp_extract_all.cpp +++ b/src/function/scalar/string/regexp/regexp_extract_all.cpp @@ -94,7 +94,7 @@ void ExtractSingleTuple(const string_t &string, duckdb_re2::RE2 &pattern, int32_ } else { // Every group is a substring of the original, we can find out the offset using the pointer // the 'match_group' address is guaranteed to be bigger than that of the source - D_ASSERT((const char *)match_group.begin() >= string.GetData()); + D_ASSERT(const_char_ptr_cast(match_group.begin()) >= string.GetData()); idx_t offset = match_group.begin() - string.GetData(); list_content[child_idx] = string_t(string.GetData() + offset, match_group.size()); } diff --git a/src/function/scalar/string/strip_accents.cpp b/src/function/scalar/string/strip_accents.cpp index feb6f936fb33..758c72646ec7 100644 --- a/src/function/scalar/string/strip_accents.cpp +++ b/src/function/scalar/string/strip_accents.cpp @@ -23,7 +23,7 @@ struct StripAccentsOperator { // non-ascii, perform collation auto stripped = utf8proc_remove_accents((const utf8proc_uint8_t *)input.GetData(), input.GetSize()); - auto result_str = StringVector::AddString(result, (const char *)stripped); + auto result_str = StringVector::AddString(result, const_char_ptr_cast(stripped)); free(stripped); return result_str; } diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index ed086bf43c4e..04d6256e15d0 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -145,11 +145,11 @@ static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Ve continue; } if (state0_data.validity.RowIsValid(state0_idx) && !state1_data.validity.RowIsValid(state1_idx)) { - result_ptr[i] = StringVector::AddStringOrBlob(result, (const char *)state0.GetData(), bind_data.state_size); + result_ptr[i] = StringVector::AddStringOrBlob(result, const_char_ptr_cast(state0.GetData()), bind_data.state_size); continue; } if (!state0_data.validity.RowIsValid(state0_idx) && state1_data.validity.RowIsValid(state1_idx)) { - result_ptr[i] = StringVector::AddStringOrBlob(result, (const char *)state1.GetData(), bind_data.state_size); + result_ptr[i] = StringVector::AddStringOrBlob(result, const_char_ptr_cast(state1.GetData()), bind_data.state_size); continue; } diff --git a/src/include/duckdb/common/radix.hpp b/src/include/duckdb/common/radix.hpp index ae54a20eb585..58e28f428d7f 100644 --- a/src/include/duckdb/common/radix.hpp +++ b/src/include/duckdb/common/radix.hpp @@ -36,7 +36,7 @@ struct Radix { public: static inline bool IsLittleEndian() { int n = 1; - if (*(char *)&n == 1) { + if (*char_ptr_cast(&n) == 1) { return true; } else { return false; diff --git a/src/include/duckdb/common/types/null_value.hpp b/src/include/duckdb/common/types/null_value.hpp index 7671567142a2..af584d17b41b 100644 --- a/src/include/duckdb/common/types/null_value.hpp +++ b/src/include/duckdb/common/types/null_value.hpp @@ -42,7 +42,7 @@ inline string_t NullValue() { template <> inline char *NullValue() { - return (char *)NullValue(); + return (char *) NullValue(); // NOLINT } template <> diff --git a/src/include/duckdb/common/types/string_type.hpp b/src/include/duckdb/common/types/string_type.hpp index 87b7ee7fc139..3749dc17aeb9 100644 --- a/src/include/duckdb/common/types/string_type.hpp +++ b/src/include/duckdb/common/types/string_type.hpp @@ -55,7 +55,7 @@ struct string_t { #else memset(value.pointer.prefix, 0, PREFIX_BYTES); #endif - value.pointer.ptr = (char *)data; + value.pointer.ptr = (char *) data; // NOLINT } } string_t(const char *data) : string_t(data, strlen(data)) { // NOLINT: Allow implicit conversion from `const char*` @@ -69,14 +69,14 @@ struct string_t { } const char *GetData() const { - return IsInlined() ? (const char *)value.inlined.inlined : value.pointer.ptr; + return IsInlined() ? const_char_ptr_cast(value.inlined.inlined) : value.pointer.ptr; } const char *GetDataUnsafe() const { return GetData(); } char *GetDataWriteable() const { - return IsInlined() ? (char *)value.inlined.inlined : value.pointer.ptr; + return IsInlined() ? (char *) value.inlined.inlined : value.pointer.ptr; // NOLINT } const char *GetPrefix() const { @@ -110,7 +110,7 @@ struct string_t { } else { // copy the data into the prefix #ifndef DUCKDB_DEBUG_NO_INLINE - auto dataptr = (char *)GetData(); + auto dataptr = GetData(); memcpy(value.pointer.prefix, dataptr, PREFIX_LENGTH); #else memset(value.pointer.prefix, 0, PREFIX_BYTES); diff --git a/src/include/duckdb/common/types/vector_buffer.hpp b/src/include/duckdb/common/types/vector_buffer.hpp index 961e36e9922f..0d8c1813cb79 100644 --- a/src/include/duckdb/common/types/vector_buffer.hpp +++ b/src/include/duckdb/common/types/vector_buffer.hpp @@ -166,7 +166,7 @@ class DictionaryBuffer : public VectorBuffer { class VectorStringBuffer : public VectorBuffer { public: VectorStringBuffer(); - VectorStringBuffer(VectorBufferType type); + explicit VectorStringBuffer(VectorBufferType type); public: string_t AddString(const char *data, idx_t len) { @@ -219,7 +219,7 @@ class VectorFSSTStringBuffer : public VectorStringBuffer { class VectorStructBuffer : public VectorBuffer { public: VectorStructBuffer(); - VectorStructBuffer(const LogicalType &struct_type, idx_t capacity = STANDARD_VECTOR_SIZE); + explicit VectorStructBuffer(const LogicalType &struct_type, idx_t capacity = STANDARD_VECTOR_SIZE); VectorStructBuffer(Vector &other, const SelectionVector &sel, idx_t count); ~VectorStructBuffer() override; @@ -238,8 +238,8 @@ class VectorStructBuffer : public VectorBuffer { class VectorListBuffer : public VectorBuffer { public: - VectorListBuffer(unique_ptr vector, idx_t initial_capacity = STANDARD_VECTOR_SIZE); - VectorListBuffer(const LogicalType &list_type, idx_t initial_capacity = STANDARD_VECTOR_SIZE); + explicit VectorListBuffer(unique_ptr vector, idx_t initial_capacity = STANDARD_VECTOR_SIZE); + explicit VectorListBuffer(const LogicalType &list_type, idx_t initial_capacity = STANDARD_VECTOR_SIZE); ~VectorListBuffer() override; public: diff --git a/src/include/duckdb/execution/operator/persistent/csv_buffer.hpp b/src/include/duckdb/execution/operator/persistent/csv_buffer.hpp index 68bb749cb907..532b4c629dfc 100644 --- a/src/include/duckdb/execution/operator/persistent/csv_buffer.hpp +++ b/src/include/duckdb/execution/operator/persistent/csv_buffer.hpp @@ -50,7 +50,7 @@ class CSVBuffer { BufferHandle AllocateBuffer(idx_t buffer_size); char *Ptr() { - return (char *)handle.Ptr(); + return char_ptr_cast(handle.Ptr()); } private: diff --git a/src/include/duckdb/main/capi/cast/utils.hpp b/src/include/duckdb/main/capi/cast/utils.hpp index 7fcc01affaa1..fa9a73c68db9 100644 --- a/src/include/duckdb/main/capi/cast/utils.hpp +++ b/src/include/duckdb/main/capi/cast/utils.hpp @@ -82,7 +82,7 @@ struct ToCStringCastWrapper { auto result_size = result_string.GetSize(); auto result_data = result_string.GetData(); - char *allocated_data = (char *)duckdb_malloc(result_size + 1); + char *allocated_data = char_ptr_cast(duckdb_malloc(result_size + 1)); memcpy(allocated_data, result_data, result_size); allocated_data[result_size] = '\0'; result.data = allocated_data; diff --git a/src/main/capi/appender-c.cpp b/src/main/capi/appender-c.cpp index 663fa8ed57e0..8efa299b713e 100644 --- a/src/main/capi/appender-c.cpp +++ b/src/main/capi/appender-c.cpp @@ -12,7 +12,7 @@ using duckdb::timestamp_t; duckdb_state duckdb_appender_create(duckdb_connection connection, const char *schema, const char *table, duckdb_appender *out_appender) { - Connection *conn = (Connection *)connection; + Connection *conn = reinterpret_cast(connection); if (!connection || !table || !out_appender) { return DuckDBError; @@ -39,7 +39,7 @@ duckdb_state duckdb_appender_destroy(duckdb_appender *appender) { return DuckDBError; } duckdb_appender_close(*appender); - auto wrapper = (AppenderWrapper *)*appender; + auto wrapper = reinterpret_cast(*appender); if (wrapper) { delete wrapper; } @@ -52,7 +52,7 @@ duckdb_state duckdb_appender_run_function(duckdb_appender appender, FUN &&functi if (!appender) { return DuckDBError; } - auto wrapper = (AppenderWrapper *)appender; + auto wrapper = reinterpret_cast(appender); if (!wrapper->appender) { return DuckDBError; } @@ -72,7 +72,7 @@ const char *duckdb_appender_error(duckdb_appender appender) { if (!appender) { return nullptr; } - auto wrapper = (AppenderWrapper *)appender; + auto wrapper = reinterpret_cast(appender); if (wrapper->error.empty()) { return nullptr; } @@ -92,7 +92,7 @@ duckdb_state duckdb_append_internal(duckdb_appender appender, T value) { if (!appender) { return DuckDBError; } - auto *appender_instance = (AppenderWrapper *)appender; + auto *appender_instance = reinterpret_cast(appender); try { appender_instance->appender->Append(value); } catch (std::exception &ex) { diff --git a/src/main/capi/arrow-c.cpp b/src/main/capi/arrow-c.cpp index c1098700f268..576e5c96cf5d 100644 --- a/src/main/capi/arrow-c.cpp +++ b/src/main/capi/arrow-c.cpp @@ -23,7 +23,7 @@ duckdb_state duckdb_query_arrow_schema(duckdb_arrow result, duckdb_arrow_schema if (!out_schema) { return DuckDBSuccess; } - auto wrapper = (ArrowResultWrapper *)result; + auto wrapper = reinterpret_cast(result); ArrowConverter::ToArrowSchema((ArrowSchema *)*out_schema, wrapper->result->types, wrapper->result->names, wrapper->timezone_config); return DuckDBSuccess; @@ -33,7 +33,7 @@ duckdb_state duckdb_query_arrow_array(duckdb_arrow result, duckdb_arrow_array *o if (!out_array) { return DuckDBSuccess; } - auto wrapper = (ArrowResultWrapper *)result; + auto wrapper = reinterpret_cast(result); auto success = wrapper->result->TryFetch(wrapper->current_chunk, wrapper->result->GetErrorObject()); if (!success) { // LCOV_EXCL_START return DuckDBError; @@ -41,12 +41,12 @@ duckdb_state duckdb_query_arrow_array(duckdb_arrow result, duckdb_arrow_array *o if (!wrapper->current_chunk || wrapper->current_chunk->size() == 0) { return DuckDBSuccess; } - ArrowConverter::ToArrowArray(*wrapper->current_chunk, (ArrowArray *)*out_array); + ArrowConverter::ToArrowArray(*wrapper->current_chunk, reinterpret_cast(*out_array)); return DuckDBSuccess; } idx_t duckdb_arrow_row_count(duckdb_arrow result) { - auto wrapper = (ArrowResultWrapper *)result; + auto wrapper = reinterpret_cast(result); if (wrapper->result->HasError()) { return 0; } @@ -54,12 +54,12 @@ idx_t duckdb_arrow_row_count(duckdb_arrow result) { } idx_t duckdb_arrow_column_count(duckdb_arrow result) { - auto wrapper = (ArrowResultWrapper *)result; + auto wrapper = reinterpret_cast(result); return wrapper->result->ColumnCount(); } idx_t duckdb_arrow_rows_changed(duckdb_arrow result) { - auto wrapper = (ArrowResultWrapper *)result; + auto wrapper = reinterpret_cast(result); if (wrapper->result->HasError()) { return 0; } @@ -76,20 +76,20 @@ idx_t duckdb_arrow_rows_changed(duckdb_arrow result) { } const char *duckdb_query_arrow_error(duckdb_arrow result) { - auto wrapper = (ArrowResultWrapper *)result; + auto wrapper = reinterpret_cast(result); return wrapper->result->GetError().c_str(); } void duckdb_destroy_arrow(duckdb_arrow *result) { if (*result) { - auto wrapper = (ArrowResultWrapper *)*result; + auto wrapper = reinterpret_cast(*result); delete wrapper; *result = nullptr; } } duckdb_state duckdb_execute_prepared_arrow(duckdb_prepared_statement prepared_statement, duckdb_arrow *out_result) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || wrapper->statement->HasError() || !out_result) { return DuckDBError; } @@ -105,6 +105,6 @@ duckdb_state duckdb_execute_prepared_arrow(duckdb_prepared_statement prepared_st auto result = wrapper->statement->Execute(wrapper->values, false); D_ASSERT(result->type == QueryResultType::MATERIALIZED_RESULT); arrow_wrapper->result = duckdb::unique_ptr_cast(std::move(result)); - *out_result = (duckdb_arrow)arrow_wrapper; + *out_result = reinterpret_cast(arrow_wrapper); return !arrow_wrapper->result->HasError() ? DuckDBSuccess : DuckDBError; } diff --git a/src/main/capi/cast/from_decimal-c.cpp b/src/main/capi/cast/from_decimal-c.cpp index 28cf038e1bb9..e6bc6f98c671 100644 --- a/src/main/capi/cast/from_decimal-c.cpp +++ b/src/main/capi/cast/from_decimal-c.cpp @@ -34,7 +34,7 @@ bool CastDecimalCInternal(duckdb_result *source, duckdb_string &result, idx_t co default: throw duckdb::InternalException("Unimplemented internal type for decimal"); } - result.data = (char *)duckdb_malloc(sizeof(char) * (result_string.GetSize() + 1)); + result.data = reinterpret_cast(duckdb_malloc(sizeof(char) * (result_string.GetSize() + 1))); memcpy(result.data, result_string.GetData(), result_string.GetSize()); result.data[result_string.GetSize()] = '\0'; result.size = result_string.GetSize(); diff --git a/src/main/capi/cast/utils-c.cpp b/src/main/capi/cast/utils-c.cpp index e40d2d54679f..779d96006f4f 100644 --- a/src/main/capi/cast/utils-c.cpp +++ b/src/main/capi/cast/utils-c.cpp @@ -68,7 +68,7 @@ duckdb_blob FetchDefaultValue::Operation() { template <> bool FromCBlobCastWrapper::Operation(duckdb_blob input, duckdb_string &result) { - string_t input_str((const char *)input.data, input.size); + string_t input_str(const_char_ptr_cast(input.data), input.size); return ToCStringCastWrapper::template Operation(input_str, result); } diff --git a/src/main/capi/config-c.cpp b/src/main/capi/config-c.cpp index e7c2e797f338..0287e0737e74 100644 --- a/src/main/capi/config-c.cpp +++ b/src/main/capi/config-c.cpp @@ -16,7 +16,7 @@ duckdb_state duckdb_create_config(duckdb_config *out_config) { } catch (...) { // LCOV_EXCL_START return DuckDBError; } // LCOV_EXCL_STOP - *out_config = (duckdb_config)config; + *out_config = reinterpret_cast(config); return DuckDBSuccess; } diff --git a/src/main/capi/data_chunk-c.cpp b/src/main/capi/data_chunk-c.cpp index 27bfcf8ad734..b73a7a9d274e 100644 --- a/src/main/capi/data_chunk-c.cpp +++ b/src/main/capi/data_chunk-c.cpp @@ -10,7 +10,7 @@ duckdb_data_chunk duckdb_create_data_chunk(duckdb_logical_type *ctypes, idx_t co } duckdb::vector types; for (idx_t i = 0; i < column_count; i++) { - auto ltype = (duckdb::LogicalType *)ctypes[i]; + auto ltype = reinterpret_cast(ctypes[i]); types.push_back(*ltype); } @@ -21,7 +21,7 @@ duckdb_data_chunk duckdb_create_data_chunk(duckdb_logical_type *ctypes, idx_t co void duckdb_destroy_data_chunk(duckdb_data_chunk *chunk) { if (chunk && *chunk) { - auto dchunk = (duckdb::DataChunk *)*chunk; + auto dchunk = reinterpret_cast(*chunk); delete dchunk; *chunk = nullptr; } @@ -31,7 +31,7 @@ void duckdb_data_chunk_reset(duckdb_data_chunk chunk) { if (!chunk) { return; } - auto dchunk = (duckdb::DataChunk *)chunk; + auto dchunk = reinterpret_cast(chunk); dchunk->Reset(); } @@ -39,7 +39,7 @@ idx_t duckdb_data_chunk_get_column_count(duckdb_data_chunk chunk) { if (!chunk) { return 0; } - auto dchunk = (duckdb::DataChunk *)chunk; + auto dchunk = reinterpret_cast(chunk); return dchunk->ColumnCount(); } @@ -47,7 +47,7 @@ duckdb_vector duckdb_data_chunk_get_vector(duckdb_data_chunk chunk, idx_t col_id if (!chunk || col_idx >= duckdb_data_chunk_get_column_count(chunk)) { return nullptr; } - auto dchunk = (duckdb::DataChunk *)chunk; + auto dchunk = reinterpret_cast(chunk); return reinterpret_cast(&dchunk->data[col_idx]); } @@ -55,7 +55,7 @@ idx_t duckdb_data_chunk_get_size(duckdb_data_chunk chunk) { if (!chunk) { return 0; } - auto dchunk = (duckdb::DataChunk *)chunk; + auto dchunk = reinterpret_cast(chunk); return dchunk->size(); } @@ -63,7 +63,7 @@ void duckdb_data_chunk_set_size(duckdb_data_chunk chunk, idx_t size) { if (!chunk) { return; } - auto dchunk = (duckdb::DataChunk *)chunk; + auto dchunk = reinterpret_cast(chunk); dchunk->SetCardinality(size); } @@ -71,7 +71,7 @@ duckdb_logical_type duckdb_vector_get_column_type(duckdb_vector vector) { if (!vector) { return nullptr; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); return reinterpret_cast(new duckdb::LogicalType(v->GetType())); } @@ -79,7 +79,7 @@ void *duckdb_vector_get_data(duckdb_vector vector) { if (!vector) { return nullptr; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); return duckdb::FlatVector::GetData(*v); } @@ -87,7 +87,7 @@ uint64_t *duckdb_vector_get_validity(duckdb_vector vector) { if (!vector) { return nullptr; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); return duckdb::FlatVector::Validity(*v).GetData(); } @@ -95,7 +95,7 @@ void duckdb_vector_ensure_validity_writable(duckdb_vector vector) { if (!vector) { return; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); auto &validity = duckdb::FlatVector::Validity(*v); validity.EnsureWritable(); } @@ -108,7 +108,7 @@ void duckdb_vector_assign_string_element_len(duckdb_vector vector, idx_t index, if (!vector) { return; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); auto data = duckdb::FlatVector::GetData(*v); data[index] = duckdb::StringVector::AddString(*v, str, str_len); } @@ -117,7 +117,7 @@ duckdb_vector duckdb_list_vector_get_child(duckdb_vector vector) { if (!vector) { return nullptr; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); return reinterpret_cast(&duckdb::ListVector::GetEntry(*v)); } @@ -125,7 +125,7 @@ idx_t duckdb_list_vector_get_size(duckdb_vector vector) { if (!vector) { return 0; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); return duckdb::ListVector::GetListSize(*v); } @@ -133,7 +133,7 @@ duckdb_state duckdb_list_vector_set_size(duckdb_vector vector, idx_t size) { if (!vector) { return duckdb_state::DuckDBError; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); duckdb::ListVector::SetListSize(*v, size); return duckdb_state::DuckDBSuccess; } @@ -142,7 +142,7 @@ duckdb_state duckdb_list_vector_reserve(duckdb_vector vector, idx_t required_cap if (!vector) { return duckdb_state::DuckDBError; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); duckdb::ListVector::Reserve(*v, required_capacity); return duckdb_state::DuckDBSuccess; } @@ -151,7 +151,7 @@ duckdb_vector duckdb_struct_vector_get_child(duckdb_vector vector, idx_t index) if (!vector) { return nullptr; } - auto v = (duckdb::Vector *)vector; + auto v = reinterpret_cast(vector); return reinterpret_cast(duckdb::StructVector::GetEntries(*v)[index].get()); } diff --git a/src/main/capi/duckdb-c.cpp b/src/main/capi/duckdb-c.cpp index c149ab0ad47e..e82621118084 100644 --- a/src/main/capi/duckdb-c.cpp +++ b/src/main/capi/duckdb-c.cpp @@ -33,7 +33,7 @@ duckdb_state duckdb_open(const char *path, duckdb_database *out) { void duckdb_close(duckdb_database *database) { if (database && *database) { - auto wrapper = (DatabaseData *)*database; + auto wrapper = reinterpret_cast(*database); delete wrapper; *database = nullptr; } @@ -43,7 +43,7 @@ duckdb_state duckdb_connect(duckdb_database database, duckdb_connection *out) { if (!database || !out) { return DuckDBError; } - auto wrapper = (DatabaseData *)database; + auto wrapper = reinterpret_cast(database); Connection *connection; try { connection = new Connection(*wrapper->database); @@ -56,14 +56,14 @@ duckdb_state duckdb_connect(duckdb_database database, duckdb_connection *out) { void duckdb_disconnect(duckdb_connection *connection) { if (connection && *connection) { - Connection *conn = (Connection *)*connection; + Connection *conn = reinterpret_cast(*connection); delete conn; *connection = nullptr; } } duckdb_state duckdb_query(duckdb_connection connection, const char *query, duckdb_result *out) { - Connection *conn = (Connection *)connection; + Connection *conn = reinterpret_cast(connection); auto result = conn->Query(query); return duckdb_translate_result(std::move(result), out); } diff --git a/src/main/capi/duckdb_value-c.cpp b/src/main/capi/duckdb_value-c.cpp index aef2087e23af..0474963f4c93 100644 --- a/src/main/capi/duckdb_value-c.cpp +++ b/src/main/capi/duckdb_value-c.cpp @@ -2,7 +2,7 @@ void duckdb_destroy_value(duckdb_value *value) { if (value && *value) { - auto val = (duckdb::Value *)*value; + auto val = reinterpret_cast(*value); delete val; *value = nullptr; } @@ -22,18 +22,18 @@ duckdb_value duckdb_create_int64(int64_t input) { } char *duckdb_get_varchar(duckdb_value value) { - auto val = (duckdb::Value *)value; + auto val = reinterpret_cast(value); auto str_val = val->DefaultCastAs(duckdb::LogicalType::VARCHAR); auto &str = duckdb::StringValue::Get(str_val); - auto result = (char *)malloc(sizeof(char *) * (str.size() + 1)); + auto result = reinterpret_cast(malloc(sizeof(char *) * (str.size() + 1))); memcpy(result, str.c_str(), str.size()); result[str.size()] = '\0'; return result; } int64_t duckdb_get_int64(duckdb_value value) { - auto val = (duckdb::Value *)value; + auto val = reinterpret_cast(value); if (!val->DefaultTryCastAs(duckdb::LogicalType::BIGINT)) { return 0; } diff --git a/src/main/capi/logical_types-c.cpp b/src/main/capi/logical_types-c.cpp index 5a3e5c094f75..a9dcb8789596 100644 --- a/src/main/capi/logical_types-c.cpp +++ b/src/main/capi/logical_types-c.cpp @@ -4,7 +4,7 @@ static bool AssertLogicalTypeId(duckdb_logical_type type, duckdb::LogicalTypeId if (!type) { return false; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); if (ltype.id() != type_id) { return false; } @@ -15,7 +15,7 @@ static bool AssertInternalType(duckdb_logical_type type, duckdb::PhysicalType ph if (!type) { return false; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); if (ltype.InternalType() != physical_type) { return false; } @@ -31,7 +31,7 @@ duckdb_logical_type duckdb_create_list_type(duckdb_logical_type type) { return nullptr; } duckdb::LogicalType *ltype = new duckdb::LogicalType; - *ltype = duckdb::LogicalType::LIST(*(duckdb::LogicalType *)type); + *ltype = duckdb::LogicalType::LIST(*reinterpret_cast(type)); return reinterpret_cast(ltype); } @@ -40,7 +40,7 @@ duckdb_logical_type duckdb_create_union_type(duckdb_logical_type member_types_p, if (!member_types_p || !member_names) { return nullptr; } - duckdb::LogicalType *member_types = (duckdb::LogicalType *)member_types_p; + duckdb::LogicalType *member_types = reinterpret_cast(member_types_p); duckdb::LogicalType *mtype = new duckdb::LogicalType; duckdb::child_list_t members; @@ -56,7 +56,7 @@ duckdb_logical_type duckdb_create_map_type(duckdb_logical_type key_type, duckdb_ return nullptr; } duckdb::LogicalType *mtype = new duckdb::LogicalType; - *mtype = duckdb::LogicalType::MAP(*(duckdb::LogicalType *)key_type, *(duckdb::LogicalType *)value_type); + *mtype = duckdb::LogicalType::MAP(*reinterpret_cast(key_type), *reinterpret_cast(value_type)); return reinterpret_cast(mtype); } @@ -68,13 +68,13 @@ duckdb_type duckdb_get_type_id(duckdb_logical_type type) { if (!type) { return DUCKDB_TYPE_INVALID; } - auto ltype = (duckdb::LogicalType *)type; + auto ltype = reinterpret_cast(type); return duckdb::ConvertCPPTypeToC(*ltype); } void duckdb_destroy_logical_type(duckdb_logical_type *type) { if (type && *type) { - auto ltype = (duckdb::LogicalType *)*type; + auto ltype = reinterpret_cast(*type); delete ltype; *type = nullptr; } @@ -84,7 +84,7 @@ uint8_t duckdb_decimal_width(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::DECIMAL)) { return 0; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return duckdb::DecimalType::GetWidth(ltype); } @@ -92,7 +92,7 @@ uint8_t duckdb_decimal_scale(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::DECIMAL)) { return 0; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return duckdb::DecimalType::GetScale(ltype); } @@ -100,7 +100,7 @@ duckdb_type duckdb_decimal_internal_type(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::DECIMAL)) { return DUCKDB_TYPE_INVALID; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); switch (ltype.InternalType()) { case duckdb::PhysicalType::INT16: return DUCKDB_TYPE_SMALLINT; @@ -119,7 +119,7 @@ duckdb_type duckdb_enum_internal_type(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::ENUM)) { return DUCKDB_TYPE_INVALID; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); switch (ltype.InternalType()) { case duckdb::PhysicalType::UINT8: return DUCKDB_TYPE_UTINYINT; @@ -136,7 +136,7 @@ uint32_t duckdb_enum_dictionary_size(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::ENUM)) { return 0; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return duckdb::EnumType::GetSize(ltype); } @@ -144,7 +144,7 @@ char *duckdb_enum_dictionary_value(duckdb_logical_type type, idx_t index) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::ENUM)) { return nullptr; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); auto &vector = duckdb::EnumType::GetValuesInsertOrder(ltype); auto value = vector.GetValue(index); return strdup(duckdb::StringValue::Get(value).c_str()); @@ -155,7 +155,7 @@ duckdb_logical_type duckdb_list_type_child_type(duckdb_logical_type type) { !AssertLogicalTypeId(type, duckdb::LogicalTypeId::MAP)) { return nullptr; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); if (ltype.id() != duckdb::LogicalTypeId::LIST && ltype.id() != duckdb::LogicalTypeId::MAP) { return nullptr; } @@ -166,7 +166,7 @@ duckdb_logical_type duckdb_map_type_key_type(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::MAP)) { return nullptr; } - auto &mtype = *((duckdb::LogicalType *)type); + auto &mtype = *(reinterpret_cast(type)); if (mtype.id() != duckdb::LogicalTypeId::MAP) { return nullptr; } @@ -177,7 +177,7 @@ duckdb_logical_type duckdb_map_type_value_type(duckdb_logical_type type) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::MAP)) { return nullptr; } - auto &mtype = *((duckdb::LogicalType *)type); + auto &mtype = *(reinterpret_cast(type)); if (mtype.id() != duckdb::LogicalTypeId::MAP) { return nullptr; } @@ -188,7 +188,7 @@ idx_t duckdb_struct_type_child_count(duckdb_logical_type type) { if (!AssertInternalType(type, duckdb::PhysicalType::STRUCT)) { return 0; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return duckdb::StructType::GetChildCount(ltype); } @@ -210,7 +210,7 @@ char *duckdb_union_type_member_name(duckdb_logical_type type, idx_t index) { if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::UNION)) { return nullptr; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return strdup(duckdb::UnionType::GetMemberName(ltype, index).c_str()); } @@ -221,7 +221,7 @@ duckdb_logical_type duckdb_union_type_member_type(duckdb_logical_type type, idx_ if (!AssertLogicalTypeId(type, duckdb::LogicalTypeId::UNION)) { return nullptr; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return reinterpret_cast( new duckdb::LogicalType(duckdb::UnionType::GetMemberType(ltype, index))); } @@ -230,7 +230,7 @@ char *duckdb_struct_type_child_name(duckdb_logical_type type, idx_t index) { if (!AssertInternalType(type, duckdb::PhysicalType::STRUCT)) { return nullptr; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); return strdup(duckdb::StructType::GetChildName(ltype, index).c_str()); } @@ -238,7 +238,7 @@ duckdb_logical_type duckdb_struct_type_child_type(duckdb_logical_type type, idx_ if (!AssertInternalType(type, duckdb::PhysicalType::STRUCT)) { return nullptr; } - auto <ype = *((duckdb::LogicalType *)type); + auto <ype = *(reinterpret_cast(type)); if (ltype.InternalType() != duckdb::PhysicalType::STRUCT) { return nullptr; } diff --git a/src/main/capi/pending-c.cpp b/src/main/capi/pending-c.cpp index 8b9975c93c14..292710bb54b9 100644 --- a/src/main/capi/pending-c.cpp +++ b/src/main/capi/pending-c.cpp @@ -14,7 +14,7 @@ duckdb_state duckdb_pending_prepared_internal(duckdb_prepared_statement prepared if (!prepared_statement || !out_result) { return DuckDBError; } - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); auto result = new PendingStatementWrapper(); result->allow_streaming = allow_streaming; try { @@ -25,7 +25,7 @@ duckdb_state duckdb_pending_prepared_internal(duckdb_prepared_statement prepared result->statement = make_uniq(duckdb::PreservedError(ex)); } duckdb_state return_value = !result->statement->HasError() ? DuckDBSuccess : DuckDBError; - *out_result = (duckdb_pending_result)result; + *out_result = reinterpret_cast(result); return return_value; } @@ -43,7 +43,7 @@ void duckdb_destroy_pending(duckdb_pending_result *pending_result) { if (!pending_result || !*pending_result) { return; } - auto wrapper = (PendingStatementWrapper *)*pending_result; + auto wrapper = reinterpret_cast(pending_result); if (wrapper->statement) { wrapper->statement->Close(); } @@ -55,7 +55,7 @@ const char *duckdb_pending_error(duckdb_pending_result pending_result) { if (!pending_result) { return nullptr; } - auto wrapper = (PendingStatementWrapper *)pending_result; + auto wrapper = reinterpret_cast(pending_result); if (!wrapper->statement) { return nullptr; } @@ -66,7 +66,7 @@ duckdb_pending_state duckdb_pending_execute_task(duckdb_pending_result pending_r if (!pending_result) { return DUCKDB_PENDING_ERROR; } - auto wrapper = (PendingStatementWrapper *)pending_result; + auto wrapper = reinterpret_cast(pending_result); if (!wrapper->statement) { return DUCKDB_PENDING_ERROR; } @@ -97,7 +97,7 @@ duckdb_state duckdb_execute_pending(duckdb_pending_result pending_result, duckdb if (!pending_result || !out_result) { return DuckDBError; } - auto wrapper = (PendingStatementWrapper *)pending_result; + auto wrapper = reinterpret_cast(pending_result); if (!wrapper->statement) { return DuckDBError; } diff --git a/src/main/capi/prepared-c.cpp b/src/main/capi/prepared-c.cpp index 061b0fd1e268..9ce25fe75cb2 100644 --- a/src/main/capi/prepared-c.cpp +++ b/src/main/capi/prepared-c.cpp @@ -21,7 +21,7 @@ idx_t duckdb_extract_statements(duckdb_connection connection, const char *query, return 0; } auto wrapper = new ExtractStatementsWrapper(); - Connection *conn = (Connection *)connection; + Connection *conn = reinterpret_cast(connection); try { wrapper->statements = conn->ExtractStatements(query); } catch (const duckdb::ParserException &e) { @@ -35,7 +35,7 @@ idx_t duckdb_extract_statements(duckdb_connection connection, const char *query, duckdb_state duckdb_prepare_extracted_statement(duckdb_connection connection, duckdb_extracted_statements extracted_statements, idx_t index, duckdb_prepared_statement *out_prepared_statement) { - Connection *conn = (Connection *)connection; + Connection *conn = reinterpret_cast(connection); auto source_wrapper = (ExtractStatementsWrapper *)extracted_statements; if (!connection || !out_prepared_statement || index >= source_wrapper->statements.size()) { @@ -62,14 +62,14 @@ duckdb_state duckdb_prepare(duckdb_connection connection, const char *query, return DuckDBError; } auto wrapper = new PreparedStatementWrapper(); - Connection *conn = (Connection *)connection; + Connection *conn = reinterpret_cast(connection); wrapper->statement = conn->Prepare(query); *out_prepared_statement = (duckdb_prepared_statement)wrapper; return !wrapper->statement->HasError() ? DuckDBSuccess : DuckDBError; } const char *duckdb_prepare_error(duckdb_prepared_statement prepared_statement) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || !wrapper->statement->HasError()) { return nullptr; } @@ -77,7 +77,7 @@ const char *duckdb_prepare_error(duckdb_prepared_statement prepared_statement) { } idx_t duckdb_nparams(duckdb_prepared_statement prepared_statement) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || wrapper->statement->HasError()) { return 0; } @@ -85,7 +85,7 @@ idx_t duckdb_nparams(duckdb_prepared_statement prepared_statement) { } duckdb_type duckdb_param_type(duckdb_prepared_statement prepared_statement, idx_t param_idx) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || wrapper->statement->HasError()) { return DUCKDB_TYPE_INVALID; } @@ -97,7 +97,7 @@ duckdb_type duckdb_param_type(duckdb_prepared_statement prepared_statement, idx_ } duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || wrapper->statement->HasError()) { return DuckDBError; } @@ -106,7 +106,7 @@ duckdb_state duckdb_clear_bindings(duckdb_prepared_statement prepared_statement) } static duckdb_state duckdb_bind_value(duckdb_prepared_statement prepared_statement, idx_t param_idx, Value val) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || wrapper->statement->HasError()) { return DuckDBError; } @@ -220,7 +220,7 @@ duckdb_state duckdb_bind_decimal(duckdb_prepared_statement prepared_statement, i duckdb_state duckdb_bind_blob(duckdb_prepared_statement prepared_statement, idx_t param_idx, const void *data, idx_t length) { - return duckdb_bind_value(prepared_statement, param_idx, Value::BLOB((duckdb::const_data_ptr_t)data, length)); + return duckdb_bind_value(prepared_statement, param_idx, Value::BLOB(duckdb::const_data_ptr_cast(data), length)); } duckdb_state duckdb_bind_null(duckdb_prepared_statement prepared_statement, idx_t param_idx) { @@ -228,7 +228,7 @@ duckdb_state duckdb_bind_null(duckdb_prepared_statement prepared_statement, idx_ } duckdb_state duckdb_execute_prepared(duckdb_prepared_statement prepared_statement, duckdb_result *out_result) { - auto wrapper = (PreparedStatementWrapper *)prepared_statement; + auto wrapper = reinterpret_cast(prepared_statement); if (!wrapper || !wrapper->statement || wrapper->statement->HasError()) { return DuckDBError; } diff --git a/src/main/capi/replacement_scan-c.cpp b/src/main/capi/replacement_scan-c.cpp index e3a50f8f2ac3..2941b25a4952 100644 --- a/src/main/capi/replacement_scan-c.cpp +++ b/src/main/capi/replacement_scan-c.cpp @@ -30,7 +30,7 @@ struct CAPIReplacementScanInfo { unique_ptr duckdb_capi_replacement_callback(ClientContext &context, const string &table_name, ReplacementScanData *data) { - auto &scan_data = (CAPIReplacementScanData &)*data; + auto &scan_data = reinterpret_cast(*data); CAPIReplacementScanInfo info(&scan_data); scan_data.callback((duckdb_replacement_scan_info)&info, table_name.c_str(), scan_data.extra_data); @@ -57,7 +57,7 @@ void duckdb_add_replacement_scan(duckdb_database db, duckdb_replacement_callback if (!db || !replacement) { return; } - auto wrapper = (duckdb::DatabaseData *)db; + auto wrapper = reinterpret_cast(db); auto scan_info = duckdb::make_uniq(); scan_info->callback = replacement; scan_info->extra_data = extra_data; @@ -72,7 +72,7 @@ void duckdb_replacement_scan_set_function_name(duckdb_replacement_scan_info info if (!info_p || !function_name) { return; } - auto info = (duckdb::CAPIReplacementScanInfo *)info_p; + auto info = reinterpret_cast(info_p); info->function_name = function_name; } @@ -80,8 +80,8 @@ void duckdb_replacement_scan_add_parameter(duckdb_replacement_scan_info info_p, if (!info_p || !parameter) { return; } - auto info = (duckdb::CAPIReplacementScanInfo *)info_p; - auto val = (duckdb::Value *)parameter; + auto info = reinterpret_cast(info_p); + auto val = reinterpret_cast(parameter); info->parameters.push_back(*val); } @@ -89,6 +89,6 @@ void duckdb_replacement_scan_set_error(duckdb_replacement_scan_info info_p, cons if (!info_p || !error) { return; } - auto info = (duckdb::CAPIReplacementScanInfo *)info_p; + auto info = reinterpret_cast(info_p); info->error = error; } diff --git a/src/main/capi/result-c.cpp b/src/main/capi/result-c.cpp index e29e90632d8a..95be217c5a13 100644 --- a/src/main/capi/result-c.cpp +++ b/src/main/capi/result-c.cpp @@ -19,10 +19,10 @@ struct CStandardConverter : public CBaseConverter { struct CStringConverter { template static DST Convert(SRC input) { - auto result = (char *)duckdb_malloc(input.GetSize() + 1); + auto result = char_ptr_cast(duckdb_malloc(input.GetSize() + 1)); assert(result); memcpy((void *)result, input.GetData(), input.GetSize()); - auto write_arr = (char *)result; + auto write_arr = char_ptr_cast(result); write_arr[input.GetSize()] = '\0'; return result; } @@ -37,7 +37,7 @@ struct CBlobConverter { template static DST Convert(SRC input) { duckdb_blob result; - result.data = (char *)duckdb_malloc(input.GetSize()); + result.data = char_ptr_cast(duckdb_malloc(input.GetSize())); result.size = input.GetSize(); assert(result.data); memcpy(result.data, input.GetData(), input.GetSize()); @@ -266,7 +266,7 @@ duckdb_state duckdb_translate_result(unique_ptr result_p, duckdb_re if (result.HasError()) { // write the error message - out->__deprecated_error_message = (char *)result.GetError().c_str(); + out->__deprecated_error_message = (char *) result.GetError().c_str(); // NOLINT return DuckDBError; } // copy the data @@ -280,7 +280,7 @@ bool deprecated_materialize_result(duckdb_result *result) { if (!result) { return false; } - auto result_data = (duckdb::DuckDBResultData *)result->internal_data; + auto result_data = reinterpret_cast(result->internal_data); if (result_data->result->HasError()) { return false; } @@ -310,14 +310,14 @@ bool deprecated_materialize_result(duckdb_result *result) { result_data->result = stream_result.Materialize(); } D_ASSERT(result_data->result->type == QueryResultType::MATERIALIZED_RESULT); - auto &materialized = (MaterializedQueryResult &)*result_data->result; + auto &materialized = reinterpret_cast(*result_data->result); // convert the result to a materialized result // zero initialize the columns (so we can cleanly delete it in case a malloc fails) memset(result->__deprecated_columns, 0, sizeof(duckdb_column) * column_count); for (idx_t i = 0; i < column_count; i++) { result->__deprecated_columns[i].__deprecated_type = ConvertCPPTypeToC(result_data->result->types[i]); - result->__deprecated_columns[i].__deprecated_name = (char *)result_data->result->names[i].c_str(); + result->__deprecated_columns[i].__deprecated_name = (char *) result_data->result->names[i].c_str(); // NOLINT } result->__deprecated_row_count = materialized.RowCount(); if (result->__deprecated_row_count > 0 && @@ -344,7 +344,7 @@ static void DuckdbDestroyColumn(duckdb_column column, idx_t count) { if (column.__deprecated_data) { if (column.__deprecated_type == DUCKDB_TYPE_VARCHAR) { // varchar, delete individual strings - auto data = (char **)column.__deprecated_data; + auto data = reinterpret_cast(column.__deprecated_data); for (idx_t i = 0; i < count; i++) { if (data[i]) { duckdb_free(data[i]); @@ -352,7 +352,7 @@ static void DuckdbDestroyColumn(duckdb_column column, idx_t count) { } } else if (column.__deprecated_type == DUCKDB_TYPE_BLOB) { // blob, delete individual blobs - auto data = (duckdb_blob *)column.__deprecated_data; + auto data = reinterpret_cast(column.__deprecated_data); for (idx_t i = 0; i < count; i++) { if (data[i].data) { duckdb_free((void *)data[i].data); @@ -374,7 +374,7 @@ void duckdb_destroy_result(duckdb_result *result) { duckdb_free(result->__deprecated_columns); } if (result->internal_data) { - auto result_data = (duckdb::DuckDBResultData *)result->internal_data; + auto result_data = reinterpret_cast(result->internal_data); delete result_data; } memset(result, 0, sizeof(duckdb_result)); @@ -384,7 +384,7 @@ const char *duckdb_column_name(duckdb_result *result, idx_t col) { if (!result || col >= duckdb_column_count(result)) { return nullptr; } - auto &result_data = *((duckdb::DuckDBResultData *)result->internal_data); + auto &result_data = *(reinterpret_cast(result->internal_data)); return result_data.result->names[col].c_str(); } @@ -392,7 +392,7 @@ duckdb_type duckdb_column_type(duckdb_result *result, idx_t col) { if (!result || col >= duckdb_column_count(result)) { return DUCKDB_TYPE_INVALID; } - auto &result_data = *((duckdb::DuckDBResultData *)result->internal_data); + auto &result_data = *(reinterpret_cast(result->internal_data)); return duckdb::ConvertCPPTypeToC(result_data.result->types[col]); } @@ -400,7 +400,7 @@ duckdb_logical_type duckdb_column_logical_type(duckdb_result *result, idx_t col) if (!result || col >= duckdb_column_count(result)) { return nullptr; } - auto &result_data = *((duckdb::DuckDBResultData *)result->internal_data); + auto &result_data = *(reinterpret_cast(result->internal_data)); return reinterpret_cast(new duckdb::LogicalType(result_data.result->types[col])); } @@ -408,7 +408,7 @@ idx_t duckdb_column_count(duckdb_result *result) { if (!result) { return 0; } - auto &result_data = *((duckdb::DuckDBResultData *)result->internal_data); + auto &result_data = *(reinterpret_cast(result->internal_data)); return result_data.result->ColumnCount(); } @@ -416,12 +416,12 @@ idx_t duckdb_row_count(duckdb_result *result) { if (!result) { return 0; } - auto &result_data = *((duckdb::DuckDBResultData *)result->internal_data); + auto &result_data = *(reinterpret_cast(result->internal_data)); if (result_data.result->type == duckdb::QueryResultType::STREAM_RESULT) { // We can't know the row count beforehand return 0; } - auto &materialized = (duckdb::MaterializedQueryResult &)*result_data.result; + auto &materialized = reinterpret_cast(*result_data.result); return materialized.RowCount(); } @@ -459,7 +459,7 @@ const char *duckdb_result_error(duckdb_result *result) { if (!result) { return nullptr; } - auto &result_data = *((duckdb::DuckDBResultData *)result->internal_data); + auto &result_data = *(reinterpret_cast(result->internal_data)); return !result_data.result->HasError() ? nullptr : result_data.result->GetError().c_str(); } @@ -467,7 +467,7 @@ idx_t duckdb_result_chunk_count(duckdb_result result) { if (!result.internal_data) { return 0; } - auto &result_data = *((duckdb::DuckDBResultData *)result.internal_data); + auto &result_data = *(reinterpret_cast(result.internal_data)); if (result_data.result_set_type == duckdb::CAPIResultSetType::CAPI_RESULT_TYPE_DEPRECATED) { return 0; } @@ -475,7 +475,7 @@ idx_t duckdb_result_chunk_count(duckdb_result result) { // Can't know beforehand how many chunks are returned. return 0; } - auto &materialized = (duckdb::MaterializedQueryResult &)*result_data.result; + auto &materialized = reinterpret_cast(*result_data.result); return materialized.Collection().ChunkCount(); } @@ -483,7 +483,7 @@ duckdb_data_chunk duckdb_result_get_chunk(duckdb_result result, idx_t chunk_idx) if (!result.internal_data) { return nullptr; } - auto &result_data = *((duckdb::DuckDBResultData *)result.internal_data); + auto &result_data = *(reinterpret_cast(result.internal_data)); if (result_data.result_set_type == duckdb::CAPIResultSetType::CAPI_RESULT_TYPE_DEPRECATED) { return nullptr; } @@ -492,7 +492,7 @@ duckdb_data_chunk duckdb_result_get_chunk(duckdb_result result, idx_t chunk_idx) return nullptr; } result_data.result_set_type = duckdb::CAPIResultSetType::CAPI_RESULT_TYPE_MATERIALIZED; - auto &materialized = (duckdb::MaterializedQueryResult &)*result_data.result; + auto &materialized = reinterpret_cast(*result_data.result); auto &collection = materialized.Collection(); if (chunk_idx >= collection.ChunkCount()) { return nullptr; @@ -510,6 +510,6 @@ bool duckdb_result_is_streaming(duckdb_result result) { if (duckdb_result_error(&result) != nullptr) { return false; } - auto &result_data = *((duckdb::DuckDBResultData *)result.internal_data); + auto &result_data = *(reinterpret_cast(result.internal_data)); return result_data.result->type == duckdb::QueryResultType::STREAM_RESULT; } diff --git a/src/main/capi/table_function-c.cpp b/src/main/capi/table_function-c.cpp index 93b3c61a9d2d..fe1556bfa227 100644 --- a/src/main/capi/table_function-c.cpp +++ b/src/main/capi/table_function-c.cpp @@ -311,7 +311,7 @@ void duckdb_bind_add_result_column(duckdb_bind_info info, const char *name, duck } auto bind_info = (duckdb::CTableInternalBindInfo *)info; bind_info->names.push_back(name); - bind_info->return_types.push_back(*((duckdb::LogicalType *)type)); + bind_info->return_types.push_back(*(reinterpret_cast(type))); } idx_t duckdb_bind_get_parameter_count(duckdb_bind_info info) { diff --git a/src/main/error_manager.cpp b/src/main/error_manager.cpp index 5dd51452fa12..14038198315d 100644 --- a/src/main/error_manager.cpp +++ b/src/main/error_manager.cpp @@ -37,7 +37,7 @@ string ErrorManager::FormatExceptionRecursive(ErrorType error_type, vector(Storage::BLOCK_SIZE, uncompressed_size)); auto decompressed_target_ptr = decompressed_target_handle.Ptr(); MiniZStream s; - s.Decompress((const char *)decompression_ptr, compressed_size, (char *)decompressed_target_ptr, + s.Decompress(const_char_ptr_cast(decompression_ptr), compressed_size, char_ptr_cast(decompressed_target_ptr), uncompressed_size); auto final_buffer = decompressed_target_handle.Ptr(); From c124fc0897afb6dbe975025291ce1999466b1cd9 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 12:21:09 +0200 Subject: [PATCH 15/28] Missing dereference --- src/main/capi/pending-c.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/capi/pending-c.cpp b/src/main/capi/pending-c.cpp index 292710bb54b9..aa5fd4d59d91 100644 --- a/src/main/capi/pending-c.cpp +++ b/src/main/capi/pending-c.cpp @@ -43,7 +43,7 @@ void duckdb_destroy_pending(duckdb_pending_result *pending_result) { if (!pending_result || !*pending_result) { return; } - auto wrapper = reinterpret_cast(pending_result); + auto wrapper = reinterpret_cast(*pending_result); if (wrapper->statement) { wrapper->statement->Close(); } From 88ace2dd2d39ef0e5b4132fb0f844a351bded4a0 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 12:33:11 +0200 Subject: [PATCH 16/28] Remove C-style casts from vector and others --- src/common/fsst.cpp | 13 +- .../types/column/partitioned_column_data.cpp | 4 +- src/common/types/row/tuple_data_allocator.cpp | 4 +- src/common/types/vector.cpp | 159 +++++++++--------- src/include/duckdb/common/fsst.hpp | 4 +- src/storage/compression/fsst.cpp | 4 +- 6 files changed, 96 insertions(+), 92 deletions(-) diff --git a/src/common/fsst.cpp b/src/common/fsst.cpp index e84e02990f6e..e0351d5d9dce 100644 --- a/src/common/fsst.cpp +++ b/src/common/fsst.cpp @@ -4,23 +4,28 @@ #include "fsst.h" namespace duckdb { -string_t FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, Vector &result, unsigned char *compressed_string, + +string_t FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, Vector &result, const char *compressed_string, idx_t compressed_string_len) { D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR); unsigned char decompress_buffer[StringUncompressed::STRING_BLOCK_LIMIT + 1]; + auto fsst_decoder = reinterpret_cast(duckdb_fsst_decoder); + auto compressed_string_ptr = (unsigned char *) compressed_string; // NOLINT auto decompressed_string_size = - duckdb_fsst_decompress(reinterpret_cast(duckdb_fsst_decoder), compressed_string_len, compressed_string, + duckdb_fsst_decompress(fsst_decoder, compressed_string_len, compressed_string_ptr, StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); D_ASSERT(decompressed_string_size <= StringUncompressed::STRING_BLOCK_LIMIT); return StringVector::AddStringOrBlob(result, const_char_ptr_cast(decompress_buffer), decompressed_string_size); } -Value FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, unsigned char *compressed_string, +Value FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, const char *compressed_string, idx_t compressed_string_len) { unsigned char decompress_buffer[StringUncompressed::STRING_BLOCK_LIMIT + 1]; + auto compressed_string_ptr = (unsigned char *) compressed_string; // NOLINT + auto fsst_decoder = reinterpret_cast(duckdb_fsst_decoder); auto decompressed_string_size = - duckdb_fsst_decompress(reinterpret_cast(duckdb_fsst_decoder), compressed_string_len, compressed_string, + duckdb_fsst_decompress(fsst_decoder, compressed_string_len, compressed_string_ptr, StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); D_ASSERT(decompressed_string_size <= StringUncompressed::STRING_BLOCK_LIMIT); diff --git a/src/common/types/column/partitioned_column_data.cpp b/src/common/types/column/partitioned_column_data.cpp index 50bd3f250c07..a7c180a02c9e 100644 --- a/src/common/types/column/partitioned_column_data.cpp +++ b/src/common/types/column/partitioned_column_data.cpp @@ -19,9 +19,9 @@ PartitionedColumnData::PartitionedColumnData(const PartitionedColumnData &other) unique_ptr PartitionedColumnData::CreateShared() { switch (type) { case PartitionedColumnDataType::RADIX: - return make_uniq((RadixPartitionedColumnData &)*this); + return make_uniq(Cast()); case PartitionedColumnDataType::HIVE: - return make_uniq((HivePartitionedColumnData &)*this); + return make_uniq(Cast()); default: throw NotImplementedException("CreateShared for this type of PartitionedColumnData"); } diff --git a/src/common/types/row/tuple_data_allocator.cpp b/src/common/types/row/tuple_data_allocator.cpp index 7033903057ad..d044cd93bf02 100644 --- a/src/common/types/row/tuple_data_allocator.cpp +++ b/src/common/types/row/tuple_data_allocator.cpp @@ -241,8 +241,8 @@ void TupleDataAllocator::InitializeChunkStateInternal(TupleDataPinState &pin_sta lock_guard guard(part->lock); const auto old_base_heap_ptr = part->base_heap_ptr; if (old_base_heap_ptr != new_base_heap_ptr) { - Vector old_heap_ptrs(Value::POINTER((uintptr_t)old_base_heap_ptr + part->heap_block_offset)); - Vector new_heap_ptrs(Value::POINTER((uintptr_t)new_base_heap_ptr + part->heap_block_offset)); + Vector old_heap_ptrs(Value::POINTER(CastPointerToValue(old_base_heap_ptr + part->heap_block_offset))); + Vector new_heap_ptrs(Value::POINTER(CastPointerToValue(new_base_heap_ptr + part->heap_block_offset))); RecomputeHeapPointers(old_heap_ptrs, *ConstantVector::ZeroSelectionVector(), row_locations, new_heap_ptrs, offset, next, layout, 0); part->base_heap_ptr = new_base_heap_ptr; diff --git a/src/common/types/vector.cpp b/src/common/types/vector.cpp index 255acf8a9e0a..9d9e62517c48 100644 --- a/src/common/types/vector.cpp +++ b/src/common/types/vector.cpp @@ -199,7 +199,7 @@ void Vector::Slice(const SelectionVector &sel, idx_t count, SelCache &cache) { auto entry = cache.cache.find(target_data); if (entry != cache.cache.end()) { // cached entry exists: use that - this->buffer = make_buffer(((DictionaryBuffer &)*entry->second).GetSelVector()); + this->buffer = make_buffer(entry->second->Cast().GetSelVector()); vector_type = VectorType::DICTIONARY_VECTOR; } else { Slice(sel, count); @@ -248,7 +248,7 @@ struct DataArrays { void FindChildren(vector &to_resize, VectorBuffer &auxiliary) { if (auxiliary.GetBufferType() == VectorBufferType::LIST_BUFFER) { - auto &buffer = (VectorListBuffer &)auxiliary; + auto &buffer = auxiliary.Cast(); auto &child = buffer.GetChild(); auto data = child.GetData(); if (!data) { @@ -263,7 +263,7 @@ void FindChildren(vector &to_resize, VectorBuffer &auxiliary) { to_resize.emplace_back(arrays); } } else if (auxiliary.GetBufferType() == VectorBufferType::STRUCT_BUFFER) { - auto &buffer = (VectorStructBuffer &)auxiliary; + auto &buffer = auxiliary.Cast(); auto &children = buffer.GetChildren(); for (auto &child : children) { auto data = child->GetData(); @@ -329,46 +329,46 @@ void Vector::SetValue(idx_t index, const Value &val) { switch (GetType().InternalType()) { case PhysicalType::BOOL: - ((bool *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::INT8: - ((int8_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::INT16: - ((int16_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::INT32: - ((int32_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::INT64: - ((int64_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::INT128: - ((hugeint_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::UINT8: - ((uint8_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::UINT16: - ((uint16_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::UINT32: - ((uint32_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::UINT64: - ((uint64_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::FLOAT: - ((float *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::DOUBLE: - ((double *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::INTERVAL: - ((interval_t *)data)[index] = val.GetValueUnsafe(); + reinterpret_cast(data)[index] = val.GetValueUnsafe(); break; case PhysicalType::VARCHAR: - ((string_t *)data)[index] = StringVector::AddStringOrBlob(*this, StringValue::Get(val)); + reinterpret_cast(data)[index] = StringVector::AddStringOrBlob(*this, StringValue::Get(val)); break; case PhysicalType::STRUCT: { D_ASSERT(GetVectorType() == VectorType::CONSTANT_VECTOR || GetVectorType() == VectorType::FLAT_VECTOR); @@ -399,7 +399,7 @@ void Vector::SetValue(idx_t index, const Value &val) { } } //! now set the pointer - auto &entry = ((list_entry_t *)data)[index]; + auto &entry = reinterpret_cast(data)[index]; entry.length = val_children.size(); entry.offset = offset; break; @@ -454,64 +454,64 @@ Value Vector::GetValueInternal(const Vector &v_p, idx_t index_p) { if (vector->GetType().InternalType() != PhysicalType::VARCHAR) { throw InternalException("FSST Vector with non-string datatype found!"); } - auto str_compressed = ((string_t *)data)[index]; + auto str_compressed = reinterpret_cast(data)[index]; Value result = FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(const_cast(*vector)), - (unsigned char *)str_compressed.GetData(), str_compressed.GetSize()); + str_compressed.GetData(), str_compressed.GetSize()); return result; } switch (vector->GetType().id()) { case LogicalTypeId::BOOLEAN: - return Value::BOOLEAN(((bool *)data)[index]); + return Value::BOOLEAN(reinterpret_cast(data)[index]); case LogicalTypeId::TINYINT: - return Value::TINYINT(((int8_t *)data)[index]); + return Value::TINYINT(reinterpret_cast(data)[index]); case LogicalTypeId::SMALLINT: - return Value::SMALLINT(((int16_t *)data)[index]); + return Value::SMALLINT(reinterpret_cast(data)[index]); case LogicalTypeId::INTEGER: - return Value::INTEGER(((int32_t *)data)[index]); + return Value::INTEGER(reinterpret_cast(data)[index]); case LogicalTypeId::DATE: - return Value::DATE(((date_t *)data)[index]); + return Value::DATE(reinterpret_cast(data)[index]); case LogicalTypeId::TIME: - return Value::TIME(((dtime_t *)data)[index]); + return Value::TIME(reinterpret_cast(data)[index]); case LogicalTypeId::TIME_TZ: - return Value::TIMETZ(((dtime_t *)data)[index]); + return Value::TIMETZ(reinterpret_cast(data)[index]); case LogicalTypeId::BIGINT: - return Value::BIGINT(((int64_t *)data)[index]); + return Value::BIGINT(reinterpret_cast(data)[index]); case LogicalTypeId::UTINYINT: - return Value::UTINYINT(((uint8_t *)data)[index]); + return Value::UTINYINT(reinterpret_cast(data)[index]); case LogicalTypeId::USMALLINT: - return Value::USMALLINT(((uint16_t *)data)[index]); + return Value::USMALLINT(reinterpret_cast(data)[index]); case LogicalTypeId::UINTEGER: - return Value::UINTEGER(((uint32_t *)data)[index]); + return Value::UINTEGER(reinterpret_cast(data)[index]); case LogicalTypeId::UBIGINT: - return Value::UBIGINT(((uint64_t *)data)[index]); + return Value::UBIGINT(reinterpret_cast(data)[index]); case LogicalTypeId::TIMESTAMP: - return Value::TIMESTAMP(((timestamp_t *)data)[index]); + return Value::TIMESTAMP(reinterpret_cast(data)[index]); case LogicalTypeId::TIMESTAMP_NS: - return Value::TIMESTAMPNS(((timestamp_t *)data)[index]); + return Value::TIMESTAMPNS(reinterpret_cast(data)[index]); case LogicalTypeId::TIMESTAMP_MS: - return Value::TIMESTAMPMS(((timestamp_t *)data)[index]); + return Value::TIMESTAMPMS(reinterpret_cast(data)[index]); case LogicalTypeId::TIMESTAMP_SEC: - return Value::TIMESTAMPSEC(((timestamp_t *)data)[index]); + return Value::TIMESTAMPSEC(reinterpret_cast(data)[index]); case LogicalTypeId::TIMESTAMP_TZ: - return Value::TIMESTAMPTZ(((timestamp_t *)data)[index]); + return Value::TIMESTAMPTZ(reinterpret_cast(data)[index]); case LogicalTypeId::HUGEINT: - return Value::HUGEINT(((hugeint_t *)data)[index]); + return Value::HUGEINT(reinterpret_cast(data)[index]); case LogicalTypeId::UUID: - return Value::UUID(((hugeint_t *)data)[index]); + return Value::UUID(reinterpret_cast(data)[index]); case LogicalTypeId::DECIMAL: { auto width = DecimalType::GetWidth(type); auto scale = DecimalType::GetScale(type); switch (type.InternalType()) { case PhysicalType::INT16: - return Value::DECIMAL(((int16_t *)data)[index], width, scale); + return Value::DECIMAL(reinterpret_cast(data)[index], width, scale); case PhysicalType::INT32: - return Value::DECIMAL(((int32_t *)data)[index], width, scale); + return Value::DECIMAL(reinterpret_cast(data)[index], width, scale); case PhysicalType::INT64: - return Value::DECIMAL(((int64_t *)data)[index], width, scale); + return Value::DECIMAL(reinterpret_cast(data)[index], width, scale); case PhysicalType::INT128: - return Value::DECIMAL(((hugeint_t *)data)[index], width, scale); + return Value::DECIMAL(reinterpret_cast(data)[index], width, scale); default: throw InternalException("Physical type '%s' has a width bigger than 38, which is not supported", TypeIdToString(type.InternalType())); @@ -520,38 +520,38 @@ Value Vector::GetValueInternal(const Vector &v_p, idx_t index_p) { case LogicalTypeId::ENUM: { switch (type.InternalType()) { case PhysicalType::UINT8: - return Value::ENUM(((uint8_t *)data)[index], type); + return Value::ENUM(reinterpret_cast(data)[index], type); case PhysicalType::UINT16: - return Value::ENUM(((uint16_t *)data)[index], type); + return Value::ENUM(reinterpret_cast(data)[index], type); case PhysicalType::UINT32: - return Value::ENUM(((uint32_t *)data)[index], type); + return Value::ENUM(reinterpret_cast(data)[index], type); default: throw InternalException("ENUM can only have unsigned integers as physical types"); } } case LogicalTypeId::POINTER: - return Value::POINTER(((uintptr_t *)data)[index]); + return Value::POINTER(reinterpret_cast(data)[index]); case LogicalTypeId::FLOAT: - return Value::FLOAT(((float *)data)[index]); + return Value::FLOAT(reinterpret_cast(data)[index]); case LogicalTypeId::DOUBLE: - return Value::DOUBLE(((double *)data)[index]); + return Value::DOUBLE(reinterpret_cast(data)[index]); case LogicalTypeId::INTERVAL: - return Value::INTERVAL(((interval_t *)data)[index]); + return Value::INTERVAL(reinterpret_cast(data)[index]); case LogicalTypeId::VARCHAR: { - auto str = ((string_t *)data)[index]; + auto str = reinterpret_cast(data)[index]; return Value(str.GetString()); } case LogicalTypeId::AGGREGATE_STATE: case LogicalTypeId::BLOB: { - auto str = ((string_t *)data)[index]; + auto str = reinterpret_cast(data)[index]; return Value::BLOB(const_data_ptr_cast(str.GetData()), str.GetSize()); } case LogicalTypeId::BIT: { - auto str = ((string_t *)data)[index]; + auto str = reinterpret_cast(data)[index]; return Value::BIT(const_data_ptr_cast(str.GetData()), str.GetSize()); } case LogicalTypeId::MAP: { - auto offlen = ((list_entry_t *)data)[index]; + auto offlen = reinterpret_cast(data)[index]; auto &child_vec = ListVector::GetEntry(*vector); duckdb::vector children; for (idx_t i = offlen.offset; i < offlen.offset + offlen.length; i++) { @@ -576,7 +576,7 @@ Value Vector::GetValueInternal(const Vector &v_p, idx_t index_p) { return Value::STRUCT(std::move(children)); } case LogicalTypeId::LIST: { - auto offlen = ((list_entry_t *)data)[index]; + auto offlen = reinterpret_cast(data)[index]; auto &child_vec = ListVector::GetEntry(*vector); duckdb::vector children; for (idx_t i = offlen.offset; i < offlen.offset + offlen.length; i++) { @@ -636,9 +636,9 @@ string Vector::ToString(idx_t count) const { break; case VectorType::FSST_VECTOR: { for (idx_t i = 0; i < count; i++) { - string_t compressed_string = ((string_t *)data)[i]; + string_t compressed_string = reinterpret_cast(data)[i]; Value val = FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(const_cast(*this)), - (unsigned char *)compressed_string.GetData(), + compressed_string.GetData(), compressed_string.GetSize()); retval += GetValue(i).ToString() + (i == count - 1 ? "" : ", "); } @@ -893,7 +893,7 @@ void Vector::ToUnifiedFormat(idx_t count, UnifiedVectorFormat &data) { void Vector::Sequence(int64_t start, int64_t increment, idx_t count) { this->vector_type = VectorType::SEQUENCE_VECTOR; this->buffer = make_buffer(sizeof(int64_t) * 3); - auto data = (int64_t *)buffer->GetData(); + auto data = reinterpret_cast(buffer->GetData()); data[0] = start; data[1] = increment; data[2] = int64_t(count); @@ -1580,7 +1580,7 @@ string_t StringVector::AddString(Vector &vector, string_t data) { vector.auxiliary = make_buffer(); } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::STRING_BUFFER); - auto &string_buffer = (VectorStringBuffer &)*vector.auxiliary; + auto &string_buffer = vector.auxiliary->Cast(); return string_buffer.AddString(data); } @@ -1594,7 +1594,7 @@ string_t StringVector::AddStringOrBlob(Vector &vector, string_t data) { vector.auxiliary = make_buffer(); } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::STRING_BUFFER); - auto &string_buffer = (VectorStringBuffer &)*vector.auxiliary; + auto &string_buffer = vector.auxiliary->Cast(); return string_buffer.AddBlob(data); } @@ -1607,7 +1607,7 @@ string_t StringVector::EmptyString(Vector &vector, idx_t len) { vector.auxiliary = make_buffer(); } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::STRING_BUFFER); - auto &string_buffer = (VectorStringBuffer &)*vector.auxiliary; + auto &string_buffer = vector.auxiliary->Cast(); return string_buffer.EmptyString(len); } @@ -1616,7 +1616,7 @@ void StringVector::AddHandle(Vector &vector, BufferHandle handle) { if (!vector.auxiliary) { vector.auxiliary = make_buffer(); } - auto &string_buffer = (VectorStringBuffer &)*vector.auxiliary; + auto &string_buffer = vector.auxiliary->Cast(); string_buffer.AddHeapReference(make_buffer(std::move(handle))); } @@ -1626,7 +1626,7 @@ void StringVector::AddBuffer(Vector &vector, buffer_ptr buffer) { if (!vector.auxiliary) { vector.auxiliary = make_buffer(); } - auto &string_buffer = (VectorStringBuffer &)*vector.auxiliary; + auto &string_buffer = vector.auxiliary->Cast(); string_buffer.AddHeapReference(std::move(buffer)); } @@ -1661,7 +1661,7 @@ string_t FSSTVector::AddCompressedString(Vector &vector, string_t data) { vector.auxiliary = make_buffer(); } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::FSST_BUFFER); - auto &fsst_string_buffer = (VectorFSSTStringBuffer &)*vector.auxiliary; + auto &fsst_string_buffer = vector.auxiliary->Cast(); return fsst_string_buffer.AddBlob(data); } @@ -1671,8 +1671,8 @@ void *FSSTVector::GetDecoder(const Vector &vector) { throw InternalException("GetDecoder called on FSST Vector without registered buffer"); } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::FSST_BUFFER); - auto &fsst_string_buffer = (VectorFSSTStringBuffer &)*vector.auxiliary; - return (duckdb_fsst_decoder_t *)fsst_string_buffer.GetDecoder(); + auto &fsst_string_buffer = vector.auxiliary->Cast(); + return fsst_string_buffer.GetDecoder(); } void FSSTVector::RegisterDecoder(Vector &vector, buffer_ptr &duckdb_fsst_decoder) { @@ -1683,7 +1683,7 @@ void FSSTVector::RegisterDecoder(Vector &vector, buffer_ptr &duckdb_fsst_d } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::FSST_BUFFER); - auto &fsst_string_buffer = (VectorFSSTStringBuffer &)*vector.auxiliary; + auto &fsst_string_buffer = vector.auxiliary->Cast(); fsst_string_buffer.AddDecoder(duckdb_fsst_decoder); } @@ -1695,7 +1695,7 @@ void FSSTVector::SetCount(Vector &vector, idx_t count) { } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::FSST_BUFFER); - auto &fsst_string_buffer = (VectorFSSTStringBuffer &)*vector.auxiliary; + auto &fsst_string_buffer = vector.auxiliary->Cast(); fsst_string_buffer.SetCount(count); } @@ -1707,7 +1707,7 @@ idx_t FSSTVector::GetCount(Vector &vector) { } D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::FSST_BUFFER); - auto &fsst_string_buffer = (VectorFSSTStringBuffer &)*vector.auxiliary; + auto &fsst_string_buffer = vector.auxiliary->Cast(); return fsst_string_buffer.GetCount(); } @@ -1724,7 +1724,7 @@ void FSSTVector::DecompressVector(const Vector &src, Vector &dst, idx_t src_offs string_t compressed_string = ldata[source_idx]; if (dst_mask.RowIsValid(target_idx) && compressed_string.GetSize() > 0) { tdata[target_idx] = FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(src), dst, - (unsigned char *)compressed_string.GetData(), + compressed_string.GetData(), compressed_string.GetSize()); } else { tdata[target_idx] = string_t(nullptr, 0); @@ -1824,7 +1824,7 @@ vector> &StructVector::GetEntries(Vector &vector) { vector.GetVectorType() == VectorType::CONSTANT_VECTOR); D_ASSERT(vector.auxiliary); D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::STRUCT_BUFFER); - return ((VectorStructBuffer *)vector.auxiliary.get())->GetChildren(); + return vector.auxiliary->Cast().GetChildren(); } const vector> &StructVector::GetEntries(const Vector &vector) { @@ -1844,7 +1844,7 @@ const Vector &ListVector::GetEntry(const Vector &vector) { vector.GetVectorType() == VectorType::CONSTANT_VECTOR); D_ASSERT(vector.auxiliary); D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::LIST_BUFFER); - return ((VectorListBuffer *)vector.auxiliary.get())->GetChild(); + return vector.auxiliary->Cast().GetChild(); } Vector &ListVector::GetEntry(Vector &vector) { @@ -1858,7 +1858,7 @@ void ListVector::Reserve(Vector &vector, idx_t required_capacity) { vector.GetVectorType() == VectorType::CONSTANT_VECTOR); D_ASSERT(vector.auxiliary); D_ASSERT(vector.auxiliary->GetBufferType() == VectorBufferType::LIST_BUFFER); - auto &child_buffer = *((VectorListBuffer *)vector.auxiliary.get()); + auto &child_buffer = vector.auxiliary->Cast(); child_buffer.Reserve(required_capacity); } @@ -1868,7 +1868,7 @@ idx_t ListVector::GetListSize(const Vector &vec) { return ListVector::GetListSize(child); } D_ASSERT(vec.auxiliary); - return ((VectorListBuffer &)*vec.auxiliary).GetSize(); + return vec.auxiliary->Cast().GetSize(); } idx_t ListVector::GetListCapacity(const Vector &vec) { @@ -1877,7 +1877,7 @@ idx_t ListVector::GetListCapacity(const Vector &vec) { return ListVector::GetListSize(child); } D_ASSERT(vec.auxiliary); - return ((VectorListBuffer &)*vec.auxiliary).GetCapacity(); + return vec.auxiliary->Cast().GetCapacity(); } void ListVector::ReferenceEntry(Vector &vector, Vector &other) { @@ -1894,7 +1894,7 @@ void ListVector::SetListSize(Vector &vec, idx_t size) { auto &child = DictionaryVector::Child(vec); ListVector::SetListSize(child, size); } - ((VectorListBuffer &)*vec.auxiliary).SetSize(size); + vec.auxiliary->Cast().SetSize(size); } void ListVector::Append(Vector &target, const Vector &source, idx_t source_size, idx_t source_offset) { @@ -1902,7 +1902,7 @@ void ListVector::Append(Vector &target, const Vector &source, idx_t source_size, //! Nothing to add return; } - auto &target_buffer = (VectorListBuffer &)*target.auxiliary; + auto &target_buffer = target.auxiliary->Cast(); target_buffer.Append(source, source_size, source_offset); } @@ -1912,12 +1912,12 @@ void ListVector::Append(Vector &target, const Vector &source, const SelectionVec //! Nothing to add return; } - auto &target_buffer = (VectorListBuffer &)*target.auxiliary; + auto &target_buffer = target.auxiliary->Cast(); target_buffer.Append(source, sel, source_size, source_offset); } void ListVector::PushBack(Vector &target, const Value &insert) { - auto &target_buffer = (VectorListBuffer &)*target.auxiliary; + auto &target_buffer = target.auxiliary->Cast(); target_buffer.PushBack(insert); } @@ -1989,7 +1989,6 @@ ConsecutiveChildListInfo ListVector::GetConsecutiveChildListInfo(Vector &list, i } void ListVector::GetConsecutiveChildSelVector(Vector &list, SelectionVector &sel, idx_t offset, idx_t count) { - UnifiedVectorFormat unified_list_data; list.ToUnifiedFormat(offset + count, unified_list_data); auto list_data = UnifiedVectorFormat::GetData(unified_list_data); diff --git a/src/include/duckdb/common/fsst.hpp b/src/include/duckdb/common/fsst.hpp index f528bfda418b..b953c7d06135 100644 --- a/src/include/duckdb/common/fsst.hpp +++ b/src/include/duckdb/common/fsst.hpp @@ -12,9 +12,9 @@ namespace duckdb { class FSSTPrimitives { public: - static string_t DecompressValue(void *duckdb_fsst_decoder, Vector &result, unsigned char *compressed_string, + static string_t DecompressValue(void *duckdb_fsst_decoder, Vector &result, const char *compressed_string, idx_t compressed_string_len); - static Value DecompressValue(void *duckdb_fsst_decoder, unsigned char *compressed_string, + static Value DecompressValue(void *duckdb_fsst_decoder, const char *compressed_string, idx_t compressed_string_len); }; } // namespace duckdb diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index cd54a4632a58..c26980d6fd79 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -618,7 +618,7 @@ void FSSTStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &sta if (str_len > 0) { result_data[i + result_offset] = FSSTPrimitives::DecompressValue( - scan_state.duckdb_fsst_decoder.get(), result, (unsigned char *)str_ptr, str_len); + scan_state.duckdb_fsst_decoder.get(), result, str_ptr, str_len); } else { result_data[i + result_offset] = string_t(nullptr, 0); } @@ -669,7 +669,7 @@ void FSSTStorage::StringFetchRow(ColumnSegment &segment, ColumnFetchState &state segment, dict, result, base_ptr, delta_decode_buffer[offsets.unused_delta_decoded_values], string_length); result_data[result_idx] = FSSTPrimitives::DecompressValue( - (void *)&decoder, result, (unsigned char *)compressed_string.GetData(), compressed_string.GetSize()); + (void *)&decoder, result, compressed_string.GetData(), compressed_string.GetSize()); } else { // There's no fsst symtable, this only happens for empty strings or nulls, we can just emit an empty string result_data[result_idx] = string_t(nullptr, 0); From 1821ba4e186f8b50cac2284870f47b4a5dbd77ea Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 13:03:25 +0200 Subject: [PATCH 17/28] Even more C-style cast removal --- extension/parquet/column_reader.cpp | 28 +++--- extension/parquet/column_writer.cpp | 92 +++++++++++-------- extension/parquet/include/column_writer.hpp | 24 +++++ .../include/parquet_rle_bp_decoder.hpp | 7 +- .../parquet/include/resizable_buffer.hpp | 4 + extension/parquet/parquet-extension.cpp | 2 +- .../types/column/column_data_collection.cpp | 2 +- src/common/types/list_segment.cpp | 2 +- .../types/row/partitioned_tuple_data.cpp | 2 +- .../aggregate/physical_streaming_window.cpp | 2 +- .../join/physical_blockwise_nl_join.cpp | 2 +- .../join/physical_nested_loop_join.cpp | 2 +- .../operator/projection/physical_pivot.cpp | 2 +- src/main/client_context.cpp | 2 +- src/main/database_manager.cpp | 2 +- src/main/extension/extension_load.cpp | 2 +- .../compression/dictionary_compression.cpp | 8 +- .../compression/fixed_size_uncompressed.cpp | 4 +- src/storage/compression/fsst.cpp | 8 +- .../compression/string_uncompressed.cpp | 4 +- .../compression/validity_uncompressed.cpp | 10 +- src/storage/single_file_block_manager.cpp | 4 +- src/storage/table/update_segment.cpp | 22 ++--- src/transaction/cleanup_state.cpp | 4 +- src/transaction/commit_state.cpp | 12 +-- src/transaction/rollback_state.cpp | 6 +- tools/pythonpkg/src/pyconnection.cpp | 8 +- 27 files changed, 156 insertions(+), 111 deletions(-) diff --git a/extension/parquet/column_reader.cpp b/extension/parquet/column_reader.cpp index 19c3e40bca33..74bd935ee6fd 100644 --- a/extension/parquet/column_reader.cpp +++ b/extension/parquet/column_reader.cpp @@ -270,7 +270,7 @@ void ColumnReader::ResetPage() { void ColumnReader::PreparePageV2(PageHeader &page_hdr) { D_ASSERT(page_hdr.type == PageType::DATA_PAGE_V2); - auto &trans = (ThriftFileTransport &)*protocol->getTransport(); + auto &trans = reinterpret_cast(*protocol->getTransport()); AllocateBlock(page_hdr.uncompressed_page_size + 1); bool uncompressed = false; @@ -315,7 +315,7 @@ void ColumnReader::AllocateCompressed(idx_t size) { } void ColumnReader::PreparePage(PageHeader &page_hdr) { - auto &trans = (ThriftFileTransport &)*protocol->getTransport(); + auto &trans = reinterpret_cast(*protocol->getTransport()); AllocateBlock(page_hdr.uncompressed_page_size + 1); if (chunk->meta_data.codec == CompressionCodec::UNCOMPRESSED) { @@ -396,7 +396,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { uint32_t rep_length = is_v1 ? block->read() : v2_header.repetition_levels_byte_length; block->available(rep_length); repeated_decoder = - make_uniq((const uint8_t *)block->ptr, rep_length, RleBpDecoder::ComputeBitWidth(max_repeat)); + make_uniq(block->Uint8Ptr(), rep_length, RleBpDecoder::ComputeBitWidth(max_repeat)); block->inc(rep_length); } else if (is_v2 && v2_header.repetition_levels_byte_length > 0) { block->inc(v2_header.repetition_levels_byte_length); @@ -406,7 +406,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { uint32_t def_length = is_v1 ? block->read() : v2_header.definition_levels_byte_length; block->available(def_length); defined_decoder = - make_uniq((const uint8_t *)block->ptr, def_length, RleBpDecoder::ComputeBitWidth(max_define)); + make_uniq(block->Uint8Ptr(), def_length, RleBpDecoder::ComputeBitWidth(max_define)); block->inc(def_length); } else if (is_v2 && v2_header.definition_levels_byte_length > 0) { block->inc(v2_header.definition_levels_byte_length); @@ -418,7 +418,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { // where is it otherwise?? auto dict_width = block->read(); // TODO somehow dict_width can be 0 ? - dict_decoder = make_uniq((const uint8_t *)block->ptr, block->len, dict_width); + dict_decoder = make_uniq(block->Uint8Ptr(), block->len, dict_width); block->inc(block->len); break; } @@ -427,11 +427,11 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { throw std::runtime_error("RLE encoding is only supported for boolean data"); } block->inc(sizeof(uint32_t)); - rle_decoder = make_uniq((const uint8_t *)block->ptr, block->len, 1); + rle_decoder = make_uniq(block->Uint8Ptr(), block->len, 1); break; } case Encoding::DELTA_BINARY_PACKED: { - dbp_decoder = make_uniq((const uint8_t *)block->ptr, block->len); + dbp_decoder = make_uniq(block->Uint8Ptr(), block->len); block->inc(block->len); break; } @@ -478,12 +478,12 @@ idx_t ColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, uint8_t if (HasRepeats()) { D_ASSERT(repeated_decoder); - repeated_decoder->GetBatch((char *)repeat_out + result_offset, read_now); + repeated_decoder->GetBatch(repeat_out + result_offset, read_now); } if (HasDefines()) { D_ASSERT(defined_decoder); - defined_decoder->GetBatch((char *)define_out + result_offset, read_now); + defined_decoder->GetBatch(define_out + result_offset, read_now); } idx_t null_count = 0; @@ -566,7 +566,7 @@ void ColumnReader::ApplyPendingSkips(idx_t num_values) { while (remaining) { idx_t to_read = MinValue(remaining, STANDARD_VECTOR_SIZE); - read += Read(to_read, none_filter, (uint8_t *)dummy_define.ptr, (uint8_t *)dummy_repeat.ptr, dummy_result); + read += Read(to_read, none_filter, dummy_define.Uint8Ptr(), (uint8_t *)dummy_repeat.ptr, dummy_result); remaining -= to_read; } @@ -630,7 +630,7 @@ void StringColumnReader::Dictionary(shared_ptr data, idx_t num } static shared_ptr ReadDbpData(Allocator &allocator, ResizeableBuffer &buffer, idx_t &value_count) { - auto decoder = make_uniq((const uint8_t *)buffer.ptr, buffer.len); + auto decoder = make_uniq(buffer.Uint8Ptr(), buffer.len); value_count = decoder->TotalValues(); auto result = make_shared(); result->resize(allocator, sizeof(uint32_t) * value_count); @@ -1186,7 +1186,7 @@ unique_ptr ParquetDecimalUtils::CreateReader(ParquetReader &reader //===--------------------------------------------------------------------===// struct UUIDValueConversion { static hugeint_t DictRead(ByteBuffer &dict, uint32_t &offset, ColumnReader &reader) { - auto dict_ptr = (hugeint_t *)dict.ptr; + auto dict_ptr = reinterpret_cast(dict.ptr); return dict_ptr[offset]; } @@ -1232,7 +1232,7 @@ class UUIDColumnReader : public TemplatedColumnReader dictionary_data, idx_t num_entries) { // NOLINT AllocateDict(num_entries * sizeof(hugeint_t)); - auto dict_ptr = (hugeint_t *)this->dict->ptr; + auto dict_ptr = reinterpret_cast(this->dict->ptr); for (idx_t i = 0; i < num_entries; i++) { dict_ptr[i] = UUIDValueConversion::PlainRead(*dictionary_data, *this); } @@ -1246,7 +1246,7 @@ struct IntervalValueConversion { static constexpr const idx_t PARQUET_INTERVAL_SIZE = 12; static interval_t DictRead(ByteBuffer &dict, uint32_t &offset, ColumnReader &reader) { - auto dict_ptr = (interval_t *)dict.ptr; + auto dict_ptr = reinterpret_cast(dict.ptr); return dict_ptr[offset]; } diff --git a/extension/parquet/column_writer.cpp b/extension/parquet/column_writer.cpp index 2e113ba50682..9ddeb013f68b 100644 --- a/extension/parquet/column_writer.cpp +++ b/extension/parquet/column_writer.cpp @@ -193,8 +193,8 @@ void ColumnWriter::CompressPage(BufferedSerializer &temp_writer, size_t &compres case CompressionCodec::SNAPPY: { compressed_size = duckdb_snappy::MaxCompressedLength(temp_writer.blob.size); compressed_buf = duckdb::unique_ptr(new data_t[compressed_size]); - duckdb_snappy::RawCompress((const char *)temp_writer.blob.data.get(), temp_writer.blob.size, - (char *)compressed_buf.get(), &compressed_size); + duckdb_snappy::RawCompress(const_char_ptr_cast(temp_writer.blob.data.get()), temp_writer.blob.size, + char_ptr_cast(compressed_buf.get()), &compressed_size); compressed_data = compressed_buf.get(); D_ASSERT(compressed_size <= duckdb_snappy::MaxCompressedLength(temp_writer.blob.size)); break; @@ -203,7 +203,7 @@ void ColumnWriter::CompressPage(BufferedSerializer &temp_writer, size_t &compres MiniZStream s; compressed_size = s.MaxCompressedLength(temp_writer.blob.size); compressed_buf = duckdb::unique_ptr(new data_t[compressed_size]); - s.Compress((const char *)temp_writer.blob.data.get(), temp_writer.blob.size, (char *)compressed_buf.get(), + s.Compress(const_char_ptr_cast(temp_writer.blob.data.get()), temp_writer.blob.size, char_ptr_cast(compressed_buf.get()), &compressed_size); compressed_data = compressed_buf.get(); break; @@ -280,6 +280,18 @@ class ColumnWriterPageState { public: virtual ~ColumnWriterPageState() { } + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; struct PageInformation { @@ -412,7 +424,7 @@ void BasicColumnWriter::FlushPageState(Serializer &temp_writer, ColumnWriterPage } void BasicColumnWriter::Prepare(ColumnWriterState &state_p, ColumnWriterState *parent, Vector &vector, idx_t count) { - auto &state = (BasicColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &col_chunk = state.row_group.columns[state.col_idx]; idx_t start = 0; @@ -448,7 +460,7 @@ duckdb_parquet::format::Encoding::type BasicColumnWriter::GetEncoding(BasicColum } void BasicColumnWriter::BeginWrite(ColumnWriterState &state_p) { - auto &state = (BasicColumnWriterState &)state_p; + auto &state = state_p.Cast(); // set up the page write info state.stats_state = InitializeStatsState(); @@ -576,7 +588,7 @@ idx_t BasicColumnWriter::GetRowSize(Vector &vector, idx_t index, BasicColumnWrit } void BasicColumnWriter::Write(ColumnWriterState &state_p, Vector &vector, idx_t count) { - auto &state = (BasicColumnWriterState &)state_p; + auto &state = state_p.Cast(); idx_t remaining = count; idx_t offset = 0; @@ -640,7 +652,7 @@ void BasicColumnWriter::SetParquetStatistics(BasicColumnWriterState &state, } void BasicColumnWriter::FinalizeWrite(ColumnWriterState &state_p) { - auto &state = (BasicColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &column_chunk = state.row_group.columns[state.col_idx]; // flush the last page (if any remains) @@ -863,10 +875,10 @@ class BooleanStatisticsState : public ColumnWriterStatistics { return GetMaxValue(); } string GetMinValue() override { - return HasStats() ? string((char *)&min, sizeof(bool)) : string(); + return HasStats() ? string(const_char_ptr_cast(&min), sizeof(bool)) : string(); } string GetMaxValue() override { - return HasStats() ? string((char *)&max, sizeof(bool)) : string(); + return HasStats() ? string(const_char_ptr_cast(&max), sizeof(bool)) : string(); } }; @@ -891,8 +903,8 @@ class BooleanColumnWriter : public BasicColumnWriter { void WriteVector(Serializer &temp_writer, ColumnWriterStatistics *stats_p, ColumnWriterPageState *state_p, Vector &input_column, idx_t chunk_start, idx_t chunk_end) override { - auto &stats = (BooleanStatisticsState &)*stats_p; - auto &state = (BooleanWriterPageState &)*state_p; + auto &stats = stats_p->Cast(); + auto &state = state_p->Cast(); auto &mask = FlatVector::Validity(input_column); auto *ptr = FlatVector::GetData(input_column); @@ -921,7 +933,7 @@ class BooleanColumnWriter : public BasicColumnWriter { } void FlushPageState(Serializer &temp_writer, ColumnWriterPageState *state_p) override { - auto &state = (BooleanWriterPageState &)*state_p; + auto &state = state_p->Cast(); if (state.byte_pos > 0) { temp_writer.Write(state.byte); state.byte = 0; @@ -1018,7 +1030,7 @@ class FixedDecimalColumnWriter : public BasicColumnWriter { Vector &input_column, idx_t chunk_start, idx_t chunk_end) override { auto &mask = FlatVector::Validity(input_column); auto *ptr = FlatVector::GetData(input_column); - auto &stats = (FixedDecimalStatistics &)*stats_p; + auto &stats = stats_p->Cast(); data_t temp_buffer[16]; for (idx_t r = chunk_start; r < chunk_end; r++) { @@ -1247,7 +1259,7 @@ class StringColumnWriter : public BasicColumnWriter { } void Analyze(ColumnWriterState &state_p, ColumnWriterState *parent, Vector &vector, idx_t count) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); idx_t vcount = parent ? parent->definition_levels.size() - state.definition_levels.size() : count; idx_t parent_index = state.definition_levels.size(); @@ -1295,7 +1307,7 @@ class StringColumnWriter : public BasicColumnWriter { } void FinalizeAnalyze(ColumnWriterState &state_p) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); // check if a dictionary will require more space than a plain write, or if the dictionary page is going to // be too large @@ -1311,9 +1323,9 @@ class StringColumnWriter : public BasicColumnWriter { void WriteVector(Serializer &temp_writer, ColumnWriterStatistics *stats_p, ColumnWriterPageState *page_state_p, Vector &input_column, idx_t chunk_start, idx_t chunk_end) override { - auto &page_state = (StringWriterPageState &)*page_state_p; + auto &page_state = page_state_p->Cast(); auto &mask = FlatVector::Validity(input_column); - auto &stats = (StringStatisticsState &)*stats_p; + auto &stats = stats_p->Cast(); auto *ptr = FlatVector::GetData(input_column); if (page_state.IsDictionaryEncoded()) { @@ -1348,12 +1360,12 @@ class StringColumnWriter : public BasicColumnWriter { } duckdb::unique_ptr InitializePageState(BasicColumnWriterState &state_p) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); return make_uniq(state.key_bit_width, state.dictionary); } void FlushPageState(Serializer &temp_writer, ColumnWriterPageState *state_p) override { - auto &page_state = (StringWriterPageState &)*state_p; + auto &page_state = state_p->Cast(); if (page_state.bit_width != 0) { if (!page_state.written_value) { // all values are null @@ -1366,24 +1378,24 @@ class StringColumnWriter : public BasicColumnWriter { } duckdb_parquet::format::Encoding::type GetEncoding(BasicColumnWriterState &state_p) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); return state.IsDictionaryEncoded() ? Encoding::RLE_DICTIONARY : Encoding::PLAIN; } bool HasDictionary(BasicColumnWriterState &state_p) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); return state.IsDictionaryEncoded(); } idx_t DictionarySize(BasicColumnWriterState &state_p) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); D_ASSERT(state.IsDictionaryEncoded()); return state.dictionary.size(); } void FlushDictionary(BasicColumnWriterState &state_p, ColumnWriterStatistics *stats_p) override { - auto &stats = (StringStatisticsState &)*stats_p; - auto &state = (StringColumnWriterState &)state_p; + auto &stats = stats_p->Cast(); + auto &state = state_p.Cast(); if (!state.IsDictionaryEncoded()) { return; } @@ -1408,7 +1420,7 @@ class StringColumnWriter : public BasicColumnWriter { } idx_t GetRowSize(Vector &vector, idx_t index, BasicColumnWriterState &state_p) override { - auto &state = (StringColumnWriterState &)state_p; + auto &state = state_p.Cast(); if (state.IsDictionaryEncoded()) { return (state.key_bit_width + 7) / 8; } else { @@ -1471,7 +1483,7 @@ class EnumColumnWriter : public BasicColumnWriter { void WriteVector(Serializer &temp_writer, ColumnWriterStatistics *stats_p, ColumnWriterPageState *page_state_p, Vector &input_column, idx_t chunk_start, idx_t chunk_end) override { - auto &page_state = (EnumWriterPageState &)*page_state_p; + auto &page_state = page_state_p->Cast(); switch (enum_type.InternalType()) { case PhysicalType::UINT8: WriteEnumInternal(temp_writer, input_column, chunk_start, chunk_end, page_state); @@ -1492,7 +1504,7 @@ class EnumColumnWriter : public BasicColumnWriter { } void FlushPageState(Serializer &temp_writer, ColumnWriterPageState *state_p) override { - auto &page_state = (EnumWriterPageState &)*state_p; + auto &page_state = state_p->Cast(); if (!page_state.written_value) { // all values are null // just write the bit width @@ -1515,7 +1527,7 @@ class EnumColumnWriter : public BasicColumnWriter { } void FlushDictionary(BasicColumnWriterState &state, ColumnWriterStatistics *stats_p) override { - auto &stats = (StringStatisticsState &)*stats_p; + auto &stats = stats_p->Cast(); // write the enum values to a dictionary page auto &enum_values = EnumType::GetValuesInsertOrder(enum_type); auto enum_count = EnumType::GetSize(enum_type); @@ -1599,7 +1611,7 @@ bool StructColumnWriter::HasAnalyze() { } void StructColumnWriter::Analyze(ColumnWriterState &state_p, ColumnWriterState *parent, Vector &vector, idx_t count) { - auto &state = (StructColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &child_vectors = StructVector::GetEntries(vector); for (idx_t child_idx = 0; child_idx < child_writers.size(); child_idx++) { // Need to check again. It might be that just one child needs it but the rest not @@ -1611,7 +1623,7 @@ void StructColumnWriter::Analyze(ColumnWriterState &state_p, ColumnWriterState * } void StructColumnWriter::FinalizeAnalyze(ColumnWriterState &state_p) { - auto &state = (StructColumnWriterState &)state_p; + auto &state = state_p.Cast(); for (idx_t child_idx = 0; child_idx < child_writers.size(); child_idx++) { // Need to check again. It might be that just one child needs it but the rest not if (child_writers[child_idx]->HasAnalyze()) { @@ -1621,7 +1633,7 @@ void StructColumnWriter::FinalizeAnalyze(ColumnWriterState &state_p) { } void StructColumnWriter::Prepare(ColumnWriterState &state_p, ColumnWriterState *parent, Vector &vector, idx_t count) { - auto &state = (StructColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &validity = FlatVector::Validity(vector); if (parent) { @@ -1639,14 +1651,14 @@ void StructColumnWriter::Prepare(ColumnWriterState &state_p, ColumnWriterState * } void StructColumnWriter::BeginWrite(ColumnWriterState &state_p) { - auto &state = (StructColumnWriterState &)state_p; + auto &state = state_p.Cast(); for (idx_t child_idx = 0; child_idx < child_writers.size(); child_idx++) { child_writers[child_idx]->BeginWrite(*state.child_states[child_idx]); } } void StructColumnWriter::Write(ColumnWriterState &state_p, Vector &vector, idx_t count) { - auto &state = (StructColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &child_vectors = StructVector::GetEntries(vector); for (idx_t child_idx = 0; child_idx < child_writers.size(); child_idx++) { child_writers[child_idx]->Write(*state.child_states[child_idx], *child_vectors[child_idx], count); @@ -1654,7 +1666,7 @@ void StructColumnWriter::Write(ColumnWriterState &state_p, Vector &vector, idx_t } void StructColumnWriter::FinalizeWrite(ColumnWriterState &state_p) { - auto &state = (StructColumnWriterState &)state_p; + auto &state = state_p.Cast(); for (idx_t child_idx = 0; child_idx < child_writers.size(); child_idx++) { // we add the null count of the struct to the null count of the children child_writers[child_idx]->null_count += null_count; @@ -1713,19 +1725,19 @@ bool ListColumnWriter::HasAnalyze() { return child_writer->HasAnalyze(); } void ListColumnWriter::Analyze(ColumnWriterState &state_p, ColumnWriterState *parent, Vector &vector, idx_t count) { - auto &state = (ListColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &list_child = ListVector::GetEntry(vector); auto list_count = ListVector::GetListSize(vector); child_writer->Analyze(*state.child_state, &state_p, list_child, list_count); } void ListColumnWriter::FinalizeAnalyze(ColumnWriterState &state_p) { - auto &state = (ListColumnWriterState &)state_p; + auto &state = state_p.Cast(); child_writer->FinalizeAnalyze(*state.child_state); } void ListColumnWriter::Prepare(ColumnWriterState &state_p, ColumnWriterState *parent, Vector &vector, idx_t count) { - auto &state = (ListColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto list_data = FlatVector::GetData(vector); auto &validity = FlatVector::Validity(vector); @@ -1782,12 +1794,12 @@ void ListColumnWriter::Prepare(ColumnWriterState &state_p, ColumnWriterState *pa } void ListColumnWriter::BeginWrite(ColumnWriterState &state_p) { - auto &state = (ListColumnWriterState &)state_p; + auto &state = state_p.Cast(); child_writer->BeginWrite(*state.child_state); } void ListColumnWriter::Write(ColumnWriterState &state_p, Vector &vector, idx_t count) { - auto &state = (ListColumnWriterState &)state_p; + auto &state = state_p.Cast(); auto &list_child = ListVector::GetEntry(vector); Vector child_list(list_child); @@ -1796,7 +1808,7 @@ void ListColumnWriter::Write(ColumnWriterState &state_p, Vector &vector, idx_t c } void ListColumnWriter::FinalizeWrite(ColumnWriterState &state_p) { - auto &state = (ListColumnWriterState &)state_p; + auto &state = state_p.Cast(); child_writer->FinalizeWrite(*state.child_state); } diff --git a/extension/parquet/include/column_writer.hpp b/extension/parquet/include/column_writer.hpp index 26b475cccd32..90b939378bc8 100644 --- a/extension/parquet/include/column_writer.hpp +++ b/extension/parquet/include/column_writer.hpp @@ -24,6 +24,18 @@ class ColumnWriterState { vector definition_levels; vector repetition_levels; vector is_empty; + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; class ColumnWriterStatistics { @@ -34,6 +46,18 @@ class ColumnWriterStatistics { virtual string GetMax(); virtual string GetMinValue(); virtual string GetMaxValue(); + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return (TARGET &)*this; + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return (const TARGET &)*this; + } }; class ColumnWriter { diff --git a/extension/parquet/include/parquet_rle_bp_decoder.hpp b/extension/parquet/include/parquet_rle_bp_decoder.hpp index 986682406976..2c70489e5971 100644 --- a/extension/parquet/include/parquet_rle_bp_decoder.hpp +++ b/extension/parquet/include/parquet_rle_bp_decoder.hpp @@ -30,7 +30,7 @@ class RleBpDecoder { template void GetBatch(char *values_target_ptr, uint32_t batch_size) { - auto values = (T *)values_target_ptr; + auto values = reinterpret_cast(values_target_ptr); uint32_t values_read = 0; while (values_read < batch_size) { @@ -62,6 +62,11 @@ class RleBpDecoder { } } + template + void GetBatch(uint8_t *values_target_ptr, uint32_t batch_size) { + return GetBatch(reinterpret_cast(values_target_ptr), batch_size); + } + static uint8_t ComputeBitWidth(idx_t val) { if (val == 0) { return 0; diff --git a/extension/parquet/include/resizable_buffer.hpp b/extension/parquet/include/resizable_buffer.hpp index b2d2b963c539..863f0b76b642 100644 --- a/extension/parquet/include/resizable_buffer.hpp +++ b/extension/parquet/include/resizable_buffer.hpp @@ -59,6 +59,10 @@ class ByteBuffer { // on to the 10 thousandth impl throw std::runtime_error("Out of buffer"); } } + + uint8_t *Uint8Ptr() { + return reinterpret_cast(ptr); + } }; class ResizeableBuffer : public ByteBuffer { diff --git a/extension/parquet/parquet-extension.cpp b/extension/parquet/parquet-extension.cpp index c1d0152db16e..d28f5d2de048 100644 --- a/extension/parquet/parquet-extension.cpp +++ b/extension/parquet/parquet-extension.cpp @@ -343,7 +343,7 @@ class ParquetScanFunction { static unique_ptr ParquetScanInitGlobal(ClientContext &context, TableFunctionInitInput &input) { - auto &bind_data = (ParquetReadBindData &)*input.bind_data; + auto &bind_data = input.bind_data->Cast(); auto result = make_uniq(); result->file_opening = vector(bind_data.files.size(), false); diff --git a/src/common/types/column/column_data_collection.cpp b/src/common/types/column/column_data_collection.cpp index a71f78a7a54f..8b83d5278ac4 100644 --- a/src/common/types/column/column_data_collection.cpp +++ b/src/common/types/column/column_data_collection.cpp @@ -505,7 +505,7 @@ void ColumnDataCopy(ColumnDataMetaData &meta_data, const UnifiedVector target_validity.SetAllValid(STANDARD_VECTOR_SIZE); } - auto target_entries = (string_t *)base_ptr; + auto target_entries = reinterpret_cast(base_ptr); for (idx_t i = 0; i < append_count; i++) { auto source_idx = source_data.sel->get_index(offset + i); auto target_idx = current_segment.count + i; diff --git a/src/common/types/list_segment.cpp b/src/common/types/list_segment.cpp index dcc160ad8ee2..cb15832144c8 100644 --- a/src/common/types/list_segment.cpp +++ b/src/common/types/list_segment.cpp @@ -148,7 +148,7 @@ void DestroyListSegment(const ListSegmentFunctions &functions, ListSegment *segm static ListSegment *CreateStructSegment(const ListSegmentFunctions &functions, Allocator &allocator, uint16_t capacity) { // allocate data and set header - auto segment = (ListSegment *)AllocateStructData(allocator, capacity, functions.child_functions.size()); + auto segment = reinterpret_cast(AllocateStructData(allocator, capacity, functions.child_functions.size())); segment->capacity = capacity; segment->count = 0; segment->next = nullptr; diff --git a/src/common/types/row/partitioned_tuple_data.cpp b/src/common/types/row/partitioned_tuple_data.cpp index c78a063b85f2..6228c2bbb75b 100644 --- a/src/common/types/row/partitioned_tuple_data.cpp +++ b/src/common/types/row/partitioned_tuple_data.cpp @@ -19,7 +19,7 @@ PartitionedTupleData::PartitionedTupleData(const PartitionedTupleData &other) unique_ptr PartitionedTupleData::CreateShared() { switch (type) { case PartitionedTupleDataType::RADIX: - return make_uniq((RadixPartitionedTupleData &)*this); + return make_uniq(Cast()); default: throw NotImplementedException("CreateShared for this type of PartitionedTupleData"); } diff --git a/src/execution/operator/aggregate/physical_streaming_window.cpp b/src/execution/operator/aggregate/physical_streaming_window.cpp index 28e1267660d1..684ad8c4966b 100644 --- a/src/execution/operator/aggregate/physical_streaming_window.cpp +++ b/src/execution/operator/aggregate/physical_streaming_window.cpp @@ -109,7 +109,7 @@ unique_ptr PhysicalStreamingWindow::GetOperatorState(ExecutionCon OperatorResultType PhysicalStreamingWindow::Execute(ExecutionContext &context, DataChunk &input, DataChunk &chunk, GlobalOperatorState &gstate_p, OperatorState &state_p) const { auto &gstate = gstate_p.Cast(); - auto &state = (StreamingWindowState &)state_p; + auto &state = state_p.Cast(); if (!state.initialized) { state.Initialize(context.client, input, select_list); } diff --git a/src/execution/operator/join/physical_blockwise_nl_join.cpp b/src/execution/operator/join/physical_blockwise_nl_join.cpp index 26d9fe8a2f19..4e8d31f608dd 100644 --- a/src/execution/operator/join/physical_blockwise_nl_join.cpp +++ b/src/execution/operator/join/physical_blockwise_nl_join.cpp @@ -244,7 +244,7 @@ unique_ptr PhysicalBlockwiseNLJoin::GetGlobalSourceState(Clie unique_ptr PhysicalBlockwiseNLJoin::GetLocalSourceState(ExecutionContext &context, GlobalSourceState &gstate) const { - return make_uniq(*this, (BlockwiseNLJoinGlobalScanState &)gstate); + return make_uniq(*this, gstate.Cast()); } SourceResultType PhysicalBlockwiseNLJoin::GetData(ExecutionContext &context, DataChunk &chunk, diff --git a/src/execution/operator/join/physical_nested_loop_join.cpp b/src/execution/operator/join/physical_nested_loop_join.cpp index 85c51f28fbcf..9e4398c0508a 100644 --- a/src/execution/operator/join/physical_nested_loop_join.cpp +++ b/src/execution/operator/join/physical_nested_loop_join.cpp @@ -443,7 +443,7 @@ unique_ptr PhysicalNestedLoopJoin::GetGlobalSourceState(Clien unique_ptr PhysicalNestedLoopJoin::GetLocalSourceState(ExecutionContext &context, GlobalSourceState &gstate) const { - return make_uniq(*this, (NestedLoopJoinGlobalScanState &)gstate); + return make_uniq(*this, gstate.Cast()); } SourceResultType PhysicalNestedLoopJoin::GetData(ExecutionContext &context, DataChunk &chunk, diff --git a/src/execution/operator/projection/physical_pivot.cpp b/src/execution/operator/projection/physical_pivot.cpp index 855908d49b55..e25a947b01a4 100644 --- a/src/execution/operator/projection/physical_pivot.cpp +++ b/src/execution/operator/projection/physical_pivot.cpp @@ -21,7 +21,7 @@ PhysicalPivot::PhysicalPivot(vector types_p, unique_ptr(aggr.function.state_size()); aggr.function.initialize(state.get()); - Vector state_vector(Value::POINTER((uintptr_t)state.get())); + Vector state_vector(Value::POINTER(CastPointerToValue(state.get()))); Vector result_vector(aggr_expr->return_type); AggregateInputData aggr_input_data(aggr.bind_info.get(), Allocator::DefaultAllocator()); aggr.function.finalize(state_vector, aggr_input_data, result_vector, 1, 0); diff --git a/src/main/client_context.cpp b/src/main/client_context.cpp index 1779f08e3cbd..b1ad53a681c2 100644 --- a/src/main/client_context.cpp +++ b/src/main/client_context.cpp @@ -918,7 +918,7 @@ void ClientContext::RegisterFunction(CreateFunctionInfo &info) { auto existing_function = Catalog::GetEntry(*this, INVALID_CATALOG, info.schema, info.name, OnEntryNotFound::RETURN_NULL); if (existing_function) { - auto &new_info = (CreateScalarFunctionInfo &)info; + auto &new_info = info.Cast(); if (new_info.functions.MergeFunctionSet(existing_function->functions)) { // function info was updated from catalog entry, rewrite is needed info.on_conflict = OnCreateConflict::REPLACE_ON_CONFLICT; diff --git a/src/main/database_manager.cpp b/src/main/database_manager.cpp index e1d4226f309d..e96d51c5a76f 100644 --- a/src/main/database_manager.cpp +++ b/src/main/database_manager.cpp @@ -27,7 +27,7 @@ optional_ptr DatabaseManager::GetDatabase(ClientContext &conte if (StringUtil::Lower(name) == TEMP_CATALOG) { return context.client_data->temporary_objects.get(); } - return (AttachedDatabase *)databases->GetEntry(context, name).get(); + return reinterpret_cast(databases->GetEntry(context, name).get()); } void DatabaseManager::AddDatabase(ClientContext &context, unique_ptr db_instance) { diff --git a/src/main/extension/extension_load.cpp b/src/main/extension/extension_load.cpp index b969aee8ea29..a1324748b356 100644 --- a/src/main/extension/extension_load.cpp +++ b/src/main/extension/extension_load.cpp @@ -86,7 +86,7 @@ bool ExtensionHelper::TryInitialLoad(DBConfig &config, FileSystem &fs, const str auto signature_offset = handle->GetFileSize() - signature.size(); - const idx_t maxLenChunks = 1024 * 1024; + const idx_t maxLenChunks = 1024ULL * 1024ULL; const idx_t numChunks = (signature_offset + maxLenChunks - 1) / maxLenChunks; std::vector hash_chunks(numChunks); std::vector splits(numChunks + 1); diff --git a/src/storage/compression/dictionary_compression.cpp b/src/storage/compression/dictionary_compression.cpp index d29e1b0c6f94..b407ff91e708 100644 --- a/src/storage/compression/dictionary_compression.cpp +++ b/src/storage/compression/dictionary_compression.cpp @@ -274,7 +274,7 @@ struct DictionaryCompressionCompressState : public DictionaryCompressionState { // calculate ptr and offsets auto base_ptr = handle.Ptr(); - auto header_ptr = (dictionary_compression_header_t *)base_ptr; + auto header_ptr = reinterpret_cast(base_ptr); auto compressed_selection_buffer_offset = DictionaryCompressionStorage::DICTIONARY_HEADER_SIZE; auto index_buffer_offset = compressed_selection_buffer_offset + compressed_selection_buffer_size; @@ -444,12 +444,12 @@ unique_ptr DictionaryCompressionStorage::StringInitScan(Column // Load header values auto dict = DictionaryCompressionStorage::GetDictionary(segment, state->handle); - auto header_ptr = (dictionary_compression_header_t *)baseptr; + auto header_ptr = reinterpret_cast(baseptr); auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); auto index_buffer_count = Load(data_ptr_cast(&header_ptr->index_buffer_count)); state->current_width = (bitpacking_width_t)(Load(data_ptr_cast(&header_ptr->bitpacking_width))); - auto index_buffer_ptr = (uint32_t *)(baseptr + index_buffer_offset); + auto index_buffer_ptr = reinterpret_cast(baseptr + index_buffer_offset); state->dictionary = make_buffer(segment.type, index_buffer_count); auto dict_child_data = FlatVector::GetData(*(state->dictionary)); @@ -470,7 +470,7 @@ template void DictionaryCompressionStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result, idx_t result_offset) { // clear any previously locked buffers and get the primary buffer handle - auto &scan_state = (CompressedStringScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast(); auto start = segment.GetRelativeIndex(state.row_index); auto baseptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); diff --git a/src/storage/compression/fixed_size_uncompressed.cpp b/src/storage/compression/fixed_size_uncompressed.cpp index 50a41bfb14a1..8b12c251f635 100644 --- a/src/storage/compression/fixed_size_uncompressed.cpp +++ b/src/storage/compression/fixed_size_uncompressed.cpp @@ -35,7 +35,7 @@ bool FixedSizeAnalyze(AnalyzeState &state_p, Vector &input, idx_t count) { template idx_t FixedSizeFinalAnalyze(AnalyzeState &state_p) { - auto &state = (FixedSizeAnalyzeState &)state_p; + auto &state = state_p.template Cast(); return sizeof(T) * state.count; } @@ -134,7 +134,7 @@ unique_ptr FixedSizeInitScan(ColumnSegment &segment) { template void FixedSizeScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result, idx_t result_offset) { - auto &scan_state = (FixedSizeScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast(); auto start = segment.GetRelativeIndex(state.row_index); auto data = scan_state.handle.Ptr() + segment.GetBlockOffset(); diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index c26980d6fd79..59ff43508b1b 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -155,7 +155,7 @@ idx_t FSSTStorage::StringFinalAnalyze(AnalyzeState &state_p) { vector fsst_string_ptrs; for (auto &str : state.fsst_strings) { fsst_string_sizes.push_back(str.GetSize()); - fsst_string_ptrs.push_back((unsigned char *)str.GetData()); + fsst_string_ptrs.push_back((unsigned char *)str.GetData()); // NOLINT } state.fsst_encoder = duckdb_fsst_create(string_count, &fsst_string_sizes[0], &fsst_string_ptrs[0], 0); @@ -332,13 +332,13 @@ class FSSTCompressionState : public CompressionState { // calculate ptr and offsets auto base_ptr = handle.Ptr(); - auto header_ptr = (fsst_compression_header_t *)base_ptr; + auto header_ptr = reinterpret_cast(base_ptr); auto compressed_index_buffer_offset = sizeof(fsst_compression_header_t); auto symbol_table_offset = compressed_index_buffer_offset + compressed_index_buffer_size; D_ASSERT(current_segment->count == index_buffer.size()); BitpackingPrimitives::PackBuffer(base_ptr + compressed_index_buffer_offset, - (uint32_t *)(index_buffer.data()), current_segment->count, + reinterpret_cast(index_buffer.data()), current_segment->count, current_width); // Write the fsst symbol table or nothing @@ -522,7 +522,7 @@ unique_ptr FSSTStorage::StringInitScan(ColumnSegment &segment) auto base_ptr = state->handle.Ptr() + segment.GetBlockOffset(); state->duckdb_fsst_decoder = make_buffer(); - auto retval = ParseFSSTSegmentHeader(base_ptr, (duckdb_fsst_decoder_t *)state->duckdb_fsst_decoder.get(), + auto retval = ParseFSSTSegmentHeader(base_ptr, reinterpret_cast(state->duckdb_fsst_decoder.get()), &state->current_width); if (!retval) { state->duckdb_fsst_decoder = nullptr; diff --git a/src/storage/compression/string_uncompressed.cpp b/src/storage/compression/string_uncompressed.cpp index 16f8fa15a647..3963447587a3 100644 --- a/src/storage/compression/string_uncompressed.cpp +++ b/src/storage/compression/string_uncompressed.cpp @@ -73,12 +73,12 @@ unique_ptr UncompressedStringStorage::StringInitScan(ColumnSeg void UncompressedStringStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t scan_count, Vector &result, idx_t result_offset) { // clear any previously locked buffers and get the primary buffer handle - auto &scan_state = (StringScanState &)*state.scan_state; + auto &scan_state = state.scan_state->Cast(); auto start = segment.GetRelativeIndex(state.row_index); auto baseptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); auto dict = GetDictionary(segment, scan_state.handle); - auto base_data = (int32_t *)(baseptr + DICTIONARY_HEADER_SIZE); + auto base_data = reinterpret_cast(baseptr + DICTIONARY_HEADER_SIZE); auto result_data = FlatVector::GetData(result); int32_t previous_offset = start > 0 ? base_data[start - 1] : 0; diff --git a/src/storage/compression/validity_uncompressed.cpp b/src/storage/compression/validity_uncompressed.cpp index c458cbff8626..33389b297c0b 100644 --- a/src/storage/compression/validity_uncompressed.cpp +++ b/src/storage/compression/validity_uncompressed.cpp @@ -227,7 +227,7 @@ void ValidityScanPartial(ColumnSegment &segment, ColumnScanState &state, idx_t s auto &result_mask = FlatVector::Validity(result); auto buffer_ptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); D_ASSERT(scan_state.block_id == segment.block->BlockId()); - auto input_data = (validity_t *)buffer_ptr; + auto input_data = reinterpret_cast(buffer_ptr); #ifdef DEBUG // this method relies on all the bits we are going to write to being set to valid @@ -353,8 +353,8 @@ void ValidityScan(ColumnSegment &segment, ColumnScanState &state, idx_t scan_cou auto &result_mask = FlatVector::Validity(result); auto buffer_ptr = scan_state.handle.Ptr() + segment.GetBlockOffset(); D_ASSERT(scan_state.block_id == segment.block->BlockId()); - auto input_data = (validity_t *)buffer_ptr; - auto result_data = (validity_t *)result_mask.GetData(); + auto input_data = reinterpret_cast(buffer_ptr); + auto result_data = result_mask.GetData(); idx_t start_offset = start / ValidityMask::BITS_PER_VALUE; idx_t entry_scan_count = (scan_count + ValidityMask::BITS_PER_VALUE - 1) / ValidityMask::BITS_PER_VALUE; for (idx_t i = 0; i < entry_scan_count; i++) { @@ -364,7 +364,7 @@ void ValidityScan(ColumnSegment &segment, ColumnScanState &state, idx_t scan_cou } if (!result_data) { result_mask.Initialize(MaxValue(STANDARD_VECTOR_SIZE, scan_count)); - result_data = (validity_t *)result_mask.GetData(); + result_data = result_mask.GetData(); } result_data[i] = input_entry; } @@ -450,7 +450,7 @@ void ValidityRevertAppend(ColumnSegment &segment, idx_t start_row) { idx_t byte_pos = start_bit / 8; idx_t bit_start = byte_pos * 8; idx_t bit_end = (byte_pos + 1) * 8; - ValidityMask mask((validity_t *)handle.Ptr() + byte_pos); + ValidityMask mask(reinterpret_cast(handle.Ptr() + byte_pos)); for (idx_t i = start_bit; i < bit_end; i++) { mask.SetValid(i - bit_start); } diff --git a/src/storage/single_file_block_manager.cpp b/src/storage/single_file_block_manager.cpp index e6859fb7db1b..cb8e93d5fc68 100644 --- a/src/storage/single_file_block_manager.cpp +++ b/src/storage/single_file_block_manager.cpp @@ -170,7 +170,7 @@ void SingleFileBlockManager::CreateNewDatabase() { h2.free_list = INVALID_BLOCK; h2.block_count = 0; SerializeHeaderStructure(h2, header_buffer.buffer); - ChecksumAndWrite(header_buffer, Storage::FILE_HEADER_SIZE * 2); + ChecksumAndWrite(header_buffer, Storage::FILE_HEADER_SIZE * 2ULL); // ensure that writing to disk is completed before returning handle->Sync(); // we start with h2 as active_header, this way our initial write will be in h1 @@ -197,7 +197,7 @@ void SingleFileBlockManager::LoadExistingDatabase() { DatabaseHeader h1, h2; ReadAndChecksum(header_buffer, Storage::FILE_HEADER_SIZE); h1 = DeserializeHeaderStructure(header_buffer.buffer); - ReadAndChecksum(header_buffer, Storage::FILE_HEADER_SIZE * 2); + ReadAndChecksum(header_buffer, Storage::FILE_HEADER_SIZE * 2ULL); h2 = DeserializeHeaderStructure(header_buffer.buffer); // check the header with the highest iteration count if (h1.iteration > h2.iteration) { diff --git a/src/storage/table/update_segment.cpp b/src/storage/table/update_segment.cpp index 2757c5343a44..3c342d611625 100644 --- a/src/storage/table/update_segment.cpp +++ b/src/storage/table/update_segment.cpp @@ -48,9 +48,9 @@ Value UpdateInfo::GetValue(idx_t index) { switch (type.id()) { case LogicalTypeId::VALIDITY: - return Value::BOOLEAN(((bool *)tuple_data)[index]); + return Value::BOOLEAN(reinterpret_cast(tuple_data)[index]); case LogicalTypeId::INTEGER: - return Value::INTEGER(((int32_t *)tuple_data)[index]); + return Value::INTEGER(reinterpret_cast(tuple_data)[index]); default: throw NotImplementedException("Unimplemented type for UpdateInfo::GetValue"); } @@ -85,7 +85,7 @@ void UpdateInfo::Verify() { // Update Fetch //===--------------------------------------------------------------------===// static void MergeValidityInfo(UpdateInfo *current, ValidityMask &result_mask) { - auto info_data = (bool *)current->tuple_data; + auto info_data = reinterpret_cast(current->tuple_data); for (idx_t i = 0; i < current->N; i++) { result_mask.Set(current->tuples[i], info_data[i]); } @@ -100,7 +100,7 @@ static void UpdateMergeValidity(transaction_t start_time, transaction_t transact template static void MergeUpdateInfo(UpdateInfo *current, T *result_data) { - auto info_data = (T *)current->tuple_data; + auto info_data = reinterpret_cast(current->tuple_data); if (current->N == STANDARD_VECTOR_SIZE) { // special case: update touches ALL tuples of this vector // in this case we can just memcpy the data @@ -241,7 +241,7 @@ void UpdateSegment::FetchCommitted(idx_t vector_index, Vector &result) { //===--------------------------------------------------------------------===// static void MergeUpdateInfoRangeValidity(UpdateInfo *current, idx_t start, idx_t end, idx_t result_offset, ValidityMask &result_mask) { - auto info_data = (bool *)current->tuple_data; + auto info_data = reinterpret_cast(current->tuple_data); for (idx_t i = 0; i < current->N; i++) { auto tuple_idx = current->tuples[i]; if (tuple_idx < start) { @@ -261,7 +261,7 @@ static void FetchCommittedRangeValidity(UpdateInfo *info, idx_t start, idx_t end template static void MergeUpdateInfoRange(UpdateInfo *current, idx_t start, idx_t end, idx_t result_offset, T *result_data) { - auto info_data = (T *)current->tuple_data; + auto info_data = reinterpret_cast(current->tuple_data); for (idx_t i = 0; i < current->N; i++) { auto tuple_idx = current->tuples[i]; if (tuple_idx < start) { @@ -352,7 +352,7 @@ static void FetchRowValidity(transaction_t start_time, transaction_t transaction Vector &result, idx_t result_idx) { auto &result_mask = FlatVector::Validity(result); UpdateInfo::UpdatesForTransaction(info, start_time, transaction_id, [&](UpdateInfo *current) { - auto info_data = (bool *)current->tuple_data; + auto info_data = reinterpret_cast(current->tuple_data); // FIXME: we could do a binary search in here for (idx_t i = 0; i < current->N; i++) { if (current->tuples[i] == row_idx) { @@ -576,7 +576,7 @@ void UpdateSegment::InitializeUpdateInfo(UpdateInfo &info, row_t *ids, const Sel static void InitializeUpdateValidity(UpdateInfo *base_info, Vector &base_data, UpdateInfo *update_info, Vector &update, const SelectionVector &sel) { auto &update_mask = FlatVector::Validity(update); - auto tuple_data = (bool *)update_info->tuple_data; + auto tuple_data = reinterpret_cast(update_info->tuple_data); if (!update_mask.AllValid()) { for (idx_t i = 0; i < update_info->N; i++) { @@ -590,7 +590,7 @@ static void InitializeUpdateValidity(UpdateInfo *base_info, Vector &base_data, U } auto &base_mask = FlatVector::Validity(base_data); - auto base_tuple_data = (bool *)base_info->tuple_data; + auto base_tuple_data = reinterpret_cast(base_info->tuple_data); if (!base_mask.AllValid()) { for (idx_t i = 0; i < base_info->N; i++) { base_tuple_data[i] = base_mask.RowIsValidUnsafe(base_info->tuples[i]); @@ -1045,9 +1045,9 @@ static idx_t SortSelectionVector(SelectionVector &sel, idx_t count, row_t *ids) UpdateInfo *CreateEmptyUpdateInfo(TransactionData transaction, idx_t type_size, idx_t count, unsafe_unique_array &data) { data = make_unsafe_uniq_array(sizeof(UpdateInfo) + (sizeof(sel_t) + type_size) * STANDARD_VECTOR_SIZE); - auto update_info = (UpdateInfo *)data.get(); + auto update_info = reinterpret_cast(data.get()); update_info->max = STANDARD_VECTOR_SIZE; - update_info->tuples = (sel_t *)((data_ptr_cast(update_info)) + sizeof(UpdateInfo)); + update_info->tuples = reinterpret_cast((data_ptr_cast(update_info)) + sizeof(UpdateInfo)); update_info->tuple_data = (data_ptr_cast(update_info)) + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; update_info->version_number = transaction.transaction_id; return update_info; diff --git a/src/transaction/cleanup_state.cpp b/src/transaction/cleanup_state.cpp index 8fb0dc22c244..5dfa7f229793 100644 --- a/src/transaction/cleanup_state.cpp +++ b/src/transaction/cleanup_state.cpp @@ -28,12 +28,12 @@ void CleanupState::CleanupEntry(UndoFlags type, data_ptr_t data) { break; } case UndoFlags::DELETE_TUPLE: { - auto info = (DeleteInfo *)data; + auto info = reinterpret_cast(data); CleanupDelete(*info); break; } case UndoFlags::UPDATE_TUPLE: { - auto info = (UpdateInfo *)data; + auto info = reinterpret_cast(data); CleanupUpdate(*info); break; } diff --git a/src/transaction/commit_state.cpp b/src/transaction/commit_state.cpp index 0f68b46f5d53..5034e4620e70 100644 --- a/src/transaction/commit_state.cpp +++ b/src/transaction/commit_state.cpp @@ -247,7 +247,7 @@ void CommitState::CommitEntry(UndoFlags type, data_ptr_t data) { } case UndoFlags::INSERT_TUPLE: { // append: - auto info = (AppendInfo *)data; + auto info = reinterpret_cast(data); if (HAS_LOG && !info->table->info->IsTemporary()) { info->table->WriteToLog(*log, info->start_row, info->count); } @@ -257,7 +257,7 @@ void CommitState::CommitEntry(UndoFlags type, data_ptr_t data) { } case UndoFlags::DELETE_TUPLE: { // deletion: - auto info = (DeleteInfo *)data; + auto info = reinterpret_cast(data); if (HAS_LOG && !info->table->info->IsTemporary()) { WriteDelete(*info); } @@ -267,7 +267,7 @@ void CommitState::CommitEntry(UndoFlags type, data_ptr_t data) { } case UndoFlags::UPDATE_TUPLE: { // update: - auto info = (UpdateInfo *)data; + auto info = reinterpret_cast(data); if (HAS_LOG && !info->segment->column_data.GetTableInfo().IsTemporary()) { WriteUpdate(*info); } @@ -293,14 +293,14 @@ void CommitState::RevertCommit(UndoFlags type, data_ptr_t data) { break; } case UndoFlags::INSERT_TUPLE: { - auto info = (AppendInfo *)data; + auto info = reinterpret_cast(data); // revert this append info->table->RevertAppend(info->start_row, info->count); break; } case UndoFlags::DELETE_TUPLE: { // deletion: - auto info = (DeleteInfo *)data; + auto info = reinterpret_cast(data); info->table->info->cardinality += info->count; // revert the commit by writing the (uncommitted) transaction_id back into the version info info->vinfo->CommitDelete(transaction_id, info->rows, info->count); @@ -308,7 +308,7 @@ void CommitState::RevertCommit(UndoFlags type, data_ptr_t data) { } case UndoFlags::UPDATE_TUPLE: { // update: - auto info = (UpdateInfo *)data; + auto info = reinterpret_cast(data); info->version_number = transaction_id; break; } diff --git a/src/transaction/rollback_state.cpp b/src/transaction/rollback_state.cpp index 87a19cbc9681..8edfb0b437be 100644 --- a/src/transaction/rollback_state.cpp +++ b/src/transaction/rollback_state.cpp @@ -22,19 +22,19 @@ void RollbackState::RollbackEntry(UndoFlags type, data_ptr_t data) { break; } case UndoFlags::INSERT_TUPLE: { - auto info = (AppendInfo *)data; + auto info = reinterpret_cast(data); // revert the append in the base table info->table->RevertAppend(info->start_row, info->count); break; } case UndoFlags::DELETE_TUPLE: { - auto info = (DeleteInfo *)data; + auto info = reinterpret_cast(data); // reset the deleted flag on rollback info->vinfo->CommitDelete(NOT_DELETED_ID, info->rows, info->count); break; } case UndoFlags::UPDATE_TUPLE: { - auto info = (UpdateInfo *)data; + auto info = reinterpret_cast(data); info->segment->RollbackUpdate(*info); break; } diff --git a/tools/pythonpkg/src/pyconnection.cpp b/tools/pythonpkg/src/pyconnection.cpp index 0d29c07c951e..ee67e72179ca 100644 --- a/tools/pythonpkg/src/pyconnection.cpp +++ b/tools/pythonpkg/src/pyconnection.cpp @@ -1017,7 +1017,7 @@ unique_ptr DuckDBPyConnection::FromDF(const PandasDataFrame &v } auto new_df = PandasScanFunction::PandasReplaceCopiedNames(value); vector params; - params.emplace_back(Value::POINTER((uintptr_t)new_df.ptr())); + params.emplace_back(Value::POINTER(CastPointerToValue(new_df.ptr()))); auto rel = connection->TableFunction("pandas_scan", params)->Alias(name); rel->extra_dependencies = make_uniq(make_uniq(value), make_uniq(new_df)); @@ -1304,9 +1304,9 @@ static void CreateArrowScan(py::object entry, TableFunctionRef &table_function, auto stream_factory_produce = PythonTableArrowArrayStreamFactory::Produce; auto stream_factory_get_schema = PythonTableArrowArrayStreamFactory::GetSchema; - children.push_back(make_uniq(Value::POINTER((uintptr_t)stream_factory.get()))); - children.push_back(make_uniq(Value::POINTER((uintptr_t)stream_factory_produce))); - children.push_back(make_uniq(Value::POINTER((uintptr_t)stream_factory_get_schema))); + children.push_back(make_uniq(Value::POINTER(CastPointerToValue(stream_factory.get())))); + children.push_back(make_uniq(Value::POINTER(CastPointerToValue(stream_factory_produce)))); + children.push_back(make_uniq(Value::POINTER(CastPointerToValue(stream_factory_get_schema)))); table_function.function = make_uniq("arrow_scan", std::move(children)); table_function.external_dependency = From 8d4e3fbc677947740c4ed10e6e892a7051ad75d8 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 13:11:50 +0200 Subject: [PATCH 18/28] Turn resizable buffer ptr into data_ptr_t, and remove casts from Parquet reader --- extension/parquet/column_reader.cpp | 47 ++++++++++--------- extension/parquet/column_writer.cpp | 4 +- .../parquet/include/cast_column_reader.hpp | 2 +- extension/parquet/include/column_reader.hpp | 2 +- .../parquet/include/list_column_reader.hpp | 2 +- .../parquet/include/parquet_dbp_decoder.hpp | 8 ++-- .../include/parquet_rle_bp_decoder.hpp | 12 ++--- .../parquet/include/resizable_buffer.hpp | 12 ++--- .../include/row_number_column_reader.hpp | 2 +- .../parquet/include/struct_column_reader.hpp | 2 +- extension/parquet/parquet_reader.cpp | 6 +-- extension/parquet/parquet_statistics.cpp | 20 ++++---- extension/parquet/zstd_file_system.cpp | 6 +-- src/common/fsst.cpp | 4 +- src/common/gzip_file_system.cpp | 2 +- src/common/sort/merge_sorter.cpp | 9 +++- .../types/column/column_data_allocator.cpp | 4 +- src/common/types/hyperloglog.cpp | 3 +- src/common/types/list_segment.cpp | 24 ++++++---- src/common/types/row/tuple_data_allocator.cpp | 6 ++- src/common/types/vector.cpp | 16 +++---- .../scalar/string/string_split.cpp | 6 +-- src/execution/window_segment_tree.cpp | 8 ++-- src/function/scalar/string/like.cpp | 8 ++-- .../scalar/system/aggregate_export.cpp | 10 ++-- src/function/table/copy_csv.cpp | 15 +++--- src/include/duckdb/common/fsst.hpp | 3 +- src/include/duckdb/common/typedefs.hpp | 1 - .../duckdb/common/types/null_value.hpp | 2 +- .../duckdb/common/types/string_type.hpp | 4 +- src/main/capi/logical_types-c.cpp | 3 +- src/main/capi/result-c.cpp | 4 +- src/storage/compression/bitpacking.cpp | 4 +- .../compression/dictionary_compression.cpp | 2 +- src/storage/compression/fsst.cpp | 16 +++---- src/storage/statistics/string_stats.cpp | 21 +++++---- tools/rpkg/src/transform.cpp | 3 +- 37 files changed, 152 insertions(+), 151 deletions(-) diff --git a/extension/parquet/column_reader.cpp b/extension/parquet/column_reader.cpp index 74bd935ee6fd..235af296a0c9 100644 --- a/extension/parquet/column_reader.cpp +++ b/extension/parquet/column_reader.cpp @@ -299,7 +299,8 @@ void ColumnReader::PreparePageV2(PageHeader &page_hdr) { trans.read(reinterpret_cast(compressed_buffer.ptr), compressed_bytes); DecompressInternal(chunk->meta_data.codec, const_char_ptr_cast(compressed_buffer.ptr), compressed_bytes, - char_ptr_cast(block->ptr) + uncompressed_bytes, page_hdr.uncompressed_page_size - uncompressed_bytes); + char_ptr_cast(block->ptr) + uncompressed_bytes, + page_hdr.uncompressed_page_size - uncompressed_bytes); } void ColumnReader::AllocateBlock(idx_t size) { @@ -395,8 +396,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { if (HasRepeats()) { uint32_t rep_length = is_v1 ? block->read() : v2_header.repetition_levels_byte_length; block->available(rep_length); - repeated_decoder = - make_uniq(block->Uint8Ptr(), rep_length, RleBpDecoder::ComputeBitWidth(max_repeat)); + repeated_decoder = make_uniq(block->ptr, rep_length, RleBpDecoder::ComputeBitWidth(max_repeat)); block->inc(rep_length); } else if (is_v2 && v2_header.repetition_levels_byte_length > 0) { block->inc(v2_header.repetition_levels_byte_length); @@ -405,8 +405,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { if (HasDefines()) { uint32_t def_length = is_v1 ? block->read() : v2_header.definition_levels_byte_length; block->available(def_length); - defined_decoder = - make_uniq(block->Uint8Ptr(), def_length, RleBpDecoder::ComputeBitWidth(max_define)); + defined_decoder = make_uniq(block->ptr, def_length, RleBpDecoder::ComputeBitWidth(max_define)); block->inc(def_length); } else if (is_v2 && v2_header.definition_levels_byte_length > 0) { block->inc(v2_header.definition_levels_byte_length); @@ -418,7 +417,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { // where is it otherwise?? auto dict_width = block->read(); // TODO somehow dict_width can be 0 ? - dict_decoder = make_uniq(block->Uint8Ptr(), block->len, dict_width); + dict_decoder = make_uniq(block->ptr, block->len, dict_width); block->inc(block->len); break; } @@ -427,11 +426,11 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { throw std::runtime_error("RLE encoding is only supported for boolean data"); } block->inc(sizeof(uint32_t)); - rle_decoder = make_uniq(block->Uint8Ptr(), block->len, 1); + rle_decoder = make_uniq(block->ptr, block->len, 1); break; } case Encoding::DELTA_BINARY_PACKED: { - dbp_decoder = make_uniq(block->Uint8Ptr(), block->len); + dbp_decoder = make_uniq(block->ptr, block->len); block->inc(block->len); break; } @@ -452,7 +451,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { } } -idx_t ColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, +idx_t ColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result) { // we need to reset the location because multiple column readers share the same protocol auto &trans = (ThriftFileTransport &)*protocol->getTransport(); @@ -566,7 +565,7 @@ void ColumnReader::ApplyPendingSkips(idx_t num_values) { while (remaining) { idx_t to_read = MinValue(remaining, STANDARD_VECTOR_SIZE); - read += Read(to_read, none_filter, dummy_define.Uint8Ptr(), (uint8_t *)dummy_repeat.ptr, dummy_result); + read += Read(to_read, none_filter, dummy_define.ptr, dummy_repeat.ptr, dummy_result); remaining -= to_read; } @@ -623,14 +622,15 @@ void StringColumnReader::Dictionary(shared_ptr data, idx_t num } dict->available(str_len); - auto actual_str_len = VerifyString(dict->ptr, str_len); - dict_strings[dict_idx] = string_t(dict->ptr, actual_str_len); + auto dict_str = reinterpret_cast(dict->ptr); + auto actual_str_len = VerifyString(dict_str, str_len); + dict_strings[dict_idx] = string_t(dict_str, actual_str_len); dict->inc(str_len); } } static shared_ptr ReadDbpData(Allocator &allocator, ResizeableBuffer &buffer, idx_t &value_count) { - auto decoder = make_uniq(buffer.Uint8Ptr(), buffer.len); + auto decoder = make_uniq(buffer.ptr, buffer.len); value_count = decoder->TotalValues(); auto result = make_shared(); result->resize(allocator, sizeof(uint32_t) * value_count); @@ -750,8 +750,9 @@ string_t StringParquetValueConversion::PlainRead(ByteBuffer &plain_data, ColumnR auto &scr = reader.Cast(); uint32_t str_len = scr.fixed_width_string_length == 0 ? plain_data.read() : scr.fixed_width_string_length; plain_data.available(str_len); - auto actual_str_len = reader.Cast().VerifyString(plain_data.ptr, str_len); - auto ret_str = string_t(plain_data.ptr, actual_str_len); + auto plain_str = char_ptr_cast(plain_data.ptr); + auto actual_str_len = reader.Cast().VerifyString(plain_str, str_len); + auto ret_str = string_t(plain_str, actual_str_len); plain_data.inc(str_len); return ret_str; } @@ -765,8 +766,8 @@ void StringParquetValueConversion::PlainSkip(ByteBuffer &plain_data, ColumnReade //===--------------------------------------------------------------------===// // List Column Reader //===--------------------------------------------------------------------===// -idx_t ListColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, - Vector &result_out) { +idx_t ListColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, + data_ptr_t repeat_out, Vector &result_out) { idx_t result_offset = 0; auto result_ptr = FlatVector::GetData(result_out); auto &result_mask = FlatVector::Validity(result_out); @@ -935,8 +936,8 @@ void RowNumberColumnReader::InitializeRead(idx_t row_group_idx_p, const vector(result); for (idx_t i = 0; i < num_values; i++) { @@ -966,8 +967,8 @@ void CastColumnReader::InitializeRead(idx_t row_group_idx_p, const vectorInitializeRead(row_group_idx_p, columns, protocol_p); } -idx_t CastColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, - Vector &result) { +idx_t CastColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, + data_ptr_t repeat_out, Vector &result) { intermediate_chunk.Reset(); auto &intermediate_vector = intermediate_chunk.data[0]; @@ -1018,8 +1019,8 @@ void StructColumnReader::InitializeRead(idx_t row_group_idx_p, const vector(new data_t[compressed_size]); - s.Compress(const_char_ptr_cast(temp_writer.blob.data.get()), temp_writer.blob.size, char_ptr_cast(compressed_buf.get()), - &compressed_size); + s.Compress(const_char_ptr_cast(temp_writer.blob.data.get()), temp_writer.blob.size, + char_ptr_cast(compressed_buf.get()), &compressed_size); compressed_data = compressed_buf.get(); break; } diff --git a/extension/parquet/include/cast_column_reader.hpp b/extension/parquet/include/cast_column_reader.hpp index 129272dba957..01849ef4db4a 100644 --- a/extension/parquet/include/cast_column_reader.hpp +++ b/extension/parquet/include/cast_column_reader.hpp @@ -28,7 +28,7 @@ class CastColumnReader : public ColumnReader { unique_ptr Stats(idx_t row_group_idx_p, const vector &columns) override; void InitializeRead(idx_t row_group_idx_p, const vector &columns, TProtocol &protocol_p) override; - idx_t Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, + idx_t Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result) override; void Skip(idx_t num_values) override; diff --git a/extension/parquet/include/column_reader.hpp b/extension/parquet/include/column_reader.hpp index 790357efb8eb..2fef0e806294 100644 --- a/extension/parquet/include/column_reader.hpp +++ b/extension/parquet/include/column_reader.hpp @@ -51,7 +51,7 @@ class ColumnReader { const SchemaElement &schema_p, idx_t schema_idx_p, idx_t max_define, idx_t max_repeat); virtual void InitializeRead(idx_t row_group_index, const vector &columns, TProtocol &protocol_p); - virtual idx_t Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, + virtual idx_t Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result_out); virtual void Skip(idx_t num_values); diff --git a/extension/parquet/include/list_column_reader.hpp b/extension/parquet/include/list_column_reader.hpp index 3ddb8557dd5c..780163928a14 100644 --- a/extension/parquet/include/list_column_reader.hpp +++ b/extension/parquet/include/list_column_reader.hpp @@ -21,7 +21,7 @@ class ListColumnReader : public ColumnReader { ListColumnReader(ParquetReader &reader, LogicalType type_p, const SchemaElement &schema_p, idx_t schema_idx_p, idx_t max_define_p, idx_t max_repeat_p, duckdb::unique_ptr child_column_reader_p); - idx_t Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, + idx_t Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result_out) override; void ApplyPendingSkips(idx_t num_values) override; diff --git a/extension/parquet/include/parquet_dbp_decoder.hpp b/extension/parquet/include/parquet_dbp_decoder.hpp index ca8911de2227..9f01354e4177 100644 --- a/extension/parquet/include/parquet_dbp_decoder.hpp +++ b/extension/parquet/include/parquet_dbp_decoder.hpp @@ -4,7 +4,7 @@ namespace duckdb { class DbpDecoder { public: - DbpDecoder(const uint8_t *buffer, uint32_t buffer_len) : buffer_((char *)buffer, buffer_len) { + DbpDecoder(data_ptr_t buffer, uint32_t buffer_len) : buffer_(buffer, buffer_len) { // // overall header block_value_count = ParquetDecodeUtils::VarintDecode(buffer_); @@ -35,8 +35,8 @@ class DbpDecoder { } template - void GetBatch(char *values_target_ptr, uint32_t batch_size) { - auto values = (T *)values_target_ptr; + void GetBatch(data_ptr_t values_target_ptr, uint32_t batch_size) { + auto values = reinterpret_cast(values_target_ptr); if (batch_size == 0) { return; @@ -97,7 +97,7 @@ class DbpDecoder { return; } auto data = duckdb::unique_ptr(new uint32_t[values_left_in_miniblock]); - GetBatch((char *)data.get(), values_left_in_miniblock); + GetBatch(data_ptr_cast(data.get()), values_left_in_miniblock); } uint64_t TotalValues() { diff --git a/extension/parquet/include/parquet_rle_bp_decoder.hpp b/extension/parquet/include/parquet_rle_bp_decoder.hpp index 2c70489e5971..125edf1dd294 100644 --- a/extension/parquet/include/parquet_rle_bp_decoder.hpp +++ b/extension/parquet/include/parquet_rle_bp_decoder.hpp @@ -18,9 +18,8 @@ class RleBpDecoder { public: /// Create a decoder object. buffer/buffer_len is the decoded data. /// bit_width is the width of each value (before encoding). - RleBpDecoder(const uint8_t *buffer, uint32_t buffer_len, uint32_t bit_width) - : buffer_((char *)buffer, buffer_len), bit_width_(bit_width), current_value_(0), repeat_count_(0), - literal_count_(0) { + RleBpDecoder(data_ptr_t buffer, uint32_t buffer_len, uint32_t bit_width) + : buffer_(buffer, buffer_len), bit_width_(bit_width), current_value_(0), repeat_count_(0), literal_count_(0) { if (bit_width >= 64) { throw std::runtime_error("Decode bit width too large"); } @@ -29,7 +28,7 @@ class RleBpDecoder { } template - void GetBatch(char *values_target_ptr, uint32_t batch_size) { + void GetBatch(data_ptr_t values_target_ptr, uint32_t batch_size) { auto values = reinterpret_cast(values_target_ptr); uint32_t values_read = 0; @@ -62,11 +61,6 @@ class RleBpDecoder { } } - template - void GetBatch(uint8_t *values_target_ptr, uint32_t batch_size) { - return GetBatch(reinterpret_cast(values_target_ptr), batch_size); - } - static uint8_t ComputeBitWidth(idx_t val) { if (val == 0) { return 0; diff --git a/extension/parquet/include/resizable_buffer.hpp b/extension/parquet/include/resizable_buffer.hpp index 863f0b76b642..39ee933883fe 100644 --- a/extension/parquet/include/resizable_buffer.hpp +++ b/extension/parquet/include/resizable_buffer.hpp @@ -19,9 +19,9 @@ namespace duckdb { class ByteBuffer { // on to the 10 thousandth impl public: ByteBuffer() {}; - ByteBuffer(char *ptr, uint64_t len) : ptr(ptr), len(len) {}; + ByteBuffer(data_ptr_t ptr, uint64_t len) : ptr(ptr), len(len) {}; - char *ptr = nullptr; + data_ptr_t ptr = nullptr; uint64_t len = 0; public: @@ -41,7 +41,7 @@ class ByteBuffer { // on to the 10 thousandth impl template T get() { available(sizeof(T)); - T val = Load(data_ptr_cast(ptr)); + T val = Load(ptr); return val; } @@ -59,10 +59,6 @@ class ByteBuffer { // on to the 10 thousandth impl throw std::runtime_error("Out of buffer"); } } - - uint8_t *Uint8Ptr() { - return reinterpret_cast(ptr); - } }; class ResizeableBuffer : public ByteBuffer { @@ -80,7 +76,7 @@ class ResizeableBuffer : public ByteBuffer { if (new_size > alloc_len) { alloc_len = NextPowerOfTwo(new_size); allocated_data = allocator.Allocate(alloc_len); - ptr = (char *)allocated_data.get(); + ptr = allocated_data.get(); } } diff --git a/extension/parquet/include/row_number_column_reader.hpp b/extension/parquet/include/row_number_column_reader.hpp index 039fee3aee05..cdd5df1f3443 100644 --- a/extension/parquet/include/row_number_column_reader.hpp +++ b/extension/parquet/include/row_number_column_reader.hpp @@ -26,7 +26,7 @@ class RowNumberColumnReader : public ColumnReader { idx_t max_define_p, idx_t max_repeat_p); public: - idx_t Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, + idx_t Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result) override; unique_ptr Stats(idx_t row_group_idx_p, const vector &columns) override; diff --git a/extension/parquet/include/struct_column_reader.hpp b/extension/parquet/include/struct_column_reader.hpp index 0a6af9a35ed0..b437ef890552 100644 --- a/extension/parquet/include/struct_column_reader.hpp +++ b/extension/parquet/include/struct_column_reader.hpp @@ -29,7 +29,7 @@ class StructColumnReader : public ColumnReader { void InitializeRead(idx_t row_group_idx_p, const vector &columns, TProtocol &protocol_p) override; - idx_t Read(uint64_t num_values, parquet_filter_t &filter, uint8_t *define_out, uint8_t *repeat_out, + idx_t Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result) override; void Skip(idx_t num_values) override; diff --git a/extension/parquet/parquet_reader.cpp b/extension/parquet/parquet_reader.cpp index eee9a696ce09..c29ebc6ea12c 100644 --- a/extension/parquet/parquet_reader.cpp +++ b/extension/parquet/parquet_reader.cpp @@ -58,7 +58,7 @@ static shared_ptr LoadMetadata(Allocator &allocator, F auto current_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); auto proto = CreateThriftProtocol(allocator, file_handle, false); - auto &transport = ((ThriftFileTransport &)*proto->getTransport()); + auto &transport = reinterpret_cast(*proto->getTransport()); auto file_size = transport.GetSize(); if (file_size < 12) { throw InvalidInputException("File '%s' too small to be a Parquet file", file_handle.path); @@ -71,11 +71,11 @@ static shared_ptr LoadMetadata(Allocator &allocator, F transport.SetLocation(file_size - 8); transport.read((uint8_t *)buf.ptr, 8); - if (strncmp(buf.ptr + 4, "PAR1", 4) != 0) { + if (memcmp(buf.ptr + 4, "PAR1", 4) != 0) { throw InvalidInputException("No magic bytes found at end of file '%s'", file_handle.path); } // read four-byte footer length from just before the end magic bytes - auto footer_len = *(uint32_t *)buf.ptr; + auto footer_len = *reinterpret_cast(buf.ptr); if (footer_len <= 0 || file_size < 12 + footer_len) { throw InvalidInputException("Footer length error in file '%s'", file_handle.path); } diff --git a/extension/parquet/parquet_statistics.cpp b/extension/parquet/parquet_statistics.cpp index 20aa93f28432..5f243aecf721 100644 --- a/extension/parquet/parquet_statistics.cpp +++ b/extension/parquet/parquet_statistics.cpp @@ -123,21 +123,17 @@ Value ParquetStatisticsUtils::ConvertValue(const LogicalType &type, } switch (type.InternalType()) { case PhysicalType::INT16: - return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), - width, scale); + return Value::DECIMAL(ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, + scale); case PhysicalType::INT32: - return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), - width, scale); + return Value::DECIMAL(ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, + scale); case PhysicalType::INT64: - return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), - width, scale); + return Value::DECIMAL(ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, + scale); case PhysicalType::INT128: - return Value::DECIMAL( - ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), - width, scale); + return Value::DECIMAL(ParquetDecimalUtils::ReadDecimalValue(stats_data, stats.size()), width, + scale); default: throw InternalException("Unsupported internal type for decimal"); } diff --git a/extension/parquet/zstd_file_system.cpp b/extension/parquet/zstd_file_system.cpp index a6e8b3099e5d..1998e3147ba3 100644 --- a/extension/parquet/zstd_file_system.cpp +++ b/extension/parquet/zstd_file_system.cpp @@ -61,9 +61,9 @@ bool ZstdStreamWrapper::Read(StreamData &sd) { throw IOException(duckdb_zstd::ZSTD_getErrorName(res)); } - sd.in_buff_start = (data_ptr_t)in_buffer.src + in_buffer.pos; // NOLINT - sd.in_buff_end = (data_ptr_t)in_buffer.src + in_buffer.size; // NOLINT - sd.out_buff_end = (data_ptr_t)out_buffer.dst + out_buffer.pos; // NOLINT + sd.in_buff_start = (data_ptr_t)in_buffer.src + in_buffer.pos; // NOLINT + sd.in_buff_end = (data_ptr_t)in_buffer.src + in_buffer.size; // NOLINT + sd.out_buff_end = (data_ptr_t)out_buffer.dst + out_buffer.pos; // NOLINT return false; } diff --git a/src/common/fsst.cpp b/src/common/fsst.cpp index e0351d5d9dce..6c8c3de41292 100644 --- a/src/common/fsst.cpp +++ b/src/common/fsst.cpp @@ -10,7 +10,7 @@ string_t FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, Vector &resu D_ASSERT(result.GetVectorType() == VectorType::FLAT_VECTOR); unsigned char decompress_buffer[StringUncompressed::STRING_BLOCK_LIMIT + 1]; auto fsst_decoder = reinterpret_cast(duckdb_fsst_decoder); - auto compressed_string_ptr = (unsigned char *) compressed_string; // NOLINT + auto compressed_string_ptr = (unsigned char *)compressed_string; // NOLINT auto decompressed_string_size = duckdb_fsst_decompress(fsst_decoder, compressed_string_len, compressed_string_ptr, StringUncompressed::STRING_BLOCK_LIMIT + 1, &decompress_buffer[0]); @@ -22,7 +22,7 @@ string_t FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, Vector &resu Value FSSTPrimitives::DecompressValue(void *duckdb_fsst_decoder, const char *compressed_string, idx_t compressed_string_len) { unsigned char decompress_buffer[StringUncompressed::STRING_BLOCK_LIMIT + 1]; - auto compressed_string_ptr = (unsigned char *) compressed_string; // NOLINT + auto compressed_string_ptr = (unsigned char *)compressed_string; // NOLINT auto fsst_decoder = reinterpret_cast(duckdb_fsst_decoder); auto decompressed_string_size = duckdb_fsst_decompress(fsst_decoder, compressed_string_len, compressed_string_ptr, diff --git a/src/common/gzip_file_system.cpp b/src/common/gzip_file_system.cpp index c59fd1a801e5..82c23546912e 100644 --- a/src/common/gzip_file_system.cpp +++ b/src/common/gzip_file_system.cpp @@ -193,7 +193,7 @@ bool MiniZStreamWrapper::Read(StreamData &sd) { throw IOException("Failed to decode gzip stream: %s", duckdb_miniz::mz_error(ret)); } // update pointers following inflate() - sd.in_buff_start = (data_ptr_t) mz_stream_ptr->next_in; // NOLINT + sd.in_buff_start = (data_ptr_t)mz_stream_ptr->next_in; // NOLINT sd.in_buff_end = sd.in_buff_start + mz_stream_ptr->avail_in; sd.out_buff_end = data_ptr_cast(mz_stream_ptr->next_out); D_ASSERT(sd.out_buff_end + mz_stream_ptr->avail_out == sd.out_buff.get() + sd.out_buf_size); diff --git a/src/common/sort/merge_sorter.cpp b/src/common/sort/merge_sorter.cpp index c7ef208d70df..24ce46619bb1 100644 --- a/src/common/sort/merge_sorter.cpp +++ b/src/common/sort/merge_sorter.cpp @@ -544,7 +544,9 @@ void MergeSorter::MergeData(SortedData &result_data, SortedData &l_data, SortedD const bool &l_smaller = left_smaller[copied + i]; const bool r_smaller = !l_smaller; const auto &entry_size = next_entry_sizes[copied + i]; - memcpy(result_heap_ptr, reinterpret_cast(l_smaller * CastPointerToValue(l_heap_ptr) + r_smaller * CastPointerToValue(r_heap_ptr)), + memcpy(result_heap_ptr, + reinterpret_cast(l_smaller * CastPointerToValue(l_heap_ptr) + + r_smaller * CastPointerToValue(r_heap_ptr)), entry_size); D_ASSERT(Load(result_heap_ptr) == entry_size); result_heap_ptr += entry_size; @@ -585,7 +587,10 @@ void MergeSorter::MergeRows(data_ptr_t &l_ptr, idx_t &l_entry_idx, const idx_t & const bool &l_smaller = left_smaller[copied + i]; const bool r_smaller = !l_smaller; // Use comparison bool (0 or 1) to copy an entry from either side - FastMemcpy(target_ptr, reinterpret_cast(l_smaller * CastPointerToValue(l_ptr) + r_smaller * CastPointerToValue(r_ptr)), entry_size); + FastMemcpy( + target_ptr, + reinterpret_cast(l_smaller * CastPointerToValue(l_ptr) + r_smaller * CastPointerToValue(r_ptr)), + entry_size); target_ptr += entry_size; // Use the comparison bool to increment entries and pointers l_entry_idx += l_smaller; diff --git a/src/common/types/column/column_data_allocator.cpp b/src/common/types/column/column_data_allocator.cpp index f17495f33367..b0a81935d546 100644 --- a/src/common/types/column/column_data_allocator.cpp +++ b/src/common/types/column/column_data_allocator.cpp @@ -162,10 +162,10 @@ data_ptr_t ColumnDataAllocator::GetDataPointer(ChunkManagementState &state, uint // in-memory allocator: construct pointer from block_id and offset if (sizeof(uintptr_t) == sizeof(uint32_t)) { uintptr_t pointer_value = uintptr_t(block_id); - return (data_ptr_t) pointer_value; // NOLINT - convert from pointer value back to pointer + return (data_ptr_t)pointer_value; // NOLINT - convert from pointer value back to pointer } else if (sizeof(uintptr_t) == sizeof(uint64_t)) { uintptr_t pointer_value = (uintptr_t(offset) << 32) | uintptr_t(block_id); - return (data_ptr_t) pointer_value; // NOLINT - convert from pointer value back to pointer + return (data_ptr_t)pointer_value; // NOLINT - convert from pointer value back to pointer } else { throw InternalException("ColumnDataCollection: Architecture not supported!?"); } diff --git a/src/common/types/hyperloglog.cpp b/src/common/types/hyperloglog.cpp index 7029831deee5..ed555aeeb7a0 100644 --- a/src/common/types/hyperloglog.cpp +++ b/src/common/types/hyperloglog.cpp @@ -121,7 +121,8 @@ inline uint64_t TemplatedHash(const T &elem) { template <> inline uint64_t TemplatedHash(const hugeint_t &elem) { - return TemplatedHash(Load(const_data_ptr_cast(&elem.upper))) ^ TemplatedHash(elem.lower); + return TemplatedHash(Load(const_data_ptr_cast(&elem.upper))) ^ + TemplatedHash(elem.lower); } template diff --git a/src/common/types/list_segment.cpp b/src/common/types/list_segment.cpp index cb15832144c8..10ff112742cc 100644 --- a/src/common/types/list_segment.cpp +++ b/src/common/types/list_segment.cpp @@ -23,7 +23,8 @@ static T *GetPrimitiveData(ListSegment *segment) { template static const T *GetPrimitiveData(const ListSegment *segment) { - return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + + segment->capacity * sizeof(bool)); } //===--------------------------------------------------------------------===// @@ -38,21 +39,23 @@ static data_ptr_t AllocateListData(Allocator &allocator, uint16_t capacity) { } static uint64_t *GetListLengthData(ListSegment *segment) { - return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + + segment->capacity * sizeof(bool)); } static const uint64_t *GetListLengthData(const ListSegment *segment) { - return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + + segment->capacity * sizeof(bool)); } static const LinkedList *GetListChildData(const ListSegment *segment) { return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + - segment->capacity * (sizeof(bool) + sizeof(uint64_t))); + segment->capacity * (sizeof(bool) + sizeof(uint64_t))); } static LinkedList *GetListChildData(ListSegment *segment) { return reinterpret_cast(data_ptr_cast(segment) + sizeof(ListSegment) + - segment->capacity * (sizeof(bool) + sizeof(uint64_t))); + segment->capacity * (sizeof(bool) + sizeof(uint64_t))); } //===--------------------------------------------------------------------===// @@ -67,11 +70,13 @@ static data_ptr_t AllocateStructData(Allocator &allocator, uint16_t capacity, id } static ListSegment **GetStructData(ListSegment *segment) { - return reinterpret_cast(data_ptr_cast(segment) + + sizeof(ListSegment) + segment->capacity * sizeof(bool)); + return reinterpret_cast(data_ptr_cast(segment) + +sizeof(ListSegment) + + segment->capacity * sizeof(bool)); } -static const ListSegment * const * GetStructData(const ListSegment *segment) { - return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + segment->capacity * sizeof(bool)); +static const ListSegment *const *GetStructData(const ListSegment *segment) { + return reinterpret_cast(const_data_ptr_cast(segment) + sizeof(ListSegment) + + segment->capacity * sizeof(bool)); } static bool *GetNullMask(ListSegment *segment) { @@ -148,7 +153,8 @@ void DestroyListSegment(const ListSegmentFunctions &functions, ListSegment *segm static ListSegment *CreateStructSegment(const ListSegmentFunctions &functions, Allocator &allocator, uint16_t capacity) { // allocate data and set header - auto segment = reinterpret_cast(AllocateStructData(allocator, capacity, functions.child_functions.size())); + auto segment = + reinterpret_cast(AllocateStructData(allocator, capacity, functions.child_functions.size())); segment->capacity = capacity; segment->count = 0; segment->next = nullptr; diff --git a/src/common/types/row/tuple_data_allocator.cpp b/src/common/types/row/tuple_data_allocator.cpp index d044cd93bf02..17ca9057cb5a 100644 --- a/src/common/types/row/tuple_data_allocator.cpp +++ b/src/common/types/row/tuple_data_allocator.cpp @@ -241,8 +241,10 @@ void TupleDataAllocator::InitializeChunkStateInternal(TupleDataPinState &pin_sta lock_guard guard(part->lock); const auto old_base_heap_ptr = part->base_heap_ptr; if (old_base_heap_ptr != new_base_heap_ptr) { - Vector old_heap_ptrs(Value::POINTER(CastPointerToValue(old_base_heap_ptr + part->heap_block_offset))); - Vector new_heap_ptrs(Value::POINTER(CastPointerToValue(new_base_heap_ptr + part->heap_block_offset))); + Vector old_heap_ptrs( + Value::POINTER(CastPointerToValue(old_base_heap_ptr + part->heap_block_offset))); + Vector new_heap_ptrs( + Value::POINTER(CastPointerToValue(new_base_heap_ptr + part->heap_block_offset))); RecomputeHeapPointers(old_heap_ptrs, *ConstantVector::ZeroSelectionVector(), row_locations, new_heap_ptrs, offset, next, layout, 0); part->base_heap_ptr = new_base_heap_ptr; diff --git a/src/common/types/vector.cpp b/src/common/types/vector.cpp index 9d9e62517c48..6e5bd6215390 100644 --- a/src/common/types/vector.cpp +++ b/src/common/types/vector.cpp @@ -455,9 +455,8 @@ Value Vector::GetValueInternal(const Vector &v_p, idx_t index_p) { throw InternalException("FSST Vector with non-string datatype found!"); } auto str_compressed = reinterpret_cast(data)[index]; - Value result = - FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(const_cast(*vector)), - str_compressed.GetData(), str_compressed.GetSize()); + Value result = FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(const_cast(*vector)), + str_compressed.GetData(), str_compressed.GetSize()); return result; } @@ -638,8 +637,7 @@ string Vector::ToString(idx_t count) const { for (idx_t i = 0; i < count; i++) { string_t compressed_string = reinterpret_cast(data)[i]; Value val = FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(const_cast(*this)), - compressed_string.GetData(), - compressed_string.GetSize()); + compressed_string.GetData(), compressed_string.GetSize()); retval += GetValue(i).ToString() + (i == count - 1 ? "" : ", "); } } break; @@ -983,7 +981,8 @@ void Vector::FormatSerialize(FormatSerializer &serializer, idx_t count) { auto row_idx = vdata.sel->get_index(i); flat_mask.Set(i, vdata.validity.RowIsValid(row_idx)); } - serializer.WriteProperty("validity", const_data_ptr_cast(flat_mask.GetData()), flat_mask.ValidityMaskSize(count)); + serializer.WriteProperty("validity", const_data_ptr_cast(flat_mask.GetData()), + flat_mask.ValidityMaskSize(count)); } if (TypeIsConstantSize(logical_type.InternalType())) { // constant size type: simple copy @@ -1723,9 +1722,8 @@ void FSSTVector::DecompressVector(const Vector &src, Vector &dst, idx_t src_offs auto target_idx = dst_offset + i; string_t compressed_string = ldata[source_idx]; if (dst_mask.RowIsValid(target_idx) && compressed_string.GetSize() > 0) { - tdata[target_idx] = FSSTPrimitives::DecompressValue(FSSTVector::GetDecoder(src), dst, - compressed_string.GetData(), - compressed_string.GetSize()); + tdata[target_idx] = FSSTPrimitives::DecompressValue( + FSSTVector::GetDecoder(src), dst, compressed_string.GetData(), compressed_string.GetSize()); } else { tdata[target_idx] = string_t(nullptr, 0); } diff --git a/src/core_functions/scalar/string/string_split.cpp b/src/core_functions/scalar/string/string_split.cpp index ef9137d87b9f..7a41d3bd862b 100644 --- a/src/core_functions/scalar/string/string_split.cpp +++ b/src/core_functions/scalar/string/string_split.cpp @@ -36,8 +36,8 @@ struct RegularStringSplit { if (delim_size == 0) { return 0; } - return ContainsFun::Find(const_uchar_ptr_cast(input_data), input_size, - const_uchar_ptr_cast(delim_data), delim_size); + return ContainsFun::Find(const_uchar_ptr_cast(input_data), input_size, const_uchar_ptr_cast(delim_data), + delim_size); } }; @@ -45,7 +45,7 @@ struct ConstantRegexpStringSplit { static idx_t Find(const char *input_data, idx_t input_size, const char *delim_data, idx_t delim_size, idx_t &match_size, void *data) { D_ASSERT(data); - auto regex = reinterpret_cast(data); + auto regex = reinterpret_cast(data); duckdb_re2::StringPiece match; if (!regex->Match(duckdb_re2::StringPiece(input_data, input_size), 0, input_size, RE2::UNANCHORED, &match, 1)) { return DConstants::INVALID_INDEX; diff --git a/src/execution/window_segment_tree.cpp b/src/execution/window_segment_tree.cpp index 2756e51a1aa6..abad7f8b296c 100644 --- a/src/execution/window_segment_tree.cpp +++ b/src/execution/window_segment_tree.cpp @@ -12,7 +12,8 @@ namespace duckdb { WindowAggregateState::WindowAggregateState(AggregateObject aggr, const LogicalType &result_type_p) : aggr(std::move(aggr)), result_type(result_type_p), state(aggr.function.state_size()), - statev(Value::POINTER(CastPointerToValue(state.data()))), statep(Value::POINTER(CastPointerToValue(state.data()))) { + statev(Value::POINTER(CastPointerToValue(state.data()))), + statep(Value::POINTER(CastPointerToValue(state.data()))) { statev.SetVectorType(VectorType::FLAT_VECTOR); // Prevent conversion of results to constants } @@ -183,8 +184,9 @@ void WindowConstantAggregate::Compute(Vector &target, idx_t rid, idx_t start, id WindowSegmentTree::WindowSegmentTree(AggregateObject aggr_p, const LogicalType &result_type_p, DataChunk *input, const ValidityMask &filter_mask_p, WindowAggregationMode mode_p) : aggr(std::move(aggr_p)), result_type(result_type_p), state(aggr.function.state_size()), - statep(Value::POINTER(CastPointerToValue(state.data()))), frame(0, 0), statev(Value::POINTER(CastPointerToValue(state.data()))), - internal_nodes(0), input_ref(input), filter_mask(filter_mask_p), mode(mode_p) { + statep(Value::POINTER(CastPointerToValue(state.data()))), frame(0, 0), + statev(Value::POINTER(CastPointerToValue(state.data()))), internal_nodes(0), input_ref(input), + filter_mask(filter_mask_p), mode(mode_p) { statep.Flatten(input->size()); statev.SetVectorType(VectorType::FLAT_VECTOR); // Prevent conversion of results to constants diff --git a/src/function/scalar/string/like.cpp b/src/function/scalar/string/like.cpp index ebdb66a0fafa..55f28fb99589 100644 --- a/src/function/scalar/string/like.cpp +++ b/src/function/scalar/string/like.cpp @@ -105,8 +105,8 @@ struct LikeMatcher : public FunctionData { for (; segment_idx < end_idx; segment_idx++) { auto &segment = segments[segment_idx]; // find the pattern of the current segment - idx_t next_offset = ContainsFun::Find( - str_data, str_len, const_uchar_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); + idx_t next_offset = ContainsFun::Find(str_data, str_len, const_uchar_ptr_cast(segment.pattern.c_str()), + segment.pattern.size()); if (next_offset == DConstants::INVALID_INDEX) { // could not find this pattern in the string: no match return false; @@ -130,8 +130,8 @@ struct LikeMatcher : public FunctionData { } else { auto &segment = segments.back(); // find the pattern of the current segment - idx_t next_offset = ContainsFun::Find( - str_data, str_len, const_uchar_ptr_cast(segment.pattern.c_str()), segment.pattern.size()); + idx_t next_offset = ContainsFun::Find(str_data, str_len, const_uchar_ptr_cast(segment.pattern.c_str()), + segment.pattern.size()); return next_offset != DConstants::INVALID_INDEX; } } diff --git a/src/function/scalar/system/aggregate_export.cpp b/src/function/scalar/system/aggregate_export.cpp index 04d6256e15d0..6e61591fd4cb 100644 --- a/src/function/scalar/system/aggregate_export.cpp +++ b/src/function/scalar/system/aggregate_export.cpp @@ -145,11 +145,13 @@ static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Ve continue; } if (state0_data.validity.RowIsValid(state0_idx) && !state1_data.validity.RowIsValid(state1_idx)) { - result_ptr[i] = StringVector::AddStringOrBlob(result, const_char_ptr_cast(state0.GetData()), bind_data.state_size); + result_ptr[i] = + StringVector::AddStringOrBlob(result, const_char_ptr_cast(state0.GetData()), bind_data.state_size); continue; } if (!state0_data.validity.RowIsValid(state0_idx) && state1_data.validity.RowIsValid(state1_idx)) { - result_ptr[i] = StringVector::AddStringOrBlob(result, const_char_ptr_cast(state1.GetData()), bind_data.state_size); + result_ptr[i] = + StringVector::AddStringOrBlob(result, const_char_ptr_cast(state1.GetData()), bind_data.state_size); continue; } @@ -165,8 +167,8 @@ static void AggregateStateCombine(DataChunk &input, ExpressionState &state_p, Ve AggregateInputData aggr_input_data(nullptr, Allocator::DefaultAllocator()); bind_data.aggr.combine(local_state.state_vector0, local_state.state_vector1, aggr_input_data, 1); - result_ptr[i] = - StringVector::AddStringOrBlob(result, const_char_ptr_cast(local_state.state_buffer1.get()), bind_data.state_size); + result_ptr[i] = StringVector::AddStringOrBlob(result, const_char_ptr_cast(local_state.state_buffer1.get()), + bind_data.state_size); } } diff --git a/src/function/table/copy_csv.cpp b/src/function/table/copy_csv.cpp index 560734dc55bf..5d128c96cefb 100644 --- a/src/function/table/copy_csv.cpp +++ b/src/function/table/copy_csv.cpp @@ -187,15 +187,14 @@ static bool RequiresQuotes(WriteCSVData &csv_data, const char *str, idx_t len) { // check for delimiter if (options.delimiter.length() != 0 && - ContainsFun::Find(const_uchar_ptr_cast(str), len, - const_uchar_ptr_cast(options.delimiter.c_str()), + ContainsFun::Find(const_uchar_ptr_cast(str), len, const_uchar_ptr_cast(options.delimiter.c_str()), options.delimiter.size()) != DConstants::INVALID_INDEX) { return true; } // check for quote - if (options.quote.length() != 0 && ContainsFun::Find(const_uchar_ptr_cast(str), len, - const_uchar_ptr_cast(options.quote.c_str()), - options.quote.size()) != DConstants::INVALID_INDEX) { + if (options.quote.length() != 0 && + ContainsFun::Find(const_uchar_ptr_cast(str), len, const_uchar_ptr_cast(options.quote.c_str()), + options.quote.size()) != DConstants::INVALID_INDEX) { return true; } return false; @@ -225,13 +224,11 @@ static void WriteQuotedString(Serializer &serializer, WriteCSVData &csv_data, co // complex CSV // check for quote or escape separately if (options.quote.length() != 0 && - ContainsFun::Find(const_uchar_ptr_cast(str), len, - const_uchar_ptr_cast(options.quote.c_str()), + ContainsFun::Find(const_uchar_ptr_cast(str), len, const_uchar_ptr_cast(options.quote.c_str()), options.quote.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } else if (options.escape.length() != 0 && - ContainsFun::Find(const_uchar_ptr_cast(str), len, - const_uchar_ptr_cast(options.escape.c_str()), + ContainsFun::Find(const_uchar_ptr_cast(str), len, const_uchar_ptr_cast(options.escape.c_str()), options.escape.size()) != DConstants::INVALID_INDEX) { requires_escape = true; } diff --git a/src/include/duckdb/common/fsst.hpp b/src/include/duckdb/common/fsst.hpp index b953c7d06135..f0cf6faf4d69 100644 --- a/src/include/duckdb/common/fsst.hpp +++ b/src/include/duckdb/common/fsst.hpp @@ -14,7 +14,6 @@ class FSSTPrimitives { public: static string_t DecompressValue(void *duckdb_fsst_decoder, Vector &result, const char *compressed_string, idx_t compressed_string_len); - static Value DecompressValue(void *duckdb_fsst_decoder, const char *compressed_string, - idx_t compressed_string_len); + static Value DecompressValue(void *duckdb_fsst_decoder, const char *compressed_string, idx_t compressed_string_len); }; } // namespace duckdb diff --git a/src/include/duckdb/common/typedefs.hpp b/src/include/duckdb/common/typedefs.hpp index c517436abcc7..96dcd89be73d 100644 --- a/src/include/duckdb/common/typedefs.hpp +++ b/src/include/duckdb/common/typedefs.hpp @@ -48,7 +48,6 @@ const char *const_char_ptr_cast(const SRC *src) { return reinterpret_cast(src); } - template const unsigned char *const_uchar_ptr_cast(const SRC *src) { return reinterpret_cast(src); diff --git a/src/include/duckdb/common/types/null_value.hpp b/src/include/duckdb/common/types/null_value.hpp index af584d17b41b..ed96b721a257 100644 --- a/src/include/duckdb/common/types/null_value.hpp +++ b/src/include/duckdb/common/types/null_value.hpp @@ -42,7 +42,7 @@ inline string_t NullValue() { template <> inline char *NullValue() { - return (char *) NullValue(); // NOLINT + return (char *)NullValue(); // NOLINT } template <> diff --git a/src/include/duckdb/common/types/string_type.hpp b/src/include/duckdb/common/types/string_type.hpp index 3749dc17aeb9..a72f9a6f35cc 100644 --- a/src/include/duckdb/common/types/string_type.hpp +++ b/src/include/duckdb/common/types/string_type.hpp @@ -55,7 +55,7 @@ struct string_t { #else memset(value.pointer.prefix, 0, PREFIX_BYTES); #endif - value.pointer.ptr = (char *) data; // NOLINT + value.pointer.ptr = (char *)data; // NOLINT } } string_t(const char *data) : string_t(data, strlen(data)) { // NOLINT: Allow implicit conversion from `const char*` @@ -76,7 +76,7 @@ struct string_t { } char *GetDataWriteable() const { - return IsInlined() ? (char *) value.inlined.inlined : value.pointer.ptr; // NOLINT + return IsInlined() ? (char *)value.inlined.inlined : value.pointer.ptr; // NOLINT } const char *GetPrefix() const { diff --git a/src/main/capi/logical_types-c.cpp b/src/main/capi/logical_types-c.cpp index a9dcb8789596..66f8bb3bd4cc 100644 --- a/src/main/capi/logical_types-c.cpp +++ b/src/main/capi/logical_types-c.cpp @@ -56,7 +56,8 @@ duckdb_logical_type duckdb_create_map_type(duckdb_logical_type key_type, duckdb_ return nullptr; } duckdb::LogicalType *mtype = new duckdb::LogicalType; - *mtype = duckdb::LogicalType::MAP(*reinterpret_cast(key_type), *reinterpret_cast(value_type)); + *mtype = duckdb::LogicalType::MAP(*reinterpret_cast(key_type), + *reinterpret_cast(value_type)); return reinterpret_cast(mtype); } diff --git a/src/main/capi/result-c.cpp b/src/main/capi/result-c.cpp index 95be217c5a13..cd1495b52f4b 100644 --- a/src/main/capi/result-c.cpp +++ b/src/main/capi/result-c.cpp @@ -266,7 +266,7 @@ duckdb_state duckdb_translate_result(unique_ptr result_p, duckdb_re if (result.HasError()) { // write the error message - out->__deprecated_error_message = (char *) result.GetError().c_str(); // NOLINT + out->__deprecated_error_message = (char *)result.GetError().c_str(); // NOLINT return DuckDBError; } // copy the data @@ -317,7 +317,7 @@ bool deprecated_materialize_result(duckdb_result *result) { memset(result->__deprecated_columns, 0, sizeof(duckdb_column) * column_count); for (idx_t i = 0; i < column_count; i++) { result->__deprecated_columns[i].__deprecated_type = ConvertCPPTypeToC(result_data->result->types[i]); - result->__deprecated_columns[i].__deprecated_name = (char *) result_data->result->names[i].c_str(); // NOLINT + result->__deprecated_columns[i].__deprecated_name = (char *)result_data->result->names[i].c_str(); // NOLINT } result->__deprecated_row_count = materialized.RowCount(); if (result->__deprecated_row_count > 0 && diff --git a/src/storage/compression/bitpacking.cpp b/src/storage/compression/bitpacking.cpp index 9820694175a0..ac1286265669 100644 --- a/src/storage/compression/bitpacking.cpp +++ b/src/storage/compression/bitpacking.cpp @@ -860,8 +860,8 @@ void BitpackingFetchRow(ColumnSegment &segment, ColumnFetchState &state, row_t r D_ASSERT(scan_state.current_group.mode == BitpackingMode::FOR || scan_state.current_group.mode == BitpackingMode::DELTA_FOR); - BitpackingPrimitives::UnPackBlock(data_ptr_cast(scan_state.decompression_buffer), decompression_group_start_pointer, - scan_state.current_width, skip_sign_extend); + BitpackingPrimitives::UnPackBlock(data_ptr_cast(scan_state.decompression_buffer), + decompression_group_start_pointer, scan_state.current_width, skip_sign_extend); *current_result_ptr = *(T *)(scan_state.decompression_buffer + offset_in_compression_group); *current_result_ptr += scan_state.current_frame_of_reference; diff --git a/src/storage/compression/dictionary_compression.cpp b/src/storage/compression/dictionary_compression.cpp index b407ff91e708..1c74c102e975 100644 --- a/src/storage/compression/dictionary_compression.cpp +++ b/src/storage/compression/dictionary_compression.cpp @@ -554,7 +554,7 @@ void DictionaryCompressionStorage::StringFetchRow(ColumnSegment &segment, Column auto header_ptr = reinterpret_cast(baseptr); auto dict = DictionaryCompressionStorage::GetDictionary(segment, handle); auto index_buffer_offset = Load(data_ptr_cast(&header_ptr->index_buffer_offset)); - auto width = (bitpacking_width_t) Load(data_ptr_cast(&header_ptr->bitpacking_width)); + auto width = (bitpacking_width_t)Load(data_ptr_cast(&header_ptr->bitpacking_width)); auto index_buffer_ptr = reinterpret_cast(baseptr + index_buffer_offset); auto base_data = data_ptr_cast(baseptr + DICTIONARY_HEADER_SIZE); auto result_data = FlatVector::GetData(result); diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index 59ff43508b1b..9f2a2f4c66e1 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -338,8 +338,8 @@ class FSSTCompressionState : public CompressionState { D_ASSERT(current_segment->count == index_buffer.size()); BitpackingPrimitives::PackBuffer(base_ptr + compressed_index_buffer_offset, - reinterpret_cast(index_buffer.data()), current_segment->count, - current_width); + reinterpret_cast(index_buffer.data()), + current_segment->count, current_width); // Write the fsst symbol table or nothing if (fsst_encoder != nullptr) { @@ -522,8 +522,8 @@ unique_ptr FSSTStorage::StringInitScan(ColumnSegment &segment) auto base_ptr = state->handle.Ptr() + segment.GetBlockOffset(); state->duckdb_fsst_decoder = make_buffer(); - auto retval = ParseFSSTSegmentHeader(base_ptr, reinterpret_cast(state->duckdb_fsst_decoder.get()), - &state->current_width); + auto retval = ParseFSSTSegmentHeader( + base_ptr, reinterpret_cast(state->duckdb_fsst_decoder.get()), &state->current_width); if (!retval) { state->duckdb_fsst_decoder = nullptr; } @@ -617,8 +617,8 @@ void FSSTStorage::StringScanPartial(ColumnSegment &segment, ColumnScanState &sta dict, baseptr, delta_decode_buffer[i + offsets.unused_delta_decoded_values]); if (str_len > 0) { - result_data[i + result_offset] = FSSTPrimitives::DecompressValue( - scan_state.duckdb_fsst_decoder.get(), result, str_ptr, str_len); + result_data[i + result_offset] = + FSSTPrimitives::DecompressValue(scan_state.duckdb_fsst_decoder.get(), result, str_ptr, str_len); } else { result_data[i + result_offset] = string_t(nullptr, 0); } @@ -668,8 +668,8 @@ void FSSTStorage::StringFetchRow(ColumnSegment &segment, ColumnFetchState &state string_t compressed_string = UncompressedStringStorage::FetchStringFromDict( segment, dict, result, base_ptr, delta_decode_buffer[offsets.unused_delta_decoded_values], string_length); - result_data[result_idx] = FSSTPrimitives::DecompressValue( - (void *)&decoder, result, compressed_string.GetData(), compressed_string.GetSize()); + result_data[result_idx] = FSSTPrimitives::DecompressValue((void *)&decoder, result, compressed_string.GetData(), + compressed_string.GetSize()); } else { // There's no fsst symtable, this only happens for empty strings or nulls, we can just emit an empty string result_data[result_idx] = string_t(nullptr, 0); diff --git a/src/storage/statistics/string_stats.cpp b/src/storage/statistics/string_stats.cpp index 65dfba0be8b5..5f1aefd15960 100644 --- a/src/storage/statistics/string_stats.cpp +++ b/src/storage/statistics/string_stats.cpp @@ -140,8 +140,8 @@ void StringStats::Update(BaseStatistics &stats, const string_t &value) { if (unicode == UnicodeType::UNICODE) { string_data.has_unicode = true; } else if (unicode == UnicodeType::INVALID) { - throw InvalidInputException( - ErrorManager::InvalidUnicodeError(string(const_char_ptr_cast(data), size), "segment statistics update")); + throw InvalidInputException(ErrorManager::InvalidUnicodeError(string(const_char_ptr_cast(data), size), + "segment statistics update")); } } } @@ -219,10 +219,11 @@ string StringStats::ToString(const BaseStatistics &stats) { auto &string_data = StringStats::GetDataUnsafe(stats); idx_t min_len = GetValidMinMaxSubstring(string_data.min); idx_t max_len = GetValidMinMaxSubstring(string_data.max); - return StringUtil::Format( - "[Min: %s, Max: %s, Has Unicode: %s, Max String Length: %s]", string(const_char_ptr_cast(string_data.min), min_len), - string(const_char_ptr_cast(string_data.max), max_len), string_data.has_unicode ? "true" : "false", - string_data.has_max_string_length ? to_string(string_data.max_string_length) : "?"); + return StringUtil::Format("[Min: %s, Max: %s, Has Unicode: %s, Max String Length: %s]", + string(const_char_ptr_cast(string_data.min), min_len), + string(const_char_ptr_cast(string_data.max), max_len), + string_data.has_unicode ? "true" : "false", + string_data.has_max_string_length ? to_string(string_data.max_string_length) : "?"); } void StringStats::Verify(const BaseStatistics &stats, Vector &vector, const SelectionVector &sel, idx_t count) { @@ -256,13 +257,13 @@ void StringStats::Verify(const BaseStatistics &stats, Vector &vector, const Sele throw InternalException("Invalid unicode detected in vector: %s", vector.ToString(count)); } } - if (StringValueComparison(const_data_ptr_cast(data), MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), - string_data.min) < 0) { + if (StringValueComparison(const_data_ptr_cast(data), + MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), string_data.min) < 0) { throw InternalException("Statistics mismatch: value is smaller than min.\nStatistics: %s\nVector: %s", stats.ToString(), vector.ToString(count)); } - if (StringValueComparison(const_data_ptr_cast(data), MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), - string_data.max) > 0) { + if (StringValueComparison(const_data_ptr_cast(data), + MinValue(len, StringStatsData::MAX_STRING_MINMAX_SIZE), string_data.max) > 0) { throw InternalException("Statistics mismatch: value is bigger than max.\nStatistics: %s\nVector: %s", stats.ToString(), vector.ToString(count)); } diff --git a/tools/rpkg/src/transform.cpp b/tools/rpkg/src/transform.cpp index a1fe2770c28a..ea6ff8b45c39 100644 --- a/tools/rpkg/src/transform.cpp +++ b/tools/rpkg/src/transform.cpp @@ -216,7 +216,8 @@ void duckdb_r_transform(Vector &src_vec, const SEXP dest, idx_t dest_offset, idx if (!mask.RowIsValid(row_idx)) { SET_STRING_ELT(dest, dest_offset + row_idx, NA_STRING); } else { - SET_STRING_ELT(dest, dest_offset + row_idx, (SEXP)(data_ptr_cast(child_ptr[row_idx]) - sexp_header_size)); + SET_STRING_ELT(dest, dest_offset + row_idx, + (SEXP)(data_ptr_cast(child_ptr[row_idx]) - sexp_header_size)); } } return; From f4bf9bd55ef2c1e8c95bde01d6d4a1cfd7396449 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 13:53:02 +0200 Subject: [PATCH 19/28] Less c-style casts in the Parquet extension --- extension/parquet/column_reader.cpp | 38 +++++++++---------- extension/parquet/column_writer.cpp | 2 +- extension/parquet/include/column_reader.hpp | 3 +- extension/parquet/parquet-extension.cpp | 2 +- extension/parquet/parquet_metadata.cpp | 2 +- extension/parquet/parquet_reader.cpp | 30 +++++++-------- .../transaction/duck_transaction_manager.hpp | 4 +- src/storage/compression/fsst.cpp | 2 +- .../compression/validity_uncompressed.cpp | 4 +- src/transaction/duck_transaction.cpp | 14 ++++--- src/transaction/duck_transaction_manager.cpp | 30 +++++++-------- tools/pythonpkg/src/pandas/scan.cpp | 12 +++--- tools/pythonpkg/src/pyfilesystem.cpp | 2 +- tools/pythonpkg/src/pyresult.cpp | 4 +- tools/pythonpkg/src/python_udf.cpp | 6 +-- 15 files changed, 79 insertions(+), 76 deletions(-) diff --git a/extension/parquet/column_reader.cpp b/extension/parquet/column_reader.cpp index 235af296a0c9..0ce15ab01cd4 100644 --- a/extension/parquet/column_reader.cpp +++ b/extension/parquet/column_reader.cpp @@ -284,22 +284,21 @@ void ColumnReader::PreparePageV2(PageHeader &page_hdr) { uncompressed = true; } if (uncompressed) { - trans.read(reinterpret_cast(block->ptr), page_hdr.compressed_page_size); + trans.read(block->ptr, page_hdr.compressed_page_size); return; } // copy repeats & defines as-is because FOR SOME REASON they are uncompressed auto uncompressed_bytes = page_hdr.data_page_header_v2.repetition_levels_byte_length + page_hdr.data_page_header_v2.definition_levels_byte_length; - trans.read(reinterpret_cast(block->ptr), uncompressed_bytes); + trans.read(block->ptr, uncompressed_bytes); auto compressed_bytes = page_hdr.compressed_page_size - uncompressed_bytes; AllocateCompressed(compressed_bytes); - trans.read(reinterpret_cast(compressed_buffer.ptr), compressed_bytes); + trans.read(compressed_buffer.ptr, compressed_bytes); - DecompressInternal(chunk->meta_data.codec, const_char_ptr_cast(compressed_buffer.ptr), compressed_bytes, - char_ptr_cast(block->ptr) + uncompressed_bytes, + DecompressInternal(chunk->meta_data.codec, compressed_buffer.ptr, compressed_bytes, block->ptr + uncompressed_bytes, page_hdr.uncompressed_page_size - uncompressed_bytes); } @@ -330,24 +329,24 @@ void ColumnReader::PreparePage(PageHeader &page_hdr) { AllocateCompressed(page_hdr.compressed_page_size + 1); trans.read((uint8_t *)compressed_buffer.ptr, page_hdr.compressed_page_size); - DecompressInternal(chunk->meta_data.codec, (const char *)compressed_buffer.ptr, page_hdr.compressed_page_size, - (char *)block->ptr, page_hdr.uncompressed_page_size); + DecompressInternal(chunk->meta_data.codec, compressed_buffer.ptr, page_hdr.compressed_page_size, block->ptr, + page_hdr.uncompressed_page_size); } -void ColumnReader::DecompressInternal(CompressionCodec::type codec, const char *src, idx_t src_size, char *dst, - idx_t dst_size) { +void ColumnReader::DecompressInternal(CompressionCodec::type codec, const_data_ptr_t src, idx_t src_size, + data_ptr_t dst, idx_t dst_size) { switch (codec) { case CompressionCodec::UNCOMPRESSED: throw InternalException("Parquet data unexpectedly uncompressed"); case CompressionCodec::GZIP: { MiniZStream s; - s.Decompress(src, src_size, dst, dst_size); + s.Decompress(const_char_ptr_cast(src), src_size, char_ptr_cast(dst), dst_size); break; } case CompressionCodec::SNAPPY: { { size_t uncompressed_size = 0; - auto res = duckdb_snappy::GetUncompressedLength(src, src_size, &uncompressed_size); + auto res = duckdb_snappy::GetUncompressedLength(const_char_ptr_cast(src), src_size, &uncompressed_size); if (!res) { throw std::runtime_error("Snappy decompression failure"); } @@ -355,7 +354,7 @@ void ColumnReader::DecompressInternal(CompressionCodec::type codec, const char * throw std::runtime_error("Snappy decompression failure: Uncompressed data size mismatch"); } } - auto res = duckdb_snappy::RawUncompress(src, src_size, dst); + auto res = duckdb_snappy::RawUncompress(const_char_ptr_cast(src), src_size, char_ptr_cast(dst)); if (!res) { throw std::runtime_error("Snappy decompression failure"); } @@ -454,7 +453,7 @@ void ColumnReader::PrepareDataPage(PageHeader &page_hdr) { idx_t ColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, data_ptr_t define_out, data_ptr_t repeat_out, Vector &result) { // we need to reset the location because multiple column readers share the same protocol - auto &trans = (ThriftFileTransport &)*protocol->getTransport(); + auto &trans = reinterpret_cast(*protocol->getTransport()); trans.SetLocation(chunk_read_offset); // Perform any skips that were not applied yet. @@ -500,7 +499,8 @@ idx_t ColumnReader::Read(uint64_t num_values, parquet_filter_t &filter, data_ptr offset_buffer.resize(reader.allocator, sizeof(uint32_t) * (read_now - null_count)); dict_decoder->GetBatch(offset_buffer.ptr, read_now - null_count); DictReference(result); - Offsets((uint32_t *)offset_buffer.ptr, define_out, read_now, filter, result_offset, result); + Offsets(reinterpret_cast(offset_buffer.ptr), define_out, read_now, filter, result_offset, + result); } else if (dbp_decoder) { // TODO keep this in the state auto read_buf = make_shared(); @@ -648,7 +648,7 @@ void StringColumnReader::PrepareDeltaLengthByteArray(ResizeableBuffer &buffer) { byte_array_data = make_uniq(LogicalType::VARCHAR, nullptr); return; } - auto length_data = (uint32_t *)length_buffer->ptr; + auto length_data = reinterpret_cast(length_buffer->ptr); byte_array_data = make_uniq(LogicalType::VARCHAR, value_count); byte_array_count = value_count; delta_offset = 0; @@ -675,8 +675,8 @@ void StringColumnReader::PrepareDeltaByteArray(ResizeableBuffer &buffer) { byte_array_data = make_uniq(LogicalType::VARCHAR, nullptr); return; } - auto prefix_data = (uint32_t *)prefix_buffer->ptr; - auto suffix_data = (uint32_t *)suffix_buffer->ptr; + auto prefix_data = reinterpret_cast(prefix_buffer->ptr); + auto suffix_data = reinterpret_cast(suffix_buffer->ptr); byte_array_data = make_uniq(LogicalType::VARCHAR, prefix_count); byte_array_count = prefix_count; delta_offset = 0; @@ -1100,7 +1100,7 @@ idx_t StructColumnReader::GroupRowsAvailable() { template struct DecimalParquetValueConversion { static DUCKDB_PHYSICAL_TYPE DictRead(ByteBuffer &dict, uint32_t &offset, ColumnReader &reader) { - auto dict_ptr = (DUCKDB_PHYSICAL_TYPE *)dict.ptr; + auto dict_ptr = reinterpret_cast(dict.ptr); return dict_ptr[offset]; } @@ -1284,7 +1284,7 @@ class IntervalColumnReader : public TemplatedColumnReader dictionary_data, idx_t num_entries) override { // NOLINT AllocateDict(num_entries * sizeof(interval_t)); - auto dict_ptr = (interval_t *)this->dict->ptr; + auto dict_ptr = reinterpret_cast(this->dict->ptr); for (idx_t i = 0; i < num_entries; i++) { dict_ptr[i] = IntervalValueConversion::PlainRead(*dictionary_data, *this); } diff --git a/extension/parquet/column_writer.cpp b/extension/parquet/column_writer.cpp index 037f376fceb2..37d32a200b3f 100644 --- a/extension/parquet/column_writer.cpp +++ b/extension/parquet/column_writer.cpp @@ -983,7 +983,7 @@ class FixedDecimalStatistics : public ColumnWriterStatistics { string GetStats(hugeint_t &input) { data_t buffer[16]; WriteParquetDecimal(input, buffer); - return string((char *)buffer, 16); + return string(const_char_ptr_cast(buffer), 16); } bool HasStats() { diff --git a/extension/parquet/include/column_reader.hpp b/extension/parquet/include/column_reader.hpp index 2fef0e806294..d26ee88adeee 100644 --- a/extension/parquet/include/column_reader.hpp +++ b/extension/parquet/include/column_reader.hpp @@ -143,7 +143,8 @@ class ColumnReader { void PreparePage(PageHeader &page_hdr); void PrepareDataPage(PageHeader &page_hdr); void PreparePageV2(PageHeader &page_hdr); - void DecompressInternal(CompressionCodec::type codec, const char *src, idx_t src_size, char *dst, idx_t dst_size); + void DecompressInternal(CompressionCodec::type codec, const_data_ptr_t src, idx_t src_size, data_ptr_t dst, + idx_t dst_size); const duckdb_parquet::format::ColumnChunk *chunk = nullptr; diff --git a/extension/parquet/parquet-extension.cpp b/extension/parquet/parquet-extension.cpp index d28f5d2de048..57467e61353d 100644 --- a/extension/parquet/parquet-extension.cpp +++ b/extension/parquet/parquet-extension.cpp @@ -343,7 +343,7 @@ class ParquetScanFunction { static unique_ptr ParquetScanInitGlobal(ClientContext &context, TableFunctionInitInput &input) { - auto &bind_data = input.bind_data->Cast(); + auto &bind_data = input.bind_data->CastNoConst(); auto result = make_uniq(); result->file_opening = vector(bind_data.files.size(), false); diff --git a/extension/parquet/parquet_metadata.cpp b/extension/parquet/parquet_metadata.cpp index b15e4db233bd..f37194291226 100644 --- a/extension/parquet/parquet_metadata.cpp +++ b/extension/parquet/parquet_metadata.cpp @@ -19,7 +19,7 @@ struct ParquetMetaDataBindData : public TableFunctionData { public: bool Equals(const FunctionData &other_p) const override { - auto &other = (const ParquetMetaDataBindData &)other_p; + auto &other = other_p.Cast(); return other.return_types == return_types && files == other.files; } }; diff --git a/extension/parquet/parquet_reader.cpp b/extension/parquet/parquet_reader.cpp index c29ebc6ea12c..34c7d3c1c3ea 100644 --- a/extension/parquet/parquet_reader.cpp +++ b/extension/parquet/parquet_reader.cpp @@ -484,7 +484,7 @@ unique_ptr ParquetReader::ReadStatistics(const string &name) { unique_ptr column_stats; auto file_meta_data = GetFileMetadata(); auto root_reader = CreateReader(); - auto column_reader = ((StructColumnReader *)root_reader.get())->GetChildReader(file_col_idx); + auto column_reader = root_reader->Cast().GetChildReader(file_col_idx); for (idx_t row_group_idx = 0; row_group_idx < file_meta_data->row_groups.size(); row_group_idx++) { auto &row_group = file_meta_data->row_groups[row_group_idx]; @@ -573,7 +573,7 @@ idx_t ParquetReader::GetGroupOffset(ParquetReaderScanState &state) { void ParquetReader::PrepareRowGroupBuffer(ParquetReaderScanState &state, idx_t col_idx) { auto &group = GetGroup(state); auto column_id = reader_data.column_ids[col_idx]; - auto column_reader = ((StructColumnReader *)state.root_reader.get())->GetChildReader(column_id); + auto column_reader = state.root_reader->Cast().GetChildReader(column_id); // TODO move this to columnreader too if (reader_data.filters) { @@ -760,14 +760,14 @@ static void FilterOperationSwitch(Vector &v, Value &constant, parquet_filter_t & static void ApplyFilter(Vector &v, TableFilter &filter, parquet_filter_t &filter_mask, idx_t count) { switch (filter.filter_type) { case TableFilterType::CONJUNCTION_AND: { - auto &conjunction = (ConjunctionAndFilter &)filter; + auto &conjunction = filter.Cast(); for (auto &child_filter : conjunction.child_filters) { ApplyFilter(v, *child_filter, filter_mask, count); } break; } case TableFilterType::CONJUNCTION_OR: { - auto &conjunction = (ConjunctionOrFilter &)filter; + auto &conjunction = filter.Cast(); parquet_filter_t or_mask; for (auto &child_filter : conjunction.child_filters) { parquet_filter_t child_mask = filter_mask; @@ -778,7 +778,7 @@ static void ApplyFilter(Vector &v, TableFilter &filter, parquet_filter_t &filter break; } case TableFilterType::CONSTANT_COMPARISON: { - auto &constant_filter = (ConstantFilter &)filter; + auto &constant_filter = filter.Cast(); switch (constant_filter.comparison_type) { case ExpressionType::COMPARE_EQUAL: FilterOperationSwitch(v, constant_filter.constant, filter_mask, count); @@ -831,7 +831,7 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul state.current_group++; state.group_offset = 0; - auto &trans = (ThriftFileTransport &)*state.thrift_file_proto->getTransport(); + auto &trans = reinterpret_cast(*state.thrift_file_proto->getTransport()); trans.ClearPrefetch(); state.current_group_prefetched = false; @@ -846,8 +846,8 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul auto file_col_idx = reader_data.column_ids[col_idx]; - auto root_reader = ((StructColumnReader *)state.root_reader.get()); - to_scan_compressed_bytes += root_reader->GetChildReader(file_col_idx)->TotalCompressedSize(); + auto &root_reader = state.root_reader->Cast(); + to_scan_compressed_bytes += root_reader.GetChildReader(file_col_idx)->TotalCompressedSize(); } auto &group = GetGroup(state); @@ -880,14 +880,14 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul // Prefetch column-wise for (idx_t col_idx = 0; col_idx < reader_data.column_ids.size(); col_idx++) { auto file_col_idx = reader_data.column_ids[col_idx]; - auto root_reader = ((StructColumnReader *)state.root_reader.get()); + auto &root_reader = state.root_reader->Cast(); bool has_filter = false; if (reader_data.filters) { auto entry = reader_data.filters->filters.find(reader_data.column_mapping[col_idx]); has_filter = entry != reader_data.filters->filters.end(); } - root_reader->GetChildReader(file_col_idx)->RegisterPrefetch(trans, !(lazy_fetch && !has_filter)); + root_reader.GetChildReader(file_col_idx)->RegisterPrefetch(trans, !(lazy_fetch && !has_filter)); } trans.FinalizeRegistration(); @@ -924,7 +924,7 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul auto define_ptr = (uint8_t *)state.define_buf.ptr; auto repeat_ptr = (uint8_t *)state.repeat_buf.ptr; - auto root_reader = ((StructColumnReader *)state.root_reader.get()); + auto &root_reader = state.root_reader->Cast(); if (reader_data.filters) { vector need_to_read(reader_data.column_ids.size(), true); @@ -947,7 +947,7 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul auto result_idx = reader_data.column_mapping[id]; auto &result_vector = result.data[result_idx]; - auto child_reader = root_reader->GetChildReader(file_col_idx); + auto child_reader = root_reader.GetChildReader(file_col_idx); child_reader->Read(result.size(), filter_mask, define_ptr, repeat_ptr, result_vector); need_to_read[id] = false; @@ -962,11 +962,11 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul } auto file_col_idx = reader_data.column_ids[col_idx]; if (filter_mask.none()) { - root_reader->GetChildReader(file_col_idx)->Skip(result.size()); + root_reader.GetChildReader(file_col_idx)->Skip(result.size()); continue; } auto &result_vector = result.data[reader_data.column_mapping[col_idx]]; - auto child_reader = root_reader->GetChildReader(file_col_idx); + auto child_reader = root_reader.GetChildReader(file_col_idx); child_reader->Read(result.size(), filter_mask, define_ptr, repeat_ptr, result_vector); } @@ -982,7 +982,7 @@ bool ParquetReader::ScanInternal(ParquetReaderScanState &state, DataChunk &resul for (idx_t col_idx = 0; col_idx < reader_data.column_ids.size(); col_idx++) { auto file_col_idx = reader_data.column_ids[col_idx]; auto &result_vector = result.data[reader_data.column_mapping[col_idx]]; - auto child_reader = root_reader->GetChildReader(file_col_idx); + auto child_reader = root_reader.GetChildReader(file_col_idx); auto rows_read = child_reader->Read(result.size(), filter_mask, define_ptr, repeat_ptr, result_vector); if (rows_read != result.size()) { throw InvalidInputException("Mismatch in parquet read for column %llu, expected %llu rows, got %llu", diff --git a/src/include/duckdb/transaction/duck_transaction_manager.hpp b/src/include/duckdb/transaction/duck_transaction_manager.hpp index a913b2f01b65..12ed08e0e130 100644 --- a/src/include/duckdb/transaction/duck_transaction_manager.hpp +++ b/src/include/duckdb/transaction/duck_transaction_manager.hpp @@ -46,9 +46,9 @@ class DuckTransactionManager : public TransactionManager { } private: - bool CanCheckpoint(DuckTransaction *current = nullptr); + bool CanCheckpoint(optional_ptr current = nullptr); //! Remove the given transaction from the list of active transactions - void RemoveTransaction(DuckTransaction *transaction) noexcept; + void RemoveTransaction(DuckTransaction &transaction) noexcept; void LockClients(vector &client_locks, ClientContext &context); private: diff --git a/src/storage/compression/fsst.cpp b/src/storage/compression/fsst.cpp index 9f2a2f4c66e1..70e7f9870ff7 100644 --- a/src/storage/compression/fsst.cpp +++ b/src/storage/compression/fsst.cpp @@ -431,7 +431,7 @@ void FSSTStorage::Compress(CompressionState &state_p, Vector &scan_vector, idx_t total_count++; total_size += data[idx].GetSize(); sizes_in.push_back(data[idx].GetSize()); - strings_in.push_back((unsigned char *)data[idx].GetData()); + strings_in.push_back((unsigned char *)data[idx].GetData()); // NOLINT } // Only Nulls or empty strings in this vector, nothing to compress diff --git a/src/storage/compression/validity_uncompressed.cpp b/src/storage/compression/validity_uncompressed.cpp index 33389b297c0b..4046418e6f6b 100644 --- a/src/storage/compression/validity_uncompressed.cpp +++ b/src/storage/compression/validity_uncompressed.cpp @@ -382,7 +382,7 @@ void ValidityFetchRow(ColumnSegment &segment, ColumnFetchState &state, row_t row auto &buffer_manager = BufferManager::GetBufferManager(segment.db); auto handle = buffer_manager.Pin(segment.block); auto dataptr = handle.Ptr() + segment.GetBlockOffset(); - ValidityMask mask((validity_t *)dataptr); + ValidityMask mask(reinterpret_cast(dataptr)); auto &result_mask = FlatVector::Validity(result); if (!mask.RowIsValidUnsafe(row_id)) { result_mask.SetInvalid(result_idx); @@ -421,7 +421,7 @@ idx_t ValidityAppend(CompressionAppendState &append_state, ColumnSegment &segmen return append_count; } - ValidityMask mask((validity_t *)append_state.handle.Ptr()); + ValidityMask mask(reinterpret_cast(append_state.handle.Ptr())); for (idx_t i = 0; i < append_count; i++) { auto idx = data.sel->get_index(offset + i); if (!data.validity.RowIsValidUnsafe(idx)) { diff --git a/src/transaction/duck_transaction.cpp b/src/transaction/duck_transaction.cpp index 3804180f2e84..a26fd5f1225f 100644 --- a/src/transaction/duck_transaction.cpp +++ b/src/transaction/duck_transaction.cpp @@ -71,8 +71,8 @@ void DuckTransaction::PushCatalogEntry(CatalogEntry &entry, data_ptr_t extra_dat } void DuckTransaction::PushDelete(DataTable &table, ChunkVectorInfo *vinfo, row_t rows[], idx_t count, idx_t base_row) { - auto delete_info = - (DeleteInfo *)undo_buffer.CreateEntry(UndoFlags::DELETE_TUPLE, sizeof(DeleteInfo) + sizeof(row_t) * count); + auto delete_info = reinterpret_cast( + undo_buffer.CreateEntry(UndoFlags::DELETE_TUPLE, sizeof(DeleteInfo) + sizeof(row_t) * count)); delete_info->vinfo = vinfo; delete_info->table = &table; delete_info->count = count; @@ -81,18 +81,20 @@ void DuckTransaction::PushDelete(DataTable &table, ChunkVectorInfo *vinfo, row_t } void DuckTransaction::PushAppend(DataTable &table, idx_t start_row, idx_t row_count) { - auto append_info = (AppendInfo *)undo_buffer.CreateEntry(UndoFlags::INSERT_TUPLE, sizeof(AppendInfo)); + auto append_info = + reinterpret_cast(undo_buffer.CreateEntry(UndoFlags::INSERT_TUPLE, sizeof(AppendInfo))); append_info->table = &table; append_info->start_row = start_row; append_info->count = row_count; } UpdateInfo *DuckTransaction::CreateUpdateInfo(idx_t type_size, idx_t entries) { - auto update_info = (UpdateInfo *)undo_buffer.CreateEntry( + data_ptr_t base_info = undo_buffer.CreateEntry( UndoFlags::UPDATE_TUPLE, sizeof(UpdateInfo) + (sizeof(sel_t) + type_size) * STANDARD_VECTOR_SIZE); + auto update_info = reinterpret_cast(base_info); update_info->max = STANDARD_VECTOR_SIZE; - update_info->tuples = (sel_t *)((data_ptr_cast(update_info)) + sizeof(UpdateInfo)); - update_info->tuple_data = (data_ptr_cast(update_info)) + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; + update_info->tuples = reinterpret_cast(base_info + sizeof(UpdateInfo)); + update_info->tuple_data = base_info + sizeof(UpdateInfo) + sizeof(sel_t) * update_info->max; update_info->version_number = transaction_id; return update_info; } diff --git a/src/transaction/duck_transaction_manager.cpp b/src/transaction/duck_transaction_manager.cpp index 53ed9ec8d04e..b5dab24df81b 100644 --- a/src/transaction/duck_transaction_manager.cpp +++ b/src/transaction/duck_transaction_manager.cpp @@ -152,7 +152,7 @@ void DuckTransactionManager::Checkpoint(ClientContext &context, bool force) { // remove the transaction id from the list of active transactions // potentially resulting in garbage collection - RemoveTransaction(transaction.get()); + RemoveTransaction(*transaction); if (transaction_context) { transaction_context->transaction.ClearTransaction(); } @@ -164,7 +164,7 @@ void DuckTransactionManager::Checkpoint(ClientContext &context, bool force) { storage_manager.CreateCheckpoint(); } -bool DuckTransactionManager::CanCheckpoint(DuckTransaction *current) { +bool DuckTransactionManager::CanCheckpoint(optional_ptr current) { if (db.IsSystem()) { return false; } @@ -176,7 +176,7 @@ bool DuckTransactionManager::CanCheckpoint(DuckTransaction *current) { return false; } for (auto &transaction : active_transactions) { - if (transaction.get() != current) { + if (transaction.get() != current.get()) { return false; } } @@ -184,14 +184,14 @@ bool DuckTransactionManager::CanCheckpoint(DuckTransaction *current) { } string DuckTransactionManager::CommitTransaction(ClientContext &context, Transaction *transaction_p) { - auto transaction = (DuckTransaction *)transaction_p; + auto &transaction = transaction_p->Cast(); vector client_locks; auto lock = make_uniq>(transaction_lock); CheckpointLock checkpoint_lock(*this); // check if we can checkpoint - bool checkpoint = thread_is_checkpointing ? false : CanCheckpoint(transaction); + bool checkpoint = thread_is_checkpointing ? false : CanCheckpoint(&transaction); if (checkpoint) { - if (transaction->AutomaticCheckpoint(db)) { + if (transaction.AutomaticCheckpoint(db)) { checkpoint_lock.Lock(); // we might be able to checkpoint: lock all clients // to avoid deadlock we release the transaction lock while locking the clients @@ -200,7 +200,7 @@ string DuckTransactionManager::CommitTransaction(ClientContext &context, Transac LockClients(client_locks, context); lock = make_uniq>(transaction_lock); - checkpoint = CanCheckpoint(transaction); + checkpoint = CanCheckpoint(&transaction); if (!checkpoint) { checkpoint_lock.Unlock(); client_locks.clear(); @@ -212,12 +212,12 @@ string DuckTransactionManager::CommitTransaction(ClientContext &context, Transac // obtain a commit id for the transaction transaction_t commit_id = current_start_timestamp++; // commit the UndoBuffer of the transaction - string error = transaction->Commit(db, commit_id, checkpoint); + string error = transaction.Commit(db, commit_id, checkpoint); if (!error.empty()) { // commit unsuccessful: rollback the transaction instead checkpoint = false; - transaction->commit_id = 0; - transaction->Rollback(); + transaction.commit_id = 0; + transaction.Rollback(); } if (!checkpoint) { // we won't checkpoint after all: unlock the clients again @@ -239,19 +239,19 @@ string DuckTransactionManager::CommitTransaction(ClientContext &context, Transac } void DuckTransactionManager::RollbackTransaction(Transaction *transaction_p) { - auto transaction = (DuckTransaction *)transaction_p; + auto &transaction = transaction_p->Cast(); // obtain the transaction lock during this function lock_guard lock(transaction_lock); // rollback the transaction - transaction->Rollback(); + transaction.Rollback(); // remove the transaction id from the list of active transactions // potentially resulting in garbage collection RemoveTransaction(transaction); } -void DuckTransactionManager::RemoveTransaction(DuckTransaction *transaction) noexcept { +void DuckTransactionManager::RemoveTransaction(DuckTransaction &transaction) noexcept { // remove the transaction from the list of active transactions idx_t t_index = active_transactions.size(); // check for the lowest and highest start time in the list of transactions @@ -259,7 +259,7 @@ void DuckTransactionManager::RemoveTransaction(DuckTransaction *transaction) noe transaction_t lowest_transaction_id = MAX_TRANSACTION_ID; transaction_t lowest_active_query = MAXIMUM_QUERY_ID; for (idx_t i = 0; i < active_transactions.size(); i++) { - if (active_transactions[i].get() == transaction) { + if (active_transactions[i].get() == &transaction) { t_index = i; } else { transaction_t active_query = active_transactions[i]->active_query; @@ -275,7 +275,7 @@ void DuckTransactionManager::RemoveTransaction(DuckTransaction *transaction) noe D_ASSERT(t_index != active_transactions.size()); auto current_transaction = std::move(active_transactions[t_index]); auto current_query = DatabaseManager::Get(db).ActiveQueryNumber(); - if (transaction->commit_id != 0) { + if (transaction.commit_id != 0) { // the transaction was committed, add it to the list of recently // committed transactions recently_committed_transactions.push_back(std::move(current_transaction)); diff --git a/tools/pythonpkg/src/pandas/scan.cpp b/tools/pythonpkg/src/pandas/scan.cpp index 9be4bc630145..722f575c4370 100644 --- a/tools/pythonpkg/src/pandas/scan.cpp +++ b/tools/pythonpkg/src/pandas/scan.cpp @@ -74,7 +74,7 @@ idx_t PandasScanFunction::PandasScanGetBatchIndex(ClientContext &context, const unique_ptr PandasScanFunction::PandasScanBind(ClientContext &context, TableFunctionBindInput &input, vector &return_types, vector &names) { py::gil_scoped_acquire acquire; - py::handle df((PyObject *)(input.inputs[0].GetPointer())); + py::handle df(reinterpret_cast(input.inputs[0].GetPointer())); vector pandas_bind_data; @@ -112,14 +112,14 @@ idx_t PandasScanFunction::PandasScanMaxThreads(ClientContext &context, const Fun if (ClientConfig::GetConfig(context).verify_parallelism) { return context.db->NumberOfThreads(); } - auto &bind_data = (const PandasScanFunctionData &)*bind_data_p; + auto &bind_data = bind_data_p->Cast(); return bind_data.row_count / PANDAS_PARTITION_COUNT + 1; } bool PandasScanFunction::PandasScanParallelStateNext(ClientContext &context, const FunctionData *bind_data_p, LocalTableFunctionState *lstate, GlobalTableFunctionState *gstate) { - auto &bind_data = (const PandasScanFunctionData &)*bind_data_p; + auto &bind_data = bind_data_p->Cast(); auto ¶llel_state = gstate->Cast(); auto &state = lstate->Cast(); @@ -139,7 +139,7 @@ bool PandasScanFunction::PandasScanParallelStateNext(ClientContext &context, con double PandasScanFunction::PandasProgress(ClientContext &context, const FunctionData *bind_data_p, const GlobalTableFunctionState *gstate) { - auto &bind_data = (const PandasScanFunctionData &)*bind_data_p; + auto &bind_data = bind_data_p->Cast(); if (bind_data.row_count == 0) { return 100; } @@ -164,7 +164,7 @@ void PandasScanFunction::PandasBackendScanSwitch(PandasColumnBindData &bind_data //! The main pandas scan function: note that this can be called in parallel without the GIL //! hence this needs to be GIL-safe, i.e. no methods that create Python objects are allowed void PandasScanFunction::PandasScanFunc(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &data = (PandasScanFunctionData &)*data_p.bind_data; + auto &data = data_p.bind_data->Cast(); auto &state = data_p.local_state->Cast(); if (state.start >= state.end) { @@ -189,7 +189,7 @@ void PandasScanFunction::PandasScanFunc(ClientContext &context, TableFunctionInp unique_ptr PandasScanFunction::PandasScanCardinality(ClientContext &context, const FunctionData *bind_data) { - auto &data = (PandasScanFunctionData &)*bind_data; + auto &data = bind_data->Cast(); return make_uniq(data.row_count, data.row_count); } diff --git a/tools/pythonpkg/src/pyfilesystem.cpp b/tools/pythonpkg/src/pyfilesystem.cpp index f5d9a9d4caca..71712109c7a5 100644 --- a/tools/pythonpkg/src/pyfilesystem.cpp +++ b/tools/pythonpkg/src/pyfilesystem.cpp @@ -66,7 +66,7 @@ int64_t PythonFilesystem::Write(FileHandle &handle, void *buffer, int64_t nr_byt const auto &write = PythonFileHandle::GetHandle(handle).attr("write"); - auto data = py::bytes(std::string((const char *)buffer, nr_bytes)); + auto data = py::bytes(std::string(const_char_ptr_cast(buffer), nr_bytes)); return py::int_(write(data)); } diff --git a/tools/pythonpkg/src/pyresult.cpp b/tools/pythonpkg/src/pyresult.cpp index 1f7adf2c82be..1053ca4605d6 100644 --- a/tools/pythonpkg/src/pyresult.cpp +++ b/tools/pythonpkg/src/pyresult.cpp @@ -166,7 +166,7 @@ py::dict DuckDBPyResult::FetchNumpyInternal(bool stream, idx_t vectors_per_chunk } // iterate over the result to materialize the data needed for the NumPy arrays - idx_t initial_capacity = STANDARD_VECTOR_SIZE * 2; + idx_t initial_capacity = STANDARD_VECTOR_SIZE * 2ULL; if (result->type == QueryResultType::MATERIALIZED_RESULT) { // materialized query result: we know exactly how much space we need auto &materialized = (MaterializedQueryResult &)*result; @@ -333,7 +333,7 @@ duckdb::pyarrow::RecordBatchReader DuckDBPyResult::FetchRecordBatchReader(idx_t auto record_batch_reader_func = pyarrow_lib_module.attr("RecordBatchReader").attr("_import_from_c"); //! We have to construct an Arrow Array Stream ResultArrowArrayStreamWrapper *result_stream = new ResultArrowArrayStreamWrapper(std::move(result), rows_per_batch); - py::object record_batch_reader = record_batch_reader_func((uint64_t)&result_stream->stream); + py::object record_batch_reader = record_batch_reader_func((uint64_t)&result_stream->stream); // NOLINT return py::cast(record_batch_reader); } diff --git a/tools/pythonpkg/src/python_udf.cpp b/tools/pythonpkg/src/python_udf.cpp index b0893837cf85..b5039946b9c2 100644 --- a/tools/pythonpkg/src/python_udf.cpp +++ b/tools/pythonpkg/src/python_udf.cpp @@ -59,9 +59,9 @@ static void ConvertPyArrowToDataChunk(const py::object &table, Vector &out, Clie // Prepare the inputs for the bind vector children; children.reserve(3); - children.push_back(Value::POINTER((uintptr_t)stream_factory.get())); - children.push_back(Value::POINTER((uintptr_t)stream_factory_produce)); - children.push_back(Value::POINTER((uintptr_t)stream_factory_get_schema)); + children.push_back(Value::POINTER(CastPointerToValue(stream_factory.get()))); + children.push_back(Value::POINTER(CastPointerToValue(stream_factory_produce))); + children.push_back(Value::POINTER(CastPointerToValue(stream_factory_get_schema))); named_parameter_map_t named_params; vector input_types; vector input_names; From 26651f259128e9aa1e2ba586682ba52d66086d91 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 13:56:06 +0200 Subject: [PATCH 20/28] Revert R changes --- tools/rpkg/src/scan.cpp | 24 ++++++++++++------------ tools/rpkg/src/transform.cpp | 5 ++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tools/rpkg/src/scan.cpp b/tools/rpkg/src/scan.cpp index 51aeac58891f..bef98b2c7edd 100644 --- a/tools/rpkg/src/scan.cpp +++ b/tools/rpkg/src/scan.cpp @@ -85,23 +85,23 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context switch (rtypes[col_idx]) { case RType::LOGICAL: duckdb_col_type = LogicalType::BOOLEAN; - coldata_ptr = data_ptr_cast(LOGICAL_POINTER(coldata)); + coldata_ptr = (data_ptr_t)LOGICAL_POINTER(coldata); break; case RType::INTEGER: duckdb_col_type = LogicalType::INTEGER; - coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); + coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); break; case RType::NUMERIC: duckdb_col_type = LogicalType::DOUBLE; - coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); + coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); break; case RType::INTEGER64: duckdb_col_type = LogicalType::BIGINT; - coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); + coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); break; case RType::FACTOR: { // TODO What about factors that use numeric? - coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); + coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); strings levels = GET_LEVELS(coldata); Vector duckdb_levels(LogicalType::VARCHAR, levels.size()); auto levels_ptr = FlatVector::GetData(duckdb_levels); @@ -112,7 +112,7 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context break; } case RType::STRING: - coldata_ptr = data_ptr_cast(DATAPTR_RO(coldata)); + coldata_ptr = (data_ptr_t)DATAPTR_RO(coldata); if (experimental) { duckdb_col_type = RStringsType::Get(); } else { @@ -121,7 +121,7 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context break; case RType::TIMESTAMP: duckdb_col_type = LogicalType::TIMESTAMP; - coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); + coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); break; case RType::TIME_SECONDS: case RType::TIME_MINUTES: @@ -129,7 +129,7 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context case RType::TIME_DAYS: case RType::TIME_WEEKS: duckdb_col_type = LogicalType::TIME; - coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); + coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); break; case RType::TIME_SECONDS_INTEGER: case RType::TIME_MINUTES_INTEGER: @@ -137,24 +137,24 @@ static duckdb::unique_ptr DataFrameScanBind(ClientContext &context case RType::TIME_DAYS_INTEGER: case RType::TIME_WEEKS_INTEGER: duckdb_col_type = LogicalType::TIME; - coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); + coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); break; case RType::DATE: if (!IS_NUMERIC(coldata)) { cpp11::stop("DATE should really be integer"); } - coldata_ptr = data_ptr_cast(NUMERIC_POINTER(coldata)); + coldata_ptr = (data_ptr_t)NUMERIC_POINTER(coldata); duckdb_col_type = LogicalType::DATE; break; case RType::DATE_INTEGER: if (!IS_INTEGER(coldata)) { cpp11::stop("DATE_INTEGER should really be integer"); } - coldata_ptr = data_ptr_cast(INTEGER_POINTER(coldata)); + coldata_ptr = (data_ptr_t)INTEGER_POINTER(coldata); duckdb_col_type = LogicalType::DATE; break; case RType::BLOB: - coldata_ptr = data_ptr_cast(DATAPTR_RO(coldata)); + coldata_ptr = (data_ptr_t)DATAPTR_RO(coldata); duckdb_col_type = LogicalType::BLOB; break; default: diff --git a/tools/rpkg/src/transform.cpp b/tools/rpkg/src/transform.cpp index ea6ff8b45c39..9138df48405c 100644 --- a/tools/rpkg/src/transform.cpp +++ b/tools/rpkg/src/transform.cpp @@ -207,7 +207,7 @@ SEXP ToRString(const string_t &input) { void duckdb_r_transform(Vector &src_vec, const SEXP dest, idx_t dest_offset, idx_t n, bool integer64) { if (src_vec.GetType().GetAlias() == R_STRING_TYPE_NAME) { - ptrdiff_t sexp_header_size = data_ptr_cast(DATAPTR(R_BlankString)) - data_ptr_cast(R_BlankString); + ptrdiff_t sexp_header_size = (data_ptr_t)DATAPTR(R_BlankString) - (data_ptr_t)R_BlankString; auto child_ptr = FlatVector::GetData(src_vec); auto &mask = FlatVector::Validity(src_vec); @@ -216,8 +216,7 @@ void duckdb_r_transform(Vector &src_vec, const SEXP dest, idx_t dest_offset, idx if (!mask.RowIsValid(row_idx)) { SET_STRING_ELT(dest, dest_offset + row_idx, NA_STRING); } else { - SET_STRING_ELT(dest, dest_offset + row_idx, - (SEXP)(data_ptr_cast(child_ptr[row_idx]) - sexp_header_size)); + SET_STRING_ELT(dest, dest_offset + row_idx, (SEXP)((data_ptr_t)child_ptr[row_idx] - sexp_header_size)); } } return; From 14a5d1107874085cc3a189bbf64a7ddaf3db945d Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 13:58:42 +0200 Subject: [PATCH 21/28] Remove extra ; --- src/include/duckdb/common/types/hyperloglog.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/duckdb/common/types/hyperloglog.hpp b/src/include/duckdb/common/types/hyperloglog.hpp index 384f36ed4455..2a84bbf14ec7 100644 --- a/src/include/duckdb/common/types/hyperloglog.hpp +++ b/src/include/duckdb/common/types/hyperloglog.hpp @@ -14,7 +14,7 @@ namespace duckdb_hll { struct robj; -}; +} namespace duckdb { From 125689595aebbb1b12e82bc0eb72ed439a40f01b Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 14:02:06 +0200 Subject: [PATCH 22/28] CastNoConst --- tools/pythonpkg/src/pandas/scan.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/pythonpkg/src/pandas/scan.cpp b/tools/pythonpkg/src/pandas/scan.cpp index 722f575c4370..aef6283d380f 100644 --- a/tools/pythonpkg/src/pandas/scan.cpp +++ b/tools/pythonpkg/src/pandas/scan.cpp @@ -164,7 +164,7 @@ void PandasScanFunction::PandasBackendScanSwitch(PandasColumnBindData &bind_data //! The main pandas scan function: note that this can be called in parallel without the GIL //! hence this needs to be GIL-safe, i.e. no methods that create Python objects are allowed void PandasScanFunction::PandasScanFunc(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { - auto &data = data_p.bind_data->Cast(); + auto &data = data_p.bind_data->CastNoConst(); auto &state = data_p.local_state->Cast(); if (state.start >= state.end) { From 281057c8884846ff34f007f2a4692886cc08bf0f Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 14:08:08 +0200 Subject: [PATCH 23/28] Format fix --- src/common/types/hyperloglog.cpp | 4 ++- src/function/table/system/test_all_types.cpp | 6 +++-- src/include/duckdb.h | 28 ++++++++++---------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/common/types/hyperloglog.cpp b/src/common/types/hyperloglog.cpp index ed555aeeb7a0..c0455298ed43 100644 --- a/src/common/types/hyperloglog.cpp +++ b/src/common/types/hyperloglog.cpp @@ -58,7 +58,9 @@ HyperLogLog *HyperLogLog::MergePointer(HyperLogLog &other) { } unique_ptr HyperLogLog::Merge(HyperLogLog logs[], idx_t count) { - auto hlls_uptr = unique_ptr {new duckdb_hll::robj *[count]}; + auto hlls_uptr = unique_ptr { + new duckdb_hll::robj *[count] + }; auto hlls = hlls_uptr.get(); for (idx_t i = 0; i < count; i++) { hlls[i] = logs[i].hll; diff --git a/src/function/table/system/test_all_types.cpp b/src/function/table/system/test_all_types.cpp index 2c0f86f350fc..be429f8eb314 100644 --- a/src/function/table/system/test_all_types.cpp +++ b/src/function/table/system/test_all_types.cpp @@ -59,7 +59,8 @@ vector TestAllTypesFun::GetTestTypes() { result.emplace_back(LogicalType::INTERVAL, "interval", Value::INTERVAL(min_interval), Value::INTERVAL(max_interval)); // strings/blobs/bitstrings - result.emplace_back(LogicalType::VARCHAR, "varchar", Value("🦆🦆🦆🦆🦆🦆"), Value(string("goo\x00se", 6))); + result.emplace_back(LogicalType::VARCHAR, "varchar", Value("🦆🦆🦆🦆🦆🦆"), + Value(string("goo\x00se", 6))); result.emplace_back(LogicalType::BLOB, "blob", Value::BLOB("thisisalongblob\\x00withnullbytes"), Value::BLOB("\\x00\\x00\\x00a")); result.emplace_back(LogicalType::BIT, "bit", Value::BIT("0010001001011100010101011010111"), Value::BIT("10101")); @@ -123,7 +124,8 @@ vector TestAllTypesFun::GetTestTypes() { auto varchar_list_type = LogicalType::LIST(LogicalType::VARCHAR); auto empty_varchar_list = Value::EMPTYLIST(LogicalType::VARCHAR); - auto varchar_list = Value::LIST({Value("🦆🦆🦆🦆🦆🦆"), Value("goose"), Value(LogicalType::VARCHAR), Value("")}); + auto varchar_list = + Value::LIST({Value("🦆🦆🦆🦆🦆🦆"), Value("goose"), Value(LogicalType::VARCHAR), Value("")}); result.emplace_back(varchar_list_type, "varchar_array", empty_varchar_list, varchar_list); // nested arrays diff --git a/src/include/duckdb.h b/src/include/duckdb.h index a8b48e254e36..9d870acb3a04 100644 --- a/src/include/duckdb.h +++ b/src/include/duckdb.h @@ -260,46 +260,46 @@ typedef struct { typedef struct _duckdb_database { void *__db; -} *duckdb_database; +} * duckdb_database; typedef struct _duckdb_connection { void *__conn; -} *duckdb_connection; +} * duckdb_connection; typedef struct _duckdb_prepared_statement { void *__prep; -} *duckdb_prepared_statement; +} * duckdb_prepared_statement; typedef struct _duckdb_extracted_statements { void *__extrac; -} *duckdb_extracted_statements; +} * duckdb_extracted_statements; typedef struct _duckdb_pending_result { void *__pend; -} *duckdb_pending_result; +} * duckdb_pending_result; typedef struct _duckdb_appender { void *__appn; -} *duckdb_appender; +} * duckdb_appender; typedef struct _duckdb_arrow { void *__arrw; -} *duckdb_arrow; +} * duckdb_arrow; typedef struct _duckdb_config { void *__cnfg; -} *duckdb_config; +} * duckdb_config; typedef struct _duckdb_arrow_schema { void *__arrs; -} *duckdb_arrow_schema; +} * duckdb_arrow_schema; typedef struct _duckdb_arrow_array { void *__arra; -} *duckdb_arrow_array; +} * duckdb_arrow_array; typedef struct _duckdb_logical_type { void *__lglt; -} *duckdb_logical_type; +} * duckdb_logical_type; typedef struct _duckdb_data_chunk { void *__dtck; -} *duckdb_data_chunk; +} * duckdb_data_chunk; typedef struct _duckdb_vector { void *__vctr; -} *duckdb_vector; +} * duckdb_vector; typedef struct _duckdb_value { void *__val; -} *duckdb_value; +} * duckdb_value; typedef enum { DuckDBSuccess = 0, DuckDBError = 1 } duckdb_state; typedef enum { From a23725a6e51b766235f77cb1002d0559743dd0b8 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 16:06:37 +0200 Subject: [PATCH 24/28] Use reinterpret_cast in Cast headers, and more cast removal in Python client --- extension/parquet/column_writer.cpp | 4 +-- extension/parquet/include/column_reader.hpp | 4 +-- extension/parquet/include/column_writer.hpp | 8 ++--- src/common/gzip_file_system.cpp | 4 +-- src/common/types.cpp | 4 +-- src/include/duckdb/catalog/catalog.hpp | 4 +-- src/include/duckdb/catalog/catalog_entry.hpp | 4 +-- src/include/duckdb/common/allocator.hpp | 4 +-- src/include/duckdb/common/file_system.hpp | 4 +-- .../types/column/partitioned_column_data.hpp | 4 +-- .../types/row/partitioned_tuple_data.hpp | 4 +-- .../duckdb/common/types/vector_buffer.hpp | 8 ++--- .../execution/expression_executor_state.hpp | 4 +-- .../duckdb/execution/physical_operator.hpp | 4 +-- .../execution/physical_operator_states.hpp | 24 +++++++-------- .../duckdb/function/cast/default_casts.hpp | 8 ++--- .../duckdb/function/compression_function.hpp | 16 +++++----- src/include/duckdb/function/copy_function.hpp | 12 ++++---- src/include/duckdb/function/function.hpp | 6 ++-- .../duckdb/function/macro_function.hpp | 4 +-- .../duckdb/function/scalar_function.hpp | 4 +-- .../duckdb/function/table_function.hpp | 12 ++++---- src/include/duckdb/main/query_result.hpp | 4 +-- src/include/duckdb/main/relation.hpp | 12 ++++++++ src/include/duckdb/parser/base_expression.hpp | 4 +-- src/include/duckdb/parser/constraint.hpp | 4 +-- .../duckdb/parser/parsed_data/parse_info.hpp | 4 +-- src/include/duckdb/parser/query_node.hpp | 4 +-- src/include/duckdb/parser/result_modifier.hpp | 4 +-- src/include/duckdb/parser/sql_statement.hpp | 11 ++++--- src/include/duckdb/parser/tableref.hpp | 4 +-- .../duckdb/planner/bound_constraint.hpp | 4 +-- .../duckdb/planner/bound_query_node.hpp | 4 +-- .../duckdb/planner/bound_result_modifier.hpp | 4 +-- src/include/duckdb/planner/bound_tableref.hpp | 4 +-- .../duckdb/planner/logical_operator.hpp | 4 +-- src/include/duckdb/planner/table_binding.hpp | 4 +-- src/include/duckdb/planner/table_filter.hpp | 4 +-- src/include/duckdb/storage/index.hpp | 4 +-- .../duckdb/storage/partial_block_manager.hpp | 2 +- .../duckdb/storage/storage_manager.hpp | 4 +-- .../duckdb/storage/table/chunk_info.hpp | 4 +-- .../storage/table/column_checkpoint_state.hpp | 4 +-- .../duckdb/storage/table/scan_state.hpp | 8 ++--- .../duckdb/transaction/transaction.hpp | 4 +-- src/main/relation.cpp | 2 +- src/main/relation/create_table_relation.cpp | 2 +- src/main/relation/create_view_relation.cpp | 2 +- src/main/relation/delete_relation.cpp | 2 +- src/main/relation/explain_relation.cpp | 2 +- src/main/relation/insert_relation.cpp | 2 +- src/main/relation/update_relation.cpp | 2 +- src/main/relation/write_csv_relation.cpp | 2 +- src/main/relation/write_parquet_relation.cpp | 2 +- src/transaction/duck_transaction.cpp | 2 +- .../duckdb_python/numpy/array_wrapper.hpp | 2 +- tools/pythonpkg/src/map.cpp | 10 +++---- tools/pythonpkg/src/native/python_objects.cpp | 5 ++-- tools/pythonpkg/src/numpy/array_wrapper.cpp | 14 ++++----- tools/pythonpkg/src/numpy/numpy_scan.cpp | 30 +++++++++---------- tools/pythonpkg/src/pyconnection.cpp | 24 +++++++-------- tools/pythonpkg/src/pyrelation.cpp | 6 ++-- tools/pythonpkg/src/pyresult.cpp | 16 +++++----- 63 files changed, 200 insertions(+), 186 deletions(-) diff --git a/extension/parquet/column_writer.cpp b/extension/parquet/column_writer.cpp index 37d32a200b3f..405969c56f37 100644 --- a/extension/parquet/column_writer.cpp +++ b/extension/parquet/column_writer.cpp @@ -285,12 +285,12 @@ class ColumnWriterPageState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/extension/parquet/include/column_reader.hpp b/extension/parquet/include/column_reader.hpp index d26ee88adeee..33b7cb047c49 100644 --- a/extension/parquet/include/column_reader.hpp +++ b/extension/parquet/include/column_reader.hpp @@ -175,7 +175,7 @@ class ColumnReader { if (TARGET::TYPE != PhysicalType::INVALID && type.InternalType() != TARGET::TYPE) { throw InternalException("Failed to cast column reader to type - type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -183,7 +183,7 @@ class ColumnReader { if (TARGET::TYPE != PhysicalType::INVALID && type.InternalType() != TARGET::TYPE) { throw InternalException("Failed to cast column reader to type - type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/extension/parquet/include/column_writer.hpp b/extension/parquet/include/column_writer.hpp index 90b939378bc8..4885ad84900a 100644 --- a/extension/parquet/include/column_writer.hpp +++ b/extension/parquet/include/column_writer.hpp @@ -29,12 +29,12 @@ class ColumnWriterState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -51,12 +51,12 @@ class ColumnWriterStatistics { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/common/gzip_file_system.cpp b/src/common/gzip_file_system.cpp index 82c23546912e..a1c351166dcf 100644 --- a/src/common/gzip_file_system.cpp +++ b/src/common/gzip_file_system.cpp @@ -221,14 +221,14 @@ bool MiniZStreamWrapper::Read(StreamData &sd) { void MiniZStreamWrapper::Write(CompressedFile &file, StreamData &sd, data_ptr_t uncompressed_data, int64_t uncompressed_size) { // update the src and the total size - crc = duckdb_miniz::mz_crc32(crc, (const unsigned char *)uncompressed_data, uncompressed_size); + crc = duckdb_miniz::mz_crc32(crc, reinterpret_cast(uncompressed_data), uncompressed_size); total_size += uncompressed_size; auto remaining = uncompressed_size; while (remaining > 0) { idx_t output_remaining = (sd.out_buff.get() + sd.out_buf_size) - sd.out_buff_start; - mz_stream_ptr->next_in = (const unsigned char *)uncompressed_data; + mz_stream_ptr->next_in = reinterpret_cast(uncompressed_data); mz_stream_ptr->avail_in = remaining; mz_stream_ptr->next_out = sd.out_buff_start; mz_stream_ptr->avail_out = output_remaining; diff --git a/src/common/types.cpp b/src/common/types.cpp index c4cf057e4845..ca2b351a1cfa 100644 --- a/src/common/types.cpp +++ b/src/common/types.cpp @@ -814,12 +814,12 @@ struct ExtraTypeInfo { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } protected: diff --git a/src/include/duckdb/catalog/catalog.hpp b/src/include/duckdb/catalog/catalog.hpp index ad20296e71df..5881f9cffa48 100644 --- a/src/include/duckdb/catalog/catalog.hpp +++ b/src/include/duckdb/catalog/catalog.hpp @@ -333,13 +333,13 @@ class Catalog { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/catalog/catalog_entry.hpp b/src/include/duckdb/catalog/catalog_entry.hpp index fe74e1735d48..a8a0a74561db 100644 --- a/src/include/duckdb/catalog/catalog_entry.hpp +++ b/src/include/duckdb/catalog/catalog_entry.hpp @@ -72,12 +72,12 @@ class CatalogEntry { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/common/allocator.hpp b/src/include/duckdb/common/allocator.hpp index fb6f6e9fa613..cac14c293675 100644 --- a/src/include/duckdb/common/allocator.hpp +++ b/src/include/duckdb/common/allocator.hpp @@ -30,12 +30,12 @@ struct PrivateAllocatorData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/common/file_system.hpp b/src/include/duckdb/common/file_system.hpp index d410fe064760..6c9d4962ea3e 100644 --- a/src/include/duckdb/common/file_system.hpp +++ b/src/include/duckdb/common/file_system.hpp @@ -80,12 +80,12 @@ struct FileHandle { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } public: diff --git a/src/include/duckdb/common/types/column/partitioned_column_data.hpp b/src/include/duckdb/common/types/column/partitioned_column_data.hpp index 000df62f49e6..3defecb01fe8 100644 --- a/src/include/duckdb/common/types/column/partitioned_column_data.hpp +++ b/src/include/duckdb/common/types/column/partitioned_column_data.hpp @@ -113,12 +113,12 @@ class PartitionedColumnData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp b/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp index 87487e92e2a1..8c9b976b49c1 100644 --- a/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp +++ b/src/include/duckdb/common/types/row/partitioned_tuple_data.hpp @@ -140,12 +140,12 @@ class PartitionedTupleData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/common/types/vector_buffer.hpp b/src/include/duckdb/common/types/vector_buffer.hpp index 0d8c1813cb79..94341102bdaa 100644 --- a/src/include/duckdb/common/types/vector_buffer.hpp +++ b/src/include/duckdb/common/types/vector_buffer.hpp @@ -52,7 +52,7 @@ struct VectorAuxiliaryData { if (type != TARGET::TYPE) { throw InternalException("Failed to cast vector auxiliary data to type - type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -60,7 +60,7 @@ struct VectorAuxiliaryData { if (type != TARGET::TYPE) { throw InternalException("Failed to cast vector auxiliary data to type - type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -126,12 +126,12 @@ class VectorBuffer { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/execution/expression_executor_state.hpp b/src/include/duckdb/execution/expression_executor_state.hpp index db4d229bded1..c40908ed82bf 100644 --- a/src/include/duckdb/execution/expression_executor_state.hpp +++ b/src/include/duckdb/execution/expression_executor_state.hpp @@ -44,12 +44,12 @@ struct ExpressionState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/execution/physical_operator.hpp b/src/include/duckdb/execution/physical_operator.hpp index e6d311afb385..e26d44fe13b8 100644 --- a/src/include/duckdb/execution/physical_operator.hpp +++ b/src/include/duckdb/execution/physical_operator.hpp @@ -187,7 +187,7 @@ class PhysicalOperator { if (TARGET::TYPE != PhysicalOperatorType::INVALID && type != TARGET::TYPE) { throw InternalException("Failed to cast physical operator to type - physical operator type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -195,7 +195,7 @@ class PhysicalOperator { if (TARGET::TYPE != PhysicalOperatorType::INVALID && type != TARGET::TYPE) { throw InternalException("Failed to cast physical operator to type - physical operator type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/execution/physical_operator_states.hpp b/src/include/duckdb/execution/physical_operator_states.hpp index bad471c0d44e..cb4cb8677108 100644 --- a/src/include/duckdb/execution/physical_operator_states.hpp +++ b/src/include/duckdb/execution/physical_operator_states.hpp @@ -48,12 +48,12 @@ class OperatorState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -65,12 +65,12 @@ class GlobalOperatorState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -86,12 +86,12 @@ class GlobalSinkState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -106,12 +106,12 @@ class LocalSinkState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -127,12 +127,12 @@ class GlobalSourceState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -144,12 +144,12 @@ class LocalSourceState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/function/cast/default_casts.hpp b/src/include/duckdb/function/cast/default_casts.hpp index 12f6b6ab0b56..97e9ff902eff 100644 --- a/src/include/duckdb/function/cast/default_casts.hpp +++ b/src/include/duckdb/function/cast/default_casts.hpp @@ -26,12 +26,12 @@ struct BindCastInfo { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -44,12 +44,12 @@ struct BoundCastData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/function/compression_function.hpp b/src/include/duckdb/function/compression_function.hpp index 012929630b20..a691a8aa82cd 100644 --- a/src/include/duckdb/function/compression_function.hpp +++ b/src/include/duckdb/function/compression_function.hpp @@ -33,12 +33,12 @@ struct AnalyzeState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -49,12 +49,12 @@ struct CompressionState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -65,12 +65,12 @@ struct CompressedSegmentState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -85,12 +85,12 @@ struct CompressionAppendState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/function/copy_function.hpp b/src/include/duckdb/function/copy_function.hpp index ab4e98b72449..3588724ae45c 100644 --- a/src/include/duckdb/function/copy_function.hpp +++ b/src/include/duckdb/function/copy_function.hpp @@ -27,12 +27,12 @@ struct LocalFunctionData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -43,12 +43,12 @@ struct GlobalFunctionData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -59,12 +59,12 @@ struct PreparedBatchData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/function/function.hpp b/src/include/duckdb/function/function.hpp index 706fa495277a..9a9bb9e441fb 100644 --- a/src/include/duckdb/function/function.hpp +++ b/src/include/duckdb/function/function.hpp @@ -49,17 +49,17 @@ struct FunctionData { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } // FIXME: this function should be removed in the future template TARGET &CastNoConst() const { - return (TARGET &)*this; + return const_cast(reinterpret_cast(*this)); } }; diff --git a/src/include/duckdb/function/macro_function.hpp b/src/include/duckdb/function/macro_function.hpp index 29fea6c11ce8..2444cf0b5984 100644 --- a/src/include/duckdb/function/macro_function.hpp +++ b/src/include/duckdb/function/macro_function.hpp @@ -57,7 +57,7 @@ class MacroFunction { if (type != TARGET::TYPE) { throw InternalException("Failed to cast macro to type - macro type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -65,7 +65,7 @@ class MacroFunction { if (type != TARGET::TYPE) { throw InternalException("Failed to cast macro to type - macro type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/function/scalar_function.hpp b/src/include/duckdb/function/scalar_function.hpp index 7742383ca4ce..74ef87d8a34b 100644 --- a/src/include/duckdb/function/scalar_function.hpp +++ b/src/include/duckdb/function/scalar_function.hpp @@ -25,12 +25,12 @@ struct FunctionLocalState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/function/table_function.hpp b/src/include/duckdb/function/table_function.hpp index 83cd99b4aca3..003d240f70cc 100644 --- a/src/include/duckdb/function/table_function.hpp +++ b/src/include/duckdb/function/table_function.hpp @@ -31,12 +31,12 @@ struct TableFunctionInfo { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -55,12 +55,12 @@ struct GlobalTableFunctionState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -70,12 +70,12 @@ struct LocalTableFunctionState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/main/query_result.hpp b/src/include/duckdb/main/query_result.hpp index 1e4bede37cd5..2f800c4a7fda 100644 --- a/src/include/duckdb/main/query_result.hpp +++ b/src/include/duckdb/main/query_result.hpp @@ -95,7 +95,7 @@ class QueryResult : public BaseQueryResult { if (type != TARGET::TYPE) { throw InternalException("Failed to cast query result to type - query result type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -103,7 +103,7 @@ class QueryResult : public BaseQueryResult { if (type != TARGET::TYPE) { throw InternalException("Failed to cast query result to type - query result type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } public: diff --git a/src/include/duckdb/main/relation.hpp b/src/include/duckdb/main/relation.hpp index 7eaea4bd5f6f..486995fe4aa8 100644 --- a/src/include/duckdb/main/relation.hpp +++ b/src/include/duckdb/main/relation.hpp @@ -163,6 +163,18 @@ class Relation : public std::enable_shared_from_this { protected: DUCKDB_API string RenderWhitespace(idx_t depth); + +public: + template + TARGET &Cast() { + D_ASSERT(dynamic_cast(this)); + return reinterpret_cast(*this); + } + template + const TARGET &Cast() const { + D_ASSERT(dynamic_cast(this)); + return reinterpret_cast(*this); + } }; } // namespace duckdb diff --git a/src/include/duckdb/parser/base_expression.hpp b/src/include/duckdb/parser/base_expression.hpp index b60bb12ed656..08c3e1dbdc81 100644 --- a/src/include/duckdb/parser/base_expression.hpp +++ b/src/include/duckdb/parser/base_expression.hpp @@ -88,7 +88,7 @@ class BaseExpression { if (expression_class != TARGET::TYPE) { throw InternalException("Failed to cast expression to type - expression type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -96,7 +96,7 @@ class BaseExpression { if (expression_class != TARGET::TYPE) { throw InternalException("Failed to cast expression to type - expression type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/parser/constraint.hpp b/src/include/duckdb/parser/constraint.hpp index 77819d4a337d..78ae2a719c0d 100644 --- a/src/include/duckdb/parser/constraint.hpp +++ b/src/include/duckdb/parser/constraint.hpp @@ -74,7 +74,7 @@ class Constraint { if (type != TARGET::TYPE) { throw InternalException("Failed to cast constraint to type - constraint type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -82,7 +82,7 @@ class Constraint { if (type != TARGET::TYPE) { throw InternalException("Failed to cast constraint to type - constraint type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; } // namespace duckdb diff --git a/src/include/duckdb/parser/parsed_data/parse_info.hpp b/src/include/duckdb/parser/parsed_data/parse_info.hpp index 1f69bcfca49a..d1b26a59f33a 100644 --- a/src/include/duckdb/parser/parsed_data/parse_info.hpp +++ b/src/include/duckdb/parser/parsed_data/parse_info.hpp @@ -20,13 +20,13 @@ struct ParseInfo { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/parser/query_node.hpp b/src/include/duckdb/parser/query_node.hpp index 045eb991944c..2c5d42eaa49a 100644 --- a/src/include/duckdb/parser/query_node.hpp +++ b/src/include/duckdb/parser/query_node.hpp @@ -95,7 +95,7 @@ class QueryNode { if (type != TARGET::TYPE) { throw InternalException("Failed to cast query node to type - query node type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -103,7 +103,7 @@ class QueryNode { if (type != TARGET::TYPE) { throw InternalException("Failed to cast query node to type - query node type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/parser/result_modifier.hpp b/src/include/duckdb/parser/result_modifier.hpp index 8ecd7bfa2d28..9716455c2a69 100644 --- a/src/include/duckdb/parser/result_modifier.hpp +++ b/src/include/duckdb/parser/result_modifier.hpp @@ -61,7 +61,7 @@ class ResultModifier { if (type != TARGET::TYPE) { throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -69,7 +69,7 @@ class ResultModifier { if (type != TARGET::TYPE) { throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/parser/sql_statement.hpp b/src/include/duckdb/parser/sql_statement.hpp index 825fb1b1f326..d8ebc1a21cdf 100644 --- a/src/include/duckdb/parser/sql_statement.hpp +++ b/src/include/duckdb/parser/sql_statement.hpp @@ -18,6 +18,9 @@ namespace duckdb { //! SQLStatement is the base class of any type of SQL statement. class SQLStatement { +public: + static constexpr const StatementType TYPE = StatementType::INVALID_STATEMENT; + public: explicit SQLStatement(StatementType type) : type(type) { } @@ -52,18 +55,18 @@ class SQLStatement { public: template TARGET &Cast() { - if (type != TARGET::TYPE) { + if (type != TARGET::TYPE && TARGET::TYPE != StatementType::INVALID_STATEMENT) { throw InternalException("Failed to cast statement to type - statement type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { - if (type != TARGET::TYPE) { + if (type != TARGET::TYPE && TARGET::TYPE != StatementType::INVALID_STATEMENT) { throw InternalException("Failed to cast statement to type - statement type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; } // namespace duckdb diff --git a/src/include/duckdb/parser/tableref.hpp b/src/include/duckdb/parser/tableref.hpp index 147a1823e70b..91bbd548798c 100644 --- a/src/include/duckdb/parser/tableref.hpp +++ b/src/include/duckdb/parser/tableref.hpp @@ -64,7 +64,7 @@ class TableRef { if (type != TARGET::TYPE && TARGET::TYPE != TableReferenceType::INVALID) { throw InternalException("Failed to cast constraint to type - constraint type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -72,7 +72,7 @@ class TableRef { if (type != TARGET::TYPE && TARGET::TYPE != TableReferenceType::INVALID) { throw InternalException("Failed to cast constraint to type - constraint type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; } // namespace duckdb diff --git a/src/include/duckdb/planner/bound_constraint.hpp b/src/include/duckdb/planner/bound_constraint.hpp index 9ffa7061e7f1..dd0200f9b52a 100644 --- a/src/include/duckdb/planner/bound_constraint.hpp +++ b/src/include/duckdb/planner/bound_constraint.hpp @@ -37,7 +37,7 @@ class BoundConstraint { if (type != TARGET::TYPE) { throw InternalException("Failed to cast constraint to type - bound constraint type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -45,7 +45,7 @@ class BoundConstraint { if (type != TARGET::TYPE) { throw InternalException("Failed to cast constraint to type - bound constraint type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; } // namespace duckdb diff --git a/src/include/duckdb/planner/bound_query_node.hpp b/src/include/duckdb/planner/bound_query_node.hpp index 02a43217d832..cd5a78b6aa94 100644 --- a/src/include/duckdb/planner/bound_query_node.hpp +++ b/src/include/duckdb/planner/bound_query_node.hpp @@ -41,7 +41,7 @@ class BoundQueryNode { if (type != TARGET::TYPE) { throw InternalException("Failed to cast bound query node to type - query node type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -49,7 +49,7 @@ class BoundQueryNode { if (type != TARGET::TYPE) { throw InternalException("Failed to cast bound query node to type - query node type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/planner/bound_result_modifier.hpp b/src/include/duckdb/planner/bound_result_modifier.hpp index e92e4b20beaf..297f4139e721 100644 --- a/src/include/duckdb/planner/bound_result_modifier.hpp +++ b/src/include/duckdb/planner/bound_result_modifier.hpp @@ -30,7 +30,7 @@ class BoundResultModifier { if (type != TARGET::TYPE) { throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -38,7 +38,7 @@ class BoundResultModifier { if (type != TARGET::TYPE) { throw InternalException("Failed to cast result modifier to type - result modifier type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/planner/bound_tableref.hpp b/src/include/duckdb/planner/bound_tableref.hpp index d9993ea477dd..0a831c54aeed 100644 --- a/src/include/duckdb/planner/bound_tableref.hpp +++ b/src/include/duckdb/planner/bound_tableref.hpp @@ -32,7 +32,7 @@ class BoundTableRef { if (type != TARGET::TYPE) { throw InternalException("Failed to cast bound table ref to type - table ref type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -40,7 +40,7 @@ class BoundTableRef { if (type != TARGET::TYPE) { throw InternalException("Failed to cast bound table ref to type - table ref type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; } // namespace duckdb diff --git a/src/include/duckdb/planner/logical_operator.hpp b/src/include/duckdb/planner/logical_operator.hpp index 8c5223f5011b..91d005d9e10c 100644 --- a/src/include/duckdb/planner/logical_operator.hpp +++ b/src/include/duckdb/planner/logical_operator.hpp @@ -96,7 +96,7 @@ class LogicalOperator { if (TARGET::TYPE != LogicalOperatorType::LOGICAL_INVALID && type != TARGET::TYPE) { throw InternalException("Failed to cast logical operator to type - logical operator type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -104,7 +104,7 @@ class LogicalOperator { if (TARGET::TYPE != LogicalOperatorType::LOGICAL_INVALID && type != TARGET::TYPE) { throw InternalException("Failed to cast logical operator to type - logical operator type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; } // namespace duckdb diff --git a/src/include/duckdb/planner/table_binding.hpp b/src/include/duckdb/planner/table_binding.hpp index c04d731dea92..4d6679c1f7ac 100644 --- a/src/include/duckdb/planner/table_binding.hpp +++ b/src/include/duckdb/planner/table_binding.hpp @@ -62,7 +62,7 @@ struct Binding { if (binding_type != TARGET::TYPE) { throw InternalException("Failed to cast binding to type - binding type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -70,7 +70,7 @@ struct Binding { if (binding_type != TARGET::TYPE) { throw InternalException("Failed to cast binding to type - binding type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/planner/table_filter.hpp b/src/include/duckdb/planner/table_filter.hpp index 25ed82ea661b..dd3f7d7010a4 100644 --- a/src/include/duckdb/planner/table_filter.hpp +++ b/src/include/duckdb/planner/table_filter.hpp @@ -54,7 +54,7 @@ class TableFilter { if (filter_type != TARGET::TYPE) { throw InternalException("Failed to cast table to type - table filter type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -62,7 +62,7 @@ class TableFilter { if (filter_type != TARGET::TYPE) { throw InternalException("Failed to cast table to type - table filter type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/storage/index.hpp b/src/include/duckdb/storage/index.hpp index 376198a34ea7..cfca04e3c312 100644 --- a/src/include/duckdb/storage/index.hpp +++ b/src/include/duckdb/storage/index.hpp @@ -153,13 +153,13 @@ class Index { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/storage/partial_block_manager.hpp b/src/include/duckdb/storage/partial_block_manager.hpp index b0fa98ecb86e..2860566391d8 100644 --- a/src/include/duckdb/storage/partial_block_manager.hpp +++ b/src/include/duckdb/storage/partial_block_manager.hpp @@ -54,7 +54,7 @@ struct PartialBlock { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/storage/storage_manager.hpp b/src/include/duckdb/storage/storage_manager.hpp index 9bab3936ef5a..d09930d285d1 100644 --- a/src/include/duckdb/storage/storage_manager.hpp +++ b/src/include/duckdb/storage/storage_manager.hpp @@ -87,12 +87,12 @@ class StorageManager { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/storage/table/chunk_info.hpp b/src/include/duckdb/storage/table/chunk_info.hpp index fa4ee9737d91..9a593c22686b 100644 --- a/src/include/duckdb/storage/table/chunk_info.hpp +++ b/src/include/duckdb/storage/table/chunk_info.hpp @@ -51,7 +51,7 @@ class ChunkInfo { if (type != TARGET::TYPE) { throw InternalException("Failed to cast chunk info to type - query result type mismatch"); } - return (TARGET &)*this; + return reinterpret_cast(*this); } template @@ -59,7 +59,7 @@ class ChunkInfo { if (type != TARGET::TYPE) { throw InternalException("Failed to cast chunk info to type - query result type mismatch"); } - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/storage/table/column_checkpoint_state.hpp b/src/include/duckdb/storage/table/column_checkpoint_state.hpp index 5500e4b68e77..e547309a77ab 100644 --- a/src/include/duckdb/storage/table/column_checkpoint_state.hpp +++ b/src/include/duckdb/storage/table/column_checkpoint_state.hpp @@ -47,12 +47,12 @@ struct ColumnCheckpointState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/storage/table/scan_state.hpp b/src/include/duckdb/storage/table/scan_state.hpp index f69ff68df6ea..cefa13a27fe8 100644 --- a/src/include/duckdb/storage/table/scan_state.hpp +++ b/src/include/duckdb/storage/table/scan_state.hpp @@ -39,12 +39,12 @@ struct SegmentScanState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; @@ -55,12 +55,12 @@ struct IndexScanState { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/include/duckdb/transaction/transaction.hpp b/src/include/duckdb/transaction/transaction.hpp index 4bef6d73a8e1..0caa342afb11 100644 --- a/src/include/duckdb/transaction/transaction.hpp +++ b/src/include/duckdb/transaction/transaction.hpp @@ -62,12 +62,12 @@ class Transaction { template TARGET &Cast() { D_ASSERT(dynamic_cast(this)); - return (TARGET &)*this; + return reinterpret_cast(*this); } template const TARGET &Cast() const { D_ASSERT(dynamic_cast(this)); - return (const TARGET &)*this; + return reinterpret_cast(*this); } }; diff --git a/src/main/relation.cpp b/src/main/relation.cpp index a2a367af33fd..6fda202fa8ec 100644 --- a/src/main/relation.cpp +++ b/src/main/relation.cpp @@ -209,7 +209,7 @@ unique_ptr Relation::ExecuteOrThrow() { BoundStatement Relation::Bind(Binder &binder) { SelectStatement stmt; stmt.node = GetQueryNode(); - return binder.Bind((SQLStatement &)stmt); + return binder.Bind(stmt.Cast()); } shared_ptr Relation::InsertRel(const string &schema_name, const string &table_name) { diff --git a/src/main/relation/create_table_relation.cpp b/src/main/relation/create_table_relation.cpp index f5411db9e78d..e8ac45ede6f8 100644 --- a/src/main/relation/create_table_relation.cpp +++ b/src/main/relation/create_table_relation.cpp @@ -23,7 +23,7 @@ BoundStatement CreateTableRelation::Bind(Binder &binder) { info->query = std::move(select); info->on_conflict = OnCreateConflict::ERROR_ON_CONFLICT; stmt.info = std::move(info); - return binder.Bind((SQLStatement &)stmt); + return binder.Bind(stmt.Cast()); } const vector &CreateTableRelation::Columns() { diff --git a/src/main/relation/create_view_relation.cpp b/src/main/relation/create_view_relation.cpp index cd8cf828dde9..b9c80c074511 100644 --- a/src/main/relation/create_view_relation.cpp +++ b/src/main/relation/create_view_relation.cpp @@ -32,7 +32,7 @@ BoundStatement CreateViewRelation::Bind(Binder &binder) { info->schema = schema_name; info->on_conflict = replace ? OnCreateConflict::REPLACE_ON_CONFLICT : OnCreateConflict::ERROR_ON_CONFLICT; stmt.info = std::move(info); - return binder.Bind((SQLStatement &)stmt); + return binder.Bind(stmt.Cast()); } const vector &CreateViewRelation::Columns() { diff --git a/src/main/relation/delete_relation.cpp b/src/main/relation/delete_relation.cpp index b2ae9f1895ad..8afc8226e8a2 100644 --- a/src/main/relation/delete_relation.cpp +++ b/src/main/relation/delete_relation.cpp @@ -21,7 +21,7 @@ BoundStatement DeleteRelation::Bind(Binder &binder) { DeleteStatement stmt; stmt.condition = condition ? condition->Copy() : nullptr; stmt.table = std::move(basetable); - return binder.Bind((SQLStatement &)stmt); + return binder.Bind(stmt.Cast()); } const vector &DeleteRelation::Columns() { diff --git a/src/main/relation/explain_relation.cpp b/src/main/relation/explain_relation.cpp index b87a61571505..0a3f81257e38 100644 --- a/src/main/relation/explain_relation.cpp +++ b/src/main/relation/explain_relation.cpp @@ -16,7 +16,7 @@ BoundStatement ExplainRelation::Bind(Binder &binder) { auto select = make_uniq(); select->node = child->GetQueryNode(); ExplainStatement explain(std::move(select), type); - return binder.Bind((SQLStatement &)explain); + return binder.Bind(explain.Cast()); } const vector &ExplainRelation::Columns() { diff --git a/src/main/relation/insert_relation.cpp b/src/main/relation/insert_relation.cpp index 202a499b5f29..c6738d486019 100644 --- a/src/main/relation/insert_relation.cpp +++ b/src/main/relation/insert_relation.cpp @@ -21,7 +21,7 @@ BoundStatement InsertRelation::Bind(Binder &binder) { stmt.schema = schema_name; stmt.table = table_name; stmt.select_statement = std::move(select); - return binder.Bind((SQLStatement &)stmt); + return binder.Bind(stmt.Cast()); } const vector &InsertRelation::Columns() { diff --git a/src/main/relation/update_relation.cpp b/src/main/relation/update_relation.cpp index 9554a9cb9281..152d04af3933 100644 --- a/src/main/relation/update_relation.cpp +++ b/src/main/relation/update_relation.cpp @@ -30,7 +30,7 @@ BoundStatement UpdateRelation::Bind(Binder &binder) { for (auto &expr : expressions) { stmt.set_info->expressions.push_back(expr->Copy()); } - return binder.Bind((SQLStatement &)stmt); + return binder.Bind(stmt.Cast()); } const vector &UpdateRelation::Columns() { diff --git a/src/main/relation/write_csv_relation.cpp b/src/main/relation/write_csv_relation.cpp index fb1695d89bb8..016ea0186bd9 100644 --- a/src/main/relation/write_csv_relation.cpp +++ b/src/main/relation/write_csv_relation.cpp @@ -22,7 +22,7 @@ BoundStatement WriteCSVRelation::Bind(Binder &binder) { info->format = "csv"; info->options = options; copy.info = std::move(info); - return binder.Bind((SQLStatement &)copy); + return binder.Bind(copy.Cast()); } const vector &WriteCSVRelation::Columns() { diff --git a/src/main/relation/write_parquet_relation.cpp b/src/main/relation/write_parquet_relation.cpp index 9032e6f3c6ba..6cedc29cb384 100644 --- a/src/main/relation/write_parquet_relation.cpp +++ b/src/main/relation/write_parquet_relation.cpp @@ -22,7 +22,7 @@ BoundStatement WriteParquetRelation::Bind(Binder &binder) { info->format = "parquet"; info->options = options; copy.info = std::move(info); - return binder.Bind((SQLStatement &)copy); + return binder.Bind(copy.Cast()); } const vector &WriteParquetRelation::Columns() { diff --git a/src/transaction/duck_transaction.cpp b/src/transaction/duck_transaction.cpp index a26fd5f1225f..f1451b3d780b 100644 --- a/src/transaction/duck_transaction.cpp +++ b/src/transaction/duck_transaction.cpp @@ -44,7 +44,7 @@ DuckTransaction &DuckTransaction::Get(ClientContext &context, Catalog &catalog) if (!transaction.IsDuckTransaction()) { throw InternalException("DuckTransaction::Get called on non-DuckDB transaction"); } - return (DuckTransaction &)transaction; + return transaction.Cast(); } LocalStorage &DuckTransaction::GetLocalStorage() { diff --git a/tools/pythonpkg/src/include/duckdb_python/numpy/array_wrapper.hpp b/tools/pythonpkg/src/include/duckdb_python/numpy/array_wrapper.hpp index 966800bd4390..8de4f2babc19 100644 --- a/tools/pythonpkg/src/include/duckdb_python/numpy/array_wrapper.hpp +++ b/tools/pythonpkg/src/include/duckdb_python/numpy/array_wrapper.hpp @@ -52,7 +52,7 @@ struct ArrayWrapper { class NumpyResultConversion { public: - NumpyResultConversion(vector &types, idx_t initial_capacity); + NumpyResultConversion(const vector &types, idx_t initial_capacity); void Append(DataChunk &chunk); diff --git a/tools/pythonpkg/src/map.cpp b/tools/pythonpkg/src/map.cpp index 0aebac4cc138..d9ec300135e4 100644 --- a/tools/pythonpkg/src/map.cpp +++ b/tools/pythonpkg/src/map.cpp @@ -25,7 +25,7 @@ struct MapFunctionData : public TableFunctionData { vector in_names, out_names; }; -static py::handle FunctionCall(NumpyResultConversion &conversion, vector &names, PyObject *function) { +static py::handle FunctionCall(NumpyResultConversion &conversion, const vector &names, PyObject *function) { py::dict in_numpy_dict; for (idx_t col_idx = 0; col_idx < names.size(); col_idx++) { in_numpy_dict[names[col_idx].c_str()] = conversion.ToArray(col_idx); @@ -131,8 +131,8 @@ unique_ptr MapFunction::MapFunctionBind(ClientContext &context, Ta auto data_uptr = make_uniq(); auto &data = *data_uptr; - data.function = (PyObject *)input.inputs[0].GetPointer(); - auto explicit_schema = (PyObject *)input.inputs[1].GetPointer(); + data.function = reinterpret_cast(input.inputs[0].GetPointer()); + auto explicit_schema = reinterpret_cast(input.inputs[1].GetPointer()); data.in_names = input.input_table_names; data.in_types = input.input_table_types; @@ -154,7 +154,7 @@ unique_ptr MapFunction::MapFunctionBind(ClientContext &context, Ta return std::move(data_uptr); } -static string TypeVectorToString(vector &types) { +static string TypeVectorToString(const vector &types) { return StringUtil::Join(types, types.size(), ", ", [](const LogicalType &argument) { return argument.ToString(); }); } @@ -166,7 +166,7 @@ OperatorResultType MapFunction::MapFunctionExec(ExecutionContext &context, Table return OperatorResultType::NEED_MORE_INPUT; } - auto &data = (MapFunctionData &)*data_p.bind_data; + auto &data = data_p.bind_data->Cast(); D_ASSERT(input.GetTypes() == data.in_types); NumpyResultConversion conversion(data.in_types, input.size()); diff --git a/tools/pythonpkg/src/native/python_objects.cpp b/tools/pythonpkg/src/native/python_objects.cpp index 9441cc861a1c..fb088b39d87a 100644 --- a/tools/pythonpkg/src/native/python_objects.cpp +++ b/tools/pythonpkg/src/native/python_objects.cpp @@ -275,7 +275,7 @@ Value PyDate::ToDuckValue() { } void PythonObject::Initialize() { - PyDateTime_IMPORT; // Python datetime initialize #2 + PyDateTime_IMPORT; // NOLINT: Python datetime initialize #2 } py::object PythonObject::FromValue(const Value &val, const LogicalType &type) { @@ -303,8 +303,7 @@ py::object PythonObject::FromValue(const Value &val, const LogicalType &type) { case LogicalTypeId::UBIGINT: return py::cast(val.GetValue()); case LogicalTypeId::HUGEINT: - return py::reinterpret_steal( - PyLong_FromString((char *)val.GetValue().c_str(), nullptr, 10)); + return py::reinterpret_steal(PyLong_FromString(val.GetValue().c_str(), nullptr, 10)); case LogicalTypeId::FLOAT: return py::cast(val.GetValue()); case LogicalTypeId::DOUBLE: diff --git a/tools/pythonpkg/src/numpy/array_wrapper.cpp b/tools/pythonpkg/src/numpy/array_wrapper.cpp index 8f7d363aedee..c9611cc57d41 100644 --- a/tools/pythonpkg/src/numpy/array_wrapper.cpp +++ b/tools/pythonpkg/src/numpy/array_wrapper.cpp @@ -192,7 +192,7 @@ struct StringConvert { for (idx_t i = 0; i < len; i++) { if (data[i] > 127) { // there are! fallback to slower case - return ConvertUnicodeValue((const char *)data, len, i); + return ConvertUnicodeValue(const_char_ptr_cast(data), len, i); } } // no unicode: fast path @@ -323,7 +323,7 @@ template static bool ConvertColumn(idx_t target_offset, data_ptr_t target_data, bool *target_mask, UnifiedVectorFormat &idata, idx_t count) { auto src_ptr = UnifiedVectorFormat::GetData(idata); - auto out_ptr = (NUMPY_T *)target_data; + auto out_ptr = reinterpret_cast(target_data); if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { idx_t src_idx = idata.sel->get_index(i); @@ -352,7 +352,7 @@ template static bool ConvertColumnCategoricalTemplate(idx_t target_offset, data_ptr_t target_data, UnifiedVectorFormat &idata, idx_t count) { auto src_ptr = UnifiedVectorFormat::GetData(idata); - auto out_ptr = (NUMPY_T *)target_data; + auto out_ptr = reinterpret_cast(target_data); if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { idx_t src_idx = idata.sel->get_index(i); @@ -379,7 +379,7 @@ static bool ConvertColumnCategoricalTemplate(idx_t target_offset, data_ptr_t tar template static bool ConvertNested(idx_t target_offset, data_ptr_t target_data, bool *target_mask, Vector &input, UnifiedVectorFormat &idata, idx_t count) { - auto out_ptr = (NUMPY_T *)target_data; + auto out_ptr = reinterpret_cast(target_data); if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { idx_t src_idx = idata.sel->get_index(i); @@ -428,7 +428,7 @@ template static bool ConvertDecimalInternal(idx_t target_offset, data_ptr_t target_data, bool *target_mask, UnifiedVectorFormat &idata, idx_t count, double division) { auto src_ptr = UnifiedVectorFormat::GetData(idata); - auto out_ptr = (double *)target_data; + auto out_ptr = reinterpret_cast(target_data); if (!idata.validity.AllValid()) { for (idx_t i = 0; i < count; i++) { idx_t src_idx = idata.sel->get_index(i); @@ -627,7 +627,7 @@ void ArrayWrapper::Resize(idx_t new_capacity) { void ArrayWrapper::Append(idx_t current_offset, Vector &input, idx_t count) { auto dataptr = data->data; - auto maskptr = (bool *)mask->data; + auto maskptr = reinterpret_cast(mask->data); D_ASSERT(dataptr); D_ASSERT(maskptr); D_ASSERT(input.GetType() == data->type); @@ -774,7 +774,7 @@ py::object ArrayWrapper::ToArray(idx_t count) const { return masked_array; } -NumpyResultConversion::NumpyResultConversion(vector &types, idx_t initial_capacity) +NumpyResultConversion::NumpyResultConversion(const vector &types, idx_t initial_capacity) : count(0), capacity(0) { owned_data.reserve(types.size()); for (auto &type : types) { diff --git a/tools/pythonpkg/src/numpy/numpy_scan.cpp b/tools/pythonpkg/src/numpy/numpy_scan.cpp index 8c0e9362f548..cdff2ce4be39 100644 --- a/tools/pythonpkg/src/numpy/numpy_scan.cpp +++ b/tools/pythonpkg/src/numpy/numpy_scan.cpp @@ -62,11 +62,11 @@ void ScanNumpyCategory(py::array &column, idx_t count, idx_t offset, Vector &out template void ScanNumpyMasked(PandasColumnBindData &bind_data, idx_t count, idx_t offset, Vector &out) { D_ASSERT(bind_data.pandas_col->Backend() == PandasColumnBackend::NUMPY); - auto &numpy_col = (PandasNumpyColumn &)*bind_data.pandas_col; + auto &numpy_col = reinterpret_cast(*bind_data.pandas_col); ScanNumpyColumn(numpy_col.array, numpy_col.stride, offset, out, count); auto &result_mask = FlatVector::Validity(out); if (bind_data.mask) { - auto mask = (bool *)bind_data.mask->numpy_array.data(); + auto mask = reinterpret_cast(bind_data.mask->numpy_array.data()); for (idx_t i = 0; i < count; i++) { auto is_null = mask[offset + i]; if (is_null) { @@ -77,10 +77,10 @@ void ScanNumpyMasked(PandasColumnBindData &bind_data, idx_t count, idx_t offset, } template -void ScanNumpyFpColumn(T *src_ptr, idx_t stride, idx_t count, idx_t offset, Vector &out) { +void ScanNumpyFpColumn(const T *src_ptr, idx_t stride, idx_t count, idx_t offset, Vector &out) { auto &mask = FlatVector::Validity(out); if (stride == sizeof(T)) { - FlatVector::SetData(out, data_ptr_cast(src_ptr + offset)); + FlatVector::SetData(out, (data_ptr_t)(src_ptr + offset)); // NOLINT // Turn NaN values into NULL auto tgt_ptr = FlatVector::GetData(out); for (idx_t i = 0; i < count; i++) { @@ -190,7 +190,7 @@ void NumpyScan::ScanObjectColumn(PyObject **col, idx_t count, idx_t offset, Vect //! 'count' is the amount of values we will convert in this batch void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, Vector &out) { D_ASSERT(bind_data.pandas_col->Backend() == PandasColumnBackend::NUMPY); - auto &numpy_col = (PandasNumpyColumn &)*bind_data.pandas_col; + auto &numpy_col = reinterpret_cast(*bind_data.pandas_col); auto &array = numpy_col.array; switch (bind_data.numpy_type) { @@ -222,10 +222,10 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, ScanNumpyMasked(bind_data, count, offset, out); break; case NumpyNullableType::FLOAT_32: - ScanNumpyFpColumn((float *)array.data(), numpy_col.stride, count, offset, out); + ScanNumpyFpColumn(reinterpret_cast(array.data()), numpy_col.stride, count, offset, out); break; case NumpyNullableType::FLOAT_64: - ScanNumpyFpColumn((double *)array.data(), numpy_col.stride, count, offset, out); + ScanNumpyFpColumn(reinterpret_cast(array.data()), numpy_col.stride, count, offset, out); break; case NumpyNullableType::DATETIME: case NumpyNullableType::DATETIME_TZ: { @@ -245,7 +245,7 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, break; } case NumpyNullableType::TIMEDELTA: { - auto src_ptr = (int64_t *)array.data(); + auto src_ptr = reinterpret_cast(array.data()); auto tgt_ptr = FlatVector::GetData(out); auto &mask = FlatVector::Validity(out); @@ -272,7 +272,7 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, case NumpyNullableType::OBJECT: { //! We have determined the underlying logical type of this object column // Get the source pointer of the numpy array - auto src_ptr = (PyObject **)array.data(); + auto src_ptr = (PyObject **)array.data(); // NOLINT if (out.GetType().id() != LogicalTypeId::VARCHAR) { return NumpyScan::ScanObjectColumn(src_ptr, count, offset, out); } @@ -298,7 +298,7 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, if (import_cache.pandas().libs.NAType.IsLoaded()) { // If pandas is imported, check if the type is NAType auto val_type = Py_TYPE(val); - auto na_type = (PyTypeObject *)import_cache.pandas().libs.NAType().ptr(); + auto na_type = reinterpret_cast(import_cache.pandas().libs.NAType().ptr()); if (val_type == na_type) { out_mask.SetInvalid(row); continue; @@ -329,16 +329,16 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, } if (PyUnicode_IS_COMPACT_ASCII(val)) { // ascii string: we can zero copy - tgt_ptr[row] = string_t((const char *)PyUnicode_DATA(val), PyUnicode_GET_LENGTH(val)); + tgt_ptr[row] = string_t(const_char_ptr_cast(PyUnicode_DATA(val)), PyUnicode_GET_LENGTH(val)); } else { // unicode gunk - auto ascii_obj = (PyASCIIObject *)val; - auto unicode_obj = (PyCompactUnicodeObject *)val; + auto ascii_obj = reinterpret_cast(val); + auto unicode_obj = reinterpret_cast(val); // compact unicode string: is there utf8 data available? if (unicode_obj->utf8) { // there is! zero copy - tgt_ptr[row] = string_t((const char *)unicode_obj->utf8, unicode_obj->utf8_length); - } else if (PyUnicode_IS_COMPACT(unicode_obj) && !PyUnicode_IS_ASCII(unicode_obj)) { + tgt_ptr[row] = string_t(const_char_ptr_cast(unicode_obj->utf8), unicode_obj->utf8_length); + } else if (PyUnicode_IS_COMPACT(unicode_obj) && !PyUnicode_IS_ASCII(unicode_obj)) { // NOLINT auto kind = PyUnicode_KIND(val); switch (kind) { case PyUnicode_1BYTE_KIND: diff --git a/tools/pythonpkg/src/pyconnection.cpp b/tools/pythonpkg/src/pyconnection.cpp index ee67e72179ca..827d6a72feb5 100644 --- a/tools/pythonpkg/src/pyconnection.cpp +++ b/tools/pythonpkg/src/pyconnection.cpp @@ -357,7 +357,7 @@ void DuckDBPyConnection::Initialize(py::handle &m) { .def("__exit__", &DuckDBPyConnection::Exit, py::arg("exc_type"), py::arg("exc"), py::arg("traceback")); InitializeConnectionMethods(connection_module); - PyDateTime_IMPORT; + PyDateTime_IMPORT; // NOLINT DuckDBPyConnection::ImportCache(); } @@ -543,9 +543,9 @@ void DuckDBPyConnection::RegisterArrowObject(const py::object &arrow_object, con py::gil_scoped_release release; temporary_views[name] = connection - ->TableFunction("arrow_scan", {Value::POINTER((uintptr_t)stream_factory.get()), - Value::POINTER((uintptr_t)stream_factory_produce), - Value::POINTER((uintptr_t)stream_factory_get_schema)}) + ->TableFunction("arrow_scan", {Value::POINTER(CastPointerToValue(stream_factory.get())), + Value::POINTER(CastPointerToValue(stream_factory_produce)), + Value::POINTER(CastPointerToValue(stream_factory_get_schema))}) ->CreateView(name, true, true); } vector> dependencies; @@ -569,7 +569,7 @@ shared_ptr DuckDBPyConnection::RegisterPythonObject(const st { py::gil_scoped_release release; temporary_views[name] = - connection->TableFunction("pandas_scan", {Value::POINTER((uintptr_t)new_df.ptr())}) + connection->TableFunction("pandas_scan", {Value::POINTER(CastPointerToValue(new_df.ptr()))}) ->CreateView(name, true, true); } @@ -736,7 +736,7 @@ unique_ptr DuckDBPyConnection::ReadCSV( } auto read_csv_p = connection->ReadCSV(name, options); - auto &read_csv = (ReadCSVRelation &)*read_csv_p; + auto &read_csv = read_csv_p->Cast(); if (file_like_object_wrapper) { D_ASSERT(!read_csv.extra_dependencies); read_csv.extra_dependencies = std::move(file_like_object_wrapper); @@ -1098,9 +1098,9 @@ unique_ptr DuckDBPyConnection::FromArrow(py::object &arrow_obj auto stream_factory_get_schema = PythonTableArrowArrayStreamFactory::GetSchema; auto rel = connection - ->TableFunction("arrow_scan", {Value::POINTER((uintptr_t)stream_factory.get()), - Value::POINTER((uintptr_t)stream_factory_produce), - Value::POINTER((uintptr_t)stream_factory_get_schema)}) + ->TableFunction("arrow_scan", {Value::POINTER(CastPointerToValue(stream_factory.get())), + Value::POINTER(CastPointerToValue(stream_factory_produce)), + Value::POINTER(CastPointerToValue(stream_factory_get_schema))}) ->Alias(name); rel->extra_dependencies = make_uniq(make_uniq(std::move(stream_factory), arrow_object)); @@ -1330,7 +1330,7 @@ static unique_ptr TryReplacement(py::dict &dict, py::str &table_name, } else { string name = "df_" + StringUtil::GenerateRandomName(); auto new_df = PandasScanFunction::PandasReplaceCopiedNames(entry); - children.push_back(make_uniq(Value::POINTER((uintptr_t)new_df.ptr()))); + children.push_back(make_uniq(Value::POINTER(CastPointerToValue(new_df.ptr())))); table_function->function = make_uniq("pandas_scan", std::move(children)); table_function->external_dependency = make_uniq(make_uniq(entry), make_uniq(new_df)); @@ -1382,7 +1382,7 @@ static unique_ptr TryReplacement(py::dict &dict, py::str &table_name, throw NotImplementedException("Unsupported Numpy object"); break; } - children.push_back(make_uniq(Value::POINTER((uintptr_t)data.ptr()))); + children.push_back(make_uniq(Value::POINTER(CastPointerToValue(data.ptr())))); table_function->function = make_uniq("pandas_scan", std::move(children)); table_function->external_dependency = make_uniq(make_uniq(entry), make_uniq(data)); @@ -1571,7 +1571,7 @@ ModifiedMemoryFileSystem &DuckDBPyConnection::GetObjectFileSystem() { "This operation could not be completed because required module 'fsspec' is not installed"); } internal_object_filesystem = make_shared(modified_memory_fs()); - auto &abstract_fs = (AbstractFileSystem &)*internal_object_filesystem; + auto &abstract_fs = reinterpret_cast(*internal_object_filesystem); RegisterFilesystem(abstract_fs); } return *internal_object_filesystem; diff --git a/tools/pythonpkg/src/pyrelation.cpp b/tools/pythonpkg/src/pyrelation.cpp index ecdb772f9312..45fd21ccac6d 100644 --- a/tools/pythonpkg/src/pyrelation.cpp +++ b/tools/pythonpkg/src/pyrelation.cpp @@ -847,8 +847,8 @@ void DuckDBPyRelation::Create(const string &table) { unique_ptr DuckDBPyRelation::Map(py::function fun, Optional schema) { AssertRelation(); vector params; - params.emplace_back(Value::POINTER((uintptr_t)fun.ptr())); - params.emplace_back(Value::POINTER((uintptr_t)schema.ptr())); + params.emplace_back(Value::POINTER(CastPointerToValue(fun.ptr()))); + params.emplace_back(Value::POINTER(CastPointerToValue(schema.ptr()))); auto relation = make_uniq(rel->TableFunction("python_map_function", params)); auto rel_dependency = make_uniq(); rel_dependency->map_function = std::move(fun); @@ -882,7 +882,7 @@ string DuckDBPyRelation::Explain(ExplainType type) { py::gil_scoped_release release; auto res = rel->Explain(type); D_ASSERT(res->type == duckdb::QueryResultType::MATERIALIZED_RESULT); - auto &materialized = (duckdb::MaterializedQueryResult &)*res; + auto &materialized = res->Cast(); auto &coll = materialized.Collection(); string result; for (auto &row : coll.Rows()) { diff --git a/tools/pythonpkg/src/pyresult.cpp b/tools/pythonpkg/src/pyresult.cpp index 1053ca4605d6..b472a8a36199 100644 --- a/tools/pythonpkg/src/pyresult.cpp +++ b/tools/pythonpkg/src/pyresult.cpp @@ -47,7 +47,7 @@ unique_ptr DuckDBPyResult::FetchChunk() { } unique_ptr DuckDBPyResult::FetchNext(QueryResult &result) { - if (!result_closed && result.type == QueryResultType::STREAM_RESULT && !((StreamQueryResult &)result).IsOpen()) { + if (!result_closed && result.type == QueryResultType::STREAM_RESULT && !result.Cast().IsOpen()) { result_closed = true; return nullptr; } @@ -59,7 +59,7 @@ unique_ptr DuckDBPyResult::FetchNext(QueryResult &result) { } unique_ptr DuckDBPyResult::FetchNextRaw(QueryResult &result) { - if (!result_closed && result.type == QueryResultType::STREAM_RESULT && !((StreamQueryResult &)result).IsOpen()) { + if (!result_closed && result.type == QueryResultType::STREAM_RESULT && !result.Cast().IsOpen()) { result_closed = true; return nullptr; } @@ -169,13 +169,13 @@ py::dict DuckDBPyResult::FetchNumpyInternal(bool stream, idx_t vectors_per_chunk idx_t initial_capacity = STANDARD_VECTOR_SIZE * 2ULL; if (result->type == QueryResultType::MATERIALIZED_RESULT) { // materialized query result: we know exactly how much space we need - auto &materialized = (MaterializedQueryResult &)*result; + auto &materialized = result->Cast(); initial_capacity = materialized.RowCount(); } NumpyResultConversion conversion(result->types, initial_capacity); if (result->type == QueryResultType::MATERIALIZED_RESULT) { - auto &materialized = (MaterializedQueryResult &)*result; + auto &materialized = result->Cast(); for (auto &chunk : materialized.Collection().Chunks()) { conversion.Append(chunk); } @@ -186,22 +186,22 @@ py::dict DuckDBPyResult::FetchNumpyInternal(bool stream, idx_t vectors_per_chunk if (!stream) { vectors_per_chunk = NumericLimits::Maximum(); } - auto stream_result = (StreamQueryResult *)result.get(); + auto &stream_result = result->Cast(); for (idx_t count_vec = 0; count_vec < vectors_per_chunk; count_vec++) { - if (!stream_result->IsOpen()) { + if (!stream_result.IsOpen()) { break; } unique_ptr chunk; { py::gil_scoped_release release; - chunk = FetchNextRaw(*stream_result); + chunk = FetchNextRaw(stream_result); } if (!chunk || chunk->size() == 0) { //! finished break; } conversion.Append(*chunk); - InsertCategory(*stream_result, categories); + InsertCategory(stream_result, categories); } } From bfe1fbdedc261f0a83ac465e666ed7c55c845269 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 16:34:17 +0200 Subject: [PATCH 25/28] Python - move unicode methods into separate functions --- .../include/duckdb_python/python_objects.hpp | 38 +++--- .../src/include/duckdb_python/pyutil.hpp | 58 +++++++++ .../src/native/python_conversion.cpp | 6 +- tools/pythonpkg/src/native/python_objects.cpp | 118 +++++++++++++++--- tools/pythonpkg/src/numpy/array_wrapper.cpp | 12 +- tools/pythonpkg/src/numpy/numpy_scan.cpp | 27 ++-- 6 files changed, 203 insertions(+), 56 deletions(-) create mode 100644 tools/pythonpkg/src/include/duckdb_python/pyutil.hpp diff --git a/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp b/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp index 6557375202a6..ee850b83e9ba 100644 --- a/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp +++ b/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp @@ -1,6 +1,7 @@ #pragma once #include "duckdb_python/pybind11/pybind_wrapper.hpp" +#include "duckdb_python/pyutil.hpp" #include "duckdb/common/types/time.hpp" #include "duckdb/common/types/date.hpp" #include "duckdb/common/types/timestamp.hpp" @@ -12,21 +13,6 @@ namespace duckdb { -/* Backport for Python < 3.10 */ -#if PY_VERSION_HEX < 0x030a00a1 -#ifndef PyDateTime_TIME_GET_TZINFO -#define PyDateTime_TIME_GET_TZINFO(o) ((((PyDateTime_Time *)o)->hastzinfo) ? ((PyDateTime_Time *)o)->tzinfo : Py_None) -#endif -#ifndef PyDateTime_DATE_GET_TZINFO -#define PyDateTime_DATE_GET_TZINFO(o) \ - ((((PyDateTime_DateTime *)o)->hastzinfo) ? ((PyDateTime_DateTime *)o)->tzinfo : Py_None) -#endif -#endif - -#define PyDateTime_TIMEDELTA_GET_DAYS(o) (((PyDateTime_Delta *)(o))->days) -#define PyDateTime_TIMEDELTA_GET_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) -#define PyDateTime_TIMEDELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) - struct PyDictionary { public: PyDictionary(py::object dict); @@ -114,6 +100,11 @@ struct PyTimeDelta { public: interval_t ToInterval(); + +private: + static int64_t GetDays(PyObject *obj); + static int64_t GetSeconds(PyObject *obj); + static int64_t GetMicros(PyObject *obj); }; struct PyTime { @@ -129,6 +120,13 @@ struct PyTime { public: dtime_t ToDuckTime(); Value ToDuckValue(); + +private: + static int32_t GetHours(PyObject *obj); + static int32_t GetMinutes(PyObject *obj); + static int32_t GetSeconds(PyObject *obj); + static int32_t GetMicros(PyObject *obj); + static PyObject *GetTZInfo(PyObject *obj); }; struct PyDateTime { @@ -149,6 +147,16 @@ struct PyDateTime { date_t ToDate(); dtime_t ToDuckTime(); Value ToDuckValue(); + +public: + static int32_t GetYears(PyObject *obj); + static int32_t GetMonths(PyObject *obj); + static int32_t GetDays(PyObject *obj); + static int32_t GetHours(PyObject *obj); + static int32_t GetMinutes(PyObject *obj); + static int32_t GetSeconds(PyObject *obj); + static int32_t GetMicros(PyObject *obj); + static PyObject *GetTZInfo(PyObject *obj); }; struct PyDate { diff --git a/tools/pythonpkg/src/include/duckdb_python/pyutil.hpp b/tools/pythonpkg/src/include/duckdb_python/pyutil.hpp new file mode 100644 index 000000000000..38487020bf56 --- /dev/null +++ b/tools/pythonpkg/src/include/duckdb_python/pyutil.hpp @@ -0,0 +1,58 @@ +#pragma once + +#include "duckdb_python/pybind11/pybind_wrapper.hpp" +#include "duckdb/common/types.hpp" + +namespace duckdb { + +struct PyUtil { + static idx_t PyByteArrayGetSize(PyObject *obj) { + return PyByteArray_GET_SIZE(obj); // NOLINT + } + + static Py_buffer *PyMemoryViewGetBuffer(PyObject *obj) { + return PyMemoryView_GET_BUFFER(obj); + } + + static bool PyUnicodeIsCompactASCII(PyObject *obj) { + return PyUnicode_IS_COMPACT_ASCII(obj); + } + + static const char *PyUnicodeData(PyObject *obj) { + return const_char_ptr_cast(PyUnicode_DATA(obj)); + } + + static char *PyUnicodeDataMutable(PyObject *obj) { + return char_ptr_cast(PyUnicode_DATA(obj)); + } + + static idx_t PyUnicodeGetLength(PyObject *obj) { + return PyUnicode_GET_LENGTH(obj); + } + + static bool PyUnicodeIsCompact(PyCompactUnicodeObject *obj) { + return PyUnicode_IS_COMPACT(obj); + } + + static bool PyUnicodeIsASCII(PyCompactUnicodeObject *obj) { + return PyUnicode_IS_ASCII(obj); + } + + static int PyUnicodeKind(PyObject *obj) { + return PyUnicode_KIND(obj); + } + + static Py_UCS1 *PyUnicode1ByteData(PyObject *obj) { + return PyUnicode_1BYTE_DATA(obj); + } + + static Py_UCS2 *PyUnicode2ByteData(PyObject *obj) { + return PyUnicode_2BYTE_DATA(obj); + } + + static Py_UCS4 *PyUnicode4ByteData(PyObject *obj) { + return PyUnicode_4BYTE_DATA(obj); + } +}; + +} // namespace duckdb diff --git a/tools/pythonpkg/src/native/python_conversion.cpp b/tools/pythonpkg/src/native/python_conversion.cpp index e679fe311ae5..d2169e46c6bc 100644 --- a/tools/pythonpkg/src/native/python_conversion.cpp +++ b/tools/pythonpkg/src/native/python_conversion.cpp @@ -359,14 +359,14 @@ Value TransformPythonValue(py::handle ele, const LogicalType &target_type, bool return ele.cast(); case PythonObjectType::ByteArray: { auto byte_array = ele.ptr(); - const_data_ptr_t bytes = const_data_ptr_cast(PyByteArray_AsString(byte_array)); - idx_t byte_length = PyByteArray_GET_SIZE(byte_array); + const_data_ptr_t bytes = const_data_ptr_cast(PyByteArray_AsString(byte_array)); // NOLINT + idx_t byte_length = PyUtil::PyByteArrayGetSize(byte_array); // NOLINT return Value::BLOB(bytes, byte_length); } case PythonObjectType::MemoryView: { py::memoryview py_view = ele.cast(); PyObject *py_view_ptr = py_view.ptr(); - Py_buffer *py_buf = PyMemoryView_GET_BUFFER(py_view_ptr); + Py_buffer *py_buf = PyUtil::PyMemoryViewGetBuffer(py_view_ptr); // NOLINT return Value::BLOB(const_data_ptr_t(py_buf->buf), idx_t(py_buf->len)); } case PythonObjectType::Bytes: { diff --git a/tools/pythonpkg/src/native/python_objects.cpp b/tools/pythonpkg/src/native/python_objects.cpp index fb088b39d87a..ba4d03cc07c4 100644 --- a/tools/pythonpkg/src/native/python_objects.cpp +++ b/tools/pythonpkg/src/native/python_objects.cpp @@ -10,6 +10,21 @@ #include "datetime.h" // Python datetime initialize #1 +/* Backport for Python < 3.10 */ +#if PY_VERSION_HEX < 0x030a00a1 +#ifndef PyDateTime_TIME_GET_TZINFO +#define PyDateTime_TIME_GET_TZINFO(o) ((((PyDateTime_Time *)o)->hastzinfo) ? ((PyDateTime_Time *)o)->tzinfo : Py_None) +#endif +#ifndef PyDateTime_DATE_GET_TZINFO +#define PyDateTime_DATE_GET_TZINFO(o) \ + ((((PyDateTime_DateTime *)o)->hastzinfo) ? ((PyDateTime_DateTime *)o)->tzinfo : Py_None) +#endif +#endif + +#define PyDateTime_TIMEDELTA_GET_DAYS(o) (((PyDateTime_Delta *)(o))->days) +#define PyDateTime_TIMEDELTA_GET_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) +#define PyDateTime_TIMEDELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) + namespace duckdb { PyDictionary::PyDictionary(py::object dict) { @@ -21,9 +36,9 @@ PyDictionary::PyDictionary(py::object dict) { PyTimeDelta::PyTimeDelta(py::handle &obj) { auto ptr = obj.ptr(); - days = PyDateTime_TIMEDELTA_GET_DAYS(ptr); - seconds = PyDateTime_TIMEDELTA_GET_SECONDS(ptr); - microseconds = PyDateTime_TIMEDELTA_GET_MICROSECONDS(ptr); + days = PyTimeDelta::GetDays(ptr); + seconds = PyTimeDelta::GetSeconds(ptr); + microseconds = PyTimeDelta::GetMicros(ptr); } interval_t PyTimeDelta::ToInterval() { @@ -42,6 +57,18 @@ interval_t PyTimeDelta::ToInterval() { return interval; } +int64_t PyTimeDelta::GetDays(PyObject *obj) { + return PyDateTime_TIMEDELTA_GET_DAYS(obj); // NOLINT +} + +int64_t PyTimeDelta::GetSeconds(PyObject *obj) { + return PyDateTime_TIMEDELTA_GET_SECONDS(obj); // NOLINT +} + +int64_t PyTimeDelta::GetMicros(PyObject *obj) { + return PyDateTime_TIMEDELTA_GET_MICROSECONDS(obj); // NOLINT +} + PyDecimal::PyDecimal(py::handle &obj) : obj(obj) { auto as_tuple = obj.attr("as_tuple")(); @@ -198,15 +225,16 @@ Value PyDecimal::ToDuckValue() { PyTime::PyTime(py::handle &obj) : obj(obj) { auto ptr = obj.ptr(); - hour = PyDateTime_TIME_GET_HOUR(ptr); - minute = PyDateTime_TIME_GET_MINUTE(ptr); - second = PyDateTime_TIME_GET_SECOND(ptr); - microsecond = PyDateTime_TIME_GET_MICROSECOND(ptr); - timezone_obj = PyDateTime_TIME_GET_TZINFO(ptr); + hour = PyTime::GetHours(ptr); // NOLINT + minute = PyTime::GetMinutes(ptr); // NOLINT + second = PyTime::GetSeconds(ptr); // NOLINT + microsecond = PyTime::GetMicros(ptr); // NOLINT + timezone_obj = PyTime::GetTZInfo(ptr); // NOLINT } dtime_t PyTime::ToDuckTime() { return Time::FromTime(hour, minute, second, microsecond); } + Value PyTime::ToDuckValue() { auto duckdb_time = this->ToDuckTime(); if (this->timezone_obj != Py_None) { @@ -219,6 +247,26 @@ Value PyTime::ToDuckValue() { return Value::TIME(duckdb_time); } +int32_t PyTime::GetHours(PyObject *obj) { + return PyDateTime_TIME_GET_HOUR(obj); // NOLINT +} + +int32_t PyTime::GetMinutes(PyObject *obj) { + return PyDateTime_TIME_GET_MINUTE(obj); // NOLINT +} + +int32_t PyTime::GetSeconds(PyObject *obj) { + return PyDateTime_TIME_GET_SECOND(obj); // NOLINT +} + +int32_t PyTime::GetMicros(PyObject *obj) { + return PyDateTime_TIME_GET_MICROSECOND(obj); // NOLINT +} + +PyObject *PyTime::GetTZInfo(PyObject *obj) { + return PyDateTime_TIME_GET_TZINFO(obj); // NOLINT +} + interval_t PyTimezone::GetUTCOffset(PyObject *tzone_obj) { auto tzinfo = py::reinterpret_borrow(tzone_obj); auto res = tzinfo.attr("utcoffset")(py::none()); @@ -228,14 +276,14 @@ interval_t PyTimezone::GetUTCOffset(PyObject *tzone_obj) { PyDateTime::PyDateTime(py::handle &obj) : obj(obj) { auto ptr = obj.ptr(); - year = PyDateTime_GET_YEAR(ptr); - month = PyDateTime_GET_MONTH(ptr); - day = PyDateTime_GET_DAY(ptr); - hour = PyDateTime_DATE_GET_HOUR(ptr); - minute = PyDateTime_DATE_GET_MINUTE(ptr); - second = PyDateTime_DATE_GET_SECOND(ptr); - micros = PyDateTime_DATE_GET_MICROSECOND(ptr); - tzone_obj = PyDateTime_DATE_GET_TZINFO(ptr); + year = PyDateTime::GetYears(ptr); + month = PyDateTime::GetMonths(ptr); + day = PyDateTime::GetDays(ptr); + hour = PyDateTime::GetHours(ptr); + minute = PyDateTime::GetMinutes(ptr); + second = PyDateTime::GetSeconds(ptr); + micros = PyDateTime::GetMicros(ptr); + tzone_obj = PyDateTime::GetTZInfo(ptr); } timestamp_t PyDateTime::ToTimestamp() { @@ -263,11 +311,43 @@ dtime_t PyDateTime::ToDuckTime() { return Time::FromTime(hour, minute, second, micros); } +int32_t PyDateTime::GetYears(PyObject *obj) { + return PyDateTime_GET_YEAR(obj); // NOLINT +} + +int32_t PyDateTime::GetMonths(PyObject *obj) { + return PyDateTime_GET_MONTH(obj); // NOLINT +} + +int32_t PyDateTime::GetDays(PyObject *obj) { + return PyDateTime_GET_DAY(obj); // NOLINT +} + +int32_t PyDateTime::GetHours(PyObject *obj) { + return PyDateTime_DATE_GET_HOUR(obj); // NOLINT +} + +int32_t PyDateTime::GetMinutes(PyObject *obj) { + return PyDateTime_DATE_GET_MINUTE(obj); // NOLINT +} + +int32_t PyDateTime::GetSeconds(PyObject *obj) { + return PyDateTime_DATE_GET_SECOND(obj); // NOLINT +} + +int32_t PyDateTime::GetMicros(PyObject *obj) { + return PyDateTime_DATE_GET_MICROSECOND(obj); // NOLINT +} + +PyObject *PyDateTime::GetTZInfo(PyObject *obj) { + return PyDateTime_DATE_GET_TZINFO(obj); // NOLINT +} + PyDate::PyDate(py::handle &ele) { auto ptr = ele.ptr(); - year = PyDateTime_GET_YEAR(ptr); - month = PyDateTime_GET_MONTH(ptr); - day = PyDateTime_GET_DAY(ptr); + year = PyDateTime::GetYears(ptr); + month = PyDateTime::GetMonths(ptr); + day = PyDateTime::GetDays(ptr); } Value PyDate::ToDuckValue() { diff --git a/tools/pythonpkg/src/numpy/array_wrapper.cpp b/tools/pythonpkg/src/numpy/array_wrapper.cpp index c9611cc57d41..e041c8b9360b 100644 --- a/tools/pythonpkg/src/numpy/array_wrapper.cpp +++ b/tools/pythonpkg/src/numpy/array_wrapper.cpp @@ -162,18 +162,18 @@ struct StringConvert { // based on the max codepoint, we construct the result string auto result = PyUnicode_New(start_pos + codepoint_count, max_codepoint); // based on the resulting unicode kind, we fill in the code points - auto kind = PyUnicode_KIND(result); + auto kind = PyUtil::PyUnicodeKind(result); switch (kind) { case PyUnicode_1BYTE_KIND: - ConvertUnicodeValueTemplated(PyUnicode_1BYTE_DATA(result), codepoints, codepoint_count, data, + ConvertUnicodeValueTemplated(PyUtil::PyUnicode1ByteData(result), codepoints, codepoint_count, data, start_pos); break; case PyUnicode_2BYTE_KIND: - ConvertUnicodeValueTemplated(PyUnicode_2BYTE_DATA(result), codepoints, codepoint_count, data, + ConvertUnicodeValueTemplated(PyUtil::PyUnicode2ByteData(result), codepoints, codepoint_count, data, start_pos); break; case PyUnicode_4BYTE_KIND: - ConvertUnicodeValueTemplated(PyUnicode_4BYTE_DATA(result), codepoints, codepoint_count, data, + ConvertUnicodeValueTemplated(PyUtil::PyUnicode4ByteData(result), codepoints, codepoint_count, data, start_pos); break; default: @@ -186,7 +186,7 @@ struct StringConvert { static PyObject *ConvertValue(string_t val) { // we could use PyUnicode_FromStringAndSize here, but it does a lot of verification that we don't need // because of that it is a lot slower than it needs to be - auto data = (uint8_t *)val.GetData(); + auto data = const_data_ptr_cast(val.GetData()); auto len = val.GetSize(); // check if there are any non-ascii characters in there for (idx_t i = 0; i < len; i++) { @@ -198,7 +198,7 @@ struct StringConvert { // no unicode: fast path // directly construct the string and memcpy it auto result = PyUnicode_New(len, 127); - auto target_data = PyUnicode_DATA(result); + auto target_data = PyUtil::PyUnicodeDataMutable(result); memcpy(target_data, data, len); return result; } diff --git a/tools/pythonpkg/src/numpy/numpy_scan.cpp b/tools/pythonpkg/src/numpy/numpy_scan.cpp index cdff2ce4be39..7cbd68186c69 100644 --- a/tools/pythonpkg/src/numpy/numpy_scan.cpp +++ b/tools/pythonpkg/src/numpy/numpy_scan.cpp @@ -229,7 +229,7 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, break; case NumpyNullableType::DATETIME: case NumpyNullableType::DATETIME_TZ: { - auto src_ptr = (int64_t *)array.data(); + auto src_ptr = reinterpret_cast(array.data()); auto tgt_ptr = FlatVector::GetData(out); auto &mask = FlatVector::Validity(out); @@ -318,7 +318,7 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, obj = py::str(object_handle); }, *val, *gil); - val = (PyObject *)bind_data.object_str_val.GetPointerTop()->ptr(); + val = reinterpret_cast(bind_data.object_str_val.GetPointerTop()->ptr()); } } // Python 3 string representation: @@ -327,9 +327,9 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, out_mask.SetInvalid(row); continue; } - if (PyUnicode_IS_COMPACT_ASCII(val)) { + if (PyUtil::PyUnicodeIsCompactASCII(val)) { // ascii string: we can zero copy - tgt_ptr[row] = string_t(const_char_ptr_cast(PyUnicode_DATA(val)), PyUnicode_GET_LENGTH(val)); + tgt_ptr[row] = string_t(PyUtil::PyUnicodeData(val), PyUtil::PyUnicodeGetLength(val)); } else { // unicode gunk auto ascii_obj = reinterpret_cast(val); @@ -338,20 +338,21 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, if (unicode_obj->utf8) { // there is! zero copy tgt_ptr[row] = string_t(const_char_ptr_cast(unicode_obj->utf8), unicode_obj->utf8_length); - } else if (PyUnicode_IS_COMPACT(unicode_obj) && !PyUnicode_IS_ASCII(unicode_obj)) { // NOLINT - auto kind = PyUnicode_KIND(val); + } else if (PyUtil::PyUnicodeIsCompact(unicode_obj) && + !PyUtil::PyUnicodeIsASCII(unicode_obj)) { // NOLINT + auto kind = PyUtil::PyUnicodeKind(val); switch (kind) { case PyUnicode_1BYTE_KIND: - tgt_ptr[row] = - DecodePythonUnicode(PyUnicode_1BYTE_DATA(val), PyUnicode_GET_LENGTH(val), out); + tgt_ptr[row] = DecodePythonUnicode(PyUtil::PyUnicode1ByteData(val), + PyUtil::PyUnicodeGetLength(val), out); break; case PyUnicode_2BYTE_KIND: - tgt_ptr[row] = - DecodePythonUnicode(PyUnicode_2BYTE_DATA(val), PyUnicode_GET_LENGTH(val), out); + tgt_ptr[row] = DecodePythonUnicode(PyUtil::PyUnicode2ByteData(val), + PyUtil::PyUnicodeGetLength(val), out); break; case PyUnicode_4BYTE_KIND: - tgt_ptr[row] = - DecodePythonUnicode(PyUnicode_4BYTE_DATA(val), PyUnicode_GET_LENGTH(val), out); + tgt_ptr[row] = DecodePythonUnicode(PyUtil::PyUnicode4ByteData(val), + PyUtil::PyUnicodeGetLength(val), out); break; default: throw NotImplementedException( @@ -359,7 +360,7 @@ void NumpyScan::Scan(PandasColumnBindData &bind_data, idx_t count, idx_t offset, } } else if (ascii_obj->state.kind == PyUnicode_WCHAR_KIND) { throw InvalidInputException("Unsupported: decode not ready legacy string"); - } else if (!PyUnicode_IS_COMPACT(unicode_obj) && ascii_obj->state.kind != PyUnicode_WCHAR_KIND) { + } else if (!PyUtil::PyUnicodeIsCompact(unicode_obj) && ascii_obj->state.kind != PyUnicode_WCHAR_KIND) { throw InvalidInputException("Unsupported: decode ready legacy string"); } else { throw InvalidInputException("Unsupported string type: no clue what this string is"); From 91a585dd291b2a58c099179b0f7901d09a88b42f Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 18:05:16 +0200 Subject: [PATCH 26/28] Move these defines to header --- .../src/include/duckdb_python/python_objects.hpp | 15 +++++++++++++++ tools/pythonpkg/src/native/python_objects.cpp | 15 --------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp b/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp index ee850b83e9ba..05cbd6fe2709 100644 --- a/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp +++ b/tools/pythonpkg/src/include/duckdb_python/python_objects.hpp @@ -11,6 +11,21 @@ #include "datetime.h" //from python +/* Backport for Python < 3.10 */ +#if PY_VERSION_HEX < 0x030a00a1 +#ifndef PyDateTime_TIME_GET_TZINFO +#define PyDateTime_TIME_GET_TZINFO(o) ((((PyDateTime_Time *)o)->hastzinfo) ? ((PyDateTime_Time *)o)->tzinfo : Py_None) +#endif +#ifndef PyDateTime_DATE_GET_TZINFO +#define PyDateTime_DATE_GET_TZINFO(o) \ + ((((PyDateTime_DateTime *)o)->hastzinfo) ? ((PyDateTime_DateTime *)o)->tzinfo : Py_None) +#endif +#endif + +#define PyDateTime_TIMEDELTA_GET_DAYS(o) (((PyDateTime_Delta *)(o))->days) +#define PyDateTime_TIMEDELTA_GET_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) +#define PyDateTime_TIMEDELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) + namespace duckdb { struct PyDictionary { diff --git a/tools/pythonpkg/src/native/python_objects.cpp b/tools/pythonpkg/src/native/python_objects.cpp index ba4d03cc07c4..60a9c764345c 100644 --- a/tools/pythonpkg/src/native/python_objects.cpp +++ b/tools/pythonpkg/src/native/python_objects.cpp @@ -10,21 +10,6 @@ #include "datetime.h" // Python datetime initialize #1 -/* Backport for Python < 3.10 */ -#if PY_VERSION_HEX < 0x030a00a1 -#ifndef PyDateTime_TIME_GET_TZINFO -#define PyDateTime_TIME_GET_TZINFO(o) ((((PyDateTime_Time *)o)->hastzinfo) ? ((PyDateTime_Time *)o)->tzinfo : Py_None) -#endif -#ifndef PyDateTime_DATE_GET_TZINFO -#define PyDateTime_DATE_GET_TZINFO(o) \ - ((((PyDateTime_DateTime *)o)->hastzinfo) ? ((PyDateTime_DateTime *)o)->tzinfo : Py_None) -#endif -#endif - -#define PyDateTime_TIMEDELTA_GET_DAYS(o) (((PyDateTime_Delta *)(o))->days) -#define PyDateTime_TIMEDELTA_GET_SECONDS(o) (((PyDateTime_Delta *)(o))->seconds) -#define PyDateTime_TIMEDELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta *)(o))->microseconds) - namespace duckdb { PyDictionary::PyDictionary(py::object dict) { From 43864299201b924a12e37a0961b1e7cd7f875008 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 19:57:52 +0200 Subject: [PATCH 27/28] Fix sql auto complete extension --- extension/autocomplete/sql_auto_complete-extension.cpp | 4 ++-- tools/sqlite3_api_wrapper/CMakeLists.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/extension/autocomplete/sql_auto_complete-extension.cpp b/extension/autocomplete/sql_auto_complete-extension.cpp index d5b3ffbdf021..a4dde2ab25e3 100644 --- a/extension/autocomplete/sql_auto_complete-extension.cpp +++ b/extension/autocomplete/sql_auto_complete-extension.cpp @@ -33,7 +33,7 @@ struct SQLAutoCompleteData : public GlobalTableFunctionState { }; struct AutoCompleteCandidate { - AutoCompleteCandidate(string candidate_p, int32_t score_bonus = 0) + explicit AutoCompleteCandidate(string candidate_p, int32_t score_bonus = 0) : candidate(std::move(candidate_p)), score_bonus(score_bonus) { } @@ -408,7 +408,7 @@ void SQLAutoCompleteExtension::Load(DuckDB &db) { } std::string SQLAutoCompleteExtension::Name() { - return "sql_auto_complete"; + return "autocomplete"; } } // namespace duckdb diff --git a/tools/sqlite3_api_wrapper/CMakeLists.txt b/tools/sqlite3_api_wrapper/CMakeLists.txt index 5b4e4c301a3e..84ac5390e3bc 100644 --- a/tools/sqlite3_api_wrapper/CMakeLists.txt +++ b/tools/sqlite3_api_wrapper/CMakeLists.txt @@ -10,7 +10,7 @@ add_definitions(-DUSE_DUCKDB_SHELL_WRAPPER) include_directories(../../third_party/utf8proc/include) -if(NOT BUILD_AUTOCOMPLETE_EXTENSION) +if(NOT BUILD_AUTOCOMPLETE_EXTENSION AND NOT DISABLE_BUILTIN_EXTENSIONS) include_directories(../../extension/autocomplete/include) set(ALL_OBJECT_FILES ${ALL_OBJECT_FILES} From 0c5ecc309bcc735af682cb362ac141b0a58eb358 Mon Sep 17 00:00:00 2001 From: Mark Raasveldt Date: Tue, 23 May 2023 23:48:19 +0200 Subject: [PATCH 28/28] Coverage fixes --- .github/config/uncovered_files.csv | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/config/uncovered_files.csv b/.github/config/uncovered_files.csv index f693f86b75e7..5abdd8c7da41 100644 --- a/.github/config/uncovered_files.csv +++ b/.github/config/uncovered_files.csv @@ -30,7 +30,7 @@ common/exception_format_value.cpp 8 common/field_writer.cpp 3 common/file_buffer.cpp 5 common/file_system.cpp 19 -common/fsst.cpp 5 +common/fsst.cpp 6 common/gzip_file_system.cpp 40 common/hive_partitioning.cpp 54 common/local_file_system.cpp 16 @@ -53,7 +53,7 @@ common/serializer/buffered_deserializer.cpp 3 common/serializer/buffered_file_reader.cpp 9 common/serializer/buffered_file_writer.cpp 2 common/sort/comparators.cpp 72 -common/sort/merge_sorter.cpp 99 +common/sort/merge_sorter.cpp 100 common/sort/partition_state.cpp 43 common/sort/sort_state.cpp 10 common/sort/sorted_block.cpp 20 @@ -258,7 +258,7 @@ function/scalar_function.cpp 7 function/table/arrow.cpp 61 function/table/arrow_conversion.cpp 225 function/table/checkpoint.cpp 7 -function/table/copy_csv.cpp 14 +function/table/copy_csv.cpp 16 function/table/pragma_detailed_profiling_output.cpp 86 function/table/pragma_last_profiling_output.cpp 51 function/table/range.cpp 5 @@ -390,7 +390,7 @@ include/duckdb/main/connection_manager.hpp 3 include/duckdb/main/database.hpp 2 include/duckdb/main/prepared_statement.hpp 9 include/duckdb/main/query_profiler.hpp 9 -include/duckdb/main/query_result.hpp 9 +include/duckdb/main/query_result.hpp 11 include/duckdb/main/relation.hpp 3 include/duckdb/main/relation/subquery_relation.hpp 3 include/duckdb/main/relation/write_parquet_relation.hpp 3 @@ -564,7 +564,7 @@ parser/statement/multi_statement.cpp 7 parser/statement/select_statement.cpp 2 parser/statement/set_statement.cpp 3 parser/statement/update_statement.cpp 2 -parser/tableref.cpp 8 +parser/tableref.cpp 9 parser/tableref/basetableref.cpp 2 parser/tableref/emptytableref.cpp 3 parser/tableref/expressionlistref.cpp 5