提交 2797ce7b authored 作者: Owner's avatar Owner

Issue#619 CTE have wrong column names when AS newname used with simple column expression

上级 d8eaa1f6
...@@ -5032,10 +5032,20 @@ public class Parser { ...@@ -5032,10 +5032,20 @@ public class Parser {
querySQL = StringUtils.cache(withQuery.getPlanSQL()); querySQL = StringUtils.cache(withQuery.getPlanSQL());
ArrayList<Expression> withExpressions = withQuery.getExpressions(); ArrayList<Expression> withExpressions = withQuery.getExpressions();
for (int i = 0; i < withExpressions.size(); ++i) { for (int i = 0; i < withExpressions.size(); ++i) {
String columnName = cols != null ? cols[i] Expression columnExp = withExpressions.get(i);
: withExpressions.get(i).getColumnName(); // use the passed in column name if supplied, otherwise use alias (if used) otherwise use column name
// derived from column expression
String columnName;
if (cols != null){
columnName = cols[i];
} else if (columnExp.getAlias()!=null){
columnName = columnExp.getAlias();
}
else{
columnName = columnExp.getColumnName();
}
columnTemplateList.add(new Column(columnName, columnTemplateList.add(new Column(columnName,
withExpressions.get(i).getType())); columnExp.getType()));
} }
} finally { } finally {
session.removeLocalTempTable(recursiveTable); session.removeLocalTempTable(recursiveTable);
......
...@@ -34,6 +34,7 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -34,6 +34,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
testChainedQuery(); testChainedQuery();
testParameterizedQuery(); testParameterizedQuery();
testNumberedParameterizedQuery(); testNumberedParameterizedQuery();
testColumnNames();
testInsert(); testInsert();
testUpdate(); testUpdate();
...@@ -438,4 +439,46 @@ public class TestGeneralCommonTableQueries extends TestBase { ...@@ -438,4 +439,46 @@ public class TestGeneralCommonTableQueries extends TestBase {
conn.close(); conn.close();
deleteDb("commonTableExpressionQueries"); deleteDb("commonTableExpressionQueries");
} }
private void testColumnNames() throws Exception {
deleteDb("commonTableExpressionQueries");
Connection conn = getConnection("commonTableExpressionQueries");
PreparedStatement prep;
ResultSet rs;
conn.setAutoCommit(false);
prep = conn.prepareStatement("WITH t1 AS ("
+" SELECT 1 AS ONE, R.X AS TWO, 'T1' AS THREE, X FROM SYSTEM_RANGE(1,1) R"
+")"
+"SELECT * FROM t1");
rs = prep.executeQuery();
for (int n : new int[] { 1 }) {
assertTrue(rs.next());
assertEquals(n, rs.getInt(1));
assertEquals(n, rs.getInt(4));
}
assertEquals("ONE",rs.getMetaData().getColumnLabel(1));
assertEquals("TWO",rs.getMetaData().getColumnLabel(2));
assertEquals("THREE",rs.getMetaData().getColumnLabel(3));
assertEquals("X",rs.getMetaData().getColumnLabel(4));
assertFalse(rs.next());
try{
prep = conn.prepareStatement("SELECT * FROM t1 UNION ALL SELECT * FROM t2 "+
"UNION ALL SELECT X, 'Q' FROM SYSTEM_RANGE(5,6)");
rs = prep.executeQuery();
fail("Temp view T1 was accessible after previous WITH statement finished "+
"- but should not have been.");
}
catch(JdbcSQLException e){
// ensure the T1 table has been removed even without auto commit
assertContains(e.getMessage(),"Table \"T1\" not found;");
}
conn.close();
deleteDb("commonTableExpressionQueries");
}
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论