Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

Commit

Permalink
DRILL-166 Update CodeGenerator code to support more generic signatures.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacques-n committed Aug 22, 2013
1 parent 0fc89a3 commit ac8590d
Show file tree
Hide file tree
Showing 51 changed files with 1,302 additions and 408 deletions.
Expand Up @@ -18,6 +18,7 @@
package org.apache.drill.common.expression;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.drill.common.expression.IfExpression.IfCondition;
Expand All @@ -28,9 +29,12 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.UnmodifiableIterator;

public class IfExpression extends LogicalExpressionBase implements Iterable<IfCondition>{
public class IfExpression extends LogicalExpressionBase{
static final Logger logger = LoggerFactory.getLogger(IfExpression.class);

public final ImmutableList<IfCondition> conditions;
Expand Down Expand Up @@ -106,9 +110,22 @@ public static Builder newBuilder(){
}


@Override
public UnmodifiableIterator<IfCondition> iterator() {
return conditions.iterator();
}

public Iterable<IfCondition> conditionIterable(){

return ImmutableList.copyOf(conditions);
}

@Override
public Iterator<LogicalExpression> iterator() {
List<LogicalExpression> children = Lists.newLinkedList();

for(IfCondition ic : conditions){
children.add(ic.condition);
children.add(ic.expression);
}
children.add(this.elseExpression);
return children.iterator();
}


}
Expand Up @@ -43,7 +43,7 @@

//@JsonDeserialize(using = LogicalExpression.De.class) // Excluded as we need to register this with the DrillConfig.
@JsonSerialize(using = LogicalExpression.Se.class)
public interface LogicalExpression {
public interface LogicalExpression extends Iterable<LogicalExpression>{
static final Logger logger = LoggerFactory.getLogger(LogicalExpression.class);

public abstract MajorType getMajorType();
Expand Down
Expand Up @@ -17,6 +17,7 @@
******************************************************************************/
package org.apache.drill.common.expression;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -26,6 +27,7 @@
import org.apache.drill.common.types.TypeProtos.MajorType;
import org.apache.drill.common.types.Types;

import com.google.common.collect.Iterators;
import com.google.protobuf.DescriptorProtos.UninterpretedOption.NamePart;

public class SchemaPath extends LogicalExpressionBase {
Expand Down Expand Up @@ -131,6 +133,11 @@ public boolean equals(Object obj) {
return true;
}

@Override
public Iterator<LogicalExpression> iterator() {
return Iterators.emptyIterator();
}

@Override
public String toString() {
return "SchemaPath [rootSegment=" + rootSegment + "]";
Expand Down
Expand Up @@ -17,12 +17,16 @@
******************************************************************************/
package org.apache.drill.common.expression;

import java.util.Iterator;

import org.apache.drill.common.expression.visitors.ExprVisitor;
import org.apache.drill.common.types.TypeProtos.DataMode;
import org.apache.drill.common.types.TypeProtos.MajorType;
import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.common.types.Types;

import com.google.common.collect.Iterators;

public class ValueExpressions {

public static LogicalExpression getNumericExpression(String s, ExpressionPosition ep) {
Expand Down Expand Up @@ -54,6 +58,12 @@ protected ValueExpression(String value, ExpressionPosition pos) {
}

protected abstract V parseValue(String s);

@Override
public Iterator<LogicalExpression> iterator() {
return Iterators.emptyIterator();
}


}

Expand Down Expand Up @@ -109,6 +119,11 @@ public <T, V, E extends Exception> T accept(ExprVisitor<T, V, E> visitor, V valu
return visitor.visitDoubleConstant(this, value);
}

@Override
public Iterator<LogicalExpression> iterator() {
return Iterators.emptyIterator();
}

}

public static class LongExpression extends LogicalExpressionBase {
Expand All @@ -135,6 +150,12 @@ public MajorType getMajorType() {
public <T, V, E extends Exception> T accept(ExprVisitor<T, V, E> visitor, V value) throws E {
return visitor.visitLongConstant(this, value);
}

@Override
public Iterator<LogicalExpression> iterator() {
return Iterators.emptyIterator();
}

}

public static class QuotedString extends ValueExpression<String> {
Expand Down
Expand Up @@ -54,7 +54,7 @@ public Boolean visitFunctionCall(FunctionCall call) {

@Override
public Boolean visitIfExpression(IfExpression ifExpr) {
for(IfCondition c : ifExpr){
for(IfCondition c : ifExpr.conditions){
if(c.condition.accept(this, null) || c.expression.accept(this, null)) return true;
}
return ifExpr.elseExpression.accept(this, null);
Expand Down
Expand Up @@ -47,9 +47,10 @@ public Boolean visitFunctionCall(FunctionCall call) {

@Override
public Boolean visitIfExpression(IfExpression ifExpr) {
for(IfCondition c : ifExpr){
for(IfCondition c : ifExpr.conditions){
if(!c.condition.accept(this, null) || !c.expression.accept(this, null)) return false;
}
if(!ifExpr.elseExpression.accept(this, null)) return false;
return true;
}

Expand Down
10 changes: 10 additions & 0 deletions sandbox/prototype/exec/java-exec/pom.xml
Expand Up @@ -22,6 +22,11 @@
<artifactId>asm-commons</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.paranamer</groupId>
<artifactId>paranamer</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>com.sun.codemodel</groupId>
<artifactId>codemodel</artifactId>
Expand Down Expand Up @@ -58,6 +63,11 @@
<artifactId>metrics-core</artifactId>
<version>3.0.0-BETA1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.apache.drill</groupId>
<artifactId>common</artifactId>
Expand Down
Expand Up @@ -17,21 +17,36 @@
******************************************************************************/
package org.apache.drill.exec.compile;


import org.apache.drill.exec.compile.sig.CodeGeneratorSignature;
import org.apache.drill.exec.compile.sig.DefaultGeneratorSignature;
import org.apache.drill.exec.compile.sig.SignatureHolder;

public class TemplateClassDefinition<T>{

static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TemplateClassDefinition.class);

private final Class<T> externalInterface;
private final String templateClassName;
private final Class<?> internalInterface;
private final Class<?> evalReturnType;

public TemplateClassDefinition(Class<T> externalInterface, String templateClassName, Class<?> internalInterface, Class<?> evalReturnType) {
private final SignatureHolder signature;

public TemplateClassDefinition(Class<T> externalInterface, String templateClassName, Class<?> internalInterface) {
this(externalInterface, templateClassName, internalInterface, DefaultGeneratorSignature.class);
}

public <X extends CodeGeneratorSignature> TemplateClassDefinition(Class<T> externalInterface, String templateClassName, Class<?> internalInterface, Class<X> signature) {
super();
this.externalInterface = externalInterface;
this.templateClassName = templateClassName;
this.internalInterface = internalInterface;
this.evalReturnType = evalReturnType;
SignatureHolder holder = null;
try{
holder = new SignatureHolder(signature);
}catch(Exception ex){
logger.error("Failure while trying to build signature holder for signature. {}", signature.getSimpleName(), ex);
}
this.signature = holder;

}

public Class<T> getExternalInterface() {
Expand All @@ -46,8 +61,15 @@ public String getTemplateClassName() {
return templateClassName;
}

public Class<?> getEvalReturnType() {
return evalReturnType;
public SignatureHolder getSignature(){
return signature;
}

@Override
public String toString() {
return "TemplateClassDefinition [externalInterface=" + externalInterface + ", templateClassName="
+ templateClassName + ", internalInterface=" + internalInterface + ", signature=" + signature + "]";
}


}
@@ -0,0 +1,23 @@
package org.apache.drill.exec.compile.sig;


public class CodeGeneratorArgument {

private final String name;
private final Class<?> type;

public CodeGeneratorArgument(String name, Class<?> type) {
super();
this.name = name;
this.type = type;
}

public String getName() {
return name;
}

public Class<?> getType() {
return type;
}

}
@@ -0,0 +1,60 @@
package org.apache.drill.exec.compile.sig;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.thoughtworks.paranamer.AnnotationParanamer;
import com.thoughtworks.paranamer.Paranamer;

public class CodeGeneratorMethod implements Iterable<CodeGeneratorArgument>{
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CodeGeneratorMethod.class);

private final String methodName;
private final Class<?> returnType;
private final CodeGeneratorArgument[] arguments;
private final Class<?>[] exs;
private final Method underlyingMethod;

public CodeGeneratorMethod(Method m){
this.underlyingMethod = m;
this.methodName = m.getName();
this.returnType = m.getReturnType();
// Paranamer para = new BytecodeReadingParanamer();
Paranamer para = new AnnotationParanamer();
String[] parameterNames = para.lookupParameterNames(m, true);
if(parameterNames == null) throw new RuntimeException(String.format("Unable to read the parameter names for method %s. This is likely due to the class files not including the appropriate debugging information. Look up java -g for more information.", m));
Class<?>[] types = m.getParameterTypes();
if(parameterNames.length != types.length) throw new RuntimeException(String.format("Unexpected number of parameter names %s. Expected %s on method %s.", Arrays.toString(parameterNames), Arrays.toString(types), m.toGenericString()));
arguments = new CodeGeneratorArgument[parameterNames.length];
for(int i =0 ; i < parameterNames.length; i++){
arguments[i] = new CodeGeneratorArgument(parameterNames[i], types[i]);
}
exs = m.getExceptionTypes();
}

public String getMethodName() {
return methodName;
}
public Class<?> getReturnType() {
return returnType;
}

public Iterable<Class<?>> getThrowsIterable(){
return ImmutableList.copyOf(exs);
}

@Override
public Iterator<CodeGeneratorArgument> iterator() {
return Iterators.forArray(arguments);
}

@Override
public String toString() {
return "CodeGeneratorMethod [" + underlyingMethod.toGenericString() + "]";
}


}
@@ -0,0 +1,5 @@
package org.apache.drill.exec.compile.sig;


public interface CodeGeneratorSignature {
}

0 comments on commit ac8590d

Please sign in to comment.