Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::Transformer Class Reference
Collaboration diagram for duckdb::Transformer:

Classes

struct  CreatePivotEntry

Public Member Functions

 Transformer (ParserOptions &options)
 Transformer (Transformer &parent)
bool TransformParseTree (duckdb_libpgquery::PGList *tree, vector< unique_ptr< SQLStatement > > &statements)
 Transforms a Postgres parse tree into a set of SQL Statements.
string NodetypeToString (duckdb_libpgquery::PGNodeTag type)
idx_t ParamCount () const

Static Public Member Functions

static void SetQueryLocation (ParsedExpression &expr, int query_location)
static void SetQueryLocation (TableRef &ref, int query_location)
template<class T>
static T & PGCast (duckdb_libpgquery::PGNode &node)
template<class T>
static optional_ptr< T > PGPointerCast (void *ptr)

Private Member Functions

void Clear ()
bool InWindowDefinition ()
Transformer & RootTransformer ()
const Transformer & RootTransformer () const
void SetParamCount (idx_t new_count)
void ClearParameters ()
void SetParam (const string &name, idx_t index, PreparedParamType type)
bool GetParam (const string &name, idx_t &index, PreparedParamType type)
void AddPivotEntry (string enum_name, unique_ptr< SelectNode > source, unique_ptr< ParsedExpression > column, unique_ptr< QueryNode > subquery, bool has_parameters)
unique_ptr< SQLStatement > GenerateCreateEnumStmt (unique_ptr< CreatePivotEntry > entry)
bool HasPivotEntries ()
idx_t PivotEntryCount ()
vector< unique_ptr< CreatePivotEntry > > & GetPivotEntries ()
void PivotEntryCheck (const string &type)
void ExtractCTEsRecursive (CommonTableExpressionMap &cte_map)
unique_ptr< SQLStatement > TransformStatement (duckdb_libpgquery::PGNode &stmt)
 Transforms a Postgres statement into a single SQL statement.
unique_ptr< SQLStatement > TransformStatementInternal (duckdb_libpgquery::PGNode &stmt)
 Transforms a Postgres statement into a single SQL statement.
unique_ptr< SelectStatement > TransformSelectStmt (duckdb_libpgquery::PGSelectStmt &select, bool is_select=true)
 Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a SelectStatement.
unique_ptr< SelectStatement > TransformSelectStmt (duckdb_libpgquery::PGNode &node, bool is_select=true)
unique_ptr< SQLStatement > TransformAlter (duckdb_libpgquery::PGAlterTableStmt &stmt)
 Transform a Postgres T_AlterStmt node into a AlterStatement.
unique_ptr< AlterStatement > TransformAlterDatabase (duckdb_libpgquery::PGAlterDatabaseStmt &stmt)
 Transform a Postgres T_AlterDatabaseStmt node into a AlterStatement.
unique_ptr< AlterStatement > TransformRename (duckdb_libpgquery::PGRenameStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGRenameStmt node into a RenameStatement.
unique_ptr< CreateStatement > TransformCreateTable (duckdb_libpgquery::PGCreateStmt &node)
 Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement.
unique_ptr< CreateStatement > TransformCreateTableAs (duckdb_libpgquery::PGCreateTableAsStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement.
unique_ptr< CreateStatement > TransformCreateSchema (duckdb_libpgquery::PGCreateSchemaStmt &stmt)
 Transform a Postgres node into a CreateStatement.
unique_ptr< CreateStatement > TransformCreateSequence (duckdb_libpgquery::PGCreateSeqStmt &node)
 Transform a Postgres duckdb_libpgquery::T_PGCreateSeqStmt node into a CreateStatement.
unique_ptr< CreateStatement > TransformCreateView (duckdb_libpgquery::PGViewStmt &node)
 Transform a Postgres duckdb_libpgquery::T_PGViewStmt node into a CreateStatement.
unique_ptr< CreateStatement > TransformCreateIndex (duckdb_libpgquery::PGIndexStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGIndexStmt node into CreateStatement.
unique_ptr< CreateStatement > TransformCreateFunction (duckdb_libpgquery::PGCreateFunctionStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGCreateFunctionStmt node into CreateStatement.
unique_ptr< CreateStatement > TransformCreateType (duckdb_libpgquery::PGCreateTypeStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement.
unique_ptr< AlterStatement > TransformCommentOn (duckdb_libpgquery::PGCommentOnStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement.
unique_ptr< AlterStatement > TransformAlterSequence (duckdb_libpgquery::PGAlterSeqStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGAlterSeqStmt node into CreateStatement.
unique_ptr< SQLStatement > TransformDrop (duckdb_libpgquery::PGDropStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGDropStmt node into a Drop[Table,Schema]Statement.
unique_ptr< InsertStatement > TransformInsert (duckdb_libpgquery::PGInsertStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGInsertStmt node into a InsertStatement.
InsertColumnOrder TransformColumnOrder (duckdb_libpgquery::PGInsertColumnOrder insert_column_order)
vector< string > TransformInsertColumns (duckdb_libpgquery::PGList &cols)
unique_ptr< OnConflictInfo > TransformOnConflictClause (duckdb_libpgquery::PGOnConflictClause *node, const string &relname)
 Transform a Postgres duckdb_libpgquery::T_PGOnConflictClause node into a OnConflictInfo.
unique_ptr< OnConflictInfo > DummyOnConflictClause (duckdb_libpgquery::PGOnConflictActionAlias type, const string &relname)
 Transform a ON CONFLICT shorthand into a OnConflictInfo.
unique_ptr< CopyStatement > TransformCopy (duckdb_libpgquery::PGCopyStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGCopyStmt node into a CopyStatement.
void TransformCopyOptions (CopyInfo &info, optional_ptr< duckdb_libpgquery::PGList > options)
void TransformCreateSecretOptions (CreateSecretInfo &info, optional_ptr< duckdb_libpgquery::PGList > options)
void TransformTableOptions (case_insensitive_map_t< unique_ptr< ParsedExpression > > &info, optional_ptr< duckdb_libpgquery::PGList > options, bool throw_if_value=false)
unique_ptr< TransactionStatement > TransformTransaction (duckdb_libpgquery::PGTransactionStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGTransactionStmt node into a TransactionStatement.
unique_ptr< DeleteStatement > TransformDelete (duckdb_libpgquery::PGDeleteStmt &stmt)
 Transform a Postgres T_DeleteStatement node into a DeleteStatement.
unique_ptr< UpdateStatement > TransformUpdate (duckdb_libpgquery::PGUpdateStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGUpdateStmt node into a UpdateStatement.
unique_ptr< UpdateExtensionsStatement > TransformUpdateExtensions (duckdb_libpgquery::PGUpdateExtensionsStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGUpdateExtensionsStmt node into a UpdateExtensionsStatement.
unique_ptr< SQLStatement > TransformPragma (duckdb_libpgquery::PGPragmaStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGPragmaStmt node into a PragmaStatement.
unique_ptr< ExportStatement > TransformExport (duckdb_libpgquery::PGExportStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGExportStmt node into a ExportStatement.
unique_ptr< PragmaStatement > TransformImport (duckdb_libpgquery::PGImportStmt &stmt)
 Transform a Postgres duckdb_libpgquery::T_PGImportStmt node into a PragmaStatement.
unique_ptr< ExplainStatement > TransformExplain (duckdb_libpgquery::PGExplainStmt &stmt)
unique_ptr< SQLStatement > TransformVacuum (duckdb_libpgquery::PGVacuumStmt &stmt)
unique_ptr< QueryNode > TransformShow (duckdb_libpgquery::PGVariableShowStmt &stmt)
unique_ptr< SelectStatement > TransformShowStmt (duckdb_libpgquery::PGVariableShowStmt &stmt)
unique_ptr< QueryNode > TransformShowSelect (duckdb_libpgquery::PGVariableShowSelectStmt &stmt)
unique_ptr< SelectStatement > TransformShowSelectStmt (duckdb_libpgquery::PGVariableShowSelectStmt &stmt)
unique_ptr< AttachStatement > TransformAttach (duckdb_libpgquery::PGAttachStmt &stmt)
unique_ptr< DetachStatement > TransformDetach (duckdb_libpgquery::PGDetachStmt &stmt)
unique_ptr< SetStatement > TransformUse (duckdb_libpgquery::PGUseStmt &stmt)
unique_ptr< SQLStatement > TransformCopyDatabase (duckdb_libpgquery::PGCopyDatabaseStmt &stmt)
unique_ptr< CreateStatement > TransformSecret (duckdb_libpgquery::PGCreateSecretStmt &stmt)
unique_ptr< DropStatement > TransformDropSecret (duckdb_libpgquery::PGDropSecretStmt &stmt)
unique_ptr< PrepareStatement > TransformPrepare (duckdb_libpgquery::PGPrepareStmt &stmt)
unique_ptr< ExecuteStatement > TransformExecute (duckdb_libpgquery::PGExecuteStmt &stmt)
unique_ptr< CallStatement > TransformCall (duckdb_libpgquery::PGCallStmt &stmt)
unique_ptr< DropStatement > TransformDeallocate (duckdb_libpgquery::PGDeallocateStmt &stmt)
unique_ptr< QueryNode > TransformPivotStatement (duckdb_libpgquery::PGSelectStmt &select)
unique_ptr< SQLStatement > CreatePivotStatement (unique_ptr< SQLStatement > statement)
PivotColumn TransformPivotColumn (duckdb_libpgquery::PGPivot &pivot, bool is_pivot)
vector< PivotColumn > TransformPivotList (duckdb_libpgquery::PGList &list, bool is_pivot)
unique_ptr< SQLStatement > TransformMergeInto (duckdb_libpgquery::PGMergeIntoStmt &stmt)
unique_ptr< MergeIntoAction > TransformMergeIntoAction (duckdb_libpgquery::PGMatchAction &action)
unique_ptr< SetStatement > TransformSet (duckdb_libpgquery::PGVariableSetStmt &set)
unique_ptr< SetStatement > TransformSetVariable (duckdb_libpgquery::PGVariableSetStmt &stmt)
unique_ptr< SetStatement > TransformResetVariable (duckdb_libpgquery::PGVariableSetStmt &stmt)
unique_ptr< SQLStatement > TransformCheckpoint (duckdb_libpgquery::PGCheckPointStmt &stmt)
unique_ptr< LoadStatement > TransformLoad (duckdb_libpgquery::PGLoadStmt &stmt)
unique_ptr< QueryNode > TransformSelectNode (duckdb_libpgquery::PGNode &select, bool is_select=true)
 Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a QueryNode.
unique_ptr< QueryNode > TransformSelectNodeInternal (duckdb_libpgquery::PGSelectStmt &select, bool is_select=true)
unique_ptr< QueryNode > TransformSelectInternal (duckdb_libpgquery::PGSelectStmt &select)
void TransformModifiers (duckdb_libpgquery::PGSelectStmt &stmt, QueryNode &node)
bool SetOperationsMatch (duckdb_libpgquery::PGSelectStmt &root, duckdb_libpgquery::PGNode &node)
void TransformSetOperationChildren (duckdb_libpgquery::PGSelectStmt &stmt, SetOperationNode &result)
unique_ptr< ParsedExpression > TransformBoolExpr (duckdb_libpgquery::PGBoolExpr &root)
 Transform a Postgres boolean expression into an Expression.
unique_ptr< ParsedExpression > TransformCase (duckdb_libpgquery::PGCaseExpr &root)
 Transform a Postgres case expression into an Expression.
unique_ptr< ParsedExpression > TransformTypeCast (duckdb_libpgquery::PGTypeCast &root)
 Transform a Postgres type cast into an Expression.
unique_ptr< ParsedExpression > TransformCoalesce (duckdb_libpgquery::PGAExpr &root)
 Transform a Postgres coalesce into an Expression.
unique_ptr< ParsedExpression > TransformColumnRef (duckdb_libpgquery::PGColumnRef &root)
 Transform a Postgres column reference into an Expression.
unique_ptr< ConstantExpression > TransformValue (duckdb_libpgquery::PGValue val)
 Transform a Postgres constant value into an Expression.
unique_ptr< ParsedExpression > TransformAExpr (duckdb_libpgquery::PGAExpr &root)
 Transform a Postgres operator into an Expression.
unique_ptr< ParsedExpression > TransformAExprInternal (duckdb_libpgquery::PGAExpr &root)
unique_ptr< ParsedExpression > TransformExpression (optional_ptr< duckdb_libpgquery::PGNode > node)
 Transform a Postgres abstract expression into an Expression.
unique_ptr< ParsedExpression > TransformExpression (duckdb_libpgquery::PGNode &node)
unique_ptr< ParsedExpression > TransformFuncCall (duckdb_libpgquery::PGFuncCall &root)
 Transform a Postgres function call into an Expression.
unique_ptr< ParsedExpression > TransformInterval (duckdb_libpgquery::PGIntervalConstant &root)
 Transform a Postgres boolean expression into an Expression.
unique_ptr< ParsedExpression > TransformLambda (duckdb_libpgquery::PGLambdaFunction &node)
 Transform a LAMBDA node (e.g., lambda x, y: x + y) into a lambda expression.
unique_ptr< ParsedExpression > TransformSingleArrow (duckdb_libpgquery::PGSingleArrowFunction &node)
 Transform a single arrow operator (e.g., (x, y) -> x + y) into a lambda expression.
unique_ptr< ParsedExpression > TransformArrayAccess (duckdb_libpgquery::PGAIndirection &node)
 Transform a Postgres array access node (e.g. x[1] or x[1:3]).
unique_ptr< ParsedExpression > TransformPositionalReference (duckdb_libpgquery::PGPositionalReference &node)
 Transform a positional reference (e.g. #1).
unique_ptr< ParsedExpression > TransformStarExpression (duckdb_libpgquery::PGAStar &node)
unique_ptr< ParsedExpression > TransformBooleanTest (duckdb_libpgquery::PGBooleanTest &node)
unique_ptr< ParsedExpression > TransformConstant (duckdb_libpgquery::PGAConst &c)
 Transform a Postgres constant value into an Expression.
unique_ptr< ParsedExpression > TransformGroupingFunction (duckdb_libpgquery::PGGroupingFunc &n)
unique_ptr< ParsedExpression > TransformResTarget (duckdb_libpgquery::PGResTarget &root)
unique_ptr< ParsedExpression > TransformNullTest (duckdb_libpgquery::PGNullTest &root)
unique_ptr< ParsedExpression > TransformParamRef (duckdb_libpgquery::PGParamRef &node)
unique_ptr< ParsedExpression > TransformNamedArg (duckdb_libpgquery::PGNamedArgExpr &root)
unique_ptr< ParsedExpression > TransformMultiAssignRef (duckdb_libpgquery::PGMultiAssignRef &root)
 Transform multi assignment reference into an Expression.
unique_ptr< ParsedExpression > TransformSQLValueFunction (duckdb_libpgquery::PGSQLValueFunction &node)
unique_ptr< ParsedExpression > TransformSubquery (duckdb_libpgquery::PGSubLink &root)
unique_ptr< Constraint > TransformConstraint (duckdb_libpgquery::PGConstraint &constraint)
unique_ptr< Constraint > TransformConstraint (duckdb_libpgquery::PGConstraint &constraint, ColumnDefinition &column, idx_t index)
unique_ptr< UpdateSetInfo > TransformUpdateSetInfo (duckdb_libpgquery::PGList *target_list, duckdb_libpgquery::PGNode *where_clause)
vector< unique_ptr< ParsedExpression > > TransformIndexParameters (duckdb_libpgquery::PGList &list, const string &relation_name)
unique_ptr< ParsedExpression > TransformCollateExpr (duckdb_libpgquery::PGCollateClause &collate)
string TransformCollation (optional_ptr< duckdb_libpgquery::PGCollateClause > collate)
ColumnDefinition TransformColumnDefinition (duckdb_libpgquery::PGColumnDef &cdef)
OnCreateConflict TransformOnConflict (duckdb_libpgquery::PGOnCreateConflict conflict)
string TransformAlias (duckdb_libpgquery::PGAlias *root, vector< string > &column_name_alias)
vector< string > TransformStringList (duckdb_libpgquery::PGList *list)
void TransformCTE (duckdb_libpgquery::PGWithClause &de_with_clause, CommonTableExpressionMap &cte_map)
unique_ptr< SelectStatement > TransformRecursiveCTE (duckdb_libpgquery::PGCommonTableExpr &node, CommonTableExpressionInfo &info)
unique_ptr< ParsedExpression > TransformUnaryOperator (const string &op, unique_ptr< ParsedExpression > child)
unique_ptr< ParsedExpression > TransformBinaryOperator (string op, unique_ptr< ParsedExpression > left, unique_ptr< ParsedExpression > right)
unique_ptr< TableRef > TransformTableRefNode (duckdb_libpgquery::PGNode &n)
 Transform a Postgres node into a TableRef.
unique_ptr< TableRef > TransformFrom (optional_ptr< duckdb_libpgquery::PGList > root)
 Transform a Postgres FROM clause into a TableRef.
unique_ptr< TableRef > TransformRangeVar (duckdb_libpgquery::PGRangeVar &root)
 Transform a Postgres table reference into a TableRef.
unique_ptr< TableRef > TransformRangeFunction (duckdb_libpgquery::PGRangeFunction &root)
 Transform a Postgres table-producing function into a TableRef.
unique_ptr< TableRef > TransformJoin (duckdb_libpgquery::PGJoinExpr &root)
 Transform a Postgres join node into a TableRef.
unique_ptr< TableRef > TransformPivot (duckdb_libpgquery::PGPivotExpr &root)
 Transform a Postgres pivot node into a TableRef.
unique_ptr< TableRef > TransformRangeSubselect (duckdb_libpgquery::PGRangeSubselect &root)
 Transform a table producing subquery into a TableRef.
unique_ptr< TableRef > TransformValuesList (duckdb_libpgquery::PGList *list)
 Transform a VALUES list into a set of expressions.
vector< string > TransformUsingClause (duckdb_libpgquery::PGList &usingClause)
 Transform using clause.
QualifiedName TransformQualifiedName (duckdb_libpgquery::PGRangeVar &root)
 Transform a range var into a (schema) qualified name.
unique_ptr< ParsedExpression > TransformTypeExpressionInternal (duckdb_libpgquery::PGTypeName &name)
 Transform a Postgres TypeName string into a LogicalType (non-LIST types).
unique_ptr< ParsedExpression > TransformTypeExpression (duckdb_libpgquery::PGTypeName &name)
LogicalType TransformTypeName (duckdb_libpgquery::PGTypeName &name)
 Transform a Postgres TypeName string into a LogicalType.
bool TransformGroupBy (optional_ptr< duckdb_libpgquery::PGList > group, SelectNode &result)
 Transform a Postgres GROUP BY expression into a list of Expression.
void TransformGroupByNode (duckdb_libpgquery::PGNode &n, GroupingExpressionMap &map, SelectNode &result, vector< GroupingSet > &result_sets)
void AddGroupByExpression (unique_ptr< ParsedExpression > expression, GroupingExpressionMap &map, GroupByNode &result, vector< idx_t > &result_set)
void TransformGroupByExpression (duckdb_libpgquery::PGNode &n, GroupingExpressionMap &map, GroupByNode &result, vector< idx_t > &result_set)
bool TransformOrderBy (duckdb_libpgquery::PGList *order, vector< OrderByNode > &result)
 Transform a Postgres ORDER BY expression into an OrderByDescription.
unique_ptr< ParsedExpression > TransformInExpression (const string &name, duckdb_libpgquery::PGAExpr &root)
 Transform to a IN or NOT IN expression.
void TransformExpressionList (duckdb_libpgquery::PGList &list, vector< unique_ptr< ParsedExpression > > &result)
 Transform a Postgres SELECT clause into a list of Expressions.
void TransformWindowDef (duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr, const char *window_name=nullptr)
 Transform a Postgres PARTITION BY/ORDER BY specification into lists of expressions.
void TransformWindowFrame (duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr)
 Transform a Postgres window frame specification into frame expressions.
unique_ptr< SampleOptions > TransformSampleOptions (optional_ptr< duckdb_libpgquery::PGNode > options)
bool ExpressionIsEmptyStar (ParsedExpression &expr)
 Returns true if an expression is only a star (i.e. "*", without any other decorators).
OnEntryNotFound TransformOnEntryNotFound (bool missing_ok)
Vector PGListToVector (optional_ptr< duckdb_libpgquery::PGList > column_list, idx_t &size)
vector< string > TransformConflictTarget (duckdb_libpgquery::PGList &list)
unique_ptr< MacroFunction > TransformMacroFunction (duckdb_libpgquery::PGFunctionDefinition &function)
vector< string > TransformNameList (duckdb_libpgquery::PGList &list)
void InitializeStackCheck ()
StackChecker< Transformer > StackCheck (idx_t extra_stack=1)

Private Attributes

optional_ptr< Transformer > parent
ParserOptions & options
 Parser options.
idx_t prepared_statement_parameter_index = 0
 The current prepared statement parameter index.
case_insensitive_map_t< idx_t > named_param_map
 Map from named parameter to parameter index;.
PreparedParamType last_param_type = PreparedParamType::INVALID
 Last parameter type.
case_insensitive_map_t< duckdb_libpgquery::PGWindowDef * > window_clauses
 Holds window expressions defined by name. We need those when transforming the expressions referring to them.
vector< unique_ptr< CreatePivotEntry > > pivot_entries
 The set of pivot entries to create.
vector< reference< CommonTableExpressionMap > > stored_cte_map
 Sets of stored CTEs, if any.
bool in_window_definition = false
 Whether or not we are currently binding a window definition.
idx_t stack_depth
 Current stack depth.

Friends

class StackChecker< Transformer >

Detailed Description

The transformer class is responsible for transforming the internal Postgres parser representation into the DuckDB representation

Constructor & Destructor Documentation

◆ Transformer() [1/2]

duckdb::Transformer::Transformer ( ParserOptions & options)
explicit
358302 : parent(nullptr), options(options), stack_depth(DConstants::INVALID_INDEX) {
358303}
ParserOptions & options
Parser options.
Definition duckdb.cpp:346486
idx_t stack_depth
Current stack depth.
Definition duckdb.cpp:346802
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117

◆ Transformer() [2/2]

duckdb::Transformer::Transformer ( Transformer & parent)
358306 : parent(&parent), options(parent.options), stack_depth(DConstants::INVALID_INDEX) {
358307}

◆ ~Transformer()

duckdb::Transformer::~Transformer ( )
358309 {
358310}

Member Function Documentation

◆ TransformParseTree()

bool duckdb::Transformer::TransformParseTree ( duckdb_libpgquery::PGList * tree,
vector< unique_ptr< SQLStatement > > & statements )

Transforms a Postgres parse tree into a set of SQL Statements.

358317 {
358318 InitializeStackCheck();
358319 for (auto entry = tree->head; entry != nullptr; entry = entry->next) {
358320 Clear();
358321 auto n = PGPointerCast<duckdb_libpgquery::PGNode>(entry->data.ptr_value);
358322 auto stmt = TransformStatement(*n);
358323 D_ASSERT(stmt);
358324 if (HasPivotEntries()) {
358325 stmt = CreatePivotStatement(std::move(stmt));
358326 }
358327 statements.push_back(std::move(stmt));
358328 }
358329 return true;
358330}
unique_ptr< SQLStatement > TransformStatement(duckdb_libpgquery::PGNode &stmt)
Transforms a Postgres statement into a single SQL statement.
Definition duckdb.cpp:358347
Here is the call graph for this function:
Here is the caller graph for this function:

◆ NodetypeToString()

std::string duckdb::Transformer::NodetypeToString ( duckdb_libpgquery::PGNodeTag type)
353238 { // LCOV_EXCL_START
353239 switch (type) {
353240 case duckdb_libpgquery::T_PGInvalid:
353241 return "T_Invalid";
353242 case duckdb_libpgquery::T_PGIndexInfo:
353243 return "T_IndexInfo";
353244 case duckdb_libpgquery::T_PGExprContext:
353245 return "T_ExprContext";
353246 case duckdb_libpgquery::T_PGProjectionInfo:
353247 return "T_ProjectionInfo";
353248 case duckdb_libpgquery::T_PGJunkFilter:
353249 return "T_JunkFilter";
353250 case duckdb_libpgquery::T_PGResultRelInfo:
353251 return "T_ResultRelInfo";
353252 case duckdb_libpgquery::T_PGEState:
353253 return "T_EState";
353254 case duckdb_libpgquery::T_PGTupleTableSlot:
353255 return "T_TupleTableSlot";
353256 case duckdb_libpgquery::T_PGPlan:
353257 return "T_Plan";
353258 case duckdb_libpgquery::T_PGResult:
353259 return "T_Result";
353260 case duckdb_libpgquery::T_PGProjectSet:
353261 return "T_ProjectSet";
353262 case duckdb_libpgquery::T_PGModifyTable:
353263 return "T_ModifyTable";
353264 case duckdb_libpgquery::T_PGAppend:
353265 return "T_Append";
353266 case duckdb_libpgquery::T_PGMergeAppend:
353267 return "T_MergeAppend";
353268 case duckdb_libpgquery::T_PGRecursiveUnion:
353269 return "T_RecursiveUnion";
353270 case duckdb_libpgquery::T_PGBitmapAnd:
353271 return "T_BitmapAnd";
353272 case duckdb_libpgquery::T_PGBitmapOr:
353273 return "T_BitmapOr";
353274 case duckdb_libpgquery::T_PGScan:
353275 return "T_Scan";
353276 case duckdb_libpgquery::T_PGSeqScan:
353277 return "T_SeqScan";
353278 case duckdb_libpgquery::T_PGSampleScan:
353279 return "T_SampleScan";
353280 case duckdb_libpgquery::T_PGIndexScan:
353281 return "T_IndexScan";
353282 case duckdb_libpgquery::T_PGIndexOnlyScan:
353283 return "T_IndexOnlyScan";
353284 case duckdb_libpgquery::T_PGBitmapIndexScan:
353285 return "T_BitmapIndexScan";
353286 case duckdb_libpgquery::T_PGBitmapHeapScan:
353287 return "T_BitmapHeapScan";
353288 case duckdb_libpgquery::T_PGTidScan:
353289 return "T_TidScan";
353290 case duckdb_libpgquery::T_PGSubqueryScan:
353291 return "T_SubqueryScan";
353292 case duckdb_libpgquery::T_PGFunctionScan:
353293 return "T_FunctionScan";
353294 case duckdb_libpgquery::T_PGValuesScan:
353295 return "T_ValuesScan";
353296 case duckdb_libpgquery::T_PGTableFuncScan:
353297 return "T_TableFuncScan";
353298 case duckdb_libpgquery::T_PGCteScan:
353299 return "T_CteScan";
353300 case duckdb_libpgquery::T_PGNamedTuplestoreScan:
353301 return "T_NamedTuplestoreScan";
353302 case duckdb_libpgquery::T_PGWorkTableScan:
353303 return "T_WorkTableScan";
353304 case duckdb_libpgquery::T_PGForeignScan:
353305 return "T_ForeignScan";
353306 case duckdb_libpgquery::T_PGCustomScan:
353307 return "T_CustomScan";
353308 case duckdb_libpgquery::T_PGJoin:
353309 return "T_Join";
353310 case duckdb_libpgquery::T_PGNestLoop:
353311 return "T_NestLoop";
353312 case duckdb_libpgquery::T_PGMergeJoin:
353313 return "T_MergeJoin";
353314 case duckdb_libpgquery::T_PGHashJoin:
353315 return "T_HashJoin";
353316 case duckdb_libpgquery::T_PGMaterial:
353317 return "T_Material";
353318 case duckdb_libpgquery::T_PGSort:
353319 return "T_Sort";
353320 case duckdb_libpgquery::T_PGGroup:
353321 return "T_Group";
353322 case duckdb_libpgquery::T_PGAgg:
353323 return "T_Agg";
353324 case duckdb_libpgquery::T_PGWindowAgg:
353325 return "T_WindowAgg";
353326 case duckdb_libpgquery::T_PGUnique:
353327 return "T_Unique";
353328 case duckdb_libpgquery::T_PGGather:
353329 return "T_Gather";
353330 case duckdb_libpgquery::T_PGGatherMerge:
353331 return "T_GatherMerge";
353332 case duckdb_libpgquery::T_PGHash:
353333 return "T_Hash";
353334 case duckdb_libpgquery::T_PGSetOp:
353335 return "T_SetOp";
353336 case duckdb_libpgquery::T_PGLockRows:
353337 return "T_LockRows";
353338 case duckdb_libpgquery::T_PGLimit:
353339 return "T_Limit";
353340 case duckdb_libpgquery::T_PGNestLoopParam:
353341 return "T_NestLoopParam";
353342 case duckdb_libpgquery::T_PGPlanRowMark:
353343 return "T_PlanRowMark";
353344 case duckdb_libpgquery::T_PGPlanInvalItem:
353345 return "T_PlanInvalItem";
353346 case duckdb_libpgquery::T_PGPlanState:
353347 return "T_PlanState";
353348 case duckdb_libpgquery::T_PGResultState:
353349 return "T_ResultState";
353350 case duckdb_libpgquery::T_PGProjectSetState:
353351 return "T_ProjectSetState";
353352 case duckdb_libpgquery::T_PGModifyTableState:
353353 return "T_ModifyTableState";
353354 case duckdb_libpgquery::T_PGAppendState:
353355 return "T_AppendState";
353356 case duckdb_libpgquery::T_PGMergeAppendState:
353357 return "T_MergeAppendState";
353358 case duckdb_libpgquery::T_PGRecursiveUnionState:
353359 return "T_RecursiveUnionState";
353360 case duckdb_libpgquery::T_PGBitmapAndState:
353361 return "T_BitmapAndState";
353362 case duckdb_libpgquery::T_PGBitmapOrState:
353363 return "T_BitmapOrState";
353364 case duckdb_libpgquery::T_PGScanState:
353365 return "T_ScanState";
353366 case duckdb_libpgquery::T_PGSeqScanState:
353367 return "T_SeqScanState";
353368 case duckdb_libpgquery::T_PGSampleScanState:
353369 return "T_SampleScanState";
353370 case duckdb_libpgquery::T_PGIndexScanState:
353371 return "T_IndexScanState";
353372 case duckdb_libpgquery::T_PGIndexOnlyScanState:
353373 return "T_IndexOnlyScanState";
353374 case duckdb_libpgquery::T_PGBitmapIndexScanState:
353375 return "T_BitmapIndexScanState";
353376 case duckdb_libpgquery::T_PGBitmapHeapScanState:
353377 return "T_BitmapHeapScanState";
353378 case duckdb_libpgquery::T_PGTidScanState:
353379 return "T_TidScanState";
353380 case duckdb_libpgquery::T_PGSubqueryScanState:
353381 return "T_SubqueryScanState";
353382 case duckdb_libpgquery::T_PGFunctionScanState:
353383 return "T_FunctionScanState";
353384 case duckdb_libpgquery::T_PGTableFuncScanState:
353385 return "T_TableFuncScanState";
353386 case duckdb_libpgquery::T_PGValuesScanState:
353387 return "T_ValuesScanState";
353388 case duckdb_libpgquery::T_PGCteScanState:
353389 return "T_CteScanState";
353390 case duckdb_libpgquery::T_PGNamedTuplestoreScanState:
353391 return "T_NamedTuplestoreScanState";
353392 case duckdb_libpgquery::T_PGWorkTableScanState:
353393 return "T_WorkTableScanState";
353394 case duckdb_libpgquery::T_PGForeignScanState:
353395 return "T_ForeignScanState";
353396 case duckdb_libpgquery::T_PGCustomScanState:
353397 return "T_CustomScanState";
353398 case duckdb_libpgquery::T_PGJoinState:
353399 return "T_JoinState";
353400 case duckdb_libpgquery::T_PGNestLoopState:
353401 return "T_NestLoopState";
353402 case duckdb_libpgquery::T_PGMergeJoinState:
353403 return "T_MergeJoinState";
353404 case duckdb_libpgquery::T_PGHashJoinState:
353405 return "T_HashJoinState";
353406 case duckdb_libpgquery::T_PGMaterialState:
353407 return "T_MaterialState";
353408 case duckdb_libpgquery::T_PGSortState:
353409 return "T_SortState";
353410 case duckdb_libpgquery::T_PGGroupState:
353411 return "T_GroupState";
353412 case duckdb_libpgquery::T_PGAggState:
353413 return "T_AggState";
353414 case duckdb_libpgquery::T_PGWindowAggState:
353415 return "T_WindowAggState";
353416 case duckdb_libpgquery::T_PGUniqueState:
353417 return "T_UniqueState";
353418 case duckdb_libpgquery::T_PGGatherState:
353419 return "T_GatherState";
353420 case duckdb_libpgquery::T_PGGatherMergeState:
353421 return "T_GatherMergeState";
353422 case duckdb_libpgquery::T_PGHashState:
353423 return "T_HashState";
353424 case duckdb_libpgquery::T_PGSetOpState:
353425 return "T_SetOpState";
353426 case duckdb_libpgquery::T_PGLockRowsState:
353427 return "T_LockRowsState";
353428 case duckdb_libpgquery::T_PGLimitState:
353429 return "T_LimitState";
353430 case duckdb_libpgquery::T_PGAlias:
353431 return "T_Alias";
353432 case duckdb_libpgquery::T_PGRangeVar:
353433 return "T_RangeVar";
353434 case duckdb_libpgquery::T_PGTableFunc:
353435 return "T_TableFunc";
353436 case duckdb_libpgquery::T_PGExpr:
353437 return "T_Expr";
353438 case duckdb_libpgquery::T_PGVar:
353439 return "T_Var";
353440 case duckdb_libpgquery::T_PGConst:
353441 return "T_Const";
353442 case duckdb_libpgquery::T_PGParam:
353443 return "T_Param";
353444 case duckdb_libpgquery::T_PGAggref:
353445 return "T_Aggref";
353446 case duckdb_libpgquery::T_PGGroupingFunc:
353447 return "T_GroupingFunc";
353448 case duckdb_libpgquery::T_PGWindowFunc:
353449 return "T_WindowFunc";
353450 case duckdb_libpgquery::T_PGArrayRef:
353451 return "T_ArrayRef";
353452 case duckdb_libpgquery::T_PGFuncExpr:
353453 return "T_FuncExpr";
353454 case duckdb_libpgquery::T_PGNamedArgExpr:
353455 return "T_NamedArgExpr";
353456 case duckdb_libpgquery::T_PGOpExpr:
353457 return "T_OpExpr";
353458 case duckdb_libpgquery::T_PGDistinctExpr:
353459 return "T_DistinctExpr";
353460 case duckdb_libpgquery::T_PGNullIfExpr:
353461 return "T_NullIfExpr";
353462 case duckdb_libpgquery::T_PGScalarArrayOpExpr:
353463 return "T_ScalarArrayOpExpr";
353464 case duckdb_libpgquery::T_PGBoolExpr:
353465 return "T_BoolExpr";
353466 case duckdb_libpgquery::T_PGSubLink:
353467 return "T_SubLink";
353468 case duckdb_libpgquery::T_PGSubPlan:
353469 return "T_SubPlan";
353470 case duckdb_libpgquery::T_PGAlternativeSubPlan:
353471 return "T_AlternativeSubPlan";
353472 case duckdb_libpgquery::T_PGFieldSelect:
353473 return "T_FieldSelect";
353474 case duckdb_libpgquery::T_PGFieldStore:
353475 return "T_FieldStore";
353476 case duckdb_libpgquery::T_PGRelabelType:
353477 return "T_RelabelType";
353478 case duckdb_libpgquery::T_PGCoerceViaIO:
353479 return "T_CoerceViaIO";
353480 case duckdb_libpgquery::T_PGArrayCoerceExpr:
353481 return "T_ArrayCoerceExpr";
353482 case duckdb_libpgquery::T_PGConvertRowtypeExpr:
353483 return "T_ConvertRowtypeExpr";
353484 case duckdb_libpgquery::T_PGCollateExpr:
353485 return "T_CollateExpr";
353486 case duckdb_libpgquery::T_PGCaseExpr:
353487 return "T_CaseExpr";
353488 case duckdb_libpgquery::T_PGCaseWhen:
353489 return "T_CaseWhen";
353490 case duckdb_libpgquery::T_PGCaseTestExpr:
353491 return "T_CaseTestExpr";
353492 case duckdb_libpgquery::T_PGArrayExpr:
353493 return "T_ArrayExpr";
353494 case duckdb_libpgquery::T_PGRowExpr:
353495 return "T_RowExpr";
353496 case duckdb_libpgquery::T_PGRowCompareExpr:
353497 return "T_RowCompareExpr";
353498 case duckdb_libpgquery::T_PGCoalesceExpr:
353499 return "T_CoalesceExpr";
353500 case duckdb_libpgquery::T_PGMinMaxExpr:
353501 return "T_MinMaxExpr";
353502 case duckdb_libpgquery::T_PGSQLValueFunction:
353503 return "T_SQLValueFunction";
353504 case duckdb_libpgquery::T_PGXmlExpr:
353505 return "T_XmlExpr";
353506 case duckdb_libpgquery::T_PGNullTest:
353507 return "T_NullTest";
353508 case duckdb_libpgquery::T_PGBooleanTest:
353509 return "T_BooleanTest";
353510 case duckdb_libpgquery::T_PGCoerceToDomain:
353511 return "T_CoerceToDomain";
353512 case duckdb_libpgquery::T_PGCoerceToDomainValue:
353513 return "T_CoerceToDomainValue";
353514 case duckdb_libpgquery::T_PGSetToDefault:
353515 return "T_SetToDefault";
353516 case duckdb_libpgquery::T_PGCurrentOfExpr:
353517 return "T_CurrentOfExpr";
353518 case duckdb_libpgquery::T_PGNextValueExpr:
353519 return "T_NextValueExpr";
353520 case duckdb_libpgquery::T_PGInferenceElem:
353521 return "T_InferenceElem";
353522 case duckdb_libpgquery::T_PGTargetEntry:
353523 return "T_TargetEntry";
353524 case duckdb_libpgquery::T_PGRangeTblRef:
353525 return "T_RangeTblRef";
353526 case duckdb_libpgquery::T_PGJoinExpr:
353527 return "T_JoinExpr";
353528 case duckdb_libpgquery::T_PGFromExpr:
353529 return "T_FromExpr";
353530 case duckdb_libpgquery::T_PGOnConflictExpr:
353531 return "T_OnConflictExpr";
353532 case duckdb_libpgquery::T_PGIntoClause:
353533 return "T_IntoClause";
353534 case duckdb_libpgquery::T_PGExprState:
353535 return "T_ExprState";
353536 case duckdb_libpgquery::T_PGAggrefExprState:
353537 return "T_AggrefExprState";
353538 case duckdb_libpgquery::T_PGWindowFuncExprState:
353539 return "T_WindowFuncExprState";
353540 case duckdb_libpgquery::T_PGSetExprState:
353541 return "T_SetExprState";
353542 case duckdb_libpgquery::T_PGSubPlanState:
353543 return "T_SubPlanState";
353544 case duckdb_libpgquery::T_PGAlternativeSubPlanState:
353545 return "T_AlternativeSubPlanState";
353546 case duckdb_libpgquery::T_PGDomainConstraintState:
353547 return "T_DomainConstraintState";
353548 case duckdb_libpgquery::T_PGPlannerInfo:
353549 return "T_PlannerInfo";
353550 case duckdb_libpgquery::T_PGPlannerGlobal:
353551 return "T_PlannerGlobal";
353552 case duckdb_libpgquery::T_PGRelOptInfo:
353553 return "T_RelOptInfo";
353554 case duckdb_libpgquery::T_PGIndexOptInfo:
353555 return "T_IndexOptInfo";
353556 case duckdb_libpgquery::T_PGForeignKeyOptInfo:
353557 return "T_ForeignKeyOptInfo";
353558 case duckdb_libpgquery::T_PGParamPathInfo:
353559 return "T_ParamPathInfo";
353560 case duckdb_libpgquery::T_PGPath:
353561 return "T_Path";
353562 case duckdb_libpgquery::T_PGIndexPath:
353563 return "T_IndexPath";
353564 case duckdb_libpgquery::T_PGBitmapHeapPath:
353565 return "T_BitmapHeapPath";
353566 case duckdb_libpgquery::T_PGBitmapAndPath:
353567 return "T_BitmapAndPath";
353568 case duckdb_libpgquery::T_PGBitmapOrPath:
353569 return "T_BitmapOrPath";
353570 case duckdb_libpgquery::T_PGTidPath:
353571 return "T_TidPath";
353572 case duckdb_libpgquery::T_PGSubqueryScanPath:
353573 return "T_SubqueryScanPath";
353574 case duckdb_libpgquery::T_PGForeignPath:
353575 return "T_ForeignPath";
353576 case duckdb_libpgquery::T_PGCustomPath:
353577 return "T_CustomPath";
353578 case duckdb_libpgquery::T_PGNestPath:
353579 return "T_NestPath";
353580 case duckdb_libpgquery::T_PGMergePath:
353581 return "T_MergePath";
353582 case duckdb_libpgquery::T_PGHashPath:
353583 return "T_HashPath";
353584 case duckdb_libpgquery::T_PGAppendPath:
353585 return "T_AppendPath";
353586 case duckdb_libpgquery::T_PGMergeAppendPath:
353587 return "T_MergeAppendPath";
353588 case duckdb_libpgquery::T_PGResultPath:
353589 return "T_ResultPath";
353590 case duckdb_libpgquery::T_PGMaterialPath:
353591 return "T_MaterialPath";
353592 case duckdb_libpgquery::T_PGUniquePath:
353593 return "T_UniquePath";
353594 case duckdb_libpgquery::T_PGGatherPath:
353595 return "T_GatherPath";
353596 case duckdb_libpgquery::T_PGGatherMergePath:
353597 return "T_GatherMergePath";
353598 case duckdb_libpgquery::T_PGProjectionPath:
353599 return "T_ProjectionPath";
353600 case duckdb_libpgquery::T_PGProjectSetPath:
353601 return "T_ProjectSetPath";
353602 case duckdb_libpgquery::T_PGSortPath:
353603 return "T_SortPath";
353604 case duckdb_libpgquery::T_PGGroupPath:
353605 return "T_GroupPath";
353606 case duckdb_libpgquery::T_PGUpperUniquePath:
353607 return "T_UpperUniquePath";
353608 case duckdb_libpgquery::T_PGAggPath:
353609 return "T_AggPath";
353610 case duckdb_libpgquery::T_PGGroupingSetsPath:
353611 return "T_GroupingSetsPath";
353612 case duckdb_libpgquery::T_PGMinMaxAggPath:
353613 return "T_MinMaxAggPath";
353614 case duckdb_libpgquery::T_PGWindowAggPath:
353615 return "T_WindowAggPath";
353616 case duckdb_libpgquery::T_PGSetOpPath:
353617 return "T_SetOpPath";
353618 case duckdb_libpgquery::T_PGRecursiveUnionPath:
353619 return "T_RecursiveUnionPath";
353620 case duckdb_libpgquery::T_PGLockRowsPath:
353621 return "T_LockRowsPath";
353622 case duckdb_libpgquery::T_PGModifyTablePath:
353623 return "T_ModifyTablePath";
353624 case duckdb_libpgquery::T_PGLimitPath:
353625 return "T_LimitPath";
353626 case duckdb_libpgquery::T_PGEquivalenceClass:
353627 return "T_EquivalenceClass";
353628 case duckdb_libpgquery::T_PGEquivalenceMember:
353629 return "T_EquivalenceMember";
353630 case duckdb_libpgquery::T_PGPathKey:
353631 return "T_PathKey";
353632 case duckdb_libpgquery::T_PGPathTarget:
353633 return "T_PathTarget";
353634 case duckdb_libpgquery::T_PGRestrictInfo:
353635 return "T_RestrictInfo";
353636 case duckdb_libpgquery::T_PGPlaceHolderVar:
353637 return "T_PlaceHolderVar";
353638 case duckdb_libpgquery::T_PGSpecialJoinInfo:
353639 return "T_SpecialJoinInfo";
353640 case duckdb_libpgquery::T_PGAppendRelInfo:
353641 return "T_AppendRelInfo";
353642 case duckdb_libpgquery::T_PGPartitionedChildRelInfo:
353643 return "T_PartitionedChildRelInfo";
353644 case duckdb_libpgquery::T_PGPlaceHolderInfo:
353645 return "T_PlaceHolderInfo";
353646 case duckdb_libpgquery::T_PGMinMaxAggInfo:
353647 return "T_MinMaxAggInfo";
353648 case duckdb_libpgquery::T_PGPlannerParamItem:
353649 return "T_PlannerParamItem";
353650 case duckdb_libpgquery::T_PGRollupData:
353651 return "T_RollupData";
353652 case duckdb_libpgquery::T_PGGroupingSetData:
353653 return "T_GroupingSetData";
353654 case duckdb_libpgquery::T_PGStatisticExtInfo:
353655 return "T_StatisticExtInfo";
353656 case duckdb_libpgquery::T_PGMemoryContext:
353657 return "T_MemoryContext";
353658 case duckdb_libpgquery::T_PGAllocSetContext:
353659 return "T_AllocSetContext";
353660 case duckdb_libpgquery::T_PGSlabContext:
353661 return "T_SlabContext";
353662 case duckdb_libpgquery::T_PGValue:
353663 return "T_Value";
353664 case duckdb_libpgquery::T_PGInteger:
353665 return "T_Integer";
353666 case duckdb_libpgquery::T_PGFloat:
353667 return "T_Float";
353668 case duckdb_libpgquery::T_PGString:
353669 return "T_String";
353670 case duckdb_libpgquery::T_PGBitString:
353671 return "T_BitString";
353672 case duckdb_libpgquery::T_PGNull:
353673 return "T_Null";
353674 case duckdb_libpgquery::T_PGList:
353675 return "T_List";
353676 case duckdb_libpgquery::T_PGIntList:
353677 return "T_IntList";
353678 case duckdb_libpgquery::T_PGOidList:
353679 return "T_OidList";
353680 case duckdb_libpgquery::T_PGExtensibleNode:
353681 return "T_ExtensibleNode";
353682 case duckdb_libpgquery::T_PGRawStmt:
353683 return "T_RawStmt";
353684 case duckdb_libpgquery::T_PGQuery:
353685 return "T_Query";
353686 case duckdb_libpgquery::T_PGPlannedStmt:
353687 return "T_PlannedStmt";
353688 case duckdb_libpgquery::T_PGInsertStmt:
353689 return "T_InsertStmt";
353690 case duckdb_libpgquery::T_PGDeleteStmt:
353691 return "T_DeleteStmt";
353692 case duckdb_libpgquery::T_PGUpdateStmt:
353693 return "T_UpdateStmt";
353694 case duckdb_libpgquery::T_PGUpdateExtensionsStmt:
353695 return "T_UpdateExtensionsStmt";
353696 case duckdb_libpgquery::T_PGSelectStmt:
353697 return "T_SelectStmt";
353698 case duckdb_libpgquery::T_PGAlterTableStmt:
353699 return "T_AlterTableStmt";
353700 case duckdb_libpgquery::T_PGAlterTableCmd:
353701 return "T_AlterTableCmd";
353702 case duckdb_libpgquery::T_PGAlterDomainStmt:
353703 return "T_AlterDomainStmt";
353704 case duckdb_libpgquery::T_PGSetOperationStmt:
353705 return "T_SetOperationStmt";
353706 case duckdb_libpgquery::T_PGGrantStmt:
353707 return "T_GrantStmt";
353708 case duckdb_libpgquery::T_PGGrantRoleStmt:
353709 return "T_GrantRoleStmt";
353710 case duckdb_libpgquery::T_PGAlterDefaultPrivilegesStmt:
353711 return "T_AlterDefaultPrivilegesStmt";
353712 case duckdb_libpgquery::T_PGClosePortalStmt:
353713 return "T_ClosePortalStmt";
353714 case duckdb_libpgquery::T_PGClusterStmt:
353715 return "T_ClusterStmt";
353716 case duckdb_libpgquery::T_PGCommentOnStmt:
353717 return "T_CommentOnStmt";
353718 case duckdb_libpgquery::T_PGCopyStmt:
353719 return "T_CopyStmt";
353720 case duckdb_libpgquery::T_PGCreateStmt:
353721 return "T_CreateStmt";
353722 case duckdb_libpgquery::T_PGDefineStmt:
353723 return "T_DefineStmt";
353724 case duckdb_libpgquery::T_PGDropStmt:
353725 return "T_DropStmt";
353726 case duckdb_libpgquery::T_PGTruncateStmt:
353727 return "T_TruncateStmt";
353728 case duckdb_libpgquery::T_PGCommentStmt:
353729 return "T_CommentStmt";
353730 case duckdb_libpgquery::T_PGFetchStmt:
353731 return "T_FetchStmt";
353732 case duckdb_libpgquery::T_PGIndexStmt:
353733 return "T_IndexStmt";
353734 case duckdb_libpgquery::T_PGCreateFunctionStmt:
353735 return "T_CreateFunctionStmt";
353736 case duckdb_libpgquery::T_PGAlterFunctionStmt:
353737 return "T_AlterFunctionStmt";
353738 case duckdb_libpgquery::T_PGDoStmt:
353739 return "T_DoStmt";
353740 case duckdb_libpgquery::T_PGRenameStmt:
353741 return "T_RenameStmt";
353742 case duckdb_libpgquery::T_PGRuleStmt:
353743 return "T_RuleStmt";
353744 case duckdb_libpgquery::T_PGNotifyStmt:
353745 return "T_NotifyStmt";
353746 case duckdb_libpgquery::T_PGListenStmt:
353747 return "T_ListenStmt";
353748 case duckdb_libpgquery::T_PGUnlistenStmt:
353749 return "T_UnlistenStmt";
353750 case duckdb_libpgquery::T_PGTransactionStmt:
353751 return "T_TransactionStmt";
353752 case duckdb_libpgquery::T_PGViewStmt:
353753 return "T_ViewStmt";
353754 case duckdb_libpgquery::T_PGLoadStmt:
353755 return "T_LoadStmt";
353756 case duckdb_libpgquery::T_PGCreateDomainStmt:
353757 return "T_CreateDomainStmt";
353758 case duckdb_libpgquery::T_PGCreatedbStmt:
353759 return "T_CreatedbStmt";
353760 case duckdb_libpgquery::T_PGDropdbStmt:
353761 return "T_DropdbStmt";
353762 case duckdb_libpgquery::T_PGVacuumStmt:
353763 return "T_VacuumStmt";
353764 case duckdb_libpgquery::T_PGExplainStmt:
353765 return "T_ExplainStmt";
353766 case duckdb_libpgquery::T_PGCreateTableAsStmt:
353767 return "T_CreateTableAsStmt";
353768 case duckdb_libpgquery::T_PGCreateSeqStmt:
353769 return "T_CreateSeqStmt";
353770 case duckdb_libpgquery::T_PGAlterSeqStmt:
353771 return "T_AlterSeqStmt";
353772 case duckdb_libpgquery::T_PGVariableSetStmt:
353773 return "T_VariableSetStmt";
353774 case duckdb_libpgquery::T_PGVariableShowStmt:
353775 return "T_VariableShowStmt";
353776 case duckdb_libpgquery::T_PGVariableShowSelectStmt:
353777 return "T_VariableShowSelectStmt";
353778 case duckdb_libpgquery::T_PGDiscardStmt:
353779 return "T_DiscardStmt";
353780 case duckdb_libpgquery::T_PGCreateTrigStmt:
353781 return "T_CreateTrigStmt";
353782 case duckdb_libpgquery::T_PGCreatePLangStmt:
353783 return "T_CreatePLangStmt";
353784 case duckdb_libpgquery::T_PGCreateRoleStmt:
353785 return "T_CreateRoleStmt";
353786 case duckdb_libpgquery::T_PGAlterRoleStmt:
353787 return "T_AlterRoleStmt";
353788 case duckdb_libpgquery::T_PGDropRoleStmt:
353789 return "T_DropRoleStmt";
353790 case duckdb_libpgquery::T_PGLockStmt:
353791 return "T_LockStmt";
353792 case duckdb_libpgquery::T_PGConstraintsSetStmt:
353793 return "T_ConstraintsSetStmt";
353794 case duckdb_libpgquery::T_PGReindexStmt:
353795 return "T_ReindexStmt";
353796 case duckdb_libpgquery::T_PGCheckPointStmt:
353797 return "T_CheckPointStmt";
353798 case duckdb_libpgquery::T_PGCreateSchemaStmt:
353799 return "T_CreateSchemaStmt";
353800 case duckdb_libpgquery::T_PGAlterDatabaseStmt:
353801 return "T_AlterDatabaseStmt";
353802 case duckdb_libpgquery::T_PGAlterDatabaseSetStmt:
353803 return "T_AlterDatabaseSetStmt";
353804 case duckdb_libpgquery::T_PGAlterRoleSetStmt:
353805 return "T_AlterRoleSetStmt";
353806 case duckdb_libpgquery::T_PGCreateConversionStmt:
353807 return "T_CreateConversionStmt";
353808 case duckdb_libpgquery::T_PGCreateCastStmt:
353809 return "T_CreateCastStmt";
353810 case duckdb_libpgquery::T_PGCreateOpClassStmt:
353811 return "T_CreateOpClassStmt";
353812 case duckdb_libpgquery::T_PGCreateOpFamilyStmt:
353813 return "T_CreateOpFamilyStmt";
353814 case duckdb_libpgquery::T_PGAlterOpFamilyStmt:
353815 return "T_AlterOpFamilyStmt";
353816 case duckdb_libpgquery::T_PGPrepareStmt:
353817 return "T_PrepareStmt";
353818 case duckdb_libpgquery::T_PGExecuteStmt:
353819 return "T_ExecuteStmt";
353820 case duckdb_libpgquery::T_PGCallStmt:
353821 return "T_CallStmt";
353822 case duckdb_libpgquery::T_PGDeallocateStmt:
353823 return "T_DeallocateStmt";
353824 case duckdb_libpgquery::T_PGDeclareCursorStmt:
353825 return "T_DeclareCursorStmt";
353826 case duckdb_libpgquery::T_PGCreateTableSpaceStmt:
353827 return "T_CreateTableSpaceStmt";
353828 case duckdb_libpgquery::T_PGDropTableSpaceStmt:
353829 return "T_DropTableSpaceStmt";
353830 case duckdb_libpgquery::T_PGAlterObjectDependsStmt:
353831 return "T_AlterObjectDependsStmt";
353832 case duckdb_libpgquery::T_PGAlterObjectSchemaStmt:
353833 return "T_AlterObjectSchemaStmt";
353834 case duckdb_libpgquery::T_PGAlterOwnerStmt:
353835 return "T_AlterOwnerStmt";
353836 case duckdb_libpgquery::T_PGAlterOperatorStmt:
353837 return "T_AlterOperatorStmt";
353838 case duckdb_libpgquery::T_PGDropOwnedStmt:
353839 return "T_DropOwnedStmt";
353840 case duckdb_libpgquery::T_PGReassignOwnedStmt:
353841 return "T_ReassignOwnedStmt";
353842 case duckdb_libpgquery::T_PGCompositeTypeStmt:
353843 return "T_CompositeTypeStmt";
353844 case duckdb_libpgquery::T_PGCreateTypeStmt:
353845 return "T_CreateTypeStmt";
353846 case duckdb_libpgquery::T_PGCreateRangeStmt:
353847 return "T_CreateRangeStmt";
353848 case duckdb_libpgquery::T_PGAlterEnumStmt:
353849 return "T_AlterEnumStmt";
353850 case duckdb_libpgquery::T_PGAlterTSDictionaryStmt:
353851 return "T_AlterTSDictionaryStmt";
353852 case duckdb_libpgquery::T_PGAlterTSConfigurationStmt:
353853 return "T_AlterTSConfigurationStmt";
353854 case duckdb_libpgquery::T_PGCreateFdwStmt:
353855 return "T_CreateFdwStmt";
353856 case duckdb_libpgquery::T_PGAlterFdwStmt:
353857 return "T_AlterFdwStmt";
353858 case duckdb_libpgquery::T_PGCreateForeignServerStmt:
353859 return "T_CreateForeignServerStmt";
353860 case duckdb_libpgquery::T_PGAlterForeignServerStmt:
353861 return "T_AlterForeignServerStmt";
353862 case duckdb_libpgquery::T_PGCreateUserMappingStmt:
353863 return "T_CreateUserMappingStmt";
353864 case duckdb_libpgquery::T_PGAlterUserMappingStmt:
353865 return "T_AlterUserMappingStmt";
353866 case duckdb_libpgquery::T_PGDropUserMappingStmt:
353867 return "T_DropUserMappingStmt";
353868 case duckdb_libpgquery::T_PGAlterTableSpaceOptionsStmt:
353869 return "T_AlterTableSpaceOptionsStmt";
353870 case duckdb_libpgquery::T_PGAlterTableMoveAllStmt:
353871 return "T_AlterTableMoveAllStmt";
353872 case duckdb_libpgquery::T_PGSecLabelStmt:
353873 return "T_SecLabelStmt";
353874 case duckdb_libpgquery::T_PGCreateForeignTableStmt:
353875 return "T_CreateForeignTableStmt";
353876 case duckdb_libpgquery::T_PGImportForeignSchemaStmt:
353877 return "T_ImportForeignSchemaStmt";
353878 case duckdb_libpgquery::T_PGCreateExtensionStmt:
353879 return "T_CreateExtensionStmt";
353880 case duckdb_libpgquery::T_PGAlterExtensionStmt:
353881 return "T_AlterExtensionStmt";
353882 case duckdb_libpgquery::T_PGAlterExtensionContentsStmt:
353883 return "T_AlterExtensionContentsStmt";
353884 case duckdb_libpgquery::T_PGCreateEventTrigStmt:
353885 return "T_CreateEventTrigStmt";
353886 case duckdb_libpgquery::T_PGAlterEventTrigStmt:
353887 return "T_AlterEventTrigStmt";
353888 case duckdb_libpgquery::T_PGRefreshMatViewStmt:
353889 return "T_RefreshMatViewStmt";
353890 case duckdb_libpgquery::T_PGReplicaIdentityStmt:
353891 return "T_ReplicaIdentityStmt";
353892 case duckdb_libpgquery::T_PGAlterSystemStmt:
353893 return "T_AlterSystemStmt";
353894 case duckdb_libpgquery::T_PGCreatePolicyStmt:
353895 return "T_CreatePolicyStmt";
353896 case duckdb_libpgquery::T_PGAlterPolicyStmt:
353897 return "T_AlterPolicyStmt";
353898 case duckdb_libpgquery::T_PGCreateTransformStmt:
353899 return "T_CreateTransformStmt";
353900 case duckdb_libpgquery::T_PGCreateAmStmt:
353901 return "T_CreateAmStmt";
353902 case duckdb_libpgquery::T_PGCreatePublicationStmt:
353903 return "T_CreatePublicationStmt";
353904 case duckdb_libpgquery::T_PGAlterPublicationStmt:
353905 return "T_AlterPublicationStmt";
353906 case duckdb_libpgquery::T_PGCreateSubscriptionStmt:
353907 return "T_CreateSubscriptionStmt";
353908 case duckdb_libpgquery::T_PGAlterSubscriptionStmt:
353909 return "T_AlterSubscriptionStmt";
353910 case duckdb_libpgquery::T_PGDropSubscriptionStmt:
353911 return "T_DropSubscriptionStmt";
353912 case duckdb_libpgquery::T_PGCreateStatsStmt:
353913 return "T_CreateStatsStmt";
353914 case duckdb_libpgquery::T_PGAlterCollationStmt:
353915 return "T_AlterCollationStmt";
353916 case duckdb_libpgquery::T_PGAExpr:
353917 return "TAExpr";
353918 case duckdb_libpgquery::T_PGColumnRef:
353919 return "T_ColumnRef";
353920 case duckdb_libpgquery::T_PGParamRef:
353921 return "T_ParamRef";
353922 case duckdb_libpgquery::T_PGAConst:
353923 return "TAConst";
353924 case duckdb_libpgquery::T_PGFuncCall:
353925 return "T_FuncCall";
353926 case duckdb_libpgquery::T_PGAStar:
353927 return "TAStar";
353928 case duckdb_libpgquery::T_PGAIndices:
353929 return "TAIndices";
353930 case duckdb_libpgquery::T_PGAIndirection:
353931 return "TAIndirection";
353932 case duckdb_libpgquery::T_PGAArrayExpr:
353933 return "TAArrayExpr";
353934 case duckdb_libpgquery::T_PGResTarget:
353935 return "T_ResTarget";
353936 case duckdb_libpgquery::T_PGMultiAssignRef:
353937 return "T_MultiAssignRef";
353938 case duckdb_libpgquery::T_PGTypeCast:
353939 return "T_TypeCast";
353940 case duckdb_libpgquery::T_PGCollateClause:
353941 return "T_CollateClause";
353942 case duckdb_libpgquery::T_PGSortBy:
353943 return "T_SortBy";
353944 case duckdb_libpgquery::T_PGWindowDef:
353945 return "T_WindowDef";
353946 case duckdb_libpgquery::T_PGRangeSubselect:
353947 return "T_RangeSubselect";
353948 case duckdb_libpgquery::T_PGRangeFunction:
353949 return "T_RangeFunction";
353950 case duckdb_libpgquery::T_PGRangeTableSample:
353951 return "T_RangeTableSample";
353952 case duckdb_libpgquery::T_PGRangeTableFunc:
353953 return "T_RangeTableFunc";
353954 case duckdb_libpgquery::T_PGRangeTableFuncCol:
353955 return "T_RangeTableFuncCol";
353956 case duckdb_libpgquery::T_PGTypeName:
353957 return "T_TypeName";
353958 case duckdb_libpgquery::T_PGColumnDef:
353959 return "T_ColumnDef";
353960 case duckdb_libpgquery::T_PGIndexElem:
353961 return "T_IndexElem";
353962 case duckdb_libpgquery::T_PGConstraint:
353963 return "T_Constraint";
353964 case duckdb_libpgquery::T_PGDefElem:
353965 return "T_DefElem";
353966 case duckdb_libpgquery::T_PGRangeTblEntry:
353967 return "T_RangeTblEntry";
353968 case duckdb_libpgquery::T_PGRangeTblFunction:
353969 return "T_RangeTblFunction";
353970 case duckdb_libpgquery::T_PGTableSampleClause:
353971 return "T_TableSampleClause";
353972 case duckdb_libpgquery::T_PGWithCheckOption:
353973 return "T_WithCheckOption";
353974 case duckdb_libpgquery::T_PGSortGroupClause:
353975 return "T_SortGroupClause";
353976 case duckdb_libpgquery::T_PGGroupingSet:
353977 return "T_GroupingSet";
353978 case duckdb_libpgquery::T_PGWindowClause:
353979 return "T_WindowClause";
353980 case duckdb_libpgquery::T_PGObjectWithArgs:
353981 return "T_ObjectWithArgs";
353982 case duckdb_libpgquery::T_PGAccessPriv:
353983 return "T_AccessPriv";
353984 case duckdb_libpgquery::T_PGCreateOpClassItem:
353985 return "T_CreateOpClassItem";
353986 case duckdb_libpgquery::T_PGTableLikeClause:
353987 return "T_TableLikeClause";
353988 case duckdb_libpgquery::T_PGFunctionParameter:
353989 return "T_FunctionParameter";
353990 case duckdb_libpgquery::T_PGLockingClause:
353991 return "T_LockingClause";
353992 case duckdb_libpgquery::T_PGRowMarkClause:
353993 return "T_RowMarkClause";
353994 case duckdb_libpgquery::T_PGXmlSerialize:
353995 return "T_XmlSerialize";
353996 case duckdb_libpgquery::T_PGWithClause:
353997 return "T_WithClause";
353998 case duckdb_libpgquery::T_PGInferClause:
353999 return "T_InferClause";
354000 case duckdb_libpgquery::T_PGOnConflictClause:
354001 return "T_OnConflictClause";
354002 case duckdb_libpgquery::T_PGCommonTableExpr:
354003 return "T_CommonTableExpr";
354004 case duckdb_libpgquery::T_PGRoleSpec:
354005 return "T_RoleSpec";
354006 case duckdb_libpgquery::T_PGTriggerTransition:
354007 return "T_TriggerTransition";
354008 case duckdb_libpgquery::T_PGPartitionElem:
354009 return "T_PartitionElem";
354010 case duckdb_libpgquery::T_PGPartitionSpec:
354011 return "T_PartitionSpec";
354012 case duckdb_libpgquery::T_PGPartitionBoundSpec:
354013 return "T_PartitionBoundSpec";
354014 case duckdb_libpgquery::T_PGPartitionRangeDatum:
354015 return "T_PartitionRangeDatum";
354016 case duckdb_libpgquery::T_PGPartitionCmd:
354017 return "T_PartitionCmd";
354018 case duckdb_libpgquery::T_PGIdentifySystemCmd:
354019 return "T_IdentifySystemCmd";
354020 case duckdb_libpgquery::T_PGBaseBackupCmd:
354021 return "T_BaseBackupCmd";
354022 case duckdb_libpgquery::T_PGCreateReplicationSlotCmd:
354023 return "T_CreateReplicationSlotCmd";
354024 case duckdb_libpgquery::T_PGDropReplicationSlotCmd:
354025 return "T_DropReplicationSlotCmd";
354026 case duckdb_libpgquery::T_PGStartReplicationCmd:
354027 return "T_StartReplicationCmd";
354028 case duckdb_libpgquery::T_PGTimeLineHistoryCmd:
354029 return "T_TimeLineHistoryCmd";
354030 case duckdb_libpgquery::T_PGSQLCmd:
354031 return "T_SQLCmd";
354032 case duckdb_libpgquery::T_PGTriggerData:
354033 return "T_TriggerData";
354034 case duckdb_libpgquery::T_PGEventTriggerData:
354035 return "T_EventTriggerData";
354036 case duckdb_libpgquery::T_PGReturnSetInfo:
354037 return "T_ReturnSetInfo";
354038 case duckdb_libpgquery::T_PGWindowObjectData:
354039 return "T_WindowObjectData";
354040 case duckdb_libpgquery::T_PGTIDBitmap:
354041 return "T_TIDBitmap";
354042 case duckdb_libpgquery::T_PGInlineCodeBlock:
354043 return "T_InlineCodeBlock";
354044 case duckdb_libpgquery::T_PGFdwRoutine:
354045 return "T_FdwRoutine";
354046 case duckdb_libpgquery::T_PGIndexAmRoutine:
354047 return "T_IndexAmRoutine";
354048 case duckdb_libpgquery::T_PGTsmRoutine:
354049 return "T_TsmRoutine";
354050 case duckdb_libpgquery::T_PGForeignKeyCacheInfo:
354051 return "T_ForeignKeyCacheInfo";
354052 case duckdb_libpgquery::T_PGAttachStmt:
354053 return "T_PGAttachStmt";
354054 case duckdb_libpgquery::T_PGUseStmt:
354055 return "T_PGUseStmt";
354056 default:
354057 return "(UNKNOWN)";
354058 }
354059} // LCOV_EXCL_STOP

◆ ParamCount()

idx_t duckdb::Transformer::ParamCount ( ) const
358372 {
358373 auto &root = RootTransformer();
358374 return root.prepared_statement_parameter_index;
358375}

◆ Clear()

void duckdb::Transformer::Clear ( )
private
358312 {
358313 ClearParameters();
358314 pivot_entries.clear();
358315}
vector< unique_ptr< CreatePivotEntry > > pivot_entries
The set of pivot entries to create.
Definition duckdb.cpp:346496

◆ InWindowDefinition()

bool duckdb::Transformer::InWindowDefinition ( )
private
352171 {
352172 if (in_window_definition) {
352173 return true;
352174 }
352175 if (parent) {
352176 return parent->InWindowDefinition();
352177 }
352178 return false;
352179}
bool in_window_definition
Whether or not we are currently binding a window definition.
Definition duckdb.cpp:346500

◆ RootTransformer() [1/2]

Transformer & duckdb::Transformer::RootTransformer ( )
private
358356 {
358357 reference<Transformer> node = *this;
358358 while (node.get().parent) {
358359 node = *node.get().parent;
358360 }
358361 return node.get();
358362}

◆ RootTransformer() [2/2]

const Transformer & duckdb::Transformer::RootTransformer ( ) const
private
358364 {
358365 reference<const Transformer> node = *this;
358366 while (node.get().parent) {
358367 node = *node.get().parent;
358368 }
358369 return node.get();
358370}

◆ SetParamCount()

void duckdb::Transformer::SetParamCount ( idx_t new_count)
private
358377 {
358378 auto &root = RootTransformer();
358379 root.prepared_statement_parameter_index = new_count;
358380}

◆ ClearParameters()

void duckdb::Transformer::ClearParameters ( )
private
358382 {
358383 auto &root = RootTransformer();
358384 root.prepared_statement_parameter_index = 0;
358385 root.named_param_map.clear();
358386}

◆ SetParam()

void duckdb::Transformer::SetParam ( const string & name,
idx_t index,
PreparedParamType type )
private
358405 {
358406 auto &root = RootTransformer();
358407 ParamTypeCheck(root.last_param_type, type);
358408 root.last_param_type = type;
358409 D_ASSERT(!root.named_param_map.count(identifier));
358410 root.named_param_map[identifier] = index;
358411}
index

◆ GetParam()

bool duckdb::Transformer::GetParam ( const string & name,
idx_t & index,
PreparedParamType type )
private
358413 {
358414 auto &root = RootTransformer();
358415 ParamTypeCheck(root.last_param_type, type);
358416 auto entry = root.named_param_map.find(identifier);
358417 if (entry == root.named_param_map.end()) {
358418 return false;
358419 }
358420 index = entry->second;
358421 return true;
358422}

◆ AddPivotEntry()

void duckdb::Transformer::AddPivotEntry ( string enum_name,
unique_ptr< SelectNode > source,
unique_ptr< ParsedExpression > column,
unique_ptr< QueryNode > subquery,
bool has_parameters )
private
356610 {
356611 if (parent) {
356612 parent->AddPivotEntry(std::move(enum_name), std::move(base), std::move(column), std::move(subquery),
356613 has_parameters);
356614 return;
356615 }
356616 auto result = make_uniq<CreatePivotEntry>();
356617 result->enum_name = std::move(enum_name);
356618 result->base = std::move(base);
356619 result->column = std::move(column);
356620 result->subquery = std::move(subquery);
356621 result->has_parameters = has_parameters;
356622
356623 pivot_entries.push_back(std::move(result));
356624}

◆ GenerateCreateEnumStmt()

unique_ptr< SQLStatement > duckdb::Transformer::GenerateCreateEnumStmt ( unique_ptr< CreatePivotEntry > entry)
private
356650 {
356651 auto result = make_uniq<CreateStatement>();
356652 auto info = make_uniq<CreateTypeInfo>();
356653
356654 info->temporary = true;
356655 info->internal = false;
356656 info->catalog = INVALID_CATALOG;
356657 info->schema = INVALID_SCHEMA;
356658 info->name = std::move(entry->enum_name);
356659 info->on_conflict = OnCreateConflict::REPLACE_ON_CONFLICT;
356660
356661 // generate the query that will result in the enum creation
356662 unique_ptr<QueryNode> subselect;
356663 if (!entry->subquery) {
356664 auto select_node = std::move(entry->base);
356665 auto columnref = entry->column->Copy();
356666 auto cast = make_uniq<CastExpression>(LogicalType::VARCHAR, std::move(columnref));
356667 select_node->select_list.push_back(std::move(cast));
356668
356669 auto is_not_null =
356670 make_uniq<OperatorExpression>(ExpressionType::OPERATOR_IS_NOT_NULL, std::move(entry->column));
356671 select_node->where_clause = std::move(is_not_null);
356672
356673 // order by the column
356674 select_node->modifiers.push_back(make_uniq<DistinctModifier>());
356675 auto modifier = make_uniq<OrderModifier>();
356676 modifier->orders.emplace_back(OrderType::ASCENDING, OrderByNullType::ORDER_DEFAULT,
356677 make_uniq<ConstantExpression>(Value::INTEGER(1)));
356678 select_node->modifiers.push_back(std::move(modifier));
356679 subselect = std::move(select_node);
356680 } else {
356681 subselect = std::move(entry->subquery);
356682 }
356683
356684 auto select = make_uniq<SelectStatement>();
356685 select->node = std::move(subselect);
356686 info->query = std::move(select);
356687 info->type = LogicalType::INVALID;
356688
356689 result->info = std::move(info);
356690 return std::move(result);
356691}
static DUCKDB_API Value INTEGER(int32_t value)
Create an integer Value from a specified value.
Definition duckdb.cpp:132139
GMat select(const GMat &src1, const GMat &src2, const GMat &mask)
Widget2D cast() const

◆ HasPivotEntries()

bool duckdb::Transformer::HasPivotEntries ( )
private
356626 {
356627 return !GetPivotEntries().empty();
356628}

◆ PivotEntryCount()

idx_t duckdb::Transformer::PivotEntryCount ( )
private
356630 {
356631 return GetPivotEntries().size();
356632}

◆ GetPivotEntries()

vector< unique_ptr< Transformer::CreatePivotEntry > > & duckdb::Transformer::GetPivotEntries ( )
private
356634 {
356635 if (parent) {
356636 return parent->GetPivotEntries();
356637 }
356638 return pivot_entries;
356639}

◆ PivotEntryCheck()

void duckdb::Transformer::PivotEntryCheck ( const string & type)
private
356641 {
356642 auto &entries = GetPivotEntries();
356643 if (!entries.empty()) {
356644 throw ParserException(
356645 "PIVOT statements with pivot elements extracted from the data cannot be used in %ss.\nIn order to use "
356646 "PIVOT in a %s the PIVOT values must be manually specified, e.g.:\nPIVOT ... ON %s IN (val1, val2, ...)",
356647 type, type, entries[0]->column->ToString());
356648 }
356649}

◆ ExtractCTEsRecursive()

void duckdb::Transformer::ExtractCTEsRecursive ( CommonTableExpressionMap & cte_map)
private
354120 {
354121 for (auto &cte_entry : stored_cte_map) {
354122 for (auto &entry : cte_entry.get().map) {
354123 auto found_entry = cte_map.map.find(entry.first);
354124 if (found_entry != cte_map.map.end()) {
354125 // entry already present - use top-most entry
354126 continue;
354127 }
354128 cte_map.map[entry.first] = entry.second->Copy();
354129 }
354130 }
354131 if (parent) {
354132 parent->ExtractCTEsRecursive(cte_map);
354133 }
354134}
vector< reference< CommonTableExpressionMap > > stored_cte_map
Sets of stored CTEs, if any.
Definition duckdb.cpp:346498

◆ TransformStatement()

unique_ptr< SQLStatement > duckdb::Transformer::TransformStatement ( duckdb_libpgquery::PGNode & stmt)
private

Transforms a Postgres statement into a single SQL statement.

358347 {
358348 auto result = TransformStatementInternal(stmt);
358349 if (!named_param_map.empty()) {
358350 // Avoid overriding a previous move with nothing
358351 result->named_param_map = named_param_map;
358352 }
358353 return result;
358354}
case_insensitive_map_t< idx_t > named_param_map
Map from named parameter to parameter index;.
Definition duckdb.cpp:346490
unique_ptr< SQLStatement > TransformStatementInternal(duckdb_libpgquery::PGNode &stmt)
Transforms a Postgres statement into a single SQL statement.
Definition duckdb.cpp:358424
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformStatementInternal()

unique_ptr< SQLStatement > duckdb::Transformer::TransformStatementInternal ( duckdb_libpgquery::PGNode & stmt)
private

Transforms a Postgres statement into a single SQL statement.

358424 {
358425 switch (stmt.type) {
358426 case duckdb_libpgquery::T_PGRawStmt: {
358427 auto &raw_stmt = PGCast<duckdb_libpgquery::PGRawStmt>(stmt);
358428 auto result = TransformStatement(*raw_stmt.stmt);
358429 if (result) {
358430 result->stmt_location = NumericCast<idx_t>(raw_stmt.stmt_location);
358431 result->stmt_length = NumericCast<idx_t>(raw_stmt.stmt_len);
358432 }
358433 return result;
358434 }
358435 case duckdb_libpgquery::T_PGSelectStmt:
358436 return TransformSelectStmt(PGCast<duckdb_libpgquery::PGSelectStmt>(stmt));
358437 case duckdb_libpgquery::T_PGCreateStmt:
358438 return TransformCreateTable(PGCast<duckdb_libpgquery::PGCreateStmt>(stmt));
358439 case duckdb_libpgquery::T_PGCreateSchemaStmt:
358440 return TransformCreateSchema(PGCast<duckdb_libpgquery::PGCreateSchemaStmt>(stmt));
358441 case duckdb_libpgquery::T_PGViewStmt:
358442 return TransformCreateView(PGCast<duckdb_libpgquery::PGViewStmt>(stmt));
358443 case duckdb_libpgquery::T_PGCreateSeqStmt:
358444 return TransformCreateSequence(PGCast<duckdb_libpgquery::PGCreateSeqStmt>(stmt));
358445 case duckdb_libpgquery::T_PGCreateFunctionStmt:
358446 return TransformCreateFunction(PGCast<duckdb_libpgquery::PGCreateFunctionStmt>(stmt));
358447 case duckdb_libpgquery::T_PGDropStmt:
358448 return TransformDrop(PGCast<duckdb_libpgquery::PGDropStmt>(stmt));
358449 case duckdb_libpgquery::T_PGInsertStmt:
358450 return TransformInsert(PGCast<duckdb_libpgquery::PGInsertStmt>(stmt));
358451 case duckdb_libpgquery::T_PGCopyStmt:
358452 return TransformCopy(PGCast<duckdb_libpgquery::PGCopyStmt>(stmt));
358453 case duckdb_libpgquery::T_PGTransactionStmt:
358454 return TransformTransaction(PGCast<duckdb_libpgquery::PGTransactionStmt>(stmt));
358455 case duckdb_libpgquery::T_PGDeleteStmt:
358456 return TransformDelete(PGCast<duckdb_libpgquery::PGDeleteStmt>(stmt));
358457 case duckdb_libpgquery::T_PGUpdateStmt:
358458 return TransformUpdate(PGCast<duckdb_libpgquery::PGUpdateStmt>(stmt));
358459 case duckdb_libpgquery::T_PGUpdateExtensionsStmt:
358460 return TransformUpdateExtensions(PGCast<duckdb_libpgquery::PGUpdateExtensionsStmt>(stmt));
358461 case duckdb_libpgquery::T_PGIndexStmt:
358462 return TransformCreateIndex(PGCast<duckdb_libpgquery::PGIndexStmt>(stmt));
358463 case duckdb_libpgquery::T_PGAlterTableStmt:
358464 return TransformAlter(PGCast<duckdb_libpgquery::PGAlterTableStmt>(stmt));
358465 case duckdb_libpgquery::T_PGAlterDatabaseStmt:
358466 return TransformAlterDatabase(PGCast<duckdb_libpgquery::PGAlterDatabaseStmt>(stmt));
358467 case duckdb_libpgquery::T_PGRenameStmt:
358468 return TransformRename(PGCast<duckdb_libpgquery::PGRenameStmt>(stmt));
358469 case duckdb_libpgquery::T_PGPrepareStmt:
358470 return TransformPrepare(PGCast<duckdb_libpgquery::PGPrepareStmt>(stmt));
358471 case duckdb_libpgquery::T_PGExecuteStmt:
358472 return TransformExecute(PGCast<duckdb_libpgquery::PGExecuteStmt>(stmt));
358473 case duckdb_libpgquery::T_PGDeallocateStmt:
358474 return TransformDeallocate(PGCast<duckdb_libpgquery::PGDeallocateStmt>(stmt));
358475 case duckdb_libpgquery::T_PGCreateTableAsStmt:
358476 return TransformCreateTableAs(PGCast<duckdb_libpgquery::PGCreateTableAsStmt>(stmt));
358477 case duckdb_libpgquery::T_PGPragmaStmt:
358478 return TransformPragma(PGCast<duckdb_libpgquery::PGPragmaStmt>(stmt));
358479 case duckdb_libpgquery::T_PGExportStmt:
358480 return TransformExport(PGCast<duckdb_libpgquery::PGExportStmt>(stmt));
358481 case duckdb_libpgquery::T_PGImportStmt:
358482 return TransformImport(PGCast<duckdb_libpgquery::PGImportStmt>(stmt));
358483 case duckdb_libpgquery::T_PGExplainStmt:
358484 return TransformExplain(PGCast<duckdb_libpgquery::PGExplainStmt>(stmt));
358485 case duckdb_libpgquery::T_PGVacuumStmt:
358486 return TransformVacuum(PGCast<duckdb_libpgquery::PGVacuumStmt>(stmt));
358487 case duckdb_libpgquery::T_PGVariableShowStmt:
358488 return TransformShowStmt(PGCast<duckdb_libpgquery::PGVariableShowStmt>(stmt));
358489 case duckdb_libpgquery::T_PGVariableShowSelectStmt:
358490 return TransformShowSelectStmt(PGCast<duckdb_libpgquery::PGVariableShowSelectStmt>(stmt));
358491 case duckdb_libpgquery::T_PGCallStmt:
358492 return TransformCall(PGCast<duckdb_libpgquery::PGCallStmt>(stmt));
358493 case duckdb_libpgquery::T_PGVariableSetStmt:
358494 return TransformSet(PGCast<duckdb_libpgquery::PGVariableSetStmt>(stmt));
358495 case duckdb_libpgquery::T_PGCheckPointStmt:
358496 return TransformCheckpoint(PGCast<duckdb_libpgquery::PGCheckPointStmt>(stmt));
358497 case duckdb_libpgquery::T_PGLoadStmt:
358498 return TransformLoad(PGCast<duckdb_libpgquery::PGLoadStmt>(stmt));
358499 case duckdb_libpgquery::T_PGCreateTypeStmt:
358500 return TransformCreateType(PGCast<duckdb_libpgquery::PGCreateTypeStmt>(stmt));
358501 case duckdb_libpgquery::T_PGAlterSeqStmt:
358502 return TransformAlterSequence(PGCast<duckdb_libpgquery::PGAlterSeqStmt>(stmt));
358503 case duckdb_libpgquery::T_PGAttachStmt:
358504 return TransformAttach(PGCast<duckdb_libpgquery::PGAttachStmt>(stmt));
358505 case duckdb_libpgquery::T_PGDetachStmt:
358506 return TransformDetach(PGCast<duckdb_libpgquery::PGDetachStmt>(stmt));
358507 case duckdb_libpgquery::T_PGUseStmt:
358508 return TransformUse(PGCast<duckdb_libpgquery::PGUseStmt>(stmt));
358509 case duckdb_libpgquery::T_PGCopyDatabaseStmt:
358510 return TransformCopyDatabase(PGCast<duckdb_libpgquery::PGCopyDatabaseStmt>(stmt));
358511 case duckdb_libpgquery::T_PGCreateSecretStmt:
358512 return TransformSecret(PGCast<duckdb_libpgquery::PGCreateSecretStmt>(stmt));
358513 case duckdb_libpgquery::T_PGDropSecretStmt:
358514 return TransformDropSecret(PGCast<duckdb_libpgquery::PGDropSecretStmt>(stmt));
358515 case duckdb_libpgquery::T_PGCommentOnStmt:
358516 return TransformCommentOn(PGCast<duckdb_libpgquery::PGCommentOnStmt>(stmt));
358517 case duckdb_libpgquery::T_PGMergeIntoStmt:
358518 return TransformMergeInto(PGCast<duckdb_libpgquery::PGMergeIntoStmt>(stmt));
358519 default:
358520 throw NotImplementedException(NodetypeToString(stmt.type));
358521 }
358522}
unique_ptr< ExportStatement > TransformExport(duckdb_libpgquery::PGExportStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGExportStmt node into a ExportStatement.
Definition duckdb.cpp:356328
unique_ptr< AlterStatement > TransformRename(duckdb_libpgquery::PGRenameStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGRenameStmt node into a RenameStatement.
Definition duckdb.cpp:356966
unique_ptr< CreateStatement > TransformCreateSchema(duckdb_libpgquery::PGCreateSchemaStmt &stmt)
Transform a Postgres node into a CreateStatement.
Definition duckdb.cpp:355572
unique_ptr< CreateStatement > TransformCreateSequence(duckdb_libpgquery::PGCreateSeqStmt &node)
Transform a Postgres duckdb_libpgquery::T_PGCreateSeqStmt node into a CreateStatement.
Definition duckdb.cpp:355607
unique_ptr< AlterStatement > TransformAlterSequence(duckdb_libpgquery::PGAlterSeqStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGAlterSeqStmt node into CreateStatement.
Definition duckdb.cpp:354776
unique_ptr< DeleteStatement > TransformDelete(duckdb_libpgquery::PGDeleteStmt &stmt)
Transform a Postgres T_DeleteStatement node into a DeleteStatement.
Definition duckdb.cpp:356120
unique_ptr< SQLStatement > TransformDrop(duckdb_libpgquery::PGDropStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGDropStmt node into a Drop[Table,Schema]Statement.
Definition duckdb.cpp:356171
unique_ptr< SQLStatement > TransformAlter(duckdb_libpgquery::PGAlterTableStmt &stmt)
Transform a Postgres T_AlterStmt node into a AlterStatement.
Definition duckdb.cpp:354924
unique_ptr< CopyStatement > TransformCopy(duckdb_libpgquery::PGCopyStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGCopyStmt node into a CopyStatement.
Definition duckdb.cpp:355306
unique_ptr< CreateStatement > TransformCreateTable(duckdb_libpgquery::PGCreateStmt &node)
Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement.
Definition duckdb.cpp:355823
unique_ptr< CreateStatement > TransformCreateIndex(duckdb_libpgquery::PGIndexStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGIndexStmt node into CreateStatement.
Definition duckdb.cpp:355508
unique_ptr< SQLStatement > TransformPragma(duckdb_libpgquery::PGPragmaStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGPragmaStmt node into a PragmaStatement.
Definition duckdb.cpp:356823
unique_ptr< UpdateExtensionsStatement > TransformUpdateExtensions(duckdb_libpgquery::PGUpdateExtensionsStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGUpdateExtensionsStmt node into a UpdateExtensionsStatemen...
Definition duckdb.cpp:357664
unique_ptr< AlterStatement > TransformAlterDatabase(duckdb_libpgquery::PGAlterDatabaseStmt &stmt)
Transform a Postgres T_AlterDatabaseStmt node into a AlterStatement.
Definition duckdb.cpp:354737
unique_ptr< InsertStatement > TransformInsert(duckdb_libpgquery::PGInsertStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGInsertStmt node into a InsertStatement.
Definition duckdb.cpp:356402
unique_ptr< CreateStatement > TransformCreateTableAs(duckdb_libpgquery::PGCreateTableAsStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement.
Definition duckdb.cpp:355916
unique_ptr< PragmaStatement > TransformImport(duckdb_libpgquery::PGImportStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGImportStmt node into a PragmaStatement.
Definition duckdb.cpp:356350
unique_ptr< TransactionStatement > TransformTransaction(duckdb_libpgquery::PGTransactionStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGTransactionStmt node into a TransactionStatement.
Definition duckdb.cpp:357610
unique_ptr< CreateStatement > TransformCreateView(duckdb_libpgquery::PGViewStmt &node)
Transform a Postgres duckdb_libpgquery::T_PGViewStmt node into a CreateStatement.
Definition duckdb.cpp:356065
unique_ptr< SelectStatement > TransformSelectStmt(duckdb_libpgquery::PGSelectStmt &select, bool is_select=true)
Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a SelectStatement.
Definition duckdb.cpp:357156
unique_ptr< UpdateStatement > TransformUpdate(duckdb_libpgquery::PGUpdateStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGUpdateStmt node into a UpdateStatement.
Definition duckdb.cpp:357643
unique_ptr< CreateStatement > TransformCreateFunction(duckdb_libpgquery::PGCreateFunctionStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGCreateFunctionStmt node into CreateStatement.
Definition duckdb.cpp:355429
unique_ptr< AlterStatement > TransformCommentOn(duckdb_libpgquery::PGCommentOnStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement.
Definition duckdb.cpp:355178
unique_ptr< CreateStatement > TransformCreateType(duckdb_libpgquery::PGCreateTypeStmt &stmt)
Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement.
Definition duckdb.cpp:356019
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformSelectStmt() [1/2]

unique_ptr< SelectStatement > duckdb::Transformer::TransformSelectStmt ( duckdb_libpgquery::PGSelectStmt & select,
bool is_select = true )
private

Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a SelectStatement.

357156 {
357157 auto result = make_uniq<SelectStatement>();
357158 result->node = TransformSelectNodeInternal(select, is_select);
357159 return result;
357160}
Here is the caller graph for this function:

◆ TransformSelectStmt() [2/2]

unique_ptr< SelectStatement > duckdb::Transformer::TransformSelectStmt ( duckdb_libpgquery::PGNode & node,
bool is_select = true )
private
357162 {
357163 auto select_node = TransformSelectNode(node, is_select);
357164 auto select_statement = make_uniq<SelectStatement>();
357165 select_statement->node = std::move(select_node);
357166 return select_statement;
357167}
unique_ptr< QueryNode > TransformSelectNode(duckdb_libpgquery::PGNode &select, bool is_select=true)
Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a QueryNode.
Definition duckdb.cpp:357128

◆ TransformAlter()

unique_ptr< SQLStatement > duckdb::Transformer::TransformAlter ( duckdb_libpgquery::PGAlterTableStmt & stmt)
private

Transform a Postgres T_AlterStmt node into a AlterStatement.

354924 {
354925 D_ASSERT(stmt.relation);
354926 if (stmt.cmds->length != 1) {
354927 throw ParserException("Only one ALTER command per statement is supported");
354928 }
354929
354930 auto result = make_uniq<AlterStatement>();
354931 auto qualified_name = TransformQualifiedName(*stmt.relation);
354932
354933 // Check the ALTER type.
354934 for (auto c = stmt.cmds->head; c != nullptr; c = c->next) {
354935 auto command = PGPointerCast<duckdb_libpgquery::PGAlterTableCmd>(c->data.ptr_value);
354936 AlterEntryData data(qualified_name.catalog, qualified_name.schema, qualified_name.name,
354937 TransformOnEntryNotFound(stmt.missing_ok));
354938
354939 switch (command->subtype) {
354940 case duckdb_libpgquery::PG_AT_AddColumn: {
354941 auto column_name_list = PGPointerCast<duckdb_libpgquery::PGList>(command->def_list->head->data.ptr_value);
354942 auto column_def = PGPointerCast<duckdb_libpgquery::PGColumnDef>(command->def_list->tail->data.ptr_value);
354943 if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) {
354944 throw ParserException("Adding columns is only supported for tables");
354945 }
354946 if (column_def->category == duckdb_libpgquery::COL_GENERATED) {
354947 throw ParserException("Adding generated columns after table creation is not supported yet");
354948 }
354949 auto column_entry = TransformColumnDefinition(*column_def);
354950 if (column_def->constraints) {
354951 for (auto cell = column_def->constraints->head; cell != nullptr; cell = cell->next) {
354952 auto pg_constraint = PGPointerCast<duckdb_libpgquery::PGConstraint>(cell->data.ptr_value);
354953 auto constraint = TransformConstraint(*pg_constraint, column_entry, 0);
354954 if (!constraint) {
354955 continue;
354956 }
354957 throw ParserException("Adding columns with constraints not yet supported");
354958 }
354959 }
354960 auto column_names = TransformNameList(*column_name_list);
354961 if (column_names.empty()) {
354962 throw InternalException("Expected a name");
354963 }
354964 column_entry.SetName(column_names.back());
354965 if (column_names.size() == 1) {
354966 // ADD COLUMN
354967 if (!column_entry.HasDefaultValue() ||
354968 column_entry.DefaultValue().GetExpressionClass() == ExpressionClass::CONSTANT) {
354969 result->info =
354970 make_uniq<AddColumnInfo>(std::move(data), std::move(column_entry), command->missing_ok);
354971 break;
354972 }
354973 auto null_column = column_entry.Copy();
354974 null_column.SetDefaultValue(make_uniq<ConstantExpression>(ConstantExpression(Value(nullptr))));
354975 return unique_ptr<SQLStatement>(std::move(TransformAndMaterializeAlter(
354976 stmt, data, make_uniq<AddColumnInfo>(data, std::move(null_column), command->missing_ok),
354977 column_entry.GetName(), column_entry.DefaultValue().Copy())));
354978
354979 } else {
354980 // ADD FIELD
354981 column_names.pop_back();
354982 result->info = make_uniq<AddFieldInfo>(std::move(data), std::move(column_names),
354983 std::move(column_entry), command->missing_ok);
354984 }
354985 break;
354986 }
354987 case duckdb_libpgquery::PG_AT_DropColumn: {
354988 auto cascade = command->behavior == duckdb_libpgquery::PG_DROP_CASCADE;
354989 if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) {
354990 throw ParserException("Dropping columns is only supported for tables");
354991 }
354992 auto column_names = TransformNameList(*command->def_list);
354993 if (column_names.empty()) {
354994 throw InternalException("Expected a name");
354995 }
354996 if (column_names.size() == 1) {
354997 result->info =
354998 make_uniq<RemoveColumnInfo>(std::move(data), column_names[0], command->missing_ok, cascade);
354999 } else {
355000 result->info =
355001 make_uniq<RemoveFieldInfo>(std::move(data), std::move(column_names), command->missing_ok, cascade);
355002 }
355003 break;
355004 }
355005 case duckdb_libpgquery::PG_AT_ColumnDefault: {
355006 auto expr = TransformExpression(command->def);
355007 if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) {
355008 throw ParserException("Alter column's default is only supported for tables");
355009 }
355010 result->info = make_uniq<SetDefaultInfo>(std::move(data), command->name, std::move(expr));
355011 break;
355012 }
355013 case duckdb_libpgquery::PG_AT_AlterColumnType: {
355014 auto column_def = PGPointerCast<duckdb_libpgquery::PGColumnDef>(command->def);
355015 auto column_entry = TransformColumnDefinition(*column_def);
355016
355017 unique_ptr<ParsedExpression> expr;
355018 if (stmt.relkind != duckdb_libpgquery::PG_OBJECT_TABLE) {
355019 throw ParserException("Alter column's type is only supported for tables");
355020 }
355021
355022 if (column_entry.GetType() == LogicalType::UNKNOWN && !column_def->raw_default) {
355023 throw ParserException("Omitting the type is only possible in combination with USING");
355024 }
355025
355026 if (column_def->raw_default) {
355027 expr = TransformExpression(column_def->raw_default);
355028 } else {
355029 auto col_ref = make_uniq<ColumnRefExpression>(command->name);
355030 expr = make_uniq<CastExpression>(column_entry.Type(), std::move(col_ref));
355031 }
355032 result->info =
355033 make_uniq<ChangeColumnTypeInfo>(std::move(data), command->name, column_entry.Type(), std::move(expr));
355034 break;
355035 }
355036 case duckdb_libpgquery::PG_AT_SetNotNull: {
355037 result->info = make_uniq<SetNotNullInfo>(std::move(data), command->name);
355038 break;
355039 }
355040 case duckdb_libpgquery::PG_AT_DropNotNull: {
355041 result->info = make_uniq<DropNotNullInfo>(std::move(data), command->name);
355042 break;
355043 }
355044 case duckdb_libpgquery::PG_AT_AddConstraint: {
355045 auto pg_constraint = PGCast<duckdb_libpgquery::PGConstraint>(*command->def);
355046 if (pg_constraint.contype != duckdb_libpgquery::PGConstrType::PG_CONSTR_PRIMARY) {
355047 throw NotImplementedException("No support for that ALTER TABLE option yet!");
355048 }
355049
355050 auto constraint = TransformConstraint(pg_constraint);
355051 result->info = make_uniq<AddConstraintInfo>(std::move(data), std::move(constraint));
355052 break;
355053 }
355054 case duckdb_libpgquery::PG_AT_SetPartitionedBy: {
355055 vector<unique_ptr<ParsedExpression>> partition_keys;
355056 if (command->def_list) {
355057 TransformExpressionList(*command->def_list, partition_keys);
355058 }
355059 result->info = make_uniq<SetPartitionedByInfo>(std::move(data), std::move(partition_keys));
355060 break;
355061 }
355062 case duckdb_libpgquery::PG_AT_SetSortedBy: {
355063 vector<OrderByNode> orders;
355064 if (command->def_list) {
355065 TransformOrderBy(command->def_list, orders);
355066 }
355067 result->info = make_uniq<SetSortedByInfo>(std::move(data), std::move(orders));
355068 break;
355069 }
355070 case duckdb_libpgquery::PG_AT_SetRelOptions: {
355071 case_insensitive_map_t<unique_ptr<ParsedExpression>> options;
355072 if (command->options) {
355073 TransformTableOptions(options, command->options);
355074 }
355075 result->info = make_uniq<SetTableOptionsInfo>(std::move(data), std::move(options));
355076 break;
355077 }
355078 case duckdb_libpgquery::PG_AT_ResetRelOptions: {
355079 case_insensitive_map_t<unique_ptr<ParsedExpression>> rel_options_map;
355080 case_insensitive_set_t options;
355081 if (command->options) {
355082 // TransformTableOptions will throw if key values are parsed
355083 TransformTableOptions(rel_options_map, command->options, true);
355084 // make sure RESET only supports single value options
355085 // default of true is allowed
355086 for (auto &option : rel_options_map) {
355087 options.insert(option.first);
355088 }
355089 }
355090 result->info = make_uniq<ResetTableOptionsInfo>(std::move(data), std::move(options));
355091 break;
355092 }
355093 default:
355094 throw NotImplementedException("No support for that ALTER TABLE option yet!");
355095 }
355096 }
355097 return unique_ptr<SQLStatement>(std::move(result));
355098}
unique_ptr< ParsedExpression > TransformExpression(optional_ptr< duckdb_libpgquery::PGNode > node)
Transform a Postgres abstract expression into an Expression.
Definition duckdb.cpp:352010
QualifiedName TransformQualifiedName(duckdb_libpgquery::PGRangeVar &root)
Transform a range var into a (schema) qualified name.
Definition duckdb.cpp:357880
void TransformExpressionList(duckdb_libpgquery::PGList &list, vector< unique_ptr< ParsedExpression > > &result)
Transform a Postgres SELECT clause into a list of Expressions.
Definition duckdb.cpp:352017
bool TransformOrderBy(duckdb_libpgquery::PGList *order, vector< OrderByNode > &result)
Transform a Postgres ORDER BY expression into an OrderByDescription.
Definition duckdb.cpp:354450
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformAlterDatabase()

unique_ptr< AlterStatement > duckdb::Transformer::TransformAlterDatabase ( duckdb_libpgquery::PGAlterDatabaseStmt & stmt)
private

Transform a Postgres T_AlterDatabaseStmt node into a AlterStatement.

354737 {
354738 auto result = make_uniq<AlterStatement>();
354739
354740 auto database_name = stmt.dbname ? string(stmt.dbname) : string();
354741 if (database_name.empty()) {
354742 throw ParserException("ALTER DATABASE requires a database name");
354743 }
354744
354745 OnEntryNotFound if_not_found = OnEntryNotFound::THROW_EXCEPTION;
354746 if (stmt.missing_ok) {
354747 if_not_found = OnEntryNotFound::RETURN_NULL;
354748 }
354749
354750 switch (stmt.alter_type) {
354751 case duckdb_libpgquery::PG_ALTER_DATABASE_RENAME: {
354752 if (!stmt.new_name) {
354753 throw ParserException("ALTER DATABASE RENAME requires a new name");
354754 }
354755 auto info = make_uniq<RenameDatabaseInfo>(database_name, string(stmt.new_name), if_not_found);
354756 result->info = std::move(info);
354757 break;
354758 }
354759 default:
354760 throw ParserException("Unsupported ALTER DATABASE operation");
354761 }
354762
354763 return result;
354764}
Here is the caller graph for this function:

◆ TransformRename()

unique_ptr< AlterStatement > duckdb::Transformer::TransformRename ( duckdb_libpgquery::PGRenameStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGRenameStmt node into a RenameStatement.

356966 {
356967 if (!stmt.relation) {
356968 throw NotImplementedException("Altering schemas is not yet supported");
356969 }
356970
356971 unique_ptr<AlterInfo> info;
356972
356973 AlterEntryData data;
356974 data.if_not_found = TransformOnEntryNotFound(stmt.missing_ok);
356975 data.catalog = stmt.relation->catalogname ? stmt.relation->catalogname : INVALID_CATALOG;
356976 data.schema = stmt.relation->schemaname ? stmt.relation->schemaname : INVALID_SCHEMA;
356977 if (stmt.relation->relname) {
356978 data.name = stmt.relation->relname;
356979 }
356980 // first we check the type of ALTER
356981 switch (stmt.renameType) {
356982 case duckdb_libpgquery::PG_OBJECT_COLUMN: {
356983 // change column name
356984
356985 // get the old name and the new name
356986 auto names = TransformNameList(*stmt.name_list);
356987 string new_name = stmt.newname;
356988 if (names.size() == 1) {
356989 info = make_uniq<RenameColumnInfo>(std::move(data), std::move(names[0]), std::move(new_name));
356990 } else {
356991 info = make_uniq<RenameFieldInfo>(std::move(data), std::move(names), std::move(new_name));
356992 }
356993 break;
356994 }
356995 case duckdb_libpgquery::PG_OBJECT_TABLE: {
356996 // change table name
356997 string new_name = stmt.newname;
356998 info = make_uniq<RenameTableInfo>(std::move(data), new_name);
356999 break;
357000 }
357001 case duckdb_libpgquery::PG_OBJECT_VIEW: {
357002 // change view name
357003 string new_name = stmt.newname;
357004 info = make_uniq<RenameViewInfo>(std::move(data), new_name);
357005 break;
357006 }
357007 case duckdb_libpgquery::PG_OBJECT_DATABASE:
357008 default:
357009 throw NotImplementedException("Schema element not supported yet!");
357010 }
357011 D_ASSERT(info);
357012
357013 auto result = make_uniq<AlterStatement>();
357014 result->info = std::move(info);
357015 return result;
357016}
Here is the caller graph for this function:

◆ TransformCreateTable()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateTable ( duckdb_libpgquery::PGCreateStmt & node)
private

Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement.

355823 {
355824 auto result = make_uniq<CreateStatement>();
355825 auto info = make_uniq<CreateTableInfo>();
355826
355827 if (stmt.inhRelations) {
355828 throw NotImplementedException("inherited relations not implemented");
355829 }
355830 D_ASSERT(stmt.relation);
355831
355832 info->catalog = INVALID_CATALOG;
355833 auto qname = TransformQualifiedName(*stmt.relation);
355834 info->catalog = qname.catalog;
355835 info->schema = qname.schema;
355836 info->table = qname.name;
355837 info->on_conflict = TransformOnConflict(stmt.onconflict);
355838 info->temporary =
355839 stmt.relation->relpersistence == duckdb_libpgquery::PGPostgresRelPersistence::PG_RELPERSISTENCE_TEMP;
355840
355841 if (info->temporary && stmt.oncommit != duckdb_libpgquery::PGOnCommitAction::PG_ONCOMMIT_PRESERVE_ROWS &&
355842 stmt.oncommit != duckdb_libpgquery::PGOnCommitAction::PG_ONCOMMIT_NOOP) {
355843 throw NotImplementedException("Only ON COMMIT PRESERVE ROWS is supported");
355844 }
355845 if (!stmt.tableElts) {
355846 throw ParserException("Table must have at least one column!");
355847 }
355848
355849 idx_t column_count = 0;
355850 for (auto c = stmt.tableElts->head; c != nullptr; c = lnext(c)) {
355851 auto node = PGPointerCast<duckdb_libpgquery::PGNode>(c->data.ptr_value);
355852 switch (node->type) {
355853 case duckdb_libpgquery::T_PGColumnDef: {
355854 auto pg_col_def = PGPointerCast<duckdb_libpgquery::PGColumnDef>(c->data.ptr_value);
355855 auto col_def = TransformColumnDefinition(*pg_col_def);
355856
355857 if (pg_col_def->constraints) {
355858 for (auto cell = pg_col_def->constraints->head; cell != nullptr; cell = cell->next) {
355859 auto pg_constraint = PGPointerCast<duckdb_libpgquery::PGConstraint>(cell->data.ptr_value);
355860 auto constraint = TransformConstraint(*pg_constraint, col_def, info->columns.LogicalColumnCount());
355861 if (constraint) {
355862 info->constraints.push_back(std::move(constraint));
355863 }
355864 }
355865 }
355866
355867 info->columns.AddColumn(std::move(col_def));
355868 column_count++;
355869 break;
355870 }
355871 case duckdb_libpgquery::T_PGConstraint: {
355872 auto pg_constraint = PGPointerCast<duckdb_libpgquery::PGConstraint>(c->data.ptr_value);
355873 info->constraints.push_back(TransformConstraint(*pg_constraint));
355874 break;
355875 }
355876 default:
355877 throw NotImplementedException("ColumnDef type not handled yet");
355878 }
355879 }
355880
355881 vector<unique_ptr<ParsedExpression>> partition_keys;
355882 if (stmt.partition_list) {
355883 TransformExpressionList(*stmt.partition_list, partition_keys);
355884 }
355885 info->partition_keys = std::move(partition_keys);
355886
355887 vector<unique_ptr<ParsedExpression>> order_keys;
355888 if (stmt.sort_list) {
355889 TransformExpressionList(*stmt.sort_list, order_keys);
355890 }
355891 info->sort_keys = std::move(order_keys);
355892
355893 if (stmt.options) {
355894 TransformTableOptions(info->options, stmt.options);
355895 }
355896
355897 if (!column_count) {
355898 throw ParserException("Table must have at least one column!");
355899 }
355900
355901 result->info = std::move(info);
355902 return result;
355903}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCreateTableAs()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateTableAs ( duckdb_libpgquery::PGCreateTableAsStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGCreateStmt node into a CreateStatement.

355916 {
355917 if (stmt.relkind == duckdb_libpgquery::PG_OBJECT_MATVIEW) {
355918 throw NotImplementedException("Materialized view not implemented");
355919 }
355920 if (stmt.is_select_into) {
355921 throw NotImplementedException("Unimplemented features for CREATE TABLE as");
355922 }
355923 if (stmt.query->type != duckdb_libpgquery::T_PGSelectStmt) {
355924 throw ParserException("CREATE TABLE AS requires a SELECT clause");
355925 }
355926
355927 auto result = make_uniq<CreateStatement>();
355928 auto info = make_uniq<CreateTableInfo>();
355929 auto qname = TransformQualifiedName(*stmt.into->rel);
355930 if (qname.name.empty()) {
355931 throw ParserException("Empty table name not supported");
355932 }
355933 auto query = TransformSelectStmt(*stmt.query, false);
355934
355935 vector<unique_ptr<ParsedExpression>> partition_keys;
355936 if (stmt.into->partition_list) {
355937 TransformExpressionList(*stmt.into->partition_list, partition_keys);
355938 }
355939 info->partition_keys = std::move(partition_keys);
355940
355941 vector<unique_ptr<ParsedExpression>> order_keys;
355942 if (stmt.into->sort_list) {
355943 TransformExpressionList(*stmt.into->sort_list, order_keys);
355944 }
355945 info->sort_keys = std::move(order_keys);
355946
355947 if (stmt.into->options) {
355948 TransformTableOptions(info->options, *stmt.into->options);
355949 }
355950
355951 // push a LIMIT 0 if 'WITH NO DATA' is specified
355952 if (stmt.into->skipData) {
355953 auto limit_modifier = make_uniq<LimitModifier>();
355954 limit_modifier->limit = make_uniq<ConstantExpression>(Value::BIGINT(0));
355955 auto limit_node = make_uniq<SelectNode>();
355956 limit_node->modifiers.push_back(std::move(limit_modifier));
355957 limit_node->from_table = make_uniq<SubqueryRef>(std::move(query));
355958 limit_node->select_list.push_back(make_uniq<StarExpression>());
355959 auto limit_stmt = make_uniq<SelectStatement>();
355960 limit_stmt->node = std::move(limit_node);
355961 query = std::move(limit_stmt);
355962 }
355963
355964 if (stmt.into->colNames) {
355965 auto cols = TransformStringList(stmt.into->colNames);
355966 for (idx_t i = 0; i < cols.size(); i++) {
355967 // We really don't know the type of the columns during parsing, so we just use UNKNOWN
355968 info->columns.AddColumn(ColumnDefinition(cols[i], LogicalType::UNKNOWN));
355969 }
355970 }
355971
355972 info->catalog = qname.catalog;
355973 info->schema = qname.schema;
355974 info->table = qname.name;
355975 info->on_conflict = TransformOnConflict(stmt.onconflict);
355976 info->temporary =
355977 stmt.into->rel->relpersistence == duckdb_libpgquery::PGPostgresRelPersistence::PG_RELPERSISTENCE_TEMP;
355978 info->query = std::move(query);
355979 result->info = std::move(info);
355980 return result;
355981}
static DUCKDB_API Value BIGINT(int64_t value)
Create a bigint Value from a specified value.
Definition duckdb.cpp:132146
int cols
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCreateSchema()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateSchema ( duckdb_libpgquery::PGCreateSchemaStmt & stmt)
private

Transform a Postgres node into a CreateStatement.

355572 {
355573 auto result = make_uniq<CreateStatement>();
355574 auto info = make_uniq<CreateSchemaInfo>();
355575
355576 D_ASSERT(stmt.schemaname);
355577 info->catalog = stmt.catalogname ? stmt.catalogname : INVALID_CATALOG;
355578 info->schema = stmt.schemaname;
355579 info->on_conflict = TransformOnConflict(stmt.onconflict);
355580
355581 if (stmt.schemaElts) {
355582 // schema elements
355583 for (auto cell = stmt.schemaElts->head; cell != nullptr; cell = cell->next) {
355584 auto node = PGPointerCast<duckdb_libpgquery::PGNode>(cell->data.ptr_value);
355585 switch (node->type) {
355586 case duckdb_libpgquery::T_PGCreateStmt:
355587 case duckdb_libpgquery::T_PGViewStmt:
355588 default:
355589 throw NotImplementedException("Schema element not supported yet!");
355590 }
355591 }
355592 }
355593 result->info = std::move(info);
355594 return result;
355595}
Here is the caller graph for this function:

◆ TransformCreateSequence()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateSequence ( duckdb_libpgquery::PGCreateSeqStmt & node)
private

Transform a Postgres duckdb_libpgquery::T_PGCreateSeqStmt node into a CreateStatement.

355607 {
355608 auto result = make_uniq<CreateStatement>();
355609 auto info = make_uniq<CreateSequenceInfo>();
355610
355611 auto qname = TransformQualifiedName(*stmt.sequence);
355612 info->catalog = qname.catalog;
355613 info->schema = qname.schema;
355614 info->name = qname.name;
355615
355616 if (stmt.options) {
355617 int64_t default_start_value = info->start_value;
355618 bool has_start_value = false;
355619 unordered_set<SequenceInfo, EnumClassHash> used;
355620 duckdb_libpgquery::PGListCell *cell = nullptr;
355621 for_each_cell(cell, stmt.options->head) {
355622 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
355623 string opt_name = string(def_elem->defname);
355624 auto val = PGPointerCast<duckdb_libpgquery::PGValue>(def_elem->arg);
355625 bool nodef = def_elem->defaction == duckdb_libpgquery::PG_DEFELEM_UNSPEC && !val; // e.g. NO MINVALUE
355626 int64_t opt_value = 0;
355627
355628 if (val) {
355629 if (val->type == duckdb_libpgquery::T_PGInteger) {
355630 opt_value = val->val.ival;
355631 } else if (val->type == duckdb_libpgquery::T_PGFloat) {
355632 if (!TryCast::Operation<string_t, int64_t>(string_t(val->val.str), opt_value, true)) {
355633 throw ParserException("Expected an integer argument for option %s", opt_name);
355634 }
355635 } else {
355636 throw ParserException("Expected an integer argument for option %s", opt_name);
355637 }
355638 }
355639 if (opt_name == "increment") {
355640 if (used.find(SequenceInfo::SEQ_INC) != used.end()) {
355641 throw ParserException("Increment should be passed at most once");
355642 }
355643 used.insert(SequenceInfo::SEQ_INC);
355644 if (nodef) {
355645 continue;
355646 }
355647
355648 info->increment = opt_value;
355649 if (info->increment == 0) {
355650 throw ParserException("Increment must not be zero");
355651 }
355652 if (info->increment < 0) {
355653 default_start_value = info->max_value = -1;
355654 info->min_value = NumericLimits<int64_t>::Minimum();
355655 } else {
355656 default_start_value = info->min_value = 1;
355657 info->max_value = NumericLimits<int64_t>::Maximum();
355658 }
355659 } else if (opt_name == "minvalue") {
355660 if (used.find(SequenceInfo::SEQ_MIN) != used.end()) {
355661 throw ParserException("Minvalue should be passed at most once");
355662 }
355663 used.insert(SequenceInfo::SEQ_MIN);
355664 if (nodef) {
355665 continue;
355666 }
355667
355668 info->min_value = opt_value;
355669 if (info->increment > 0) {
355670 default_start_value = info->min_value;
355671 }
355672 } else if (opt_name == "maxvalue") {
355673 if (used.find(SequenceInfo::SEQ_MAX) != used.end()) {
355674 throw ParserException("Maxvalue should be passed at most once");
355675 }
355676 used.insert(SequenceInfo::SEQ_MAX);
355677 if (nodef) {
355678 continue;
355679 }
355680
355681 info->max_value = opt_value;
355682 if (info->increment < 0) {
355683 default_start_value = info->max_value;
355684 }
355685 } else if (opt_name == "start") {
355686 if (used.find(SequenceInfo::SEQ_START) != used.end()) {
355687 throw ParserException("Start should be passed at most once");
355688 }
355689 used.insert(SequenceInfo::SEQ_START);
355690 if (nodef) {
355691 continue;
355692 }
355693 has_start_value = true;
355694 info->start_value = opt_value;
355695 } else if (opt_name == "cycle") {
355696 if (used.find(SequenceInfo::SEQ_CYCLE) != used.end()) {
355697 throw ParserException("Cycle should be passed at most once");
355698 }
355699 used.insert(SequenceInfo::SEQ_CYCLE);
355700 if (nodef) {
355701 continue;
355702 }
355703
355704 info->cycle = opt_value > 0;
355705 } else {
355706 throw ParserException("Unrecognized option \"%s\" for CREATE SEQUENCE", opt_name);
355707 }
355708 }
355709 if (!has_start_value) {
355710 info->start_value = default_start_value;
355711 }
355712 }
355713 info->temporary = !stmt.sequence->relpersistence;
355714 info->on_conflict = TransformOnConflict(stmt.onconflict);
355715 if (info->max_value <= info->min_value) {
355716 throw ParserException("MINVALUE (%lld) must be less than MAXVALUE (%lld)", info->min_value, info->max_value);
355717 }
355718 if (info->start_value < info->min_value) {
355719 throw ParserException("START value (%lld) cannot be less than MINVALUE (%lld)", info->start_value,
355720 info->min_value);
355721 }
355722 if (info->start_value > info->max_value) {
355723 throw ParserException("START value (%lld) cannot be greater than MAXVALUE (%lld)", info->start_value,
355724 info->max_value);
355725 }
355726 result->info = std::move(info);
355727 return result;
355728}
::int64_t int64_t
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCreateView()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateView ( duckdb_libpgquery::PGViewStmt & node)
private

Transform a Postgres duckdb_libpgquery::T_PGViewStmt node into a CreateStatement.

356065 {
356066 D_ASSERT(stmt.type == duckdb_libpgquery::T_PGViewStmt);
356067 D_ASSERT(stmt.view);
356068
356069 auto result = make_uniq<CreateStatement>();
356070 auto info = make_uniq<CreateViewInfo>();
356071
356072 auto qname = TransformQualifiedName(*stmt.view);
356073 info->catalog = qname.catalog;
356074 info->schema = qname.schema;
356075 info->view_name = qname.name;
356076 info->temporary = !stmt.view->relpersistence;
356077 if (info->temporary && IsInvalidCatalog(info->catalog)) {
356078 info->catalog = TEMP_CATALOG;
356079 }
356080 info->on_conflict = TransformOnConflict(stmt.onconflict);
356081
356082 info->query = TransformSelectStmt(*stmt.query, false);
356083
356084 PivotEntryCheck("view");
356085
356086 if (stmt.aliases && stmt.aliases->length > 0) {
356087 for (auto c = stmt.aliases->head; c != nullptr; c = lnext(c)) {
356088 auto val = PGPointerCast<duckdb_libpgquery::PGValue>(c->data.ptr_value);
356089 switch (val->type) {
356090 case duckdb_libpgquery::T_PGString: {
356091 info->aliases.emplace_back(val->val.str);
356092 break;
356093 }
356094 default:
356095 throw NotImplementedException("View projection type");
356096 }
356097 }
356098 if (info->aliases.empty()) {
356099 throw ParserException("Need at least one column name in CREATE VIEW projection list");
356100 }
356101 }
356102
356103 if (stmt.options && stmt.options->length > 0) {
356104 throw NotImplementedException("VIEW options");
356105 }
356106
356107 if (stmt.withCheckOption != duckdb_libpgquery::PGViewCheckOption::PG_NO_CHECK_OPTION) {
356108 throw NotImplementedException("VIEW CHECK options");
356109 }
356110 result->info = std::move(info);
356111 return result;
356112}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCreateIndex()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateIndex ( duckdb_libpgquery::PGIndexStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGIndexStmt node into CreateStatement.

355508 {
355509 auto result = make_uniq<CreateStatement>();
355510 auto info = make_uniq<CreateIndexInfo>();
355511 if (stmt.unique) {
355512 info->constraint_type = IndexConstraintType::UNIQUE;
355513 } else {
355514 info->constraint_type = IndexConstraintType::NONE;
355515 }
355516
355517 info->on_conflict = TransformOnConflict(stmt.onconflict);
355518 info->expressions = TransformIndexParameters(*stmt.indexParams, stmt.relation->relname);
355519
355520 info->index_type = StringUtil::Upper(string(stmt.accessMethod));
355521
355522 if (stmt.relation->schemaname) {
355523 info->schema = stmt.relation->schemaname;
355524 }
355525 if (stmt.relation->catalogname) {
355526 info->catalog = stmt.relation->catalogname;
355527 }
355528 info->table = stmt.relation->relname;
355529 if (stmt.idxname) {
355530 info->index_name = stmt.idxname;
355531 } else {
355532 throw NotImplementedException("Please provide an index name, e.g., CREATE INDEX my_name ...");
355533 }
355534
355535 // Parse the options list
355536 if (stmt.options) {
355537 duckdb_libpgquery::PGListCell *cell;
355538 for_each_cell(cell, stmt.options->head) {
355539 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
355540 Value val;
355541 if (def_elem->arg) {
355542 auto expr = TransformExpression(def_elem->arg);
355543 if (expr->expression_class != ExpressionClass::CONSTANT) {
355544 throw InvalidInputException("Create index option must be a constant value");
355545 }
355546 auto &const_expr = expr->Cast<ConstantExpression>();
355547 val = const_expr.value;
355548 } else {
355549 val = Value::BOOLEAN(true);
355550 }
355551 info->options[StringUtil::Lower(def_elem->defname)] = std::move(val);
355552 }
355553 }
355554
355555 for (auto &expr : info->expressions) {
355556 info->parsed_expressions.emplace_back(expr->Copy());
355557 }
355558 if (stmt.whereClause) {
355559 throw NotImplementedException("Creating partial indexes is not supported currently");
355560 }
355561 result->info = std::move(info);
355562 return result;
355563}
static DUCKDB_API string Lower(const string &str)
Convert a string to lowercase.
Definition duckdb.cpp:109630
static DUCKDB_API string Upper(const string &str)
Convert a string to UPPERCASE.
Definition duckdb.cpp:109624
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCreateFunction()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateFunction ( duckdb_libpgquery::PGCreateFunctionStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGCreateFunctionStmt node into CreateStatement.

355429 {
355430 D_ASSERT(stmt.type == duckdb_libpgquery::T_PGCreateFunctionStmt);
355431 D_ASSERT(stmt.functions);
355432
355433 auto result = make_uniq<CreateStatement>();
355434 auto qname = TransformQualifiedName(*stmt.name);
355435
355436 vector<unique_ptr<MacroFunction>> macros;
355437 for (auto c = stmt.functions->head; c != nullptr; c = lnext(c)) {
355438 auto &function_def = *PGPointerCast<duckdb_libpgquery::PGFunctionDefinition>(c->data.ptr_value);
355439 macros.push_back(TransformMacroFunction(function_def));
355440 }
355441 PivotEntryCheck("macro");
355442
355443 auto catalog_type =
355444 macros[0]->type == MacroType::SCALAR_MACRO ? CatalogType::MACRO_ENTRY : CatalogType::TABLE_MACRO_ENTRY;
355445 auto info = make_uniq<CreateMacroInfo>(catalog_type);
355446 info->catalog = qname.catalog;
355447 info->schema = qname.schema;
355448 info->name = qname.name;
355449
355450 // temporary macro
355451 switch (stmt.name->relpersistence) {
355452 case duckdb_libpgquery::PG_RELPERSISTENCE_TEMP:
355453 info->temporary = true;
355454 break;
355455 case duckdb_libpgquery::PG_RELPERSISTENCE_UNLOGGED:
355456 throw ParserException("Unlogged flag not supported for macros: '%s'", qname.name);
355457 case duckdb_libpgquery::RELPERSISTENCE_PERMANENT:
355458 info->temporary = false;
355459 break;
355460 default:
355461 throw ParserException("Unsupported persistence flag for table '%s'", qname.name);
355462 }
355463
355464 // what to do on conflict
355465 info->on_conflict = TransformOnConflict(stmt.onconflict);
355466 info->macros = std::move(macros);
355467
355468 result->info = std::move(info);
355469
355470 return result;
355471}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCreateType()

unique_ptr< CreateStatement > duckdb::Transformer::TransformCreateType ( duckdb_libpgquery::PGCreateTypeStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement.

356019 {
356020 auto result = make_uniq<CreateStatement>();
356021 auto info = make_uniq<CreateTypeInfo>();
356022
356023 auto qualified_name = TransformQualifiedName(*stmt.typeName);
356024 info->catalog = qualified_name.catalog;
356025 info->schema = qualified_name.schema;
356026 info->name = qualified_name.name;
356027 info->temporary = !stmt.typeName->relpersistence;
356028 info->on_conflict = TransformOnConflict(stmt.onconflict);
356029
356030 switch (stmt.kind) {
356031 case duckdb_libpgquery::PG_NEWTYPE_ENUM: {
356032 info->internal = false;
356033 if (stmt.query) {
356034 // CREATE TYPE mood AS ENUM (SELECT ...)
356035 D_ASSERT(stmt.vals == nullptr);
356036 auto query = TransformSelectStmt(*stmt.query, false);
356037 info->query = std::move(query);
356038 info->type = LogicalType::INVALID;
356039 } else {
356040 D_ASSERT(stmt.query == nullptr);
356041 idx_t size = 0;
356042 auto ordered_array = PGListToVector(stmt.vals, size);
356043 info->type = LogicalType::ENUM(ordered_array, size);
356044 }
356045 } break;
356046
356047 case duckdb_libpgquery::PG_NEWTYPE_ALIAS: {
356048 LogicalType target_type = TransformTypeName(*stmt.ofType);
356049 info->type = target_type;
356050 } break;
356051
356052 default:
356053 throw InternalException("Unknown kind of new type");
356054 }
356055 result->info = std::move(info);
356056 return result;
356057}
LogicalType TransformTypeName(duckdb_libpgquery::PGTypeName &name)
Transform a Postgres TypeName string into a LogicalType.
Definition duckdb.cpp:354725
GOpaque< Size > size(const GMat &src)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCommentOn()

unique_ptr< AlterStatement > duckdb::Transformer::TransformCommentOn ( duckdb_libpgquery::PGCommentOnStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGCreateTypeStmt node into CreateStatement.

355178 {
355179 QualifiedName qualified_name;
355180 string column_name;
355181
355182 if (stmt.object_type != duckdb_libpgquery::PG_OBJECT_COLUMN) {
355183 qualified_name = TransformQualifiedName(*stmt.name);
355184 } else {
355185 auto transformed_expr = TransformExpression(stmt.column_expr);
355186
355187 if (transformed_expr->GetExpressionType() != ExpressionType::COLUMN_REF) {
355188 throw ParserException("Unexpected expression found, expected column reference to comment on (e.g. "
355189 "'schema.table.column'), found '%s'",
355190 transformed_expr->ToString());
355191 }
355192
355193 auto colref_expr = transformed_expr->Cast<ColumnRefExpression>();
355194
355195 if (colref_expr.column_names.size() > 4) {
355196 throw ParserException("Invalid column reference: '%s', too many dots", colref_expr.ToString());
355197 }
355198 if (colref_expr.column_names.size() < 2) {
355199 throw ParserException("Invalid column reference: '%s', please specify a table", colref_expr.ToString());
355200 }
355201
355202 column_name = colref_expr.GetColumnName();
355203 qualified_name.name = colref_expr.column_names.size() > 1 ? colref_expr.GetTableName() : "";
355204
355205 if (colref_expr.column_names.size() == 4) {
355206 qualified_name.catalog = colref_expr.column_names[0];
355207 qualified_name.schema = colref_expr.column_names[1];
355208 } else if (colref_expr.column_names.size() == 3) {
355209 qualified_name.schema = colref_expr.column_names[0];
355210 }
355211 }
355212
355213 auto res = make_uniq<AlterStatement>();
355214 unique_ptr<AlterInfo> info;
355215
355216 auto expr = TransformExpression(stmt.value);
355217 if (expr->GetExpressionClass() != ExpressionClass::CONSTANT) {
355218 throw NotImplementedException("Can only use constants as comments");
355219 }
355220 auto comment_value = expr->Cast<ConstantExpression>().value;
355221
355222 CatalogType type = CatalogType::INVALID;
355223
355224 // Regular CatalogTypes
355225 switch (stmt.object_type) {
355226 case duckdb_libpgquery::PG_OBJECT_TABLE:
355227 type = CatalogType::TABLE_ENTRY;
355228 break;
355229 case duckdb_libpgquery::PG_OBJECT_INDEX:
355230 type = CatalogType::INDEX_ENTRY;
355231 break;
355232 case duckdb_libpgquery::PG_OBJECT_VIEW:
355233 type = CatalogType::VIEW_ENTRY;
355234 break;
355235 case duckdb_libpgquery::PG_OBJECT_FUNCTION:
355236 type = CatalogType::MACRO_ENTRY;
355237 break;
355238 case duckdb_libpgquery::PG_OBJECT_TABLE_MACRO:
355239 type = CatalogType::TABLE_MACRO_ENTRY;
355240 break;
355241 case duckdb_libpgquery::PG_OBJECT_SEQUENCE:
355242 type = CatalogType::SEQUENCE_ENTRY;
355243 break;
355244 case duckdb_libpgquery::PG_OBJECT_TYPE:
355245 type = CatalogType::TYPE_ENTRY;
355246 break;
355247 default:
355248 break;
355249 }
355250
355251 if (type != CatalogType::INVALID) {
355252 info = make_uniq<SetCommentInfo>(type, qualified_name.catalog, qualified_name.schema, qualified_name.name,
355253 comment_value, OnEntryNotFound::THROW_EXCEPTION);
355254 } else if (stmt.object_type == duckdb_libpgquery::PG_OBJECT_COLUMN) {
355255 info = make_uniq<SetColumnCommentInfo>(qualified_name.catalog, qualified_name.schema, qualified_name.name,
355256 column_name, comment_value, OnEntryNotFound::THROW_EXCEPTION);
355257 } else if (stmt.object_type == duckdb_libpgquery::PG_OBJECT_DATABASE) {
355258 throw NotImplementedException("Adding comments to databases is not implemented");
355259 } else if (stmt.object_type == duckdb_libpgquery::PG_OBJECT_SCHEMA) {
355260 throw NotImplementedException("Adding comments to schemas is not implemented");
355261 }
355262
355263 if (info) {
355264 res->info = std::move(info);
355265 return res;
355266 }
355267
355268 throw NotImplementedException("Can not comment on this type");
355269}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformAlterSequence()

unique_ptr< AlterStatement > duckdb::Transformer::TransformAlterSequence ( duckdb_libpgquery::PGAlterSeqStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGAlterSeqStmt node into CreateStatement.

354776 {
354777 auto result = make_uniq<AlterStatement>();
354778
354779 auto qname = TransformQualifiedName(*stmt.sequence);
354780 auto sequence_catalog = qname.catalog;
354781 auto sequence_schema = qname.schema;
354782 auto sequence_name = qname.name;
354783
354784 if (!stmt.options) {
354785 throw InternalException("Expected an argument for ALTER SEQUENCE.");
354786 }
354787
354788 unordered_set<SequenceInfo, EnumClassHash> used;
354789 duckdb_libpgquery::PGListCell *cell;
354790 for_each_cell(cell, stmt.options->head) {
354791 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
354792 string opt_name = string(def_elem->defname);
354793
354794 if (opt_name == "owned_by") {
354795 if (used.find(SequenceInfo::SEQ_OWN) != used.end()) {
354796 throw ParserException("Owned by value should be passed at most once");
354797 }
354798 used.insert(SequenceInfo::SEQ_OWN);
354799
354800 auto val = PGPointerCast<duckdb_libpgquery::PGList>(def_elem->arg);
354801 if (!val) {
354802 throw InternalException("Expected an argument for option %s", opt_name);
354803 }
354804 D_ASSERT(val);
354805 if (val->type != duckdb_libpgquery::T_PGList) {
354806 throw InternalException("Expected a string argument for option %s", opt_name);
354807 }
354808 auto opt_values = vector<string>();
354809
354810 for (auto c = val->head; c != nullptr; c = lnext(c)) {
354811 auto target = PGPointerCast<duckdb_libpgquery::PGResTarget>(c->data.ptr_value);
354812 opt_values.emplace_back(target->name);
354813 }
354814 D_ASSERT(!opt_values.empty());
354815 string owner_schema = INVALID_SCHEMA;
354816 string owner_name;
354817 if (opt_values.size() == 2) {
354818 owner_schema = opt_values[0];
354819 owner_name = opt_values[1];
354820 } else if (opt_values.size() == 1) {
354821 owner_schema = DEFAULT_SCHEMA;
354822 owner_name = opt_values[0];
354823 } else {
354824 throw InternalException("Wrong argument for %s. Expected either <schema>.<name> or <name>", opt_name);
354825 }
354826 auto info = make_uniq<ChangeOwnershipInfo>(CatalogType::SEQUENCE_ENTRY, sequence_catalog, sequence_schema,
354827 sequence_name, owner_schema, owner_name,
354828 TransformOnEntryNotFound(stmt.missing_ok));
354829 result->info = std::move(info);
354830 } else {
354831 throw NotImplementedException("ALTER SEQUENCE option not supported yet!");
354832 }
354833 }
354834 result->info->if_not_found = TransformOnEntryNotFound(stmt.missing_ok);
354835 return result;
354836}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformDrop()

unique_ptr< SQLStatement > duckdb::Transformer::TransformDrop ( duckdb_libpgquery::PGDropStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGDropStmt node into a Drop[Table,Schema]Statement.

356171 {
356172 auto result = make_uniq<DropStatement>();
356173 auto &info = *result->info.get();
356174 if (stmt.objects->length != 1) {
356175 throw NotImplementedException("Can only drop one object at a time");
356176 }
356177 switch (stmt.removeType) {
356178 case duckdb_libpgquery::PG_OBJECT_TABLE:
356179 info.type = CatalogType::TABLE_ENTRY;
356180 break;
356181 case duckdb_libpgquery::PG_OBJECT_SCHEMA:
356182 info.type = CatalogType::SCHEMA_ENTRY;
356183 break;
356184 case duckdb_libpgquery::PG_OBJECT_INDEX:
356185 info.type = CatalogType::INDEX_ENTRY;
356186 break;
356187 case duckdb_libpgquery::PG_OBJECT_VIEW:
356188 info.type = CatalogType::VIEW_ENTRY;
356189 break;
356190 case duckdb_libpgquery::PG_OBJECT_SEQUENCE:
356191 info.type = CatalogType::SEQUENCE_ENTRY;
356192 break;
356193 case duckdb_libpgquery::PG_OBJECT_FUNCTION:
356194 info.type = CatalogType::MACRO_ENTRY;
356195 break;
356196 case duckdb_libpgquery::PG_OBJECT_TABLE_MACRO:
356197 info.type = CatalogType::TABLE_MACRO_ENTRY;
356198 break;
356199 case duckdb_libpgquery::PG_OBJECT_TYPE:
356200 info.type = CatalogType::TYPE_ENTRY;
356201 break;
356202 default:
356203 throw NotImplementedException("Cannot drop this type yet");
356204 }
356205
356206 switch (stmt.removeType) {
356207 case duckdb_libpgquery::PG_OBJECT_SCHEMA: {
356208 auto view_list = PGPointerCast<duckdb_libpgquery::PGList>(stmt.objects->head->data.ptr_value);
356209 if (view_list->length == 2) {
356210 info.catalog = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->data.ptr_value)->val.str;
356211 info.name = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->next->data.ptr_value)->val.str;
356212 } else if (view_list->length == 1) {
356213 info.name = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->data.ptr_value)->val.str;
356214 } else {
356215 throw ParserException("Expected \"catalog.schema\" or \"schema\"");
356216 }
356217 break;
356218 }
356219 default: {
356220 auto view_list = PGPointerCast<duckdb_libpgquery::PGList>(stmt.objects->head->data.ptr_value);
356221 if (view_list->length == 3) {
356222 info.catalog = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->data.ptr_value)->val.str;
356223 info.schema = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->next->data.ptr_value)->val.str;
356224 info.name = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->next->next->data.ptr_value)->val.str;
356225 } else if (view_list->length == 2) {
356226 info.schema = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->data.ptr_value)->val.str;
356227 info.name = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->next->data.ptr_value)->val.str;
356228 } else if (view_list->length == 1) {
356229 info.name = PGPointerCast<duckdb_libpgquery::PGValue>(view_list->head->data.ptr_value)->val.str;
356230 } else {
356231 throw ParserException("Expected \"catalog.schema.name\", \"schema.name\"or \"name\"");
356232 }
356233 break;
356234 }
356235 }
356236 info.cascade = stmt.behavior == duckdb_libpgquery::PGDropBehavior::PG_DROP_CASCADE;
356237 info.if_not_found = TransformOnEntryNotFound(stmt.missing_ok);
356238 return std::move(result);
356239}
Here is the caller graph for this function:

◆ TransformInsert()

unique_ptr< InsertStatement > duckdb::Transformer::TransformInsert ( duckdb_libpgquery::PGInsertStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGInsertStmt node into a InsertStatement.

356402 {
356403 auto result = make_uniq<InsertStatement>();
356404 if (stmt.withClause) {
356405 TransformCTE(*PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause), result->cte_map);
356406 }
356407
356408 // first check if there are any columns specified
356409 if (stmt.cols) {
356410 result->columns = TransformInsertColumns(*stmt.cols);
356411 }
356412
356413 // Grab and transform the returning columns from the parser.
356414 if (stmt.returningList) {
356415 TransformExpressionList(*stmt.returningList, result->returning_list);
356416 }
356417 if (stmt.selectStmt) {
356418 result->select_statement = TransformSelectStmt(*stmt.selectStmt, false);
356419 } else {
356420 result->default_values = true;
356421 }
356422
356423 auto qname = TransformQualifiedName(*stmt.relation);
356424 result->table = qname.name;
356425 result->schema = qname.schema;
356426
356427 if (stmt.onConflictClause) {
356428 if (stmt.onConflictAlias != duckdb_libpgquery::PG_ONCONFLICT_ALIAS_NONE) {
356429 // OR REPLACE | OR IGNORE are shorthands for the ON CONFLICT clause
356430 throw ParserException("You can not provide both OR REPLACE|IGNORE and an ON CONFLICT clause, please remove "
356431 "the first if you want to have more granual control");
356432 }
356433 result->on_conflict_info = TransformOnConflictClause(stmt.onConflictClause, result->schema);
356434 result->table_ref = TransformRangeVar(*stmt.relation);
356435 }
356436 if (stmt.onConflictAlias != duckdb_libpgquery::PG_ONCONFLICT_ALIAS_NONE) {
356437 D_ASSERT(!stmt.onConflictClause);
356438 result->on_conflict_info = DummyOnConflictClause(stmt.onConflictAlias, result->schema);
356439 result->table_ref = TransformRangeVar(*stmt.relation);
356440 }
356441 result->column_order = TransformColumnOrder(stmt.insert_column_order);
356442 result->catalog = qname.catalog;
356443 return result;
356444}
unique_ptr< OnConflictInfo > DummyOnConflictClause(duckdb_libpgquery::PGOnConflictActionAlias type, const string &relname)
Transform a ON CONFLICT shorthand into a OnConflictInfo.
Definition duckdb.cpp:357727
unique_ptr< OnConflictInfo > TransformOnConflictClause(duckdb_libpgquery::PGOnConflictClause *node, const string &relname)
Transform a Postgres duckdb_libpgquery::T_PGOnConflictClause node into a OnConflictInfo.
Definition duckdb.cpp:357748
unique_ptr< TableRef > TransformRangeVar(duckdb_libpgquery::PGRangeVar &root)
Transform a Postgres table reference into a TableRef.
Definition duckdb.cpp:357855
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformColumnOrder()

InsertColumnOrder duckdb::Transformer::TransformColumnOrder ( duckdb_libpgquery::PGInsertColumnOrder insert_column_order)
private
356391 {
356392 switch (insert_column_order) {
356393 case duckdb_libpgquery::PG_INSERT_BY_POSITION:
356394 return InsertColumnOrder::INSERT_BY_POSITION;
356395 case duckdb_libpgquery::PG_INSERT_BY_NAME:
356396 return InsertColumnOrder::INSERT_BY_NAME;
356397 default:
356398 throw InternalException("Unrecognized insert column order in TransformInsert");
356399 }
356400}

◆ TransformInsertColumns()

vector< string > duckdb::Transformer::TransformInsertColumns ( duckdb_libpgquery::PGList & cols)
private
356382 {
356383 vector<string> result;
356384 for (auto c = cols.head; c != nullptr; c = lnext(c)) {
356385 auto target = PGPointerCast<duckdb_libpgquery::PGResTarget>(c->data.ptr_value);
356386 result.emplace_back(target->name);
356387 }
356388 return result;
356389}

◆ TransformOnConflictClause()

unique_ptr< OnConflictInfo > duckdb::Transformer::TransformOnConflictClause ( duckdb_libpgquery::PGOnConflictClause * node,
const string & relname )
private

Transform a Postgres duckdb_libpgquery::T_PGOnConflictClause node into a OnConflictInfo.

357749 {
357750 auto stmt = PGPointerCast<duckdb_libpgquery::PGOnConflictClause>(node);
357751 D_ASSERT(stmt);
357752
357753 auto result = make_uniq<OnConflictInfo>();
357754 result->action_type = TransformOnConflictAction(stmt.get());
357755
357756 if (stmt->infer) {
357757 // A filter for the ON CONFLICT ... is specified.
357758 if (!stmt->infer->indexElems) {
357759 throw NotImplementedException("ON CONSTRAINT conflict target is not supported yet");
357760 }
357761 result->indexed_columns = TransformConflictTarget(*stmt->infer->indexElems);
357762 if (stmt->infer->whereClause) {
357763 result->condition = TransformExpression(stmt->infer->whereClause);
357764 }
357765 }
357766
357767 if (result->action_type == OnConflictAction::UPDATE) {
357768 result->set_info = TransformUpdateSetInfo(stmt->targetList, stmt->whereClause);
357769 }
357770 return result;
357771}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DummyOnConflictClause()

unique_ptr< OnConflictInfo > duckdb::Transformer::DummyOnConflictClause ( duckdb_libpgquery::PGOnConflictActionAlias type,
const string & relname )
private

Transform a ON CONFLICT shorthand into a OnConflictInfo.

357728 {
357729 switch (type) {
357730 case duckdb_libpgquery::PGOnConflictActionAlias::PG_ONCONFLICT_ALIAS_REPLACE: {
357731 // This can not be fully resolved yet until the bind stage
357732 auto result = make_uniq<OnConflictInfo>();
357733 result->action_type = OnConflictAction::REPLACE;
357734 return result;
357735 }
357736 case duckdb_libpgquery::PGOnConflictActionAlias::PG_ONCONFLICT_ALIAS_IGNORE: {
357737 // We can just fully replace this with DO NOTHING, and be done with it
357738 auto result = make_uniq<OnConflictInfo>();
357739 result->action_type = OnConflictAction::NOTHING;
357740 return result;
357741 }
357742 default: {
357743 throw InternalException("Type not implemented for PGOnConflictActionAlias");
357744 }
357745 }
357746}
Here is the caller graph for this function:

◆ TransformCopy()

unique_ptr< CopyStatement > duckdb::Transformer::TransformCopy ( duckdb_libpgquery::PGCopyStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGCopyStmt node into a CopyStatement.

355306 {
355307 auto result = make_uniq<CopyStatement>();
355308 auto &info = *result->info;
355309
355310 // get file_path and is_from
355311 info.is_from = stmt.is_from;
355312
355313 info.file_path_expression = TransformExpression(*stmt.filename);
355314 if (info.file_path_expression->type == ExpressionType::VALUE_CONSTANT) {
355315 auto &constant_expr = info.file_path_expression->Cast<ConstantExpression>();
355316 if (constant_expr.value.type().id() == LogicalTypeId::VARCHAR) {
355317 info.file_path = StringValue::Get(constant_expr.value);
355318 info.file_path_expression.reset();
355319 }
355320 }
355321
355322 // get select_list
355323 if (stmt.attlist) {
355324 for (auto n = stmt.attlist->head; n != nullptr; n = n->next) {
355325 auto target = PGPointerCast<duckdb_libpgquery::PGResTarget>(n->data.ptr_value);
355326 if (target->name) {
355327 info.select_list.emplace_back(target->name);
355328 }
355329 }
355330 }
355331
355332 if (stmt.relation) {
355333 auto ref = TransformRangeVar(*stmt.relation);
355334 auto &table = ref->Cast<BaseTableRef>();
355335 info.table = table.table_name;
355336 info.schema = table.schema_name;
355337 info.catalog = table.catalog_name;
355338 } else {
355339 info.select_statement = TransformSelectNode(*stmt.query);
355340 }
355341
355342 // handle the different options of the COPY statement
355343 TransformCopyOptions(info, stmt.options);
355344
355345 return result;
355346}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformCopyOptions()

void duckdb::Transformer::TransformCopyOptions ( CopyInfo & info,
optional_ptr< duckdb_libpgquery::PGList > options )
private
355285 {
355286 if (!options) {
355287 return;
355288 }
355289
355290 duckdb_libpgquery::PGListCell *cell;
355291 // iterate over each option
355292 for_each_cell(cell, options->head) {
355293 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
355294 string name = def_elem->defname;
355295 if (info.parsed_options.find(name) != info.parsed_options.end()) {
355296 throw ParserException("Unexpected duplicate option \"%s\"", name);
355297 }
355298 unique_ptr<ParsedExpression> expr;
355299 if (def_elem->arg) {
355300 expr = TransformExpression(*def_elem->arg);
355301 }
355302 info.parsed_options[name] = std::move(expr);
355303 }
355304}

◆ TransformCreateSecretOptions()

void duckdb::Transformer::TransformCreateSecretOptions ( CreateSecretInfo & info,
optional_ptr< duckdb_libpgquery::PGList > options )
private
357039 {
357040 if (!options) {
357041 return;
357042 }
357043
357044 duckdb_libpgquery::PGListCell *cell;
357045
357046 // iterate over each option
357047 for_each_cell(cell, options->head) {
357048 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
357049 auto lower_name = StringUtil::Lower(def_elem->defname);
357050 if (lower_name == "scope") {
357051 info.scope = TransformExpression(def_elem->arg);
357052 continue;
357053 }
357054 if (lower_name == "type") {
357055 info.type = TransformExpression(def_elem->arg);
357056 if (info.type->type == ExpressionType::COLUMN_REF) {
357057 info.type = make_uniq<ConstantExpression>(GetConstantExpressionValue(*info.type));
357058 }
357059 continue;
357060 }
357061 if (lower_name == "provider") {
357062 info.provider = TransformExpression(def_elem->arg);
357063 if (info.provider->type == ExpressionType::COLUMN_REF) {
357064 info.provider = make_uniq<ConstantExpression>(GetConstantExpressionValue(*info.provider));
357065 }
357066 continue;
357067 }
357068
357069 // All the other options end up in the generic
357070 case_insensitive_map_t<vector<ParsedExpression>> vector_options;
357071
357072 if (info.options.find(lower_name) != info.options.end()) {
357073 throw BinderException("Duplicate query param found while parsing create secret: '%s'", lower_name);
357074 }
357075
357076 auto expr = TransformExpression(def_elem->arg);
357077 if (expr->type == ExpressionType::COLUMN_REF) {
357078 expr = make_uniq<ConstantExpression>(GetConstantExpressionValue(*expr));
357079 }
357080 info.options[lower_name] = std::move(expr);
357081 }
357082}

◆ TransformTableOptions()

void duckdb::Transformer::TransformTableOptions ( case_insensitive_map_t< unique_ptr< ParsedExpression > > & info,
optional_ptr< duckdb_libpgquery::PGList > options,
bool throw_if_value = false )
private
358539 {
358540 if (!pg_options) {
358541 return;
358542 }
358543
358544 duckdb_libpgquery::PGListCell *cell;
358545 for_each_cell(cell, pg_options->head) {
358546 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
358547 auto lower_name = StringUtil::Lower(def_elem->defname);
358548 if (options.find(lower_name) != options.end()) {
358549 throw ParserException("Duplicate table property \"%s\"", lower_name);
358550 }
358551 if (!def_elem->arg) {
358552 options.emplace(lower_name, make_uniq<ConstantExpression>(Value()));
358553 continue;
358554 }
358555 auto expr = TransformExpression(def_elem->arg);
358556 if (throw_if_value) {
358557 throw ParserException("\"%s\"", expr->ToString());
358558 }
358559 options.emplace(lower_name, std::move(expr));
358560 }
358561}

◆ TransformTransaction()

unique_ptr< TransactionStatement > duckdb::Transformer::TransformTransaction ( duckdb_libpgquery::PGTransactionStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGTransactionStmt node into a TransactionStatement.

357610 {
357611 // stmt.transaction_type
357612 auto type = TransformTransactionType(stmt.kind);
357613 auto info = make_uniq<TransactionInfo>(type);
357614 info->modifier = TransformTransactionModifier(stmt.transaction_type);
357615 return make_uniq<TransactionStatement>(std::move(info));
357616}
Here is the caller graph for this function:

◆ TransformDelete()

unique_ptr< DeleteStatement > duckdb::Transformer::TransformDelete ( duckdb_libpgquery::PGDeleteStmt & stmt)
private

Transform a Postgres T_DeleteStatement node into a DeleteStatement.

356120 {
356121 auto result = make_uniq<DeleteStatement>();
356122 if (stmt.withClause) {
356123 TransformCTE(*PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause), result->cte_map);
356124 }
356125
356126 result->condition = TransformExpression(stmt.whereClause);
356127 result->table = TransformRangeVar(*stmt.relation);
356128 if (result->table->type != TableReferenceType::BASE_TABLE) {
356129 throw InvalidInputException("Can only delete from base tables!");
356130 }
356131 if (stmt.usingClause) {
356132 for (auto n = stmt.usingClause->head; n != nullptr; n = n->next) {
356133 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(n->data.ptr_value);
356134 auto using_entry = TransformTableRefNode(*target);
356135 result->using_clauses.push_back(std::move(using_entry));
356136 }
356137 }
356138
356139 if (stmt.returningList) {
356140 TransformExpressionList(*stmt.returningList, result->returning_list);
356141 }
356142
356143 return result;
356144}
unique_ptr< TableRef > TransformTableRefNode(duckdb_libpgquery::PGNode &n)
Transform a Postgres node into a TableRef.
Definition duckdb.cpp:358211
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformUpdate()

unique_ptr< UpdateStatement > duckdb::Transformer::TransformUpdate ( duckdb_libpgquery::PGUpdateStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGUpdateStmt node into a UpdateStatement.

357643 {
357644 auto result = make_uniq<UpdateStatement>();
357645 if (stmt.withClause) {
357646 auto with_clause = PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause);
357647 TransformCTE(*with_clause, result->cte_map);
357648 }
357649
357650 result->table = TransformRangeVar(*stmt.relation);
357651 if (stmt.fromClause) {
357652 result->from_table = TransformFrom(stmt.fromClause);
357653 }
357654 result->set_info = TransformUpdateSetInfo(stmt.targetList, stmt.whereClause);
357655
357656 // Grab and transform the returning columns from the parser.
357657 if (stmt.returningList) {
357658 TransformExpressionList(*stmt.returningList, result->returning_list);
357659 }
357660 return result;
357661}
unique_ptr< TableRef > TransformFrom(optional_ptr< duckdb_libpgquery::PGList > root)
Transform a Postgres FROM clause into a TableRef.
Definition duckdb.cpp:357907
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformUpdateExtensions()

unique_ptr< UpdateExtensionsStatement > duckdb::Transformer::TransformUpdateExtensions ( duckdb_libpgquery::PGUpdateExtensionsStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGUpdateExtensionsStmt node into a UpdateExtensionsStatement.

357664 {
357665 auto result = make_uniq<UpdateExtensionsStatement>();
357666 auto info = make_uniq<UpdateExtensionsInfo>();
357667
357668 if (stmt.extensions) {
357669 auto column_list = PGPointerCast<duckdb_libpgquery::PGList>(stmt.extensions);
357670 for (auto c = column_list->head; c != nullptr; c = c->next) {
357671 auto value = PGPointerCast<duckdb_libpgquery::PGValue>(c->data.ptr_value);
357672 info->extensions_to_update.emplace_back(value->val.str);
357673 }
357674 }
357675
357676 result->info = std::move(info);
357677 return result;
357678}
Here is the caller graph for this function:

◆ TransformPragma()

unique_ptr< SQLStatement > duckdb::Transformer::TransformPragma ( duckdb_libpgquery::PGPragmaStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGPragmaStmt node into a PragmaStatement.

356823 {
356824 auto result = make_uniq<PragmaStatement>();
356825 auto &info = *result->info;
356826
356827 info.name = stmt.name;
356828 // parse the arguments, if any
356829 if (stmt.args) {
356830 for (auto cell = stmt.args->head; cell != nullptr; cell = cell->next) {
356831 auto node = PGPointerCast<duckdb_libpgquery::PGNode>(cell->data.ptr_value);
356832 auto expr = TransformExpression(node);
356833
356834 if (expr->GetExpressionType() == ExpressionType::COMPARE_EQUAL) {
356835 auto &comp = expr->Cast<ComparisonExpression>();
356836 if (comp.left->GetExpressionType() != ExpressionType::COLUMN_REF) {
356837 throw ParserException("Named parameter requires a column reference on the LHS");
356838 }
356839 auto &columnref = comp.left->Cast<ColumnRefExpression>();
356840 info.named_parameters.insert(make_pair(columnref.GetName(), std::move(comp.right)));
356841 } else if (expr->GetExpressionType() == ExpressionType::COLUMN_REF) {
356842 auto &colref = expr->Cast<ColumnRefExpression>();
356843 if (!colref.IsQualified()) {
356844 info.parameters.emplace_back(make_uniq<ConstantExpression>(Value(colref.GetColumnName())));
356845 } else {
356846 info.parameters.emplace_back(make_uniq<ConstantExpression>(Value(expr->ToString())));
356847 }
356848 } else {
356849 info.parameters.emplace_back(std::move(expr));
356850 }
356851 }
356852 }
356853 // now parse the pragma type
356854 switch (stmt.kind) {
356855 case duckdb_libpgquery::PG_PRAGMA_TYPE_NOTHING: {
356856 if (!info.parameters.empty() || !info.named_parameters.empty()) {
356857 throw InternalException("PRAGMA statement that is not a call or assignment cannot contain parameters");
356858 }
356859 break;
356860 case duckdb_libpgquery::PG_PRAGMA_TYPE_ASSIGNMENT:
356861 if (info.parameters.size() != 1) {
356862 throw ParserException("PRAGMA statement with assignment should contain exactly one parameter");
356863 }
356864 if (!info.named_parameters.empty()) {
356865 throw ParserException("PRAGMA statement with assignment cannot have named parameters");
356866 }
356867 // SQLite does not distinguish between:
356868 // "PRAGMA table_info='integers'"
356869 // "PRAGMA table_info('integers')"
356870 // for compatibility, any pragmas that match the SQLite ones are parsed as calls
356871 case_insensitive_set_t sqlite_compat_pragmas {"table_info"};
356872 if (sqlite_compat_pragmas.find(info.name) != sqlite_compat_pragmas.end()) {
356873 break;
356874 }
356875 auto set_statement =
356876 make_uniq<SetVariableStatement>(info.name, std::move(info.parameters[0]), SetScope::AUTOMATIC);
356877 return std::move(set_statement);
356878 }
356879 case duckdb_libpgquery::PG_PRAGMA_TYPE_CALL:
356880 break;
356881 default:
356882 throw InternalException("Unknown pragma type");
356883 }
356884
356885 return std::move(result);
356886}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformExport()

unique_ptr< ExportStatement > duckdb::Transformer::TransformExport ( duckdb_libpgquery::PGExportStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGExportStmt node into a ExportStatement.

356328 {
356329 auto info = make_uniq<CopyInfo>();
356330 info->file_path = stmt.filename;
356331 info->format = "csv";
356332 info->is_from = false;
356333 // handle export options
356334 TransformCopyOptions(*info, stmt.options);
356335
356336 auto result = make_uniq<ExportStatement>(std::move(info));
356337 if (stmt.database) {
356338 result->database = stmt.database;
356339 }
356340 return result;
356341}
Here is the caller graph for this function:

◆ TransformImport()

unique_ptr< PragmaStatement > duckdb::Transformer::TransformImport ( duckdb_libpgquery::PGImportStmt & stmt)
private

Transform a Postgres duckdb_libpgquery::T_PGImportStmt node into a PragmaStatement.

356350 {
356351 auto result = make_uniq<PragmaStatement>();
356352 result->info->name = "import_database";
356353 result->info->parameters.emplace_back(make_uniq<ConstantExpression>(Value(stmt.filename)));
356354 return result;
356355}
Here is the caller graph for this function:

◆ TransformExplain()

unique_ptr< ExplainStatement > duckdb::Transformer::TransformExplain ( duckdb_libpgquery::PGExplainStmt & stmt)
private
356294 {
356295 auto explain_type = ExplainType::EXPLAIN_STANDARD;
356296 auto explain_format = ExplainFormat::DEFAULT;
356297 bool format_is_set = false;
356298 if (stmt.options) {
356299 for (auto n = stmt.options->head; n; n = n->next) {
356300 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(n->data.ptr_value);
356301 auto def_name = def_elem->defname;
356302 auto elem = StringUtil::Lower(def_name);
356303 if (elem == "analyze") {
356304 explain_type = ExplainType::EXPLAIN_ANALYZE;
356305 } else if (elem == "format") {
356306 if (def_elem->arg) {
356307 if (format_is_set) {
356308 throw InvalidInputException("FORMAT can not be provided more than once");
356309 }
356310 auto val = TransformValue(*PGPointerCast<duckdb_libpgquery::PGValue>(def_elem->arg))->value;
356311 format_is_set = true;
356312 explain_format = ParseFormat(val);
356313 }
356314 } else {
356315 throw NotImplementedException("Unimplemented explain type: %s", elem);
356316 }
356317 }
356318 }
356319 return make_uniq<ExplainStatement>(TransformStatement(*stmt.query), explain_type, explain_format);
356320}
unique_ptr< ConstantExpression > TransformValue(duckdb_libpgquery::PGValue val)
Transform a Postgres constant value into an Expression.
Definition duckdb.cpp:351836

◆ TransformVacuum()

unique_ptr< SQLStatement > duckdb::Transformer::TransformVacuum ( duckdb_libpgquery::PGVacuumStmt & stmt)
private
357831 {
357832 auto result = make_uniq<VacuumStatement>(ParseOptions(stmt.options));
357833
357834 if (stmt.relation) {
357835 result->info->ref = TransformRangeVar(*stmt.relation);
357836 result->info->has_table = true;
357837 }
357838
357839 if (stmt.va_cols) {
357840 D_ASSERT(result->info->has_table);
357841 for (auto col_node = stmt.va_cols->head; col_node != nullptr; col_node = col_node->next) {
357842 auto value = PGPointerCast<duckdb_libpgquery::PGValue>(col_node->data.ptr_value);
357843 result->info->columns.emplace_back(value->val.str);
357844 }
357845 }
357846 return std::move(result);
357847}

◆ TransformShow()

unique_ptr< QueryNode > duckdb::Transformer::TransformShow ( duckdb_libpgquery::PGVariableShowStmt & stmt)
private
357492 {
357493 // create the query that holds the show statement
357494 auto select_node = make_uniq<SelectNode>();
357495 select_node->select_list.push_back(make_uniq<StarExpression>());
357496 auto showref = make_uniq<ShowRef>();
357497 if (stmt.set) {
357498 if (std::string(stmt.set) == "__show_tables_from_database") {
357499 showref->show_type = ShowType::SHOW_FROM;
357500 auto qualified_name = TransformQualifiedName(*stmt.relation);
357501 if (!IsInvalidCatalog(qualified_name.catalog)) {
357502 throw ParserException("Expected \"SHOW TABLES FROM database\", \"SHOW TABLES FROM schema\", or "
357503 "\"SHOW TABLES FROM database.schema\"");
357504 }
357505 if (qualified_name.schema.empty()) {
357506 showref->schema_name = qualified_name.name;
357507 } else {
357508 showref->catalog_name = qualified_name.schema;
357509 showref->schema_name = qualified_name.name;
357510 }
357511 } else {
357512 // describing a set (e.g. SHOW ALL TABLES) - push it in the table name
357513 showref->table_name = stmt.set;
357514 showref->show_type = ShowType::SHOW_UNQUALIFIED;
357515 }
357516 } else if (!stmt.relation->schemaname) {
357517 // describing an unqualified relation - check if this is a "special" relation
357518 string table_name = StringUtil::Lower(stmt.relation->relname);
357519 if (table_name == "databases" || table_name == "schemas" || table_name == "tables" ||
357520 table_name == "variables") {
357521 showref->table_name = "\"" + std::move(table_name) + "\"";
357522 showref->show_type = ShowType::SHOW_UNQUALIFIED;
357523 }
357524 }
357525 if (showref->table_name.empty() && showref->show_type != ShowType::SHOW_FROM) {
357526 // describing a single relation
357527 // wrap the relation in a "SELECT * FROM [table_name]" query
357528 auto show_select_node = make_uniq<SelectNode>();
357529 show_select_node->select_list.push_back(make_uniq<StarExpression>());
357530 auto tableref = TransformRangeVar(*stmt.relation);
357531 show_select_node->from_table = std::move(tableref);
357532 showref->query = std::move(show_select_node);
357533 }
357534
357535 // If the show type is set to default, check if summary
357536 if (showref->show_type == ShowType::DESCRIBE) {
357537 showref->show_type = stmt.is_summary ? ShowType::SUMMARY : ShowType::DESCRIBE;
357538 }
357539 select_node->from_table = std::move(showref);
357540 return std::move(select_node);
357541}

◆ TransformShowStmt()

unique_ptr< SelectStatement > duckdb::Transformer::TransformShowStmt ( duckdb_libpgquery::PGVariableShowStmt & stmt)
private
357543 {
357544 auto result = make_uniq<SelectStatement>();
357545 result->node = TransformShow(stmt);
357546 return result;
357547}

◆ TransformShowSelect()

unique_ptr< QueryNode > duckdb::Transformer::TransformShowSelect ( duckdb_libpgquery::PGVariableShowSelectStmt & stmt)
private
357559 {
357560 // we capture the select statement of SHOW
357561 auto select_node = make_uniq<SelectNode>();
357562 select_node->select_list.push_back(make_uniq<StarExpression>());
357563
357564 auto show_ref = make_uniq<ShowRef>();
357565 show_ref->show_type = stmt.is_summary ? ShowType::SUMMARY : ShowType::DESCRIBE;
357566 show_ref->query = TransformSelectNode(*stmt.stmt);
357567 select_node->from_table = std::move(show_ref);
357568 return std::move(select_node);
357569}

◆ TransformShowSelectStmt()

unique_ptr< SelectStatement > duckdb::Transformer::TransformShowSelectStmt ( duckdb_libpgquery::PGVariableShowSelectStmt & stmt)
private
357571 {
357572 auto result = make_uniq<SelectStatement>();
357573 result->node = TransformShowSelect(stmt);
357574 return result;
357575}

◆ TransformAttach()

unique_ptr< AttachStatement > duckdb::Transformer::TransformAttach ( duckdb_libpgquery::PGAttachStmt & stmt)
private
355108 {
355109 auto result = make_uniq<AttachStatement>();
355110 auto info = make_uniq<AttachInfo>();
355111 info->name = stmt.name ? stmt.name : string();
355112 info->path = stmt.path;
355113 info->on_conflict = TransformOnConflict(stmt.onconflict);
355114
355115 if (stmt.options) {
355116 duckdb_libpgquery::PGListCell *cell;
355117 for_each_cell(cell, stmt.options->head) {
355118 auto def_elem = PGPointerCast<duckdb_libpgquery::PGDefElem>(cell->data.ptr_value);
355119 unique_ptr<ParsedExpression> expr;
355120 if (def_elem->arg) {
355121 expr = TransformExpression(def_elem->arg);
355122 } else {
355123 expr = make_uniq<ConstantExpression>(Value::BOOLEAN(true));
355124 }
355125 info->parsed_options[StringUtil::Lower(def_elem->defname)] = std::move(expr);
355126 }
355127 }
355128 result->info = std::move(info);
355129 return result;
355130}

◆ TransformDetach()

unique_ptr< DetachStatement > duckdb::Transformer::TransformDetach ( duckdb_libpgquery::PGDetachStmt & stmt)
private
356154 {
356155 auto result = make_uniq<DetachStatement>();
356156 auto info = make_uniq<DetachInfo>();
356157 info->name = stmt.db_name;
356158 info->if_not_found = TransformOnEntryNotFound(stmt.missing_ok);
356159
356160 result->info = std::move(info);
356161 return result;
356162}

◆ TransformUse()

unique_ptr< SetStatement > duckdb::Transformer::TransformUse ( duckdb_libpgquery::PGUseStmt & stmt)
private
357780 {
357781 auto qualified_name = TransformQualifiedName(*stmt.name);
357782 if (!IsInvalidCatalog(qualified_name.catalog)) {
357783 throw ParserException("Expected \"USE database\" or \"USE database.schema\"");
357784 }
357785 string name;
357786 if (IsInvalidSchema(qualified_name.schema)) {
357787 name = KeywordHelper::WriteOptionallyQuoted(qualified_name.name, '"');
357788 } else {
357789 name = KeywordHelper::WriteOptionallyQuoted(qualified_name.schema, '"') + "." +
357790 KeywordHelper::WriteOptionallyQuoted(qualified_name.name, '"');
357791 }
357792 auto name_expr = make_uniq<ConstantExpression>(Value(name));
357793 return make_uniq<SetVariableStatement>("schema", std::move(name_expr), SetScope::AUTOMATIC);
357794}
static string WriteOptionallyQuoted(const string &text, char quote='"', bool allow_caps = true, KeywordCategory category = KeywordCategory::KEYWORD_NONE)
Writes a string that is optionally quoted + escaped so it can be used as an identifier.
Definition duckdb.cpp:338034

◆ TransformCopyDatabase()

unique_ptr< SQLStatement > duckdb::Transformer::TransformCopyDatabase ( duckdb_libpgquery::PGCopyDatabaseStmt & stmt)
private
355356 {
355357 if (stmt.copy_database_flag) {
355358 // copy a specific subset of the database
355359 CopyDatabaseType type;
355360 if (StringUtil::Equals(stmt.copy_database_flag, "schema")) {
355361 type = CopyDatabaseType::COPY_SCHEMA;
355362 } else if (StringUtil::Equals(stmt.copy_database_flag, "data")) {
355363 type = CopyDatabaseType::COPY_DATA;
355364 } else {
355365 throw NotImplementedException("Unsupported flag for COPY DATABASE");
355366 }
355367 return make_uniq<CopyDatabaseStatement>(stmt.from_database, stmt.to_database, type);
355368 } else {
355369 auto result = make_uniq<PragmaStatement>();
355370 result->info->name = "copy_database";
355371 result->info->parameters.emplace_back(make_uniq<ConstantExpression>(Value(stmt.from_database)));
355372 result->info->parameters.emplace_back(make_uniq<ConstantExpression>(Value(stmt.to_database)));
355373 return std::move(result);
355374 }
355375}
static bool Equals(const char *s1, const char *s2)
Definition duckdb.hpp:4060

◆ TransformSecret()

unique_ptr< CreateStatement > duckdb::Transformer::TransformSecret ( duckdb_libpgquery::PGCreateSecretStmt & stmt)
private
357084 {
357085 auto result = make_uniq<CreateStatement>();
357086
357087 auto create_secret_info =
357088 make_uniq<CreateSecretInfo>(TransformOnConflict(stmt.onconflict),
357089 EnumUtil::FromString<SecretPersistType>(StringUtil::Upper(stmt.persist_type)));
357090
357091 if (stmt.secret_name) {
357092 create_secret_info->name = StringUtil::Lower(stmt.secret_name);
357093 }
357094
357095 if (stmt.secret_storage) {
357096 create_secret_info->storage_type = StringUtil::Lower(stmt.secret_storage);
357097 }
357098
357099 if (stmt.options) {
357100 TransformCreateSecretOptions(*create_secret_info, stmt.options);
357101 }
357102
357103 if (!create_secret_info->type) {
357104 throw ParserException("Failed to create secret - secret must have a type defined");
357105 }
357106 if (create_secret_info->name.empty()) {
357107 auto value = GetConstantExpressionValue(*create_secret_info->type);
357108 if (value.IsNull()) {
357109 throw InvalidInputException(
357110 "Can not combine a non-constant expression for the secret type with a default-named secret. Either "
357111 "provide an explicit secret name or use a constant expression for the secret type.");
357112 }
357113 create_secret_info->name = "__default_" + StringUtil::Lower(value.ToString());
357114 }
357115
357116 result->info = std::move(create_secret_info);
357117
357118 return result;
357119}

◆ TransformDropSecret()

unique_ptr< DropStatement > duckdb::Transformer::TransformDropSecret ( duckdb_libpgquery::PGDropSecretStmt & stmt)
private
356241 {
356242 auto result = make_uniq<DropStatement>();
356243 auto info = make_uniq<DropInfo>();
356244 auto extra_info = make_uniq<ExtraDropSecretInfo>();
356245
356246 info->type = CatalogType::SECRET_ENTRY;
356247 info->name = stmt.secret_name;
356248 info->if_not_found = stmt.missing_ok ? OnEntryNotFound::RETURN_NULL : OnEntryNotFound::THROW_EXCEPTION;
356249
356250 extra_info->persist_mode = EnumUtil::FromString<SecretPersistType>(StringUtil::Upper(stmt.persist_type));
356251 extra_info->secret_storage = stmt.secret_storage;
356252
356253 if (extra_info->persist_mode == SecretPersistType::TEMPORARY) {
356254 if (!extra_info->secret_storage.empty()) {
356255 throw ParserException("Can not combine TEMPORARY with specifying a storage for drop secret");
356256 }
356257 }
356258
356259 info->extra_drop_info = std::move(extra_info);
356260 result->info = std::move(info);
356261
356262 return result;
356263}

◆ TransformPrepare()

unique_ptr< PrepareStatement > duckdb::Transformer::TransformPrepare ( duckdb_libpgquery::PGPrepareStmt & stmt)
private
356898 {
356899 if (stmt.argtypes && stmt.argtypes->length > 0) {
356900 throw NotImplementedException("Prepared statement argument types are not supported, use CAST");
356901 }
356902
356903 auto result = make_uniq<PrepareStatement>();
356904 result->name = string(stmt.name);
356905 result->statement = TransformStatement(*stmt.query);
356906 ClearParameters();
356907
356908 return result;
356909}

◆ TransformExecute()

unique_ptr< ExecuteStatement > duckdb::Transformer::TransformExecute ( duckdb_libpgquery::PGExecuteStmt & stmt)
private
356915 {
356916 auto result = make_uniq<ExecuteStatement>();
356917 result->name = string(stmt.name);
356918
356919 vector<unique_ptr<ParsedExpression>> intermediate_values;
356920 if (stmt.params) {
356921 TransformExpressionList(*stmt.params, intermediate_values);
356922 }
356923
356924 idx_t param_idx = 0;
356925 for (idx_t i = 0; i < intermediate_values.size(); i++) {
356926 auto &expr = intermediate_values[i];
356927 if (!expr->IsScalar()) {
356928 throw InvalidInputException(NotAcceptedExpressionException());
356929 }
356930 if (!expr->GetAlias().empty() && param_idx != 0) {
356931 // Found unnamed parameters mixed with named parameters
356932 throw NotImplementedException("Mixing named parameters and positional parameters is not supported yet");
356933 }
356934 auto param_name = expr->GetAlias();
356935 if (expr->GetAlias().empty()) {
356936 param_name = std::to_string(param_idx + 1);
356937 if (param_idx != i) {
356938 throw NotImplementedException("Mixing named parameters and positional parameters is not supported yet");
356939 }
356940 param_idx++;
356941 }
356942 expr->ClearAlias();
356943 result->named_values[param_name] = std::move(expr);
356944 }
356945 intermediate_values.clear();
356946 return result;
356947}

◆ TransformCall()

unique_ptr< CallStatement > duckdb::Transformer::TransformCall ( duckdb_libpgquery::PGCallStmt & stmt)
private
355138 {
355139 auto result = make_uniq<CallStatement>();
355140 result->function = TransformFuncCall(*PGPointerCast<duckdb_libpgquery::PGFuncCall>(stmt.func));
355141 return result;
355142}
unique_ptr< ParsedExpression > TransformFuncCall(duckdb_libpgquery::PGFuncCall &root)
Transform a Postgres function call into an Expression.
Definition duckdb.cpp:352203

◆ TransformDeallocate()

unique_ptr< DropStatement > duckdb::Transformer::TransformDeallocate ( duckdb_libpgquery::PGDeallocateStmt & stmt)
private
356949 {
356950 if (!stmt.name) {
356951 throw ParserException("DEALLOCATE requires a name");
356952 }
356953
356954 auto result = make_uniq<DropStatement>();
356955 result->info->type = CatalogType::PREPARED_STATEMENT;
356956 result->info->name = string(stmt.name);
356957 return result;
356958}

◆ TransformPivotStatement()

unique_ptr< QueryNode > duckdb::Transformer::TransformPivotStatement ( duckdb_libpgquery::PGSelectStmt & select)
private
356725 {
356726 auto pivot = select.pivot;
356727 auto current_param_count = ParamCount();
356728 auto source = TransformTableRefNode(*pivot->source);
356729 auto next_param_count = ParamCount();
356730 bool has_parameters = next_param_count > current_param_count;
356731
356732 auto select_node = make_uniq<SelectNode>();
356733 // handle the CTEs
356734 if (select.withClause) {
356735 TransformCTE(*PGPointerCast<duckdb_libpgquery::PGWithClause>(select.withClause), select_node->cte_map);
356736 }
356737 if (!pivot->columns) {
356738 // no pivot columns - not actually a pivot
356739 select_node->from_table = std::move(source);
356740 if (pivot->groups) {
356741 auto groups = TransformStringList(pivot->groups);
356742 GroupingSet set;
356743 for (idx_t gr = 0; gr < groups.size(); gr++) {
356744 auto &group = groups[gr];
356745 auto colref = make_uniq<ColumnRefExpression>(group);
356746 select_node->select_list.push_back(colref->Copy());
356747 select_node->groups.group_expressions.push_back(std::move(colref));
356748 set.insert(gr);
356749 }
356750 select_node->groups.grouping_sets.push_back(std::move(set));
356751 }
356752 if (pivot->aggrs) {
356753 TransformExpressionList(*pivot->aggrs, select_node->select_list);
356754 }
356755 // transform order by/limit modifiers
356756 TransformModifiers(select, *select_node);
356757 return std::move(select_node);
356758 }
356759
356760 // generate CREATE TYPE statements for each of the columns that do not have an IN list
356761 bool is_pivot = !pivot->unpivots;
356762 auto columns = TransformPivotList(*pivot->columns, is_pivot);
356763 for (idx_t c = 0; c < columns.size(); c++) {
356764 auto &col = columns[c];
356765 if (!col.pivot_enum.empty() || !col.entries.empty()) {
356766 continue;
356767 }
356768 if (col.pivot_expressions.size() != 1) {
356769 throw InternalException("PIVOT statement with multiple names in pivot entry!?");
356770 }
356771 auto enum_name = "__pivot_enum_" + UUID::ToString(UUID::GenerateRandomUUID());
356772
356773 auto new_select = make_uniq<SelectNode>();
356774 ExtractCTEsRecursive(new_select->cte_map);
356775 new_select->from_table = source->Copy();
356776 AddPivotEntry(enum_name, std::move(new_select), col.pivot_expressions[0]->Copy(), std::move(col.subquery),
356777 has_parameters);
356778 col.pivot_enum = enum_name;
356779 }
356780
356781 // generate the actual query, including the pivot
356782 select_node->select_list.push_back(make_uniq<StarExpression>());
356783
356784 auto pivot_ref = make_uniq<PivotRef>();
356785 pivot_ref->source = std::move(source);
356786 if (pivot->unpivots) {
356787 pivot_ref->unpivot_names = TransformStringList(pivot->unpivots);
356788 } else {
356789 if (pivot->aggrs) {
356790 TransformExpressionList(*pivot->aggrs, pivot_ref->aggregates);
356791 } else {
356792 // pivot but no aggregates specified - push a count star
356793 vector<unique_ptr<ParsedExpression>> children;
356794 auto function = make_uniq<FunctionExpression>("count_star", std::move(children));
356795 pivot_ref->aggregates.push_back(std::move(function));
356796 }
356797 }
356798 if (pivot->groups) {
356799 pivot_ref->groups = TransformStringList(pivot->groups);
356800 }
356801 pivot_ref->pivots = std::move(columns);
356802 SetQueryLocation(*pivot_ref, pivot->location);
356803 select_node->from_table = std::move(pivot_ref);
356804 // transform order by/limit modifiers
356805 TransformModifiers(select, *select_node);
356806
356807 return std::move(select_node);
356808}
static void ToString(hugeint_t input, char *buf)
Convert a hugeint object to a uuid style string.
Definition duckdb.cpp:130940
static hugeint_t GenerateRandomUUID(RandomEngine &engine)
Generate a random UUID v4 value.
Definition duckdb.cpp:131021

◆ CreatePivotStatement()

unique_ptr< SQLStatement > duckdb::Transformer::CreatePivotStatement ( unique_ptr< SQLStatement > statement)
private
356703 {
356704 auto result = make_uniq<MultiStatement>();
356705 for (auto &pivot : pivot_entries) {
356706 if (pivot->has_parameters) {
356707 throw ParserException(
356708 "PIVOT statements with pivot elements extracted from the data cannot have parameters in their source.\n"
356709 "In order to use parameters the PIVOT values must be manually specified, e.g.:\n"
356710 "PIVOT ... ON %s IN (val1, val2, ...)",
356711 pivot->column->ToString());
356712 }
356713 result->statements.push_back(GenerateCreateEnumStmt(std::move(pivot)));
356714 }
356715 result->stmt_location = statement->stmt_location;
356716 result->stmt_length = statement->stmt_length;
356717 result->statements.push_back(std::move(statement));
356718 // FIXME: drop the types again!?
356719 // for(auto &pivot : pivot_entries) {
356720 // result->statements.push_back(GenerateDropEnumStmt(std::move(pivot->enum_name)));
356721 // }
356722 return std::move(result);
356723}

◆ TransformPivotColumn()

PivotColumn duckdb::Transformer::TransformPivotColumn ( duckdb_libpgquery::PGPivot & pivot,
bool is_pivot )
private
358058 {
358059 PivotColumn col;
358060 if (pivot.pivot_columns) {
358061 TransformExpressionList(*pivot.pivot_columns, col.pivot_expressions);
358062 for (auto &expr : col.pivot_expressions) {
358063 if (expr->IsScalar()) {
358064 throw ParserException(expr->GetQueryLocation(), "Cannot pivot on constant value \"%s\"",
358065 expr->ToString());
358066 }
358067 if (expr->HasSubquery()) {
358068 throw ParserException(expr->GetQueryLocation(), "Cannot pivot on subquery \"%s\"", expr->ToString());
358069 }
358070 }
358071 } else if (pivot.unpivot_columns) {
358072 col.unpivot_names = TransformStringList(pivot.unpivot_columns);
358073 } else {
358074 throw InternalException("Either pivot_columns or unpivot_columns must be defined");
358075 }
358076 if (pivot.pivot_value) {
358077 for (auto node = pivot.pivot_value->head; node != nullptr; node = node->next) {
358078 auto n = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
358079 auto expr = TransformExpression(n);
358080 PivotColumnEntry entry;
358081 entry.alias = expr->GetAlias();
358082 entry.expr = std::move(expr);
358083 col.entries.push_back(std::move(entry));
358084 }
358085 }
358086 if (pivot.subquery) {
358087 col.subquery = TransformSelectNode(*pivot.subquery);
358088 }
358089 if (pivot.pivot_enum) {
358090 col.pivot_enum = pivot.pivot_enum;
358091 }
358092 return col;
358093}

◆ TransformPivotList()

vector< PivotColumn > duckdb::Transformer::TransformPivotList ( duckdb_libpgquery::PGList & list,
bool is_pivot )
private
358095 {
358096 vector<PivotColumn> result;
358097 for (auto node = list.head; node != nullptr; node = node->next) {
358098 auto pivot = PGPointerCast<duckdb_libpgquery::PGPivot>(node->data.ptr_value);
358099 result.push_back(TransformPivotColumn(*pivot, is_pivot));
358100 }
358101 return result;
358102}

◆ TransformMergeInto()

unique_ptr< SQLStatement > duckdb::Transformer::TransformMergeInto ( duckdb_libpgquery::PGMergeIntoStmt & stmt)
private
356526 {
356527 auto result = make_uniq<MergeIntoStatement>();
356528
356529 if (stmt.withClause) {
356530 auto with_clause = PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause);
356531 TransformCTE(*with_clause, result->cte_map);
356532 }
356533 result->target = TransformRangeVar(*stmt.targetTable);
356534 result->source = TransformTableRefNode(*stmt.source);
356535 if (stmt.joinCondition) {
356536 result->join_condition = TransformExpression(*stmt.joinCondition);
356537 }
356538 if (stmt.usingClause) {
356539 result->using_columns = TransformUsingClause(*stmt.usingClause);
356540 }
356541
356542 unique_ptr<MergeIntoAction> when_matched_action;
356543 unique_ptr<MergeIntoAction> when_not_matched_action;
356544
356545 map<MergeActionCondition, unique_ptr<MergeIntoAction>> unconditional_actions;
356546 for (auto cell = stmt.matchActions->head; cell; cell = cell->next) {
356547 auto match_action = PGPointerCast<duckdb_libpgquery::PGMatchAction>(cell->data.ptr_value);
356548 auto action = TransformMergeIntoAction(*match_action);
356549 MergeActionCondition action_condition;
356550 switch (match_action->when) {
356551 case duckdb_libpgquery::MERGE_ACTION_WHEN_MATCHED:
356552 action_condition = MergeActionCondition::WHEN_MATCHED;
356553 break;
356554 case duckdb_libpgquery::MERGE_ACTION_WHEN_NOT_MATCHED_BY_SOURCE:
356555 action_condition = MergeActionCondition::WHEN_NOT_MATCHED_BY_SOURCE;
356556 break;
356557 case duckdb_libpgquery::MERGE_ACTION_WHEN_NOT_MATCHED_BY_TARGET:
356558 action_condition = MergeActionCondition::WHEN_NOT_MATCHED_BY_TARGET;
356559 break;
356560 default:
356561 throw InternalException("Unknown merge action");
356562 }
356563 if (!action->condition) {
356564 // unconditional action - check if we already have an unconditional action for this match
356565 auto entry = unconditional_actions.find(action_condition);
356566 if (entry != unconditional_actions.end()) {
356567 string action_condition_str = MergeIntoStatement::ActionConditionToString(action_condition);
356568 throw ParserException(
356569 "Unconditional %s clause was already defined - only one unconditional %s clause is supported",
356570 action_condition_str, action_condition_str);
356571 }
356572 unconditional_actions.emplace(action_condition, std::move(action));
356573 continue;
356574 }
356575 result->actions[action_condition].push_back(std::move(action));
356576 }
356577 // finally add the unconditional actions
356578 for (auto &entry : unconditional_actions) {
356579 result->actions[entry.first].push_back(std::move(entry.second));
356580 }
356581 if (stmt.returningList) {
356582 TransformExpressionList(*stmt.returningList, result->returning_list);
356583 }
356584 return std::move(result);
356585}
vector< string > TransformUsingClause(duckdb_libpgquery::PGList &usingClause)
Transform using clause.
Definition duckdb.cpp:357954

◆ TransformMergeIntoAction()

unique_ptr< MergeIntoAction > duckdb::Transformer::TransformMergeIntoAction ( duckdb_libpgquery::PGMatchAction & action)
private
356484 {
356485 auto result = make_uniq<MergeIntoAction>();
356486 if (action.andClause) {
356487 result->condition = TransformExpression(action.andClause);
356488 }
356489 switch (action.actionType) {
356490 case duckdb_libpgquery::MERGE_ACTION_TYPE_UPDATE:
356491 result->action_type = MergeActionType::MERGE_UPDATE;
356492 if (action.updateTargets) {
356493 result->update_info = TransformUpdateSetInfo(action.updateTargets, nullptr);
356494 }
356495 result->column_order = TransformColumnOrder(action.insert_column_order);
356496 break;
356497 case duckdb_libpgquery::MERGE_ACTION_TYPE_DELETE:
356498 result->action_type = MergeActionType::MERGE_DELETE;
356499 break;
356500 case duckdb_libpgquery::MERGE_ACTION_TYPE_INSERT:
356501 result->action_type = MergeActionType::MERGE_INSERT;
356502 if (action.insertCols) {
356503 result->insert_columns = TransformInsertColumns(*action.insertCols);
356504 }
356505 if (action.insertValues) {
356506 TransformExpressionList(*action.insertValues, result->expressions);
356507 }
356508 result->column_order = TransformColumnOrder(action.insert_column_order);
356509 result->default_values = action.defaultValues;
356510 break;
356511 case duckdb_libpgquery::MERGE_ACTION_TYPE_DO_NOTHING:
356512 result->action_type = MergeActionType::MERGE_DO_NOTHING;
356513 break;
356514 case duckdb_libpgquery::MERGE_ACTION_TYPE_ERROR:
356515 result->action_type = MergeActionType::MERGE_ERROR;
356516 if (action.errorMessage) {
356517 result->expressions.push_back(TransformExpression(*action.errorMessage));
356518 }
356519 break;
356520 default:
356521 throw InternalException("Unsupported merge into action");
356522 }
356523 return result;
356524}

◆ TransformSet()

unique_ptr< SetStatement > duckdb::Transformer::TransformSet ( duckdb_libpgquery::PGVariableSetStmt & set)
private
357466 {
357467 D_ASSERT(stmt.type == duckdb_libpgquery::T_PGVariableSetStmt);
357468
357469 SetType set_type = ToSetType(stmt.kind);
357470 switch (set_type) {
357471 case SetType::SET:
357472 return TransformSetVariable(stmt);
357473 case SetType::RESET:
357474 return TransformResetVariable(stmt);
357475 default:
357476 throw InternalException("Type not implemented for SetType");
357477 }
357478}

◆ TransformSetVariable()

unique_ptr< SetStatement > duckdb::Transformer::TransformSetVariable ( duckdb_libpgquery::PGVariableSetStmt & stmt)
private
357422 {
357423 if (stmt.scope == duckdb_libpgquery::VariableSetScope::VAR_SET_SCOPE_LOCAL) {
357424 throw NotImplementedException("SET LOCAL is not implemented.");
357425 }
357426
357427 string name(stmt.name);
357428 D_ASSERT(!name.empty()); // parser protect us!
357429 if (stmt.args->length != 1) {
357430 throw ParserException("SET needs a single scalar value parameter");
357431 }
357432 auto scope = ToSetScope(stmt.scope);
357433 D_ASSERT(stmt.args->head && stmt.args->head->data.ptr_value);
357434 auto const_val = PGPointerCast<duckdb_libpgquery::PGNode>(stmt.args->head->data.ptr_value);
357435 auto expr = TransformExpression(const_val);
357436 if (expr->GetExpressionType() == ExpressionType::COLUMN_REF) {
357437 auto &colref = expr->Cast<ColumnRefExpression>();
357438 Value val;
357439 if (!colref.IsQualified()) {
357440 val = Value(colref.GetColumnName());
357441 } else {
357442 val = Value(expr->ToString());
357443 }
357444 expr = make_uniq<ConstantExpression>(std::move(val));
357445 }
357446 if (expr->GetExpressionType() == ExpressionType::VALUE_DEFAULT) {
357447 // set to default = reset
357448 return make_uniq<ResetVariableStatement>(std::move(name), scope);
357449 }
357450 return make_uniq<SetVariableStatement>(std::move(name), std::move(expr), scope);
357451}

◆ TransformResetVariable()

unique_ptr< SetStatement > duckdb::Transformer::TransformResetVariable ( duckdb_libpgquery::PGVariableSetStmt & stmt)
private
357453 {
357454 D_ASSERT(stmt.kind == duckdb_libpgquery::VariableSetKind::VAR_RESET);
357455
357456 if (stmt.scope == duckdb_libpgquery::VariableSetScope::VAR_SET_SCOPE_LOCAL) {
357457 throw NotImplementedException("RESET LOCAL is not implemented.");
357458 }
357459
357460 string name(stmt.name);
357461 D_ASSERT(!name.empty()); // parser protect us!
357462
357463 return make_uniq<ResetVariableStatement>(name, ToSetScope(stmt.scope));
357464}

◆ TransformCheckpoint()

unique_ptr< SQLStatement > duckdb::Transformer::TransformCheckpoint ( duckdb_libpgquery::PGCheckPointStmt & stmt)
private
355152 {
355153 vector<unique_ptr<ParsedExpression>> children;
355154 // transform into "CALL checkpoint()" or "CALL force_checkpoint()"
355155 auto checkpoint_name = stmt.force ? "force_checkpoint" : "checkpoint";
355156 auto result = make_uniq<CallStatement>();
355157 auto function = make_uniq<FunctionExpression>(checkpoint_name, std::move(children));
355158 function->catalog = SYSTEM_CATALOG;
355159 function->schema = DEFAULT_SCHEMA;
355160 if (stmt.name) {
355161 function->children.push_back(make_uniq<ConstantExpression>(Value(stmt.name)));
355162 }
355163 result->function = std::move(function);
355164 return std::move(result);
355165}

◆ TransformLoad()

unique_ptr< LoadStatement > duckdb::Transformer::TransformLoad ( duckdb_libpgquery::PGLoadStmt & stmt)
private
356452 {
356453 D_ASSERT(stmt.type == duckdb_libpgquery::T_PGLoadStmt);
356454
356455 auto load_stmt = make_uniq<LoadStatement>();
356456 auto load_info = make_uniq<LoadInfo>();
356457 load_info->filename = stmt.filename ? string(stmt.filename) : "";
356458 load_info->repository = stmt.repository ? string(stmt.repository) : "";
356459 load_info->repo_is_alias = stmt.repo_is_alias;
356460 load_info->version = stmt.version ? string(stmt.version) : "";
356461 switch (stmt.load_type) {
356462 case duckdb_libpgquery::PG_LOAD_TYPE_LOAD:
356463 load_info->load_type = LoadType::LOAD;
356464 break;
356465 case duckdb_libpgquery::PG_LOAD_TYPE_INSTALL:
356466 load_info->load_type = LoadType::INSTALL;
356467 break;
356468 case duckdb_libpgquery::PG_LOAD_TYPE_FORCE_INSTALL:
356469 load_info->load_type = LoadType::FORCE_INSTALL;
356470 break;
356471 }
356472 load_stmt->info = std::move(load_info);
356473 return load_stmt;
356474}

◆ TransformSelectNode()

unique_ptr< QueryNode > duckdb::Transformer::TransformSelectNode ( duckdb_libpgquery::PGNode & select,
bool is_select = true )
private

Transform a Postgres duckdb_libpgquery::T_PGSelectStmt node into a QueryNode.

357128 {
357129 switch (node.type) {
357130 case duckdb_libpgquery::T_PGVariableShowSelectStmt:
357131 return TransformShowSelect(PGCast<duckdb_libpgquery::PGVariableShowSelectStmt>(node));
357132 case duckdb_libpgquery::T_PGVariableShowStmt:
357133 return TransformShow(PGCast<duckdb_libpgquery::PGVariableShowStmt>(node));
357134 default:
357135 return TransformSelectNodeInternal(PGCast<duckdb_libpgquery::PGSelectStmt>(node), is_select);
357136 }
357137}
Here is the caller graph for this function:

◆ TransformSelectNodeInternal()

unique_ptr< QueryNode > duckdb::Transformer::TransformSelectNodeInternal ( duckdb_libpgquery::PGSelectStmt & select,
bool is_select = true )
private
357140 {
357141 // Both Insert/Create Table As uses this.
357142 if (is_select) {
357143 if (select.intoClause) {
357144 throw ParserException("SELECT INTO not supported!");
357145 }
357146 if (select.lockingClause) {
357147 throw ParserException("SELECT locking clause is not supported!");
357148 }
357149 }
357150 if (select.pivot) {
357151 return TransformPivotStatement(select);
357152 }
357153 return TransformSelectInternal(select);
357154}

◆ TransformSelectInternal()

unique_ptr< QueryNode > duckdb::Transformer::TransformSelectInternal ( duckdb_libpgquery::PGSelectStmt & select)
private
357268 {
357269 D_ASSERT(stmt.type == duckdb_libpgquery::T_PGSelectStmt);
357270 auto stack_checker = StackCheck();
357271
357272 unique_ptr<QueryNode> node;
357273
357274 switch (stmt.op) {
357275 case duckdb_libpgquery::PG_SETOP_NONE: {
357276 node = make_uniq<SelectNode>();
357277 auto &result = node->Cast<SelectNode>();
357278 if (stmt.withClause) {
357279 TransformCTE(*PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause), node->cte_map);
357280 }
357281 if (stmt.windowClause) {
357282 for (auto window_ele = stmt.windowClause->head; window_ele != nullptr; window_ele = window_ele->next) {
357283 auto window_def = PGPointerCast<duckdb_libpgquery::PGWindowDef>(window_ele->data.ptr_value);
357284 D_ASSERT(window_def);
357285 D_ASSERT(window_def->name);
357286 string window_name(window_def->name);
357287 auto it = window_clauses.find(window_name);
357288 if (it != window_clauses.end()) {
357289 throw ParserException("window \"%s\" is already defined", window_name);
357290 }
357291 window_clauses[window_name] = window_def.get();
357292 }
357293 }
357294
357295 // checks distinct clause
357296 if (stmt.distinctClause != nullptr) {
357297 auto modifier = make_uniq<DistinctModifier>();
357298 // checks distinct on clause
357299 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(stmt.distinctClause->head->data.ptr_value);
357300 if (target) {
357301 // add the columns defined in the ON clause to the select list
357302 TransformExpressionList(*stmt.distinctClause, modifier->distinct_on_targets);
357303 }
357304 result.modifiers.push_back(std::move(modifier));
357305 }
357306
357307 // do this early so the value lists also have a `FROM`
357308 if (stmt.valuesLists) {
357309 // VALUES list, create an ExpressionList
357310 D_ASSERT(!stmt.fromClause);
357311 result.from_table = TransformValuesList(stmt.valuesLists);
357312 result.select_list.push_back(make_uniq<StarExpression>());
357313 } else {
357314 if (!stmt.targetList) {
357315 throw ParserException("SELECT clause without selection list");
357316 }
357317 // transform in the specified order to ensure positional parameters are correctly set
357318 if (stmt.from_first) {
357319 result.from_table = TransformFrom(stmt.fromClause);
357320 TransformExpressionList(*stmt.targetList, result.select_list);
357321 } else {
357322 TransformExpressionList(*stmt.targetList, result.select_list);
357323 result.from_table = TransformFrom(stmt.fromClause);
357324 }
357325 }
357326
357327 // where
357328 result.where_clause = TransformExpression(stmt.whereClause);
357329 // group by
357330 TransformGroupBy(stmt.groupClause, result);
357331 // having
357332 result.having = TransformExpression(stmt.havingClause);
357333 // qualify
357334 result.qualify = TransformExpression(stmt.qualifyClause);
357335 // sample
357336 result.sample = TransformSampleOptions(stmt.sampleOptions);
357337 break;
357338 }
357339 case duckdb_libpgquery::PG_SETOP_UNION:
357340 case duckdb_libpgquery::PG_SETOP_EXCEPT:
357341 case duckdb_libpgquery::PG_SETOP_INTERSECT:
357342 case duckdb_libpgquery::PG_SETOP_UNION_BY_NAME: {
357343 node = make_uniq<SetOperationNode>();
357344 auto &result = node->Cast<SetOperationNode>();
357345 if (stmt.withClause) {
357346 TransformCTE(*PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause), result.cte_map);
357347 }
357348 TransformSetOperationChildren(stmt, result);
357349
357350 result.setop_all = stmt.all;
357351 switch (stmt.op) {
357352 case duckdb_libpgquery::PG_SETOP_UNION:
357353 result.setop_type = SetOperationType::UNION;
357354 break;
357355 case duckdb_libpgquery::PG_SETOP_EXCEPT:
357356 result.setop_type = SetOperationType::EXCEPT;
357357 break;
357358 case duckdb_libpgquery::PG_SETOP_INTERSECT:
357359 result.setop_type = SetOperationType::INTERSECT;
357360 break;
357361 case duckdb_libpgquery::PG_SETOP_UNION_BY_NAME:
357362 result.setop_type = SetOperationType::UNION_BY_NAME;
357363 break;
357364 default:
357365 throw InternalException("Unexpected setop type");
357366 }
357367 if (stmt.sampleOptions) {
357368 throw ParserException("SAMPLE clause is only allowed in regular SELECT statements");
357369 }
357370 break;
357371 }
357372 default:
357373 throw NotImplementedException("Statement type %d not implemented!", stmt.op);
357374 }
357375
357376 TransformModifiers(stmt, *node);
357377
357378 return node;
357379}
unique_ptr< TableRef > TransformValuesList(duckdb_libpgquery::PGList *list)
Transform a VALUES list into a set of expressions.
Definition duckdb.cpp:356364
case_insensitive_map_t< duckdb_libpgquery::PGWindowDef * > window_clauses
Holds window expressions defined by name. We need those when transforming the expressions referring t...
Definition duckdb.cpp:346494
bool TransformGroupBy(optional_ptr< duckdb_libpgquery::PGList > group, SelectNode &result)
Transform a Postgres GROUP BY expression into a list of Expression.
Definition duckdb.cpp:354399

◆ TransformModifiers()

void duckdb::Transformer::TransformModifiers ( duckdb_libpgquery::PGSelectStmt & stmt,
QueryNode & node )
private
357181 {
357182 // transform the common properties
357183 // both the set operations and the regular select can have an ORDER BY/LIMIT attached to them
357184 vector<OrderByNode> orders;
357185 TransformOrderBy(stmt.sortClause, orders);
357186 if (!orders.empty()) {
357187 auto order_modifier = make_uniq<OrderModifier>();
357188 order_modifier->orders = std::move(orders);
357189 node.modifiers.push_back(std::move(order_modifier));
357190 }
357191
357192 if (stmt.limitCount || stmt.limitOffset) {
357193 if (stmt.limitCount && stmt.limitCount->type == duckdb_libpgquery::T_PGLimitPercent) {
357194 auto limit_percent_modifier = make_uniq<LimitPercentModifier>();
357195 auto expr_node = PGPointerCast<duckdb_libpgquery::PGLimitPercent>(stmt.limitCount)->limit_percent;
357196 limit_percent_modifier->limit = TransformExpression(expr_node);
357197 if (stmt.limitOffset) {
357198 limit_percent_modifier->offset = TransformExpression(stmt.limitOffset);
357199 }
357200 node.modifiers.push_back(std::move(limit_percent_modifier));
357201 } else {
357202 auto limit_modifier = make_uniq<LimitModifier>();
357203 if (stmt.offset_first) {
357204 if (stmt.limitOffset) {
357205 limit_modifier->offset = TransformExpression(stmt.limitOffset);
357206 }
357207 if (stmt.limitCount) {
357208 limit_modifier->limit = TransformExpression(stmt.limitCount);
357209 }
357210 } else {
357211 if (stmt.limitCount) {
357212 limit_modifier->limit = TransformExpression(stmt.limitCount);
357213 }
357214 if (stmt.limitOffset) {
357215 limit_modifier->offset = TransformExpression(stmt.limitOffset);
357216 }
357217 }
357218 node.modifiers.push_back(std::move(limit_modifier));
357219 }
357220 }
357221}

◆ SetOperationsMatch()

bool duckdb::Transformer::SetOperationsMatch ( duckdb_libpgquery::PGSelectStmt & root,
duckdb_libpgquery::PGNode & node )
private
357223 {
357224 if (node.type != duckdb_libpgquery::T_PGSelectStmt) {
357225 // not a select or set-op - set operations cannot match
357226 return false;
357227 }
357228 auto &stmt = PGCast<duckdb_libpgquery::PGSelectStmt>(node);
357229 if (root.op != stmt.op || root.all != stmt.all) {
357230 // set operation type does not match
357231 return false;
357232 }
357233 if (root.op != duckdb_libpgquery::PG_SETOP_UNION && root.op != duckdb_libpgquery::PG_SETOP_UNION_BY_NAME) {
357234 // only generate multi-child nodes for UNION/UNION ALL
357235 return false;
357236 }
357237 // check if this is a "simple" set operation
357238 if (stmt.withClause || stmt.sortClause || stmt.limitCount || stmt.limitOffset || stmt.sampleOptions) {
357239 // it is not - we need to unfold it
357240 return false;
357241 }
357242 return true;
357243}

◆ TransformSetOperationChildren()

void duckdb::Transformer::TransformSetOperationChildren ( duckdb_libpgquery::PGSelectStmt & stmt,
SetOperationNode & result )
private
357245 {
357246 D_ASSERT(stmt.larg && stmt.rarg);
357247 vector<reference<duckdb_libpgquery::PGNode>> set_operations;
357248 set_operations.push_back(*stmt.larg);
357249 set_operations.push_back(*stmt.rarg);
357250
357251 for (idx_t i = 0; i < set_operations.size(); i++) {
357252 auto &node = set_operations[i].get();
357253 // check if this set operation can be merged into the parents' set operation
357254 if (!SetOperationsMatch(stmt, node)) {
357255 // it cannot - transform the child
357256 result.children.push_back(TransformSelectNode(node));
357257 } else {
357258 // it can - recurse into children
357259 // note that we must process the children in a specific order - so we need to expand the children in-place
357260 auto &select = PGCast<duckdb_libpgquery::PGSelectStmt>(node);
357261 set_operations[i] = *select.larg;
357262 set_operations.insert(set_operations.begin() + static_cast<int64_t>(i + 1), *select.rarg);
357263 i--;
357264 }
357265 }
357266}

◆ TransformBoolExpr()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformBoolExpr ( duckdb_libpgquery::PGBoolExpr & root)
private

Transform a Postgres boolean expression into an Expression.

351487 {
351488 unique_ptr<ParsedExpression> result;
351489 for (auto node = root.args->head; node != nullptr; node = node->next) {
351490 auto next = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value));
351491
351492 switch (root.boolop) {
351493 case duckdb_libpgquery::PG_AND_EXPR: {
351494 if (!result) {
351495 result = std::move(next);
351496 } else {
351497 result = make_uniq<ConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(result),
351498 std::move(next));
351499 }
351500 break;
351501 }
351502 case duckdb_libpgquery::PG_OR_EXPR: {
351503 if (!result) {
351504 result = std::move(next);
351505 } else {
351506 result = make_uniq<ConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(result),
351507 std::move(next));
351508 }
351509 break;
351510 }
351511 case duckdb_libpgquery::PG_NOT_EXPR: {
351512 if (next->GetExpressionType() == ExpressionType::COMPARE_IN) {
351513 // convert COMPARE_IN to COMPARE_NOT_IN
351514 next->SetExpressionTypeUnsafe(ExpressionType::COMPARE_NOT_IN);
351515 result = std::move(next);
351516 } else if ((next->GetExpressionType() >= ExpressionType::COMPARE_EQUAL &&
351517 next->GetExpressionType() <= ExpressionType::COMPARE_GREATERTHANOREQUALTO) ||
351518 next->GetExpressionType() == ExpressionType::COMPARE_DISTINCT_FROM ||
351519 next->GetExpressionType() == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
351520 // NOT on a comparison: we can negate the comparison
351521 // e.g. NOT(x > y) is equivalent to x <= y
351522 // NOT(x IS DISTINCT FROM y) is equivalent to x IS NOT DISTINCT FROM y
351523 next->SetExpressionTypeUnsafe(NegateComparisonExpression(next->GetExpressionType()));
351524 result = std::move(next);
351525 } else {
351526 result = make_uniq<OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(next));
351527 }
351528 break;
351529 }
351530 }
351531 }
351532 SetQueryLocation(*result, root.location);
351533 return result;
351534}
ExpressionType NegateComparisonExpression(ExpressionType type)
Negate a comparison expression, turning e.g. = into !=, or < into >=.
Definition duckdb.cpp:68019
Here is the call graph for this function:

◆ TransformCase()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformCase ( duckdb_libpgquery::PGCaseExpr & root)
private

Transform a Postgres case expression into an Expression.

351598 {
351599 auto case_node = make_uniq<CaseExpression>();
351600 auto root_arg = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(root.arg));
351601 for (auto cell = root.args->head; cell != nullptr; cell = cell->next) {
351602 CaseCheck case_check;
351603
351604 auto w = PGPointerCast<duckdb_libpgquery::PGCaseWhen>(cell->data.ptr_value);
351605 auto test_raw = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(w->expr));
351606 unique_ptr<ParsedExpression> test;
351607 if (root_arg) {
351608 case_check.when_expr =
351609 make_uniq<ComparisonExpression>(ExpressionType::COMPARE_EQUAL, root_arg->Copy(), std::move(test_raw));
351610 } else {
351611 case_check.when_expr = std::move(test_raw);
351612 }
351613 case_check.then_expr = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(w->result));
351614 case_node->case_checks.push_back(std::move(case_check));
351615 }
351616
351617 if (root.defresult) {
351618 case_node->else_expr = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(root.defresult));
351619 } else {
351620 case_node->else_expr = make_uniq<ConstantExpression>(Value(LogicalType::SQLNULL));
351621 }
351622 SetQueryLocation(*case_node, root.location);
351623 return std::move(case_node);
351624}
Here is the call graph for this function:

◆ TransformTypeCast()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformTypeCast ( duckdb_libpgquery::PGTypeCast & root)
private

Transform a Postgres type cast into an Expression.

351636 {
351637 // get the type to cast to
351638 auto type_name = root.typeName;
351639 LogicalType target_type = TransformTypeName(*type_name);
351640
351641 // check for a constant BLOB value, then return ConstantExpression with BLOB
351642 if (!root.tryCast && target_type == LogicalType::BLOB && root.arg->type == duckdb_libpgquery::T_PGAConst) {
351643 auto c = PGPointerCast<duckdb_libpgquery::PGAConst>(root.arg);
351644 if (c->val.type == duckdb_libpgquery::T_PGString) {
351645 CastParameters parameters;
351646 if (root.location >= 0) {
351647 parameters.query_location = NumericCast<idx_t>(root.location);
351648 }
351649 auto blob_data = Blob::ToBlob(string(c->val.val.str), parameters);
351650 auto result = make_uniq<ConstantExpression>(Value::BLOB_RAW(blob_data));
351651 SetQueryLocation(*result, root.location);
351652 return std::move(result);
351653 }
351654 }
351655 // transform the expression node
351656 auto expression = TransformExpression(root.arg);
351657 bool try_cast = root.tryCast;
351658
351659 // now create a cast operation
351660 auto result = make_uniq<CastExpression>(target_type, std::move(expression), try_cast);
351661 SetQueryLocation(*result, root.location);
351662 return std::move(result);
351663}
static DUCKDB_API void ToBlob(string_t str, data_ptr_t output)
Convert a string to a blob. This function should ONLY be called after calling GetBlobSize,...
Definition duckdb.cpp:113232
Here is the call graph for this function:

◆ TransformCoalesce()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformCoalesce ( duckdb_libpgquery::PGAExpr & root)
private

Transform a Postgres coalesce into an Expression.

351673 {
351674 auto coalesce_args = PGPointerCast<duckdb_libpgquery::PGList>(root.lexpr);
351675 D_ASSERT(coalesce_args->length > 0); // parser ensures this already
351676
351677 auto coalesce_op = make_uniq<OperatorExpression>(ExpressionType::OPERATOR_COALESCE);
351678 for (auto cell = coalesce_args->head; cell; cell = cell->next) {
351679 // get the value of the COALESCE
351680 auto value_expr = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(cell->data.ptr_value));
351681 coalesce_op->children.push_back(std::move(value_expr));
351682 }
351683 return std::move(coalesce_op);
351684}
Here is the call graph for this function:

◆ TransformColumnRef()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformColumnRef ( duckdb_libpgquery::PGColumnRef & root)
private

Transform a Postgres column reference into an Expression.

351800 {
351801 auto fields = root.fields;
351802 auto head_node = PGPointerCast<duckdb_libpgquery::PGNode>(fields->head->data.ptr_value);
351803 switch (head_node->type) {
351804 case duckdb_libpgquery::T_PGString: {
351805 if (fields->length < 1) {
351806 throw InternalException("Unexpected field length");
351807 }
351808 vector<string> column_names;
351809 for (auto node = fields->head; node; node = node->next) {
351810 column_names.emplace_back(PGPointerCast<duckdb_libpgquery::PGValue>(node->data.ptr_value)->val.str);
351811 }
351812 auto colref = make_uniq<ColumnRefExpression>(std::move(column_names));
351813 SetQueryLocation(*colref, root.location);
351814 return std::move(colref);
351815 }
351816 case duckdb_libpgquery::T_PGAStar: {
351817 return TransformStarExpression(PGCast<duckdb_libpgquery::PGAStar>(*head_node));
351818 }
351819 default:
351820 throw NotImplementedException("ColumnRef not implemented!");
351821 }
351822}

◆ TransformValue()

unique_ptr< ConstantExpression > duckdb::Transformer::TransformValue ( duckdb_libpgquery::PGValue val)
private

Transform a Postgres constant value into an Expression.

351836 {
351837 switch (val.type) {
351838 case duckdb_libpgquery::T_PGInteger:
351839 D_ASSERT(val.val.ival <= NumericLimits<int32_t>::Maximum());
351840 return make_uniq<ConstantExpression>(Value::INTEGER((int32_t)val.val.ival));
351841 case duckdb_libpgquery::T_PGBitString: // FIXME: this should actually convert to BLOB
351842 case duckdb_libpgquery::T_PGString:
351843 return make_uniq<ConstantExpression>(Value(string(val.val.str)));
351844 case duckdb_libpgquery::T_PGFloat: {
351845 string_t str_val(val.val.str);
351846 bool try_cast_as_integer = true;
351847 bool try_cast_as_decimal = true;
351848 optional_idx decimal_position = optional_idx::Invalid();
351849 idx_t num_underscores = 0;
351850 idx_t num_integer_underscores = 0;
351851 for (idx_t i = 0; i < str_val.GetSize(); i++) {
351852 if (val.val.str[i] == '.') {
351853 // decimal point: cast as either decimal or double
351854 try_cast_as_integer = false;
351855 decimal_position = i;
351856 }
351857 if (val.val.str[i] == 'e' || val.val.str[i] == 'E') {
351858 // found exponent, cast as double
351859 try_cast_as_integer = false;
351860 try_cast_as_decimal = false;
351861 }
351862 if (val.val.str[i] == '_') {
351863 num_underscores++;
351864 if (!decimal_position.IsValid()) {
351865 num_integer_underscores++;
351866 }
351867 }
351868 }
351869 if (try_cast_as_integer) {
351870 int64_t bigint_value;
351871 // try to cast as bigint first
351872 if (TryCast::Operation<string_t, int64_t>(str_val, bigint_value)) {
351873 // successfully cast to bigint: bigint value
351874 return make_uniq<ConstantExpression>(Value::BIGINT(bigint_value));
351875 }
351876 hugeint_t hugeint_value;
351877 // if that is not successful; try to cast as hugeint
351878 if (TryCast::Operation<string_t, hugeint_t>(str_val, hugeint_value)) {
351879 // successfully cast to bigint: bigint value
351880 return make_uniq<ConstantExpression>(Value::HUGEINT(hugeint_value));
351881 }
351882 uhugeint_t uhugeint_value;
351883 // if that is not successful; try to cast as uhugeint
351884 if (TryCast::Operation<string_t, uhugeint_t>(str_val, uhugeint_value)) {
351885 // successfully cast to bigint: bigint value
351886 return make_uniq<ConstantExpression>(Value::UHUGEINT(uhugeint_value));
351887 }
351888 }
351889 idx_t decimal_offset = val.val.str[0] == '-' ? 3 : 2;
351890 if (try_cast_as_decimal && decimal_position.IsValid() &&
351891 str_val.GetSize() - num_underscores < Decimal::MAX_WIDTH_DECIMAL + decimal_offset) {
351892 // figure out the width/scale based on the decimal position
351893 auto width = NumericCast<uint8_t>(str_val.GetSize() - 1 - num_underscores);
351894 auto scale = NumericCast<uint8_t>(width - decimal_position.GetIndex() + num_integer_underscores);
351895 if (val.val.str[0] == '-') {
351896 width--;
351897 }
351898 if (width <= Decimal::MAX_WIDTH_DECIMAL) {
351899 // we can cast the value as a decimal
351900 Value val = Value(str_val);
351901 val = val.DefaultCastAs(LogicalType::DECIMAL(width, scale));
351902 return make_uniq<ConstantExpression>(std::move(val));
351903 }
351904 }
351905 // if there is a decimal or the value is too big to cast as either hugeint or bigint
351906 double dbl_value = Cast::Operation<string_t, double>(str_val);
351907 return make_uniq<ConstantExpression>(Value::DOUBLE(dbl_value));
351908 }
351909 case duckdb_libpgquery::T_PGNull:
351910 return make_uniq<ConstantExpression>(Value(LogicalType::SQLNULL));
351911 default:
351912 throw NotImplementedException("Value not implemented!");
351913 }
351914}
static DUCKDB_API Value HUGEINT(hugeint_t value)
Create a hugeint Value from a specified value.
Definition duckdb.cpp:132153
static DUCKDB_API Value DOUBLE(double value)
Create a double Value from a specified value.
Definition duckdb.cpp:132317
static DUCKDB_API Value UHUGEINT(uhugeint_t value)
Create a uhugeint Value from a specified value.
Definition duckdb.cpp:132160
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformAExpr()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformAExpr ( duckdb_libpgquery::PGAExpr & root)
private

Transform a Postgres operator into an Expression.

352977 {
352978 auto result = TransformAExprInternal(root);
352979 if (result) {
352980 SetQueryLocation(*result, root.location);
352981 }
352982 return result;
352983}

◆ TransformAExprInternal()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformAExprInternal ( duckdb_libpgquery::PGAExpr & root)
private
352840 {
352841 auto name = string(PGPointerCast<duckdb_libpgquery::PGValue>(root.name->head->data.ptr_value)->val.str);
352842
352843 switch (root.kind) {
352844 case duckdb_libpgquery::PG_AEXPR_OP_ALL:
352845 case duckdb_libpgquery::PG_AEXPR_OP_ANY: {
352846 // left=ANY(right)
352847 // we turn this into left=ANY((SELECT UNNEST(right)))
352848 auto left_expr = TransformExpression(root.lexpr);
352849 auto right_expr = TransformExpression(root.rexpr);
352850
352851 auto subquery_expr = make_uniq<SubqueryExpression>();
352852 auto select_statement = make_uniq<SelectStatement>();
352853 auto select_node = make_uniq<SelectNode>();
352854 vector<unique_ptr<ParsedExpression>> children;
352855 children.push_back(std::move(right_expr));
352856
352857 select_node->select_list.push_back(make_uniq<FunctionExpression>("UNNEST", std::move(children)));
352858 select_node->from_table = make_uniq<EmptyTableRef>();
352859 select_statement->node = std::move(select_node);
352860 subquery_expr->subquery = std::move(select_statement);
352861 subquery_expr->subquery_type = SubqueryType::ANY;
352862 subquery_expr->child = std::move(left_expr);
352863 subquery_expr->comparison_type = OperatorToExpressionType(name);
352864 SetQueryLocation(*subquery_expr, root.location);
352865 if (subquery_expr->comparison_type == ExpressionType::INVALID) {
352866 throw ParserException("Unsupported comparison \"%s\" for ANY/ALL subquery", name);
352867 }
352868
352869 if (root.kind == duckdb_libpgquery::PG_AEXPR_OP_ALL) {
352870 // ALL sublink is equivalent to NOT(ANY) with inverted comparison
352871 // e.g. [= ALL()] is equivalent to [NOT(<> ANY())]
352872 // first invert the comparison type
352873 subquery_expr->comparison_type = NegateComparisonExpression(subquery_expr->comparison_type);
352874 return make_uniq<OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(subquery_expr));
352875 }
352876 return std::move(subquery_expr);
352877 }
352878 case duckdb_libpgquery::PG_AEXPR_IN: {
352879 return TransformInExpression(name, root);
352880 }
352881 // rewrite NULLIF(a, b) into CASE WHEN a=b THEN NULL ELSE a END
352882 case duckdb_libpgquery::PG_AEXPR_NULLIF: {
352883 vector<unique_ptr<ParsedExpression>> children;
352884 children.push_back(TransformExpression(root.lexpr));
352885 children.push_back(TransformExpression(root.rexpr));
352886 return make_uniq<FunctionExpression>("nullif", std::move(children));
352887 }
352888 // rewrite (NOT) X BETWEEN A AND B into (NOT) AND(GREATERTHANOREQUALTO(X,
352889 // A), LESSTHANOREQUALTO(X, B))
352890 case duckdb_libpgquery::PG_AEXPR_BETWEEN:
352891 case duckdb_libpgquery::PG_AEXPR_NOT_BETWEEN: {
352892 auto between_args = PGPointerCast<duckdb_libpgquery::PGList>(root.rexpr);
352893 if (between_args->length != 2 || !between_args->head->data.ptr_value || !between_args->tail->data.ptr_value) {
352894 throw InternalException("(NOT) BETWEEN needs two args");
352895 }
352896
352897 auto input = TransformExpression(root.lexpr);
352898 auto between_left =
352899 TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(between_args->head->data.ptr_value));
352900 auto between_right =
352901 TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(between_args->tail->data.ptr_value));
352902
352903 auto compare_between =
352904 make_uniq<BetweenExpression>(std::move(input), std::move(between_left), std::move(between_right));
352905 if (root.kind == duckdb_libpgquery::PG_AEXPR_BETWEEN) {
352906 return std::move(compare_between);
352907 } else {
352908 return make_uniq<OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(compare_between));
352909 }
352910 }
352911 // rewrite SIMILAR TO into regexp_full_match('asdf', '.*sd.*')
352912 case duckdb_libpgquery::PG_AEXPR_SIMILAR: {
352913 auto left_expr = TransformExpression(root.lexpr);
352914 auto right_expr = TransformExpression(root.rexpr);
352915
352916 vector<unique_ptr<ParsedExpression>> children;
352917 children.push_back(std::move(left_expr));
352918
352919 auto &similar_func = right_expr->Cast<FunctionExpression>();
352920 D_ASSERT(similar_func.function_name == "similar_escape");
352921 D_ASSERT(similar_func.children.size() == 2);
352922 if (similar_func.children[1]->GetExpressionType() != ExpressionType::VALUE_CONSTANT) {
352923 throw NotImplementedException("Custom escape in SIMILAR TO");
352924 }
352925 auto &constant = similar_func.children[1]->Cast<ConstantExpression>();
352926 if (!constant.value.IsNull()) {
352927 throw NotImplementedException("Custom escape in SIMILAR TO");
352928 }
352929 // take the child of the similar_func
352930 children.push_back(std::move(similar_func.children[0]));
352931
352932 // this looks very odd, but seems to be the way to find out its NOT IN
352933 bool invert_similar = false;
352934 if (name == "!~") {
352935 // NOT SIMILAR TO
352936 invert_similar = true;
352937 }
352938 const auto regex_function = "regexp_full_match";
352939 auto result = make_uniq<FunctionExpression>(regex_function, std::move(children));
352940
352941 if (invert_similar) {
352942 return make_uniq<OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(result));
352943 } else {
352944 return std::move(result);
352945 }
352946 }
352947 case duckdb_libpgquery::PG_AEXPR_NOT_DISTINCT: {
352948 auto left_expr = TransformExpression(root.lexpr);
352949 auto right_expr = TransformExpression(root.rexpr);
352950 return make_uniq<ComparisonExpression>(ExpressionType::COMPARE_NOT_DISTINCT_FROM, std::move(left_expr),
352951 std::move(right_expr));
352952 }
352953 case duckdb_libpgquery::PG_AEXPR_DISTINCT: {
352954 auto left_expr = TransformExpression(root.lexpr);
352955 auto right_expr = TransformExpression(root.rexpr);
352956 return make_uniq<ComparisonExpression>(ExpressionType::COMPARE_DISTINCT_FROM, std::move(left_expr),
352957 std::move(right_expr));
352958 }
352959
352960 default:
352961 break;
352962 }
352963 auto left_expr = TransformExpression(root.lexpr);
352964 auto right_expr = TransformExpression(root.rexpr);
352965
352966 if (!left_expr) {
352967 // prefix operator
352968 return TransformUnaryOperator(name, std::move(right_expr));
352969 } else if (!right_expr) {
352970 // postfix operator, only ! is currently supported
352971 return TransformUnaryOperator(name + "__postfix", std::move(left_expr));
352972 } else {
352973 return TransformBinaryOperator(std::move(name), std::move(left_expr), std::move(right_expr));
352974 }
352975}
unique_ptr< ParsedExpression > TransformInExpression(const string &name, duckdb_libpgquery::PGAExpr &root)
Transform to a IN or NOT IN expression.
Definition duckdb.cpp:352811

◆ TransformExpression() [1/2]

unique_ptr< ParsedExpression > duckdb::Transformer::TransformExpression ( optional_ptr< duckdb_libpgquery::PGNode > node)
private

Transform a Postgres abstract expression into an Expression.

352010 {
352011 if (!node) {
352012 return nullptr;
352013 }
352014 return TransformExpression(*node);
352015}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformExpression() [2/2]

unique_ptr< ParsedExpression > duckdb::Transformer::TransformExpression ( duckdb_libpgquery::PGNode & node)
private
351949 {
351950 auto stack_checker = StackCheck();
351951
351952 switch (node.type) {
351953 case duckdb_libpgquery::T_PGColumnRef:
351954 return TransformColumnRef(PGCast<duckdb_libpgquery::PGColumnRef>(node));
351955 case duckdb_libpgquery::T_PGAConst:
351956 return TransformConstant(PGCast<duckdb_libpgquery::PGAConst>(node));
351957 case duckdb_libpgquery::T_PGAExpr:
351958 return TransformAExpr(PGCast<duckdb_libpgquery::PGAExpr>(node));
351959 case duckdb_libpgquery::T_PGFuncCall:
351960 return TransformFuncCall(PGCast<duckdb_libpgquery::PGFuncCall>(node));
351961 case duckdb_libpgquery::T_PGBoolExpr:
351962 return TransformBoolExpr(PGCast<duckdb_libpgquery::PGBoolExpr>(node));
351963 case duckdb_libpgquery::T_PGTypeCast:
351964 return TransformTypeCast(PGCast<duckdb_libpgquery::PGTypeCast>(node));
351965 case duckdb_libpgquery::T_PGCaseExpr:
351966 return TransformCase(PGCast<duckdb_libpgquery::PGCaseExpr>(node));
351967 case duckdb_libpgquery::T_PGSubLink:
351968 return TransformSubquery(PGCast<duckdb_libpgquery::PGSubLink>(node));
351969 case duckdb_libpgquery::T_PGCoalesceExpr:
351970 return TransformCoalesce(PGCast<duckdb_libpgquery::PGAExpr>(node));
351971 case duckdb_libpgquery::T_PGNullTest:
351972 return TransformNullTest(PGCast<duckdb_libpgquery::PGNullTest>(node));
351973 case duckdb_libpgquery::T_PGResTarget:
351974 return TransformResTarget(PGCast<duckdb_libpgquery::PGResTarget>(node));
351975 case duckdb_libpgquery::T_PGParamRef:
351976 return TransformParamRef(PGCast<duckdb_libpgquery::PGParamRef>(node));
351977 case duckdb_libpgquery::T_PGNamedArgExpr:
351978 return TransformNamedArg(PGCast<duckdb_libpgquery::PGNamedArgExpr>(node));
351979 case duckdb_libpgquery::T_PGSQLValueFunction:
351980 return TransformSQLValueFunction(PGCast<duckdb_libpgquery::PGSQLValueFunction>(node));
351981 case duckdb_libpgquery::T_PGSetToDefault:
351982 return make_uniq<DefaultExpression>();
351983 case duckdb_libpgquery::T_PGCollateClause:
351984 return TransformCollateExpr(PGCast<duckdb_libpgquery::PGCollateClause>(node));
351985 case duckdb_libpgquery::T_PGIntervalConstant:
351986 return TransformInterval(PGCast<duckdb_libpgquery::PGIntervalConstant>(node));
351987 case duckdb_libpgquery::T_PGLambdaFunction:
351988 return TransformLambda(PGCast<duckdb_libpgquery::PGLambdaFunction>(node));
351989 case duckdb_libpgquery::T_PGSingleArrowFunction:
351990 return TransformSingleArrow(PGCast<duckdb_libpgquery::PGSingleArrowFunction>(node));
351991 case duckdb_libpgquery::T_PGAIndirection:
351992 return TransformArrayAccess(PGCast<duckdb_libpgquery::PGAIndirection>(node));
351993 case duckdb_libpgquery::T_PGPositionalReference:
351994 return TransformPositionalReference(PGCast<duckdb_libpgquery::PGPositionalReference>(node));
351995 case duckdb_libpgquery::T_PGGroupingFunc:
351996 return TransformGroupingFunction(PGCast<duckdb_libpgquery::PGGroupingFunc>(node));
351997 case duckdb_libpgquery::T_PGAStar:
351998 return TransformStarExpression(PGCast<duckdb_libpgquery::PGAStar>(node));
351999 case duckdb_libpgquery::T_PGBooleanTest:
352000 return TransformBooleanTest(PGCast<duckdb_libpgquery::PGBooleanTest>(node));
352001 case duckdb_libpgquery::T_PGMultiAssignRef:
352002 return TransformMultiAssignRef(PGCast<duckdb_libpgquery::PGMultiAssignRef>(node));
352003 case duckdb_libpgquery::T_PGString:
352004 return TransformValue(PGCast<duckdb_libpgquery::PGValue>(node));
352005 default:
352006 throw NotImplementedException("Expression type %s (%d)", NodetypeToString(node.type), (int)node.type);
352007 }
352008}
unique_ptr< ParsedExpression > TransformBoolExpr(duckdb_libpgquery::PGBoolExpr &root)
Transform a Postgres boolean expression into an Expression.
Definition duckdb.cpp:351487
unique_ptr< ParsedExpression > TransformMultiAssignRef(duckdb_libpgquery::PGMultiAssignRef &root)
Transform multi assignment reference into an Expression.
Definition duckdb.cpp:352712
unique_ptr< ParsedExpression > TransformTypeCast(duckdb_libpgquery::PGTypeCast &root)
Transform a Postgres type cast into an Expression.
Definition duckdb.cpp:351636
unique_ptr< ParsedExpression > TransformAExpr(duckdb_libpgquery::PGAExpr &root)
Transform a Postgres operator into an Expression.
Definition duckdb.cpp:352977
unique_ptr< ParsedExpression > TransformColumnRef(duckdb_libpgquery::PGColumnRef &root)
Transform a Postgres column reference into an Expression.
Definition duckdb.cpp:351800
unique_ptr< ParsedExpression > TransformConstant(duckdb_libpgquery::PGAConst &c)
Transform a Postgres constant value into an Expression.
Definition duckdb.cpp:351916
unique_ptr< ParsedExpression > TransformCase(duckdb_libpgquery::PGCaseExpr &root)
Transform a Postgres case expression into an Expression.
Definition duckdb.cpp:351598
unique_ptr< ParsedExpression > TransformPositionalReference(duckdb_libpgquery::PGPositionalReference &node)
Transform a positional reference (e.g. #1).
Definition duckdb.cpp:353055
unique_ptr< ParsedExpression > TransformCoalesce(duckdb_libpgquery::PGAExpr &root)
Transform a Postgres coalesce into an Expression.
Definition duckdb.cpp:351673
unique_ptr< ParsedExpression > TransformLambda(duckdb_libpgquery::PGLambdaFunction &node)
Transform a LAMBDA node (e.g., lambda x, y: x + y) into a lambda expression.
Definition duckdb.cpp:352680
unique_ptr< ParsedExpression > TransformArrayAccess(duckdb_libpgquery::PGAIndirection &node)
Transform a Postgres array access node (e.g. x[1] or x[1:3]).
Definition duckdb.cpp:351409
unique_ptr< ParsedExpression > TransformInterval(duckdb_libpgquery::PGIntervalConstant &root)
Transform a Postgres boolean expression into an Expression.
Definition duckdb.cpp:352501
unique_ptr< ParsedExpression > TransformSingleArrow(duckdb_libpgquery::PGSingleArrowFunction &node)
Transform a single arrow operator (e.g., (x, y) -> x + y) into a lambda expression.
Definition duckdb.cpp:352693

◆ TransformFuncCall()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformFuncCall ( duckdb_libpgquery::PGFuncCall & root)
private

Transform a Postgres function call into an Expression.

352203 {
352204 auto name = root.funcname;
352205 string catalog, schema, function_name;
352206 if (name->length == 3) {
352207 // catalog + schema + name
352208 catalog = PGPointerCast<duckdb_libpgquery::PGValue>(name->head->data.ptr_value)->val.str;
352209 schema = PGPointerCast<duckdb_libpgquery::PGValue>(name->head->next->data.ptr_value)->val.str;
352210 function_name = PGPointerCast<duckdb_libpgquery::PGValue>(name->head->next->next->data.ptr_value)->val.str;
352211 } else if (name->length == 2) {
352212 // schema + name
352213 catalog = INVALID_CATALOG;
352214 schema = PGPointerCast<duckdb_libpgquery::PGValue>(name->head->data.ptr_value)->val.str;
352215 function_name = PGPointerCast<duckdb_libpgquery::PGValue>(name->head->next->data.ptr_value)->val.str;
352216 } else if (name->length == 1) {
352217 // unqualified name
352218 catalog = INVALID_CATALOG;
352219 schema = INVALID_SCHEMA;
352220 function_name = PGPointerCast<duckdb_libpgquery::PGValue>(name->head->data.ptr_value)->val.str;
352221 } else {
352222 throw ParserException("TransformFuncCall - Expected 1, 2 or 3 qualifications");
352223 }
352224
352225 // transform children
352226 vector<unique_ptr<ParsedExpression>> children;
352227 if (root.args) {
352228 TransformExpressionList(*root.args, children);
352229 }
352230 if (children.size() == 1 && ExpressionIsEmptyStar(*children[0]) && !root.agg_distinct && !root.agg_order) {
352231 // COUNT(*) gets translated into COUNT()
352232 children.clear();
352233 }
352234
352235 auto lowercase_name = StringUtil::Lower(function_name);
352236 if (root.over) {
352237 if (InWindowDefinition()) {
352238 throw ParserException("window functions are not allowed in window definitions");
352239 }
352240
352241 const auto win_fun_type = WindowExpression::WindowToExpressionType(lowercase_name);
352242 if (win_fun_type == ExpressionType::INVALID) {
352243 throw InternalException("Unknown/unsupported window function");
352244 }
352245
352246 if (win_fun_type != ExpressionType::WINDOW_AGGREGATE && root.agg_distinct) {
352247 throw ParserException("DISTINCT is not implemented for non-aggregate window functions!");
352248 }
352249
352250 if (root.agg_order && !IsOrderableWindowFunction(win_fun_type)) {
352251 throw ParserException("ORDER BY is not supported for the window function \"%s\"", lowercase_name.c_str());
352252 }
352253
352254 if (win_fun_type != ExpressionType::WINDOW_AGGREGATE && root.agg_filter) {
352255 throw ParserException("FILTER is not implemented for non-aggregate window functions!");
352256 }
352257 if (root.export_state) {
352258 throw ParserException("EXPORT_STATE is not supported for window functions!");
352259 }
352260
352261 if (win_fun_type == ExpressionType::WINDOW_AGGREGATE &&
352262 root.agg_ignore_nulls != duckdb_libpgquery::PG_DEFAULT_NULLS) {
352263 throw ParserException("RESPECT/IGNORE NULLS is not supported for windowed aggregates");
352264 }
352265
352266 auto expr = make_uniq<WindowExpression>(win_fun_type, std::move(catalog), std::move(schema), lowercase_name);
352267 expr->ignore_nulls = (root.agg_ignore_nulls == duckdb_libpgquery::PG_IGNORE_NULLS);
352268 expr->distinct = root.agg_distinct;
352269
352270 if (root.agg_filter) {
352271 auto filter_expr = TransformExpression(root.agg_filter);
352272 expr->filter_expr = std::move(filter_expr);
352273 }
352274
352275 if (root.agg_order) {
352276 auto order_bys = make_uniq<OrderModifier>();
352277 TransformOrderBy(root.agg_order, order_bys->orders);
352278 expr->arg_orders = std::move(order_bys->orders);
352279 }
352280
352281 if (win_fun_type == ExpressionType::WINDOW_AGGREGATE) {
352282 expr->children = std::move(children);
352283 } else {
352284 if (!children.empty()) {
352285 expr->children.push_back(std::move(children[0]));
352286 }
352287 if (win_fun_type == ExpressionType::WINDOW_LEAD || win_fun_type == ExpressionType::WINDOW_LAG) {
352288 if (children.size() > 1) {
352289 expr->offset_expr = std::move(children[1]);
352290 }
352291 if (children.size() > 2) {
352292 expr->default_expr = std::move(children[2]);
352293 }
352294 if (children.size() > 3) {
352295 throw ParserException("Incorrect number of parameters for function %s", lowercase_name);
352296 }
352297 } else if (win_fun_type == ExpressionType::WINDOW_NTH_VALUE) {
352298 if (children.size() > 1) {
352299 expr->children.push_back(std::move(children[1]));
352300 }
352301 if (children.size() > 2) {
352302 throw ParserException("Incorrect number of parameters for function %s", lowercase_name);
352303 }
352304 } else {
352305 if (children.size() > 1) {
352306 throw ParserException("Incorrect number of parameters for function %s", lowercase_name);
352307 }
352308 }
352309 }
352310 auto window_spec = PGPointerCast<duckdb_libpgquery::PGWindowDef>(root.over);
352311 if (window_spec->name) {
352312 auto it = window_clauses.find(string(window_spec->name));
352313 if (it == window_clauses.end()) {
352314 throw ParserException("window \"%s\" does not exist", window_spec->name);
352315 }
352316 window_spec = it->second;
352317 D_ASSERT(window_spec);
352318 }
352319 auto window_ref = window_spec;
352320 auto window_name = window_ref->refname;
352321 if (window_ref->refname) {
352322 auto it = window_clauses.find(string(window_spec->refname));
352323 if (it == window_clauses.end()) {
352324 throw ParserException("window \"%s\" does not exist", window_spec->refname);
352325 }
352326 window_ref = it->second;
352327 D_ASSERT(window_ref);
352328 if (window_ref->startOffset || window_ref->endOffset || window_ref->frameOptions != FRAMEOPTION_DEFAULTS) {
352329 throw ParserException("cannot copy window \"%s\" because it has a frame clause", window_spec->refname);
352330 }
352331 }
352332 in_window_definition = true;
352333 TransformWindowDef(*window_ref, *expr);
352334 if (window_ref != window_spec) {
352335 TransformWindowDef(*window_spec, *expr, window_name);
352336 }
352337 TransformWindowFrame(*window_spec, *expr);
352338 in_window_definition = false;
352339 SetQueryLocation(*expr, root.location);
352340 return std::move(expr);
352341 }
352342
352343 if (root.agg_ignore_nulls != duckdb_libpgquery::PG_DEFAULT_NULLS) {
352344 throw ParserException("RESPECT/IGNORE NULLS is not supported for non-window functions");
352345 }
352346
352347 unique_ptr<ParsedExpression> filter_expr;
352348 if (root.agg_filter) {
352349 filter_expr = TransformExpression(root.agg_filter);
352350 }
352351
352352 auto order_bys = make_uniq<OrderModifier>();
352353 TransformOrderBy(root.agg_order, order_bys->orders);
352354
352355 // Ordered aggregates can be either WITHIN GROUP or after the function arguments
352356 if (root.agg_within_group) {
352357 // https://www.postgresql.org/docs/current/functions-aggregate.html#FUNCTIONS-ORDEREDSET-TABLE
352358 // Since we implement "ordered aggregates" without sorting,
352359 // we map all the ones we support to the corresponding aggregate function.
352360 if (order_bys->orders.size() != 1) {
352361 throw ParserException("Cannot use multiple ORDER BY clauses with WITHIN GROUP");
352362 }
352363 if (lowercase_name == "percentile_cont") {
352364 if (children.size() != 1) {
352365 throw ParserException("Wrong number of arguments for PERCENTILE_CONT");
352366 }
352367 lowercase_name = "quantile_cont";
352368 } else if (lowercase_name == "percentile_disc") {
352369 if (children.size() != 1) {
352370 throw ParserException("Wrong number of arguments for PERCENTILE_DISC");
352371 }
352372 lowercase_name = "quantile_disc";
352373 } else if (lowercase_name == "mode") {
352374 if (!children.empty()) {
352375 throw ParserException("Wrong number of arguments for MODE");
352376 }
352377 lowercase_name = "mode";
352378 } else {
352379 throw ParserException("Unknown ordered aggregate \"%s\".", function_name);
352380 }
352381 }
352382
352383 // star gets eaten in the parser
352384 if (lowercase_name == "count" && children.empty()) {
352385 lowercase_name = "count_star";
352386 }
352387
352388 if (lowercase_name == "if") {
352389 if (children.size() != 3) {
352390 throw ParserException("Wrong number of arguments to IF.");
352391 }
352392 auto expr = make_uniq<CaseExpression>();
352393 CaseCheck check;
352394 check.when_expr = std::move(children[0]);
352395 check.then_expr = std::move(children[1]);
352396 expr->case_checks.push_back(std::move(check));
352397 expr->else_expr = std::move(children[2]);
352398 return std::move(expr);
352399 } else if (lowercase_name == "unpack") {
352400 if (children.size() != 1) {
352401 throw ParserException("Wrong number of arguments to the UNPACK operator");
352402 }
352403 auto expr = make_uniq<OperatorExpression>(ExpressionType::OPERATOR_UNPACK);
352404 expr->children = std::move(children);
352405 return std::move(expr);
352406 } else if (lowercase_name == "try") {
352407 if (children.size() != 1) {
352408 throw ParserException("Wrong number of arguments provided to TRY expression");
352409 }
352410 auto try_expression = make_uniq<OperatorExpression>(ExpressionType::OPERATOR_TRY);
352411 try_expression->children = std::move(children);
352412 return std::move(try_expression);
352413 } else if (lowercase_name == "construct_array") {
352414 auto construct_array = make_uniq<OperatorExpression>(ExpressionType::ARRAY_CONSTRUCTOR);
352415 construct_array->children = std::move(children);
352416 return std::move(construct_array);
352417 } else if (lowercase_name == "__internal_position_operator") {
352418 if (children.size() != 2) {
352419 throw ParserException("Wrong number of arguments to __internal_position_operator.");
352420 }
352421 // swap arguments for POSITION(x IN y)
352422 std::swap(children[0], children[1]);
352423 lowercase_name = "position";
352424 } else if (lowercase_name == "ifnull") {
352425 if (children.size() != 2) {
352426 throw ParserException("Wrong number of arguments to IFNULL.");
352427 }
352428
352429 // Two-argument COALESCE
352430 auto coalesce_op = make_uniq<OperatorExpression>(ExpressionType::OPERATOR_COALESCE);
352431 coalesce_op->children.push_back(std::move(children[0]));
352432 coalesce_op->children.push_back(std::move(children[1]));
352433 return std::move(coalesce_op);
352434 } else if (lowercase_name == "list" && order_bys->orders.size() == 1) {
352435 // list(expr ORDER BY expr <sense> <nulls>) => list_sort(list(expr), <sense>, <nulls>)
352436 if (children.size() != 1) {
352437 throw ParserException("Wrong number of arguments to LIST.");
352438 }
352439 auto arg_expr = children[0].get();
352440 auto &order_by = order_bys->orders[0];
352441 if (arg_expr->Equals(*order_by.expression)) {
352442 auto sense = make_uniq<ConstantExpression>(EnumUtil::ToChars(order_by.type));
352443 auto nulls = make_uniq<ConstantExpression>(EnumUtil::ToChars(order_by.null_order));
352444 order_bys = nullptr;
352445 auto unordered = make_uniq<FunctionExpression>(catalog, schema, lowercase_name.c_str(), std::move(children),
352446 std::move(filter_expr), std::move(order_bys),
352447 root.agg_distinct, false, root.export_state);
352448 lowercase_name = "list_sort";
352449 order_bys.reset(); // NOLINT
352450 filter_expr.reset(); // NOLINT
352451 children.clear(); // NOLINT
352452 root.agg_distinct = false;
352453 children.emplace_back(std::move(unordered));
352454 children.emplace_back(std::move(sense));
352455 children.emplace_back(std::move(nulls));
352456 }
352457 } else if (lowercase_name == "date") {
352458 if (children.size() != 1) {
352459 throw ParserException("Wrong number of arguments provided to DATE function");
352460 }
352461 return std::move(make_uniq<CastExpression>(LogicalType::DATE, std::move(children[0])));
352462 }
352463
352464 auto function = make_uniq<FunctionExpression>(std::move(catalog), std::move(schema), lowercase_name.c_str(),
352465 std::move(children), std::move(filter_expr), std::move(order_bys),
352466 root.agg_distinct, false, root.export_state);
352467 SetQueryLocation(*function, root.location);
352468
352469 return std::move(function);
352470}
bool ExpressionIsEmptyStar(ParsedExpression &expr)
Returns true if an expression is only a star (i.e. "*", without any other decorators).
Definition duckdb.cpp:352160
void TransformWindowFrame(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr)
Transform a Postgres window frame specification into frame expressions.
Definition duckdb.cpp:352099
void TransformWindowDef(duckdb_libpgquery::PGWindowDef &window_spec, WindowExpression &expr, const char *window_name=nullptr)
Transform a Postgres PARTITION BY/ORDER BY specification into lists of expressions.
Definition duckdb.cpp:352041
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformInterval()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformInterval ( duckdb_libpgquery::PGIntervalConstant & root)
private

Transform a Postgres boolean expression into an Expression.

352501 {
352502 // handle post-fix notation of INTERVAL
352503
352504 // three scenarios
352505 // interval (expr) year
352506 // interval 'string' year
352507 // interval int year
352508 unique_ptr<ParsedExpression> expr;
352509 switch (node.val_type) {
352510 case duckdb_libpgquery::T_PGAExpr:
352511 expr = TransformExpression(node.eval);
352512 break;
352513 case duckdb_libpgquery::T_PGString:
352514 expr = make_uniq<ConstantExpression>(Value(node.sval));
352515 break;
352516 case duckdb_libpgquery::T_PGInteger:
352517 expr = make_uniq<ConstantExpression>(Value(node.ival));
352518 break;
352519 default:
352520 throw InternalException("Unsupported interval transformation");
352521 }
352522 SetQueryLocation(*expr, node.location);
352523
352524 if (!node.typmods) {
352525 auto result = make_uniq<CastExpression>(LogicalType::INTERVAL, std::move(expr));
352526 SetQueryLocation(*result, node.location);
352527 return std::move(result);
352528 }
352529
352530 int32_t mask = NumericCast<int32_t>(
352531 PGPointerCast<duckdb_libpgquery::PGAConst>(node.typmods->head->data.ptr_value)->val.val.ival);
352532 // these seemingly random constants are from libpg_query/include/utils/datetime.hpp
352533 // they are copied here to avoid having to include this header
352534 // the bitshift is from the function INTERVAL_MASK in the parser
352535 constexpr int32_t MONTH_MASK = 1 << 1;
352536 constexpr int32_t YEAR_MASK = 1 << 2;
352537 constexpr int32_t DAY_MASK = 1 << 3;
352538 constexpr int32_t HOUR_MASK = 1 << 10;
352539 constexpr int32_t MINUTE_MASK = 1 << 11;
352540 constexpr int32_t SECOND_MASK = 1 << 12;
352541 constexpr int32_t MILLISECOND_MASK = 1 << 13;
352542 constexpr int32_t MICROSECOND_MASK = 1 << 14;
352543 constexpr int32_t WEEK_MASK = 1 << 24;
352544 constexpr int32_t DECADE_MASK = 1 << 25;
352545 constexpr int32_t CENTURY_MASK = 1 << 26;
352546 constexpr int32_t MILLENNIUM_MASK = 1 << 27;
352547 constexpr int32_t QUARTER_MASK = 1 << 29;
352548
352549 // we need to check certain combinations
352550 // because certain interval masks (e.g. INTERVAL '10' HOURS TO DAYS) set multiple bits
352551 // for now we don't support all of the combined ones
352552 // (we might add support if someone complains about it)
352553
352554 string fname;
352555 LogicalType parse_type = LogicalType::DOUBLE;
352556 LogicalType target_type;
352557 if (mask & YEAR_MASK && mask & MONTH_MASK) {
352558 // DAY TO HOUR
352559 throw ParserException("YEAR TO MONTH is not supported");
352560 } else if (mask & DAY_MASK && mask & HOUR_MASK) {
352561 // DAY TO HOUR
352562 throw ParserException("DAY TO HOUR is not supported");
352563 } else if (mask & DAY_MASK && mask & MINUTE_MASK) {
352564 // DAY TO MINUTE
352565 throw ParserException("DAY TO MINUTE is not supported");
352566 } else if (mask & DAY_MASK && mask & SECOND_MASK) {
352567 // DAY TO SECOND
352568 throw ParserException("DAY TO SECOND is not supported");
352569 } else if (mask & HOUR_MASK && mask & MINUTE_MASK) {
352570 // DAY TO SECOND
352571 throw ParserException("HOUR TO MINUTE is not supported");
352572 } else if (mask & HOUR_MASK && mask & SECOND_MASK) {
352573 // DAY TO SECOND
352574 throw ParserException("HOUR TO SECOND is not supported");
352575 } else if (mask & MINUTE_MASK && mask & SECOND_MASK) {
352576 // DAY TO SECOND
352577 throw ParserException("MINUTE TO SECOND is not supported");
352578 } else if (mask & YEAR_MASK) {
352579 // YEAR
352580 fname = "to_years";
352581 target_type = LogicalType::INTEGER;
352582 } else if (mask & MONTH_MASK) {
352583 // MONTH
352584 fname = "to_months";
352585 target_type = LogicalType::INTEGER;
352586 } else if (mask & DAY_MASK) {
352587 // DAY
352588 fname = "to_days";
352589 target_type = LogicalType::INTEGER;
352590 } else if (mask & HOUR_MASK) {
352591 // HOUR
352592 fname = "to_hours";
352593 target_type = LogicalType::BIGINT;
352594 } else if (mask & MINUTE_MASK) {
352595 // MINUTE
352596 fname = "to_minutes";
352597 target_type = LogicalType::BIGINT;
352598 } else if (mask & SECOND_MASK) {
352599 // SECOND
352600 fname = "to_seconds";
352601 target_type = LogicalType::DOUBLE;
352602 } else if (mask & MILLISECOND_MASK) {
352603 // MILLISECOND
352604 fname = "to_milliseconds";
352605 target_type = LogicalType::DOUBLE;
352606 } else if (mask & MICROSECOND_MASK) {
352607 // MICROSECOND
352608 fname = "to_microseconds";
352609 target_type = LogicalType::BIGINT;
352610 } else if (mask & WEEK_MASK) {
352611 // WEEK
352612 fname = "to_weeks";
352613 target_type = LogicalType::INTEGER;
352614 } else if (mask & QUARTER_MASK) {
352615 // QUARTER
352616 fname = "to_quarters";
352617 target_type = LogicalType::INTEGER;
352618 } else if (mask & DECADE_MASK) {
352619 // DECADE
352620 fname = "to_decades";
352621 target_type = LogicalType::INTEGER;
352622 } else if (mask & CENTURY_MASK) {
352623 // CENTURY
352624 fname = "to_centuries";
352625 target_type = LogicalType::INTEGER;
352626 } else if (mask & MILLENNIUM_MASK) {
352627 // MILLENNIUM
352628 fname = "to_millennia";
352629 target_type = LogicalType::INTEGER;
352630 } else {
352631 throw InternalException("Unsupported interval post-fix");
352632 }
352633 // first push a cast to the parse type
352634 expr = make_uniq<CastExpression>(parse_type, std::move(expr));
352635 SetQueryLocation(*expr, node.location);
352636
352637 // next, truncate it if the target type doesn't match the parse type
352638 if (target_type != parse_type) {
352639 vector<unique_ptr<ParsedExpression>> children;
352640 children.push_back(std::move(expr));
352641 expr = make_uniq<FunctionExpression>("trunc", std::move(children));
352642 expr = make_uniq<CastExpression>(target_type, std::move(expr));
352643 }
352644 // now push the operation
352645 vector<unique_ptr<ParsedExpression>> children;
352646 children.push_back(std::move(expr));
352647 auto result = make_uniq<FunctionExpression>(fname, std::move(children));
352648 SetQueryLocation(*result, node.location);
352649 return std::move(result);
352650}
GMat mask(const GMat &src, const GMat &mask)
::int32_t int32_t
Here is the call graph for this function:

◆ TransformLambda()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformLambda ( duckdb_libpgquery::PGLambdaFunction & node)
private

Transform a LAMBDA node (e.g., lambda x, y: x + y) into a lambda expression.

352680 {
352681 D_ASSERT(node.lhs);
352682 D_ASSERT(node.rhs);
352683
352684 auto parameters = TransformStringList(node.lhs);
352685 auto rhs = TransformExpression(node.rhs);
352686 D_ASSERT(!parameters.empty());
352687 D_ASSERT(rhs);
352688 auto result = make_uniq<LambdaExpression>(std::move(parameters), std::move(rhs));
352689 SetQueryLocation(*result, node.location);
352690 return std::move(result);
352691}
Here is the call graph for this function:

◆ TransformSingleArrow()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformSingleArrow ( duckdb_libpgquery::PGSingleArrowFunction & node)
private

Transform a single arrow operator (e.g., (x, y) -> x + y) into a lambda expression.

352693 {
352694 D_ASSERT(node.lhs);
352695 D_ASSERT(node.rhs);
352696
352697 auto lhs = TransformExpression(node.lhs);
352698 auto rhs = TransformExpression(node.rhs);
352699 D_ASSERT(lhs);
352700 D_ASSERT(rhs);
352701 auto result = make_uniq<LambdaExpression>(std::move(lhs), std::move(rhs));
352702 SetQueryLocation(*result, node.location);
352703 return std::move(result);
352704}
Here is the call graph for this function:

◆ TransformArrayAccess()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformArrayAccess ( duckdb_libpgquery::PGAIndirection & node)
private

Transform a Postgres array access node (e.g. x[1] or x[1:3]).

351409 {
351410 // Transform the source expression.
351411 unique_ptr<ParsedExpression> result;
351412 result = TransformExpression(indirection_node.arg);
351413
351414 // Iterate the indices.
351415 // For more complex expressions like (foo).field_name[42] a single indirection
351416 // node can contain multiple indices.
351417 idx_t list_size = 0;
351418 for (auto node = indirection_node.indirection->head; node != nullptr; node = node->next) {
351419 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
351420
351421 switch (target->type) {
351422 case duckdb_libpgquery::T_PGAIndices: {
351423 // Index access.
351424 auto indices = PGCast<duckdb_libpgquery::PGAIndices>(*target.get());
351425 vector<unique_ptr<ParsedExpression>> children;
351426 children.push_back(std::move(result));
351427
351428 if (indices.is_slice) {
351429 // If either the lower or upper bound is not specified, we use an empty constant LIST,
351430 // which we handle in the execution.
351431 auto constant_list = make_uniq<ConstantExpression>(Value::LIST(LogicalType::INTEGER, vector<Value>()));
351432
351433 auto lower = indices.lidx ? TransformExpression(indices.lidx) : constant_list->Copy();
351434 children.push_back(std::move(lower));
351435 auto upper = indices.uidx ? TransformExpression(indices.uidx) : constant_list->Copy();
351436 children.push_back(std::move(upper));
351437
351438 if (indices.step) {
351439 children.push_back(TransformExpression(indices.step));
351440 }
351441 result = make_uniq<OperatorExpression>(ExpressionType::ARRAY_SLICE, std::move(children));
351442 break;
351443 }
351444
351445 // Array access.
351446 D_ASSERT(!indices.lidx && indices.uidx);
351447 children.push_back(TransformExpression(indices.uidx));
351448 result = make_uniq<OperatorExpression>(ExpressionType::ARRAY_EXTRACT, std::move(children));
351449 break;
351450 }
351451 case duckdb_libpgquery::T_PGString: {
351452 auto value = PGCast<duckdb_libpgquery::PGValue>(*target.get());
351453 vector<unique_ptr<ParsedExpression>> children;
351454 children.push_back(std::move(result));
351455 children.push_back(TransformValue(value));
351456 result = make_uniq<OperatorExpression>(ExpressionType::STRUCT_EXTRACT, std::move(children));
351457 break;
351458 }
351459 case duckdb_libpgquery::T_PGFuncCall: {
351460 auto func = PGCast<duckdb_libpgquery::PGFuncCall>(*target.get());
351461 auto function = TransformFuncCall(func);
351462 if (function->GetExpressionType() != ExpressionType::FUNCTION) {
351463 throw ParserException("%s.%s() call must be a function", result->ToString(), function->ToString());
351464 }
351465 auto &function_expr = function->Cast<FunctionExpression>();
351466 function_expr.children.insert(function_expr.children.begin(), std::move(result));
351467 result = std::move(function);
351468 break;
351469 }
351470 default:
351471 throw NotImplementedException("Unimplemented subscript type");
351472 }
351473
351474 list_size++;
351475 StackCheck(list_size);
351476 }
351477 return result;
351478}
static DUCKDB_API Value LIST(const LogicalType &child_type, vector< Value > values)
Create a list value with the given entries.
Definition duckdb.cpp:132555
Here is the call graph for this function:

◆ TransformPositionalReference()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformPositionalReference ( duckdb_libpgquery::PGPositionalReference & node)
private

Transform a positional reference (e.g. #1).

353055 {
353056 if (node.position <= 0) {
353057 throw ParserException("Positional reference node needs to be >= 1");
353058 }
353059 auto result = make_uniq<PositionalReferenceExpression>(NumericCast<idx_t>(node.position));
353060 SetQueryLocation(*result, node.location);
353061 return std::move(result);
353062}

◆ TransformStarExpression()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformStarExpression ( duckdb_libpgquery::PGAStar & node)
private
351722 {
351723 auto result = make_uniq<StarExpression>(star.relation ? star.relation : string());
351724 if (star.except_list) {
351725 for (auto head = star.except_list->head; head; head = head->next) {
351726 auto exclude_column_list = PGPointerCast<duckdb_libpgquery::PGList>(head->data.ptr_value);
351727 auto exclude_column = TransformQualifiedColumnName(*exclude_column_list);
351728 // qualified - add to exclude list
351729 if (result->exclude_list.find(exclude_column) != result->exclude_list.end()) {
351730 throw ParserException("Duplicate entry \"%s\" in EXCLUDE list", exclude_column.ToString());
351731 }
351732 result->exclude_list.insert(std::move(exclude_column));
351733 }
351734 }
351735 if (star.replace_list) {
351736 for (auto head = star.replace_list->head; head; head = head->next) {
351737 auto list = PGPointerCast<duckdb_libpgquery::PGList>(head->data.ptr_value);
351738 D_ASSERT(list->length == 2);
351739 auto replace_expression =
351740 TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(list->head->data.ptr_value));
351741 auto value = PGPointerCast<duckdb_libpgquery::PGValue>(list->tail->data.ptr_value);
351742 D_ASSERT(value->type == duckdb_libpgquery::T_PGString);
351743 string replace_entry = value->val.str;
351744 if (result->replace_list.find(replace_entry) != result->replace_list.end()) {
351745 throw ParserException("Duplicate entry \"%s\" in REPLACE list", replace_entry);
351746 }
351747 if (result->exclude_list.find(QualifiedColumnName(replace_entry)) != result->exclude_list.end()) {
351748 throw ParserException("Column \"%s\" cannot occur in both EXCLUDE and REPLACE list", replace_entry);
351749 }
351750 result->replace_list.insert(make_pair(std::move(replace_entry), std::move(replace_expression)));
351751 }
351752 }
351753 if (star.rename_list) {
351754 for (auto head = star.rename_list->head; head; head = head->next) {
351755 auto list = PGPointerCast<duckdb_libpgquery::PGList>(head->data.ptr_value);
351756 D_ASSERT(list->length == 2);
351757 auto rename_column_list = PGPointerCast<duckdb_libpgquery::PGList>(list->head->data.ptr_value);
351758 auto rename_column = TransformQualifiedColumnName(*rename_column_list);
351759 string new_name = char_ptr_cast(list->tail->data.ptr_value);
351760 if (result->rename_list.find(rename_column) != result->rename_list.end()) {
351761 throw ParserException("Duplicate entry \"%s\" in EXCLUDE list", rename_column.ToString());
351762 }
351763 if (result->exclude_list.find(rename_column) != result->exclude_list.end()) {
351764 throw ParserException("Column \"%s\" cannot occur in both EXCLUDE and RENAME list",
351765 rename_column.ToString());
351766 }
351767 if (result->replace_list.find(rename_column.column) != result->replace_list.end()) {
351768 throw ParserException("Column \"%s\" cannot occur in both REPLACE and RENAME list",
351769 rename_column.ToString());
351770 }
351771 result->rename_list.insert(make_pair(std::move(rename_column), std::move(new_name)));
351772 }
351773 }
351774 if (star.expr) {
351775 D_ASSERT(star.columns);
351776 D_ASSERT(result->relation_name.empty());
351777 D_ASSERT(result->exclude_list.empty());
351778 D_ASSERT(result->replace_list.empty());
351779 result->expr = TransformExpression(star.expr);
351780 if (StarExpression::IsStar(*result->expr)) {
351781 auto &child_star = result->expr->Cast<StarExpression>();
351782 result->relation_name = child_star.relation_name;
351783 result->exclude_list = std::move(child_star.exclude_list);
351784 result->replace_list = std::move(child_star.replace_list);
351785 result->rename_list = std::move(child_star.rename_list);
351786 result->expr.reset();
351787 } else if (result->expr->GetExpressionType() == ExpressionType::LAMBDA) {
351788 vector<unique_ptr<ParsedExpression>> children;
351789 children.push_back(make_uniq<StarExpression>());
351790 children.push_back(std::move(result->expr));
351791 auto list_filter = make_uniq<FunctionExpression>("list_filter", std::move(children));
351792 result->expr = std::move(list_filter);
351793 }
351794 }
351795 result->columns = star.columns;
351796 SetQueryLocation(*result, star.location);
351797 return std::move(result);
351798}

◆ TransformBooleanTest()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformBooleanTest ( duckdb_libpgquery::PGBooleanTest & node)
private
351565 {
351566 auto argument = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(node.arg));
351567
351568 switch (node.booltesttype) {
351569 case duckdb_libpgquery::PGBoolTestType::PG_IS_TRUE:
351570 return TransformBooleanTestInternal(std::move(argument), ExpressionType::COMPARE_NOT_DISTINCT_FROM, true,
351571 node.location);
351572 case duckdb_libpgquery::PGBoolTestType::IS_NOT_TRUE:
351573 return TransformBooleanTestInternal(std::move(argument), ExpressionType::COMPARE_DISTINCT_FROM, true,
351574 node.location);
351575 case duckdb_libpgquery::PGBoolTestType::IS_FALSE:
351576 return TransformBooleanTestInternal(std::move(argument), ExpressionType::COMPARE_NOT_DISTINCT_FROM, false,
351577 node.location);
351578 case duckdb_libpgquery::PGBoolTestType::IS_NOT_FALSE:
351579 return TransformBooleanTestInternal(std::move(argument), ExpressionType::COMPARE_DISTINCT_FROM, false,
351580 node.location);
351581 case duckdb_libpgquery::PGBoolTestType::IS_UNKNOWN: // IS NULL
351582 return TransformBooleanTestIsNull(std::move(argument), ExpressionType::OPERATOR_IS_NULL, node.location);
351583 case duckdb_libpgquery::PGBoolTestType::IS_NOT_UNKNOWN: // IS NOT NULL
351584 return TransformBooleanTestIsNull(std::move(argument), ExpressionType::OPERATOR_IS_NOT_NULL, node.location);
351585 default:
351586 throw NotImplementedException("Unknown boolean test type %d", node.booltesttype);
351587 }
351588}

◆ TransformConstant()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformConstant ( duckdb_libpgquery::PGAConst & c)
private

Transform a Postgres constant value into an Expression.

351916 {
351917 auto constant = TransformValue(c.val);
351918 SetQueryLocation(*constant, c.location);
351919 return std::move(constant);
351920}
Here is the call graph for this function:

◆ TransformGroupingFunction()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformGroupingFunction ( duckdb_libpgquery::PGGroupingFunc & n)
private
352482 {
352483 auto op = make_uniq<OperatorExpression>(ExpressionType::GROUPING_FUNCTION);
352484 for (auto node = grouping.args->head; node; node = node->next) {
352485 auto n = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
352486 op->children.push_back(TransformExpression(n));
352487 }
352488 SetQueryLocation(*op, grouping.location);
352489 return std::move(op);
352490}

◆ TransformResTarget()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformResTarget ( duckdb_libpgquery::PGResTarget & root)
private
351930 {
351931 auto expr = TransformExpression(root.val);
351932 if (!expr) {
351933 return nullptr;
351934 }
351935 if (root.name) {
351936 expr->SetAlias(root.name);
351937 }
351938 return expr;
351939}

◆ TransformNullTest()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformNullTest ( duckdb_libpgquery::PGNullTest & root)
private
352659 {
352660 auto arg = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(root.arg));
352661 if (root.argisrow) {
352662 throw NotImplementedException("IS NULL argisrow");
352663 }
352664 ExpressionType expr_type = (root.nulltesttype == duckdb_libpgquery::PG_IS_NULL)
352665 ? ExpressionType::OPERATOR_IS_NULL
352666 : ExpressionType::OPERATOR_IS_NOT_NULL;
352667
352668 auto result = make_uniq<OperatorExpression>(expr_type, std::move(arg));
352669 SetQueryLocation(*result, root.location);
352670 return std::move(result);
352671}
internal::named_arg< T, Char > arg(const S &name, const T &arg)
Definition duckdb.cpp:16242

◆ TransformParamRef()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformParamRef ( duckdb_libpgquery::PGParamRef & node)
private
353016 {
353017 auto expr = make_uniq<ParameterExpression>();
353018
353019 auto param = GetParameterIdentifier(node);
353020 idx_t known_param_index = DConstants::INVALID_INDEX;
353021 // This is a named parameter, try to find an entry for it
353022 GetParam(param.identifier, known_param_index, param.type);
353023
353024 if (known_param_index == DConstants::INVALID_INDEX) {
353025 // We have not seen this parameter before
353026 if (node.number != 0) {
353027 // Preserve the parameter number
353028 known_param_index = NumericCast<idx_t>(node.number);
353029 } else {
353030 known_param_index = ParamCount() + 1;
353031 if (!node.name) {
353032 param.identifier = StringUtil::Format("%d", known_param_index);
353033 }
353034 }
353035
353036 if (!named_param_map.count(param.identifier)) {
353037 // Add it to the named parameter map so we can find it next time it's referenced
353038 SetParam(param.identifier, known_param_index, param.type);
353039 }
353040 }
353041
353042 expr->identifier = param.identifier;
353043 idx_t new_param_count = MaxValue<idx_t>(ParamCount(), known_param_index);
353044 SetParamCount(new_param_count);
353045 return std::move(expr);
353046}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ TransformNamedArg()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformNamedArg ( duckdb_libpgquery::PGNamedArgExpr & root)
private
351941 {
351942 auto expr = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(root.arg));
351943 if (root.name) {
351944 expr->SetAlias(root.name);
351945 }
351946 return expr;
351947}

◆ TransformMultiAssignRef()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformMultiAssignRef ( duckdb_libpgquery::PGMultiAssignRef & root)
private

Transform multi assignment reference into an Expression.

352712 {
352713 // Early-out, if the root is not a function call.
352714 if (root.source->type != duckdb_libpgquery::T_PGFuncCall) {
352715 return TransformExpression(root.source);
352716 }
352717
352718 auto func = PGCast<duckdb_libpgquery::PGFuncCall>(*root.source);
352719
352720 // Only allow ROW function.
352721 auto function = PGPointerCast<duckdb_libpgquery::PGValue>(func.funcname->tail->data.ptr_value);
352722 char const *function_name = function->val.str;
352723 if (!function_name || !StringUtil::CIEquals(function_name, "row")) {
352724 return TransformExpression(root.source);
352725 }
352726
352727 // Too many columns, e.g., (x, y) != (1, 2, 3).
352728 int64_t value_count = func.args ? func.args->length : 0;
352729 if (int64_t(root.ncolumns) < value_count || !func.args) {
352730 throw ParserException("Could not perform assignment, expected %d values, got %d", root.ncolumns, value_count);
352731 }
352732
352733 // Get the expression corresponding with the current column.
352734 int64_t idx = 1;
352735 auto list = func.args->head;
352736 while (list && idx < int64_t(root.colno)) {
352737 list = list->next;
352738 ++idx;
352739 }
352740
352741 // Not enough columns, e.g., (x, y, z) != (1, 2).
352742 if (!list) {
352743 throw ParserException("Could not perform assignment, expected %d values, got %d", root.ncolumns,
352744 func.args->length);
352745 }
352746
352747 auto node = PGPointerCast<duckdb_libpgquery::PGNode>(list->data.ptr_value);
352748 return TransformExpression(node);
352749}
static DUCKDB_API bool CIEquals(const string &l1, const string &l2)
Case insensitive equals.
Definition duckdb.cpp:109696
Here is the call graph for this function:

◆ TransformSQLValueFunction()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformSQLValueFunction ( duckdb_libpgquery::PGSQLValueFunction & node)
private
352472 {
352473 throw InternalException("SQL value functions should not be emitted by the parser");
352474}

◆ TransformSubquery()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformSubquery ( duckdb_libpgquery::PGSubLink & root)
private
353085 {
353086 auto subquery_expr = make_uniq<SubqueryExpression>();
353087 SetQueryLocation(*subquery_expr, root.location);
353088
353089 switch (root.subLinkType) {
353090 case duckdb_libpgquery::PG_EXISTS_SUBLINK: {
353091 subquery_expr->subquery_type = SubqueryType::EXISTS;
353092 subquery_expr->subquery = TransformSelectStmt(*root.subselect);
353093 D_ASSERT(subquery_expr->subquery);
353094 break;
353095 }
353096 case duckdb_libpgquery::PG_ANY_SUBLINK:
353097 case duckdb_libpgquery::PG_ALL_SUBLINK: {
353098 // comparison with ANY() or ALL()
353099 subquery_expr->subquery_type = SubqueryType::ANY;
353100 // transform the test expression first to preserve positional parameter order
353101 subquery_expr->child = TransformExpression(root.testexpr);
353102 // get the operator name
353103 if (!root.operName) {
353104 // simple IN
353105 subquery_expr->comparison_type = ExpressionType::COMPARE_EQUAL;
353106 } else {
353107 auto operator_name =
353108 string((PGPointerCast<duckdb_libpgquery::PGValue>(root.operName->head->data.ptr_value))->val.str);
353109 subquery_expr->comparison_type = OperatorToExpressionType(operator_name);
353110 }
353111 if (subquery_expr->comparison_type != ExpressionType::COMPARE_EQUAL &&
353112 subquery_expr->comparison_type != ExpressionType::COMPARE_NOTEQUAL &&
353113 subquery_expr->comparison_type != ExpressionType::COMPARE_GREATERTHAN &&
353114 subquery_expr->comparison_type != ExpressionType::COMPARE_GREATERTHANOREQUALTO &&
353115 subquery_expr->comparison_type != ExpressionType::COMPARE_LESSTHAN &&
353116 subquery_expr->comparison_type != ExpressionType::COMPARE_LESSTHANOREQUALTO) {
353117 throw ParserException("ANY and ALL operators require one of =,<>,>,<,>=,<= comparisons!");
353118 }
353119 subquery_expr->subquery = TransformSelectStmt(*root.subselect);
353120 D_ASSERT(subquery_expr->subquery);
353121 if (root.subLinkType == duckdb_libpgquery::PG_ALL_SUBLINK) {
353122 // ALL sublink is equivalent to NOT(ANY) with inverted comparison
353123 // e.g. [= ALL()] is equivalent to [NOT(<> ANY())]
353124 // first invert the comparison type
353125 subquery_expr->comparison_type = NegateComparisonExpression(subquery_expr->comparison_type);
353126 return make_uniq<OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(subquery_expr));
353127 }
353128 break;
353129 }
353130 case duckdb_libpgquery::PG_EXPR_SUBLINK: {
353131 // return a single scalar value from the subquery
353132 // no child expression to compare to
353133 subquery_expr->subquery_type = SubqueryType::SCALAR;
353134 subquery_expr->subquery = TransformSelectStmt(*root.subselect);
353135 D_ASSERT(subquery_expr->subquery);
353136 break;
353137 }
353138 case duckdb_libpgquery::PG_ARRAY_SUBLINK: {
353139 // ARRAY expression
353140 // wrap subquery into
353141 // "SELECT CASE WHEN ARRAY_AGG(col) IS NULL THEN [] ELSE ARRAY_AGG(col) END FROM (...) tbl"
353142 auto select_node = make_uniq<SelectNode>();
353143
353144 subquery_expr->subquery = TransformSelectStmt(*root.subselect);
353145 D_ASSERT(subquery_expr->subquery);
353146
353147 unique_ptr<ParsedExpression> array_agg_child;
353148 optional_ptr<SelectNode> sub_select;
353149 if (subquery_expr->subquery->node->type == QueryNodeType::SELECT_NODE) {
353150 // easy case - subquery is a SELECT
353151 sub_select = subquery_expr->subquery->node->Cast<SelectNode>();
353152 if (sub_select->select_list.size() != 1) {
353153 throw BinderException(*subquery_expr, "Subquery returns %zu columns - expected 1",
353154 sub_select->select_list.size());
353155 }
353156 array_agg_child = make_uniq<PositionalReferenceExpression>(1ULL);
353157 } else {
353158 // subquery is not a SELECT but a UNION or CTE
353159 // we can still support this but it is more challenging since we can't push columns for the ORDER BY
353160 auto columns_star = make_uniq<StarExpression>();
353161 columns_star->columns = true;
353162 array_agg_child = std::move(columns_star);
353163 }
353164
353165 // ARRAY_AGG(COLUMNS(*))
353166 vector<unique_ptr<ParsedExpression>> children;
353167 children.push_back(std::move(array_agg_child));
353168 auto aggr = make_uniq<FunctionExpression>("array_agg", std::move(children));
353169 // push ORDER BY modifiers into the array_agg
353170 for (auto &modifier : subquery_expr->subquery->node->modifiers) {
353171 if (modifier->type == ResultModifierType::ORDER_MODIFIER) {
353172 aggr->order_bys = unique_ptr_cast<ResultModifier, OrderModifier>(modifier->Copy());
353173 break;
353174 }
353175 }
353176 // transform constants (e.g. ORDER BY 1) into positional references (ORDER BY #1)
353177 idx_t array_idx = 0;
353178 if (aggr->order_bys) {
353179 for (auto &order : aggr->order_bys->orders) {
353180 if (order.expression->GetExpressionType() == ExpressionType::VALUE_CONSTANT) {
353181 auto &constant_expr = order.expression->Cast<ConstantExpression>();
353182 Value bigint_value;
353183 string error;
353184 if (constant_expr.value.DefaultTryCastAs(LogicalType::BIGINT, bigint_value, &error)) {
353185 int64_t order_index = BigIntValue::Get(bigint_value);
353186 idx_t positional_index = order_index < 0 ? NumericLimits<idx_t>::Maximum() : idx_t(order_index);
353187 order.expression = make_uniq<PositionalReferenceExpression>(positional_index);
353188 }
353189 } else if (sub_select) {
353190 // if we have a SELECT we can push the ORDER BY clause into the SELECT list and reference it
353191 auto alias = "__array_internal_idx_" + to_string(++array_idx);
353192 order.expression->alias = alias;
353193 sub_select->select_list.push_back(std::move(order.expression));
353194 order.expression = make_uniq<ColumnRefExpression>(alias);
353195 } else {
353196 // otherwise we remove order qualifications
353197 RemoveOrderQualificationRecursive(order.expression);
353198 }
353199 }
353200 }
353201 // ARRAY_AGG(COLUMNS(*)) IS NULL
353202 auto agg_is_null = make_uniq<OperatorExpression>(ExpressionType::OPERATOR_IS_NULL, aggr->Copy());
353203 // empty list
353204 vector<unique_ptr<ParsedExpression>> list_children;
353205 auto empty_list = make_uniq<FunctionExpression>("list_value", std::move(list_children));
353206 // CASE
353207 auto case_expr = make_uniq<CaseExpression>();
353208 CaseCheck check;
353209 check.when_expr = std::move(agg_is_null);
353210 check.then_expr = std::move(empty_list);
353211 case_expr->case_checks.push_back(std::move(check));
353212 case_expr->else_expr = std::move(aggr);
353213
353214 select_node->select_list.push_back(std::move(case_expr));
353215
353216 // FROM (...) tbl
353217 auto child_subquery = make_uniq<SubqueryRef>(std::move(subquery_expr->subquery));
353218 select_node->from_table = std::move(child_subquery);
353219
353220 auto new_subquery = make_uniq<SelectStatement>();
353221 new_subquery->node = std::move(select_node);
353222 subquery_expr->subquery = std::move(new_subquery);
353223
353224 subquery_expr->subquery_type = SubqueryType::SCALAR;
353225 break;
353226 }
353227 default:
353228 throw NotImplementedException("Subquery of type %d not implemented\n", (int)root.subLinkType);
353229 }
353230 return std::move(subquery_expr);
353231}
std::string to_string(const T &value)
Definition duckdb.cpp:19425

◆ TransformConstraint() [1/2]

unique_ptr< Constraint > duckdb::Transformer::TransformConstraint ( duckdb_libpgquery::PGConstraint & constraint)
private
351324 {
351325 switch (constraint.contype) {
351326 case duckdb_libpgquery::PG_CONSTR_UNIQUE:
351327 case duckdb_libpgquery::PG_CONSTR_PRIMARY: {
351328 bool is_primary_key = constraint.contype == duckdb_libpgquery::PG_CONSTR_PRIMARY;
351329 if (!constraint.keys) {
351330 throw ParserException("UNIQUE USING INDEX is not supported");
351331 }
351332 vector<string> columns;
351333 for (auto kc = constraint.keys->head; kc; kc = kc->next) {
351334 auto value = PGPointerCast<duckdb_libpgquery::PGValue>(kc->data.ptr_value);
351335 columns.emplace_back(value->val.str);
351336 }
351337 return make_uniq<UniqueConstraint>(columns, is_primary_key);
351338 }
351339 case duckdb_libpgquery::PG_CONSTR_CHECK: {
351340 auto expression = TransformExpression(constraint.raw_expr);
351341 if (expression->HasSubquery()) {
351342 throw ParserException("subqueries prohibited in CHECK constraints");
351343 }
351344 return make_uniq<CheckConstraint>(std::move(expression));
351345 }
351346 case duckdb_libpgquery::PG_CONSTR_FOREIGN:
351347 return TransformForeignKeyConstraint(constraint);
351348 default:
351349 throw NotImplementedException("Constraint type not handled yet!");
351350 }
351351}

◆ TransformConstraint() [2/2]

unique_ptr< Constraint > duckdb::Transformer::TransformConstraint ( duckdb_libpgquery::PGConstraint & constraint,
ColumnDefinition & column,
idx_t index )
private
351354 {
351355 switch (constraint.contype) {
351356 case duckdb_libpgquery::PG_CONSTR_NOTNULL:
351357 return make_uniq<NotNullConstraint>(LogicalIndex(index));
351358 case duckdb_libpgquery::PG_CONSTR_CHECK:
351359 return TransformConstraint(constraint);
351360 case duckdb_libpgquery::PG_CONSTR_PRIMARY:
351361 return make_uniq<UniqueConstraint>(LogicalIndex(index), column.GetName(), true);
351362 case duckdb_libpgquery::PG_CONSTR_UNIQUE:
351363 return make_uniq<UniqueConstraint>(LogicalIndex(index), column.GetName(), false);
351364 case duckdb_libpgquery::PG_CONSTR_NULL:
351365 return nullptr;
351366 case duckdb_libpgquery::PG_CONSTR_GENERATED_VIRTUAL: {
351367 if (column.HasDefaultValue()) {
351368 throw InvalidInputException("\"%s\" has a DEFAULT value set, it can not become a GENERATED column",
351369 column.Name());
351370 }
351371 column.SetGeneratedExpression(TransformExpression(constraint.raw_expr));
351372 return nullptr;
351373 }
351374 case duckdb_libpgquery::PG_CONSTR_GENERATED_STORED:
351375 throw InvalidInputException("Can not create a STORED generated column!");
351376 case duckdb_libpgquery::PG_CONSTR_DEFAULT:
351377 column.SetDefaultValue(TransformExpression(constraint.raw_expr));
351378 return nullptr;
351379 case duckdb_libpgquery::PG_CONSTR_COMPRESSION: {
351380 auto compression_type = EnumUtil::FromString<CompressionType>(constraint.compression_name);
351381 switch (compression_type) {
351382 case CompressionType::COMPRESSION_AUTO:
351383 case CompressionType::COMPRESSION_CONSTANT:
351384 case CompressionType::COMPRESSION_EMPTY:
351385 throw InvalidInputException("Compression method %d cannot be forced", constraint.compression_name);
351386 default:
351387 break;
351388 }
351389 column.SetCompressionType(compression_type);
351390 return nullptr;
351391 }
351392 case duckdb_libpgquery::PG_CONSTR_FOREIGN:
351393 return TransformForeignKeyConstraint(constraint, &column.Name());
351394 default:
351395 throw NotImplementedException("Constraint not implemented!");
351396 }
351397}

◆ TransformUpdateSetInfo()

unique_ptr< UpdateSetInfo > duckdb::Transformer::TransformUpdateSetInfo ( duckdb_libpgquery::PGList * target_list,
duckdb_libpgquery::PGNode * where_clause )
private
357626 {
357627 auto result = make_uniq<UpdateSetInfo>();
357628 auto root = target_list;
357629
357630 for (auto cell = root->head; cell != nullptr; cell = cell->next) {
357631 auto target = PGPointerCast<duckdb_libpgquery::PGResTarget>(cell->data.ptr_value);
357632 if (target->indirection) {
357633 throw ParserException("Qualified column names in UPDATE .. SET not supported");
357634 }
357635 result->columns.emplace_back(target->name);
357636 result->expressions.push_back(TransformExpression(target->val));
357637 }
357638
357639 result->condition = TransformExpression(where_clause);
357640 return result;
357641}

◆ TransformIndexParameters()

vector< unique_ptr< ParsedExpression > > duckdb::Transformer::TransformIndexParameters ( duckdb_libpgquery::PGList & list,
const string & relation_name )
private
355485 {
355486 vector<unique_ptr<ParsedExpression>> expressions;
355487 for (auto cell = list.head; cell != nullptr; cell = cell->next) {
355488 auto index_element = PGPointerCast<duckdb_libpgquery::PGIndexElem>(cell->data.ptr_value);
355489 if (index_element->collation) {
355490 throw NotImplementedException("Index with collation not supported yet!");
355491 }
355492 if (index_element->opclass) {
355493 throw NotImplementedException("Index with opclass not supported yet!");
355494 }
355495
355496 if (index_element->name) {
355497 // create a column reference expression
355498 expressions.push_back(make_uniq<ColumnRefExpression>(index_element->name, relation_name));
355499 } else {
355500 // parse the index expression
355501 D_ASSERT(index_element->expr);
355502 expressions.push_back(TransformExpression(index_element->expr));
355503 }
355504 }
355505 return expressions;
355506}

◆ TransformCollateExpr()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformCollateExpr ( duckdb_libpgquery::PGCollateClause & collate)
private
355775 {
355776 auto child = TransformExpression(collate.arg);
355777 auto collation = TransformCollation(&collate);
355778 return make_uniq<CollateExpression>(collation, std::move(child));
355779}

◆ TransformCollation()

string duckdb::Transformer::TransformCollation ( optional_ptr< duckdb_libpgquery::PGCollateClause > collate)
private
355742 {
355743 if (!collate) {
355744 return string();
355745 }
355746 string collation;
355747 for (auto c = collate->collname->head; c != nullptr; c = lnext(c)) {
355748 auto pgvalue = PGPointerCast<duckdb_libpgquery::PGValue>(c->data.ptr_value);
355749 if (pgvalue->type != duckdb_libpgquery::T_PGString) {
355750 throw ParserException("Expected a string as collation type!");
355751 }
355752 auto collation_argument = string(pgvalue->val.str);
355753 if (collation.empty()) {
355754 collation = collation_argument;
355755 } else {
355756 collation += "." + collation_argument;
355757 }
355758 }
355759 return collation;
355760}

◆ TransformColumnDefinition()

ColumnDefinition duckdb::Transformer::TransformColumnDefinition ( duckdb_libpgquery::PGColumnDef & cdef)
private
355781 {
355782 string name;
355783 if (cdef.colname) {
355784 name = cdef.colname;
355785 }
355786
355787 auto optional_type = cdef.category == duckdb_libpgquery::COL_GENERATED;
355788 LogicalType target_type;
355789 if (optional_type && !cdef.typeName) {
355790 target_type = LogicalType::ANY;
355791 } else if (!cdef.typeName) {
355792 // ALTER TABLE tbl ALTER TYPE USING ...
355793 target_type = LogicalType::UNKNOWN;
355794 } else if (cdef.collClause) {
355795 if (cdef.category == duckdb_libpgquery::COL_GENERATED) {
355796 throw ParserException("Collations are not supported on generated columns");
355797 }
355798
355799 auto typename_expr = TransformTypeExpressionInternal(*cdef.typeName);
355800 if (typename_expr->type != ExpressionType::TYPE) {
355801 throw ParserException("Only type names can have collations!");
355802 }
355803
355804 auto &type_expr = typename_expr->Cast<TypeExpression>();
355805 if (!StringUtil::CIEquals(type_expr.GetTypeName(), "VARCHAR")) {
355806 throw ParserException("Only VARCHAR columns can have collations!");
355807 }
355808
355809 // Push back collation as a parameter of the type expression
355810 auto collation = TransformCollation(cdef.collClause);
355811 auto collation_expr = make_uniq<ConstantExpression>(Value(collation));
355812 collation_expr->SetAlias("collation");
355813 type_expr.GetChildren().push_back(std::move(collation_expr));
355814
355815 target_type = LogicalType::UNBOUND(std::move(typename_expr));
355816 } else {
355817 target_type = TransformTypeName(*cdef.typeName);
355818 }
355819
355820 return ColumnDefinition(name, target_type);
355821}
unique_ptr< ParsedExpression > TransformTypeExpressionInternal(duckdb_libpgquery::PGTypeName &name)
Transform a Postgres TypeName string into a LogicalType (non-LIST types).
Definition duckdb.cpp:354570

◆ TransformOnConflict()

OnCreateConflict duckdb::Transformer::TransformOnConflict ( duckdb_libpgquery::PGOnCreateConflict conflict)
private
355762 {
355763 switch (conflict) {
355764 case duckdb_libpgquery::PG_ERROR_ON_CONFLICT:
355765 return OnCreateConflict::ERROR_ON_CONFLICT;
355766 case duckdb_libpgquery::PG_IGNORE_ON_CONFLICT:
355767 return OnCreateConflict::IGNORE_ON_CONFLICT;
355768 case duckdb_libpgquery::PG_REPLACE_ON_CONFLICT:
355769 return OnCreateConflict::REPLACE_ON_CONFLICT;
355770 default:
355771 throw InternalException("Unrecognized OnConflict type");
355772 }
355773}

◆ TransformAlias()

string duckdb::Transformer::TransformAlias ( duckdb_libpgquery::PGAlias * root,
vector< string > & column_name_alias )
private
354078 {
354079 if (!root) {
354080 return "";
354081 }
354082 column_name_alias = TransformStringList(root->colnames);
354083 return root->aliasname;
354084}

◆ TransformStringList()

vector< string > duckdb::Transformer::TransformStringList ( duckdb_libpgquery::PGList * list)
private
354066 {
354067 vector<string> result;
354068 if (!list) {
354069 return result;
354070 }
354071 for (auto node = list->head; node != nullptr; node = node->next) {
354072 auto value = PGPointerCast<duckdb_libpgquery::PGValue>(node->data.ptr_value);
354073 result.emplace_back(value->val.str);
354074 }
354075 return result;
354076}

◆ TransformCTE()

void duckdb::Transformer::TransformCTE ( duckdb_libpgquery::PGWithClause & de_with_clause,
CommonTableExpressionMap & cte_map )
private
354136 {
354137 stored_cte_map.push_back(cte_map);
354138
354139 // TODO: might need to update in case of future lawsuit
354140 D_ASSERT(de_with_clause.ctes);
354141 for (auto cte_ele = de_with_clause.ctes->head; cte_ele != nullptr; cte_ele = cte_ele->next) {
354142 auto info = make_uniq<CommonTableExpressionInfo>();
354143
354144 auto &cte = *PGPointerCast<duckdb_libpgquery::PGCommonTableExpr>(cte_ele->data.ptr_value);
354145 if (cte.recursive_keys) {
354146 auto key_target = PGPointerCast<duckdb_libpgquery::PGNode>(cte.recursive_keys->head->data.ptr_value);
354147 if (key_target) {
354148 TransformExpressionList(*cte.recursive_keys, info->key_targets);
354149 }
354150 }
354151
354152 if (cte.aliascolnames) {
354153 for (auto node = cte.aliascolnames->head; node != nullptr; node = node->next) {
354154 auto value = PGPointerCast<duckdb_libpgquery::PGValue>(node->data.ptr_value);
354155 info->aliases.emplace_back(value->val.str);
354156 }
354157 }
354158 // lets throw some errors on unsupported features early
354159 if (cte.ctecolnames) {
354160 throw NotImplementedException("Column name setting not supported in CTEs");
354161 }
354162 if (cte.ctecoltypes) {
354163 throw NotImplementedException("Column type setting not supported in CTEs");
354164 }
354165 if (cte.ctecoltypmods) {
354166 throw NotImplementedException("Column type modification not supported in CTEs");
354167 }
354168 if (cte.ctecolcollations) {
354169 throw NotImplementedException("CTE collations not supported");
354170 }
354171 // we need a query
354172 if (!cte.ctequery || cte.ctequery->type != duckdb_libpgquery::T_PGSelectStmt) {
354173 throw ParserException("A CTE needs a SELECT");
354174 }
354175
354176 // CTE transformation can either result in inlining for non recursive CTEs, or in recursive CTE bindings
354177 // otherwise.
354178 if (cte.cterecursive || de_with_clause.recursive) {
354179 info->query = TransformRecursiveCTE(cte, *info);
354180 } else {
354181 Transformer cte_transformer(*this);
354182 info->query = cte_transformer.TransformSelectStmt(*cte.ctequery);
354183 }
354184 D_ASSERT(info->query);
354185 auto cte_name = string(cte.ctename);
354186
354187 auto it = cte_map.map.find(cte_name);
354188 if (it != cte_map.map.end()) {
354189 // can't have two CTEs with same name
354190 throw ParserException("Duplicate CTE name \"%s\"", cte_name);
354191 }
354192
354193 if (cte.ctematerialized == duckdb_libpgquery::PGCTEMaterializeDefault) {
354194#ifdef DUCKDB_ALTERNATIVE_VERIFY
354195 info->materialized = CTEMaterialize::CTE_MATERIALIZE_ALWAYS;
354196#else
354197 info->materialized = CTEMaterialize::CTE_MATERIALIZE_DEFAULT;
354198#endif
354199 } else if (cte.ctematerialized == duckdb_libpgquery::PGCTEMaterializeAlways) {
354200 info->materialized = CTEMaterialize::CTE_MATERIALIZE_ALWAYS;
354201 } else if (cte.ctematerialized == duckdb_libpgquery::PGCTEMaterializeNever) {
354202 info->materialized = CTEMaterialize::CTE_MATERIALIZE_NEVER;
354203 }
354204
354205 cte_map.map[cte_name] = std::move(info);
354206 }
354207}

◆ TransformRecursiveCTE()

unique_ptr< SelectStatement > duckdb::Transformer::TransformRecursiveCTE ( duckdb_libpgquery::PGCommonTableExpr & node,
CommonTableExpressionInfo & info )
private
354210 {
354211 auto &stmt = *PGPointerCast<duckdb_libpgquery::PGSelectStmt>(cte.ctequery);
354212
354213 unique_ptr<SelectStatement> select;
354214 switch (stmt.op) {
354215 case duckdb_libpgquery::PG_SETOP_UNION: {
354216 select = make_uniq<SelectStatement>();
354217 select->node = make_uniq_base<QueryNode, RecursiveCTENode>();
354218 auto &result = select->node->Cast<RecursiveCTENode>();
354219 result.ctename = string(cte.ctename);
354220 result.union_all = stmt.all;
354221 if (stmt.withClause) {
354222 auto with_clause = PGPointerCast<duckdb_libpgquery::PGWithClause>(stmt.withClause);
354223 TransformCTE(*with_clause, result.cte_map);
354224 }
354225 result.left = TransformSelectNode(*stmt.larg);
354226 result.right = TransformSelectNode(*stmt.rarg);
354227 result.aliases = info.aliases;
354228 for (auto &key : info.key_targets) {
354229 result.key_targets.emplace_back(key->Copy());
354230 }
354231 break;
354232 }
354233 case duckdb_libpgquery::PG_SETOP_EXCEPT:
354234 case duckdb_libpgquery::PG_SETOP_INTERSECT:
354235 default: {
354236 // This CTE is not recursive. Fallback to regular query transformation.
354237 auto node = TransformSelectNode(*cte.ctequery);
354238 auto result = make_uniq<SelectStatement>();
354239 result->node = std::move(node);
354240 return result;
354241 }
354242 }
354243
354244 if (stmt.limitCount || stmt.limitOffset) {
354245 throw ParserException("LIMIT or OFFSET in a recursive query is not allowed");
354246 }
354247 if (stmt.sortClause) {
354248 throw ParserException("ORDER BY in a recursive query is not allowed");
354249 }
354250 return select;
354251}

◆ TransformUnaryOperator()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformUnaryOperator ( const string & op,
unique_ptr< ParsedExpression > child )
private
352769 {
352770 vector<unique_ptr<ParsedExpression>> children;
352771 children.push_back(std::move(child));
352772
352773 // built-in operator function
352774 auto result = make_uniq<FunctionExpression>(op, std::move(children));
352775 result->is_operator = true;
352776 return std::move(result);
352777}

◆ TransformBinaryOperator()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformBinaryOperator ( string op,
unique_ptr< ParsedExpression > left,
unique_ptr< ParsedExpression > right )
private
352780 {
352781 vector<unique_ptr<ParsedExpression>> children;
352782 children.push_back(std::move(left));
352783 children.push_back(std::move(right));
352784
352785 if (options.integer_division && op == "/") {
352786 op = "//";
352787 }
352788 if (op == "~" || op == "!~") {
352789 // rewrite 'asdf' SIMILAR TO '.*sd.*' into regexp_full_match('asdf', '.*sd.*')
352790 bool invert_similar = op == "!~";
352791
352792 auto result = make_uniq<FunctionExpression>("regexp_full_match", std::move(children));
352793 if (invert_similar) {
352794 return make_uniq<OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(result));
352795 } else {
352796 return std::move(result);
352797 }
352798 } else {
352799 auto target_type = OperatorToExpressionType(op);
352800 if (target_type != ExpressionType::INVALID) {
352801 // built-in comparison operator
352802 return make_uniq<ComparisonExpression>(target_type, std::move(children[0]), std::move(children[1]));
352803 }
352804 // not a special operator: convert to a function expression
352805 auto result = make_uniq<FunctionExpression>(std::move(op), std::move(children));
352806 result->is_operator = true;
352807 return std::move(result);
352808 }
352809}

◆ TransformTableRefNode()

unique_ptr< TableRef > duckdb::Transformer::TransformTableRefNode ( duckdb_libpgquery::PGNode & n)
private

Transform a Postgres node into a TableRef.

358211 {
358212 auto stack_checker = StackCheck();
358213
358214 switch (n.type) {
358215 case duckdb_libpgquery::T_PGRangeVar:
358216 return TransformRangeVar(PGCast<duckdb_libpgquery::PGRangeVar>(n));
358217 case duckdb_libpgquery::T_PGJoinExpr:
358218 return TransformJoin(PGCast<duckdb_libpgquery::PGJoinExpr>(n));
358219 case duckdb_libpgquery::T_PGRangeSubselect:
358220 return TransformRangeSubselect(PGCast<duckdb_libpgquery::PGRangeSubselect>(n));
358221 case duckdb_libpgquery::T_PGRangeFunction:
358222 return TransformRangeFunction(PGCast<duckdb_libpgquery::PGRangeFunction>(n));
358223 case duckdb_libpgquery::T_PGPivotExpr:
358224 return TransformPivot(PGCast<duckdb_libpgquery::PGPivotExpr>(n));
358225 default:
358226 throw NotImplementedException("From Type %d not supported", n.type);
358227 }
358228}
unique_ptr< TableRef > TransformRangeSubselect(duckdb_libpgquery::PGRangeSubselect &root)
Transform a table producing subquery into a TableRef.
Definition duckdb.cpp:358142
unique_ptr< TableRef > TransformPivot(duckdb_libpgquery::PGPivotExpr &root)
Transform a Postgres pivot node into a TableRef.
Definition duckdb.cpp:358104
unique_ptr< TableRef > TransformJoin(duckdb_libpgquery::PGJoinExpr &root)
Transform a Postgres join node into a TableRef.
Definition duckdb.cpp:357965
unique_ptr< TableRef > TransformRangeFunction(duckdb_libpgquery::PGRangeFunction &root)
Transform a Postgres table-producing function into a TableRef.
Definition duckdb.cpp:358163
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformFrom()

unique_ptr< TableRef > duckdb::Transformer::TransformFrom ( optional_ptr< duckdb_libpgquery::PGList > root)
private

Transform a Postgres FROM clause into a TableRef.

357907 {
357908 if (!root) {
357909 return make_uniq<EmptyTableRef>();
357910 }
357911
357912 if (root->length > 1) {
357913 // Implicit Cross Product
357914 auto result = make_uniq<JoinRef>(JoinRefType::CROSS);
357915 result->is_implicit = true;
357916 JoinRef *cur_root = result.get();
357917 idx_t list_size = 0;
357918 for (auto node = root->head; node != nullptr; node = node->next) {
357919 auto n = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
357920 unique_ptr<TableRef> next = TransformTableRefNode(*n);
357921 if (!cur_root->left) {
357922 cur_root->left = std::move(next);
357923 } else if (!cur_root->right) {
357924 cur_root->right = std::move(next);
357925 } else {
357926 auto old_res = std::move(result);
357927 result = make_uniq<JoinRef>(JoinRefType::CROSS);
357928 result->is_implicit = true;
357929 result->left = std::move(old_res);
357930 result->right = std::move(next);
357931 cur_root = result.get();
357932 }
357933 list_size++;
357934 StackCheck(list_size);
357935 }
357936 return std::move(result);
357937 }
357938
357939 auto n = PGPointerCast<duckdb_libpgquery::PGNode>(root->head->data.ptr_value);
357940 return TransformTableRefNode(*n);
357941}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformRangeVar()

unique_ptr< TableRef > duckdb::Transformer::TransformRangeVar ( duckdb_libpgquery::PGRangeVar & root)
private

Transform a Postgres table reference into a TableRef.

357855 {
357856 auto result = make_uniq<BaseTableRef>();
357857
357858 result->alias = TransformAlias(root.alias, result->column_name_alias);
357859 if (root.relname) {
357860 result->table_name = root.relname;
357861 }
357862 if (root.catalogname) {
357863 result->catalog_name = root.catalogname;
357864 }
357865 if (root.schemaname) {
357866 result->schema_name = root.schemaname;
357867 }
357868 if (root.at_clause) {
357869 auto &at_clause = PGCast<duckdb_libpgquery::PGAtClause>(*root.at_clause);
357870 auto at_expr = TransformExpression(*at_clause.expr);
357871 result->at_clause = make_uniq<AtClause>(at_clause.unit, std::move(at_expr));
357872 }
357873 if (root.sample) {
357874 result->sample = TransformSampleOptions(root.sample);
357875 }
357876 SetQueryLocation(*result, root.location);
357877 return std::move(result);
357878}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformRangeFunction()

unique_ptr< TableRef > duckdb::Transformer::TransformRangeFunction ( duckdb_libpgquery::PGRangeFunction & root)
private

Transform a Postgres table-producing function into a TableRef.

358163 {
358164 if (root.is_rowsfrom) {
358165 throw NotImplementedException("ROWS FROM() not implemented");
358166 }
358167 if (root.functions->length != 1) {
358168 throw NotImplementedException("Need exactly one function");
358169 }
358170 auto function_sublist = PGPointerCast<duckdb_libpgquery::PGList>(root.functions->head->data.ptr_value);
358171 D_ASSERT(function_sublist->length == 2);
358172 auto call_tree = PGPointerCast<duckdb_libpgquery::PGNode>(function_sublist->head->data.ptr_value);
358173 auto coldef = function_sublist->head->next->data.ptr_value;
358174
358175 if (coldef) {
358176 throw NotImplementedException("Explicit column definition not supported yet");
358177 }
358178 // transform the function call
358179 auto result = make_uniq<TableFunctionRef>();
358180 if (root.ordinality) {
358181 result->with_ordinality = OrdinalityType::WITH_ORDINALITY;
358182 }
358183 switch (call_tree->type) {
358184 case duckdb_libpgquery::T_PGFuncCall: {
358185 auto func_call = PGPointerCast<duckdb_libpgquery::PGFuncCall>(call_tree.get());
358186 result->function = TransformFuncCall(*func_call);
358187 SetQueryLocation(*result, func_call->location);
358188 break;
358189 }
358190 case duckdb_libpgquery::T_PGSQLValueFunction:
358191 result->function =
358192 TransformSQLValueFunction(*PGPointerCast<duckdb_libpgquery::PGSQLValueFunction>(call_tree.get()));
358193 break;
358194 default:
358195 throw ParserException("Not a function call or value function");
358196 }
358197 result->alias = TransformAlias(root.alias, result->column_name_alias);
358198 if (root.sample) {
358199 result->sample = TransformSampleOptions(root.sample);
358200 }
358201 return std::move(result);
358202}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformJoin()

unique_ptr< TableRef > duckdb::Transformer::TransformJoin ( duckdb_libpgquery::PGJoinExpr & root)
private

Transform a Postgres join node into a TableRef.

357965 {
357966 auto result = make_uniq<JoinRef>(JoinRefType::REGULAR);
357967 switch (root.jointype) {
357968 case duckdb_libpgquery::PG_JOIN_INNER: {
357969 result->type = JoinType::INNER;
357970 break;
357971 }
357972 case duckdb_libpgquery::PG_JOIN_LEFT: {
357973 result->type = JoinType::LEFT;
357974 break;
357975 }
357976 case duckdb_libpgquery::PG_JOIN_FULL: {
357977 result->type = JoinType::OUTER;
357978 break;
357979 }
357980 case duckdb_libpgquery::PG_JOIN_RIGHT: {
357981 result->type = JoinType::RIGHT;
357982 break;
357983 }
357984 case duckdb_libpgquery::PG_JOIN_SEMI: {
357985 result->type = JoinType::SEMI;
357986 break;
357987 }
357988 case duckdb_libpgquery::PG_JOIN_ANTI: {
357989 result->type = JoinType::ANTI;
357990 break;
357991 }
357992 case duckdb_libpgquery::PG_JOIN_POSITION: {
357993 result->ref_type = JoinRefType::POSITIONAL;
357994 break;
357995 }
357996 default:
357997 throw NotImplementedException("Join type %d not supported\n", root.jointype);
357998 }
357999
358000 // Check the type of the left and right argument before transforming.
358001 result->left = TransformTableRefNode(*root.larg);
358002 result->right = TransformTableRefNode(*root.rarg);
358003
358004 switch (root.joinreftype) {
358005 case duckdb_libpgquery::PG_JOIN_NATURAL:
358006 result->ref_type = JoinRefType::NATURAL;
358007 break;
358008 case duckdb_libpgquery::PG_JOIN_ASOF:
358009 result->ref_type = JoinRefType::ASOF;
358010 break;
358011 default:
358012 break;
358013 }
358014
358015 SetQueryLocation(*result, root.location);
358016 if (root.usingClause && root.usingClause->length > 0) {
358017 // usingClause is a list of strings.
358018 result->using_columns = TransformUsingClause(*root.usingClause);
358019 return std::move(result);
358020 }
358021
358022 // Check if this is a cross product.
358023 if (!root.quals && result->using_columns.empty() && result->ref_type == JoinRefType::REGULAR) {
358024 result->ref_type = JoinRefType::CROSS;
358025 }
358026 result->condition = TransformExpression(root.quals);
358027
358028 if (root.alias) {
358029 // This is a join with an alias, so we wrap it in a subquery.
358030 auto select_node = make_uniq<SelectNode>();
358031 select_node->select_list.push_back(make_uniq<StarExpression>());
358032 select_node->from_table = std::move(result);
358033
358034 auto select = make_uniq<SelectStatement>();
358035 select->node = std::move(select_node);
358036
358037 auto subquery = make_uniq<SubqueryRef>(std::move(select));
358038 SetQueryLocation(*subquery, root.location);
358039
358040 // Apply the alias to the subquery.
358041 subquery->alias = TransformAlias(root.alias, subquery->column_name_alias);
358042 return std::move(subquery);
358043 }
358044 return std::move(result);
358045}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformPivot()

unique_ptr< TableRef > duckdb::Transformer::TransformPivot ( duckdb_libpgquery::PGPivotExpr & root)
private

Transform a Postgres pivot node into a TableRef.

358104 {
358105 auto result = make_uniq<PivotRef>();
358106 result->source = TransformTableRefNode(*root.source);
358107 if (root.aggrs) {
358108 TransformExpressionList(*root.aggrs, result->aggregates);
358109 }
358110 if (root.unpivots) {
358111 result->unpivot_names = TransformStringList(root.unpivots);
358112 }
358113 auto is_pivot = result->unpivot_names.empty();
358114 result->pivots = TransformPivotList(*root.pivots, is_pivot);
358115 if (!is_pivot && result->pivots.size() > 1) {
358116 throw ParserException("UNPIVOT requires a single pivot element");
358117 }
358118 if (root.groups) {
358119 result->groups = TransformStringList(root.groups);
358120 }
358121 for (auto &pivot : result->pivots) {
358122 if (!is_pivot) {
358123 // unpivot
358124 if (pivot.unpivot_names.size() != 1) {
358125 throw ParserException("UNPIVOT requires a single column name for the PIVOT IN clause");
358126 }
358127 D_ASSERT(pivot.pivot_expressions.empty());
358128 }
358129 }
358130 result->include_nulls = root.include_nulls;
358131 result->alias = TransformAlias(root.alias, result->column_name_alias);
358132 SetQueryLocation(*result, root.location);
358133 return std::move(result);
358134}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformRangeSubselect()

unique_ptr< TableRef > duckdb::Transformer::TransformRangeSubselect ( duckdb_libpgquery::PGRangeSubselect & root)
private

Transform a table producing subquery into a TableRef.

358142 {
358143 Transformer subquery_transformer(*this);
358144 auto subquery = subquery_transformer.TransformSelectStmt(*root.subquery);
358145 if (!subquery) {
358146 return nullptr;
358147 }
358148 auto result = make_uniq<SubqueryRef>(std::move(subquery));
358149 result->alias = TransformAlias(root.alias, result->column_name_alias);
358150 if (root.sample) {
358151 result->sample = TransformSampleOptions(root.sample);
358152 }
358153 return std::move(result);
358154}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformValuesList()

unique_ptr< TableRef > duckdb::Transformer::TransformValuesList ( duckdb_libpgquery::PGList * list)
private

Transform a VALUES list into a set of expressions.

356364 {
356365 auto result = make_uniq<ExpressionListRef>();
356366 for (auto value_list = list->head; value_list != nullptr; value_list = value_list->next) {
356367 auto target = PGPointerCast<duckdb_libpgquery::PGList>(value_list->data.ptr_value);
356368
356369 vector<unique_ptr<ParsedExpression>> insert_values;
356370 TransformExpressionList(*target, insert_values);
356371 if (!result->values.empty()) {
356372 if (result->values[0].size() != insert_values.size()) {
356373 throw ParserException("VALUES lists must all be the same length");
356374 }
356375 }
356376 result->values.push_back(std::move(insert_values));
356377 }
356378 result->alias = "valueslist";
356379 return std::move(result);
356380}
Here is the call graph for this function:

◆ TransformUsingClause()

vector< string > duckdb::Transformer::TransformUsingClause ( duckdb_libpgquery::PGList & usingClause)
private

Transform using clause.

357954 {
357955 vector<string> result;
357956 for (auto node = usingClause.head; node != nullptr; node = node->next) {
357957 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
357958 D_ASSERT(target->type == duckdb_libpgquery::T_PGString);
357959 auto value = PGCast<duckdb_libpgquery::PGValue>(*target.get());
357960 result.push_back(string(value.val.str));
357961 }
357962 return result;
357963}
Here is the caller graph for this function:

◆ TransformQualifiedName()

QualifiedName duckdb::Transformer::TransformQualifiedName ( duckdb_libpgquery::PGRangeVar & root)
private

Transform a range var into a (schema) qualified name.

357880 {
357881 QualifiedName qname;
357882 if (root.catalogname) {
357883 qname.catalog = root.catalogname;
357884 } else {
357885 qname.catalog = INVALID_CATALOG;
357886 }
357887 if (root.schemaname) {
357888 qname.schema = root.schemaname;
357889 } else {
357890 qname.schema = INVALID_SCHEMA;
357891 }
357892 if (root.relname) {
357893 qname.name = root.relname;
357894 } else {
357895 throw ParserException("Empty table name not supported");
357896 }
357897 return qname;
357898}
Here is the caller graph for this function:

◆ TransformTypeExpressionInternal()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformTypeExpressionInternal ( duckdb_libpgquery::PGTypeName & name)
private

Transform a Postgres TypeName string into a LogicalType (non-LIST types).

354570 {
354571 // Parse typename/any qualifications
354572
354573 string unbound_name;
354574 string schema_name;
354575 string catalog_name;
354576
354577 if (type_name.names->length == 0) {
354578 throw ParserException("Type name cannot be empty");
354579 }
354580 if (type_name.names->length == 1) {
354581 auto unbound_name_cell = type_name.names->head;
354582
354583 unbound_name = PGPointerCast<duckdb_libpgquery::PGValue>(unbound_name_cell->data.ptr_value)->val.str;
354584 }
354585 if (type_name.names->length == 2) {
354586 auto schema_name_cell = type_name.names->head;
354587 auto unbound_name_cell = schema_name_cell->next;
354588
354589 schema_name = PGPointerCast<duckdb_libpgquery::PGValue>(schema_name_cell->data.ptr_value)->val.str;
354590 unbound_name = PGPointerCast<duckdb_libpgquery::PGValue>(unbound_name_cell->data.ptr_value)->val.str;
354591 }
354592 if (type_name.names->length == 3) {
354593 auto catalog_name_cell = type_name.names->head;
354594 auto schema_name_cell = catalog_name_cell->next;
354595 auto unbound_name_cell = schema_name_cell->next;
354596
354597 catalog_name = PGPointerCast<duckdb_libpgquery::PGValue>(catalog_name_cell->data.ptr_value)->val.str;
354598 schema_name = PGPointerCast<duckdb_libpgquery::PGValue>(schema_name_cell->data.ptr_value)->val.str;
354599 unbound_name = PGPointerCast<duckdb_libpgquery::PGValue>(unbound_name_cell->data.ptr_value)->val.str;
354600 }
354601 if (type_name.names->length >= 4) {
354602 throw ParserException(
354603 "Too many qualifications for type name - expected [catalog.schema.name] or [schema.name]");
354604 }
354605
354606 D_ASSERT(!unbound_name.empty());
354607
354608 // The postgres parser emits a bunch of strange type names - we want to normalize them here so that the alias for
354609 // columns from expressions containing these types actually use the DuckDB type name.
354610 // Eventually we should make the parser emit the correct names directly.
354611 auto known_type_id = TransformStringToLogicalTypeId(unbound_name);
354612 if (known_type_id != LogicalTypeId::UNBOUND) {
354613 unbound_name = LogicalTypeIdToString(known_type_id);
354614 }
354615
354616 // Parse type modifiers
354617 vector<unique_ptr<ParsedExpression>> type_params;
354618 for (auto typemod = type_name.typmods ? type_name.typmods->head : nullptr; typemod; typemod = typemod->next) {
354619 // Type mods are always a list of (name, node) pairs
354620
354621 string name_str;
354622 auto typemod_node = PGPointerCast<duckdb_libpgquery::PGNode>(typemod->data.ptr_value);
354623
354624 if (typemod_node->type == duckdb_libpgquery::T_PGList) {
354625 auto &typemod_pair = *PGPointerCast<duckdb_libpgquery::PGList>(typemod->data.ptr_value);
354626 if (typemod_pair.length != 2) {
354627 throw ParserException("Expected type modifier to be a pair of (name, value)");
354628 }
354629
354630 // This is the actual argument node
354631 typemod_node = PGPointerCast<duckdb_libpgquery::PGNode>(typemod_pair.tail->data.ptr_value);
354632
354633 // Extract name of the type modifier (optional)
354634 auto name_node = PGPointerCast<duckdb_libpgquery::PGNode>(typemod_pair.head->data.ptr_value);
354635 if (name_node) {
354636 if (name_node->type != duckdb_libpgquery::T_PGString) {
354637 throw ParserException("Expected a constant as type modifier name");
354638 }
354639 name_str = PGPointerCast<duckdb_libpgquery::PGValue>(name_node.get())->val.str;
354640 }
354641 }
354642
354643 // Extract value of the type modifier
354644 // This is either:
354645 // 1. A constant value
354646 // 2. A expression
354647 // 3. A type name
354648
354649 if (typemod_node->type == duckdb_libpgquery::T_PGTypeName) {
354650 auto type_node = *PGPointerCast<duckdb_libpgquery::PGTypeName>(typemod_node.get());
354651 auto type_expr = TransformTypeExpression(type_node);
354652 type_expr->SetAlias(std::move(name_str));
354653 type_params.push_back(std::move(type_expr));
354654 } else {
354655 // Expression
354656 auto expr = TransformExpression(*typemod_node);
354657
354658 // TODO: Allow arbitrary expressions in the future
354659 if (expr->GetExpressionClass() != ExpressionClass::CONSTANT) {
354660 throw ParserException("Expected a constant as type modifier");
354661 }
354662
354663 expr->SetAlias(std::move(name_str));
354664 type_params.push_back(std::move(expr));
354665 }
354666 }
354667
354668 auto result = make_uniq<TypeExpression>(catalog_name, schema_name, unbound_name, std::move(type_params));
354669
354670 // Assign query location
354671 if (type_name.location >= 0) {
354672 result->query_location = NumericCast<idx_t>(type_name.location);
354673 }
354674
354675 return std::move(result);
354676}
Here is the call graph for this function:

◆ TransformTypeExpression()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformTypeExpression ( duckdb_libpgquery::PGTypeName & name)
private
354678 {
354679 if (type_name.type != duckdb_libpgquery::T_PGTypeName) {
354680 throw ParserException("Expected a type");
354681 }
354682 auto stack_checker = StackCheck();
354683
354684 auto result = TransformTypeExpressionInternal(type_name);
354685
354686 if (type_name.arrayBounds) {
354687 // For both arrays and lists, the inner type is stored as the first type parameter
354688
354689 idx_t extra_stack = 0;
354690 for (auto cell = type_name.arrayBounds->head; cell != nullptr; cell = cell->next) {
354691 StackCheck(extra_stack++);
354692 auto arg = PGPointerCast<duckdb_libpgquery::PGNode>(cell->data.ptr_value);
354693
354694 if (arg->type == duckdb_libpgquery::T_PGInteger) {
354695 auto int_node = PGPointerCast<duckdb_libpgquery::PGValue>(arg.get());
354696 auto size = int_node->val.ival;
354697
354698 vector<unique_ptr<ParsedExpression>> type_params;
354699 type_params.push_back(std::move(result));
354700
354701 if (size == -1) {
354702 // LIST type
354703 result = make_uniq<TypeExpression>("list", std::move(type_params));
354704 } else {
354705 // ARRAY type
354706 type_params.push_back(make_uniq<ConstantExpression>(Value::BIGINT(int_node->val.ival)));
354707 result = make_uniq<TypeExpression>("array", std::move(type_params));
354708 }
354709
354710 continue;
354711 }
354712
354713 throw ParserException("ARRAY bounds must only contain expressions");
354714 }
354715 }
354716
354717 // Assign query location
354718 if (type_name.location >= 0) {
354719 result->query_location = NumericCast<idx_t>(type_name.location);
354720 }
354721
354722 return result;
354723}

◆ TransformTypeName()

LogicalType duckdb::Transformer::TransformTypeName ( duckdb_libpgquery::PGTypeName & name)
private

Transform a Postgres TypeName string into a LogicalType.

354725 {
354726 auto type_expr = TransformTypeExpression(type_name);
354727 return LogicalType::UNBOUND(std::move(type_expr));
354728}
Here is the caller graph for this function:

◆ TransformGroupBy()

bool duckdb::Transformer::TransformGroupBy ( optional_ptr< duckdb_libpgquery::PGList > group,
SelectNode & result )
private

Transform a Postgres GROUP BY expression into a list of Expression.

354399 {
354400 if (!group) {
354401 return false;
354402 }
354403 auto &result = select_node.groups;
354404 GroupingExpressionMap map;
354405 for (auto node = group->head; node != nullptr; node = node->next) {
354406 auto n = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
354407 vector<GroupingSet> result_sets;
354408 TransformGroupByNode(*n, map, select_node, result_sets);
354409 CheckGroupingSetMax(result_sets.size());
354410 if (result.grouping_sets.empty()) {
354411 // no grouping sets yet: use the current set of grouping sets
354412 result.grouping_sets = std::move(result_sets);
354413 } else {
354414 // compute the cross product
354415 vector<GroupingSet> new_sets;
354416 idx_t grouping_set_count = result.grouping_sets.size() * result_sets.size();
354417 CheckGroupingSetMax(grouping_set_count);
354418 new_sets.reserve(grouping_set_count);
354419 for (idx_t current_idx = 0; current_idx < result.grouping_sets.size(); current_idx++) {
354420 auto &current_set = result.grouping_sets[current_idx];
354421 for (idx_t new_idx = 0; new_idx < result_sets.size(); new_idx++) {
354422 auto &new_set = result_sets[new_idx];
354423 GroupingSet set;
354424 set.insert(current_set.begin(), current_set.end());
354425 set.insert(new_set.begin(), new_set.end());
354426 new_sets.push_back(std::move(set));
354427 }
354428 }
354429 result.grouping_sets = std::move(new_sets);
354430 }
354431 }
354432 if (result.group_expressions.size() == 1 && result.grouping_sets.size() == 1 &&
354433 ExpressionIsEmptyStar(*result.group_expressions[0])) {
354434 // GROUP BY *
354435 result.group_expressions.clear();
354436 result.grouping_sets.clear();
354437 select_node.aggregate_handling = AggregateHandling::FORCE_AGGREGATES;
354438 }
354439 return true;
354440}
Here is the call graph for this function:

◆ TransformGroupByNode()

void duckdb::Transformer::TransformGroupByNode ( duckdb_libpgquery::PGNode & n,
GroupingExpressionMap & map,
SelectNode & result,
vector< GroupingSet > & result_sets )
private
354337 {
354338 if (n.type == duckdb_libpgquery::T_PGGroupingSet) {
354339 auto &grouping_set = PGCast<duckdb_libpgquery::PGGroupingSet>(n);
354340 switch (grouping_set.kind) {
354341 case duckdb_libpgquery::GROUPING_SET_EMPTY:
354342 result_sets.emplace_back();
354343 break;
354344 case duckdb_libpgquery::GROUPING_SET_ALL: {
354345 result.aggregate_handling = AggregateHandling::FORCE_AGGREGATES;
354346 break;
354347 }
354348 case duckdb_libpgquery::GROUPING_SET_SETS: {
354349 for (auto node = grouping_set.content->head; node; node = node->next) {
354350 auto pg_node = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
354351 TransformGroupByNode(*pg_node, map, result, result_sets);
354352 }
354353 break;
354354 }
354355 case duckdb_libpgquery::GROUPING_SET_ROLLUP: {
354356 vector<GroupingSet> rollup_sets;
354357 for (auto node = grouping_set.content->head; node; node = node->next) {
354358 auto pg_node = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
354359 vector<idx_t> rollup_set;
354360 TransformGroupByExpression(*pg_node, map, result.groups, rollup_set);
354361 rollup_sets.push_back(VectorToGroupingSet(rollup_set));
354362 }
354363 // generate the subsets of the rollup set and add them to the grouping sets
354364 GroupingSet current_set;
354365 result_sets.push_back(current_set);
354366 for (idx_t i = 0; i < rollup_sets.size(); i++) {
354367 MergeGroupingSet(current_set, rollup_sets[i]);
354368 result_sets.push_back(current_set);
354369 }
354370 break;
354371 }
354372 case duckdb_libpgquery::GROUPING_SET_CUBE: {
354373 vector<GroupingSet> cube_sets;
354374 for (auto node = grouping_set.content->head; node; node = node->next) {
354375 auto pg_node = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
354376 vector<idx_t> cube_set;
354377 TransformGroupByExpression(*pg_node, map, result.groups, cube_set);
354378 cube_sets.push_back(VectorToGroupingSet(cube_set));
354379 }
354380 // generate the subsets of the rollup set and add them to the grouping sets
354381 CheckGroupingSetCubes(result_sets.size(), cube_sets.size());
354382
354383 GroupingSet current_set;
354384 AddCubeSets(current_set, cube_sets, result_sets, 0);
354385 break;
354386 }
354387 default:
354388 throw InternalException("Unsupported GROUPING SET type %d", grouping_set.kind);
354389 }
354390 } else {
354391 vector<idx_t> indexes;
354392 TransformGroupByExpression(n, map, result.groups, indexes);
354393 result_sets.push_back(VectorToGroupingSet(indexes));
354394 }
354395}

◆ AddGroupByExpression()

void duckdb::Transformer::AddGroupByExpression ( unique_ptr< ParsedExpression > expression,
GroupingExpressionMap & map,
GroupByNode & result,
vector< idx_t > & result_set )
private
354295 {
354296 if (expression->GetExpressionType() == ExpressionType::FUNCTION) {
354297 auto &func = expression->Cast<FunctionExpression>();
354298 if (func.function_name == "row") {
354299 for (auto &child : func.children) {
354300 AddGroupByExpression(std::move(child), map, result, result_set);
354301 }
354302 return;
354303 }
354304 }
354305 auto entry = map.map.find(*expression);
354306 idx_t result_idx;
354307 if (entry == map.map.end()) {
354308 result_idx = result.group_expressions.size();
354309 map.map[*expression] = result_idx;
354310 result.group_expressions.push_back(std::move(expression));
354311 } else {
354312 result_idx = entry->second;
354313 }
354314 result_set.push_back(result_idx);
354315}

◆ TransformGroupByExpression()

void duckdb::Transformer::TransformGroupByExpression ( duckdb_libpgquery::PGNode & n,
GroupingExpressionMap & map,
GroupByNode & result,
vector< idx_t > & result_set )
private
354329 {
354330 auto expression = TransformExpression(n);
354331 AddGroupByExpression(std::move(expression), map, result, indexes);
354332}

◆ TransformOrderBy()

bool duckdb::Transformer::TransformOrderBy ( duckdb_libpgquery::PGList * order,
vector< OrderByNode > & result )
private

Transform a Postgres ORDER BY expression into an OrderByDescription.

354450 {
354451 if (!order) {
354452 return false;
354453 }
354454
354455 for (auto node = order->head; node != nullptr; node = node->next) {
354456 auto temp_node = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
354457 if (temp_node->type != duckdb_libpgquery::T_PGSortBy) {
354458 throw NotImplementedException("ORDER BY list member type %d\n", temp_node->type);
354459 }
354460
354461 OrderType type;
354462 OrderByNullType null_order;
354463 auto sort = PGCast<duckdb_libpgquery::PGSortBy>(*temp_node.get());
354464 auto target = sort.node;
354465
354466 if (sort.sortby_dir == duckdb_libpgquery::PG_SORTBY_DEFAULT) {
354467 type = OrderType::ORDER_DEFAULT;
354468 } else if (sort.sortby_dir == duckdb_libpgquery::PG_SORTBY_ASC) {
354469 type = OrderType::ASCENDING;
354470 } else if (sort.sortby_dir == duckdb_libpgquery::PG_SORTBY_DESC) {
354471 type = OrderType::DESCENDING;
354472 } else {
354473 throw NotImplementedException("Unimplemented order by type");
354474 }
354475
354476 if (sort.sortby_nulls == duckdb_libpgquery::PG_SORTBY_NULLS_DEFAULT) {
354477 null_order = OrderByNullType::ORDER_DEFAULT;
354478 } else if (sort.sortby_nulls == duckdb_libpgquery::PG_SORTBY_NULLS_FIRST) {
354479 null_order = OrderByNullType::NULLS_FIRST;
354480 } else if (sort.sortby_nulls == duckdb_libpgquery::PG_SORTBY_NULLS_LAST) {
354481 null_order = OrderByNullType::NULLS_LAST;
354482 } else {
354483 throw NotImplementedException("Unimplemented order by type");
354484 }
354485
354486 auto order_expression = TransformExpression(target);
354487 result.emplace_back(type, null_order, std::move(order_expression));
354488 }
354489 return true;
354490}
void sort(InputArray src, OutputArray dst, int flags)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformInExpression()

unique_ptr< ParsedExpression > duckdb::Transformer::TransformInExpression ( const string & name,
duckdb_libpgquery::PGAExpr & root )
private

Transform to a IN or NOT IN expression.

352811 {
352812 auto left_expr = TransformExpression(root.lexpr);
352813 ExpressionType operator_type;
352814 // this looks very odd, but seems to be the way to find out its NOT IN
352815 if (name == "<>") {
352816 // NOT IN
352817 operator_type = ExpressionType::COMPARE_NOT_IN;
352818 } else {
352819 // IN
352820 operator_type = ExpressionType::COMPARE_IN;
352821 }
352822
352823 if (root.rexpr->type == duckdb_libpgquery::T_PGList) {
352824 auto result = make_uniq<OperatorExpression>(operator_type, std::move(left_expr));
352825 TransformExpressionList(*PGPointerCast<duckdb_libpgquery::PGList>(root.rexpr), result->children);
352826 return std::move(result);
352827 }
352828 auto expr = TransformExpression(*root.rexpr);
352829
352830 vector<unique_ptr<ParsedExpression>> children;
352831 children.push_back(std::move(expr));
352832 children.push_back(std::move(left_expr));
352833 auto result = make_uniq_base<ParsedExpression, FunctionExpression>("contains", std::move(children));
352834 if (operator_type == ExpressionType::COMPARE_NOT_IN) {
352835 result = make_uniq_base<ParsedExpression, OperatorExpression>(ExpressionType::OPERATOR_NOT, std::move(result));
352836 }
352837 return result;
352838}
Here is the call graph for this function:

◆ TransformExpressionList()

void duckdb::Transformer::TransformExpressionList ( duckdb_libpgquery::PGList & list,
vector< unique_ptr< ParsedExpression > > & result )
private

Transform a Postgres SELECT clause into a list of Expressions.

352018 {
352019 for (auto node = list.head; node != nullptr; node = node->next) {
352020 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
352021
352022 auto expr = TransformExpression(*target);
352023 result.push_back(std::move(expr));
352024 }
352025}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformWindowDef()

void duckdb::Transformer::TransformWindowDef ( duckdb_libpgquery::PGWindowDef & window_spec,
WindowExpression & expr,
const char * window_name = nullptr )
private

Transform a Postgres PARTITION BY/ORDER BY specification into lists of expressions.

352042 {
352043 // next: partitioning/ordering expressions
352044 if (window_spec.partitionClause) {
352045 if (window_name && !expr.partitions.empty()) {
352046 throw ParserException("Cannot override PARTITION BY clause of window \"%s\"", window_name);
352047 }
352048 TransformExpressionList(*window_spec.partitionClause, expr.partitions);
352049 }
352050 if (window_spec.orderClause) {
352051 if (window_name && !expr.orders.empty()) {
352052 throw ParserException("Cannot override ORDER BY clause of window \"%s\"", window_name);
352053 }
352054 TransformOrderBy(window_spec.orderClause, expr.orders);
352055 for (auto &order : expr.orders) {
352056 if (order.expression->GetExpressionType() == ExpressionType::STAR) {
352057 auto &star = order.expression->Cast<StarExpression>();
352058 if (!star.expr) {
352059 throw ParserException("Cannot ORDER BY ALL in a window expression");
352060 }
352061 }
352062 }
352063 }
352064}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformWindowFrame()

void duckdb::Transformer::TransformWindowFrame ( duckdb_libpgquery::PGWindowDef & window_spec,
WindowExpression & expr )
private

Transform a Postgres window frame specification into frame expressions.

352099 {
352100 // finally: specifics of bounds
352101 expr.start_expr = TransformExpression(window_spec.startOffset);
352102 expr.end_expr = TransformExpression(window_spec.endOffset);
352103
352104 if ((window_spec.frameOptions & FRAMEOPTION_END_UNBOUNDED_PRECEDING) ||
352105 (window_spec.frameOptions & FRAMEOPTION_START_UNBOUNDED_FOLLOWING)) {
352106 throw InternalException(
352107 "Window frames starting with unbounded following or ending in unbounded preceding make no sense");
352108 }
352109
352110 if (window_spec.frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) {
352111 expr.start = WindowBoundary::UNBOUNDED_PRECEDING;
352112 } else if (window_spec.frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING) {
352113 expr.start = TransformFrameOption(window_spec.frameOptions, WindowBoundary::EXPR_PRECEDING_ROWS,
352114 WindowBoundary::EXPR_PRECEDING_RANGE, WindowBoundary::EXPR_PRECEDING_GROUPS);
352115 } else if (window_spec.frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING) {
352116 expr.start = TransformFrameOption(window_spec.frameOptions, WindowBoundary::EXPR_FOLLOWING_ROWS,
352117 WindowBoundary::EXPR_FOLLOWING_RANGE, WindowBoundary::EXPR_FOLLOWING_GROUPS);
352118 } else if (window_spec.frameOptions & FRAMEOPTION_START_CURRENT_ROW) {
352119 expr.start = TransformFrameOption(window_spec.frameOptions, WindowBoundary::CURRENT_ROW_ROWS,
352120 WindowBoundary::CURRENT_ROW_RANGE, WindowBoundary::CURRENT_ROW_GROUPS);
352121 }
352122
352123 if (window_spec.frameOptions & FRAMEOPTION_END_UNBOUNDED_FOLLOWING) {
352124 expr.end = WindowBoundary::UNBOUNDED_FOLLOWING;
352125 } else if (window_spec.frameOptions & FRAMEOPTION_END_OFFSET_PRECEDING) {
352126 expr.end = TransformFrameOption(window_spec.frameOptions, WindowBoundary::EXPR_PRECEDING_ROWS,
352127 WindowBoundary::EXPR_PRECEDING_RANGE, WindowBoundary::EXPR_PRECEDING_GROUPS);
352128 } else if (window_spec.frameOptions & FRAMEOPTION_END_OFFSET_FOLLOWING) {
352129 expr.end = TransformFrameOption(window_spec.frameOptions, WindowBoundary::EXPR_FOLLOWING_ROWS,
352130 WindowBoundary::EXPR_FOLLOWING_RANGE, WindowBoundary::EXPR_FOLLOWING_GROUPS);
352131 } else if (window_spec.frameOptions & FRAMEOPTION_END_CURRENT_ROW) {
352132 expr.end = TransformFrameOption(window_spec.frameOptions, WindowBoundary::CURRENT_ROW_ROWS,
352133 WindowBoundary::CURRENT_ROW_RANGE, WindowBoundary::CURRENT_ROW_GROUPS);
352134 }
352135
352136 D_ASSERT(expr.start != WindowBoundary::INVALID && expr.end != WindowBoundary::INVALID);
352137 if (((window_spec.frameOptions & (FRAMEOPTION_START_OFFSET_PRECEDING | FRAMEOPTION_START_OFFSET_FOLLOWING)) &&
352138 !expr.start_expr) ||
352139 ((window_spec.frameOptions & (FRAMEOPTION_END_OFFSET_PRECEDING | FRAMEOPTION_END_OFFSET_FOLLOWING)) &&
352140 !expr.end_expr)) {
352141 throw InternalException("Failed to transform window boundary expression");
352142 }
352143
352144 if (window_spec.frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW) {
352145 expr.exclude_clause = WindowExcludeMode::CURRENT_ROW;
352146 } else if (window_spec.frameOptions & FRAMEOPTION_EXCLUDE_GROUP) {
352147 expr.exclude_clause = WindowExcludeMode::GROUP;
352148 } else if (window_spec.frameOptions & FRAMEOPTION_EXCLUDE_TIES) {
352149 expr.exclude_clause = WindowExcludeMode::TIES;
352150 } else {
352151 expr.exclude_clause = WindowExcludeMode::NO_OTHER;
352152 }
352153
352154 if (expr.exclude_clause != WindowExcludeMode::NO_OTHER && !expr.arg_orders.empty() &&
352155 !IsExcludableWindowFunction(expr.type)) {
352156 throw ParserException("EXCLUDE is not supported for the window function \"%s\"", expr.function_name.c_str());
352157 }
352158}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformSampleOptions()

unique_ptr< SampleOptions > duckdb::Transformer::TransformSampleOptions ( optional_ptr< duckdb_libpgquery::PGNode > options)
private
354514 {
354515 if (!options) {
354516 return nullptr;
354517 }
354518 auto result = make_uniq<SampleOptions>();
354519 auto &sample_options = PGCast<duckdb_libpgquery::PGSampleOptions>(*options);
354520 auto &sample_size = *PGPointerCast<duckdb_libpgquery::PGSampleSize>(sample_options.sample_size);
354521 auto sample_expression = TransformExpression(sample_size.sample_size);
354522 if (sample_expression->GetExpressionType() != ExpressionType::VALUE_CONSTANT) {
354523 throw ParserException(sample_expression->GetQueryLocation(),
354524 "Only constants are supported in sample clause currently");
354525 }
354526 auto &const_expr = sample_expression->Cast<ConstantExpression>();
354527 auto &sample_value = const_expr.value;
354528 result->is_percentage = sample_size.is_percentage;
354529 if (sample_size.is_percentage) {
354530 // sample size is given in sample_size: use system sampling
354531 auto percentage = sample_value.GetValue<double>();
354532 if (percentage < 0 || percentage > 100) {
354533 throw ParserException("Sample sample_size %llf out of range, must be between 0 and 100", percentage);
354534 }
354535 result->sample_size = Value::DOUBLE(percentage);
354536 result->method = SampleMethod::SYSTEM_SAMPLE;
354537 } else {
354538 // sample size is given in rows: use reservoir sampling
354539 auto rows = sample_value.GetValue<int64_t>();
354540 if (rows < 0 || sample_value.GetValue<uint64_t>() > SampleOptions::MAX_SAMPLE_ROWS) {
354541 throw ParserException("Sample rows %lld out of range, must be between 0 and %lld", rows,
354542 SampleOptions::MAX_SAMPLE_ROWS);
354543 }
354544 result->sample_size = Value::BIGINT(rows);
354545 result->method = SampleMethod::RESERVOIR_SAMPLE;
354546 }
354547 if (sample_options.method) {
354548 result->method = GetSampleMethod(sample_options.method);
354549 }
354550 if (sample_options.has_seed && sample_options.seed >= 0) {
354551 result->seed = static_cast<idx_t>(sample_options.seed);
354552 result->repeatable = true;
354553 }
354554 return result;
354555}
int rows

◆ ExpressionIsEmptyStar()

bool duckdb::Transformer::ExpressionIsEmptyStar ( ParsedExpression & expr)
private

Returns true if an expression is only a star (i.e. "*", without any other decorators).

352160 {
352161 if (expr.GetExpressionClass() != ExpressionClass::STAR) {
352162 return false;
352163 }
352164 auto &star = expr.Cast<StarExpression>();
352165 if (!star.columns && star.exclude_list.empty() && star.replace_list.empty()) {
352166 return true;
352167 }
352168 return false;
352169}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TransformOnEntryNotFound()

OnEntryNotFound duckdb::Transformer::TransformOnEntryNotFound ( bool missing_ok)
private
354854 {
354855 return missing_ok ? OnEntryNotFound::RETURN_NULL : OnEntryNotFound::THROW_EXCEPTION;
354856}

◆ PGListToVector()

Vector duckdb::Transformer::PGListToVector ( optional_ptr< duckdb_libpgquery::PGList > column_list,
idx_t & size )
private
355992 {
355993 if (!column_list) {
355994 Vector result(LogicalType::VARCHAR);
355995 return result;
355996 }
355997 // First we discover the size of this list
355998 for (auto c = column_list->head; c != nullptr; c = lnext(c)) {
355999 size++;
356000 }
356001
356002 Vector result(LogicalType::VARCHAR, size);
356003 auto result_ptr = FlatVector::GetData<string_t>(result);
356004
356005 size = 0;
356006 for (auto c = column_list->head; c != nullptr; c = lnext(c)) {
356007 auto &type_val = *PGPointerCast<duckdb_libpgquery::PGAConst>(c->data.ptr_value);
356008 auto &entry_value_node = type_val.val;
356009 if (entry_value_node.type != duckdb_libpgquery::T_PGString) {
356010 throw ParserException("Expected a string constant as value");
356011 }
356012
356013 auto entry_value = string(entry_value_node.val.str);
356014 result_ptr[size++] = StringVector::AddStringOrBlob(result, entry_value);
356015 }
356016 return result;
356017}
static DUCKDB_API string_t AddStringOrBlob(Vector &vector, const char *data, idx_t len)
Definition duckdb.cpp:137893

◆ TransformConflictTarget()

vector< string > duckdb::Transformer::TransformConflictTarget ( duckdb_libpgquery::PGList & list)
private
357703 {
357704 vector<string> columns;
357705 for (auto cell = list.head; cell != nullptr; cell = cell->next) {
357706 auto index_element = PGPointerCast<duckdb_libpgquery::PGIndexElem>(cell->data.ptr_value);
357707 if (index_element->collation) {
357708 throw NotImplementedException("Index with collation not supported yet!");
357709 }
357710 if (index_element->opclass) {
357711 throw NotImplementedException("Index with opclass not supported yet!");
357712 }
357713 if (!index_element->name) {
357714 throw NotImplementedException("Non-column index element not supported yet!");
357715 }
357716 if (index_element->nulls_ordering) {
357717 throw NotImplementedException("Index with null_ordering not supported yet!");
357718 }
357719 if (index_element->ordering) {
357720 throw NotImplementedException("Index with ordering not supported yet!");
357721 }
357722 columns.emplace_back(index_element->name);
357723 }
357724 return columns;
357725}

◆ TransformMacroFunction()

unique_ptr< MacroFunction > duckdb::Transformer::TransformMacroFunction ( duckdb_libpgquery::PGFunctionDefinition & function)
private
355387 {
355388 unique_ptr<MacroFunction> macro_func;
355389 if (def.function) {
355390 auto expression = TransformExpression(def.function);
355391 macro_func = make_uniq<ScalarMacroFunction>(std::move(expression));
355392 } else if (def.query) {
355393 auto query_node = TransformSelectNode(*def.query);
355394 macro_func = make_uniq<TableMacroFunction>(std::move(query_node));
355395 }
355396
355397 if (!def.params) {
355398 return macro_func;
355399 }
355400
355401 case_insensitive_set_t parameter_names;
355402 for (auto node = def.params->head; node != nullptr; node = node->next) {
355403 auto target = PGPointerCast<duckdb_libpgquery::PGNode>(node->data.ptr_value);
355404 if (target->type != duckdb_libpgquery::T_PGFunctionParameter) {
355405 throw InternalException("TODO");
355406 }
355407 auto &param = PGCast<duckdb_libpgquery::PGFunctionParameter>(*target);
355408
355409 // Transform parameter name/type
355410 if (!parameter_names.insert(param.name).second) {
355411 throw ParserException("Duplicate parameter '%s' in macro definition", param.name);
355412 }
355413 macro_func->parameters.emplace_back(make_uniq<ColumnRefExpression>(param.name));
355414 macro_func->types.emplace_back(param.typeName ? TransformTypeName(*param.typeName) : LogicalType::UNKNOWN);
355415
355416 // Transform parameter default value
355417 if (param.defaultValue) {
355418 auto default_expr = TransformExpression(PGPointerCast<duckdb_libpgquery::PGNode>(param.defaultValue));
355419 default_expr->SetAlias(param.name);
355420 macro_func->default_parameters[param.name] = std::move(default_expr);
355421 } else if (!macro_func->default_parameters.empty()) {
355422 throw ParserException("Parameter without a default follows parameter with a default");
355423 }
355424 }
355425
355426 return macro_func;
355427}

◆ TransformNameList()

vector< string > duckdb::Transformer::TransformNameList ( duckdb_libpgquery::PGList & list)
private
354858 {
354859 vector<string> result;
354860 for (auto c = list.head; c != nullptr; c = c->next) {
354861 result.emplace_back(static_cast<char *>(c->data.ptr_value));
354862 }
354863 return result;
354864}

◆ SetQueryLocation() [1/2]

void duckdb::Transformer::SetQueryLocation ( ParsedExpression & expr,
int query_location )
static
358524 {
358525 if (query_location < 0) {
358526 return;
358527 }
358528 expr.SetQueryLocation(optional_idx(static_cast<idx_t>(query_location)));
358529}

◆ SetQueryLocation() [2/2]

void duckdb::Transformer::SetQueryLocation ( TableRef & ref,
int query_location )
static
358531 {
358532 if (query_location < 0) {
358533 return;
358534 }
358535 ref.query_location = optional_idx(static_cast<idx_t>(query_location));
358536}

◆ InitializeStackCheck()

void duckdb::Transformer::InitializeStackCheck ( )
private
358332 {
358333 stack_depth = 0;
358334}

◆ StackCheck()

StackChecker< Transformer > duckdb::Transformer::StackCheck ( idx_t extra_stack = 1)
private
358336 {
358337 auto &root = RootTransformer();
358338 D_ASSERT(root.stack_depth != DConstants::INVALID_INDEX);
358339 if (root.stack_depth + extra_stack >= options.max_expression_depth) {
358340 throw ParserException("Max expression depth limit of %lld exceeded. Use \"SET max_expression_depth TO x\" to "
358341 "increase the maximum expression depth.",
358342 options.max_expression_depth);
358343 }
358344 return StackChecker<Transformer>(root, extra_stack);
358345}

◆ PGCast()

template<class T>
T & duckdb::Transformer::PGCast ( duckdb_libpgquery::PGNode & node)
inlinestatic
346809 {
346810 return reinterpret_cast<T &>(node);
346811 }

◆ PGPointerCast()

template<class T>
optional_ptr< T > duckdb::Transformer::PGPointerCast ( void * ptr)
inlinestatic
346813 {
346814 return optional_ptr<T>(reinterpret_cast<T *>(ptr));
346815 }

The documentation for this class was generated from the following file: