Skip to content

Commit

Permalink
Update calls to Expressions.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
jduo committed Sep 10, 2024
1 parent 7938119 commit 7cc5816
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static Bindable toBindable(Map<String, Object> parameters,
parameters);

final ClassDeclaration expr = relImplementor.implementRoot(rel, prefer);
String s = Expressions.toString(expr.memberDeclarations, "\n", false);
String s = Expressions.toString(expr.memberDeclarations, "\n", false, true);

if (CalciteSystemProperty.DEBUG.value()) {
Util.debugCode(System.out, s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,9 @@ public final class CalciteSystemProperty<T> {
public static final CalciteSystemProperty<Integer> FUNCTION_LEVEL_CACHE_MAX_SIZE =
intProperty("calcite.function.cache.maxSize", 0, v -> v >= 0);

public static final CalciteSystemProperty<Boolean> ENABLE_CODE_SPLITTING =
booleanProperty("calcite.linq.enable_code_splitting", false);

private static CalciteSystemProperty<Boolean> booleanProperty(String key,
boolean defaultValue) {
// Note that "" -> true (convenient for command-lines flags like '-Dflag')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static Scalar.Producer baz(ParameterExpression context_,
final ClassDeclaration classDeclaration =
Expressions.classDecl(Modifier.PUBLIC, "Buzz", null,
ImmutableList.of(Scalar.Producer.class), declarations);
String s = Expressions.toString(declarations, "\n", false);
String s = Expressions.toString(declarations, "\n", false, true);
if (CalciteSystemProperty.DEBUG.value()) {
Util.debugCode(System.out, s);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private static String compile(RexBuilder rexBuilder, List<RexNode> constExps,
Expressions.methodDecl(Modifier.PUBLIC, Object[].class,
BuiltInMethod.FUNCTION1_APPLY.method.getName(),
ImmutableList.of(root0_), blockBuilder.toBlock());
String code = Expressions.toString(methodDecl);
String code = Expressions.toString(methodDecl, true);
if (CalciteSystemProperty.DEBUG.value()) {
Util.debugCode(System.out, code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public final class EnumUtilsTest {
EnumUtils.convert(date, int.class);
final Expression dateToInteger =
EnumUtils.convert(date, Integer.class);
assertThat(Expressions.toString(dateToInt),
assertThat(Expressions.toString(dateToInt, true),
is("org.apache.calcite.runtime.SqlFunctions.toInt(x)"));
assertThat(Expressions.toString(dateToInteger),
assertThat(Expressions.toString(dateToInteger, true),
is("org.apache.calcite.runtime.SqlFunctions.toIntOptional(x)"));

// java.sql.Time x;
Expand All @@ -59,9 +59,9 @@ public final class EnumUtilsTest {
EnumUtils.convert(time, int.class);
final Expression timeToInteger =
EnumUtils.convert(time, Integer.class);
assertThat(Expressions.toString(timeToInt),
assertThat(Expressions.toString(timeToInt, true),
is("org.apache.calcite.runtime.SqlFunctions.toInt(x)"));
assertThat(Expressions.toString(timeToInteger),
assertThat(Expressions.toString(timeToInteger, true),
is("org.apache.calcite.runtime.SqlFunctions.toIntOptional(x)"));

// java.sql.TimeStamp x;
Expand All @@ -71,9 +71,9 @@ public final class EnumUtilsTest {
EnumUtils.convert(timestamp, long.class);
final Expression timeStampToLong =
EnumUtils.convert(timestamp, Long.class);
assertThat(Expressions.toString(timeStampToLongPrimitive),
assertThat(Expressions.toString(timeStampToLongPrimitive, true),
is("org.apache.calcite.runtime.SqlFunctions.toLong(x)"));
assertThat(Expressions.toString(timeStampToLong),
assertThat(Expressions.toString(timeStampToLong, true),
is("org.apache.calcite.runtime.SqlFunctions.toLongOptional(x)"));
}

Expand All @@ -86,77 +86,77 @@ public final class EnumUtilsTest {
Expressions.convert_(intVariable, byte.class);
final Expression converted0 =
EnumUtils.convert(bytePrimitiveConverted, Byte.class);
assertThat(Expressions.toString(converted0),
assertThat(Expressions.toString(converted0, true),
is("Byte.valueOf((byte) intV)"));

// (char)(int) -> Character: Character.valueOf((char) intV)
final Expression characterPrimitiveConverted =
Expressions.convert_(intVariable, char.class);
final Expression converted1 =
EnumUtils.convert(characterPrimitiveConverted, Character.class);
assertThat(Expressions.toString(converted1),
assertThat(Expressions.toString(converted1, true),
is("Character.valueOf((char) intV)"));

// (short)(int) -> Short: Short.valueOf((short) intV)
final Expression shortPrimitiveConverted =
Expressions.convert_(intVariable, short.class);
final Expression converted2 =
EnumUtils.convert(shortPrimitiveConverted, Short.class);
assertThat(Expressions.toString(converted2),
assertThat(Expressions.toString(converted2, true),
is("Short.valueOf((short) intV)"));

// (long)(int) -> Long: Long.valueOf(intV)
final Expression longPrimitiveConverted =
Expressions.convert_(intVariable, long.class);
final Expression converted3 =
EnumUtils.convert(longPrimitiveConverted, Long.class);
assertThat(Expressions.toString(converted3),
assertThat(Expressions.toString(converted3, true),
is("Long.valueOf(intV)"));

// (float)(int) -> Float: Float.valueOf(intV)
final Expression floatPrimitiveConverted =
Expressions.convert_(intVariable, float.class);
final Expression converted4 =
EnumUtils.convert(floatPrimitiveConverted, Float.class);
assertThat(Expressions.toString(converted4),
assertThat(Expressions.toString(converted4, true),
is("Float.valueOf(intV)"));

// (double)(int) -> Double: Double.valueOf(intV)
final Expression doublePrimitiveConverted =
Expressions.convert_(intVariable, double.class);
final Expression converted5 =
EnumUtils.convert(doublePrimitiveConverted, Double.class);
assertThat(Expressions.toString(converted5),
assertThat(Expressions.toString(converted5, true),
is("Double.valueOf(intV)"));

final Expression byteConverted =
EnumUtils.convert(intVariable, Byte.class);
assertThat(Expressions.toString(byteConverted),
assertThat(Expressions.toString(byteConverted, true),
is("Byte.valueOf((byte) intV)"));

final Expression shortConverted =
EnumUtils.convert(intVariable, Short.class);
assertThat(Expressions.toString(shortConverted),
assertThat(Expressions.toString(shortConverted, true),
is("Short.valueOf((short) intV)"));

final Expression integerConverted =
EnumUtils.convert(intVariable, Integer.class);
assertThat(Expressions.toString(integerConverted),
assertThat(Expressions.toString(integerConverted, true),
is("Integer.valueOf(intV)"));

final Expression longConverted =
EnumUtils.convert(intVariable, Long.class);
assertThat(Expressions.toString(longConverted),
assertThat(Expressions.toString(longConverted, true),
is("Long.valueOf((long) intV)"));

final Expression floatConverted =
EnumUtils.convert(intVariable, Float.class);
assertThat(Expressions.toString(floatConverted),
assertThat(Expressions.toString(floatConverted, true),
is("Float.valueOf((float) intV)"));

final Expression doubleConverted =
EnumUtils.convert(intVariable, Double.class);
assertThat(Expressions.toString(doubleConverted),
assertThat(Expressions.toString(doubleConverted, true),
is("Double.valueOf((double) intV)"));
}

Expand All @@ -167,8 +167,8 @@ public final class EnumUtilsTest {
final ConstantExpression nullLiteral2 = Expressions.constant(null, Object.class);
final Expression e1 = EnumUtils.convert(nullLiteral1, String.class);
final Expression e2 = EnumUtils.convert(nullLiteral2, String.class);
assertThat(Expressions.toString(e1), is("(String) null"));
assertThat(Expressions.toString(e2), is("(String) (Object) null"));
assertThat(Expressions.toString(e1, true), is("(String) null"));
assertThat(Expressions.toString(e2, true), is("(String) (Object) null"));
}

@Test void testMethodCallExpression() {
Expand All @@ -178,7 +178,7 @@ public final class EnumUtilsTest {
final MethodCallExpression arrayMethodCall =
EnumUtils.call(null, SqlFunctions.class,
BuiltInMethod.ARRAY.getMethodName(), Arrays.asList(arg0, arg1));
assertThat(Expressions.toString(arrayMethodCall),
assertThat(Expressions.toString(arrayMethodCall, true),
is("org.apache.calcite.runtime.SqlFunctions.array(1, \"x\")"));

// test for Object.class argument type
Expand All @@ -187,7 +187,7 @@ public final class EnumUtilsTest {
EnumUtils.call(null, XmlFunctions.class,
BuiltInMethod.EXTRACT_VALUE.getMethodName(),
Arrays.asList(arg1, nullLiteral));
assertThat(Expressions.toString(xmlExtractMethodCall),
assertThat(Expressions.toString(xmlExtractMethodCall, true),
is("org.apache.calcite.runtime.XmlFunctions.extractValue(\"x\", (String) null)"));

// test "mod(decimal, long)" match to "mod(decimal, decimal)"
Expand All @@ -196,7 +196,7 @@ public final class EnumUtilsTest {
final MethodCallExpression modMethodCall =
EnumUtils.call(null, SqlFunctions.class, "mod",
Arrays.asList(arg2, arg3));
assertThat(Expressions.toString(modMethodCall),
assertThat(Expressions.toString(modMethodCall, true),
is("org.apache.calcite.runtime.SqlFunctions.mod("
+ "java.math.BigDecimal.valueOf(125L, 1), "
+ "new java.math.BigDecimal(\n 3L))"));
Expand All @@ -207,7 +207,7 @@ public final class EnumUtilsTest {
final MethodCallExpression geoMethodCall =
EnumUtils.call(null, SpatialTypeFunctions.class, "ST_MakePoint",
Arrays.asList(arg4, arg5));
assertThat(Expressions.toString(geoMethodCall),
assertThat(Expressions.toString(geoMethodCall, true),
is("org.apache.calcite.runtime.SpatialTypeFunctions.ST_MakePoint("
+ "new java.math.BigDecimal(\n 1), "
+ "new java.math.BigDecimal(\n 2))"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ public final class PhysTypeTest {
+ " }\n"
+ "}\n"
+ ")";
assertEquals(Expressions.toString(e), expected);
assertEquals(Expressions.toString(e, true), expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void assertJavaCodeContains(String expected, Node node) {
}

private void assertJavaCodeContains(String expected, List<Node> nodes) {
final String javaCode = Expressions.toString(nodes, "\n", false);
final String javaCode = Expressions.toString(nodes, "\n", false, true);
assertThat(javaCode, containsString(expected));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static List<String> innodbFieldNames(final RelDataType rowType) {
InnodbMethod.INNODB_QUERYABLE_QUERY.method, fields,
selectFields, cond, ascOrder));
if (CalciteSystemProperty.DEBUG.value()) {
System.out.println("Innodb: " + Expressions.toString(enumerable));
System.out.println("Innodb: " + Expressions.toString(enumerable, true));
}
list.add(Expressions.return_(null, enumerable));
return implementor.result(physType, list.toBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Type getType() {
}

@Override public String toString() {
ExpressionWriter writer = new ExpressionWriter(true);
ExpressionWriter writer = new ExpressionWriter(true, true);
accept(writer, 0, 0);
return writer.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ class ExpressionWriter {
private boolean indentPending;

private final boolean generics;
private final boolean methodSplitting;

ExpressionWriter() {
this(true);
this(true, true);
}

ExpressionWriter(boolean generics) {
ExpressionWriter(boolean generics, boolean methodSplitting) {
this.generics = generics;
this.methodSplitting = methodSplitting;
}

public ExpressionWriter duplicateState() {
final ExpressionWriter writer = new ExpressionWriter(this.generics);
final ExpressionWriter writer = new ExpressionWriter(this.generics, this.methodSplitting);
writer.indentPending = this.indentPending;
writer.spacer.add(this.spacer.get());
return writer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ private Expressions() {}
* extra type information in generics.
*/
public static String toString(List<? extends Node> expressions, String sep,
boolean generics) {
final ExpressionWriter writer = new ExpressionWriter(generics);
boolean generics, boolean methodSplit) {
final ExpressionWriter writer = new ExpressionWriter(generics, methodSplit);
for (Node expression : expressions) {
writer.write(expression);
writer.append(sep);
Expand All @@ -67,8 +67,8 @@ public static String toString(List<? extends Node> expressions, String sep,
/**
* Converts an expression to Java source code.
*/
public static String toString(Node expression) {
return toString(Collections.singletonList(expression), "", true);
public static String toString(Node expression, boolean methodSplit) {
return toString(Collections.singletonList(expression), "", true, methodSplit);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private BlockBuilder appendBlockWithSameVariable(
+ " x = 1;\n"
+ " int x0;\n"
+ " x0 = 42;\n"
+ "}\n", Expressions.toString(outer.toBlock()),
+ "}\n", Expressions.toString(outer.toBlock(), false),
"x in the second block should be renamed to avoid name clash");
}

Expand All @@ -121,7 +121,7 @@ private BlockBuilder appendBlockWithSameVariable(
+ " x = 1;\n"
+ " int x0 = 8;\n"
+ " x0 = 42;\n"
+ "}\n", Expressions.toString(outer.toBlock()),
+ "}\n", Expressions.toString(outer.toBlock(), false),
"x in the second block should be renamed to avoid name clash");
}

Expand All @@ -142,7 +142,7 @@ private BlockBuilder appendBlockWithSameVariable(
+ " final Object _i = new org.apache.calcite.linq4j.test.BlockBuilderTest.Identity()"
+ ".apply(\"test\");\n"
+ "}\n",
Expressions.toString(bb.toBlock()));
Expressions.toString(bb.toBlock(), false));

}

Expand All @@ -159,7 +159,7 @@ private BlockBuilder appendBlockWithSameVariable(

assertEquals("{\n"
+ " return false;\n"
+ "}\n", Expressions.toString(outer.toBlock()),
+ "}\n", Expressions.toString(outer.toBlock(), false),
"Expected to optimize Boolean.FALSE = null to false");
}

Expand Down
Loading

0 comments on commit 7cc5816

Please sign in to comment.