提交 99791cc5 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #620 from stumc/Issue#619

Issue#619 CTE have wrong column names when AS newname used with simpl…
......@@ -5032,10 +5032,20 @@ public class Parser {
querySQL = StringUtils.cache(withQuery.getPlanSQL());
ArrayList<Expression> withExpressions = withQuery.getExpressions();
for (int i = 0; i < withExpressions.size(); ++i) {
String columnName = cols != null ? cols[i]
: withExpressions.get(i).getColumnName();
Expression columnExp = withExpressions.get(i);
// 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,
withExpressions.get(i).getType()));
columnExp.getType()));
}
} finally {
session.removeLocalTempTable(recursiveTable);
......
......@@ -34,6 +34,7 @@ public class TestGeneralCommonTableQueries extends TestBase {
testChainedQuery();
testParameterizedQuery();
testNumberedParameterizedQuery();
testColumnNames();
testInsert();
testUpdate();
......@@ -438,4 +439,34 @@ public class TestGeneralCommonTableQueries extends TestBase {
conn.close();
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());
conn.close();
deleteDb("commonTableExpressionQueries");
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论