Skip to content

Commit

Permalink
fix Having Filter parameters are not normalized (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin-2019 authored Aug 11, 2023
1 parent a1886b3 commit e535b58
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/jd/jdbc/planbuilder/AbstractRoutePlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.jd.jdbc.sqlparser.ast.SQLOrderBy;
import com.jd.jdbc.sqlparser.ast.SQLSetQuantifier;
import com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOpExpr;
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.NotEqual;
import com.jd.jdbc.sqlparser.ast.expr.SQLIdentifierExpr;
import com.jd.jdbc.sqlparser.ast.expr.SQLInListExpr;
import com.jd.jdbc.sqlparser.ast.expr.SQLIntegerExpr;
Expand Down Expand Up @@ -55,8 +56,6 @@
import lombok.Getter;
import lombok.Setter;

import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.NotEqual;

@Getter
@Setter
public abstract class AbstractRoutePlan implements Builder {
Expand Down Expand Up @@ -223,6 +222,12 @@ public void pushGroupBy(SQLSelectGroupByClause groupBy) {
for (SQLExpr item : groupBy.getItems()) {
newGroupBy.addItem(item);
}
if (groupBy.isWithCube()) {
newGroupBy.setWithCube(true);
}
if (groupBy.isWithRollUp()) {
newGroupBy.setWithRollUp(true);
}
((MySqlSelectQueryBlock) this.select).setGroupBy(newGroupBy);
}

Expand Down
44 changes: 22 additions & 22 deletions src/main/java/com/jd/jdbc/planbuilder/PrimitiveBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
import com.google.protobuf.UnknownFieldSet;
import com.jd.jdbc.VSchemaManager;
import com.jd.jdbc.engine.Engine;
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutExists;
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutIn;
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutNotIn;
import static com.jd.jdbc.engine.Engine.PulloutOpcode.PulloutValue;
import static com.jd.jdbc.engine.Engine.RouteOpcode.SelectDBA;
import static com.jd.jdbc.engine.Engine.RouteOpcode.SelectReference;
import com.jd.jdbc.engine.OrderedAggregateEngine;
import com.jd.jdbc.engine.RouteEngine;
import com.jd.jdbc.engine.table.TableRouteEngine;
Expand All @@ -32,13 +38,23 @@
import com.jd.jdbc.planbuilder.tableplan.TableRoutePlan;
import com.jd.jdbc.sqlparser.SQLUtils;
import com.jd.jdbc.sqlparser.SqlParser;
import static com.jd.jdbc.sqlparser.SqlParser.GroupByExpr.ColName;
import static com.jd.jdbc.sqlparser.SqlParser.GroupByExpr.Literal;
import static com.jd.jdbc.sqlparser.SqlParser.HAVING_STR;
import static com.jd.jdbc.sqlparser.SqlParser.SelectExpr.AliasedExpr;
import static com.jd.jdbc.sqlparser.SqlParser.SelectExpr.Nextval;
import static com.jd.jdbc.sqlparser.SqlParser.SelectExpr.StarExpr;
import static com.jd.jdbc.sqlparser.SqlParser.WHERE_STR;
import com.jd.jdbc.sqlparser.ast.SQLExpr;
import com.jd.jdbc.sqlparser.ast.SQLLimit;
import com.jd.jdbc.sqlparser.ast.SQLObject;
import com.jd.jdbc.sqlparser.ast.SQLOrderBy;
import com.jd.jdbc.sqlparser.ast.SQLSetQuantifier;
import com.jd.jdbc.sqlparser.ast.expr.SQLAllColumnExpr;
import com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOpExpr;
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.BooleanAnd;
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.BooleanOr;
import static com.jd.jdbc.sqlparser.ast.expr.SQLBinaryOperator.Equality;
import com.jd.jdbc.sqlparser.ast.expr.SQLExistsExpr;
import com.jd.jdbc.sqlparser.ast.expr.SQLIdentifierExpr;
import com.jd.jdbc.sqlparser.ast.expr.SQLInSubQueryExpr;
Expand All @@ -48,6 +64,7 @@
import com.jd.jdbc.sqlparser.ast.expr.SQLVariantRefListExpr;
import com.jd.jdbc.sqlparser.ast.statement.SQLExprTableSource;
import com.jd.jdbc.sqlparser.ast.statement.SQLJoinTableSource;
import static com.jd.jdbc.sqlparser.ast.statement.SQLJoinTableSource.JoinType;
import com.jd.jdbc.sqlparser.ast.statement.SQLSelect;
import com.jd.jdbc.sqlparser.ast.statement.SQLSelectGroupByClause;
import com.jd.jdbc.sqlparser.ast.statement.SQLSelectItem;
Expand All @@ -65,12 +82,17 @@
import com.jd.jdbc.sqlparser.dialect.mysql.visitor.VtRewriteTableSchemaVisitor;
import com.jd.jdbc.sqlparser.support.logging.Log;
import com.jd.jdbc.sqlparser.support.logging.LogFactory;
import static com.jd.jdbc.sqlparser.utils.JdbcConstants.MYSQL;
import com.jd.jdbc.sqlparser.utils.TableNameUtils;
import com.jd.jdbc.sqltypes.VtPlanValue;
import com.jd.jdbc.sqltypes.VtValue;
import com.jd.jdbc.tindexes.LogicTable;
import com.jd.jdbc.tindexes.TableIndex;
import com.jd.jdbc.vindexes.VKeyspace;
import static com.jd.jdbc.vindexes.Vschema.CODE_PINNED_TABLE;
import static com.jd.jdbc.vindexes.Vschema.TYPE_PINNED_TABLE;
import static com.jd.jdbc.vindexes.Vschema.TYPE_REFERENCE;
import static com.jd.jdbc.vindexes.Vschema.TYPE_SEQUENCE;
import com.jd.jdbc.vindexes.hash.Binary;
import com.jd.jdbc.vindexes.hash.BinaryHash;
import com.jd.jdbc.vindexes.hash.Hash;
Expand Down Expand Up @@ -896,28 +918,6 @@ private void buildTablePrimitive(SQLExprTableSource tableExpr) throws SQLExcepti
this.symtab = symtab;
symtab.addVSchemaTable(tableExpr, vschemaTable, routeBuilder);

// sub := &tableSubstitution{
// oldExpr: tableExpr,
// }
// if tableExpr.As.IsEmpty() {
// if tableName.Name != vschemaTable.Name {
// // Table name does not match. Change and alias it to old name.
// sub.newExpr = &sqlparser.AliasedTableExpr{
// Expr: sqlparser.TableName{Name: vschemaTable.Name},
// As: tableName.Name,
// }
// }
// } else {
// // Table is already aliased.
// if tableName.Name != vschemaTable.Name {
// // Table name does not match. Change it and reuse existing alias.
// sub.newExpr = &sqlparser.AliasedTableExpr{
// Expr: sqlparser.TableName{Name: vschemaTable.Name},
// As: tableExpr.As,
// }
// }
// }

RouteEngine routeEngine;
if (TYPE_SEQUENCE.equalsIgnoreCase(vschemaTable.getType())) {
routeEngine = new RouteEngine(Engine.RouteOpcode.SelectNext, keyspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public boolean visit(final SQLOrderBy x) {

@Override
public boolean visit(final SQLSelectGroupByClause x) {
if (x.getHaving() != null) {
x.getHaving().accept(this);
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,38 @@ public boolean visit(final SQLOrderBy x) {

@Override
public boolean visit(final SQLSelectGroupByClause x) {
if (parameterizedGroupbyOutput) {
return super.visit(x);
int itemSize = x.getItems().size();
if (itemSize > 0) {
print0(ucase ? "GROUP BY " : "group by ");
this.indentCount++;
for (int i = 0; i < itemSize; ++i) {
if (i != 0) {
if (groupItemSingleLine) {
println(", ");
} else {
print(", ");
}
}
print0(SQLUtils.toMySqlString(x.getItems().get(i), SQLUtils.NOT_FORMAT_OPTION));
}
this.indentCount--;
}

print0(SQLUtils.toMySqlString(x, SQLUtils.NOT_FORMAT_OPTION));
if (x.getHaving() != null) {
if (itemSize > 0) {
println();
}
print0(ucase ? "HAVING " : "having ");
x.getHaving().accept(this);
}

if (x.isWithRollUp()) {
print0(ucase ? " WITH ROLLUP" : " with rollup");
}

if (x.isWithCube()) {
print0(ucase ? " WITH CUBE" : " with cube");
}

return false;
}
Expand Down

0 comments on commit e535b58

Please sign in to comment.