Skip to content

Commit 7d6357d

Browse files
authored
Fix the bug of explicit makeNullLiteral for UDT fields (#4475)
Signed-off-by: Songkan Tang <songkant@amazon.com>
1 parent 4954cab commit 7d6357d

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/ExtendedRexBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public RexNode makeCast(
126126
// ImmutableList.of(exp, makeZeroLiteral(sourceType)));
127127
}
128128
} else if (OpenSearchTypeFactory.isUserDefinedType(type)) {
129+
if (RexLiteral.isNullLiteral(exp)) {
130+
return super.makeCast(pos, type, exp, matchNullability, safe, format);
131+
}
129132
var udt = ((AbstractExprRelDataType<?>) type).getUdt();
130133
var argExprType = OpenSearchTypeFactory.convertRelDataTypeToExprType(sourceType);
131134
return switch (udt) {

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLAppendCommandIT.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT;
99
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
10+
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_WEBLOGS;
1011
import static org.opensearch.sql.util.MatcherUtils.rows;
1112
import static org.opensearch.sql.util.MatcherUtils.schema;
1213
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;
@@ -28,6 +29,7 @@ public void init() throws Exception {
2829
enableCalcite();
2930
loadIndex(Index.ACCOUNT);
3031
loadIndex(Index.BANK);
32+
loadIndex(Index.WEBLOG);
3133
}
3234

3335
@Test
@@ -236,4 +238,39 @@ public void testAppendWithConflictTypeColumn() throws IOException {
236238
rows(null, null, "NM", 412d),
237239
rows(null, null, "AZ", 414d));
238240
}
241+
242+
@Test
243+
public void testAppendSchemaMergeWithTimestampUDT() throws IOException {
244+
JSONObject actual =
245+
executeQuery(
246+
String.format(
247+
Locale.ROOT,
248+
"source=%s | fields account_number, age | append [ source=%s | fields"
249+
+ " account_number, age, birthdate ] | where isnotnull(birthdate) and"
250+
+ " account_number > 30",
251+
TEST_INDEX_ACCOUNT,
252+
TEST_INDEX_BANK));
253+
verifySchemaInOrder(
254+
actual,
255+
schema("account_number", "bigint"),
256+
schema("age", "bigint"),
257+
schema("age0", "int"),
258+
schema("birthdate", "string"));
259+
verifyDataRows(actual, rows(32, null, 34, "2018-08-11 00:00:00"));
260+
}
261+
262+
@Test
263+
public void testAppendSchemaMergeWithIpUDT() throws IOException {
264+
JSONObject actual =
265+
executeQuery(
266+
String.format(
267+
Locale.ROOT,
268+
"source=%s | fields account_number, age | append [ source=%s | fields host ] |"
269+
+ " where cidrmatch(host, '0.0.0.0/24')",
270+
TEST_INDEX_ACCOUNT,
271+
TEST_INDEX_WEBLOGS));
272+
verifySchemaInOrder(
273+
actual, schema("account_number", "bigint"), schema("age", "bigint"), schema("host", "ip"));
274+
verifyDataRows(actual, rows(null, null, "0.0.0.2"));
275+
}
239276
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
setup:
2+
- do:
3+
query.settings:
4+
body:
5+
transient:
6+
plugins.calcite.enabled : true
7+
8+
---
9+
teardown:
10+
- do:
11+
query.settings:
12+
body:
13+
transient:
14+
plugins.calcite.enabled : false
15+
16+
---
17+
"Append UDT fields should merge schema successfully":
18+
- skip:
19+
features:
20+
- headers
21+
- allowed_warnings
22+
- do:
23+
indices.create:
24+
index: log-test1
25+
body:
26+
mappings:
27+
properties:
28+
"timestamp":
29+
type: date
30+
- do:
31+
indices.create:
32+
index: log-test2
33+
body:
34+
mappings:
35+
properties:
36+
"host":
37+
type: ip
38+
39+
- do:
40+
bulk:
41+
index: log-test1
42+
refresh: true
43+
body:
44+
- '{"index": {}}'
45+
- '{ "timestamp" : "2025-09-04T16:15:00.000Z" }'
46+
- do:
47+
bulk:
48+
index: log-test2
49+
refresh: true
50+
body:
51+
- '{"index": {}}'
52+
- '{ "host" : "0.0.0.2" }'
53+
54+
- do:
55+
headers:
56+
Content-Type: 'application/json'
57+
ppl:
58+
body:
59+
query: source=log-test1 | append [ source=log-test2 ]
60+
61+
- match: { total: 2 }
62+
- match: { datarows: [["2025-09-04 16:15:00", null], [null, "0.0.0.2"]] }

0 commit comments

Comments
 (0)