@Override public void inTxn(SSConnection conn, WorkerGroup wg) throws PEException { final SchemaContext sc = SchemaContext.createContext(conn); sc.forceMutableSource(); List<PEContainerTenant> loaded = Functional.apply( tenants, new UnaryFunction<PEContainerTenant, SchemaCacheKey<PEContainerTenant>>() { @Override public PEContainerTenant evaluate(SchemaCacheKey<PEContainerTenant> object) { return sc.getSource().find(sc, object); } }); sc.beginSaveContext(); try { List<CatalogEntity> out = Functional.apply( loaded, new UnaryFunction<CatalogEntity, PEContainerTenant>() { @Override public CatalogEntity evaluate(PEContainerTenant object) { return object.getPersistent(sc); } }); deleted = out; } finally { sc.endSaveContext(); } }
@Test public void testSimpleB() throws Throwable { SchemaContext db = buildSchema(TestName.MULTI, schema); PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db); stmtTest( db, "select desc from A group by id", SelectStatement.class, bes( new ProjectingExpectedStep( ExecutionType.SELECT, group, "temp1", TransientExecutionEngine.AGGREGATION, StaticDistributionModel.MODEL_NAME, new String[] {}, new String[][] {}, "SELECT `A`.`desc` AS A1d1_7,`A`.`id` AS A1i0_8", "FROM `A`", "GROUP BY A1i0_8 ASC"), new ProjectingExpectedStep( ExecutionType.SELECT, null, "SELECT temp1.A1d1_7 AS t2A0", "FROM temp1", "ORDER BY temp1.A1i0_8 ASC"))); }
@Override public void executeNested( ExecutionState estate, WorkerGroup wg, DBResultConsumer resultConsumer) throws Throwable { SSConnection conn = estate.getConnection(); SchemaContext cntxt = conn.getSchemaContext(); cntxt.beginSaveContext(); PersistentGroup pg = null; try { pg = onGroup.persistTree(cntxt); } finally { cntxt.endSaveContext(); } QueryStepRebalance rangeRebalance = new QueryStepRebalance(pg, rangeInfo, ignoreFKS); rangeRebalance.executeSelf(estate, wg, DBEmptyTextResultConsumer.INSTANCE); }
public static boolean setTimestampVariableForUnspecifiedColumn( SchemaContext sc, DMLStatement dmls, PEColumn column) { boolean ret = false; // only set the timestamp variable if this is a timestamp column if (column.getType().getBaseType().getDataType() != Types.TIMESTAMP) { return ret; } // for an update statement if the on update is set and // the column is not specified then set the timestamp variable if ((dmls instanceof UpdateStatement) && column.isOnUpdated()) { ret = true; return ret; } boolean isNullable = column.isNullable(); ExpressionNode defaultValue = column.getDefaultValue(); if (defaultValue == null) { // no default value column modifier specified // now we need to know if the on update has also been set or not if (!column.isOnUpdated() && !isNullable) { // on update is not specified so default value becomes current timestamp ret = true; } // else { // With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, // the column is automatically updated to the current timestamp. // The default is 0 unless the column is defined with the NULL attribute, // in which case the default is NULL. // } } else { if (dmls instanceof UpdateStatement) { if (column.isOnUpdated()) { ret = true; } } else { if (column.getDefaultValue() == null) { // null default value // do not set timestamp variable } else { Object o = column.getDefaultValue(); if (o instanceof IdentifierLiteralExpression) { if (StringUtils.equals( ((IdentifierLiteralExpression) o).asString(sc.getValues()), "0")) { // do nothing } else { // for a timestamp column only other choice is current_timestamp ret = true; } } else if (o instanceof LiteralExpression) { // for literal default value (ie. 0 or 'yyyy-mm-dd hh:mm:ss') // do not set timestamp variable } } } } return ret; }
@Test public void testComplexA() throws Throwable { SchemaContext db = buildSchema(TestName.MULTI, schema); PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db); stmtTest( db, "select n.desc,r.desc from N n, R r where n.id = r.id group by r.flags", SelectStatement.class, bes( bpes( bes( new ProjectingExpectedStep( "SELECT n.`desc` AS nd1_3,n.`id` AS ni0_4 FROM `N` AS n", group, "temp1", TransientExecutionEngine.LARGE, StaticDistributionModel.MODEL_NAME, new String[] {"ni0_4"})), bes( new ProjectingExpectedStep( "SELECT r.`desc` AS rd1_4,r.`flags` AS rf2_5,r.`id` AS ri0_6 FROM `R` AS r", group, "temp2", TransientExecutionEngine.LARGE, StaticDistributionModel.MODEL_NAME, new String[] {"ri0_6"}))), new ProjectingExpectedStep( "SELECT temp1.nd1_3 AS t3n0_11,temp2.rd1_4 AS t4r0_12,temp2.rf2_5 AS t4r1_13 FROM temp1, temp2 WHERE temp1.ni0_4 = temp2.ri0_6", TransientExecutionEngine.LARGE, "temp3", TransientExecutionEngine.LARGE, StaticDistributionModel.MODEL_NAME, new String[] {"t4r1_13"}), new ProjectingExpectedStep( ExecutionType.SELECT, "SELECT temp3.t3n0_11 AS t5t0,temp3.t4r0_12 AS t5t1,temp3.t4r1_13 AS t5t2 FROM temp3 GROUP BY t5t2 ASC", TransientExecutionEngine.LARGE, "temp4", TransientExecutionEngine.AGGREGATION, StaticDistributionModel.MODEL_NAME, new String[] {}), new ProjectingExpectedStep( "SELECT temp4.t5t0 AS t6t0,temp4.t5t1 AS t6t1 FROM temp4 ORDER BY temp4.t5t2 ASC", null))); }
@Override public String getSQL(SchemaContext sc, Emitter emitter, EmitOptions opts, boolean unused) { if (likeClause != null) { StringBuilder buf = new StringBuilder(); buf.append("SHOW TABLE STATUS LIKE "); Singletons.require(DBNative.class) .getEmitter() .emitExpression(sc, sc.getValues(), likeClause, buf, -1); return buf.toString(); } else return "show table status ..."; }
// varieties of order by: // order by non ref col // order by ref col // order by ref expr (via alias instance) // order by non ref expr (expr in order by) // going to do all of these twice: once on the random table, and once on the bcast table @Test public void testNonRefColRandom() throws Throwable { SchemaContext db = buildSchema(TestName.MULTI, schema); PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db); stmtTest( db, "select desc from R group by id", SelectStatement.class, bes( new ProjectingExpectedStep( ExecutionType.SELECT, group, "temp1", TransientExecutionEngine.LARGE, StaticDistributionModel.MODEL_NAME, new String[] {"R1i0_8"}, new String[][] {{"R1i0_8"}}, "SELECT `R`.`desc` AS R1d1_7,`R`.`id` AS R1i0_8", "FROM `R`"), new ProjectingExpectedStep( ExecutionType.SELECT, TransientExecutionEngine.LARGE, "temp2", TransientExecutionEngine.AGGREGATION, StaticDistributionModel.MODEL_NAME, new String[] {}, new String[][] {}, "SELECT temp1.R1d1_7 AS t2R0,temp1.R1i0_8 AS t2R1", "FROM temp1", "GROUP BY t2R1 ASC"), new ProjectingExpectedStep( ExecutionType.SELECT, null, "SELECT temp2.t2R0 AS t3t0", "FROM temp2", "ORDER BY temp2.t2R1 ASC"))); }
@Test public void testComplexC() throws Throwable { SchemaContext db = buildSchema(TestName.MULTI, schema); PEPersistentGroup group = db.getCurrentDatabase().getDefaultStorage(db); stmtTest( db, "select id, concat(desc, slug) as complete from A group by complete", SelectStatement.class, bes( new ProjectingExpectedStep( ExecutionType.SELECT, group, "temp1", TransientExecutionEngine.LARGE, StaticDistributionModel.MODEL_NAME, new String[] {"complete"}, new String[][] {}, "SELECT `A`.`id` AS A1i0_5,concat( `A`.`desc`,`A`.`slug` ) AS complete", "FROM `A`"), new ProjectingExpectedStep( ExecutionType.SELECT, TransientExecutionEngine.LARGE, "temp2", TransientExecutionEngine.AGGREGATION, StaticDistributionModel.MODEL_NAME, new String[] {}, new String[][] {}, "SELECT temp1.A1i0_5 AS t2A0,temp1.complete AS complete", "FROM temp1", "GROUP BY complete ASC"), new ProjectingExpectedStep( ExecutionType.SELECT, null, "SELECT temp2.t2A0 AS t3t0,temp2.complete AS complete", "FROM temp2", "ORDER BY complete ASC"))); }
private static SchemaCacheKey<PEContainerTenant> convert( SchemaContext sc, Map<PEColumn, ConstantExpression> unorderedValues) { PETable ofTab = unorderedValues.keySet().iterator().next().getTable().asTable(); ContainerDistributionVector cdv = (ContainerDistributionVector) ofTab.getDistributionVector(sc); PEContainer cont = cdv.getContainer(sc); List<PEColumn> discColOrder = cont.getDiscriminantColumns(sc); List<Pair<PEColumn, LiteralExpression>> ordered = new ArrayList<Pair<PEColumn, LiteralExpression>>(); for (PEColumn pec : discColOrder) { ConstantExpression ce = unorderedValues.get(pec); if (ce == null) throw new SchemaException(Pass.PLANNER, "Malformed discriminant key"); ordered.add(new Pair<PEColumn, LiteralExpression>(pec, (LiteralExpression) ce)); } String discValue = PEContainerTenant.buildDiscriminantValue(sc, sc.getValues(), ordered); return PEContainerTenant.getContainerTenantKey(cont, discValue); }
@Override protected ExecutionPlan buildExplain(SchemaContext sc, BehaviorConfiguration config) throws PEException { boolean noplan = explain.hasSetting(ExplainOption.NOPLAN); if (noplan) { normalize(sc); ProjectingExecutionStep ses = ProjectingExecutionStep.build( sc, getDatabase(sc), getStorageGroups(sc).get(0), EngineConstant.BROADEST_DISTRIBUTION_VECTOR.getValue(this, sc), null, this, DMLExplainReason.EXPLAIN_NOPLAN.makeRecord()); ExecutionPlan expep = new ExecutionPlan(null, sc.getValueManager(), StatementType.EXPLAIN); expep.getSequence().append(ses); return expep; } // we would check for alternate configuration here, but not quite yet return super.buildExplain(sc, config); }
@Override public void plan(SchemaContext pc, ExecutionSequence es, BehaviorConfiguration config) throws PEException { Database<?> ondb = null; for (TableKey tk : tables) { if (ondb == null) ondb = tk.getTable().getDatabase(pc); else if (!ondb.equals(tk.getTable().getDatabase(pc))) throw new SchemaException(Pass.PLANNER, "Unable to show table status across databases"); } String origCommand = buildInitialShowStatusCommand(); if (onGroup.isSingleSiteGroup()) { es.append(ProjectingExecutionStep.build(pc, ondb, onGroup, origCommand)); } else { // System.out.println(origCommand); TempGroupManager tgm = new TempGroupManager(); TempTable t1 = buildFirstTempTable(pc, tgm.getGroup(true)); if (!pc.hasCurrentDatabase() && !t1.hasDatabase(pc)) { // if no current database is set and the temp table has no database // we need to set one t1.setDatabase(pc, (PEDatabase) ondb, false); } SelectStatement firstSelect = buildFirstSelect(pc, t1); DistributionKeyTemplate dkt = buildKeyTemplate(); for (int i = 0; i < firstSelect.getProjectionEdge().size(); i++) dkt.addColumn(firstSelect.getProjectionEdge().get(i), i); DistributionVector sourceVect = buildVector(pc); tgm.plan(pc); es.append( RedistributionExecutionStep.build( pc, ondb, onGroup, origCommand, sourceVect, t1, t1.getStorageGroup(pc), dkt, null)); SelectStatement aggCommand = buildAggCommand(firstSelect); // System.out.println(aggCommand.getSQL()); es.append( ProjectingExecutionStep.build( pc, ondb, t1.getStorageGroup(pc), null, null, aggCommand, null)); } }
private ProjectionInfo buildProjectionMetadata(SchemaContext pc, List<ExpressionNode> proj) { Emitter emitter = Singletons.require(HostService.class).getDBNative().getEmitter(); try { emitter.setOptions(EmitOptions.RESULTSETMETADATA); emitter.pushContext(pc.getTokens()); ProjectionInfo pi = new ProjectionInfo(proj.size()); for (int i = 0; i < proj.size(); i++) { ExpressionNode e = proj.get(i); String columnName = null; String aliasName = null; ColumnInstance ci = null; if (e.getSourceLocation() != null && e.getSourceLocation().isComputed()) { aliasName = e.getSourceLocation().getText(); } if (e instanceof ExpressionAlias) { ExpressionAlias ea = (ExpressionAlias) e; Alias aname = ea.getAlias(); if (aname != null) aliasName = PEStringUtils.dequote(aname.getSQL()); ExpressionNode cname = ea.getTarget(); StringBuilder buf = new StringBuilder(); emitter.emitExpression(pc, cname, buf); columnName = buf.toString(); if (cname instanceof ColumnInstance) { ci = (ColumnInstance) cname; } else { aliasName = PEStringUtils.dequote(aliasName); columnName = aliasName; } } else if (e instanceof ColumnInstance) { ci = (ColumnInstance) e; StringBuilder buf = new StringBuilder(); emitter.emitExpression(pc, e, buf); columnName = buf.toString(); aliasName = PEStringUtils.dequote(columnName); } else { if (aliasName != null) { // via above columnName = aliasName; } else { StringBuilder buf = new StringBuilder(); emitter.emitExpression(pc, e, buf); columnName = (e instanceof LiteralExpression) ? PEStringUtils.dequote(buf.toString()) : buf.toString(); aliasName = columnName; } } ColumnInfo colInfo = pi.addColumn(i + 1, columnName, (aliasName == null ? columnName : aliasName)); if (ci != null) { String tblName = null; String dbName = null; Column<?> backingColumn = ci.getColumn(); TableKey tk = ci.getTableInstance().getTableKey(); if (tk instanceof MTTableKey) { MTTableKey mtk = (MTTableKey) tk; tblName = mtk.getScope().getName().getUnqualified().getUnquotedName().get(); PETenant tenant = mtk.getScope().getTenant(pc); PEDatabase pdb = tenant.getDatabase(pc); if (pdb.getMTMode() == MultitenantMode.ADAPTIVE) dbName = tenant.getName().getUnqualified().getUnquotedName().get(); else dbName = pdb.getName().getUnqualified().getUnquotedName().get(); } else { Table<?> tab = tk.getTable(); if (tab.isInfoSchema()) { dbName = PEConstants.INFORMATION_SCHEMA_DBNAME; } else { Database<?> tabDb = tab.getDatabase(pc); if (tab.isTempTable() && (tabDb == null)) { tabDb = pc.getCurrentDatabase(false); if (tabDb == null) { tabDb = pc.getAnyNonSchemaDatabase(); } final TempTable tabAstempTable = ((TempTable) tab); tabAstempTable.setDatabase(pc, (PEDatabase) tabDb, true); tabAstempTable.refreshColumnLookupTable(); } if (tabDb != null) { dbName = tabDb.getName().getUnqualified().getUnquotedName().get(); } } tblName = tab.getName(pc).getUnqualified().getUnquotedName().get(); } if (tblName != null) colInfo.setDatabaseAndTable(dbName, tblName); if (backingColumn instanceof PEColumn) { // set flags PEColumn pec = (PEColumn) backingColumn; if (!pec.isNotNullable() || pec.isNullable()) colInfo.setAttribute(ColumnAttribute.NULLABLE); if (pec.isAutoIncrement()) colInfo.setAttribute(ColumnAttribute.AUTO_INCREMENT); if (pec.isKeyPart()) { colInfo.setAttribute(ColumnAttribute.KEY_PART); if (pec.isPrimaryKeyPart()) colInfo.setAttribute(ColumnAttribute.PRIMARY_KEY_PART); if (pec.isUniquePart()) colInfo.setAttribute(ColumnAttribute.UNIQUE_PART); } } } } return pi; } finally { emitter.popContext(); } }