We recently noticed some questions on a newsgroup that attracted our attention. The question was something similar to this:
How to create an XQuery which generates SQL like
[cc lang="sql" theme="xquery"]
SELECT t1.columnName1 FROM Table t1 WHERE t1.columnName2 IN('as','fa','pr')[/cc]
My immediate reaction was: why is he worrying about that? You should't need to think about how to write an XQuery to obtain a specific SQL; it is the XQuery processor's goal to digest your XQuery and make the "best" SQL out of it...
The obvious XQuery that comes to my mind would be...
for $ts in fn:collection("Table")/Table
where $t1/columnName2 = ('as','fa','pr')
Strangely enough, experts on that newsgroup suggested to instead write the query like:
[cc lang="xquery"]for $t in Table() where $t/columnName2 = 'as' or
$t/columnName2 = 'fa' or $t/columnName2 = 'pr'
...or, assuming a sequence of values on which to filter:
[cc lang="xquery"]for $v in $values for $t in Table()
where $t/columnName2 = $v
Why would they suggest such an unnatural way to solve that problem in XQuery? The reality is that I'm thinking in terms of what DataDirect XQuery would do; while they are thinking about different XQuery processors. DataDirect XQuery has been designed to re-write XQuery expressions in SQL without forcing the user to code XQuery in a specific way. In XQuery it's possible to express the same logic in many different ways; but it shouldn't be the XQuery author's responsibility to guess about how the underlying XQuery engine optimizes what he writes; it should be the XQuery engine that is able to take the "right decisions" no matter how the user codes the solution in XQuery (at least in reasonably equivalent scenarios, like the one described above).
For the record, the XQueries described above all end up generating the same execution plan in DataDirect XQuery:
digg_skin = 'compact';
View all posts from Minollo on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
Copyright © 2019 Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.