Typed Before the First Row: Elasticsearch SQL Results That Follow Your Mapping

SoftClient4ES 0.25.0 types every SQL result from your Elasticsearch mappings before the first row: no search at prepare, and the schema you get is the schema the rows arrive in.

  • Elasticsearch
  • SQL
  • Arrow Flight SQL
  • BI

SoftClient4ES 0.25.0 is out. One idea runs through the whole release: every result takes its column types from your Elasticsearch mappings, before a single row is read, whichever way you connect. It matters most to the data engineer moving Elasticsearch data into Python or DuckDB, the BI user behind DBeaver, Superset or Grafana, and the platform team that runs read-only service accounts.

A column’s type should not depend on who wrote the first document

Elasticsearch stores each document as its client wrote it. A date is 2024/01/31 in one index and epoch seconds in another. A number arrives as "5". A field meant to hold one value holds three. A SQL layer has to turn that into typed columns, and until this release ours looked at the values to decide. To learn a query’s types, the Flight SQL server ran it once with LIMIT 100 and inferred them from the rows; the JDBC driver typed each column from its first non-null value.

That had three costs:

  • Types depended on the data. Over Flight SQL, a column that was NULL in every sampled row became text. Over the JDBC driver 0.3.4, an integer field whose first value was 3000000000 (a value Elasticsearch had ignored as malformed) was reported as BIGINT, and a long field whose first value was 5 as INT.
  • Dates could arrive as strings. GROUP BY keys over a yyyy/MM/dd or epoch_second date came back as text.
  • Every prepare over Flight SQL searched your cluster. Asking for a statement’s schema ran the statement.

The mapping already knows

From 0.25.0, the engine computes one schema per statement before any row, from the mapping, or from _field_caps for an account that may not read the mapping. The schema covers plain queries, aggregations, UNION ALL, JOINs, UNION / INTERSECT / EXCEPT, CTEs and derived tables. Each value is then read by its column’s declared type: a date by its mapping’s own format, a number as Elasticsearch indexed it (127.9 in a byte field reads 127), binary as bytes, a geo point as {lat, lon}.

This lives in the Apache-2.0 core, as a public API (see The result schema). Arrow Flight SQL, ADBC, the JDBC driver and materialized views only translate it. None of them guesses a type the mapping declares.

Two places where this matters on real clusters:

  • Index patterns that drifted. Say code is a keyword holding "007" in logs-2026.10 and a long holding 7 in logs-2026.11. SELECT code FROM logs-* answers one VARCHAR column: "007" and "7". That is DuckDB’s rule for types that disagree, the same rule set operations use.
  • Read-only service accounts. A BI account with only the index read privilege is now typed from _field_caps, and its SELECT without a LIMIT answers. On 0.24.0 it failed with “Failed to get ES version”.

Ask for the schema without running the query

For the Python and DuckDB reader, here is the whole check, with the standard Arrow Flight SQL ADBC driver against the SoftClient4ES Flight SQL server:

import adbc_driver_flightsql.dbapi as flight_sql
conn = flight_sql.connect("grpc://localhost:32010")
cur = conn.cursor()
sql = ("SELECT o.id, o.amount, o.created_at, c.name FROM orders o "
"JOIN customers c ON o.customer_id = c.id WHERE o.amount > 50")
declared = cur.adbc_execute_schema(sql) # typed; no search runs on Elasticsearch
cur.execute(sql)
table = cur.fetch_arrow_table() # the rows arrive in that schema

declared and table.schema are equal: id: int32, amount: double, created_at: timestamp[ms], name: string, the types the mapping declares (integer, double, date, keyword). A JOIN, a set operation or a CTE now has its schema at prepare; it used to be empty. A NULL in one branch of a UNION takes the type of the other branches. A prepared JOIN with a ? standing for a value is typed at prepare too.

Preparing a statement now runs zero searches, where earlier releases ran a probe. A 50-column prepare went from 6.7 ms to 0.14 ms (Elasticsearch 8.18.3, medians). Typing a flat JOIN at prepare costs 1.04 to 1.54 ms (warm medians; 2.3 to 3.7 ms with a cold schema cache), work the earlier releases did not do at all.

Wrong answers that used to look right

Where a “before” is given below, it was measured on the previous release.

  • SUM over whole numbers is a BIGINT, on statements Elasticsearch runs. In a UNION ALL, a sum of 6,000,000,000 used to wrap to 1705032704 on 0.24.0. It now answers 6000000000. Over a JOIN, the relational engine’s own type applies.
  • A value Elasticsearch ignored is NULL. Take an integer field with ignore_malformed: true. A 3000000000 used to reach the column as a Long, and getInt returned -1294967296 with no error. A "x" in a long failed the getter with For input string: "x". Both now read NULL, as Elasticsearch decided (Elasticsearch 7.17 and later). A number Elasticsearch did index but that is too big for the column’s type (in an object sub-field, or in a column declared INT over a long) now fails with Value 3000000000 is out of range for INT instead of wrapping.
  • Types that do not mix are refused, not guessed. Over Arrow Flight SQL and ADBC, a UNION ALL of a long with a date showed the long 7 as the date 1970-01-01. It is now refused before it runs, as DuckDB refuses it.
  • Several values in a one-value column fail with their remedy. The message names the column, the document, and the ALTER TABLE … ALTER COLUMN … SET DATA TYPE ARRAY<…> that declares the column a list. That ALTER changes the mapping’s metadata only and runs in place, with no reindex. See Columns holding several values.

Lighter on the cluster and the client

  • No probe search at prepare, as above.
  • Computed-only SELECTs skip _source. SELECT n + 1 AS v FROM t no longer asks Elasticsearch for each document’s _source, only for the computed value. A scroll over 20,000 documents of about 2 KB took 72.3 ms instead of 139.3 ms (warm JIT, against the build just before the change).
  • Reading is cheaper on the client. Reading a row of 12 columns, 3 of them dates, from a recorded response costs 1.4 µs instead of 11.7 µs (the reading step alone, warm JIT, against the build just before the change). Building the readers costs about 22 µs, once per statement.

JDBC: metadata before execution

The JDBC driver 0.3.5 reports the engine’s declared schema in ResultSetMetaData: a byte field is a TINYINT, a column no row holds keeps its type, and getObject answers the class getColumnClassName names. PreparedStatement.getMetaData() now answers before the statement runs, for a JOIN too; it used to answer null. A tool that reads the columns at prepare gets the declared ones.

Materialized views keep the types too

A materialized view that joins two indices now stores the types its query declares, per table, so DESCRIBE MATERIALIZED VIEW and the drivers report them. Views are an extension under the Elastic License 2.0, free to use. A view created before this release keeps its earlier types until you drop it and create it again; an unchanged CREATE OR REPLACE does not re-type it.

What stays as stored, on purpose

  • Columns with no SQL type (flattened, enabled: false objects, an unmapped key you name) arrive as text over Arrow Flight SQL and ADBC; the JDBC driver types them from their first non-null value.
  • A date whose format names a time zone (yyyy-MM-dd HH:mm z) stays VARCHAR, as stored.
  • An account that may not read the mapping reads ISO dates as TIMESTAMP. A custom-format or epoch date fails with an error naming the remedy, unless the index was created or altered through SoftClient4ES 0.25.0, which records the format: that record is read over one index, on Elasticsearch 7.6 and later, not over several indices.
  • The NULL rule for ignored values covers top-level fields, on Elasticsearch 7.17 and later.

The full list is in Known limitations.

Before you upgrade

  • SUM over whole numbers, COUNT and DATEDIFF are BIGINT; ROUND takes its operand’s type; REAL is a 32-bit float.
  • Dates are dates, including keys that came back as text: java.time values in the Scala API, java.sql.Date / java.sql.Timestamp from JDBC getObject (the JDBC driver 0.3.4 answered java.time values, such as java.time.ZonedDateTime), Arrow dates and timestamps over Flight SQL and ADBC.
  • Each column takes its mapping’s width: a long is BIGINT (Long, Arrow int64) even when its values fit in 32 bits, where the JDBC driver 0.3.4 could report INT.
  • Over Arrow Flight SQL and ADBC, timestamps carry milliseconds. A date_nanos column read through the relational engine (a JOIN, for instance) is now a timestamp at millisecond precision (…00.123456789Z reads …00.123), where the Flight SQL server 0.3.4 answered its text, nanoseconds included. A plain query already read it at millisecond precision; through the JDBC driver, a plain query keeps the nanoseconds.
  • SELECT * returns the mapping’s columns, in mapping order. A key that is stored but not mapped is not returned; name it in the query to read it.
  • Several values in a one-value column fail with the remedy; declare the column ARRAY<X> in place.
  • A set operation refuses the type pairs DuckDB refuses.
  • Scala API users rebuild: QueryRows and QueryStream carry the schema.
  • Two safety fixes come with the release. A failed ALTER TABLE migration never deletes the index: it ends unchanged, restored, or with the documents kept in the temporary index the error names. And a cached client is never shared between two identities, which affected the JDBC driver 0.3.4 and the JVM ADBC driver 0.3.4 with several users in one JVM.

Try it

  • The installer installs the latest release, 0.25.0: curl -fsSL https://raw.githubusercontent.com/SOFTNETWORK-APP/SoftClient4ES/main/install.sh | bash.
  • The Flight SQL server is the Docker image softnetwork/softclient4es8-arrow-flight-sql:0.3.5, on port 32010.
  • The JDBC driver and the JVM ADBC driver are 0.3.5: see Installation and ADBC. From Python, install the standard driver with pip install adbc-driver-flightsql pyarrow and connect to the Flight SQL server.
  • The core is Apache-2.0. The Flight SQL server, the JVM ADBC driver, the JDBC driver and the views are under the Elastic License 2.0, free to use.

Read more in The result schema, Arrow Flight SQL, JDBC, Known limitations and the 0.25.0 release notes.

Upgrade, then prepare your trickiest query: the index with custom date formats, or the pattern whose mappings drifted. Compare the types your tool shows with your mapping. If one differs, tell us in GitHub Discussions.