row = rows.get(next++);
+ for (int i = 0; i < columns.size(); i++) {
+ TSDataType type = types.get(i);
+ Object value = CatalogRows.convert(row.get(columns.get(i)), type);
+ if (value == null) {
+ columnBuilders[i].appendNull();
+ continue;
+ }
+ switch (type) {
+ case INT64:
+ columnBuilders[i].writeLong((Long) value);
+ break;
+ case INT32:
+ columnBuilders[i].writeInt((Integer) value);
+ break;
+ case FLOAT:
+ columnBuilders[i].writeFloat((Float) value);
+ break;
+ case DOUBLE:
+ columnBuilders[i].writeDouble((Double) value);
+ break;
+ case BOOLEAN:
+ columnBuilders[i].writeBoolean((Boolean) value);
+ break;
+ default:
+ columnBuilders[i].writeBinary(BytesUtils.valueOf((String) value));
+ }
+ }
+ resultBuilder.declarePosition();
+ }
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/catalog/PgCatalogTables.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/catalog/PgCatalogTables.java
new file mode 100644
index 0000000000000..0031609ba0959
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/catalog/PgCatalogTables.java
@@ -0,0 +1,585 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.catalog;
+
+import org.apache.tsfile.enums.TSDataType;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The catalog tables registered in IoTDB's {@code information_schema}: PostgreSQL 14 column names,
+ * types mapped to the table model ({@code oid} and {@code regproc} → INT64, {@code name}/{@code
+ * char}/{@code text} and arrays → STRING, {@code bool} → BOOLEAN, {@code int2}/{@code int4} →
+ * INT32, {@code float4} → FLOAT).
+ *
+ * Only definitions, no other work: the core loads this class while it initialises its
+ * information schema, and a failure there would stop the database from starting.
+ */
+public final class PgCatalogTables {
+
+ /** A column: name and table-model type. */
+ public static final class Column {
+ private final String name;
+ private final TSDataType type;
+ private final boolean array;
+
+ Column(String name, TSDataType type, boolean array) {
+ this.name = name;
+ this.type = type;
+ this.array = array;
+ }
+
+ /**
+ * True for PostgreSQL array columns ({@code aclitem[]}, {@code oid[]}, {@code int2vector},
+ * ...): STRING here and always NULL, because the table model has no arrays.
+ */
+ public boolean array() {
+ return array;
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public TSDataType type() {
+ return type;
+ }
+ }
+
+ private static final TSDataType OID = TSDataType.INT64;
+ private static final TSDataType REGPROC = TSDataType.INT64;
+ private static final TSDataType NAME = TSDataType.STRING;
+ private static final TSDataType TEXT = TSDataType.STRING;
+ private static final TSDataType CHAR = TSDataType.STRING;
+ private static final TSDataType BOOL = TSDataType.BOOLEAN;
+ private static final TSDataType INT2 = TSDataType.INT32;
+ private static final TSDataType INT4 = TSDataType.INT32;
+ private static final TSDataType FLOAT4 = TSDataType.FLOAT;
+
+ /** Prefix of PostgreSQL's own {@code information_schema} views. */
+ public static final String INFORMATION_SCHEMA_PREFIX = "pg_is_";
+
+ private static final Map> TABLES = new LinkedHashMap<>();
+
+ static {
+ table("pg_namespace").col("oid", OID).col("nspname", NAME).col("nspowner", OID).array("nspacl");
+ table("pg_class")
+ .col("oid", OID)
+ .col("relname", NAME)
+ .col("relnamespace", OID)
+ .col("reltype", OID)
+ .col("reloftype", OID)
+ .col("relowner", OID)
+ .col("relam", OID)
+ .col("relfilenode", OID)
+ .col("reltablespace", OID)
+ .col("relpages", INT4)
+ .col("reltuples", FLOAT4)
+ .col("relallvisible", INT4)
+ .col("reltoastrelid", OID)
+ .col("relhasindex", BOOL)
+ .col("relisshared", BOOL)
+ .col("relpersistence", CHAR)
+ .col("relkind", CHAR)
+ .col("relnatts", INT2)
+ .col("relchecks", INT2)
+ .col("relhasrules", BOOL)
+ .col("relhastriggers", BOOL)
+ .col("relhassubclass", BOOL)
+ .col("relrowsecurity", BOOL)
+ .col("relforcerowsecurity", BOOL)
+ .col("relispopulated", BOOL)
+ .col("relreplident", CHAR)
+ .col("relispartition", BOOL)
+ .col("relrewrite", OID)
+ .array("relacl")
+ .array("reloptions")
+ .col("relpartbound", TEXT);
+ table("pg_attribute")
+ .col("attrelid", OID)
+ .col("attname", NAME)
+ .col("atttypid", OID)
+ .col("attstattarget", INT4)
+ .col("attlen", INT2)
+ .col("attnum", INT2)
+ .col("attndims", INT4)
+ .col("attcacheoff", INT4)
+ .col("atttypmod", INT4)
+ .col("attbyval", BOOL)
+ .col("attalign", CHAR)
+ .col("attstorage", CHAR)
+ .col("attcompression", CHAR)
+ .col("attnotnull", BOOL)
+ .col("atthasdef", BOOL)
+ .col("atthasmissing", BOOL)
+ .col("attidentity", CHAR)
+ .col("attgenerated", CHAR)
+ .col("attisdropped", BOOL)
+ .col("attislocal", BOOL)
+ .col("attinhcount", INT4)
+ .col("attcollation", OID)
+ .array("attacl")
+ .array("attoptions")
+ .array("attfdwoptions");
+ table("pg_type")
+ .col("oid", OID)
+ .col("typname", NAME)
+ .col("typnamespace", OID)
+ .col("typowner", OID)
+ .col("typlen", INT2)
+ .col("typbyval", BOOL)
+ .col("typtype", CHAR)
+ .col("typcategory", CHAR)
+ .col("typispreferred", BOOL)
+ .col("typisdefined", BOOL)
+ .col("typdelim", CHAR)
+ .col("typrelid", OID)
+ .col("typsubscript", REGPROC)
+ .col("typelem", OID)
+ .col("typarray", OID)
+ .col("typinput", REGPROC)
+ .col("typoutput", REGPROC)
+ .col("typreceive", REGPROC)
+ .col("typsend", REGPROC)
+ .col("typmodin", REGPROC)
+ .col("typmodout", REGPROC)
+ .col("typanalyze", REGPROC)
+ .col("typalign", CHAR)
+ .col("typstorage", CHAR)
+ .col("typnotnull", BOOL)
+ .col("typbasetype", OID)
+ .col("typtypmod", INT4)
+ .col("typndims", INT4)
+ .col("typcollation", OID)
+ .col("typdefaultbin", TEXT)
+ .col("typdefault", TEXT)
+ .array("typacl");
+ table("pg_proc")
+ .col("oid", OID)
+ .col("proname", NAME)
+ .col("pronamespace", OID)
+ .col("proowner", OID)
+ .col("prolang", OID)
+ .col("procost", FLOAT4)
+ .col("prorows", FLOAT4)
+ .col("provariadic", OID)
+ .col("prosupport", REGPROC)
+ .col("prokind", CHAR)
+ .col("prosecdef", BOOL)
+ .col("proleakproof", BOOL)
+ .col("proisstrict", BOOL)
+ .col("proretset", BOOL)
+ .col("provolatile", CHAR)
+ .col("proparallel", CHAR)
+ .col("pronargs", INT2)
+ .col("pronargdefaults", INT2)
+ .col("prorettype", OID)
+ .array("proargtypes")
+ .array("proallargtypes")
+ .array("proargmodes")
+ .array("proargnames")
+ .col("proargdefaults", TEXT)
+ .array("protrftypes")
+ .col("prosrc", TEXT)
+ .col("probin", TEXT)
+ .col("prosqlbody", TEXT)
+ .array("proconfig")
+ .array("proacl");
+ table("pg_description")
+ .col("objoid", OID)
+ .col("classoid", OID)
+ .col("objsubid", INT4)
+ .col("description", TEXT);
+ table("pg_range")
+ .col("rngtypid", OID)
+ .col("rngsubtype", OID)
+ .col("rngmultitypid", OID)
+ .col("rngcollation", OID)
+ .col("rngsubopc", OID)
+ .col("rngcanonical", REGPROC)
+ .col("rngsubdiff", REGPROC);
+ table("pg_enum")
+ .col("oid", OID)
+ .col("enumtypid", OID)
+ .col("enumsortorder", FLOAT4)
+ .col("enumlabel", NAME);
+ table("pg_index")
+ .col("indexrelid", OID)
+ .col("indrelid", OID)
+ .col("indnatts", INT2)
+ .col("indnkeyatts", INT2)
+ .col("indisunique", BOOL)
+ .col("indisprimary", BOOL)
+ .col("indisexclusion", BOOL)
+ .col("indimmediate", BOOL)
+ .col("indisclustered", BOOL)
+ .col("indisvalid", BOOL)
+ .col("indcheckxmin", BOOL)
+ .col("indisready", BOOL)
+ .col("indislive", BOOL)
+ .col("indisreplident", BOOL)
+ .array("indkey")
+ .array("indcollation")
+ .array("indclass")
+ .array("indoption")
+ .col("indexprs", TEXT)
+ .col("indpred", TEXT);
+ table("pg_constraint")
+ .col("oid", OID)
+ .col("conname", NAME)
+ .col("connamespace", OID)
+ .col("contype", CHAR)
+ .col("condeferrable", BOOL)
+ .col("condeferred", BOOL)
+ .col("convalidated", BOOL)
+ .col("conrelid", OID)
+ .col("contypid", OID)
+ .col("conindid", OID)
+ .col("conparentid", OID)
+ .col("confrelid", OID)
+ .col("confupdtype", CHAR)
+ .col("confdeltype", CHAR)
+ .col("confmatchtype", CHAR)
+ .col("conislocal", BOOL)
+ .col("coninhcount", INT4)
+ .col("connoinherit", BOOL)
+ .array("conkey")
+ .array("confkey")
+ .array("conpfeqop")
+ .array("conppeqop")
+ .array("conffeqop")
+ .array("conexclop")
+ .col("conbin", TEXT);
+ table("pg_inherits")
+ .col("inhrelid", OID)
+ .col("inhparent", OID)
+ .col("inhseqno", INT4)
+ .col("inhdetachpending", BOOL);
+ table("pg_attrdef").col("oid", OID).col("adrelid", OID).col("adnum", INT2).col("adbin", TEXT);
+ table("pg_am")
+ .col("oid", OID)
+ .col("amname", NAME)
+ .col("amhandler", REGPROC)
+ .col("amtype", CHAR);
+ table("pg_policy")
+ .col("oid", OID)
+ .col("polname", NAME)
+ .col("polrelid", OID)
+ .col("polcmd", CHAR)
+ .col("polpermissive", BOOL)
+ .array("polroles")
+ .col("polqual", TEXT)
+ .col("polwithcheck", TEXT);
+ table("pg_statistic_ext")
+ .col("oid", OID)
+ .col("stxrelid", OID)
+ .col("stxname", NAME)
+ .col("stxnamespace", OID)
+ .col("stxowner", OID)
+ .col("stxstattarget", INT4)
+ .array("stxkeys")
+ .array("stxkind")
+ .col("stxexprs", TEXT);
+ table("pg_publication")
+ .col("oid", OID)
+ .col("pubname", NAME)
+ .col("pubowner", OID)
+ .col("puballtables", BOOL)
+ .col("pubinsert", BOOL)
+ .col("pubupdate", BOOL)
+ .col("pubdelete", BOOL)
+ .col("pubtruncate", BOOL)
+ .col("pubviaroot", BOOL);
+ table("pg_publication_rel").col("oid", OID).col("prpubid", OID).col("prrelid", OID);
+ table("pg_collation")
+ .col("oid", OID)
+ .col("collname", NAME)
+ .col("collnamespace", OID)
+ .col("collowner", OID)
+ .col("collprovider", CHAR)
+ .col("collisdeterministic", BOOL)
+ .col("collencoding", INT4)
+ .col("collcollate", NAME)
+ .col("collctype", NAME)
+ .col("collversion", TEXT);
+ table("pg_database")
+ .col("oid", OID)
+ .col("datname", NAME)
+ .col("datdba", OID)
+ .col("encoding", INT4)
+ .col("datcollate", NAME)
+ .col("datctype", NAME)
+ .col("datistemplate", BOOL)
+ .col("datallowconn", BOOL)
+ .col("datconnlimit", INT4)
+ .col("datlastsysoid", OID)
+ .col("dattablespace", OID)
+ .array("datacl");
+ table("pg_roles")
+ .col("oid", OID)
+ .col("rolname", NAME)
+ .col("rolsuper", BOOL)
+ .col("rolinherit", BOOL)
+ .col("rolcreaterole", BOOL)
+ .col("rolcreatedb", BOOL)
+ .col("rolcanlogin", BOOL)
+ .col("rolreplication", BOOL)
+ .col("rolconnlimit", INT4)
+ .col("rolpassword", TEXT)
+ .col("rolvaliduntil", TEXT)
+ .col("rolbypassrls", BOOL)
+ .array("rolconfig");
+ table("pg_user")
+ .col("usename", NAME)
+ .col("usesysid", OID)
+ .col("usecreatedb", BOOL)
+ .col("usesuper", BOOL)
+ .col("userepl", BOOL)
+ .col("usebypassrls", BOOL)
+ .col("passwd", TEXT)
+ .col("valuntil", TEXT)
+ .array("useconfig");
+ table("pg_settings")
+ .col("name", TEXT)
+ .col("setting", TEXT)
+ .col("unit", TEXT)
+ .col("category", TEXT)
+ .col("short_desc", TEXT)
+ .col("extra_desc", TEXT)
+ .col("context", TEXT)
+ .col("vartype", TEXT)
+ .col("source", TEXT)
+ .col("min_val", TEXT)
+ .col("max_val", TEXT)
+ .array("enumvals")
+ .col("boot_val", TEXT)
+ .col("reset_val", TEXT)
+ .col("sourcefile", TEXT)
+ .col("sourceline", INT4)
+ .col("pending_restart", BOOL);
+
+ // PostgreSQL's information_schema views; sql_identifier, character_data and yes_or_no are
+ // text, cardinal_number is int4
+ table("pg_is_schemata")
+ .col("catalog_name", TEXT)
+ .col("schema_name", TEXT)
+ .col("schema_owner", TEXT)
+ .col("default_character_set_catalog", TEXT)
+ .col("default_character_set_schema", TEXT)
+ .col("default_character_set_name", TEXT)
+ .col("sql_path", TEXT);
+ table("pg_is_character_sets")
+ .col("character_set_catalog", TEXT)
+ .col("character_set_schema", TEXT)
+ .col("character_set_name", TEXT)
+ .col("character_repertoire", TEXT)
+ .col("form_of_use", TEXT)
+ .col("default_collate_catalog", TEXT)
+ .col("default_collate_schema", TEXT)
+ .col("default_collate_name", TEXT);
+ table("pg_is_tables")
+ .col("table_catalog", TEXT)
+ .col("table_schema", TEXT)
+ .col("table_name", TEXT)
+ .col("table_type", TEXT)
+ .col("self_referencing_column_name", TEXT)
+ .col("reference_generation", TEXT)
+ .col("user_defined_type_catalog", TEXT)
+ .col("user_defined_type_schema", TEXT)
+ .col("user_defined_type_name", TEXT)
+ .col("is_insertable_into", TEXT)
+ .col("is_typed", TEXT)
+ .col("commit_action", TEXT);
+ table("pg_is_columns")
+ .col("table_catalog", TEXT)
+ .col("table_schema", TEXT)
+ .col("table_name", TEXT)
+ .col("column_name", TEXT)
+ .col("ordinal_position", INT4)
+ .col("column_default", TEXT)
+ .col("is_nullable", TEXT)
+ .col("data_type", TEXT)
+ .col("character_maximum_length", INT4)
+ .col("character_octet_length", INT4)
+ .col("numeric_precision", INT4)
+ .col("numeric_precision_radix", INT4)
+ .col("numeric_scale", INT4)
+ .col("datetime_precision", INT4)
+ .col("interval_type", TEXT)
+ .col("interval_precision", INT4)
+ .col("character_set_catalog", TEXT)
+ .col("character_set_schema", TEXT)
+ .col("character_set_name", TEXT)
+ .col("collation_catalog", TEXT)
+ .col("collation_schema", TEXT)
+ .col("collation_name", TEXT)
+ .col("domain_catalog", TEXT)
+ .col("domain_schema", TEXT)
+ .col("domain_name", TEXT)
+ .col("udt_catalog", TEXT)
+ .col("udt_schema", TEXT)
+ .col("udt_name", TEXT)
+ .col("scope_catalog", TEXT)
+ .col("scope_schema", TEXT)
+ .col("scope_name", TEXT)
+ .col("maximum_cardinality", INT4)
+ .col("dtd_identifier", TEXT)
+ .col("is_self_referencing", TEXT)
+ .col("is_identity", TEXT)
+ .col("identity_generation", TEXT)
+ .col("identity_start", TEXT)
+ .col("identity_increment", TEXT)
+ .col("identity_maximum", TEXT)
+ .col("identity_minimum", TEXT)
+ .col("identity_cycle", TEXT)
+ .col("is_generated", TEXT)
+ .col("generation_expression", TEXT)
+ .col("is_updatable", TEXT);
+ table("pg_is_views")
+ .col("table_catalog", TEXT)
+ .col("table_schema", TEXT)
+ .col("table_name", TEXT)
+ .col("view_definition", TEXT)
+ .col("check_option", TEXT)
+ .col("is_updatable", TEXT)
+ .col("is_insertable_into", TEXT)
+ .col("is_trigger_updatable", TEXT)
+ .col("is_trigger_deletable", TEXT)
+ .col("is_trigger_insertable_into", TEXT);
+ table("pg_is_table_constraints")
+ .col("constraint_catalog", TEXT)
+ .col("constraint_schema", TEXT)
+ .col("constraint_name", TEXT)
+ .col("table_catalog", TEXT)
+ .col("table_schema", TEXT)
+ .col("table_name", TEXT)
+ .col("constraint_type", TEXT)
+ .col("is_deferrable", TEXT)
+ .col("initially_deferred", TEXT)
+ .col("enforced", TEXT);
+ table("pg_is_key_column_usage")
+ .col("constraint_catalog", TEXT)
+ .col("constraint_schema", TEXT)
+ .col("constraint_name", TEXT)
+ .col("table_catalog", TEXT)
+ .col("table_schema", TEXT)
+ .col("table_name", TEXT)
+ .col("column_name", TEXT)
+ .col("ordinal_position", INT4)
+ .col("position_in_unique_constraint", INT4);
+ table("pg_is_referential_constraints")
+ .col("constraint_catalog", TEXT)
+ .col("constraint_schema", TEXT)
+ .col("constraint_name", TEXT)
+ .col("unique_constraint_catalog", TEXT)
+ .col("unique_constraint_schema", TEXT)
+ .col("unique_constraint_name", TEXT)
+ .col("match_option", TEXT)
+ .col("update_rule", TEXT)
+ .col("delete_rule", TEXT);
+ }
+
+ /**
+ * Tables that never have rows: the table model has no ranges, enums, indexes, constraints,
+ * defaults, inheritance, policies, extended statistics or publications. A query over one of them
+ * returns no rows whatever it selects (the translator relies on this).
+ */
+ private static final java.util.Set ALWAYS_EMPTY =
+ new java.util.HashSet<>(
+ java.util.Arrays.asList(
+ "pg_range",
+ "pg_enum",
+ "pg_index",
+ "pg_constraint",
+ "pg_inherits",
+ "pg_attrdef",
+ "pg_policy",
+ "pg_statistic_ext",
+ "pg_publication",
+ "pg_publication_rel",
+ "pg_is_table_constraints",
+ "pg_is_key_column_usage",
+ "pg_is_referential_constraints"));
+
+ private PgCatalogTables() {}
+
+ /** True if {@code table} never has rows. */
+ public static boolean alwaysEmpty(String table) {
+ return ALWAYS_EMPTY.contains(table);
+ }
+
+ /** A column of a catalog table, or {@code null}. */
+ public static Column column(String table, String column) {
+ List columns = TABLES.get(table);
+ if (columns != null) {
+ for (Column c : columns) {
+ if (c.name().equals(column)) {
+ return c;
+ }
+ }
+ }
+ return null;
+ }
+
+ /** Table name → columns in order. */
+ public static Map> tables() {
+ return Collections.unmodifiableMap(TABLES);
+ }
+
+ /** True if {@code table} is one of the catalog tables. */
+ public static boolean contains(String table) {
+ return TABLES.containsKey(table);
+ }
+
+ /** The PostgreSQL name of a catalog table ({@code pg_is_tables} → {@code tables}). */
+ public static String postgresName(String table) {
+ return table.startsWith(INFORMATION_SCHEMA_PREFIX)
+ ? table.substring(INFORMATION_SCHEMA_PREFIX.length())
+ : table;
+ }
+
+ private static Builder table(String name) {
+ List columns = new ArrayList<>();
+ TABLES.put(name, columns);
+ return new Builder(columns);
+ }
+
+ private static final class Builder {
+ private final List columns;
+
+ Builder(List columns) {
+ this.columns = columns;
+ }
+
+ Builder col(String name, TSDataType type) {
+ columns.add(new Column(name, type, false));
+ return this;
+ }
+
+ Builder array(String name) {
+ columns.add(new Column(name, TSDataType.STRING, true));
+ return this;
+ }
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/catalog/StaticCatalog.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/catalog/StaticCatalog.java
new file mode 100644
index 0000000000000..4e58ca8b761f9
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/catalog/StaticCatalog.java
@@ -0,0 +1,922 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.catalog;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Constant catalog content: the types of the type mapping and the catalog types, their array types
+ * and their I/O functions as in PostgreSQL 14 (OIDs, names, sizes, alignment), access methods and
+ * settings. Npgsql 4.0 loads the types at connect time and classifies a type as an array by its
+ * receive function {@code array_recv}.
+ */
+public final class StaticCatalog {
+
+ /** OIDs of the two system namespaces. */
+ public static final long PG_CATALOG = 11;
+
+ public static final long INFORMATION_SCHEMA = 13000;
+
+ /** OID of the bootstrap superuser, the owner of every catalog object. */
+ public static final long OWNER = 10;
+
+ /** OID of {@code pg_class}, the {@code classoid} of table and column descriptions. */
+ public static final long PG_CLASS = 1259;
+
+ /** OID of the default collation, set for text columns. */
+ public static final long DEFAULT_COLLATION = 100;
+
+ private static final long ARRAY_IN = 750;
+ private static final long ARRAY_OUT = 751;
+ private static final long ARRAY_RECV = 2400;
+ private static final long ARRAY_SEND = 2401;
+ private static final long ARRAY_SUBSCRIPT = 6179;
+ private static final long RAW_ARRAY_SUBSCRIPT = 6180;
+ private static final long CSTRING = 2275;
+ private static final long BYTEA = 17;
+
+ /** A {@code pg_type} row. */
+ public static final class TypeDef {
+ final long oid;
+ final String name;
+ final int len;
+ final boolean byval;
+ final char typtype;
+ final char category;
+ final boolean preferred;
+ final char align;
+ final char storage;
+ final long elem;
+ final long array;
+ final long subscript;
+ final long[] io;
+ final long collation;
+
+ TypeDef(
+ long oid,
+ String name,
+ int len,
+ boolean byval,
+ char typtype,
+ char category,
+ boolean preferred,
+ char align,
+ char storage,
+ long elem,
+ long array,
+ long subscript,
+ long[] io,
+ long collation) {
+ this.oid = oid;
+ this.name = name;
+ this.len = len;
+ this.byval = byval;
+ this.typtype = typtype;
+ this.category = category;
+ this.preferred = preferred;
+ this.align = align;
+ this.storage = storage;
+ this.elem = elem;
+ this.array = array;
+ this.subscript = subscript;
+ this.io = io;
+ this.collation = collation;
+ }
+
+ public long oid() {
+ return oid;
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public int len() {
+ return len;
+ }
+
+ public boolean byval() {
+ return byval;
+ }
+
+ public char align() {
+ return align;
+ }
+
+ public char storage() {
+ return storage;
+ }
+
+ public long collation() {
+ return collation;
+ }
+ }
+
+ /** A system relation: a catalog table or view. */
+ static final class SystemRelation {
+ final long oid;
+ final String table;
+ final long namespace;
+ final String name;
+ final char kind;
+
+ SystemRelation(long oid, String table, long namespace, String name, char kind) {
+ this.oid = oid;
+ this.table = table;
+ this.namespace = namespace;
+ this.name = name;
+ this.kind = kind;
+ }
+ }
+
+ private static final Map PROCS = new LinkedHashMap<>();
+ private static final Map TYPES = new LinkedHashMap<>();
+ private static final Map PROC_RESULT = new HashMap<>();
+ private static final List RELATIONS = new ArrayList<>();
+
+ static {
+ // name, oid, len, byval, category, preferred, align, storage, array, collation,
+ // in, out, recv, send, typmodin, typmodout (0 = none) with the function names
+ base(
+ 16,
+ "bool",
+ 1,
+ true,
+ 'B',
+ true,
+ 'c',
+ 'p',
+ 1000,
+ 0,
+ f(1242, "boolin"),
+ f(1243, "boolout"),
+ f(2436, "boolrecv"),
+ f(2437, "boolsend"));
+ base(
+ 17,
+ "bytea",
+ -1,
+ false,
+ 'U',
+ false,
+ 'i',
+ 'x',
+ 1001,
+ 0,
+ f(1244, "byteain"),
+ f(31, "byteaout"),
+ f(2412, "bytearecv"),
+ f(2413, "byteasend"));
+ base(
+ 18,
+ "char",
+ 1,
+ true,
+ 'Z',
+ false,
+ 'c',
+ 'p',
+ 1002,
+ 0,
+ f(1245, "charin"),
+ f(33, "charout"),
+ f(2434, "charrecv"),
+ f(2435, "charsend"));
+ base(
+ 19,
+ "name",
+ 64,
+ false,
+ 'S',
+ false,
+ 'c',
+ 'p',
+ 1003,
+ 950,
+ f(34, "namein"),
+ f(35, "nameout"),
+ f(2422, "namerecv"),
+ f(2423, "namesend"));
+ base(
+ 20,
+ "int8",
+ 8,
+ true,
+ 'N',
+ false,
+ 'd',
+ 'p',
+ 1016,
+ 0,
+ f(460, "int8in"),
+ f(461, "int8out"),
+ f(2408, "int8recv"),
+ f(2409, "int8send"));
+ base(
+ 21,
+ "int2",
+ 2,
+ true,
+ 'N',
+ false,
+ 's',
+ 'p',
+ 1005,
+ 0,
+ f(38, "int2in"),
+ f(39, "int2out"),
+ f(2404, "int2recv"),
+ f(2405, "int2send"));
+ base(
+ 23,
+ "int4",
+ 4,
+ true,
+ 'N',
+ false,
+ 'i',
+ 'p',
+ 1007,
+ 0,
+ f(42, "int4in"),
+ f(43, "int4out"),
+ f(2406, "int4recv"),
+ f(2407, "int4send"));
+ base(
+ 24,
+ "regproc",
+ 4,
+ true,
+ 'N',
+ false,
+ 'i',
+ 'p',
+ 1008,
+ 0,
+ f(44, "regprocin"),
+ f(45, "regprocout"),
+ f(2444, "regprocrecv"),
+ f(2445, "regprocsend"));
+ base(
+ 25,
+ "text",
+ -1,
+ false,
+ 'S',
+ true,
+ 'i',
+ 'x',
+ 1009,
+ DEFAULT_COLLATION,
+ f(46, "textin"),
+ f(47, "textout"),
+ f(2414, "textrecv"),
+ f(2415, "textsend"));
+ base(
+ 26,
+ "oid",
+ 4,
+ true,
+ 'N',
+ true,
+ 'i',
+ 'p',
+ 1028,
+ 0,
+ f(1798, "oidin"),
+ f(1799, "oidout"),
+ f(2418, "oidrecv"),
+ f(2419, "oidsend"));
+ base(
+ 700,
+ "float4",
+ 4,
+ true,
+ 'N',
+ false,
+ 'i',
+ 'p',
+ 1021,
+ 0,
+ f(200, "float4in"),
+ f(201, "float4out"),
+ f(2424, "float4recv"),
+ f(2425, "float4send"));
+ base(
+ 701,
+ "float8",
+ 8,
+ true,
+ 'N',
+ true,
+ 'd',
+ 'p',
+ 1022,
+ 0,
+ f(214, "float8in"),
+ f(215, "float8out"),
+ f(2426, "float8recv"),
+ f(2427, "float8send"));
+ base(
+ 1043,
+ "varchar",
+ -1,
+ false,
+ 'S',
+ false,
+ 'i',
+ 'x',
+ 1015,
+ DEFAULT_COLLATION,
+ f(1046, "varcharin"),
+ f(1047, "varcharout"),
+ f(2432, "varcharrecv"),
+ f(2433, "varcharsend"),
+ f(2915, "varchartypmodin"),
+ f(2916, "varchartypmodout"));
+ base(
+ 1082,
+ "date",
+ 4,
+ true,
+ 'D',
+ false,
+ 'i',
+ 'p',
+ 1182,
+ 0,
+ f(1084, "date_in"),
+ f(1085, "date_out"),
+ f(2468, "date_recv"),
+ f(2469, "date_send"));
+ base(
+ 1114,
+ "timestamp",
+ 8,
+ true,
+ 'D',
+ false,
+ 'd',
+ 'p',
+ 1115,
+ 0,
+ f(1312, "timestamp_in"),
+ f(1313, "timestamp_out"),
+ f(2474, "timestamp_recv"),
+ f(2475, "timestamp_send"),
+ f(2905, "timestamptypmodin"),
+ f(2906, "timestamptypmodout"));
+ base(
+ 1184,
+ "timestamptz",
+ 8,
+ true,
+ 'D',
+ true,
+ 'd',
+ 'p',
+ 1185,
+ 0,
+ f(1150, "timestamptz_in"),
+ f(1151, "timestamptz_out"),
+ f(2476, "timestamptz_recv"),
+ f(2477, "timestamptz_send"),
+ f(2907, "timestamptztypmodin"),
+ f(2908, "timestamptztypmodout"));
+ base(
+ 1700,
+ "numeric",
+ -1,
+ false,
+ 'N',
+ false,
+ 'i',
+ 'm',
+ 1231,
+ 0,
+ f(1701, "numeric_in"),
+ f(1702, "numeric_out"),
+ f(2460, "numeric_recv"),
+ f(2461, "numeric_send"),
+ f(2917, "numerictypmodin"),
+ f(2918, "numerictypmodout"));
+ base(
+ 2206,
+ "regtype",
+ 4,
+ true,
+ 'N',
+ false,
+ 'i',
+ 'p',
+ 2211,
+ 0,
+ f(2220, "regtypein"),
+ f(2221, "regtypeout"),
+ f(2454, "regtyperecv"),
+ f(2455, "regtypesend"));
+ pseudo(
+ 705,
+ "unknown",
+ -2,
+ false,
+ 'X',
+ 'c',
+ 'p',
+ 0,
+ f(109, "unknownin"),
+ f(110, "unknownout"),
+ f(2416, "unknownrecv"),
+ f(2417, "unknownsend"));
+ pseudo(
+ 2249,
+ "record",
+ -1,
+ false,
+ 'P',
+ 'd',
+ 'x',
+ 2287,
+ f(2290, "record_in"),
+ f(2291, "record_out"),
+ f(2402, "record_recv"),
+ f(2403, "record_send"));
+ pseudo(
+ 2278,
+ "void",
+ 4,
+ true,
+ 'P',
+ 'i',
+ 'p',
+ 0,
+ f(2298, "void_in"),
+ f(2299, "void_out"),
+ f(3120, "void_recv"),
+ f(3121, "void_send"));
+ PROCS.put(ARRAY_IN, "array_in");
+ PROCS.put(ARRAY_OUT, "array_out");
+ PROCS.put(ARRAY_RECV, "array_recv");
+ PROCS.put(ARRAY_SEND, "array_send");
+ PROCS.put(ARRAY_SUBSCRIPT, "array_subscript_handler");
+ PROCS.put(RAW_ARRAY_SUBSCRIPT, "raw_array_subscript_handler");
+ PROC_RESULT.put(ARRAY_OUT, CSTRING);
+ PROC_RESULT.put(ARRAY_SEND, BYTEA);
+ // the array types, after their element types
+ for (TypeDef element : new ArrayList<>(TYPES.values())) {
+ if (element.array != 0) {
+ TYPES.put(
+ element.array,
+ new TypeDef(
+ element.array,
+ "_" + element.name,
+ -1,
+ false,
+ 'b',
+ 'A',
+ false,
+ element.align == 'd' ? 'd' : 'i',
+ 'x',
+ element.oid,
+ 0,
+ ARRAY_SUBSCRIPT,
+ new long[] {ARRAY_IN, ARRAY_OUT, ARRAY_RECV, ARRAY_SEND, 0, 0},
+ element.collation));
+ }
+ }
+
+ // catalog relations with the OIDs PostgreSQL 14 gives them; the others get fixed OIDs
+ // below the first user OID, in the (append-only) order of PgCatalogTables
+ Map known = new HashMap<>();
+ known.put("pg_type", 1247L);
+ known.put("pg_attribute", 1249L);
+ known.put("pg_proc", 1255L);
+ known.put("pg_class", PG_CLASS);
+ known.put("pg_database", 1262L);
+ known.put("pg_am", 2601L);
+ known.put("pg_attrdef", 2604L);
+ known.put("pg_constraint", 2606L);
+ known.put("pg_description", 2609L);
+ known.put("pg_index", 2610L);
+ known.put("pg_inherits", 2611L);
+ known.put("pg_namespace", 2615L);
+ known.put("pg_enum", 3501L);
+ known.put("pg_collation", 3456L);
+ known.put("pg_range", 3541L);
+ long next = 12100;
+ for (String table : PgCatalogTables.tables().keySet()) {
+ Long oid = known.get(table);
+ if (oid == null) {
+ oid = next++;
+ }
+ boolean view =
+ table.startsWith(PgCatalogTables.INFORMATION_SCHEMA_PREFIX)
+ || table.equals("pg_roles")
+ || table.equals("pg_user")
+ || table.equals("pg_settings");
+ RELATIONS.add(
+ new SystemRelation(
+ oid,
+ table,
+ table.startsWith(PgCatalogTables.INFORMATION_SCHEMA_PREFIX)
+ ? INFORMATION_SCHEMA
+ : PG_CATALOG,
+ PgCatalogTables.postgresName(table),
+ view ? 'v' : 'r'));
+ }
+ }
+
+ private StaticCatalog() {}
+
+ /** The type with this OID, if it is in the catalog. */
+ public static Optional type(long oid) {
+ return Optional.ofNullable(TYPES.get(oid));
+ }
+
+ /** SQL names of types as {@code format_type} prints them, where they differ from typname. */
+ private static final Map SQL_NAMES = new HashMap<>();
+
+ /** Names a type literal may use for a type ({@code 'integer'::regtype}). */
+ private static final Map ALIASES = new HashMap<>();
+
+ static {
+ SQL_NAMES.put("bool", "boolean");
+ SQL_NAMES.put("char", "\"char\"");
+ SQL_NAMES.put("int8", "bigint");
+ SQL_NAMES.put("int2", "smallint");
+ SQL_NAMES.put("int4", "integer");
+ SQL_NAMES.put("float4", "real");
+ SQL_NAMES.put("float8", "double precision");
+ SQL_NAMES.put("varchar", "character varying");
+ SQL_NAMES.put("timestamp", "timestamp without time zone");
+ SQL_NAMES.put("timestamptz", "timestamp with time zone");
+ for (Map.Entry name : SQL_NAMES.entrySet()) {
+ ALIASES.put(name.getValue(), name.getKey());
+ }
+ ALIASES.put("\"char\"", "char");
+ ALIASES.put("int", "int4");
+ ALIASES.put("float", "float8");
+ ALIASES.put("character varying", "varchar");
+ ALIASES.put("timestamp with time zone", "timestamptz");
+ }
+
+ /**
+ * {@code format_type(oid, -1)} for every type of the catalog: the SQL name, arrays as the element
+ * name with {@code []}.
+ */
+ public static Map formatTypeNames() {
+ Map names = new LinkedHashMap<>();
+ for (TypeDef type : TYPES.values()) {
+ if (type.category == 'A') {
+ TypeDef element = TYPES.get(type.elem);
+ names.put(type.oid, SQL_NAMES.getOrDefault(element.name, element.name) + "[]");
+ } else {
+ names.put(type.oid, SQL_NAMES.getOrDefault(type.name, type.name));
+ }
+ }
+ return names;
+ }
+
+ /**
+ * The OID of a type given by name ({@code int4}, {@code integer}, {@code _int4}, {@code int4[]}).
+ */
+ public static Optional typeOid(String name) {
+ String n = name.trim().replaceAll("\\s+", " ");
+ if (n.startsWith("pg_catalog.")) {
+ n = n.substring("pg_catalog.".length());
+ }
+ boolean array = n.endsWith("[]");
+ if (array) {
+ n = n.substring(0, n.length() - 2).trim();
+ }
+ n = ALIASES.getOrDefault(n, n);
+ for (TypeDef type : TYPES.values()) {
+ if (type.name.equals(n)) {
+ if (!array) {
+ return Optional.of(type.oid);
+ }
+ return type.array == 0 ? Optional.empty() : Optional.of(type.array);
+ }
+ }
+ return Optional.empty();
+ }
+
+ /** The OID of a function of the catalog given by name ({@code array_in}). */
+ public static Optional procOid(String name) {
+ String n = name.startsWith("pg_catalog.") ? name.substring("pg_catalog.".length()) : name;
+ for (Map.Entry proc : PROCS.entrySet()) {
+ if (proc.getValue().equals(n)) {
+ return Optional.of(proc.getKey());
+ }
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * The OID of a catalog table or view, by schema ({@code null} = search {@code pg_catalog}) and
+ * PostgreSQL name.
+ */
+ public static Optional systemRelationOid(String schema, String name) {
+ for (SystemRelation relation : RELATIONS) {
+ boolean schemaMatches =
+ schema == null
+ ? relation.namespace == PG_CATALOG
+ : (schema.equals("pg_catalog") && relation.namespace == PG_CATALOG)
+ || (schema.equals("information_schema")
+ && relation.namespace == INFORMATION_SCHEMA);
+ if (schemaMatches && relation.name.equals(name)) {
+ return Optional.of(relation.oid);
+ }
+ }
+ return Optional.empty();
+ }
+
+ static List systemRelations() {
+ return Collections.unmodifiableList(RELATIONS);
+ }
+
+ /** Rows of {@code pg_type}. */
+ static List> types() {
+ List> rows = new ArrayList<>();
+ for (TypeDef t : TYPES.values()) {
+ Map row = new HashMap<>();
+ row.put("oid", t.oid);
+ row.put("typname", t.name);
+ row.put("typnamespace", PG_CATALOG);
+ row.put("typowner", OWNER);
+ row.put("typlen", t.len);
+ row.put("typbyval", t.byval);
+ row.put("typtype", String.valueOf(t.typtype));
+ row.put("typcategory", String.valueOf(t.category));
+ row.put("typispreferred", t.preferred);
+ row.put("typisdefined", true);
+ row.put("typdelim", ",");
+ row.put("typrelid", 0L);
+ row.put("typsubscript", t.subscript);
+ row.put("typelem", t.elem);
+ row.put("typarray", t.array);
+ row.put("typinput", t.io[0]);
+ row.put("typoutput", t.io[1]);
+ row.put("typreceive", t.io[2]);
+ row.put("typsend", t.io[3]);
+ row.put("typmodin", t.io[4]);
+ row.put("typmodout", t.io[5]);
+ row.put("typanalyze", 0L);
+ row.put("typalign", String.valueOf(t.align));
+ row.put("typstorage", String.valueOf(t.storage));
+ row.put("typnotnull", false);
+ row.put("typbasetype", 0L);
+ row.put("typtypmod", -1);
+ row.put("typndims", 0);
+ row.put("typcollation", t.collation);
+ rows.add(row);
+ }
+ return rows;
+ }
+
+ /** Rows of {@code pg_proc}: the functions {@code pg_type} refers to. */
+ static List> procs() {
+ List> rows = new ArrayList<>();
+ for (Map.Entry proc : PROCS.entrySet()) {
+ Map row = new HashMap<>();
+ row.put("oid", proc.getKey());
+ row.put("proname", proc.getValue());
+ row.put("pronamespace", PG_CATALOG);
+ row.put("proowner", OWNER);
+ row.put("prolang", 12L);
+ row.put("procost", 1.0f);
+ row.put("prorows", 0.0f);
+ row.put("provariadic", 0L);
+ row.put("prosupport", 0L);
+ row.put("prokind", "f");
+ row.put("prosecdef", false);
+ row.put("proleakproof", false);
+ row.put("proisstrict", true);
+ row.put("proretset", false);
+ row.put("provolatile", "i");
+ row.put("proparallel", "s");
+ row.put("pronargs", 1);
+ row.put("pronargdefaults", 0);
+ row.put("prorettype", PROC_RESULT.getOrDefault(proc.getKey(), 0L));
+ row.put("prosrc", proc.getValue());
+ rows.add(row);
+ }
+ return rows;
+ }
+
+ /** Rows of {@code pg_am}. */
+ static List> accessMethods() {
+ List> rows = new ArrayList<>();
+ rows.add(row("oid", 2L, "amname", "heap", "amhandler", 3L, "amtype", "t"));
+ rows.add(row("oid", 403L, "amname", "btree", "amhandler", 330L, "amtype", "i"));
+ return rows;
+ }
+
+ /** Rows of {@code pg_collation}: the built-in collations PostgreSQL always has. */
+ static List> collations() {
+ List> rows = new ArrayList<>();
+ collation(rows, DEFAULT_COLLATION, "default", "d", -1, null);
+ collation(rows, 950, "C", "c", -1, "C");
+ collation(rows, 951, "POSIX", "c", -1, "POSIX");
+ // UTF8 only; information_schema.character_sets names it as the default collation
+ collation(rows, 12545, "ucs_basic", "c", 6, "C");
+ return rows;
+ }
+
+ private static void collation(
+ List> rows,
+ long oid,
+ String name,
+ String provider,
+ int encoding,
+ String locale) {
+ Map row =
+ row(
+ "oid",
+ oid,
+ "collname",
+ name,
+ "collnamespace",
+ PG_CATALOG,
+ "collowner",
+ OWNER,
+ "collprovider",
+ provider,
+ "collisdeterministic",
+ true,
+ "collencoding",
+ encoding);
+ row.put("collcollate", locale);
+ row.put("collctype", locale);
+ rows.add(row);
+ }
+
+ /** Rows of {@code pg_settings}: the fixed parameters clients read. */
+ static List> settings() {
+ List> rows = new ArrayList<>();
+ setting(rows, "block_size", "8192", "integer");
+ setting(rows, "client_encoding", "UTF8", "string");
+ setting(rows, "DateStyle", "ISO, MDY", "string");
+ setting(rows, "default_transaction_read_only", "on", "bool");
+ setting(rows, "integer_datetimes", "on", "bool");
+ setting(rows, "IntervalStyle", "postgres", "enum");
+ setting(rows, "max_identifier_length", "63", "integer");
+ setting(rows, "max_index_keys", "32", "integer");
+ setting(rows, "server_encoding", "UTF8", "string");
+ setting(rows, "server_version", "14.0", "string");
+ setting(rows, "server_version_num", "140000", "integer");
+ setting(rows, "standard_conforming_strings", "on", "bool");
+ setting(rows, "transaction_isolation", "read committed", "string");
+ setting(rows, "transaction_read_only", "on", "bool");
+ return rows;
+ }
+
+ private static void setting(
+ List> rows, String name, String value, String vartype) {
+ Map row =
+ row(
+ "name",
+ name,
+ "setting",
+ value,
+ "category",
+ "Preset Options",
+ "context",
+ "internal",
+ "vartype",
+ vartype,
+ "source",
+ "default");
+ row.put("boot_val", value);
+ row.put("reset_val", value);
+ row.put("pending_restart", false);
+ rows.add(row);
+ }
+
+ static Map row(Object... keysAndValues) {
+ Map row = new HashMap<>();
+ for (int i = 0; i < keysAndValues.length; i += 2) {
+ row.put((String) keysAndValues[i], keysAndValues[i + 1]);
+ }
+ return row;
+ }
+
+ // ---------------------------------------------------------------------------- definitions
+
+ private static final class Fn {
+ final long oid;
+ final String name;
+
+ Fn(long oid, String name) {
+ this.oid = oid;
+ this.name = name;
+ }
+ }
+
+ private static Fn f(long oid, String name) {
+ return new Fn(oid, name);
+ }
+
+ private static void base(
+ long oid,
+ String name,
+ int len,
+ boolean byval,
+ char category,
+ boolean preferred,
+ char align,
+ char storage,
+ long array,
+ long collation,
+ Fn... io) {
+ // name is a fixed-length array of char (subscriptable), as in PostgreSQL
+ long elem = name.equals("name") ? 18 : 0;
+ long subscript = name.equals("name") ? RAW_ARRAY_SUBSCRIPT : 0;
+ add(
+ new TypeDef(
+ oid,
+ name,
+ len,
+ byval,
+ 'b',
+ category,
+ preferred,
+ align,
+ storage,
+ elem,
+ array,
+ subscript,
+ register(oid, io),
+ collation));
+ }
+
+ private static void pseudo(
+ long oid,
+ String name,
+ int len,
+ boolean byval,
+ char category,
+ char align,
+ char storage,
+ long array,
+ Fn... io) {
+ add(
+ new TypeDef(
+ oid,
+ name,
+ len,
+ byval,
+ 'p',
+ category,
+ false,
+ align,
+ storage,
+ 0,
+ array,
+ 0,
+ register(oid, io),
+ 0));
+ }
+
+ private static void add(TypeDef type) {
+ TYPES.put(type.oid, type);
+ }
+
+ /** Registers the functions and returns in, out, recv, send, typmodin, typmodout. */
+ private static long[] register(long type, Fn... io) {
+ long[] oids = new long[6];
+ for (int i = 0; i < io.length; i++) {
+ oids[i] = io[i].oid;
+ PROCS.put(io[i].oid, io[i].name);
+ }
+ // in and recv return the type, out returns cstring, send returns bytea
+ PROC_RESULT.put(oids[0], type);
+ PROC_RESULT.put(oids[1], CSTRING);
+ PROC_RESULT.put(oids[2], type);
+ PROC_RESULT.put(oids[3], BYTEA);
+ return oids;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/config/BindAddresses.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/config/BindAddresses.java
new file mode 100644
index 0000000000000..5a0ff015d48c4
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/config/BindAddresses.java
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.config;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.Optional;
+import java.util.regex.Pattern;
+
+/**
+ * Checks the address the PostgreSQL endpoint is bound to: an IPv4 or IPv6 literal that is a
+ * loopback address, a wildcard or an address of this machine. Host names are refused (a name can
+ * resolve to several or changing addresses), and no DNS lookup is ever made.
+ */
+final class BindAddresses {
+
+ private static final Pattern IPV4 =
+ Pattern.compile(
+ "^(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}$");
+
+ private BindAddresses() {}
+
+ /**
+ * Why an address cannot be used for binding, or empty if it can.
+ *
+ * @param value the configured address
+ * @return the reason, or empty for a usable address
+ */
+ static Optional problem(String value) {
+ if (value == null || value.trim().isEmpty()) {
+ return Optional.of("no address given; use an IP address such as 127.0.0.1");
+ }
+ String trimmed = value.trim();
+ InetAddress address = parseLiteral(trimmed);
+ if (address == null) {
+ return Optional.of(
+ "'" + trimmed + "' is not an IP address; use an IP address such as 127.0.0.1");
+ }
+ if (address.isAnyLocalAddress() || address.isLoopbackAddress()) {
+ return Optional.empty();
+ }
+ try {
+ if (NetworkInterface.getByInetAddress(address) != null) {
+ return Optional.empty();
+ }
+ } catch (SocketException e) {
+ return Optional.of(
+ "the network interfaces of this machine cannot be read: " + e.getMessage());
+ }
+ return Optional.of("'" + trimmed + "' is not an address of this machine");
+ }
+
+ /** Whether the value is a wildcard address (all interfaces). */
+ static boolean isWildcard(String value) {
+ InetAddress address = value == null ? null : parseLiteral(value.trim());
+ return address != null && address.isAnyLocalAddress();
+ }
+
+ /** Parses an IP literal without any DNS lookup; returns null for anything else. */
+ static InetAddress parseLiteral(String value) {
+ String candidate = value;
+ if (candidate.startsWith("[") && candidate.endsWith("]")) {
+ candidate = candidate.substring(1, candidate.length() - 1);
+ }
+ if (!IPV4.matcher(candidate).matches() && !looksLikeIpv6(candidate)) {
+ return null;
+ }
+ try {
+ // a literal never triggers a lookup
+ return InetAddress.getByName(candidate);
+ } catch (UnknownHostException | SecurityException e) {
+ return null;
+ }
+ }
+
+ /** Hex digits and colons (at least one), an optional embedded IPv4 tail and zone id. */
+ private static boolean looksLikeIpv6(String value) {
+ int percent = value.indexOf('%');
+ String address = percent < 0 ? value : value.substring(0, percent);
+ if (address.indexOf(':') < 0) {
+ return false;
+ }
+ for (int i = 0; i < address.length(); i++) {
+ char c = address.charAt(i);
+ if (!(c == ':' || c == '.' || isAsciiHexDigit(c))) {
+ return false;
+ }
+ }
+ if (percent < 0) {
+ return true;
+ }
+ String zone = value.substring(percent + 1);
+ if (zone.isEmpty()) {
+ return false;
+ }
+ for (int i = 0; i < zone.length(); i++) {
+ char c = zone.charAt(i);
+ if (!(isAsciiHexDigit(c)
+ || (c >= 'g' && c <= 'z')
+ || (c >= 'G' && c <= 'Z')
+ || c == '_'
+ || c == '.'
+ || c == '-')) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static boolean isAsciiHexDigit(char c) {
+ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/config/PostgreSqlServiceConfig.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/config/PostgreSqlServiceConfig.java
new file mode 100644
index 0000000000000..13fbaef7e97e8
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/config/PostgreSqlServiceConfig.java
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.config;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.function.Function;
+
+/**
+ * The service's settings. Values that cannot be read are reported as problems and replaced by their
+ * defaults, so a configuration is always complete.
+ */
+public final class PostgreSqlServiceConfig {
+
+ public static final String ENABLED = "enable_postgresql_service";
+ public static final String PREFIX = "postgresql_service_";
+ public static final String ADDRESS = PREFIX + "address";
+ public static final String PORT = PREFIX + "port";
+ public static final String MAX_CONNECTIONS = PREFIX + "max_connections";
+ public static final String IDLE_TIMEOUT = PREFIX + "idle_timeout_in_ms";
+ public static final String STARTUP_TIMEOUT = PREFIX + "startup_timeout_in_ms";
+ public static final String WRITE_TIMEOUT = PREFIX + "write_timeout_in_ms";
+
+ public static final boolean DEFAULT_ENABLED = false;
+ public static final String DEFAULT_ADDRESS = "127.0.0.1";
+ public static final int DEFAULT_PORT = 5432;
+ public static final int DEFAULT_MAX_CONNECTIONS = 32;
+ public static final Duration DEFAULT_IDLE_TIMEOUT = Duration.ofMinutes(30);
+ public static final Duration DEFAULT_STARTUP_TIMEOUT = Duration.ofSeconds(10);
+ public static final Duration DEFAULT_WRITE_TIMEOUT = Duration.ofMinutes(5);
+
+ private final boolean enabled;
+ private final String address;
+ private final int port;
+ private final int maxConnections;
+ private final Duration idleTimeout;
+ private final Duration startupTimeout;
+ private final Duration writeTimeout;
+ private final List problems;
+
+ private PostgreSqlServiceConfig(Function values) {
+ List found = new ArrayList<>();
+ this.enabled = parseBoolean(values, ENABLED, DEFAULT_ENABLED, found);
+ this.address = parseAddress(values, found);
+ this.port = parseInt(values, PORT, DEFAULT_PORT, 1, 65535, found);
+ this.maxConnections =
+ parseInt(values, MAX_CONNECTIONS, DEFAULT_MAX_CONNECTIONS, 1, Integer.MAX_VALUE, found);
+ this.idleTimeout = parseDuration(values, IDLE_TIMEOUT, DEFAULT_IDLE_TIMEOUT, found);
+ this.startupTimeout = parseDuration(values, STARTUP_TIMEOUT, DEFAULT_STARTUP_TIMEOUT, found);
+ this.writeTimeout = parseDuration(values, WRITE_TIMEOUT, DEFAULT_WRITE_TIMEOUT, found);
+ this.problems = Collections.unmodifiableList(found);
+ }
+
+ /** Reads the settings; keys that are missing or blank take their defaults. */
+ public static PostgreSqlServiceConfig from(Map values) {
+ return new PostgreSqlServiceConfig(values::get);
+ }
+
+ public boolean enabled() {
+ return enabled;
+ }
+
+ public String address() {
+ return address;
+ }
+
+ public int port() {
+ return port;
+ }
+
+ public int maxConnections() {
+ return maxConnections;
+ }
+
+ public Duration idleTimeout() {
+ return idleTimeout;
+ }
+
+ public Duration startupTimeout() {
+ return startupTimeout;
+ }
+
+ public Duration writeTimeout() {
+ return writeTimeout;
+ }
+
+ /** Problems found while reading the values; empty when every value was valid. */
+ public List problems() {
+ return problems;
+ }
+
+ /**
+ * Whether the bind address is a loopback address. Only loopback addresses may accept unencrypted
+ * connections. The wildcard is not loopback.
+ */
+ /** Whether the service listens on all interfaces. */
+ public boolean isWildcard() {
+ return BindAddresses.isWildcard(address);
+ }
+
+ public boolean isLoopback() {
+ if (BindAddresses.isWildcard(address)) {
+ return false;
+ }
+ try {
+ // the address is an IP literal (checked by BindAddresses), so no name lookup happens
+ return InetAddress.getByName(address).isLoopbackAddress();
+ } catch (UnknownHostException e) {
+ return false;
+ }
+ }
+
+ private static String text(Function values, String key) {
+ String value = values.apply(key);
+ return value == null || value.trim().isEmpty() ? null : value.trim();
+ }
+
+ private static boolean parseBoolean(
+ Function values, String key, boolean defaultValue, List problems) {
+ String value = text(values, key);
+ if (value == null) {
+ return defaultValue;
+ }
+ if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
+ return Boolean.parseBoolean(value);
+ }
+ problems.add(key + ": must be true or false, was '" + value + "'");
+ return defaultValue;
+ }
+
+ private static String parseAddress(Function values, List problems) {
+ String value = text(values, ADDRESS);
+ if (value == null) {
+ return DEFAULT_ADDRESS;
+ }
+ Optional problem = BindAddresses.problem(value);
+ if (problem.isPresent()) {
+ problems.add(ADDRESS + ": " + problem.get());
+ return DEFAULT_ADDRESS;
+ }
+ return value;
+ }
+
+ private static int parseInt(
+ Function values,
+ String key,
+ int defaultValue,
+ int min,
+ int max,
+ List problems) {
+ String value = text(values, key);
+ if (value == null) {
+ return defaultValue;
+ }
+ try {
+ int parsed = Integer.parseInt(value);
+ if (parsed >= min && parsed <= max) {
+ return parsed;
+ }
+ } catch (NumberFormatException e) {
+ // reported below
+ }
+ problems.add(
+ key + ": must be a whole number from " + min + " to " + max + ", was '" + value + "'");
+ return defaultValue;
+ }
+
+ private static Duration parseDuration(
+ Function values, String key, Duration defaultValue, List problems) {
+ String value = text(values, key);
+ if (value == null) {
+ return defaultValue;
+ }
+ try {
+ long millis = Long.parseLong(value);
+ if (millis > 0) {
+ return Duration.ofMillis(millis);
+ }
+ } catch (NumberFormatException e) {
+ // reported below
+ }
+ problems.add(key + ": must be a positive number of milliseconds, was '" + value + "'");
+ return defaultValue;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/execution/ResultCursor.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/execution/ResultCursor.java
new file mode 100644
index 0000000000000..82c6d3a1512a3
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/execution/ResultCursor.java
@@ -0,0 +1,250 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.execution;
+
+import org.apache.iotdb.postgresql.protocol.BackendMessages;
+import org.apache.iotdb.postgresql.protocol.PgException;
+import org.apache.iotdb.postgresql.protocol.SqlState;
+import org.apache.iotdb.postgresql.types.PgType;
+import org.apache.iotdb.postgresql.types.PgValueEncoder;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import org.apache.tsfile.block.column.Column;
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.block.TsBlock;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Streams the rows of a {@link RunningQuery} as DataRow messages: block by block, writing only
+ * while the channel is writable, so memory stays bounded however large the result is. Keeps its
+ * position, so a portal can continue after PortalSuspended.
+ */
+public final class ResultCursor implements AutoCloseable {
+
+ /** A client that stopped reading for longer than the write timeout. */
+ public static final class WriteTimeoutException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ WriteTimeoutException() {
+ super("client stopped reading", null, false, false);
+ }
+ }
+
+ /** Bytes of DataRows collected before they are handed to the channel. */
+ private static final int BATCH_BYTES = 64 * 1024;
+
+ private final RunningQuery query;
+ private final PgType[] types;
+ private short[] formats;
+ private final PgValueEncoder encoder;
+ private TsBlock block;
+ private int position;
+ private boolean exhausted;
+
+ /**
+ * @param formats result formats as sent in Bind: none (all text), one (for all columns) or one
+ * per column; 0 text, 1 binary
+ */
+ public ResultCursor(RunningQuery query, short[] formats, PgValueEncoder encoder)
+ throws PgException {
+ this.query = query;
+ this.encoder = encoder;
+ List dataTypes = query.types();
+ this.types = new PgType[dataTypes.size()];
+ for (int i = 0; i < types.length; i++) {
+ TSDataType dataType = dataTypes.get(i);
+ Optional type = PgType.of(dataType);
+ if (!type.isPresent()) {
+ throw new PgException(
+ SqlState.FEATURE_NOT_SUPPORTED,
+ "column \""
+ + query.names().get(i)
+ + "\" has type "
+ + dataType
+ + ", which is not"
+ + " available through PostgreSQL");
+ }
+ types[i] = type.get();
+ }
+ this.formats = expand(formats, types.length);
+ }
+
+ private static short[] expand(short[] formats, int columns) throws PgException {
+ short[] result = new short[columns];
+ if (formats == null || formats.length == 0) {
+ return result;
+ }
+ if (formats.length == 1) {
+ java.util.Arrays.fill(result, formats[0]);
+ return result;
+ }
+ if (formats.length != columns) {
+ throw new PgException(
+ SqlState.PROTOCOL_VIOLATION,
+ "bind message has "
+ + formats.length
+ + " result formats but query has "
+ + columns
+ + " columns");
+ }
+ return formats.clone();
+ }
+
+ /**
+ * Changes the result formats for the rows sent from now on: a SQL cursor's FETCH chooses them per
+ * fetch (Bind of the FETCH, or BINARY of the DECLARE).
+ */
+ public void setFormats(short[] formats) throws PgException {
+ this.formats = expand(formats, types.length);
+ }
+
+ /** The RowDescription fields. */
+ public List fields() {
+ List fields = new ArrayList<>(types.length);
+ for (int i = 0; i < types.length; i++) {
+ fields.add(
+ new BackendMessages.Field(
+ query.names().get(i), types[i].oid(), types[i].size(), formats[i]));
+ }
+ return fields;
+ }
+
+ /** True once every row was sent. */
+ public boolean exhausted() {
+ return exhausted;
+ }
+
+ /**
+ * Sends up to {@code maxRows} rows (0 = all) as DataRow messages, appending to {@code out} and
+ * handing full batches to the channel.
+ *
+ * @return the number of rows sent
+ */
+ public long send(Channel channel, ByteBuf out, long maxRows, Duration writeTimeout)
+ throws PgException, WriteTimeoutException {
+ return advance(channel, out, maxRows, writeTimeout);
+ }
+
+ /**
+ * Skips up to {@code maxRows} rows (0 = all) without sending them (SQL {@code MOVE}).
+ *
+ * @return the number of rows skipped
+ */
+ public long skip(long maxRows) throws PgException {
+ try {
+ return advance(null, null, maxRows, null);
+ } catch (WriteTimeoutException e) {
+ throw new IllegalStateException("nothing is written while skipping", e);
+ }
+ }
+
+ /** Sends ({@code out != null}) or skips rows. */
+ private long advance(Channel channel, ByteBuf out, long maxRows, Duration writeTimeout)
+ throws PgException, WriteTimeoutException {
+ long sent = 0;
+ while (maxRows == 0 || sent < maxRows) {
+ if (block == null || position >= block.getPositionCount()) {
+ Optional next = query.next();
+ if (!next.isPresent()) {
+ exhausted = true;
+ break;
+ }
+ block = next.get();
+ position = 0;
+ }
+ int end = block.getPositionCount();
+ if (maxRows > 0) {
+ end = (int) Math.min(end, position + (maxRows - sent));
+ }
+ if (out == null) {
+ sent += end - position;
+ position = end;
+ continue;
+ }
+ Column[] columns = new Column[types.length];
+ for (int c = 0; c < columns.length; c++) {
+ columns[c] = block.getColumn(query.blockIndex(c));
+ }
+ for (; position < end; position++) {
+ int row = BackendMessages.beginDataRow(out, columns.length);
+ for (int c = 0; c < columns.length; c++) {
+ if (columns[c].isNull(position)) {
+ BackendMessages.nullValue(out);
+ } else if (formats[c] == 0) {
+ BackendMessages.value(out, encoder.text(types[c], columns[c], position));
+ } else {
+ BackendMessages.value(out, encoder.binary(types[c], columns[c], position));
+ }
+ }
+ BackendMessages.end(out, row);
+ sent++;
+ if (out.readableBytes() >= BATCH_BYTES) {
+ flush(channel, out, writeTimeout);
+ }
+ }
+ }
+ return sent;
+ }
+
+ /** Hands the collected bytes to the channel and waits until it can take more. */
+ public static void flush(Channel channel, ByteBuf out, Duration writeTimeout)
+ throws WriteTimeoutException {
+ if (out.readableBytes() == 0) {
+ return;
+ }
+ // a copy: the caller keeps using its buffer while Netty writes this one
+ channel.writeAndFlush(out.copy());
+ out.clear();
+ awaitWritable(channel, writeTimeout);
+ }
+
+ private static void awaitWritable(Channel channel, Duration writeTimeout)
+ throws WriteTimeoutException {
+ long deadline = System.nanoTime() + writeTimeout.toNanos();
+ long pause = 1;
+ while (!channel.isWritable() && channel.isActive()) {
+ if (System.nanoTime() > deadline) {
+ throw new WriteTimeoutException();
+ }
+ try {
+ Thread.sleep(pause);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ return;
+ }
+ pause = Math.min(pause * 2, 50);
+ }
+ }
+
+ /** Cancels the query (CancelRequest, write timeout). */
+ public void cancel() {
+ query.cancel();
+ }
+
+ @Override
+ public void close() {
+ query.close();
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/execution/RunningQuery.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/execution/RunningQuery.java
new file mode 100644
index 0000000000000..0a0460ab2f71c
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/execution/RunningQuery.java
@@ -0,0 +1,215 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.execution;
+
+import org.apache.iotdb.commons.exception.IoTDBException;
+import org.apache.iotdb.commons.exception.SemanticException;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Literal;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Statement;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.StringLiteral;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.parser.ParsingException;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.protocol.session.IClientSession;
+import org.apache.iotdb.db.protocol.session.SessionManager;
+import org.apache.iotdb.db.queryengine.plan.Coordinator;
+import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult;
+import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution;
+import org.apache.iotdb.db.queryengine.plan.planner.LocalExecutionPlanner;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ExecuteImmediate;
+import org.apache.iotdb.db.queryengine.plan.relational.sql.parser.SqlParser;
+import org.apache.iotdb.postgresql.protocol.PgErrors;
+import org.apache.iotdb.postgresql.protocol.PgException;
+import org.apache.iotdb.postgresql.protocol.SqlState;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.tsfile.enums.TSDataType;
+import org.apache.tsfile.read.common.block.TsBlock;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * One table-model query started through the engine: parsed with the table dialect, run with the
+ * engine's query timeout as a read-only user query, and always cleaned up.
+ */
+public final class RunningQuery implements AutoCloseable {
+
+ private final long queryId;
+ private final IQueryExecution execution;
+ private final List names;
+ private final List types;
+ private final int[] blockIndex;
+ private volatile boolean closed;
+
+ private RunningQuery(long queryId, IQueryExecution execution) {
+ this.queryId = queryId;
+ this.execution = execution;
+ this.names = new ArrayList<>(execution.getDatasetHeader().getRespColumns());
+ this.types = new ArrayList<>(execution.getDatasetHeader().getRespDataTypes());
+ List mapping = execution.getDatasetHeader().getColumnIndex2TsBlockColumnIndexList();
+ this.blockIndex = new int[names.size()];
+ for (int i = 0; i < blockIndex.length; i++) {
+ blockIndex[i] = mapping == null ? i : mapping.get(i);
+ }
+ }
+
+ /**
+ * Starts a query.
+ *
+ * @param sql table-model SQL (from the translator)
+ * @param parameters the values of its {@code ?} in order; empty for none
+ */
+ public static RunningQuery start(IClientSession client, String sql, List parameters)
+ throws PgException {
+ return start(client, sql, parameters, java.time.Duration.ZERO);
+ }
+
+ /**
+ * Starts a query with the session's {@code statement_timeout}.
+ *
+ * @param statementTimeout limits the engine's query timeout; zero for none
+ */
+ public static RunningQuery start(
+ IClientSession client,
+ String sql,
+ List parameters,
+ java.time.Duration statementTimeout)
+ throws PgException {
+ SessionManager sessions = SessionManager.getInstance();
+ SqlParser parser = new SqlParser();
+ IClientSession previous = sessions.getCurrSession();
+ sessions.setCurrSession(client);
+ Long queryId = null;
+ boolean started = false;
+ try {
+ Statement statement =
+ parameters.isEmpty()
+ ? parser.createStatement(sql, client.getZoneId(), client)
+ : new ExecuteImmediate(new StringLiteral(sql), parameters);
+ queryId = sessions.requestQueryId();
+ ExecutionResult result =
+ Coordinator.getInstance()
+ .executeForTableModel(
+ statement,
+ parser,
+ client,
+ queryId,
+ sessions.getSessionInfo(client),
+ sql,
+ LocalExecutionPlanner.getInstance().metadata,
+ org.apache.iotdb.postgresql.session.StatementTimeout.engineTimeoutMillis(
+ IoTDBDescriptor.getInstance().getConfig().getQueryTimeoutThreshold(),
+ statementTimeout),
+ true,
+ false,
+ true);
+ if (result.status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()
+ && result.status.getCode() != TSStatusCode.REDIRECTION_RECOMMEND.getStatusCode()) {
+ throw PgErrors.of(result.status);
+ }
+ IQueryExecution execution = Coordinator.getInstance().getQueryExecution(queryId);
+ if (execution == null) {
+ throw new PgException(SqlState.INTERNAL_ERROR, "the statement did not start a query");
+ }
+ started = true;
+ return new RunningQuery(queryId, execution);
+ } catch (PgException e) {
+ throw e;
+ } catch (RuntimeException e) {
+ throw PgErrors.of(errorCode(e), e);
+ } finally {
+ if (!started && queryId != null) {
+ Coordinator.getInstance().cleanupQueryExecution(queryId);
+ }
+ // the session stays registered (SHOW SESSIONS); only this thread forgets it
+ sessions.restoreSession(previous, null);
+ }
+ }
+
+ private static int errorCode(Throwable error) {
+ for (Throwable t = error; t != null; t = t.getCause()) {
+ if (t instanceof IoTDBException) {
+ return ((IoTDBException) t).getErrorCode();
+ }
+ if (t instanceof org.apache.iotdb.commons.exception.IoTDBRuntimeException) {
+ return ((org.apache.iotdb.commons.exception.IoTDBRuntimeException) t).getErrorCode();
+ }
+ }
+ if (error instanceof SemanticException) {
+ return TSStatusCode.SEMANTIC_ERROR.getStatusCode();
+ }
+ if (error instanceof ParsingException) {
+ return TSStatusCode.SQL_PARSE_ERROR.getStatusCode();
+ }
+ return TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode();
+ }
+
+ /** Column names in result order. */
+ public List names() {
+ return Collections.unmodifiableList(names);
+ }
+
+ /** Column types in result order. */
+ public List types() {
+ return Collections.unmodifiableList(types);
+ }
+
+ /** The block column of result column {@code i}. */
+ public int blockIndex(int i) {
+ return blockIndex[i];
+ }
+
+ /** The next non-empty block, or empty at the end. */
+ public Optional next() throws PgException {
+ try {
+ while (execution.hasNextResult()) {
+ Optional block = execution.getBatchResult();
+ if (!block.isPresent()) {
+ return Optional.empty();
+ }
+ if (!block.get().isEmpty()) {
+ return block;
+ }
+ }
+ return Optional.empty();
+ } catch (IoTDBException e) {
+ throw PgErrors.of(e.getErrorCode(), e);
+ } catch (RuntimeException e) {
+ if (closed) {
+ throw new PgException(SqlState.QUERY_CANCELED, "canceling statement due to user request");
+ }
+ throw PgErrors.of(errorCode(e), e);
+ }
+ }
+
+ /** Stops the query (CancelRequest, write timeout); the reader then gets 57014. */
+ public void cancel() {
+ closed = true;
+ execution.cancel();
+ }
+
+ @Override
+ public void close() {
+ closed = true;
+ Coordinator.getInstance().cleanupQueryExecution(queryId);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/BackendMessages.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/BackendMessages.java
new file mode 100644
index 0000000000000..677117af765c4
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/BackendMessages.java
@@ -0,0 +1,242 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.protocol;
+
+import io.netty.buffer.ByteBuf;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+/**
+ * Writes the server's messages of protocol 3.0 into a buffer. Each method appends one complete
+ * message; streaming rows use {@link #beginDataRow}, {@link #value}, {@link #nullValue} and {@link
+ * #end}.
+ */
+public final class BackendMessages {
+
+ /** One column of a RowDescription. */
+ public static final class Field {
+ final String name;
+ final int tableOid;
+ final short columnNumber;
+ final int typeOid;
+ final short typeSize;
+ final int typeModifier;
+ final short format;
+
+ public Field(String name, int typeOid, short typeSize, short format) {
+ this(name, 0, (short) 0, typeOid, typeSize, -1, format);
+ }
+
+ public Field(
+ String name,
+ int tableOid,
+ short columnNumber,
+ int typeOid,
+ short typeSize,
+ int typeModifier,
+ short format) {
+ this.name = name;
+ this.tableOid = tableOid;
+ this.columnNumber = columnNumber;
+ this.typeOid = typeOid;
+ this.typeSize = typeSize;
+ this.typeModifier = typeModifier;
+ this.format = format;
+ }
+
+ public short format() {
+ return format;
+ }
+ }
+
+ private BackendMessages() {}
+
+ /** The single byte answering an SSLRequest or GSSENCRequest (not a framed message). */
+ public static void sslResponse(ByteBuf out, boolean accept) {
+ out.writeByte(accept ? 'S' : 'N');
+ }
+
+ public static void authenticationOk(ByteBuf out) {
+ int start = begin(out, 'R');
+ out.writeInt(0);
+ end(out, start);
+ }
+
+ public static void authenticationCleartextPassword(ByteBuf out) {
+ int start = begin(out, 'R');
+ out.writeInt(3);
+ end(out, start);
+ }
+
+ public static void parameterStatus(ByteBuf out, String name, String value) {
+ int start = begin(out, 'S');
+ cstring(out, name);
+ cstring(out, value);
+ end(out, start);
+ }
+
+ public static void backendKeyData(ByteBuf out, int processId, int secretKey) {
+ int start = begin(out, 'K');
+ out.writeInt(processId);
+ out.writeInt(secretKey);
+ end(out, start);
+ }
+
+ /** {@code status}: 'I' idle, 'T' in a transaction block, 'E' in a failed transaction block. */
+ public static void readyForQuery(ByteBuf out, char status) {
+ int start = begin(out, 'Z');
+ out.writeByte(status);
+ end(out, start);
+ }
+
+ public static void negotiateProtocolVersion(
+ ByteBuf out, int newestMinorVersion, List unsupportedOptions) {
+ int start = begin(out, 'v');
+ out.writeInt(newestMinorVersion);
+ out.writeInt(unsupportedOptions.size());
+ for (String option : unsupportedOptions) {
+ cstring(out, option);
+ }
+ end(out, start);
+ }
+
+ public static void rowDescription(ByteBuf out, List fields) {
+ int start = begin(out, 'T');
+ out.writeShort(fields.size());
+ for (Field field : fields) {
+ cstring(out, field.name);
+ out.writeInt(field.tableOid);
+ out.writeShort(field.columnNumber);
+ out.writeInt(field.typeOid);
+ out.writeShort(field.typeSize);
+ out.writeInt(field.typeModifier);
+ out.writeShort(field.format);
+ }
+ end(out, start);
+ }
+
+ /** Starts a DataRow; returns the position to pass to {@link #end}. */
+ public static int beginDataRow(ByteBuf out, int columns) {
+ int start = begin(out, 'D');
+ out.writeShort(columns);
+ return start;
+ }
+
+ public static void value(ByteBuf out, byte[] value) {
+ out.writeInt(value.length);
+ out.writeBytes(value);
+ }
+
+ public static void nullValue(ByteBuf out) {
+ out.writeInt(-1);
+ }
+
+ public static void commandComplete(ByteBuf out, String tag) {
+ int start = begin(out, 'C');
+ cstring(out, tag);
+ end(out, start);
+ }
+
+ public static void emptyQueryResponse(ByteBuf out) {
+ end(out, begin(out, 'I'));
+ }
+
+ public static void parseComplete(ByteBuf out) {
+ end(out, begin(out, '1'));
+ }
+
+ public static void bindComplete(ByteBuf out) {
+ end(out, begin(out, '2'));
+ }
+
+ public static void closeComplete(ByteBuf out) {
+ end(out, begin(out, '3'));
+ }
+
+ public static void noData(ByteBuf out) {
+ end(out, begin(out, 'n'));
+ }
+
+ public static void portalSuspended(ByteBuf out) {
+ end(out, begin(out, 's'));
+ }
+
+ public static void parameterDescription(ByteBuf out, int[] typeOids) {
+ int start = begin(out, 't');
+ out.writeShort(typeOids.length);
+ for (int oid : typeOids) {
+ out.writeInt(oid);
+ }
+ end(out, start);
+ }
+
+ /** An ErrorResponse with severity ERROR or FATAL. */
+ public static void errorResponse(ByteBuf out, PgException error) {
+ response(
+ out,
+ 'E',
+ error.isFatal() ? "FATAL" : "ERROR",
+ error.sqlState(),
+ error.getMessage(),
+ error.hint());
+ }
+
+ public static void noticeResponse(ByteBuf out, String severity, String sqlState, String message) {
+ response(out, 'N', severity, sqlState, message, null);
+ }
+
+ private static void response(
+ ByteBuf out, char type, String severity, String sqlState, String message, String hint) {
+ int start = begin(out, type);
+ field(out, 'S', severity);
+ field(out, 'V', severity);
+ field(out, 'C', sqlState);
+ field(out, 'M', message == null ? "" : message);
+ if (hint != null) {
+ field(out, 'H', hint);
+ }
+ out.writeByte(0);
+ end(out, start);
+ }
+
+ private static void field(ByteBuf out, char code, String value) {
+ out.writeByte(code);
+ cstring(out, value);
+ }
+
+ /** Writes the type byte and a placeholder length; returns the length's position. */
+ public static int begin(ByteBuf out, char type) {
+ out.writeByte(type);
+ int lengthIndex = out.writerIndex();
+ out.writeInt(0);
+ return lengthIndex;
+ }
+
+ /** Fills in the length of the message started at {@code lengthIndex}. */
+ public static void end(ByteBuf out, int lengthIndex) {
+ out.setInt(lengthIndex, out.writerIndex() - lengthIndex);
+ }
+
+ public static void cstring(ByteBuf out, String value) {
+ out.writeBytes(value.getBytes(StandardCharsets.UTF_8));
+ out.writeByte(0);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/FrontendMessage.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/FrontendMessage.java
new file mode 100644
index 0000000000000..663612d2c733e
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/FrontendMessage.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.protocol;
+
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * A message from the client: either a packet of the startup phase (no type byte; {@link #type()} is
+ * 0) or a typed message. The payload excludes the type byte and the length.
+ */
+public final class FrontendMessage {
+
+ /** Request codes of startup packets. */
+ public static final int PROTOCOL_3_0 = 196_608;
+
+ public static final int SSL_REQUEST = 80_877_103;
+ public static final int GSSENC_REQUEST = 80_877_104;
+ public static final int CANCEL_REQUEST = 80_877_102;
+
+ private final char type;
+ private final ByteBuffer payload;
+
+ public FrontendMessage(char type, byte[] payload) {
+ this.type = type;
+ this.payload = ByteBuffer.wrap(payload);
+ }
+
+ /** The type byte, or 0 for a startup packet. */
+ public char type() {
+ return type;
+ }
+
+ public boolean isStartupPacket() {
+ return type == 0;
+ }
+
+ public int remaining() {
+ return payload.remaining();
+ }
+
+ public byte readByte() throws PgException {
+ try {
+ return payload.get();
+ } catch (BufferUnderflowException e) {
+ throw truncated();
+ }
+ }
+
+ public short readInt16() throws PgException {
+ try {
+ return payload.getShort();
+ } catch (BufferUnderflowException e) {
+ throw truncated();
+ }
+ }
+
+ public int readInt32() throws PgException {
+ try {
+ return payload.getInt();
+ } catch (BufferUnderflowException e) {
+ throw truncated();
+ }
+ }
+
+ /** {@code n} bytes, or {@code null} for the length -1 used for NULL values. */
+ public byte[] readBytes(int n) throws PgException {
+ if (n == -1) {
+ return null;
+ }
+ if (n < 0 || n > payload.remaining()) {
+ throw truncated();
+ }
+ byte[] bytes = new byte[n];
+ payload.get(bytes);
+ return bytes;
+ }
+
+ /** A NUL-terminated UTF-8 string. */
+ public String readCString() throws PgException {
+ int start = payload.position();
+ for (int i = start; i < payload.limit(); i++) {
+ if (payload.get(i) == 0) {
+ String value =
+ new String(
+ payload.array(), payload.arrayOffset() + start, i - start, StandardCharsets.UTF_8);
+ payload.position(i + 1);
+ return value;
+ }
+ }
+ throw truncated();
+ }
+
+ private PgException truncated() {
+ return PgException.fatal(
+ SqlState.PROTOCOL_VIOLATION,
+ "invalid message format" + (type == 0 ? "" : " of message type '" + type + "'"));
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgErrors.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgErrors.java
new file mode 100644
index 0000000000000..240c214744e50
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgErrors.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.protocol;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Maps the engine's status codes to SQLSTATEs. Unknown codes become XX000 with the engine's
+ * message.
+ */
+public final class PgErrors {
+
+ private static final Map BY_CODE = new HashMap<>();
+ private static final Pattern MISSING_TABLE =
+ Pattern.compile("table\\b.*\\b(does not exist|not exists?|cannot be found)");
+ private static final Pattern MISSING_DATABASE =
+ Pattern.compile("database\\b.*\\b(does not exist|not exists?|cannot be found)");
+ private static final Pattern MISSING_COLUMN =
+ Pattern.compile("column\\b.*\\b(does not exist|not exists?|cannot be resolved)");
+
+ static {
+ BY_CODE.put(TSStatusCode.SQL_PARSE_ERROR.getStatusCode(), SqlState.SYNTAX_ERROR);
+ BY_CODE.put(TSStatusCode.TABLE_NOT_EXISTS.getStatusCode(), SqlState.UNDEFINED_TABLE);
+ BY_CODE.put(TSStatusCode.COLUMN_NOT_EXISTS.getStatusCode(), SqlState.UNDEFINED_COLUMN);
+ BY_CODE.put(TSStatusCode.DATABASE_NOT_EXIST.getStatusCode(), SqlState.INVALID_SCHEMA_NAME);
+ BY_CODE.put(TSStatusCode.NO_PERMISSION.getStatusCode(), SqlState.INSUFFICIENT_PRIVILEGE);
+ BY_CODE.put(TSStatusCode.QUERY_WAS_KILLED.getStatusCode(), SqlState.QUERY_CANCELED);
+ BY_CODE.put(TSStatusCode.QUERY_TIMEOUT.getStatusCode(), SqlState.QUERY_CANCELED);
+ BY_CODE.put(TSStatusCode.MPP_MEMORY_NOT_ENOUGH.getStatusCode(), SqlState.OUT_OF_MEMORY);
+ BY_CODE.put(
+ TSStatusCode.QUERY_EXECUTION_MEMORY_NOT_ENOUGH.getStatusCode(), SqlState.OUT_OF_MEMORY);
+ BY_CODE.put(
+ TSStatusCode.TOO_MANY_CONCURRENT_QUERIES_ERROR.getStatusCode(),
+ SqlState.INSUFFICIENT_RESOURCES);
+ BY_CODE.put(
+ TSStatusCode.NUMERIC_VALUE_OUT_OF_RANGE.getStatusCode(),
+ SqlState.NUMERIC_VALUE_OUT_OF_RANGE);
+ BY_CODE.put(TSStatusCode.DIVISION_BY_ZERO.getStatusCode(), SqlState.DIVISION_BY_ZERO);
+ BY_CODE.put(TSStatusCode.DATE_OUT_OF_RANGE.getStatusCode(), SqlState.DATETIME_FIELD_OVERFLOW);
+ BY_CODE.put(TSStatusCode.UNSUPPORTED_OPERATION.getStatusCode(), SqlState.FEATURE_NOT_SUPPORTED);
+ }
+
+ private PgErrors() {}
+
+ /** The SQLSTATE for an engine status code and message. */
+ public static String sqlState(int code, String message) {
+ String mapped = BY_CODE.get(code);
+ if (mapped != null) {
+ return mapped;
+ }
+ if (code == TSStatusCode.SEMANTIC_ERROR.getStatusCode()) {
+ // the analyzer reports missing objects as semantic errors with a message
+ String text = message == null ? "" : message.toLowerCase(Locale.ROOT);
+ if (MISSING_COLUMN.matcher(text).find()) {
+ return SqlState.UNDEFINED_COLUMN;
+ }
+ if (MISSING_TABLE.matcher(text).find()) {
+ return SqlState.UNDEFINED_TABLE;
+ }
+ if (MISSING_DATABASE.matcher(text).find()) {
+ return SqlState.INVALID_SCHEMA_NAME;
+ }
+ return SqlState.SYNTAX_OR_ACCESS_RULE;
+ }
+ return SqlState.INTERNAL_ERROR;
+ }
+
+ /** An ERROR (not FATAL) for a failed engine status. */
+ public static PgException of(TSStatus status) {
+ String message =
+ status.getMessage() == null ? "error " + status.getCode() : status.getMessage();
+ return new PgException(sqlState(status.getCode(), message), message);
+ }
+
+ /** An ERROR for an exception from the engine, with the status code it carries if any. */
+ public static PgException of(int code, Throwable error) {
+ String message = error.getMessage() == null ? error.toString() : error.getMessage();
+ return new PgException(sqlState(code, message), message);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgException.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgException.java
new file mode 100644
index 0000000000000..eb3d0e514fbae
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgException.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.protocol;
+
+/** An error that is sent to the client as an ErrorResponse with this SQLSTATE. */
+public class PgException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ private final String sqlState;
+ private final boolean fatal;
+ private final String hint;
+
+ public PgException(String sqlState, String message) {
+ this(sqlState, message, null, false);
+ }
+
+ public PgException(String sqlState, String message, String hint, boolean fatal) {
+ super(message);
+ this.sqlState = sqlState;
+ this.hint = hint;
+ this.fatal = fatal;
+ }
+
+ /** A FATAL error, after which the server closes the connection. */
+ public static PgException fatal(String sqlState, String message) {
+ return new PgException(sqlState, message, null, true);
+ }
+
+ public String sqlState() {
+ return sqlState;
+ }
+
+ public boolean isFatal() {
+ return fatal;
+ }
+
+ /** A hint for the user, or {@code null}. */
+ public String hint() {
+ return hint;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgFrameDecoder.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgFrameDecoder.java
new file mode 100644
index 0000000000000..879de1aa843bc
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/PgFrameDecoder.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.protocol;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.handler.codec.DecoderException;
+
+import java.util.List;
+
+/**
+ * Splits the byte stream into {@link FrontendMessage}s: untyped startup packets until {@link
+ * #startupComplete()}, typed messages afterwards. Lengths are checked before anything is buffered,
+ * so a client cannot make the server allocate more than {@code maxMessageSize}.
+ */
+public final class PgFrameDecoder extends ByteToMessageDecoder {
+
+ /** Startup packets are small; PostgreSQL itself allows 10,000 bytes. */
+ static final int MAX_STARTUP_PACKET = 10_000;
+
+ private final int maxMessageSize;
+ private boolean startup = true;
+
+ public PgFrameDecoder(int maxMessageSize) {
+ this.maxMessageSize = maxMessageSize;
+ }
+
+ /** Switches to typed messages once the StartupMessage has been read. */
+ public void startupComplete() {
+ startup = false;
+ }
+
+ @Override
+ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) {
+ if (startup) {
+ if (in.readableBytes() < 4) {
+ return;
+ }
+ int length = in.getInt(in.readerIndex());
+ if (length < 8 || length > MAX_STARTUP_PACKET) {
+ throw new DecoderException(
+ PgException.fatal(SqlState.PROTOCOL_VIOLATION, "invalid length of startup packet"));
+ }
+ if (in.readableBytes() < length) {
+ return;
+ }
+ in.skipBytes(4);
+ byte[] payload = new byte[length - 4];
+ in.readBytes(payload);
+ out.add(new FrontendMessage((char) 0, payload));
+ return;
+ }
+ if (in.readableBytes() < 5) {
+ return;
+ }
+ char type = (char) (in.getByte(in.readerIndex()) & 0xFF);
+ int length = in.getInt(in.readerIndex() + 1);
+ if (length < 4 || length > maxMessageSize) {
+ throw new DecoderException(
+ PgException.fatal(SqlState.PROTOCOL_VIOLATION, "invalid message length"));
+ }
+ if (in.readableBytes() < 1 + length) {
+ return;
+ }
+ in.skipBytes(5);
+ byte[] payload = new byte[length - 4];
+ in.readBytes(payload);
+ out.add(new FrontendMessage(type, payload));
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/SqlState.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/SqlState.java
new file mode 100644
index 0000000000000..c1538f6b64ac9
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/protocol/SqlState.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.protocol;
+
+/** SQLSTATE codes the adapter sends. */
+public final class SqlState {
+
+ public static final String READ_ONLY_SQL_TRANSACTION = "25006";
+ public static final String FEATURE_NOT_SUPPORTED = "0A000";
+ public static final String SYNTAX_ERROR = "42601";
+ public static final String UNDEFINED_TABLE = "42P01";
+ public static final String UNDEFINED_COLUMN = "42703";
+ public static final String INVALID_SCHEMA_NAME = "3F000";
+ public static final String INSUFFICIENT_PRIVILEGE = "42501";
+ public static final String QUERY_CANCELED = "57014";
+ public static final String TOO_MANY_CONNECTIONS = "53300";
+ public static final String OUT_OF_MEMORY = "53200";
+ public static final String INTERNAL_ERROR = "XX000";
+ public static final String INVALID_PASSWORD = "28P01";
+ public static final String INVALID_AUTHORIZATION = "28000";
+ public static final String PROTOCOL_VIOLATION = "08P01";
+ public static final String CONNECTION_FAILURE = "08006";
+ public static final String ADMIN_SHUTDOWN = "57P01";
+ public static final String INVALID_PARAMETER_VALUE = "22023";
+ public static final String INVALID_CURSOR_NAME = "34000";
+ public static final String DUPLICATE_CURSOR = "42P03";
+ public static final String DUPLICATE_PREPARED_STATEMENT = "42P05";
+ public static final String INVALID_SQL_STATEMENT_NAME = "26000";
+ public static final String INVALID_TEXT_REPRESENTATION = "22P02";
+ public static final String SYNTAX_OR_ACCESS_RULE = "42000";
+ public static final String NUMERIC_VALUE_OUT_OF_RANGE = "22003";
+ public static final String DIVISION_BY_ZERO = "22012";
+ public static final String DATETIME_FIELD_OVERFLOW = "22008";
+ public static final String INSUFFICIENT_RESOURCES = "53000";
+
+ private SqlState() {}
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgConnectionHandler.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgConnectionHandler.java
new file mode 100644
index 0000000000000..ccc807db1d3fe
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgConnectionHandler.java
@@ -0,0 +1,405 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.server;
+
+import org.apache.iotdb.commons.conf.IoTDBConstant;
+import org.apache.iotdb.commons.queryengine.common.SqlDialect;
+import org.apache.iotdb.db.protocol.basic.BasicOpenSessionResp;
+import org.apache.iotdb.db.protocol.session.ExternalServiceClientSession;
+import org.apache.iotdb.db.protocol.session.SessionManager;
+import org.apache.iotdb.db.queryengine.plan.Coordinator;
+import org.apache.iotdb.postgresql.protocol.BackendMessages;
+import org.apache.iotdb.postgresql.protocol.FrontendMessage;
+import org.apache.iotdb.postgresql.protocol.PgException;
+import org.apache.iotdb.postgresql.protocol.PgFrameDecoder;
+import org.apache.iotdb.postgresql.protocol.SqlState;
+import org.apache.iotdb.postgresql.session.CancelRegistry;
+import org.apache.iotdb.postgresql.session.PgSession;
+import org.apache.iotdb.postgresql.session.QueryProtocol;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.TSProtocolVersion;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.SimpleChannelInboundHandler;
+import io.netty.handler.codec.DecoderException;
+import io.netty.handler.ssl.SslHandler;
+import io.netty.util.concurrent.ScheduledFuture;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.net.ssl.SSLEngine;
+
+import java.net.InetSocketAddress;
+import java.time.DateTimeException;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/** One client connection: the startup phase, the login and then the query layer. */
+final class PgConnectionHandler extends SimpleChannelInboundHandler {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(PgConnectionHandler.class);
+
+ private enum State {
+ AWAIT_STARTUP,
+ PASSWORD,
+ READY,
+ CLOSED
+ }
+
+ private final PgServerContext context;
+ private final PgFrameDecoder decoder;
+ private final SerialExecutor serial;
+
+ private volatile State state = State.AWAIT_STARTUP;
+ private boolean tls;
+ private String user;
+ private final Map startupParameters = new LinkedHashMap<>();
+ private ExternalServiceClientSession dbSession;
+ private CancelRegistry.Key key;
+ private QueryProtocol protocol;
+ private ScheduledFuture> startupTimeout;
+
+ PgConnectionHandler(PgServerContext context, PgFrameDecoder decoder) {
+ this.context = context;
+ this.decoder = decoder;
+ this.serial = new SerialExecutor(context.workers);
+ }
+
+ @Override
+ public void channelActive(ChannelHandlerContext ctx) {
+ if (context.active.incrementAndGet() > context.config.maxConnections()) {
+ context.refusedConnections.incrementAndGet();
+ fail(
+ ctx, PgException.fatal(SqlState.TOO_MANY_CONNECTIONS, "sorry, too many clients already"));
+ return;
+ }
+ startupTimeout =
+ ctx.executor()
+ .schedule(
+ () -> {
+ if (state != State.READY && state != State.CLOSED) {
+ ctx.close();
+ }
+ },
+ context.config.startupTimeout().toMillis(),
+ TimeUnit.MILLISECONDS);
+ ctx.fireChannelActive();
+ }
+
+ @Override
+ public void channelInactive(ChannelHandlerContext ctx) {
+ context.active.decrementAndGet();
+ state = State.CLOSED;
+ if (startupTimeout != null) {
+ startupTimeout.cancel(false);
+ }
+ serial.execute(this::release);
+ ctx.fireChannelInactive();
+ }
+
+ private void release() {
+ if (key != null) {
+ context.cancels.unregister(key);
+ }
+ if (protocol != null) {
+ protocol.close();
+ }
+ if (dbSession != null) {
+ SessionManager.getInstance()
+ .closeSession(dbSession, Coordinator.getInstance()::cleanupQueryExecution);
+ }
+ }
+
+ @Override
+ protected void channelRead0(ChannelHandlerContext ctx, FrontendMessage message) {
+ try {
+ switch (state) {
+ case AWAIT_STARTUP:
+ startup(ctx, message);
+ return;
+ case PASSWORD:
+ authentication(ctx, message);
+ return;
+ case READY:
+ if (message.type() == 'X') {
+ ctx.close();
+ return;
+ }
+ serial.execute(() -> protocol.handle(message, ctx.channel()));
+ return;
+ default:
+ // closed: ignore late messages
+ }
+ } catch (PgException e) {
+ fail(ctx, e);
+ }
+ }
+
+ // ---------------------------------------------------------------------------- startup phase
+
+ private void startup(ChannelHandlerContext ctx, FrontendMessage message) throws PgException {
+ if (!message.isStartupPacket()) {
+ throw PgException.fatal(SqlState.PROTOCOL_VIOLATION, "expected a startup message");
+ }
+ int code = message.readInt32();
+ switch (code) {
+ case FrontendMessage.SSL_REQUEST:
+ sslRequest(ctx);
+ return;
+ case FrontendMessage.GSSENC_REQUEST:
+ ByteBuf no = ctx.alloc().buffer(1);
+ BackendMessages.sslResponse(no, false);
+ ctx.writeAndFlush(no);
+ return;
+ case FrontendMessage.CANCEL_REQUEST:
+ int processId = message.readInt32();
+ int secretKey = message.readInt32();
+ context.cancels.cancel(processId, secretKey);
+ ctx.close();
+ return;
+ default:
+ startupMessage(ctx, message, code);
+ }
+ }
+
+ private void sslRequest(ChannelHandlerContext ctx) throws PgException {
+ if (tls) {
+ throw PgException.fatal(SqlState.PROTOCOL_VIOLATION, "SSL was already negotiated");
+ }
+ ByteBuf answer = ctx.alloc().buffer(1);
+ if (context.sslContext == null) {
+ BackendMessages.sslResponse(answer, false);
+ ctx.writeAndFlush(answer);
+ return;
+ }
+ SSLEngine engine = context.sslContext.createSSLEngine();
+ engine.setUseClientMode(false);
+ TlsPolicy.restrict(engine);
+ // startTls: the 'S' still goes out unencrypted, everything after it is TLS
+ ctx.pipeline().addFirst("tls", new SslHandler(engine, true));
+ tls = true;
+ BackendMessages.sslResponse(answer, true);
+ ctx.writeAndFlush(answer);
+ }
+
+ private void startupMessage(ChannelHandlerContext ctx, FrontendMessage message, int code)
+ throws PgException {
+ int major = code >>> 16;
+ int minor = code & 0xFFFF;
+ if (major != 3) {
+ throw PgException.fatal(
+ SqlState.PROTOCOL_VIOLATION, "unsupported frontend protocol " + major + "." + minor);
+ }
+ if (!tls && !context.loopback) {
+ // off loopback every connection negotiates TLS first
+ context.refusedUnencrypted.incrementAndGet();
+ throw PgException.fatal(
+ SqlState.INVALID_AUTHORIZATION,
+ "encryption required: connect with SSL (sslmode=require)");
+ }
+ List unsupportedOptions = new ArrayList<>();
+ while (message.remaining() > 1) {
+ String name = message.readCString();
+ String value = message.readCString();
+ if (name.startsWith("_pq_.")) {
+ unsupportedOptions.add(name);
+ } else {
+ startupParameters.put(name.toLowerCase(Locale.ROOT), value);
+ }
+ }
+ user = startupParameters.get("user");
+ if (user == null || user.isEmpty()) {
+ throw PgException.fatal(SqlState.INVALID_AUTHORIZATION, "no PostgreSQL user name specified");
+ }
+ String encoding = startupParameters.get("client_encoding");
+ if (encoding != null && !isUtf8(encoding)) {
+ throw PgException.fatal(
+ SqlState.FEATURE_NOT_SUPPORTED, "only client_encoding UTF8 is supported");
+ }
+ decoder.startupComplete();
+ ByteBuf out = ctx.alloc().buffer();
+ if (minor > 0 || !unsupportedOptions.isEmpty()) {
+ BackendMessages.negotiateProtocolVersion(out, 0, unsupportedOptions);
+ }
+ InetSocketAddress remote =
+ ctx.channel().remoteAddress() instanceof InetSocketAddress
+ ? (InetSocketAddress) ctx.channel().remoteAddress()
+ : new InetSocketAddress(0); // not a TCP channel (tests)
+ dbSession = new ExternalServiceClientSession("postgresql", remote);
+ // The password inside TLS (off loopback every connection is encrypted, see above). The
+ // database stores no SCRAM verifiers, so SCRAM-SHA-256 cannot be offered.
+ state = State.PASSWORD;
+ BackendMessages.authenticationCleartextPassword(out);
+ ctx.writeAndFlush(out);
+ }
+
+ private static boolean isUtf8(String encoding) {
+ String normalized = encoding.replace("'", "").replace("-", "").trim().toUpperCase(Locale.ROOT);
+ return normalized.equals("UTF8") || normalized.equals("UNICODE");
+ }
+
+ // ---------------------------------------------------------------------------- authentication
+
+ private void authentication(ChannelHandlerContext ctx, FrontendMessage message)
+ throws PgException {
+ if (message.type() != 'p') {
+ throw PgException.fatal(SqlState.PROTOCOL_VIOLATION, "expected an authentication message");
+ }
+ if (state != State.PASSWORD) {
+ throw PgException.fatal(SqlState.PROTOCOL_VIOLATION, "unexpected authentication message");
+ }
+ String password = message.readCString();
+ state = State.CLOSED; // until the login is done; no further message is valid
+ serial.execute(() -> passwordLogin(ctx, password));
+ }
+
+ private void passwordLogin(ChannelHandlerContext ctx, String password) {
+ try {
+ BasicOpenSessionResp response =
+ SessionManager.getInstance()
+ .login(
+ dbSession,
+ user,
+ password,
+ zone().getId(),
+ TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3,
+ IoTDBConstant.ClientVersion.V_1_0,
+ SqlDialect.TABLE);
+ if (response.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
+ loginFailed(ctx, response);
+ return;
+ }
+ loggedIn(ctx, ctx.alloc().buffer());
+ } catch (RuntimeException e) {
+ LOGGER.warn("PostgreSQL login failed", e);
+ fail(ctx, PgException.fatal(SqlState.CONNECTION_FAILURE, "login failed"));
+ }
+ }
+
+ private void loginFailed(ChannelHandlerContext ctx, BasicOpenSessionResp response) {
+ context.failedLogins.incrementAndGet();
+ int code = response.getCode();
+ String sqlState =
+ code == TSStatusCode.USER_LOGIN_LOCKED.getStatusCode()
+ || code == TSStatusCode.NO_PERMISSION.getStatusCode()
+ ? SqlState.INVALID_AUTHORIZATION
+ : code == TSStatusCode.EXECUTE_STATEMENT_ERROR.getStatusCode()
+ ? SqlState.CONNECTION_FAILURE
+ : SqlState.INVALID_PASSWORD;
+ fail(ctx, PgException.fatal(sqlState, response.getMessage()));
+ }
+
+ /** AuthenticationOk, the parameters, BackendKeyData and ReadyForQuery. */
+ private void loggedIn(ChannelHandlerContext ctx, ByteBuf out) {
+ PgSession session = new PgSession(dbSession, user, zone());
+ startupParameters.forEach(session::setParameter);
+ BackendMessages.authenticationOk(out);
+ Map reported = new LinkedHashMap<>();
+ reported.put("server_version", context.serverVersion);
+ reported.put("server_encoding", "UTF8");
+ reported.put("client_encoding", "UTF8");
+ reported.put("DateStyle", "ISO, MDY");
+ reported.put("IntervalStyle", "postgres");
+ reported.put("TimeZone", session.zone().getId());
+ reported.put("integer_datetimes", "on");
+ reported.put("standard_conforming_strings", "on");
+ reported.put("is_superuser", "off");
+ reported.put("session_authorization", user);
+ reported.put("application_name", startupParameters.getOrDefault("application_name", ""));
+ reported.forEach(
+ (name, value) -> {
+ BackendMessages.parameterStatus(out, name, value);
+ session.setParameter(name.toLowerCase(Locale.ROOT), value);
+ });
+ session.markStartup();
+ protocol = context.protocols.apply(session);
+ key = context.cancels.register(protocol::cancel);
+ session.setProcessId(key.processId());
+ BackendMessages.backendKeyData(out, key.processId(), key.secretKey());
+ BackendMessages.readyForQuery(out, 'I');
+ state = State.READY;
+ if (startupTimeout != null) {
+ startupTimeout.cancel(false);
+ }
+ ctx.writeAndFlush(out);
+ }
+
+ private ZoneId zone() {
+ String zone = startupParameters.get("timezone");
+ if (zone != null) {
+ try {
+ return ZoneId.of(zone.replace("'", "").trim());
+ } catch (DateTimeException e) {
+ // fall back to the server's zone
+ }
+ }
+ return ZoneId.systemDefault();
+ }
+
+ // ---------------------------------------------------------------------------- errors
+
+ @Override
+ public void userEventTriggered(ChannelHandlerContext ctx, Object event) {
+ if (event instanceof io.netty.handler.timeout.IdleStateEvent) {
+ // only connections without a running query are idle
+ if (state != State.READY || protocol == null || !protocol.busy()) {
+ fail(
+ ctx,
+ PgException.fatal(
+ SqlState.ADMIN_SHUTDOWN, "terminating connection due to idle timeout"));
+ }
+ return;
+ }
+ ctx.fireUserEventTriggered(event);
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+ Throwable root =
+ cause instanceof DecoderException && cause.getCause() != null ? cause.getCause() : cause;
+ if (root instanceof PgException) {
+ fail(ctx, (PgException) root);
+ } else {
+ LOGGER.debug("PostgreSQL connection {} failed", ctx.channel().remoteAddress(), cause);
+ ctx.close();
+ }
+ }
+
+ /** Sends the error; a FATAL one closes the connection afterwards. */
+ private void fail(ChannelHandlerContext ctx, PgException error) {
+ if (error.isFatal()) {
+ state = State.CLOSED;
+ }
+ ByteBuf out = ctx.alloc().buffer();
+ BackendMessages.errorResponse(out, error);
+ if (error.isFatal()) {
+ ctx.writeAndFlush(out).addListener(ChannelFutureListener.CLOSE);
+ } else {
+ ctx.writeAndFlush(out);
+ }
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgServer.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgServer.java
new file mode 100644
index 0000000000000..c59a189a75399
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgServer.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.server;
+
+import org.apache.iotdb.postgresql.protocol.PgFrameDecoder;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.timeout.IdleStateHandler;
+import io.netty.util.concurrent.DefaultThreadFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.InetSocketAddress;
+import java.util.concurrent.TimeUnit;
+
+/** The listening endpoint: Netty event loops, TLS on demand, timeouts, back pressure. */
+public final class PgServer {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(PgServer.class);
+
+ /** Largest client message; Bind messages with large values stay well below. */
+ static final int MAX_MESSAGE_SIZE = 64 * 1024 * 1024;
+
+ private final PgServerContext context;
+ private EventLoopGroup boss;
+ private EventLoopGroup workers;
+ private Channel channel;
+
+ public PgServer(PgServerContext context) {
+ this.context = context;
+ }
+
+ /** Binds the endpoint; fails (with the address named) when the port cannot be opened. */
+ public void start() throws InterruptedException {
+ boss = new NioEventLoopGroup(1, new DefaultThreadFactory("pg-service-accept", true));
+ workers =
+ new NioEventLoopGroup(
+ Math.min(4, Runtime.getRuntime().availableProcessors()),
+ new DefaultThreadFactory("pg-service-io", true));
+ long idleMillis = context.config.idleTimeout().toMillis();
+ ServerBootstrap bootstrap =
+ new ServerBootstrap()
+ .group(boss, workers)
+ .channel(NioServerSocketChannel.class)
+ .childOption(ChannelOption.TCP_NODELAY, true)
+ .childHandler(
+ new ChannelInitializer() {
+ @Override
+ protected void initChannel(SocketChannel ch) {
+ PgFrameDecoder decoder = new PgFrameDecoder(MAX_MESSAGE_SIZE);
+ ch.pipeline()
+ // the connection handler decides whether an idle connection is closed
+ .addLast(
+ "idle", new IdleStateHandler(idleMillis, 0, 0, TimeUnit.MILLISECONDS))
+ .addLast("frames", decoder)
+ .addLast("connection", new PgConnectionHandler(context, decoder));
+ }
+ });
+ InetSocketAddress address =
+ context.config.isWildcard()
+ ? new InetSocketAddress(context.config.port())
+ : new InetSocketAddress(context.config.address(), context.config.port());
+ try {
+ channel = bootstrap.bind(address).sync().channel();
+ } catch (RuntimeException | InterruptedException e) {
+ stop();
+ throw e;
+ }
+ LOGGER.info("PostgreSQL endpoint listening on {}", channel.localAddress());
+ }
+
+ /** The bound address (with the actual port when port 0 was configured). */
+ public InetSocketAddress localAddress() {
+ return channel == null ? null : (InetSocketAddress) channel.localAddress();
+ }
+
+ public void stop() {
+ if (channel != null) {
+ channel.close().syncUninterruptibly();
+ channel = null;
+ }
+ if (boss != null) {
+ boss.shutdownGracefully(0, 2, TimeUnit.SECONDS);
+ boss = null;
+ }
+ if (workers != null) {
+ workers.shutdownGracefully(0, 2, TimeUnit.SECONDS);
+ workers = null;
+ }
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgServerContext.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgServerContext.java
new file mode 100644
index 0000000000000..8e476d9db7161
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/PgServerContext.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.server;
+
+import org.apache.iotdb.postgresql.config.PostgreSqlServiceConfig;
+import org.apache.iotdb.postgresql.session.CancelRegistry;
+import org.apache.iotdb.postgresql.session.PgSession;
+import org.apache.iotdb.postgresql.session.QueryProtocol;
+
+import javax.net.ssl.SSLContext;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Function;
+
+/** What every connection of one server shares. */
+public final class PgServerContext {
+
+ final PostgreSqlServiceConfig config;
+ final SSLContext sslContext;
+ final boolean loopback;
+ final CancelRegistry cancels = new CancelRegistry();
+ final Executor workers;
+ final Function protocols;
+ final String serverVersion;
+
+ final java.util.concurrent.atomic.AtomicInteger active =
+ new java.util.concurrent.atomic.AtomicInteger();
+ final AtomicLong refusedConnections = new AtomicLong();
+ final AtomicLong refusedUnencrypted = new AtomicLong();
+ final AtomicLong failedLogins = new AtomicLong();
+
+ /**
+ * @param sslContext the TLS context, or {@code null} when no key store is configured (only
+ * allowed on loopback)
+ * @param protocols creates the query layer of a logged-in connection
+ * @param serverVersion the reported {@code server_version}
+ */
+ public PgServerContext(
+ PostgreSqlServiceConfig config,
+ SSLContext sslContext,
+ Executor workers,
+ Function protocols,
+ String serverVersion) {
+ this.config = config;
+ this.sslContext = sslContext;
+ this.loopback = config.isLoopback();
+ this.workers = workers;
+ this.protocols = protocols;
+ this.serverVersion = serverVersion;
+ }
+
+ public int openConnections() {
+ return active.get();
+ }
+
+ public long refusedConnections() {
+ return refusedConnections.get();
+ }
+
+ public long refusedUnencrypted() {
+ return refusedUnencrypted.get();
+ }
+
+ public long failedLogins() {
+ return failedLogins.get();
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/SerialExecutor.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/SerialExecutor.java
new file mode 100644
index 0000000000000..3e649f409af28
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/SerialExecutor.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.server;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+import java.util.concurrent.Executor;
+
+/**
+ * Runs one connection's tasks one after another on a shared pool, so blocking work (logins,
+ * queries) leaves Netty's event loop free while the connection's messages keep their order.
+ */
+final class SerialExecutor implements Executor {
+
+ private final Executor pool;
+ private final Queue tasks = new ArrayDeque<>();
+ private boolean running;
+
+ SerialExecutor(Executor pool) {
+ this.pool = pool;
+ }
+
+ @Override
+ public void execute(Runnable task) {
+ synchronized (tasks) {
+ tasks.add(task);
+ if (running) {
+ return;
+ }
+ running = true;
+ }
+ pool.execute(this::drain);
+ }
+
+ private void drain() {
+ while (true) {
+ Runnable next;
+ synchronized (tasks) {
+ next = tasks.poll();
+ if (next == null) {
+ running = false;
+ return;
+ }
+ }
+ try {
+ next.run();
+ } catch (RuntimeException e) {
+ // a task reports its own errors to the client; never stop the queue
+ }
+ }
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/TlsPolicy.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/TlsPolicy.java
new file mode 100644
index 0000000000000..f04cef26bf508
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/server/TlsPolicy.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.server;
+
+import javax.net.ssl.SSLEngine;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * The TLS settings of the PostgreSQL endpoint: TLS 1.2 and 1.3 with AEAD cipher suites only. Older
+ * protocols and CBC suites are never enabled, even if the JVM still offers them.
+ */
+final class TlsPolicy {
+
+ static final List PROTOCOLS =
+ Collections.unmodifiableList(Arrays.asList("TLSv1.3", "TLSv1.2"));
+
+ static final List CIPHER_SUITES =
+ Collections.unmodifiableList(
+ Arrays.asList(
+ "TLS_AES_256_GCM_SHA384",
+ "TLS_AES_128_GCM_SHA256",
+ "TLS_CHACHA20_POLY1305_SHA256",
+ "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
+ "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
+ "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
+ "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
+ "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
+ "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256"));
+
+ private TlsPolicy() {}
+
+ /** Restricts the engine to the allowed protocols and suites; never falls back to weaker ones. */
+ static void restrict(SSLEngine engine) {
+ String[] protocols = supported(PROTOCOLS, engine.getSupportedProtocols());
+ String[] suites = supported(CIPHER_SUITES, engine.getSupportedCipherSuites());
+ if (protocols.length == 0 || suites.length == 0) {
+ throw new IllegalStateException(
+ "The JVM supports none of the allowed TLS protocols " + PROTOCOLS + " or cipher suites");
+ }
+ engine.setEnabledProtocols(protocols);
+ engine.setEnabledCipherSuites(suites);
+ }
+
+ private static String[] supported(List allowed, String[] supportedByJvm) {
+ Set supported = new HashSet<>(Arrays.asList(supportedByJvm));
+ List result = new ArrayList<>();
+ for (String value : allowed) {
+ if (supported.contains(value)) {
+ result.add(value);
+ }
+ }
+ return result.toArray(new String[0]);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/CancelRegistry.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/CancelRegistry.java
new file mode 100644
index 0000000000000..fde7eb44e4718
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/CancelRegistry.java
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import java.security.SecureRandom;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Process ids and secret keys of the open connections (BackendKeyData), so that a CancelRequest on
+ * a new connection can find the connection whose query it cancels.
+ */
+public final class CancelRegistry {
+
+ private static final SecureRandom RANDOM = new SecureRandom();
+
+ private static final class Entry {
+ final int secretKey;
+ final Runnable cancel;
+
+ Entry(int secretKey, Runnable cancel) {
+ this.secretKey = secretKey;
+ this.cancel = cancel;
+ }
+ }
+
+ private final AtomicInteger nextProcessId = new AtomicInteger(1000 + RANDOM.nextInt(50_000));
+ private final Map entries = new ConcurrentHashMap<>();
+
+ /** A registered connection: its process id and secret key. */
+ public static final class Key {
+ private final int processId;
+ private final int secretKey;
+
+ Key(int processId, int secretKey) {
+ this.processId = processId;
+ this.secretKey = secretKey;
+ }
+
+ public int processId() {
+ return processId;
+ }
+
+ public int secretKey() {
+ return secretKey;
+ }
+ }
+
+ /** Registers a connection; {@code cancel} stops its running query, if any. */
+ public Key register(Runnable cancel) {
+ int processId = nextProcessId.getAndIncrement();
+ int secretKey = RANDOM.nextInt();
+ entries.put(processId, new Entry(secretKey, cancel));
+ return new Key(processId, secretKey);
+ }
+
+ public void unregister(Key key) {
+ entries.remove(key.processId);
+ }
+
+ /**
+ * Cancels the query of the connection with this process id if the secret key matches; an unknown
+ * id or a wrong key does nothing (as in PostgreSQL, nothing is reported).
+ */
+ public void cancel(int processId, int secretKey) {
+ Entry entry = entries.get(processId);
+ if (entry != null && entry.secretKey == secretKey) {
+ entry.cancel.run();
+ }
+ }
+
+ public int size() {
+ return entries.size();
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/CursorCommand.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/CursorCommand.java
new file mode 100644
index 0000000000000..f9e8cb9265210
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/CursorCommand.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import org.apache.iotdb.postgresql.protocol.PgException;
+import org.apache.iotdb.postgresql.protocol.SqlState;
+
+import java.util.Locale;
+import java.util.Optional;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A SQL cursor statement: {@code DECLARE}, {@code FETCH}, {@code MOVE}, {@code CLOSE}. psycopg's
+ * server-side cursors use them instead of protocol portals. Only forward movement exists: the
+ * engine streams a query once.
+ */
+public final class CursorCommand {
+
+ /** The kind of statement. */
+ public enum Kind {
+ DECLARE,
+ FETCH,
+ MOVE,
+ CLOSE
+ }
+
+ private static final int FLAGS = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;
+ private static final String NAME = "(\"(?:[^\"]++|\"\")++\"|[A-Za-z_][\\w$]*+)";
+
+ private static final Pattern DECLARE =
+ Pattern.compile(
+ "DECLARE\\s+"
+ + NAME
+ + "\\s+((?:(?:BINARY|INSENSITIVE|ASENSITIVE|NO\\s+SCROLL|SCROLL)\\s+)*)"
+ + "CURSOR\\s+(?:(WITH|WITHOUT)\\s+HOLD\\s+)?FOR\\s+(.+)",
+ FLAGS);
+ private static final Pattern FETCH_OR_MOVE =
+ Pattern.compile(
+ "(FETCH|MOVE)"
+ + "(?:\\s+(FORWARD|NEXT|BACKWARD|PRIOR|FIRST|LAST|ABSOLUTE|RELATIVE))?"
+ + "(?:\\s+(ALL|[+-]?\\d+))?"
+ + "(?:\\s+(?:FROM|IN))?"
+ + "\\s+"
+ + NAME,
+ FLAGS);
+ private static final Pattern CLOSE = Pattern.compile("CLOSE\\s+(ALL|" + NAME + ")", FLAGS);
+ private static final Pattern START = Pattern.compile("(DECLARE|FETCH|MOVE|CLOSE)\\b.*", FLAGS);
+
+ private final Kind kind;
+ private final String name;
+ private final long count;
+ private final boolean hold;
+ private final boolean binary;
+ private final String query;
+
+ private CursorCommand(
+ Kind kind, String name, long count, boolean hold, boolean binary, String query) {
+ this.kind = kind;
+ this.name = name;
+ this.count = count;
+ this.hold = hold;
+ this.binary = binary;
+ this.query = query;
+ }
+
+ /** The kind of statement. */
+ public Kind kind() {
+ return kind;
+ }
+
+ /** The cursor's name; {@code null} for {@code CLOSE ALL}. */
+ public String name() {
+ return name;
+ }
+
+ /** FETCH/MOVE: the number of rows, 0 for all. */
+ public long count() {
+ return count;
+ }
+
+ /** DECLARE: {@code WITH HOLD} (the cursor survives the end of the transaction). */
+ public boolean hold() {
+ return hold;
+ }
+
+ /** DECLARE: {@code BINARY} (rows in binary format by default). */
+ public boolean binary() {
+ return binary;
+ }
+
+ /** DECLARE: the query. */
+ public String query() {
+ return query;
+ }
+
+ /**
+ * Parses a cursor statement.
+ *
+ * @return empty if {@code statement} is not a cursor statement
+ * @throws PgException 0A000 for backward or absolute movement and scrollable cursors, 42601 for
+ * malformed cursor statements
+ */
+ public static Optional parse(String statement) throws PgException {
+ String sql = statement.trim();
+ if (!START.matcher(sql).matches()) {
+ return Optional.empty();
+ }
+ Matcher m = DECLARE.matcher(sql);
+ if (m.matches()) {
+ String options = m.group(2).toUpperCase(Locale.ROOT);
+ if (options.matches("(?s).*(^|\\s)SCROLL\\b.*") && !options.contains("NO SCROLL")) {
+ throw new PgException(SqlState.FEATURE_NOT_SUPPORTED, "SCROLL cursors are not supported");
+ }
+ return Optional.of(
+ new CursorCommand(
+ Kind.DECLARE,
+ fold(m.group(1)),
+ 0,
+ "WITH".equalsIgnoreCase(m.group(3)),
+ options.contains("BINARY"),
+ m.group(4).trim()));
+ }
+ m = FETCH_OR_MOVE.matcher(sql);
+ if (m.matches()) {
+ Kind kind = m.group(1).equalsIgnoreCase("FETCH") ? Kind.FETCH : Kind.MOVE;
+ String direction = m.group(2) == null ? "FORWARD" : m.group(2).toUpperCase(Locale.ROOT);
+ if (!direction.equals("FORWARD") && !direction.equals("NEXT")) {
+ throw new PgException(
+ SqlState.FEATURE_NOT_SUPPORTED,
+ kind + " " + direction + " is not supported: cursors only move forward");
+ }
+ long count = 1;
+ if (m.group(3) != null) {
+ if (direction.equals("NEXT")) {
+ throw new PgException(
+ SqlState.SYNTAX_ERROR, "syntax error at or near \"" + m.group(3) + "\"");
+ }
+ if (m.group(3).equalsIgnoreCase("ALL")) {
+ count = 0;
+ } else {
+ count = Long.parseLong(m.group(3));
+ if (count <= 0) {
+ throw new PgException(
+ SqlState.FEATURE_NOT_SUPPORTED,
+ kind + " " + m.group(3) + " is not supported: cursors only move forward");
+ }
+ }
+ }
+ return Optional.of(new CursorCommand(kind, fold(m.group(4)), count, false, false, null));
+ }
+ m = CLOSE.matcher(sql);
+ if (m.matches()) {
+ String name = m.group(1).equalsIgnoreCase("ALL") ? null : fold(m.group(2));
+ return Optional.of(new CursorCommand(Kind.CLOSE, name, 0, false, false, null));
+ }
+ throw new PgException(SqlState.SYNTAX_ERROR, "syntax error in cursor statement");
+ }
+
+ private static String fold(String identifier) {
+ if (identifier.startsWith("\"")) {
+ return identifier.substring(1, identifier.length() - 1).replace("\"\"", "\"");
+ }
+ return identifier.toLowerCase(Locale.ROOT);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/PgSession.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/PgSession.java
new file mode 100644
index 0000000000000..effded653f2b6
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/PgSession.java
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import org.apache.iotdb.db.protocol.session.ExternalServiceClientSession;
+
+import java.time.ZoneId;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * The state of one logged-in connection that the query layer needs: the database session, the
+ * session parameters and the transaction status.
+ */
+public final class PgSession {
+
+ private final ExternalServiceClientSession session;
+ private final String user;
+ private final Map parameters = new LinkedHashMap<>();
+ private final Map startup = new LinkedHashMap<>();
+ private volatile ZoneId startupZone;
+ private volatile ZoneId zone;
+ private volatile char transactionStatus = 'I';
+ private volatile int processId;
+
+ public PgSession(ExternalServiceClientSession session, String user, ZoneId zone) {
+ this.session = session;
+ this.user = user;
+ this.zone = zone;
+ }
+
+ public ExternalServiceClientSession session() {
+ return session;
+ }
+
+ public String user() {
+ return user;
+ }
+
+ public ZoneId zone() {
+ return zone;
+ }
+
+ public void setZone(ZoneId zone) {
+ this.zone = zone;
+ // the engine reads the zone from the database session (time literals, date_bin origins)
+ session.setZoneId(zone);
+ }
+
+ /** The process id of BackendKeyData ({@code pg_backend_pid()}). */
+ public int processId() {
+ return processId;
+ }
+
+ public void setProcessId(int processId) {
+ this.processId = processId;
+ }
+
+ /** 'I' idle, 'T' in a transaction block, 'E' in a failed transaction block. */
+ public char transactionStatus() {
+ return transactionStatus;
+ }
+
+ public void setTransactionStatus(char status) {
+ this.transactionStatus = status;
+ }
+
+ /** Session parameters by lower-case name (set at startup or with SET). */
+ public synchronized String parameter(String name) {
+ return parameters.get(name);
+ }
+
+ public synchronized void setParameter(String name, String value) {
+ parameters.put(name, value);
+ }
+
+ /** Records the current parameters as the values {@code RESET} returns to (end of startup). */
+ public synchronized void markStartup() {
+ startup.clear();
+ startup.putAll(parameters);
+ startupZone = zone;
+ }
+
+ /**
+ * {@code RESET name}: the value at the end of startup, or none.
+ *
+ * @return the value now in effect, {@code null} if the parameter has none
+ */
+ public synchronized String reset(String name) {
+ String value = startup.get(name);
+ if (value == null) {
+ parameters.remove(name);
+ } else {
+ parameters.put(name, value);
+ }
+ if (name.equals("timezone") && startupZone != null) {
+ setZone(startupZone);
+ }
+ return value;
+ }
+
+ /** {@code RESET ALL}: every parameter back to its value at the end of startup. */
+ public synchronized void resetAll() {
+ parameters.clear();
+ parameters.putAll(startup);
+ if (startupZone != null) {
+ setZone(startupZone);
+ }
+ }
+
+ public synchronized Map parameters() {
+ return Collections.unmodifiableMap(new LinkedHashMap<>(parameters));
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/QueryProtocol.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/QueryProtocol.java
new file mode 100644
index 0000000000000..a3c5224a8a7eb
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/QueryProtocol.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import org.apache.iotdb.postgresql.protocol.FrontendMessage;
+
+import io.netty.channel.Channel;
+
+/**
+ * Handles the messages of a logged-in connection (simple and extended query protocol). Called on
+ * the connection's own ordered executor, never on Netty's event loop, so it may block.
+ */
+public interface QueryProtocol {
+
+ /** Handles one message of the ready state ('Q', 'P', 'B', 'D', 'E', 'S', 'C', 'H'). */
+ void handle(FrontendMessage message, Channel channel);
+
+ /** True while a query runs or a result is being sent; idle timeouts do not apply then. */
+ boolean busy();
+
+ /** Cancels the running query, if any (CancelRequest); may be called from any thread. */
+ void cancel();
+
+ /** Releases everything when the connection closes. */
+ void close();
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SearchPath.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SearchPath.java
new file mode 100644
index 0000000000000..349cc46546c98
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SearchPath.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+
+/** The {@code search_path} setting as a list of schema (database) names. */
+public final class SearchPath {
+
+ private SearchPath() {}
+
+ /**
+ * The folded names of a {@code search_path} value such as {@code "$user", public, mydb}; {@code
+ * $user} is left out (users have no schema of their own). Empty for no value.
+ */
+ public static List parse(String value) {
+ if (value == null || value.trim().isEmpty()) {
+ return Collections.emptyList();
+ }
+ List names = new ArrayList<>();
+ for (String part : value.split(",")) {
+ String name = part.trim();
+ if (name.length() >= 2 && name.startsWith("\"") && name.endsWith("\"")) {
+ name = name.substring(1, name.length() - 1).replace("\"\"", "\"");
+ } else if (name.length() >= 2 && name.startsWith("'") && name.endsWith("'")) {
+ name = name.substring(1, name.length() - 1);
+ } else {
+ name = name.toLowerCase(Locale.ROOT);
+ }
+ if (!name.isEmpty() && !name.equals("$user")) {
+ names.add(name);
+ }
+ }
+ return names;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SessionCommands.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SessionCommands.java
new file mode 100644
index 0000000000000..af4e4792ea184
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SessionCommands.java
@@ -0,0 +1,375 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import org.apache.iotdb.postgresql.protocol.PgException;
+import org.apache.iotdb.postgresql.protocol.SqlState;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.DateTimeException;
+import java.time.ZoneId;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Statements handled by the adapter without the engine: transaction commands, SET/RESET/SHOW,
+ * DISCARD, UNLISTEN, DEALLOCATE and the pool-reset function call of Npgsql. SQL cursors ({@code
+ * DECLARE}, {@code FETCH}, {@code MOVE}, {@code CLOSE}) are {@link CursorCommand}s.
+ */
+public final class SessionCommands {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SessionCommands.class);
+
+ /** What a session command produces. */
+ public static final class Result {
+ private final String tag;
+ private final String showName;
+ private final String showValue;
+ private final Map parameterStatus;
+
+ Result(String tag, String showName, String showValue, Map parameterStatus) {
+ this.tag = tag;
+ this.showName = showName;
+ this.showValue = showValue;
+ this.parameterStatus = parameterStatus;
+ }
+
+ /** The CommandComplete tag. */
+ public String tag() {
+ return tag;
+ }
+
+ /** For SHOW and the function call: the column name, else {@code null}. */
+ public String showName() {
+ return showName;
+ }
+
+ public String showValue() {
+ return showValue;
+ }
+
+ /** Reported parameters that changed (ParameterStatus to send). */
+ public Map parameterStatus() {
+ return parameterStatus;
+ }
+ }
+
+ /** Parameters reported in ParameterStatus, with their names as PostgreSQL writes them. */
+ private static final Map REPORTED = new LinkedHashMap<>();
+
+ static {
+ for (String name :
+ Arrays.asList(
+ "application_name",
+ "client_encoding",
+ "DateStyle",
+ "IntervalStyle",
+ "TimeZone",
+ "standard_conforming_strings",
+ "integer_datetimes",
+ "is_superuser",
+ "server_encoding",
+ "server_version",
+ "session_authorization")) {
+ REPORTED.put(name.toLowerCase(Locale.ROOT), name);
+ }
+ }
+
+ private static final Set READ_ONLY =
+ new HashSet<>(
+ Arrays.asList(
+ "server_version",
+ "server_version_num",
+ "server_encoding",
+ "integer_datetimes",
+ "is_superuser",
+ "session_authorization"));
+
+ private static final int FLAGS = Pattern.CASE_INSENSITIVE | Pattern.DOTALL;
+ private static final Pattern BEGIN = Pattern.compile("(BEGIN|START\\s+TRANSACTION)\\b.*", FLAGS);
+ private static final Pattern COMMIT =
+ Pattern.compile("(COMMIT|END)(\\s+(WORK|TRANSACTION))?", FLAGS);
+ private static final Pattern ROLLBACK =
+ Pattern.compile("(ROLLBACK|ABORT)(\\s+(WORK|TRANSACTION))?", FLAGS);
+ private static final Pattern SET_TIME_ZONE =
+ Pattern.compile("SET\\s+(?:SESSION\\s+|LOCAL\\s+)?TIME\\s+ZONE\\s+(.+)", FLAGS);
+ private static final Pattern SET_AUTHORIZATION_DEFAULT =
+ Pattern.compile("SET\\s+SESSION\\s+AUTHORIZATION\\s+DEFAULT", FLAGS);
+ private static final Pattern SET =
+ Pattern.compile(
+ "SET\\s++(?:SESSION\\s++|LOCAL\\s++)?+([A-Za-z_][\\w.]*+)(?:\\s*+=|\\s++TO\\s)\\s*+(.+)",
+ FLAGS);
+ private static final Pattern RESET = Pattern.compile("RESET\\s+([A-Za-z_][\\w.]*)", FLAGS);
+ private static final Pattern SHOW = Pattern.compile("SHOW\\s+([A-Za-z_][\\w.]*)", FLAGS);
+ private static final Pattern DISCARD =
+ Pattern.compile("DISCARD\\s+(ALL|TEMP|TEMPORARY|SEQUENCES|PLANS)", FLAGS);
+ private static final Pattern UNLISTEN = Pattern.compile("UNLISTEN\\s+\\*", FLAGS);
+ private static final Pattern DEALLOCATE =
+ Pattern.compile("DEALLOCATE\\s+(?:PREPARE\\s+)?([A-Za-z_\"][\\w\"]*)", FLAGS);
+ private static final Pattern ADVISORY_UNLOCK_ALL =
+ Pattern.compile("SELECT\\s+(?:pg_catalog\\.)?pg_advisory_unlock_all\\s*\\(\\s*\\)", FLAGS);
+
+ private SessionCommands() {}
+
+ private static final Pattern[] ALL_COMMANDS = {
+ BEGIN,
+ COMMIT,
+ ROLLBACK,
+ SET_TIME_ZONE,
+ SET_AUTHORIZATION_DEFAULT,
+ SET,
+ RESET,
+ SHOW,
+ DISCARD,
+ UNLISTEN,
+ DEALLOCATE,
+ ADVISORY_UNLOCK_ALL
+ };
+
+ /** True if {@code statement} is a session command (nothing is executed). */
+ public static boolean isCommand(String statement) {
+ String sql = statement.trim();
+ for (Pattern pattern : ALL_COMMANDS) {
+ if (pattern.matcher(sql).matches()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /** True if {@code statement} is a SHOW command (it returns one row). */
+ public static boolean isShow(String statement) {
+ return SHOW.matcher(statement.trim()).matches();
+ }
+
+ /** The column name of a SHOW command's result. */
+ public static String showName(String statement) {
+ Matcher m = SHOW.matcher(statement.trim());
+ return m.matches() ? m.group(1) : "?column?";
+ }
+
+ /** The command's result if {@code statement} is a session command, else empty. */
+ public static Optional execute(PgSession session, String statement) throws PgException {
+ String sql = statement.trim();
+ if (BEGIN.matcher(sql).matches()) {
+ session.setTransactionStatus('T');
+ return result("BEGIN");
+ }
+ if (COMMIT.matcher(sql).matches()) {
+ session.setTransactionStatus('I');
+ return result("COMMIT");
+ }
+ if (ROLLBACK.matcher(sql).matches()) {
+ session.setTransactionStatus('I');
+ return result("ROLLBACK");
+ }
+ if (SET_AUTHORIZATION_DEFAULT.matcher(sql).matches()) {
+ return result("SET");
+ }
+ Matcher m = SET_TIME_ZONE.matcher(sql);
+ if (m.matches()) {
+ return Optional.of(set(session, "timezone", m.group(1)));
+ }
+ m = SET.matcher(sql);
+ if (m.matches()) {
+ return Optional.of(set(session, m.group(1), m.group(2)));
+ }
+ m = RESET.matcher(sql);
+ if (m.matches()) {
+ String name = m.group(1).toLowerCase(Locale.ROOT);
+ if (name.equals("all")) {
+ session.resetAll();
+ return result("RESET");
+ }
+ if (READ_ONLY.contains(name)) {
+ throw new PgException(
+ SqlState.INVALID_PARAMETER_VALUE, "parameter \"" + m.group(1) + "\" cannot be changed");
+ }
+ String value = session.reset(name);
+ String reported = REPORTED.get(name);
+ if (reported != null && value != null) {
+ return Optional.of(
+ new Result("RESET", null, null, Collections.singletonMap(reported, value)));
+ }
+ return result("RESET");
+ }
+ m = SHOW.matcher(sql);
+ if (m.matches()) {
+ String name = m.group(1).toLowerCase(Locale.ROOT);
+ return Optional.of(
+ new Result("SHOW", m.group(1), show(session, name), Collections.emptyMap()));
+ }
+ m = DISCARD.matcher(sql);
+ if (m.matches()) {
+ return result("DISCARD " + m.group(1).toUpperCase(Locale.ROOT));
+ }
+ if (UNLISTEN.matcher(sql).matches()) {
+ return result("UNLISTEN");
+ }
+ m = DEALLOCATE.matcher(sql);
+ if (m.matches()) {
+ return result(m.group(1).equalsIgnoreCase("ALL") ? "DEALLOCATE ALL" : "DEALLOCATE");
+ }
+ if (ADVISORY_UNLOCK_ALL.matcher(sql).matches()) {
+ return Optional.of(
+ new Result("SELECT 1", "pg_advisory_unlock_all", "", Collections.emptyMap()));
+ }
+ return Optional.empty();
+ }
+
+ private static Optional result(String tag) {
+ return Optional.of(new Result(tag, null, null, Collections.emptyMap()));
+ }
+
+ private static Result set(PgSession session, String rawName, String rawValue) throws PgException {
+ String name = rawName.toLowerCase(Locale.ROOT);
+ String value = unquote(rawValue.trim());
+ if (READ_ONLY.contains(name)) {
+ throw new PgException(
+ SqlState.INVALID_PARAMETER_VALUE, "parameter \"" + rawName + "\" cannot be changed");
+ }
+ switch (name) {
+ case "client_encoding":
+ if (!value.replace("-", "").equalsIgnoreCase("UTF8")
+ && !value.equalsIgnoreCase("UNICODE")) {
+ throw new PgException(
+ SqlState.FEATURE_NOT_SUPPORTED, "only client_encoding UTF8 is supported");
+ }
+ value = "UTF8";
+ break;
+ case "bytea_output":
+ if (!value.equalsIgnoreCase("hex")) {
+ throw new PgException(
+ SqlState.FEATURE_NOT_SUPPORTED, "only bytea_output hex is supported");
+ }
+ break;
+ case "intervalstyle":
+ if (!value.equalsIgnoreCase("postgres")) {
+ throw new PgException(
+ SqlState.FEATURE_NOT_SUPPORTED, "only IntervalStyle postgres is supported");
+ }
+ break;
+ case "datestyle":
+ if (!value.toUpperCase(Locale.ROOT).startsWith("ISO")) {
+ throw new PgException(SqlState.FEATURE_NOT_SUPPORTED, "only DateStyle ISO is supported");
+ }
+ value = "ISO, MDY";
+ break;
+ case "timezone":
+ try {
+ ZoneId zone =
+ value.equalsIgnoreCase("LOCAL") || value.equalsIgnoreCase("DEFAULT")
+ ? ZoneId.systemDefault()
+ : ZoneId.of(value);
+ session.setZone(zone);
+ value = zone.getId();
+ } catch (DateTimeException e) {
+ throw new PgException(
+ SqlState.INVALID_PARAMETER_VALUE,
+ "invalid value for parameter \"TimeZone\": \"" + value + "\"");
+ }
+ break;
+ case "statement_timeout":
+ try {
+ StatementTimeout.parse(value);
+ } catch (IllegalArgumentException e) {
+ throw new PgException(SqlState.INVALID_PARAMETER_VALUE, e.getMessage());
+ }
+ break;
+ case "search_path":
+ case "application_name":
+ case "extra_float_digits":
+ case "lock_timeout":
+ case "client_min_messages":
+ case "standard_conforming_strings":
+ case "ssl_renegotiation_limit":
+ // kept; the query layer reads the ones it honours
+ break;
+ default:
+ LOGGER.debug("Unknown PostgreSQL parameter {} accepted and ignored", name);
+ }
+ session.setParameter(name, value);
+ Map status = Collections.emptyMap();
+ String reported = REPORTED.get(name);
+ if (reported != null) {
+ status = Collections.singletonMap(reported, value);
+ }
+ return new Result("SET", null, null, status);
+ }
+
+ private static String show(PgSession session, String name) throws PgException {
+ switch (name) {
+ case "server_version_num":
+ return "140000";
+ case "timezone":
+ return session.zone().getId();
+ case "transaction_isolation":
+ case "default_transaction_isolation":
+ return "read committed";
+ case "transaction_read_only":
+ case "default_transaction_read_only":
+ return "on";
+ case "standard_conforming_strings":
+ case "integer_datetimes":
+ return "on";
+ case "client_encoding":
+ case "server_encoding":
+ return "UTF8";
+ case "datestyle":
+ return "ISO, MDY";
+ case "intervalstyle":
+ return "postgres";
+ case "max_identifier_length":
+ return "63";
+ case "statement_timeout":
+ String timeout = session.parameter(name);
+ return timeout == null ? "0" : timeout;
+ case "application_name":
+ String application = session.parameter(name);
+ return application == null ? "" : application;
+ default:
+ String value = session.parameter(name);
+ if (value == null) {
+ throw new PgException("42704", "unrecognized configuration parameter \"" + name + "\"");
+ }
+ return value;
+ }
+ }
+
+ private static String unquote(String value) {
+ if (value.length() >= 2
+ && (value.startsWith("'") && value.endsWith("'")
+ || value.startsWith("\"") && value.endsWith("\""))) {
+ char q = value.charAt(0);
+ return value.substring(1, value.length() - 1).replace("" + q + q, "" + q);
+ }
+ return value;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SqlProtocol.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SqlProtocol.java
new file mode 100644
index 0000000000000..0c123f93aa45b
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/SqlProtocol.java
@@ -0,0 +1,931 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.Literal;
+import org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.NullLiteral;
+import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache;
+import org.apache.iotdb.postgresql.execution.ResultCursor;
+import org.apache.iotdb.postgresql.execution.RunningQuery;
+import org.apache.iotdb.postgresql.protocol.BackendMessages;
+import org.apache.iotdb.postgresql.protocol.FrontendMessage;
+import org.apache.iotdb.postgresql.protocol.PgException;
+import org.apache.iotdb.postgresql.protocol.SqlState;
+import org.apache.iotdb.postgresql.sql.PgToTableSql;
+import org.apache.iotdb.postgresql.sql.PgToTableSql.Translation;
+import org.apache.iotdb.postgresql.sql.TranslationContext;
+import org.apache.iotdb.postgresql.types.ParameterDecoder;
+import org.apache.iotdb.postgresql.types.PgType;
+import org.apache.iotdb.postgresql.types.PgValueEncoder;
+import org.apache.iotdb.postgresql.types.TimestampPrecision;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * The query layer: session commands, and queries translated to table-model SQL and run by the
+ * engine, through the simple and the extended query protocol . Runs on the connection's ordered
+ * executor.
+ */
+public final class SqlProtocol implements QueryProtocol {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(SqlProtocol.class);
+
+ /** A parsed statement (Parse). */
+ private static final class Prepared {
+ final String sql;
+ final boolean empty;
+ final boolean command;
+ final Translation translation;
+ final int[] parameterTypes;
+ final CursorCommand cursor;
+
+ Prepared(String sql, boolean empty, boolean command, Translation translation, int[] types) {
+ this(sql, empty, command, translation, types, null);
+ }
+
+ Prepared(
+ String sql,
+ boolean empty,
+ boolean command,
+ Translation translation,
+ int[] types,
+ CursorCommand cursor) {
+ this.sql = sql;
+ this.empty = empty;
+ this.command = command;
+ this.translation = translation;
+ this.parameterTypes = types;
+ this.cursor = cursor;
+ }
+ }
+
+ /** A SQL cursor (DECLARE): a running query fetched by name. */
+ private static final class SqlCursor {
+ final ResultCursor rows;
+ final boolean hold;
+ final boolean binary;
+ final boolean inTransaction;
+
+ SqlCursor(ResultCursor rows, boolean hold, boolean binary, boolean inTransaction) {
+ this.rows = rows;
+ this.hold = hold;
+ this.binary = binary;
+ this.inTransaction = inTransaction;
+ }
+ }
+
+ /** A bound statement (Bind), possibly with a running query. */
+ private static final class Portal {
+ final Prepared statement;
+ final List parameters;
+ final short[] resultFormats;
+ ResultCursor cursor;
+ boolean finished;
+
+ Portal(Prepared statement, List parameters, short[] resultFormats) {
+ this.statement = statement;
+ this.parameters = parameters;
+ this.resultFormats = resultFormats;
+ }
+ }
+
+ private final PgSession session;
+ private final Duration writeTimeout;
+ private final TimestampPrecision precision;
+ private final Map statements = new HashMap<>();
+ private final Map portals = new HashMap<>();
+ private final Map cursors = new HashMap<>();
+ private boolean skipUntilSync;
+ private final AtomicReference running = new AtomicReference<>();
+ private volatile boolean cancelRequested;
+ private volatile boolean busy;
+
+ public SqlProtocol(PgSession session, Duration writeTimeout, TimestampPrecision precision) {
+ this.session = session;
+ this.writeTimeout = writeTimeout;
+ this.precision = precision;
+ }
+
+ // ---------------------------------------------------------------------------- dispatch
+
+ @Override
+ public void handle(FrontendMessage message, Channel channel) {
+ ByteBuf out = channel.alloc().buffer();
+ if (!session.session().isLogin()) {
+ // the session was ended from outside
+ LOGGER.info("PostgreSQL session of {} was ended; closing the connection", session.user());
+ closePortals();
+ closeCursors();
+ BackendMessages.errorResponse(
+ out,
+ PgException.fatal(
+ SqlState.ADMIN_SHUTDOWN, "terminating connection due to administrator command"));
+ channel.writeAndFlush(out).addListener(f -> channel.close());
+ return;
+ }
+ busy = true;
+ try {
+ char type = message.type();
+ if (skipUntilSync && type != 'S' && type != 'Q') {
+ return;
+ }
+ switch (type) {
+ case 'Q':
+ simpleQuery(message.readCString(), channel, out);
+ break;
+ case 'P':
+ parse(message, out);
+ break;
+ case 'B':
+ bind(message, out);
+ break;
+ case 'D':
+ describe(message, out);
+ break;
+ case 'E':
+ execute(message, channel, out);
+ break;
+ case 'C':
+ close(message, out);
+ break;
+ case 'S':
+ sync(out);
+ break;
+ case 'H':
+ break;
+ case 'F':
+ throw new PgException(SqlState.FEATURE_NOT_SUPPORTED, "the function call protocol");
+ case 'd':
+ case 'c':
+ case 'f':
+ throw new PgException(SqlState.FEATURE_NOT_SUPPORTED, "COPY is not supported");
+ default:
+ throw PgException.fatal(
+ SqlState.PROTOCOL_VIOLATION, "invalid frontend message type '" + type + "'");
+ }
+ } catch (PgException e) {
+ fail(e, out);
+ if (e.isFatal()) {
+ busy = false;
+ channel.writeAndFlush(out).addListener(f -> channel.close());
+ return;
+ }
+ if (message.type() != 'Q') {
+ // extended protocol: discard messages until Sync
+ skipUntilSync = true;
+ }
+ } catch (ResultCursor.WriteTimeoutException e) {
+ LOGGER.info(
+ "PostgreSQL client {} stopped reading results for {}; closing the connection",
+ session.user(),
+ writeTimeout);
+ closePortals();
+ out.clear();
+ BackendMessages.errorResponse(
+ out,
+ PgException.fatal(
+ SqlState.ADMIN_SHUTDOWN,
+ "terminating connection because the client stopped reading"));
+ busy = false;
+ channel.writeAndFlush(out).addListener(f -> channel.close());
+ return;
+ } catch (RuntimeException | LinkageError e) {
+ // never leave the client waiting: an unexpected failure is reported like any other error
+ LOGGER.warn("PostgreSQL message '{}' of {} failed", message.type(), session.user(), e);
+ closePortals();
+ out.clear();
+ fail(new PgException(SqlState.INTERNAL_ERROR, "internal error: " + e), out);
+ if (message.type() != 'Q') {
+ skipUntilSync = true;
+ } else {
+ BackendMessages.readyForQuery(out, session.transactionStatus());
+ }
+ } finally {
+ busy = false;
+ }
+ if (out.readableBytes() > 0) {
+ channel.writeAndFlush(out);
+ } else {
+ out.release();
+ }
+ }
+
+ private void fail(PgException e, ByteBuf out) {
+ BackendMessages.errorResponse(out, e);
+ if (session.transactionStatus() == 'T') {
+ session.setTransactionStatus('E');
+ }
+ }
+
+ // ---------------------------------------------------------------------------- simple query
+
+ private void simpleQuery(String sql, Channel channel, ByteBuf out)
+ throws ResultCursor.WriteTimeoutException {
+ skipUntilSync = false;
+ // a simple query closes the unnamed statement and portal
+ statements.remove("");
+ closePortal("");
+ List parts = StatementSplitter.split(sql);
+ if (parts.isEmpty()) {
+ BackendMessages.emptyQueryResponse(out);
+ }
+ for (String statement : parts) {
+ if (statement.isEmpty()) {
+ BackendMessages.emptyQueryResponse(out);
+ continue;
+ }
+ try {
+ if (session.transactionStatus() == 'E' && !isTransactionEnd(statement)) {
+ throw new PgException(
+ "25P02",
+ "current transaction is aborted, commands ignored until end of transaction block");
+ }
+ Prepared prepared = prepare(statement, new int[0]);
+ Portal portal = new Portal(prepared, Collections.emptyList(), new short[0]);
+ try {
+ run(portal, channel, out, 0, true);
+ } finally {
+ if (portal.cursor != null) {
+ portal.cursor.close();
+ }
+ }
+ } catch (PgException e) {
+ fail(e, out);
+ break;
+ }
+ }
+ BackendMessages.readyForQuery(out, session.transactionStatus());
+ }
+
+ private static boolean isTransactionEnd(String statement) {
+ String s = statement.trim().toUpperCase(Locale.ROOT);
+ return s.startsWith("COMMIT")
+ || s.startsWith("ROLLBACK")
+ || s.startsWith("END")
+ || s.startsWith("ABORT");
+ }
+
+ // ---------------------------------------------------------------------------- extended query
+
+ private void parse(FrontendMessage message, ByteBuf out) throws PgException {
+ String name = message.readCString();
+ String sql = message.readCString();
+ int count = message.readInt16();
+ int[] types = new int[count];
+ for (int i = 0; i < count; i++) {
+ types[i] = message.readInt32();
+ }
+ if (!name.isEmpty() && statements.containsKey(name)) {
+ throw new PgException(
+ SqlState.DUPLICATE_PREPARED_STATEMENT,
+ "prepared statement \"" + name + "\" already exists");
+ }
+ if (session.transactionStatus() == 'E' && !isTransactionEnd(sql)) {
+ throw new PgException(
+ "25P02",
+ "current transaction is aborted, commands ignored until end of transaction block");
+ }
+ statements.put(name, prepare(sql, types));
+ BackendMessages.parseComplete(out);
+ }
+
+ private Prepared prepare(String sql, int[] types) throws PgException {
+ List parts = StatementSplitter.split(sql);
+ if (parts.isEmpty() || (parts.size() == 1 && parts.get(0).isEmpty())) {
+ return new Prepared(sql, true, false, null, types);
+ }
+ if (parts.size() > 1) {
+ throw new PgException(
+ SqlState.SYNTAX_ERROR, "cannot insert multiple commands into a prepared statement");
+ }
+ String statement = parts.get(0);
+ Optional cursor = CursorCommand.parse(statement);
+ if (cursor.isPresent()) {
+ Translation query = null;
+ int[] allTypes = types;
+ if (cursor.get().kind() == CursorCommand.Kind.DECLARE) {
+ query = PgToTableSql.translate(cursor.get().query(), translationContext());
+ if (query.parameterCount() > types.length) {
+ allTypes = java.util.Arrays.copyOf(types, query.parameterCount());
+ }
+ }
+ return new Prepared(statement, false, false, query, allTypes, cursor.get());
+ }
+ if (SessionCommands.isCommand(statement)) {
+ return new Prepared(statement, false, true, null, types);
+ }
+ Translation translation =
+ PgToTableSql.translate(statement, translationContext()).withTypedParameters(types);
+ int[] allTypes = types;
+ if (translation.parameterCount() > types.length) {
+ allTypes = java.util.Arrays.copyOf(types, translation.parameterCount());
+ }
+ return new Prepared(statement, false, false, translation, allTypes);
+ }
+
+ private void bind(FrontendMessage message, ByteBuf out) throws PgException {
+ String portalName = message.readCString();
+ String statementName = message.readCString();
+ Prepared statement = statements.get(statementName);
+ if (statement == null) {
+ throw new PgException(
+ SqlState.INVALID_SQL_STATEMENT_NAME,
+ "prepared statement \"" + statementName + "\" does not exist");
+ }
+ short[] parameterFormats = readFormats(message);
+ int count = message.readInt16();
+ if (count != statement.parameterTypes.length) {
+ throw new PgException(
+ SqlState.PROTOCOL_VIOLATION,
+ "bind message supplies "
+ + count
+ + " parameters, but prepared statement \""
+ + statementName
+ + "\" requires "
+ + statement.parameterTypes.length);
+ }
+ ParameterDecoder decoder = new ParameterDecoder(session.zone(), precision);
+ Literal[] values = new Literal[count];
+ for (int i = 0; i < count; i++) {
+ int length = message.readInt32();
+ byte[] value = message.readBytes(length);
+ short format =
+ parameterFormats.length == 0
+ ? 0
+ : parameterFormats.length == 1 ? parameterFormats[0] : parameterFormats[i];
+ values[i] = decoder.decode(statement.parameterTypes[i], format == 1, value);
+ }
+ short[] resultFormats = readFormats(message);
+ List ordered = new ArrayList<>();
+ if (statement.translation != null) {
+ for (int n : statement.translation.parameterOrder()) {
+ ordered.add(values[n - 1]);
+ }
+ }
+ if (!portalName.isEmpty() && portals.containsKey(portalName)) {
+ throw new PgException(
+ SqlState.DUPLICATE_CURSOR, "portal \"" + portalName + "\" already exists");
+ }
+ closePortal(portalName);
+ portals.put(portalName, new Portal(statement, ordered, resultFormats));
+ BackendMessages.bindComplete(out);
+ }
+
+ private static short[] readFormats(FrontendMessage message) throws PgException {
+ int count = message.readInt16();
+ short[] formats = new short[count];
+ for (int i = 0; i < count; i++) {
+ formats[i] = message.readInt16();
+ if (formats[i] != 0 && formats[i] != 1) {
+ throw new PgException(
+ SqlState.PROTOCOL_VIOLATION, "unsupported format code: " + formats[i]);
+ }
+ }
+ return formats;
+ }
+
+ private void describe(FrontendMessage message, ByteBuf out) throws PgException {
+ char kind = (char) message.readByte();
+ String name = message.readCString();
+ if (kind == 'S') {
+ Prepared statement = statements.get(name);
+ if (statement == null) {
+ throw new PgException(
+ SqlState.INVALID_SQL_STATEMENT_NAME,
+ "prepared statement \"" + name + "\" does not exist");
+ }
+ int[] reported = statement.parameterTypes.clone();
+ for (int i = 0; i < reported.length; i++) {
+ if (reported[i] == 0) {
+ reported[i] = PgType.TEXT.oid();
+ }
+ }
+ BackendMessages.parameterDescription(out, reported);
+ describeResult(statement, null, out);
+ return;
+ }
+ if (kind != 'P') {
+ throw new PgException(SqlState.PROTOCOL_VIOLATION, "invalid DESCRIBE message subtype");
+ }
+ Portal portal = portals.get(name);
+ if (portal == null) {
+ // SQL cursors are portals too (psycopg describes its DECLAREd cursor this way)
+ SqlCursor cursor = cursors.get(name);
+ if (cursor == null) {
+ throw new PgException(
+ SqlState.INVALID_CURSOR_NAME, "portal \"" + name + "\" does not exist");
+ }
+ cursor.rows.setFormats(fetchFormats(cursor, null));
+ BackendMessages.rowDescription(out, cursor.rows.fields());
+ return;
+ }
+ describeResult(portal.statement, portal, out);
+ }
+
+ /**
+ * RowDescription or NoData. For a portal the query starts here (Execute continues it); for a
+ * statement the query is planned with NULL parameters and stopped at once.
+ */
+ private void describeResult(Prepared statement, Portal portal, ByteBuf out) throws PgException {
+ if (statement.empty) {
+ BackendMessages.noData(out);
+ return;
+ }
+ if (statement.cursor != null) {
+ if (statement.cursor.kind() == CursorCommand.Kind.FETCH) {
+ SqlCursor cursor = cursor(statement.cursor.name());
+ cursor.rows.setFormats(fetchFormats(cursor, portal));
+ BackendMessages.rowDescription(out, cursor.rows.fields());
+ } else {
+ BackendMessages.noData(out);
+ }
+ return;
+ }
+ if (statement.command) {
+ if (SessionCommands.isShow(statement.sql)) {
+ BackendMessages.rowDescription(
+ out,
+ Collections.singletonList(
+ new BackendMessages.Field(
+ SessionCommands.showName(statement.sql),
+ PgType.TEXT.oid(),
+ PgType.TEXT.size(),
+ (short) 0)));
+ } else {
+ BackendMessages.noData(out);
+ }
+ return;
+ }
+ if (portal != null) {
+ start(portal);
+ BackendMessages.rowDescription(out, portal.cursor.fields());
+ return;
+ }
+ // planned with a value of each declared type: the engine refuses comparisons with untyped
+ // NULLs; untyped parameters are tried as a number, then as text, then as NULL
+ PgException failure = null;
+ boolean untyped = false;
+ for (int n : statement.translation.parameterOrder()) {
+ untyped |= n - 1 >= statement.parameterTypes.length || statement.parameterTypes[n - 1] == 0;
+ }
+ for (int attempt = 0; attempt < (untyped ? 3 : 1); attempt++) {
+ List values = new ArrayList<>();
+ for (int n : statement.translation.parameterOrder()) {
+ int oid = n - 1 < statement.parameterTypes.length ? statement.parameterTypes[n - 1] : 0;
+ values.add(placeholder(oid == 0 ? new int[] {20, 25, 0}[attempt] : oid));
+ }
+ try (RunningQuery query = startQuery(statement.translation, values)) {
+ ResultCursor cursor = new ResultCursor(query, new short[0], encoder());
+ BackendMessages.rowDescription(out, cursor.fields());
+ return;
+ } catch (PgException e) {
+ failure = failure == null ? e : failure;
+ }
+ }
+ throw failure;
+ }
+
+ /** A value of the given parameter type, for planning a statement without its parameters. */
+ private static Literal placeholder(int oid) {
+ switch (oid) {
+ case 16:
+ return new org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.BooleanLiteral(
+ "false");
+ case 20:
+ case 21:
+ case 23:
+ case 26:
+ case 1114:
+ case 1184:
+ return new org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.LongLiteral("0");
+ case 700:
+ case 701:
+ case 1700:
+ return new org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.DoubleLiteral("0");
+ case 25:
+ case 1043:
+ case 19:
+ case 1042:
+ return new org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.StringLiteral("");
+ case 1082:
+ return new org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.StringLiteral(
+ "1970-01-01");
+ case 17:
+ return new org.apache.iotdb.commons.queryengine.plan.relational.sql.ast.BinaryLiteral(
+ new byte[0]);
+ default:
+ return new NullLiteral();
+ }
+ }
+
+ private void execute(FrontendMessage message, Channel channel, ByteBuf out)
+ throws PgException, ResultCursor.WriteTimeoutException {
+ String name = message.readCString();
+ int maxRows = message.readInt32();
+ Portal portal = portals.get(name);
+ if (portal == null) {
+ throw new PgException(SqlState.INVALID_CURSOR_NAME, "portal \"" + name + "\" does not exist");
+ }
+ if (session.transactionStatus() == 'E'
+ && !(portal.statement.command && isTransactionEnd(portal.statement.sql))) {
+ throw new PgException(
+ "25P02",
+ "current transaction is aborted, commands ignored until end of transaction block");
+ }
+ run(portal, channel, out, Math.max(0, maxRows), false);
+ }
+
+ private void close(FrontendMessage message, ByteBuf out) throws PgException {
+ char kind = (char) message.readByte();
+ String name = message.readCString();
+ if (kind == 'S') {
+ statements.remove(name);
+ } else if (kind == 'P') {
+ closePortal(name);
+ SqlCursor cursor = cursors.remove(name);
+ if (cursor != null) {
+ cursor.rows.close();
+ }
+ } else {
+ throw new PgException(SqlState.PROTOCOL_VIOLATION, "invalid CLOSE message subtype");
+ }
+ BackendMessages.closeComplete(out);
+ }
+
+ private void sync(ByteBuf out) {
+ skipUntilSync = false;
+ if (session.transactionStatus() == 'I') {
+ // end of the implicit transaction: its portals and cursors without HOLD end too
+ closePortals();
+ endTransactionCursors(false);
+ }
+ BackendMessages.readyForQuery(out, session.transactionStatus());
+ }
+
+ // ---------------------------------------------------------------------------- running
+
+ /**
+ * Runs a portal: a session command, or the next rows of its query.
+ *
+ * @param maxRows 0 for all rows
+ * @param describe whether to send RowDescription first (simple query)
+ */
+ private void run(Portal portal, Channel channel, ByteBuf out, long maxRows, boolean describe)
+ throws PgException, ResultCursor.WriteTimeoutException {
+ Prepared statement = portal.statement;
+ if (statement.empty) {
+ BackendMessages.emptyQueryResponse(out);
+ return;
+ }
+ if (statement.cursor != null) {
+ runCursorCommand(portal, channel, out, describe);
+ return;
+ }
+ if (statement.command) {
+ Optional result = SessionCommands.execute(session, statement.sql);
+ if (!result.isPresent()) {
+ throw new PgException(SqlState.INTERNAL_ERROR, "not a session command");
+ }
+ SessionCommands.Result r = result.get();
+ r.parameterStatus().forEach((n, v) -> BackendMessages.parameterStatus(out, n, v));
+ if (r.showName() != null) {
+ if (describe) {
+ BackendMessages.rowDescription(
+ out,
+ Collections.singletonList(
+ new BackendMessages.Field(
+ r.showName(), PgType.TEXT.oid(), PgType.TEXT.size(), (short) 0)));
+ }
+ int row = BackendMessages.beginDataRow(out, 1);
+ BackendMessages.value(out, r.showValue().getBytes(StandardCharsets.UTF_8));
+ BackendMessages.end(out, row);
+ }
+ BackendMessages.commandComplete(out, r.tag());
+ if (r.tag().equals("COMMIT") || r.tag().equals("ROLLBACK")) {
+ closePortalsExcept(portal);
+ endTransactionCursors(r.tag().equals("ROLLBACK"));
+ } else if (r.tag().equals("DISCARD ALL")) {
+ closeCursors();
+ }
+ return;
+ }
+ if (portal.finished) {
+ BackendMessages.commandComplete(out, "SELECT 0");
+ return;
+ }
+ start(portal);
+ if (describe) {
+ BackendMessages.rowDescription(out, portal.cursor.fields());
+ }
+ running.set(portal.cursor);
+ long sent;
+ try {
+ sent = portal.cursor.send(channel, out, maxRows, writeTimeout);
+ } catch (PgException e) {
+ closeCursor(portal);
+ if (cancelRequested) {
+ throw new PgException(SqlState.QUERY_CANCELED, "canceling statement due to user request");
+ }
+ throw e;
+ } finally {
+ running.set(null);
+ }
+ if (portal.cursor.exhausted()) {
+ closeCursor(portal);
+ portal.finished = true;
+ BackendMessages.commandComplete(out, "SELECT " + sent);
+ } else {
+ BackendMessages.portalSuspended(out);
+ }
+ }
+
+ private void start(Portal portal) throws PgException {
+ if (portal.cursor != null) {
+ return;
+ }
+ cancelRequested = false;
+ RunningQuery query = startQuery(portal.statement.translation, portal.parameters);
+ try {
+ portal.cursor = new ResultCursor(query, portal.resultFormats, encoder());
+ } catch (PgException e) {
+ query.close();
+ throw e;
+ }
+ }
+
+ private RunningQuery startQuery(Translation translation, List parameters)
+ throws PgException {
+ return RunningQuery.start(
+ session.session(),
+ translation.sqlFor(parameters),
+ translation.remaining(parameters),
+ StatementTimeout.parse(session.parameter("statement_timeout")));
+ }
+
+ private PgValueEncoder encoder() {
+ return new PgValueEncoder(session.zone(), precision);
+ }
+
+ private static void closeCursor(Portal portal) {
+ if (portal.cursor != null) {
+ portal.cursor.close();
+ portal.cursor = null;
+ }
+ }
+
+ private void closePortal(String name) {
+ Portal portal = portals.remove(name);
+ if (portal != null) {
+ closeCursor(portal);
+ }
+ }
+
+ private void closePortals() {
+ for (Portal portal : portals.values()) {
+ closeCursor(portal);
+ }
+ portals.clear();
+ }
+
+ private void closePortalsExcept(Portal keep) {
+ portals
+ .entrySet()
+ .removeIf(
+ e -> {
+ if (e.getValue() == keep) {
+ return false;
+ }
+ closeCursor(e.getValue());
+ return true;
+ });
+ }
+
+ // ---------------------------------------------------------------------------- SQL cursors
+
+ private void runCursorCommand(Portal portal, Channel channel, ByteBuf out, boolean describe)
+ throws PgException, ResultCursor.WriteTimeoutException {
+ CursorCommand command = portal.statement.cursor;
+ switch (command.kind()) {
+ case DECLARE:
+ declare(portal, command, out);
+ return;
+ case FETCH:
+ {
+ SqlCursor cursor = cursor(command.name());
+ cursor.rows.setFormats(fetchFormats(cursor, describe ? null : portal));
+ if (describe) {
+ BackendMessages.rowDescription(out, cursor.rows.fields());
+ }
+ long sent = moveOrFetch(command.name(), cursor, channel, out, command.count());
+ BackendMessages.commandComplete(out, "FETCH " + sent);
+ return;
+ }
+ case MOVE:
+ {
+ SqlCursor cursor = cursor(command.name());
+ long skipped = moveOrFetch(command.name(), cursor, null, null, command.count());
+ BackendMessages.commandComplete(out, "MOVE " + skipped);
+ return;
+ }
+ case CLOSE:
+ default:
+ if (command.name() == null) {
+ closeCursors();
+ } else {
+ cursor(command.name());
+ cursors.remove(command.name()).rows.close();
+ }
+ BackendMessages.commandComplete(out, "CLOSE CURSOR");
+ }
+ }
+
+ private void declare(Portal portal, CursorCommand command, ByteBuf out) throws PgException {
+ if (!command.hold() && session.transactionStatus() == 'I') {
+ throw new PgException("25P01", "DECLARE CURSOR can only be used in transaction blocks");
+ }
+ if (cursors.containsKey(command.name()) || portals.containsKey(command.name())) {
+ throw new PgException(
+ SqlState.DUPLICATE_CURSOR, "cursor \"" + command.name() + "\" already exists");
+ }
+ cancelRequested = false;
+ RunningQuery query = startQuery(portal.statement.translation, portal.parameters);
+ ResultCursor rows;
+ try {
+ rows = new ResultCursor(query, new short[0], encoder());
+ } catch (PgException e) {
+ query.close();
+ throw e;
+ }
+ cursors.put(
+ command.name(),
+ new SqlCursor(rows, command.hold(), command.binary(), session.transactionStatus() == 'T'));
+ BackendMessages.commandComplete(out, "DECLARE CURSOR");
+ }
+
+ /** Sends ({@code out != null}) or skips up to {@code count} rows (0 = all). */
+ private long moveOrFetch(String name, SqlCursor cursor, Channel channel, ByteBuf out, long count)
+ throws PgException, ResultCursor.WriteTimeoutException {
+ running.set(cursor.rows);
+ try {
+ return out == null
+ ? cursor.rows.skip(count)
+ : cursor.rows.send(channel, out, count, writeTimeout);
+ } catch (PgException e) {
+ cursors.remove(name);
+ cursor.rows.close();
+ if (cancelRequested) {
+ throw new PgException(SqlState.QUERY_CANCELED, "canceling statement due to user request");
+ }
+ throw e;
+ } finally {
+ running.set(null);
+ }
+ }
+
+ private SqlCursor cursor(String name) throws PgException {
+ SqlCursor cursor = cursors.get(name);
+ if (cursor == null) {
+ throw new PgException(SqlState.INVALID_CURSOR_NAME, "cursor \"" + name + "\" does not exist");
+ }
+ return cursor;
+ }
+
+ /**
+ * The result formats of a FETCH: those of its Bind (extended protocol), else binary for a BINARY
+ * cursor and text otherwise.
+ */
+ private static short[] fetchFormats(SqlCursor cursor, Portal portal) {
+ if (portal != null && portal.resultFormats.length > 0) {
+ return portal.resultFormats;
+ }
+ return cursor.binary ? new short[] {1} : new short[0];
+ }
+
+ /** At COMMIT, ROLLBACK or the end of an implicit transaction. */
+ private void endTransactionCursors(boolean rollback) {
+ cursors
+ .entrySet()
+ .removeIf(
+ e -> {
+ SqlCursor cursor = e.getValue();
+ // ROLLBACK also ends the HOLD cursors declared in the rolled-back transaction
+ if (!cursor.hold || (rollback && cursor.inTransaction)) {
+ cursor.rows.close();
+ return true;
+ }
+ return false;
+ });
+ }
+
+ private void closeCursors() {
+ for (SqlCursor cursor : cursors.values()) {
+ cursor.rows.close();
+ }
+ cursors.clear();
+ }
+
+ // ---------------------------------------------------------------------------- translation
+
+ private TranslationContext translationContext() {
+ return new TranslationContext() {
+ @Override
+ public List searchPath() {
+ return SearchPath.parse(session.parameter("search_path"));
+ }
+
+ @Override
+ public boolean tableExists(String database, String table) {
+ try {
+ return DataNodeTableCache.getInstance().getTable(database, table) != null;
+ } catch (RuntimeException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public String user() {
+ return session.user();
+ }
+
+ @Override
+ public String version() {
+ return "PostgreSQL " + session.parameter("server_version") + ", read-only adapter";
+ }
+
+ @Override
+ public String currentDatabase() {
+ String database = session.parameter("database");
+ return database == null || database.isEmpty() ? session.user() : database;
+ }
+
+ @Override
+ public int backendPid() {
+ return session.processId();
+ }
+
+ @Override
+ public java.time.ZoneId zone() {
+ return session.zone();
+ }
+
+ @Override
+ public TimestampPrecision precision() {
+ return precision;
+ }
+ };
+ }
+
+ // ---------------------------------------------------------------------------- QueryProtocol
+
+ @Override
+ public boolean busy() {
+ return busy;
+ }
+
+ @Override
+ public void cancel() {
+ ResultCursor cursor = running.get();
+ if (cursor != null) {
+ cancelRequested = true;
+ cursor.cancel();
+ }
+ }
+
+ @Override
+ public void close() {
+ closePortals();
+ closeCursors();
+ statements.clear();
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/StatementSplitter.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/StatementSplitter.java
new file mode 100644
index 0000000000000..779d20fff84c4
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/StatementSplitter.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Splits a simple-query string into statements at semicolons outside quotes, dollar quotes and
+ * comments, like PostgreSQL. Empty statements are kept as empty strings (EmptyQueryResponse).
+ */
+public final class StatementSplitter {
+
+ private StatementSplitter() {}
+
+ public static List split(String sql) {
+ List statements = new ArrayList<>();
+ StringBuilder current = new StringBuilder();
+ int i = 0;
+ int n = sql.length();
+ while (i < n) {
+ char c = sql.charAt(i);
+ if (c == '\'' || c == '"') {
+ int end = closing(sql, i + 1, c);
+ current.append(sql, i, end);
+ i = end;
+ } else if (c == '-' && i + 1 < n && sql.charAt(i + 1) == '-') {
+ int end = sql.indexOf('\n', i);
+ end = end < 0 ? n : end;
+ current.append(sql, i, end);
+ i = end;
+ } else if (c == '/' && i + 1 < n && sql.charAt(i + 1) == '*') {
+ int end = sql.indexOf("*/", i + 2);
+ end = end < 0 ? n : end + 2;
+ current.append(sql, i, end);
+ i = end;
+ } else if (c == '$' && dollarTag(sql, i) != null) {
+ String tag = dollarTag(sql, i);
+ int end = sql.indexOf(tag, i + tag.length());
+ end = end < 0 ? n : end + tag.length();
+ current.append(sql, i, end);
+ i = end;
+ } else if (c == ';') {
+ statements.add(current.toString().trim());
+ current.setLength(0);
+ i++;
+ } else {
+ current.append(c);
+ i++;
+ }
+ }
+ String last = current.toString().trim();
+ if (!last.isEmpty() || statements.isEmpty()) {
+ statements.add(last);
+ }
+ return statements;
+ }
+
+ /** The end (exclusive) of a quoted part starting after the opening quote; '' and "" escape. */
+ private static int closing(String sql, int from, char quote) {
+ int i = from;
+ while (i < sql.length()) {
+ if (sql.charAt(i) == quote) {
+ if (i + 1 < sql.length() && sql.charAt(i + 1) == quote) {
+ i += 2;
+ continue;
+ }
+ return i + 1;
+ }
+ i++;
+ }
+ return sql.length();
+ }
+
+ /** {@code $tag$} starting at {@code i}, or null. */
+ private static String dollarTag(String sql, int i) {
+ int end = i + 1;
+ while (end < sql.length()
+ && (Character.isLetterOrDigit(sql.charAt(end)) || sql.charAt(end) == '_')) {
+ end++;
+ }
+ if (end < sql.length() && sql.charAt(end) == '$') {
+ return sql.substring(i, end + 1);
+ }
+ return null;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/StatementTimeout.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/StatementTimeout.java
new file mode 100644
index 0000000000000..f6d79ca7feccb
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/session/StatementTimeout.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.session;
+
+import java.time.Duration;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * PostgreSQL's {@code statement_timeout}: a number of milliseconds or a number with a unit ({@code
+ * us}, {@code ms}, {@code s}, {@code min}, {@code h}, {@code d}); 0 switches it off.
+ */
+public final class StatementTimeout {
+
+ private static final Pattern VALUE =
+ Pattern.compile("(\\d+(?:\\.\\d+)?)\\s*(us|ms|s|min|h|d)?", Pattern.CASE_INSENSITIVE);
+
+ private StatementTimeout() {}
+
+ /**
+ * Parses a value.
+ *
+ * @return {@link Duration#ZERO} for no timeout (0 or no value)
+ * @throws IllegalArgumentException if the value is not a valid time
+ */
+ public static Duration parse(String value) {
+ if (value == null || value.trim().isEmpty()) {
+ return Duration.ZERO;
+ }
+ Matcher m = VALUE.matcher(value.trim());
+ if (!m.matches()) {
+ throw new IllegalArgumentException(
+ "invalid value for parameter \"statement_timeout\": \"" + value + "\"");
+ }
+ double amount = Double.parseDouble(m.group(1));
+ String unit = m.group(2) == null ? "ms" : m.group(2).toLowerCase(Locale.ROOT);
+ double millis;
+ switch (unit) {
+ case "us":
+ millis = amount / 1000;
+ break;
+ case "s":
+ millis = amount * 1000;
+ break;
+ case "min":
+ millis = amount * 60_000;
+ break;
+ case "h":
+ millis = amount * 3_600_000;
+ break;
+ case "d":
+ millis = amount * 86_400_000;
+ break;
+ default:
+ millis = amount;
+ }
+ // PostgreSQL rounds to whole milliseconds; a positive value below 1 ms is 1 ms
+ long rounded = Math.round(millis);
+ return Duration.ofMillis(amount > 0 && rounded == 0 ? 1 : rounded);
+ }
+
+ /** The timeout the engine gets: the smaller of its own and the session's (if set). */
+ public static long engineTimeoutMillis(long engineTimeout, Duration statementTimeout) {
+ if (statementTimeout.isZero()) {
+ return engineTimeout;
+ }
+ long session = statementTimeout.toMillis();
+ return engineTimeout <= 0 ? session : Math.min(engineTimeout, session);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/ConstructCheck.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/ConstructCheck.java
new file mode 100644
index 0000000000000..ae3492d4e326c
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/ConstructCheck.java
@@ -0,0 +1,440 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.sql;
+
+import net.sf.jsqlparser.expression.Function;
+import net.sf.jsqlparser.statement.select.Distinct;
+import net.sf.jsqlparser.statement.select.Join;
+import net.sf.jsqlparser.statement.select.OrderByElement;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.WithItem;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Checks a parsed statement against the constructs with an exact translation and prepares it: every
+ * node class must be on the allow list, and flags that change the meaning of an allowed node
+ * (DISTINCT ON, NATURAL JOIN, ORDER BY inside an aggregate, ...) are refused. Walks the syntax tree
+ * through its fields, so a construct the list does not know can never pass unnoticed.
+ *
+ * Preparation: ORDER BY items get PostgreSQL's default null ordering (NULLS LAST ascending,
+ * NULLS FIRST descending) written out, and the names of common table expressions are collected.
+ */
+final class ConstructCheck {
+
+ private static final String PACKAGE = "net.sf.jsqlparser.";
+
+ private static final Set ALLOWED =
+ new HashSet<>(
+ Arrays.asList(
+ "statement.select.PlainSelect",
+ "statement.select.SetOperationList",
+ "statement.select.UnionOp",
+ "statement.select.ParenthesedSelect",
+ "statement.select.SelectItem",
+ "statement.select.AllColumns",
+ "statement.select.AllTableColumns",
+ "statement.select.Join",
+ "statement.select.OrderByElement",
+ "statement.select.GroupByElement",
+ "statement.select.Limit",
+ "statement.select.Offset",
+ "statement.select.Fetch",
+ "statement.select.Distinct",
+ "statement.select.WithItem",
+ "statement.select.ParenthesedFromItem",
+ "statement.create.table.ColDataType",
+ "schema.Table",
+ "schema.Column",
+ "schema.Database",
+ "schema.Server",
+ "expression.Alias",
+ "expression.BooleanValue",
+ "expression.LongValue",
+ "expression.DoubleValue",
+ "expression.StringValue",
+ "expression.NullValue",
+ "expression.JdbcParameter",
+ "expression.Function",
+ "expression.CastExpression",
+ "expression.CaseExpression",
+ "expression.WhenClause",
+ "expression.SignedExpression",
+ "expression.NotExpression",
+ "expression.TimeKeyExpression",
+ "expression.operators.conditional.AndExpression",
+ "expression.operators.conditional.OrExpression",
+ "expression.operators.relational.EqualsTo",
+ "expression.operators.relational.NotEqualsTo",
+ "expression.operators.relational.GreaterThan",
+ "expression.operators.relational.GreaterThanEquals",
+ "expression.operators.relational.MinorThan",
+ "expression.operators.relational.MinorThanEquals",
+ "expression.operators.relational.InExpression",
+ "expression.operators.relational.Between",
+ "expression.operators.relational.IsNullExpression",
+ "expression.operators.relational.IsBooleanExpression",
+ "expression.operators.relational.LikeExpression",
+ "expression.operators.relational.RegExpMatchOperator",
+ "expression.ExtractExpression",
+ "expression.IntervalExpression",
+ "expression.IntervalQualifier",
+ "expression.DateTimeLiteralExpression",
+ "expression.DateUnitExpression",
+ "expression.ArrayExpression",
+ "expression.AnalyticExpression",
+ "expression.WindowDefinition",
+ "expression.PartitionByClause",
+ "expression.OrderByClause",
+ "expression.operators.relational.ExistsExpression",
+ "expression.operators.relational.ExpressionList",
+ "expression.operators.relational.ParenthesedExpressionList",
+ "expression.operators.arithmetic.Addition",
+ "expression.operators.arithmetic.Subtraction",
+ "expression.operators.arithmetic.Multiplication",
+ "expression.operators.arithmetic.Division",
+ "expression.operators.arithmetic.Modulo",
+ "expression.operators.arithmetic.Concat"));
+
+ private static final Map NAMES = new HashMap<>();
+
+ static {
+ NAMES.put("ArrayConstructor", "ARRAY");
+ NAMES.put("AnyComparisonExpression", "ANY/ALL/SOME");
+ NAMES.put("IntervalExpression", "INTERVAL");
+ NAMES.put("Values", "VALUES");
+ NAMES.put("ParenthesedValues", "VALUES");
+ NAMES.put("ExtractExpression", "EXTRACT");
+ NAMES.put("IntersectOp", "INTERSECT");
+ NAMES.put("ExceptOp", "EXCEPT");
+ NAMES.put("MinusOp", "MINUS");
+ NAMES.put("SimilarToExpression", "SIMILAR TO");
+ NAMES.put("RegExpMatchOperator", "regular expression operators");
+ NAMES.put("JsonExpression", "JSON operators");
+ NAMES.put("JsonOperator", "JSON operators");
+ NAMES.put("ForClause", "FOR UPDATE/SHARE");
+ NAMES.put("LateralSubSelect", "LATERAL");
+ NAMES.put("TableFunction", "table functions in FROM");
+ NAMES.put("JdbcNamedParameter", "named parameters");
+ NAMES.put("UserVariable", "variables");
+ NAMES.put("HexValue", "hexadecimal literals");
+ NAMES.put("DateValue", "{d ...} literals");
+ NAMES.put("TimestampValue", "{ts ...} literals");
+ NAMES.put("TimeValue", "{t ...} literals");
+ NAMES.put("CollateExpression", "COLLATE");
+ NAMES.put("TrimFunction", "TRIM");
+ NAMES.put("OverlapsCondition", "OVERLAPS");
+ NAMES.put("RowConstructor", "row constructors");
+ NAMES.put("XorExpression", "XOR");
+ NAMES.put("BitwiseAnd", "bitwise operators");
+ NAMES.put("BitwiseOr", "bitwise operators");
+ NAMES.put("BitwiseXor", "bitwise operators");
+ NAMES.put("BitwiseLeftShift", "bitwise operators");
+ NAMES.put("BitwiseRightShift", "bitwise operators");
+ NAMES.put("IntegerDivision", "DIV");
+ NAMES.put("GroupingSets", "GROUPING SETS");
+ }
+
+ private static final Map, Field[]> FIELDS = new ConcurrentHashMap<>();
+
+ private final IdentityHashMap visited = new IdentityHashMap<>();
+ private final Set cteNames = new HashSet<>();
+
+ /** Table qualifier (alias or name) → catalog table it refers to; {@code null} if ambiguous. */
+ private final Map catalogTables = new HashMap<>();
+
+ private boolean otherTables;
+
+ /** What the check found out about the statement. */
+ static final class Result {
+ final Set cteNames;
+ final Map catalogTables;
+ final boolean otherTables;
+
+ Result(Set cteNames, Map catalogTables, boolean otherTables) {
+ this.cteNames = cteNames;
+ this.catalogTables = catalogTables;
+ this.otherTables = otherTables;
+ }
+ }
+
+ private ConstructCheck() {}
+
+ /**
+ * Checks and prepares the statement.
+ *
+ * @return the folded names of its common table expressions
+ * @throws UnsupportedConstructException naming the first construct without exact translation
+ */
+ static Result check(Object statement) {
+ ConstructCheck check = new ConstructCheck();
+ check.walk(statement);
+ // a CTE named like a catalog table hides it
+ check.catalogTables.keySet().removeIf(check.cteNames::contains);
+ return new Result(check.cteNames, check.catalogTables, check.otherTables);
+ }
+
+ /** Records which catalog table a table reference names, under its alias or name. */
+ private void recordTable(net.sf.jsqlparser.schema.Table table) {
+ String schema = Identifiers.fold(table.getSchemaName());
+ String name = Identifiers.fold(table.getName());
+ if (name == null) {
+ return;
+ }
+ String catalog = null;
+ if (schema == null && name.startsWith("pg_")) {
+ catalog = name;
+ } else if ("pg_catalog".equals(schema)) {
+ catalog = name;
+ } else if ("information_schema".equals(schema)) {
+ catalog = "pg_is_" + name;
+ }
+ String key = table.getAlias() != null ? Identifiers.fold(table.getAlias().getName()) : name;
+ if (catalog == null || !org.apache.iotdb.postgresql.catalog.PgCatalogTables.contains(catalog)) {
+ // a user table (or a CTE) under the same qualifier makes the qualifier ambiguous
+ otherTables = true;
+ catalogTables.put(key, null);
+ return;
+ }
+ if (catalogTables.containsKey(key) && !catalog.equals(catalogTables.get(key))) {
+ catalogTables.put(key, null);
+ } else {
+ catalogTables.put(key, catalog);
+ }
+ }
+
+ private void walk(Object node) {
+ if (node == null || visited.containsKey(node)) {
+ return;
+ }
+ if (node instanceof Collection) {
+ for (Object element : (Collection>) node) {
+ walk(element);
+ }
+ return;
+ }
+ Class> type = node.getClass();
+ if (type.isArray()) {
+ if (!type.getComponentType().isPrimitive()) {
+ for (int i = 0; i < Array.getLength(node); i++) {
+ walk(Array.get(node, i));
+ }
+ }
+ return;
+ }
+ String name = type.getName();
+ if (!name.startsWith(PACKAGE) || type.isEnum() || name.startsWith(PACKAGE + "parser.")) {
+ return;
+ }
+ visited.put(node, Boolean.TRUE);
+ String relative = name.substring(PACKAGE.length());
+ if (!ALLOWED.contains(relative)) {
+ throw new UnsupportedConstructException(describe(type));
+ }
+ checkFlags(node);
+ for (Field field : fields(type)) {
+ try {
+ walk(field.get(node));
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException("cannot inspect " + field, e);
+ }
+ }
+ }
+
+ private void checkFlags(Object node) {
+ if (node instanceof net.sf.jsqlparser.schema.Column) {
+ // the qualifier of a column is a Table node too, but not a table reference
+ net.sf.jsqlparser.schema.Table qualifier =
+ ((net.sf.jsqlparser.schema.Column) node).getTable();
+ if (qualifier != null) {
+ visited.put(qualifier, Boolean.TRUE);
+ }
+ } else if (node instanceof net.sf.jsqlparser.schema.Table) {
+ recordTable((net.sf.jsqlparser.schema.Table) node);
+ } else if (node instanceof net.sf.jsqlparser.expression.AnalyticExpression) {
+ checkWindow((net.sf.jsqlparser.expression.AnalyticExpression) node);
+ } else if (node instanceof PlainSelect) {
+ PlainSelect select = (PlainSelect) node;
+ if (select.getIntoTables() != null || select.getIntoTempTable() != null) {
+ throw new UnsupportedConstructException("SELECT INTO");
+ }
+ if (select.getWindowDefinitions() != null && !select.getWindowDefinitions().isEmpty()) {
+ throw new UnsupportedConstructException("WINDOW clauses");
+ }
+ if (select.getForMode() != null) {
+ throw new UnsupportedConstructException("FOR UPDATE/SHARE");
+ }
+ } else if (node instanceof Distinct) {
+ Distinct distinct = (Distinct) node;
+ if (distinct.getOnSelectItems() != null && !distinct.getOnSelectItems().isEmpty()) {
+ throw new UnsupportedConstructException("DISTINCT ON");
+ }
+ if (distinct.isUseUnique() || distinct.isUseDistinctRow()) {
+ throw new UnsupportedConstructException("SELECT UNIQUE");
+ }
+ } else if (node instanceof Join) {
+ Join join = (Join) node;
+ if (join.isNatural()) {
+ throw new UnsupportedConstructException("NATURAL JOIN");
+ }
+ if (join.isApply() || join.isSemi() || join.isStraight() || join.isWindowJoin()) {
+ throw new UnsupportedConstructException("this kind of JOIN");
+ }
+ } else if (node instanceof Function) {
+ Function function = (Function) node;
+ if (function.getOrderByElements() != null && !function.getOrderByElements().isEmpty()) {
+ throw new UnsupportedConstructException("ORDER BY inside " + function.getName() + "()");
+ }
+ // Function.getAttribute()/getAttributeName() fail on functions without one (JSqlParser 5.4)
+ if (field(function, "attributeExpression") != null
+ || field(function, "attributeColumn") != null) {
+ throw new UnsupportedConstructException("field access on " + function.getName() + "()");
+ }
+ if (function.isUnique()
+ || function.isIgnoreNulls()
+ || function.getNullHandling() != null
+ || function.getExtraKeyword() != null
+ || (function.getKeywordArguments() != null && !function.getKeywordArguments().isEmpty())
+ || function.getKeep() != null
+ || function.getLimit() != null
+ || function.getHavingClause() != null
+ || function.getNamedParameters() != null
+ || function.getChainedParameters() != null) {
+ throw new UnsupportedConstructException("this form of " + function.getName() + "()");
+ }
+ } else if (node instanceof OrderByElement) {
+ OrderByElement order = (OrderByElement) node;
+ if (order.getNullOrdering() == null) {
+ // PostgreSQL's defaults, written out so the engine's defaults do not matter
+ order.setNullOrdering(
+ order.isAsc()
+ ? OrderByElement.NullOrdering.NULLS_LAST
+ : OrderByElement.NullOrdering.NULLS_FIRST);
+ }
+ } else if (node instanceof WithItem) {
+ WithItem> with = (WithItem>) node;
+ if (with.isRecursive()) {
+ throw new UnsupportedConstructException("WITH RECURSIVE");
+ }
+ if (with.getWithItemList() != null && !with.getWithItemList().isEmpty()) {
+ throw new UnsupportedConstructException("column lists of WITH queries");
+ }
+ if (with.getAlias() != null) {
+ cteNames.add(Identifiers.fold(with.getAlias().getName()));
+ }
+ }
+ }
+
+ /** Window functions the engine has with PostgreSQL's meaning, and aggregates over windows. */
+ private static final Set WINDOW_FUNCTIONS =
+ new HashSet<>(
+ Arrays.asList(
+ "row_number",
+ "rank",
+ "dense_rank",
+ "percent_rank",
+ "cume_dist",
+ "ntile",
+ "first_value",
+ "last_value",
+ "nth_value",
+ "lead",
+ "lag",
+ "count",
+ "sum",
+ "avg",
+ "min",
+ "max"));
+
+ private static void checkWindow(net.sf.jsqlparser.expression.AnalyticExpression window) {
+ String name = window.getName().toLowerCase(java.util.Locale.ROOT);
+ if (!WINDOW_FUNCTIONS.contains(name)) {
+ throw new UnsupportedConstructException("the window function " + name + "()");
+ }
+ if (window.getType() != net.sf.jsqlparser.expression.AnalyticType.OVER
+ || window.getWindowElement() != null
+ || window.getWindowName() != null
+ || window.isDistinct()
+ || window.isUnique()
+ || window.isIgnoreNulls()
+ || window.isIgnoreNullsOutside()
+ || window.getFilterExpression() != null
+ || window.getKeep() != null
+ || window.getFuncOrderBy() != null) {
+ // explicit frames, FILTER, named windows, DISTINCT and IGNORE NULLS stay out
+ throw new UnsupportedConstructException("this form of the window function " + name + "()");
+ }
+ }
+
+ private static Object field(Object node, String name) {
+ for (Class> c = node.getClass(); c != null && c != Object.class; c = c.getSuperclass()) {
+ try {
+ Field field = c.getDeclaredField(name);
+ field.setAccessible(true);
+ return field.get(node);
+ } catch (NoSuchFieldException e) {
+ // look in the superclass
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException("cannot inspect " + name, e);
+ }
+ }
+ return null;
+ }
+
+ private static Field[] fields(Class> type) {
+ return FIELDS.computeIfAbsent(
+ type,
+ t -> {
+ java.util.List result = new java.util.ArrayList<>();
+ for (Class> c = t; c != null && c != Object.class; c = c.getSuperclass()) {
+ for (Field field : c.getDeclaredFields()) {
+ if (Modifier.isStatic(field.getModifiers())
+ || field.getType().isPrimitive()
+ || field.getType() == String.class) {
+ continue;
+ }
+ field.setAccessible(true);
+ result.add(field);
+ }
+ }
+ return result.toArray(new Field[0]);
+ });
+ }
+
+ private static String describe(Class> type) {
+ String simple = type.getSimpleName();
+ String known = NAMES.get(simple);
+ if (known != null) {
+ return known;
+ }
+ // CamelCase class name to words: "SimilarToExpression" -> "similar to expression"
+ return simple.replaceAll("([a-z])([A-Z])", "$1 $2").toLowerCase(java.util.Locale.ROOT);
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/DateTimeRules.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/DateTimeRules.java
new file mode 100644
index 0000000000000..8fbf2425b2e0f
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/DateTimeRules.java
@@ -0,0 +1,398 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.sql;
+
+import org.apache.iotdb.postgresql.types.TimestampPrecision;
+
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.ZoneId;
+import java.time.zone.ZoneOffsetTransition;
+import java.time.zone.ZoneRules;
+import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Date and time rules: {@code date_trunc}, {@code date_part}/{@code EXTRACT} and interval
+ * arithmetic, each only where the table-model form gives PostgreSQL's result in the session time
+ * zone. The engine's {@code date_bin} is calendar-aware for months (in the session time zone) and
+ * fixed-length for everything else; its casts between TIMESTAMP and DATE use the session time zone.
+ *
+ * Templates contain {@code %s} where the argument goes, already cast to TIMESTAMP.
+ */
+final class DateTimeRules {
+
+ /** Transitions after this instant are checked (data before 1970 is not expected). */
+ private static final Instant SINCE = Instant.parse("1970-01-01T00:00:00Z");
+
+ private static final Instant UNTIL = Instant.parse("2100-01-01T00:00:00Z");
+
+ private static final long MICROS_PER_SECOND = 1_000_000L;
+
+ private DateTimeRules() {}
+
+ /** The template for {@code date_trunc(unit, %s)}. */
+ static String truncate(String rawUnit, ZoneId zone, TimestampPrecision precision) {
+ String unit = rawUnit.toLowerCase(Locale.ROOT);
+ switch (unit) {
+ case "microseconds":
+ return fixedBin(1, zone, precision, 1, unit);
+ case "milliseconds":
+ return fixedBin(1_000, zone, precision, 1, unit);
+ case "second":
+ return fixedBin(MICROS_PER_SECOND, zone, precision, 1, unit);
+ case "minute":
+ return fixedBin(60 * MICROS_PER_SECOND, zone, precision, 60, unit);
+ case "hour":
+ return fixedBin(3600 * MICROS_PER_SECOND, zone, precision, 3600, unit);
+ case "day":
+ // the engine's casts between TIMESTAMP and DATE use the session time zone: local midnight
+ return "CAST(CAST(%s AS DATE) AS TIMESTAMP)";
+ case "week":
+ if (!withoutTransitions(zone)) {
+ throw new UnsupportedConstructException(
+ "date_trunc('week') in a time zone with daylight saving time (" + zone + ")");
+ }
+ // weeks start on Monday; 1970-01-05 was one
+ return "date_bin(1w, %s, " + localMidnight(LocalDate.of(1970, 1, 5), zone, precision) + ")";
+ case "month":
+ return monthBin(1, zone, precision);
+ case "quarter":
+ return monthBin(3, zone, precision);
+ case "year":
+ return monthBin(12, zone, precision);
+ case "decade":
+ return monthBin(120, zone, precision);
+ default:
+ throw new UnsupportedConstructException("date_trunc('" + rawUnit + "')");
+ }
+ }
+
+ private static String fixedBin(
+ long micros, ZoneId zone, TimestampPrecision precision, int offsetMultiple, String unit) {
+ if (offsetMultiple > 1 && !offsetsAreMultiplesOf(zone, offsetMultiple)) {
+ throw new UnsupportedConstructException(
+ "date_trunc('" + unit + "') in the time zone " + zone + " (its UTC offset is not whole)");
+ }
+ return "date_bin(" + duration(micros, precision, "date_trunc('" + unit + "')") + ", %s, 0)";
+ }
+
+ private static String monthBin(int months, ZoneId zone, TimestampPrecision precision) {
+ // month bins are calendar-aware; the origin is local 1970-01-01 00:00
+ return "date_bin("
+ + months
+ + "mo, %s, "
+ + localMidnight(LocalDate.of(1970, 1, 1), zone, precision)
+ + ")";
+ }
+
+ /** The table-model duration literal of a whole number of microseconds. */
+ private static String duration(long micros, TimestampPrecision precision, String what) {
+ long units = precision.fromEpochMicros(micros);
+ if (precision.toEpochMicros(units) != micros) {
+ throw new UnsupportedConstructException(what + " below the timestamp precision");
+ }
+ if (micros % (3600 * MICROS_PER_SECOND) == 0) {
+ return micros / (3600 * MICROS_PER_SECOND) + "h";
+ }
+ if (micros % (60 * MICROS_PER_SECOND) == 0) {
+ return micros / (60 * MICROS_PER_SECOND) + "m";
+ }
+ if (micros % MICROS_PER_SECOND == 0) {
+ return micros / MICROS_PER_SECOND + "s";
+ }
+ if (micros % 1000 == 0) {
+ return micros / 1000 + "ms";
+ }
+ return micros + "us";
+ }
+
+ private static long localMidnight(LocalDate date, ZoneId zone, TimestampPrecision precision) {
+ Instant instant = date.atStartOfDay(zone).toInstant();
+ return precision.fromEpochMicros(
+ instant.getEpochSecond() * MICROS_PER_SECOND + instant.getNano() / 1000);
+ }
+
+ /** True if every UTC offset of {@code zone} since 1970 is a multiple of {@code seconds}. */
+ static boolean offsetsAreMultiplesOf(ZoneId zone, int seconds) {
+ ZoneRules rules = zone.getRules();
+ if (rules.getOffset(SINCE).getTotalSeconds() % seconds != 0) {
+ return false;
+ }
+ ZoneOffsetTransition transition = rules.nextTransition(SINCE);
+ while (transition != null && transition.getInstant().isBefore(UNTIL)) {
+ if (transition.getOffsetAfter().getTotalSeconds() % seconds != 0) {
+ return false;
+ }
+ transition = rules.nextTransition(transition.getInstant());
+ }
+ return true;
+ }
+
+ /** True if {@code zone} has had one UTC offset since 1970 (no daylight saving time). */
+ static boolean withoutTransitions(ZoneId zone) {
+ ZoneRules rules = zone.getRules();
+ if (rules.isFixedOffset()) {
+ return true;
+ }
+ ZoneOffsetTransition transition = rules.nextTransition(SINCE);
+ return transition == null || !transition.getInstant().isBefore(UNTIL);
+ }
+
+ // ---------------------------------------------------------------------------- literals
+
+ private static final Pattern TIMESTAMP =
+ Pattern.compile(
+ "(\\d{4}-\\d{1,2}-\\d{1,2})(?:[ T](\\d{1,2}):(\\d{2})(?::(\\d{2})(?:\\.(\\d{1,9}))?)?)?"
+ + "\\s*(Z|[+-]\\d{1,2}(?::?\\d{2})?)?");
+
+ /**
+ * A timestamp literal in timestamp units, as PostgreSQL reads it: {@code timestamp} literals
+ * ignore an offset and are local times in the session time zone; {@code timestamptz} literals use
+ * their offset, or the session time zone without one.
+ */
+ static long timestampLiteral(
+ String text, boolean withTimeZone, ZoneId zone, TimestampPrecision precision) {
+ Matcher m = TIMESTAMP.matcher(text.trim());
+ if (!m.matches()) {
+ throw new UnsupportedConstructException("the timestamp literal '" + text + "'");
+ }
+ java.time.LocalDateTime local;
+ try {
+ local =
+ java.time.LocalDate.parse(normalizeDate(m.group(1)))
+ .atTime(
+ m.group(2) == null ? 0 : Integer.parseInt(m.group(2)),
+ m.group(3) == null ? 0 : Integer.parseInt(m.group(3)),
+ m.group(4) == null ? 0 : Integer.parseInt(m.group(4)),
+ m.group(5) == null
+ ? 0
+ : Integer.parseInt((m.group(5) + "000000000").substring(0, 9)));
+ } catch (java.time.DateTimeException e) {
+ throw new UnsupportedConstructException("the timestamp literal '" + text + "'");
+ }
+ Instant instant;
+ if (withTimeZone && m.group(6) != null) {
+ instant = local.toInstant(offset(m.group(6)));
+ } else {
+ instant = local.atZone(zone).toInstant();
+ }
+ long micros = instant.getEpochSecond() * MICROS_PER_SECOND + instant.getNano() / 1000;
+ long units = precision.fromEpochMicros(micros);
+ if (precision.toEpochMicros(units) != micros) {
+ throw new UnsupportedConstructException(
+ "the timestamp literal '" + text + "' (below the timestamp precision)");
+ }
+ return units;
+ }
+
+ /** A DATE literal as ISO text ({@code 2026-3-1} → {@code 2026-03-01}). */
+ static String dateLiteral(String text) {
+ try {
+ return java.time.LocalDate.parse(normalizeDate(text.trim())).toString();
+ } catch (java.time.DateTimeException e) {
+ throw new UnsupportedConstructException("the date literal '" + text + "'");
+ }
+ }
+
+ private static String normalizeDate(String date) {
+ String[] parts = date.split("-");
+ if (parts.length != 3) {
+ return date;
+ }
+ return parts[0]
+ + "-"
+ + (parts[1].length() == 1 ? "0" : "")
+ + parts[1]
+ + "-"
+ + (parts[2].length() == 1 ? "0" : "")
+ + parts[2];
+ }
+
+ private static java.time.ZoneOffset offset(String text) {
+ if (text.equals("Z")) {
+ return java.time.ZoneOffset.UTC;
+ }
+ String digits = text.substring(1).replace(":", "");
+ int hours =
+ Integer.parseInt(digits.length() <= 2 ? digits : digits.substring(0, digits.length() - 2));
+ int minutes =
+ digits.length() <= 2 ? 0 : Integer.parseInt(digits.substring(digits.length() - 2));
+ int sign = text.startsWith("-") ? -1 : 1;
+ return java.time.ZoneOffset.ofHoursMinutes(sign * hours, sign * minutes);
+ }
+
+ // ---------------------------------------------------------------------------- EXTRACT
+
+ /** The expression for {@code EXTRACT(field FROM %s)} / {@code date_part('field', %s)}. */
+ static String extract(String rawField, ZoneId zone, TimestampPrecision precision) {
+ String field = rawField.toLowerCase(Locale.ROOT);
+ switch (field) {
+ case "year":
+ case "quarter":
+ case "month":
+ case "day":
+ case "doy":
+ case "hour":
+ case "minute":
+ return "EXTRACT(" + field.toUpperCase(Locale.ROOT) + " FROM %s)";
+ case "isodow":
+ // the engine counts Monday = 1 ... Sunday = 7, as isodow
+ return "EXTRACT(DOW FROM %s)";
+ case "dow":
+ // PostgreSQL: Sunday = 0 ... Saturday = 6
+ return "(EXTRACT(DOW FROM %s) % 7)";
+ case "second":
+ {
+ if (!offsetsAreMultiplesOf(zone, 60)) {
+ throw new UnsupportedConstructException(
+ "the date part 'second' in the time zone "
+ + zone
+ + " (its UTC offset is not whole)");
+ }
+ // with the fraction, as PostgreSQL (microsecond resolution)
+ long units = precision.fromEpochMicros(MICROS_PER_SECOND);
+ long minute = 60 * units;
+ return "(CAST(((CAST(%s AS INT64) % "
+ + minute
+ + ") + "
+ + minute
+ + ") % "
+ + minute
+ + " AS DOUBLE) / "
+ + units
+ + ")";
+ }
+ case "epoch":
+ return "(CAST(CAST(%s AS INT64) AS DOUBLE) / "
+ + precision.fromEpochMicros(MICROS_PER_SECOND)
+ + ")";
+ default:
+ throw new UnsupportedConstructException("the date part '" + rawField + "'");
+ }
+ }
+
+ // ---------------------------------------------------------------------------- intervals
+
+ private static final Pattern PART =
+ Pattern.compile("([+-]?+\\d++(?:\\.\\d++)?+)\\s*+([a-z]++)", Pattern.CASE_INSENSITIVE);
+ private static final Pattern CLOCK =
+ Pattern.compile("([+-]?+)(\\d++):(\\d{2})(?::(\\d{2}(?:\\.\\d++)?+))?+");
+
+ /**
+ * The length of an interval literal in timestamp units, for arithmetic with timestamps. Only
+ * units of fixed length: days and weeks only in time zones without daylight saving time (in
+ * PostgreSQL, adding a day keeps the local time across a DST change); months and years never.
+ */
+ static long intervalUnits(String text, ZoneId zone, TimestampPrecision precision) {
+ String rest = text.trim().toLowerCase(Locale.ROOT);
+ double micros = 0;
+ boolean any = false;
+ Matcher clock = CLOCK.matcher(rest);
+ if (clock.find() && clock.end() == rest.length()) {
+ double sign = clock.group(1).equals("-") ? -1 : 1;
+ double seconds =
+ Long.parseLong(clock.group(2)) * 3600
+ + Long.parseLong(clock.group(3)) * 60
+ + (clock.group(4) == null ? 0 : Double.parseDouble(clock.group(4)));
+ micros += sign * seconds * MICROS_PER_SECOND;
+ rest = rest.substring(0, clock.start()).trim();
+ any = true;
+ }
+ Matcher m = PART.matcher(rest);
+ int position = 0;
+ while (m.find()) {
+ if (!rest.substring(position, m.start()).trim().isEmpty()) {
+ throw new UnsupportedConstructException("the interval '" + text + "'");
+ }
+ position = m.end();
+ double amount = Double.parseDouble(m.group(1));
+ micros += amount * unitMicros(m.group(2), zone, text);
+ any = true;
+ }
+ if (!any || !rest.substring(position).trim().isEmpty()) {
+ throw new UnsupportedConstructException("the interval '" + text + "'");
+ }
+ long whole = Math.round(micros);
+ if (Math.abs(micros - whole) > 1e-6) {
+ throw new UnsupportedConstructException("the interval '" + text + "' (below a microsecond)");
+ }
+ long units = precision.fromEpochMicros(whole);
+ if (precision.toEpochMicros(units) != whole) {
+ throw new UnsupportedConstructException(
+ "the interval '" + text + "' (below the timestamp precision)");
+ }
+ return units;
+ }
+
+ private static double unitMicros(String unit, ZoneId zone, String text) {
+ switch (unit) {
+ case "us":
+ case "usec":
+ case "usecs":
+ case "microsecond":
+ case "microseconds":
+ return 1;
+ case "ms":
+ case "msec":
+ case "msecs":
+ case "millisecond":
+ case "milliseconds":
+ return 1_000;
+ case "s":
+ case "sec":
+ case "secs":
+ case "second":
+ case "seconds":
+ return MICROS_PER_SECOND;
+ case "m":
+ case "min":
+ case "mins":
+ case "minute":
+ case "minutes":
+ return 60.0 * MICROS_PER_SECOND;
+ case "h":
+ case "hr":
+ case "hrs":
+ case "hour":
+ case "hours":
+ return 3600.0 * MICROS_PER_SECOND;
+ case "d":
+ case "day":
+ case "days":
+ case "w":
+ case "week":
+ case "weeks":
+ if (!withoutTransitions(zone)) {
+ throw new UnsupportedConstructException(
+ "interval days in a time zone with daylight saving time (" + zone + ")");
+ }
+ return (unit.startsWith("w") ? 7 : 1) * 86_400.0 * MICROS_PER_SECOND;
+ default:
+ throw new UnsupportedConstructException(
+ "the interval unit '"
+ + unit
+ + "' in '"
+ + text
+ + "' (months and years have no fixed length)");
+ }
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/EmptyRelations.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/EmptyRelations.java
new file mode 100644
index 0000000000000..d90ecb6fd6195
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/EmptyRelations.java
@@ -0,0 +1,388 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.sql;
+
+import org.apache.iotdb.postgresql.catalog.PgCatalogTables;
+
+import net.sf.jsqlparser.expression.Alias;
+import net.sf.jsqlparser.expression.BooleanValue;
+import net.sf.jsqlparser.expression.CastExpression;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.Function;
+import net.sf.jsqlparser.expression.NullValue;
+import net.sf.jsqlparser.schema.Column;
+import net.sf.jsqlparser.schema.Table;
+import net.sf.jsqlparser.statement.create.table.ColDataType;
+import net.sf.jsqlparser.statement.select.FromItem;
+import net.sf.jsqlparser.statement.select.Join;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.SelectItem;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+
+/**
+ * Queries over catalog tables that never have rows: a SELECT whose FROM reads such a table through
+ * inner, cross or left joins (as the left side), without aggregates outside a GROUP BY, returns no
+ * rows whatever it selects. Its select list becomes typed NULLs with the same output names, its
+ * WHERE {@code FALSE}, so that constructs without a translation there (psql's {@code ANY(array)},
+ * {@code ARRAY(...)}, statistics functions) do not refuse the statement. Runs before {@link
+ * ConstructCheck}.
+ */
+final class EmptyRelations {
+
+ private static final String PACKAGE = "net.sf.jsqlparser.";
+
+ private static final Set AGGREGATES =
+ new HashSet<>(
+ Arrays.asList(
+ "count",
+ "sum",
+ "avg",
+ "min",
+ "max",
+ "bool_and",
+ "bool_or",
+ "every",
+ "string_agg",
+ "array_agg",
+ "stddev",
+ "stddev_pop",
+ "stddev_samp",
+ "variance",
+ "var_pop",
+ "var_samp"));
+
+ private final IdentityHashMap visited = new IdentityHashMap<>();
+ private final List selects = new ArrayList<>();
+
+ private EmptyRelations() {}
+
+ /** Rewrites every qualifying SELECT of {@code statement} in place. */
+ static void simplify(Object statement) {
+ EmptyRelations finder = new EmptyRelations();
+ finder.walk(statement);
+ // innermost first: a SELECT over a derived table that is empty is empty as well
+ java.util.Set emptied =
+ java.util.Collections.newSetFromMap(new IdentityHashMap<>());
+ for (int i = finder.selects.size() - 1; i >= 0; i--) {
+ PlainSelect select = finder.selects.get(i);
+ String table = emptyTable(select);
+ if (table != null) {
+ empty(select, table);
+ emptied.add(select);
+ } else if (emptyDerived(select, emptied)) {
+ emptyOverDerived(select);
+ emptied.add(select);
+ }
+ }
+ }
+
+ /** The always-empty catalog table that makes {@code select} empty, or {@code null}. */
+ private static String emptyTable(PlainSelect select) {
+ if (select.getGroupBy() == null && hasAggregate(select.getSelectItems())) {
+ // an aggregate over no rows still returns one row
+ return null;
+ }
+ List joins = select.getJoins() == null ? new ArrayList<>() : select.getJoins();
+ String first = catalogTable(select.getFromItem());
+ if (first != null && PgCatalogTables.alwaysEmpty(first)) {
+ for (Join join : joins) {
+ if (join.isRight() || join.isFull()) {
+ return null;
+ }
+ }
+ return first;
+ }
+ for (Join join : joins) {
+ String table = catalogTable(join.getFromItem());
+ boolean inner = join.isInner() || join.isCross() || join.isSimple() || isPlain(join);
+ if (table != null && PgCatalogTables.alwaysEmpty(table) && inner) {
+ // an inner or cross join with an empty table is empty, wherever it stands
+ return table;
+ }
+ }
+ return null;
+ }
+
+ private static boolean isPlain(Join join) {
+ return !join.isLeft()
+ && !join.isRight()
+ && !join.isFull()
+ && !join.isOuter()
+ && !join.isNatural()
+ && !join.isSemi()
+ && !join.isApply();
+ }
+
+ static boolean hasAggregate(List> items) {
+ for (SelectItem> item : items) {
+ if (containsAggregate(item.getExpression(), new IdentityHashMap<>())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static boolean containsAggregate(Object node, IdentityHashMap seen) {
+ if (node == null || seen.containsKey(node)) {
+ return false;
+ }
+ if (node instanceof Function
+ && AGGREGATES.contains(((Function) node).getName().toLowerCase(Locale.ROOT))) {
+ return true;
+ }
+ if (node instanceof Collection) {
+ for (Object element : (Collection>) node) {
+ if (containsAggregate(element, seen)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ Class> type = node.getClass();
+ if (!type.getName().startsWith(PACKAGE)
+ || type.isEnum()
+ || node instanceof net.sf.jsqlparser.statement.select.Select) {
+ // a sub-query's aggregates are its own
+ return false;
+ }
+ seen.put(node, Boolean.TRUE);
+ for (Field field : fields(type)) {
+ try {
+ if (containsAggregate(field.get(node), seen)) {
+ return true;
+ }
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException("cannot inspect " + field, e);
+ }
+ }
+ return false;
+ }
+
+ /** True if {@code select} reads an emptied derived table (FROM or inner/cross join). */
+ private static boolean emptyDerived(PlainSelect select, java.util.Set emptied) {
+ if (select.getGroupBy() == null && hasAggregate(select.getSelectItems())) {
+ return false;
+ }
+ if (isEmptied(select.getFromItem(), emptied)) {
+ if (select.getJoins() != null) {
+ for (Join join : select.getJoins()) {
+ if (join.isRight() || join.isFull()) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
+ private static boolean isEmptied(FromItem item, java.util.Set emptied) {
+ return item instanceof net.sf.jsqlparser.statement.select.ParenthesedSelect
+ && ((net.sf.jsqlparser.statement.select.ParenthesedSelect) item).getSelect()
+ instanceof PlainSelect
+ && emptied.contains(
+ (PlainSelect)
+ ((net.sf.jsqlparser.statement.select.ParenthesedSelect) item).getSelect());
+ }
+
+ /** Like {@link #empty}, keeping the (empty) derived table as the FROM item. */
+ private static void emptyOverDerived(PlainSelect select) {
+ List> items = new ArrayList<>();
+ for (SelectItem> item : select.getSelectItems()) {
+ String name =
+ item.getAlias() != null ? Identifiers.fold(item.getAlias().getName()) : outputName(item);
+ items.add(typedNull(org.apache.tsfile.enums.TSDataType.STRING, name));
+ }
+ select.setSelectItems(items);
+ select.setJoins(null);
+ select.setWhere(new BooleanValue(false));
+ select.setGroupByElement(null);
+ select.setHaving(null);
+ select.setDistinct(null);
+ select.setOrderByElements(null);
+ select.setLimit(null);
+ select.setOffset(null);
+ select.setFetch(null);
+ }
+
+ /** The catalog table a FROM item names (PgCatalogTables name), or {@code null}. */
+ private static String catalogTable(FromItem item) {
+ if (!(item instanceof Table)) {
+ return null;
+ }
+ Table table = (Table) item;
+ String schema = Identifiers.fold(table.getSchemaName());
+ String name = Identifiers.fold(table.getName());
+ String catalog = null;
+ if (schema == null && name != null && name.startsWith("pg_")) {
+ catalog = name;
+ } else if ("pg_catalog".equals(schema)) {
+ catalog = name;
+ } else if ("information_schema".equals(schema)) {
+ catalog = "pg_is_" + name;
+ }
+ return catalog != null && PgCatalogTables.contains(catalog) ? catalog : null;
+ }
+
+ private static void empty(PlainSelect select, String table) {
+ List> items = new ArrayList<>();
+ for (SelectItem> item : select.getSelectItems()) {
+ if (item.getExpression() instanceof net.sf.jsqlparser.statement.select.AllColumns) {
+ for (PgCatalogTables.Column column : PgCatalogTables.tables().get(table)) {
+ items.add(typedNull(column.type(), column.name()));
+ }
+ continue;
+ }
+ PgCatalogTables.Column column = column(item.getExpression(), table);
+ String name =
+ item.getAlias() != null ? Identifiers.fold(item.getAlias().getName()) : outputName(item);
+ items.add(
+ typedNull(
+ column == null ? org.apache.tsfile.enums.TSDataType.STRING : column.type(), name));
+ }
+ select.setSelectItems(items);
+ Table from = new Table("pg_catalog", PgCatalogTables.postgresName(table));
+ if (table.startsWith(PgCatalogTables.INFORMATION_SCHEMA_PREFIX)) {
+ from = new Table("information_schema", PgCatalogTables.postgresName(table));
+ }
+ select.setFromItem(from);
+ select.setJoins(null);
+ select.setWhere(new BooleanValue(false));
+ select.setGroupByElement(null);
+ select.setHaving(null);
+ select.setDistinct(null);
+ select.setOrderByElements(null);
+ select.setLimit(null);
+ select.setOffset(null);
+ select.setFetch(null);
+ }
+
+ private static PgCatalogTables.Column column(Expression expression, String table) {
+ if (expression instanceof Column) {
+ return PgCatalogTables.column(table, Identifiers.fold(((Column) expression).getColumnName()));
+ }
+ return null;
+ }
+
+ /** The name PostgreSQL gives an output column without alias. */
+ private static String outputName(SelectItem> item) {
+ Expression expression = item.getExpression();
+ while (expression instanceof CastExpression) {
+ expression = ((CastExpression) expression).getLeftExpression();
+ }
+ if (expression instanceof Column) {
+ return Identifiers.fold(((Column) expression).getColumnName());
+ }
+ if (expression instanceof Function) {
+ List parts = ((Function) expression).getMultipartName();
+ return Identifiers.fold(parts.get(parts.size() - 1));
+ }
+ return "?column?";
+ }
+
+ private static SelectItem> typedNull(org.apache.tsfile.enums.TSDataType type, String name) {
+ CastExpression cast = new CastExpression();
+ cast.setLeftExpression(new NullValue());
+ cast.setColDataType(new ColDataType(pgType(type)));
+ SelectItem item = new SelectItem<>(cast);
+ item.setAlias(new Alias(Identifiers.quote(name), true));
+ return item;
+ }
+
+ private static String pgType(org.apache.tsfile.enums.TSDataType type) {
+ switch (type) {
+ case INT32:
+ return "int4";
+ case INT64:
+ return "int8";
+ case FLOAT:
+ return "float4";
+ case DOUBLE:
+ return "float8";
+ case BOOLEAN:
+ return "bool";
+ default:
+ return "text";
+ }
+ }
+
+ // ---------------------------------------------------------------------------- walking
+
+ private void walk(Object node) {
+ if (node == null || visited.containsKey(node)) {
+ return;
+ }
+ if (node instanceof Collection) {
+ for (Object element : (Collection>) node) {
+ walk(element);
+ }
+ return;
+ }
+ Class> type = node.getClass();
+ if (type.isArray()) {
+ if (!type.getComponentType().isPrimitive()) {
+ for (int i = 0; i < Array.getLength(node); i++) {
+ walk(Array.get(node, i));
+ }
+ }
+ return;
+ }
+ if (!type.getName().startsWith(PACKAGE)
+ || type.isEnum()
+ || type.getName().startsWith(PACKAGE + "parser.")) {
+ return;
+ }
+ visited.put(node, Boolean.TRUE);
+ if (node instanceof PlainSelect) {
+ selects.add((PlainSelect) node);
+ }
+ for (Field field : fields(type)) {
+ try {
+ walk(field.get(node));
+ } catch (IllegalAccessException e) {
+ throw new IllegalStateException("cannot inspect " + field, e);
+ }
+ }
+ }
+
+ private static List fields(Class> type) {
+ List fields = new ArrayList<>();
+ for (Class> c = type; c != null && c != Object.class; c = c.getSuperclass()) {
+ for (Field field : c.getDeclaredFields()) {
+ if (!Modifier.isStatic(field.getModifiers()) && !field.getType().isPrimitive()) {
+ field.setAccessible(true);
+ fields.add(field);
+ }
+ }
+ }
+ return fields;
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/Identifiers.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/Identifiers.java
new file mode 100644
index 0000000000000..6d97c7bab6a26
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/Identifiers.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.sql;
+
+import java.util.Locale;
+
+/** PostgreSQL identifier rules: unquoted names fold to lower case, quoted names keep theirs. */
+final class Identifiers {
+
+ private Identifiers() {}
+
+ /** The name an identifier as written in SQL stands for. */
+ static String fold(String identifier) {
+ if (identifier == null) {
+ return null;
+ }
+ if (identifier.length() >= 2 && identifier.startsWith("\"") && identifier.endsWith("\"")) {
+ return identifier.substring(1, identifier.length() - 1).replace("\"\"", "\"");
+ }
+ return identifier.toLowerCase(Locale.ROOT);
+ }
+
+ /** The name as a double-quoted table-model identifier. */
+ static String quote(String name) {
+ return '"' + name.replace("\"", "\"\"") + '"';
+ }
+
+ /** A string literal of the table model. */
+ static String literal(String value) {
+ return '\'' + value.replace("'", "''") + '\'';
+ }
+}
diff --git a/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/OuterConditions.java b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/OuterConditions.java
new file mode 100644
index 0000000000000..4a8e6f9d29aed
--- /dev/null
+++ b/external-service-impl/postgresql/src/main/java/org/apache/iotdb/postgresql/sql/OuterConditions.java
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.postgresql.sql;
+
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
+import net.sf.jsqlparser.schema.Column;
+import net.sf.jsqlparser.schema.Table;
+import net.sf.jsqlparser.statement.select.FromItem;
+import net.sf.jsqlparser.statement.select.Join;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.Select;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * The conditions of a sub-query's WHERE that only read the outer row: every column they use is
+ * qualified with a name the sub-query does not define. Used to move them in front of the sub-query,
+ * where the engine can evaluate them.
+ */
+final class OuterConditions {
+
+ private OuterConditions() {}
+
+ /**
+ * The outer-only conjuncts of {@code select}'s WHERE; empty if there are none or the sub-query
+ * aggregates or groups (then no rows does not mean NULL).
+ */
+ static List split(PlainSelect select) {
+ if (select.getWhere() == null
+ || select.getGroupBy() != null
+ || select.getHaving() != null
+ || EmptyRelations.hasAggregate(select.getSelectItems())) {
+ return Collections.emptyList();
+ }
+ Set local = new HashSet<>();
+ addName(local, select.getFromItem());
+ if (select.getJoins() != null) {
+ for (Join join : select.getJoins()) {
+ addName(local, join.getFromItem());
+ }
+ }
+ List outer = new ArrayList<>();
+ for (Expression conjunct : conjuncts(select.getWhere())) {
+ List columns = new ArrayList<>();
+ if (!columns(conjunct, columns, new IdentityHashMap<>()) || columns.isEmpty()) {
+ continue;
+ }
+ boolean onlyOuter = true;
+ for (Column column : columns) {
+ Table table = column.getTable();
+ if (table == null
+ || table.getName() == null
+ || table.getSchemaName() != null
+ || local.contains(Identifiers.fold(table.getName()))) {
+ onlyOuter = false;
+ break;
+ }
+ }
+ if (onlyOuter) {
+ outer.add(conjunct);
+ }
+ }
+ if (outer.size() == conjuncts(select.getWhere()).size()) {
+ // nothing would stay inside: keep the statement as it is
+ return Collections.emptyList();
+ }
+ return outer;
+ }
+
+ /** The WHERE without {@code removed} (by identity); {@code null} if nothing remains. */
+ static Expression remaining(Expression where, List removed) {
+ List kept = new ArrayList<>();
+ for (Expression conjunct : conjuncts(where)) {
+ boolean drop = false;
+ for (Expression r : removed) {
+ drop |= r == conjunct;
+ }
+ if (!drop) {
+ kept.add(conjunct);
+ }
+ }
+ return kept.isEmpty() ? null : and(kept);
+ }
+
+ static Expression and(List