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 4 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 @@ -49,6 +49,8 @@
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.DecreasedEpochValue;
import com.teragrep.pth10.ast.time.TimeQualifierInterface;
import com.teragrep.pth10.ast.time.TimeQualifier;
import com.teragrep.pth_03.antlr.DPLParser;
import com.teragrep.pth_03.antlr.DPLParserBaseVisitor;
Expand Down Expand Up @@ -188,19 +190,27 @@ 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 TimeQualifierInterface timeQualifierInterface = new TimeQualifier(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename TimeQualiferInterface to TimeQualifier, and the implementing class to TimeQualifierImpl

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

value,
catCtx.getTimeFormatString(),
node.getSymbol().getType(),
doc
);
final ElementNode returnValue;
if (timeQualifierInterface.isStartTime()) {
long decreaseValue = 3 * 60 * 60 * 1000;
startTime = timeQualifierInterface.epoch() - decreaseValue;
returnValue = new ElementNode(new DecreasedEpochValue(timeQualifierInterface, decreaseValue).xmlElement());
}
else if (tq.isEndTime()) {
endTime = tq.epoch();
else if (timeQualifierInterface.isEndTime()) {
endTime = timeQualifierInterface.epoch();
returnValue = new ElementNode(timeQualifierInterface.xmlElement());
}
else {
throw new UnsupportedOperationException("Unexpected token: " + node.getSymbol().getText());
}

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

/**
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/com/teragrep/pth10/ast/time/DecreasedEpochValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.w3c.dom.Element;

/** Decreases the amount from qualifier epoch value */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comment that this is a temporary workaround for the pth_06 database schema

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added explanatory comment in javadoc

public class DecreasedEpochValue implements TimeQualifierInterface {

TimeQualifierInterface origin;
long decreaseEpochValue;

public DecreasedEpochValue(TimeQualifierInterface origin, long decreaseEpochValue) {
this.origin = origin;
this.decreaseEpochValue = decreaseEpochValue;
}

@Override
public Element xmlElement() {
final Element el = origin.xmlElement();
final long newValue = Long.parseLong(el.getAttribute("value")) - decreaseEpochValue;
el.setAttribute("value", Long.toString(newValue));
return el;
}

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

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

@Override
public long epoch() {
return origin.epoch() - decreaseEpochValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

import java.util.Objects;

public final class TimeQualifier {
public final class TimeQualifier implements TimeQualifierInterface {

private final int token;
private final String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.w3c.dom.Element;

public interface TimeQualifierInterface {

Element xmlElement();

boolean isStartTime();

boolean isEndTime();

long epoch();
}
20 changes: 20 additions & 0 deletions src/test/java/com/teragrep/pth10/TimeQualifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
*/
package com.teragrep.pth10;

import com.teragrep.pth10.ast.time.DecreasedEpochValue;
import com.teragrep.pth10.ast.time.TimeQualifier;
import com.teragrep.pth10.ast.time.TimeQualifierInterface;
import com.teragrep.pth_03.antlr.DPLLexer;
import org.apache.spark.sql.Column;
import org.apache.spark.sql.functions;
Expand Down Expand Up @@ -160,6 +162,24 @@ public void testEndtimeU() {
Assertions.assertEquals(el.toString(), tq.xmlElement().toString());
}

@Test
public void testDecreaseDecorator() {
long decreaseAmount = 1000L;
final String value = "2024-31-10";
final String timeformat = "%Y-%d-%m";
final int earliestType = DPLLexer.EARLIEST;
final Document doc = Assertions
.assertDoesNotThrow(() -> DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
TimeQualifierInterface tq = new DecreasedEpochValue(
new TimeQualifier(value, timeformat, earliestType, doc),
decreaseAmount
);
Element el = doc.createElement("earliest");
el.setAttribute("operation", "GE");
el.setAttribute("value", Long.toString(1730325600L - decreaseAmount));
Assertions.assertEquals(el.toString(), tq.xmlElement().toString());
}

@Test
public void testInvalidToken() {
final String value = "024-10-31:00:00:00";
Expand Down