@@ -209,6 +209,11 @@ const (
209209 // checking the parameters of a window function in order to reject nested
210210 // window functions.
211211 WindowFuncAncestor
212+
213+ // ConditionalAncestor is temporarily added to ScalarAncestors while type
214+ // checking condition expressions CASE, COALESCE, and IF. Used to reject
215+ // set-returning functions within conditional expressions.
216+ ConditionalAncestor
212217)
213218
214219// Push adds the given ancestor to s.
@@ -424,11 +429,8 @@ func (expr *CaseExpr) TypeCheck(
424429 ctx context.Context , semaCtx * SemaContext , desired * types.T ,
425430) (TypedExpr , error ) {
426431 if semaCtx != nil {
427- // We need to save and restore the previous value of the field in
428- // semaCtx in case we are recursively called within a subquery
429- // context.
430- defer semaCtx .Properties .Restore (semaCtx .Properties )
431- semaCtx .Properties .Require ("CASE" , RejectGenerators )
432+ defer semaCtx .Properties .Ancestors .PopTo (semaCtx .Properties .Ancestors )
433+ semaCtx .Properties .Ancestors .Push (ConditionalAncestor )
432434 }
433435 var err error
434436 tmpExprs := make ([]Expr , 0 , len (expr .Whens )+ 1 )
@@ -877,11 +879,8 @@ func (expr *CoalesceExpr) TypeCheck(
877879 ctx context.Context , semaCtx * SemaContext , desired * types.T ,
878880) (TypedExpr , error ) {
879881 if semaCtx != nil {
880- // We need to save and restore the previous value of the field in
881- // semaCtx in case we are recursively called within a subquery
882- // context.
883- defer semaCtx .Properties .Restore (semaCtx .Properties )
884- semaCtx .Properties .Require ("COALESCE" , RejectGenerators )
882+ defer semaCtx .Properties .Ancestors .PopTo (semaCtx .Properties .Ancestors )
883+ semaCtx .Properties .Ancestors .Push (ConditionalAncestor )
885884 }
886885 typedSubExprs , retType , err := typeCheckSameTypedExprs (ctx , semaCtx , desired , expr .Exprs ... )
887886 if err != nil {
@@ -1029,6 +1028,9 @@ func (sc *SemaContext) checkFunctionUsage(expr *FuncExpr, def *ResolvedFunctionD
10291028 if sc .Properties .IsSet (RejectGenerators ) {
10301029 return NewInvalidFunctionUsageError (GeneratorClass , sc .Properties .required .context )
10311030 }
1031+ if sc .Properties .Ancestors .Has (ConditionalAncestor ) {
1032+ return NewInvalidFunctionUsageError (GeneratorClass , "conditional expressions" )
1033+ }
10321034 sc .Properties .Derived .SeenGenerator = true
10331035 }
10341036 return nil
@@ -1355,11 +1357,8 @@ func (expr *IfExpr) TypeCheck(
13551357 ctx context.Context , semaCtx * SemaContext , desired * types.T ,
13561358) (TypedExpr , error ) {
13571359 if semaCtx != nil {
1358- // We need to save and restore the previous value of the field in
1359- // semaCtx in case we are recursively called within a subquery
1360- // context.
1361- defer semaCtx .Properties .Restore (semaCtx .Properties )
1362- semaCtx .Properties .Require ("IF" , RejectGenerators )
1360+ defer semaCtx .Properties .Ancestors .PopTo (semaCtx .Properties .Ancestors )
1361+ semaCtx .Properties .Ancestors .Push (ConditionalAncestor )
13631362 }
13641363 typedCond , err := typeCheckAndRequireBoolean (ctx , semaCtx , expr .Cond , "IF condition" )
13651364 if err != nil {
0 commit comments