88import org .codehaus .commons .compiler .ISimpleCompiler ;
99import org .codehaus .janino .CompilerFactory ;
1010
11+ import co .clflushopt .glint .query .logical .expr .LogicalColumnExpr ;
1112import co .clflushopt .glint .query .logical .plan .LogicalPlan ;
1213import co .clflushopt .glint .query .logical .plan .Scan ;
14+ import co .clflushopt .glint .types .ArrowTypes ;
15+ import co .clflushopt .glint .types .Field ;
1316
1417/**
1518 * Core Query Compiler using Janino for runtime code generation. This class
@@ -113,30 +116,48 @@ public Object compile(LogicalPlan logicalPlan) throws Exception {
113116 }
114117
115118 /**
116- * Generates source code for a given logical plan.
119+ * Compile a logical plan to Java code .
117120 *
118121 * @param logicalPlan The logical plan to generate code for
119122 * @return Generated Java source code as a string
120123 */
121124 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;
125138
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+ }
132142
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+ """
133154 public class LogicalPlanExecutor {
134155 public long execute() {
135156 return executePlan(%s);
136157 }
137158
138159 private long executePlan(LogicalPlan plan) {
139- // Handle different logical plan types
160+ // TODO: Handle different logical plan types
140161 if (plan instanceof Scan) {
141162 Scan scan = (Scan) plan;
142163 return executeScan(scan);
@@ -154,14 +175,71 @@ private long executeScan(Scan scan) {
154175 for (RecordBatch batch : dataSource.scan(projections)) {
155176 totalRecords += batch.getRowCount();
156177 System.out.println("Scan batch size: " + batch.getRowCount() +
157- " Path: " + scan.getPath());
178+ " Path: " + scan.getPath());
158179 }
159180
160181 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";
161234 }
162235 }
163236 """ ,
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+ );
165243 }
166244
167245 /**
0 commit comments