Search in sources :

Example 6 with Create

use of org.teiid.query.sql.lang.Create in project teiid by teiid.

the class TestCreateDrop method testTypeAliases.

@Test
public void testTypeAliases() {
    Create create = new Create();
    // $NON-NLS-1$
    create.setTable(new GroupSymbol("tempTable"));
    List<ElementSymbol> columns = new ArrayList<ElementSymbol>();
    // $NON-NLS-1$
    ElementSymbol column = new ElementSymbol("c1");
    column.setType(DataTypeManager.DefaultDataClasses.STRING);
    columns.add(column);
    // $NON-NLS-1$
    column = new ElementSymbol("c2");
    column.setType(DataTypeManager.DefaultDataClasses.BYTE);
    columns.add(column);
    // $NON-NLS-1$
    column = new ElementSymbol("c3");
    column.setType(DataTypeManager.DefaultDataClasses.SHORT);
    columns.add(column);
    // $NON-NLS-1$
    column = new ElementSymbol("c4");
    column.setType(DataTypeManager.DefaultDataClasses.FLOAT);
    columns.add(column);
    // $NON-NLS-1$
    column = new ElementSymbol("c5");
    column.setType(DataTypeManager.DefaultDataClasses.BIG_DECIMAL);
    columns.add(column);
    create.setElementSymbolsAsColumns(columns);
    // $NON-NLS-1$
    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);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Create(org.teiid.query.sql.lang.Create) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 7 with Create

use of org.teiid.query.sql.lang.Create in project teiid by teiid.

the class TestCreateDrop method testForeignTemp.

@Test
public void testForeignTemp() {
    Create create = new Create();
    // $NON-NLS-1$
    create.setTable(new GroupSymbol("tempTable"));
    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, 0);
    t.addColumn(c);
    c = new Column();
    c.setName("y");
    c.setUUID("tid:0");
    Datatype decimal = SystemMetadata.getInstance().getRuntimeTypeMap().get("decimal");
    c.setDatatype(decimal, true, 0);
    t.addColumn(c);
    t.setCardinality(10000);
    create.setTableMetadata(t);
    // $NON-NLS-1$ //$NON-NLS-2$
    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);
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) Create(org.teiid.query.sql.lang.Create) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Datatype(org.teiid.metadata.Datatype) Test(org.junit.Test)

Example 8 with Create

use of org.teiid.query.sql.lang.Create in project teiid by teiid.

the class TempTableStore method getOrCreateTempTable.

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
            // $NON-NLS-1$
            LogManager.logDetail(LogConstants.CTX_DQP, "binding global temp table to session", group);
            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) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, "Creating temporary table for with clause", tempTableID);
                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));
    }
    // $NON-NLS-1$
    LogManager.logDetail(LogConstants.CTX_DQP, "Creating temporary table", tempTableID);
    Create create = new Create();
    create.setTable(new GroupSymbol(tempTableID));
    create.setElementSymbolsAsColumns(columns);
    return addTempTable(tempTableID, create, buffer, true, context);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Create(org.teiid.query.sql.lang.Create) TempMetadataID(org.teiid.query.metadata.TempMetadataID) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Insert(org.teiid.query.sql.lang.Insert) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 9 with Create

use of org.teiid.query.sql.lang.Create in project teiid by teiid.

the class RulePlaceAccess method addAccessNode.

/**
 * Adds a access node if the node is a source leaf node.
 *
 * @param metadata
 * @param sourceNode
 * @return true if the source node has an access pattern
 * @throws QueryMetadataException
 * @throws TeiidComponentException
 */
private void addAccessNode(QueryMetadataInterface metadata, PlanNode sourceNode, CapabilitiesFinder finder, boolean[] additionalRules) throws QueryMetadataException, TeiidComponentException {
    boolean isInsert = false;
    Object req = sourceNode.getProperty(NodeConstants.Info.ATOMIC_REQUEST);
    if (req == null) {
        req = sourceNode.getProperty(NodeConstants.Info.NESTED_COMMAND);
    }
    if (sourceNode.getProperty(NodeConstants.Info.TABLE_FUNCTION) != null) {
        return;
    }
    if (req instanceof Insert) {
        isInsert = true;
    } else {
        PlanNode parent = sourceNode.getParent();
        if (parent.getType() == NodeConstants.Types.PROJECT && parent.getProperty(NodeConstants.Info.INTO_GROUP) != null) {
            isInsert = true;
        }
    }
    PlanNode apNode = sourceNode;
    if (sourceNode.getChildCount() == 0) {
        // Create the access node and insert
        PlanNode accessNode = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
        accessNode.addGroups(sourceNode.getGroups());
        copyProperties(sourceNode, accessNode);
        SourceHint sourceHint = (SourceHint) sourceNode.removeProperty(Info.SOURCE_HINT);
        // TODO: trim the hint to only the sources possible under this model (typically 1, but could be more in
        // multi-source
        accessNode.setProperty(Info.SOURCE_HINT, sourceHint);
        Object hint = sourceNode.removeProperty(NodeConstants.Info.IS_OPTIONAL);
        if (hint != null) {
            accessNode.setProperty(NodeConstants.Info.IS_OPTIONAL, hint);
        }
        Object modelId = null;
        if (sourceNode.getGroups().size() == 1) {
            GroupSymbol gs = sourceNode.getGroups().iterator().next();
            modelId = gs.getModelMetadataId();
            if (modelId != null) {
                accessNode.setProperty(NodeConstants.Info.MODEL_ID, modelId);
            }
        }
        if (req instanceof Create || req instanceof Drop) {
            modelId = TempMetadataAdapter.TEMP_MODEL;
        } else {
            modelId = RuleRaiseAccess.getModelIDFromAccess(accessNode, metadata);
        }
        if (modelId != null) {
            boolean multiSource = metadata.isMultiSource(modelId);
            if (multiSource) {
                accessNode.setProperty(Info.IS_MULTI_SOURCE, multiSource);
            }
            accessNode.setProperty(NodeConstants.Info.MODEL_ID, modelId);
        }
        if (req == null && modelId != null) {
            // add "conformed" sources if they exist
            GroupSymbol group = sourceNode.getGroups().iterator().next();
            Object gid = group.getMetadataID();
            String sources = metadata.getExtensionProperty(gid, CONFORMED_SOURCES, false);
            if (sources != null) {
                Set<Object> conformed = new LinkedHashSet<Object>();
                conformed.add(modelId);
                for (String source : StringUtil.split(sources, ",")) {
                    // $NON-NLS-1$
                    Object mid = metadata.getModelID(source.trim());
                    if (metadata.isVirtualModel(mid)) {
                        // TODO: could validate this up-front
                        throw new QueryMetadataException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31148, metadata.getName(mid), group));
                    }
                    conformed.add(mid);
                }
                accessNode.setProperty(Info.CONFORMED_SOURCES, conformed);
            }
        }
        // Insert
        sourceNode.addAsParent(accessNode);
        apNode = accessNode;
        // set additional information
        for (GroupSymbol group : accessNode.getGroups()) {
            if (group.getCheckMatViewStatus() != null) {
                LinkedHashSet<Object> viewsToCheck = new LinkedHashSet<Object>();
                viewsToCheck.add(group.getCheckMatViewStatus());
                accessNode.setProperty(Info.CHECK_MAT_VIEW, viewsToCheck);
            }
            Object modelID = metadata.getModelID(group.getMetadataID());
            if (CapabilitiesUtil.requiresCriteria(modelID, metadata, finder)) {
                additionalRules[1] = true;
            }
        }
    }
    // Add access pattern(s), if any, as property of access node
    if (!isInsert && addAccessPatternsProperty(apNode, metadata)) {
        additionalRules[0] = true;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Insert(org.teiid.query.sql.lang.Insert) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) SourceHint(org.teiid.query.sql.lang.SourceHint) Drop(org.teiid.query.sql.lang.Drop) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Create(org.teiid.query.sql.lang.Create) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol)

Example 10 with Create

use of org.teiid.query.sql.lang.Create in project teiid by teiid.

the class ExecDynamicSqlInstruction method process.

/**
 * <p>
 * Processing this instruction executes the ProcessorPlan for the command on
 * the CommandStatement of the update procedure language. Executing this
 * plan does not effect the values of any of the variables defined as part
 * of the update procedure and hence the results of the ProcessPlan
 * execution need not be stored for further processing. The results are
 * removed from the buffer manager immediately after execution. The program
 * counter is incremented after execution of the plan.
 * </p>
 *
 * @throws BlockedException
 *             if this processing the plan throws a currentVarContext
 */
public void process(ProcedurePlan procEnv) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    VariableContext localContext = procEnv.getCurrentVariableContext();
    String query = null;
    try {
        Clob value = (Clob) procEnv.evaluateExpression(dynamicCommand.getSql());
        if (value == null) {
            throw new QueryProcessingException(QueryPlugin.Util.getString(// $NON-NLS-1$
            "ExecDynamicSqlInstruction.0"));
        }
        if (value.length() > MAX_SQL_LENGTH) {
            throw new QueryProcessingException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31204, MAX_SQL_LENGTH));
        }
        query = value.getSubString(1, MAX_SQL_LENGTH);
        LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, // $NON-NLS-1$
        new Object[] { "Executing dynamic sql ", query });
        Command command = QueryParser.getQueryParser().parseCommand(query);
        // special handling for dynamic anon blocks
        if (command instanceof CreateProcedureCommand) {
            if (dynamicCommand.getIntoGroup() != null || returnable) {
                // and the creation of an inline view
                throw new QueryProcessingException(QueryPlugin.Event.TEIID31250, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31250));
            }
            ((CreateProcedureCommand) command).setResultSetColumns(Collections.EMPTY_LIST);
        }
        command.setExternalGroupContexts(dynamicCommand.getExternalGroupContexts());
        command.setTemporaryMetadata(dynamicCommand.getTemporaryMetadata().clone());
        updateContextWithUsingValues(procEnv, localContext);
        TempMetadataStore metadataStore = command.getTemporaryMetadata();
        if (dynamicCommand.getUsing() != null && !dynamicCommand.getUsing().isEmpty()) {
            metadataStore.addTempGroup(Reserved.USING, new LinkedList<ElementSymbol>(dynamicCommand.getUsing().getClauseMap().keySet()));
            GroupSymbol using = new GroupSymbol(Reserved.USING);
            using.setMetadataID(metadataStore.getTempGroupID(Reserved.USING));
            command.addExternalGroupToContext(using);
            metadataStore.addTempGroup(ProcedureReservedWords.DVARS, new LinkedList<ElementSymbol>(dynamicCommand.getUsing().getClauseMap().keySet()));
            using = new GroupSymbol(ProcedureReservedWords.DVARS);
            using.setMetadataID(metadataStore.getTempGroupID(ProcedureReservedWords.DVARS));
            command.addExternalGroupToContext(using);
        }
        QueryResolver.resolveCommand(command, metadata.getDesignTimeMetadata());
        validateDynamicCommand(procEnv, command, value.toString());
        // create a new set of variables including vars
        Map<ElementSymbol, Expression> nameValueMap = createVariableValuesMap(localContext);
        ValidationVisitor visitor = new ValidationVisitor();
        Request.validateWithVisitor(visitor, metadata, command);
        boolean insertInto = false;
        boolean updateCommand = false;
        if (!command.returnsResultSet() && !(command instanceof StoredProcedure)) {
            if (dynamicCommand.isAsClauseSet()) {
                if (dynamicCommand.getProjectedSymbols().size() != 1 || ((Expression) dynamicCommand.getProjectedSymbols().get(0)).getType() != DataTypeManager.DefaultDataClasses.INTEGER) {
                    throw new QueryProcessingException(QueryPlugin.Event.TEIID31157, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31157));
                }
            }
            updateCommand = true;
        } else if (dynamicCommand.getAsColumns() != null && !dynamicCommand.getAsColumns().isEmpty()) {
            // $NON-NLS-1$
            command = QueryRewriter.createInlineViewQuery(new GroupSymbol("X"), command, metadata, dynamicCommand.getAsColumns());
            if (dynamicCommand.getIntoGroup() != null) {
                Insert insert = new Insert(dynamicCommand.getIntoGroup(), dynamicCommand.getAsColumns(), Collections.emptyList());
                insert.setQueryExpression((Query) command);
                command = insert;
                insertInto = true;
            }
        }
        // if this is an update procedure, it could reassign variables
        command = QueryRewriter.rewrite(command, metadata, procEnv.getContext(), command instanceof CreateProcedureCommand ? Collections.EMPTY_MAP : nameValueMap);
        ProcessorPlan commandPlan = QueryOptimizer.optimizePlan(command, metadata, idGenerator, capFinder, AnalysisRecord.createNonRecordingRecord(), procEnv.getContext());
        if (command instanceof CreateProcedureCommand && commandPlan instanceof ProcedurePlan) {
            ((ProcedurePlan) commandPlan).setValidateAccess(procEnv.isValidateAccess());
        }
        CreateCursorResultSetInstruction inst = new CreateCursorResultSetInstruction(null, commandPlan, (insertInto || updateCommand) ? Mode.UPDATE : returnable ? Mode.HOLD : Mode.NOHOLD) {

            @Override
            public void process(ProcedurePlan procEnv) throws BlockedException, TeiidComponentException, TeiidProcessingException {
                boolean done = true;
                try {
                    super.process(procEnv);
                } catch (BlockedException e) {
                    done = false;
                    throw e;
                } finally {
                    if (done) {
                        procEnv.getContext().popCall();
                    }
                }
            }
        };
        dynamicProgram = new Program(false);
        dynamicProgram.addInstruction(inst);
        if (dynamicCommand.getIntoGroup() != null) {
            String groupName = dynamicCommand.getIntoGroup().getName();
            if (!procEnv.getTempTableStore().hasTempTable(groupName, true)) {
                // create the temp table in the parent scope
                Create create = new Create();
                create.setTable(new GroupSymbol(groupName));
                for (ElementSymbol es : (List<ElementSymbol>) dynamicCommand.getAsColumns()) {
                    Column c = new Column();
                    c.setName(es.getShortName());
                    c.setRuntimeType(DataTypeManager.getDataTypeName(es.getType()));
                    create.getColumns().add(c);
                }
                procEnv.getDataManager().registerRequest(procEnv.getContext(), create, TempMetadataAdapter.TEMP_MODEL.getName(), new RegisterRequestParameter());
            }
            // backwards compatibility to support into with a rowcount
            if (updateCommand) {
                Insert insert = new Insert();
                insert.setGroup(new GroupSymbol(groupName));
                for (ElementSymbol es : (List<ElementSymbol>) dynamicCommand.getAsColumns()) {
                    ElementSymbol col = new ElementSymbol(es.getShortName(), insert.getGroup());
                    col.setType(es.getType());
                    insert.addVariable(col);
                }
                insert.addValue(new Constant(procEnv.getCurrentVariableContext().getValue(ProcedurePlan.ROWCOUNT)));
                QueryResolver.resolveCommand(insert, metadata.getDesignTimeMetadata());
                TupleSource ts = procEnv.getDataManager().registerRequest(procEnv.getContext(), insert, TempMetadataAdapter.TEMP_MODEL.getName(), new RegisterRequestParameter());
                ts.nextTuple();
                ts.closeSource();
            }
        }
        // Add group to recursion stack
        if (parentProcCommand.getUpdateType() != Command.TYPE_UNKNOWN) {
            // $NON-NLS-1$
            procEnv.getContext().pushCall(Command.getCommandToken(parentProcCommand.getUpdateType()) + " " + parentProcCommand.getVirtualGroup());
        } else {
            if (parentProcCommand.getVirtualGroup() != null) {
                procEnv.getContext().pushCall(parentProcCommand.getVirtualGroup().toString());
            }
        }
        procEnv.push(dynamicProgram);
    } catch (SQLException e) {
        Object[] params = { dynamicCommand, dynamicCommand.getSql(), e.getMessage() };
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30168, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30168, params));
    } catch (TeiidProcessingException e) {
        Object[] params = { dynamicCommand, query == null ? dynamicCommand.getSql() : query, e.getMessage() };
        throw new QueryProcessingException(QueryPlugin.Event.TEIID30168, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30168, params));
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ValidationVisitor(org.teiid.query.validator.ValidationVisitor) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) Constant(org.teiid.query.sql.symbol.Constant) Insert(org.teiid.query.sql.lang.Insert) BlockedException(org.teiid.common.buffer.BlockedException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Column(org.teiid.metadata.Column) Create(org.teiid.query.sql.lang.Create) List(java.util.List) LinkedList(java.util.LinkedList) VariableContext(org.teiid.query.sql.util.VariableContext) RegisterRequestParameter(org.teiid.query.processor.RegisterRequestParameter) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) DynamicCommand(org.teiid.query.sql.lang.DynamicCommand) Expression(org.teiid.query.sql.symbol.Expression) TupleSource(org.teiid.common.buffer.TupleSource) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Clob(java.sql.Clob) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Aggregations

Create (org.teiid.query.sql.lang.Create)16 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)11 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)5 List (java.util.List)3 Insert (org.teiid.query.sql.lang.Insert)3 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)2 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)2 Column (org.teiid.metadata.Column)2 TempMetadataID (org.teiid.query.metadata.TempMetadataID)2 CacheHint (org.teiid.query.sql.lang.CacheHint)2 Drop (org.teiid.query.sql.lang.Drop)2 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1