Skip to content

Commit e535b58

Browse files
authored
fix Having Filter parameters are not normalized (#127)
1 parent a1886b3 commit e535b58

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

src/main/java/com/jd/jdbc/planbuilder/AbstractRoutePlan.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.jd.jdbc.sqlparser.ast.SQLOrderBy;
2727
import com.jd.jdbc.sqlparser.ast.SQLSetQuantifier;
2828
import com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOpExpr;
29+
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.NotEqual;
2930
import com.jd.jdbc.sqlparser.ast.expr.SQLIdentifierExpr;
3031
import com.jd.jdbc.sqlparser.ast.expr.SQLInListExpr;
3132
import com.jd.jdbc.sqlparser.ast.expr.SQLIntegerExpr;
@@ -55,8 +56,6 @@
5556
import lombok.Getter;
5657
import lombok.Setter;
5758

58-
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.NotEqual;
59-
6059
@Getter
6160
@Setter
6261
public abstract class AbstractRoutePlan implements Builder {
@@ -223,6 +222,12 @@ public void pushGroupBy(SQLSelectGroupByClause groupBy) {
223222
for (SQLExpr item : groupBy.getItems()) {
224223
newGroupBy.addItem(item);
225224
}
225+
if (groupBy.isWithCube()) {
226+
newGroupBy.setWithCube(true);
227+
}
228+
if (groupBy.isWithRollUp()) {
229+
newGroupBy.setWithRollUp(true);
230+
}
226231
((MySqlSelectQueryBlock) this.select).setGroupBy(newGroupBy);
227232
}
228233

src/main/java/com/jd/jdbc/planbuilder/PrimitiveBuilder.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
import com.google.protobuf.UnknownFieldSet;
2222
import com.jd.jdbc.VSchemaManager;
2323
import com.jd.jdbc.engine.Engine;
24+
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutExists;
25+
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutIn;
26+
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutNotIn;
27+
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutValue;
28+
import static com.jd.jdbc.engine.Engine.RouteOpcode.SelectDBA;
29+
import static com.jd.jdbc.engine.Engine.RouteOpcode.SelectReference;
2430
import com.jd.jdbc.engine.OrderedAggregateEngine;
2531
import com.jd.jdbc.engine.RouteEngine;
2632
import com.jd.jdbc.engine.table.TableRouteEngine;
@@ -32,13 +38,23 @@
3238
import com.jd.jdbc.planbuilder.tableplan.TableRoutePlan;
3339
import com.jd.jdbc.sqlparser.SQLUtils;
3440
import com.jd.jdbc.sqlparser.SqlParser;
41+
import static com.jd.jdbc.sqlparser.SqlParser.GroupByExpr.ColName;
42+
import static com.jd.jdbc.sqlparser.SqlParser.GroupByExpr.Literal;
43+
import static com.jd.jdbc.sqlparser.SqlParser.HAVING_STR;
44+
import static com.jd.jdbc.sqlparser.SqlParser.SelectExpr.AliasedExpr;
45+
import static com.jd.jdbc.sqlparser.SqlParser.SelectExpr.Nextval;
46+
import static com.jd.jdbc.sqlparser.SqlParser.SelectExpr.StarExpr;
47+
import static com.jd.jdbc.sqlparser.SqlParser.WHERE_STR;
3548
import com.jd.jdbc.sqlparser.ast.SQLExpr;
3649
import com.jd.jdbc.sqlparser.ast.SQLLimit;
3750
import com.jd.jdbc.sqlparser.ast.SQLObject;
3851
import com.jd.jdbc.sqlparser.ast.SQLOrderBy;
3952
import com.jd.jdbc.sqlparser.ast.SQLSetQuantifier;
4053
import com.jd.jdbc.sqlparser.ast.expr.SQLAllColumnExpr;
4154
import com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOpExpr;
55+
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.BooleanAnd;
56+
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.BooleanOr;
57+
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.Equality;
4258
import com.jd.jdbc.sqlparser.ast.expr.SQLExistsExpr;
4359
import com.jd.jdbc.sqlparser.ast.expr.SQLIdentifierExpr;
4460
import com.jd.jdbc.sqlparser.ast.expr.SQLInSubQueryExpr;
@@ -48,6 +64,7 @@
4864
import com.jd.jdbc.sqlparser.ast.expr.SQLVariantRefListExpr;
4965
import com.jd.jdbc.sqlparser.ast.statement.SQLExprTableSource;
5066
import com.jd.jdbc.sqlparser.ast.statement.SQLJoinTableSource;
67+
import static com.jd.jdbc.sqlparser.ast.statement.SQLJoinTableSource.JoinType;
5168
import com.jd.jdbc.sqlparser.ast.statement.SQLSelect;
5269
import com.jd.jdbc.sqlparser.ast.statement.SQLSelectGroupByClause;
5370
import com.jd.jdbc.sqlparser.ast.statement.SQLSelectItem;
@@ -65,12 +82,17 @@
6582
import com.jd.jdbc.sqlparser.dialect.mysql.visitor.VtRewriteTableSchemaVisitor;
6683
import com.jd.jdbc.sqlparser.support.logging.Log;
6784
import com.jd.jdbc.sqlparser.support.logging.LogFactory;
85+
import static com.jd.jdbc.sqlparser.utils.JdbcConstants.MYSQL;
6886
import com.jd.jdbc.sqlparser.utils.TableNameUtils;
6987
import com.jd.jdbc.sqltypes.VtPlanValue;
7088
import com.jd.jdbc.sqltypes.VtValue;
7189
import com.jd.jdbc.tindexes.LogicTable;
7290
import com.jd.jdbc.tindexes.TableIndex;
7391
import com.jd.jdbc.vindexes.VKeyspace;
92+
import static com.jd.jdbc.vindexes.Vschema.CODE_PINNED_TABLE;
93+
import static com.jd.jdbc.vindexes.Vschema.TYPE_PINNED_TABLE;
94+
import static com.jd.jdbc.vindexes.Vschema.TYPE_REFERENCE;
95+
import static com.jd.jdbc.vindexes.Vschema.TYPE_SEQUENCE;
7496
import com.jd.jdbc.vindexes.hash.Binary;
7597
import com.jd.jdbc.vindexes.hash.BinaryHash;
7698
import com.jd.jdbc.vindexes.hash.Hash;
@@ -896,28 +918,6 @@ private void buildTablePrimitive(SQLExprTableSource tableExpr) throws SQLExcepti
896918
this.symtab = symtab;
897919
symtab.addVSchemaTable(tableExpr, vschemaTable, routeBuilder);
898920

899-
// sub := &tableSubstitution{
900-
// oldExpr: tableExpr,
901-
// }
902-
// if tableExpr.As.IsEmpty() {
903-
// if tableName.Name != vschemaTable.Name {
904-
// // Table name does not match. Change and alias it to old name.
905-
// sub.newExpr = &sqlparser.AliasedTableExpr{
906-
// Expr: sqlparser.TableName{Name: vschemaTable.Name},
907-
// As: tableName.Name,
908-
// }
909-
// }
910-
// } else {
911-
// // Table is already aliased.
912-
// if tableName.Name != vschemaTable.Name {
913-
// // Table name does not match. Change it and reuse existing alias.
914-
// sub.newExpr = &sqlparser.AliasedTableExpr{
915-
// Expr: sqlparser.TableName{Name: vschemaTable.Name},
916-
// As: tableExpr.As,
917-
// }
918-
// }
919-
// }
920-
921921
RouteEngine routeEngine;
922922
if (TYPE_SEQUENCE.equalsIgnoreCase(vschemaTable.getType())) {
923923
routeEngine = new RouteEngine(Engine.RouteOpcode.SelectNext, keyspace);

src/main/java/com/jd/jdbc/sqlparser/dialect/mysql/visitor/VtReplaceVariantRefExprVisitor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public boolean visit(final SQLOrderBy x) {
6363

6464
@Override
6565
public boolean visit(final SQLSelectGroupByClause x) {
66+
if (x.getHaving() != null) {
67+
x.getHaving().accept(this);
68+
}
6669
return false;
6770
}
6871

src/main/java/com/jd/jdbc/sqlparser/dialect/mysql/visitor/VtSmartNormalizeVisitor.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,38 @@ public boolean visit(final SQLOrderBy x) {
226226

227227
@Override
228228
public boolean visit(final SQLSelectGroupByClause x) {
229-
if (parameterizedGroupbyOutput) {
230-
return super.visit(x);
229+
int itemSize = x.getItems().size();
230+
if (itemSize > 0) {
231+
print0(ucase ? "GROUP BY " : "group by ");
232+
this.indentCount++;
233+
for (int i = 0; i < itemSize; ++i) {
234+
if (i != 0) {
235+
if (groupItemSingleLine) {
236+
println(", ");
237+
} else {
238+
print(", ");
239+
}
240+
}
241+
print0(SQLUtils.toMySqlString(x.getItems().get(i), SQLUtils.NOT_FORMAT_OPTION));
242+
}
243+
this.indentCount--;
231244
}
232245

233-
print0(SQLUtils.toMySqlString(x, SQLUtils.NOT_FORMAT_OPTION));
246+
if (x.getHaving() != null) {
247+
if (itemSize > 0) {
248+
println();
249+
}
250+
print0(ucase ? "HAVING " : "having ");
251+
x.getHaving().accept(this);
252+
}
253+
254+
if (x.isWithRollUp()) {
255+
print0(ucase ? " WITH ROLLUP" : " with rollup");
256+
}
257+
258+
if (x.isWithCube()) {
259+
print0(ucase ? " WITH CUBE" : " with cube");
260+
}
234261

235262
return false;
236263
}

0 commit comments

Comments
 (0)