Operating on Vertices - Aster Execution Engine

Teradata Aster® Developer Guide

Product
Aster Execution Engine
Release Number
7.00.02
Published
July 2017
Language
English (United States)
Last Update
2018-04-13
dita:mapPath
xnl1494366523182.ditamap
dita:ditavalPath
Generic_no_ie_no_tempfilter.ditaval
dita:id
ffu1489104705746
lifecycle
previous
Product Category
Software
This procedure expands the HelloWorld function.
  1. Add the following private class variable:
       private String path = "";

    This variable will be referenced by the operateOnVertex() and emitFinalRows() methods.

  2. Add the following code to the operateOnVertex() method of the HelloWorld SQL-GR function. This code shows you how to use edge iterators and how to send and receive messages. The code also shows you how to get the iteration number and issue local and global halts.
    int target_vertex_id;
    VertexKey target_vertex_key;
    VertexKey vertex_key = vertexState.getVertex().getVertexKey();
    int vertex_id = vertex_key.getIntAt(0);
    EdgeIterator edgeIterator = vertexState.getEdgeIterator();
    System.out.println("++++++++++++++++++++++++++++++++++++++");
    System.out.println("This is iteration # " + globals.getIteration());
    System.out.println("Operating on vertex " + vertex_id);
    System.out.println();	
    if(vertex_id == sourceArgument){
    	System.out.println("Operating on the source node ("+vertex_id+")...");	
    	while (edgeIterator.advanceToNextEdge()){
    		target_vertex_key = edgeIterator.getEdge().getTargetVertexKey();
    		target_vertex_id = target_vertex_key.getIntAt(0);
    		System.out.println("There is an edge to vertex: "+target_vertex_id);	
    		// Add vertex ID to message.
    		System.out.println("Adding to message");	
    		outputMessages.addInt(vertex_id);
    		outputMessages.addString(String.valueOf(vertex_id));
    		// Send message to target vertex.
    		System.out.println("Sending message...");	
    		outputMessages.emitVertexMessage(target_vertex_key);
    	}
    	System.out.println();	
    	vertexState.localHalt();
    } else if(vertex_id == destinationArgument){
    		System.out.println("Operating on the destination node");
    		// If a message is received with containing the ID of the source node, 
		// issue a global halt.
    	if (inputMessages.advanceToNextMessage()){
    		if (inputMessages.getIntAt(0)== sourceArgument){
    			System.out.println("The destination vertex is reachable from vertex " +
				sourceArgument);
    			path = inputMessages.getStringAt(1)+
				" > "+String.valueOf(destinationArgument);
    			globals.globalHalt();
    		}
    	}
    	System.out.println();
    }
    else {
    	if (inputMessages.advanceToNextMessage()){
    		if (inputMessages.getIntAt(0)== sourceArgument){
    			System.out.println("Vertex (" + vertex_id + 
			") is reachable from vertex " + inputMessages.getIntAt(0));
    			// Add vertex ID of source vertex to message.
    			while (edgeIterator.advanceToNextEdge()){
    				target_vertex_key = edgeIterator.getEdge().getTargetVertexKey();
    				target_vertex_id = target_vertex_key.getIntAt(0);
    				System.out.println("There is an edge to vertex: "+target_vertex_id);	
    				// Add vertex ID of source vertex to message.
    				outputMessages.addInt(sourceArgument);
    				outputMessages.addString(inputMessages.getStringAt(1)+
				                    " > "+String.valueOf(vertex_id));
    				// Send message to target vertex.
    				outputMessages.emitVertexMessage(target_vertex_key);
    			}
    			vertexState.localHalt();	
    		}					
    	}
    }
  3. Add the following code to the emitFinalRows() method.
    if (globals.isGlobalHalt() &&(vertexState.getVertex().getVertexKey().getIntAt(0)==
				destinationArgument)){
    	// This means that the destination vertex is reachable from the source vertex.
    	// Emit the path leading to the destination vertex.
    	emitter.addString(path);
    	emitter.emitRow();
    		}
  4. Run the following query:
    select * from HelloWorld(
      on vertices as vtable partition by vid
      on edges as etable partition by src
      source(1) destination(4)
    ) order by 1;
    The output looks like this:
    path          
    -----------------------
    1 > 2 > 7 > 6 > 3 > 4
    (1 row)

    For more examples, see SQL-GR™ Function Examples_stub.