use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Replace method getPlanSQL.
@Override
public String getPlanSQL() {
StatementBuilder buff = new StatementBuilder("REPLACE INTO ");
buff.append(table.getSQL()).append('(');
for (Column c : columns) {
buff.appendExceptFirst(", ");
buff.append(c.getSQL());
}
buff.append(')');
buff.append('\n');
if (list.size() > 0) {
buff.append("VALUES ");
int row = 0;
for (Expression[] expr : list) {
if (row++ > 0) {
buff.append(", ");
}
buff.append('(');
buff.resetCount();
for (Expression e : expr) {
buff.appendExceptFirst(", ");
if (e == null) {
buff.append("DEFAULT");
} else {
buff.append(e.getSQL());
}
}
buff.append(')');
}
} else {
buff.append(query.getPlanSQL());
}
return buff.toString();
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Select method getPlanSQL.
@Override
public String getPlanSQL() {
// can not use the field sqlStatement because the parameter
// indexes may be incorrect: ? may be in fact ?2 for a subquery
// but indexes may be set manually as well
Expression[] exprList = expressions.toArray(new Expression[expressions.size()]);
StatementBuilder buff = new StatementBuilder("SELECT");
if (distinct) {
buff.append(" DISTINCT");
}
for (int i = 0; i < visibleColumnCount; i++) {
buff.appendExceptFirst(",");
buff.append('\n');
buff.append(StringUtils.indent(exprList[i].getSQL(), 4, false));
}
buff.append("\nFROM ");
TableFilter filter = topTableFilter;
if (filter != null) {
buff.resetCount();
int i = 0;
do {
buff.appendExceptFirst("\n");
buff.append(filter.getPlanSQL(i++ > 0));
filter = filter.getJoin();
} while (filter != null);
} else {
buff.resetCount();
int i = 0;
for (TableFilter f : topFilters) {
do {
buff.appendExceptFirst("\n");
buff.append(f.getPlanSQL(i++ > 0));
f = f.getJoin();
} while (f != null);
}
}
if (condition != null) {
buff.append("\nWHERE ").append(StringUtils.unEnclose(condition.getSQL()));
}
if (groupIndex != null) {
buff.append("\nGROUP BY ");
buff.resetCount();
for (int gi : groupIndex) {
Expression g = exprList[gi];
g = g.getNonAliasExpression();
buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(g.getSQL()));
}
}
if (group != null) {
buff.append("\nGROUP BY ");
buff.resetCount();
for (Expression g : group) {
buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(g.getSQL()));
}
}
if (having != null) {
// could be set in addGlobalCondition
// in this case the query is not run directly, just getPlanSQL is
// called
Expression h = having;
buff.append("\nHAVING ").append(StringUtils.unEnclose(h.getSQL()));
} else if (havingIndex >= 0) {
Expression h = exprList[havingIndex];
buff.append("\nHAVING ").append(StringUtils.unEnclose(h.getSQL()));
}
if (sort != null) {
buff.append("\nORDER BY ").append(sort.getSQL(exprList, visibleColumnCount));
}
if (orderList != null) {
buff.append("\nORDER BY ");
buff.resetCount();
for (SelectOrderBy o : orderList) {
buff.appendExceptFirst(", ");
buff.append(StringUtils.unEnclose(o.getSQL()));
}
}
if (limitExpr != null) {
buff.append("\nLIMIT ").append(StringUtils.unEnclose(limitExpr.getSQL()));
if (offsetExpr != null) {
buff.append(" OFFSET ").append(StringUtils.unEnclose(offsetExpr.getSQL()));
}
}
if (sampleSizeExpr != null) {
buff.append("\nSAMPLE_SIZE ").append(StringUtils.unEnclose(sampleSizeExpr.getSQL()));
}
if (isForUpdate) {
buff.append("\nFOR UPDATE");
}
buff.append("\n/* cost: " + cost + " */");
return buff.toString();
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Select method expandColumnList.
private void expandColumnList() {
Database db = session.getDatabase();
// the expressions may change within the loop
for (int i = 0; i < expressions.size(); i++) {
Expression expr = expressions.get(i);
if (!expr.isWildcard()) {
continue;
}
String schemaName = expr.getSchemaName();
String tableAlias = expr.getTableAlias();
if (tableAlias == null) {
int temp = i;
expressions.remove(i);
for (TableFilter filter : filters) {
Wildcard c2 = new Wildcard(filter.getTable().getSchema().getName(), filter.getTableAlias());
expressions.add(i++, c2);
}
i = temp - 1;
} else {
TableFilter filter = null;
for (TableFilter f : filters) {
if (db.equalsIdentifiers(tableAlias, f.getTableAlias())) {
if (schemaName == null || db.equalsIdentifiers(schemaName, f.getSchemaName())) {
filter = f;
break;
}
}
}
if (filter == null) {
throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableAlias);
}
Table t = filter.getTable();
String alias = filter.getTableAlias();
expressions.remove(i);
Column[] columns = t.getColumns();
for (Column c : columns) {
if (filter.isNaturalJoinColumn(c)) {
continue;
}
ExpressionColumn ec = new ExpressionColumn(session.getDatabase(), null, alias, c.getName());
expressions.add(i++, ec);
}
i--;
}
}
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Select method isEverything.
@Override
public boolean isEverything(ExpressionVisitor visitor) {
switch(visitor.getType()) {
case ExpressionVisitor.DETERMINISTIC:
{
if (isForUpdate) {
return false;
}
for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter f = filters.get(i);
if (!f.getTable().isDeterministic()) {
return false;
}
}
break;
}
case ExpressionVisitor.EVALUATABLE:
{
if (!session.getDatabase().getSettings().optimizeEvaluatableSubqueries) {
return false;
}
break;
}
case ExpressionVisitor.GET_DEPENDENCIES:
{
for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter f = filters.get(i);
Table table = f.getTable();
visitor.addDependency(table);
table.addDependencies(visitor.getDependencies());
}
break;
}
default:
}
ExpressionVisitor v2 = visitor.incrementQueryLevel(1);
boolean result = true;
for (int i = 0, size = expressions.size(); i < size; i++) {
Expression e = expressions.get(i);
if (!e.isEverything(v2)) {
result = false;
break;
}
}
if (result && condition != null && !condition.isEverything(v2)) {
result = false;
}
if (result && having != null && !having.isEverything(v2)) {
result = false;
}
return result;
}
use of com.wplatform.ddal.command.expression.Expression in project jdbc-shards by wplatform.
the class Select method prepare.
@Override
public void prepare() {
if (isPrepared) {
// sometimes a subquery is prepared twice (CREATE TABLE AS SELECT)
return;
}
if (SysProperties.CHECK && !checkInit) {
DbException.throwInternalError("not initialized");
}
if (orderList != null) {
sort = prepareOrder(orderList, expressions.size());
orderList = null;
}
for (int i = 0; i < expressions.size(); i++) {
Expression e = expressions.get(i);
expressions.set(i, e.optimize(session));
}
if (condition != null) {
condition = condition.optimize(session);
for (TableFilter f : filters) {
// left outer join child on p = pc where c is null;
if (!f.isJoinOuter() && !f.isJoinOuterIndirect()) {
condition.createIndexConditions(session, f);
}
}
}
if (isGroupQuery && groupIndex == null && havingIndex < 0) {
isQuickAggregateQuery = true;
}
cost = preparePlan();
TableMate last = null;
for (TableFilter filter : filters) {
if (!filter.isFromTableMate()) {
break;
}
if (filters.size() == 1) {
isAccordantQuery = true;
break;
}
TableMate table = (TableMate) filter.getTable();
if (last != null && !last.isRelationSymmetry(table)) {
break;
}
last = table;
}
if (distinct && session.getDatabase().getSettings().optimizeDistinct && !isGroupQuery && filters.size() == 1 && expressions.size() == 1 && condition == null) {
//分布式的查询不合适
}
if (sort != null && !isGroupQuery) {
}
if (isGroupQuery && getGroupByExpressionCount() > 0) {
}
expressionArray = new Expression[expressions.size()];
expressions.toArray(expressionArray);
isPrepared = true;
}
Aggregations