Fix NullPointerException in SqlJoin.toString
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/cd92b77c Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/cd92b77c Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/cd92b77c Branch: refs/heads/master Commit: cd92b77c476f7b095b330507402bfc6c3208e300 Parents: 239babd Author: Julian Hyde <[email protected]> Authored: Sat Jan 9 12:18:29 2016 -0800 Committer: Julian Hyde <[email protected]> Committed: Sun Jan 10 00:51:25 2016 -0800 ---------------------------------------------------------------------- .../java/org/apache/calcite/sql/SqlJoin.java | 14 +++--- .../java/org/apache/calcite/sql/SqlWriter.java | 5 ++ .../calcite/sql/pretty/SqlPrettyWriter.java | 50 ++++++++------------ 3 files changed, 34 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/cd92b77c/core/src/main/java/org/apache/calcite/sql/SqlJoin.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/SqlJoin.java b/core/src/main/java/org/apache/calcite/sql/SqlJoin.java index fc909c4..12b1fb0 100644 --- a/core/src/main/java/org/apache/calcite/sql/SqlJoin.java +++ b/core/src/main/java/org/apache/calcite/sql/SqlJoin.java @@ -21,6 +21,8 @@ import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.util.ImmutableNullableList; import org.apache.calcite.util.Util; +import com.google.common.base.Preconditions; + import java.util.List; /** @@ -58,18 +60,15 @@ public class SqlJoin extends SqlCall { SqlNode condition) { super(pos); this.left = left; - this.natural = natural; - this.joinType = joinType; + this.natural = Preconditions.checkNotNull(natural); + this.joinType = Preconditions.checkNotNull(joinType); this.right = right; - this.conditionType = conditionType; + this.conditionType = Preconditions.checkNotNull(conditionType); this.condition = condition; assert natural.getTypeName() == SqlTypeName.BOOLEAN; - assert conditionType != null; assert conditionType.symbolValue() instanceof JoinConditionType; - assert joinType != null; assert joinType.symbolValue() instanceof JoinType; - } //~ Methods ---------------------------------------------------------------- @@ -196,6 +195,8 @@ public class SqlJoin extends SqlCall { int rightPrec) { final SqlJoin join = (SqlJoin) call; + final SqlWriter.Frame joinFrame = + writer.startList(SqlWriter.FrameTypeEnum.JOIN); join.left.unparse( writer, leftPrec, @@ -249,6 +250,7 @@ public class SqlJoin extends SqlCall { throw Util.unexpected(join.getConditionType()); } } + writer.endList(joinFrame); } } } http://git-wip-us.apache.org/repos/asf/calcite/blob/cd92b77c/core/src/main/java/org/apache/calcite/sql/SqlWriter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/SqlWriter.java b/core/src/main/java/org/apache/calcite/sql/SqlWriter.java index 8e72026..d45c831 100644 --- a/core/src/main/java/org/apache/calcite/sql/SqlWriter.java +++ b/core/src/main/java/org/apache/calcite/sql/SqlWriter.java @@ -194,6 +194,11 @@ public interface SqlWriter { FROM_LIST, /** + * Pair-wise join. + */ + JOIN(false), + + /** * WHERE clause. */ WHERE_LIST, http://git-wip-us.apache.org/repos/asf/calcite/blob/cd92b77c/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java b/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java index 2be197e..0d78656 100644 --- a/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java +++ b/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java @@ -25,6 +25,8 @@ import org.apache.calcite.sql.util.SqlString; import org.apache.calcite.util.Util; import org.apache.calcite.util.trace.CalciteLogger; +import com.google.common.base.Throwables; + import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.InvocationTargetException; @@ -34,6 +36,7 @@ import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.logging.Logger; @@ -306,11 +309,10 @@ public class SqlPrettyWriter implements SqlWriter { final Bean properties = getBean(); final String[] propertyNames = properties.getPropertyNames(); int count = 0; - for (int i = 0; i < propertyNames.length; i++) { - String key = propertyNames[i]; + for (String key : propertyNames) { final Object value = bean.get(key); final Object defaultValue = DEFAULT_BEAN.get(key); - if (com.google.common.base.Objects.equal(value, defaultValue)) { + if (Objects.equals(value, defaultValue)) { continue; } if (count++ > 0) { @@ -327,8 +329,7 @@ public class SqlPrettyWriter implements SqlWriter { resetSettings(); final Bean bean = getBean(); final String[] propertyNames = bean.getPropertyNames(); - for (int i = 0; i < propertyNames.length; i++) { - String propertyName = propertyNames[i]; + for (String propertyName : propertyNames) { final String value = properties.getProperty(propertyName); if (value != null) { bean.set(propertyName, value); @@ -662,6 +663,7 @@ public class SqlPrettyWriter implements SqlWriter { false); case FROM_LIST: + case JOIN: return new FrameImpl( frameType, keyword, @@ -849,7 +851,7 @@ public class SqlPrettyWriter implements SqlWriter { protected void whiteSpace() { if (needWhitespace) { - if (nextWhitespace == NL) { + if (nextWhitespace.equals(NL)) { newlineAndIndent(); } else { pw.print(nextWhitespace); @@ -1044,15 +1046,15 @@ public class SqlPrettyWriter implements SqlWriter { /** * Whether to print a newline before each separator. */ - public boolean newlineBeforeSep; + public final boolean newlineBeforeSep; /** * Whether to print a newline after each separator. */ - public boolean newlineAfterSep; + public final boolean newlineAfterSep; private final boolean newlineBeforeClose; private final boolean newlineAfterClose; - private boolean newlineAfterOpen; + private final boolean newlineAfterOpen; FrameImpl( FrameType frameType, @@ -1107,18 +1109,14 @@ public class SqlPrettyWriter implements SqlWriter { */ private static class Bean { private final SqlPrettyWriter o; - private final Map<String, Method> getterMethods = - new HashMap<String, Method>(); - private final Map<String, Method> setterMethods = - new HashMap<String, Method>(); + private final Map<String, Method> getterMethods = new HashMap<>(); + private final Map<String, Method> setterMethods = new HashMap<>(); Bean(SqlPrettyWriter o) { this.o = o; // Figure out the getter/setter methods for each attribute. - final Method[] methods = o.getClass().getMethods(); - for (int i = 0; i < methods.length; i++) { - Method method = methods[i]; + for (Method method : o.getClass().getMethods()) { if (method.getName().startsWith("set") && (method.getReturnType() == Void.class) && (method.getParameterTypes().length == 1)) { @@ -1157,13 +1155,9 @@ public class SqlPrettyWriter implements SqlWriter { public void set(String name, String value) { final Method method = setterMethods.get(name); try { - method.invoke( - o, - value); - } catch (IllegalAccessException e) { - throw Util.newInternal(e); - } catch (InvocationTargetException e) { - throw Util.newInternal(e); + method.invoke(o, value); + } catch (IllegalAccessException | InvocationTargetException e) { + throw Throwables.propagate(e); } } @@ -1171,18 +1165,16 @@ public class SqlPrettyWriter implements SqlWriter { final Method method = getterMethods.get(name); try { return method.invoke(o); - } catch (IllegalAccessException e) { - throw Util.newInternal(e); - } catch (InvocationTargetException e) { - throw Util.newInternal(e); + } catch (IllegalAccessException | InvocationTargetException e) { + throw Throwables.propagate(e); } } public String[] getPropertyNames() { - final Set<String> names = new HashSet<String>(); + final Set<String> names = new HashSet<>(); names.addAll(getterMethods.keySet()); names.addAll(setterMethods.keySet()); - return (String[]) names.toArray(new String[names.size()]); + return names.toArray(new String[names.size()]); } } }
