0

I am new Apache Calcite and am able to fetch the data from DB by using relational algebra ,but not able to do insert,update, delete,drop operation. If can share sample code will be more helpful.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Please show what you have tried and why it didn't help. – phd Jul 27 '17 at 12:08
  • final FrameworkConfig config = config().build(); final RelBuilder builder = RelBuilder.create(config); final RelNode node = builder.scan("enliven") //.distinct() //.peek(0); //.project(builder.field("Name"),builder.field("event_id")) //.project(builder.field("Name")) //.limit(0, 1) .build(); PreparedStatement pd = RelRunners.run(node); ResultSet resultSet = pd.executeQuery(); – Kamatchi Gunasekaran Jul 27 '17 at 12:20
  • An above one is to get data from calcite, below one to insert data final RelNode node1 =builder.scan("enliven") //.distinct() //.peek(0); .project(builder.field("Name"),builder.field("event_id")) .values(string, values) //.project(builder.field("Name")) //.limit(0, 1) .build(); PreparedStatement pd1 = RelRunners.run(node1); pd1.execute(); – Kamatchi Gunasekaran Jul 27 '17 at 12:27
  • 2
    Pleae [edit] your question and insert everything there. – phd Jul 27 '17 at 14:07

1 Answers1

4

As far as I am aware, RelBuilder can not build a RelNode for INSERT, UPDATE, DELETE, DROP operations.

For DML (INSERT, UPDATE, DELETE, MERGE), the equivalent relational algebra uses TableModify, so you can call LogicalTableModify.create to build one TableModify node and use RelBuilder to build a RelNode`` as its input as follows:

RelNode node = builder.scan("envliven").project("Name");
TableModify modifyNode = LogicalTableModify.create((table,
  schema, node,
  UPDATE, updateColumnList,
  sourceExpressionList, flattened);

For DDL (DROP, CREATE, ALTER), there is no corresponding relational algebra,but you can use the SqlNode to execute directly like CalcitePrepareImpl.executeDdl.

For example:

update nation set n_nationkey = 1 where n_nationkey = 2;

RelNode as follows:

LogicalTableModify(table=[[test, nation]], operation=[UPDATE], updateColumnList=[[n_nationkey]], sourceExpressionList=[[1]], flattened=[false]) 
  LogicalProject(n_nationkey=[$0], n_name=[$1], n_regionkey=[$2], n_comment=[$3], EXPR$0=[1]) 
   LogicalFilter(condition=[=($0,2)])
     LogicalTableScan(table=[[test, nation]])

For UPDATE, the updateColumnList has the column you updated, and the sourceExpressionList has the new values.

For INSERT:

insert into nation(n_nationkey, n_name) values(1, 'test');

RelNode as follows:

LogicalTableModify(table=[[test, nation]], operation=[INSERT], flattened=[false])
  LogicalProject(n_nationkey=[$0], n_name=[$1], n_regionkey=[null], n_comment=[null])
    LogicalValues(tuples=[[{ 1, _UTF-16'test                     ' }]])
Michael Mior
  • 28,107
  • 9
  • 89
  • 113
inferno
  • 684
  • 6
  • 21