MCPcopy Create free account
hub / github.com/brimdata/super / scalarSubqueryCheck

Method scalarSubqueryCheck

compiler/semantic/expr.go:1315–1361  ·  view source on GitHub ↗
(n ast.Node, seq sem.Seq, thisType super.Type)

Source from the content-addressed store, hash-verified

1313}
1314
1315func (t *translator) scalarSubqueryCheck(n ast.Node, seq sem.Seq, thisType super.Type) (sem.Seq, super.Type) {
1316 // In a SQL expression (except for RHS of EXISTS or IN, which both use array result),
1317 // a subquery returns a scalar but the result of the subquery is a relation.
1318 // The runtime already checks if the subquery returns multiple "rows", so
1319 // here we just check if the relation is a single column and error appropriately
1320 // or otherwise pull out the value to make the scalar.
1321 // values is_error(this) ? this : (len(this) == 1 ? this[1] : error(...) )
1322 lenCall := &sem.CallExpr{
1323 Node: n,
1324 Tag: "len",
1325 Args: []sem.Expr{sem.NewThis(n, nil)},
1326 }
1327 lenCond := &sem.BinaryExpr{
1328 Node: n,
1329 Op: "==",
1330 LHS: lenCall,
1331 RHS: sem.NewLiteral(n, super.NewInt64(1)),
1332 }
1333 // Note no need to check index_base here since we are directly
1334 // creating the underlying index expression.
1335 indexExpr := &sem.IndexExpr{
1336 Node: n,
1337 Expr: sem.NewThis(n, nil),
1338 Index: sem.NewLiteral(n, super.NewInt64(0)),
1339 }
1340 innerCond := &sem.CondExpr{
1341 Node: n,
1342 Cond: lenCond,
1343 Then: indexExpr,
1344 Else: sem.NewStructuredError(n, "subquery expression cannot have multiple columns", sem.NewThis(n, nil)),
1345 }
1346 isErrCond := &sem.CallExpr{
1347 Node: n,
1348 Tag: "is_error",
1349 Args: []sem.Expr{sem.NewThis(n, nil)},
1350 }
1351 outerCond := &sem.CondExpr{
1352 Node: n,
1353 Cond: isErrCond,
1354 Then: sem.NewThis(n, nil),
1355 Else: innerCond,
1356 }
1357 return append(seq, &sem.ValuesOp{
1358 Node: n,
1359 Exprs: []sem.Expr{outerCond},
1360 }), t.checker.expr(thisType, outerCond)
1361}
1362
1363func isCorrelated(seq sem.Seq) bool {
1364 if len(seq) >= 1 {

Callers 1

subqueryExprMethod · 0.95

Calls 5

NewThisFunction · 0.92
NewLiteralFunction · 0.92
NewInt64Function · 0.92
NewStructuredErrorFunction · 0.92
exprMethod · 0.45

Tested by

no test coverage detected