Skip to content

Commit

Permalink
ignite-sql-tests - params fix
Browse files Browse the repository at this point in the history
  • Loading branch information
S.Vladykin committed Feb 8, 2015
1 parent 90d61e0 commit 61eb681
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 27 deletions.
Expand Up @@ -28,7 +28,7 @@
*/ */
public class GridCacheSqlQuery implements Externalizable { public class GridCacheSqlQuery implements Externalizable {
/** */ /** */
private static final Object[] EMPTY_PARAMS = {}; public static final Object[] EMPTY_PARAMS = {};


/** */ /** */
String alias; String alias;
Expand Down
Expand Up @@ -22,20 +22,13 @@
/** /**
* Abstract SQL element. * Abstract SQL element.
*/ */
public abstract class GridSqlElement implements Cloneable { public abstract class GridSqlElement implements Cloneable, Iterable<GridSqlElement> {
/** */ /** */
protected List<GridSqlElement> children = new ArrayList<>(); protected List<GridSqlElement> children = new ArrayList<>();


/** {@inheritDoc} */ /** {@inheritDoc} */
public abstract String getSQL(); public abstract String getSQL();


/**
* @return Children.
*/
public List<GridSqlElement> children() {
return children;
}

/** /**
* Clears all children. * Clears all children.
*/ */
Expand Down Expand Up @@ -101,4 +94,9 @@ public void child(int idx, GridSqlElement child) {
public int size() { public int size() {
return children.size(); return children.size();
} }

/** {@inheritDoc} */
@Override public Iterator<GridSqlElement> iterator() {
return children.iterator();
}
} }
Expand Up @@ -111,13 +111,13 @@ public GridSqlFunction setCastType(String castType) {


if (type == CAST) { if (type == CAST) {
assert !F.isEmpty(castType) : castType; assert !F.isEmpty(castType) : castType;
assert children().size() == 1; assert size() == 1;


buff.append(child().getSQL()).append(" AS ").append(castType); buff.append(child().getSQL()).append(" AS ").append(castType);
} }
else if (type == CONVERT) { else if (type == CONVERT) {
assert !F.isEmpty(castType) : castType; assert !F.isEmpty(castType) : castType;
assert children().size() == 1; assert size() == 1;


buff.append(child().getSQL()).append(',').append(castType); buff.append(child().getSQL()).append(',').append(castType);
} }
Expand Down
Expand Up @@ -61,7 +61,7 @@ public GridSqlElement rightTable() {
* @return {@code JOIN ON} condition. * @return {@code JOIN ON} condition.
*/ */
@Nullable public GridSqlElement on() { @Nullable public GridSqlElement on() {
return children.size() < 3 ? null : child(2); return size() < 3 ? null : child(2);
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
Expand All @@ -76,15 +76,8 @@ public GridSqlElement rightTable() {


GridSqlElement on = on(); GridSqlElement on = on();


if (on != null) { if (on != null)
String onSql = on.getSQL(); buff.append(" \n ON ").append(StringUtils.unEnclose(on.getSQL()));

// This is needed for parsing tests to work correctly.
if (onSql.charAt(0) == '(' && onSql.charAt(onSql.length() - 1) == ')')
onSql = onSql.substring(1, onSql.length() - 1);

buff.append(" \n ON ").append(onSql);
}


return buff.toString(); return buff.toString();
} }
Expand Down
Expand Up @@ -190,15 +190,15 @@ private static class ConditionInSqlGenerator implements SqlGenerator {


buff.append(operation.child(0).getSQL()).append(" IN("); buff.append(operation.child(0).getSQL()).append(" IN(");


assert operation.children().size() > 1; assert operation.size() > 1;


if (operation.children().size() == 2) { if (operation.size() == 2) {
String child = operation.child(1).getSQL(); String child = operation.child(1).getSQL();


buff.append(' ').append(StringUtils.unEnclose(child)).append(' '); buff.append(' ').append(StringUtils.unEnclose(child)).append(' ');
} }
else { else {
for (int i = 1; i < operation.children().size(); i++) { for (int i = 1; i < operation.size(); i++) {
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(operation.child(i).getSQL()); buff.append(operation.child(i).getSQL());
} }
Expand Down
Expand Up @@ -124,13 +124,66 @@ public static GridCacheTwoStepQuery split(Connection conn, String query, Object[
} }


// Build resulting two step query. // Build resulting two step query.
GridCacheTwoStepQuery res = new GridCacheTwoStepQuery(rdcQry.getSQL()); GridCacheTwoStepQuery res = new GridCacheTwoStepQuery(rdcQry.getSQL(),
findParams(rdcQry, params, new ArrayList<>()).toArray());


res.addMapQuery(mergeTable, mapQry.getSQL(), params); res.addMapQuery(mergeTable, mapQry.getSQL(),
findParams(mapQry, params, new ArrayList<>(params.length)).toArray());


return res; return res;
} }


/**
* @param qry Select.
* @param params Parameters.
* @param target Extracted parameters.
* @return Extracted parameters list.
*/
private static List<Object> findParams(GridSqlSelect qry, Object[] params, ArrayList<Object> target) {
for (GridSqlElement el : qry.select())
findParams(el, params, target);

findParams(qry.from(), params, target);
findParams(qry.where(), params, target);

for (GridSqlElement el : qry.groups())
findParams(el, params, target);

findParams(qry.having(), params, target);

for (GridSqlElement el : qry.sort().keySet())
findParams(el, params, target);

findParams(qry.limit(), params, target);
findParams(qry.offset(), params, target);

return target;
}

/**
* @param el Element.
* @param params Parameters.
* @param target Extracted parameters.
*/
private static void findParams(GridSqlElement el, Object[] params, ArrayList<Object> target) {
if (el == null)
return;

if (el instanceof GridSqlParameter) {
// H2 Supports queries like "select ?5" but first 4 non-existing parameters are need to be set to any value.
// Here we will set them to NULL.
int idx = ((GridSqlParameter)el).index();

while (target.size() < idx)
target.add(null);

target.add(idx, params[idx]);
}
else
for (GridSqlElement child : el)
findParams(child, params, target);
}

/** /**
* @param mapSelect Selects for map query. * @param mapSelect Selects for map query.
* @param rdcSelect Selects for reduce query. * @param rdcSelect Selects for reduce query.
Expand Down
Expand Up @@ -35,6 +35,7 @@
import static org.apache.ignite.cache.CacheAtomicityMode.*; import static org.apache.ignite.cache.CacheAtomicityMode.*;
import static org.apache.ignite.cache.CacheDistributionMode.*; import static org.apache.ignite.cache.CacheDistributionMode.*;
import static org.apache.ignite.cache.CachePreloadMode.*; import static org.apache.ignite.cache.CachePreloadMode.*;
import static org.apache.ignite.cache.query.Query.*;


/** /**
* Tests cross cache queries. * Tests cross cache queries.
Expand Down Expand Up @@ -214,6 +215,24 @@ public void testTwoStepGroupAndAggregates() throws Exception {
assertEquals(3, top); assertEquals(3, top);
} }


/**
* @throws Exception If failed.
*/
public void testApiQueries() throws Exception {
fillCaches();

IgniteCache<Object,Object> c = ignite.jcache("partitioned");

c.queryFields(sql("select cast(? as varchar) from FactPurchase").setArgs("aaa")).getAll();

List<List<?>> res = c.queryFields(sql("select cast(? as varchar), id " +
"from FactPurchase order by id limit ? offset ?").setArgs("aaa", 1, 1)).getAll();

assertEquals(1, res.size());
assertEquals("aaa", res.get(0).get(0));
assertEquals(8, res.get(0).get(1));
}

/** /**
* @param l List. * @param l List.
* @param idx Index. * @param idx Index.
Expand Down
Expand Up @@ -108,13 +108,20 @@ public class GridQueryParsingTest extends GridCommonAbstractTest {
/** /**
* *
*/ */
public void testAllExampless() throws Exception { public void testAllExamples() throws Exception {
checkQuery("select ? limit ? offset ?");

checkQuery("select cool1()"); checkQuery("select cool1()");
checkQuery("select cool1() z"); checkQuery("select cool1() z");


checkQuery("select b,a from table0('aaa', 100)"); checkQuery("select b,a from table0('aaa', 100)");
checkQuery("select * from table0('aaa', 100)"); checkQuery("select * from table0('aaa', 100)");
checkQuery("select * from table0('aaa', 100) t0"); checkQuery("select * from table0('aaa', 100) t0");
checkQuery("select x.a, y.b from table0('aaa', 100) x natural join table0('bbb', 100) y");
checkQuery("select * from table0('aaa', 100) x join table0('bbb', 100) y on x.a=y.a and x.b = 'bbb'");
checkQuery("select * from table0('aaa', 100) x left join table0('bbb', 100) y on x.a=y.a and x.b = 'bbb'");
checkQuery("select * from table0('aaa', 100) x left join table0('bbb', 100) y on x.a=y.a where x.b = 'bbb'");
checkQuery("select * from table0('aaa', 100) x left join table0('bbb', 100) y where x.b = 'bbb'");


checkQuery("select avg(old) from Person left join Address on Person.addrId = Address.id " + checkQuery("select avg(old) from Person left join Address on Person.addrId = Address.id " +
"where lower(Address.street) = lower(?)"); "where lower(Address.street) = lower(?)");
Expand Down

0 comments on commit 61eb681

Please sign in to comment.