Skip to content

Commit

Permalink
Support OpenSearch ip field type
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoyuki Morita <[email protected]>
  • Loading branch information
ykmr1224 committed Feb 11, 2025
1 parent 1a6b9f3 commit 0174ca5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/opensearch-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ The following table defines the data type mapping between OpenSearch index field
| date(Date) | DateType |
| keyword | StringType, VarcharType, CharType |
| text | StringType(meta(osType)=text) |
| ip | StringType(meta(osType)=ip) |
| object | StructType |
| alias | Inherits referenced field type |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ object FlintDataType {
// binary types
case JString("binary") => BinaryType

// ip type
case JString("ip") =>
metadataBuilder.putString("osType", "ip")
StringType

// not supported
case unknown => throw new IllegalStateException(s"unsupported data type: $unknown")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,27 @@ class OpenSearchTableQueryITSuite extends OpenSearchCatalogSuite with FlintPPLSu
checkAnswer(df, Row(1, 1))
}
}

test("Query index with ip data type") {
val index1 = "t0001"
withIndexName(index1) {
indexWithIp(index1)

var df = spark.sql(s"""SELECT client, server FROM ${catalogName}.default.$index1""")
val ip0 = "192.168.0.10"
val ip1 = "192.168.0.11"
val ip2 = "100.10.12.123"
val ip3 = "2001:db8:3333:4444:5555:6666:7777:8888"
val ip4 = "2001:db8::1234:5678"
checkAnswer(df, Seq(Row(ip0, ip2), Row(ip1, ip2), Row(ip3, ip4)))

df = spark.sql(
s"""SELECT client, server FROM ${catalogName}.default.$index1 WHERE client = '192.168.0.10'""")
checkAnswer(df, Seq(Row(ip0, ip2)))

df = spark.sql(
s"""SELECT client, server FROM ${catalogName}.default.$index1 WHERE server = '2001:db8::1234:5678'""")
checkAnswer(df, Seq(Row(ip3, ip4)))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ trait OpenSearchSuite extends BeforeAndAfterAll {
index(indexName, oneNodeSetting, mappings, docs)
}

def indexWithIp(indexName: String): Unit = {
val mappings = """{
| "properties": {
| "client": {
| "type": "ip"
| },
| "server": {
| "type": "ip"
| }
| }
|}""".stripMargin
val docs = Seq(
"""{
| "client": "192.168.0.10",
| "server": "100.10.12.123"
|}""".stripMargin,
"""{
| "client": "192.168.0.11",
| "server": "100.10.12.123"
|}""".stripMargin,
"""{
| "client": "2001:db8:3333:4444:5555:6666:7777:8888",
| "server": "2001:db8::1234:5678"
|}""".stripMargin)
index(indexName, oneNodeSetting, mappings, docs)
}

def index(index: String, settings: String, mappings: String, docs: Seq[String]): Unit = {
openSearchClient.indices.create(
new CreateIndexRequest(index)
Expand Down

0 comments on commit 0174ca5

Please sign in to comment.