Skip to content

Commit 48f7272

Browse files
committed
Refactor TableSource to store columnNames for better SQL projection
1 parent 28413ad commit 48f7272

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

  • wayang-commons/wayang-basic/src/main/java/org/apache/wayang/basic/operators
  • wayang-platforms/wayang-jdbc-template/src/main/java/org/apache/wayang/jdbc/operators

wayang-commons/wayang-basic/src/main/java/org/apache/wayang/basic/operators/TableSource.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
public class TableSource extends UnarySource<Record> {
3030

3131
private final String tableName;
32+
private final String[] columnNames; // Line 32
33+
34+
// Add this method somewhere around line 35
35+
public String[] getColumnNames() {
36+
return this.columnNames;
37+
}
3238

3339
public String getTableName() {
3440
return tableName;
@@ -42,12 +48,15 @@ public String getTableName() {
4248
* into Wayang, so as to allow specific optimizations
4349
*/
4450
public TableSource(String tableName, String... columnNames) {
45-
this(tableName, createOutputDataSetType(columnNames));
51+
super(createOutputDataSetType(columnNames));
52+
this.tableName = tableName;
53+
this.columnNames = columnNames; // Assigned here!
4654
}
4755

4856
public TableSource(String tableName, DataSetType<Record> type) {
4957
super(type);
5058
this.tableName = tableName;
59+
this.columnNames=new String[0];
5160
}
5261

5362
/**
@@ -71,6 +80,7 @@ private static DataSetType<Record> createOutputDataSetType(String[] columnNames)
7180
public TableSource(TableSource that) {
7281
super(that);
7382
this.tableName = that.getTableName();
83+
this.columnNames = that.getColumnNames(); // ADD THIS LINE HERE!
7484
}
7585

7686
}

wayang-platforms/wayang-jdbc-template/src/main/java/org/apache/wayang/jdbc/operators/JdbcTableSource.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ public JdbcTableSource(JdbcTableSource that) {
5555

5656
@Override
5757
public String createSqlClause(Connection connection, FunctionCompiler compiler) {
58-
return this.getTableName();
59-
}
58+
// Call the method we just created in the parent
59+
String[] cols = this.getColumnNames();
60+
61+
if (cols == null || cols.length == 0) {
62+
return String.format("SELECT * FROM %s", this.getTableName());
63+
}
6064

65+
String columns = String.join(", ", cols);
66+
return String.format("SELECT %s FROM %s", columns, this.getTableName());
67+
}
6168

6269
@Override
6370
public String getLoadProfileEstimatorConfigurationKey() {

0 commit comments

Comments
 (0)