You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by ts...@apache.org on 2013/02/17 20:25:28 UTC
[3/9] git commit: JDBC driver, SQL parser, and planner that generates
logical plan. Test very simple statement that returns a single column that is
a map; returns 5 rows.
JDBC driver, SQL parser, and planner that generates logical plan. Test very simple statement that returns a single column that is a map; returns 5 rows.
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/7f294adb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/7f294adb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/7f294adb
Branch: refs/heads/master
Commit: 7f294adb649064e26dd2f28864260b17b07315ef
Parents: d8238cc
Author: Julian Hyde <ju...@gmail.com>
Authored: Thu Feb 14 17:00:48 2013 -0800
Committer: Julian Hyde <ju...@gmail.com>
Committed: Thu Feb 14 17:03:26 2013 -0800
----------------------------------------------------------------------
.gitignore | 1 +
.../apache/drill/common/logical/LogicalPlan.java | 39 +++-
.../org/apache/drill/exec/ref/ROPConverter.java | 29 ++-
.../drill/exec/ref/ReferenceInterpreter.java | 13 +-
.../apache/drill/exec/ref/rops/BaseSinkROP.java | 2 +-
.../apache/drill/exec/ref/rops/JSONScanner.java | 6 +-
.../org/apache/drill/exec/ref/rops/JSONWriter.java | 3 +-
.../apache/drill/exec/ref/rops/QueueSinkROP.java | 65 ++++++
.../java/org/apache/drill/exec/ref/rops/ROP.java | 5 +-
.../org/apache/drill/exec/ref/rops/ROPBase.java | 5 +-
sandbox/prototype/sqlparser/pom.xml | 34 +++-
.../org/apache/drill/jdbc/DrillDriverVersion.java | 41 ++++
.../java/org/apache/drill/jdbc/DrillTable.java | 101 ++++++++++
.../main/java/org/apache/drill/jdbc/Driver.java | 104 ++++++++++
.../org/apache/drill/optiq/DrillImplementor.java | 97 +++++++++
.../java/org/apache/drill/optiq/DrillOptiq.java | 46 +++++
.../main/java/org/apache/drill/optiq/DrillRel.java | 29 +++
.../java/org/apache/drill/optiq/DrillScan.java | 45 ++++
.../org/apache/drill/optiq/EnumerableDrill.java | 156 +++++++++++++++
.../org/apache/drill/optiq/EnumerableDrillRel.java | 101 ++++++++++
.../apache/drill/optiq/EnumerableDrillRule.java | 59 ++++++
.../java/org/apache/drill/jdbc/test/JdbcTest.java | 88 ++++++++
22 files changed, 1032 insertions(+), 37 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index 4f8967e..628a4e3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@
.idea/
*.iml
target/
+*~
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/common/src/main/java/org/apache/drill/common/logical/LogicalPlan.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/common/src/main/java/org/apache/drill/common/logical/LogicalPlan.java b/sandbox/prototype/common/src/main/java/org/apache/drill/common/logical/LogicalPlan.java
index e7f01cf..7726330 100644
--- a/sandbox/prototype/common/src/main/java/org/apache/drill/common/logical/LogicalPlan.java
+++ b/sandbox/prototype/common/src/main/java/org/apache/drill/common/logical/LogicalPlan.java
@@ -18,11 +18,14 @@
package org.apache.drill.common.logical;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.drill.common.logical.OperatorGraph.OpNode;
import org.apache.drill.common.logical.data.LogicalOperator;
import org.apache.drill.common.logical.graph.GraphAlgos;
@@ -95,14 +98,10 @@ public class LogicalPlan {
public List<DataSource> getDataSources() {
return new ArrayList<DataSource>(dataSources.values());
}
-
-
- public static void main(String[] args) throws Exception {
-
-
+ private static ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
-
+
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
mapper.configure(Feature.ALLOW_COMMENTS, true);
@@ -110,10 +109,32 @@ public class LogicalPlan {
mapper.registerSubtypes(LogicalOperator.SUB_TYPES);
mapper.registerSubtypes(DataSource.SUB_TYPES);
mapper.registerSubtypes(RecordMaker.SUB_TYPES);
+ return mapper;
+ }
+ public static void main(String[] args) throws Exception {
String externalPlan = Files.toString(new File("src/test/resources/simple_plan.json"), Charsets.UTF_8);
- LogicalPlan plan = mapper.readValue(externalPlan, LogicalPlan.class);
- System.out.println(mapper.writeValueAsString(plan));
+ LogicalPlan plan = parse(externalPlan);
+ }
+
+ /** Parses a logical plan. */
+ public static LogicalPlan parse(String planString) {
+ ObjectMapper mapper = createMapper();
+ try {
+ LogicalPlan plan = mapper.readValue(planString, LogicalPlan.class);
+ System.out.println(mapper.writeValueAsString(plan));
+ return plan;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /** Converts a logical plan to a string. (Opposite of {@link #parse}.) */
+ public String unparse() {
+ try {
+ return createMapper().writeValueAsString(this);
+ } catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
}
-
}
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ROPConverter.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ROPConverter.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ROPConverter.java
index c4aba43..db9f38b 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ROPConverter.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ROPConverter.java
@@ -22,6 +22,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
+import java.util.Queue;
import org.apache.drill.common.logical.LogicalPlan;
import org.apache.drill.common.logical.data.Aggregate;
@@ -33,11 +34,7 @@ import org.apache.drill.common.logical.sources.DataSource;
import org.apache.drill.common.logical.sources.JSONDataSource;
import org.apache.drill.exec.ref.eval.EvaluatorFactory;
import org.apache.drill.exec.ref.exceptions.SetupException;
-import org.apache.drill.exec.ref.rops.AggregateSimpleROP;
-import org.apache.drill.exec.ref.rops.JSONScanner;
-import org.apache.drill.exec.ref.rops.JSONWriter;
-import org.apache.drill.exec.ref.rops.ROP;
-import org.apache.drill.exec.ref.rops.UnionROP;
+import org.apache.drill.exec.ref.rops.*;
class ROPConverter {
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ROPConverter.class);
@@ -45,11 +42,13 @@ class ROPConverter {
private LogicalPlan plan;
private IteratorRegistry registry;
private EvaluatorFactory builder;
+ private final List<Queue> sinkQueues;
- public ROPConverter(LogicalPlan plan, IteratorRegistry registry, EvaluatorFactory builder) {
+ public ROPConverter(LogicalPlan plan, IteratorRegistry registry, EvaluatorFactory builder, List<Queue> sinkQueues) {
this.plan = plan;
this.registry = registry;
this.builder = builder;
+ this.sinkQueues = sinkQueues;
}
public void convert(LogicalOperator o) throws SetupException {
@@ -100,8 +99,22 @@ class ROPConverter {
}
public void convertSpecific(Write write) throws SetupException {
- JSONWriter writer = new JSONWriter(write);
- writer.init(registry, builder);
+ if (write.getFileName().startsWith("socket:")) {
+ final String ordinalString =
+ write.getFileName().substring("socket:".length());
+ final Queue queue;
+ try {
+ int ordinal = Integer.valueOf(ordinalString);
+ queue = sinkQueues.get(ordinal);
+ } catch (IndexOutOfBoundsException | NumberFormatException e) {
+ throw new RuntimeException("bad socket ordinal " + ordinalString);
+ }
+ final QueueSinkROP queueSink = new QueueSinkROP(write, queue);
+ queueSink.init(registry, builder);
+ } else {
+ JSONWriter writer = new JSONWriter(write);
+ writer.init(registry, builder);
+ }
}
public void convertSpecific(Aggregate agg) throws SetupException {
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ReferenceInterpreter.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ReferenceInterpreter.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ReferenceInterpreter.java
index 7621b7f..3df6b4b 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ReferenceInterpreter.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/ReferenceInterpreter.java
@@ -19,10 +19,7 @@ package org.apache.drill.exec.ref;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.*;
import org.apache.drill.common.logical.LogicalPlan;
import org.apache.drill.common.logical.data.LogicalOperator;
@@ -48,10 +45,11 @@ public class ReferenceInterpreter {
private ROPConverter converter;
private IteratorRegistry registry;
- public ReferenceInterpreter(LogicalPlan p, IteratorRegistry r, EvaluatorFactory builder){
+ public ReferenceInterpreter(LogicalPlan p, IteratorRegistry r,
+ EvaluatorFactory builder, List<Queue> sinkQueues){
this.plan = p;
this.registry = r;
- this.converter = new ROPConverter(p, registry, builder);
+ this.converter = new ROPConverter(p, registry, builder, sinkQueues);
}
/** Generate Reference equivalents to each operation and then collect and store all the sinks.
@@ -95,7 +93,8 @@ public class ReferenceInterpreter {
String externalPlan = Files.toString(new File(jsonFile), Charsets.UTF_8);
LogicalPlan plan = mapper.readValue(externalPlan, LogicalPlan.class);
IteratorRegistry ir = new IteratorRegistry();
- ReferenceInterpreter i = new ReferenceInterpreter(plan, ir, new BasicEvaluatorFactory(ir));
+ ReferenceInterpreter i = new ReferenceInterpreter(plan, ir, new BasicEvaluatorFactory(ir),
+ Collections.<Queue>emptyList());
i.setup();
Collection<RunOutcome> outcomes = i.run();
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/BaseSinkROP.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/BaseSinkROP.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/BaseSinkROP.java
index 9cf70e6..60bf6e2 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/BaseSinkROP.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/BaseSinkROP.java
@@ -96,7 +96,7 @@ public abstract class BaseSinkROP<T extends SinkOperator> extends SingleInputROP
exception = e ;
}
- this.cleanup();
+ cleanup(outcome);
return new RunOutcome(outcome, pos, recordCount, exception);
}
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONScanner.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONScanner.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONScanner.java
index 924f3b1..ba29312 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONScanner.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONScanner.java
@@ -23,9 +23,7 @@ import java.util.Iterator;
import org.apache.drill.common.expression.SchemaPath;
import org.apache.drill.common.logical.data.Scan;
-import org.apache.drill.exec.ref.RecordIterator;
-import org.apache.drill.exec.ref.RecordPointer;
-import org.apache.drill.exec.ref.UnbackedRecord;
+import org.apache.drill.exec.ref.*;
import org.apache.drill.exec.ref.exceptions.RecordException;
import org.apache.drill.exec.ref.values.DataValue;
import org.apache.drill.exec.ref.values.SimpleArrayValue;
@@ -170,7 +168,7 @@ public class JSONScanner extends ROPBase<Scan> {
}
@Override
- public void cleanup() {
+ public void cleanup(RunOutcome.OutcomeType outcome) {
try {
parser.close();
this.input.close();
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONWriter.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONWriter.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONWriter.java
index 23b4e79..6bc6c2e 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONWriter.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/JSONWriter.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import org.apache.drill.common.logical.data.Write;
import org.apache.drill.exec.ref.RecordPointer;
+import org.apache.drill.exec.ref.RunOutcome;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
@@ -64,7 +65,7 @@ public class JSONWriter extends BaseSinkROP<Write> {
}
@Override
- public void cleanup() {
+ public void cleanup(RunOutcome.OutcomeType outcome) {
try {
writer.close();
output.close();
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/QueueSinkROP.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/QueueSinkROP.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/QueueSinkROP.java
new file mode 100644
index 0000000..bba818d
--- /dev/null
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/QueueSinkROP.java
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.exec.ref.rops;
+
+import org.apache.drill.common.logical.data.SinkOperator;
+import org.apache.drill.exec.ref.RecordPointer;
+import org.apache.drill.exec.ref.RunOutcome;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Queue;
+
+/**
+ * Sink that writes each record to a queue.
+ */
+public class QueueSinkROP extends BaseSinkROP<SinkOperator> {
+ // Records are written as byte arrays representing JSON documents. The
+ // receiver presumably re-hydrates them. A representation using compact maps
+ // and lists would probably be better.
+ //
+ // When processing is finished, an OutcomeType object is written.
+ private final Queue<Object> queue;
+
+ public QueueSinkROP(SinkOperator config, Queue<Object> queue) {
+ super(config);
+ this.queue = queue;
+ }
+
+ protected void setupSink() throws IOException {
+ // nothing
+ }
+
+ @Override
+ public long sinkRecord(RecordPointer r) throws IOException {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ final JSONDataWriter writer = new JSONDataWriter(baos);
+ r.write(writer);
+ writer.close();
+ queue.add(baos.toByteArray());
+ return 0;
+ }
+
+ @Override
+ public void cleanup(RunOutcome.OutcomeType outcome) {
+ assert outcome != null;
+ queue.add(outcome);
+ }
+}
+
+// End QueueSinkROP.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROP.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROP.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROP.java
index fe932c2..59b58d3 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROP.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROP.java
@@ -1,12 +1,11 @@
package org.apache.drill.exec.ref.rops;
-import org.apache.drill.exec.ref.IteratorRegistry;
-import org.apache.drill.exec.ref.RecordIterator;
+import org.apache.drill.exec.ref.*;
import org.apache.drill.exec.ref.eval.EvaluatorFactory;
import org.apache.drill.exec.ref.exceptions.SetupException;
public interface ROP {
public void init(IteratorRegistry registry, EvaluatorFactory builder) throws SetupException;
public RecordIterator getOutput();
- public void cleanup();
+ public void cleanup(RunOutcome.OutcomeType outcome);
}
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROPBase.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROPBase.java b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROPBase.java
index e7672fd..396d211 100644
--- a/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROPBase.java
+++ b/sandbox/prototype/exec/ref/src/main/java/org/apache/drill/exec/ref/rops/ROPBase.java
@@ -18,8 +18,7 @@
package org.apache.drill.exec.ref.rops;
import org.apache.drill.common.logical.data.LogicalOperator;
-import org.apache.drill.exec.ref.IteratorRegistry;
-import org.apache.drill.exec.ref.RecordIterator;
+import org.apache.drill.exec.ref.*;
import org.apache.drill.exec.ref.eval.EvaluatorFactory;
import org.apache.drill.exec.ref.exceptions.SetupException;
@@ -53,7 +52,7 @@ public abstract class ROPBase<T extends LogicalOperator> implements ROP{
}
@Override
- public void cleanup() {
+ public void cleanup(RunOutcome.OutcomeType outcome) {
}
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/pom.xml
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/pom.xml b/sandbox/prototype/sqlparser/pom.xml
index f5f2d5d..733fb1b 100644
--- a/sandbox/prototype/sqlparser/pom.xml
+++ b/sandbox/prototype/sqlparser/pom.xml
@@ -8,6 +8,7 @@
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>sqlparser</artifactId>
+ <packaging>jar</packaging>
<name>sqlparser</name>
<url>http://maven.apache.org</url>
<properties>
@@ -17,8 +18,39 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
- <version>3.8.1</version>
+ <version>4.11</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>net.hydromatic</groupId>
+ <artifactId>optiq</artifactId>
+ <version>0.1</version>
+ </dependency>
+ <dependency>
+ <groupId>net.hydromatic</groupId>
+ <artifactId>linq4j</artifactId>
+ <version>0.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.drill.exec</groupId>
+ <artifactId>ref</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.codehaus.janino</groupId>
+ <artifactId>janino</artifactId>
+ <version>2.6.1</version>
+ </dependency>
+ <!-- Specify xalan and xerces versions to avoid setXIncludeAware error. -->
+ <dependency>
+ <groupId>xerces</groupId>
+ <artifactId>xercesImpl</artifactId>
+ <version>2.9.1</version>
+ </dependency>
+ <dependency>
+ <groupId>xalan</groupId>
+ <artifactId>xalan</artifactId>
+ <version>2.7.1</version>
+ </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillDriverVersion.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillDriverVersion.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillDriverVersion.java
new file mode 100644
index 0000000..26f5b01
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillDriverVersion.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.jdbc;
+
+import net.hydromatic.optiq.jdbc.DriverVersion;
+
+/**
+ * Version information for Drill JDBC Driver.
+ */
+class DrillDriverVersion extends DriverVersion {
+ /** Creates a DrillDriverVersion. */
+ DrillDriverVersion() {
+ super(
+ "Apache Drill JDBC Driver",
+ "1.0",
+ "Drill",
+ "1.0",
+ true,
+ 1,
+ 0,
+ 1,
+ 0);
+ }
+}
+
+// End DrillDriverVersion.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillTable.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillTable.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillTable.java
new file mode 100644
index 0000000..eae7fdb
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/DrillTable.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.jdbc;
+
+import net.hydromatic.linq4j.BaseQueryable;
+import net.hydromatic.linq4j.Enumerator;
+import net.hydromatic.linq4j.Linq4j;
+import net.hydromatic.linq4j.expressions.Expression;
+import net.hydromatic.linq4j.expressions.Expressions;
+import net.hydromatic.linq4j.expressions.MethodCallExpression;
+import net.hydromatic.optiq.*;
+
+import org.apache.drill.common.logical.sources.DataSource;
+import org.apache.drill.optiq.DrillOptiq;
+import org.apache.drill.optiq.DrillScan;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.relopt.RelOptTable;
+import org.eigenbase.reltype.RelDataType;
+import org.eigenbase.reltype.RelDataTypeFactory;
+import org.eigenbase.sql.type.SqlTypeName;
+
+import java.lang.reflect.Type;
+import java.util.Collections;
+
+/** Optiq Table used by Drill. */
+public class DrillTable extends BaseQueryable<Object>
+ implements TranslatableTable<Object>
+{
+ private final Schema schema;
+ private final String name;
+ private final RelDataType rowType;
+ public final DataSource dataSource;
+
+ /** Creates a DrillTable. */
+ public DrillTable(Schema schema,
+ Type elementType,
+ Expression expression,
+ RelDataType rowType,
+ String name,
+ DataSource dataSource) {
+ super(schema.getQueryProvider(), elementType, expression);
+ this.schema = schema;
+ this.name = name;
+ this.rowType = rowType;
+ this.dataSource = dataSource;
+ }
+
+ static void addTable(RelDataTypeFactory typeFactory, MutableSchema schema,
+ String name, DataSource dataSource) {
+ final MethodCallExpression call = Expressions.call(schema.getExpression(),
+ BuiltinMethod.DATA_CONTEXT_GET_TABLE.method,
+ Expressions.constant(name),
+ Expressions.constant(Object.class));
+ final RelDataType rowType =
+ typeFactory.createStructType(
+ Collections.singletonList(
+ typeFactory.createSqlType(SqlTypeName.ANY)),
+ Collections.singletonList("_extra"));
+ final DrillTable table =
+ new DrillTable(schema, Object.class, call, rowType, name, dataSource);
+ schema.addTable(name, table);
+ }
+
+ @Override
+ public DataContext getDataContext() {
+ return schema;
+ }
+
+ @Override
+ public RelDataType getRowType() {
+ return rowType;
+ }
+
+ @Override
+ public Enumerator<Object> enumerator() {
+ return Linq4j.emptyEnumerator();
+ }
+
+ public RelNode toRel(RelOptTable.ToRelContext context, RelOptTable table) {
+ return new DrillScan(context.getCluster(),
+ context.getCluster().traitSetOf(DrillOptiq.CONVENTION),
+ table);
+ }
+}
+
+// End DrillTable.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/Driver.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/Driver.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/Driver.java
new file mode 100644
index 0000000..73cdcd8
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/jdbc/Driver.java
@@ -0,0 +1,104 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.jdbc;
+
+import net.hydromatic.optiq.*;
+import net.hydromatic.optiq.impl.java.MapSchema;
+import net.hydromatic.optiq.jdbc.*;
+import org.apache.drill.common.logical.sources.JSONDataSource;
+
+import java.sql.SQLException;
+import java.util.Collections;
+
+/**
+ * JDBC driver for Apache Drill.
+ */
+public class Driver extends UnregisteredDriver {
+ public static final String CONNECT_STRING_PREFIX = "jdbc:drill:";
+
+ static {
+ new Driver().register();
+ }
+
+ protected String getConnectStringPrefix() {
+ return CONNECT_STRING_PREFIX;
+ }
+
+ protected DriverVersion createDriverVersion() {
+ return new DrillDriverVersion();
+ }
+
+ @Override
+ protected Handler createHandler() {
+ return new DrillHandler();
+ }
+
+ private static class DrillHandler extends HandlerImpl {
+ public void onConnectionInit(OptiqConnection connection)
+ throws SQLException
+ {
+ super.onConnectionInit(connection);
+
+ // The "schema" parameter currently gives a name to the schema. In future
+ // it will choose a schema that (presumably) already exists.
+ final String schemaName =
+ connection.getProperties().getProperty("schema");
+ if (schemaName == null) {
+ throw new SQLException("schema connection property must be specified");
+ }
+ final MutableSchema rootSchema = connection.getRootSchema();
+ final MapSchema schema =
+ MapSchema.create(connection, rootSchema, schemaName);
+
+ connection.setSchema(schemaName);
+
+ final JSONDataSource dataSource = new JSONDataSource();
+ dataSource.name = "donuts-json";
+ dataSource.files =
+ Collections.singletonList("src/test/resources/donuts.json");
+
+ // "tables" is a temporary parameter. We should replace with
+ // "schemaUri", which is the URI of a schema.json file, or the name of a
+ // schema from a server, if drill is running in a server.
+ final String tables = connection.getProperties().getProperty("tables");
+ if (tables == null) {
+ throw new SQLException("tables connection property must be specified");
+ }
+ final String[] tables2 = tables.split(",");
+ for (String table : tables2) {
+ DrillTable.addTable(connection.getTypeFactory(), schema, table,
+ dataSource);
+ }
+ }
+ }
+
+/*
+
+optiq work
+==========
+
+1. todo: test can cast(<any> as varchar) (or indeed to any type)
+
+2. We declare a variant record by adding a '_extra any' field. It's nice that
+there can be some declared fields, and some undeclared. todo: Better syntactic
+sugar.
+
+*/
+}
+
+// End Driver.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillImplementor.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillImplementor.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillImplementor.java
new file mode 100644
index 0000000..f4ca721
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillImplementor.java
@@ -0,0 +1,97 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.optiq;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Context for converting a tree of {@link DrillRel} nodes into a Drill logical
+ * plan.
+ */
+public class DrillImplementor {
+ final ObjectMapper mapper = new ObjectMapper();
+ {
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
+ mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
+ mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
+ }
+ private final ObjectNode rootNode = mapper.createObjectNode();
+ private final ArrayNode operatorsNode;
+
+ public DrillImplementor() {
+ final ObjectNode headNode = mapper.createObjectNode();
+ rootNode.put("head", headNode);
+ headNode.put("type", "apache_drill_logical_plan");
+ headNode.put("version", "1");
+
+ final ObjectNode generatorNode = mapper.createObjectNode();
+ headNode.put("generator", generatorNode);
+ generatorNode.put("type", "manual");
+ generatorNode.put("info", "na");
+
+ // TODO: populate sources based on the sources of scans that occur in
+ // the query
+ final ArrayNode sourcesNode = mapper.createArrayNode();
+ rootNode.put("sources", sourcesNode);
+ final ObjectNode sourceNode = mapper.createObjectNode();
+ sourcesNode.add(sourceNode);
+ sourceNode.put("type", "json");
+ sourceNode.put("name", "donuts-json");
+ final ArrayNode filesNode = mapper.createArrayNode();
+ sourceNode.put("files", filesNode);
+ filesNode.add("src/test/resources/donuts.json");
+
+ final ArrayNode queryNode = mapper.createArrayNode();
+ rootNode.put("query", queryNode);
+
+ final ObjectNode sequenceOpNode = mapper.createObjectNode();
+ queryNode.add(sequenceOpNode);
+ sequenceOpNode.put("op", "sequence");
+
+ this.operatorsNode = mapper.createArrayNode();
+ sequenceOpNode.put("do", operatorsNode);
+ }
+
+ public void go(DrillRel root) {
+ root.implement(this);
+
+ // Add a last node, to write to the output queue.
+ final ObjectNode writeOp = mapper.createObjectNode();
+ writeOp.put("op", "write");
+ writeOp.put("memo", "output sink");
+ writeOp.put("file", "socket:0");
+ add(writeOp);
+ }
+
+ public void add(ObjectNode operator) {
+ operatorsNode.add(operator);
+ }
+
+ /** Returns the generated plan. */
+ public String getJsonString() {
+ String s = rootNode.toString();
+ System.out.println(s);
+ return s;
+ }
+}
+
+// End DrillImplementor.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillOptiq.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillOptiq.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillOptiq.java
new file mode 100644
index 0000000..d219706
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillOptiq.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.optiq;
+
+import org.eigenbase.relopt.Convention;
+import org.eigenbase.relopt.RelOptPlanner;
+
+/**
+ * @author jhyde
+ */
+public class DrillOptiq {
+ /** Calling convention for relational expressions that are "implemented" by
+ * generating Drill logical plans. */
+ public static final Convention CONVENTION = new Convention.Impl("DRILL", DrillRel.class);
+
+ static void registerStandardPlannerRules(RelOptPlanner planner) {
+ planner.addRule(EnumerableDrillRule.ARRAY_INSTANCE);
+ planner.addRule(EnumerableDrillRule.CUSTOM_INSTANCE);
+
+// planner.addRule( CascadingTableModificationConverterRule.INSTANCE );
+// planner.addRule( CascadingAggregateConverterRule.INSTANCE );
+// planner.addRule( CascadingCalcConverterRule.INSTANCE );
+
+// planner.addRule( CascadingFilterRule.INSTANCE );
+// planner.addRule( CascadingProjectRule.INSTANCE );
+// planner.addRule( CascadingSortRule.INSTANCE );
+// planner.addRule( CascadingJoinRule.INSTANCE );
+// planner.addRule( CascadingUnionRule.INSTANCE );
+// planner.addRule( AbstractConverter.ExpandConversionRule.instance );
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillRel.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillRel.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillRel.java
new file mode 100644
index 0000000..176a5dc
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillRel.java
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.optiq;
+
+import org.eigenbase.rel.RelNode;
+
+/**
+ * Relational expression that is implemented in Drill.
+ */
+public interface DrillRel extends RelNode {
+ void implement(DrillImplementor implementor);
+}
+
+// End DrillRel.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillScan.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillScan.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillScan.java
new file mode 100644
index 0000000..87a32ad
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/DrillScan.java
@@ -0,0 +1,45 @@
+package org.apache.drill.optiq;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.drill.jdbc.DrillTable;
+
+import org.eigenbase.rel.TableAccessRelBase;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelOptTable;
+import org.eigenbase.relopt.RelTraitSet;
+
+/**
+ * Scan of a Drill table.
+ */
+public class DrillScan extends TableAccessRelBase implements DrillRel {
+ private final DrillTable drillTable;
+
+ /** Creates a DrillScan. */
+ public DrillScan(RelOptCluster cluster,
+ RelTraitSet traits,
+ RelOptTable table)
+ {
+ super(cluster, traits, table);
+ assert traits.contains(DrillOptiq.CONVENTION);
+ this.drillTable = table.unwrap(DrillTable.class);
+ assert drillTable != null;
+ }
+
+ @Override
+ public void register(RelOptPlanner planner) {
+ super.register(planner);
+ DrillOptiq.registerStandardPlannerRules(planner);
+ }
+
+ public void implement(DrillImplementor implementor) {
+ final ObjectNode node = implementor.mapper.createObjectNode();
+ node.put("op", "scan");
+ node.put("memo", "initial_scan");
+ node.put("ref", "donuts");
+ node.put("source", drillTable.dataSource.getName());
+ implementor.add(node);
+ }
+}
+
+// End DrillScan.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrill.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrill.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrill.java
new file mode 100644
index 0000000..c68bf85
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrill.java
@@ -0,0 +1,156 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.optiq;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import net.hydromatic.linq4j.AbstractEnumerable;
+import net.hydromatic.linq4j.Enumerable;
+import net.hydromatic.linq4j.Enumerator;
+
+import org.apache.drill.common.logical.LogicalPlan;
+import org.apache.drill.exec.ref.IteratorRegistry;
+import org.apache.drill.exec.ref.ReferenceInterpreter;
+import org.apache.drill.exec.ref.RunOutcome;
+import org.apache.drill.exec.ref.eval.BasicEvaluatorFactory;
+
+import java.io.IOException;
+import java.util.*;
+import java.util.concurrent.*;
+
+/**
+ * Runtime helper that executes a Drill query and converts it into an
+ * {@link Enumerable}.
+ */
+public class EnumerableDrill<E extends JsonNode>
+ extends AbstractEnumerable<E>
+ implements Enumerable<E> {
+ private final LogicalPlan plan;
+ final BlockingQueue queue = new ArrayBlockingQueue(100);
+
+ /** Creates a DrillEnumerable.
+ *
+ * @param plan Logical plan
+ * @param clazz Type of elements returned from enumerable
+ */
+ public EnumerableDrill(LogicalPlan plan, Class<E> clazz) {
+ this.plan = plan;
+ }
+
+ /** Creates a DrillEnumerable from a plan represented as a string. */
+ public static <E extends JsonNode> EnumerableDrill<E> of(String plan,
+ Class<E> clazz) {
+ return new EnumerableDrill<E>(LogicalPlan.parse(plan), clazz);
+ }
+
+ /** Runs the plan as a background task. */
+ Future<Collection<RunOutcome>> runPlan(
+ CompletionService<Collection<RunOutcome>> service) {
+ IteratorRegistry ir = new IteratorRegistry();
+
+ final ReferenceInterpreter i = new ReferenceInterpreter(plan, ir,
+ new BasicEvaluatorFactory(ir), Collections.singletonList((Queue) queue));
+ try {
+ i.setup();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return service.submit(
+ new Callable<Collection<RunOutcome>>() {
+ @Override
+ public Collection<RunOutcome> call() throws Exception {
+ Collection<RunOutcome> outcomes = i.run();
+
+ for (RunOutcome outcome : outcomes) {
+ System.out.println("============");
+ System.out.println(outcome);
+ if (outcome.outcome == RunOutcome.OutcomeType.FAILED
+ && outcome.exception != null) {
+ outcome.exception.printStackTrace();
+ }
+ }
+ return outcomes;
+ }
+ });
+ }
+
+ @Override
+ public Enumerator<E> enumerator() {
+ // TODO: use a completion service from the container
+ final ExecutorCompletionService<Collection<RunOutcome>> service =
+ new ExecutorCompletionService<Collection<RunOutcome>>(
+ new ThreadPoolExecutor( 1, 1, 1, TimeUnit.SECONDS,
+ new LinkedBlockingDeque<Runnable>(10)));
+
+ // Run the plan using an executor. It runs in a different thread, writing
+ // results to our queue.
+ //
+ // TODO: use the result of task, and check for exceptions
+ final Future<Collection<RunOutcome>> task = runPlan(service);
+
+ return new Enumerator<E>() {
+ final ObjectMapper mapper = new ObjectMapper();
+ private E current;
+
+ @Override
+ public E current() {
+ return current;
+ }
+
+ @Override
+ public boolean moveNext() {
+ try {
+ Object o = queue.take();
+ if (o instanceof RunOutcome.OutcomeType) {
+ switch ((RunOutcome.OutcomeType) o) {
+ case SUCCESS:
+ return false; // end of data
+ case CANCELED:
+ throw new RuntimeException("canceled");
+ case FAILED:
+ default:
+ throw new RuntimeException("failed");
+ }
+ } else {
+ current = (E) parseJson((byte[]) o);
+ return true;
+ }
+ } catch (InterruptedException e) {
+ Thread.interrupted();
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void reset() {
+ throw new UnsupportedOperationException();
+ }
+
+ JsonNode parseJson(byte[] bytes) {
+ try {
+ return mapper.readTree(bytes);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+ }
+}
+
+// End EnumerableDrill.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRel.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRel.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRel.java
new file mode 100644
index 0000000..706bb5b
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRel.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.optiq;
+
+import net.hydromatic.linq4j.expressions.BlockBuilder;
+import net.hydromatic.linq4j.expressions.BlockExpression;
+import net.hydromatic.linq4j.expressions.Expressions;
+import net.hydromatic.optiq.impl.java.JavaTypeFactory;
+import net.hydromatic.optiq.rules.java.*;
+
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.SingleRel;
+import org.eigenbase.relopt.RelOptCluster;
+import org.eigenbase.relopt.RelOptCost;
+import org.eigenbase.relopt.RelOptPlanner;
+import org.eigenbase.relopt.RelTraitSet;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Method;
+import java.util.List;
+
+
+/**
+ * Relational expression that converts from Drill to Enumerable. At runtime
+ * it executes a Drill query and returns the results as an
+ * {@link net.hydromatic.linq4j.Enumerable}.
+ */
+public class EnumerableDrillRel extends SingleRel implements EnumerableRel {
+ private static final Logger LOG =
+ LoggerFactory.getLogger(EnumerableDrillRel.class);
+
+ private PhysType physType;
+
+ public EnumerableDrillRel(RelOptCluster cluster,
+ RelTraitSet traitSet,
+ RelNode input)
+ {
+ super(cluster, traitSet, input);
+ assert getConvention() instanceof EnumerableConvention;
+ physType = PhysTypeImpl.of((JavaTypeFactory) cluster.getTypeFactory(),
+ input.getRowType(),
+ (EnumerableConvention) getConvention());
+ }
+
+ public PhysType getPhysType() {
+ return physType;
+ }
+
+ @Override
+ public RelOptCost computeSelfCost(RelOptPlanner planner) {
+ return super.computeSelfCost(planner).multiplyBy(.1);
+ }
+
+ @Override
+ public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
+ return new EnumerableDrillRel(getCluster(), traitSet, sole(inputs));
+ }
+
+ public BlockExpression implement(EnumerableRelImplementor implementor) {
+ LOG.debug("implementing enumerable");
+
+ final DrillImplementor drillImplementor = new DrillImplementor();
+ DrillRel input = (DrillRel) getChild();
+
+ drillImplementor.go(input);
+ final Method method;
+ try {
+ method =
+ EnumerableDrill.class.getMethod("of", String.class, Class.class);
+ } catch (NoSuchMethodException e) {
+ throw new RuntimeException(e);
+ }
+ String plan = drillImplementor.getJsonString();
+ return new BlockBuilder()
+ .append(
+ Expressions.call(
+ method,
+ Expressions.constant(plan),
+ Expressions.constant(Class.class)))
+ .toBlock();
+ }
+}
+
+// End EnumerableDrillRel.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRule.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRule.java b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRule.java
new file mode 100644
index 0000000..eb3c9da
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/main/java/org/apache/drill/optiq/EnumerableDrillRule.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.optiq;
+
+import net.hydromatic.optiq.rules.java.EnumerableConvention;
+import org.eigenbase.rel.RelNode;
+import org.eigenbase.rel.convert.ConverterRule;
+import org.eigenbase.relopt.volcano.AbstractConverter;
+
+/**
+ * Rule that converts any Drill relational expression to enumerable format by
+ * adding a {@link EnumerableDrillRel}.
+ */
+public class EnumerableDrillRule extends ConverterRule {
+ public static final EnumerableDrillRule ARRAY_INSTANCE =
+ new EnumerableDrillRule(EnumerableConvention.ARRAY);
+ public static final EnumerableDrillRule CUSTOM_INSTANCE =
+ new EnumerableDrillRule(EnumerableConvention.CUSTOM);
+
+ private EnumerableDrillRule(EnumerableConvention outConvention) {
+ super(AbstractConverter.class,
+ DrillOptiq.CONVENTION,
+ outConvention,
+ "Convert drill rels to Enumerable " + outConvention);
+ }
+
+ @Override
+ public boolean isGuaranteed() {
+ return true;
+ }
+
+ @Override
+ public RelNode convert(RelNode rel) {
+ if (!rel.getTraitSet().contains(DrillOptiq.CONVENTION)) {
+ return null;
+ }
+
+ return new EnumerableDrillRel(rel.getCluster(),
+ rel.getTraitSet().replace(getOutConvention()),
+ rel);
+ }
+}
+
+// End EnumerableDrillRule.java
http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/7f294adb/sandbox/prototype/sqlparser/src/test/java/org/apache/drill/jdbc/test/JdbcTest.java
----------------------------------------------------------------------
diff --git a/sandbox/prototype/sqlparser/src/test/java/org/apache/drill/jdbc/test/JdbcTest.java b/sandbox/prototype/sqlparser/src/test/java/org/apache/drill/jdbc/test/JdbcTest.java
new file mode 100644
index 0000000..a6c3bf0
--- /dev/null
+++ b/sandbox/prototype/sqlparser/src/test/java/org/apache/drill/jdbc/test/JdbcTest.java
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package org.apache.drill.jdbc.test;
+
+import junit.framework.TestCase;
+
+import java.sql.*;
+
+/** Unit tests for Drill's JDBC driver. */
+public class JdbcTest extends TestCase {
+ /** Load driver. */
+ public void testLoadDriver() throws ClassNotFoundException {
+ Class.forName("org.apache.drill.jdbc.Driver");
+ }
+
+ /** Load driver and make a connection. */
+ public void testConnect() throws Exception {
+ Class.forName("org.apache.drill.jdbc.Driver");
+ final Connection connection = DriverManager.getConnection(
+ "jdbc:drill:schema=DONUTS;tables=EMP,DEPT");
+ connection.close();
+ }
+
+ /** Load driver, make a connection, prepare a statement. */
+ public void _testPrepare() throws Exception {
+ Class.forName("org.apache.drill.jdbc.Driver");
+ final Connection connection = DriverManager.getConnection(
+ "jdbc:drill:schema=DONUTS;tables=EMP,DEPT");
+ final Statement statement = connection.prepareStatement(
+ "select * from emp where cast(name as varchar(10)) = 'Eric'");
+ statement.close();
+ connection.close();
+ }
+
+ /** Simple query against JSON. */
+ public void testSelectJson() throws Exception {
+ Class.forName("org.apache.drill.jdbc.Driver");
+ final Connection connection = DriverManager.getConnection(
+ "jdbc:drill:schema=DONUTS;tables=DONUTS");
+ final Statement statement = connection.createStatement();
+ final ResultSet resultSet = statement.executeQuery(
+ "select * from donuts");
+ assertEquals(
+ "_extra={\"donuts\":{\"type\":\"donut\",\"ppu\":0.55,\"topping\":[{\"type\":\"None\",\"id\":\"5001\"},{\"type\":\"Glazed\",\"id\":\"5002\"},{\"type\":\"Sugar\",\"id\":\"5005\"},{\"type\":\"Powdered Sugar\",\"id\":\"5007\"},{\"type\":\"Chocolate with Sprinkles\",\"id\":\"5006\"},{\"type\":\"Chocolate\",\"id\":\"5003\"},{\"type\":\"Maple\",\"id\":\"5004\"}],\"name\":\"Cake\",\"sales\":35,\"batters\":{\"batter\":[{\"type\":\"Regular\",\"id\":\"1001\"},{\"type\":\"Chocolate\",\"id\":\"1002\"},{\"type\":\"Blueberry\",\"id\":\"1003\"},{\"type\":\"Devil's Food\",\"id\":\"1004\"}]},\"id\":\"0001\"}}\n"
+ + "_extra={\"donuts\":{\"type\":\"donut\",\"ppu\":0.69,\"topping\":[{\"type\":\"None\",\"id\":\"5001\"},{\"type\":\"Glazed\",\"id\":\"5002\"},{\"type\":\"Sugar\",\"id\":\"5005\"},{\"type\":\"Chocolate\",\"id\":\"5003\"},{\"type\":\"Maple\",\"id\":\"5004\"}],\"name\":\"Raised\",\"sales\":145,\"batters\":{\"batter\":[{\"type\":\"Regular\",\"id\":\"1001\"}]},\"id\":\"0002\"}}\n"
+ + "_extra={\"donuts\":{\"type\":\"donut\",\"ppu\":0.55,\"topping\":[{\"type\":\"None\",\"id\":\"5001\"},{\"type\":\"Glazed\",\"id\":\"5002\"},{\"type\":\"Chocolate\",\"id\":\"5003\"},{\"type\":\"Maple\",\"id\":\"5004\"}],\"name\":\"Old Fashioned\",\"sales\":300,\"batters\":{\"batter\":[{\"type\":\"Regular\",\"id\":\"1001\"},{\"type\":\"Chocolate\",\"id\":\"1002\"}]},\"id\":\"0003\"}}\n"
+ + "_extra={\"donuts\":{\"type\":\"donut\",\"ppu\":0.69,\"topping\":[{\"type\":\"None\",\"id\":\"5001\"},{\"type\":\"Glazed\",\"id\":\"5002\"},{\"type\":\"Sugar\",\"id\":\"5005\"},{\"type\":\"Powdered Sugar\",\"id\":\"5007\"},{\"type\":\"Chocolate with Sprinkles\",\"id\":\"5006\"},{\"type\":\"Chocolate\",\"id\":\"5003\"},{\"type\":\"Maple\",\"id\":\"5004\"}],\"name\":\"Filled\",\"sales\":14,\"batters\":{\"batter\":[{\"type\":\"Regular\",\"id\":\"1001\"},{\"type\":\"Chocolate\",\"id\":\"1002\"},{\"type\":\"Blueberry\",\"id\":\"1003\"},{\"type\":\"Devil's Food\",\"id\":\"1004\"}]},\"id\":\"0004\",\"filling\":[{\"type\":\"None\",\"id\":\"6001\"},{\"type\":\"Raspberry\",\"id\":\"6002\"},{\"type\":\"Lemon\",\"id\":\"6003\"},{\"type\":\"Chocolate\",\"id\":\"6004\"},{\"type\":\"Kreme\",\"id\":\"6005\"}]}}\n"
+ + "_extra={\"donuts\":{\"type\":\"donut\",\"ppu\":1.0,\"topping\":[{\"type\":\"Glazed\",\"id\":\"5002\"}],\"name\":\"Apple Fritter\",\"sales\":700,\"batters\":{\"batter\":[{\"type\":\"Regular\",\"id\":\"1001\"}]},\"id\":\"0005\"}}\n",
+ toString(resultSet));
+ resultSet.close();
+ statement.close();
+ connection.close();
+ }
+
+ static String toString(ResultSet resultSet) throws SQLException {
+ StringBuilder buf = new StringBuilder();
+ while (resultSet.next()) {
+ int n = resultSet.getMetaData().getColumnCount();
+ String sep = "";
+ for (int i = 1; i <= n; i++) {
+ buf.append(sep)
+ .append(resultSet.getMetaData().getColumnLabel(i))
+ .append("=")
+ .append(resultSet.getObject(i));
+ sep = "; ";
+ }
+ buf.append("\n");
+ }
+ return buf.toString();
+ }
+}
+
+// End JdbcTest.java