org.apache.calcite.linq4j.Linq4j Java Examples
The following examples show how to use
org.apache.calcite.linq4j.Linq4j.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testToMapWithComparer() { final Map<String, String> map = Linq4j.asEnumerable(Arrays.asList("foo", "bar", "far")) .toMap(Functions.identitySelector(), new EqualityComparer<String>() { public boolean equal(String v1, String v2) { return String.CASE_INSENSITIVE_ORDER.compare(v1, v2) == 0; } public int hashCode(String s) { return s == null ? Objects.hashCode(null) : s.toLowerCase(Locale.ROOT).hashCode(); } }); assertEquals(3, map.size()); assertTrue(map.get("foo").equals("foo")); assertTrue(map.get("Foo").equals("foo")); assertTrue(map.get("FOO").equals("foo")); }
Example #2
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testList() { final List<Employee> employees = Arrays.asList( new Employee(100, "Fred", 10), new Employee(110, "Bill", 30), new Employee(120, "Eric", 10), new Employee(130, "Janet", 10)); final Map<Employee, Department> empDepts = new HashMap<>(); for (Employee employee : employees) { empDepts.put(employee, depts[(employee.deptno - 10) / 10]); } final List<Grouping<Object, Map.Entry<Employee, Department>>> result = new ArrayList<>(); Linq4j.asEnumerable(empDepts.entrySet()) .groupBy((Function1<Map.Entry<Employee, Department>, Object>) Map.Entry::getValue) .into(result); assertNotNull(result.toString()); }
Example #3
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testGroupByWithKeySelectorAndElementSelectorAndComparer() { String s = Linq4j.asEnumerable(emps) .groupBy(EMP_DEPTNO_SELECTOR, EMP_NAME_SELECTOR, new EqualityComparer<Integer>() { public boolean equal(Integer v1, Integer v2) { return true; } public int hashCode(Integer integer) { return 0; } }) .select(group -> String.format(Locale.ROOT, "%s: %s", group.getKey(), stringJoin("+", group))) .toList() .toString(); assertEquals( "[10: Fred+Bill+Eric+Janet]", s); }
Example #4
Source File: CodesFunction.java From calcite with Apache License 2.0 | 6 votes |
public static QueryableTable getTable(String name) { return new AbstractQueryableTable(Object[].class) { @Override public RelDataType getRowType(RelDataTypeFactory typeFactory) { return typeFactory.builder() .add("TYPE", SqlTypeName.VARCHAR) .add("CODEVALUE", SqlTypeName.VARCHAR) .build(); } @Override public Queryable<String[]> asQueryable(QueryProvider queryProvider, SchemaPlus schema, String tableName) { if (name == null) { return Linq4j.<String[]>emptyEnumerable().asQueryable(); } return Linq4j.asEnumerable(new String[][]{ new String[]{"HASHCODE", "" + name.hashCode()}, new String[]{"BASE64", Base64.getEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8))} }).asQueryable(); } }; }
Example #5
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testDefaultIfEmpty2() { final List<String> experience = Arrays.asList("jimi", "mitch", "noel"); final Enumerable<String> notEmptyEnumerable = Linq4j.asEnumerable(experience).defaultIfEmpty("dummy"); final Enumerator<String> notEmptyEnumerator = notEmptyEnumerable.enumerator(); notEmptyEnumerator.moveNext(); assertEquals("jimi", notEmptyEnumerator.current()); notEmptyEnumerator.moveNext(); assertEquals("mitch", notEmptyEnumerator.current()); notEmptyEnumerator.moveNext(); assertEquals("noel", notEmptyEnumerator.current()); final Enumerable<String> emptyEnumerable = Linq4j.asEnumerable(Linq4j.<String>emptyEnumerable()).defaultIfEmpty("N/A"); final Enumerator<String> emptyEnumerator = emptyEnumerable.enumerator(); assertTrue(emptyEnumerator.moveNext()); assertEquals("N/A", emptyEnumerator.current()); assertFalse(emptyEnumerator.moveNext()); }
Example #6
Source File: MazeTable.java From calcite with Apache License 2.0 | 6 votes |
public Enumerable<Object[]> scan(DataContext root) { final Random random = seed >= 0 ? new Random(seed) : new Random(); final Maze maze = new Maze(width, height); final PrintWriter pw = Util.printWriter(System.out); maze.layout(random, pw); if (Maze.DEBUG) { maze.print(pw, true); } return new AbstractEnumerable<Object[]>() { public Enumerator<Object[]> enumerator() { final Set<Integer> solutionSet; if (solution) { solutionSet = maze.solve(0, 0); } else { solutionSet = null; } return Linq4j.transform(maze.enumerator(solutionSet), s -> new Object[] {s}); } }; }
Example #7
Source File: StreamTest.java From calcite with Apache License 2.0 | 6 votes |
public Enumerable<Object[]> scan(DataContext root) { return Linq4j.asEnumerable(() -> new Iterator<Object[]>() { private final String[] items = {"paint", "paper", "brush"}; private int counter = 0; public boolean hasNext() { return true; } public Object[] next() { final int index = counter++; return new Object[]{ System.currentTimeMillis(), index, items[index % items.length], 10}; } public void remove() { throw new UnsupportedOperationException(); } }); }
Example #8
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testToLookup() { final Lookup<Integer, Employee> lookup = Linq4j.asEnumerable(emps).toLookup( EMP_DEPTNO_SELECTOR); int n = 0; for (Grouping<Integer, Employee> grouping : lookup) { ++n; switch (grouping.getKey()) { case 10: assertEquals(3, grouping.count()); break; case 30: assertEquals(1, grouping.count()); break; default: fail("unknown department number " + grouping); } } assertEquals(n, 2); }
Example #9
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testToMap2WithComparer() { final Map<String, String> map = Linq4j.asEnumerable(Arrays.asList("foo", "bar", "far")) .toMap(Functions.identitySelector(), x -> x == null ? null : x.toUpperCase(Locale.ROOT), new EqualityComparer<String>() { public boolean equal(String v1, String v2) { return String.CASE_INSENSITIVE_ORDER.compare(v1, v2) == 0; } public int hashCode(String s) { return s == null ? Objects.hashCode(null) : s.toLowerCase(Locale.ROOT).hashCode(); } }); assertEquals(3, map.size()); assertTrue(map.get("foo").equals("FOO")); assertTrue(map.get("Foo").equals("FOO")); assertTrue(map.get("FOO").equals("FOO")); }
Example #10
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
/** * Tests the version of {@link ExtendedEnumerable#groupBy} * that uses an accumulator; does not build intermediate lists. */ @Test void testGroupBy() { String s = Linq4j.asEnumerable(emps) .groupBy( EMP_DEPTNO_SELECTOR, (Function0<String>) () -> null, (v1, e0) -> v1 == null ? e0.name : (v1 + "+" + e0.name), (v1, v2) -> v1 + ": " + v2) .orderBy(Functions.identitySelector()) .toList() .toString(); assertEquals( "[10: Fred+Eric+Janet, 30: Bill]", s); }
Example #11
Source File: QuarkMetaImpl.java From quark with Apache License 2.0 | 6 votes |
@Override public Frame fetch(StatementHandle h, long offset, int fetchMaxRowCount) { final QuarkConnectionImpl calciteConnection = getConnection(); QuarkJdbcStatement stmt = calciteConnection.server.getStatement(h); final Signature signature = stmt.getSignature(); final Iterator<Object> iterator; if (stmt.getResultSet() == null) { final Iterable<Object> iterable = Linq4j.emptyEnumerable(); iterator = iterable.iterator(); stmt.setResultSet(iterator); } else { iterator = stmt.getResultSet(); } final List<List<Object>> list = new ArrayList<>(); List<List<Object>> rows = MetaImpl.collect(signature.cursorFactory, LimitIterator.of(iterator, fetchMaxRowCount), list); boolean done = fetchMaxRowCount == 0 || list.size() < fetchMaxRowCount; return new Meta.Frame(offset, done, (List<Object>) (List) rows); }
Example #12
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testTakeWhileEnumerableFunction() { final Enumerable<Department> enumerableDepts = Linq4j.asEnumerable(depts); final List<Department> deptList = EnumerableDefaults.takeWhile( enumerableDepts, new Predicate2<Department, Integer>() { int index = 0; public boolean apply(Department v1, Integer v2) { // Make sure we're passed the correct indices assertEquals(index++, (int) v2, "Invalid index passed to function"); return 20 != v1.deptno; } }).toList(); assertEquals(1, deptList.size()); assertEquals(depts[0], deptList.get(0)); }
Example #13
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testRightJoin() { // Note #1: Left join means emit nulls on LHS but not RHS. // Employees with bad departments are eliminated; // departments with no employees are not eliminated. // Note #2: Order of employees is preserved. String s = Linq4j.asEnumerable(emps) .concat(Linq4j.asEnumerable(badEmps)) .hashJoin( Linq4j.asEnumerable(depts), EMP_DEPTNO_SELECTOR, DEPT_DEPTNO_SELECTOR, (v1, v2) -> (v1 == null ? null : v1.name) + " works in " + (v2 == null ? null : v2.name), null, true, false) .orderBy(Functions.identitySelector()) .toList() .toString(); assertEquals( "[Bill works in Marketing, " + "Eric works in Sales, " + "Fred works in Sales, " + "Janet works in Sales, " + "null works in HR]", s); }
Example #14
Source File: CalciteMetaImpl.java From calcite with Apache License 2.0 | 6 votes |
protected MetaResultSet createResultSet( Map<String, Object> internalParameters, List<ColumnMetaData> columns, CursorFactory cursorFactory, final Frame firstFrame) { try { final CalciteConnectionImpl connection = getConnection(); final AvaticaStatement statement = connection.createStatement(); final CalcitePrepare.CalciteSignature<Object> signature = new CalcitePrepare.CalciteSignature<Object>("", ImmutableList.of(), internalParameters, null, columns, cursorFactory, null, ImmutableList.of(), -1, null, Meta.StatementType.SELECT) { @Override public Enumerable<Object> enumerable( DataContext dataContext) { return Linq4j.asEnumerable(firstFrame.rows); } }; return MetaResultSet.create(connection.id, statement.getId(), true, signature, firstFrame); } catch (SQLException e) { throw new RuntimeException(e); } }
Example #15
Source File: QuarkMetaImpl.java From quark with Apache License 2.0 | 6 votes |
protected MetaResultSet createResultSet( Map<String, Object> internalParameters, List<ColumnMetaData> columns, CursorFactory cursorFactory, final Frame firstFrame) { try { final QuarkConnectionImpl connection = getConnection(); final AvaticaStatement statement = connection.createStatement(); final CalcitePrepare.CalciteSignature<Object> signature = new CalcitePrepare.CalciteSignature<Object>("", ImmutableList.<AvaticaParameter>of(), internalParameters, null, columns, cursorFactory, null, ImmutableList.<RelCollation>of(), -1, null, Meta.StatementType.SELECT) { @Override public Enumerable<Object> enumerable( DataContext dataContext) { return Linq4j.asEnumerable(firstFrame.rows); } }; return MetaResultSet.create(connection.id, statement.getId(), true, signature, firstFrame); } catch (SQLException e) { throw new RuntimeException(e); } }
Example #16
Source File: ListTable.java From calcite with Apache License 2.0 | 6 votes |
public <T> Queryable<T> asQueryable(final QueryProvider queryProvider, SchemaPlus schema, String tableName) { return new AbstractQueryable<T>() { public Type getElementType() { return elementType; } public Expression getExpression() { return expression; } public QueryProvider getProvider() { return queryProvider; } public Iterator<T> iterator() { //noinspection unchecked return list.iterator(); } public Enumerator<T> enumerator() { //noinspection unchecked return Linq4j.enumerator(list); } }; }
Example #17
Source File: HashJoinPlan.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
@Override public Scanner scan(DataContext dataContext, long flags) { Scanner left = this.left.scan(dataContext, flags); Scanner right = this.right.scan(dataContext, flags); Enumerable<DataAccessor> outer = Linq4j.asEnumerable(() -> left); Enumerable<DataAccessor> inner = Linq4j.asEnumerable(() -> right); Function1<DataAccessor, JoinKey> leftJoinKey = a0 -> JoinKey.keyExtractor(leftKeys).apply(a0); Function1<DataAccessor, JoinKey> rightJoinKey = a0 -> JoinKey.keyExtractor(rightKeys).apply(a0); Function2<DataAccessor, DataAccessor, DataAccessor> resultSelector = new Function2<DataAccessor, DataAccessor, DataAccessor>() { @Override public DataAccessor apply(DataAccessor v0, DataAccessor v1) { return null; } }; return Scanner.of(EnumerableDefaults.hashJoin(outer, inner, leftJoinKey, rightJoinKey, resultSelector, null,generateNullsOnLeft,generateNullsOnRight).iterator()); }
Example #18
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testDefaultIfEmpty() { final List<String> experience = Arrays.asList("jimi", "mitch", "noel"); final Enumerable<String> notEmptyEnumerable = Linq4j.asEnumerable(experience).defaultIfEmpty(); final Enumerator<String> notEmptyEnumerator = notEmptyEnumerable.enumerator(); notEmptyEnumerator.moveNext(); assertEquals("jimi", notEmptyEnumerator.current()); notEmptyEnumerator.moveNext(); assertEquals("mitch", notEmptyEnumerator.current()); notEmptyEnumerator.moveNext(); assertEquals("noel", notEmptyEnumerator.current()); final Enumerable<String> emptyEnumerable = Linq4j.asEnumerable(Linq4j.<String>emptyEnumerable()).defaultIfEmpty(); final Enumerator<String> emptyEnumerator = emptyEnumerable.enumerator(); assertTrue(emptyEnumerator.moveNext()); assertNull(emptyEnumerator.current()); assertFalse(emptyEnumerator.moveNext()); }
Example #19
Source File: EnumerablesTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testMergeSemiJoinWithNullKeys() { assertThat( EnumerableDefaults.mergeJoin( Linq4j.asEnumerable( Arrays.asList( new Emp(30, "Fred"), new Emp(20, "Sebastian"), new Emp(30, "Theodore"), new Emp(20, "Zoey"), new Emp(40, null), new Emp(30, null))), Linq4j.asEnumerable( Arrays.asList( new Dept(15, "Marketing"), new Dept(20, "Sales"), new Dept(30, "Theodore"), new Dept(25, "Theodore"), new Dept(33, "Zoey"), new Dept(40, null))), e -> e.name, d -> d.name, (e, d) -> e.name.startsWith("T"), (v0, v1) -> v0, JoinType.SEMI, null).toList().toString(), equalTo("[Emp(30, Theodore)]")); }
Example #20
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testJoin() { // Note #1: Inner on both sides. Employees with bad departments, // and departments with no employees are eliminated. // Note #2: Order of employees is preserved. String s = Linq4j.asEnumerable(emps) .concat(Linq4j.asEnumerable(badEmps)) .hashJoin( Linq4j.asEnumerable(depts), EMP_DEPTNO_SELECTOR, DEPT_DEPTNO_SELECTOR, (v1, v2) -> v1.name + " works in " + v2.name) .orderBy(Functions.identitySelector()) .toList() .toString(); assertEquals( "[Bill works in Marketing, " + "Eric works in Sales, " + "Fred works in Sales, " + "Janet works in Sales]", s); }
Example #21
Source File: UtilTest.java From calcite with Apache License 2.0 | 6 votes |
@Test void testFlatListProduct() { final List<Enumerator<List<String>>> list = new ArrayList<>(); list.add(Linq4j.enumerator(l2(l1("a"), l1("b")))); list.add(Linq4j.enumerator(l3(l2("x", "p"), l2("y", "q"), l2("z", "r")))); final Enumerable<FlatLists.ComparableList<String>> product = SqlFunctions.product(list, 3, false); int n = 0; FlatLists.ComparableList<String> previous = FlatLists.of(); for (FlatLists.ComparableList<String> strings : product) { if (n++ == 1) { assertThat(strings.size(), is(3)); assertThat(strings.get(0), is("a")); assertThat(strings.get(1), is("y")); assertThat(strings.get(2), is("q")); } if (previous != null) { assertTrue(previous.compareTo(strings) < 0); } previous = strings; } assertThat(n, is(6)); }
Example #22
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testSelectManyWithResultSelector() { final List<String> nameSeqs = Linq4j.asEnumerable(depts) .selectMany(DEPT_EMPLOYEES_SELECTOR, (element, subElement) -> subElement.name + "@" + element.name) .select((v0, v1) -> "#" + v1 + ": " + v0) .toList(); assertEquals( "[#0: Fred@Sales, #1: Eric@Sales, #2: Janet@Sales, #3: Bill@Marketing]", nameSeqs.toString()); }
Example #23
Source File: CalciteMetaImpl.java From calcite with Apache License 2.0 | 5 votes |
public Enumerable<MetaColumn> columns(final MetaTable table_) { final CalciteMetaTable table = (CalciteMetaTable) table_; final RelDataType rowType = table.calciteTable.getRowType(getConnection().typeFactory); return Linq4j.asEnumerable(rowType.getFieldList()) .select(field -> { final int precision = field.getType().getSqlTypeName().allowsPrec() && !(field.getType() instanceof RelDataTypeFactoryImpl.JavaType) ? field.getType().getPrecision() : -1; return new MetaColumn( table.tableCat, table.tableSchem, table.tableName, field.getName(), field.getType().getSqlTypeName().getJdbcOrdinal(), field.getType().getFullTypeString(), precision, field.getType().getSqlTypeName().allowsScale() ? field.getType().getScale() : null, 10, field.getType().isNullable() ? DatabaseMetaData.columnNullable : DatabaseMetaData.columnNoNulls, precision, field.getIndex() + 1, field.getType().isNullable() ? "YES" : "NO"); }); }
Example #24
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testTakeQueryable() { final Queryable<Department> querableDepts = Linq4j.asEnumerable(depts).asQueryable(); final List<Department> queryableResult = QueryableDefaults.take(querableDepts, 2).toList(); assertEquals(2, queryableResult.size()); assertEquals(depts[0], queryableResult.get(0)); assertEquals(depts[1], queryableResult.get(1)); }
Example #25
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testAllPredicate() { Predicate1<Employee> allEmpnoGE100 = emp -> emp.empno >= 100; Predicate1<Employee> allEmpnoGT100 = emp -> emp.empno > 100; assertTrue(Linq4j.asEnumerable(emps).all(allEmpnoGE100)); assertFalse(Linq4j.asEnumerable(emps).all(allEmpnoGT100)); }
Example #26
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testToLookupSelector() { final Lookup<Integer, String> lookup = Linq4j.asEnumerable(emps).toLookup( EMP_DEPTNO_SELECTOR, EMP_NAME_SELECTOR); int n = 0; for (Grouping<Integer, String> grouping : lookup) { ++n; switch (grouping.getKey()) { case 10: assertEquals(3, grouping.count()); assertTrue(grouping.contains("Fred")); assertTrue(grouping.contains("Eric")); assertTrue(grouping.contains("Janet")); assertFalse(grouping.contains("Bill")); break; case 30: assertEquals(1, grouping.count()); assertTrue(grouping.contains("Bill")); assertFalse(grouping.contains("Fred")); break; default: fail("unknown department number " + grouping); } } assertEquals(n, 2); assertEquals( "[10:3, 30:1]", lookup.applyResultSelector((v1, v2) -> v1 + ":" + v2.count()) .orderBy(Functions.identitySelector()) .toList().toString()); }
Example #27
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testGroupJoin() { // Note #1: Group join is a "left join": "bad employees" are filtered // out, but empty departments are not. // Note #2: Order of departments is preserved. String s = Linq4j.asEnumerable(depts) .groupJoin( Linq4j.asEnumerable(emps) .concat(Linq4j.asEnumerable(badEmps)), DEPT_DEPTNO_SELECTOR, EMP_DEPTNO_SELECTOR, (v1, v2) -> { final StringBuilder buf = new StringBuilder("["); int n = 0; for (Employee employee : v2) { if (n++ > 0) { buf.append(", "); } buf.append(employee.name); } return buf.append("] work(s) in ").append(v1.name) .toString(); }) .toList() .toString(); assertEquals( "[[Fred, Eric, Janet] work(s) in Sales, " + "[] work(s) in HR, " + "[Bill] work(s) in Marketing]", s); }
Example #28
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testTakeWhileNNoMatch() { final Queryable<Department> queryableDepts = Linq4j.asEnumerable(depts).asQueryable(); Predicate2<Department, Integer> function2 = Functions.falsePredicate2(); final List<Department> deptList = QueryableDefaults.takeWhileN( queryableDepts, Expressions.lambda(function2)) .toList(); assertEquals(0, deptList.size()); }
Example #29
Source File: CalciteResultSet.java From calcite with Apache License 2.0 | 5 votes |
private Cursor createCursor(ColumnMetaData.AvaticaType elementType, Iterable iterable) { final Enumerator enumerator = Linq4j.iterableEnumerator(iterable); //noinspection unchecked return !(elementType instanceof ColumnMetaData.StructType) || ((ColumnMetaData.StructType) elementType).columns.size() == 1 ? new ObjectEnumeratorCursor(enumerator) : new ArrayEnumeratorCursor(enumerator); }
Example #30
Source File: Linq4jTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testTakeEnumerableGreaterThanLength() { final Enumerable<Department> enumerableDepts = Linq4j.asEnumerable(depts); final List<Department> depList = EnumerableDefaults.take(enumerableDepts, 5).toList(); assertEquals(3, depList.size()); assertEquals(depts[0], depList.get(0)); assertEquals(depts[1], depList.get(1)); assertEquals(depts[2], depList.get(2)); }