<@U0A71G31CDV> Any possibility to perform conditio...
# questions-and-troubleshooting
s
@Rocky Any possibility to perform conditional inserts through HTTP Stream Load API ?
r
Yes — two different kinds of "conditional insert" are supported over the Stream Load HTTP API: 1. Filter rows at load time (
where
header)
StarRocks filters out rows that don't match the condition and loads only the rest:
Copy code
bash
curl --location-trusted -u <user>:<password> \
    -H "Expect:100-continue" \
    -H "column_separator:," \
    -H "columns: col1, col2, col3" \
    -H "where: col1 = 20180601" \
    -T example4.csv -XPUT \
    http://<fe_host>:<fe_http_port>/api/test_db/table4/_stream_load
Note from the docs: if your CSV columns map positionally onto the table but you need to reference columns in the WHERE clause, you must define temporary column names via the
columns
header (as above). Also, rows filtered out by
where
do not count toward
max_filter_ratio
— that ratio only covers rows rejected for data-quality reasons. 2. Conditional update / upsert (
merge_condition
header)
For Primary Key tables (v2.5+), you can specify a non-primary-key column as the condition; a source record only overwrites the destination record if its value in that column is greater than or equal to the existing one:
Copy code
bash
curl --location-trusted -u <user>:<password> \
    -H "Expect:100-continue" \
    -H "label:label10" \
    -H "column_separator:," \
    -H "merge_condition:version" \
    -T example5.csv -XPUT \
    http://<fe_host>:<fe_http_port>/api/test_db/table5/_stream_load
Caveats the docs call out for `merge_condition`: • Only Primary Key tables support conditional updates; the specified column cannot be a primary key column. • DELETE operations don't support conditional updates. • Partial updates + conditional updates: not allowed together before v3.1.3; supported from v3.1.3 on. Column-mode partial update + conditional update fails in shared-nothing clusters (supported in shared-data clusters from v4.1.1). • Known issue: merge condition was ignored when Merge Commit was enabled; fixed in 4.1.0, 4.0.2, 3.5.9. So: filtering =
where
, upsert-if-newer =
merge_condition
. There's no arbitrary
INSERT ... SELECT ... WHERE
semantics via Stream Load beyond these two mechanisms — for row-level conditional logic on existing table data you'd need to check the source rows yourself before sending, or use SQL
INSERT ... SELECT
. References • sql-reference/sql-statements/loading_unloading/STREAM_LOAD.md • sql-reference/sql-statements/loading_unloading/routine_load/CREATE_ROUTINE_LOAD.md • loading/Etl_in_loading.md • faq/data_migration/loading/Stream_load_faq.md • sql-reference/sql-statements/loading_unloading/BROKER_LOAD.md • loading/Load_to_Primary_Key_tables.md
s
@Rocky I was indeed thinking about
INSERT … SELECT … WHERE
style operation. I’m thinking of stream loading rows into a staging table and have a periodic task perform
INSERT … SELECT … WHERE
off the staging table.
r
Yes, that pattern is fully supported — it's a normal ETL flow. Notes on making it work well: The statement itself
INSERT INTO target SELECT ... FROM staging WHERE <cond>
supports joins, CTEs, and subqueries; the target must be an internal StarRocks table, and source can be internal or external. Use
WITH LABEL
if you want a predictable label to poll status. Scheduling the periodic task
SUBMIT TASK
can do it server-side (scheduling supported from v3.3):
Copy code
sql
SUBMIT TASK stage_to_target
SCHEDULE START('2025-01-01 00:00:00') EVERY(INTERVAL 1 MINUTE)
PROPERTIES ("session.insert_timeout" = "3600")
AS
INSERT INTO target SELECT ... FROM staging WHERE processed = false;
Constraints from the docs: supported ETL statements are
CREATE TABLE AS SELECT
,
INSERT
, and `CACHE SELECT`; minimum interval is 10 seconds (
task_min_schedule_interval_s
); monitor via
INFORMATION_SCHEMA.task_runs
. An external scheduler (cron/Airflow) issuing the INSERT is equally fine. The real design question: idempotency / re-runs • If the target is a Duplicate Key table, re-running the same SELECT loads the rows again — you'll need a watermark column and a
WHERE
that excludes already-processed staging rows. • If the target is a Primary Key table, INSERT is treated as UPSERT, so re-running is naturally idempotent for the same key set. This is the usual choice for this pattern. • Alternatively, if each batch maps to a partition,
INSERT OVERWRITE ... PARTITION(...)
gives you batch-level idempotency (and Dynamic Overwrite from v3.4 handles auto-creating partitions). Cleaning up staging
DELETE ... WHERE
with full semantics (arbitrary columns, subqueries,
USING
joins from v3.0) only works on Primary Key tables. On Duplicate/Aggregate/Unique Key tables DELETE is limited to simple filters on key columns and partition-level deletes. So if you want to delete processed rows from staging after a successful insert, make staging a Primary Key table. Note the doc warning that you should not run DELETE concurrently with loading, or ACID isn't guaranteed — serialize the two. Caveats worth knowing •
strict_mode
/
max_filter_ratio
properties are only supported for
INSERT from FILES()
, not for
INSERT ... SELECT
from tables. To avoid failing a whole batch on one bad row, set
enable_insert_strict = false
(filters non-conforming rows) or clean in the SELECT. • The docs explicitly warn that frequent small INSERTs create excessive data versions and hurt query performance; they recommend against using INSERT as a high-frequency loading routine. Batch sensibly (minutes, not seconds, and reasonably sized). • I'd flag one thing as _my inference, not documented_: with a "watermark > last_run_time" filter, a row arriving in staging with an out-of-order timestamp after your watermark advanced would be skipped. If staging writes can be late-arriving, key the filter on something monotonic (auto-increment-ish id, or an ingest-time column) or rely on the Primary Key target's upsert rather than the WHERE clause. Worth considering before building it If the goal is still "apply row only if newer than what's in the table," loading directly into the Primary Key target with
merge_condition
avoids the staging table, the scheduler, and the staging cleanup entirely. The staging-table approach earns its keep when the transformation is real (joins, aggregation, dedup across rows/files) rather than a per-row predicate. References • loading/InsertInto.md • faq/resource_isolation_faq.md • sql-reference/sql-statements/loading_unloading/INSERT.md • knowledge/usage/best-practice.md (StarRocks knowledge base) • faq/data_migration/loading/Insert_into_faq.md
s
@Rocky So if the insert filter condition depends on looking up other rows in the table, stream load
where
and
merge_condition
cannot do that right
r
Correct. Neither can consult other rows of the table: •
where
filters the incoming rows only. The FAQ notes StarRocks "first transforms and then filters the source data," i.e. the condition is evaluated against the (transformed) columns of the record being loaded, plus functions/expression over those columns —
where: event_type = 1
,
where: col1 = 20180601
. No subqueries, no access to destination-table contents. It's a row-local predicate, and it's also applied identically in Broker Load and Routine Load. •
merge_condition
is a per-row comparison against exactly one thing: the currently stored row with the same primary key, on the single column you name, with
>=
semantics. From the docs: "Data will be updated only when the value of the data to be loaded into this column is greater than or equal to the current value of this column." So it can't ask "does this user exist in another table", "has this order already been shipped", "is there a row with attribute X" — only "is my
version
at least as new as my own row's `version`". So anything that depends on other rows — a different table, a different primary key, a join, an aggregation, a NOT EXISTS check — has to happen before the load or after it, not inside the load headers. Your staging-table plan is the right shape, and the lookup lives in the
INSERT … SELECT … WHERE
(joins, CTEs, subqueries,
NOT EXISTS
are all fine there). Two smaller points: • If the lookup is only "does a row with this key already exist in the destination table, and what's its current value" per matching primary key,
merge_condition
may be enough and you can skip staging entirely. • I don't see a
MERGE INTO
statement in the StarRocks docs — if what you really want is
WHEN MATCHED THEN UPDATE / WHEN NOT MATCHED THEN INSERT
semantics, I'm not certain StarRocks supports it in your version, so verify against the SQL reference for your exact release; otherwise express it as
INSERT … SELECT
into a Primary Key target plus a separate `DELETE`/`where` for the unmatched side. Also worth pre-empting: don't try to smuggle the lookup into the
columns
header as a function — that field only maps/transforms source columns, same limitation as
where
. References • sql-reference/sql-statements/loading_unloading/BROKER_LOAD.md • sql-reference/sql-statements/loading_unloading/routine_load/CREATE_ROUTINE_LOAD.md • sql-reference/sql-statements/loading_unloading/STREAM_LOAD.md • faq/data_migration/loading/Stream_load_faq.md • loading/Etl_in_loading.md • loading/Load_to_Primary_Key_tables.md