|
| 1 | +// Copyright 2022 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package funcdesc |
| 7 | + |
| 8 | +import ( |
| 9 | + "github.com/cockroachdb/cockroach/pkg/clusterversion" |
| 10 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" |
| 11 | + "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" |
| 12 | +) |
| 13 | + |
| 14 | +var schemaExprContextAllowingUDF = map[tree.SchemaExprContext]clusterversion.Key{ |
| 15 | + tree.CheckConstraintExpr: clusterversion.MinSupported, |
| 16 | + tree.ColumnDefaultExprInNewTable: clusterversion.MinSupported, |
| 17 | + tree.ColumnDefaultExprInSetDefault: clusterversion.MinSupported, |
| 18 | + |
| 19 | + tree.PolicyUsingExpr: clusterversion.V25_2, |
| 20 | + tree.PolicyWithCheckExpr: clusterversion.V25_2, |
| 21 | + |
| 22 | + tree.ColumnDefaultExprInAddColumn: clusterversion.V25_3, |
| 23 | + tree.ColumnDefaultExprInNewView: clusterversion.V25_3, |
| 24 | + tree.ColumnOnUpdateExpr: clusterversion.V25_3, |
| 25 | + tree.ExpressionIndexElementExpr: clusterversion.V25_3, |
| 26 | + tree.IndexPredicateExpr: clusterversion.V25_3, |
| 27 | + tree.StoredComputedColumnExpr: clusterversion.V25_3, |
| 28 | + tree.VirtualComputedColumnExpr: clusterversion.V25_3, |
| 29 | +} |
| 30 | + |
| 31 | +// MaybeFailOnUDFUsage returns an error if the given expression or any |
| 32 | +// sub-expression used a UDF unless it's explicitly listed as an allowed use |
| 33 | +// case. |
| 34 | +func MaybeFailOnUDFUsage( |
| 35 | + expr tree.TypedExpr, exprContext tree.SchemaExprContext, version clusterversion.ClusterVersion, |
| 36 | +) error { |
| 37 | + if supportedVersion, ok := schemaExprContextAllowingUDF[exprContext]; ok && version.IsActive(supportedVersion) { |
| 38 | + return nil |
| 39 | + } |
| 40 | + visitor := &tree.UDFDisallowanceVisitor{} |
| 41 | + tree.WalkExpr(visitor, expr) |
| 42 | + if visitor.FoundUDF { |
| 43 | + return unimplemented.NewWithIssue(83234, "usage of user-defined function from relations not supported") |
| 44 | + } |
| 45 | + return nil |
| 46 | +} |
0 commit comments