Skip to content

Commit

Permalink
chore: Update vendored sources to duckdb/duckdb@9e97aa3
Browse files Browse the repository at this point in the history
Merge pull request duckdb/duckdb#11622 from Mytherin/fuzzerissues
Merge pull request duckdb/duckdb#11610 from guenp/guenp/fix-timestamp-is-date
  • Loading branch information
krlmlr committed Apr 12, 2024
1 parent d5b6991 commit f0fc038
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 55 deletions.
6 changes: 5 additions & 1 deletion src/duckdb/src/function/cast_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,12 @@ int64_t CastRules::ImplicitCast(const LogicalType &from, const LogicalType &to)
}
if (from.id() == LogicalTypeId::ARRAY && to.id() == LogicalTypeId::LIST) {
// Arrays can be cast to lists for the cost of casting the child type
auto child_cost = ImplicitCast(ArrayType::GetChildType(from), ListType::GetChildType(to));
if (child_cost < 0) {
return -1;
}
// add 1 because we prefer ARRAY->ARRAY casts over ARRAY->LIST casts
return ImplicitCast(ArrayType::GetChildType(from), ListType::GetChildType(to)) + 1;
return child_cost + 1;
}
if (from.id() == LogicalTypeId::LIST && (to.id() == LogicalTypeId::ARRAY && !ArrayType::IsAnySize(to))) {
// Lists can be cast to arrays for the cost of casting the child type, if the target size is known
Expand Down
25 changes: 23 additions & 2 deletions src/duckdb/src/function/scalar/string/length.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ static unique_ptr<BaseStatistics> LengthPropagateStats(ClientContext &context, F
//------------------------------------------------------------------
// ARRAY / LIST LENGTH
//------------------------------------------------------------------

static void ListLengthFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &input = args.data[0];
D_ASSERT(input.GetType().id() == LogicalTypeId::LIST);
Expand All @@ -83,9 +82,31 @@ static void ListLengthFunction(DataChunk &args, ExpressionState &state, Vector &

static void ArrayLengthFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &input = args.data[0];
// If the input is an array, the length is constant

UnifiedVectorFormat format;
args.data[0].ToUnifiedFormat(args.size(), format);

// for arrays the length is constant
result.SetVectorType(VectorType::CONSTANT_VECTOR);
ConstantVector::GetData<int64_t>(result)[0] = static_cast<int64_t>(ArrayType::GetSize(input.GetType()));

// but we do need to take null values into account
if (format.validity.AllValid()) {
// if there are no null values we can just return the constant
return;
}
// otherwise we flatten and inherit the null values of the parent
result.Flatten(args.size());
auto &result_validity = FlatVector::Validity(result);
for (idx_t r = 0; r < args.size(); r++) {
auto idx = format.sel->get_index(r);
if (!format.validity.RowIsValid(idx)) {
result_validity.SetInvalid(r);
}
}
if (args.AllConstant()) {
result.SetVectorType(VectorType::CONSTANT_VECTOR);
}
}

static unique_ptr<FunctionData> ArrayOrListLengthBind(ClientContext &context, ScalarFunction &bound_function,
Expand Down
6 changes: 3 additions & 3 deletions src/duckdb/src/function/table/version/pragma_version.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef DUCKDB_PATCH_VERSION
#define DUCKDB_PATCH_VERSION "2-dev815"
#define DUCKDB_PATCH_VERSION "2-dev832"
#endif
#ifndef DUCKDB_MINOR_VERSION
#define DUCKDB_MINOR_VERSION 10
Expand All @@ -8,10 +8,10 @@
#define DUCKDB_MAJOR_VERSION 0
#endif
#ifndef DUCKDB_VERSION
#define DUCKDB_VERSION "v0.10.2-dev815"
#define DUCKDB_VERSION "v0.10.2-dev832"
#endif
#ifndef DUCKDB_SOURCE_ID
#define DUCKDB_SOURCE_ID "b9199653c3"
#define DUCKDB_SOURCE_ID "9e97aa38b7"
#endif
#include "duckdb/function/table/system_functions.hpp"
#include "duckdb/main/database.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class LambdaFunctions {
result_validity = &FlatVector::Validity(result);

if (list_column.GetType().id() == LogicalTypeId::SQLNULL) {
result_validity->SetInvalid(0);
result.SetVectorType(VectorType::CONSTANT_VECTOR);
ConstantVector::SetNull(result, true);
result_is_null = true;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct FlattenDependentJoins {

//! Detects which Logical Operators have correlated expressions that they are dependent upon, filling the
//! has_correlated_expressions map.
bool DetectCorrelatedExpressions(LogicalOperator *op, bool lateral = false, idx_t lateral_depth = 0);
bool DetectCorrelatedExpressions(LogicalOperator &op, bool lateral = false, idx_t lateral_depth = 0);

//! Mark entire subtree of Logical Operators as correlated by adding them to the has_correlated_expressions map.
bool MarkSubtreeCorrelated(LogicalOperator &op);
Expand All @@ -35,7 +35,7 @@ struct FlattenDependentJoins {
ColumnBinding base_binding;
idx_t delim_offset;
idx_t data_offset;
unordered_map<LogicalOperator *, bool> has_correlated_expressions;
reference_map_t<LogicalOperator, bool> has_correlated_expressions;
column_binding_map_t<idx_t> correlated_map;
column_binding_map_t<idx_t> replacement_map;
const vector<CorrelatedColumnInfo> &correlated_columns;
Expand Down
20 changes: 10 additions & 10 deletions src/duckdb/src/planner/binder/query_node/plan_subquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static unique_ptr<Expression> PlanCorrelatedSubquery(Binder &binder, BoundSubque
FlattenDependentJoins flatten(binder, correlated_columns, perform_delim);

// first we check which logical operators have correlated expressions in the first place
flatten.DetectCorrelatedExpressions(plan.get());
flatten.DetectCorrelatedExpressions(*plan);
// now we push the dependent join down
auto dependent_join = flatten.PushDownDependentJoin(std::move(plan));

Expand All @@ -279,7 +279,7 @@ static unique_ptr<Expression> PlanCorrelatedSubquery(Binder &binder, BoundSubque
delim_join->mark_index = mark_index;
// RHS
FlattenDependentJoins flatten(binder, correlated_columns, perform_delim, true);
flatten.DetectCorrelatedExpressions(plan.get());
flatten.DetectCorrelatedExpressions(*plan);
auto dependent_join = flatten.PushDownDependentJoin(std::move(plan));

// fetch the set of columns
Expand Down Expand Up @@ -307,7 +307,7 @@ static unique_ptr<Expression> PlanCorrelatedSubquery(Binder &binder, BoundSubque
delim_join->mark_index = mark_index;
// RHS
FlattenDependentJoins flatten(binder, correlated_columns, true, true);
flatten.DetectCorrelatedExpressions(plan.get());
flatten.DetectCorrelatedExpressions(*plan);
auto dependent_join = flatten.PushDownDependentJoin(std::move(plan));

// fetch the columns
Expand Down Expand Up @@ -411,8 +411,8 @@ void Binder::PlanSubqueries(unique_ptr<Expression> &expr_ptr, unique_ptr<Logical
}

unique_ptr<LogicalOperator> Binder::PlanLateralJoin(unique_ptr<LogicalOperator> left, unique_ptr<LogicalOperator> right,
vector<CorrelatedColumnInfo> &correlated_columns,
JoinType join_type, unique_ptr<Expression> condition) {
vector<CorrelatedColumnInfo> &correlated, JoinType join_type,
unique_ptr<Expression> condition) {
// scan the right operator for correlated columns
// correlated LATERAL JOIN
vector<JoinCondition> conditions;
Expand All @@ -423,13 +423,13 @@ unique_ptr<LogicalOperator> Binder::PlanLateralJoin(unique_ptr<LogicalOperator>
arbitrary_expressions);
}

auto perform_delim = PerformDuplicateElimination(*this, correlated_columns);
auto delim_join = CreateDuplicateEliminatedJoin(correlated_columns, join_type, std::move(left), perform_delim);
auto perform_delim = PerformDuplicateElimination(*this, correlated);
auto delim_join = CreateDuplicateEliminatedJoin(correlated, join_type, std::move(left), perform_delim);

FlattenDependentJoins flatten(*this, correlated_columns, perform_delim);
FlattenDependentJoins flatten(*this, correlated, perform_delim);

// first we check which logical operators have correlated expressions in the first place
flatten.DetectCorrelatedExpressions(right.get(), true);
flatten.DetectCorrelatedExpressions(*right, true);
// now we push the dependent join down
auto dependent_join = flatten.PushDownDependentJoin(std::move(right));

Expand All @@ -448,7 +448,7 @@ unique_ptr<LogicalOperator> Binder::PlanLateralJoin(unique_ptr<LogicalOperator>
D_ASSERT(delim_join->conditions.empty());
delim_join->conditions = std::move(conditions);
// then add the delim join conditions
CreateDelimJoinConditions(*delim_join, correlated_columns, plan_columns, flatten.delim_offset, perform_delim);
CreateDelimJoinConditions(*delim_join, correlated, plan_columns, flatten.delim_offset, perform_delim);
delim_join->AddChild(std::move(dependent_join));

// check if there are any arbitrary expressions left
Expand Down
16 changes: 8 additions & 8 deletions src/duckdb/src/planner/expression_binder/lateral_binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ static void ReduceColumnDepth(vector<CorrelatedColumnInfo> &columns,
}
}

static void ReduceExpressionSubquery(BoundSubqueryExpression &expr,
const vector<CorrelatedColumnInfo> &correlated_columns) {
ReduceColumnDepth(expr.binder->correlated_columns, correlated_columns);
}

class ExpressionDepthReducerRecursive : public BoundNodeVisitor {
public:
explicit ExpressionDepthReducerRecursive(const vector<CorrelatedColumnInfo> &correlated)
Expand All @@ -111,6 +106,13 @@ class ExpressionDepthReducerRecursive : public BoundNodeVisitor {
BoundNodeVisitor::VisitBoundTableRef(ref);
}

static void ReduceExpressionSubquery(BoundSubqueryExpression &expr,
const vector<CorrelatedColumnInfo> &correlated_columns) {
ReduceColumnDepth(expr.binder->correlated_columns, correlated_columns);
ExpressionDepthReducerRecursive recursive(correlated_columns);
recursive.VisitBoundQueryNode(*expr.subquery);
}

private:
const vector<CorrelatedColumnInfo> &correlated_columns;
};
Expand All @@ -127,9 +129,7 @@ class ExpressionDepthReducer : public LogicalOperatorVisitor {
}

unique_ptr<Expression> VisitReplace(BoundSubqueryExpression &expr, unique_ptr<Expression> *expr_ptr) override {
ReduceExpressionSubquery(expr, correlated_columns);
ExpressionDepthReducerRecursive recursive(correlated_columns);
recursive.VisitBoundQueryNode(*expr.subquery);
ExpressionDepthReducerRecursive::ReduceExpressionSubquery(expr, correlated_columns);
return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void SelectBindState::AddRegularColumn() {
}

idx_t SelectBindState::GetFinalIndex(idx_t index) const {
if (expanded_column_indices.empty()) {
if (index >= expanded_column_indices.size()) {
return index;
}
return expanded_column_indices[index];
Expand Down
53 changes: 26 additions & 27 deletions src/duckdb/src/planner/subquery/flatten_dependent_join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,28 @@ FlattenDependentJoins::FlattenDependentJoins(Binder &binder, const vector<Correl
}
}

bool FlattenDependentJoins::DetectCorrelatedExpressions(LogicalOperator *op, bool lateral, idx_t lateral_depth) {
bool FlattenDependentJoins::DetectCorrelatedExpressions(LogicalOperator &op, bool lateral, idx_t lateral_depth) {

bool is_lateral_join = false;

D_ASSERT(op);
// check if this entry has correlated expressions
if (op->type == LogicalOperatorType::LOGICAL_DEPENDENT_JOIN) {
if (op.type == LogicalOperatorType::LOGICAL_DEPENDENT_JOIN) {
is_lateral_join = true;
}
HasCorrelatedExpressions visitor(correlated_columns, lateral, lateral_depth);
visitor.VisitOperator(*op);
visitor.VisitOperator(op);
bool has_correlation = visitor.has_correlated_expressions;
int child_idx = 0;
// now visit the children of this entry and check if they have correlated expressions
for (auto &child : op->children) {
for (auto &child : op.children) {
auto new_lateral_depth = lateral_depth;
if (is_lateral_join && child_idx == 1) {
new_lateral_depth = lateral_depth + 1;
}
// we OR the property with its children such that has_correlation is true if either
// (1) this node has a correlated expression or
// (2) one of its children has a correlated expression
if (DetectCorrelatedExpressions(child.get(), lateral, new_lateral_depth)) {
if (DetectCorrelatedExpressions(*child, lateral, new_lateral_depth)) {
has_correlation = true;
}
child_idx++;
Expand All @@ -60,29 +59,29 @@ bool FlattenDependentJoins::DetectCorrelatedExpressions(LogicalOperator *op, boo
// If we detect correlation in a materialized or recursive CTE, the entire right side of the operator
// needs to be marked as correlated. Otherwise, function PushDownDependentJoinInternal does not do the
// right thing.
if (op->type == LogicalOperatorType::LOGICAL_MATERIALIZED_CTE ||
op->type == LogicalOperatorType::LOGICAL_RECURSIVE_CTE) {
if (op.type == LogicalOperatorType::LOGICAL_MATERIALIZED_CTE ||
op.type == LogicalOperatorType::LOGICAL_RECURSIVE_CTE) {
if (has_correlation) {
MarkSubtreeCorrelated(*op->children[1].get());
MarkSubtreeCorrelated(*op.children[1].get());
}
}
return has_correlation;
}

bool FlattenDependentJoins::MarkSubtreeCorrelated(LogicalOperator &op) {
// Do not mark base table scans as correlated
auto entry = has_correlated_expressions.find(&op);
auto entry = has_correlated_expressions.find(op);
D_ASSERT(entry != has_correlated_expressions.end());
bool has_correlation = entry->second;
for (auto &child : op.children) {
has_correlation |= MarkSubtreeCorrelated(*child.get());
}
if (op.type != LogicalOperatorType::LOGICAL_GET || op.children.size() == 1) {
if (op.type == LogicalOperatorType::LOGICAL_CTE_REF) {
has_correlated_expressions[&op] = true;
has_correlated_expressions[op] = true;
return true;
} else {
has_correlated_expressions[&op] = has_correlation;
has_correlated_expressions[op] = has_correlation;
}
}
return has_correlation;
Expand All @@ -99,17 +98,17 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoin(unique_
return result;
}

bool SubqueryDependentFilter(Expression *expr) {
if (expr->expression_class == ExpressionClass::BOUND_CONJUNCTION &&
expr->GetExpressionType() == ExpressionType::CONJUNCTION_AND) {
auto &bound_conjuction = expr->Cast<BoundConjunctionExpression>();
bool SubqueryDependentFilter(Expression &expr) {
if (expr.expression_class == ExpressionClass::BOUND_CONJUNCTION &&
expr.GetExpressionType() == ExpressionType::CONJUNCTION_AND) {
auto &bound_conjuction = expr.Cast<BoundConjunctionExpression>();
for (auto &child : bound_conjuction.children) {
if (SubqueryDependentFilter(child.get())) {
if (SubqueryDependentFilter(*child)) {
return true;
}
}
}
if (expr->expression_class == ExpressionClass::BOUND_SUBQUERY) {
if (expr.expression_class == ExpressionClass::BOUND_SUBQUERY) {
return true;
}
return false;
Expand All @@ -119,7 +118,7 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
bool &parent_propagate_null_values,
idx_t lateral_depth) {
// first check if the logical operator has correlated expressions
auto entry = has_correlated_expressions.find(plan.get());
auto entry = has_correlated_expressions.find(*plan);
D_ASSERT(entry != has_correlated_expressions.end());
if (!entry->second) {
// we reached a node without correlated expressions
Expand Down Expand Up @@ -151,7 +150,7 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
// filter
// first we flatten the dependent join in the child of the filter
for (auto &expr : plan->expressions) {
any_join |= SubqueryDependentFilter(expr.get());
any_join |= SubqueryDependentFilter(*expr);
}
plan->children[0] =
PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
Expand Down Expand Up @@ -288,8 +287,8 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
case LogicalOperatorType::LOGICAL_CROSS_PRODUCT: {
// cross product
// push into both sides of the plan
bool left_has_correlation = has_correlated_expressions.find(plan->children[0].get())->second;
bool right_has_correlation = has_correlated_expressions.find(plan->children[1].get())->second;
bool left_has_correlation = has_correlated_expressions.find(*plan->children[0])->second;
bool right_has_correlation = has_correlated_expressions.find(*plan->children[1])->second;
if (!right_has_correlation) {
// only left has correlation: push into left
plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
Expand Down Expand Up @@ -350,8 +349,8 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
auto &join = plan->Cast<LogicalJoin>();
D_ASSERT(plan->children.size() == 2);
// check the correlated expressions in the children of the join
bool left_has_correlation = has_correlated_expressions.find(plan->children[0].get())->second;
bool right_has_correlation = has_correlated_expressions.find(plan->children[1].get())->second;
bool left_has_correlation = has_correlated_expressions.find(*plan->children[0])->second;
bool right_has_correlation = has_correlated_expressions.find(*plan->children[1])->second;

if (join.join_type == JoinType::INNER) {
// inner join
Expand Down Expand Up @@ -433,12 +432,12 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
auto &comparison_join = join.Cast<LogicalComparisonJoin>();
comparison_join.conditions.push_back(std::move(cond));
} else {
auto &any_join = join.Cast<LogicalAnyJoin>();
auto &logical_any_join = join.Cast<LogicalAnyJoin>();
auto comparison = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_NOT_DISTINCT_FROM,
std::move(left), std::move(right));
auto conjunction = make_uniq<BoundConjunctionExpression>(
ExpressionType::CONJUNCTION_AND, std::move(comparison), std::move(any_join.condition));
any_join.condition = std::move(conjunction);
ExpressionType::CONJUNCTION_AND, std::move(comparison), std::move(logical_any_join.condition));
logical_any_join.condition = std::move(conjunction);
}
}
// then we replace any correlated expressions with the corresponding entry in the correlated_map
Expand Down

0 comments on commit f0fc038

Please # to comment.