Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timechart refactoring #490

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,54 +47,40 @@

import com.teragrep.pth10.ast.*;
import com.teragrep.pth10.ast.bo.*;
import com.teragrep.pth10.ast.bo.Token.Type;
import com.teragrep.pth10.ast.commands.aggregate.AggregateFunction;
import com.teragrep.pth10.steps.timechart.TimechartStep;
import com.teragrep.pth_03.antlr.DPLParser;
import com.teragrep.pth_03.antlr.DPLParserBaseVisitor;
import com.teragrep.pth_03.shaded.org.antlr.v4.runtime.tree.ParseTree;
import org.apache.spark.sql.Column;
import org.apache.spark.sql.functions;
import org.apache.spark.unsafe.types.CalendarInterval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Class that contains the visitor methods for the <code>timechart</code> command<br>
* Provides a pivoted dataset, making it easier to form time-field graphs in the UI
* <pre>Dataset.groupBy("_time").pivot(aggregateField).sum(fieldname)</pre>
*/
public class TimechartTransformation extends DPLParserBaseVisitor<Node> {
public final class TimechartTransformation extends DPLParserBaseVisitor<Node> {

private static final Logger LOGGER = LoggerFactory.getLogger(TimechartTransformation.class);
private DPLParserCatalystContext catCtx = null;
private DPLParserCatalystVisitor catVisitor;
private Document doc;
private final DPLParserCatalystContext catCtx;

EvalTransformation evalTransformation;
AggregateFunction aggregateFunction;
private String aggregateField = null;
private final AggregateFunction aggregateFunction;

public TimechartStep timechartStep = null;
// fields set in visit functions
private Column span;
private final ArrayList<Column> aggFunCols = new ArrayList<>();
private final ArrayList<String> divByInsts = new ArrayList<>();

public TimechartTransformation(DPLParserCatalystContext catCtx, DPLParserCatalystVisitor catVisitor) {
this.doc = null;
public TimechartTransformation(final DPLParserCatalystContext catCtx) {
this.catCtx = catCtx;
this.catVisitor = catVisitor;
this.evalTransformation = new EvalTransformation(catCtx);
this.aggregateFunction = new AggregateFunction(catCtx);
}

public String getAggregateField() {
return this.aggregateField;
}

/**
* timechartTransformation : COMMAND_MODE_TIMECHART (t_timechart_sepParameter)? (t_timechart_formatParameter)?
* (t_timechart_fixedrangeParameter)? (t_timechart_partialParameter)? (t_timechart_contParameter)?
Expand All @@ -108,70 +94,20 @@ public Node visitTimechartTransformation(DPLParser.TimechartTransformationContex
}

private Node timechartTransformationEmitCatalyst(DPLParser.TimechartTransformationContext ctx) {
this.timechartStep = new TimechartStep();

Column span = null;

if (ctx.t_timechart_binOptParameter() != null && !ctx.t_timechart_binOptParameter().isEmpty()) {
LOGGER.info("Timechart Optional parameters: <[{}]>", ctx.t_timechart_binOptParameter().get(0).getText());

ColumnNode spanNode = (ColumnNode) visit(ctx.t_timechart_binOptParameter().get(0));
if (spanNode != null) {
span = spanNode.getColumn();
}
}

Column funCol = null;
List<Column> listOfAggFunCols = new ArrayList<>();
List<String> listOfDivideByInst = new ArrayList<>();
for (int i = 0; i < ctx.getChildCount(); i++) {
ParseTree child = ctx.getChild(i);

if (child instanceof DPLParser.AggregateFunctionContext) {
// go through each agg. function
DPLParser.AggregateFunctionContext aggFunCtx = (DPLParser.AggregateFunctionContext) child;
Node funNode = visit(aggFunCtx);
if (funNode != null) {
if (funCol != null) {
listOfAggFunCols.add(funCol);
}
funCol = ((ColumnNode) funNode).getColumn();
}
}
else if (child instanceof DPLParser.T_timechart_divideByInstructionContext) {
String divByInst = ((StringNode) visitT_timechart_divideByInstruction(
(DPLParser.T_timechart_divideByInstructionContext) child
)).toString();
listOfDivideByInst.add(divByInst);
}
else if (child instanceof DPLParser.T_timechart_fieldRenameInstructionContext) {
if (funCol != null) {
funCol = funCol.as(visit(child).toString());
}
}
}
listOfAggFunCols.add(funCol); // need to add last one; for loop above only adds if there's a new one coming
span = createDefaultSpan();

if (span == null) {
span = createDefaultSpan();
}
visitChildren(ctx); // visit all the parameters

timechartStep.setHdfsPath(this.catVisitor.getHdfsPath());
timechartStep.setCatCtx(catCtx);
timechartStep.setSpan(span);
timechartStep.setAggCols(listOfAggFunCols);
timechartStep.setDivByInsts(listOfDivideByInst);
TimechartStep timechartStep = new TimechartStep(aggFunCols, divByInsts, span);

// span
this.catCtx.setTimeChartSpanSeconds(getSpanSeconds(span));

LOGGER.debug("span= <[{}]>", timechartStep.getSpan().toString());
LOGGER.debug("aggcols= <[{}]>", Arrays.toString(timechartStep.getAggCols().toArray()));
LOGGER.debug("divby= <[{}]>", Arrays.toString(timechartStep.getDivByInsts().toArray()));
LOGGER.debug("span= <[{}]>", span);
LOGGER.debug("aggcols= <[{}]>", aggFunCols);
LOGGER.debug("divby= <[{}]>", divByInsts);

return new StepNode(timechartStep);

//throw new RuntimeException("Chart transformation operation not supported yet");
}

/**
Expand Down Expand Up @@ -209,47 +145,35 @@ else if (isWithinNumber && spanChar != ' ') {

@Override
public Node visitAggregateFunction(DPLParser.AggregateFunctionContext ctx) {
Node rv = aggregateFunction.visitAggregateFunction(ctx);
if (aggregateField == null)
aggregateField = aggregateFunction.getAggregateField();
return aggregateFunction.visitAggregateFunction(ctx);
ColumnNode aggCol = (ColumnNode) aggregateFunction.visitAggregateFunction(ctx);
aggFunCols.add(aggCol.getColumn());
return new NullNode();
}

@Override
public Node visitT_timechart_divideByInstruction(DPLParser.T_timechart_divideByInstructionContext ctx) {
// LOGGER.info(ctx.getChildCount()+"--visitT_chart_divideByInstruction incoming{}", ctx.getText());
if (ctx.getChildCount() == 0) {
return null;
}
String target = ctx.getChild(1).getChild(0).toString();
String field = ctx.fieldType().getChild(0).toString();
divByInsts.add(field);

if (doc != null) {
Element el = doc.createElement("divideBy");
el.setAttribute("field", target);
return new ElementNode(el);
}
else {
return new StringNode(new Token(Type.STRING, target));
}
return new NullNode();
}

@Override
public Node visitT_timechart_fieldRenameInstruction(DPLParser.T_timechart_fieldRenameInstructionContext ctx) {
String field = ctx.getChild(1).getText();
if (doc != null) {
Element el = doc.createElement("fieldRename");
el.setAttribute("field", field);
return new ElementNode(el);
}
else {
return new StringNode(new Token(Type.STRING, field));
String rename = ctx.getChild(1).getText();
if (!aggFunCols.isEmpty()) {
Column latestAgg = aggFunCols.remove(aggFunCols.size() - 1);
aggFunCols.add(latestAgg.as(rename)); // rename the newest visited aggregation column
}

return new NullNode();
}

@Override
public Node visitT_timechart_binOptParameter(DPLParser.T_timechart_binOptParameterContext ctx) {
LOGGER.info("visitT_timechart_binOptParameter:<{}>", ctx.getText());
return visitChildren(ctx);
span = ((ColumnNode) visitChildren(ctx)).getColumn();
return new NullNode();
}

@Override
Expand All @@ -268,10 +192,9 @@ public Node visitT_timechart_binSpanParameter(DPLParser.T_timechart_binSpanParam
* @return
*/
private Column createDefaultSpan() {
long sec = 0;
final long sec;
final String duration;
TimeRange tr = TimeRange.ONE_HOUR;
String duration = "1 days"; // Default duration
// LOGGER.info("createDefaultSpan="+catCtx.getTimeRange());
DPLParserConfig pConf = catCtx.getParserConfig();
if (pConf != null) {
tr = pConf.getTimeRange();
Expand Down Expand Up @@ -332,7 +255,6 @@ private CalendarInterval getSpanLength(String value) {
// default timescale is sec
String timescale = "sec";
int numericalValue;
int month = 0;
long sec = 0;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(value);
Expand Down Expand Up @@ -400,7 +322,7 @@ private CalendarInterval getSpanLength(String value) {
break;
}
}
return new CalendarInterval(month, 0, sec * 1000 * 1000L);
return new CalendarInterval(0, 0, sec * 1000 * 1000L);
}

@Override
Expand Down Expand Up @@ -483,33 +405,8 @@ public Node visitT_timechart_evaledField(DPLParser.T_timechart_evaledFieldContex
return visitChildren(ctx);
}

/*@Override public Node visitT_timechart_singleAggregation(DPLParser.T_timechart_singleAggregationContext ctx) {
String oper = ctx.getText();
String defaultField="*";
Node rv = null;
Column col = null;
if(oper.equalsIgnoreCase("count") || oper.equalsIgnoreCase("c")) {
aggregateField = "count"; // use default name
col = org.apache.spark.sql.functions.count(defaultField);
// LOGGER.info("T_timechart_singleAggregation (Catalyst):{}", col.expr().sql()+" default field="+defaultField);
traceBuffer.add("Visit AggregateMethodCount(Catalyst):{}", col.expr().sql());
rv = new ColumnNode(col);
}else {
rv = this.aggregateFunction.visitAggregateFunction(ctx.aggregateFunction());
this.aggregateField = aggregateFunction.getAggregateField();
}
// Check whether field needs to be renamed
if(ctx.t_timechart_fieldRenameInstruction() != null){
Node renameCmd = visitT_timechart_fieldRenameInstruction(ctx.t_timechart_fieldRenameInstruction());
aggregateField = renameCmd.toString();
// rv = new ColumnNode(((ColumnNode) rv).getColumn().as(renameCmd.toString()));
}
return rv;
}*/

@Override
public Node visitSpanType(DPLParser.SpanTypeContext ctx) {
// LOGGER.info("visitSpanType:"+ctx.getText());
return visitChildren(ctx);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public Node visitChartTransformation(DPLParser.ChartTransformationContext ctx) {
@Override
public Node visitTimechartTransformation(DPLParser.TimechartTransformationContext ctx) {
// timechart command
return new TimechartTransformation(catCtx, catVisitor).visitTimechartTransformation(ctx);
return new TimechartTransformation(catCtx).visitTimechartTransformation(ctx);
}

@Override
Expand Down

This file was deleted.

Loading