-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add a 'while' clause to FLWOR expressions #187
Comments
This is not true. This can be done even in pure XPath 3.1 and a complete working example is provided in my comment here: #80 (comment)
|
@michaelhkay What substantial reasons do you see for dropping this initial proposal? Why address this only in XQuery and not in XPath as your initial proposal did? Here is one example from your initial proposal:
And I believe that with a small enhancement from my side, the proposal looked even better: (: Expr 2 :)
for $item in $input
with-accumulator $all initially true() then ($all and $item)
while $all
yield-accumulator Therefore I propose to support, unless there are pressing reasons not to, your initial proposal in issue #80, with consequent amendments, as shown above. |
Thanks for the new proposal, and the decision to look at I had expressed some concerns on extending the FLWOR expression in #80 (comment), but I see that the And I better understand the fundamental differences between the proposed
@dnovatchev True indeed; but I assume that just a handful people will be able to write and understand queries of this type? |
Yes, the absolute statement that this cannot be done in XQuery is not true, and it is not true even in the case of XPath. I hope that @michaelhkay could edit and correct this misleading, absolute statement?
One can make assumptions that seem likely, but are not based on data, and use such assumptions to support decisions. It is definitely better if we could rely on existing data. While the pure XPath technique can seem fairly advanced, using recursion with named functions in XQuery is a common practice/feature/capability and many thousands of developers, practically everybody who can write code for n! (factorial) in any programming language, can write recursive functions in XQuery. Even the seemingly advanced technique for recursion in pure XPath isn't as misunderstood as some people may suppose. This technique was first proposed more than 10 years ago. It has been publicized in my blog, and at the Balisage 2013 conference in front of many people. It was extensively explained in the Pluralsight course (2014) "The Evolution of XPath: What's New in Xpath 3.0". We have objective data for this course: Since its publishing it has been watched by more than 2000 people. Even if just half of these people understood the technique, this makes 1000 people, not just a "handful" And what do you think, @ChristianGruen: even if this subjective statement were true, that "just a handful people will be able to write and understand queries of this type": should we just support the status quo (supposed unawareness |
Thanks for your thoughts. I’ll use the chat to dive deeper into this to keep the discussion in the issue focused. |
One of the user communities that is remarkably well served by QT are folks who do not consider themselves programmers. These are academics and other professionals in non-programming disciplines, folks who picked up a tool like XSLT because it allowed them to get some stuff done without having to learn Perl or Python or Java or C# or Haskell or Scala or whatever your favorite language is. These users are important to me! I have met many who self-identify as "non-programmers". That they clearly are programmers is irrelevant. Many are unprepared to engage, and some believe themselves incapable of engaging, in the mental gymnastics that allow one to write a recursive function, let alone a recursive function with a HOF parameter. Features like The QT languages are not an academic exercise in purity of expression. They are real world tools that ordinary humans use to get important jobs done. |
Firstly, I'm not withdrawing the original proposal, I'm just breaking it up into bite-size chunks that I hope are more manageable, in order to help focus the discussion. My assertion about the inability to do this currently was in the context of FLWOR expressions; I thought the context made that clear. Of course I know that you can achieve anything if you write recursive functions, but I also know there are many users who find recursive functions very difficult to write and debug. Users coming from SQL, in particular, like to do everything they can using FLWOR expressions alone. |
@ndw, Yes, and I really support this. My question to @michaelhkay was why in his current state his proposal was stepped back from the initial one and now is targeting only XQuery.
Absolutely, and this group is certainly not a static one, thus we are in a unique position to do our best to help and support their further learning and development. |
@michaelhkay Sorry for misunderstanding this
Understood. I would fully support this feature in the broader context of XPath. |
@ChristianGruen "The while clause helps to interrupt operations on sequences". The most common use case is where the while clause applies a condition to items in the primary input sequence. But of course it's more general than that; in the general case it interrupts operations on the tuple stream, and may be based on conditions other than the raw input. We should add some examples of this. For example, to limit the number of items in the output:
or to process the top 3 items in sorted order
Of course it becomes even more powerful if we add the ability to "accumulate" values such as the total size of the paragraphs output so far, which is what the "with" clause is about. |
Absolutely, I agree. And a great feature of FLWOR expressions is that you can place the clauses wherever you like. That is something basic HOF functions cannot provide (they have other strengths). |
I am totally in favor of this proposal. I am against having an identically named |
Revisiting this after six months, I retain the view that this would be a useful addition to XQuery, and would prove popular in the same way that Looking at the feedback on the proposal, @dnovatchev seems to be criticizing it both because the feature is technically redundant (the requirement can be met using recursion or fold functions) and because it doesn't go far enough (in issue #80 I originally proposed two new clauses, |
We now have a proposal for subsequence-where() that provides two predicates, from and to, defining inclusive endpoints of a subsequence selection. One use case that isn't well handled by that proposal is "select all the items before the first X, or up to the end if there is no X". I'm therefore going to propose |
This proposal adds a new clause, the while clause, to FLWOR expressions. I'll start with the proposal, and then give some rationale.
Proposal
We add a new WhileClause which can appear anywhere a WhereClause can appear. The semantics are deliberately almost identical to the WhereClause.
3.12.x While Clause
[60] WhileClause ::= "while" ExprSingle
A while clause serves as a filter for the tuples in its input tuple stream. The expression in the while clause, called the while-expression, is evaluated once for each of these tuples. If the effective boolean value of the while-expression is true, the tuple is retained in the output tuple stream; otherwise the tuple and all subsequent tuples in the stream are discarded.
Examples:
This example illustrates the effect of a while clause on a tuple stream:
Input tuple stream:
while clause:
while $a > $b
Output tuple stream:
The following query illustrates how a while clause might be used to extract all items in an input sequence before the first one that fails to satisfy some condition. In this case it selects the leading
para
elements in the input sequence, stopping before the first element that is not apara
element.Note:
Although the semantics are described in terms of discarding all the tuples following the first one that fails to match the condition, a practical implementation is likely to avoid evaluating those tuples, thus giving an "early exit" from the iteration performed by the FLWOR expression.
Justification
FLWOR expressions remain the primary control construct in XQuery, despite the introduction of higher-order functions. The inability to process input selectively until some condition is true remains one of their biggest limitations. The window clause provides a workaround for some use cases, but it is very complex, and although it can partition an input sequence based on conditions found in the data, it has no direct way of stopping processing when a condition is found. Also, it only operates on input sequences, not on the tuple stream, so it isn't able to handle a composite condition involving multiple variables, for example
for $i at $p in //emp while ($i/location = 'x' and $p < 50) return $i
.That particular example could be written with a
where
clause. But if awhere
clause is used, both the human reader and the query optimiser need to do some thinking to work out that early exit is possible (once $p reaches 50, no further tuples are going to have values less than 50).The text was updated successfully, but these errors were encountered: