123123import java .util .ArrayList ;
124124import java .util .HashMap ;
125125import java .util .List ;
126+ import java .util .Locale ;
126127import java .util .Map ;
127128import java .util .Optional ;
128129import java .util .Set ;
@@ -690,11 +691,44 @@ private Plan bindHiveTableSink(MatchingContext<UnboundHiveTableSink<Plan>> ctx)
690691 + "(input format: " + inputFormat + "). LZO tables are read-only in Doris." );
691692 }
692693
694+ // Get static partition columns if present:
695+ // INSERT [OVERWRITE] TABLE t PARTITION(col='val', ...) SELECT ...
696+ Map <String , Expression > staticPartitions = sink .getStaticPartitionKeyValues ();
697+ Set <String > staticPartitionColNames = staticPartitions != null
698+ ? staticPartitions .keySet ()
699+ : Sets .newHashSet ();
700+ // Hive column/partition names are case-insensitive (stored lowercase in HMS), so
701+ // e.g. PARTITION(dt='21', DT='22') refers to the same column twice. Detect such
702+ // case-insensitive duplicates here and fail fast, instead of silently letting a
703+ // later value overwrite an earlier one in the case-insensitive columnToOutput map.
704+ // Use Locale.ROOT to keep the folding locale-independent (matching how the Hive
705+ // schema is loaded), otherwise e.g. the Turkish locale would fold 'I' to 'ı' and
706+ // fail to match a metadata column named 'id'.
707+ Set <String > lowerStaticPartitionColNames = Sets .newHashSet ();
708+ for (String name : staticPartitionColNames ) {
709+ if (!lowerStaticPartitionColNames .add (name .toLowerCase (Locale .ROOT ))) {
710+ throw new AnalysisException ("Duplicate partition column: " + name );
711+ }
712+ }
713+
714+ // Validate static partition columns against the hive table's partition columns
715+ if (sink .hasStaticPartition ()) {
716+ validateHiveStaticPartition (staticPartitions , table );
717+ }
718+
719+ // Build bindColumns: exclude static partition columns from the columns that
720+ // need to come from SELECT, because their values come from the PARTITION clause.
693721 List <Column > bindColumns ;
694722 if (sink .getColNames ().isEmpty ()) {
695- bindColumns = table .getBaseSchema (true ).stream ().collect (ImmutableList .toImmutableList ());
723+ bindColumns = table .getBaseSchema (true ).stream ()
724+ .filter (col -> !lowerStaticPartitionColNames .contains (col .getName ().toLowerCase (Locale .ROOT )))
725+ .collect (ImmutableList .toImmutableList ());
696726 } else {
697727 bindColumns = sink .getColNames ().stream ().map (cn -> {
728+ if (lowerStaticPartitionColNames .contains (cn .toLowerCase (Locale .ROOT ))) {
729+ throw new AnalysisException (String .format (
730+ "column %s is a static partition column, should not be in the insert column list" , cn ));
731+ }
698732 Column column = table .getColumn (cn );
699733 if (column == null ) {
700734 throw new AnalysisException (String .format ("column %s is not found in table %s" ,
@@ -715,15 +749,71 @@ private Plan bindHiveTableSink(MatchingContext<UnboundHiveTableSink<Plan>> ctx)
715749 Optional .empty (),
716750 child );
717751 // we need to insert all the columns of the target table
752+ // (except the static partition columns which are supplied by the PARTITION clause)
718753 if (boundSink .getCols ().size () != child .getOutput ().size ()) {
719754 throw new AnalysisException ("insert into cols should be corresponding to the query output" );
720755 }
721756 Map <String , NamedExpression > columnToOutput = getColumnToOutput (ctx , table , false , false ,
722757 boundSink , child );
758+
759+ // For static partition columns, add constant expressions from the PARTITION clause.
760+ // This ensures every written row carries the static partition value, so all rows
761+ // land in the specified partition (and INSERT OVERWRITE only overwrites that partition).
762+ if (!staticPartitionColNames .isEmpty ()) {
763+ for (Map .Entry <String , Expression > entry : staticPartitions .entrySet ()) {
764+ Expression valueExpr = entry .getValue ();
765+ Column column = table .getColumn (entry .getKey ());
766+ if (column != null ) {
767+ Expression castExpr = TypeCoercionUtils .castIfNotSameType (
768+ valueExpr , DataType .fromCatalogType (column .getType ()));
769+ // Use the canonical (lowercase) column name from the table schema as both the
770+ // map key and the alias name, so it aligns with getOutputProjectByCoercion which
771+ // looks up columnToOutput by table.getFullSchema() column names.
772+ columnToOutput .put (column .getName (), new Alias (castExpr , column .getName ()));
773+ }
774+ }
775+ }
776+
723777 LogicalProject <?> fullOutputProject = getOutputProjectByCoercion (table .getFullSchema (), child , columnToOutput );
724778 return boundSink .withChildAndUpdateOutput (fullOutputProject );
725779 }
726780
781+ /**
782+ * Validate static partition specification for Hive table.
783+ * The specified columns must be partition columns of the target hive table,
784+ * and each partition value must be a literal.
785+ */
786+ private void validateHiveStaticPartition (Map <String , Expression > staticPartitions , HMSExternalTable table ) {
787+ if (staticPartitions == null || staticPartitions .isEmpty ()) {
788+ return ;
789+ }
790+ Set <String > partitionColNames = table .getPartitionColumnNames ();
791+ if (partitionColNames .isEmpty ()) {
792+ throw new AnalysisException (
793+ String .format ("Table %s is not a partitioned table, cannot use static partition syntax" ,
794+ table .getName ()));
795+ }
796+ // build a case-insensitive view of partition column names
797+ Set <String > lowerPartitionColNames = Sets .newHashSet ();
798+ for (String name : partitionColNames ) {
799+ lowerPartitionColNames .add (name .toLowerCase ());
800+ }
801+ for (Map .Entry <String , Expression > entry : staticPartitions .entrySet ()) {
802+ String partitionColName = entry .getKey ();
803+ Expression partitionValue = entry .getValue ();
804+ if (!lowerPartitionColNames .contains (partitionColName .toLowerCase ())) {
805+ throw new AnalysisException (
806+ String .format ("Unknown partition column '%s' in table '%s'. Available partition columns: %s" ,
807+ partitionColName , table .getName (), partitionColNames ));
808+ }
809+ if (!(partitionValue instanceof Literal )) {
810+ throw new AnalysisException (
811+ String .format ("Partition value for column '%s' must be a literal, but got: %s" ,
812+ partitionColName , partitionValue ));
813+ }
814+ }
815+ }
816+
727817 private Plan bindIcebergTableSink (MatchingContext <UnboundIcebergTableSink <Plan >> ctx ) {
728818 UnboundIcebergTableSink <?> sink = ctx .root ;
729819 Pair <IcebergExternalDatabase , IcebergExternalTable > pair = bind (ctx .cascadesContext , sink );
0 commit comments