private void validateUpdatePlan(
      ModelMetaData model,
      ValidatorReport report,
      IQueryMetadataInterface metadata,
      Table t,
      String plan,
      int type)
      throws Exception {

    Command command = null;
    QueryResolver queryResolver = new QueryResolver(queryParser);
    if (isTeiidOrGreater(Version.TEIID_8_12_4)) {
      command = queryParser.parseProcedure(plan, true);
      SyntaxFactory factory = new SyntaxFactory(queryParser.getTeiidVersion());
      IGroupSymbol groupSymbol = factory.createGroupSymbol(t.getFullName());
      queryResolver.resolveCommand(command, (GroupSymbol) groupSymbol, type, metadata, false);
    } else {
      command = queryParser.parseCommand(plan);
      queryResolver.resolveCommand(command, metadata);
    }

    // determineDependencies(t, command); -- these should be tracked against triggers
    ValidatorReport resolverReport = new Validator().validate(command, metadata);
    processReport(model, t, report, resolverReport);
  }
  @Test
  public void testSchema() throws Exception {
    translator = new ODataExecutionFactory();
    translator.start();

    MetadataFactory mf = tripPinMetadata();
    TransformationMetadata metadata = getTransformationMetadata(mf, this.translator);

    String ddl = DDLStringVisitor.getDDLString(mf.getSchema(), null, null);
    // System.out.println(ddl);

    MetadataFactory mf2 =
        new MetadataFactory(
            null,
            1,
            "northwind",
            SystemMetadata.getInstance().getRuntimeTypeMap(),
            new Properties(),
            null);
    QueryParser.getQueryParser().parseDDL(mf2, ddl);

    Procedure p = mf.getSchema().getProcedure("ResetDataSource");
    assertNotNull(p);
    assertEquals(0, p.getParameters().size());
  }
 /** Ensures that views are named with v_ even without metadata */
 @Test
 public void testViewAliasing() throws Exception {
   String sql = "select y.e1 from (select pm1.g1.e1 from pm1.g1) y"; // $NON-NLS-1$
   Query command = (Query) QueryParser.getQueryParser().parseCommand(sql);
   ((ElementSymbol) command.getSelect().getSymbol(0))
       .setGroupSymbol(new GroupSymbol("y")); // $NON-NLS-1$
   command.acceptVisitor(new AliasGenerator(true));
   assertEquals(
       "SELECT v_0.c_0 FROM (SELECT g_0.e1 AS c_0 FROM pm1.g1 AS g_0) AS v_0",
       command.toString()); // $NON-NLS-1$
 }
示例#4
0
 private static Command helpGetCommand(String sql, QueryMetadataInterface metadata)
     throws Exception {
   Command command = QueryParser.getQueryParser().parseCommand(sql);
   QueryResolver.resolveCommand(command, metadata);
   return command;
 }
  private void validate(
      VDBMetaData vdb,
      ModelMetaData model,
      AbstractMetadataRecord record,
      ValidatorReport report,
      IQueryMetadataInterface metadata,
      MetadataFactory mf) {
    ValidatorReport resolverReport = null;
    try {
      if (record instanceof Procedure) {
        Procedure p = (Procedure) record;
        Command command = queryParser.parseProcedure(p.getQueryPlan(), false);
        GroupSymbol gs = createASTNode(ASTNodes.GROUP_SYMBOL);
        gs.setName(p.getFullName());
        QueryResolver resolver = new QueryResolver(queryParser);
        resolver.resolveCommand(command, gs, ICommand.TYPE_STORED_PROCEDURE, metadata, false);
        Validator validator = new Validator();
        resolverReport = validator.validate(command, metadata);
        determineDependencies(p, command);
      } else if (record instanceof Table) {
        Table t = (Table) record;

        GroupSymbol symbol = createASTNode(ASTNodes.GROUP_SYMBOL);
        symbol.setName(t.getFullName());
        ResolverUtil.resolveGroup(symbol, metadata);
        String selectTransformation = t.getSelectTransformation();

        boolean columnsIsEmpty = t.getColumns() == null || t.getColumns().isEmpty();

        // Consider columns if teid 8.11 or lower
        boolean considerColumns_811 =
            isTeiidOrGreater(Version.TEIID_8_12_4) ? true : columnsIsEmpty;
        // Consider columns if teiid 8.12.4+
        boolean considerColumns_8124 =
            isTeiidOrGreater(Version.TEIID_8_12_4) ? columnsIsEmpty : true;

        if (t.isVirtual() && considerColumns_811) {
          QueryCommand command = (QueryCommand) queryParser.parseCommand(selectTransformation);
          QueryResolver resolver = new QueryResolver(queryParser);
          resolver.resolveCommand(command, metadata);
          Validator validator = new Validator();
          resolverReport = validator.validate(command, metadata);

          if (!resolverReport.hasItems() && considerColumns_8124) {
            List<Expression> symbols = command.getProjectedSymbols();
            for (Expression column : symbols) {
              try {
                addColumn(Symbol.getShortName(column), column.getType(), t, mf);
              } catch (Exception e) {
                log(report, model, e.getMessage());
              }
            }
          }

          if (considerColumns_8124) {
            determineDependencies(t, command);
            if (t.getInsertPlan() != null && t.isInsertPlanEnabled()) {
              validateUpdatePlan(
                  model, report, metadata, t, t.getInsertPlan(), Command.TYPE_INSERT);
            }
            if (t.getUpdatePlan() != null && t.isUpdatePlanEnabled()) {
              validateUpdatePlan(
                  model, report, metadata, t, t.getUpdatePlan(), Command.TYPE_UPDATE);
            }
            if (t.getDeletePlan() != null && t.isDeletePlanEnabled()) {
              validateUpdatePlan(
                  model, report, metadata, t, t.getDeletePlan(), Command.TYPE_DELETE);
            }
          }
        }

        boolean addCacheHint = false;
        if (t.isMaterialized() && t.getMaterializedTable() == null) {
          List<KeyRecord> fbis = t.getFunctionBasedIndexes();
          List<GroupSymbol> groups = Arrays.asList(symbol);
          if (fbis != null && !fbis.isEmpty()) {
            for (KeyRecord fbi : fbis) {
              for (int j = 0; j < fbi.getColumns().size(); j++) {
                Column c = fbi.getColumns().get(j);
                if (c.getParent() != fbi) {
                  continue;
                }
                String exprString = c.getNameInSource();
                try {
                  Expression ex = queryParser.parseExpression(exprString);
                  ResolverVisitor resolverVisitor = new ResolverVisitor(teiidVersion);
                  resolverVisitor.resolveLanguageObject(ex, groups, metadata);
                  if (!ValueIteratorProviderCollectorVisitor.getValueIteratorProviders(ex)
                      .isEmpty()) {
                    log(
                        report,
                        model,
                        Messages.gs(Messages.TEIID.TEIID31114, exprString, fbi.getFullName()));
                  }
                  EvaluatableVisitor ev = new EvaluatableVisitor(teiidVersion);
                  PreOrPostOrderNavigator.doVisit(ex, ev, PostOrderNavigator.PRE_ORDER);
                  if (ev.getDeterminismLevel().compareTo(Determinism.VDB_DETERMINISTIC) < 0) {
                    log(
                        report,
                        model,
                        Messages.gs(Messages.TEIID.TEIID31115, exprString, fbi.getFullName()));
                  }
                } catch (QueryResolverException e) {
                  log(
                      report,
                      model,
                      Messages.gs(
                          Messages.TEIID.TEIID31116,
                          exprString,
                          fbi.getFullName(),
                          e.getMessage()));
                }
              }
            }
          }
        } else {
          addCacheHint = true;
        }

        // this seems to parse, resolve and validate.
        QueryResolver resolver = new QueryResolver(queryParser);
        QueryNode node =
            resolver.resolveView(
                symbol,
                new QueryNode(t.getSelectTransformation()),
                SQLConstants.Reserved.SELECT,
                metadata);
        CacheHint cacheHint = node.getCommand().getCacheHint();
        Long ttl = -1L;
        if (cacheHint != null
            && cacheHint.getTtl() != null
            && addCacheHint
            && t.getProperty(MATVIEW_TTL, false) == null) {
          ttl = cacheHint.getTtl();
          t.setProperty(MATVIEW_TTL, String.valueOf(ttl));
        }
      }
      if (resolverReport != null && resolverReport.hasItems()) {
        for (ValidatorFailure v : resolverReport.getItems()) {
          log(
              report,
              model,
              v.getStatus() == IValidatorFailure.VFStatus.ERROR ? Severity.ERROR : Severity.WARNING,
              v.getMessage());
        }
      }
      processReport(model, record, report, resolverReport);
    } catch (Exception e) {
      log(
          report,
          model,
          Messages.gs(Messages.TEIID.TEIID31080, record.getFullName(), e.getMessage()));
    }
  }