org.apache.calcite.schema.Schema Java Examples

The following examples show how to use org.apache.calcite.schema.Schema. 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: CsvTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1031">[CALCITE-1031]
 * In prepared statement, CsvScannableTable.scan is called twice</a>. To see
 * the bug, place a breakpoint in CsvScannableTable.scan, and note that it is
 * called twice. It should only be called once. */
@Test void testPrepared() throws SQLException {
  final Properties properties = new Properties();
  properties.setProperty("caseSensitive", "true");
  try (Connection connection =
      DriverManager.getConnection("jdbc:calcite:", properties)) {
    final CalciteConnection calciteConnection = connection.unwrap(
        CalciteConnection.class);

    final Schema schema =
        CsvSchemaFactory.INSTANCE
            .create(calciteConnection.getRootSchema(), null,
                ImmutableMap.of("directory",
                    resourcePath("sales"), "flavor", "scannable"));
    calciteConnection.getRootSchema().add("TEST", schema);
    final String sql = "select * from \"TEST\".\"DEPTS\" where \"NAME\" = ?";
    final PreparedStatement statement2 =
        calciteConnection.prepareStatement(sql);

    statement2.setString(1, "Sales");
    final ResultSet resultSet1 = statement2.executeQuery();
    Consumer<ResultSet> expect = expect("DEPTNO=10; NAME=Sales");
    expect.accept(resultSet1);
  }
}
 
Example #2
Source File: CachingCalciteSchema.java    From calcite with Apache License 2.0 6 votes vote down vote up
private SubSchemaCache(final CalciteSchema calciteSchema,
    Set<String> names) {
  this.names = NameSet.immutableCopyOf(names);
  this.cache = CacheBuilder.newBuilder().build(
      new CacheLoader<String, CalciteSchema>() {
        @SuppressWarnings("NullableProblems")
        @Override public CalciteSchema load(String schemaName) {
          final Schema subSchema =
              calciteSchema.schema.getSubSchema(schemaName);
          if (subSchema == null) {
            throw new RuntimeException("sub-schema " + schemaName
                + " not found");
          }
          return new CachingCalciteSchema(calciteSchema, subSchema, schemaName);
        }
      });
}
 
Example #3
Source File: QuboleDB.java    From quark with Apache License 2.0 6 votes vote down vote up
@Override
public ImmutableMap<String, Schema> getSchemas() throws QuarkException {
  Map<String, List<SchemaOrdinal>> schemas;
  try {
    schemas = getSchemaListDescribed();
  } catch (ExecutionException | InterruptedException e) {
    LOG.error("Getting Schema metadata for " + this.endpoint
        + " failed. Error: " + e.getMessage(), e);
    throw new QuarkException(e);
  }
  ImmutableMap.Builder<String, Schema> schemaBuilder = new ImmutableMap.Builder<>();

  for (String schemaName: schemas.keySet()) {
    String schemaKey = schemaName;
    if (!this.isCaseSensitive()) {
      schemaKey = schemaName.toUpperCase();
    }
    schemaBuilder.put(schemaKey, new QuboleSchema(schemaKey,
        schemas.get(schemaName), this.isCaseSensitive(), this.getDataTypes()));
  }
  return schemaBuilder.build();
}
 
Example #4
Source File: JdbcDB.java    From quark with Apache License 2.0 6 votes vote down vote up
private ImmutableMap<String, Schema> getSchemaFromResultSet(ResultSet rs,
                                                            ImmutableMap<String, Integer>
                                                                dataTypes)
    throws SQLException {
  if (rs == null || !rs.next()) {
    return ImmutableMap.of();
  }
  ImmutableMap.Builder<String, Schema> schemaBuilder = new ImmutableMap.Builder<>();

  while (!rs.isAfterLast()) {
    String currentSchema = rs.getString(1);
    String schemaKey = currentSchema;
    if (!this.isCaseSensitive()) {
      schemaKey = currentSchema.toUpperCase();
    }

    schemaBuilder.put(schemaKey, new JdbcSchema(currentSchema,
        rs, this.isCaseSensitive(), dataTypes));
  }
  return schemaBuilder.build();
}
 
Example #5
Source File: CassandraSchemaFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Schema create(SchemaPlus parentSchema, String name,
    Map<String, Object> operand) {
  Map map = (Map) operand;
  String host = (String) map.get("host");
  String keyspace = (String) map.get("keyspace");
  String username = (String) map.get("username");
  String password = (String) map.get("password");

  if (map.containsKey("port")) {
    Object portObj = map.get("port");
    int port;
    if (portObj instanceof String) {
      port = Integer.parseInt((String) portObj);
    } else {
      port = (int) portObj;
    }
    return new CassandraSchema(host, port, keyspace, username, password, parentSchema, name);
  } else {
    return new CassandraSchema(host, keyspace, username, password, parentSchema, name);
  }
}
 
Example #6
Source File: RedisSchemaFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Schema create(SchemaPlus schema, String name,
    Map<String, Object> operand) {
  Preconditions.checkArgument(operand.get("tables") != null,
      "tables must be specified");
  Preconditions.checkArgument(operand.get("host") != null,
      "host must be specified");
  Preconditions.checkArgument(operand.get("port") != null,
      "port must be specified");
  Preconditions.checkArgument(operand.get("database") != null,
      "database must be specified");

  @SuppressWarnings("unchecked") List<Map<String, Object>> tables =
      (List) operand.get("tables");
  String host = operand.get("host").toString();
  int port = (int) operand.get("port");
  int database = Integer.parseInt(operand.get("database").toString());
  String password = operand.get("password") == null ? null
      : operand.get("password").toString();
  return new RedisSchema(host, port, database, password, tables);
}
 
Example #7
Source File: MongoSchemaFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
public Schema create(SchemaPlus parentSchema, String name,
    Map<String, Object> operand) {
  final String host = (String) operand.get("host");
  final String database = (String) operand.get("database");
  final String authMechanismName = (String) operand.get("authMechanism");

  final MongoClientOptions.Builder options = MongoClientOptions.builder();

  final MongoCredential credential;
  if (authMechanismName != null) {
    credential = createCredential(operand);
  } else {
    credential = null;
  }

  return new MongoSchema(host, database, credential, options.build());
}
 
Example #8
Source File: ElasticsearchSchemaFactory.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
public Schema create(SchemaPlus parentSchema, String name, Map<String, Object> operand) {
    final Map map = (Map) operand;
    final ObjectMapper mapper = new ObjectMapper();
    mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    try {
        final Map<String, Integer> esAddresses =
                mapper.readValue((String) map.get("esAddresses"),
                        new TypeReference<Map<String, Integer>>() { });
        final Map<String, String> esClusterConfig =
                mapper.readValue((String) map.get("esClusterConfig"),
                        new TypeReference<Map<String, String>>() { });
        final String esIndexName = (String) map.get("esIndexName");
        return new ElasticsearchSchema(esAddresses, esClusterConfig, esIndexName);
    } catch (IOException e) {
        throw new RuntimeException("Cannot parse values from json", e);
    }
}
 
Example #9
Source File: GeodeSchemaFactory.java    From calcite with Apache License 2.0 6 votes vote down vote up
public synchronized Schema create(SchemaPlus parentSchema, String name,
    Map<String, Object> operand) {
  Map map = (Map) operand;
  String locatorHost = (String) map.get(LOCATOR_HOST);
  int locatorPort = Integer.valueOf((String) map.get(LOCATOR_PORT));
  String[] regionNames = ((String) map.get(REGIONS)).split(COMMA_DELIMITER);
  String pbxSerializablePackagePath = (String) map.get(PDX_SERIALIZABLE_PACKAGE_PATH);

  boolean allowSpatialFunctions = true;
  if (map.containsKey(ALLOW_SPATIAL_FUNCTIONS)) {
    allowSpatialFunctions = Boolean.valueOf((String) map.get(ALLOW_SPATIAL_FUNCTIONS));
  }

  if (allowSpatialFunctions) {
    ModelHandler.addFunctions(parentSchema, null, ImmutableList.of(),
        GeoFunctions.class.getName(), "*", true);
  }

  return new GeodeSchema(
      createClientCache(locatorHost, locatorPort, pbxSerializablePackagePath, true),
      Arrays.asList(regionNames));
}
 
Example #10
Source File: CachingCalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
private CachingCalciteSchema(CalciteSchema parent, Schema schema,
    String name, NameMap<CalciteSchema> subSchemaMap,
    NameMap<TableEntry> tableMap, NameMap<LatticeEntry> latticeMap, NameMap<TypeEntry> typeMap,
    NameMultimap<FunctionEntry> functionMap, NameSet functionNames,
    NameMap<FunctionEntry> nullaryFunctionMap,
    List<? extends List<String>> path) {
  super(parent, schema, name, subSchemaMap, tableMap, latticeMap, typeMap,
      functionMap, functionNames, nullaryFunctionMap, path);
  this.implicitSubSchemaCache =
      new AbstractCached<SubSchemaCache>() {
        public SubSchemaCache build() {
          return new SubSchemaCache(CachingCalciteSchema.this,
              CachingCalciteSchema.this.schema.getSubSchemaNames());
        }
      };
  this.implicitTableCache =
      new AbstractCached<NameSet>() {
        public NameSet build() {
          return NameSet.immutableCopyOf(
              CachingCalciteSchema.this.schema.getTableNames());
        }
      };
  this.implicitFunctionCache =
      new AbstractCached<NameSet>() {
        public NameSet build() {
          return NameSet.immutableCopyOf(
              CachingCalciteSchema.this.schema.getFunctionNames());
        }
      };
  this.implicitTypeCache =
      new AbstractCached<NameSet>() {
        public NameSet build() {
          return NameSet.immutableCopyOf(
              CachingCalciteSchema.this.schema.getTypeNames());
        }
      };
}
 
Example #11
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 #12
Source File: CloneSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Schema create(
    SchemaPlus parentSchema,
    String name,
    Map<String, Object> operand) {
  SchemaPlus schema =
      parentSchema.add(name,
          JdbcSchema.create(parentSchema, name + "$source", operand));
  return new CloneSchema(schema);
}
 
Example #13
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 #14
Source File: CatalogManagerCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Schema getSubSchema(String name) {
	if (catalogManager.schemaExists(name)) {
		return new CatalogCalciteSchema(name, catalogManager, isStreamingMode);
	} else {
		return null;
	}
}
 
Example #15
Source File: TpchSchemaFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Schema create(SchemaPlus parentSchema, String name,
    Map<String, Object> operand) {
  Map map = (Map) operand;
  double scale = Util.first((Double) map.get("scale"), 1D);
  int part = Util.first((Integer) map.get("part"), 1);
  int partCount = Util.first((Integer) map.get("partCount"), 1);
  boolean columnPrefix = Util.first((Boolean) map.get("columnPrefix"), true);
  return new TpchSchema(scale, part, partCount, columnPrefix);
}
 
Example #16
Source File: CatalogCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Look up a sub-schema (database) by the given sub-schema name.
 *
 * @param schemaName name of sub-schema to look up
 * @return the sub-schema with a given database name, or null
 */
@Override
public Schema getSubSchema(String schemaName) {
	if (catalogManager.schemaExists(catalogName, schemaName)) {
		return new DatabaseCalciteSchema(schemaName, catalogName, catalogManager, isStreamingMode);
	} else {
		return null;
	}
}
 
Example #17
Source File: SubSchemaWrapper.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<String> getSubPartitions(String table,
                                         List<String> partitionColumns,
                                         List<String> partitionValues
) throws PartitionNotFoundException {
  Schema defaultSchema = getDefaultSchema();
  if (defaultSchema instanceof AbstractSchema) {
    return ((AbstractSchema) defaultSchema).getSubPartitions(table, partitionColumns, partitionValues);
  } else {
    return Collections.emptyList();
  }

}
 
Example #18
Source File: CatalogManagerCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Schema getSubSchema(String name) {
	if (catalogManager.schemaExists(name)) {
		return new CatalogCalciteSchema(isStreamingMode, name, catalogManager, tableConfig);
	} else {
		return null;
	}
}
 
Example #19
Source File: CatalogCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Look up a sub-schema (database) by the given sub-schema name.
 *
 * @param schemaName name of sub-schema to look up
 * @return the sub-schema with a given database name, or null
 */
@Override
public Schema getSubSchema(String schemaName) {

	if (catalog.databaseExists(schemaName)) {
		return new DatabaseCalciteSchema(isStreamingMode, schemaName, catalogName, catalog);
	} else {
		return null;
	}
}
 
Example #20
Source File: DruidSchemaFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Schema create(SchemaPlus parentSchema, String name,
    Map<String, Object> operand) {
  final String url = operand.get("url") instanceof String
      ? (String) operand.get("url")
      : DEFAULT_URL;
  final String coordinatorUrl = operand.get("coordinatorUrl") instanceof String
      ? (String) operand.get("coordinatorUrl")
      : url.replace(":8082", ":8081");
  // "tables" is a hidden attribute, copied in from the enclosing custom
  // schema
  final boolean containsTables = operand.get("tables") instanceof List
      && ((List) operand.get("tables")).size() > 0;
  return new DruidSchema(url, coordinatorUrl, !containsTables);
}
 
Example #21
Source File: JdbcDB.java    From quark with Apache License 2.0 5 votes vote down vote up
public ImmutableMap<String, Schema> getSchemas() throws QuarkException {
  Connection conn = null;
  Statement stmt = null;
  try {
    conn = this.getConnection();
    stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(this.getCatalogSql());
    ImmutableMap<String, Schema> schemaMap = getSchemaFromResultSet(rs, this.getTypes(conn));
    rs.close();
    return schemaMap;
  } catch (ClassNotFoundException | SQLException s) {
    throw new QuarkException(s);
  } finally {
    try {
      if (stmt != null) {
        stmt.close();
      }

      if (conn != null) {
        conn.close();
      }
    } catch (SQLException e) {
      LOG.error("Exception thrown while closing connection to "
          + this.url + " " + e.getMessage(), e);
    }
  }
}
 
Example #22
Source File: DataSourceSchema.java    From quark with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(QueryContext queryContext, SchemaPlus schemaPlus) throws QuarkException {
  subSchemaMap = this.getDataSource().getSchemas();
  this.schemaPlus = schemaPlus;
  for (Map.Entry<String, Schema> entry : subSchemaMap.entrySet()) {
    SchemaPlus subSchemaPlus = this.schemaPlus.add(entry.getKey(), entry.getValue());
    QuarkSchema quarkSchema = (QuarkSchema) entry.getValue();
    quarkSchema.initialize(queryContext, subSchemaPlus);
  }
}
 
Example #23
Source File: CachingCalciteSchema.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Adds a child schema of this schema. */
public CalciteSchema add(String name, Schema schema) {
  final CalciteSchema calciteSchema =
      new CachingCalciteSchema(this, schema, name);
  subSchemaMap.put(name, calciteSchema);
  return calciteSchema;
}
 
Example #24
Source File: EnumerableRepeatUnionHierarchyTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest(name = "{index} : hierarchy(startIds:{2}, ascendant:{3}, "
    + "maxDepth:{4}, all:{0})")
@MethodSource("data")
public void testHierarchy(
    boolean all,
    int[] startIds,
    String startIdsStr,
    boolean ascendant,
    int maxDepth,
    String[] expected) {
  final String fromField;
  final String toField;
  if (ascendant) {
    fromField = "subordinateid";
    toField = "managerid";
  } else {
    fromField = "managerid";
    toField = "subordinateid";
  }

  final Schema schema = new ReflectiveSchema(new HierarchySchema());
  CalciteAssert.that()
      .withSchema("s", schema)
      .query("?")
      .withRel(buildHierarchy(all, startIds, fromField, toField, maxDepth))
      .returnsOrdered(expected);
}
 
Example #25
Source File: GeodeSimpleSchemaFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override public Schema create(SchemaPlus parentSchema,
    String name, Map<String, Object> operand) {
  Map map = (Map) operand;

  String locatorHost = (String) map.get(LOCATOR_HOST);
  int locatorPort = Integer.valueOf((String) map.get(LOCATOR_PORT));
  String[] regionNames = ((String) map.get(REGIONS)).split(COMMA_DELIMITER);
  String pdxSerializablePackagePath = (String) map.get(PDX_SERIALIZABLE_PACKAGE_PATH);

  return new GeodeSimpleSchema(locatorHost, locatorPort, regionNames, pdxSerializablePackagePath);
}
 
Example #26
Source File: TpcdsSchemaFactory.java    From calcite with Apache License 2.0 4 votes vote down vote up
public Schema create(SchemaPlus parentSchema, String name,
    Map<String, Object> operand) {
  @SuppressWarnings("RawTypeCanBeGeneric") final Map map = operand;
  double scale = Util.first((Double) map.get("scale"), 1D);
  return new TpcdsSchema(scale);
}
 
Example #27
Source File: SimpleCalciteSchema.java    From calcite with Apache License 2.0 4 votes vote down vote up
public CalciteSchema add(String name, Schema schema) {
  final CalciteSchema calciteSchema =
      new SimpleCalciteSchema(this, schema, name);
  subSchemaMap.put(name, calciteSchema);
  return calciteSchema;
}
 
Example #28
Source File: MycatTableBase.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Schema.TableType getJdbcTableType() {
    return Schema.TableType.TABLE;
}
 
Example #29
Source File: ApexSQLTable.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public Schema.TableType getJdbcTableType()
{
  return Schema.TableType.STREAM;
}
 
Example #30
Source File: JSqlSchema.java    From kafka-eagle with Apache License 2.0 4 votes vote down vote up
@Override
protected Map<String, Schema> getSubSchemaMap() {
	return super.getSubSchemaMap();
}