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

decrease earliest epoch value of query xml by 3 hours query XML #415

Open
wants to merge 7 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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.17.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<directory>${project.basedir}/target</directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
import com.teragrep.pth10.ast.bo.*;
import com.teragrep.pth10.ast.bo.Token.Type;
import com.teragrep.pth10.ast.commands.EmitMode;
import com.teragrep.pth10.ast.time.DecreasedEpochXmlElementValue;
import com.teragrep.pth10.ast.time.TimeQualifier;
import com.teragrep.pth10.ast.time.TimeQualifierImpl;
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.TerminalNode;
Expand Down Expand Up @@ -188,19 +190,28 @@ private ElementNode timeQualifierEmitXml(DPLParser.TimeQualifierContext ctx) {
TerminalNode node = (TerminalNode) ctx.getChild(0);
String value = ctx.getChild(1).getText();

TimeQualifier tq = new TimeQualifier(value, catCtx.getTimeFormatString(), node.getSymbol().getType(), doc);

if (tq.isStartTime()) {
startTime = tq.epoch();
final TimeQualifier timeQualifier = new TimeQualifierImpl(
value,
catCtx.getTimeFormatString(),
node.getSymbol().getType(),
doc
);
final ElementNode returnValue;
if (timeQualifier.isStartTime()) {
final long decreaseValue = 3 * 60 * 60 * 1000; // decrease 3 hours from earliest
final TimeQualifier decreasedQualifier = new DecreasedEpochXmlElementValue(timeQualifier, decreaseValue);
startTime = decreasedQualifier.epoch();
returnValue = new ElementNode(decreasedQualifier.xmlElement());
}
else if (tq.isEndTime()) {
endTime = tq.epoch();
else if (timeQualifier.isEndTime()) {
endTime = timeQualifier.epoch();
returnValue = new ElementNode(timeQualifier.xmlElement());
}
else {
throw new UnsupportedOperationException("Unexpected token: " + node.getSymbol().getText());
}

return new ElementNode(tq.xmlElement());
return returnValue;
}

/**
Expand All @@ -215,7 +226,12 @@ private ColumnNode timeQualifierEmitCatalyst(DPLParser.TimeQualifierContext ctx)
TerminalNode node = (TerminalNode) ctx.getChild(0);
String value = ctx.getChild(1).getText();

TimeQualifier tq = new TimeQualifier(value, catCtx.getTimeFormatString(), node.getSymbol().getType(), doc);
TimeQualifierImpl tq = new TimeQualifierImpl(
value,
catCtx.getTimeFormatString(),
node.getSymbol().getType(),
doc
);

if (tq.isStartTime()) {
startTime = tq.epoch();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Teragrep Data Processing Language (DPL) translator for Apache Spark (pth_10)
* Copyright (C) 2019-2024 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.pth10.ast.time;

import org.apache.spark.sql.Column;
import org.w3c.dom.Element;

import java.util.Objects;

/**
* Decreases set amount from xmlElement() Element value tag that contains an epoch value. Used as a temporary workaround
* for pth_06 database schema.
*/
public final class DecreasedEpochXmlElementValue implements TimeQualifier {

private final TimeQualifier origin;
private final long decreaseAmount;

public DecreasedEpochXmlElementValue(final TimeQualifier origin, final long decreaseAmount) {
this.origin = origin;
this.decreaseAmount = decreaseAmount;
}

@Override
public Element xmlElement() {
final long decreasedValue = origin.epoch() - decreaseAmount;
final Element element = origin.xmlElement();
// set decreased value
element.setAttribute("value", Long.toString(decreasedValue));
return element;
}

@Override
public boolean isStartTime() {
return origin.isStartTime();
}

@Override
public boolean isEndTime() {
return origin.isEndTime();
}

@Override
public long epoch() {
return origin.epoch();
}

@Override
public Column column() {
return origin.column();
}

@Override
public boolean isUnixEpoch() {
return origin.isUnixEpoch();
}

@Override
public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass())
return false;
final DecreasedEpochXmlElementValue cast = (DecreasedEpochXmlElementValue) o;
return decreaseAmount == cast.decreaseAmount && Objects.equals(origin, cast.origin);
}

@Override
public int hashCode() {
return Objects.hash(origin, decreaseAmount);
}
}
118 changes: 7 additions & 111 deletions src/main/java/com/teragrep/pth10/ast/time/TimeQualifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,124 +45,20 @@
*/
package com.teragrep.pth10.ast.time;

import com.teragrep.pth_03.antlr.DPLLexer;
import org.apache.spark.sql.Column;
import org.apache.spark.sql.functions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Objects;
public interface TimeQualifier {

public final class TimeQualifier {
Element xmlElement();

private final int token;
private final String value;
private final String timeformat;
private final Document doc;
boolean isStartTime();

public TimeQualifier(final String value, final String timeformat, final int type, final Document doc) {
this.token = type;
this.value = value;
this.timeformat = timeformat;
this.doc = doc;
}
boolean isEndTime();

public long epoch() {
if (isUnixEpoch()) {
try {
return Long.parseLong(value);
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid unix epoch: <[" + value + "]>", e);
}
}
return new EpochTimestamp(value, timeformat).epoch();
}
long epoch();

public Column column() {
Column col = new Column("`_time`");
Column column();

switch (token) {
case DPLLexer.EARLIEST:
case DPLLexer.INDEX_EARLIEST:
case DPLLexer.STARTTIMEU: {
col = col.geq(functions.from_unixtime(functions.lit(epoch())));
break;
}
case DPLLexer.LATEST:
case DPLLexer.INDEX_LATEST:
case DPLLexer.ENDTIMEU: {
col = col.lt(functions.from_unixtime(functions.lit(epoch())));
break;
}
default: {
throw new RuntimeException("TimeQualifier <" + token + "> not implemented yet.");
}
}

return col;
}

public Element xmlElement() {
// Handle date calculations
Element el;
switch (token) {
case DPLLexer.EARLIEST:
case DPLLexer.STARTTIMEU: {
el = doc.createElement("earliest");
el.setAttribute("operation", "GE");
break;
}
case DPLLexer.INDEX_EARLIEST: {
el = doc.createElement("index_earliest");
el.setAttribute("operation", "GE");
break;
}
case DPLLexer.LATEST:
case DPLLexer.ENDTIMEU: {
el = doc.createElement("latest");
el.setAttribute("operation", "LE");
break;
}
case DPLLexer.INDEX_LATEST: {
el = doc.createElement("index_latest");
el.setAttribute("operation", "LE");
break;
}
default: {
throw new RuntimeException("TimeQualifier <" + token + "> not implemented yet.");
}
}

el.setAttribute("value", Long.toString(epoch()));
return el;
}

public boolean isStartTime() {
return token == DPLLexer.EARLIEST || token == DPLLexer.INDEX_EARLIEST || token == DPLLexer.STARTTIMEU;
}

public boolean isEndTime() {
return token == DPLLexer.LATEST || token == DPLLexer.INDEX_LATEST || token == DPLLexer.ENDTIMEU;
}

public boolean isUnixEpoch() {
return token == DPLLexer.STARTTIMEU || token == DPLLexer.ENDTIMEU;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
TimeQualifier that = (TimeQualifier) o;
return Objects.equals(token, that.token) && Objects.equals(value, that.value)
&& Objects.equals(timeformat, that.timeformat) && Objects.equals(doc, that.doc);
}

@Override
public int hashCode() {
return Objects.hash(token, value, timeformat, doc);
}
boolean isUnixEpoch();
}
Loading