Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In 'from' clause, allow initializers to reference previous variables #12

Closed
julianhyde opened this issue Feb 29, 2020 · 1 comment
Closed

Comments

@julianhyde
Copy link
Collaborator

julianhyde commented Feb 29, 2020

In 'from' clause, allow initializers to reference previous variables. For example:

from word in ["hello", "world"],
    c in String_explode word
  yield c;

The initializer of c is String_explode word, which references the word variable defined in the outer loop.

Comparison with SQL

In SQL, this is a called a lateral join.

SQL example 1. Use the LATERAL keyword to signal that a sub-query in the FROM clause can reference other variables:

SELECT *
FROM orders AS o,
 LATERAL (SELECT * FROM lineItems AS i WHERE i.orderId = o.id)

Note that the sub-query references o. Without LATERAL, that would be illegal.

SQL example 2. The UNNEST function is implicitly lateral:

SELECT *
FROM orders AS o,
  LATERAL UNNEST(o.lineItems) AS i

SQL example 3. Use the CROSS APPLY syntax to call a table function, again, implicitly lateral:

SELECT *
FROM emps AS e
  CROSS APPLY findDependents(e.empno)

Efficient implementation?

Should we use an efficient implementation, so that if the initializer does not reference previous variables, it is evaluated at the start?

No. If the user wants that efficiency, they can assign the initializer to a variable. For example, in

from i in String_explode "abc",
    j in String_explode "xy";

i's initializer String_explode "abc" should be called once, and j's initializer String_explode "xy" should be called 3 times. But in

from i in String_explode "",
    j in String_explode "xy";

i's initializer String_explode "" should be called once, and j's initializer String_explode "xy" should be called 0 times. In

let
  val i0 = String_explode "abc"
  val j0 = String_explode "xy"
in
  from i in i0,
      j in j0
end

each String_explode is evaluated once.

@julianhyde
Copy link
Collaborator Author

julianhyde commented Mar 2, 2020

Fixed in 7397c39.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant