Skip to content

Commit 2d7fc8e

Browse files
authored
Merge pull request #1 from substrait-io/vbarua/sql-to-ddl-suggestions
refactor: suggestions for PR
2 parents 0dc6eb3 + d1af907 commit 2d7fc8e

10 files changed

Lines changed: 88 additions & 176 deletions

File tree

core/src/main/java/io/substrait/util/NoException.java

Lines changed: 0 additions & 5 deletions
This file was deleted.

isthmus/src/main/java/io/substrait/isthmus/SubstraitRelNodeConverter.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import io.substrait.isthmus.expression.ExpressionRexConverter;
1717
import io.substrait.isthmus.expression.ScalarFunctionConverter;
1818
import io.substrait.isthmus.expression.WindowFunctionConverter;
19+
import io.substrait.relation.AbstractDdlRel;
1920
import io.substrait.relation.AbstractRelVisitor;
2021
import io.substrait.relation.AbstractUpdate;
22+
import io.substrait.relation.AbstractWriteRel;
2123
import io.substrait.relation.Aggregate;
2224
import io.substrait.relation.Cross;
2325
import io.substrait.relation.EmptyScan;
@@ -94,7 +96,6 @@ public class SubstraitRelNodeConverter
9496
protected final RelBuilder relBuilder;
9597
protected final RexBuilder rexBuilder;
9698
private final TypeConverter typeConverter;
97-
private final SubstraitRelNodeConverterDdmlValidator substraitRelNodeConverterDdmlValidator;
9899

99100
public SubstraitRelNodeConverter(
100101
SimpleExtension.ExtensionCollection extensions,
@@ -143,8 +144,6 @@ public SubstraitRelNodeConverter(
143144
this.aggregateFunctionConverter = aggregateFunctionConverter;
144145
this.expressionRexConverter = expressionRexConverter;
145146
this.expressionRexConverter.setRelNodeConverter(this);
146-
this.substraitRelNodeConverterDdmlValidator =
147-
new SubstraitRelNodeConverterDdmlValidator(relBuilder, this);
148147
}
149148

150149
public static RelNode convert(
@@ -557,12 +556,21 @@ public RelNode visit(NamedUpdate update, Context context) {
557556

558557
@Override
559558
public RelNode visit(NamedDdl namedDdl, Context context) {
560-
final ValidationResult validationResult =
561-
namedDdl.accept(substraitRelNodeConverterDdmlValidator, context);
562-
if (!validationResult.isValid()) {
563-
throw new IllegalArgumentException(
564-
String.join(System.lineSeparator(), validationResult.getMessages()));
559+
if (namedDdl.getOperation() != AbstractDdlRel.DdlOp.CREATE
560+
|| namedDdl.getObject() != AbstractDdlRel.DdlObject.VIEW) {
561+
throw new UnsupportedOperationException(
562+
String.format(
563+
"Can only handle NamedDdl with (%s, %s), given (%s, %s)",
564+
AbstractDdlRel.DdlOp.CREATE,
565+
AbstractDdlRel.DdlObject.VIEW,
566+
namedDdl.getOperation(),
567+
namedDdl.getObject()));
565568
}
569+
570+
if (namedDdl.getViewDefinition().isEmpty()) {
571+
throw new IllegalArgumentException("NamedDdl view definition must be set");
572+
}
573+
566574
Rel viewDefinition = namedDdl.getViewDefinition().get();
567575
RelNode relNode = viewDefinition.accept(this, context);
568576
RelRoot relRoot = RelRoot.of(relNode, SqlKind.SELECT);
@@ -606,6 +614,17 @@ public RelNode visit(VirtualTableScan virtualTableScan, Context context) {
606614
}
607615

608616
private RelNode handleCreateTableAs(NamedWrite namedWrite, Context context) {
617+
if (namedWrite.getCreateMode() != AbstractWriteRel.CreateMode.REPLACE_IF_EXISTS
618+
|| namedWrite.getOutputMode() != AbstractWriteRel.OutputMode.NO_OUTPUT) {
619+
throw new UnsupportedOperationException(
620+
String.format(
621+
"Can only handle CTAS NamedWrite with (%s, %s), given (%s, %s)",
622+
AbstractWriteRel.CreateMode.REPLACE_IF_EXISTS,
623+
AbstractWriteRel.OutputMode.NO_OUTPUT,
624+
namedWrite.getCreateMode(),
625+
namedWrite.getOutputMode()));
626+
}
627+
609628
Rel input = namedWrite.getInput();
610629
RelNode relNode = input.accept(this, context);
611630
RelRoot relRoot = RelRoot.of(relNode, SqlKind.SELECT);
@@ -614,12 +633,6 @@ private RelNode handleCreateTableAs(NamedWrite namedWrite, Context context) {
614633

615634
@Override
616635
public RelNode visit(NamedWrite write, Context context) {
617-
final ValidationResult validationResult =
618-
write.accept(substraitRelNodeConverterDdmlValidator, context);
619-
if (!validationResult.isValid()) {
620-
throw new IllegalArgumentException(
621-
String.join(System.lineSeparator(), validationResult.getMessages()));
622-
}
623636
RelNode input = write.getInput().accept(this, context);
624637
assert relBuilder.getRelOptSchema() != null;
625638
final RelOptTable targetTable =
@@ -636,8 +649,10 @@ public RelNode visit(NamedWrite write, Context context) {
636649
case CTAS:
637650
return handleCreateTableAs(write, context);
638651
default:
639-
// checked by validation
640-
throw new IllegalArgumentException("Couldn't determine operation");
652+
throw new UnsupportedOperationException(
653+
String.format(
654+
"NamedWrite with WriteOp %s cannot be converted to a Calcite RelNode. Consider using a more specific Rel (e.g NamedUpdate)",
655+
write.getOperation()));
641656
}
642657

643658
// checked by validation

isthmus/src/main/java/io/substrait/isthmus/SubstraitRelNodeConverterDdmlValidator.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

isthmus/src/main/java/io/substrait/isthmus/SubstraitRelVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ public Rel handleCreateTable(CreateTable createTable) {
465465
.operation(AbstractWriteRel.WriteOp.CTAS)
466466
.createMode(AbstractWriteRel.CreateMode.REPLACE_IF_EXISTS)
467467
.outputMode(AbstractWriteRel.OutputMode.NO_OUTPUT)
468-
.names(createTable.getNames())
468+
.names(createTable.getTableName())
469469
.build();
470470
}
471471

@@ -481,7 +481,7 @@ public Rel handleCreateView(CreateView createView) {
481481
.tableDefaults(defaults)
482482
.operation(AbstractDdlRel.DdlOp.CREATE)
483483
.object(AbstractDdlRel.DdlObject.VIEW)
484-
.names(createView.getNames())
484+
.names(createView.getViewName())
485485
.build();
486486
}
487487

isthmus/src/main/java/io/substrait/isthmus/ValidationResult.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

isthmus/src/main/java/io/substrait/isthmus/calcite/rel/CreateTable.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,43 @@
22

33
import java.util.List;
44
import org.apache.calcite.rel.AbstractRelNode;
5+
import org.apache.calcite.rel.RelNode;
56
import org.apache.calcite.rel.RelRoot;
7+
import org.apache.calcite.rel.RelWriter;
68
import org.apache.calcite.rel.type.RelDataType;
79

810
public class CreateTable extends AbstractRelNode {
911

10-
private List<String> names;
11-
private RelRoot input;
12+
private final List<String> tableName;
13+
private final RelRoot input;
1214

13-
public RelRoot getInput() {
14-
return input;
15-
}
16-
17-
public void setInput(RelRoot input) {
18-
this.input = input;
19-
}
20-
21-
public CreateTable(List<String> names, RelRoot input) {
15+
public CreateTable(List<String> tableName, RelRoot input) {
2216
super(input.rel.getCluster(), input.rel.getTraitSet());
2317

24-
this.names = names;
18+
this.tableName = tableName;
2519
this.input = input;
2620
}
2721

2822
@Override
2923
protected RelDataType deriveRowType() {
30-
// return new DdlRelDataType();
3124
return input.validatedRowType;
3225
}
3326

34-
public List<String> getNames() {
35-
return names;
27+
@Override
28+
public RelWriter explainTerms(RelWriter pw) {
29+
return super.explainTerms(pw).input("input", getInput().rel).item("tableName", getTableName());
3630
}
3731

38-
public void setNames(List<String> names) {
39-
this.names = names;
32+
@Override
33+
public List<RelNode> getInputs() {
34+
return List.of(input.rel);
35+
}
36+
37+
public List<String> getTableName() {
38+
return tableName;
39+
}
40+
41+
public RelRoot getInput() {
42+
return input;
4043
}
4144
}

isthmus/src/main/java/io/substrait/isthmus/calcite/rel/CreateView.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,41 @@
22

33
import java.util.List;
44
import org.apache.calcite.rel.AbstractRelNode;
5+
import org.apache.calcite.rel.RelNode;
56
import org.apache.calcite.rel.RelRoot;
7+
import org.apache.calcite.rel.RelWriter;
68
import org.apache.calcite.rel.type.RelDataType;
79

810
public class CreateView extends AbstractRelNode {
9-
private List<String> names;
10-
private RelRoot input;
11+
private final List<String> viewName;
12+
private final RelRoot input;
1113

12-
public CreateView(List<String> names, RelRoot input) {
14+
public CreateView(List<String> viewName, RelRoot input) {
1315
super(input.rel.getCluster(), input.rel.getTraitSet());
14-
this.names = names;
16+
this.viewName = viewName;
1517
this.input = input;
1618
}
1719

1820
@Override
1921
protected RelDataType deriveRowType() {
20-
// return new DdlRelDataType();
2122
return input.validatedRowType;
2223
}
2324

24-
public List<String> getNames() {
25-
return names;
25+
@Override
26+
public RelWriter explainTerms(RelWriter pw) {
27+
return super.explainTerms(pw).input("input", getInput().rel).item("viewName", getViewName());
2628
}
2729

28-
public void setNames(List<String> names) {
29-
this.names = names;
30+
@Override
31+
public List<RelNode> getInputs() {
32+
return List.of(input.rel);
3033
}
3134

32-
public RelRoot getInput() {
33-
return input;
35+
public List<String> getViewName() {
36+
return viewName;
3437
}
3538

36-
public void setInput(RelRoot input) {
37-
this.input = input;
39+
public RelRoot getInput() {
40+
return input;
3841
}
3942
}

isthmus/src/test/java/io/substrait/isthmus/DdlRoundtripTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public DdlRoundtripTest() throws SqlParseException {
1818
@Test
1919
void testCreateTable() throws Exception {
2020
String sql = "create table dst1 as select * from src1";
21-
assertFullRoundTripWorkaroundOptimizer(sql, catalogReader);
21+
assertFullRoundTripWithIdentityProjectionWorkaround(sql, catalogReader);
2222
}
2323

2424
@Test
2525
void testCreateView() throws Exception {
2626
String sql = "create view dst1 as select * from src1";
27-
assertFullRoundTripWorkaroundOptimizer(sql, catalogReader);
27+
assertFullRoundTripWithIdentityProjectionWorkaround(sql, catalogReader);
2828
}
2929
}

isthmus/src/test/java/io/substrait/isthmus/DmlRoundtripTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ public DmlRoundtripTest() throws SqlParseException {}
1616

1717
@Test
1818
void testDelete() throws SqlParseException {
19-
assertFullRoundTripWorkaroundOptimizer("delete from src1 where intcol=10", catalogReader);
19+
assertFullRoundTripWithIdentityProjectionWorkaround(
20+
"delete from src1 where intcol=10", catalogReader);
2021
}
2122

2223
@Test
2324
void testUpdate() throws SqlParseException {
24-
assertFullRoundTripWorkaroundOptimizer(
25+
assertFullRoundTripWithIdentityProjectionWorkaround(
2526
"update src1 set intcol=10 where charcol='a'", catalogReader);
2627
}
2728

2829
@Test
2930
void testInsert() throws SqlParseException {
30-
assertFullRoundTripWorkaroundOptimizer(
31+
assertFullRoundTripWithIdentityProjectionWorkaround(
3132
"insert into src1 (intcol, charcol) values (1,'a'); ", catalogReader);
32-
assertFullRoundTripWorkaroundOptimizer(
33+
assertFullRoundTripWithIdentityProjectionWorkaround(
3334
"insert into src1 (intcol, charcol) select intcol,charcol from src2;", catalogReader);
3435
}
3536
}

0 commit comments

Comments
 (0)