未验证 提交 9ccc03d7 编写于 作者: J jaugsburger 提交者: GitHub

fix(griffin) - problem with asof join with where clause. also dangling where...

fix(griffin) - problem with asof join with where clause. also dangling where statement fix. Fixed #307 (#326)
上级 9828d735
......@@ -65,6 +65,7 @@ class SqlOptimiser {
private final ObjList<JoinContext> joinClausesSwap1 = new ObjList<>();
private final ObjList<JoinContext> joinClausesSwap2 = new ObjList<>();
private final IntList tempCrosses = new IntList();
private final IntList tempList = new IntList();
private final LiteralCollector literalCollector = new LiteralCollector();
private final IntHashSet tablesSoFar = new IntHashSet();
private final IntHashSet postFilterRemoved = new IntHashSet();
......@@ -1425,12 +1426,27 @@ class SqlOptimiser {
literalCollector.resetNullCount();
traversalAlgo.traverse(node, literalCollector.lhs());
tempList.clear();
for (int j = 0; j < literalCollectorAIndexes.size(); j++) {
int tableExpressionReference = literalCollectorAIndexes.getQuick(j);
int position = tempList.binarySearch(tableExpressionReference);
if (position < 0) {
tempList.add(-(position + 1), tableExpressionReference);
}
}
int distinctIndexes = tempList.size();
// at this point we must not have constant conditions in where clause
// this could be either referencing constant of a sub-query
if (literalCollectorAIndexes.size() == 0) {
// keep condition with this model
addWhereNode(model, node);
continue;
} else if (distinctIndexes > 1) {
int greatest = tempList.get(distinctIndexes - 1);
model.setPostJoinWhereClause(concatFilters(model.getPostJoinWhereClause(), nodes.getQuick(greatest)));
continue;
}
// by now all where clause must reference single table only and all column references have to be valid
......
......@@ -717,8 +717,13 @@ public final class SqlParser {
// expect [where]
if (tok != null && isWhereKeyword(tok)) {
model.setWhereClause(expr(lexer, model));
tok = optTok(lexer);
ExpressionNode expr = expr(lexer, model);
if (expr != null) {
model.setWhereClause(expr);
tok = optTok(lexer);
} else {
throw SqlException.$((lexer.lastTokenPosition()), "empty where clause");
}
}
// expect [group by]
......
......@@ -37,7 +37,7 @@ public class AsOfJoinTest extends AbstractGriffinTest {
}
@Test
public void testAsofJoinFOrSelectWithoutTimestamp() throws Exception {
public void testAsofJoinForSelectWithoutTimestamp() throws Exception {
final String expected = "tag\thi\tlo\n" +
"AA\t315515118\t315515118\n" +
"BB\t-727724771\t-727724771\n" +
......@@ -101,4 +101,56 @@ public class AsOfJoinTest extends AbstractGriffinTest {
false
);
}
@Test
public void testAsofJoinForSelectWithoutTimestampAndWithWhereStatement() throws Exception {
final String expected = "tag\thi\tlo\n" +
"AA\t315515118\t315515118\n" +
"BB\t-727724771\t-727724771\n" +
"CC\t-948263339\t-948263339\n" +
"CC\t592859671\t592859671\n" +
"AA\t-847531048\t-847531048\n" +
"BB\t-2041844972\t-2041844972\n" +
"BB\t-1575378703\t-1575378703\n" +
"BB\t1545253512\t1545253512\n" +
"AA\t1573662097\t1573662097\n" +
"AA\t339631474\t339631474\n";
assertQuery(
"tag\thi\tlo\n",
"(select a.tag, a.seq hi, b.seq lo from tab a asof join tab b on (tag)) where hi > lo + 1",
"create table tab (\n" +
" tag symbol index,\n" +
" seq int,\n" +
" ts timestamp\n" +
") timestamp(ts) partition by DAY",
null,
"insert into tab select * from (select rnd_symbol('AA', 'BB', 'CC') tag, \n" +
" rnd_int() seq, \n" +
" timestamp_sequence(172800000000, 360000000) ts \n" +
" from long_sequence(10)) timestamp (ts)",
expected,
false
);
}
@Test
public void testAsofJoinForSelectWithoutTimestampAndWithWhereStatementV2() throws Exception {
final String expected = "tag\thi\tlo\n";
assertQuery(
"tag\thi\tlo\n",
"select a.tag, a.seq hi, b.seq lo from tab a asof join tab b on (tag) where b.seq < a.seq",
"create table tab (\n" +
" tag symbol index,\n" +
" seq int,\n" +
" ts timestamp\n" +
") timestamp(ts) partition by DAY",
null,
"insert into tab select * from (select rnd_symbol('AA', 'BB', 'CC') tag, \n" +
" rnd_int() seq, \n" +
" timestamp_sequence(172800000000, 360000000) ts \n" +
" from long_sequence(10)) timestamp (ts)",
expected,
false
);
}
}
......@@ -4863,6 +4863,36 @@ public class SqlParserTest extends AbstractGriffinTest {
);
}
@Test
public void testFilterPostJoin() throws SqlException {
assertQuery(
"select-choose a.tag tag, a.seq hi, b.seq lo from (select [tag, seq] from tab a asof join select [seq, tag] from tab b on b.tag = a.tag post-join-where b.seq < a.seq)",
"select a.tag, a.seq hi, b.seq lo from tab a asof join tab b on (tag) where b.seq < a.seq",
modelOf("tab").col("tag", ColumnType.STRING).col("seq", ColumnType.LONG)
);
}
@Test
public void testFilterPostJoinSubQuery() throws SqlException {
assertQuery(
"select-choose tag, hi, lo from ((select-choose [a.tag tag, a.seq hi, b.seq lo] a.tag tag, a.seq hi, b.seq lo from (select [tag, seq] from tab a asof join select [seq, tag] from tab b on b.tag = a.tag)) _xQdbA1)",
"(select a.tag, a.seq hi, b.seq lo from tab a asof join tab b on (tag)) where hi > lo + 1",
modelOf("tab").col("tag", ColumnType.STRING).col("seq", ColumnType.LONG)
);
}
@Test
public void testEmptyWhere() throws Exception {
assertFailure(
"(select a.tag, a.seq hi, b.seq lo from tab a asof join tab b on (tag)) where",
"create table tab (\n" +
" tag string,\n" +
" seq long\n" +
") partition by NONE",
71,
"empty where clause"
);
}
private static void assertSyntaxError(
SqlCompiler compiler,
String query,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册