org.apache.calcite.jdbc.CalcitePrepare Java Examples

The following examples show how to use org.apache.calcite.jdbc.CalcitePrepare. 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: Schemas.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Analyzes a view. For use within Calcite only. */
public static CalcitePrepare.AnalyzeViewResult analyzeView(
    final CalciteConnection connection, final CalciteSchema schema,
    final List<String> schemaPath, final String viewSql,
    List<String> viewPath, boolean fail) {
  final CalcitePrepare prepare = CalcitePrepare.DEFAULT_FACTORY.apply();
  final ImmutableMap<CalciteConnectionProperty, String> propValues =
      ImmutableMap.of();
  final CalcitePrepare.Context context =
      makeContext(connection, schema, schemaPath, viewPath, propValues);
  CalcitePrepare.Dummy.push(context);
  try {
    return prepare.analyzeView(context, viewSql, fail);
  } finally {
    CalcitePrepare.Dummy.pop(context);
  }
}
 
Example #2
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a {@code CREATE TYPE} command. */
public void execute(SqlCreateType create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
  final SqlValidator validator = validator(context, false);
  pair.left.add(pair.right, typeFactory -> {
    if (create.dataType != null) {
      return create.dataType.deriveType(validator);
    } else {
      final RelDataTypeFactory.Builder builder = typeFactory.builder();
      for (SqlNode def : create.attributeDefs) {
        final SqlAttributeDefinition attributeDef =
            (SqlAttributeDefinition) def;
        final SqlDataTypeSpec typeSpec = attributeDef.dataType;
        final RelDataType type = typeSpec.deriveType(validator);
        builder.add(attributeDef.name.getSimple(), type);
      }
      return builder.build();
    }
  });
}
 
Example #3
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a {@code CREATE VIEW} command. */
public void execute(SqlCreateView create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair =
      schema(context, true, create.name);
  final SchemaPlus schemaPlus = pair.left.plus();
  for (Function function : schemaPlus.getFunctions(pair.right)) {
    if (function.getParameters().isEmpty()) {
      if (!create.getReplace()) {
        throw SqlUtil.newContextException(create.name.getParserPosition(),
            RESOURCE.viewExists(pair.right));
      }
      pair.left.removeFunction(pair.right);
    }
  }
  final SqlNode q = renameColumns(create.columnList, create.query);
  final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
  final ViewTableMacro viewTableMacro =
      ViewTable.viewMacro(schemaPlus, sql, pair.left.path(null),
          context.getObjectPath(), false);
  final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
  Util.discard(x);
  schemaPlus.add(pair.right, viewTableMacro);
}
 
Example #4
Source File: ServerTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Contains calls to all overloaded {@code execute} methods in
 * {@link DdlExecutorImpl} to silence warnings that these methods are not
 * called. (They are, not from this test, but via reflection.) */
@Test void testAll() {
  //noinspection ConstantConditions
  if (true) {
    return;
  }
  final ServerDdlExecutor executor = ServerDdlExecutor.INSTANCE;
  final Object o = "x";
  final CalcitePrepare.Context context = (CalcitePrepare.Context) o;
  executor.execute((SqlNode) o, context);
  executor.execute((SqlCreateFunction) o, context);
  executor.execute((SqlCreateTable) o, context);
  executor.execute((SqlCreateSchema) o, context);
  executor.execute((SqlCreateMaterializedView) o, context);
  executor.execute((SqlCreateView) o, context);
  executor.execute((SqlCreateType) o, context);
  executor.execute((SqlCreateSchema) o, context);
  executor.execute((SqlCreateForeignSchema) o, context);
  executor.execute((SqlDropMaterializedView) o, context);
  executor.execute((SqlDropFunction) o, context);
  executor.execute((SqlDropSchema) o, context);
}
 
Example #5
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a {@code DROP MATERIALIZED VIEW} command. */
public void execute(SqlDropMaterializedView drop,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, drop.name);
  final Table table = pair.left.plus().getTable(pair.right);
  if (table != null) {
    // Materialized view exists.
    execute((SqlDropObject) drop, context);
    if (table instanceof Wrapper) {
      final MaterializationKey materializationKey =
          ((Wrapper) table).unwrap(MaterializationKey.class);
      if (materializationKey != null) {
        MaterializationService.instance()
            .removeMaterialization(materializationKey);
      }
    }
  }
}
 
Example #6
Source File: SqlDropTableExtension.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(CalcitePrepare.Context context) {
    final List<String> path = context.getDefaultSchemaPath();
    CalciteSchema schema = context.getRootSchema();
    for (String p : path) {
        schema = schema.getSubSchema(p, true);
    }

    final Pair<CalciteSchema, String> pair =
        SqlDdlNodes.schema(context, true, name);
    switch (getKind()) {
        case DROP_TABLE:
            Schema schemaPlus = schema.plus().unwrap(Schema.class);
            boolean existed = schemaPlus.dropTable(name.getSimple());
            pair.left.removeTable(name.getSimple());
            if (!existed && !ifExists) {
                throw SqlUtil.newContextException(name.getParserPosition(),
                    RESOURCE.tableNotFound(name.getSimple()));
            }
            break;
        default:
            throw new AssertionError(getKind());
    }
}
 
Example #7
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Executes a prepare action. */
public <R> R perform(CalciteServerStatement statement,
    FrameworkConfig config, Frameworks.BasePrepareAction<R> action) {
  final CalcitePrepare.Context prepareContext =
      statement.createPrepareContext();
  final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
  final CalciteSchema schema =
      config.getDefaultSchema() != null
          ? CalciteSchema.from(config.getDefaultSchema())
          : prepareContext.getRootSchema();
  CalciteCatalogReader catalogReader =
      new CalciteCatalogReader(schema.root(),
          schema.path(null),
          typeFactory,
          prepareContext.config());
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  final RelOptPlanner planner =
      createPlanner(prepareContext,
          config.getContext(),
          config.getCostFactory());
  final RelOptCluster cluster = createCluster(planner, rexBuilder);
  return action.apply(cluster, catalogReader,
      prepareContext.getRootSchema().plus(), statement);
}
 
Example #8
Source File: Schemas.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Parses and validates a SQL query. For use within Calcite only. */
public static CalcitePrepare.ParseResult parse(
    final CalciteConnection connection, final CalciteSchema schema,
    final List<String> schemaPath, final String sql) {
  final CalcitePrepare prepare = CalcitePrepare.DEFAULT_FACTORY.apply();
  final ImmutableMap<CalciteConnectionProperty, String> propValues =
      ImmutableMap.of();
  final CalcitePrepare.Context context =
      makeContext(connection, schema, schemaPath, null, propValues);
  CalcitePrepare.Dummy.push(context);
  try {
    return prepare.parse(context, sql);
  } finally {
    CalcitePrepare.Dummy.pop(context);
  }
}
 
Example #9
Source File: Schemas.java    From calcite with Apache License 2.0 6 votes vote down vote up
/** Parses and validates a SQL query and converts to relational algebra. For
 * use within Calcite only. */
public static CalcitePrepare.ConvertResult convert(
    final CalciteConnection connection, final CalciteSchema schema,
    final List<String> schemaPath, final String sql) {
  final CalcitePrepare prepare = CalcitePrepare.DEFAULT_FACTORY.apply();
  final ImmutableMap<CalciteConnectionProperty, String> propValues =
      ImmutableMap.of();
  final CalcitePrepare.Context context =
      makeContext(connection, schema, schemaPath, null, propValues);
  CalcitePrepare.Dummy.push(context);
  try {
    return prepare.convert(context, sql);
  } finally {
    CalcitePrepare.Dummy.pop(context);
  }
}
 
Example #10
Source File: ViewTableMacro.java    From calcite with Apache License 2.0 6 votes vote down vote up
public TranslatableTable apply(List<Object> arguments) {
  final CalciteConnection connection =
      MaterializedViewTable.MATERIALIZATION_CONNECTION;
  CalcitePrepare.AnalyzeViewResult parsed =
      Schemas.analyzeView(connection, schema, schemaPath, viewSql, viewPath,
          modifiable != null && modifiable);
  final List<String> schemaPath1 =
      schemaPath != null ? schemaPath : schema.path(null);
  if ((modifiable == null || modifiable)
      && parsed.modifiable
      && parsed.table != null) {
    return modifiableViewTable(parsed, viewSql, schemaPath1, viewPath, schema);
  } else {
    return viewTable(parsed, viewSql, schemaPath1, viewPath);
  }
}
 
Example #11
Source File: QuarkMetaImpl.java    From quark with Apache License 2.0 6 votes vote down vote up
protected MetaResultSet createResultSet(
    Map<String, Object> internalParameters, List<ColumnMetaData> columns,
    CursorFactory cursorFactory, final Frame firstFrame) {
  try {
    final QuarkConnectionImpl connection = getConnection();
    final AvaticaStatement statement = connection.createStatement();
    final CalcitePrepare.CalciteSignature<Object> signature =
        new CalcitePrepare.CalciteSignature<Object>("",
            ImmutableList.<AvaticaParameter>of(), internalParameters, null,
            columns, cursorFactory, null, ImmutableList.<RelCollation>of(), -1,
            null, Meta.StatementType.SELECT) {
          @Override public Enumerable<Object> enumerable(
              DataContext dataContext) {
            return Linq4j.asEnumerable(firstFrame.rows);
          }
        };
    return MetaResultSet.create(connection.id, statement.getId(), true,
        signature, firstFrame);
  } catch (SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #12
Source File: QuarkResultSet.java    From quark with Apache License 2.0 6 votes vote down vote up
@Override
public ResultSet create(ColumnMetaData.AvaticaType elementType,
                        Iterable<Object> iterable) {
  final List<ColumnMetaData> columnMetaDataList;
  if (elementType instanceof ColumnMetaData.StructType) {
    columnMetaDataList = ((ColumnMetaData.StructType) elementType).columns;
  } else {
    columnMetaDataList =
        ImmutableList.of(ColumnMetaData.dummy(elementType, false));
  }
  final CalcitePrepare.CalciteSignature signature =
      (CalcitePrepare.CalciteSignature) this.signature;
  final CalcitePrepare.CalciteSignature<Object> newSignature =
      new CalcitePrepare.CalciteSignature<>(signature.sql,
          signature.parameters, signature.internalParameters,
          signature.rowType, columnMetaDataList, Meta.CursorFactory.ARRAY,
          signature.rootSchema, ImmutableList.<RelCollation>of(), -1, null);
  ResultSetMetaData subResultSetMetaData =
      new AvaticaResultSetMetaData(statement, null, newSignature);
  final QuarkResultSet resultSet =
      new QuarkResultSet(statement, signature, subResultSetMetaData,
          localCalendar.getTimeZone(), new Meta.Frame(0, true, iterable));
  final Cursor cursor = resultSet.createCursor(elementType, iterable);
  return resultSet.execute2(cursor, columnMetaDataList);
}
 
Example #13
Source File: CalciteMaterializer.java    From calcite with Apache License 2.0 5 votes vote down vote up
CalciteMaterializer(CalcitePrepareImpl prepare,
    CalcitePrepare.Context context,
    CatalogReader catalogReader, CalciteSchema schema,
    RelOptCluster cluster, SqlRexConvertletTable convertletTable) {
  super(prepare, context, catalogReader, catalogReader.getTypeFactory(),
      schema, EnumerableRel.Prefer.ANY, cluster, BindableConvention.INSTANCE,
      convertletTable);
}
 
Example #14
Source File: ViewTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Allows a sub-class to return an extension of {@link ModifiableViewTable}
 * by overriding this method. */
protected ModifiableViewTable modifiableViewTable(CalcitePrepare.AnalyzeViewResult parsed,
    String viewSql, List<String> schemaPath, List<String> viewPath,
    CalciteSchema schema) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new ModifiableViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath,
      parsed.table, Schemas.path(schema.root(), parsed.tablePath),
      parsed.constraint, parsed.columnMapping);
}
 
Example #15
Source File: EnumerableInterpretable.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static Bindable toBindable(Map<String, Object> parameters,
    CalcitePrepare.SparkHandler spark, EnumerableRel rel,
    EnumerableRel.Prefer prefer) {
  EnumerableRelImplementor relImplementor =
      new EnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

  final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
  String s = Expressions.toString(expr.memberDeclarations, "\n", false);

  if (CalciteSystemProperty.DEBUG.value()) {
    Util.debugCode(System.out, s);
  }

  Hook.JAVA_PLAN.run(s);

  try {
    if (spark != null && spark.enabled()) {
      return spark.compile(expr, s);
    } else {
      return getBindable(expr, s, rel.getRowType().getFieldCount());
    }
  } catch (Exception e) {
    throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n"
        + s, e);
  }
}
 
Example #16
Source File: Prepare.java    From calcite with Apache License 2.0 5 votes vote down vote up
public Prepare(CalcitePrepare.Context context, CatalogReader catalogReader,
    Convention resultConvention) {
  assert context != null;
  this.context = context;
  this.catalogReader = catalogReader;
  this.resultConvention = resultConvention;
}
 
Example #17
Source File: ViewTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Allows a sub-class to return an extension of {@link ViewTable} by
 * overriding this method. */
protected ViewTable viewTable(CalcitePrepare.AnalyzeViewResult parsed,
    String viewSql, List<String> schemaPath, List<String> viewPath) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new ViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath);
}
 
Example #18
Source File: InterpretableRel.java    From calcite with Apache License 2.0 5 votes vote down vote up
public InterpreterImplementor(Compiler compiler,
    CalcitePrepare.SparkHandler spark,
    DataContext dataContext) {
  this.compiler = compiler;
  this.spark = spark;
  this.dataContext = dataContext;
}
 
Example #19
Source File: MockCatalogReader.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Override protected ModifiableViewTable modifiableViewTable(
    CalcitePrepare.AnalyzeViewResult parsed, String viewSql,
    List<String> schemaPath, List<String> viewPath, CalciteSchema schema) {
  final JavaTypeFactory typeFactory = (JavaTypeFactory) parsed.typeFactory;
  final Type elementType = typeFactory.getJavaClass(parsed.rowType);
  return new MockModifiableViewTable(elementType,
      RelDataTypeImpl.proto(parsed.rowType), viewSql, schemaPath, viewPath,
      parsed.table, Schemas.path(schema.root(), parsed.tablePath),
      parsed.constraint, parsed.columnMapping);
}
 
Example #20
Source File: ExtensionDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Populates the table called {@code name} by executing {@code query}. */
protected static void populate(SqlIdentifier name, SqlNode query,
    CalcitePrepare.Context context) {
  // Generate, prepare and execute an "INSERT INTO table query" statement.
  // (It's a bit inefficient that we convert from SqlNode to SQL and back
  // again.)
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .defaultSchema(
          Objects.requireNonNull(
              Schemas.subSchema(context.getRootSchema(),
                  context.getDefaultSchemaPath())).plus())
      .build();
  final Planner planner = Frameworks.getPlanner(config);
  try {
    final StringBuilder buf = new StringBuilder();
    final SqlPrettyWriter w =
        new SqlPrettyWriter(
            SqlPrettyWriter.config()
                .withDialect(CalciteSqlDialect.DEFAULT)
                .withAlwaysUseParentheses(false),
            buf);
    buf.append("INSERT INTO ");
    name.unparse(w, 0, 0);
    buf.append(" ");
    query.unparse(w, 0, 0);
    final String sql = buf.toString();
    final SqlNode query1 = planner.parse(sql);
    final SqlNode query2 = planner.validate(query1);
    final RelRoot r = planner.rel(query2);
    final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
    int rowCount = prepare.executeUpdate();
    Util.discard(rowCount);
    prepare.close();
  } catch (SqlParseException | ValidationException
      | RelConversionException | SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #21
Source File: TraitPropagationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
private static RelNode run(PropAction action, RuleSet rules)
    throws Exception {

  FrameworkConfig config = Frameworks.newConfigBuilder()
      .ruleSets(rules).build();

  final Properties info = new Properties();
  final Connection connection = DriverManager
      .getConnection("jdbc:calcite:", info);
  final CalciteServerStatement statement = connection
      .createStatement().unwrap(CalciteServerStatement.class);
  final CalcitePrepare.Context prepareContext =
        statement.createPrepareContext();
  final JavaTypeFactory typeFactory = prepareContext.getTypeFactory();
  CalciteCatalogReader catalogReader =
        new CalciteCatalogReader(prepareContext.getRootSchema(),
            prepareContext.getDefaultSchemaPath(),
            typeFactory,
            prepareContext.config());
  final RexBuilder rexBuilder = new RexBuilder(typeFactory);
  final RelOptPlanner planner = new VolcanoPlanner(config.getCostFactory(),
      config.getContext());

  // set up rules before we generate cluster
  planner.clearRelTraitDefs();
  planner.addRelTraitDef(RelCollationTraitDef.INSTANCE);
  planner.addRelTraitDef(ConventionTraitDef.INSTANCE);

  planner.clear();
  for (RelOptRule r : rules) {
    planner.addRule(r);
  }

  final RelOptCluster cluster = RelOptCluster.create(planner, rexBuilder);
  return action.apply(cluster, catalogReader,
      prepareContext.getRootSchema().plus());
}
 
Example #22
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code CREATE SCHEMA} command. */
public void execute(SqlCreateSchema create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
  final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
  if (subSchema0 != null) {
    if (!create.getReplace() && !create.ifNotExists) {
      throw SqlUtil.newContextException(create.name.getParserPosition(),
          RESOURCE.schemaExists(pair.right));
    }
  }
  final Schema subSchema = new AbstractSchema();
  pair.left.add(pair.right, subSchema);
}
 
Example #23
Source File: CalcitePrepareImpl.java    From kareldb with Apache License 2.0 5 votes vote down vote up
@Override
protected RelOptPlanner createPlanner(
    final CalcitePrepare.Context prepareContext,
    org.apache.calcite.plan.Context externalContext,
    RelOptCostFactory costFactory) {
    RelOptPlanner planner = super.createPlanner(prepareContext, externalContext, costFactory);
    planner.removeRule(EnumerableRules.ENUMERABLE_TABLE_MODIFICATION_RULE);
    planner.addRule(EnumerableTableModifyExtensionRule.INSTANCE);
    return planner;
}
 
Example #24
Source File: HiveEnumerableInterpretable.java    From marble with Apache License 2.0 5 votes vote down vote up
public static Bindable toBindable(Map<String, Object> parameters,
    CalcitePrepare.SparkHandler spark, EnumerableRel rel,
    EnumerableRel.Prefer prefer) {
  HiveEnumerableRelImplementor relImplementor =
      new HiveEnumerableRelImplementor(rel.getCluster().getRexBuilder(),
          parameters);

  final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
  String s = Expressions.toString(expr.memberDeclarations, "\n", false);

  if (CalcitePrepareImpl.DEBUG) {
    Util.debugCode(System.out, s);
  }

  Hook.JAVA_PLAN.run(s);

  try {
    if (spark != null && spark.enabled()) {
      return spark.compile(expr, s);
    } else {
      return getBindable(expr, s,
          rel.getRowType().getFieldCount());
    }
  } catch (Exception e) {
    throw Helper.INSTANCE.wrap("Error while compiling generated Java code:\n"
        + s, e);
  }
}
 
Example #25
Source File: PreparedStatementImpl.java    From quark with Apache License 2.0 5 votes vote down vote up
PreparedStatementImpl(QuarkConnectionImpl connection,
                      Meta.StatementHandle h, CalcitePrepare.CalciteSignature signature,
                      int resultSetType, int resultSetConcurrency,
                      int resultSetHoldability)
    throws SQLException {
  super(connection, h, signature, resultSetType, resultSetConcurrency,
      resultSetHoldability);
}
 
Example #26
Source File: Materializer.java    From quark with Apache License 2.0 5 votes vote down vote up
public void populateMaterializations(CalcitePrepare.Context context,
                                     QuarkMaterializeCluster.RelOptPlannerHolder holder) {
  MaterializePrepare prepare = new MaterializePrepare(holder);
  for (Prepare.Materialization materialization : materializations) {
    if (materialization.queryRel == null || materialization.tableRel == null) {
      prepare.populateMaterializations(context, materialization);
    }
    holder.getPlanner().addMaterialization(
        new RelOptMaterialization(materialization.tableRel,
            materialization.queryRel,
            materialization.getStarTableIdentified()));
  }
}
 
Example #27
Source File: QueryContext.java    From quark with Apache License 2.0 5 votes vote down vote up
public CalcitePrepare.Context getPrepareContext() {
  return new CalcitePrepare.Context() {

    @Override
    public JavaTypeFactory getTypeFactory() {
      return typeFactory;
    }

    @Override
    public CalciteSchema getRootSchema() {
      return CalciteSchema.from(rootSchema);
    }

    @Override
    public List<String> getDefaultSchemaPath() {
      return defaultSchema;
    }

    @Override
    public CalciteConnectionConfig config() {
      return cfg;
    }

    @Override
    public CalcitePrepare.SparkHandler spark() {
      return CalcitePrepare.Dummy.getSparkHandler(false);
    }

    @Override
    public DataContext getDataContext() {
      return null;
    }
  };
}
 
Example #28
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Populates the table called {@code name} by executing {@code query}. */
static void populate(SqlIdentifier name, SqlNode query,
    CalcitePrepare.Context context) {
  // Generate, prepare and execute an "INSERT INTO table query" statement.
  // (It's a bit inefficient that we convert from SqlNode to SQL and back
  // again.)
  final FrameworkConfig config = Frameworks.newConfigBuilder()
      .defaultSchema(context.getRootSchema().plus())
      .build();
  final Planner planner = Frameworks.getPlanner(config);
  try {
    final StringBuilder buf = new StringBuilder();
    final SqlWriterConfig writerConfig =
        SqlPrettyWriter.config().withAlwaysUseParentheses(false);
    final SqlPrettyWriter w = new SqlPrettyWriter(writerConfig, buf);
    buf.append("INSERT INTO ");
    name.unparse(w, 0, 0);
    buf.append(' ');
    query.unparse(w, 0, 0);
    final String sql = buf.toString();
    final SqlNode query1 = planner.parse(sql);
    final SqlNode query2 = planner.validate(query1);
    final RelRoot r = planner.rel(query2);
    final PreparedStatement prepare = context.getRelRunner().prepare(r.rel);
    int rowCount = prepare.executeUpdate();
    Util.discard(rowCount);
    prepare.close();
  } catch (SqlParseException | ValidationException
      | RelConversionException | SQLException e) {
    throw new RuntimeException(e);
  }
}
 
Example #29
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code DROP SCHEMA} command. */
public void execute(SqlDropSchema drop,
    CalcitePrepare.Context context) {
  final List<String> path = context.getDefaultSchemaPath();
  CalciteSchema schema = context.getRootSchema();
  for (String p : path) {
    schema = schema.getSubSchema(p, true);
  }
  final boolean existed = schema.removeSubSchema(drop.name.getSimple());
  if (!existed && !drop.ifExists) {
    throw SqlUtil.newContextException(drop.name.getParserPosition(),
        RESOURCE.schemaNotFound(drop.name.getSimple()));
  }
}
 
Example #30
Source File: ServerDdlExecutor.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Executes a {@code CREATE MATERIALIZED VIEW} command. */
public void execute(SqlCreateMaterializedView create,
    CalcitePrepare.Context context) {
  final Pair<CalciteSchema, String> pair = schema(context, true, create.name);
  if (pair.left.plus().getTable(pair.right) != null) {
    // Materialized view exists.
    if (!create.ifNotExists) {
      // They did not specify IF NOT EXISTS, so give error.
      throw SqlUtil.newContextException(create.name.getParserPosition(),
          RESOURCE.tableExists(pair.right));
    }
    return;
  }
  final SqlNode q = renameColumns(create.columnList, create.query);
  final String sql = q.toSqlString(CalciteSqlDialect.DEFAULT).getSql();
  final List<String> schemaPath = pair.left.path(null);
  final ViewTableMacro viewTableMacro =
      ViewTable.viewMacro(pair.left.plus(), sql, schemaPath,
          context.getObjectPath(), false);
  final TranslatableTable x = viewTableMacro.apply(ImmutableList.of());
  final RelDataType rowType = x.getRowType(context.getTypeFactory());

  // Table does not exist. Create it.
  final MaterializedViewTable table =
      new MaterializedViewTable(pair.right, RelDataTypeImpl.proto(rowType));
  pair.left.add(pair.right, table);
  populate(create.name, create.query, context);
  table.key =
      MaterializationService.instance().defineMaterialization(pair.left, null,
          sql, schemaPath, pair.right, true, true);
}