提交 899022c9 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #443 from alexpaschenko/opt-cmp

Optimized column vs constant comparisons
...@@ -194,6 +194,13 @@ public class Comparison extends Condition { ...@@ -194,6 +194,13 @@ public class Comparison extends Condition {
return ValueExpression.getNull(); return ValueExpression.getNull();
} }
} }
int colType = left.getType();
int constType = r.getType();
int resType = Value.getHigherOrder(colType, constType);
// If not the column values will need to be promoted
// to constant type, but vise versa, then let's do this here once.
if (constType != resType)
right = ValueExpression.get(r.convertTo(resType));
} else if (right instanceof Parameter) { } else if (right instanceof Parameter) {
((Parameter) right).setColumn( ((Parameter) right).setColumn(
((ExpressionColumn) left).getColumn()); ((ExpressionColumn) left).getColumn());
......
...@@ -58,6 +58,7 @@ public class TestOptimizations extends TestBase { ...@@ -58,6 +58,7 @@ public class TestOptimizations extends TestBase {
testNestedIn(); testNestedIn();
testConstantIn1(); testConstantIn1();
testConstantIn2(); testConstantIn2();
testConstantTypeConversionToColumnType();
testNestedInSelectAndLike(); testNestedInSelectAndLike();
testNestedInSelect(); testNestedInSelect();
testInSelectJoin(); testInSelectJoin();
...@@ -463,6 +464,25 @@ public class TestOptimizations extends TestBase { ...@@ -463,6 +464,25 @@ public class TestOptimizations extends TestBase {
conn.close(); conn.close();
} }
private void testConstantTypeConversionToColumnType() throws SQLException {
deleteDb("optimizations");
Connection conn = getConnection("optimizations;IGNORECASE=TRUE");
Statement stat = conn.createStatement();
stat.executeUpdate("CREATE TABLE test (x int)");
ResultSet resultSet;
resultSet = stat.executeQuery(
"EXPLAIN SELECT x FROM test WHERE x = '5'");
assertTrue(resultSet.next());
// String constant '5' has been converted to int constant 5 on optimization
assertTrue(resultSet.getString(1).endsWith("X = 5"));
stat.execute("drop table test");
conn.close();
}
private void testNestedInSelect() throws SQLException { private void testNestedInSelect() throws SQLException {
deleteDb("optimizations"); deleteDb("optimizations");
Connection conn = getConnection("optimizations"); Connection conn = getConnection("optimizations");
......
...@@ -8172,9 +8172,9 @@ select * from s; ...@@ -8172,9 +8172,9 @@ select * from s;
> rows: 1 > rows: 1
select some(y>10), every(y>10), min(y), max(y) from t; select some(y>10), every(y>10), min(y), max(y) from t;
> BOOL_OR(Y > 10) BOOL_AND(Y > 10) MIN(Y) MAX(Y) > BOOL_OR(Y > 10.0) BOOL_AND(Y > 10.0) MIN(Y) MAX(Y)
> --------------- ---------------- ------ ------ > ----------------- ------------------ ------ ------
> null null null null > null null null null
> rows: 1 > rows: 1
insert into t values(1000000004, 4); insert into t values(1000000004, 4);
...@@ -8230,9 +8230,9 @@ stddev_pop(distinct y) s_py, stddev_samp(distinct y) s_sy, var_pop(distinct y) v ...@@ -8230,9 +8230,9 @@ stddev_pop(distinct y) s_py, stddev_samp(distinct y) s_sy, var_pop(distinct y) v
> rows: 1 > rows: 1
select some(y>10), every(y>10), min(y), max(y) from t; select some(y>10), every(y>10), min(y), max(y) from t;
> BOOL_OR(Y > 10) BOOL_AND(Y > 10) MIN(Y) MAX(Y) > BOOL_OR(Y > 10.0) BOOL_AND(Y > 10.0) MIN(Y) MAX(Y)
> --------------- ---------------- ------ ------ > ----------------- ------------------ ------ ------
> TRUE FALSE 4.0 16.0 > TRUE FALSE 4.0 16.0
> rows: 1 > rows: 1
drop view s; drop view s;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论