/** * @see org.teiid.query.resolver.CommandResolver#resolveCommand(org.teiid.query.sql.lang.Command, * TempMetadataAdapter, boolean) */ @Override public void resolveCommand( Command command, TempMetadataAdapter metadata, boolean resolveNullLiterals) throws Exception { DynamicCommand dynamicCmd = (DynamicCommand) command; Iterator columns = dynamicCmd.getAsColumns().iterator(); Set<GroupSymbol> groups = new HashSet<GroupSymbol>(); // if there is no into group, just create temp metadata ids if (dynamicCmd.getIntoGroup() == null) { while (columns.hasNext()) { ElementSymbol column = (ElementSymbol) columns.next(); column.setMetadataID(new TempMetadataID(column.getShortName(), column.getType())); } } else if (dynamicCmd.getIntoGroup().isTempGroupSymbol()) { while (columns.hasNext()) { ElementSymbol column = (ElementSymbol) columns.next(); GroupSymbol gs = getTeiidParser().createASTNode(ASTNodes.GROUP_SYMBOL); gs.setName(dynamicCmd.getIntoGroup().getName()); column.setGroupSymbol(gs); } } ResolverVisitor visitor = new ResolverVisitor(getTeiidParser().getVersion()); visitor.resolveLanguageObject( dynamicCmd, groups, dynamicCmd.getExternalGroupContexts(), metadata); String sqlType = getDataTypeManager().getDataTypeName(dynamicCmd.getSql().getType()); String targetType = DataTypeManagerService.DefaultDataTypes.STRING.getId(); if (!targetType.equals(sqlType) && !getDataTypeManager().isImplicitConversion(sqlType, targetType)) { throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30100, sqlType)); } if (dynamicCmd.getUsing() != null && !dynamicCmd.getUsing().isEmpty()) { for (SetClause clause : dynamicCmd.getUsing().getClauses()) { ElementSymbol id = clause.getSymbol(); GroupSymbol gs = getTeiidParser().createASTNode(ASTNodes.GROUP_SYMBOL); gs.setName(ProcedureReservedWords.DVARS); id.setGroupSymbol(gs); id.setType(clause.getValue().getType()); id.setMetadataID(new TempMetadataID(id.getName(), id.getType())); } } GroupSymbol intoSymbol = dynamicCmd.getIntoGroup(); if (intoSymbol != null) { if (!intoSymbol.isImplicitTempGroupSymbol()) { ResolverUtil.resolveGroup(intoSymbol, metadata); } else { List symbols = dynamicCmd.getAsColumns(); ResolverUtil.resolveImplicitTempGroup(metadata, intoSymbol, symbols); } } }
TempTable getOrCreateTempTable( String tempTableID, Command command, BufferManager buffer, boolean delegate, boolean forUpdate, CommandContext context) throws TeiidProcessingException, BlockedException, TeiidComponentException { TempTable tempTable = getTempTable(tempTableID, command, buffer, delegate, forUpdate, context); if (tempTable != null) { if (processors != null) { TableProcessor withProcessor = processors.get(tempTableID); if (withProcessor != null) { buildWithTable(tempTableID, withProcessor, tempTable); } } return tempTable; } // allow implicit temp group definition List<ElementSymbol> columns = null; if (command instanceof Insert) { Insert insert = (Insert) command; GroupSymbol group = insert.getGroup(); if (group.isImplicitTempGroupSymbol()) { columns = insert.getVariables(); } } if (columns == null) { if (processors != null) { TableProcessor withProcessor = processors.get(tempTableID); if (withProcessor != null) { LogManager.logDetail( LogConstants.CTX_DQP, "Creating temporary table for with clause", tempTableID); //$NON-NLS-1$ Create create = new Create(); create.setTable(new GroupSymbol(tempTableID)); create.setElementSymbolsAsColumns(withProcessor.columns); tempTable = addTempTable(tempTableID, create, buffer, true, context); buildWithTable(tempTableID, withProcessor, tempTable); return tempTable; } } throw new QueryProcessingException( QueryPlugin.Event.TEIID30226, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30226, tempTableID)); } LogManager.logDetail( LogConstants.CTX_DQP, "Creating temporary table", tempTableID); // $NON-NLS-1$ Create create = new Create(); create.setTable(new GroupSymbol(tempTableID)); create.setElementSymbolsAsColumns(columns); return addTempTable(tempTableID, create, buffer, true, context); }
/** * Get batch from child node Walk through each row of child batch Bind values to insertCommand * Execute insertCommand Update insertCount When no more data is available, output batch with * single row containing insertCount */ public TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException { while (phase == REQUEST_CREATION) { checkExitConditions(); /* If we don't have a batch to work, get the next */ if (currentBatch == null) { if (sourceDone) { phase = RESPONSE_PROCESSING; break; } currentBatch = getChildren()[0].nextBatch(); // can throw BlockedException sourceDone = currentBatch.getTerminationFlag(); this.batchRow = currentBatch.getBeginRow(); // normally we would want to skip a 0 sized batch, but it typically represents the terminal // batch // and for implicit temp tables we need to issue an empty insert if (currentBatch.getRowCount() == 0 && (!currentBatch.getTerminationFlag() || mode != Mode.ITERATOR)) { currentBatch = null; continue; } } int batchSize = currentBatch.getRowCount(); int requests = 1; switch (mode) { case ITERATOR: if (buffer == null) { buffer = getBufferManager() .createTupleBuffer(intoElements, getConnectionID(), TupleSourceType.PROCESSOR); } buffer.addTupleBatch(currentBatch, true); if (currentBatch.getTerminationFlag() && (buffer.getRowCount() != 0 || intoGroup.isImplicitTempGroupSymbol())) { Insert insert = new Insert(intoGroup, intoElements, null); buffer.close(); insert.setTupleSource(buffer.createIndexedTupleSource(true)); // Register insert command against source registerRequest(insert); } else { requests = 0; } break; case BATCH: // Register batched update command against source int endRow = currentBatch.getEndRow(); List<Command> rows = new ArrayList<Command>(endRow - batchRow); for (int rowNum = batchRow; rowNum <= endRow; rowNum++) { Insert insert = new Insert( intoGroup, intoElements, convertValuesToConstants(currentBatch.getTuple(rowNum), intoElements)); rows.add(insert); } registerRequest(new BatchedUpdateCommand(rows)); break; case SINGLE: batchSize = 1; // Register insert command against source // Defect 16036 - submit a new INSERT command to the DataManager. registerRequest( new Insert( intoGroup, intoElements, convertValuesToConstants(currentBatch.getTuple(batchRow), intoElements))); } this.batchRow += batchSize; if (batchRow > currentBatch.getEndRow()) { currentBatch = null; } this.requestsRegistered += requests; } checkExitConditions(); // End this node's work addBatchRow(Arrays.asList(insertCount)); terminateBatches(); return pullBatch(); }
TempTable getOrCreateTempTable( String tempTableID, Command command, BufferManager buffer, boolean delegate, boolean forUpdate, CommandContext context, GroupSymbol group) throws TeiidProcessingException, BlockedException, TeiidComponentException { if (!(group.getMetadataID() instanceof TempMetadataID)) { // TODO: use a proper metadata TempTableStore tts = context.getSessionTempTableStore(); context.setDeterminismLevel(Determinism.SESSION_DETERMINISTIC); if (tts.getTempTable(tempTableID) == null) { // implicitly create global (session scoped) temp table LogManager.logDetail( LogConstants.CTX_DQP, "binding global temp table to session", group); // $NON-NLS-1$ QueryMetadataInterface metadata = context.getMetadata(); Create create = GlobalTableStoreImpl.getCreateCommand(group, false, metadata); tts.addTempTable(tempTableID, create, buffer, true, context); } return getTempTable(tempTableID, command, buffer, delegate, forUpdate, context); } TempTable tempTable = getTempTable(tempTableID, command, buffer, delegate, forUpdate, context); if (tempTable != null) { if (processors != null) { TableProcessor withProcessor = processors.get(tempTableID); if (withProcessor != null) { TempTable tt = withProcessor.process(tempTable); if (tt != tempTable) { return tt; } processors.remove(tempTableID); } } return tempTable; } // allow implicit temp group definition List<ElementSymbol> columns = null; if (command instanceof Insert) { Insert insert = (Insert) command; if (group.isImplicitTempGroupSymbol()) { columns = insert.getVariables(); } } if (columns == null) { if (processors != null) { TableProcessor withProcessor = processors.get(tempTableID); if (withProcessor != null) { LogManager.logDetail( LogConstants.CTX_DQP, "Creating temporary table for with clause", tempTableID); //$NON-NLS-1$ Create create = new Create(); create.setTable(new GroupSymbol(tempTableID)); create.setElementSymbolsAsColumns(withProcessor.columns); withProcessor.alterCreate(create); tempTable = addTempTable(tempTableID, create, buffer, true, context); TempTable tt = withProcessor.process(tempTable); if (tt != tempTable) { return tt; } processors.remove(tempTableID); return tempTable; } } if (delegate && this.parentTempTableStore != null) { // may be a cte from a higher scope that needs to have creation triggered return parentTempTableStore.getOrCreateTempTable( tempTableID, command, buffer, delegate, forUpdate, context, group); } throw new QueryProcessingException( QueryPlugin.Event.TEIID30226, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30226, tempTableID)); } LogManager.logDetail( LogConstants.CTX_DQP, "Creating temporary table", tempTableID); // $NON-NLS-1$ Create create = new Create(); create.setTable(new GroupSymbol(tempTableID)); create.setElementSymbolsAsColumns(columns); return addTempTable(tempTableID, create, buffer, true, context); }