Java Code Examples for org.apache.calcite.schema.Table#getJdbcTableType()

The following examples show how to use org.apache.calcite.schema.Table#getJdbcTableType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaHandler.java    From Bats with Apache License 2.0 6 votes vote down vote up
WorkspaceSchemaFactory.WorkspaceSchema getWorkspaceSchema(List<String> tableSchema, String tableName) {
  SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
  AbstractSchema temporarySchema = SchemaUtilites.resolveToTemporarySchema(tableSchema, defaultSchema, context.getConfig());

  if (context.getSession().isTemporaryTable(temporarySchema, context.getConfig(), tableName)) {
    produceErrorResult(String.format("Indicated table [%s] is temporary table", tableName), true);
  }

  AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, tableSchema);
  Table table = SqlHandlerUtil.getTableFromSchema(drillSchema, tableName);
  if (table == null || table.getJdbcTableType() != Schema.TableType.TABLE) {
    produceErrorResult(String.format("Table [%s] was not found", tableName), true);
  }

  if (!(drillSchema instanceof WorkspaceSchemaFactory.WorkspaceSchema)) {
    produceErrorResult(String.format("Table [`%s`.`%s`] must belong to file storage plugin",
      drillSchema.getFullSchemaName(), tableName), true);
  }

  Preconditions.checkState(drillSchema instanceof WorkspaceSchemaFactory.WorkspaceSchema);
  return (WorkspaceSchemaFactory.WorkspaceSchema) drillSchema;
}
 
Example 2
Source File: InfoSchemaRecordGenerator.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Visit the tables in the given schema. The
 * @param  schemaPath  the path to the given schema
 * @param  schema  the given schema
 */
public void visitTables(String schemaPath, SchemaPlus schema) {
  final AbstractSchema drillSchema = schema.unwrap(AbstractSchema.class);
  for (Pair<String, ? extends Table> tableNameToTable : drillSchema.getTablesByNames(schema.getTableNames())) {
    final String tableName = tableNameToTable.getKey();
    final Table table = tableNameToTable.getValue();
    final TableType tableType = table.getJdbcTableType();
    // Visit the table, and if requested ...
    if(shouldVisitTable(schemaPath, tableName, tableType) && visitTable(schemaPath, tableName, table)) {
      // ... do for each of the table's fields.
      final RelDataType tableRow = table.getRowType(new JavaTypeFactoryImpl(DRILL_REL_DATATYPE_SYSTEM));
      for (RelDataTypeField field: tableRow.getFieldList()) {
        if (shouldVisitColumn(schemaPath, tableName, field.getName())) {
          visitField(schemaPath, tableName, field);
        }
      }
    }
  }
}
 
Example 3
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id) {
	// Resolve identifier as a table.
	final SqlValidatorScope.ResolvedImpl resolved =
		new SqlValidatorScope.ResolvedImpl();
	scope.resolveTable(id.names, catalogReader.nameMatcher(),
		SqlValidatorScope.Path.EMPTY, resolved);
	if (resolved.count() != 1) {
		throw newValidationError(id, RESOURCE.tableNameNotFound(id.toString()));
	}
	// We've found a table. But is it a sequence?
	final SqlValidatorNamespace ns = resolved.only().namespace;
	if (ns instanceof TableNamespace) {
		final Table table = ns.getTable().unwrap(Table.class);
		switch (table.getJdbcTableType()) {
			case SEQUENCE:
			case TEMPORARY_SEQUENCE:
				return;
		}
	}
	throw newValidationError(id, RESOURCE.notASequence(id.toString()));
}
 
Example 4
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
public void validateSequenceValue(SqlValidatorScope scope, SqlIdentifier id) {
	// Resolve identifier as a table.
	final SqlValidatorScope.ResolvedImpl resolved =
		new SqlValidatorScope.ResolvedImpl();
	scope.resolveTable(id.names, catalogReader.nameMatcher(),
		SqlValidatorScope.Path.EMPTY, resolved);
	if (resolved.count() != 1) {
		throw newValidationError(id, RESOURCE.tableNameNotFound(id.toString()));
	}
	// We've found a table. But is it a sequence?
	final SqlValidatorNamespace ns = resolved.only().namespace;
	if (ns instanceof TableNamespace) {
		final Table table = ns.getTable().unwrap(Table.class);
		switch (table.getJdbcTableType()) {
			case SEQUENCE:
			case TEMPORARY_SEQUENCE:
				return;
		}
	}
	throw newValidationError(id, RESOURCE.notASequence(id.toString()));
}
 
Example 5
Source File: ViewHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Validates if view can be created in indicated schema:
 * checks if object (persistent / temporary table) with the same name exists
 * or if view with the same name exists but replace flag is not set
 * or if object with the same name exists but if not exists flag is set.
 *
 * @param drillSchema schema where views will be created
 * @param view create view call
 * @param context query context
 * @return if view can be created in indicated schema
 * @throws UserException if view cannot be created in indicated schema and no duplicate check requested
 */
private boolean checkViewCreationPossibility(AbstractSchema drillSchema, SqlCreateView view, QueryContext context) {
  final String schemaPath = drillSchema.getFullSchemaName();
  final String viewName = view.getName();
  final Table table = SqlHandlerUtil.getTableFromSchema(drillSchema, viewName);

  final boolean isTable = (table != null && table.getJdbcTableType() != Schema.TableType.VIEW)
    || context.getSession().isTemporaryTable(drillSchema, context.getConfig(), viewName);
  final boolean isView = (table != null && table.getJdbcTableType() == Schema.TableType.VIEW);

  switch (view.getSqlCreateType()) {
    case SIMPLE:
      if (isTable) {
        throw UserException
          .validationError()
          .message("A non-view table with given name [%s] already exists in schema [%s]", viewName, schemaPath)
          .build(logger);
      } else if (isView) {
        throw UserException
          .validationError()
          .message("A view with given name [%s] already exists in schema [%s]", viewName, schemaPath)
          .build(logger);
      }
      break;
    case OR_REPLACE:
      if (isTable) {
        throw UserException
          .validationError()
          .message("A non-view table with given name [%s] already exists in schema [%s]", viewName, schemaPath)
          .build(logger);
      }
      break;
    case IF_NOT_EXISTS:
      if (isTable || isView) {
        return false;
      }
      break;
  }
  return true;
}
 
Example 6
Source File: ViewHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws IOException, ForemanSetupException {
  SqlDropView dropView = unwrap(sqlNode, SqlDropView.class);
  final String viewName = FileSelection.removeLeadingSlash(dropView.getName());
  final AbstractSchema drillSchema =
      SchemaUtilites.resolveToMutableDrillSchema(context.getNewDefaultSchema(), dropView.getSchemaPath());

  final String schemaPath = drillSchema.getFullSchemaName();

  final Table viewToDrop = SqlHandlerUtil.getTableFromSchema(drillSchema, viewName);
  if (dropView.checkViewExistence()) {
    if (viewToDrop == null || viewToDrop.getJdbcTableType() != Schema.TableType.VIEW){
      return DirectPlan.createDirectPlan(context, false,
          String.format("View [%s] not found in schema [%s].", viewName, schemaPath));
    }
  } else {
    if (viewToDrop != null && viewToDrop.getJdbcTableType() != Schema.TableType.VIEW) {
      throw UserException.validationError()
          .message("[%s] is not a VIEW in schema [%s]", viewName, schemaPath)
          .build(logger);
    } else if (viewToDrop == null) {
      throw UserException.validationError()
          .message("Unknown view [%s] in schema [%s].", viewName, schemaPath)
          .build(logger);
    }
  }

  SqlHandlerUtil.dropViewFromSchema(drillSchema, viewName);

  return DirectPlan.createDirectPlan(context, true,
      String.format("View [%s] deleted successfully from schema [%s].", viewName, schemaPath));
}
 
Example 7
Source File: InfoSchemaRecordGenerator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitTable(String schemaName, String tableName, Table table) {
  Preconditions.checkNotNull(table, "Error. Table %s.%s provided is null.", schemaName, tableName);

  // skip over unknown table types
  if (table.getJdbcTableType() != null) {
    records.add(new Records.Table(IS_CATALOG_NAME, schemaName, tableName,
        table.getJdbcTableType().toString()));
  }

  return false;
}
 
Example 8
Source File: InfoSchemaRecordGenerator.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visitTable(String schemaName, String tableName, Table table) {
  if (table.getJdbcTableType() == TableType.VIEW) {
    records.add(new Records.View(IS_CATALOG_NAME, schemaName, tableName,
                ((DrillViewInfoProvider) table).getViewSql()));
  }
  return false;
}
 
Example 9
Source File: CalciteMetaImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
CalciteMetaTable(Table calciteTable, String tableCat,
    String tableSchem, String tableName) {
  super(tableCat, tableSchem, tableName,
      calciteTable.getJdbcTableType().jdbcName);
  this.calciteTable = Objects.requireNonNull(calciteTable);
}
 
Example 10
Source File: UserSession.java    From Bats with Apache License 2.0 3 votes vote down vote up
/**
 * Checks if passed table is temporary, table name is case-insensitive.
 * Before looking for table checks if passed schema is temporary and returns false if not
 * since temporary tables are allowed to be created in temporary workspace only.
 * If passed workspace is temporary, looks for temporary table.
 * First checks if table name is among temporary tables, if not returns false.
 * If temporary table named was resolved, checks that temporary table exists on disk,
 * to ensure that temporary table actually exists and resolved table name is not orphan
 * (for example, in result of unsuccessful temporary table creation).
 *
 * @param drillSchema table schema
 * @param config drill config
 * @param tableName original table name
 * @return true if temporary table exists in schema, false otherwise
 */
public boolean isTemporaryTable(AbstractSchema drillSchema, DrillConfig config, String tableName) {
  if (drillSchema == null || !SchemaUtilites.isTemporaryWorkspace(drillSchema.getFullSchemaName(), config)) {
    return false;
  }
  String temporaryTableName = resolveTemporaryTableName(tableName);
  if (temporaryTableName != null) {
    Table temporaryTable = SqlHandlerUtil.getTableFromSchema(drillSchema, temporaryTableName);
    if (temporaryTable != null && temporaryTable.getJdbcTableType() == Schema.TableType.TABLE) {
      return true;
    }
  }
  return false;
}