You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by da...@apache.org on 2019/09/23 08:45:44 UTC
[calcite] branch master updated: [CALCITE-3288] In
ConstantExpression support SET literals (xy2953396112)
This is an automated email from the ASF dual-hosted git repository.
danny0405 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/master by this push:
new 78c2a00 [CALCITE-3288] In ConstantExpression support SET literals (xy2953396112)
78c2a00 is described below
commit 78c2a007b5b597f673323b80dde21c23d31f23ea
Author: dz <95...@qq.com>
AuthorDate: Mon Sep 23 13:50:58 2019 +0800
[CALCITE-3288] In ConstantExpression support SET literals (xy2953396112)
close apache/calcite#1408
---
.../calcite/linq4j/tree/ConstantExpression.java | 30 ++++++++
.../apache/calcite/linq4j/test/ExpressionTest.java | 82 ++++++++++++++++++++--
2 files changed, 108 insertions(+), 4 deletions(-)
diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java
index 7680e0b..18b2263 100644
--- a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java
+++ b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/ConstantExpression.java
@@ -25,6 +25,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -185,6 +186,10 @@ public class ConstantExpression extends Expression {
if (value instanceof Map) {
return writeMap(writer, (Map) value);
}
+ if (value instanceof Set) {
+ return writeSet(writer, (Set) value);
+ }
+
Constructor constructor = matchingConstructor(value);
if (constructor != null) {
writer.append("new ").append(value.getClass());
@@ -245,6 +250,31 @@ public class ConstantExpression extends Expression {
return writer.append(end);
}
+ private static ExpressionWriter writeSet(ExpressionWriter writer, Set set) {
+ writer.append("com.google.common.collect.ImmutableSet.");
+ if (set.isEmpty()) {
+ return writer.append("of()");
+ }
+ if (set.size() < 5) {
+ return set(writer, set, "of(", ",", ")");
+ }
+ return set(writer, set, "builder().add(", ")\n.add(", ").build()");
+ }
+
+ private static ExpressionWriter set(ExpressionWriter writer, Set set,
+ String begin, String entrySep, String end) {
+ writer.append(begin);
+ boolean comma = false;
+ for (Object o : set.toArray()) {
+ if (comma) {
+ writer.append(entrySep).indent();
+ }
+ write(writer, o, null);
+ comma = true;
+ }
+ return writer.append(end);
+ }
+
private static Constructor matchingConstructor(Object value) {
final Field[] fields = value.getClass().getFields();
for (Constructor<?> constructor : value.getClass().getConstructors()) {
diff --git a/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java b/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java
index 8515041..005e8ae 100644
--- a/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java
+++ b/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java
@@ -35,6 +35,8 @@ import org.apache.calcite.linq4j.tree.Types;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
import org.junit.Test;
@@ -47,9 +49,12 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.TreeSet;
import static org.apache.calcite.linq4j.test.BlockBuilderBase.ONE;
@@ -1454,12 +1459,12 @@ public class ExpressionTest {
Expressions.toString(Expressions.constant(Arrays.asList())));
}
- @Test public void testEneElementListLiteral() throws Exception {
+ @Test public void testOneElementListLiteral() throws Exception {
assertEquals("java.util.Arrays.asList(1)",
Expressions.toString(Expressions.constant(Arrays.asList(1))));
}
- @Test public void testTwoElementListLiteral() throws Exception {
+ @Test public void testTwoElementsListLiteral() throws Exception {
assertEquals("java.util.Arrays.asList(1,\n"
+ " 2)",
Expressions.toString(Expressions.constant(Arrays.asList(1, 2))));
@@ -1485,13 +1490,13 @@ public class ExpressionTest {
Expressions.toString(Expressions.constant(Collections.singletonMap("abc", 42))));
}
- @Test public void testTwoElementMapLiteral() throws Exception {
+ @Test public void testTwoElementsMapLiteral() throws Exception {
assertEquals("com.google.common.collect.ImmutableMap.of(\"abc\", 42,\n"
+ "\"def\", 43)",
Expressions.toString(Expressions.constant(ImmutableMap.of("abc", 42, "def", 43))));
}
- @Test public void testTenElementMapLiteral() throws Exception {
+ @Test public void testTenElementsMapLiteral() throws Exception {
Map<String, String> map = new LinkedHashMap<>(); // for consistent output
for (int i = 0; i < 10; i++) {
map.put("key_" + i, "value_" + i);
@@ -1515,6 +1520,75 @@ public class ExpressionTest {
assertThat(value, is(3));
}
+ @Test public void testEmptySetLiteral() throws Exception {
+ assertEquals("com.google.common.collect.ImmutableSet.of()",
+ Expressions.toString(Expressions.constant(new HashSet())));
+ }
+
+ @Test public void testOneElementSetLiteral() throws Exception {
+ assertEquals("com.google.common.collect.ImmutableSet.of(1)",
+ Expressions.toString(Expressions.constant(Sets.newHashSet(1))));
+ }
+
+ @Test public void testTwoElementsSetLiteral() throws Exception {
+ assertEquals("com.google.common.collect.ImmutableSet.of(1,2)",
+ Expressions.toString(Expressions.constant(ImmutableSet.of(1, 2))));
+ }
+
+ @Test public void testTenElementsSetLiteral() throws Exception {
+ Set set = new HashSet(); // for consistent output
+ for (int i = 0; i < 10; i++) {
+ set.add(i);
+ }
+ assertEquals("com.google.common.collect.ImmutableSet.builder().add(0)\n"
+ + ".add(1)\n"
+ + ".add(2)\n"
+ + ".add(3)\n"
+ + ".add(4)\n"
+ + ".add(5)\n"
+ + ".add(6)\n"
+ + ".add(7)\n"
+ + ".add(8)\n"
+ + ".add(9).build()",
+ Expressions.toString(Expressions.constant(set)));
+ }
+
+ @Test public void testTenElementsLinkedHashSetLiteral() throws Exception {
+ Set set = new LinkedHashSet(); // for consistent output
+ for (Integer i = 0; i < 10; i++) {
+ set.add(i);
+ }
+ assertEquals("com.google.common.collect.ImmutableSet.builder().add(0)\n"
+ + ".add(1)\n"
+ + ".add(2)\n"
+ + ".add(3)\n"
+ + ".add(4)\n"
+ + ".add(5)\n"
+ + ".add(6)\n"
+ + ".add(7)\n"
+ + ".add(8)\n"
+ + ".add(9).build()",
+ Expressions.toString(Expressions.constant(set)));
+ }
+
+ @Test public void testTenElementsSetStringLiteral() throws Exception {
+ Set set = new LinkedHashSet(); // for consistent output
+ for (int i = 10; i > 0; i--) {
+ set.add(String.valueOf(i));
+ }
+ assertEquals("com.google.common.collect.ImmutableSet.builder().add(\"10\")\n"
+ + ".add(\"9\")\n"
+ + ".add(\"8\")\n"
+ + ".add(\"7\")\n"
+ + ".add(\"6\")\n"
+ + ".add(\"5\")\n"
+ + ".add(\"4\")\n"
+ + ".add(\"3\")\n"
+ + ".add(\"2\")\n"
+ + ".add(\"1\").build()",
+ Expressions.toString(Expressions.constant(set)));
+ }
+
/** An enum. */
enum MyEnum {
X,