Skip to content

Commit

Permalink
Strip quotes of eval field names, add test for quoted fields in evalT…
Browse files Browse the repository at this point in the history
…est, add test for rename with single and double quotes (#430)
  • Loading branch information
eemhu authored Dec 3, 2024
1 parent eb9114a commit fa56c50
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/teragrep/pth10/steps/eval/EvalStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
*/
package com.teragrep.pth10.steps.eval;

import com.teragrep.pth10.ast.TextString;
import com.teragrep.pth10.ast.UnquotedText;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.functions;
Expand Down Expand Up @@ -78,7 +80,7 @@ public Dataset<Row> get(Dataset<Row> dataset) {
}

// perform eval and long->ts
ds = ds.withColumn(leftSide, rightSide);
ds = ds.withColumn(new UnquotedText(new TextString(leftSide)).read(), rightSide);
if (timeColumnExists) {
ds = ds.withColumn("_time", functions.from_unixtime(functions.col("_time")).cast(DataTypes.TimestampType));
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/com/teragrep/pth10/RenameTransformationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,35 @@ public void testRenameMultipleFields() {
}
);
}

@Test
@DisabledIfSystemProperty(
named = "skipSparkTest",
matches = "true"
)
public void rename_DoubleQuotes_test() {
streamingTestUtil
.performDPLTest(
"| makeresults count=1 | eval \"a\" = \"something\" | rename \"a\" as \"b\"", testFile, ds -> {
Assertions
.assertEquals("[_time, b]", Arrays.toString(ds.columns()), "Batch handler dataset contained an unexpected column arrangement !");
}
);
}

@Test
@DisabledIfSystemProperty(
named = "skipSparkTest",
matches = "true"
)
public void rename_SingleQuotes_test() {
streamingTestUtil
.performDPLTest(
"| makeresults count=1 | eval 'abc(def)' = \"xyz\" | rename 'abc(def)' as '(foo)bar'", testFile,
ds -> {
Assertions
.assertEquals("[_time, (foo)bar]", Arrays.toString(ds.columns()), "Batch handler dataset contained an unexpected column arrangement !");
}
);
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/teragrep/pth10/evalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3589,4 +3589,40 @@ public void evalAfterSpath_ComparisonTest() {
Assertions.assertEquals(expected, a);
});
}

@Test
@DisabledIfSystemProperty(
named = "skipSparkTest",
matches = "true"
)
public void evalFieldWithDoubleQuotes() {
String query = "index=index_A | eval \"quotedField\" = \"string\"";
String schema = "StructType(StructField(_raw,StringType,true),StructField(_time,TimestampType,true),StructField(host,StringType,true),StructField(id,LongType,true),StructField(index,StringType,true),StructField(offset,LongType,true),StructField(partition,StringType,true),StructField(source,StringType,true),StructField(sourcetype,StringType,true),StructField(quotedField,StringType,false))";
String testFile = "src/test/resources/spath/spathTransformationTest_numeric2*.jsonl";

streamingTestUtil.performDPLTest(query, testFile, ds -> {
Assertions.assertEquals(schema, ds.schema().toString());
Row r = ds.select("quotedField").distinct().first();

Assertions.assertEquals("string", r.getAs(0));
});
}

@Test
@DisabledIfSystemProperty(
named = "skipSparkTest",
matches = "true"
)
public void evalFieldWithSingleQuotes() {
String query = "index=index_A | eval 'quotedField' = \"string\"";
String schema = "StructType(StructField(_raw,StringType,true),StructField(_time,TimestampType,true),StructField(host,StringType,true),StructField(id,LongType,true),StructField(index,StringType,true),StructField(offset,LongType,true),StructField(partition,StringType,true),StructField(source,StringType,true),StructField(sourcetype,StringType,true),StructField(quotedField,StringType,false))";
String testFile = "src/test/resources/spath/spathTransformationTest_numeric2*.jsonl";

streamingTestUtil.performDPLTest(query, testFile, ds -> {
Assertions.assertEquals(schema, ds.schema().toString());
Row r = ds.select("quotedField").distinct().first();

Assertions.assertEquals("string", r.getAs(0));
});
}
}

0 comments on commit fa56c50

Please sign in to comment.