@Override public List<String> columns() { return originalResult.columns(); }
EagerQueryResult() { fields = originalResult.columns().toArray( new String[0] ); }
@Override public String resultAsString() { List<String> columns = originalResult.columns(); StringBuilder builder = new StringBuilder(); builder.append( String.join( ITEM_SEPARATOR, columns ) ); if ( !queryResult.isEmpty() ) { builder.append( lineSeparator() ); int numberOfColumns = columns.size(); for ( Map<String,Object> row : queryResult ) { writeRow( columns, builder, numberOfColumns, row ); builder.append( lineSeparator() ); } } return builder.toString(); }
@Test @SuppressWarnings( "unchecked" ) public void shouldNotIncludePlanUnlessAskedFor() throws Exception { // Given Result result = mock( Result.class ); when( result.hasNext() ).thenReturn( false ); when( result.columns() ).thenReturn( new ArrayList<>() ); // When Map<String, Object> serialized = serializeToStringThenParseAsToMap( new CypherResultRepresentation( result, /*includeStats=*/false, false ) ); // Then assertFalse( "Didn't expect to see a plan here", serialized.containsKey( "plan" ) ); }
public CypherResultRepresentation( final Result result, boolean includeStats, boolean includePlan ) { super( RepresentationType.STRING ); resultRepresentation = createResultRepresentation( result ); columns = ListRepresentation.string( result.columns() ); statsRepresentation = includeStats ? new CypherStatisticsRepresentation( result.getQueryStatistics() ) : null; plan = includePlan ? CypherPlanRepresentation.newFromProvider(planProvider(result)) : null; }
private ListRepresentation createResultRepresentation( Result executionResult ) { final List<String> columns = executionResult.columns(); Iterable<Map<String, Object>> inner = new RepresentationExceptionHandlingIterable<>( loop( executionResult ) ); return new ListRepresentation( "data", new IterableWrapper<Representation,Map<String,Object>>( inner ) { @Override protected Representation underlyingObjectToObject( final Map<String,Object> row ) { return new ListRepresentation( "row", new IterableWrapper<Representation,String>( columns ) { @Override protected Representation underlyingObjectToObject( String column ) { return getRepresentation( row.get( column ) ); } } ); } } ); }
@SafeVarargs private static Result mockExecutionResult( ExecutionPlanDescription planDescription, Iterable<Notification> notifications, Map<String, Object>... rows ) { Set<String> keys = new TreeSet<>(); for ( Map<String, Object> row : rows ) { keys.addAll( row.keySet() ); } Result executionResult = mock( Result.class ); when( executionResult.columns() ).thenReturn( new ArrayList<>( keys ) ); final Iterator<Map<String, Object>> inner = asList( rows ).iterator(); when( executionResult.hasNext() ).thenAnswer( invocation -> inner.hasNext() ); when( executionResult.next() ).thenAnswer( invocation -> inner.next() ); when( executionResult.getQueryExecutionType() ) .thenReturn( null != planDescription ? QueryExecutionType.profiled( QueryExecutionType.QueryType.READ_WRITE ) : QueryExecutionType.query( QueryExecutionType.QueryType.READ_WRITE ) ); if ( executionResult.getQueryExecutionType().requestedExecutionPlanDescription() ) { when( executionResult.getExecutionPlanDescription() ).thenReturn( planDescription ); } mockAccept( executionResult ); when( executionResult.getNotifications() ).thenReturn( notifications ); return executionResult; }
when( result.columns() ).thenReturn( new ArrayList<>() ); when( result.getExecutionPlanDescription() ).thenReturn( plan );
@Test public void eagerResultContainsColumns() { Result result = database.execute( "MATCH (n) RETURN n.c as a, count(n) as b" ); assertEquals( 1, testCursorContext.getAdditionalAttempts() ); assertEquals( Arrays.asList("a", "b"), result.columns() ); }
Result executionResult = mock( Result.class ); mockAccept( executionResult ); when( executionResult.columns() ).thenReturn( new ArrayList<>( data.keySet() ) ); when( executionResult.hasNext() ).thenReturn( true, true, false ); when( executionResult.next() ).thenReturn( data ).thenThrow( new RuntimeException( "Stuff went wrong!" ) );
Result executionResult = mock( Result.class ); mockAccept( executionResult ); when( executionResult.columns() ).thenReturn( new ArrayList<>( data.keySet() ) ); when( executionResult.hasNext() ).thenReturn( true ).thenThrow( new RuntimeException( "Stuff went wrong!" ) );
public static SubGraph from( Result result, GraphDatabaseService gds, boolean addBetween ) final List<String> columns = result.columns(); for ( Map<String, Object> row : loop( result ) )
try Iterable<String> columns = result.columns(); writeColumns( columns ); writeRows( columns, result, configureWriters( resultDataContents ) );
public String[] writeResultHeader(Result result, CSVWriter out) { List<String> columns = result.columns(); int cols = columns.size(); String[] header = columns.toArray(new String[cols]); out.writeNext(header, applyQuotesToAll); return header; }
public ProgressInfo dump(Result result, Writer writer, Reporter reporter, ExportConfig config) throws Exception { Consumer<JsonGenerator> consumer = (jsonGenerator) -> { try { String[] header = result.columns().toArray(new String[result.columns().size()]); result.accept((row) -> { writeJsonResult(reporter, header, jsonGenerator, row, config); reporter.nextRow(); return true; }); } catch (IOException e) { throw new RuntimeException(e); } }; return dump(writer, reporter, consumer); }
@Procedure @Description("apoc.exportJson.json.query(query,file,{config,...,params:{params}}) - exports results from the cypher statement as json to the provided file") public Stream<ProgressInfo> query(@Name("query") String query, @Name("file") String fileName, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) throws Exception { Map<String,Object> params = config == null ? Collections.emptyMap() : (Map<String,Object>)config.getOrDefault("params", Collections.emptyMap()); Result result = db.execute(query,params); String source = String.format("statement: cols(%d)", result.columns().size()); return exportJson(fileName, source,result,config); }
@Procedure @Description("apoc.export.csv.query(query,file,{config,...,params:{params}}) - exports results from the cypher statement as csv to the provided file") public Stream<ProgressInfo> query(@Name("query") String query, @Name("file") String fileName, @Name("config") Map<String, Object> config) throws Exception { Map<String,Object> params = config == null ? Collections.emptyMap() : (Map<String,Object>)config.getOrDefault("params", Collections.emptyMap()); Result result = db.execute(query,params); String source = String.format("statement: cols(%d)", result.columns().size()); return exportCsv(fileName, source,result,config); }
@Override public AnyValue apply(org.neo4j.kernel.api.proc.Context ctx, AnyValue[] input) throws ProcedureException { Map<String, Object> params = functionParams(input, inputs, defaultValueMapper); try (Result result = api.execute(statement, params)) { // resourceTracker.registerCloseableResource(result); // TODO if (!result.hasNext()) return null; if (output.isEmpty()) { return ValueUtils.of(result.stream().collect(Collectors.toList())); } List<String> cols = result.columns(); if (cols.isEmpty()) return null; if (!forceSingle && outType instanceof ListType) { ListType listType = (ListType) outType; AnyType innerType = listType.innerType(); if (innerType instanceof MapType) return ValueUtils.of(result.stream().collect(Collectors.toList())); if (cols.size() == 1) return ValueUtils.of(result.stream().map(row -> row.get(cols.get(0))).collect(Collectors.toList())); } else { Map<String, Object> row = result.next(); if (outType instanceof MapType) return ValueUtils.of(row); if (cols.size() == 1) return ValueUtils.of(row.get(cols.get(0))); } throw new IllegalStateException("Result mismatch " + cols + " output type is " + output); } } }, true);
@UserFunction @Deprecated @Description("use either apoc.cypher.runFirstColumnMany for a list return or apoc.cypher.runFirstColumnSingle for returning the first row of the first column") public Object runFirstColumn(@Name("cypher") String statement, @Name("params") Map<String, Object> params, @Name(value = "expectMultipleValues",defaultValue = "true") boolean expectMultipleValues) { if (params == null) params = Collections.emptyMap(); String resolvedStatement = withParamMapping(statement, params.keySet()); if (!resolvedStatement.contains(" runtime")) resolvedStatement = "cypher runtime=slotted " + resolvedStatement; try (Result result = db.execute(resolvedStatement, params)) { String firstColumn = result.columns().get(0); try (ResourceIterator<Object> iter = result.columnAs(firstColumn)) { if (expectMultipleValues) return iter.stream().collect(Collectors.toList()); return iter.hasNext() ? iter.next() : null; } } }
/** * invoke cypherAction in batched transactions being feeded from cypherIteration running in main thread * @param cypherIterate * @param cypherAction */ @Procedure(mode = Mode.WRITE) @Description("apoc.periodic.iterate('statement returning items', 'statement per item', {batchSize:1000,iterateList:true,parallel:false,params:{},concurrency:50,retries:0}) YIELD batches, total - run the second statement for each item returned by the first statement. Returns number of batches and total processed rows") public Stream<BatchAndTotalResult> iterate( @Name("cypherIterate") String cypherIterate, @Name("cypherAction") String cypherAction, @Name("config") Map<String,Object> config) { long batchSize = Util.toLong(config.getOrDefault("batchSize", 10000)); int concurrency = Util.toInteger(config.getOrDefault("concurrency", 50)); boolean parallel = Util.toBoolean(config.getOrDefault("parallel", false)); boolean iterateList = Util.toBoolean(config.getOrDefault("iterateList", true)); long retries = Util.toLong(config.getOrDefault("retries", 0)); // todo sleep/delay or push to end of batch to try again or immediate ? Map<String,Object> params = (Map)config.getOrDefault("params", Collections.emptyMap()); try (Result result = db.execute(cypherIterate,params)) { Pair<String,Boolean> prepared = prepareInnerStatement(cypherAction, iterateList, result.columns(), "_batch"); String innerStatement = prepared.first(); iterateList=prepared.other(); log.info("starting batching from `%s` operation using iteration `%s` in separate thread", cypherIterate,cypherAction); return iterateAndExecuteBatchedInSeparateThread((int)batchSize, parallel, iterateList, retries, result, (p) -> db.execute(innerStatement, merge(params, p)).close(), concurrency); } }