@Test public void testForeignTemp() { Create create = new Create(); create.setTable(new GroupSymbol("tempTable")); // $NON-NLS-1$ create.setOn("source"); Table t = new Table(); t.setName("tempTable"); t.setUUID("tid:0"); Column c = new Column(); c.setName("x"); c.setUUID("tid:0"); Datatype string = SystemMetadata.getInstance().getRuntimeTypeMap().get("string"); c.setDatatype(string, true); t.addColumn(c); c = new Column(); c.setName("y"); c.setUUID("tid:0"); Datatype decimal = SystemMetadata.getInstance().getRuntimeTypeMap().get("decimal"); c.setDatatype(decimal, true); t.addColumn(c); t.setCardinality(10000); create.setTableMetadata(t); helpTest( "create foreign temporary table tempTable (x string, y decimal) options (cardinality 10000) on source", "CREATE FOREIGN TEMPORARY TABLE tempTable (\n x string,\n y bigdecimal\n) OPTIONS (CARDINALITY 10000) ON 'source'", create); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testTypeAliases() { Create create = getFactory().newCreate(); create.setTable(getFactory().newGroupSymbol("tempTable")); // $NON-NLS-1$ List<ElementSymbol> columns = new ArrayList<ElementSymbol>(); ElementSymbol column = getFactory().newElementSymbol("c1"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.STRING.getTypeClass()); columns.add(column); column = getFactory().newElementSymbol("c2"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.BYTE.getTypeClass()); columns.add(column); column = getFactory().newElementSymbol("c3"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.SHORT.getTypeClass()); columns.add(column); column = getFactory().newElementSymbol("c4"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.FLOAT.getTypeClass()); columns.add(column); column = getFactory().newElementSymbol("c5"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.BIG_DECIMAL.getTypeClass()); columns.add(column); create.setElementSymbolsAsColumns(columns); helpTest( "Create local TEMPORARY table tempTable (c1 varchar, c2 tinyint, c3 smallint, c4 real, c5 decimal)", "CREATE LOCAL TEMPORARY TABLE tempTable (c1 varchar, c2 tinyint, c3 smallint, c4 real, c5 decimal)", create); //$NON-NLS-1$ }
TempTable addTempTable( final String tempTableName, Create create, BufferManager buffer, boolean add, CommandContext context) throws TeiidProcessingException { List<ElementSymbol> columns = create.getColumnSymbols(); TempMetadataID id = tempMetadataStore.getTempGroupID(tempTableName); getSynchronization(context); if (id == null) { // add metadata id = tempMetadataStore.addTempGroup(tempTableName, columns, false, true); TempTableResolver.addAdditionalMetadata(create, id); } columns = new ArrayList<ElementSymbol>(create.getColumnSymbols()); if (!create.getPrimaryKey().isEmpty()) { // reorder the columns to put the key in front List<ElementSymbol> primaryKey = create.getPrimaryKey(); columns.removeAll(primaryKey); columns.addAll(0, primaryKey); } final TempTable tempTable = new TempTable(id, buffer, columns, create.getPrimaryKey().size(), sessionID); if (add) { tempTables.put(tempTableName, tempTable); } return tempTable; }
@Override public void visit(Create obj) { preVisitVisitor(obj); visitNode(obj.getTable()); visitNodes(obj.getColumnSymbols()); visitNodes(obj.getPrimaryKey()); postVisitVisitor(obj); }
void addForeignTempTable(final String tempTableName, Create create) { TempMetadataID id = tempMetadataStore.getTempGroupID(tempTableName); if (id == null) { id = tempMetadataStore.addTempGroup(tempTableName, create.getColumnSymbols(), false, true); id.setOriginalMetadataID(create.getTableMetadata()); id.getTableData().setModel(create.getTableMetadata().getParent()); } this.foreignTempTables.put(tempTableName, create.getTableMetadata()); }
public static final Create sample2() { Create create = new Create(); create.setTable(new GroupSymbol("temp_table2")); // $NON-NLS-1$ List elements = new ArrayList(); elements.add(new ElementSymbol("a")); // $NON-NLS-1$ elements.add(new ElementSymbol("b")); // $NON-NLS-1$ create.setElementSymbolsAsColumns(elements); return create; }
@Test public void testCreateTempTable1() { Create create = getFactory().newCreate(); create.setTable(getFactory().newGroupSymbol("tempTable")); // $NON-NLS-1$ List<ElementSymbol> columns = new ArrayList<ElementSymbol>(); ElementSymbol column = getFactory().newElementSymbol("c1"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.BOOLEAN.getTypeClass()); columns.add(column); column = getFactory().newElementSymbol("c2"); // $NON-NLS-1$ column.setType(DataTypeManagerService.DefaultDataTypes.BYTE.getTypeClass()); columns.add(column); create.setElementSymbolsAsColumns(columns); helpTest( "Create local TEMPORARY table tempTable (c1 boolean, c2 byte)", "CREATE LOCAL TEMPORARY TABLE tempTable (c1 boolean, c2 byte)", create); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testCreateTempTable2() { Create create = new Create(); create.setTable(new GroupSymbol("tempTable")); // $NON-NLS-1$ List<ElementSymbol> columns = new ArrayList<ElementSymbol>(); ElementSymbol column = new ElementSymbol("c1"); // $NON-NLS-1$ column.setType(DataTypeManager.DefaultDataClasses.BOOLEAN); columns.add(column); column = new ElementSymbol("c2"); // $NON-NLS-1$ column.setType(DataTypeManager.DefaultDataClasses.BYTE); columns.add(column); create.setElementSymbolsAsColumns(columns); create.getColumns().get(0).setNullType(NullType.No_Nulls); helpTest( "Create local TEMPORARY table tempTable(c1 boolean not null, c2 byte)", "CREATE LOCAL TEMPORARY TABLE tempTable (c1 boolean NOT NULL, c2 byte)", create); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testCreateTempTable1() { Create create = new Create(); create.setTable(new GroupSymbol("tempTable")); // $NON-NLS-1$ List<ElementSymbol> columns = new ArrayList<ElementSymbol>(); ElementSymbol column = new ElementSymbol("c1"); // $NON-NLS-1$ column.setType(DataTypeManager.DefaultDataClasses.BOOLEAN); columns.add(column); column = new ElementSymbol("c2"); // $NON-NLS-1$ column.setType(DataTypeManager.DefaultDataClasses.BYTE); columns.add(column); create.setElementSymbolsAsColumns(columns); create.setCommitAction(CommitAction.PRESERVE_ROWS); helpTest( "Create local TEMPORARY table tempTable (c1 boolean, c2 byte) on commit preserve rows", "CREATE LOCAL TEMPORARY TABLE tempTable (c1 boolean, c2 byte) ON COMMIT PRESERVE ROWS", create); //$NON-NLS-1$ //$NON-NLS-2$ }
@Test public void testCreateTempTableWithPrimaryKey() { Create create = new Create(); create.setTable(new GroupSymbol("tempTable")); // $NON-NLS-1$ List<ElementSymbol> columns = new ArrayList<ElementSymbol>(); ElementSymbol column = new ElementSymbol("c1"); // $NON-NLS-1$ column.setType(DataTypeManager.DefaultDataClasses.BOOLEAN); columns.add(column); column = new ElementSymbol("c2"); // $NON-NLS-1$ column.setType(DataTypeManager.DefaultDataClasses.BYTE); columns.add(column); create.setElementSymbolsAsColumns(columns); create.getPrimaryKey().add(column); helpTest( "Create local TEMPORARY table tempTable(c1 boolean, c2 byte, primary key (c2))", "CREATE LOCAL TEMPORARY TABLE tempTable (c1 boolean, c2 byte, PRIMARY KEY(c2))", create); //$NON-NLS-1$ //$NON-NLS-2$ }
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); }
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); }
@Override public void alterCreate(Create create) { if (!all) { create.getPrimaryKey().addAll(create.getColumnSymbols()); } }
TupleSource registerRequest(final CommandContext context, String modelName, final Command command) throws TeiidComponentException, TeiidProcessingException { final TempTableStore contextStore = context.getTempTableStore(); if (command instanceof Query) { Query query = (Query) command; if (modelName != null && !modelName.equals(TempMetadataAdapter.TEMP_MODEL.getID())) { return null; } return registerQuery(context, contextStore, query); } if (command instanceof ProcedureContainer) { if (command instanceof StoredProcedure) { StoredProcedure proc = (StoredProcedure) command; if (CoreConstants.SYSTEM_ADMIN_MODEL.equals(modelName)) { TupleSource result = handleSystemProcedures(context, proc); if (result != null) { return result; } } else if (proc.getGroup().isGlobalTable()) { return handleCachedProcedure(context, proc); } return null; // it's not a stored procedure we want to handle } final GroupSymbol group = ((ProcedureContainer) command).getGroup(); if (!modelName.equals(TempMetadataAdapter.TEMP_MODEL.getID()) || !group.isTempGroupSymbol()) { return null; } return new ProxyTupleSource() { @Override protected TupleSource createTupleSource() throws TeiidComponentException, TeiidProcessingException { final String groupKey = group.getNonCorrelationName(); final TempTable table = contextStore.getOrCreateTempTable( groupKey, command, bufferManager, true, true, context, group); if (command instanceof Insert) { Insert insert = (Insert) command; TupleSource ts = insert.getTupleSource(); if (ts == null) { Evaluator eval = new Evaluator(Collections.emptyMap(), TempTableDataManager.this, context); List<Object> values = new ArrayList<Object>(insert.getValues().size()); for (Expression expr : (List<Expression>) insert.getValues()) { values.add(eval.evaluate(expr, null)); } ts = new CollectionTupleSource(Arrays.asList(values).iterator()); } return table.insert(ts, insert.getVariables(), true, context); } if (command instanceof Update) { final Update update = (Update) command; final Criteria crit = update.getCriteria(); return table.update(crit, update.getChangeList()); } if (command instanceof Delete) { final Delete delete = (Delete) command; final Criteria crit = delete.getCriteria(); if (crit == null) { // TODO: we'll add a real truncate later int rows = table.truncate(false); return CollectionTupleSource.createUpdateCountTupleSource(rows); } return table.delete(crit); } throw new AssertionError("unknown command " + command); // $NON-NLS-1$ } }; } if (command instanceof Create) { Create create = (Create) command; String tempTableName = create.getTable().getName(); if (contextStore.hasTempTable(tempTableName)) { throw new QueryProcessingException( QueryPlugin.Event.TEIID30229, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30229, tempTableName)); } if (create.getTableMetadata() != null) { contextStore.addForeignTempTable(tempTableName, create); } else { contextStore.addTempTable(tempTableName, create, bufferManager, true, context); } return CollectionTupleSource.createUpdateCountTupleSource(0); } if (command instanceof Drop) { String tempTableName = ((Drop) command).getTable().getName(); contextStore.removeTempTableByName(tempTableName, context); return CollectionTupleSource.createUpdateCountTupleSource(0); } if (command instanceof AlterTempTable) { AlterTempTable att = (AlterTempTable) command; TempTable tt = contextStore.getTempTable(att.getTempTable()); Assertion.isNotNull(tt, "Table doesn't exist"); // $NON-NLS-1$ tt.setUpdatable(false); if (att.getIndexColumns() != null && tt.getRowCount() > 2 * tt.getTree().getPageSize(true)) { for (List<ElementSymbol> cols : att.getIndexColumns()) { tt.addIndex(cols, false); } } return CollectionTupleSource.createUpdateCountTupleSource(0); } return null; }