362842 {
362843
362844 if (depth > 0) {
362845 return BindResult(BinderException(function, "UNNEST() for correlated expressions is not supported yet"));
362846 }
362847
362848 ErrorData error;
362849 if (function.children.empty()) {
362850 return BindResult(BinderException(function, "UNNEST() requires at lease one argument"));
362851 }
362852 if (inside_window) {
362853 return BindResult(BinderException(function, UnsupportedUnnestMessage()));
362854 }
362855
362856 if (function.distinct || function.filter || !function.order_bys->orders.empty()) {
362857 throw InvalidInputException("\"DISTINCT\", \"FILTER\", and \"ORDER BY\" are not "
362858 "applicable to \"UNNEST\"");
362859 }
362860
362861 idx_t max_depth = 1;
362862 bool keep_parent_names = false;
362863 if (function.children.size() != 1) {
362864 bool supported_argument = false;
362865 for (
idx_t i = 1; i < function.children.size(); i++) {
362866 if (function.children[i]->HasParameter()) {
362867 throw ParameterNotAllowedException("Parameter not allowed in unnest parameter");
362868 }
362869 if (!function.children[i]->IsScalar()) {
362870 break;
362871 }
362873 BindChild(function.children[i], depth, error);
362874 if (error.HasError()) {
362875 return BindResult(std::move(error));
362876 }
362877 auto &const_child = BoundExpression::GetExpression(*function.children[i]);
362879 if (alias == "recursive") {
362880 auto recursive = value.GetValue<bool>();
362881 if (recursive) {
362882 max_depth = NumericLimits<idx_t>::Maximum();
362883 }
362884 } else if (alias == "max_depth") {
362885 max_depth = value.GetValue<
uint32_t>();
362886 if (max_depth == 0) {
362887 throw BinderException("UNNEST cannot have a max depth of 0");
362888 }
362889 } else if (alias == "keep_parent_names") {
362890 keep_parent_names = value.GetValue<bool>();
362891 } else if (!alias.empty()) {
362892 throw BinderException("Unsupported parameter \"%s\" for unnest", alias);
362893 } else {
362894 break;
362895 }
362896 supported_argument = true;
362897 }
362898 if (!supported_argument) {
362899 return BindResult(BinderException(
362900 function, "UNNEST - unsupported extra argument, unnest only supports "
362901 "recursive := [true/false], max_depth := # or keep_parent_names := [true/false]"));
362902 }
362903 }
362904 unnest_level++;
362905 BindChild(function.children[0], depth, error);
362906 if (error.HasError()) {
362907
362908
362909 auto result = BindCorrelatedColumns(function.children[0], error);
362910 if (result.HasError()) {
362911 return BindResult(result.error);
362912 }
362913 auto &bound_expr = BoundExpression::GetExpression(*function.children[0]);
362914 ExtractCorrelatedExpressions(binder, *bound_expr);
362915 }
362916 auto &child = BoundExpression::GetExpression(*function.children[0]);
362918 auto &child_type = child->return_type;
362919 unnest_level--;
362920
362921 if (unnest_level > 0) {
362922 throw BinderException(
362923 function,
362924 "Nested UNNEST calls are not supported - use UNNEST(x, recursive := true) to unnest multiple levels");
362925 }
362926
362927 switch (child_type.id()) {
362928 case LogicalTypeId::UNKNOWN:
362929 throw ParameterNotResolvedException();
362930 case LogicalTypeId::LIST:
362931 case LogicalTypeId::STRUCT:
362932 case LogicalTypeId::SQLNULL:
362933 break;
362934 default:
362935 return BindResult(BinderException(function, "UNNEST() can only be applied to lists, structs and NULL, not %s",
362936 child_type.ToString()));
362937 }
362938
362939 idx_t list_unnests;
362940 idx_t struct_unnests = 0;
362941
362942 auto unnest_expr = std::move(child);
362943 if (child_type.id() == LogicalTypeId::SQLNULL) {
362944 list_unnests = 1;
362945 } else {
362946
362947 auto type = child_type;
362948 list_unnests = 0;
362949 while (type.id() == LogicalTypeId::LIST) {
362950 type = ListType::GetChildType(type);
362951 list_unnests++;
362952 if (list_unnests >= max_depth) {
362953 break;
362954 }
362955 }
362956
362957 if (type.id() == LogicalTypeId::STRUCT) {
362958 struct_unnests = max_depth - list_unnests;
362959 }
362960 }
362961 if (struct_unnests > 0 && !root_expression) {
362962 child = std::move(unnest_expr);
362963 return BindResult(BinderException(
362964 function, "UNNEST() on a struct column can only be applied as the root element of a SELECT expression"));
362965 }
362966
362967 auto return_type = child_type;
362968 for (
idx_t current_depth = 0; current_depth < list_unnests; current_depth++) {
362969 if (return_type.id() == LogicalTypeId::LIST) {
362970 return_type = ListType::GetChildType(return_type);
362971 }
362972 auto result = make_uniq<BoundUnnestExpression>(return_type);
362973 result->child = std::move(unnest_expr);
362974 auto alias = function.GetAlias().empty() ? result->ToString() : function.GetAlias();
362975
362976 auto current_level = unnest_level + list_unnests - current_depth - 1;
362977 auto entry = node.unnests.find(current_level);
362978 idx_t unnest_table_index;
362979 idx_t unnest_column_index;
362980 if (entry == node.unnests.end()) {
362981 BoundUnnestNode unnest_node;
362982 unnest_node.index = binder.GenerateTableIndex();
362983 unnest_node.expressions.push_back(std::move(result));
362984 unnest_table_index = unnest_node.index;
362985 unnest_column_index = 0;
362986 node.unnests.insert(make_pair(current_level, std::move(unnest_node)));
362987 } else {
362988 unnest_table_index = entry->second.index;
362989 unnest_column_index = entry->second.expressions.size();
362990 entry->second.expressions.push_back(std::move(result));
362991 }
362992
362993 unnest_expr = make_uniq<BoundColumnRefExpression>(
362994 std::move(alias), return_type, ColumnBinding(unnest_table_index, unnest_column_index), depth);
362995 }
362996
362997 if (struct_unnests > 0) {
362998 vector<unique_ptr<Expression>> struct_expressions;
362999 struct_expressions.push_back(std::move(unnest_expr));
363000 for (
idx_t i = 0; i < struct_unnests; i++) {
363001 vector<unique_ptr<Expression>> new_expressions;
363002
363003 bool has_structs = false;
363004 for (auto &expr : struct_expressions) {
363005 if (expr->return_type.id() == LogicalTypeId::STRUCT) {
363006
363007 auto &child_types = StructType::GetChildTypes(expr->return_type);
363008 if (StructType::IsUnnamed(expr->return_type)) {
363009 for (
idx_t child_index = 0; child_index < child_types.size(); child_index++) {
363010 new_expressions.push_back(
363011 CreateBoundStructExtractIndex(context, expr->Copy(), child_index + 1));
363012 }
363013 } else {
363014 for (auto &entry : child_types) {
363015 vector<string> current_key_path;
363016
363017 if (keep_parent_names && expr->type == ExpressionType::BOUND_FUNCTION) {
363018 current_key_path.push_back(expr->alias);
363019 }
363020 current_key_path.push_back(entry.first);
363021 new_expressions.push_back(
363022 CreateBoundStructExtract(context, expr->Copy(), current_key_path, keep_parent_names));
363023 }
363024 }
363025 has_structs = true;
363026 } else {
363027
363028 new_expressions.push_back(std::move(expr));
363029 }
363030 }
363031 struct_expressions = std::move(new_expressions);
363032 if (!has_structs) {
363033 break;
363034 }
363035 }
363036 unnest_expr = make_uniq<BoundExpandedExpression>(std::move(struct_expressions));
363037 }
363038 return BindResult(std::move(unnest_expr));
363039}
static DUCKDB_API unique_ptr< Expression > AddArrayCastToList(ClientContext &context, unique_ptr< Expression > expr)
If the expression returns an array, cast it to return a list with the same child type....
Definition duckdb.cpp:375877
static DUCKDB_API Value EvaluateScalar(ClientContext &context, const Expression &expr, bool allow_unfoldable=false)
Evaluate a scalar expression and fold it into a single value.
Definition duckdb.cpp:149993
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237