diff --git a/pom.xml b/pom.xml index 39f1b3a698..a401328344 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,6 @@ com.alibaba druid 1.2.5_preview_01 - jar druid A JDBC datasource implementation. @@ -516,7 +515,7 @@ org.apache.calcite calcite-core - 1.18.0 + 1.26.0 provided @@ -657,13 +656,7 @@ org.apache.calcite calcite-druid - 1.13.0 - test - - - org.apache.calcite - calcite-example-csv - 1.13.0 + 1.26.0 test diff --git a/src/main/java/com/alibaba/druid/support/calcite/CalciteMySqlNodeVisitor.java b/src/main/java/com/alibaba/druid/support/calcite/CalciteMySqlNodeVisitor.java index 28d29a07e6..6376c978fc 100644 --- a/src/main/java/com/alibaba/druid/support/calcite/CalciteMySqlNodeVisitor.java +++ b/src/main/java/com/alibaba/druid/support/calcite/CalciteMySqlNodeVisitor.java @@ -15,6 +15,7 @@ import org.apache.calcite.sql.*; import org.apache.calcite.sql.fun.*; import org.apache.calcite.sql.parser.SqlParserPos; import org.apache.calcite.sql.parser.SqlParserUtil; +import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.util.DateString; import org.apache.calcite.util.TimeString; import org.apache.calcite.util.TimestampString; @@ -245,180 +246,7 @@ public class CalciteMySqlNodeVisitor extends MySqlASTVisitorAdapter { return visit((SQLSelectQueryBlock) x); } - public boolean visit(SQLSelectQueryBlock x) { - SqlNodeList keywordList = null; - List keywordNodes = new ArrayList(5); - int option = x.getDistionOption(); - if (option != 0) { - if (option == SQLSetQuantifier.DISTINCT - || option == SQLSetQuantifier.DISTINCTROW) { - keywordNodes.add(SqlSelectKeyword.DISTINCT.symbol(SqlParserPos.ZERO)); - } else if (option == SQLSetQuantifier.ALL) { - keywordNodes.add(SqlSelectKeyword.ALL.symbol(SqlParserPos.ZERO)); - } - - keywordList = new SqlNodeList(keywordNodes, SqlParserPos.ZERO); - } - - // select list - List columnNodes = new ArrayList(x.getSelectList().size()); - for (SQLSelectItem selectItem : x.getSelectList()) { - SqlNode column = convertToSqlNode(selectItem); - columnNodes.add(column); - } - - //select item - SqlNodeList selectList = new SqlNodeList(columnNodes, SqlParserPos.ZERO); - - //from - SqlNode from = null; - - SQLTableSource tableSource = x.getFrom(); - if (tableSource != null) { - from = convertToSqlNode(tableSource); - } - - //where - SqlNode where = convertToSqlNode(x.getWhere()); - - //order by - SqlNodeList orderBySqlNode = null; - SQLOrderBy orderBy = x.getOrderBy(); - if (orderBy != null) { - orderBySqlNode = convertOrderby(orderBy); - } - - //group by - SqlNodeList groupBySqlNode = null; - SqlNode having = null; - SQLSelectGroupByClause groupBys = x.getGroupBy(); - - if (groupBys != null) { - if (groupBys.getHaving() != null) { - having = convertToSqlNode(groupBys.getHaving()); - } - - if (groupBys.getItems().size() > 0) { - List groupByNodes = new ArrayList(groupBys.getItems().size()); - - for (SQLExpr groupBy : groupBys.getItems()) { - SqlNode groupByNode = convertToSqlNode(groupBy); - groupByNodes.add(groupByNode); - } - groupBySqlNode = new SqlNodeList(groupByNodes, SqlParserPos.ZERO); - } - - SqlInternalOperator op = null; - if (groupBys.isWithRollUp()) { - op = SqlStdOperatorTable.ROLLUP; - } else if (groupBys.isWithCube()) { - op = SqlStdOperatorTable.CUBE; - } - - if (op != null) { - List rollupNodes = new ArrayList(1); - - boolean isRow = false; - for (SqlNode node : groupBySqlNode.getList()) { - if (node instanceof SqlBasicCall && ((SqlBasicCall) node).getOperator() == SqlStdOperatorTable.ROW) { - isRow = true; - break; - } - } - - if (isRow) { - rollupNodes.add(op.createCall(SqlParserPos.ZERO, groupBySqlNode.toArray())); - groupBySqlNode = new SqlNodeList(rollupNodes, SqlParserPos.ZERO); - } else { - rollupNodes.add(op.createCall(SqlParserPos.ZERO, groupBySqlNode)); - groupBySqlNode = new SqlNodeList(rollupNodes, SqlParserPos.ZERO); - } - - } - } - - //limit - SqlNode offset = null; - SqlNode fetch = null; - SQLLimit limit = x.getLimit(); - if (limit != null) { - offset = convertToSqlNode(limit.getOffset()); - fetch = convertToSqlNode(limit.getRowCount()); - } - - //hints - SqlNodeList hints = convertHints(x.getHints()); - - if (orderBy != null && x.getParent() instanceof SQLUnionQuery) { - this.sqlNode = new com.alibaba.druid.support.calcite.TDDLSqlSelect(SqlParserPos.ZERO - , keywordList - , selectList - , from - , where - , groupBySqlNode - , having - , null - , null - , offset - , fetch - , hints - , null - ); - sqlNode = new SqlOrderBy(SqlParserPos.ZERO - , sqlNode - , orderBySqlNode - , null - , fetch - ); - } else { - if (orderBySqlNode == null) { - orderBySqlNode = SqlNodeList.EMPTY; - } - if (hints == null || SqlNodeList.isEmptyList(hints)) { - this.sqlNode = new SqlSelect(SqlParserPos.ZERO - , keywordList - , selectList - , from - , where - , groupBySqlNode - , having - , null - , SqlNodeList.EMPTY - , null - , null - ); - - if ((!SqlNodeList.isEmptyList(orderBySqlNode)) - || offset != null - || fetch != null - ) { - sqlNode = new SqlOrderBy(SqlParserPos.ZERO - , sqlNode - , orderBySqlNode - , offset - , fetch); - } - } else { - this.sqlNode = new com.alibaba.druid.support.calcite.TDDLSqlSelect(SqlParserPos.ZERO - , keywordList - , selectList - , from - , where - , groupBySqlNode - , having - , null - , orderBySqlNode - , offset - , fetch - , hints - , null - ); - } - } - - - return false; - } + private static Map nameHashCode64SqlTypeNameMapping = new HashMap<>(); public boolean visit(SQLTableSource x) { Class clazz = x.getClass(); @@ -622,6 +450,218 @@ public class CalciteMySqlNodeVisitor extends MySqlASTVisitorAdapter { return false; } + static { + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.BIT, SqlTypeName.BOOLEAN); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.BOOLEAN, SqlTypeName.BOOLEAN); + + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.TINYINT, SqlTypeName.TINYINT); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.SMALLINT, SqlTypeName.SMALLINT); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.INT, SqlTypeName.INTEGER); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.INTEGER, SqlTypeName.INTEGER); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.BIGINT, SqlTypeName.BIGINT); + + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.DECIMAL, SqlTypeName.DECIMAL); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.FLOAT, SqlTypeName.FLOAT); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.REAL, SqlTypeName.REAL); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.DOUBLE, SqlTypeName.DOUBLE); + + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.DATE, SqlTypeName.DATE); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.TIME, SqlTypeName.TIME); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.TIMESTAMP, SqlTypeName.TIMESTAMP); + + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.CHAR, SqlTypeName.CHAR); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.VARCHAR, SqlTypeName.VARCHAR); + + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.BINARY, SqlTypeName.BINARY); + nameHashCode64SqlTypeNameMapping.put(FnvHash.Constants.VARBINARY, SqlTypeName.VARBINARY); + } + + public boolean visit(SQLSelectQueryBlock x) { + SqlNodeList keywordList = null; + List keywordNodes = new ArrayList(5); + int option = x.getDistionOption(); + if (option != 0) { + if (option == SQLSetQuantifier.DISTINCT + || option == SQLSetQuantifier.DISTINCTROW) { + keywordNodes.add(SqlSelectKeyword.DISTINCT.symbol(SqlParserPos.ZERO)); + } else if (option == SQLSetQuantifier.ALL) { + keywordNodes.add(SqlSelectKeyword.ALL.symbol(SqlParserPos.ZERO)); + } + + keywordList = new SqlNodeList(keywordNodes, SqlParserPos.ZERO); + } + + // select list + List columnNodes = new ArrayList(x.getSelectList().size()); + for (SQLSelectItem selectItem : x.getSelectList()) { + SqlNode column = convertToSqlNode(selectItem); + columnNodes.add(column); + } + + //select item + SqlNodeList selectList = new SqlNodeList(columnNodes, SqlParserPos.ZERO); + + //from + SqlNode from = null; + + SQLTableSource tableSource = x.getFrom(); + if (tableSource != null) { + from = convertToSqlNode(tableSource); + } + + //where + SqlNode where = convertToSqlNode(x.getWhere()); + + //order by + SqlNodeList orderBySqlNode = null; + SQLOrderBy orderBy = x.getOrderBy(); + if (orderBy != null) { + orderBySqlNode = convertOrderby(orderBy); + } + + //group by + SqlNodeList groupBySqlNode = null; + SqlNode having = null; + SQLSelectGroupByClause groupBys = x.getGroupBy(); + + if (groupBys != null) { + if (groupBys.getHaving() != null) { + having = convertToSqlNode(groupBys.getHaving()); + } + + if (groupBys.getItems().size() > 0) { + List groupByNodes = new ArrayList(groupBys.getItems().size()); + + for (SQLExpr groupBy : groupBys.getItems()) { + SqlNode groupByNode = convertToSqlNode(groupBy); + groupByNodes.add(groupByNode); + } + groupBySqlNode = new SqlNodeList(groupByNodes, SqlParserPos.ZERO); + } + + SqlInternalOperator op = null; + if (groupBys.isWithRollUp()) { + op = SqlStdOperatorTable.ROLLUP; + } else if (groupBys.isWithCube()) { + op = SqlStdOperatorTable.CUBE; + } + + if (op != null) { + List rollupNodes = new ArrayList(1); + + boolean isRow = false; + for (SqlNode node : groupBySqlNode.getList()) { + if (node instanceof SqlBasicCall && ((SqlBasicCall) node).getOperator() == SqlStdOperatorTable.ROW) { + isRow = true; + break; + } + } + + if (isRow) { + rollupNodes.add(op.createCall(SqlParserPos.ZERO, groupBySqlNode.toArray())); + groupBySqlNode = new SqlNodeList(rollupNodes, SqlParserPos.ZERO); + } else { + rollupNodes.add(op.createCall(SqlParserPos.ZERO, groupBySqlNode)); + groupBySqlNode = new SqlNodeList(rollupNodes, SqlParserPos.ZERO); + } + + } + } + + //limit + SqlNode offset = null; + SqlNode fetch = null; + SQLLimit limit = x.getLimit(); + if (limit != null) { + offset = convertToSqlNode(limit.getOffset()); + fetch = convertToSqlNode(limit.getRowCount()); + } + + //hints + SqlNodeList hints = convertHints(x.getHints()); + + if (orderBy != null && x.getParent() instanceof SQLUnionQuery) { + this.sqlNode = new com.alibaba.druid.support.calcite.TDDLSqlSelect(SqlParserPos.ZERO + , keywordList + , selectList + , from + , where + , groupBySqlNode + , having + , null + , null + , offset + , fetch + , hints + , null + ); + sqlNode = new SqlOrderBy(SqlParserPos.ZERO + , sqlNode + , orderBySqlNode + , null + , fetch + ); + } else { + if (orderBySqlNode == null) { + orderBySqlNode = SqlNodeList.EMPTY; + } + if (hints == null || SqlNodeList.isEmptyList(hints)) { + this.sqlNode = new SqlSelect(SqlParserPos.ZERO + , keywordList + , selectList + , from + , where + , groupBySqlNode + , having + , null + , SqlNodeList.EMPTY + , null + , null + , null + ); + + if ((!SqlNodeList.isEmptyList(orderBySqlNode)) + || offset != null + || fetch != null + ) { + sqlNode = new SqlOrderBy(SqlParserPos.ZERO + , sqlNode + , orderBySqlNode + , offset + , fetch); + } + } else { + this.sqlNode = new com.alibaba.druid.support.calcite.TDDLSqlSelect(SqlParserPos.ZERO + , keywordList + , selectList + , from + , where + , groupBySqlNode + , having + , null + , orderBySqlNode + , offset + , fetch + , hints + , null + ); + } + } + + + return false; + } + + private SqlTypeName toSqlTypeName(SQLDataType dataType) { + long nameHashCode64 = dataType.nameHashCode64(); + SqlTypeName sqlTypeName = nameHashCode64SqlTypeNameMapping.get(nameHashCode64); + if (sqlTypeName != null) { + return sqlTypeName; + } + + throw new FastsqlException("TODO"); + } + public boolean visit(SQLCastExpr x) { SqlLiteral functionQualifier = null; @@ -649,9 +689,10 @@ public class CalciteMySqlNodeVisitor extends MySqlASTVisitorAdapter { } } - SqlDataTypeSpec sqlDataTypeSpec - = new SqlDataTypeSpec(dataTypeNode, scale, precision, null, null, SqlParserPos.ZERO); + = new SqlDataTypeSpec( + new SqlBasicTypeNameSpec(toSqlTypeName(dataType), scale, precision, SqlParserPos.ZERO) + , SqlParserPos.ZERO); SqlOperator sqlOperator = new SqlCastFunction(); diff --git a/src/main/java/com/alibaba/druid/support/calcite/TDDLSqlSelect.java b/src/main/java/com/alibaba/druid/support/calcite/TDDLSqlSelect.java index 94afe9f383..b6275af706 100644 --- a/src/main/java/com/alibaba/druid/support/calcite/TDDLSqlSelect.java +++ b/src/main/java/com/alibaba/druid/support/calcite/TDDLSqlSelect.java @@ -18,7 +18,7 @@ public class TDDLSqlSelect extends SqlSelect { public TDDLSqlSelect(SqlParserPos pos, SqlNodeList keywordList, SqlNodeList selectList, SqlNode from, SqlNode where, SqlNodeList groupBy, SqlNode having, SqlNodeList windowDecls, SqlNodeList orderBy, SqlNode offset, SqlNode fetch, SqlNodeList hints, SqlNodeList headHints) { - super(pos, keywordList, selectList, from, where, groupBy, having, windowDecls, orderBy, offset, fetch); + super(pos, keywordList, selectList, from, where, groupBy, having, windowDecls, orderBy, offset, fetch, null); this.hints = hints; this.headHints = headHints; }