Skip to content
Merged
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 @@ -8,6 +8,7 @@
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_WITH_NULL_VALUES;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_CALCS;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATATYPE_NUMERIC;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DATE_FORMATS;
import static org.opensearch.sql.util.MatcherUtils.assertJsonEquals;
import static org.opensearch.sql.util.MatcherUtils.rows;
Expand Down Expand Up @@ -37,6 +38,7 @@ public void init() throws Exception {
loadIndex(Index.BANK_WITH_NULL_VALUES);
loadIndex(Index.CALCS);
loadIndex(Index.DATE_FORMATS);
loadIndex(Index.DATA_TYPE_NUMERIC);
}

@Test
Expand Down Expand Up @@ -806,4 +808,14 @@ public void testAggWithFunction() throws IOException {
schema("gender", null, "string"));
verifyDataRows(response, rows(121764, 1, "F"), rows(65909, 1, "M"));
}

@Test
public void testAggByByteNumberWithScript() throws IOException {
JSONObject response =
executeQuery(
String.format(
"source=%s | eval a = abs(byte_number) | stats count() by a",
TEST_INDEX_DATATYPE_NUMERIC));
verifyDataRows(response, rows(1, 4));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public interface Content {
/** Is double value. */
boolean isDouble();

/** Is short value. */
boolean isShort();

/** Is byte value. */
boolean isByte();

/** Is int value. */
boolean isInt();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public boolean isNumber() {
return value instanceof Number;
}

@Override
public boolean isShort() {
return value instanceof Short;
}

@Override
public boolean isByte() {
return value instanceof Byte;
}

@Override
public boolean isInt() {
return value instanceof Integer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ public boolean isNumber() {
return value().isNumber();
}

@Override
public boolean isShort() {
return value.isShort();
}

@Override
public boolean isByte() {
return false;
}

@Override
public boolean isInt() {
return value.isInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ private ExprValue parseContent(Content content) {
if (content.isNumber()) {
if (content.isInt()) {
return new ExprIntegerValue(content.intValue());
} else if (content.isShort()) {
return new ExprShortValue(content.shortValue());
} else if (content.isByte()) {
return new ExprByteValue(content.byteValue());
} else if (content.isLong()) {
return new ExprLongValue(content.longValue());
} else if (content.isFloat()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,7 @@ private NamedFieldExpression(RexLiteral literal) {
this.type = null;
}

String getRootName() {
public String getRootName() {
return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package org.opensearch.sql.opensearch.storage.script;

import static org.opensearch.sql.data.type.ExprCoreType.BYTE;
import static org.opensearch.sql.data.type.ExprCoreType.FLOAT;
import static org.opensearch.sql.data.type.ExprCoreType.INTEGER;
import static org.opensearch.sql.data.type.ExprCoreType.SHORT;
Expand Down Expand Up @@ -211,6 +212,7 @@ private Expression tryConvertDocValue(Expression docValueExpr, ExprType exprType
switch (type) {
case INTEGER:
case SHORT:
case BYTE:
docValue = EnumUtils.convert(docValueExpr, Long.class);
break;
case FLOAT:
Expand Down Expand Up @@ -360,7 +362,7 @@ private static Pair<String, ExprType> getValidatedReferenceNameAndType(
String referenceField = expression.getReferenceForTermQuery();
if (StringUtils.isEmpty(referenceField)) {
throw new UnsupportedScriptException(
"Field name cannot be empty for expression: " + expression);
"Field name cannot be empty for expression: " + expression.getRootName());
}
return Pair.of(referenceField, exprType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ public String serialize(
* @return map of RexNode, RelDataType and OpenSearch field types
*/
public Map<String, Object> deserialize(String struct) {
Map<String, Object> objectMap = null;
try {
// Recover Map object from bytes
ByteArrayInputStream input = new ByteArrayInputStream(Base64.getDecoder().decode(struct));
ObjectInputStream objectInput = new ObjectInputStream(input);
Map<String, Object> objectMap = (Map<String, Object>) objectInput.readObject();
objectMap = (Map<String, Object>) objectInput.readObject();

// PPL Expr types are all serializable
Map<String, ExprType> fieldTypes = (Map<String, ExprType>) objectMap.get(FIELD_TYPES);
Expand All @@ -127,8 +128,12 @@ public Map<String, Object> deserialize(String struct) {

return Map.of(EXPR, rexNode, FIELD_TYPES, fieldTypes, ROW_TYPE, rowType);
} catch (Exception e) {
if (objectMap == null) {
throw new IllegalStateException(
"Failed to deserialize RexNode due to object map is null", e);
}
throw new IllegalStateException(
"Failed to deserialize RexNode and its required structure: " + struct, e);
"Failed to deserialize RexNode and its required structure: " + objectMap.get(EXPR), e);
}
}
}
Loading