use of org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter in project jackrabbit-oak by apache.
the class LargeQueryTest method testRandomizedCondition.
private void testRandomizedCondition(int seed) throws ParseException {
Random r = new Random(seed);
StringBuilder buff = new StringBuilder("//*[");
buff.append(randomCondition(r));
buff.append("]");
String xpath = buff.toString();
XPathToSQL2Converter conv = new XPathToSQL2Converter();
String sql2 = conv.convert(xpath);
int xpathIndex = sql2.lastIndexOf(" /* xpath: ");
sql2 = sql2.substring(0, xpathIndex);
// should use union now
assertTrue(sql2.indexOf(" or ") < 0);
SQL2Parser p = new SQL2Parser(null, nodeTypes, new QueryEngineSettings());
p.parse(sql2);
}
use of org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter in project jackrabbit-oak by apache.
the class XPathTest method verify.
private void verify(String xpath, String expectedSql2) throws ParseException {
String sql2 = new XPathToSQL2Converter().convert(xpath);
sql2 = formatSQL(sql2);
expectedSql2 = formatSQL(expectedSql2);
assertEquals(expectedSql2, sql2);
SQL2Parser p = new SQL2Parser(null, nodeTypes, new QueryEngineSettings());
p.parse(sql2);
}
use of org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter in project jackrabbit-oak by apache.
the class QueryEngineImpl method parseQuery.
/**
* Parse the query.
*
* @param statement the statement
* @param language the language
* @param context the context
* @param mappings the mappings
* @return the list of queries, where the first is the original, and all
* others are alternatives (for example, a "union" query)
*/
private static List<Query> parseQuery(String statement, String language, ExecutionContext context, Map<String, String> mappings) throws ParseException {
boolean isInternal = SQL2Parser.isInternal(statement);
if (isInternal) {
LOG.trace("Parsing {} statement: {}", language, statement);
} else {
LOG.debug("Parsing {} statement: {}", language, statement);
}
NamePathMapper mapper = new NamePathMapperImpl(new LocalNameMapper(context.getRoot(), mappings));
NodeTypeInfoProvider nodeTypes = context.getNodeTypeInfoProvider();
QueryEngineSettings settings = context.getSettings();
SQL2Parser parser = new SQL2Parser(mapper, nodeTypes, settings);
if (language.endsWith(NO_LITERALS)) {
language = language.substring(0, language.length() - NO_LITERALS.length());
parser.setAllowNumberLiterals(false);
parser.setAllowTextLiterals(false);
}
ArrayList<Query> queries = new ArrayList<Query>();
Query q;
if (SQL2.equals(language) || JQOM.equals(language)) {
q = parser.parse(statement, false);
} else if (SQL.equals(language)) {
parser.setSupportSQL1(true);
q = parser.parse(statement, false);
} else if (XPATH.equals(language)) {
XPathToSQL2Converter converter = new XPathToSQL2Converter();
String sql2 = converter.convert(statement);
LOG.debug("XPath > SQL2: {}", sql2);
try {
// OAK-874: No artificial XPath selector name in wildcards
parser.setIncludeSelectorNameInWildcardColumns(false);
q = parser.parse(sql2, false);
} catch (ParseException e) {
ParseException e2 = new ParseException(statement + " converted to SQL-2 " + e.getMessage(), 0);
e2.initCause(e);
throw e2;
}
} else {
throw new ParseException("Unsupported language: " + language, 0);
}
queries.add(q);
if (settings.isSql2Optimisation()) {
if (q.isInternal()) {
LOG.trace("Skipping optimisation as internal query.");
} else {
LOG.trace("Attempting optimisation");
Query q2 = q.buildAlternativeQuery();
if (q2 != q) {
LOG.debug("Alternative query available: {}", q2);
queries.add(q2);
}
}
}
// initialising all the queries.
for (Query query : queries) {
try {
query.init();
} catch (Exception e) {
ParseException e2 = new ParseException(query.getStatement() + ": " + e.getMessage(), 0);
e2.initCause(e);
throw e2;
}
}
return queries;
}
use of org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter in project jackrabbit-oak by apache.
the class AbstractQueryTest method test.
protected void test(String file) throws Exception {
String className = getClass().getName();
String shortClassName = className.replaceAll("org.apache.jackrabbit.oak.plugins.index.", "oajopi.");
// OAK-3252 getting the input/output paths for better error reporting. Still using the
// stream for input as other projects uses dependencies on sql2.txt of oak-core and it fails
// resolving the whole path on disk
File input = new File(AbstractQueryTest.class.getResource(file).getPath());
File output = new File("target/" + shortClassName + "_" + file);
InputStream in = AbstractQueryTest.class.getResourceAsStream(file);
ContinueLineReader r = new ContinueLineReader(new LineNumberReader(new InputStreamReader(in)));
PrintWriter w = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output)));
HashSet<String> knownQueries = new HashSet<String>();
boolean errors = false;
try {
while (true) {
String line = r.readLine();
if (line == null) {
break;
}
line = line.trim();
if (line.startsWith("#") || line.length() == 0) {
w.println(line);
} else if (line.startsWith("xpath2sql")) {
line = line.substring("xpath2sql".length()).trim();
w.println("xpath2sql " + line);
XPathToSQL2Converter c = new XPathToSQL2Converter();
String got;
try {
got = c.convert(line);
} catch (ParseException e) {
got = "invalid: " + e.getMessage().replace('\n', ' ');
} catch (Exception e) {
// e.printStackTrace();
got = "error: " + e.toString().replace('\n', ' ');
}
String formatted = formatSQL(got);
if (!knownQueries.add(line)) {
got = "duplicate xpath2sql query";
}
line = r.readLine().trim();
w.println(formatted);
if (!line.equals(got) && !line.equals(formatted)) {
errors = true;
}
} else if (line.startsWith("select") || line.startsWith("explain") || line.startsWith("measure") || line.startsWith("sql1") || line.startsWith("xpath")) {
w.println(line);
String language = QueryEngineImpl.SQL2;
if (line.startsWith("sql1 ")) {
language = QueryEngineImpl.SQL;
line = line.substring("sql1 ".length());
} else if (line.startsWith("xpath ")) {
language = QueryEngineImpl.XPATH;
line = line.substring("xpath ".length());
}
boolean readEnd = true;
for (String resultLine : executeQuery(line, language)) {
w.println(resultLine);
if (readEnd) {
line = r.readLine();
if (line == null) {
errors = true;
readEnd = false;
} else {
line = line.trim();
if (line.length() == 0) {
errors = true;
readEnd = false;
} else {
if (!line.equals(resultLine)) {
errors = true;
}
}
}
}
}
w.println("");
if (readEnd) {
while (true) {
line = r.readLine();
if (line == null) {
break;
}
line = line.trim();
if (line.length() == 0) {
break;
}
errors = true;
}
}
} else if (line.startsWith("commit")) {
w.println(line);
line = line.substring("commit".length()).trim();
apply(root, line);
root.commit();
}
w.flush();
}
} finally {
w.close();
r.close();
}
if (errors) {
RandomAccessFile f = new RandomAccessFile(output, "r");
byte[] data = new byte[(int) f.length()];
f.readFully(data);
f.close();
throw new Exception("Results in " + output.getPath() + " don't match expected " + "results in " + input.getPath() + "; compare the files for details; got=\n" + new String(data, "UTF-8"));
}
}
use of org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter in project jackrabbit-oak by apache.
the class FilterTest method createFilter.
private Filter createFilter(String xpath) throws ParseException {
String sql = new XPathToSQL2Converter().convert(xpath);
QueryImpl q = (QueryImpl) p.parse(sql);
return q.createFilter(true);
}
Aggregations