8
8
import org .codehaus .commons .compiler .ISimpleCompiler ;
9
9
import org .codehaus .janino .CompilerFactory ;
10
10
11
+ import co .clflushopt .glint .query .logical .expr .LogicalColumnExpr ;
11
12
import co .clflushopt .glint .query .logical .plan .LogicalPlan ;
12
13
import co .clflushopt .glint .query .logical .plan .Scan ;
14
+ import co .clflushopt .glint .types .ArrowTypes ;
15
+ import co .clflushopt .glint .types .Field ;
13
16
14
17
/**
15
18
* Core Query Compiler using Janino for runtime code generation. This class
@@ -113,30 +116,48 @@ public Object compile(LogicalPlan logicalPlan) throws Exception {
113
116
}
114
117
115
118
/**
116
- * Generates source code for a given logical plan.
119
+ * Compile a logical plan to Java code .
117
120
*
118
121
* @param logicalPlan The logical plan to generate code for
119
122
* @return Generated Java source code as a string
120
123
*/
121
124
private String generateSourceCode (LogicalPlan logicalPlan ) {
122
- return String .format (
123
- """
124
- package co.clflushopt.glint.generated;
125
+ return String .format ("""
126
+ package co.clflushopt.glint.generated;
127
+
128
+ import co.clflushopt.glint.query.logical.plan.LogicalPlan;
129
+ import co.clflushopt.glint.query.logical.plan.Scan;
130
+ import co.clflushopt.glint.query.logical.plan.Filter;
131
+ import co.clflushopt.glint.datasource.DataSource;
132
+ import co.clflushopt.glint.types.RecordBatch;
133
+ import co.clflushopt.glint.types.Schema;
134
+ import co.clflushopt.glint.types.RecordBatch;
135
+ import co.clflushopt.glint.types.ArrowTypes;
136
+
137
+ import java.util.List;
125
138
126
- import co.clflushopt.glint.query.logical.plan.LogicalPlan;
127
- import co.clflushopt.glint.query.logical.plan.Scan;
128
- import co.clflushopt.glint.datasource.DataSource;
129
- import co.clflushopt.glint.types.RecordBatch;
130
- import co.clflushopt.glint.types.Schema;
131
- import java.util.List;
139
+ %s
140
+ """ , compileScanPlan (logicalPlan ));
141
+ }
132
142
143
+ /**
144
+ * Compile scan operator.
145
+ *
146
+ *
147
+ * @param expr
148
+ * @param plan
149
+ * @return
150
+ */
151
+ private String compileScanPlan (LogicalPlan plan ) {
152
+ return String .format (
153
+ """
133
154
public class LogicalPlanExecutor {
134
155
public long execute() {
135
156
return executePlan(%s);
136
157
}
137
158
138
159
private long executePlan(LogicalPlan plan) {
139
- // Handle different logical plan types
160
+ // TODO: Handle different logical plan types
140
161
if (plan instanceof Scan) {
141
162
Scan scan = (Scan) plan;
142
163
return executeScan(scan);
@@ -154,14 +175,71 @@ private long executeScan(Scan scan) {
154
175
for (RecordBatch batch : dataSource.scan(projections)) {
155
176
totalRecords += batch.getRowCount();
156
177
System.out.println("Scan batch size: " + batch.getRowCount() +
157
- " Path: " + scan.getPath());
178
+ " Path: " + scan.getPath());
158
179
}
159
180
160
181
return totalRecords;
182
+ }
183
+ }""" ,
184
+ generatePlanArgument (plan ));
185
+
186
+ }
187
+
188
+ /**
189
+ * Generates source code specifically for column expressions.
190
+ *
191
+ * @param columnExpr The column expression to generate code for
192
+ * @param plan The logical plan context
193
+ * @return Generated Java source code for the column expression
194
+ */
195
+ private String compileColumnExpr (LogicalColumnExpr columnExpr , LogicalPlan plan ) {
196
+ // Retrieve the field information for the column
197
+ Field field = columnExpr .toField (plan );
198
+
199
+ return String .format (
200
+ """
201
+ public class ExpressionExecutor {
202
+ // Add method to find column index
203
+ private int findColumnIndex(RecordBatch batch) {
204
+ Schema schema = batch.getSchema();
205
+ return IntStream.range(0, schema.getFields().size())
206
+ .filter(idx -> schema.getFields().get(idx).name().equals(columnExpr.getName()))
207
+ .findFirst()
208
+ .orElseThrow(() -> new IllegalArgumentException("Column '" + columnExpr.getName() + "' not found in schema"));
209
+ }
210
+ /**
211
+ * Extracts the value of the column '%s' from a record batch.
212
+ *
213
+ * @param batch The record batch to extract the value from
214
+ * @param rowIndex The index of the row to extract
215
+ * @return The value of the column
216
+ */
217
+ public Object getValue(RecordBatch batch, int rowIndex) {
218
+ int columnIndex = findColumnIndex(batch);
219
+ return batch.getField(rowIndex);
220
+ }
221
+
222
+ /**
223
+ * Gets the field metadata for the column.
224
+ *
225
+ * @return Field metadata
226
+ */
227
+ public Field getFieldMetadata() {
228
+ return new Field("%s", %s);
229
+ }
230
+
231
+ @Override
232
+ public String toString() {
233
+ return "%s";
161
234
}
162
235
}
163
236
""" ,
164
- generatePlanArgument (logicalPlan ));
237
+ columnExpr .getName (), // Column name for comments
238
+ columnExpr .getName (), // Method to get column value
239
+ columnExpr .getName (), // Field name
240
+ ArrowTypes .compile (field .dataType ()), // Field type
241
+ columnExpr .toString () // String representation
242
+ );
165
243
}
166
244
167
245
/**
0 commit comments