@Override public void addGlobalCondition(Parameter param, int columnId, int comparisonType) { addParameter(param); switch (unionType) { case UNION_ALL: case UNION: case INTERSECT: { left.addGlobalCondition(param, columnId, comparisonType); right.addGlobalCondition(param, columnId, comparisonType); break; } case EXCEPT: { left.addGlobalCondition(param, columnId, comparisonType); break; } default: DbException.throwInternalError("type=" + unionType); } }
@Override public boolean allowGlobalConditions() { return left.allowGlobalConditions() && right.allowGlobalConditions(); }
return queryWithoutCache(limit, target); Value[] params = getParameterValues(); if (isEverything(ExpressionVisitor.DETERMINISTIC_VISITOR)) { if (lastResult != null && !lastResult.isClosed() && limit == lastLimit) { if (sameResultAsLast(session, params, lastParameters)) { lastResult = lastResult.createShallowCopy(session); if (lastResult != null) { closeLastResult(); LocalResult r = queryWithoutCache(limit, target); lastResult = r; lastLimit = limit;
private Expression getExpression() { if (expression == null) { ArrayList<Expression> expressions = query.getExpressions(); int columnCount = query.getColumnCount(); if (columnCount == 1) { expression = expressions.get(0); } else { Expression[] list = new Expression[columnCount]; for (int i = 0; i < columnCount; i++) { list[i] = expressions.get(i); } expression = new ExpressionList(list); } } return expression; }
@Override public Expression optimize(Session session) { left = left.optimize(session); query.setRandomAccessResult(true); if (query.getColumnCount() != 1) { throw DbException.get(ErrorCode.SUBQUERY_IS_NOT_SINGLE_COLUMN); } // Can not optimize: the data may change return this; }
@Override public void init() { if (SysProperties.CHECK && checkInit) { DbException.throwInternalError(); } checkInit = true; left.init(); right.init(); int len = left.getColumnCount(); if (len != right.getColumnCount()) { throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH); } ArrayList<Expression> le = left.getExpressions(); // set the expressions to get the right column count and names, // but can't validate at this time expressions = New.arrayList(); for (int i = 0; i < len; i++) { Expression l = le.get(i); expressions.add(l); } }
return q; if (!q.allowGlobalConditions()) { return q; int firstIndexParam = q.getParameters() == null ? 0 : q.getParameters().size(); firstIndexParam += getParameterOffset(); IntArray paramIndex = new IntArray(); if ((mask & IndexCondition.EQUALITY) != 0) { Parameter param = new Parameter(firstIndexParam + i); q.addGlobalCondition(param, idx, Comparison.EQUAL); i++; q.addGlobalCondition(param, idx, Comparison.BIGGER_EQUAL); i++; q.addGlobalCondition(param, idx, Comparison.SMALLER_EQUAL); i++; String sql = q.getPlanSQL(); q = (Query) session.prepare(sql, true); return q;
try { Query query = compileViewQuery(session, querySQL); this.querySQL = query.getSQL(); tables = New.arrayList(query.getTables()); ArrayList<Expression> expressions = query.getExpressions(); ArrayList<Column> list = New.arrayList(); for (int i = 0, count = query.getColumnCount(); i < count; i++) { Expression expr = expressions.get(i); String name = null;
query.prepare(); if (query.getColumnCount() != columns.length) { throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
left.prepare(); right.prepare(); int len = left.getColumnCount(); ArrayList<Expression> le = left.getExpressions(); ArrayList<Expression> re = right.getExpressions(); for (int i = 0; i < len; i++) { Expression l = le.get(i);
if (sort == null && !distinct && maxRows == 0 && offsetExpr == null && limitExpr == null) { left.query(0, target); right.query(0, target); return null; int columnCount = left.getColumnCount(); LocalResult result = new LocalResult(session, expressionArray, columnCount); if (sort != null) { left.setDistinct(true); right.setDistinct(true); result.setDistinct(); case UNION: case EXCEPT: left.setDistinct(true); right.setDistinct(true); result.setDistinct(); break; break; case INTERSECT: left.setDistinct(true); right.setDistinct(true); break; default: DbException.throwInternalError("type=" + unionType);
/** * Create a temporary view out of the given query. * * @param session the session * @param owner the owner of the query * @param name the view name * @param query the query * @param topQuery the top level query * @return the view table */ public static TableView createTempView(Session session, User owner, String name, Query query, Query topQuery) { Schema mainSchema = session.getDatabase().getSchema(Constants.SCHEMA_MAIN); String querySQL = query.getPlanSQL(); TableView v = new TableView(mainSchema, 0, name, querySQL, query.getParameters(), null, session, false); if (v.createException != null) { throw v.createException; } v.setTopQuery(topQuery); v.setOwner(owner); v.setTemporary(true); return v; }
ArrayList<Parameter> paramList = q.getParameters(); ArrayList<Parameter> originalParameters = viewQuery.getParameters(); if (originalParameters != null) { for (int i = 0, size = originalParameters.size(); i < size; i++) { LocalResult result = q.query(0); return new ViewCursor(this, result);
@Override protected String doExplain() { TableMate table = toTableMate(prepared.getTable()); if (workers != null) { return explainForWorker(workers); } else { Query query = prepared.getQuery(); String subPlan = query.explainPlan(); StringBuilder explain = new StringBuilder(); explain.append("insert into ").append(table.getName()).append(" with query result"); explain.append(StringUtils.indent(subPlan, 4, false)); return explain.toString(); } }
@Override public int getCost() { return query.getCostAsExpression(); }
@Override public int getColumnCount() { return left.getColumnCount(); }
public int getParameterOffset() { return topQuery == null ? 0 : topQuery.getParameters().size(); }
query.prepare(); if (query.getColumnCount() != columns.length) { throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
private void generateColumnsFromQuery() { int columnCount = prepared.getQuery().getColumnCount(); ArrayList<Expression> expressions = prepared.getQuery().getExpressions(); for (int i = 0; i < columnCount; i++) { Expression expr = expressions.get(i); int type = expr.getType(); String name = expr.getAlias(); long precision = expr.getPrecision(); int displaySize = expr.getDisplaySize(); DataType dt = DataType.getDataType(type); if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) { // dont' set precision to MAX_VALUE if this is the default precision = dt.defaultPrecision; } int scale = expr.getScale(); if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) { scale = dt.defaultScale; } if (scale > precision) { precision = scale; } Column col = new Column(name, type, precision, scale, displaySize); prepared.addColumn(col); } }